1 // Copyright 2014-2015 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 std::os::raw::c_void;
12 use Frame;
13 
14 struct UnwindFrame {
15     ctx: *mut uw::_Unwind_Context,
16 }
17 
18 impl Frame for UnwindFrame {
ip(&self) -> *mut c_void19     fn ip(&self) -> *mut c_void {
20         let mut ip_before_insn = 0;
21         let mut ip = unsafe {
22             uw::_Unwind_GetIPInfo(self.ctx, &mut ip_before_insn) as *mut c_void
23         };
24         if !ip.is_null() && ip_before_insn == 0 {
25             // this is a non-signaling frame, so `ip` refers to the address
26             // after the calling instruction. account for that.
27             ip = (ip as usize - 1) as *mut _;
28         }
29         return ip
30     }
31 
symbol_address(&self) -> *mut c_void32     fn symbol_address(&self) -> *mut c_void {
33         // dladdr() on osx gets whiny when we use FindEnclosingFunction, and
34         // it appears to work fine without it, so we only use
35         // FindEnclosingFunction on non-osx platforms. In doing so, we get a
36         // slightly more accurate stack trace in the process.
37         //
38         // This is often because panic involves the last instruction of a
39         // function being "call std::rt::begin_unwind", with no ret
40         // instructions after it. This means that the return instruction
41         // pointer points *outside* of the calling function, and by
42         // unwinding it we go back to the original function.
43         if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
44             self.ip()
45         } else {
46             unsafe { uw::_Unwind_FindEnclosingFunction(self.ip()) }
47         }
48     }
49 }
50 
51 #[inline(always)]
trace(mut cb: &mut FnMut(&Frame) -> bool)52 pub fn trace(mut cb: &mut FnMut(&Frame) -> bool) {
53     unsafe {
54         uw::_Unwind_Backtrace(trace_fn, &mut cb as *mut _ as *mut _);
55     }
56 
57     extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
58                        arg: *mut c_void) -> uw::_Unwind_Reason_Code {
59         let cb = unsafe { &mut *(arg as *mut &mut FnMut(&Frame) -> bool) };
60         let cx = UnwindFrame { ctx: ctx };
61 
62         let mut bomb = ::Bomb { enabled: true };
63         let keep_going = cb(&cx);
64         bomb.enabled = false;
65 
66         if keep_going {
67             uw::_URC_NO_REASON
68         } else {
69             uw::_URC_FAILURE
70         }
71     }
72 }
73 
74 /// Unwind library interface used for backtraces
75 ///
76 /// Note that dead code is allowed as here are just bindings
77 /// iOS doesn't use all of them it but adding more
78 /// platform-specific configs pollutes the code too much
79 #[allow(non_camel_case_types)]
80 #[allow(non_snake_case)]
81 #[allow(dead_code)]
82 mod uw {
83     pub use self::_Unwind_Reason_Code::*;
84 
85     use libc;
86     use std::os::raw::{c_int, c_void};
87 
88     #[repr(C)]
89     pub enum _Unwind_Reason_Code {
90         _URC_NO_REASON = 0,
91         _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
92         _URC_FATAL_PHASE2_ERROR = 2,
93         _URC_FATAL_PHASE1_ERROR = 3,
94         _URC_NORMAL_STOP = 4,
95         _URC_END_OF_STACK = 5,
96         _URC_HANDLER_FOUND = 6,
97         _URC_INSTALL_CONTEXT = 7,
98         _URC_CONTINUE_UNWIND = 8,
99         _URC_FAILURE = 9, // used only by ARM EABI
100     }
101 
102     pub enum _Unwind_Context {}
103 
104     pub type _Unwind_Trace_Fn =
105             extern fn(ctx: *mut _Unwind_Context,
106                       arg: *mut c_void) -> _Unwind_Reason_Code;
107 
108     extern {
109         // No native _Unwind_Backtrace on iOS
110         #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
_Unwind_Backtrace(trace: _Unwind_Trace_Fn, trace_argument: *mut c_void) -> _Unwind_Reason_Code111         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
112                                  trace_argument: *mut c_void)
113                     -> _Unwind_Reason_Code;
114 
115         // available since GCC 4.2.0, should be fine for our purpose
116         #[cfg(all(not(all(target_os = "android", target_arch = "arm")),
117                   not(all(target_os = "linux", target_arch = "arm"))))]
_Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int) -> libc::uintptr_t118         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
119                                  ip_before_insn: *mut c_int)
120                     -> libc::uintptr_t;
121 
122         #[cfg(all(not(target_os = "android"),
123                   not(all(target_os = "linux", target_arch = "arm"))))]
_Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void124         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void)
125             -> *mut c_void;
126     }
127 
128     // On android, the function _Unwind_GetIP is a macro, and this is the
129     // expansion of the macro. This is all copy/pasted directly from the
130     // header file with the definition of _Unwind_GetIP.
131     #[cfg(any(all(target_os = "android", target_arch = "arm"),
132               all(target_os = "linux", target_arch = "arm")))]
_Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t133     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
134         #[repr(C)]
135         enum _Unwind_VRS_Result {
136             _UVRSR_OK = 0,
137             _UVRSR_NOT_IMPLEMENTED = 1,
138             _UVRSR_FAILED = 2,
139         }
140         #[repr(C)]
141         enum _Unwind_VRS_RegClass {
142             _UVRSC_CORE = 0,
143             _UVRSC_VFP = 1,
144             _UVRSC_FPA = 2,
145             _UVRSC_WMMXD = 3,
146             _UVRSC_WMMXC = 4,
147         }
148         #[repr(C)]
149         enum _Unwind_VRS_DataRepresentation {
150             _UVRSD_UINT32 = 0,
151             _UVRSD_VFPX = 1,
152             _UVRSD_FPAX = 2,
153             _UVRSD_UINT64 = 3,
154             _UVRSD_FLOAT = 4,
155             _UVRSD_DOUBLE = 5,
156         }
157 
158         type _Unwind_Word = libc::c_uint;
159         extern {
160             fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
161                                klass: _Unwind_VRS_RegClass,
162                                word: _Unwind_Word,
163                                repr: _Unwind_VRS_DataRepresentation,
164                                data: *mut c_void)
165                 -> _Unwind_VRS_Result;
166         }
167 
168         let mut val: _Unwind_Word = 0;
169         let ptr = &mut val as *mut _Unwind_Word;
170         let _ = _Unwind_VRS_Get(ctx, _Unwind_VRS_RegClass::_UVRSC_CORE, 15,
171                                 _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
172                                 ptr as *mut c_void);
173         (val & !1) as libc::uintptr_t
174     }
175 
176     // This function doesn't exist on Android or ARM/Linux, so make it same
177     // to _Unwind_GetIP
178     #[cfg(any(all(target_os = "android", target_arch = "arm"),
179               all(target_os = "linux", target_arch = "arm")))]
_Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int) -> libc::uintptr_t180     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
181                                     ip_before_insn: *mut c_int)
182         -> libc::uintptr_t
183     {
184         *ip_before_insn = 0;
185         _Unwind_GetIP(ctx)
186     }
187 
188     // This function also doesn't exist on Android or ARM/Linux, so make it
189     // a no-op
190     #[cfg(any(target_os = "android",
191               all(target_os = "linux", target_arch = "arm")))]
_Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void192     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void)
193         -> *mut c_void
194     {
195         pc
196     }
197 }
198 
199 
200