Skip to content

Commit

Permalink
add line to address functionality
Browse files Browse the repository at this point in the history
this commit add a prototype of line to address functionality.

It not efficent. It not documented, but well it work.
  • Loading branch information
FlyingCanoe committed Jul 3, 2021
1 parent 3164ad3 commit d910b85
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ impl<R: gimli::Reader> Context<R> {
Ok(None)
}

/// TODO doc
pub fn find_address(&self, location: Location) -> Result<Option<u64>, Error> {
for (addr, _, loc) in LocationIter::new(self)? {
if loc == location {
return Ok(Some(addr));
}
}

Ok(None)
}

/// Return source file and lines for a range of addresses. For each location it also
/// returns the address and size of the range of the underlying instructions.
pub fn find_location_range(
Expand Down Expand Up @@ -787,6 +798,25 @@ where
}
}

struct LocationIter<'ctx, R: gimli::Reader> {
iter: LocationRangeIter<'ctx, R>,
}

impl<'ctx, R: gimli::Reader> LocationIter<'ctx, R> {
fn new(ctx: &'ctx Context<R>) -> Result<Self, Error> {
let iter = LocationRangeIter::new(ctx, 0, u64::MAX)?;
Ok(Self { iter })
}
}

impl<'ctx, R: gimli::Reader> Iterator for LocationIter<'ctx, R> {
type Item = (u64, u64, Location<'ctx>);

fn next(&mut self) -> Option<(u64, u64, Location<'ctx>)> {
self.iter.next()
}
}

#[cfg(feature = "fallible-iterator")]
impl<'ctx, R> fallible_iterator::FallibleIterator for LocationRangeIter<'ctx, R>
where
Expand Down Expand Up @@ -1173,6 +1203,7 @@ pub fn demangle_auto(name: Cow<str>, language: Option<gimli::DwLang>) -> Cow<str
}

/// A source location.
#[derive(PartialEq, Clone, Debug)]
pub struct Location<'a> {
/// The file name.
pub file: Option<&'a str>,
Expand Down

0 comments on commit d910b85

Please sign in to comment.