Skip to content

Commit 0d6f641

Browse files
committed
Port backtrace's line-tables-only test over to rustc
1 parent b3df0d7 commit 0d6f641

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/bootstrap/src/core/build_steps/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,7 @@ impl Step for TestHelpers {
31593159
.opt_level(0)
31603160
.warnings(false)
31613161
.debug(false)
3162+
.flag("-g1")
31623163
.file(builder.src.join("tests/auxiliary/rust_test_helpers.c"))
31633164
.compile("rust_test_helpers");
31643165
}

tests/auxiliary/rust_test_helpers.c

+15
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,18 @@ uint16_t issue_97463_leak_uninit_data(uint32_t a, uint32_t b, uint32_t c) {
427427

428428
return data->b; /* leak data */
429429
}
430+
431+
// Used for testing backtrace line_tables_only
432+
typedef void (*line_tables_only_callback) (void *data);
433+
434+
void line_tables_only_baz(line_tables_only_callback cb, void *data) {
435+
cb(data);
436+
}
437+
438+
void line_tables_only_bar(line_tables_only_callback cb, void *data) {
439+
line_tables_only_baz(cb, data);
440+
}
441+
442+
void line_tables_only_foo(line_tables_only_callback cb, void *data) {
443+
line_tables_only_bar(cb, data);
444+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Test that when debug info only includes line tables that backtrace is still generated
2+
// successfully. This previously failed when compiling with `clang -g1`.
3+
// Part of <https://github.com/rust-lang/rust/issues/122899> porting some backtrace tests to rustc.
4+
// ignore-tidy-linelength
5+
//@ ignore-windows
6+
//@ run-pass
7+
//@ compile-flags: -Cdebuginfo=line-tables-only
8+
#![feature(backtrace_frames)]
9+
10+
use std::backtrace::{self, Backtrace};
11+
use std::ffi::c_void;
12+
use std::ptr::addr_of_mut;
13+
14+
pub type Callback = extern "C" fn(data: *mut c_void);
15+
16+
#[link(name = "rust_test_helpers", kind = "static")]
17+
extern "C" {
18+
pub fn line_tables_only_foo(cb: Callback, data: *mut c_void);
19+
}
20+
21+
extern "C" fn store_backtrace(data: *mut c_void) {
22+
let bt = backtrace::Backtrace::capture();
23+
unsafe { *data.cast::<Option<Backtrace>>() = Some(bt) };
24+
}
25+
26+
fn assert_contains(
27+
backtrace: &Backtrace,
28+
expected_name: &str,
29+
expected_file: &str,
30+
expected_line: u32,
31+
) {
32+
// FIXME(jieyouxu): fix this ugly fragile test when `BacktraceFrame` has accessors like...
33+
// `symbols()`.
34+
let backtrace = format!("{:#?}", backtrace);
35+
eprintln!("{}", backtrace);
36+
assert!(backtrace.contains(expected_name), "backtrace does not contain expected name {}", expected_name);
37+
assert!(backtrace.contains(expected_file), "backtrace does not contain expected file {}", expected_file);
38+
assert!(backtrace.contains(&expected_line.to_string()), "backtrace does not contain expected line {}", expected_line);
39+
}
40+
41+
/// Verifies that when debug info includes only lines tables the generated
42+
/// backtrace is still generated successfully. The test exercises behaviour
43+
/// that failed previously when compiling with clang -g1.
44+
///
45+
/// The test case uses C rather than rust, since at that time when it was
46+
/// written the debug info generated at level 1 in rustc was essentially
47+
/// the same as at level 2.
48+
fn main() {
49+
std::env::set_var("RUST_BACKTRACE", "1");
50+
let mut backtrace: Option<Backtrace> = None;
51+
unsafe { line_tables_only_foo(store_backtrace, addr_of_mut!(backtrace).cast::<c_void>()) };
52+
let backtrace = backtrace.expect("backtrace");
53+
assert_contains(&backtrace, "line_tables_only_foo", "rust_test_helpers.c", 435);
54+
assert_contains(&backtrace, "line_tables_only_bar", "rust_test_helpers.c", 439);
55+
assert_contains(&backtrace, "line_tables_only_baz", "rust_test_helpers.c", 443);
56+
}

0 commit comments

Comments
 (0)