8
8
9
9
#include " lldb/Target/Thread.h"
10
10
#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
11
21
#include " lldb/Core/Debugger.h"
12
22
#include " lldb/Host/FileSystem.h"
13
23
#include " lldb/Host/HostInfo.h"
24
+ #include " lldb/Host/HostThread.h"
14
25
#include " lldb/Target/Process.h"
15
26
#include " lldb/Target/StopInfo.h"
16
27
#include " lldb/Utility/ArchSpec.h"
@@ -21,14 +32,33 @@ using namespace lldb_private::repro;
21
32
using namespace lldb ;
22
33
23
34
namespace {
35
+
36
+ #ifdef _WIN32
37
+ using SetThreadDescriptionFunctionPtr = HRESULT
38
+ WINAPI (*)(HANDLE hThread, PCWSTR lpThreadDescription);
39
+
40
+ static SetThreadDescriptionFunctionPtr SetThreadName;
41
+ #endif
42
+
24
43
class ThreadTest : public ::testing::Test {
25
44
public:
26
45
void SetUp () override {
27
46
FileSystem::Initialize ();
28
47
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
29
56
platform_linux::PlatformLinux::Initialize ();
30
57
}
31
58
void TearDown () override {
59
+ #ifdef _WIN32
60
+ PlatformWindows::Terminate ();
61
+ #endif
32
62
platform_linux::PlatformLinux::Terminate ();
33
63
HostInfo::Terminate ();
34
64
FileSystem::Terminate ();
@@ -88,6 +118,47 @@ TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) {
88
118
return target_sp;
89
119
}
90
120
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
+
91
162
TEST_F (ThreadTest, SetStopInfo) {
92
163
ArchSpec arch (" powerpc64-pc-linux" );
93
164
0 commit comments