2 // ignore-android FIXME #17520
3 // ignore-emscripten spawning processes is not supported
4 // ignore-openbsd no support for libbacktrace without filename
5 // ignore-sgx no processes
6 // ignore-msvc see #62897 and `backtrace-debuginfo.rs` test
7 // ignore-fuchsia Backtraces not symbolized
9 // compile-flags:-Cstrip=none
12 use std::process::{Command, Stdio};
17 let _v = vec![1, 2, 3];
18 if env::var_os("IS_TEST").is_some() {
27 impl Drop for Double {
28 fn drop(&mut self) { panic!("twice") }
36 fn template(me: &str) -> Command {
37 let mut m = Command::new(me);
39 .stdout(Stdio::piped())
40 .stderr(Stdio::piped());
44 fn expected(fn_name: &str) -> String {
45 format!(" backtrace::{}", fn_name)
48 #[cfg(not(panic = "abort"))]
49 fn contains_verbose_expected(s: &str, fn_name: &str) -> bool {
50 // HACK(eddyb) work around the fact that verbosely demangled stack traces
51 // (from `RUST_BACKTRACE=full`, or, as is the case here, panic-in-panic)
52 // may contain symbols with hashes in them, i.e. `backtrace[...]::`.
53 let prefix = " backtrace";
54 let suffix = &format!("::{}", fn_name);
55 s.match_indices(prefix).any(|(i, _)| {
57 .trim_start_matches('[')
58 .trim_start_matches(char::is_alphanumeric)
59 .trim_start_matches(']')
64 fn runtest(me: &str) {
65 // Make sure that the stack trace is printed
66 let p = template(me).arg("fail").env("RUST_BACKTRACE", "1").spawn().unwrap();
67 let out = p.wait_with_output().unwrap();
68 assert!(!out.status.success());
69 let s = str::from_utf8(&out.stderr).unwrap();
70 assert!(s.contains("stack backtrace") && s.contains(&expected("foo")),
72 assert!(s.contains(" 0:"), "the frame number should start at 0");
74 // Make sure the stack trace is *not* printed
75 // (Remove RUST_BACKTRACE from our own environment, in case developer
76 // is running `make check` with it on.)
77 let p = template(me).arg("fail").env_remove("RUST_BACKTRACE").spawn().unwrap();
78 let out = p.wait_with_output().unwrap();
79 assert!(!out.status.success());
80 let s = str::from_utf8(&out.stderr).unwrap();
81 assert!(!s.contains("stack backtrace") && !s.contains(&expected("foo")),
82 "bad output2: {}", s);
84 // Make sure the stack trace is *not* printed
85 // (RUST_BACKTRACE=0 acts as if it were unset from our own environment,
86 // in case developer is running `make check` with it set.)
87 let p = template(me).arg("fail").env("RUST_BACKTRACE","0").spawn().unwrap();
88 let out = p.wait_with_output().unwrap();
89 assert!(!out.status.success());
90 let s = str::from_utf8(&out.stderr).unwrap();
91 assert!(!s.contains("stack backtrace") && !s.contains(" - foo"),
92 "bad output3: {}", s);
94 #[cfg(not(panic = "abort"))]
96 // Make sure a stack trace is printed
97 let p = template(me).arg("double-fail").spawn().unwrap();
98 let out = p.wait_with_output().unwrap();
99 assert!(!out.status.success());
100 let s = str::from_utf8(&out.stderr).unwrap();
101 // loosened the following from double::h to double:: due to
102 // spurious failures on mac, 32bit, optimized
103 assert!(s.contains("stack backtrace") && contains_verbose_expected(s, "double"),
104 "bad output3: {}", s);
106 // Make sure a stack trace isn't printed too many times
107 let p = template(me).arg("double-fail")
108 .env("RUST_BACKTRACE", "1").spawn().unwrap();
109 let out = p.wait_with_output().unwrap();
110 assert!(!out.status.success());
111 let s = str::from_utf8(&out.stderr).unwrap();
114 i += s[i + 10..].find("stack backtrace").unwrap() + 10;
116 assert!(s[i + 10..].find("stack backtrace").is_none(),
117 "bad output4: {}", s);
122 let args: Vec<String> = env::args().collect();
123 if args.len() >= 2 && args[1] == "fail" {
125 } else if args.len() >= 2 && args[1] == "double-fail" {