]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/backtrace.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / 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 #![feature(rustc_attrs)]
12
13 // no-pretty-expanded FIXME #15189
14 // ignore-android FIXME #17520
15 // compile-flags:-g
16
17 use std::env;
18 use std::process::{Command, Stdio};
19 use std::str;
20
21 // FIXME #31005 MIR missing debuginfo currently.
22 #[cfg_attr(target_env = "msvc", rustc_no_mir)]
23 #[inline(never)]
24 fn foo() {
25 let _v = vec![1, 2, 3];
26 if env::var_os("IS_TEST").is_some() {
27 panic!()
28 }
29 }
30
31 // FIXME #31005 MIR missing debuginfo currently.
32 #[cfg_attr(target_env = "msvc", rustc_no_mir)]
33 #[inline(never)]
34 fn double() {
35 struct Double;
36
37 impl Drop for Double {
38 fn drop(&mut self) { panic!("twice") }
39 }
40
41 let _d = Double;
42
43 panic!("once");
44 }
45
46 fn template(me: &str) -> Command {
47 let mut m = Command::new(me);
48 m.env("IS_TEST", "1")
49 .stdout(Stdio::piped())
50 .stderr(Stdio::piped());
51 return m;
52 }
53
54 fn expected(fn_name: &str) -> String {
55 // FIXME(#32481)
56 //
57 // On windows, we read the function name from debuginfo using some
58 // system APIs. For whatever reason, these APIs seem to use the
59 // "name" field, which is only the "relative" name, not the full
60 // name with namespace info, so we just see `foo` and not
61 // `backtrace::foo` as we see on linux (which uses the linkage
62 // name).
63 if cfg!(windows) && cfg!(target_env = "msvc") {
64 format!(" - {}", fn_name)
65 } else {
66 format!(" - backtrace::{}", fn_name)
67 }
68 }
69
70 fn runtest(me: &str) {
71 // Make sure that the stack trace is printed
72 let p = template(me).arg("fail").env("RUST_BACKTRACE", "1").spawn().unwrap();
73 let out = p.wait_with_output().unwrap();
74 assert!(!out.status.success());
75 let s = str::from_utf8(&out.stderr).unwrap();
76 assert!(s.contains("stack backtrace") && s.contains(&expected("foo")),
77 "bad output: {}", s);
78
79 // Make sure the stack trace is *not* printed
80 // (Remove RUST_BACKTRACE from our own environment, in case developer
81 // is running `make check` with it on.)
82 let p = template(me).arg("fail").env_remove("RUST_BACKTRACE").spawn().unwrap();
83 let out = p.wait_with_output().unwrap();
84 assert!(!out.status.success());
85 let s = str::from_utf8(&out.stderr).unwrap();
86 assert!(!s.contains("stack backtrace") && !s.contains(&expected("foo")),
87 "bad output2: {}", s);
88
89 // Make sure the stack trace is *not* printed
90 // (RUST_BACKTRACE=0 acts as if it were unset from our own environment,
91 // in case developer is running `make check` with it set.)
92 let p = template(me).arg("fail").env("RUST_BACKTRACE","0").spawn().unwrap();
93 let out = p.wait_with_output().unwrap();
94 assert!(!out.status.success());
95 let s = str::from_utf8(&out.stderr).unwrap();
96 assert!(!s.contains("stack backtrace") && !s.contains(" - foo"),
97 "bad output3: {}", s);
98
99 // Make sure a stack trace is printed
100 let p = template(me).arg("double-fail").spawn().unwrap();
101 let out = p.wait_with_output().unwrap();
102 assert!(!out.status.success());
103 let s = str::from_utf8(&out.stderr).unwrap();
104 // loosened the following from double::h to double:: due to
105 // spurious failures on mac, 32bit, optimized
106 assert!(s.contains("stack backtrace") && s.contains(&expected("double")),
107 "bad output3: {}", s);
108
109 // Make sure a stack trace isn't printed too many times
110 let p = template(me).arg("double-fail")
111 .env("RUST_BACKTRACE", "1").spawn().unwrap();
112 let out = p.wait_with_output().unwrap();
113 assert!(!out.status.success());
114 let s = str::from_utf8(&out.stderr).unwrap();
115 let mut i = 0;
116 for _ in 0..2 {
117 i += s[i + 10..].find("stack backtrace").unwrap() + 10;
118 }
119 assert!(s[i + 10..].find("stack backtrace").is_none(),
120 "bad output4: {}", s);
121 }
122
123 fn main() {
124 if cfg!(windows) && cfg!(target_env = "gnu") {
125 return
126 }
127
128 let args: Vec<String> = env::args().collect();
129 if args.len() >= 2 && args[1] == "fail" {
130 foo();
131 } else if args.len() >= 2 && args[1] == "double-fail" {
132 double();
133 } else {
134 runtest(&args[0]);
135 }
136 }