]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/backtrace/tracing/backtrace_fn.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libstd / sys / unix / backtrace / tracing / backtrace_fn.rs
CommitLineData
e9174d1e
SL
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/// As always - iOS on arm uses SjLj exceptions and
12/// _Unwind_Backtrace is even not available there. Still,
13/// backtraces could be extracted using a backtrace function,
14/// which thanks god is public
15///
16/// As mentioned in a huge comment block in `super::super`, backtrace
17/// doesn't play well with green threads, so while it is extremely nice and
18/// simple to use it should be used only on iOS devices as the only viable
19/// option.
20
21use io;
22use io::prelude::*;
23use libc;
24use mem;
25use result::Result::Ok;
26use sync::StaticMutex;
27
28use super::super::printing::print;
29
30#[inline(never)]
31pub fn write(w: &mut Write) -> io::Result<()> {
32 extern {
33 fn backtrace(buf: *mut *mut libc::c_void,
34 sz: libc::c_int) -> libc::c_int;
35 }
36
37 // while it doesn't requires lock for work as everything is
38 // local, it still displays much nicer backtraces when a
39 // couple of threads panic simultaneously
40 static LOCK: StaticMutex = StaticMutex::new();
41 let _g = LOCK.lock();
42
54a0048b 43 writeln!(w, "stack backtrace:")?;
e9174d1e
SL
44 // 100 lines should be enough
45 const SIZE: usize = 100;
46 let mut buf: [*mut libc::c_void; SIZE] = unsafe { mem::zeroed() };
47 let cnt = unsafe { backtrace(buf.as_mut_ptr(), SIZE as libc::c_int) as usize};
48
49 // skipping the first one as it is write itself
50 for i in 1..cnt {
54a0048b 51 print(w, i as isize, buf[i], buf[i])?
e9174d1e
SL
52 }
53 Ok(())
54}