summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMarc-André Lureau2022-09-13 19:53:20 +0200
committerMarc-André Lureau2022-10-12 17:21:31 +0200
commit4db99c9d9cb6ab6290f14609e23d079219102742 (patch)
tree8a74ab3f0ae33072917fbe485c1f6680604469fe /util
parentMerge tag 'pull-testing-gdbstub-plugins-gitdm-111022-1' of https://github.com... (diff)
downloadqemu-4db99c9d9cb6ab6290f14609e23d079219102742.tar.gz
qemu-4db99c9d9cb6ab6290f14609e23d079219102742.tar.xz
qemu-4db99c9d9cb6ab6290f14609e23d079219102742.zip
win32: set threads name
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. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Acked-by: Richard Henderson <richard.henderson@linaro.org>
Diffstat (limited to 'util')
-rw-r--r--util/qemu-thread-win32.c54
1 files changed, 52 insertions, 2 deletions
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index a2d5a6e825..b9a467d7db 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -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,25 @@ 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 +469,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;
}