Skip to content

Commit 95e5839

Browse files
authored
[lldb] add support for thread names on Windows (llvm#74731)
This PR adds support for thread names in lldb on Windows. ``` (lldb) thr list Process 2960 stopped thread rust-lang#53: tid = 0x03a0, 0x00007ff84582db34 ntdll.dll`NtWaitForMultipleObjects + 20 thread rust-lang#29: tid = 0x04ec, 0x00007ff845830a14 ntdll.dll`NtWaitForAlertByThreadId + 20, name = 'SPUW.6' thread rust-lang#89: tid = 0x057c, 0x00007ff845830a14 ntdll.dll`NtWaitForAlertByThreadId + 20, name = 'PPU[0x1000019] physics[main]' thread rust-lang#3: tid = 0x0648, 0x00007ff843c2cafe combase.dll`InternalDoATClassCreate + 39518 thread rust-lang#93: tid = 0x0688, 0x00007ff845830a14 ntdll.dll`NtWaitForAlertByThreadId + 20, name = 'PPU[0x100501d] uMovie::StreamingThread' thread #1: tid = 0x087c, 0x00007ff842e7a104 win32u.dll`NtUserMsgWaitForMultipleObjectsEx + 20 thread rust-lang#96: tid = 0x0890, 0x00007ff845830a14 ntdll.dll`NtWaitForAlertByThreadId + 20, name = 'PPU[0x1002020] HLE Video Decoder' <...> ```
1 parent cd09f4b commit 95e5839

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp

+31-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Host/HostInfo.h"
10-
#include "lldb/Host/HostNativeThreadBase.h"
11-
#include "lldb/Host/windows/HostThreadWindows.h"
12-
#include "lldb/Host/windows/windows.h"
13-
#include "lldb/Target/RegisterContext.h"
1410
#include "lldb/Target/Unwind.h"
1511
#include "lldb/Utility/LLDBLog.h"
1612
#include "lldb/Utility/Log.h"
17-
#include "lldb/Utility/State.h"
1813

1914
#include "ProcessWindows.h"
20-
#include "ProcessWindowsLog.h"
2115
#include "TargetThreadWindows.h"
16+
#include "lldb/Host/windows/HostThreadWindows.h"
17+
#include <llvm/Support/ConvertUTF.h>
2218

2319
#if defined(__x86_64__) || defined(_M_AMD64)
2420
#include "x64/RegisterContextWindows_x64.h"
@@ -33,6 +29,9 @@
3329
using namespace lldb;
3430
using namespace lldb_private;
3531

32+
using GetThreadDescriptionFunctionPtr = HRESULT
33+
WINAPI (*)(HANDLE hThread, PWSTR *ppszThreadDescription);
34+
3635
TargetThreadWindows::TargetThreadWindows(ProcessWindows &process,
3736
const HostThread &thread)
3837
: Thread(process, thread.GetNativeThread().GetThreadId()),
@@ -175,3 +174,29 @@ Status TargetThreadWindows::DoResume() {
175174

176175
return Status();
177176
}
177+
178+
const char *TargetThreadWindows::GetName() {
179+
Log *log = GetLog(LLDBLog::Thread);
180+
static GetThreadDescriptionFunctionPtr GetThreadDescription = []() {
181+
HMODULE hModule = ::LoadLibraryW(L"Kernel32.dll");
182+
return hModule ? reinterpret_cast<GetThreadDescriptionFunctionPtr>(
183+
::GetProcAddress(hModule, "GetThreadDescription"))
184+
: nullptr;
185+
}();
186+
LLDB_LOGF(log, "GetProcAddress: %p",
187+
reinterpret_cast<void *>(GetThreadDescription));
188+
if (!GetThreadDescription)
189+
return m_name.c_str();
190+
PWSTR pszThreadName;
191+
if (SUCCEEDED(GetThreadDescription(
192+
m_host_thread.GetNativeThread().GetSystemHandle(), &pszThreadName))) {
193+
LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName);
194+
llvm::convertUTF16ToUTF8String(
195+
llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName),
196+
wcslen(pszThreadName) * sizeof(wchar_t)),
197+
m_name);
198+
::LocalFree(pszThreadName);
199+
}
200+
201+
return m_name.c_str();
202+
}

lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TargetThreadWindows : public lldb_private::Thread {
3434
lldb::RegisterContextSP
3535
CreateRegisterContextForFrame(StackFrame *frame) override;
3636
bool CalculateStopInfo() override;
37+
const char *GetName() override;
3738

3839
Status DoResume();
3940

@@ -42,6 +43,7 @@ class TargetThreadWindows : public lldb_private::Thread {
4243
private:
4344
lldb::RegisterContextSP m_thread_reg_ctx_sp;
4445
HostThread m_host_thread;
46+
std::string m_name;
4547
};
4648
} // namespace lldb_private
4749

lldb/unittests/Thread/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ add_lldb_unittest(ThreadTests
1111
lldbInterpreter
1212
lldbBreakpoint
1313
lldbPluginPlatformLinux
14+
lldbPluginPlatformWindows
15+
lldbPluginProcessWindowsCommon
1416
)
1517

lldb/unittests/Thread/ThreadTest.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88

99
#include "lldb/Target/Thread.h"
1010
#include "Plugins/Platform/Linux/PlatformLinux.h"
11+
#include <thread>
12+
#ifdef _WIN32
13+
#include "lldb/Host/windows/HostThreadWindows.h"
14+
#include "lldb/Host/windows/windows.h"
15+
16+
#include "Plugins/Platform/Windows/PlatformWindows.h"
17+
#include "Plugins/Process/Windows/Common/LocalDebugDelegate.h"
18+
#include "Plugins/Process/Windows/Common/ProcessWindows.h"
19+
#include "Plugins/Process/Windows/Common/TargetThreadWindows.h"
20+
#endif
1121
#include "lldb/Core/Debugger.h"
1222
#include "lldb/Host/FileSystem.h"
1323
#include "lldb/Host/HostInfo.h"
24+
#include "lldb/Host/HostThread.h"
1425
#include "lldb/Target/Process.h"
1526
#include "lldb/Target/StopInfo.h"
1627
#include "lldb/Utility/ArchSpec.h"
@@ -21,14 +32,33 @@ using namespace lldb_private::repro;
2132
using namespace lldb;
2233

2334
namespace {
35+
36+
#ifdef _WIN32
37+
using SetThreadDescriptionFunctionPtr = HRESULT
38+
WINAPI (*)(HANDLE hThread, PCWSTR lpThreadDescription);
39+
40+
static SetThreadDescriptionFunctionPtr SetThreadName;
41+
#endif
42+
2443
class ThreadTest : public ::testing::Test {
2544
public:
2645
void SetUp() override {
2746
FileSystem::Initialize();
2847
HostInfo::Initialize();
48+
#ifdef _WIN32
49+
HMODULE hModule = ::LoadLibraryW(L"Kernel32.dll");
50+
if (hModule) {
51+
SetThreadName = reinterpret_cast<SetThreadDescriptionFunctionPtr>(
52+
::GetProcAddress(hModule, "SetThreadDescription"));
53+
}
54+
PlatformWindows::Initialize();
55+
#endif
2956
platform_linux::PlatformLinux::Initialize();
3057
}
3158
void TearDown() override {
59+
#ifdef _WIN32
60+
PlatformWindows::Terminate();
61+
#endif
3262
platform_linux::PlatformLinux::Terminate();
3363
HostInfo::Terminate();
3464
FileSystem::Terminate();
@@ -88,6 +118,47 @@ TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) {
88118
return target_sp;
89119
}
90120

121+
#ifdef _WIN32
122+
std::shared_ptr<TargetThreadWindows>
123+
CreateWindowsThread(const ProcessWindowsSP &process_sp, std::thread &t) {
124+
HostThread host_thread((lldb::thread_t)t.native_handle());
125+
ThreadSP thread_sp =
126+
std::make_shared<TargetThreadWindows>(*process_sp.get(), host_thread);
127+
return std::static_pointer_cast<TargetThreadWindows>(thread_sp);
128+
}
129+
130+
TEST_F(ThreadTest, GetThreadDescription) {
131+
if (!SetThreadName)
132+
return;
133+
134+
ArchSpec arch(HostInfo::GetArchitecture());
135+
Platform::SetHostPlatform(PlatformWindows::CreateInstance(true, &arch));
136+
137+
DebuggerSP debugger_sp = Debugger::CreateInstance();
138+
ASSERT_TRUE(debugger_sp);
139+
140+
TargetSP target_sp = CreateTarget(debugger_sp, arch);
141+
ASSERT_TRUE(target_sp);
142+
143+
ListenerSP listener_sp(Listener::MakeListener("dummy"));
144+
auto process_sp = std::static_pointer_cast<ProcessWindows>(
145+
ProcessWindows::CreateInstance(target_sp, listener_sp, nullptr, false));
146+
ASSERT_TRUE(process_sp);
147+
148+
std::thread t([]() {});
149+
auto thread_sp = CreateWindowsThread(process_sp, t);
150+
DWORD tid = thread_sp->GetHostThread().GetNativeThread().GetThreadId();
151+
HANDLE hThread = ::OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, tid);
152+
ASSERT_TRUE(hThread);
153+
154+
SetThreadName(hThread, L"thread name");
155+
::CloseHandle(hThread);
156+
ASSERT_STREQ(thread_sp->GetName(), "thread name");
157+
158+
t.join();
159+
}
160+
#endif
161+
91162
TEST_F(ThreadTest, SetStopInfo) {
92163
ArchSpec arch("powerpc64-pc-linux");
93164

0 commit comments

Comments
 (0)