]> git.proxmox.com Git - rustc.git/blob - src/vendor/backtrace/src/backtrace/unix_backtrace.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / vendor / backtrace / src / backtrace / 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 use std::mem;
12 use std::os::raw::{c_void, c_int};
13
14 pub struct Frame {
15 addr: *mut c_void,
16 }
17
18 impl Frame {
19 pub fn ip(&self) -> *mut c_void { self.addr }
20 pub fn symbol_address(&self) -> *mut c_void { self.addr }
21 }
22
23 extern {
24 fn backtrace(buf: *mut *mut c_void, sz: c_int) -> c_int;
25 }
26
27 #[inline(always)]
28 pub fn trace(cb: &mut FnMut(&super::Frame) -> bool) {
29 const SIZE: usize = 100;
30
31 let mut buf: [*mut c_void; SIZE];
32 let cnt;
33 unsafe {
34 buf = mem::zeroed();
35 cnt = backtrace(buf.as_mut_ptr(), SIZE as c_int);
36 }
37
38 for addr in buf[..cnt as usize].iter() {
39 let cx = super::Frame {
40 inner: Frame { addr: *addr },
41 };
42 if !cb(&cx) {
43 return
44 }
45 }
46 }