]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/backtrace.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libstd / rt / backtrace.rs
1 // Copyright 2014 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 //! Simple backtrace functionality (to print on panic)
12
13 #![allow(non_camel_case_types)]
14
15 use prelude::v1::*;
16
17 use os;
18 use sync::atomic::{mod, Ordering};
19
20 pub use sys::backtrace::write;
21
22 // For now logging is turned off by default, and this function checks to see
23 // whether the magical environment variable is present to see if it's turned on.
24 pub fn log_enabled() -> bool {
25     static ENABLED: atomic::AtomicInt = atomic::ATOMIC_INT_INIT;
26     match ENABLED.load(Ordering::SeqCst) {
27         1 => return false,
28         2 => return true,
29         _ => {}
30     }
31
32     let val = match os::getenv("RUST_BACKTRACE") {
33         Some(..) => 2,
34         None => 1,
35     };
36     ENABLED.store(val, Ordering::SeqCst);
37     val == 2
38 }
39
40 #[cfg(test)]
41 mod test {
42     use prelude::v1::*;
43     use sys_common;
44     macro_rules! t { ($a:expr, $b:expr) => ({
45         let mut m = Vec::new();
46         sys_common::backtrace::demangle(&mut m, $a).unwrap();
47         assert_eq!(String::from_utf8(m).unwrap(), $b);
48     }) }
49
50     #[test]
51     fn demangle() {
52         t!("test", "test");
53         t!("_ZN4testE", "test");
54         t!("_ZN4test", "_ZN4test");
55         t!("_ZN4test1a2bcE", "test::a::bc");
56     }
57
58     #[test]
59     fn demangle_dollars() {
60         t!("_ZN4$UP$E", "Box");
61         t!("_ZN8$UP$testE", "Boxtest");
62         t!("_ZN8$UP$test4foobE", "Boxtest::foob");
63         t!("_ZN10$u{20}test4foobE", " test::foob");
64     }
65
66     #[test]
67     fn demangle_many_dollars() {
68         t!("_ZN14test$u{20}test4foobE", "test test::foob");
69         t!("_ZN12test$UP$test4foobE", "testBoxtest::foob");
70     }
71
72     #[test]
73     fn demangle_windows() {
74         t!("ZN4testE", "test");
75         t!("ZN14test$u{20}test4foobE", "test test::foob");
76         t!("ZN12test$UP$test4foobE", "testBoxtest::foob");
77     }
78 }