From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by sourceware.org (Postfix) with ESMTPS id 940F73857822 for ; Wed, 13 Apr 2022 19:18:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 940F73857822 Received: by mail-io1-xd2b.google.com with SMTP id r2so3003672iod.9 for ; Wed, 13 Apr 2022 12:18:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aDDl9YE7Yr1xmlm49jiui97ehFHLFNTbKl9FgcmttqY=; b=OvD1+girIzUdaJCyweD8BZvmDOLGNbTSeD7zBsZQunEHY08YJGXutMVl3a+zNWK8vk Ca8G31BrhZpq7DUjQTic3EA41x38HQ4HbZMWqNKVJ3wv15tkL+NmSrxlwgso+kuS/XQy 7CEN091kOJexT26R8viHr7/XhW/iE/M1VQAM6id7tiRMy3WsGta9/Mn3Ud2fqesNPqsE fKk3xDLugS28fHMFIOY14jRv6tiMMrHrc02KI5i0vvyc6tQVBZPLUHhlZBGYjarFmI9Y h6YZUfRMENZDIhsGRVIZr8M0Gn/o/okLQLn9MquPWfdyC0UmtbvDnI12J9NLBUk5zUoI SqSg== X-Gm-Message-State: AOAM531TkhAuPMlDfZMXzr6lTjkAnLifqipDxZ3v7xQs8kKhsImfPuvG PsWMx0j2VgUUwhbn2yU2rQxTrJoWNRle7w== X-Google-Smtp-Source: ABdhPJy17W8/5Pa6pPur42YBWZEyGnmandu0v5qsokydq2oCiKxa7AUrfQ2Ulj2iW0C/TLknx5oHxw== X-Received: by 2002:a05:6638:2041:b0:323:b749:f52d with SMTP id t1-20020a056638204100b00323b749f52dmr19358005jaj.306.1649877485877; Wed, 13 Apr 2022 12:18:05 -0700 (PDT) Received: from murgatroyd.Home (71-211-154-204.hlrn.qwest.net. [71.211.154.204]) by smtp.gmail.com with ESMTPSA id e203-20020a6bb5d4000000b0064dafa0416fsm427660iof.2.2022.04.13.12.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 12:18:05 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 8/9] Set the worker thread name on Windows Date: Wed, 13 Apr 2022 13:17:55 -0600 Message-Id: <20220413191756.1146768-9-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220413191756.1146768-1-tromey@adacore.com> References: <20220413191756.1146768-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Apr 2022 19:18:08 -0000 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 (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 + +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 &&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