Working debug-locs

This commit is contained in:
Chris Fallin 2023-02-14 20:38:52 -08:00
parent 4bbeb84832
commit c999f995be
2 changed files with 21 additions and 16 deletions

View file

@ -36,7 +36,7 @@ pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module<'_>> {
gimli::LocationLists::new(extra_sections.debug_loc, extra_sections.debug_loclists); gimli::LocationLists::new(extra_sections.debug_loc, extra_sections.debug_loclists);
dwarf.ranges = dwarf.ranges =
gimli::RangeLists::new(extra_sections.debug_ranges, extra_sections.debug_rnglists); gimli::RangeLists::new(extra_sections.debug_ranges, extra_sections.debug_rnglists);
let debug_map = DebugMap::from_dwarf(dwarf, &mut module.debug, extra_sections.code_offset)?; let debug_map = DebugMap::from_dwarf(dwarf, &mut module.debug)?;
module.debug_map = debug_map; module.debug_map = debug_map;
Ok(module) Ok(module)
@ -74,7 +74,6 @@ struct ExtraSections<'a> {
debug_loclists: gimli::DebugLocLists<gimli::EndianSlice<'a, gimli::LittleEndian>>, debug_loclists: gimli::DebugLocLists<gimli::EndianSlice<'a, gimli::LittleEndian>>,
debug_ranges: gimli::DebugRanges<gimli::EndianSlice<'a, gimli::LittleEndian>>, debug_ranges: gimli::DebugRanges<gimli::EndianSlice<'a, gimli::LittleEndian>>,
debug_rnglists: gimli::DebugRngLists<gimli::EndianSlice<'a, gimli::LittleEndian>>, debug_rnglists: gimli::DebugRngLists<gimli::EndianSlice<'a, gimli::LittleEndian>>,
code_offset: u32,
} }
fn handle_payload<'a>( fn handle_payload<'a>(
@ -172,7 +171,6 @@ fn handle_payload<'a>(
} }
} }
Payload::CodeSectionEntry(body) => { Payload::CodeSectionEntry(body) => {
extra_sections.code_offset = body.range().start as u32;
let func_idx = Func::new(*next_func); let func_idx = Func::new(*next_func);
*next_func += 1; *next_func += 1;
@ -360,20 +358,33 @@ fn handle_payload<'a>(
} }
struct DebugLocReader<'a> { struct DebugLocReader<'a> {
code_offset: u32,
locs: &'a [(u32, u32, SourceLoc)], locs: &'a [(u32, u32, SourceLoc)],
} }
impl<'a> DebugLocReader<'a> { impl<'a> DebugLocReader<'a> {
fn new(module: &'a Module) -> Self { fn new(module: &'a Module, func_offset_in_file: u32) -> Self {
let start = match module
.debug_map
.tuples
.binary_search_by(|&(start, len, _)| {
use std::cmp::Ordering::*;
if start > func_offset_in_file {
Greater
} else if (start + len) <= func_offset_in_file {
Less
} else {
Equal
}
}) {
Ok(idx) => idx,
Err(idx) => idx,
};
DebugLocReader { DebugLocReader {
code_offset: module.debug_map.code_offset, locs: &module.debug_map.tuples[start..],
locs: &module.debug_map.tuples[..],
} }
} }
fn get_loc(&mut self, offset: usize) -> SourceLoc { fn get_loc(&mut self, offset: usize) -> SourceLoc {
// TODO: add `code_offset`?
let offset = u32::try_from(offset).unwrap(); let offset = u32::try_from(offset).unwrap();
while self.locs.len() > 0 { while self.locs.len() > 0 {
let (start, len, loc) = self.locs[0]; let (start, len, loc) = self.locs[0];
@ -396,7 +407,7 @@ pub(crate) fn parse_body<'a>(
) -> Result<FunctionBody> { ) -> Result<FunctionBody> {
let mut ret: FunctionBody = FunctionBody::default(); let mut ret: FunctionBody = FunctionBody::default();
let mut debug_locs = DebugLocReader::new(module); let mut debug_locs = DebugLocReader::new(module, body.range().start as u32);
for &param in &module.signatures[my_sig].params[..] { for &param in &module.signatures[my_sig].params[..] {
ret.locals.push(param.into()); ret.locals.push(param.into());

View file

@ -48,8 +48,6 @@ impl Debug {
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct DebugMap { pub struct DebugMap {
/// Offset of code section relative to the Wasm file start.
pub code_offset: u32,
/// Each tuple is `(start, len, loc)`. The `start` offset is /// Each tuple is `(start, len, loc)`. The `start` offset is
/// relative to the code section. /// relative to the code section.
pub tuples: Vec<(u32, u32, SourceLoc)>, pub tuples: Vec<(u32, u32, SourceLoc)>,
@ -59,7 +57,6 @@ impl DebugMap {
pub(crate) fn from_dwarf<R: gimli::Reader>( pub(crate) fn from_dwarf<R: gimli::Reader>(
dwarf: gimli::Dwarf<R>, dwarf: gimli::Dwarf<R>,
debug: &mut Debug, debug: &mut Debug,
code_offset: u32,
) -> anyhow::Result<DebugMap> { ) -> anyhow::Result<DebugMap> {
let ctx = addr2line::Context::from_dwarf(dwarf)?; let ctx = addr2line::Context::from_dwarf(dwarf)?;
let mut tuples = vec![]; let mut tuples = vec![];
@ -82,9 +79,6 @@ impl DebugMap {
retain retain
}); });
Ok(DebugMap { Ok(DebugMap { tuples })
code_offset,
tuples,
})
} }
} }