From dc0db358abe19481e475e10c32149b53370f1a1c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 05:57:31 +0200 Subject: Merging upstream version 1.72.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/addr2line/src/builtin_split_dwarf_loader.rs | 164 +++++ vendor/addr2line/src/function.rs | 133 ++-- vendor/addr2line/src/lazy.rs | 4 + vendor/addr2line/src/lib.rs | 755 +++++++++++++++++---- 4 files changed, 870 insertions(+), 186 deletions(-) create mode 100644 vendor/addr2line/src/builtin_split_dwarf_loader.rs (limited to 'vendor/addr2line/src') diff --git a/vendor/addr2line/src/builtin_split_dwarf_loader.rs b/vendor/addr2line/src/builtin_split_dwarf_loader.rs new file mode 100644 index 000000000..471193124 --- /dev/null +++ b/vendor/addr2line/src/builtin_split_dwarf_loader.rs @@ -0,0 +1,164 @@ +use alloc::borrow::Cow; +use alloc::sync::Arc; +use std::fs::File; +use std::path::PathBuf; + +use object::Object; + +use crate::{LookupContinuation, LookupResult}; + +#[cfg(unix)] +fn convert_path>( + r: &R, +) -> Result { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + let bytes = r.to_slice()?; + let s = OsStr::from_bytes(&bytes); + Ok(PathBuf::from(s)) +} + +#[cfg(not(unix))] +fn convert_path>( + r: &R, +) -> Result { + let bytes = r.to_slice()?; + let s = std::str::from_utf8(&bytes).map_err(|_| gimli::Error::BadUtf8)?; + Ok(PathBuf::from(s)) +} + +fn load_section<'data: 'file, 'file, O, R, F>( + id: gimli::SectionId, + file: &'file O, + endian: R::Endian, + loader: &mut F, +) -> Result +where + O: object::Object<'data, 'file>, + R: gimli::Reader, + F: FnMut(Cow<'data, [u8]>, R::Endian) -> R, +{ + use object::ObjectSection; + + let data = id + .dwo_name() + .and_then(|dwo_name| { + file.section_by_name(dwo_name) + .and_then(|section| section.uncompressed_data().ok()) + }) + .unwrap_or(Cow::Borrowed(&[])); + Ok(loader(data, endian)) +} + +/// A simple builtin split DWARF loader. +pub struct SplitDwarfLoader +where + R: gimli::Reader, + F: FnMut(Cow<'_, [u8]>, R::Endian) -> R, +{ + loader: F, + dwarf_package: Option>, +} + +impl SplitDwarfLoader +where + R: gimli::Reader, + F: FnMut(Cow<'_, [u8]>, R::Endian) -> R, +{ + fn load_dwarf_package(loader: &mut F, path: Option) -> Option> { + let mut path = path.map(Ok).unwrap_or_else(std::env::current_exe).ok()?; + let dwp_extension = path + .extension() + .map(|previous_extension| { + let mut previous_extension = previous_extension.to_os_string(); + previous_extension.push(".dwp"); + previous_extension + }) + .unwrap_or_else(|| "dwp".into()); + path.set_extension(dwp_extension); + let file = File::open(&path).ok()?; + let map = unsafe { memmap2::Mmap::map(&file).ok()? }; + let dwp = object::File::parse(&*map).ok()?; + + let endian = if dwp.is_little_endian() { + gimli::RunTimeEndian::Little + } else { + gimli::RunTimeEndian::Big + }; + + let empty = loader(Cow::Borrowed(&[]), endian); + gimli::DwarfPackage::load( + |section_id| load_section(section_id, &dwp, endian, loader), + empty, + ) + .ok() + } + + /// Create a new split DWARF loader. + pub fn new(mut loader: F, path: Option) -> SplitDwarfLoader { + let dwarf_package = SplitDwarfLoader::load_dwarf_package(&mut loader, path); + SplitDwarfLoader { + loader, + dwarf_package, + } + } + + /// Run the provided `LookupResult` to completion, loading any necessary + /// split DWARF along the way. + pub fn run(&mut self, mut l: LookupResult) -> L::Output + where + L: LookupContinuation, + { + loop { + let (load, continuation) = match l { + LookupResult::Output(output) => break output, + LookupResult::Load { load, continuation } => (load, continuation), + }; + + let mut r: Option>> = None; + if let Some(dwp) = self.dwarf_package.as_ref() { + if let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) { + r = Some(Arc::new(cu)); + } + } + + if r.is_none() { + let mut path = PathBuf::new(); + if let Some(p) = load.comp_dir.as_ref() { + if let Ok(p) = convert_path(p) { + path.push(p); + } + } + + if let Some(p) = load.path.as_ref() { + if let Ok(p) = convert_path(p) { + path.push(p); + } + } + + if let Ok(file) = File::open(&path) { + if let Ok(map) = unsafe { memmap2::Mmap::map(&file) } { + if let Ok(file) = object::File::parse(&*map) { + let endian = if file.is_little_endian() { + gimli::RunTimeEndian::Little + } else { + gimli::RunTimeEndian::Big + }; + + r = gimli::Dwarf::load(|id| { + load_section(id, &file, endian, &mut self.loader) + }) + .ok() + .map(|mut dwo_dwarf| { + dwo_dwarf.make_dwo(&load.parent); + Arc::new(dwo_dwarf) + }); + } + } + } + } + + l = continuation.resume(r); + } + } +} diff --git a/vendor/addr2line/src/function.rs b/vendor/addr2line/src/function.rs index 44dc73f24..09c19e01e 100644 --- a/vendor/addr2line/src/function.rs +++ b/vendor/addr2line/src/function.rs @@ -5,7 +5,7 @@ use core::iter; use crate::lazy::LazyCell; use crate::maybe_small; -use crate::{Error, RangeAttributes, ResDwarf}; +use crate::{Context, DebugFile, Error, RangeAttributes}; pub(crate) struct Functions { /// List of all `DW_TAG_subprogram` details in the unit. @@ -49,13 +49,16 @@ pub(crate) struct InlinedFunctionAddress { pub(crate) struct InlinedFunction { pub(crate) dw_die_offset: gimli::UnitOffset, pub(crate) name: Option, - pub(crate) call_file: u64, + pub(crate) call_file: Option, pub(crate) call_line: u32, pub(crate) call_column: u32, } impl Functions { - pub(crate) fn parse(unit: &gimli::Unit, dwarf: &ResDwarf) -> Result, Error> { + pub(crate) fn parse( + unit: &gimli::Unit, + sections: &gimli::Dwarf, + ) -> Result, Error> { let mut functions = Vec::new(); let mut addresses = Vec::new(); let mut entries = unit.entries_raw(None)?; @@ -73,8 +76,7 @@ impl Functions { ranges.low_pc = Some(val) } gimli::AttributeValue::DebugAddrIndex(index) => { - ranges.low_pc = - Some(dwarf.sections.address(unit, index)?); + ranges.low_pc = Some(sections.address(unit, index)?); } _ => {} }, @@ -83,8 +85,7 @@ impl Functions { ranges.high_pc = Some(val) } gimli::AttributeValue::DebugAddrIndex(index) => { - ranges.high_pc = - Some(dwarf.sections.address(unit, index)?); + ranges.high_pc = Some(sections.address(unit, index)?); } gimli::AttributeValue::Udata(val) => { ranges.size = Some(val) @@ -92,9 +93,8 @@ impl Functions { _ => {} }, gimli::DW_AT_ranges => { - ranges.ranges_offset = dwarf - .sections - .attr_ranges_offset(unit, attr.value())?; + ranges.ranges_offset = + sections.attr_ranges_offset(unit, attr.value())?; } _ => {} }; @@ -104,7 +104,7 @@ impl Functions { } let function_index = functions.len(); - if ranges.for_each_range(&dwarf.sections, unit, |range| { + if ranges.for_each_range(sections, unit, |range| { addresses.push(FunctionAddress { range, function: function_index, @@ -150,13 +150,15 @@ impl Functions { pub(crate) fn parse_inlined_functions( &self, + file: DebugFile, unit: &gimli::Unit, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, ) -> Result<(), Error> { for function in &*self.functions { function .1 - .borrow_with(|| Function::parse(function.0, unit, dwarf)) + .borrow_with(|| Function::parse(function.0, file, unit, ctx, sections)) .as_ref() .map_err(Error::clone)?; } @@ -167,8 +169,10 @@ impl Functions { impl Function { pub(crate) fn parse( dw_die_offset: gimli::UnitOffset, + file: DebugFile, unit: &gimli::Unit, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, ) -> Result { let mut entries = unit.entries_raw(Some(dw_die_offset))?; let depth = entries.next_depth(); @@ -181,18 +185,18 @@ impl Function { Ok(ref attr) => { match attr.name() { gimli::DW_AT_linkage_name | gimli::DW_AT_MIPS_linkage_name => { - if let Ok(val) = dwarf.sections.attr_string(unit, attr.value()) { + if let Ok(val) = sections.attr_string(unit, attr.value()) { name = Some(val); } } gimli::DW_AT_name => { if name.is_none() { - name = dwarf.sections.attr_string(unit, attr.value()).ok(); + name = sections.attr_string(unit, attr.value()).ok(); } } gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => { if name.is_none() { - name = name_attr(attr.value(), unit, dwarf, 16)?; + name = name_attr(attr.value(), file, unit, ctx, sections, 16)?; } } _ => {} @@ -207,8 +211,10 @@ impl Function { Function::parse_children( &mut entries, depth, + file, unit, - dwarf, + ctx, + sections, &mut inlined_functions, &mut inlined_addresses, 0, @@ -247,10 +253,12 @@ impl Function { } fn parse_children( - entries: &mut gimli::EntriesRaw, + entries: &mut gimli::EntriesRaw<'_, '_, R>, depth: isize, + file: DebugFile, unit: &gimli::Unit, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, inlined_functions: &mut Vec>, inlined_addresses: &mut Vec, inlined_depth: usize, @@ -272,8 +280,10 @@ impl Function { entries, abbrev, next_depth, + file, unit, - dwarf, + ctx, + sections, inlined_functions, inlined_addresses, inlined_depth, @@ -288,7 +298,7 @@ impl Function { } fn skip( - entries: &mut gimli::EntriesRaw, + entries: &mut gimli::EntriesRaw<'_, '_, R>, abbrev: &gimli::Abbreviation, depth: isize, ) -> Result<(), Error> { @@ -344,18 +354,20 @@ impl Function { impl InlinedFunction { fn parse( dw_die_offset: gimli::UnitOffset, - entries: &mut gimli::EntriesRaw, + entries: &mut gimli::EntriesRaw<'_, '_, R>, abbrev: &gimli::Abbreviation, depth: isize, + file: DebugFile, unit: &gimli::Unit, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, inlined_functions: &mut Vec>, inlined_addresses: &mut Vec, inlined_depth: usize, ) -> Result<(), Error> { let mut ranges = RangeAttributes::default(); let mut name = None; - let mut call_file = 0; + let mut call_file = None; let mut call_line = 0; let mut call_column = 0; for spec in abbrev.attributes() { @@ -364,40 +376,50 @@ impl InlinedFunction { gimli::DW_AT_low_pc => match attr.value() { gimli::AttributeValue::Addr(val) => ranges.low_pc = Some(val), gimli::AttributeValue::DebugAddrIndex(index) => { - ranges.low_pc = Some(dwarf.sections.address(unit, index)?); + ranges.low_pc = Some(sections.address(unit, index)?); } _ => {} }, gimli::DW_AT_high_pc => match attr.value() { gimli::AttributeValue::Addr(val) => ranges.high_pc = Some(val), gimli::AttributeValue::DebugAddrIndex(index) => { - ranges.high_pc = Some(dwarf.sections.address(unit, index)?); + ranges.high_pc = Some(sections.address(unit, index)?); } gimli::AttributeValue::Udata(val) => ranges.size = Some(val), _ => {} }, gimli::DW_AT_ranges => { - ranges.ranges_offset = - dwarf.sections.attr_ranges_offset(unit, attr.value())?; + ranges.ranges_offset = sections.attr_ranges_offset(unit, attr.value())?; } gimli::DW_AT_linkage_name | gimli::DW_AT_MIPS_linkage_name => { - if let Ok(val) = dwarf.sections.attr_string(unit, attr.value()) { + if let Ok(val) = sections.attr_string(unit, attr.value()) { name = Some(val); } } gimli::DW_AT_name => { if name.is_none() { - name = dwarf.sections.attr_string(unit, attr.value()).ok(); + name = sections.attr_string(unit, attr.value()).ok(); } } gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => { if name.is_none() { - name = name_attr(attr.value(), unit, dwarf, 16)?; + name = name_attr(attr.value(), file, unit, ctx, sections, 16)?; } } gimli::DW_AT_call_file => { + // There is a spec issue [1] with how DW_AT_call_file is specified in DWARF 5. + // Before, a file index of 0 would indicate no source file, however in + // DWARF 5 this could be a valid index into the file table. + // + // Implementations such as LLVM generates a file index of 0 when DWARF 5 is + // used. + // + // Thus, if we see a version of 5 or later, treat a file index of 0 as such. + // [1]: http://wiki.dwarfstd.org/index.php?title=DWARF5_Line_Table_File_Numbers if let gimli::AttributeValue::FileIndex(fi) = attr.value() { - call_file = fi; + if fi > 0 || unit.header.version() >= 5 { + call_file = Some(fi); + } } } gimli::DW_AT_call_line => { @@ -421,7 +443,7 @@ impl InlinedFunction { call_column, }); - ranges.for_each_range(&dwarf.sections, unit, |range| { + ranges.for_each_range(sections, unit, |range| { inlined_addresses.push(InlinedFunctionAddress { range, call_depth: inlined_depth, @@ -432,8 +454,10 @@ impl InlinedFunction { Function::parse_children( entries, depth, + file, unit, - dwarf, + ctx, + sections, inlined_functions, inlined_addresses, inlined_depth + 1, @@ -443,8 +467,10 @@ impl InlinedFunction { fn name_attr( attr: gimli::AttributeValue, + mut file: DebugFile, unit: &gimli::Unit, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, recursion_limit: usize, ) -> Result, Error> where @@ -455,25 +481,18 @@ where } match attr { - gimli::AttributeValue::UnitRef(offset) => name_entry(unit, offset, dwarf, recursion_limit), + gimli::AttributeValue::UnitRef(offset) => { + name_entry(file, unit, offset, ctx, sections, recursion_limit) + } gimli::AttributeValue::DebugInfoRef(dr) => { - let res_unit = dwarf.find_unit(dr)?; - name_entry( - &res_unit.dw_unit, - gimli::UnitOffset(dr.0 - res_unit.offset.0), - dwarf, - recursion_limit, - ) + let (unit, offset) = ctx.find_unit(dr, file)?; + name_entry(file, unit, offset, ctx, sections, recursion_limit) } gimli::AttributeValue::DebugInfoRefSup(dr) => { - if let Some(sup_dwarf) = dwarf.sup.as_ref() { - let res_unit = sup_dwarf.find_unit(dr)?; - name_entry( - &res_unit.dw_unit, - gimli::UnitOffset(dr.0 - res_unit.offset.0), - sup_dwarf, - recursion_limit, - ) + if let Some(sup_sections) = sections.sup.as_ref() { + file = DebugFile::Supplementary; + let (unit, offset) = ctx.find_unit(dr, file)?; + name_entry(file, unit, offset, ctx, sup_sections, recursion_limit) } else { Ok(None) } @@ -483,9 +502,11 @@ where } fn name_entry( + file: DebugFile, unit: &gimli::Unit, offset: gimli::UnitOffset, - dwarf: &ResDwarf, + ctx: &Context, + sections: &gimli::Dwarf, recursion_limit: usize, ) -> Result, Error> where @@ -504,12 +525,12 @@ where match entries.read_attribute(*spec) { Ok(ref attr) => match attr.name() { gimli::DW_AT_linkage_name | gimli::DW_AT_MIPS_linkage_name => { - if let Ok(val) = dwarf.sections.attr_string(unit, attr.value()) { + if let Ok(val) = sections.attr_string(unit, attr.value()) { return Ok(Some(val)); } } gimli::DW_AT_name => { - if let Ok(val) = dwarf.sections.attr_string(unit, attr.value()) { + if let Ok(val) = sections.attr_string(unit, attr.value()) { name = Some(val); } } @@ -527,7 +548,7 @@ where } if let Some(next) = next { - return name_attr(next, unit, dwarf, recursion_limit - 1); + return name_attr(next, file, unit, ctx, sections, recursion_limit - 1); } Ok(None) diff --git a/vendor/addr2line/src/lazy.rs b/vendor/addr2line/src/lazy.rs index a34ed176a..2df2ed6ca 100644 --- a/vendor/addr2line/src/lazy.rs +++ b/vendor/addr2line/src/lazy.rs @@ -10,6 +10,10 @@ impl LazyCell { } } + pub fn borrow(&self) -> Option<&T> { + unsafe { &*self.contents.get() }.as_ref() + } + pub fn borrow_with(&self, closure: impl FnOnce() -> T) -> &T { // First check if we're already initialized... let ptr = self.contents.get(); diff --git a/vendor/addr2line/src/lib.rs b/vendor/addr2line/src/lib.rs index 3afa37f8f..3270aad7e 100644 --- a/vendor/addr2line/src/lib.rs +++ b/vendor/addr2line/src/lib.rs @@ -25,19 +25,18 @@ #![deny(missing_docs)] #![no_std] +#[cfg(feature = "std")] +extern crate std; + #[allow(unused_imports)] #[macro_use] extern crate alloc; -#[cfg(feature = "cpp_demangle")] -extern crate cpp_demangle; #[cfg(feature = "fallible-iterator")] pub extern crate fallible_iterator; pub extern crate gimli; #[cfg(feature = "object")] pub extern crate object; -#[cfg(feature = "rustc-demangle")] -extern crate rustc_demangle; use alloc::borrow::Cow; use alloc::boxed::Box; @@ -49,8 +48,10 @@ use alloc::vec::Vec; use core::cmp::{self, Ordering}; use core::iter; +use core::marker::PhantomData; use core::mem; use core::num::NonZeroU64; +use core::ops::ControlFlow; use core::u64; use crate::function::{Function, Functions, InlinedFunction}; @@ -67,17 +68,124 @@ mod maybe_small { pub type IntoIter = alloc::vec::IntoIter; } +#[cfg(all(feature = "std", feature = "object", feature = "memmap2"))] +/// A simple builtin split DWARF loader. +pub mod builtin_split_dwarf_loader; mod function; mod lazy; type Error = gimli::Error; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum DebugFile { + Primary, + Supplementary, + Dwo, +} + +/// Operations that consult debug information may require additional files +/// to be loaded if split DWARF is being used. This enum returns the result +/// of the operation in the `Break` variant, or information about the split +/// DWARF that is required and a continuation to invoke once it is available +/// in the `Continue` variant. +/// +/// This enum is intended to be used in a loop like so: +/// ```no_run +/// # use addr2line::*; +/// # use std::sync::Arc; +/// # let ctx: Context> = todo!(); +/// # let do_split_dwarf_load = |load: SplitDwarfLoad>| -> Option>>> { None }; +/// const ADDRESS: u64 = 0xdeadbeef; +/// let mut r = ctx.find_frames(ADDRESS); +/// let result = loop { +/// match r { +/// LookupResult::Output(result) => break result, +/// LookupResult::Load { load, continuation } => { +/// let dwo = do_split_dwarf_load(load); +/// r = continuation.resume(dwo); +/// } +/// } +/// }; +/// ``` +pub enum LookupResult { + /// The lookup requires split DWARF data to be loaded. + Load { + /// The information needed to find the split DWARF data. + load: SplitDwarfLoad<::Buf>, + /// The continuation to resume with the loaded split DWARF data. + continuation: L, + }, + /// The lookup has completed and produced an output. + Output(::Output), +} + +/// This trait represents a partially complete operation that can be resumed +/// once a load of needed split DWARF data is completed or abandoned by the +/// API consumer. +pub trait LookupContinuation: Sized { + /// The final output of this operation. + type Output; + /// The type of reader used. + type Buf: gimli::Reader; + + /// Resumes the operation with the provided data. + /// + /// After the caller loads the split DWARF data required, call this + /// method to resume the operation. The return value of this method + /// indicates if the computation has completed or if further data is + /// required. + /// + /// If the additional data cannot be located, or the caller does not + /// support split DWARF, `resume(None)` can be used to continue the + /// operation with the data that is available. + fn resume(self, input: Option>>) -> LookupResult; +} + +impl LookupResult { + /// Callers that do not handle split DWARF can call `skip_all_loads` + /// to fast-forward to the end result. This result is produced with + /// the data that is available and may be less accurate than the + /// the results that would be produced if the caller did properly + /// support split DWARF. + pub fn skip_all_loads(mut self) -> L::Output { + loop { + self = match self { + LookupResult::Output(t) => return t, + LookupResult::Load { continuation, .. } => continuation.resume(None), + }; + } + } + + fn map T>(self, f: F) -> LookupResult> { + match self { + LookupResult::Output(t) => LookupResult::Output(f(t)), + LookupResult::Load { load, continuation } => LookupResult::Load { + load, + continuation: MappedLookup { + original: continuation, + mutator: f, + }, + }, + } + } + + fn unwrap(self) -> L::Output { + match self { + LookupResult::Output(t) => t, + LookupResult::Load { .. } => unreachable!("Internal API misuse"), + } + } +} + /// The state necessary to perform address to line translation. /// /// Constructing a `Context` is somewhat costly, so users should aim to reuse `Context`s /// when performing lookups for many addresses in the same executable. pub struct Context { - dwarf: ResDwarf, + sections: Arc>, + unit_ranges: Box<[UnitRange]>, + units: Box<[ResUnit]>, + sup_units: Box<[SupUnit]>, } /// The type of `Context` that supports the `new` method. @@ -109,7 +217,7 @@ impl Context> { /// This means it is not thread safe, has no lifetime constraints (since it copies /// the input data), and works for any endianity. /// - /// Performance sensitive applications may want to use `Context::from_dwarf_with_sup` + /// Performance sensitive applications may want to use `Context::from_dwarf` /// with a more specialised `gimli::Reader` implementation. pub fn new_with_sup<'data: 'file, 'file, O: object::Object<'data, 'file>>( file: &'file O, @@ -176,7 +284,7 @@ impl Context { debug_types: default_section.clone().into(), locations: gimli::LocationLists::new( default_section.clone().into(), - default_section.clone().into(), + default_section.into(), ), ranges: gimli::RangeLists::new(debug_ranges, debug_rnglists), file_type: gimli::DwarfFileType::Main, @@ -187,18 +295,20 @@ impl Context { /// Construct a new `Context` from an existing [`gimli::Dwarf`] object. #[inline] - pub fn from_dwarf(sections: gimli::Dwarf) -> Result { - let mut dwarf = ResDwarf::parse(Arc::new(sections))?; - dwarf.sup = match dwarf.sections.sup.clone() { - Some(sup_sections) => Some(Box::new(ResDwarf::parse(sup_sections)?)), - None => None, + pub fn from_dwarf(sections: gimli::Dwarf) -> Result, Error> { + let sections = Arc::new(sections); + let (unit_ranges, units) = Context::parse_units(§ions)?; + let sup_units = if let Some(sup) = sections.sup.as_ref() { + Context::parse_sup(sup)? + } else { + Vec::new() }; - Ok(Context { dwarf }) - } - - /// The dwarf sections associated with this `Context`. - pub fn dwarf(&self) -> &gimli::Dwarf { - &self.dwarf.sections + Ok(Context { + sections, + unit_ranges: unit_ranges.into_boxed_slice(), + units: units.into_boxed_slice(), + sup_units: sup_units.into_boxed_slice(), + }) } /// Finds the CUs for the function address given. @@ -229,7 +339,6 @@ impl Context { // First up find the position in the array which could have our function // address. let pos = match self - .dwarf .unit_ranges .binary_search_by_key(&probe_high, |i| i.range.begin) { @@ -244,7 +353,7 @@ impl Context { // Once we have our index we iterate backwards from that position // looking for a matching CU. - self.dwarf.unit_ranges[..pos] + self.unit_ranges[..pos] .iter() .rev() .take_while(move |i| { @@ -265,25 +374,51 @@ impl Context { if probe_low >= i.range.end || probe_high <= i.range.begin { return None; } - Some((&self.dwarf.units[i.unit_id], &i.range)) + Some((&self.units[i.unit_id], &i.range)) }) } /// Find the DWARF unit corresponding to the given virtual memory address. - pub fn find_dwarf_unit(&self, probe: u64) -> Option<&gimli::Unit> { - for unit in self.find_units(probe) { - match unit.find_function_or_location(probe, &self.dwarf) { - Ok((Some(_), _)) | Ok((_, Some(_))) => return Some(&unit.dw_unit), - _ => {} - } + pub fn find_dwarf_and_unit( + &self, + probe: u64, + ) -> LookupResult< + impl LookupContinuation, &gimli::Unit)>, Buf = R>, + > { + let mut units_iter = self.find_units(probe); + if let Some(unit) = units_iter.next() { + return LoopingLookup::new_lookup( + unit.find_function_or_location(probe, self), + move |r| { + ControlFlow::Break(match r { + Ok((Some(_), _)) | Ok((_, Some(_))) => { + let (_file, sections, unit) = unit + .dwarf_and_unit_dwo(self) + // We've already been through both error cases here to get to this point. + .unwrap() + .unwrap(); + Some((sections, unit)) + } + _ => match units_iter.next() { + Some(next_unit) => { + return ControlFlow::Continue( + next_unit.find_function_or_location(probe, self), + ); + } + None => None, + }, + }) + }, + ); } - None + + LoopingLookup::new_complete(None) } /// Find the source file and line corresponding to the given virtual memory address. pub fn find_location(&self, probe: u64) -> Result>, Error> { for unit in self.find_units(probe) { - if let Some(location) = unit.find_location(probe, &self.dwarf.sections)? { + if let Some(location) = unit.find_location(probe, &self.sections)? { return Ok(Some(location)); } } @@ -309,33 +444,95 @@ impl Context { /// If the probe address is for an inline function then the first frame corresponds /// to the innermost inline function. Subsequent frames contain the caller and call /// location, until an non-inline caller is reached. - pub fn find_frames(&self, probe: u64) -> Result, Error> { - for unit in self.find_units(probe) { - match unit.find_function_or_location(probe, &self.dwarf)? { - (Some(function), location) => { - let inlined_functions = function.find_inlined_functions(probe); - return Ok(FrameIter(FrameIterState::Frames(FrameIterFrames { - unit, - sections: &self.dwarf.sections, - function, - inlined_functions, - next: location, - }))); - } - (None, Some(location)) => { - return Ok(FrameIter(FrameIterState::Location(Some(location)))); - } - _ => {} - } + pub fn find_frames( + &self, + probe: u64, + ) -> LookupResult, Error>, Buf = R>> + { + let mut units_iter = self.find_units(probe); + if let Some(unit) = units_iter.next() { + LoopingLookup::new_lookup(unit.find_function_or_location(probe, self), move |r| { + ControlFlow::Break(match r { + Err(e) => Err(e), + Ok((Some(function), location)) => { + let inlined_functions = function.find_inlined_functions(probe); + Ok(FrameIter(FrameIterState::Frames(FrameIterFrames { + unit, + sections: &self.sections, + function, + inlined_functions, + next: location, + }))) + } + Ok((None, Some(location))) => { + Ok(FrameIter(FrameIterState::Location(Some(location)))) + } + Ok((None, None)) => match units_iter.next() { + Some(next_unit) => { + return ControlFlow::Continue( + next_unit.find_function_or_location(probe, self), + ); + } + None => Ok(FrameIter(FrameIterState::Empty)), + }, + }) + }) + } else { + LoopingLookup::new_complete(Ok(FrameIter(FrameIterState::Empty))) } - Ok(FrameIter(FrameIterState::Empty)) + } + + /// Preload units for `probe`. + /// + /// The iterator returns pairs of `SplitDwarfLoad`s containing the + /// information needed to locate and load split DWARF for `probe` and + /// a matching callback to invoke once that data is available. + /// + /// If this method is called, and all of the returned closures are invoked, + /// addr2line guarantees that any future API call for the address `probe` + /// will not require the loading of any split DWARF. + /// + /// ```no_run + /// # use addr2line::*; + /// # use std::sync::Arc; + /// # let ctx: Context> = todo!(); + /// # let do_split_dwarf_load = |load: SplitDwarfLoad>| -> Option>>> { None }; + /// const ADDRESS: u64 = 0xdeadbeef; + /// ctx.preload_units(ADDRESS).for_each(|(load, callback)| { + /// let dwo = do_split_dwarf_load(load); + /// callback(dwo); + /// }); + /// + /// let frames_iter = match ctx.find_frames(ADDRESS) { + /// LookupResult::Output(result) => result, + /// LookupResult::Load { .. } => unreachable!("addr2line promised we wouldn't get here"), + /// }; + /// + /// // ... + /// ``` + pub fn preload_units( + &'_ self, + probe: u64, + ) -> impl Iterator< + Item = ( + SplitDwarfLoad, + impl FnOnce(Option>>) -> Result<(), gimli::Error> + '_, + ), + > { + self.find_units(probe) + .filter_map(move |unit| match unit.dwarf_and_unit_dwo(self) { + LookupResult::Output(_) => None, + LookupResult::Load { load, continuation } => Some((load, |result| { + continuation.resume(result).unwrap().map(|_| ()) + })), + }) } /// Initialize all line data structures. This is used for benchmarks. #[doc(hidden)] pub fn parse_lines(&self) -> Result<(), Error> { - for unit in &self.dwarf.units { - unit.parse_lines(&self.dwarf.sections)?; + for unit in self.units.iter() { + unit.parse_lines(&self.sections)?; } Ok(()) } @@ -343,8 +540,8 @@ impl Context { /// Initialize all function data structures. This is used for benchmarks. #[doc(hidden)] pub fn parse_functions(&self) -> Result<(), Error> { - for unit in &self.dwarf.units { - unit.parse_functions(&self.dwarf)?; + for unit in self.units.iter() { + unit.parse_functions(self).skip_all_loads()?; } Ok(()) } @@ -352,8 +549,8 @@ impl Context { /// Initialize all inlined function data structures. This is used for benchmarks. #[doc(hidden)] pub fn parse_inlined_functions(&self) -> Result<(), Error> { - for unit in &self.dwarf.units { - unit.parse_inlined_functions(&self.dwarf)?; + for unit in self.units.iter() { + unit.parse_inlined_functions(self).skip_all_loads()?; } Ok(()) } @@ -365,15 +562,22 @@ struct UnitRange { range: gimli::Range, } -struct ResDwarf { - unit_ranges: Vec, - units: Vec>, - sections: Arc>, - sup: Option>>, +struct ResUnit { + offset: gimli::DebugInfoOffset, + dw_unit: gimli::Unit, + lang: Option, + lines: LazyCell>, + funcs: LazyCell, Error>>, + dwo: LazyCell>, gimli::Unit)>>, Error>>, +} + +struct SupUnit { + offset: gimli::DebugInfoOffset, + dw_unit: gimli::Unit, } -impl ResDwarf { - fn parse(sections: Arc>) -> Result { +impl Context { + fn parse_units(sections: &gimli::Dwarf) -> Result<(Vec, Vec>), Error> { // Find all the references to compilation units in .debug_aranges. // Note that we always also iterate through all of .debug_info to // find compilation units, because .debug_aranges may be missing some. @@ -484,7 +688,7 @@ impl ResDwarf { } } } else { - have_unit_range |= ranges.for_each_range(§ions, &dw_unit, |range| { + have_unit_range |= ranges.for_each_range(sections, &dw_unit, |range| { unit_ranges.push(UnitRange { range, unit_id, @@ -500,7 +704,7 @@ impl ResDwarf { // Try to get some ranges from the line program sequences. if let Some(ref ilnp) = dw_unit.line_program { if let Ok(lines) = lines - .borrow_with(|| Lines::parse(&dw_unit, ilnp.clone(), &*sections)) + .borrow_with(|| Lines::parse(&dw_unit, ilnp.clone(), sections)) .as_ref() { for sequence in lines.sequences.iter() { @@ -523,6 +727,7 @@ impl ResDwarf { lang, lines, funcs: LazyCell::new(), + dwo: LazyCell::new(), }); } @@ -537,23 +742,60 @@ impl ResDwarf { i.max_end = max; } - Ok(ResDwarf { - units: res_units, - unit_ranges, - sections, - sup: None, - }) + Ok((unit_ranges, res_units)) } - fn find_unit(&self, offset: gimli::DebugInfoOffset) -> Result<&ResUnit, Error> { - match self - .units - .binary_search_by_key(&offset.0, |unit| unit.offset.0) - { - // There is never a DIE at the unit offset or before the first unit. - Ok(_) | Err(0) => Err(gimli::Error::NoEntryAtGivenOffset), - Err(i) => Ok(&self.units[i - 1]), + fn parse_sup(sections: &gimli::Dwarf) -> Result>, Error> { + let mut sup_units = Vec::new(); + let mut units = sections.units(); + while let Some(header) = units.next()? { + let offset = match header.offset().as_debug_info_offset() { + Some(offset) => offset, + None => continue, + }; + let dw_unit = match sections.unit(header) { + Ok(dw_unit) => dw_unit, + Err(_) => continue, + }; + sup_units.push(SupUnit { dw_unit, offset }); } + Ok(sup_units) + } + + // Find the unit containing the given offset, and convert the offset into a unit offset. + fn find_unit( + &self, + offset: gimli::DebugInfoOffset, + file: DebugFile, + ) -> Result<(&gimli::Unit, gimli::UnitOffset), Error> { + let unit = match file { + DebugFile::Primary => { + match self + .units + .binary_search_by_key(&offset.0, |unit| unit.offset.0) + { + // There is never a DIE at the unit offset or before the first unit. + Ok(_) | Err(0) => return Err(gimli::Error::NoEntryAtGivenOffset), + Err(i) => &self.units[i - 1].dw_unit, + } + } + DebugFile::Supplementary => { + match self + .sup_units + .binary_search_by_key(&offset.0, |unit| unit.offset.0) + { + // There is never a DIE at the unit offset or before the first unit. + Ok(_) | Err(0) => return Err(gimli::Error::NoEntryAtGivenOffset), + Err(i) => &self.sup_units[i - 1].dw_unit, + } + } + DebugFile::Dwo => return Err(gimli::Error::NoEntryAtGivenOffset), + }; + + let unit_offset = offset + .to_unit_offset(&unit.header) + .ok_or(gimli::Error::NoEntryAtGivenOffset)?; + Ok((unit, unit_offset)) } } @@ -680,16 +922,247 @@ struct LineRow { column: u32, } -struct ResUnit { - offset: gimli::DebugInfoOffset, - dw_unit: gimli::Unit, - lang: Option, - lines: LazyCell>, - funcs: LazyCell, Error>>, +/// This struct contains the information needed to find split DWARF data +/// and to produce a `gimli::Dwarf` for it. +pub struct SplitDwarfLoad { + /// The dwo id, for looking up in a DWARF package, or for + /// verifying an unpacked dwo found on the file system + pub dwo_id: gimli::DwoId, + /// The compilation directory `path` is relative to. + pub comp_dir: Option, + /// A path on the filesystem, relative to `comp_dir` to find this dwo. + pub path: Option, + /// Once the split DWARF data is loaded, the loader is expected + /// to call [make_dwo(parent)](gimli::read::Dwarf::make_dwo) before + /// returning the data. + pub parent: Arc>, +} + +struct SimpleLookup +where + F: FnOnce(Option>>) -> T, + R: gimli::Reader, +{ + f: F, + phantom: PhantomData<(T, R)>, +} + +impl SimpleLookup +where + F: FnOnce(Option>>) -> T, + R: gimli::Reader, +{ + fn new_complete(t: F::Output) -> LookupResult> { + LookupResult::Output(t) + } + + fn new_needs_load(load: SplitDwarfLoad, f: F) -> LookupResult> { + LookupResult::Load { + load, + continuation: SimpleLookup { + f, + phantom: PhantomData, + }, + } + } +} + +impl LookupContinuation for SimpleLookup +where + F: FnOnce(Option>>) -> T, + R: gimli::Reader, +{ + type Output = T; + type Buf = R; + + fn resume(self, v: Option>>) -> LookupResult { + LookupResult::Output((self.f)(v)) + } +} + +struct MappedLookup +where + L: LookupContinuation, + F: FnOnce(L::Output) -> T, +{ + original: L, + mutator: F, +} + +impl LookupContinuation for MappedLookup +where + L: LookupContinuation, + F: FnOnce(L::Output) -> T, +{ + type Output = T; + type Buf = L::Buf; + + fn resume(self, v: Option>>) -> LookupResult { + match self.original.resume(v) { + LookupResult::Output(t) => LookupResult::Output((self.mutator)(t)), + LookupResult::Load { load, continuation } => LookupResult::Load { + load, + continuation: MappedLookup { + original: continuation, + mutator: self.mutator, + }, + }, + } + } +} + +/// Some functions (e.g. `find_frames`) require considering multiple +/// compilation units, each of which might require their own split DWARF +/// lookup (and thus produce a continuation). +/// +/// We store the underlying continuation here as well as a mutator function +/// that will either a) decide that the result of this continuation is +/// what is needed and mutate it to the final result or b) produce another +/// `LookupResult`. `new_lookup` will in turn eagerly drive any non-continuation +/// `LookupResult` with successive invocations of the mutator, until a new +/// continuation or a final result is produced. And finally, the impl of +/// `LookupContinuation::resume` will call `new_lookup` each time the +/// computation is resumed. +struct LoopingLookup +where + L: LookupContinuation, + F: FnMut(L::Output) -> ControlFlow>, +{ + continuation: L, + mutator: F, +} + +impl LoopingLookup +where + L: LookupContinuation, + F: FnMut(L::Output) -> ControlFlow>, +{ + fn new_complete(t: T) -> LookupResult { + LookupResult::Output(t) + } + + fn new_lookup(mut r: LookupResult, mut mutator: F) -> LookupResult { + // Drive the loop eagerly so that we only ever have to represent one state + // (the r == ControlFlow::Continue state) in LoopingLookup. + loop { + match r { + LookupResult::Output(l) => match mutator(l) { + ControlFlow::Break(t) => return LookupResult::Output(t), + ControlFlow::Continue(r2) => { + r = r2; + } + }, + LookupResult::Load { load, continuation } => { + return LookupResult::Load { + load, + continuation: LoopingLookup { + continuation, + mutator, + }, + }; + } + } + } + } +} + +impl LookupContinuation for LoopingLookup +where + L: LookupContinuation, + F: FnMut(L::Output) -> ControlFlow>, +{ + type Output = T; + type Buf = L::Buf; + + fn resume(self, v: Option>>) -> LookupResult { + let r = self.continuation.resume(v); + LoopingLookup::new_lookup(r, self.mutator) + } } impl ResUnit { + fn dwarf_and_unit_dwo<'unit, 'ctx: 'unit>( + &'unit self, + ctx: &'ctx Context, + ) -> LookupResult< + SimpleLookup< + Result<(DebugFile, &'unit gimli::Dwarf, &'unit gimli::Unit), Error>, + R, + impl FnOnce( + Option>>, + ) + -> Result<(DebugFile, &'unit gimli::Dwarf, &'unit gimli::Unit), Error>, + >, + > { + loop { + break SimpleLookup::new_complete(match self.dwo.borrow() { + Some(Ok(Some(v))) => Ok((DebugFile::Dwo, &*v.0, &v.1)), + Some(Ok(None)) => Ok((DebugFile::Primary, &*ctx.sections, &self.dw_unit)), + Some(Err(e)) => Err(*e), + None => { + let dwo_id = match self.dw_unit.dwo_id { + None => { + self.dwo.borrow_with(|| Ok(None)); + continue; + } + Some(dwo_id) => dwo_id, + }; + + let comp_dir = self.dw_unit.comp_dir.clone(); + + let dwo_name = self.dw_unit.dwo_name().and_then(|s| { + if let Some(s) = s { + Ok(Some(ctx.sections.attr_string(&self.dw_unit, s)?)) + } else { + Ok(None) + } + }); + + let path = match dwo_name { + Ok(v) => v, + Err(e) => { + self.dwo.borrow_with(|| Err(e)); + continue; + } + }; + + let process_dwo = move |dwo_dwarf: Option>>| { + let dwo_dwarf = match dwo_dwarf { + None => return Ok(None), + Some(dwo_dwarf) => dwo_dwarf, + }; + let mut dwo_units = dwo_dwarf.units(); + let dwo_header = match dwo_units.next()? { + Some(dwo_header) => dwo_header, + None => return Ok(None), + }; + + let mut dwo_unit = dwo_dwarf.unit(dwo_header)?; + dwo_unit.copy_relocated_attributes(&self.dw_unit); + Ok(Some(Box::new((dwo_dwarf, dwo_unit)))) + }; + + return SimpleLookup::new_needs_load( + SplitDwarfLoad { + dwo_id, + comp_dir, + path, + parent: ctx.sections.clone(), + }, + move |dwo_dwarf| match self.dwo.borrow_with(|| process_dwo(dwo_dwarf)) { + Ok(Some(v)) => Ok((DebugFile::Dwo, &*v.0, &v.1)), + Ok(None) => Ok((DebugFile::Primary, &*ctx.sections, &self.dw_unit)), + Err(e) => Err(*e), + }, + ); + } + }); + } + } + fn parse_lines(&self, sections: &gimli::Dwarf) -> Result, Error> { + // NB: line information is always stored in the main debug file so this does not need + // to handle DWOs. let ilnp = match self.dw_unit.line_program { Some(ref ilnp) => ilnp, None => return Ok(None), @@ -701,19 +1174,39 @@ impl ResUnit { .map_err(Error::clone) } - fn parse_functions(&self, dwarf: &ResDwarf) -> Result<&Functions, Error> { + fn parse_functions_dwarf_and_unit( + &self, + unit: &gimli::Unit, + sections: &gimli::Dwarf, + ) -> Result<&Functions, Error> { self.funcs - .borrow_with(|| Functions::parse(&self.dw_unit, dwarf)) + .borrow_with(|| Functions::parse(unit, sections)) .as_ref() .map_err(Error::clone) } - fn parse_inlined_functions(&self, dwarf: &ResDwarf) -> Result<(), Error> { - self.funcs - .borrow_with(|| Functions::parse(&self.dw_unit, dwarf)) - .as_ref() - .map_err(Error::clone)? - .parse_inlined_functions(&self.dw_unit, dwarf) + fn parse_functions<'unit, 'ctx: 'unit>( + &'unit self, + ctx: &'ctx Context, + ) -> LookupResult, Error>, Buf = R>> + { + self.dwarf_and_unit_dwo(ctx).map(move |r| { + let (_file, sections, unit) = r?; + self.parse_functions_dwarf_and_unit(unit, sections) + }) + } + fn parse_inlined_functions<'unit, 'ctx: 'unit>( + &'unit self, + ctx: &'ctx Context, + ) -> LookupResult, Buf = R> + 'unit> { + self.dwarf_and_unit_dwo(ctx).map(move |r| { + let (file, sections, unit) = r?; + self.funcs + .borrow_with(|| Functions::parse(unit, sections)) + .as_ref() + .map_err(Error::clone)? + .parse_inlined_functions(file, unit, ctx, sections) + }) } fn find_location( @@ -741,27 +1234,35 @@ impl ResUnit { LocationRangeUnitIter::new(self, sections, probe_low, probe_high) } - fn find_function_or_location( - &self, + fn find_function_or_location<'unit, 'ctx: 'unit>( + &'unit self, probe: u64, - dwarf: &ResDwarf, - ) -> Result<(Option<&Function>, Option>), Error> { - let functions = self.parse_functions(dwarf)?; - let function = match functions.find_address(probe) { - Some(address) => { - let function_index = functions.addresses[address].function; - let (offset, ref function) = functions.functions[function_index]; - Some( - function - .borrow_with(|| Function::parse(offset, &self.dw_unit, dwarf)) - .as_ref() - .map_err(Error::clone)?, - ) - } - None => None, - }; - let location = self.find_location(probe, &dwarf.sections)?; - Ok((function, location)) + ctx: &'ctx Context, + ) -> LookupResult< + impl LookupContinuation< + Output = Result<(Option<&'unit Function>, Option>), Error>, + Buf = R, + >, + > { + self.dwarf_and_unit_dwo(ctx).map(move |r| { + let (file, sections, unit) = r?; + let functions = self.parse_functions_dwarf_and_unit(unit, sections)?; + let function = match functions.find_address(probe) { + Some(address) => { + let function_index = functions.addresses[address].function; + let (offset, ref function) = functions.functions[function_index]; + Some( + function + .borrow_with(|| Function::parse(offset, file, unit, ctx, sections)) + .as_ref() + .map_err(Error::clone)?, + ) + } + None => None, + }; + let location = self.find_location(probe, sections)?; + Ok((function, location)) + }) } } @@ -778,7 +1279,7 @@ pub struct LocationRangeIter<'ctx, R: gimli::Reader> { impl<'ctx, R: gimli::Reader> LocationRangeIter<'ctx, R> { #[inline] fn new(ctx: &'ctx Context, probe_low: u64, probe_high: u64) -> Result { - let sections = &ctx.dwarf.sections; + let sections = &ctx.sections; let unit_iter = ctx.find_units_range(probe_low, probe_high); Ok(Self { unit_iter: Box::new(unit_iter), @@ -879,12 +1380,11 @@ impl<'ctx> LocationRangeUnitIter<'ctx> { let row_idx = if let Some(seq) = lines.sequences.get(seq_idx) { let idx = seq.rows.binary_search_by(|row| row.address.cmp(&probe_low)); - let idx = match idx { + match idx { Ok(x) => x, Err(0) => 0, // probe below sequence, but range could overlap Err(x) => x - 1, - }; - idx + } } else { 0 }; @@ -906,12 +1406,7 @@ impl<'ctx> Iterator for LocationRangeUnitIter<'ctx> { type Item = (u64, u64, Location<'ctx>); fn next(&mut self) -> Option<(u64, u64, Location<'ctx>)> { - loop { - let seq = match self.seqs.get(self.seq_idx) { - Some(seq) => seq, - None => break, - }; - + while let Some(seq) = self.seqs.get(self.seq_idx) { if seq.start >= self.probe_high { break; } @@ -1038,11 +1533,11 @@ impl RangeAttributes { /// An iterator over function frames. pub struct FrameIter<'ctx, R>(FrameIterState<'ctx, R>) where - R: gimli::Reader + 'ctx; + R: gimli::Reader; enum FrameIterState<'ctx, R> where - R: gimli::Reader + 'ctx, + R: gimli::Reader, { Empty, Location(Option>), @@ -1051,7 +1546,7 @@ where struct FrameIterFrames<'ctx, R> where - R: gimli::Reader + 'ctx, + R: gimli::Reader, { unit: &'ctx ResUnit, sections: &'ctx gimli::Dwarf, @@ -1111,9 +1606,9 @@ where None }, }; - if func.call_file != 0 { + if let Some(call_file) = func.call_file { if let Some(lines) = frames.unit.parse_lines(frames.sections)? { - next.file = lines.files.get(func.call_file as usize).map(String::as_str); + next.file = lines.files.get(call_file as usize).map(String::as_str); } } frames.next = Some(next); @@ -1163,12 +1658,12 @@ pub struct FunctionName { impl FunctionName { /// The raw name of this function before demangling. - pub fn raw_name(&self) -> Result, Error> { + pub fn raw_name(&self) -> Result, Error> { self.name.to_string_lossy() } /// The name of this function after demangling (if applicable). - pub fn demangle(&self) -> Result, Error> { + pub fn demangle(&self) -> Result, Error> { self.raw_name().map(|x| demangle_auto(x, self.language)) } } @@ -1204,7 +1699,7 @@ pub fn demangle(name: &str, language: gimli::DwLang) -> Option { /// demangle the name. Currently, these heuristics are very basic. /// /// If demangling fails or is not required, then `name` is returned unchanged. -pub fn demangle_auto(name: Cow, language: Option) -> Cow { +pub fn demangle_auto(name: Cow<'_, str>, language: Option) -> Cow<'_, str> { match language { Some(language) => demangle(name.as_ref(), language), None => demangle(name.as_ref(), gimli::DW_LANG_Rust) @@ -1229,6 +1724,6 @@ mod tests { #[test] fn context_is_send() { fn assert_is_send() {} - assert_is_send::>>(); + assert_is_send::>>(); } } -- cgit v1.2.3