public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 9/9] Use GetThreadDescription on Windows
Date: Wed, 13 Apr 2022 13:17:56 -0600	[thread overview]
Message-ID: <20220413191756.1146768-10-tromey@adacore.com> (raw)
In-Reply-To: <20220413191756.1146768-1-tromey@adacore.com>

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
---
 gdb/nat/windows-nat.c  | 38 ++++++++++++++++++++++++++++++++++++++
 gdb/nat/windows-nat.h  |  5 +++++
 gdb/windows-nat.c      |  6 ++++--
 gdbserver/win32-low.cc |  2 +-
 4 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index 9defbd71d14..677525d1629 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,30 @@ 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;
+	  if (wcstombs_s (&needed, nullptr, 0, value, _TRUNCATE) == 0)
+	    {
+	      name.reset ((char *) xmalloc (needed));
+	      if (wcstombs_s (&needed, name.get (), needed,
+			      value, needed - 1) != 0)
+		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
@@ -658,6 +686,7 @@ initialize_loadable ()
       GPA (hm, Wow64GetThreadSelectorEntry);
 #endif
       GPA (hm, GenerateConsoleCtrlEvent);
+      GPA (hm, GetThreadDescription);
     }
 
   /* Set variables to dummy versions of these processes if the function
@@ -714,6 +743,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.  */
-- 
2.34.1


  parent reply	other threads:[~2022-04-13 19:18 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-13 19:17 [PATCH 0/9] Windows thread names Tom Tromey
2022-04-13 19:17 ` [PATCH 1/9] Fix possible Cygwin build problem Tom Tromey
2022-05-21 11:18   ` Jon Turney
2022-05-21 12:31     ` Eli Zaretskii
2022-05-26 17:00       ` Tom Tromey
2022-05-26 19:04         ` Eli Zaretskii
2022-06-02 14:12           ` Jon Turney
2022-06-02 16:04             ` Eli Zaretskii
2022-04-13 19:17 ` [PATCH 2/9] Don't call QUIT in read_string Tom Tromey
2022-04-13 19:17 ` [PATCH 3/9] Rename read_string Tom Tromey
2022-04-13 19:17 ` [PATCH 4/9] Remove the byte order parameter to target_read_string Tom Tromey
2022-04-13 19:17 ` [PATCH 5/9] Move target_read_string to target/target.c Tom Tromey
2022-04-13 19:17 ` [PATCH 6/9] Share handle_ms_vc_exception with gdbserver Tom Tromey
2022-04-13 19:17 ` [PATCH 7/9] Implement thread_name for gdbserver Tom Tromey
2022-04-13 19:17 ` [PATCH 8/9] Set the worker thread name on Windows Tom Tromey
2022-04-13 19:17 ` Tom Tromey [this message]
2022-04-13 19:33   ` [PATCH 9/9] Use GetThreadDescription " Eli Zaretskii
2022-04-14  5:26     ` Eli Zaretskii
2022-04-14 13:17       ` Tom Tromey
2022-04-14 13:51         ` Eli Zaretskii
2022-04-14 18:20           ` Tom Tromey
2022-04-14 18:18         ` Tom Tromey
2022-04-14 13:58 ` [PATCH 0/9] Windows thread names Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220413191756.1146768-10-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).