* [PATCH] Cygwin: Set threadnames with SetThreadDescription()
@ 2022-05-29 14:03 Jon Turney
2022-05-30 15:37 ` Ken Brown
0 siblings, 1 reply; 3+ messages in thread
From: Jon Turney @ 2022-05-29 14:03 UTC (permalink / raw)
To: cygwin-patches; +Cc: Jon Turney
gdb master recently learnt how to use GetThreadDescription() [1], so set
threadnames using SetThreadDescription() [available since Windows
101607] as well.
This is superior to using a special exception to indicate the thread
name to the debugger, because the thread name isn't missed if you don't
have a debugger attached at the time it's set.
It's not clear what the encoding of a thread name string is, we assume
UTF8 for the moment.
For the moment, continue to use the old method as well, for the benefit
of older gdb versions etc.
[1] https://sourceware.org/pipermail/gdb-patches/2022-April/187833.html
---
winsup/cygwin/autoload.cc | 1 +
winsup/cygwin/miscfuncs.cc | 33 +++++++++++++++++++++++++++++++--
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc
index 1f52411d8..cb564d173 100644
--- a/winsup/cygwin/autoload.cc
+++ b/winsup/cygwin/autoload.cc
@@ -592,6 +592,7 @@ LoadDLLfuncEx (PrefetchVirtualMemory, 16, kernel32, 1)
LoadDLLfunc (QueryInterruptTime, 4, KernelBase)
LoadDLLfunc (QueryInterruptTimePrecise, 4, KernelBase)
LoadDLLfunc (QueryUnbiasedInterruptTimePrecise, 4, KernelBase)
+LoadDLLfuncEx (SetThreadDescription, 8, KernelBase, 1)
LoadDLLfunc (VirtualAlloc2, 28, KernelBase)
LoadDLLfunc (NtMapViewOfSectionEx, 36, ntdll)
diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc
index 739d9de3b..546df929a 100644
--- a/winsup/cygwin/miscfuncs.cc
+++ b/winsup/cygwin/miscfuncs.cc
@@ -18,6 +18,9 @@ details. */
#include "tls_pbuf.h"
#include "mmap_alloc.h"
+/* not yet prototyped in w32api */
+extern "C" HRESULT WINAPI SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
+
/* Get handle count of an object. */
ULONG
get_obj_handle_count (HANDLE h)
@@ -993,8 +996,8 @@ wmempcpy: \n\
#define MS_VC_EXCEPTION 0x406D1388
-void
-SetThreadName(DWORD dwThreadID, const char* threadName)
+static void
+SetThreadNameExc(DWORD dwThreadID, const char* threadName)
{
if (!IsDebuggerPresent ())
return;
@@ -1025,6 +1028,32 @@ SetThreadName(DWORD dwThreadID, const char* threadName)
__endtry
}
+void
+SetThreadName(DWORD dwThreadID, const char* threadName)
+{
+ HANDLE hThread = OpenThread (THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID);
+ if (hThread)
+ {
+ /* SetThreadDescription only exists in a wide-char version, so we must
+ convert threadname to wide-char. The encoding of threadName is
+ unclear, so use UTF8 until we know better. */
+ int bufsize = MultiByteToWideChar (CP_UTF8, 0, threadName, -1, NULL, 0);
+ WCHAR buf[bufsize];
+ bufsize = MultiByteToWideChar (CP_UTF8, 0, threadName, -1, buf, bufsize);
+ HRESULT hr = SetThreadDescription (hThread, buf);
+ if (hr != S_OK)
+ {
+ debug_printf ("SetThreadDescription() failed. %08x %08x\n",
+ GetLastError (), hr);
+ }
+ CloseHandle (hThread);
+ }
+
+ /* also use the older, exception-based method of setting threadname for the
+ benefit of things which don't known about GetThreadDescription. */
+ SetThreadNameExc(dwThreadID, threadName);
+}
+
#define add_size(p,s) ((p) = ((__typeof__(p))((PBYTE)(p)+(s))))
static WORD num_cpu_per_group = 0;
--
2.36.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Cygwin: Set threadnames with SetThreadDescription()
2022-05-29 14:03 [PATCH] Cygwin: Set threadnames with SetThreadDescription() Jon Turney
@ 2022-05-30 15:37 ` Ken Brown
2022-06-01 17:48 ` Jon Turney
0 siblings, 1 reply; 3+ messages in thread
From: Ken Brown @ 2022-05-30 15:37 UTC (permalink / raw)
To: cygwin-patches
On 5/29/2022 10:03 AM, Jon Turney wrote:
> gdb master recently learnt how to use GetThreadDescription() [1], so set
> threadnames using SetThreadDescription() [available since Windows
> 101607] as well.
>
> This is superior to using a special exception to indicate the thread
> name to the debugger, because the thread name isn't missed if you don't
> have a debugger attached at the time it's set.
>
> It's not clear what the encoding of a thread name string is, we assume
> UTF8 for the moment.
>
> For the moment, continue to use the old method as well, for the benefit
> of older gdb versions etc.
LGTM, except for a few missing spaces (see below), although maybe you did that
deliberately since the existing code was already like that.
> --- a/winsup/cygwin/miscfuncs.cc
> +++ b/winsup/cygwin/miscfuncs.cc
> @@ -18,6 +18,9 @@ details. */
> #include "tls_pbuf.h"
> #include "mmap_alloc.h"
>
> +/* not yet prototyped in w32api */
> +extern "C" HRESULT WINAPI SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
^
> @@ -993,8 +996,8 @@ wmempcpy: \n\
>
> #define MS_VC_EXCEPTION 0x406D1388
>
> -void
> -SetThreadName(DWORD dwThreadID, const char* threadName)
^
> +static void
> +SetThreadNameExc(DWORD dwThreadID, const char* threadName)
^
> @@ -1025,6 +1028,32 @@ SetThreadName(DWORD dwThreadID, const char* threadName)
> __endtry
> }
>
> +void
> +SetThreadName(DWORD dwThreadID, const char* threadName)
^
[...]
> + SetThreadNameExc(dwThreadID, threadName);
^
Ken
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Cygwin: Set threadnames with SetThreadDescription()
2022-05-30 15:37 ` Ken Brown
@ 2022-06-01 17:48 ` Jon Turney
0 siblings, 0 replies; 3+ messages in thread
From: Jon Turney @ 2022-06-01 17:48 UTC (permalink / raw)
To: Cygwin Patches
On 30/05/2022 16:37, Ken Brown wrote:
> On 5/29/2022 10:03 AM, Jon Turney wrote:
>> gdb master recently learnt how to use GetThreadDescription() [1], so set
>> threadnames using SetThreadDescription() [available since Windows
>> 101607] as well.
>>
>> This is superior to using a special exception to indicate the thread
>> name to the debugger, because the thread name isn't missed if you don't
>> have a debugger attached at the time it's set.
>>
>> It's not clear what the encoding of a thread name string is, we assume
>> UTF8 for the moment.
>>
>> For the moment, continue to use the old method as well, for the benefit
>> of older gdb versions etc.
>
> LGTM, except for a few missing spaces (see below), although maybe you
> did that deliberately since the existing code was already like that.
Nope, that's just me messing up on the coding style conventions.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-06-01 17:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-29 14:03 [PATCH] Cygwin: Set threadnames with SetThreadDescription() Jon Turney
2022-05-30 15:37 ` Ken Brown
2022-06-01 17:48 ` Jon Turney
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).