Skip to content

Commit 698b9a3

Browse files
committed
Port backtrace's line-tables-only test over to rustc
1 parent 6a92312 commit 698b9a3

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

Diff for: src/bootstrap/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1395,16 +1395,19 @@ impl Build {
13951395
// its caching system since we're executing quite a lot of tests and
13961396
// ideally shouldn't pollute the cache too much.
13971397
if let Some(path) = finder.maybe_have("wasmtime") {
1398-
if let Ok(mut path) = path.into_os_string().into_string() {
1399-
path.push_str(" run -C cache=n --dir .");
1398+
if let Ok(mut cmd) = path.into_os_string().into_string() {
1399+
cmd.push_str(" run -C cache=n --dir .");
14001400
// Make sure that tests have access to RUSTC_BOOTSTRAP. This (for example) is
14011401
// required for libtest to work on beta/stable channels.
14021402
//
14031403
// NB: with Wasmtime 20 this can change to `-S inherit-env` to
14041404
// inherit the entire environment rather than just this single
14051405
// environment variable.
1406-
path.push_str(" --env RUSTC_BOOTSTRAP");
1407-
return Some(path);
1406+
cmd.push_str(" --env RUSTC_BOOTSTRAP");
1407+
// Make sure debug-info is enabled so we can have backtraces with file names and
1408+
// line numbers.
1409+
cmd.push_str(" -D debug-info=y");
1410+
return Some(cmd);
14081411
}
14091412
}
14101413

Diff for: 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+
}

Diff for: tests/ui/debuginfo/backtrace-line-tables-only.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 original test is ignore-windows
6+
//@ ignore-android FIXME #17520
7+
//@ ignore-openbsd no support for libbacktrace without filename
8+
//@ ignore-fuchsia Backtraces not symbolized
9+
//@ needs-unwind
10+
//@ run-pass
11+
//@ compile-flags: -Cdebuginfo=line-tables-only -Cstrip=none
12+
#![feature(backtrace_frames)]
13+
14+
use std::backtrace::{self, Backtrace};
15+
use std::ffi::c_void;
16+
use std::ptr::addr_of_mut;
17+
18+
pub type Callback = extern "C" fn(data: *mut c_void);
19+
20+
#[link(name = "rust_test_helpers", kind = "static")]
21+
extern "C" {
22+
pub fn line_tables_only_foo(cb: Callback, data: *mut c_void);
23+
}
24+
25+
extern "C" fn store_backtrace(data: *mut c_void) {
26+
let bt = backtrace::Backtrace::capture();
27+
unsafe { *data.cast::<Option<Backtrace>>() = Some(bt) };
28+
}
29+
30+
fn assert_contains(
31+
backtrace: &Backtrace,
32+
expected_name: &str,
33+
expected_file: &str,
34+
expected_line: u32,
35+
) {
36+
// FIXME(jieyouxu): fix this ugly fragile test when `BacktraceFrame` has accessors like...
37+
// `symbols()`.
38+
let backtrace = format!("{:#?}", backtrace);
39+
eprintln!("{}", backtrace);
40+
assert!(backtrace.contains(expected_name), "backtrace does not contain expected name {}", expected_name);
41+
assert!(backtrace.contains(expected_file), "backtrace does not contain expected file {}", expected_file);
42+
assert!(backtrace.contains(&expected_line.to_string()), "backtrace does not contain expected line {}", expected_line);
43+
}
44+
45+
/// Verifies that when debug info includes only lines tables the generated
46+
/// backtrace is still generated successfully. The test exercises behaviour
47+
/// that failed previously when compiling with clang -g1.
48+
///
49+
/// The test case uses C rather than rust, since at that time when it was
50+
/// written the debug info generated at level 1 in rustc was essentially
51+
/// the same as at level 2.
52+
fn main() {
53+
std::env::set_var("RUST_BACKTRACE", "1");
54+
std::env::set_var("WASMTIME_BACKTRACE_DETAILS", "1");
55+
let mut backtrace: Option<Backtrace> = None;
56+
unsafe { line_tables_only_foo(store_backtrace, addr_of_mut!(backtrace).cast::<c_void>()) };
57+
let backtrace = backtrace.expect("backtrace");
58+
assert_contains(&backtrace, "line_tables_only_foo", "rust_test_helpers.c", 435);
59+
assert_contains(&backtrace, "line_tables_only_bar", "rust_test_helpers.c", 439);
60+
assert_contains(&backtrace, "line_tables_only_baz", "rust_test_helpers.c", 443);
61+
}

0 commit comments

Comments
 (0)