1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 use sys_common::backtrace::Frame;
19 pub struct BacktraceContext;
23 frames: &'a mut [Frame],
27 struct UnwindError(uw::_Unwind_Reason_Code);
29 impl Error for UnwindError {
30 fn description(&self) -> &'static str {
31 "unexpected return value while unwinding"
35 impl ::fmt::Display for UnwindError {
36 fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
37 write!(f, "{}: {:?}", self.description(), self.0)
41 #[inline(never)] // if we know this is a function call, we can skip it when
43 pub fn unwind_backtrace(frames: &mut [Frame]) -> io::Result<(usize, BacktraceContext)> {
44 let mut cx = Context { idx: 0, frames };
46 unsafe { uw::_Unwind_Backtrace(trace_fn, &mut cx as *mut Context as *mut libc::c_void) };
47 // See libunwind:src/unwind/Backtrace.c for the return values.
48 // No, there is no doc.
50 // These return codes seem to be benign and need to be ignored for backtraces
51 // to show up properly on all tested platforms.
52 uw::_URC_END_OF_STACK | uw::_URC_FATAL_PHASE1_ERROR | uw::_URC_FAILURE => {
53 Ok((cx.idx, BacktraceContext))
55 _ => Err(io::Error::new(
57 UnwindError(result_unwind),
62 extern "C" fn trace_fn(
63 ctx: *mut uw::_Unwind_Context,
64 arg: *mut libc::c_void,
65 ) -> uw::_Unwind_Reason_Code {
66 let cx = unsafe { &mut *(arg as *mut Context) };
67 let mut ip_before_insn = 0;
68 let mut ip = unsafe { uw::_Unwind_GetIPInfo(ctx, &mut ip_before_insn) as *mut libc::c_void };
69 if !ip.is_null() && ip_before_insn == 0 {
70 // this is a non-signaling frame, so `ip` refers to the address
71 // after the calling instruction. account for that.
72 ip = (ip as usize - 1) as *mut _;
75 let symaddr = unsafe { uw::_Unwind_FindEnclosingFunction(ip) };
76 if cx.idx < cx.frames.len() {
77 cx.frames[cx.idx] = Frame {
78 symbol_addr: symaddr as *mut u8,
79 exact_position: ip as *mut u8,
88 pub fn foreach_symbol_fileline<F>(_: Frame, _: F, _: &BacktraceContext) -> io::Result<bool>
90 F: FnMut(&[u8], u32) -> io::Result<()>,
92 // No way to obtain this information on CloudABI.
96 pub fn resolve_symname<F>(frame: Frame, callback: F, _: &BacktraceContext) -> io::Result<()>
98 F: FnOnce(Option<&str>) -> io::Result<()>,
101 let mut info: Dl_info = intrinsics::init();
103 if dladdr(frame.exact_position as *mut _, &mut info) == 0 || info.dli_sname.is_null() {
106 CStr::from_ptr(info.dli_sname).to_str().ok()
114 dli_fname: *const libc::c_char,
115 dli_fbase: *mut libc::c_void,
116 dli_sname: *const libc::c_char,
117 dli_saddr: *mut libc::c_void,
121 fn dladdr(addr: *const libc::c_void, info: *mut Dl_info) -> libc::c_int;