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

Print thread ID in panic message #115746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,15 @@ fn default_hook(info: &PanicHookInfo<'_>) {

let msg = payload_as_str(info.payload());
let thread = thread::try_current();
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
let thread_info = thread.as_ref().map(|t| (t.id().as_u64(), t.name().unwrap_or("<unnamed>")));

let write = |err: &mut dyn crate::io::Write| {
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
let _ = match thread_info {
Some((id, name)) => {
writeln!(err, "thread '{name}' ({id}) panicked at {location}:\n{msg}")
}
None => writeln!(err, "thread '<unnamed>' panicked at {location}:\n{msg}"),
};

static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/load-panic-backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ aux-build:test-macros.rs
//@ compile-flags: -Z proc-macro-backtrace
//@ rustc-env:RUST_BACKTRACE=0
//@ normalize-stderr-test "thread '.*' panicked " -> ""
//@ normalize-stderr-test "thread '.*' \(\d+\) panicked " -> ""
//@ normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
//@ needs-unwind proc macro panics to report errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/process/multi-panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ fn check_for_no_backtrace(test: std::process::Output) {
let err = String::from_utf8_lossy(&test.stderr);
let mut it = err.lines();

assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' (")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace"));
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' panicked at")), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' (")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), None);
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/terse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"
//@ ignore-emscripten no threads support
//@ needs-unwind

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/test-attrs/terse.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ foo2 --- FAILED
failures:

---- abc stdout ----
thread 'abc' panicked at $DIR/terse.rs:12:5:
thread 'abc' (tid) panicked at $DIR/terse.rs:13:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- foo stdout ----
thread 'foo' panicked at $DIR/terse.rs:17:5:
thread 'foo' (tid) panicked at $DIR/terse.rs:18:5:
explicit panic

---- foo2 stdout ----
thread 'foo2' panicked at $DIR/terse.rs:22:5:
thread 'foo2' (tid) panicked at $DIR/terse.rs:23:5:
explicit panic


Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/test-panic-abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ normalize-stdout-test "(thread '.*') \(\d+\)" -> "$1 (tid)"

//@ ignore-android #120567
//@ ignore-wasm no panic or subprocess support
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/test-attrs/test-panic-abort.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hello, world
testing123
---- it_fails stderr ----
testing321
thread 'main' panicked at $DIR/test-panic-abort.rs:39:5:
thread 'main' (tid) panicked at $DIR/test-panic-abort.rs:40:5:
assertion `left == right` failed
left: 2
right: 5
Expand Down
Loading