diff options
author | Руслан Ижбулатов <lrn1986@gmail.com> | 2015-04-07 20:05:45 +0000 |
---|---|---|
committer | Руслан Ижбулатов <lrn1986@gmail.com> | 2016-04-26 10:40:07 +0000 |
commit | e118856430a798bbc529691ad235fd0b0684439d (patch) | |
tree | a6aa76ca49d01b02a2f11d981f5d460d2ac468e1 | |
parent | 99bdfd1bcb921c987b29a0780fa7c50c3155341e (diff) | |
download | glib-e118856430a798bbc529691ad235fd0b0684439d.tar.gz |
Add g_system_thread_set_name() implementation for W32 threads
This works by using semi-documented[1] exception to tell the debugger
that a thread needs to have its name changed.
If this exception is not caught and handled by something, it will crash
the process, so we need to set up our own handler in case there's no
debugger attached or the debugger can't handle this type of exception.
Since SEH is not supported by gcc on i686 (at the moment), we need to use VEH
instead. For completeness the MSVC-oriented code still uses SEH, although
there is no reason why it shouldn't work with the VEH variant used by MinGW.
VEH handler has to be set up somewhere (g_thread_win32_init () works nicely)
and removed once it's not needed (g_thread_win32_process_detach () is added
expressly for that purpose). Note that g_thread_win32_process_detach() is
only called when glib is unloaded by FreeLibrary(), not when glib-using
process is terminating.
This exception is known to work with WinDbg, and adding support for it into
GDB proved to be feasible (GDB patch will be sent upstream, eventually).
[1] https://msdn.microsoft.com/en-us/library/xcb2z8hs%28v=vs.71%29.aspx
https://bugzilla.gnome.org/show_bug.cgi?id=747478
-rw-r--r-- | glib/glib-init.c | 7 | ||||
-rw-r--r-- | glib/glib-init.h | 1 | ||||
-rw-r--r-- | glib/gthread-win32.c | 79 |
3 files changed, 86 insertions, 1 deletions
diff --git a/glib/glib-init.c b/glib/glib-init.c index 33dd4bf8b..5a5c2156f 100644 --- a/glib/glib-init.c +++ b/glib/glib-init.c @@ -273,6 +273,13 @@ DllMain (HINSTANCE hinstDLL, #endif break; + case DLL_PROCESS_DETACH: +#ifdef THREADS_WIN32 + if (lpvReserved == NULL) + g_thread_win32_process_detach (); +#endif + break; + default: /* do nothing */ ; diff --git a/glib/glib-init.h b/glib/glib-init.h index e893d5544..0cab67fdc 100644 --- a/glib/glib-init.h +++ b/glib/glib-init.h @@ -31,6 +31,7 @@ void g_quark_init (void); #ifdef G_OS_WIN32 #include <windows.h> +void g_thread_win32_process_detach (void); void g_thread_win32_thread_detach (void); void g_thread_win32_init (void); void g_clock_win32_init (void); diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c index 275ecc63c..8395acd1b 100644 --- a/glib/gthread-win32.c +++ b/glib/gthread-win32.c @@ -496,10 +496,67 @@ g_system_thread_wait (GRealThread *thread) win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE)); } +#define EXCEPTION_SET_THREAD_NAME ((DWORD) 0x406D1388) + +#ifndef _MSC_VER +static void *SetThreadName_VEH_handle = NULL; + +static LONG __stdcall +SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo) +{ + if (ExceptionInfo->ExceptionRecord != NULL && + ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SET_THREAD_NAME) + return EXCEPTION_CONTINUE_EXECUTION; + + return EXCEPTION_CONTINUE_SEARCH; +} +#endif + +typedef struct _THREADNAME_INFO +{ + DWORD dwType; /* must be 0x1000 */ + LPCSTR szName; /* pointer to name (in user addr space) */ + DWORD dwThreadID; /* thread ID (-1=caller thread) */ + DWORD dwFlags; /* reserved for future use, must be zero */ +} THREADNAME_INFO; + +static void +SetThreadName (DWORD dwThreadID, + LPCSTR szThreadName) +{ + THREADNAME_INFO info; + DWORD infosize; + + info.dwType = 0x1000; + info.szName = szThreadName; + info.dwThreadID = dwThreadID; + info.dwFlags = 0; + + infosize = sizeof (info) / sizeof (DWORD); + +#ifdef _MSC_VER + __try + { + RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } +#else + /* Without a debugger we *must* have an exception handler, + * otherwise raising an exception will crash the process. + */ + if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL)) + return; + + RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info); +#endif +} + void g_system_thread_set_name (const gchar *name) { - /* FIXME: implement */ + SetThreadName ((DWORD) -1, name); } /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */ @@ -988,6 +1045,14 @@ g_thread_win32_init (void) g_thread_xp_init (); InitializeCriticalSection (&g_private_lock); + +#ifndef _MSC_VER + SetThreadName_VEH_handle = AddVectoredExceptionHandler (1, &SetThreadName_VEH); + if (SetThreadName_VEH_handle == NULL) + { + /* This is bad, but what can we do? */ + } +#endif } void @@ -1028,4 +1093,16 @@ g_thread_win32_thread_detach (void) g_thread_impl_vtable.CallThisOnThreadExit (); } +void +g_thread_win32_process_detach (void) +{ +#ifndef _MSC_VER + if (SetThreadName_VEH_handle != NULL) + { + RemoveVectoredExceptionHandler (SetThreadName_VEH_handle); + SetThreadName_VEH_handle = NULL; + } +#endif +} + /* vim:set foldmethod=marker: */ |