From 327b9eb18ced12450a92b2647cbfedf1bdd81b00 Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Sat, 1 May 2021 16:39:13 +1000 Subject: [PATCH 1/3] Update object/gimli deps --- Cargo.toml | 4 ++-- benches/bench.rs | 3 +-- examples/addr2line.rs | 29 ++++++++++--------------- src/lib.rs | 50 +++++++++++++------------------------------ tests/parse.rs | 3 +-- 5 files changed, 30 insertions(+), 59 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30babbf..64f5b54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,9 @@ exclude = ["/benches/*", "/fixtures/*"] travis-ci = { repository = "gimli-rs/addr2line" } [dependencies] -gimli = { version = "0.23", default-features = false, features = ["read"] } +gimli = { version = "0.24", default-features = false, features = ["read"] } fallible-iterator = { version = "0.2", default-features = false, optional = true } -object = { version = "0.23", default-features = false, features = ["read"], optional = true } +object = { version = "0.24", default-features = false, features = ["read"], optional = true } smallvec = { version = "1", default-features = false, optional = true } rustc-demangle = { version = "0.1", optional = true } cpp_demangle = { version = "0.3", default-features = false, optional = true } diff --git a/benches/bench.rs b/benches/bench.rs index b2d828b..18b57b8 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -36,8 +36,7 @@ fn dwarf_load<'a>(object: &object::File<'a>) -> gimli::Dwarf> { .map(|section| section.uncompressed_data().unwrap()) .unwrap_or(Cow::Borrowed(&[][..]))) }; - let load_section_sup = |_| Ok(Cow::Borrowed(&[][..])); - gimli::Dwarf::load(&load_section, &load_section_sup).unwrap() + gimli::Dwarf::load(&load_section).unwrap() } fn dwarf_borrow<'a>( diff --git a/examples/addr2line.rs b/examples/addr2line.rs index 1e0f5a6..0ce1164 100644 --- a/examples/addr2line.rs +++ b/examples/addr2line.rs @@ -10,7 +10,6 @@ use std::borrow::Cow; use std::fs::File; use std::io::{BufRead, Lines, StdinLock, Write}; use std::path::Path; -use std::result; use clap::{App, Arg, Values}; use fallible_iterator::FallibleIterator; @@ -38,7 +37,7 @@ impl<'a> Iterator for Addrs<'a> { fn next(&mut self) -> Option { let text = match *self { Addrs::Args(ref mut vals) => vals.next().map(Cow::from), - Addrs::Stdin(ref mut lines) => lines.next().map(result::Result::unwrap).map(Cow::from), + Addrs::Stdin(ref mut lines) => lines.next().map(Result::unwrap).map(Cow::from), }; text.as_ref() .map(Cow::as_ref) @@ -201,24 +200,18 @@ fn main() { } else { None }; - let mut load_sup_section = |id: gimli::SectionId| -> Result<_, _> { - if let Some(ref sup_object) = sup_object { - load_file_section(id, &sup_object, endian, &arena_data) - } else { - Ok(gimli::EndianSlice::new(&[][..], endian)) - } - }; let symbols = object.symbol_map(); - let dwarf = gimli::Dwarf::load(&mut load_section, &mut load_sup_section).unwrap(); - let dwarf_sup = Some( - gimli::Dwarf::load(&mut load_sup_section, |_| -> Result<_, _> { - Ok(gimli::EndianSlice::new(&[][..], endian)) - }) - .unwrap(), - ); - - let ctx = Context::from_dwarf_with_sup(dwarf, dwarf_sup).unwrap(); + let mut dwarf = gimli::Dwarf::load(&mut load_section).unwrap(); + if let Some(ref sup_object) = sup_object { + let mut load_sup_section = |id: gimli::SectionId| -> Result<_, _> { + load_file_section(id, sup_object, endian, &arena_data) + }; + dwarf.load_sup(&mut load_sup_section).unwrap(); + + } + + let ctx = Context::from_dwarf(dwarf).unwrap(); let stdin = std::io::stdin(); let addrs = matches diff --git a/src/lib.rs b/src/lib.rs index d3c7886..4ef8fb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,11 +44,13 @@ use alloc::boxed::Box; #[cfg(feature = "object")] use alloc::rc::Rc; use alloc::string::{String, ToString}; +use alloc::sync::Arc; use alloc::vec::Vec; use core::cmp::{self, Ordering}; use core::iter; use core::mem; +use core::num::NonZeroU64; use core::u64; use crate::lazy::LazyCell; @@ -136,24 +138,11 @@ impl Context> { Ok(gimli::EndianRcSlice::new(Rc::from(&*data), endian)) } - let default_section = Ok(gimli::EndianRcSlice::new(Rc::from(&[][..]), endian)); - - let dwarf = gimli::Dwarf::load( - |id| load_section(id, file, endian), - |id| { - sup_file - .map(|f| load_section(id, f, endian)) - .unwrap_or_else(|| default_section.clone()) - }, - )?; - let sup_dwarf = match sup_file { - Some(sup_file) => Some(gimli::Dwarf::load( - |id| load_section(id, sup_file, endian), - |_| default_section.clone(), - )?), - None => None, - }; - Context::from_dwarf_with_sup(dwarf, sup_dwarf) + let mut dwarf = gimli::Dwarf::load(|id| load_section(id, file, endian))?; + if let Some(sup_file) = sup_file { + dwarf.load_sup(|id| load_section(id, sup_file, endian))?; + } + Context::from_dwarf(dwarf) } } @@ -176,12 +165,12 @@ impl Context { Self::from_dwarf(gimli::Dwarf { debug_abbrev, debug_addr, + debug_aranges: default_section.clone().into(), debug_info, debug_line, debug_line_str, debug_str, debug_str_offsets, - debug_str_sup: default_section.clone().into(), debug_types: default_section.clone().into(), locations: gimli::LocationLists::new( default_section.clone().into(), @@ -189,24 +178,15 @@ impl Context { ), ranges: gimli::RangeLists::new(debug_ranges, debug_rnglists), file_type: gimli::DwarfFileType::Main, + sup: None, }) } /// Construct a new `Context` from an existing [`gimli::Dwarf`] object. #[inline] pub fn from_dwarf(sections: gimli::Dwarf) -> Result { - Self::from_dwarf_with_sup(sections, None) - } - - /// Construct a new `Context` from an existing [`gimli::Dwarf`] object. - /// - /// Optionally also use a supplementary object file. - pub fn from_dwarf_with_sup( - sections: gimli::Dwarf, - sup_sections: Option>, - ) -> Result { - let dwarf = ResDwarf::parse(sections)?; - let sup_dwarf = match sup_sections { + let dwarf = ResDwarf::parse(Arc::new(sections))?; + let sup_dwarf = match dwarf.sections.sup.clone() { Some(sup_sections) => Some(ResDwarf::parse(sup_sections)?), None => None, }; @@ -385,11 +365,11 @@ struct UnitRange { struct ResDwarf { unit_ranges: Vec, units: Vec>, - sections: gimli::Dwarf, + sections: Arc>, } impl ResDwarf { - fn parse(sections: gimli::Dwarf) -> Result { + fn parse(sections: Arc>) -> Result { let mut unit_ranges = Vec::new(); let mut res_units = Vec::new(); let mut units = sections.units(); @@ -548,10 +528,10 @@ impl ResUnit { let address = row.address(); let file_index = row.file_index(); - let line = row.line().unwrap_or(0) as u32; + let line = row.line().map(NonZeroU64::get).unwrap_or(0) as u32; let column = match row.column() { gimli::ColumnType::LeftEdge => 0, - gimli::ColumnType::Column(x) => x as u32, + gimli::ColumnType::Column(x) => x.get() as u32, }; if let Some(last_row) = sequence_rows.last_mut() { diff --git a/tests/parse.rs b/tests/parse.rs index 69de8d0..91d66e3 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -40,8 +40,7 @@ fn dwarf_load<'a>(object: &object::File<'a>) -> gimli::Dwarf> { .unwrap_or(&[][..]); Ok(Cow::Borrowed(data)) }; - let load_section_sup = |_| Ok(Cow::Borrowed(&[][..])); - gimli::Dwarf::load(&load_section, &load_section_sup).unwrap() + gimli::Dwarf::load(&load_section).unwrap() } fn dwarf_borrow<'a>( From 512a1a97a0593428a18967883b11ff933cabf580 Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Sat, 1 May 2021 16:39:30 +1000 Subject: [PATCH 2/3] Delete `sup_dwarf` parameters --- examples/addr2line.rs | 1 - src/lib.rs | 53 +++++++++++++++---------------------------- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/examples/addr2line.rs b/examples/addr2line.rs index 0ce1164..4b228a7 100644 --- a/examples/addr2line.rs +++ b/examples/addr2line.rs @@ -208,7 +208,6 @@ fn main() { load_file_section(id, sup_object, endian, &arena_data) }; dwarf.load_sup(&mut load_sup_section).unwrap(); - } let ctx = Context::from_dwarf(dwarf).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 4ef8fb9..0657355 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,6 @@ type Error = gimli::Error; /// when performing lookups for many addresses in the same executable. pub struct Context { dwarf: ResDwarf, - sup_dwarf: Option>, } /// The type of `Context` that supports the `new` method. @@ -185,12 +184,12 @@ impl Context { /// Construct a new `Context` from an existing [`gimli::Dwarf`] object. #[inline] pub fn from_dwarf(sections: gimli::Dwarf) -> Result { - let dwarf = ResDwarf::parse(Arc::new(sections))?; - let sup_dwarf = match dwarf.sections.sup.clone() { - Some(sup_sections) => Some(ResDwarf::parse(sup_sections)?), + let mut dwarf = ResDwarf::parse(Arc::new(sections))?; + dwarf.sup = match dwarf.sections.sup.clone() { + Some(sup_sections) => Some(Arc::new(ResDwarf::parse(sup_sections)?)), None => None, }; - Ok(Context { dwarf, sup_dwarf }) + Ok(Context { dwarf }) } /// The dwarf sections associated with this `Context`. @@ -269,7 +268,7 @@ impl Context { /// 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, self.sup_dwarf.as_ref()) { + match unit.find_function_or_location(probe, &self.dwarf) { Ok((Some(_), _)) | Ok((_, Some(_))) => return Some(&unit.dw_unit), _ => {} } @@ -308,7 +307,7 @@ impl Context { /// 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, self.sup_dwarf.as_ref())? { + 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 { @@ -350,7 +349,7 @@ impl Context { #[doc(hidden)] pub fn parse_inlined_functions(&self) -> Result<(), Error> { for unit in &self.dwarf.units { - unit.parse_inlined_functions(&self.dwarf, self.sup_dwarf.as_ref())?; + unit.parse_inlined_functions(&self.dwarf)?; } Ok(()) } @@ -366,6 +365,7 @@ struct ResDwarf { unit_ranges: Vec, units: Vec>, sections: Arc>, + sup: Option>>, } impl ResDwarf { @@ -459,6 +459,7 @@ impl ResDwarf { units: res_units, unit_ranges, sections, + sup: None, }) } @@ -581,16 +582,12 @@ impl ResUnit { .map_err(Error::clone) } - fn parse_inlined_functions( - &self, - dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, - ) -> Result<(), Error> { + 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, sup_dwarf) + .parse_inlined_functions(&self.dw_unit, dwarf) } fn find_location( @@ -622,7 +619,6 @@ impl ResUnit { &self, probe: u64, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, ) -> Result<(Option<&Function>, Option>), Error> { let functions = self.parse_functions(dwarf)?; let function = match functions.find_address(probe) { @@ -631,7 +627,7 @@ impl ResUnit { let (offset, ref function) = functions.functions[function_index]; Some( function - .borrow_with(|| Function::parse(offset, &self.dw_unit, dwarf, sup_dwarf)) + .borrow_with(|| Function::parse(offset, &self.dw_unit, dwarf)) .as_ref() .map_err(Error::clone)?, ) @@ -902,7 +898,6 @@ fn name_attr( attr: gimli::AttributeValue, unit: &gimli::Unit, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, recursion_limit: usize, ) -> Result, Error> where @@ -913,27 +908,23 @@ where } match attr { - gimli::AttributeValue::UnitRef(offset) => { - name_entry(unit, offset, dwarf, sup_dwarf, recursion_limit) - } + gimli::AttributeValue::UnitRef(offset) => name_entry(unit, offset, dwarf, 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, - sup_dwarf, recursion_limit, ) } gimli::AttributeValue::DebugInfoRefSup(dr) => { - if let Some(sup_dwarf) = sup_dwarf { + 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, - None, recursion_limit, ) } else { @@ -948,7 +939,6 @@ fn name_entry( unit: &gimli::Unit, offset: gimli::UnitOffset, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, recursion_limit: usize, ) -> Result, Error> where @@ -990,7 +980,7 @@ where } if let Some(next) = next { - return name_attr(next, unit, dwarf, sup_dwarf, recursion_limit - 1); + return name_attr(next, unit, dwarf, recursion_limit - 1); } Ok(None) @@ -1137,12 +1127,11 @@ impl Functions { &self, unit: &gimli::Unit, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, ) -> Result<(), Error> { for function in &*self.functions { function .1 - .borrow_with(|| Function::parse(function.0, unit, dwarf, sup_dwarf)) + .borrow_with(|| Function::parse(function.0, unit, dwarf)) .as_ref() .map_err(Error::clone)?; } @@ -1155,7 +1144,6 @@ impl Function { dw_die_offset: gimli::UnitOffset, unit: &gimli::Unit, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, ) -> Result { let mut entries = unit.entries_raw(Some(dw_die_offset))?; let depth = entries.next_depth(); @@ -1179,7 +1167,7 @@ impl Function { } gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => { if name.is_none() { - name = name_attr(attr.value(), unit, dwarf, sup_dwarf, 16)?; + name = name_attr(attr.value(), unit, dwarf, 16)?; } } _ => {} @@ -1196,7 +1184,6 @@ impl Function { depth, unit, dwarf, - sup_dwarf, &mut inlined_functions, &mut inlined_addresses, 0, @@ -1239,7 +1226,6 @@ impl Function { depth: isize, unit: &gimli::Unit, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, inlined_functions: &mut Vec>, inlined_addresses: &mut Vec, inlined_depth: usize, @@ -1263,7 +1249,6 @@ impl Function { next_depth, unit, dwarf, - sup_dwarf, inlined_functions, inlined_addresses, inlined_depth, @@ -1354,7 +1339,6 @@ impl InlinedFunction { depth: isize, unit: &gimli::Unit, dwarf: &ResDwarf, - sup_dwarf: Option<&ResDwarf>, inlined_functions: &mut Vec>, inlined_addresses: &mut Vec, inlined_depth: usize, @@ -1393,7 +1377,7 @@ impl InlinedFunction { } gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => { if name.is_none() { - name = name_attr(attr.value(), unit, dwarf, sup_dwarf, 16)?; + name = name_attr(attr.value(), unit, dwarf, 16)?; } } gimli::DW_AT_call_file => { @@ -1435,7 +1419,6 @@ impl InlinedFunction { depth, unit, dwarf, - sup_dwarf, inlined_functions, inlined_addresses, inlined_depth + 1, From 230661277e76ec3c8e01c5553cc74be147b86139 Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Sun, 2 May 2021 13:26:09 +1000 Subject: [PATCH 3/3] Run rustfmt in CI --- .github/workflows/rust.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e1e3250..915c021 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -53,6 +53,19 @@ jobs: rustup default nightly - run: cargo bench + rustfmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install rustup + run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal + - name: Install rust + run: | + rustup install stable + rustup default stable + rustup component add rustfmt + - run: cargo fmt --all -- --check + coverage: runs-on: ubuntu-latest steps: