13b43f006SIlya Bukonkin //===-- ThreadTest.cpp ------------------------------------------===//
23b43f006SIlya Bukonkin //
33b43f006SIlya Bukonkin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43b43f006SIlya Bukonkin // See https://llvm.org/LICENSE.txt for license information.
53b43f006SIlya Bukonkin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63b43f006SIlya Bukonkin //
73b43f006SIlya Bukonkin //===----------------------------------------------------------------------===//
83b43f006SIlya Bukonkin
93b43f006SIlya Bukonkin #include "lldb/Target/Thread.h"
103b43f006SIlya Bukonkin #include "Plugins/Platform/Linux/PlatformLinux.h"
1195e5839eSoltolm #include <thread>
1295e5839eSoltolm #ifdef _WIN32
1395e5839eSoltolm #include "lldb/Host/windows/HostThreadWindows.h"
1495e5839eSoltolm #include "lldb/Host/windows/windows.h"
1595e5839eSoltolm
1695e5839eSoltolm #include "Plugins/Platform/Windows/PlatformWindows.h"
1795e5839eSoltolm #include "Plugins/Process/Windows/Common/LocalDebugDelegate.h"
1895e5839eSoltolm #include "Plugins/Process/Windows/Common/ProcessWindows.h"
1995e5839eSoltolm #include "Plugins/Process/Windows/Common/TargetThreadWindows.h"
2095e5839eSoltolm #endif
213b43f006SIlya Bukonkin #include "lldb/Core/Debugger.h"
223b43f006SIlya Bukonkin #include "lldb/Host/FileSystem.h"
233b43f006SIlya Bukonkin #include "lldb/Host/HostInfo.h"
2495e5839eSoltolm #include "lldb/Host/HostThread.h"
253b43f006SIlya Bukonkin #include "lldb/Target/Process.h"
263b43f006SIlya Bukonkin #include "lldb/Target/StopInfo.h"
273b43f006SIlya Bukonkin #include "lldb/Utility/ArchSpec.h"
283b43f006SIlya Bukonkin #include "gtest/gtest.h"
293b43f006SIlya Bukonkin
303b43f006SIlya Bukonkin using namespace lldb_private;
313b43f006SIlya Bukonkin using namespace lldb_private::repro;
323b43f006SIlya Bukonkin using namespace lldb;
333b43f006SIlya Bukonkin
343b43f006SIlya Bukonkin namespace {
3595e5839eSoltolm
3695e5839eSoltolm #ifdef _WIN32
37*f3f4387eSgmh using SetThreadDescriptionFunctionPtr =
38*f3f4387eSgmh HRESULT(WINAPI *)(HANDLE hThread, PCWSTR lpThreadDescription);
3995e5839eSoltolm
4095e5839eSoltolm static SetThreadDescriptionFunctionPtr SetThreadName;
4195e5839eSoltolm #endif
4295e5839eSoltolm
433b43f006SIlya Bukonkin class ThreadTest : public ::testing::Test {
443b43f006SIlya Bukonkin public:
SetUp()453b43f006SIlya Bukonkin void SetUp() override {
463b43f006SIlya Bukonkin FileSystem::Initialize();
473b43f006SIlya Bukonkin HostInfo::Initialize();
4895e5839eSoltolm #ifdef _WIN32
4995e5839eSoltolm HMODULE hModule = ::LoadLibraryW(L"Kernel32.dll");
5095e5839eSoltolm if (hModule) {
5195e5839eSoltolm SetThreadName = reinterpret_cast<SetThreadDescriptionFunctionPtr>(
5295e5839eSoltolm ::GetProcAddress(hModule, "SetThreadDescription"));
5395e5839eSoltolm }
5495e5839eSoltolm PlatformWindows::Initialize();
5595e5839eSoltolm #endif
563b43f006SIlya Bukonkin platform_linux::PlatformLinux::Initialize();
573b43f006SIlya Bukonkin }
TearDown()583b43f006SIlya Bukonkin void TearDown() override {
5995e5839eSoltolm #ifdef _WIN32
6095e5839eSoltolm PlatformWindows::Terminate();
6195e5839eSoltolm #endif
623b43f006SIlya Bukonkin platform_linux::PlatformLinux::Terminate();
633b43f006SIlya Bukonkin HostInfo::Terminate();
643b43f006SIlya Bukonkin FileSystem::Terminate();
653b43f006SIlya Bukonkin }
663b43f006SIlya Bukonkin };
673b43f006SIlya Bukonkin
683b43f006SIlya Bukonkin class DummyProcess : public Process {
693b43f006SIlya Bukonkin public:
DummyProcess(lldb::TargetSP target_sp,lldb::ListenerSP listener_sp)709b031d5eSMichał Górny DummyProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
719b031d5eSMichał Górny : Process(target_sp, listener_sp) {}
723b43f006SIlya Bukonkin
CanDebug(lldb::TargetSP target,bool plugin_specified_by_name)7368466861SDavid Blaikie bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override {
743b43f006SIlya Bukonkin return true;
753b43f006SIlya Bukonkin }
DoDestroy()7668466861SDavid Blaikie Status DoDestroy() override { return {}; }
RefreshStateAfterStop()7768466861SDavid Blaikie void RefreshStateAfterStop() override {}
DoReadMemory(lldb::addr_t vm_addr,void * buf,size_t size,Status & error)7868466861SDavid Blaikie size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
7968466861SDavid Blaikie Status &error) override {
803b43f006SIlya Bukonkin return 0;
813b43f006SIlya Bukonkin }
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)824bb62448SWalter Erquinigo bool DoUpdateThreadList(ThreadList &old_thread_list,
834bb62448SWalter Erquinigo ThreadList &new_thread_list) override {
843b43f006SIlya Bukonkin return false;
853b43f006SIlya Bukonkin }
GetPluginName()86a3939e15SPavel Labath llvm::StringRef GetPluginName() override { return "Dummy"; }
873b43f006SIlya Bukonkin
GetModIDNonConstRef()883b43f006SIlya Bukonkin ProcessModID &GetModIDNonConstRef() { return m_mod_id; }
893b43f006SIlya Bukonkin };
903b43f006SIlya Bukonkin
913b43f006SIlya Bukonkin class DummyThread : public Thread {
923b43f006SIlya Bukonkin public:
933b43f006SIlya Bukonkin using Thread::Thread;
943b43f006SIlya Bukonkin
~DummyThread()953b43f006SIlya Bukonkin ~DummyThread() override { DestroyThread(); }
963b43f006SIlya Bukonkin
RefreshStateAfterStop()973b43f006SIlya Bukonkin void RefreshStateAfterStop() override {}
983b43f006SIlya Bukonkin
GetRegisterContext()993b43f006SIlya Bukonkin lldb::RegisterContextSP GetRegisterContext() override { return nullptr; }
1003b43f006SIlya Bukonkin
1013b43f006SIlya Bukonkin lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)1023b43f006SIlya Bukonkin CreateRegisterContextForFrame(StackFrame *frame) override {
1033b43f006SIlya Bukonkin return nullptr;
1043b43f006SIlya Bukonkin }
1053b43f006SIlya Bukonkin
CalculateStopInfo()1063b43f006SIlya Bukonkin bool CalculateStopInfo() override { return false; }
1073b43f006SIlya Bukonkin
IsStillAtLastBreakpointHit()1083b43f006SIlya Bukonkin bool IsStillAtLastBreakpointHit() override { return true; }
1093b43f006SIlya Bukonkin };
1103b43f006SIlya Bukonkin } // namespace
1113b43f006SIlya Bukonkin
CreateTarget(DebuggerSP & debugger_sp,ArchSpec & arch)1123b43f006SIlya Bukonkin TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) {
1133b43f006SIlya Bukonkin PlatformSP platform_sp;
1143b43f006SIlya Bukonkin TargetSP target_sp;
1152634ec6cSTatyana Krasnukha debugger_sp->GetTargetList().CreateTarget(
1163b43f006SIlya Bukonkin *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
1173b43f006SIlya Bukonkin
1183b43f006SIlya Bukonkin return target_sp;
1193b43f006SIlya Bukonkin }
1203b43f006SIlya Bukonkin
12195e5839eSoltolm #ifdef _WIN32
12295e5839eSoltolm std::shared_ptr<TargetThreadWindows>
CreateWindowsThread(const ProcessWindowsSP & process_sp,std::thread & t)12395e5839eSoltolm CreateWindowsThread(const ProcessWindowsSP &process_sp, std::thread &t) {
12495e5839eSoltolm HostThread host_thread((lldb::thread_t)t.native_handle());
12595e5839eSoltolm ThreadSP thread_sp =
12695e5839eSoltolm std::make_shared<TargetThreadWindows>(*process_sp.get(), host_thread);
12795e5839eSoltolm return std::static_pointer_cast<TargetThreadWindows>(thread_sp);
12895e5839eSoltolm }
12995e5839eSoltolm
TEST_F(ThreadTest,GetThreadDescription)13095e5839eSoltolm TEST_F(ThreadTest, GetThreadDescription) {
13195e5839eSoltolm if (!SetThreadName)
13295e5839eSoltolm return;
13395e5839eSoltolm
13495e5839eSoltolm ArchSpec arch(HostInfo::GetArchitecture());
13595e5839eSoltolm Platform::SetHostPlatform(PlatformWindows::CreateInstance(true, &arch));
13695e5839eSoltolm
13795e5839eSoltolm DebuggerSP debugger_sp = Debugger::CreateInstance();
13895e5839eSoltolm ASSERT_TRUE(debugger_sp);
13995e5839eSoltolm
14095e5839eSoltolm TargetSP target_sp = CreateTarget(debugger_sp, arch);
14195e5839eSoltolm ASSERT_TRUE(target_sp);
14295e5839eSoltolm
14395e5839eSoltolm ListenerSP listener_sp(Listener::MakeListener("dummy"));
14495e5839eSoltolm auto process_sp = std::static_pointer_cast<ProcessWindows>(
14595e5839eSoltolm ProcessWindows::CreateInstance(target_sp, listener_sp, nullptr, false));
14695e5839eSoltolm ASSERT_TRUE(process_sp);
14795e5839eSoltolm
14895e5839eSoltolm std::thread t([]() {});
14995e5839eSoltolm auto thread_sp = CreateWindowsThread(process_sp, t);
15095e5839eSoltolm DWORD tid = thread_sp->GetHostThread().GetNativeThread().GetThreadId();
15195e5839eSoltolm HANDLE hThread = ::OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, tid);
15295e5839eSoltolm ASSERT_TRUE(hThread);
15395e5839eSoltolm
15495e5839eSoltolm SetThreadName(hThread, L"thread name");
15595e5839eSoltolm ::CloseHandle(hThread);
15695e5839eSoltolm ASSERT_STREQ(thread_sp->GetName(), "thread name");
15795e5839eSoltolm
15895e5839eSoltolm t.join();
15995e5839eSoltolm }
16095e5839eSoltolm #endif
16195e5839eSoltolm
TEST_F(ThreadTest,SetStopInfo)1623b43f006SIlya Bukonkin TEST_F(ThreadTest, SetStopInfo) {
1633b43f006SIlya Bukonkin ArchSpec arch("powerpc64-pc-linux");
1643b43f006SIlya Bukonkin
165d6678404SMed Ismail Bennani Platform::SetHostPlatform(
166d6678404SMed Ismail Bennani platform_linux::PlatformLinux::CreateInstance(true, &arch));
1673b43f006SIlya Bukonkin
1683b43f006SIlya Bukonkin DebuggerSP debugger_sp = Debugger::CreateInstance();
1693b43f006SIlya Bukonkin ASSERT_TRUE(debugger_sp);
1703b43f006SIlya Bukonkin
1713b43f006SIlya Bukonkin TargetSP target_sp = CreateTarget(debugger_sp, arch);
1723b43f006SIlya Bukonkin ASSERT_TRUE(target_sp);
1733b43f006SIlya Bukonkin
1743b43f006SIlya Bukonkin ListenerSP listener_sp(Listener::MakeListener("dummy"));
1753b43f006SIlya Bukonkin ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
1763b43f006SIlya Bukonkin ASSERT_TRUE(process_sp);
1773b43f006SIlya Bukonkin
1783b43f006SIlya Bukonkin DummyProcess *process = static_cast<DummyProcess *>(process_sp.get());
1793b43f006SIlya Bukonkin
1803b43f006SIlya Bukonkin ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
1813b43f006SIlya Bukonkin ASSERT_TRUE(thread_sp);
1823b43f006SIlya Bukonkin
1833b43f006SIlya Bukonkin StopInfoSP stopinfo_sp =
1843b43f006SIlya Bukonkin StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0);
1853b43f006SIlya Bukonkin ASSERT_TRUE(stopinfo_sp->IsValid() == true);
1863b43f006SIlya Bukonkin
1873b43f006SIlya Bukonkin /*
1883b43f006SIlya Bukonkin Should make stopinfo valid.
1893b43f006SIlya Bukonkin */
1903b43f006SIlya Bukonkin process->GetModIDNonConstRef().BumpStopID();
1913b43f006SIlya Bukonkin ASSERT_TRUE(stopinfo_sp->IsValid() == false);
1923b43f006SIlya Bukonkin
1933b43f006SIlya Bukonkin thread_sp->SetStopInfo(stopinfo_sp);
1943b43f006SIlya Bukonkin ASSERT_TRUE(stopinfo_sp->IsValid() == true);
1953b43f006SIlya Bukonkin }
1963b43f006SIlya Bukonkin
TEST_F(ThreadTest,GetPrivateStopInfo)1973b43f006SIlya Bukonkin TEST_F(ThreadTest, GetPrivateStopInfo) {
1983b43f006SIlya Bukonkin ArchSpec arch("powerpc64-pc-linux");
1993b43f006SIlya Bukonkin
200d6678404SMed Ismail Bennani Platform::SetHostPlatform(
201d6678404SMed Ismail Bennani platform_linux::PlatformLinux::CreateInstance(true, &arch));
2023b43f006SIlya Bukonkin
2033b43f006SIlya Bukonkin DebuggerSP debugger_sp = Debugger::CreateInstance();
2043b43f006SIlya Bukonkin ASSERT_TRUE(debugger_sp);
2053b43f006SIlya Bukonkin
2063b43f006SIlya Bukonkin TargetSP target_sp = CreateTarget(debugger_sp, arch);
2073b43f006SIlya Bukonkin ASSERT_TRUE(target_sp);
2083b43f006SIlya Bukonkin
2093b43f006SIlya Bukonkin ListenerSP listener_sp(Listener::MakeListener("dummy"));
2103b43f006SIlya Bukonkin ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
2113b43f006SIlya Bukonkin ASSERT_TRUE(process_sp);
2123b43f006SIlya Bukonkin
2133b43f006SIlya Bukonkin DummyProcess *process = static_cast<DummyProcess *>(process_sp.get());
2143b43f006SIlya Bukonkin
2153b43f006SIlya Bukonkin ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
2163b43f006SIlya Bukonkin ASSERT_TRUE(thread_sp);
2173b43f006SIlya Bukonkin
2183b43f006SIlya Bukonkin StopInfoSP stopinfo_sp =
2193b43f006SIlya Bukonkin StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0);
2203b43f006SIlya Bukonkin ASSERT_TRUE(stopinfo_sp);
2213b43f006SIlya Bukonkin
2223b43f006SIlya Bukonkin thread_sp->SetStopInfo(stopinfo_sp);
2233b43f006SIlya Bukonkin
2243b43f006SIlya Bukonkin /*
2253b43f006SIlya Bukonkin Should make stopinfo valid if thread is at last breakpoint hit.
2263b43f006SIlya Bukonkin */
2273b43f006SIlya Bukonkin process->GetModIDNonConstRef().BumpStopID();
2283b43f006SIlya Bukonkin ASSERT_TRUE(stopinfo_sp->IsValid() == false);
2293b43f006SIlya Bukonkin StopInfoSP new_stopinfo_sp = thread_sp->GetPrivateStopInfo();
2303b43f006SIlya Bukonkin ASSERT_TRUE(new_stopinfo_sp && stopinfo_sp->IsValid() == true);
2313b43f006SIlya Bukonkin }
232