]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/backtrace-api.rs
19169060038e07d5aee763e8febdd61e9790def5
[rust.git] / tests / run-pass / backtrace-api.rs
1 // normalize-stderr-test ".*/(rust|checkout)/library/" -> "RUSTLIB/"
2 // normalize-stderr-test "RUSTLIB/(.*):\d+:\d+ "-> "RUSTLIB/$1:LL:COL "
3 // normalize-stderr-test "::<.*>" -> ""
4
5 #[inline(never)] fn func_a() -> Box<[*mut ()]> { func_b::<u8>() }
6 #[inline(never)] fn func_b<T>() -> Box<[*mut ()]> { func_c() }
7
8 macro_rules! invoke_func_d {
9     () => { func_d() }
10 }
11
12 #[inline(never)] fn func_c() -> Box<[*mut ()]> { invoke_func_d!() }
13 #[inline(never)] fn func_d() -> Box<[*mut ()]> { unsafe { miri_get_backtrace(0) } }
14
15 fn main() {
16     let mut seen_main = false;
17     let frames = func_a();
18     for frame in frames.into_iter() {
19         let miri_frame = unsafe { miri_resolve_frame(*frame, 0) };
20         let name = String::from_utf8(miri_frame.name.into()).unwrap();
21         let filename = String::from_utf8(miri_frame.filename.into()).unwrap();
22
23         if name == "func_a" {
24             assert_eq!(func_a as *mut (), miri_frame.fn_ptr);
25         }
26
27         // Print every frame to stderr.
28         let out = format!("{}:{}:{} ({})", filename, miri_frame.lineno, miri_frame.colno, name);
29         eprintln!("{}", out);
30         // Print the 'main' frame (and everything before it) to stdout, skipping
31         // the printing of internal (and possibly fragile) libstd frames.
32         if !seen_main {
33             println!("{}", out);
34             seen_main = name == "main";
35         }
36     }
37 }
38
39 // This goes at the bottom of the file so that we can change it
40 // without disturbing line numbers of the functions in the backtrace.
41
42 extern "Rust" {
43     fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
44     fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
45 }
46
47 #[derive(Debug)]
48 #[repr(C)]
49 struct MiriFrame {
50     name: Box<[u8]>,
51     filename: Box<[u8]>,
52     lineno: u32,
53     colno: u32,
54     fn_ptr: *mut (),
55 }
56