]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/cloudabi/backtrace.rs
1b970187558c83603dfd039b4bd69cf71d71fd06
[rust.git] / src / libstd / sys / cloudabi / backtrace.rs
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.
4 //
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.
10
11 use error::Error;
12 use ffi::CStr;
13 use intrinsics;
14 use io;
15 use libc;
16 use sys_common::backtrace::Frame;
17 use unwind as uw;
18
19 pub struct BacktraceContext;
20
21 struct Context<'a> {
22     idx: usize,
23     frames: &'a mut [Frame],
24 }
25
26 #[derive(Debug)]
27 struct UnwindError(uw::_Unwind_Reason_Code);
28
29 impl Error for UnwindError {
30     fn description(&self) -> &'static str {
31         "unexpected return value while unwinding"
32     }
33 }
34
35 impl ::fmt::Display for UnwindError {
36     fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
37         write!(f, "{}: {:?}", self.description(), self.0)
38     }
39 }
40
41 #[inline(never)] // if we know this is a function call, we can skip it when
42                  // tracing
43 pub fn unwind_backtrace(frames: &mut [Frame]) -> io::Result<(usize, BacktraceContext)> {
44     let mut cx = Context { idx: 0, frames };
45     let result_unwind =
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.
49     match result_unwind {
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))
54         }
55         _ => Err(io::Error::new(
56             io::ErrorKind::Other,
57             UnwindError(result_unwind),
58         )),
59     }
60 }
61
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 _;
73     }
74
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,
80             inline_context: 0,
81         };
82         cx.idx += 1;
83     }
84
85     uw::_URC_NO_REASON
86 }
87
88 pub fn foreach_symbol_fileline<F>(_: Frame, _: F, _: &BacktraceContext) -> io::Result<bool>
89 where
90     F: FnMut(&[u8], u32) -> io::Result<()>,
91 {
92     // No way to obtain this information on CloudABI.
93     Ok(false)
94 }
95
96 pub fn resolve_symname<F>(frame: Frame, callback: F, _: &BacktraceContext) -> io::Result<()>
97 where
98     F: FnOnce(Option<&str>) -> io::Result<()>,
99 {
100     unsafe {
101         let mut info: Dl_info = intrinsics::init();
102         let symname =
103             if dladdr(frame.exact_position as *mut _, &mut info) == 0 || info.dli_sname.is_null() {
104                 None
105             } else {
106                 CStr::from_ptr(info.dli_sname).to_str().ok()
107             };
108         callback(symname)
109     }
110 }
111
112 #[repr(C)]
113 struct Dl_info {
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,
118 }
119
120 extern "C" {
121     fn dladdr(addr: *const libc::c_void, info: *mut Dl_info) -> libc::c_int;
122 }