about summary refs log tree commit diff
path: root/library/std/src/sys/unix/mod.rs
diff options
context:
space:
mode:
authorWesley Wiser <wesleywiser@microsoft.com>2022-05-19 17:21:20 -0400
committerWesley Wiser <wesleywiser@microsoft.com>2022-05-27 10:39:54 -0400
commit820ffc8d7ad9741a078634a42f14ea373d565467 (patch)
tree15296c74ee21f2b411a186b6acff5aa81838668c /library/std/src/sys/unix/mod.rs
parentc0672870491e84362f76ddecd50fa229f9b06dff (diff)
downloadrust-820ffc8d7ad9741a078634a42f14ea373d565467.tar.gz
rust-820ffc8d7ad9741a078634a42f14ea373d565467.zip
Call the OS function to set the main thread's name on program init
Normally, `Thread::spawn` takes care of setting the thread's name, if
one was provided, but since the main thread wasn't created by calling
`Thread::spawn`, we need to call that function in `std::rt::init`.

This is mainly useful for system tools like debuggers and profilers
which might show the thread name to a user. Prior to these changes, gdb
and WinDbg would show all thread names except the main thread's name to
a user. I've validated that this patch resolves the issue for both
debuggers.
Diffstat (limited to 'library/std/src/sys/unix/mod.rs')
-rw-r--r--library/std/src/sys/unix/mod.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs
index 8e909aab7f0..1505878e18c 100644
--- a/library/std/src/sys/unix/mod.rs
+++ b/library/std/src/sys/unix/mod.rs
@@ -1,5 +1,6 @@
 #![allow(missing_docs, nonstandard_style)]
 
+use crate::ffi::CStr;
 use crate::io::ErrorKind;
 
 pub use self::rand::hashmap_random_keys;
@@ -66,6 +67,15 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
     stack_overflow::init();
     args::init(argc, argv);
 
+    // Normally, `thread::spawn` will call `Thread::set_name` but since this thread
+    // already exists, we have to call it ourselves. We only do this on macos
+    // because some unix-like operating systems such as Linux share process-id and
+    // thread-id for the main thread and so renaming the main thread will rename the
+    // process and we only want to enable this on platforms we've tested.
+    if cfg!(target_os = "macos") {
+        thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
+    }
+
     unsafe fn sanitize_standard_fds() {
         #[cfg(not(miri))]
         // The standard fds are always available in Miri.