]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/backtrace-debuginfo.rs
Add #[rustc_no_mir] to make tests pass with -Z orbit.
[rust.git] / src / test / run-pass / backtrace-debuginfo.rs
1 // Copyright 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 #![feature(rustc_attrs)]
12
13 // We disable tail merging here because it can't preserve debuginfo and thus
14 // potentially breaks the backtraces. Also, subtle changes can decide whether
15 // tail merging suceeds, so the test might work today but fail tomorrow due to a
16 // seemingly completely unrelated change.
17 // Unfortunately, LLVM has no "disable" option for this, so we have to set
18 // "enable" to 0 instead.
19
20 // compile-flags:-g -Cllvm-args=-enable-tail-merge=0
21 // ignore-pretty as this critically relies on line numbers
22
23 use std::io;
24 use std::io::prelude::*;
25 use std::env;
26
27 #[path = "backtrace-debuginfo-aux.rs"] mod aux;
28
29 macro_rules! pos {
30     () => ((file!(), line!()))
31 }
32
33 macro_rules! dump_and_die {
34     ($($pos:expr),*) => ({
35         // FIXME(#18285): we cannot include the current position because
36         // the macro span takes over the last frame's file/line.
37         if cfg!(target_os = "macos") ||
38            cfg!(target_os = "ios") ||
39            cfg!(target_os = "android") ||
40            cfg!(all(target_os = "linux", target_arch = "arm")) ||
41            cfg!(all(windows, target_env = "gnu")) {
42             // skip these platforms as this support isn't implemented yet.
43         } else {
44             dump_filelines(&[$($pos),*]);
45             panic!();
46         }
47     })
48 }
49
50 // we can't use a function as it will alter the backtrace
51 macro_rules! check {
52     ($counter:expr; $($pos:expr),*) => ({
53         if *$counter == 0 {
54             dump_and_die!($($pos),*)
55         } else {
56             *$counter -= 1;
57         }
58     })
59 }
60
61 type Pos = (&'static str, u32);
62
63 // this goes to stdout and each line has to be occurred
64 // in the following backtrace to stderr with a correct order.
65 fn dump_filelines(filelines: &[Pos]) {
66     // Skip top frame for MSVC, because it sees the macro rather than
67     // the containing function.
68     let skip = if cfg!(target_env = "msvc") {1} else {0};
69     for &(file, line) in filelines.iter().rev().skip(skip) {
70         // extract a basename
71         let basename = file.split(&['/', '\\'][..]).last().unwrap();
72         println!("{}:{}", basename, line);
73     }
74 }
75
76 #[inline(never)]
77 #[rustc_no_mir] // FIXME #31005 MIR missing debuginfo currently.
78 fn inner(counter: &mut i32, main_pos: Pos, outer_pos: Pos) {
79     check!(counter; main_pos, outer_pos);
80     check!(counter; main_pos, outer_pos);
81     let inner_pos = pos!(); aux::callback(|aux_pos| {
82         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
83     });
84     let inner_pos = pos!(); aux::callback_inlined(|aux_pos| {
85         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
86     });
87 }
88
89 // LLVM does not yet output the required debug info to support showing inlined
90 // function calls in backtraces when targetting MSVC, so disable inlining in
91 // this case.
92 #[cfg_attr(not(target_env = "msvc"), inline(always))]
93 #[cfg_attr(target_env = "msvc", inline(never))]
94 #[rustc_no_mir] // FIXME #31005 MIR missing debuginfo currently.
95 fn inner_inlined(counter: &mut i32, main_pos: Pos, outer_pos: Pos) {
96     check!(counter; main_pos, outer_pos);
97     check!(counter; main_pos, outer_pos);
98
99     // Again, disable inlining for MSVC.
100     #[cfg_attr(not(target_env = "msvc"), inline(always))]
101     #[cfg_attr(target_env = "msvc", inline(never))]
102     fn inner_further_inlined(counter: &mut i32, main_pos: Pos, outer_pos: Pos, inner_pos: Pos) {
103         check!(counter; main_pos, outer_pos, inner_pos);
104     }
105     inner_further_inlined(counter, main_pos, outer_pos, pos!());
106
107     let inner_pos = pos!(); aux::callback(|aux_pos| {
108         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
109     });
110     let inner_pos = pos!(); aux::callback_inlined(|aux_pos| {
111         check!(counter; main_pos, outer_pos, inner_pos, aux_pos);
112     });
113
114     // this tests a distinction between two independent calls to the inlined function.
115     // (un)fortunately, LLVM somehow merges two consecutive such calls into one node.
116     inner_further_inlined(counter, main_pos, outer_pos, pos!());
117 }
118
119 #[inline(never)]
120 #[rustc_no_mir] // FIXME #31005 MIR missing debuginfo currently.
121 fn outer(mut counter: i32, main_pos: Pos) {
122     inner(&mut counter, main_pos, pos!());
123     inner_inlined(&mut counter, main_pos, pos!());
124 }
125
126 fn check_trace(output: &str, error: &str) {
127     // reverse the position list so we can start with the last item (which was the first line)
128     let mut remaining: Vec<&str> = output.lines().map(|s| s.trim()).rev().collect();
129
130     assert!(error.contains("stack backtrace"), "no backtrace in the error: {}", error);
131     for line in error.lines() {
132         if !remaining.is_empty() && line.contains(remaining.last().unwrap()) {
133             remaining.pop();
134         }
135     }
136     assert!(remaining.is_empty(),
137             "trace does not match position list: {}\n---\n{}", error, output);
138 }
139
140 fn run_test(me: &str) {
141     use std::str;
142     use std::process::Command;
143
144     let mut template = Command::new(me);
145     template.env("RUST_BACKTRACE", "1");
146
147     let mut i = 0;
148     loop {
149         let out = Command::new(me)
150                           .env("RUST_BACKTRACE", "1")
151                           .arg(i.to_string()).output().unwrap();
152         let output = str::from_utf8(&out.stdout).unwrap();
153         let error = str::from_utf8(&out.stderr).unwrap();
154         if out.status.success() {
155             assert!(output.contains("done."), "bad output for successful run: {}", output);
156             break;
157         } else {
158             check_trace(output, error);
159         }
160         i += 1;
161     }
162 }
163
164 #[inline(never)]
165 #[rustc_no_mir] // FIXME #31005 MIR missing debuginfo currently.
166 fn main() {
167     let args: Vec<String> = env::args().collect();
168     if args.len() >= 2 {
169         let case = args[1].parse().unwrap();
170         writeln!(&mut io::stderr(), "test case {}", case).unwrap();
171         outer(case, pos!());
172         println!("done.");
173     } else {
174         run_test(&args[0]);
175     }
176 }
177