Skip to content

Commit c36c7f0

Browse files
committed
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 '<unnamed>' panicked at src/main.rs:3:5: explicit panic To something like this: thread '<unnamed>' (0xff9bf) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print. Work around stage1 vs. stage2 differences in miri
1 parent 8c6f4dc commit c36c7f0

File tree

120 files changed

+307
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+307
-166
lines changed

library/std/src/panicking.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ fn default_hook(info: &PanicHookInfo<'_>) {
269269

270270
thread::with_current_name(|name| {
271271
let name = name.unwrap_or("<unnamed>");
272+
let tid = thread::current_os_id();
272273

273274
// Try to write the panic message to a buffer first to prevent other concurrent outputs
274275
// interleaving with it.
@@ -277,7 +278,7 @@ fn default_hook(info: &PanicHookInfo<'_>) {
277278

278279
let write_msg = |dst: &mut dyn crate::io::Write| {
279280
// We add a newline to ensure the panic message appears at the start of a line.
280-
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
281+
writeln!(dst, "\nthread '{name}' ({tid}) panicked at {location}:\n{msg}")
281282
};
282283

283284
if write_msg(&mut cursor).is_ok() {

library/std/src/sys/pal/hermit/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ impl Thread {
103103
}
104104
}
105105

106+
pub(crate) fn current_os_id() -> Option<u64> {
107+
None
108+
}
109+
106110
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
107111
unsafe { Ok(NonZero::new_unchecked(hermit_abi::available_parallelism())) }
108112
}

library/std/src/sys/pal/itron/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ unsafe fn terminate_and_delete_current_task() -> ! {
349349
unsafe { crate::hint::unreachable_unchecked() };
350350
}
351351

352+
pub(crate) fn current_os_id() -> Option<u64> {
353+
None
354+
}
355+
352356
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
353357
super::unsupported()
354358
}

library/std/src/sys/pal/sgx/thread.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
22

3-
use super::abi::usercalls;
3+
use super::abi::{thread, usercalls};
44
use super::unsupported;
55
use crate::ffi::CStr;
66
use crate::io;
@@ -137,6 +137,10 @@ impl Thread {
137137
}
138138
}
139139

140+
pub(crate) fn current_os_id() -> Option<u64> {
141+
Some(thread::current().addr().get() as u64)
142+
}
143+
140144
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
141145
unsupported()
142146
}

library/std/src/sys/pal/teeos/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ impl Drop for Thread {
132132
}
133133
}
134134

135+
pub(crate) fn current_os_id() -> Option<u64> {
136+
None
137+
}
138+
135139
// Note: Both `sched_getaffinity` and `sysconf` are available but not functional on
136140
// teeos, so this function always returns an Error!
137141
pub fn available_parallelism() -> io::Result<NonZero<usize>> {

library/std/src/sys/pal/uefi/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl Thread {
4444
}
4545
}
4646

47+
pub(crate) fn current_os_id() -> Option<u64> {
48+
None
49+
}
50+
4751
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
4852
// UEFI is single threaded
4953
Ok(NonZero::new(1).unwrap())

library/std/src/sys/pal/unix/stack_overflow.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ mod imp {
120120
&& thread_info.guard_page_range.contains(&fault_addr)
121121
{
122122
let name = thread_info.thread_name.as_deref().unwrap_or("<unknown>");
123-
rtprintpanic!("\nthread '{name}' has overflowed its stack\n");
123+
let tid = crate::thread::current_os_id();
124+
rtprintpanic!("\nthread '{name}' ({tid}) has overflowed its stack\n");
124125
rtabort!("stack overflow");
125126
}
126127
})
@@ -696,7 +697,8 @@ mod imp {
696697
if code == c::EXCEPTION_STACK_OVERFLOW {
697698
crate::thread::with_current_name(|name| {
698699
let name = name.unwrap_or("<unknown>");
699-
rtprintpanic!("\nthread '{name}' has overflowed its stack\n");
700+
let tid = crate::thread::current_os_id();
701+
rtprintpanic!("\nthread '{name}' ({tid}) has overflowed its stack\n");
700702
});
701703
}
702704
c::EXCEPTION_CONTINUE_SEARCH

library/std/src/sys/pal/unix/thread.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,62 @@ impl Drop for Thread {
318318
}
319319
}
320320

321+
pub(crate) fn current_os_id() -> Option<u64> {
322+
// Most Unix platforms have a way to query an integer ID of the current thread, all with
323+
// slightly different spellings.
324+
//
325+
// The OS thread ID is used rather than `pthread_self` so as to match what will be displayed
326+
// for process inspection (debuggers, trace, `top`, etc.).
327+
cfg_if::cfg_if! {
328+
// Most platforms have a function returning a `pid_t` or int, which is an `i32`.
329+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
330+
use crate::sys::weak::syscall;
331+
332+
// `libc::gettid` is only available on glibc 2.30+, but the syscall is available
333+
// since Linux 2.4.11.
334+
syscall!(fn gettid() -> libc::pid_t;);
335+
336+
// SAFETY: FFI call with no preconditions.
337+
let id: libc::pid_t = unsafe { gettid() };
338+
Some(id as u64)
339+
} else if #[cfg(target_os = "nto")] {
340+
// SAFETY: FFI call with no preconditions.
341+
let id: libc::pid_t = unsafe { libc::gettid() };
342+
Some(id as u64)
343+
} else if #[cfg(target_os = "openbsd")] {
344+
// SAFETY: FFI call with no preconditions.
345+
let id: libc::pid_t = unsafe { libc::getthrid() };
346+
Some(id as u64)
347+
} else if #[cfg(target_os = "freebsd")] {
348+
// SAFETY: FFI call with no preconditions.
349+
let id: libc::c_int = unsafe { libc::pthread_getthreadid_np() };
350+
Some(id as u64)
351+
} else if #[cfg(target_os = "netbsd")] {
352+
// SAFETY: FFI call with no preconditions.
353+
let id: libc::lwpid_t = unsafe { libc::_lwp_self() };
354+
Some(id as u64)
355+
} else if #[cfg(any(target_os = "illumos", target_os = "solaris"))] {
356+
// On Illumos, the `pthread_t` is the same as the OS thread ID.
357+
// SAFETY: FFI call with no preconditions.
358+
let id: libc::pthread_t = unsafe { libc::pthread_self() };
359+
Some(id as u64)
360+
} else if #[cfg(target_vendor = "apple")] {
361+
// Apple allows querying arbitrary thread IDs, `thread=NULL` queries the current thread.
362+
let mut id = 0u64;
363+
// SAFETY: `thread_id` is a valid pointer, no other preconditions.
364+
let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
365+
if status == 0 {
366+
Some(id)
367+
} else {
368+
None
369+
}
370+
} else {
371+
// Other platforms don't have an OS thread ID or don't have a way to access it.
372+
None
373+
}
374+
}
375+
}
376+
321377
#[cfg(any(
322378
target_os = "linux",
323379
target_os = "nto",

library/std/src/sys/pal/unsupported/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl Thread {
3131
}
3232
}
3333

34+
pub(crate) fn current_os_id() -> Option<u64> {
35+
None
36+
}
37+
3438
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
3539
unsupported()
3640
}

library/std/src/sys/pal/wasi/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl Thread {
186186
}
187187
}
188188

189+
pub(crate) fn current_os_id() -> Option<u64> {
190+
None
191+
}
192+
189193
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
190194
cfg_if::cfg_if! {
191195
if #[cfg(target_feature = "atomics")] {

0 commit comments

Comments
 (0)