Send thread names to debugger

GDB since commit 24cdb46e [1] can report and use these names.

Add utility function SetThreadName(), which sends a thread name to the
debugger.

Use that:
- to set the default thread name for main thread and newly created pthreads.
- in pthread_setname_np() for user thread names.
- for helper thread names in cygthread::create()
- for helper threads which are created directly with CreateThread.

Note that there can still be anonymous threads, created by system or
injected DLLs.

[1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=24cdb46e9f0a694b4fbc11085e094857f08c0419
This commit is contained in:
Jon Turney
2016-07-28 00:40:23 +01:00
parent fdb7df230d
commit 9e0f9ec7ae
7 changed files with 38 additions and 1 deletions

View File

@@ -1110,3 +1110,29 @@ wmemcpy: \n\
.seh_endproc \n\
");
#endif
/* Signal the thread name to any attached debugger
(See "How to: Set a Thread Name in Native Code"
https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx) */
#define MS_VC_EXCEPTION 0x406D1388
void
SetThreadName(DWORD dwThreadID, const char* threadName)
{
if (!IsDebuggerPresent ())
return;
ULONG_PTR info[] =
{
0x1000, /* type, must be 0x1000 */
(ULONG_PTR) threadName, /* pointer to threadname */
dwThreadID, /* thread ID (+ flags on x86_64) */
#ifdef __X86__
0, /* flags, must be zero */
#endif
};
RaiseException (MS_VC_EXCEPTION, 0, sizeof (info)/sizeof (ULONG_PTR), (ULONG_PTR *) &info);
}