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;
2132using namespace lldb ;
2233
2334namespace {
35+
36+ #ifdef _WIN32
37+ using SetThreadDescriptionFunctionPtr = HRESULT
38+ WINAPI (*)(HANDLE hThread, PCWSTR lpThreadDescription);
39+
40+ static SetThreadDescriptionFunctionPtr SetThreadName;
41+ #endif
42+
2443class ThreadTest : public ::testing::Test {
2544public:
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+
91162TEST_F (ThreadTest, SetStopInfo) {
92163 ArchSpec arch (" powerpc64-pc-linux" );
93164
0 commit comments