Skip to contentSkip to conversation

win32: set threads name

PATCH In review [email protected] 2 participants 3 messages Oct 11, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 win32: set threads name

From: Marc-André Lureau <[email protected]>

As described in:
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022

SetThreadDescription() is available since Windows 10, version 1607 and in some versions only by "Run Time Dynamic Linking". Its declaration is not yet in mingw, so we lookup the function the same way glib does.

Tested with Visual Studio Community 2022 debugger.

---
 util/qemu-thread-win32.c | 55 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
util/qemu-thread-win32.c +53 -2
index a2d5a6e825..b20bfa9c1f 100644
@@ -19,12 +19,39 @@
 
 static bool name_threads;
 
+typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
+                                                 PCWSTR lpThreadDescription);
+static pSetThreadDescription SetThreadDescriptionFunc;
+static HMODULE kernel32_module;
+
+static bool load_set_thread_description(void)
+{
+    static gsize _init_once = 0;
+
+    if (g_once_init_enter(&_init_once)) {
+        kernel32_module = LoadLibrary("kernel32.dll");
+        if (kernel32_module) {
+            SetThreadDescriptionFunc =
+                (pSetThreadDescription)GetProcAddress(kernel32_module,
+                                                      "SetThreadDescription");
+            if (!SetThreadDescriptionFunc) {
+                FreeLibrary(kernel32_module);
+            }
+        }
+        g_once_init_leave(&_init_once, 1);
+    }
+
+    return !!SetThreadDescriptionFunc;
+}
+
 void qemu_thread_naming(bool enable)
 {
-    /* But note we don't actually name them on Windows yet */
     name_threads = enable;
 
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this host\n");
+        name_threads = false;
+    }
 }
 
 static void error_exit(int err, const char *msg)
@@ -400,6 +427,26 @@ void *qemu_thread_join(QemuThread *thread)
     return ret;
 }
 
+static bool
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description()) {
+      return false;
+  }
+
+  namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
+  if (!namew) {
+      return false;
+  }
+
+  hr = SetThreadDescriptionFunc(h, namew);
+
+  return SUCCEEDED(hr);
+}
+
 void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
@@ -423,7 +470,11 @@ void qemu_thread_create(QemuThread *thread, const char *name,
     if (!hThread) {
         error_exit(GetLastError(), __func__);
     }
+    if (name_threads && name && !set_thread_description(hThread, name)) {
+        fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
+    }
     CloseHandle(hThread);
+
     thread->data = data;
 }
 
Signature · 1 line
2.37.3
#1 lore raw
Quoted 65 lines · On Tue, Oct 11, 2022 at 5:29 PM <[email protected]> wrote:

From: Marc-André Lureau <[email protected]>

As described in:
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022

SetThreadDescription() is available since Windows 10, version 1607 and in some versions only by "Run Time Dynamic Linking". Its declaration is not yet in mingw, so we lookup the function the same way glib does.

Tested with Visual Studio Community 2022 debugger.

---
 util/qemu-thread-win32.c | 55 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
util/qemu-thread-win32.c +29 -2
index a2d5a6e825..b20bfa9c1f 100644
@@ -19,12 +19,39 @@
 
 static bool name_threads;
 
+typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
+                                                 PCWSTR lpThreadDescription);
+static pSetThreadDescription SetThreadDescriptionFunc;
+static HMODULE kernel32_module;
+
+static bool load_set_thread_description(void)
+{
+    static gsize _init_once = 0;
+
+    if (g_once_init_enter(&_init_once)) {
+        kernel32_module = LoadLibrary("kernel32.dll");
+        if (kernel32_module) {
+            SetThreadDescriptionFunc =
+                (pSetThreadDescription)GetProcAddress(kernel32_module,
+                                                      "SetThreadDescription");
+            if (!SetThreadDescriptionFunc) {
+                FreeLibrary(kernel32_module);
+            }
+        }
+        g_once_init_leave(&_init_once, 1);
+    }
+
+    return !!SetThreadDescriptionFunc;
+}
+
 void qemu_thread_naming(bool enable)
 {
-    /* But note we don't actually name them on Windows yet */
     name_threads = enable;
 
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this host\n");
+        name_threads = false;
+    }
 }
 
 static void error_exit(int err, const char *msg)

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 430

Quoted 4 lines
util/qemu-thread-win32.c +1 -0
@@ -400,6 +427,26 @@ void *qemu_thread_join(QemuThread *thread)
     return ret;
 }
 
+static bool

This is still not fixed

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 433

Quoted 3 lines
util/qemu-thread-win32.c +3 -0
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;

and the 4 spaces here ...

Quoted 21 lines
util/qemu-thread-win32.c +16 -0
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description()) {
+      return false;
+  }
+
+  namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
+  if (!namew) {
+      return false;
+  }
+
+  hr = SetThreadDescriptionFunc(h, namew);
+
+  return SUCCEEDED(hr);
+}
+
 void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
@@ -423,7 +470,11 @@ void qemu_thread_create(QemuThread *thread, const char *name,
     if (!hThread) {

util/qemu-thread-win32.c void qemu_thread_create(QemuThread *thread, const char *name, · line 480

Quoted 10 lines
util/qemu-thread-win32.c +4 -0
         error_exit(GetLastError(), __func__);
     }
+    if (name_threads && name && !set_thread_description(hThread, name)) {
+        fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
+    }
     CloseHandle(hThread);
+
     thread->data = data;
 }
 

Regards,
Bin

#2 lore raw

Hi

Quoted 77 lines · Bin Meng
Quoted 5 lines · On Tue, Oct 11, 2022 at 5:29 PM <[email protected]> wrote:

From: Marc-André Lureau <[email protected]>

As described in:

https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022

Quoted 18 lines

SetThreadDescription() is available since Windows 10, version 1607 and in some versions only by "Run Time Dynamic Linking". Its declaration is not yet in mingw, so we lookup the function the same way glib does.

Tested with Visual Studio Community 2022 debugger.

---
 util/qemu-thread-win32.c | 55 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
util/qemu-thread-win32.c +0 -0
index a2d5a6e825..b20bfa9c1f 100644

util/qemu-thread-win32.c line 23

Quoted 5 lines
util/qemu-thread-win32.c +2 -0
@@ -19,12 +19,39 @@
 
 static bool name_threads;
 
+typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
+                                                 PCWSTR

lpThreadDescription);

util/qemu-thread-win32.c line 43

Quoted 13 lines
util/qemu-thread-win32.c +13 -0
+static pSetThreadDescription SetThreadDescriptionFunc;
+static HMODULE kernel32_module;
+
+static bool load_set_thread_description(void)
+{
+    static gsize _init_once = 0;
+
+    if (g_once_init_enter(&_init_once)) {
+        kernel32_module = LoadLibrary("kernel32.dll");
+        if (kernel32_module) {
+            SetThreadDescriptionFunc =
+                (pSetThreadDescription)GetProcAddress(kernel32_module,
+

"SetThreadDescription");

Quoted 8 lines
util/qemu-thread-win32.c +8 -0
+            if (!SetThreadDescriptionFunc) {
+                FreeLibrary(kernel32_module);
+            }
+        }
+        g_once_init_leave(&_init_once, 1);
+    }
+
+    return !!SetThreadDescriptionFunc;

util/qemu-thread-win32.c

Quoted 10 lines
util/qemu-thread-win32.c +4 -2
+}
+
 void qemu_thread_naming(bool enable)
 {
-    /* But note we don't actually name them on Windows yet */
     name_threads = enable;
 
-    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    if (enable && !load_set_thread_description()) {
+        fprintf(stderr, "qemu: thread naming not supported on this

host\n");

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 430

Quoted 10 lines
util/qemu-thread-win32.c +3 -0
+        name_threads = false;
+    }
 }
 
 static void error_exit(int err, const char *msg)
@@ -400,6 +427,26 @@ void *qemu_thread_join(QemuThread *thread)
     return ret;
 }
 
+static bool

This is still not fixed

Quoted 6 lines

util/qemu-thread-win32.c void *qemu_thread_join(QemuThread *thread) · line 433

Quoted 3 lines
util/qemu-thread-win32.c +3 -0
+set_thread_description(HANDLE h, const char *name)
+{
+  HRESULT hr;

and the 4 spaces here ...

good catch, thanks

Quoted 40 lines

util/qemu-thread-win32.c void qemu_thread_create(QemuThread *thread, const

Quoted 21 lines
util/qemu-thread-win32.c +16 -0
+  g_autofree wchar_t *namew = NULL;
+
+  if (!load_set_thread_description()) {
+      return false;
+  }
+
+  namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
+  if (!namew) {
+      return false;
+  }
+
+  hr = SetThreadDescriptionFunc(h, namew);
+
+  return SUCCEEDED(hr);
+}
+
 void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
@@ -423,7 +470,11 @@ void qemu_thread_create(QemuThread *thread, const

char *name,

util/qemu-thread-win32.c void qemu_thread_create(QemuThread *thread, const char *name,

Quoted 4 lines
util/qemu-thread-win32.c +1 -0
     if (!hThread) {
         error_exit(GetLastError(), __func__);
     }
+    if (name_threads && name && !set_thread_description(hThread, name))

{

util/qemu-thread-win32.c void qemu_thread_create(QemuThread *thread, const · line 474

Quoted 1 line
util/qemu-thread-win32.c +1 -0
+        fprintf(stderr, "qemu: failed to set thread description: %s\n",

name);

util/qemu-thread-win32.c void qemu_thread_create(QemuThread *thread, const char *name, · line 480

Quoted 6 lines
util/qemu-thread-win32.c +2 -0
+    }
     CloseHandle(hThread);
+
     thread->data = data;
 }
 

Regards,
Bin