This commit is contained in:
Chris Fallin 2023-02-13 17:02:25 -08:00
parent b24ca8de43
commit ebfd4209fe
2 changed files with 10 additions and 21 deletions

View file

@ -358,18 +358,23 @@ fn handle_payload<'a>(
}
struct DebugLocReader<'a> {
code_section_offset: u32,
locs: &'a [(u32, u32, SourceLoc)],
}
impl<'a> DebugLocReader<'a> {
fn new(module: &'a Module, offset: usize) -> Self {
DebugLocReader {
locs: module.debug_map.locs_from_offset(offset),
code_section_offset: u32::try_from(offset).unwrap(),
locs: &module.debug_map.tuples[..],
}
}
fn get_loc(&mut self, offset: usize) -> SourceLoc {
let offset = u32::try_from(offset).unwrap();
let offset = u32::try_from(offset)
.unwrap()
.checked_sub(self.code_section_offset)
.unwrap();
while self.locs.len() > 0 {
let (start, len, loc) = self.locs[0];
if offset < start {

View file

@ -5,7 +5,6 @@ use crate::entity::EntityVec;
use addr2line::gimli;
use std::collections::hash_map::Entry as HashEntry;
use std::collections::HashMap;
use std::convert::TryFrom;
declare_entity!(SourceFile, "file");
declare_entity!(SourceLoc, "loc");
@ -49,7 +48,9 @@ impl Debug {
#[derive(Clone, Debug, Default)]
pub struct DebugMap {
tuples: Vec<(u32, u32, SourceLoc)>,
/// Each tuple is `(start, len, loc)`. The `start` offset is
/// relative to the Wasm code section (*not* the entire file).
pub tuples: Vec<(u32, u32, SourceLoc)>,
}
impl DebugMap {
@ -79,21 +80,4 @@ impl DebugMap {
Ok(DebugMap { tuples })
}
pub(crate) fn locs_from_offset<'a>(&'a self, offset: usize) -> &'a [(u32, u32, SourceLoc)] {
let offset = u32::try_from(offset).unwrap();
let start = match self.tuples.binary_search_by(|&(start, len, _)| {
if offset < start {
std::cmp::Ordering::Greater
} else if offset >= (start + len) {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Equal
}
}) {
Ok(idx) => idx,
Err(first_after) => first_after,
};
&self.tuples[start..]
}
}