std/backtrace/src/symbolize/
gimli.rs1use self::gimli::read::EndianSlice;
6use self::gimli::NativeEndian as Endian;
7use self::mmap::Mmap;
8use self::stash::Stash;
9use super::BytesOrWideString;
10use super::ResolveWhat;
11use super::SymbolName;
12use addr2line::gimli;
13use core::convert::TryInto;
14use core::mem;
15use libc::c_void;
16use mystd::ffi::OsString;
17use mystd::fs::File;
18use mystd::path::Path;
19use mystd::prelude::v1::*;
20
21#[cfg(backtrace_in_libstd)]
22mod mystd {
23 pub use crate::*;
24}
25#[cfg(not(backtrace_in_libstd))]
26extern crate std as mystd;
27
28cfg_if::cfg_if! {
29 if #[cfg(windows)] {
30 #[path = "gimli/mmap_windows.rs"]
31 mod mmap;
32 } else if #[cfg(target_vendor = "apple")] {
33 #[path = "gimli/mmap_unix.rs"]
34 mod mmap;
35 } else if #[cfg(any(
36 target_os = "android",
37 target_os = "freebsd",
38 target_os = "fuchsia",
39 target_os = "haiku",
40 target_os = "hurd",
41 target_os = "linux",
42 target_os = "openbsd",
43 target_os = "solaris",
44 target_os = "illumos",
45 target_os = "aix",
46 ))] {
47 #[path = "gimli/mmap_unix.rs"]
48 mod mmap;
49 } else {
50 #[path = "gimli/mmap_fake.rs"]
51 mod mmap;
52 }
53}
54
55mod lru;
56mod stash;
57
58use lru::Lru;
59
60const MAPPINGS_CACHE_SIZE: usize = 4;
61
62struct Mapping {
63 cx: Context<'static>,
65 _map: Mmap,
66 stash: Stash,
67}
68
69enum Either<A, B> {
70 #[allow(dead_code)]
71 A(A),
72 B(B),
73}
74
75impl Mapping {
76 fn mk<F>(data: Mmap, mk: F) -> Option<Mapping>
80 where
81 F: for<'a> FnOnce(&'a [u8], &'a Stash) -> Option<Context<'a>>,
82 {
83 Mapping::mk_or_other(data, move |data, stash| {
84 let cx = mk(data, stash)?;
85 Some(Either::B(cx))
86 })
87 }
88
89 fn mk_or_other<F>(data: Mmap, mk: F) -> Option<Mapping>
92 where
93 F: for<'a> FnOnce(&'a [u8], &'a Stash) -> Option<Either<Mapping, Context<'a>>>,
94 {
95 let stash = Stash::new();
96 let cx = match mk(&data, &stash)? {
97 Either::A(mapping) => return Some(mapping),
98 Either::B(cx) => cx,
99 };
100 Some(Mapping {
101 cx: unsafe { core::mem::transmute::<Context<'_>, Context<'static>>(cx) },
104 _map: data,
105 stash,
106 })
107 }
108}
109
110struct Context<'a> {
111 dwarf: addr2line::Context<EndianSlice<'a, Endian>>,
112 object: Object<'a>,
113 package: Option<gimli::DwarfPackage<EndianSlice<'a, Endian>>>,
114}
115
116impl<'data> Context<'data> {
117 fn new(
118 stash: &'data Stash,
119 object: Object<'data>,
120 sup: Option<Object<'data>>,
121 dwp: Option<Object<'data>>,
122 ) -> Option<Context<'data>> {
123 let mut sections = gimli::Dwarf::load(|id| -> Result<_, ()> {
124 if cfg!(not(target_os = "aix")) {
125 let data = object.section(stash, id.name()).unwrap_or(&[]);
126 Ok(EndianSlice::new(data, Endian))
127 } else if let Some(name) = id.xcoff_name() {
128 let data = object.section(stash, name).unwrap_or(&[]);
129 Ok(EndianSlice::new(data, Endian))
130 } else {
131 Ok(EndianSlice::new(&[], Endian))
132 }
133 })
134 .ok()?;
135
136 if let Some(sup) = sup {
137 sections
138 .load_sup(|id| -> Result<_, ()> {
139 let data = sup.section(stash, id.name()).unwrap_or(&[]);
140 Ok(EndianSlice::new(data, Endian))
141 })
142 .ok()?;
143 }
144 let dwarf = addr2line::Context::from_dwarf(sections).ok()?;
145
146 let mut package = None;
147 if let Some(dwp) = dwp {
148 package = Some(
149 gimli::DwarfPackage::load(
150 |id| -> Result<_, gimli::Error> {
151 let data = id
152 .dwo_name()
153 .and_then(|name| dwp.section(stash, name))
154 .unwrap_or(&[]);
155 Ok(EndianSlice::new(data, Endian))
156 },
157 EndianSlice::new(&[], Endian),
158 )
159 .ok()?,
160 );
161 }
162
163 Some(Context {
164 dwarf,
165 object,
166 package,
167 })
168 }
169
170 fn find_frames(
171 &'_ self,
172 stash: &'data Stash,
173 probe: u64,
174 ) -> gimli::Result<addr2line::FrameIter<'_, EndianSlice<'data, Endian>>> {
175 use addr2line::{LookupContinuation, LookupResult};
176
177 let mut l = self.dwarf.find_frames(probe);
178 loop {
179 let (load, continuation) = match l {
180 LookupResult::Output(output) => break output,
181 LookupResult::Load { load, continuation } => (load, continuation),
182 };
183
184 l = continuation.resume(handle_split_dwarf(self.package.as_ref(), stash, load));
185 }
186 }
187}
188
189fn mmap(path: &Path) -> Option<Mmap> {
190 let file = File::open(path).ok()?;
191 let len = file.metadata().ok()?.len().try_into().ok()?;
192 unsafe { Mmap::map(&file, len, 0) }
193}
194
195cfg_if::cfg_if! {
196 if #[cfg(windows)] {
197 mod coff;
198 use self::coff::{handle_split_dwarf, Object};
199 } else if #[cfg(any(target_vendor = "apple"))] {
200 mod macho;
201 use self::macho::{handle_split_dwarf, Object};
202 } else if #[cfg(target_os = "aix")] {
203 mod xcoff;
204 use self::xcoff::{handle_split_dwarf, Object};
205 } else {
206 mod elf;
207 use self::elf::{handle_split_dwarf, Object};
208 }
209}
210
211cfg_if::cfg_if! {
212 if #[cfg(windows)] {
213 mod libs_windows;
214 use libs_windows::native_libraries;
215 } else if #[cfg(target_vendor = "apple")] {
216 mod libs_macos;
217 use libs_macos::native_libraries;
218 } else if #[cfg(target_os = "illumos")] {
219 mod libs_illumos;
220 use libs_illumos::native_libraries;
221 } else if #[cfg(all(
222 any(
223 target_os = "linux",
224 target_os = "fuchsia",
225 target_os = "freebsd",
226 target_os = "hurd",
227 target_os = "openbsd",
228 target_os = "netbsd",
229 target_os = "nto",
230 target_os = "android",
231 ),
232 not(target_env = "uclibc"),
233 ))] {
234 mod libs_dl_iterate_phdr;
235 use libs_dl_iterate_phdr::native_libraries;
236 #[path = "gimli/parse_running_mmaps_unix.rs"]
237 mod parse_running_mmaps;
238 } else if #[cfg(target_env = "libnx")] {
239 mod libs_libnx;
240 use libs_libnx::native_libraries;
241 } else if #[cfg(target_os = "haiku")] {
242 mod libs_haiku;
243 use libs_haiku::native_libraries;
244 } else if #[cfg(target_os = "aix")] {
245 mod libs_aix;
246 use libs_aix::native_libraries;
247 } else {
248 fn native_libraries() -> Vec<Library> {
250 Vec::new()
251 }
252 }
253}
254
255#[derive(Default)]
256struct Cache {
257 libraries: Vec<Library>,
259
260 mappings: Lru<(usize, Mapping), MAPPINGS_CACHE_SIZE>,
270}
271
272struct Library {
273 name: OsString,
274 #[cfg(target_os = "android")]
275 zip_offset: Option<u64>,
289 #[cfg(target_os = "aix")]
290 member_name: OsString,
296 segments: Vec<LibrarySegment>,
298 bias: usize,
304}
305
306struct LibrarySegment {
307 stated_virtual_memory_address: usize,
311 len: usize,
313}
314
315fn create_mapping(lib: &Library) -> Option<Mapping> {
316 cfg_if::cfg_if! {
317 if #[cfg(target_os = "aix")] {
318 Mapping::new(lib.name.as_ref(), &lib.member_name)
319 } else if #[cfg(target_os = "android")] {
320 Mapping::new_android(lib.name.as_ref(), lib.zip_offset)
321 } else {
322 Mapping::new(lib.name.as_ref())
323 }
324 }
325}
326
327#[cfg(target_os = "android")]
332fn extract_zip_path_android(path: &mystd::ffi::OsStr) -> Option<&mystd::ffi::OsStr> {
333 use mystd::os::unix::ffi::OsStrExt;
334
335 path.as_bytes()
336 .windows(2)
337 .enumerate()
338 .find(|(_, chunk)| chunk == b"!/")
339 .map(|(index, _)| mystd::ffi::OsStr::from_bytes(path.as_bytes().split_at(index).0))
340}
341
342pub unsafe fn clear_symbol_cache() {
344 Cache::with_global(|cache| cache.mappings.clear());
345}
346
347impl Cache {
348 fn new() -> Cache {
349 Cache {
350 mappings: Lru::default(),
351 libraries: native_libraries(),
352 }
353 }
354
355 unsafe fn with_global(f: impl FnOnce(&mut Self)) {
357 static mut MAPPINGS_CACHE: Option<Cache> = None;
368
369 #[allow(static_mut_refs)]
371 f(MAPPINGS_CACHE.get_or_insert_with(Cache::new))
372 }
373
374 fn avma_to_svma(&self, addr: *const u8) -> Option<(usize, *const u8)> {
375 self.libraries
376 .iter()
377 .enumerate()
378 .filter_map(|(i, lib)| {
379 if !lib.segments.iter().any(|s| {
391 let svma = s.stated_virtual_memory_address;
392 let start = svma.wrapping_add(lib.bias);
393 let end = start.wrapping_add(s.len);
394 let address = addr as usize;
395 start <= address && address < end
396 }) {
397 return None;
398 }
399
400 let svma = (addr as usize).wrapping_sub(lib.bias);
403 Some((i, svma as *const u8))
404 })
405 .next()
406 }
407
408 fn mapping_for_lib<'a>(&'a mut self, lib: usize) -> Option<(&'a mut Context<'a>, &'a Stash)> {
409 let cache_idx = self.mappings.iter().position(|(lib_id, _)| *lib_id == lib);
410
411 let cache_entry = if let Some(idx) = cache_idx {
412 self.mappings.move_to_front(idx)
413 } else {
414 create_mapping(&self.libraries[lib])
417 .and_then(|mapping| self.mappings.push_front((lib, mapping)))
418 };
419
420 let (_, mapping) = cache_entry?;
421 let cx: &'a mut Context<'static> = &mut mapping.cx;
422 let stash: &'a Stash = &mapping.stash;
423 Some((
426 unsafe { mem::transmute::<&'a mut Context<'static>, &'a mut Context<'a>>(cx) },
427 stash,
428 ))
429 }
430}
431
432pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
433 let addr = what.address_or_ip();
434 let mut call = |sym: Symbol<'_>| {
435 let sym = mem::transmute::<Symbol<'_>, Symbol<'static>>(sym);
439 (cb)(&super::Symbol { inner: sym });
440 };
441
442 Cache::with_global(|cache| {
443 let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::<u8>()) {
444 Some(pair) => pair,
445 None => return,
446 };
447
448 let (cx, stash) = match cache.mapping_for_lib(lib) {
451 Some((cx, stash)) => (cx, stash),
452 None => return,
453 };
454 let mut any_frames = false;
455 if let Ok(mut frames) = cx.find_frames(stash, addr as u64) {
456 while let Ok(Some(frame)) = frames.next() {
457 any_frames = true;
458 let name = match frame.function {
459 Some(f) => Some(f.name.slice()),
460 None => cx.object.search_symtab(addr as u64),
461 };
462 call(Symbol::Frame {
463 addr: addr as *mut c_void,
464 location: frame.location,
465 name,
466 });
467 }
468 }
469 if !any_frames {
470 if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) {
471 if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) {
472 while let Ok(Some(frame)) = frames.next() {
473 any_frames = true;
474 call(Symbol::Frame {
475 addr: addr as *mut c_void,
476 location: frame.location,
477 name: frame.function.map(|f| f.name.slice()),
478 });
479 }
480 }
481 }
482 }
483 if !any_frames {
484 if let Some(name) = cx.object.search_symtab(addr as u64) {
485 call(Symbol::Symtab { name });
486 }
487 }
488 });
489}
490
491pub enum Symbol<'a> {
492 Frame {
495 addr: *mut c_void,
496 location: Option<addr2line::Location<'a>>,
497 name: Option<&'a [u8]>,
498 },
499 Symtab { name: &'a [u8] },
502}
503
504impl Symbol<'_> {
505 pub fn name(&self) -> Option<SymbolName<'_>> {
506 match self {
507 Symbol::Frame { name, .. } => {
508 let name = name.as_ref()?;
509 Some(SymbolName::new(name))
510 }
511 Symbol::Symtab { name, .. } => Some(SymbolName::new(name)),
512 }
513 }
514
515 pub fn addr(&self) -> Option<*mut c_void> {
516 match self {
517 Symbol::Frame { addr, .. } => Some(*addr),
518 Symbol::Symtab { .. } => None,
519 }
520 }
521
522 pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
523 match self {
524 Symbol::Frame { location, .. } => {
525 let file = location.as_ref()?.file?;
526 Some(BytesOrWideString::Bytes(file.as_bytes()))
527 }
528 Symbol::Symtab { .. } => None,
529 }
530 }
531
532 pub fn filename(&self) -> Option<&Path> {
533 match self {
534 Symbol::Frame { location, .. } => {
535 let file = location.as_ref()?.file?;
536 Some(Path::new(file))
537 }
538 Symbol::Symtab { .. } => None,
539 }
540 }
541
542 pub fn lineno(&self) -> Option<u32> {
543 match self {
544 Symbol::Frame { location, .. } => location.as_ref()?.line,
545 Symbol::Symtab { .. } => None,
546 }
547 }
548
549 pub fn colno(&self) -> Option<u32> {
550 match self {
551 Symbol::Frame { location, .. } => location.as_ref()?.column,
552 Symbol::Symtab { .. } => None,
553 }
554 }
555}