Skip to content

Commit f5f8d0e

Browse files
committed
Make libtest have clean backtraces. Fix some nits.
1 parent 609fee7 commit f5f8d0e

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/libstd/panicking.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,9 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
554554
/// run panic hooks, and then delegate to the actual implementation of panics.
555555
#[inline(never)]
556556
#[cold]
557-
pub(crate) fn rust_panic_with_hook(msg: Box<Any + Send>,
558-
file_line_col: &(&'static str, u32, u32),
559-
entry_point: &usize) -> ! {
557+
fn rust_panic_with_hook(msg: Box<Any + Send>,
558+
file_line_col: &(&'static str, u32, u32),
559+
entry_point: &usize) -> ! {
560560
let (file, line, col) = *file_line_col;
561561

562562
let panics = update_panic_count(1);

src/libstd/rt.rs

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
// Reexport some of our utilities which are expected by other crates.
2727
pub use panicking::{begin_panic, begin_panic_fmt, update_panic_count};
2828

29+
#[cfg(feature = "backtrace")]
30+
pub use sys_common::backtrace::mark_start as mark_backtrace_start;
31+
32+
#[cfg(not(feature = "backtrace"))]
33+
pub fn mark_backtrace_start(f: &mut FnMut()) {
34+
f();
35+
}
36+
2937
#[cfg(not(test))]
3038
#[lang = "start"]
3139
fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize {

src/libstd/sys/unix/backtrace/printing/dladdr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn resolve_symname<F>(frame: Frame,
2222
{
2323
unsafe {
2424
let mut info: Dl_info = intrinsics::init();
25-
let symninfo = if dladdr(frame.exact_position as *mut _, &mut info) == 0 ||
25+
let syminfo = if dladdr(frame.exact_position as *mut _, &mut info) == 0 ||
2626
info.dli_sname.is_null() {
2727
None
2828
} else {

src/libtest/lib.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#![feature(set_stdio)]
4141
#![feature(panic_unwind)]
4242
#![feature(staged_api)]
43+
#![feature(rt)]
4344

4445
extern crate getopts;
4546
extern crate term;
@@ -1331,14 +1332,14 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd
13311332
DynBenchFn(bench) => {
13321333
DynTestFn(Box::new(move || {
13331334
bench::run_once(|b| {
1334-
__rust_begin_short_backtrace(|| bench.run(b))
1335+
begin_short_backtrace(|| bench.run(b))
13351336
})
13361337
}))
13371338
}
13381339
StaticBenchFn(benchfn) => {
13391340
DynTestFn(Box::new(move || {
13401341
bench::run_once(|b| {
1341-
__rust_begin_short_backtrace(|| benchfn(b))
1342+
begin_short_backtrace(|| benchfn(b))
13421343
})
13431344
}))
13441345
}
@@ -1440,23 +1441,26 @@ pub fn run_test(opts: &TestOpts,
14401441
}
14411442
DynTestFn(f) => {
14421443
let cb = move || {
1443-
__rust_begin_short_backtrace(f)
1444+
begin_short_backtrace(f)
14441445
};
14451446
run_test_inner(desc, monitor_ch, opts.nocapture, Box::new(cb))
14461447
}
14471448
StaticTestFn(f) =>
14481449
run_test_inner(desc, monitor_ch, opts.nocapture,
1449-
Box::new(move || __rust_begin_short_backtrace(f))),
1450+
Box::new(move || begin_short_backtrace(f))),
14501451
}
14511452
}
14521453

1453-
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
1454-
#[inline(never)]
1455-
fn __rust_begin_short_backtrace<F: FnOnce()>(f: F) {
1456-
f();
1457-
unsafe {
1458-
asm!("" ::: "memory" : "volatile"); // A dummy statement to prevent tail call optimization
1459-
}
1454+
/// Clean the backtrace by using std::rt::mark_backtrace_start
1455+
#[inline(always)]
1456+
fn begin_short_backtrace<F: FnOnce()>(f: F) {
1457+
let mut f = Some(f);
1458+
let mut r = None;
1459+
std::rt::mark_backtrace_start(&mut || {
1460+
let f = f.take().unwrap();
1461+
r = Some(f());
1462+
});
1463+
r.unwrap()
14601464
}
14611465

14621466
fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any + Send>>) -> TestResult {

0 commit comments

Comments
 (0)