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 unsafe {
345 Cache::with_global(|cache| cache.mappings.clear());
346 }
347}
348
349impl Cache {
350 fn new() -> Cache {
351 Cache {
352 mappings: Lru::default(),
353 libraries: native_libraries(),
354 }
355 }
356
357 unsafe fn with_global(f: impl FnOnce(&mut Self)) {
359 static mut MAPPINGS_CACHE: Option<Cache> = None;
370
371 unsafe {
372 #[allow(static_mut_refs)]
374 f(MAPPINGS_CACHE.get_or_insert_with(Cache::new))
375 }
376 }
377
378 fn avma_to_svma(&self, addr: *const u8) -> Option<(usize, *const u8)> {
379 self.libraries
380 .iter()
381 .enumerate()
382 .filter_map(|(i, lib)| {
383 if !lib.segments.iter().any(|s| {
395 let svma = s.stated_virtual_memory_address;
396 let start = svma.wrapping_add(lib.bias);
397 let end = start.wrapping_add(s.len);
398 let address = addr as usize;
399 start <= address && address < end
400 }) {
401 return None;
402 }
403
404 let svma = (addr as usize).wrapping_sub(lib.bias);
407 Some((i, svma as *const u8))
408 })
409 .next()
410 }
411
412 fn mapping_for_lib<'a>(&'a mut self, lib: usize) -> Option<(&'a mut Context<'a>, &'a Stash)> {
413 let cache_idx = self.mappings.iter().position(|(lib_id, _)| *lib_id == lib);
414
415 let cache_entry = if let Some(idx) = cache_idx {
416 self.mappings.move_to_front(idx)
417 } else {
418 create_mapping(&self.libraries[lib])
421 .and_then(|mapping| self.mappings.push_front((lib, mapping)))
422 };
423
424 let (_, mapping) = cache_entry?;
425 let cx: &'a mut Context<'static> = &mut mapping.cx;
426 let stash: &'a Stash = &mapping.stash;
427 Some((
430 unsafe { mem::transmute::<&'a mut Context<'static>, &'a mut Context<'a>>(cx) },
431 stash,
432 ))
433 }
434}
435
436pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
437 let addr = what.address_or_ip();
438 let mut call = |sym: Symbol<'_>| {
439 let sym = unsafe { mem::transmute::<Symbol<'_>, Symbol<'static>>(sym) };
444 (cb)(&super::Symbol { inner: sym });
445 };
446
447 unsafe {
448 Cache::with_global(|cache| {
449 let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::<u8>()) {
450 Some(pair) => pair,
451 None => return,
452 };
453
454 let (cx, stash) = match cache.mapping_for_lib(lib) {
457 Some((cx, stash)) => (cx, stash),
458 None => return,
459 };
460 let mut any_frames = false;
461 if let Ok(mut frames) = cx.find_frames(stash, addr as u64) {
462 while let Ok(Some(frame)) = frames.next() {
463 any_frames = true;
464 let name = match frame.function {
465 Some(f) => Some(f.name.slice()),
466 None => cx.object.search_symtab(addr as u64),
467 };
468 call(Symbol::Frame {
469 addr: addr as *mut c_void,
470 location: frame.location,
471 name,
472 });
473 }
474 }
475 if !any_frames {
476 if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) {
477 if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) {
478 while let Ok(Some(frame)) = frames.next() {
479 any_frames = true;
480 call(Symbol::Frame {
481 addr: addr as *mut c_void,
482 location: frame.location,
483 name: frame.function.map(|f| f.name.slice()),
484 });
485 }
486 }
487 }
488 }
489 if !any_frames {
490 if let Some(name) = cx.object.search_symtab(addr as u64) {
491 call(Symbol::Symtab { name });
492 }
493 }
494 });
495 }
496}
497
498pub enum Symbol<'a> {
499 Frame {
502 addr: *mut c_void,
503 location: Option<addr2line::Location<'a>>,
504 name: Option<&'a [u8]>,
505 },
506 Symtab { name: &'a [u8] },
509}
510
511impl Symbol<'_> {
512 pub fn name(&self) -> Option<SymbolName<'_>> {
513 match self {
514 Symbol::Frame { name, .. } => {
515 let name = name.as_ref()?;
516 Some(SymbolName::new(name))
517 }
518 Symbol::Symtab { name, .. } => Some(SymbolName::new(name)),
519 }
520 }
521
522 pub fn addr(&self) -> Option<*mut c_void> {
523 match self {
524 Symbol::Frame { addr, .. } => Some(*addr),
525 Symbol::Symtab { .. } => None,
526 }
527 }
528
529 pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
530 match self {
531 Symbol::Frame { location, .. } => {
532 let file = location.as_ref()?.file?;
533 Some(BytesOrWideString::Bytes(file.as_bytes()))
534 }
535 Symbol::Symtab { .. } => None,
536 }
537 }
538
539 pub fn filename(&self) -> Option<&Path> {
540 match self {
541 Symbol::Frame { location, .. } => {
542 let file = location.as_ref()?.file?;
543 Some(Path::new(file))
544 }
545 Symbol::Symtab { .. } => None,
546 }
547 }
548
549 pub fn lineno(&self) -> Option<u32> {
550 match self {
551 Symbol::Frame { location, .. } => location.as_ref()?.line,
552 Symbol::Symtab { .. } => None,
553 }
554 }
555
556 pub fn colno(&self) -> Option<u32> {
557 match self {
558 Symbol::Frame { location, .. } => location.as_ref()?.column,
559 Symbol::Symtab { .. } => None,
560 }
561 }
562}