From c322091cc3e7132393b6b9379a9172fea149889b Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Mon, 11 Sep 2023 00:59:31 -0400 Subject: [PATCH] Print thread ID in panic message if thread name is unknown `panic!` does not print any identifying information for threads that are unnamed. However, in many cases, the thread ID can be determined. This changes the panic message from something like this: thread '' panicked at src/main.rs:3:5: explicit panic To something like this: thread '' (1234) panicked at src/main.rs:3:5: explicit panic This change applies to both named and unnamed threads. There is no change for threads where an ID cannot be determined. --- library/std/src/panicking.rs | 9 +++++++-- tests/ui/proc-macro/load-panic-backtrace.rs | 2 +- tests/ui/process/multi-panic.rs | 4 ++-- tests/ui/test-attrs/terse.rs | 1 + tests/ui/test-attrs/terse.run.stdout | 6 +++--- tests/ui/test-attrs/test-panic-abort.rs | 1 + tests/ui/test-attrs/test-panic-abort.run.stdout | 2 +- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index ebd054156951d..cbd3eab9c58a8 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -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(""); + let thread_info = thread.as_ref().map(|t| (t.id().as_u64(), t.name().unwrap_or(""))); 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 '' panicked at {location}:\n{msg}"), + }; static FIRST_PANIC: AtomicBool = AtomicBool::new(true); diff --git a/tests/ui/proc-macro/load-panic-backtrace.rs b/tests/ui/proc-macro/load-panic-backtrace.rs index 56ef4e9e08841..ebecc3a0feb65 100644 --- a/tests/ui/proc-macro/load-panic-backtrace.rs +++ b/tests/ui/proc-macro/load-panic-backtrace.rs @@ -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 diff --git a/tests/ui/process/multi-panic.rs b/tests/ui/process/multi-panic.rs index 02b699036541d..9286d06234b63 100644 --- a/tests/ui/process/multi-panic.rs +++ b/tests/ui/process/multi-panic.rs @@ -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 '' panicked")), Some(true)); + assert_eq!(it.next().map(|l| l.starts_with("thread '' (")), 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); } diff --git a/tests/ui/test-attrs/terse.rs b/tests/ui/test-attrs/terse.rs index ab9d5cc19bd6c..0f385b676e85e 100644 --- a/tests/ui/test-attrs/terse.rs +++ b/tests/ui/test-attrs/terse.rs @@ -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 diff --git a/tests/ui/test-attrs/terse.run.stdout b/tests/ui/test-attrs/terse.run.stdout index 2b361361ae888..2712205d154be 100644 --- a/tests/ui/test-attrs/terse.run.stdout +++ b/tests/ui/test-attrs/terse.run.stdout @@ -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 diff --git a/tests/ui/test-attrs/test-panic-abort.rs b/tests/ui/test-attrs/test-panic-abort.rs index 77efaf05bbc0d..ca4e189df699f 100644 --- a/tests/ui/test-attrs/test-panic-abort.rs +++ b/tests/ui/test-attrs/test-panic-abort.rs @@ -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 diff --git a/tests/ui/test-attrs/test-panic-abort.run.stdout b/tests/ui/test-attrs/test-panic-abort.run.stdout index f5d14e77da963..60c9d1cc16521 100644 --- a/tests/ui/test-attrs/test-panic-abort.run.stdout +++ b/tests/ui/test-attrs/test-panic-abort.run.stdout @@ -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