[PULL 13/27] util: set the name for the 'main' thread on Windows

Daniel P. BerrangĂ© <[email protected]> Thu, 5 Mar 2026 17:47:29 +0000
Newsgroups org.nongnu.qemu-rust,org.nongnu.qemu-devel
Message-ID <[email protected]>
The default main thread name is undefined, so use a constructor to
explicitly set it to 'main'. This constructor is marked to run early
as the thread name is intended to be used in error reporting / logs
which may be triggered very early in QEMU execution.

This is only done on Windows platforms, because on Linux (and possibly
other POSIX platforms) changing the main thread name has a side effect
of changing the process name reported by tools like 'ps' which fetch
from the file /proc/self/task/tid/comm, expecting it to be the binary
name.

The subsequent patch will address POSIX platforms in a different way.

Reviewed-by: Richard Henderson <[email protected]>
Reviewed-by: Dr. David Alan Gilbert <[email protected]>
Signed-off-by: Daniel P. BerrangĂ© <[email protected]>
---
 util/qemu-thread-win32.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index 31ec305a73..c330d924af 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
 static pSetThreadDescription SetThreadDescriptionFunc;
 static HMODULE kernel32_module;
 
+static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
+qemu_thread_init(void)
+{
+    qemu_thread_set_name("main");
+}
+
 static bool load_set_thread_description(void)
 {
     static gsize _init_once = 0;
-- 
2.53.0