Skip to content

Commit 1fae81d

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.
1 parent 264b9c8 commit 1fae81d

File tree

122 files changed

+311
-168
lines changed

Some content is hidden

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

122 files changed

+311
-168
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
@@ -111,6 +111,10 @@ impl Thread {
111111
}
112112
}
113113

114+
pub(crate) fn current_os_id() -> Option<u64> {
115+
None
116+
}
117+
114118
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
115119
unsafe { Ok(NonZero::new_unchecked(hermit_abi::available_parallelism())) }
116120
}

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

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

360+
pub(crate) fn current_os_id() -> Option<u64> {
361+
None
362+
}
363+
360364
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
361365
super::unsupported()
362366
}

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;
@@ -145,6 +145,10 @@ impl Thread {
145145
}
146146
}
147147

148+
pub(crate) fn current_os_id() -> Option<u64> {
149+
Some(thread::current().addr().get() as u64)
150+
}
151+
148152
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
149153
unsupported()
150154
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ impl Drop for Thread {
140140
}
141141
}
142142

143+
pub(crate) fn current_os_id() -> Option<u64> {
144+
None
145+
}
146+
143147
// Note: Both `sched_getaffinity` and `sysconf` are available but not functional on
144148
// teeos, so this function always returns an Error!
145149
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
@@ -52,6 +52,10 @@ impl Thread {
5252
}
5353
}
5454

55+
pub(crate) fn current_os_id() -> Option<u64> {
56+
None
57+
}
58+
5559
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
5660
// UEFI is single threaded
5761
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
@@ -388,6 +388,62 @@ impl Drop for Thread {
388388
}
389389
}
390390

391+
pub(crate) fn current_os_id() -> Option<u64> {
392+
// Most Unix platforms have a way to query an integer ID of the current thread, all with
393+
// slightly different spellings.
394+
//
395+
// The OS thread ID is used rather than `pthread_self` so as to match what will be displayed
396+
// for process inspection (debuggers, trace, `top`, etc.).
397+
cfg_if::cfg_if! {
398+
// Most platforms have a function returning a `pid_t` or int, which is an `i32`.
399+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
400+
use crate::sys::weak::syscall;
401+
402+
// `libc::gettid` is only available on glibc 2.30+, but the syscall is available
403+
// since Linux 2.4.11.
404+
syscall!(fn gettid() -> libc::pid_t;);
405+
406+
// SAFETY: FFI call with no preconditions.
407+
let id: libc::pid_t = unsafe { gettid() };
408+
Some(id as u64)
409+
} else if #[cfg(target_os = "nto")] {
410+
// SAFETY: FFI call with no preconditions.
411+
let id: libc::pid_t = unsafe { libc::gettid() };
412+
Some(id as u64)
413+
} else if #[cfg(target_os = "openbsd")] {
414+
// SAFETY: FFI call with no preconditions.
415+
let id: libc::pid_t = unsafe { libc::getthrid() };
416+
Some(id as u64)
417+
} else if #[cfg(target_os = "freebsd")] {
418+
// SAFETY: FFI call with no preconditions.
419+
let id: libc::c_int = unsafe { libc::pthread_getthreadid_np() };
420+
Some(id as u64)
421+
} else if #[cfg(target_os = "netbsd")] {
422+
// SAFETY: FFI call with no preconditions.
423+
let id: libc::lwpid_t = unsafe { libc::_lwp_self() };
424+
Some(id as u64)
425+
} else if #[cfg(any(target_os = "illumos", target_os = "solaris"))] {
426+
// On Illumos and Solaris, the `pthread_t` is the same as the OS thread ID.
427+
// SAFETY: FFI call with no preconditions.
428+
let id: libc::pthread_t = unsafe { libc::pthread_self() };
429+
Some(id as u64)
430+
} else if #[cfg(target_vendor = "apple")] {
431+
// Apple allows querying arbitrary thread IDs, `thread=NULL` queries the current thread.
432+
let mut id = 0u64;
433+
// SAFETY: `thread_id` is a valid pointer, no other preconditions.
434+
let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
435+
if status == 0 {
436+
Some(id)
437+
} else {
438+
None
439+
}
440+
} else {
441+
// Other platforms don't have an OS thread ID or don't have a way to access it.
442+
None
443+
}
444+
}
445+
}
446+
391447
#[cfg(any(
392448
target_os = "linux",
393449
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
@@ -35,6 +35,10 @@ impl Thread {
3535
}
3636
}
3737

38+
pub(crate) fn current_os_id() -> Option<u64> {
39+
None
40+
}
41+
3842
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
3943
unsupported()
4044
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ impl Thread {
194194
}
195195
}
196196

197+
pub(crate) fn current_os_id() -> Option<u64> {
198+
None
199+
}
200+
197201
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
198202
cfg_if::cfg_if! {
199203
if #[cfg(target_feature = "atomics")] {

0 commit comments

Comments
 (0)