From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2201) id 4E6F43858424; Fri, 29 Jul 2022 11:01:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E6F43858424 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Jon TURNEY To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: Set threadnames with SetThreadDescription() X-Act-Checkin: newlib-cygwin X-Git-Author: Jon Turney X-Git-Refname: refs/heads/master X-Git-Oldrev: f145174f801b10b75fd7246d374764f3301bd575 X-Git-Newrev: d4689b99c68628d9ec2fc1ac7884906ddbf6a2fc Message-Id: <20220729110147.4E6F43858424@sourceware.org> Date: Fri, 29 Jul 2022 11:01:47 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Jul 2022 11:01:47 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dd4689b99c68= 628d9ec2fc1ac7884906ddbf6a2fc commit d4689b99c68628d9ec2fc1ac7884906ddbf6a2fc Author: Jon Turney Date: Thu May 19 17:27:39 2022 +0100 Cygwin: Set threadnames with SetThreadDescription() =20 gdb master recently learnt how to use GetThreadDescription() [1], so set threadnames using SetThreadDescription() [available since Windows 10 1607] as well. =20 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. =20 It's not clear what the encoding of a thread name string is, we assume UTF8 for the moment. =20 For the moment, continue to use the old method as well, for the benefit of older gdb versions etc. =20 [1] https://sourceware.org/pipermail/gdb-patches/2022-April/187833.html Diff: --- 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 60171e889..ef0465d4e 100644 --- a/winsup/cygwin/autoload.cc +++ b/winsup/cygwin/autoload.cc @@ -473,6 +473,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) =20 LoadDLLfunc (NtMapViewOfSectionEx, 36, ntdll) diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc index c6d564af3..a5e381dad 100644 --- a/winsup/cygwin/miscfuncs.cc +++ b/winsup/cygwin/miscfuncs.cc @@ -18,6 +18,9 @@ details. */ #include "tls_pbuf.h" #include "mmap_alloc.h" =20 +/* not yet prototyped in w32api */ +extern "C" HRESULT WINAPI SetThreadDescription (HANDLE hThread, PCWSTR lpT= hreadDescription); + /* Get handle count of an object. */ ULONG get_obj_handle_count (HANDLE h) @@ -916,8 +919,8 @@ wmempcpy: \n\ =20 #define MS_VC_EXCEPTION 0x406D1388 =20 -void -SetThreadName(DWORD dwThreadID, const char* threadName) +static void +SetThreadNameExc (DWORD dwThreadID, const char* threadName) { if (!IsDebuggerPresent ()) return; @@ -938,6 +941,32 @@ SetThreadName(DWORD dwThreadID, const char* threadName) __endtry } =20 +void +SetThreadName (DWORD dwThreadID, const char* threadName) +{ + HANDLE hThread =3D OpenThread (THREAD_SET_LIMITED_INFORMATION, FALSE, dw= ThreadID); + if (hThread) + { + /* SetThreadDescription only exists in a wide-char version, so we mu= st + convert threadname to wide-char. The encoding of threadName is + unclear, so use UTF8 until we know better. */ + int bufsize =3D MultiByteToWideChar (CP_UTF8, 0, threadName, -1, NUL= L, 0); + WCHAR buf[bufsize]; + bufsize =3D MultiByteToWideChar (CP_UTF8, 0, threadName, -1, buf, bu= fsize); + HRESULT hr =3D SetThreadDescription (hThread, buf); + if (hr !=3D 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) =3D ((__typeof__(p))((PBYTE)(p)+(s)))) =20 static WORD num_cpu_per_group =3D 0;