]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/backtrace.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libstd / sys / unix / backtrace.rs
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 /// Backtrace support built on libgcc with some extra OS-specific support
12 ///
13 /// Some methods of getting a backtrace:
14 ///
15 /// * The backtrace() functions on unix. It turns out this doesn't work very
16 ///   well for green threads on OSX, and the address to symbol portion of it
17 ///   suffers problems that are described below.
18 ///
19 /// * Using libunwind. This is more difficult than it sounds because libunwind
20 ///   isn't installed everywhere by default. It's also a bit of a hefty library,
21 ///   so possibly not the best option. When testing, libunwind was excellent at
22 ///   getting both accurate backtraces and accurate symbols across platforms.
23 ///   This route was not chosen in favor of the next option, however.
24 ///
25 /// * We're already using libgcc_s for exceptions in rust (triggering task
26 ///   unwinding and running destructors on the stack), and it turns out that it
27 ///   conveniently comes with a function that also gives us a backtrace. All of
28 ///   these functions look like _Unwind_*, but it's not quite the full
29 ///   repertoire of the libunwind API. Due to it already being in use, this was
30 ///   the chosen route of getting a backtrace.
31 ///
32 /// After choosing libgcc_s for backtraces, the sad part is that it will only
33 /// give us a stack trace of instruction pointers. Thankfully these instruction
34 /// pointers are accurate (they work for green and native threads), but it's
35 /// then up to us again to figure out how to translate these addresses to
36 /// symbols. As with before, we have a few options. Before, that, a little bit
37 /// of an interlude about symbols. This is my very limited knowledge about
38 /// symbol tables, and this information is likely slightly wrong, but the
39 /// general idea should be correct.
40 ///
41 /// When talking about symbols, it's helpful to know a few things about where
42 /// symbols are located. Some symbols are located in the dynamic symbol table
43 /// of the executable which in theory means that they're available for dynamic
44 /// linking and lookup. Other symbols end up only in the local symbol table of
45 /// the file. This loosely corresponds to pub and priv functions in Rust.
46 ///
47 /// Armed with this knowledge, we know that our solution for address to symbol
48 /// translation will need to consult both the local and dynamic symbol tables.
49 /// With that in mind, here's our options of translating an address to
50 /// a symbol.
51 ///
52 /// * Use dladdr(). The original backtrace()-based idea actually uses dladdr()
53 ///   behind the scenes to translate, and this is why backtrace() was not used.
54 ///   Conveniently, this method works fantastically on OSX. It appears dladdr()
55 ///   uses magic to consult the local symbol table, or we're putting everything
56 ///   in the dynamic symbol table anyway. Regardless, for OSX, this is the
57 ///   method used for translation. It's provided by the system and easy to do.o
58 ///
59 ///   Sadly, all other systems have a dladdr() implementation that does not
60 ///   consult the local symbol table. This means that most functions are blank
61 ///   because they don't have symbols. This means that we need another solution.
62 ///
63 /// * Use unw_get_proc_name(). This is part of the libunwind api (not the
64 ///   libgcc_s version of the libunwind api), but involves taking a dependency
65 ///   to libunwind. We may pursue this route in the future if we bundle
66 ///   libunwind, but libunwind was unwieldy enough that it was not chosen at
67 ///   this time to provide this functionality.
68 ///
69 /// * Shell out to a utility like `readelf`. Crazy though it may sound, it's a
70 ///   semi-reasonable solution. The stdlib already knows how to spawn processes,
71 ///   so in theory it could invoke readelf, parse the output, and consult the
72 ///   local/dynamic symbol tables from there. This ended up not getting chosen
73 ///   due to the craziness of the idea plus the advent of the next option.
74 ///
75 /// * Use `libbacktrace`. It turns out that this is a small library bundled in
76 ///   the gcc repository which provides backtrace and symbol translation
77 ///   functionality. All we really need from it is the backtrace functionality,
78 ///   and we only really need this on everything that's not OSX, so this is the
79 ///   chosen route for now.
80 ///
81 /// In summary, the current situation uses libgcc_s to get a trace of stack
82 /// pointers, and we use dladdr() or libbacktrace to translate these addresses
83 /// to symbols. This is a bit of a hokey implementation as-is, but it works for
84 /// all unix platforms we support right now, so it at least gets the job done.
85
86 use prelude::v1::*;
87
88 use ffi::CStr;
89 use old_io::IoResult;
90 use libc;
91 use mem;
92 use str;
93 use sync::{StaticMutex, MUTEX_INIT};
94
95 use sys_common::backtrace::*;
96
97 /// As always - iOS on arm uses SjLj exceptions and
98 /// _Unwind_Backtrace is even not available there. Still,
99 /// backtraces could be extracted using a backtrace function,
100 /// which thanks god is public
101 ///
102 /// As mentioned in a huge comment block above, backtrace doesn't
103 /// play well with green threads, so while it is extremely nice
104 /// and simple to use it should be used only on iOS devices as the
105 /// only viable option.
106 #[cfg(all(target_os = "ios", target_arch = "arm"))]
107 #[inline(never)]
108 pub fn write(w: &mut Writer) -> IoResult<()> {
109     use result;
110
111     extern {
112         fn backtrace(buf: *mut *mut libc::c_void,
113                      sz: libc::c_int) -> libc::c_int;
114     }
115
116     // while it doesn't requires lock for work as everything is
117     // local, it still displays much nicer backtraces when a
118     // couple of tasks panic simultaneously
119     static LOCK: StaticMutex = MUTEX_INIT;
120     let _g = unsafe { LOCK.lock() };
121
122     try!(writeln!(w, "stack backtrace:"));
123     // 100 lines should be enough
124     const SIZE: uint = 100;
125     let mut buf: [*mut libc::c_void; SIZE] = unsafe {mem::zeroed()};
126     let cnt = unsafe { backtrace(buf.as_mut_ptr(), SIZE as libc::c_int) as uint};
127
128     // skipping the first one as it is write itself
129     let iter = (1..cnt).map(|i| {
130         print(w, i as int, buf[i])
131     });
132     result::fold(iter, (), |_, _| ())
133 }
134
135 #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
136 #[inline(never)] // if we know this is a function call, we can skip it when
137                  // tracing
138 pub fn write(w: &mut Writer) -> IoResult<()> {
139     use old_io::IoError;
140
141     struct Context<'a> {
142         idx: int,
143         writer: &'a mut (Writer+'a),
144         last_error: Option<IoError>,
145     }
146
147     // When using libbacktrace, we use some necessary global state, so we
148     // need to prevent more than one thread from entering this block. This
149     // is semi-reasonable in terms of printing anyway, and we know that all
150     // I/O done here is blocking I/O, not green I/O, so we don't have to
151     // worry about this being a native vs green mutex.
152     static LOCK: StaticMutex = MUTEX_INIT;
153     let _g = unsafe { LOCK.lock() };
154
155     try!(writeln!(w, "stack backtrace:"));
156
157     let mut cx = Context { writer: w, last_error: None, idx: 0 };
158     return match unsafe {
159         uw::_Unwind_Backtrace(trace_fn,
160                               &mut cx as *mut Context as *mut libc::c_void)
161     } {
162         uw::_URC_NO_REASON => {
163             match cx.last_error {
164                 Some(err) => Err(err),
165                 None => Ok(())
166             }
167         }
168         _ => Ok(()),
169     };
170
171     extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
172                        arg: *mut libc::c_void) -> uw::_Unwind_Reason_Code {
173         let cx: &mut Context = unsafe { mem::transmute(arg) };
174         let ip = unsafe { uw::_Unwind_GetIP(ctx) as *mut libc::c_void };
175         // dladdr() on osx gets whiny when we use FindEnclosingFunction, and
176         // it appears to work fine without it, so we only use
177         // FindEnclosingFunction on non-osx platforms. In doing so, we get a
178         // slightly more accurate stack trace in the process.
179         //
180         // This is often because panic involves the last instruction of a
181         // function being "call std::rt::begin_unwind", with no ret
182         // instructions after it. This means that the return instruction
183         // pointer points *outside* of the calling function, and by
184         // unwinding it we go back to the original function.
185         let ip = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
186             ip
187         } else {
188             unsafe { uw::_Unwind_FindEnclosingFunction(ip) }
189         };
190
191         // Don't print out the first few frames (they're not user frames)
192         cx.idx += 1;
193         if cx.idx <= 0 { return uw::_URC_NO_REASON }
194         // Don't print ginormous backtraces
195         if cx.idx > 100 {
196             match write!(cx.writer, " ... <frames omitted>\n") {
197                 Ok(()) => {}
198                 Err(e) => { cx.last_error = Some(e); }
199             }
200             return uw::_URC_FAILURE
201         }
202
203         // Once we hit an error, stop trying to print more frames
204         if cx.last_error.is_some() { return uw::_URC_FAILURE }
205
206         match print(cx.writer, cx.idx, ip) {
207             Ok(()) => {}
208             Err(e) => { cx.last_error = Some(e); }
209         }
210
211         // keep going
212         return uw::_URC_NO_REASON
213     }
214 }
215
216 #[cfg(any(target_os = "macos", target_os = "ios"))]
217 fn print(w: &mut Writer, idx: int, addr: *mut libc::c_void) -> IoResult<()> {
218     use intrinsics;
219     #[repr(C)]
220     struct Dl_info {
221         dli_fname: *const libc::c_char,
222         dli_fbase: *mut libc::c_void,
223         dli_sname: *const libc::c_char,
224         dli_saddr: *mut libc::c_void,
225     }
226     extern {
227         fn dladdr(addr: *const libc::c_void,
228                   info: *mut Dl_info) -> libc::c_int;
229     }
230
231     let mut info: Dl_info = unsafe { intrinsics::init() };
232     if unsafe { dladdr(addr, &mut info) == 0 } {
233         output(w, idx,addr, None)
234     } else {
235         output(w, idx, addr, Some(unsafe {
236             CStr::from_ptr(info.dli_sname).to_bytes()
237         }))
238     }
239 }
240
241 #[cfg(not(any(target_os = "macos", target_os = "ios")))]
242 fn print(w: &mut Writer, idx: int, addr: *mut libc::c_void) -> IoResult<()> {
243     use env;
244     use ptr;
245
246     ////////////////////////////////////////////////////////////////////////
247     // libbacktrace.h API
248     ////////////////////////////////////////////////////////////////////////
249     type backtrace_syminfo_callback =
250         extern "C" fn(data: *mut libc::c_void,
251                       pc: libc::uintptr_t,
252                       symname: *const libc::c_char,
253                       symval: libc::uintptr_t,
254                       symsize: libc::uintptr_t);
255     type backtrace_error_callback =
256         extern "C" fn(data: *mut libc::c_void,
257                       msg: *const libc::c_char,
258                       errnum: libc::c_int);
259     enum backtrace_state {}
260     #[link(name = "backtrace", kind = "static")]
261     #[cfg(not(test))]
262     extern {}
263
264     extern {
265         fn backtrace_create_state(filename: *const libc::c_char,
266                                   threaded: libc::c_int,
267                                   error: backtrace_error_callback,
268                                   data: *mut libc::c_void)
269                                         -> *mut backtrace_state;
270         fn backtrace_syminfo(state: *mut backtrace_state,
271                              addr: libc::uintptr_t,
272                              cb: backtrace_syminfo_callback,
273                              error: backtrace_error_callback,
274                              data: *mut libc::c_void) -> libc::c_int;
275     }
276
277     ////////////////////////////////////////////////////////////////////////
278     // helper callbacks
279     ////////////////////////////////////////////////////////////////////////
280
281     extern fn error_cb(_data: *mut libc::c_void, _msg: *const libc::c_char,
282                        _errnum: libc::c_int) {
283         // do nothing for now
284     }
285     extern fn syminfo_cb(data: *mut libc::c_void,
286                          _pc: libc::uintptr_t,
287                          symname: *const libc::c_char,
288                          _symval: libc::uintptr_t,
289                          _symsize: libc::uintptr_t) {
290         let slot = data as *mut *const libc::c_char;
291         unsafe { *slot = symname; }
292     }
293
294     // The libbacktrace API supports creating a state, but it does not
295     // support destroying a state. I personally take this to mean that a
296     // state is meant to be created and then live forever.
297     //
298     // I would love to register an at_exit() handler which cleans up this
299     // state, but libbacktrace provides no way to do so.
300     //
301     // With these constraints, this function has a statically cached state
302     // that is calculated the first time this is requested. Remember that
303     // backtracing all happens serially (one global lock).
304     //
305     // An additionally oddity in this function is that we initialize the
306     // filename via self_exe_name() to pass to libbacktrace. It turns out
307     // that on Linux libbacktrace seamlessly gets the filename of the
308     // current executable, but this fails on freebsd. by always providing
309     // it, we make sure that libbacktrace never has a reason to not look up
310     // the symbols. The libbacktrace API also states that the filename must
311     // be in "permanent memory", so we copy it to a static and then use the
312     // static as the pointer.
313     //
314     // FIXME: We also call self_exe_name() on DragonFly BSD. I haven't
315     //        tested if this is required or not.
316     unsafe fn init_state() -> *mut backtrace_state {
317         static mut STATE: *mut backtrace_state = 0 as *mut backtrace_state;
318         static mut LAST_FILENAME: [libc::c_char; 256] = [0; 256];
319         if !STATE.is_null() { return STATE }
320         let selfname = if cfg!(target_os = "freebsd") ||
321                           cfg!(target_os = "dragonfly") ||
322                           cfg!(target_os = "openbsd") {
323             env::current_exe().ok()
324         } else {
325             None
326         };
327         let filename = match selfname {
328             Some(path) => {
329                 let bytes = path.as_vec();
330                 if bytes.len() < LAST_FILENAME.len() {
331                     let i = bytes.iter();
332                     for (slot, val) in LAST_FILENAME.iter_mut().zip(i) {
333                         *slot = *val as libc::c_char;
334                     }
335                     LAST_FILENAME.as_ptr()
336                 } else {
337                     ptr::null()
338                 }
339             }
340             None => ptr::null(),
341         };
342         STATE = backtrace_create_state(filename, 0, error_cb,
343                                        ptr::null_mut());
344         return STATE
345     }
346
347     ////////////////////////////////////////////////////////////////////////
348     // translation
349     ////////////////////////////////////////////////////////////////////////
350
351     // backtrace errors are currently swept under the rug, only I/O
352     // errors are reported
353     let state = unsafe { init_state() };
354     if state.is_null() {
355         return output(w, idx, addr, None)
356     }
357     let mut data = ptr::null();
358     let data_addr = &mut data as *mut *const libc::c_char;
359     let ret = unsafe {
360         backtrace_syminfo(state, addr as libc::uintptr_t,
361                           syminfo_cb, error_cb,
362                           data_addr as *mut libc::c_void)
363     };
364     if ret == 0 || data.is_null() {
365         output(w, idx, addr, None)
366     } else {
367         output(w, idx, addr, Some(unsafe { CStr::from_ptr(data).to_bytes() }))
368     }
369 }
370
371 // Finally, after all that work above, we can emit a symbol.
372 fn output(w: &mut Writer, idx: int, addr: *mut libc::c_void,
373           s: Option<&[u8]>) -> IoResult<()> {
374     try!(write!(w, "  {:2}: {:2$?} - ", idx, addr, HEX_WIDTH));
375     match s.and_then(|s| str::from_utf8(s).ok()) {
376         Some(string) => try!(demangle(w, string)),
377         None => try!(write!(w, "<unknown>")),
378     }
379     w.write_all(&['\n' as u8])
380 }
381
382 /// Unwind library interface used for backtraces
383 ///
384 /// Note that dead code is allowed as here are just bindings
385 /// iOS doesn't use all of them it but adding more
386 /// platform-specific configs pollutes the code too much
387 #[allow(non_camel_case_types)]
388 #[allow(non_snake_case)]
389 #[allow(dead_code)]
390 mod uw {
391     pub use self::_Unwind_Reason_Code::*;
392
393     use libc;
394
395     #[repr(C)]
396     pub enum _Unwind_Reason_Code {
397         _URC_NO_REASON = 0,
398         _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
399         _URC_FATAL_PHASE2_ERROR = 2,
400         _URC_FATAL_PHASE1_ERROR = 3,
401         _URC_NORMAL_STOP = 4,
402         _URC_END_OF_STACK = 5,
403         _URC_HANDLER_FOUND = 6,
404         _URC_INSTALL_CONTEXT = 7,
405         _URC_CONTINUE_UNWIND = 8,
406         _URC_FAILURE = 9, // used only by ARM EABI
407     }
408
409     pub enum _Unwind_Context {}
410
411     pub type _Unwind_Trace_Fn =
412             extern fn(ctx: *mut _Unwind_Context,
413                       arg: *mut libc::c_void) -> _Unwind_Reason_Code;
414
415     extern {
416         // No native _Unwind_Backtrace on iOS
417         #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
418         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
419                                  trace_argument: *mut libc::c_void)
420                     -> _Unwind_Reason_Code;
421
422         #[cfg(all(not(all(target_os = "android", target_arch = "arm")),
423                   not(all(target_os = "linux", target_arch = "arm"))))]
424         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t;
425
426         #[cfg(all(not(target_os = "android"),
427                   not(all(target_os = "linux", target_arch = "arm"))))]
428         pub fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void)
429             -> *mut libc::c_void;
430     }
431
432     // On android, the function _Unwind_GetIP is a macro, and this is the
433     // expansion of the macro. This is all copy/pasted directly from the
434     // header file with the definition of _Unwind_GetIP.
435     #[cfg(any(all(target_os = "android", target_arch = "arm"),
436               all(target_os = "linux", target_arch = "arm")))]
437     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
438         #[repr(C)]
439         enum _Unwind_VRS_Result {
440             _UVRSR_OK = 0,
441             _UVRSR_NOT_IMPLEMENTED = 1,
442             _UVRSR_FAILED = 2,
443         }
444         #[repr(C)]
445         enum _Unwind_VRS_RegClass {
446             _UVRSC_CORE = 0,
447             _UVRSC_VFP = 1,
448             _UVRSC_FPA = 2,
449             _UVRSC_WMMXD = 3,
450             _UVRSC_WMMXC = 4,
451         }
452         #[repr(C)]
453         enum _Unwind_VRS_DataRepresentation {
454             _UVRSD_UINT32 = 0,
455             _UVRSD_VFPX = 1,
456             _UVRSD_FPAX = 2,
457             _UVRSD_UINT64 = 3,
458             _UVRSD_FLOAT = 4,
459             _UVRSD_DOUBLE = 5,
460         }
461
462         type _Unwind_Word = libc::c_uint;
463         extern {
464             fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
465                                klass: _Unwind_VRS_RegClass,
466                                word: _Unwind_Word,
467                                repr: _Unwind_VRS_DataRepresentation,
468                                data: *mut libc::c_void)
469                 -> _Unwind_VRS_Result;
470         }
471
472         let mut val: _Unwind_Word = 0;
473         let ptr = &mut val as *mut _Unwind_Word;
474         let _ = _Unwind_VRS_Get(ctx, _Unwind_VRS_RegClass::_UVRSC_CORE, 15,
475                                 _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
476                                 ptr as *mut libc::c_void);
477         (val & !1) as libc::uintptr_t
478     }
479
480     // This function also doesn't exist on Android or ARM/Linux, so make it
481     // a no-op
482     #[cfg(any(target_os = "android",
483               all(target_os = "linux", target_arch = "arm")))]
484     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void)
485         -> *mut libc::c_void
486     {
487         pc
488     }
489 }