From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 80B423857811 for ; Wed, 13 Apr 2022 19:18:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 80B423857811 Received: by mail-io1-xd34.google.com with SMTP id p135so3029051iod.2 for ; Wed, 13 Apr 2022 12:18:07 -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=gsFmpQ9vddLX/3doRHLm/5aUAXD3VkVV7CU79LFl0is=; b=kyRpOG5Kt3Zn4f6UHGuLEw42Qn9K9o8H42QzkzDguP/eThHlpizARXgcwzfwHCV8bN VeR+mPGmkdRgoz6fSPJRHmOk31nUtJ6BrvcVIuvw1Wjw0JYvCaDMsQwKoWkqgSN4+UW4 frDX17CIAi77+IbFHgBO0gkQlT+IuG5ihnkW4sTiwj76CG8K4NmXiCUOSn23FGYrXflu yCW9RR3/8mGu1+R/hP6d79vNvHRofIwFJOLnFu8Xx/I585eA2HqO2JcIWj4CNzM9QSMv FbvNxJLUZvHg5Iw5RA1L9VRpGCUz/EHUo0pvKErjQjSVYptUWMS3c2TvIdehrmNjjFo7 HsDQ== X-Gm-Message-State: AOAM532c2Zn3O6I7XwcU8suoQh1/Eis3wo2le21Zul6FauNCE1mwMKAr wchRzXYd+EbXPJGSIe7GCfEBd9jXPFm7/w== X-Google-Smtp-Source: ABdhPJwYGYamW4X8DmwiEEnbhjR/3q5LAulQn8AqjPj/q8C641Ply3U0lZBZq+5swSE2xnbpTWG6FQ== X-Received: by 2002:a05:6638:1305:b0:326:46ef:6727 with SMTP id r5-20020a056638130500b0032646ef6727mr4909498jad.245.1649877486848; Wed, 13 Apr 2022 12:18:06 -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.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 12:18:06 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 9/9] Use GetThreadDescription on Windows Date: Wed, 13 Apr 2022 13:17:56 -0600 Message-Id: <20220413191756.1146768-10-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:09 -0000 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