]> git.lizzy.rs Git - rust.git/blob - src/test/ui/backtrace-debuginfo.rs
Auto merge of #81574 - tmiasko:p, r=oli-obk
[rust.git] / src / test / ui / backtrace-debuginfo.rs
1 // run-pass
2 // We disable tail merging here because it can't preserve debuginfo and thus
3 // potentially breaks the backtraces. Also, subtle changes can decide whether
4 // tail merging succeeds, so the test might work today but fail tomorrow due to a
5 // seemingly completely unrelated change.
6 // Unfortunately, LLVM has no "disable" option for this, so we have to set
7 // "enable" to 0 instead.
8
9 // compile-flags:-g -Cllvm-args=-enable-tail-merge=0 -Cllvm-args=-opt-bisect-limit=0
10 // compile-flags:-Cforce-frame-pointers=yes
11 // ignore-pretty issue #37195
12 // ignore-emscripten spawning processes is not supported
13 // ignore-sgx no processes
14 // normalize-stderr-test ".*\n" -> ""
15
16 // Note that above `-opt-bisect-limit=0` is used to basically disable
17 // optimizations. It creates tons of output on stderr, hence we normalize
18 // that away entirely.
19
20 use std::env;
21
22 #[path = "backtrace-debuginfo-aux.rs"] mod aux;
23
24 macro_rules! pos {
25     () => ((file!(), line!()))
26 }
27
28 macro_rules! dump_and_die {
29     ($($pos:expr),*) => ({
30         // FIXME(#18285): we cannot include the current position because
31         // the macro span takes over the last frame's file/line.
32         //
33         // You might also be wondering why a major platform,
34         // i686-pc-windows-msvc, is located in here. Some of the saga can be
35         // found on #62897, but the tl;dr; is that it appears that if the
36         // standard library doesn't have debug information or frame pointers,
37         // which it doesn't by default on the test builders, then the stack
38         // walking routines in dbghelp will randomly terminate the stack trace
39         // in libstd without going further. Presumably the addition of frame
40         // pointers and/or debuginfo fixes this since tests always work with
41         // nightly compilers (which have debuginfo). In general though this test
42         // is replicated in rust-lang/backtrace-rs and has extensive coverage
43         // there, even on i686-pc-windows-msvc. We do the best we can in
44         // rust-lang/rust to test it as well, but sometimes we just gotta keep
45         // landing PRs.
46         if cfg!(any(target_os = "android",
47                     all(target_os = "linux", target_arch = "arm"),
48                     all(target_env = "msvc", target_arch = "x86"),
49                     target_os = "freebsd",
50                     target_os = "dragonfly",
51                     target_os = "openbsd")) {
52             // skip these platforms as this support isn't implemented yet.
53         } else {
54             dump_filelines(&[$($pos),*]);
55             panic!();
56         }
57     })
58 }
59
60 // we can't use a function as it will alter the backtrace
61 macro_rules! check {
62     ($counter:expr; $($pos:expr),*) => ({
63         if *$counter == 0 {
64             dump_and_die!($($pos),*)
65         } else {
66             *$counter -= 1;
67         }
68     })
69 }
70
71 type Pos = (&'static str, u32);
72
73 // this goes to stdout and each line has to be occurred
74 // in the following backtrace to stderr with a correct order.
75 fn dump_filelines(filelines: &[Pos]) {
76     for &(file, line) in filelines.iter().rev() {
77         // extract a basename
78         let basename = file.split(&['/', '\\'][..]).last().unwrap();
79         println!("{}:{}", basename, line);
80     }
81 }
82
83 #[inline(never)]
84 fn inner(counter: &mut i32, main_pos: Pos, outer_pos: Pos) {
85     check!(counter; main_pos, outer_pos);
86     check!(counter; main_pos, outer_pos);
87     let inner_pos = pos!(); aux::callback(|aux_pos| {
88         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
89     });
90     let inner_pos = pos!(); aux::callback_inlined(|aux_pos| {
91         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
92     });
93 }
94
95 // We emit the wrong location for the caller here when inlined on MSVC
96 #[cfg_attr(not(target_env = "msvc"), inline(always))]
97 #[cfg_attr(target_env = "msvc", inline(never))]
98 fn inner_inlined(counter: &mut i32, main_pos: Pos, outer_pos: Pos) {
99     check!(counter; main_pos, outer_pos);
100     check!(counter; main_pos, outer_pos);
101
102     // Again, disable inlining for MSVC.
103     #[cfg_attr(not(target_env = "msvc"), inline(always))]
104     #[cfg_attr(target_env = "msvc", inline(never))]
105     fn inner_further_inlined(counter: &mut i32, main_pos: Pos, outer_pos: Pos, inner_pos: Pos) {
106         check!(counter; main_pos, outer_pos, inner_pos);
107     }
108     inner_further_inlined(counter, main_pos, outer_pos, pos!());
109
110     let inner_pos = pos!(); aux::callback(|aux_pos| {
111         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
112     });
113     let inner_pos = pos!(); aux::callback_inlined(|aux_pos| {
114         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
115     });
116
117     // this tests a distinction between two independent calls to the inlined function.
118     // (un)fortunately, LLVM somehow merges two consecutive such calls into one node.
119     inner_further_inlined(counter, main_pos, outer_pos, pos!());
120 }
121
122 #[inline(never)]
123 fn outer(mut counter: i32, main_pos: Pos) {
124     inner(&mut counter, main_pos, pos!());
125     inner_inlined(&mut counter, main_pos, pos!());
126 }
127
128 fn check_trace(output: &str, error: &str) -> Result<(), String> {
129     // reverse the position list so we can start with the last item (which was the first line)
130     let mut remaining: Vec<&str> = output.lines().map(|s| s.trim()).rev().collect();
131
132     if !error.contains("stack backtrace") {
133         return Err(format!("no backtrace found in stderr:\n{}", error))
134     }
135     for line in error.lines() {
136         if !remaining.is_empty() && line.contains(remaining.last().unwrap()) {
137             remaining.pop();
138         }
139     }
140     if !remaining.is_empty() {
141         return Err(format!("trace does not match position list\n\
142             still need to find {:?}\n\n\
143             --- stdout\n{}\n\
144             --- stderr\n{}",
145             remaining, output, error))
146     }
147     Ok(())
148 }
149
150 fn run_test(me: &str) {
151     use std::str;
152     use std::process::Command;
153
154     let mut i = 0;
155     let mut errors = Vec::new();
156     loop {
157         let out = Command::new(me)
158                           .env("RUST_BACKTRACE", "full")
159                           .arg(i.to_string()).output().unwrap();
160         let output = str::from_utf8(&out.stdout).unwrap();
161         let error = str::from_utf8(&out.stderr).unwrap();
162         if out.status.success() {
163             assert!(output.contains("done."), "bad output for successful run: {}", output);
164             break;
165         } else {
166             if let Err(e) = check_trace(output, error) {
167                 errors.push(e);
168             }
169         }
170         i += 1;
171     }
172     if errors.len() > 0 {
173         for error in errors {
174             println!("---------------------------------------");
175             println!("{}", error);
176         }
177
178         panic!("found some errors");
179     }
180 }
181
182 #[inline(never)]
183 fn main() {
184     let args: Vec<String> = env::args().collect();
185     if args.len() >= 2 {
186         let case = args[1].parse().unwrap();
187         eprintln!("test case {}", case);
188         outer(case, pos!());
189         println!("done.");
190     } else {
191         run_test(&args[0]);
192     }
193 }