public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Use GetThreadDescription on Windows
@ 2022-04-14 18:21 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2022-04-14 18:21 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8bbdbd6985d8d7615bab69f97f07e8bb78fb98f8

commit 8bbdbd6985d8d7615bab69f97f07e8bb78fb98f8
Author: Tom Tromey <tromey@adacore.com>
Date:   Wed Apr 13 08:45:34 2022 -0600

    Use GetThreadDescription on Windows
    
    Windows 10 introduced SetThreadDescription and GetThreadDescription, a
    simpler way to set a thread's name.  This changes gdb and gdbserver to
    use this convention when it is available.
    
    This is part of PR win32/29050.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29050

Diff:
---
 gdb/nat/windows-nat.c  | 37 +++++++++++++++++++++++++++++++++++++
 gdb/nat/windows-nat.h  |  5 +++++
 gdb/windows-nat.c      |  6 ++++--
 gdbserver/win32-low.cc |  2 +-
 4 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index 9fdef463760..bd1b9459145 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -58,6 +58,10 @@ Wow64GetThreadSelectorEntry_ftype *Wow64GetThreadSelectorEntry;
 #endif
 GenerateConsoleCtrlEvent_ftype *GenerateConsoleCtrlEvent;
 
+#define GetThreadDescription dyn_GetThreadDescription
+typedef HRESULT WINAPI (GetThreadDescription_ftype) (HANDLE, PWSTR *);
+static GetThreadDescription_ftype *GetThreadDescription;
+
 /* Note that 'debug_events' must be locally defined in the relevant
    functions.  */
 #define DEBUG_EVENTS(fmt, ...) \
@@ -106,6 +110,29 @@ windows_thread_info::resume ()
   suspended = 0;
 }
 
+const char *
+windows_thread_info::thread_name ()
+{
+  if (GetThreadDescription != nullptr)
+    {
+      PWSTR value;
+      HRESULT result = GetThreadDescription (h, &value);
+      if (SUCCEEDED (result))
+	{
+	  size_t needed = wcstombs (nullptr, value, 0);
+	  if (needed != (size_t) -1)
+	    {
+	      name.reset ((char *) xmalloc (needed));
+	      if (wcstombs (name.get (), value, needed) == (size_t) -1)
+		name.reset ();
+	    }
+	  LocalFree (value);
+	}
+    }
+
+  return name.get ();
+}
+
 /* Return the name of the DLL referenced by H at ADDRESS.  UNICODE
    determines what sort of string is read from the inferior.  Returns
    the name of the DLL, or NULL on error.  If a name is returned, it
@@ -662,6 +689,7 @@ initialize_loadable ()
       GPA (hm, Wow64GetThreadSelectorEntry);
 #endif
       GPA (hm, GenerateConsoleCtrlEvent);
+      GPA (hm, GetThreadDescription);
     }
 
   /* Set variables to dummy versions of these processes if the function
@@ -718,6 +746,15 @@ initialize_loadable ()
 	OpenProcessToken = bad;
     }
 
+  /* On some versions of Windows, this function is only available in
+     KernelBase.dll, not kernel32.dll.  */
+  if (GetThreadDescription == nullptr)
+    {
+      hm = LoadLibrary (TEXT ("KernelBase.dll"));
+      if (hm)
+	GPA (hm, GetThreadDescription);
+    }
+
 #undef GPA
 
   return result;
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 522e267176d..29fd0a3a69b 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -51,6 +51,11 @@ struct windows_thread_info
   /* Resume the thread if it has been suspended.  */
   void resume ();
 
+  /* Return the thread's name, or nullptr if not known.  The name is
+     stored in this thread and is guaranteed to live until at least
+     the next call.  */
+  const char *thread_name ();
+
   /* The Win32 thread identifier.  */
   DWORD tid;
 
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 581eb47e7ba..1068558cd21 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2997,8 +2997,10 @@ windows_nat_target::get_ada_task_ptid (long lwp, ULONGEST thread)
 const char *
 windows_nat_target::thread_name (struct thread_info *thr)
 {
-  return windows_process.thread_rec (thr->ptid,
-				     DONT_INVALIDATE_CONTEXT)->name.get ();
+  windows_thread_info *th
+    = windows_process.thread_rec (thr->ptid,
+				  DONT_INVALIDATE_CONTEXT);
+  return th->thread_name ();
 }
 
 
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 16c13f32d77..afeed1a9881 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -1508,7 +1508,7 @@ win32_process_target::thread_name (ptid_t thread)
   windows_thread_info *th
     = windows_process.thread_rec (current_thread_ptid (),
 				  DONT_INVALIDATE_CONTEXT);
-  return th->name.get ();
+  return th->thread_name ();
 }
 
 /* The win32 target ops object.  */


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-14 18:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-14 18:21 [binutils-gdb] Use GetThreadDescription on Windows Tom Tromey

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).