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 8/9] Set the worker thread name on Windows
Date: Wed, 13 Apr 2022 13:17:55 -0600	[thread overview]
Message-ID: <20220413191756.1146768-9-tromey@adacore.com> (raw)
In-Reply-To: <20220413191756.1146768-1-tromey@adacore.com>

This patch is a bit different from the rest of the series, in that it
is a change to gdb's behavior on the host.  It changes gdb's thread
pool to try to set the thread name on Windows, if SetThreadDescription
is available.

This is part of PR win32/29050.

This patch isn't likely to be useful to many people in the short term,
because the Windows port of the libstdc++ thread code is not upstream.
(AdaCore uses it, and sent it upstream, but it did not land, I don't
know why.)  However, if that patch does ever go in, or presumably if
you build using some other C++ runtime library, then this will be
useful.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29050
---
 gdbsupport/thread-pool.cc | 72 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 64 insertions(+), 8 deletions(-)

diff --git a/gdbsupport/thread-pool.cc b/gdbsupport/thread-pool.cc
index efd8b799713..9d98529231c 100644
--- a/gdbsupport/thread-pool.cc
+++ b/gdbsupport/thread-pool.cc
@@ -46,14 +46,15 @@
    difference.  */
 
 ATTRIBUTE_UNUSED static void
-set_thread_name (int (*set_name) (pthread_t, const char *, void *),
-				  const char *name)
+do_set_thread_name (int (*set_name) (pthread_t, const char *, void *),
+		    const char *name)
 {
   set_name (pthread_self (), "%s", const_cast<char *> (name));
 }
 
 ATTRIBUTE_UNUSED static void
-set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
+do_set_thread_name (int (*set_name) (pthread_t, const char *),
+		    const char *name)
 {
   set_name (pthread_self (), name);
 }
@@ -61,12 +62,69 @@ set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
 /* The macOS man page says that pthread_setname_np returns "void", but
    the headers actually declare it returning "int".  */
 ATTRIBUTE_UNUSED static void
-set_thread_name (int (*set_name) (const char *), const char *name)
+do_set_thread_name (int (*set_name) (const char *), const char *name)
 {
   set_name (name);
 }
 
-#endif	/* USE_PTHREAD_SETNAME_NP */
+static void
+set_thread_name (const char *name)
+{
+  do_set_thread_name (pthread_setname_np, name);
+}
+
+#elif defined (USE_WIN32API)
+
+#include <windows.h>
+
+typedef HRESULT WINAPI (SetThreadDescription_ftype) (HANDLE, PCWSTR);
+static SetThreadDescription_ftype *dyn_SetThreadDescription;
+static bool initialized;
+
+static void
+init_windows ()
+{
+  initialized = true;
+
+  HMODULE hm = LoadLibrary (TEXT ("kernel32.dll"));
+  if (hm)
+    dyn_SetThreadDescription
+      = (SetThreadDescription_ftype *) GetProcAddress (hm,
+						       "SetThreadDescription");
+
+  /* On some versions of Windows, this function is only available in
+     KernelBase.dll, not kernel32.dll.  */
+  if (dyn_SetThreadDescription == nullptr)
+    {
+      hm = LoadLibrary (TEXT ("KernelBase.dll"));
+      if (hm)
+	dyn_SetThreadDescription
+	  = (SetThreadDescription_ftype *) GetProcAddress (hm,
+							   "SetThreadDescription");
+    }
+}
+
+static void
+do_set_thread_name (const wchar_t *name)
+{
+  if (!initialized)
+    init_windows ();
+
+  if (dyn_SetThreadDescription != nullptr)
+    dyn_SetThreadDescription (GetCurrentThread (), name);
+}
+
+#define set_thread_name(NAME) do_set_thread_name (L ## NAME)
+
+#else /* USE_WIN32API */
+
+static void
+set_thread_name (const char *name)
+{
+}
+
+#endif
+
 #endif /* CXX_STD_THREAD */
 
 namespace gdb
@@ -159,11 +217,9 @@ thread_pool::do_post_task (std::packaged_task<void ()> &&func)
 void
 thread_pool::thread_function ()
 {
-#ifdef USE_PTHREAD_SETNAME_NP
   /* This must be done here, because on macOS one can only set the
      name of the current thread.  */
-  set_thread_name (pthread_setname_np, "gdb worker");
-#endif
+  set_thread_name ("gdb worker");
 
   /* Ensure that SIGSEGV is delivered to an alternate signal
      stack.  */
-- 
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 ` Tom Tromey [this message]
2022-04-13 19:17 ` [PATCH 9/9] Use GetThreadDescription on Windows Tom Tromey
2022-04-13 19:33   ` 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-9-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).