-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathlib.rs
478 lines (440 loc) · 16.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! This crate provides a cross-platform library and binary for translating addresses into
//! function names, file names and line numbers. Given an address in an executable or an
//! offset in a section of a relocatable object, it uses the debugging information to
//! figure out which file name and line number are associated with it.
//!
//! When used as a library, files must first be loaded using the
//! [`object`](https://github.com/gimli-rs/object) crate.
//! A context can then be created with [`Context::new`](./struct.Context.html#method.new).
//! The context caches some of the parsed information so that multiple lookups are
//! efficient.
//! Location information is obtained with
//! [`Context::find_location`](./struct.Context.html#method.find_location) or
//! [`Context::find_location_range`](./struct.Context.html#method.find_location_range).
//! Function information is obtained with
//! [`Context::find_frames`](./struct.Context.html#method.find_frames), which returns
//! a frame for each inline function. Each frame contains both name and location.
//!
//! The crate has an example CLI wrapper around the library which provides some of
//! the functionality of the `addr2line` command line tool distributed with [GNU
//! binutils](https://www.gnu.org/software/binutils/).
//!
//! Currently this library only provides information from the DWARF debugging information,
//! which is parsed using [`gimli`](https://github.com/gimli-rs/gimli). The example CLI
//! wrapper also uses symbol table information provided by the `object` crate.
#![deny(missing_docs)]
#![no_std]
#[cfg(feature = "std")]
extern crate std;
#[allow(unused_imports)]
#[macro_use]
extern crate alloc;
#[cfg(feature = "fallible-iterator")]
pub extern crate fallible_iterator;
pub extern crate gimli;
#[cfg(feature = "object")]
pub extern crate object;
use alloc::borrow::Cow;
#[cfg(feature = "object")]
use alloc::rc::Rc;
use alloc::sync::Arc;
use core::ops::ControlFlow;
use core::u64;
use crate::function::{Function, Functions, InlinedFunction, LazyFunctions};
use crate::line::{LazyLines, LineLocationRangeIter, Lines};
use crate::lookup::{LoopingLookup, SimpleLookup};
use crate::unit::{ResUnit, ResUnits, SupUnits};
#[cfg(feature = "smallvec")]
mod maybe_small {
pub type Vec<T> = smallvec::SmallVec<[T; 16]>;
pub type IntoIter<T> = smallvec::IntoIter<[T; 16]>;
}
#[cfg(not(feature = "smallvec"))]
mod maybe_small {
pub type Vec<T> = alloc::vec::Vec<T>;
pub type IntoIter<T> = alloc::vec::IntoIter<T>;
}
#[cfg(all(feature = "std", feature = "object", feature = "memmap2"))]
/// A simple builtin split DWARF loader.
pub mod builtin_split_dwarf_loader;
mod frame;
pub use frame::{demangle, demangle_auto, Frame, FrameIter, FunctionName, Location};
mod function;
mod lazy;
mod line;
mod lookup;
pub use lookup::{LookupContinuation, LookupResult, SplitDwarfLoad};
mod unit;
pub use unit::LocationRangeIter;
type Error = gimli::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DebugFile {
Primary,
Supplementary,
Dwo,
}
/// 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<R: gimli::Reader> {
sections: Arc<gimli::Dwarf<R>>,
units: ResUnits<R>,
sup_units: SupUnits<R>,
}
/// The type of `Context` that supports the `new` method.
#[cfg(feature = "std-object")]
pub type ObjectContext = Context<gimli::EndianRcSlice<gimli::RunTimeEndian>>;
#[cfg(feature = "std-object")]
impl Context<gimli::EndianRcSlice<gimli::RunTimeEndian>> {
/// Construct a new `Context`.
///
/// The resulting `Context` uses `gimli::EndianRcSlice<gimli::RunTimeEndian>`.
/// 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 a more specialised `gimli::Reader` implementation.
#[inline]
pub fn new<'data, O: object::Object<'data>>(file: &O) -> Result<Self, Error> {
Self::new_with_sup(file, None)
}
/// Construct a new `Context`.
///
/// Optionally also use a supplementary object file.
///
/// The resulting `Context` uses `gimli::EndianRcSlice<gimli::RunTimeEndian>`.
/// 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 a more specialised `gimli::Reader` implementation.
pub fn new_with_sup<'data, O: object::Object<'data>>(
file: &O,
sup_file: Option<&O>,
) -> Result<Self, Error> {
let endian = if file.is_little_endian() {
gimli::RunTimeEndian::Little
} else {
gimli::RunTimeEndian::Big
};
fn load_section<'data, O, Endian>(
id: gimli::SectionId,
file: &O,
endian: Endian,
) -> Result<gimli::EndianRcSlice<Endian>, Error>
where
O: object::Object<'data>,
Endian: gimli::Endianity,
{
use object::ObjectSection;
let data = file
.section_by_name(id.name())
.and_then(|section| section.uncompressed_data().ok())
.unwrap_or(Cow::Borrowed(&[]));
Ok(gimli::EndianRcSlice::new(Rc::from(&*data), endian))
}
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)
}
}
impl<R: gimli::Reader> Context<R> {
/// Construct a new `Context` from DWARF sections.
///
/// This method does not support using a supplementary object file.
#[allow(clippy::too_many_arguments)]
pub fn from_sections(
debug_abbrev: gimli::DebugAbbrev<R>,
debug_addr: gimli::DebugAddr<R>,
debug_aranges: gimli::DebugAranges<R>,
debug_info: gimli::DebugInfo<R>,
debug_line: gimli::DebugLine<R>,
debug_line_str: gimli::DebugLineStr<R>,
debug_ranges: gimli::DebugRanges<R>,
debug_rnglists: gimli::DebugRngLists<R>,
debug_str: gimli::DebugStr<R>,
debug_str_offsets: gimli::DebugStrOffsets<R>,
default_section: R,
) -> Result<Self, Error> {
Self::from_dwarf(gimli::Dwarf {
debug_abbrev,
debug_addr,
debug_aranges,
debug_info,
debug_line,
debug_line_str,
debug_str,
debug_str_offsets,
debug_types: default_section.clone().into(),
locations: gimli::LocationLists::new(
default_section.clone().into(),
default_section.into(),
),
ranges: gimli::RangeLists::new(debug_ranges, debug_rnglists),
file_type: gimli::DwarfFileType::Main,
sup: None,
abbreviations_cache: gimli::AbbreviationsCache::new(),
})
}
/// Construct a new `Context` from an existing [`gimli::Dwarf`] object.
#[inline]
pub fn from_dwarf(sections: gimli::Dwarf<R>) -> Result<Context<R>, Error> {
let sections = Arc::new(sections);
let units = ResUnits::parse(§ions)?;
let sup_units = if let Some(sup) = sections.sup.as_ref() {
SupUnits::parse(sup)?
} else {
SupUnits::default()
};
Ok(Context {
sections,
units,
sup_units,
})
}
}
impl<R: gimli::Reader> Context<R> {
/// Find the DWARF unit corresponding to the given virtual memory address.
pub fn find_dwarf_and_unit(
&self,
probe: u64,
) -> LookupResult<
impl LookupContinuation<Output = Option<(&gimli::Dwarf<R>, &gimli::Unit<R>)>, Buf = R>,
> {
let mut units_iter = self.units.find(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(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,
},
})
},
);
}
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<Option<Location<'_>>, Error> {
for unit in self.units.find(probe) {
if let Some(location) = unit.find_location(probe, &self.sections)? {
return Ok(Some(location));
}
}
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(
&self,
probe_low: u64,
probe_high: u64,
) -> Result<LocationRangeIter<'_, R>, Error> {
self.units
.find_location_range(probe_low, probe_high, &self.sections)
}
/// Return an iterator for the function frames corresponding to the given virtual
/// memory address.
///
/// If the probe address is not for an inline function then only one frame is
/// returned.
///
/// 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,
) -> LookupResult<impl LookupContinuation<Output = Result<FrameIter<'_, R>, Error>, Buf = R>>
{
let mut units_iter = self.units.find(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::new_frames(
unit,
&self.sections,
function,
inlined_functions,
location,
))
}
Ok((None, Some(location))) => Ok(FrameIter::new_location(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::new_empty()),
},
})
})
} else {
LoopingLookup::new_complete(Ok(FrameIter::new_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<gimli::EndianRcSlice<gimli::RunTimeEndian>> = todo!();
/// # let do_split_dwarf_load = |load: SplitDwarfLoad<gimli::EndianRcSlice<gimli::RunTimeEndian>>| -> Option<Arc<gimli::Dwarf<gimli::EndianRcSlice<gimli::RunTimeEndian>>>> { 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<R>,
impl FnOnce(Option<Arc<gimli::Dwarf<R>>>) -> Result<(), gimli::Error> + '_,
),
> {
self.units
.find(probe)
.filter_map(move |unit| match unit.dwarf_and_unit(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.units.iter() {
unit.parse_lines(&self.sections)?;
}
Ok(())
}
/// Initialize all function data structures. This is used for benchmarks.
#[doc(hidden)]
pub fn parse_functions(&self) -> Result<(), Error> {
for unit in self.units.iter() {
unit.parse_functions(self).skip_all_loads()?;
}
Ok(())
}
/// 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.units.iter() {
unit.parse_inlined_functions(self).skip_all_loads()?;
}
Ok(())
}
}
impl<R: gimli::Reader> Context<R> {
// Find the unit containing the given offset, and convert the offset into a unit offset.
fn find_unit(
&self,
offset: gimli::DebugInfoOffset<R::Offset>,
file: DebugFile,
) -> Result<(&gimli::Unit<R>, gimli::UnitOffset<R::Offset>), Error> {
let unit = match file {
DebugFile::Primary => self.units.find_offset(offset)?,
DebugFile::Supplementary => self.sup_units.find_offset(offset)?,
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))
}
}
struct RangeAttributes<R: gimli::Reader> {
low_pc: Option<u64>,
high_pc: Option<u64>,
size: Option<u64>,
ranges_offset: Option<gimli::RangeListsOffset<<R as gimli::Reader>::Offset>>,
}
impl<R: gimli::Reader> Default for RangeAttributes<R> {
fn default() -> Self {
RangeAttributes {
low_pc: None,
high_pc: None,
size: None,
ranges_offset: None,
}
}
}
impl<R: gimli::Reader> RangeAttributes<R> {
fn for_each_range<F: FnMut(gimli::Range)>(
&self,
sections: &gimli::Dwarf<R>,
unit: &gimli::Unit<R>,
mut f: F,
) -> Result<bool, Error> {
let mut added_any = false;
let mut add_range = |range: gimli::Range| {
if range.begin < range.end {
f(range);
added_any = true
}
};
if let Some(ranges_offset) = self.ranges_offset {
let mut range_list = sections.ranges(unit, ranges_offset)?;
while let Some(range) = range_list.next()? {
add_range(range);
}
} else if let (Some(begin), Some(end)) = (self.low_pc, self.high_pc) {
add_range(gimli::Range { begin, end });
} else if let (Some(begin), Some(size)) = (self.low_pc, self.size) {
add_range(gimli::Range {
begin,
end: begin + size,
});
}
Ok(added_any)
}
}
#[cfg(test)]
mod tests {
#[test]
fn context_is_send() {
fn assert_is_send<T: Send>() {}
assert_is_send::<crate::Context<gimli::read::EndianSlice<'_, gimli::LittleEndian>>>();
}
}