Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cleaning of the bottom of the backtrace #41815

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/libstd/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ pub use panicking::{begin_panic, begin_panic_fmt, update_panic_count};

#[cfg(not(test))]
#[lang = "start"]
fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize {
use mem;
fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize {
use panic;
use sys;
use sys_common;
Expand All @@ -54,7 +53,9 @@ fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize {
sys::args::init(argc, argv);

// Let's run some code!
let res = panic::catch_unwind(mem::transmute::<_, fn()>(main));
let res = panic::catch_unwind(|| {
::sys_common::backtrace::__rust_begin_short_backtrace(main)
});
sys_common::cleanup();
res.is_err()
};
Expand Down
44 changes: 40 additions & 4 deletions src/libstd/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,47 @@ fn _print(w: &mut Write, format: PrintFormat) -> io::Result<()> {
Ok(())
}

fn filter_frames(_frames: &[Frame],
_format: PrintFormat,
_context: &BacktraceContext) -> (usize, usize)
/// Returns a number of frames to remove at the beginning and at the end of the
/// backtrace, according to the backtrace format.
fn filter_frames(frames: &[Frame],
format: PrintFormat,
context: &BacktraceContext) -> (usize, usize)
{
(0, 0)
if format == PrintFormat::Full {
return (0, 0);
}

let skipped_before = 0;

let skipped_after = frames.len() - frames.iter().position(|frame| {
let mut is_marker = false;
let _ = resolve_symname(*frame, |symname| {
if let Some(mangled_symbol_name) = symname {
// Use grep to find the concerned functions
if mangled_symbol_name.contains("__rust_begin_short_backtrace") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a deterministic symbol name, right? Would it be possible to use == here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes right, that's a good point.

is_marker = true;
}
}
Ok(())
}, context);
is_marker
}).unwrap_or(frames.len());

if skipped_before + skipped_after >= frames.len() {
// Avoid showing completely empty backtraces
return (0, 0);
}

(skipped_before, skipped_after)
}


/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
#[inline(never)]
pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T
where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static
{
f()
}

/// Controls how the backtrace should be formated.
Expand Down
4 changes: 3 additions & 1 deletion src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ impl Builder {
}
unsafe {
thread_info::set(imp::guard::current(), their_thread);
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
::sys_common::backtrace::__rust_begin_short_backtrace(f)
}));
*their_packet.get() = Some(try_result);
}
};
Expand Down
26 changes: 21 additions & 5 deletions src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,12 +1314,16 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd
let testfn = match x.testfn {
DynBenchFn(bench) => {
DynTestFn(Box::new(move |()| {
bench::run_once(|b| bench.run(b))
bench::run_once(|b| {
__rust_begin_short_backtrace(|| bench.run(b))
})
}))
}
StaticBenchFn(benchfn) => {
DynTestFn(Box::new(move |()| {
bench::run_once(|b| benchfn(b))
bench::run_once(|b| {
__rust_begin_short_backtrace(|| benchfn(b))
})
}))
}
f => f,
Expand Down Expand Up @@ -1425,12 +1429,24 @@ pub fn run_test(opts: &TestOpts,
monitor_ch.send((desc, TrMetrics(mm), Vec::new())).unwrap();
return;
}
DynTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture, f),
StaticTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture,
Box::new(move |()| f())),
DynTestFn(f) => {
let cb = move |()| {
__rust_begin_short_backtrace(|| f.call_box(()))
};
run_test_inner(desc, monitor_ch, opts.nocapture, Box::new(cb))
}
StaticTestFn(f) =>
run_test_inner(desc, monitor_ch, opts.nocapture,
Box::new(move |()| __rust_begin_short_backtrace(f))),
}
}

/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
#[inline(never)]
fn __rust_begin_short_backtrace<F: FnOnce()>(f: F) {
f()
}

fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any + Send>>) -> TestResult {
match (&desc.should_panic, task_result) {
(&ShouldPanic::No, Ok(())) |
Expand Down