]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/backtrace/tracing/gcc_s.rs
Auto merge of #33098 - raphlinus:master, r=alexcrichton
[rust.git] / src / libstd / sys / unix / backtrace / tracing / gcc_s.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 use io;
12 use io::prelude::*;
13 use libc;
14 use mem;
15 use sync::StaticMutex;
16
17 use super::super::printing::print;
18 use unwind as uw;
19
20 #[inline(never)] // if we know this is a function call, we can skip it when
21                  // tracing
22 pub fn write(w: &mut Write) -> io::Result<()> {
23     struct Context<'a> {
24         idx: isize,
25         writer: &'a mut (Write+'a),
26         last_error: Option<io::Error>,
27     }
28
29     // When using libbacktrace, we use some necessary global state, so we
30     // need to prevent more than one thread from entering this block. This
31     // is semi-reasonable in terms of printing anyway, and we know that all
32     // I/O done here is blocking I/O, not green I/O, so we don't have to
33     // worry about this being a native vs green mutex.
34     static LOCK: StaticMutex = StaticMutex::new();
35     let _g = LOCK.lock();
36
37     writeln!(w, "stack backtrace:")?;
38
39     let mut cx = Context { writer: w, last_error: None, idx: 0 };
40     return match unsafe {
41         uw::_Unwind_Backtrace(trace_fn,
42                               &mut cx as *mut Context as *mut libc::c_void)
43     } {
44         uw::_URC_NO_REASON => {
45             match cx.last_error {
46                 Some(err) => Err(err),
47                 None => Ok(())
48             }
49         }
50         _ => Ok(()),
51     };
52
53     extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
54                        arg: *mut libc::c_void) -> uw::_Unwind_Reason_Code {
55         let cx: &mut Context = unsafe { mem::transmute(arg) };
56         let mut ip_before_insn = 0;
57         let mut ip = unsafe {
58             uw::_Unwind_GetIPInfo(ctx, &mut ip_before_insn) as *mut libc::c_void
59         };
60         if !ip.is_null() && ip_before_insn == 0 {
61             // this is a non-signaling frame, so `ip` refers to the address
62             // after the calling instruction. account for that.
63             ip = (ip as usize - 1) as *mut _;
64         }
65
66         // dladdr() on osx gets whiny when we use FindEnclosingFunction, and
67         // it appears to work fine without it, so we only use
68         // FindEnclosingFunction on non-osx platforms. In doing so, we get a
69         // slightly more accurate stack trace in the process.
70         //
71         // This is often because panic involves the last instruction of a
72         // function being "call std::rt::begin_unwind", with no ret
73         // instructions after it. This means that the return instruction
74         // pointer points *outside* of the calling function, and by
75         // unwinding it we go back to the original function.
76         let symaddr = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
77             ip
78         } else {
79             unsafe { uw::_Unwind_FindEnclosingFunction(ip) }
80         };
81
82         // Don't print out the first few frames (they're not user frames)
83         cx.idx += 1;
84         if cx.idx <= 0 { return uw::_URC_NO_REASON }
85         // Don't print ginormous backtraces
86         if cx.idx > 100 {
87             match write!(cx.writer, " ... <frames omitted>\n") {
88                 Ok(()) => {}
89                 Err(e) => { cx.last_error = Some(e); }
90             }
91             return uw::_URC_FAILURE
92         }
93
94         // Once we hit an error, stop trying to print more frames
95         if cx.last_error.is_some() { return uw::_URC_FAILURE }
96
97         match print(cx.writer, cx.idx, ip, symaddr) {
98             Ok(()) => {}
99             Err(e) => { cx.last_error = Some(e); }
100         }
101
102         // keep going
103         uw::_URC_NO_REASON
104     }
105 }