From 820ffc8d7ad9741a078634a42f14ea373d565467 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 19 May 2022 17:21:20 -0400 Subject: 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. --- src/test/debuginfo/thread-names.rs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/debuginfo/thread-names.rs (limited to 'src/test/debuginfo') diff --git a/src/test/debuginfo/thread-names.rs b/src/test/debuginfo/thread-names.rs new file mode 100644 index 00000000000..f42a7aec3bd --- /dev/null +++ b/src/test/debuginfo/thread-names.rs @@ -0,0 +1,40 @@ +// compile-flags:-g +// We can't set the main thread name on Linux because it renames the process (#97191) +// ignore-linux + +// === GDB TESTS ================================================================================== +// +// gdb-command:run +// +// gdb-command:info threads +// gdb-check: 1 Thread [...] [...] "main" [...] +// gdb-check:* 2 Thread [...] [...] "my new thread" [...] + +// === LLDB TESTS ================================================================================= +// +// lldb-command:run +// +// lldb-command:thread info 1 +// lldb-check:thread #1:[...]name = 'main'[...] +// lldb-command:thread info 2 +// lldb-check:thread #2:[...]name = 'my new thread'[...] + +// === CDB TESTS ================================================================================== +// +// cdb-command:g +// +// cdb-command:~ +// cdb-check: 0 Id: [...] Suspend: 1 Teb: [...] Unfrozen "main" +// cdb-check:. [...] Id: [...] Suspend: 1 Teb: [...] Unfrozen "my new thread" + +use std::thread; + +fn main() { + let handle = thread::Builder::new().name("my new thread".into()).spawn(|| { + zzz(); // #break + }).unwrap(); + + handle.join().unwrap(); +} + +fn zzz() {} -- cgit 1.4.1-3-g733a5