1 //===-- ThreadTest.cpp ------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/Thread.h" 10 #include "Plugins/Platform/Linux/PlatformLinux.h" 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Host/FileSystem.h" 13 #include "lldb/Host/HostInfo.h" 14 #include "lldb/Target/Process.h" 15 #include "lldb/Target/StopInfo.h" 16 #include "lldb/Utility/ArchSpec.h" 17 #include "gtest/gtest.h" 18 19 using namespace lldb_private; 20 using namespace lldb_private::repro; 21 using namespace lldb; 22 23 namespace { 24 class ThreadTest : public ::testing::Test { 25 public: 26 void SetUp() override { 27 FileSystem::Initialize(); 28 HostInfo::Initialize(); 29 platform_linux::PlatformLinux::Initialize(); 30 } 31 void TearDown() override { 32 platform_linux::PlatformLinux::Terminate(); 33 HostInfo::Terminate(); 34 FileSystem::Terminate(); 35 } 36 }; 37 38 class DummyProcess : public Process { 39 public: 40 DummyProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp) 41 : Process(target_sp, listener_sp) {} 42 43 bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override { 44 return true; 45 } 46 Status DoDestroy() override { return {}; } 47 void RefreshStateAfterStop() override {} 48 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 49 Status &error) override { 50 return 0; 51 } 52 bool DoUpdateThreadList(ThreadList &old_thread_list, 53 ThreadList &new_thread_list) override { 54 return false; 55 } 56 llvm::StringRef GetPluginName() override { return "Dummy"; } 57 58 ProcessModID &GetModIDNonConstRef() { return m_mod_id; } 59 }; 60 61 class DummyThread : public Thread { 62 public: 63 using Thread::Thread; 64 65 ~DummyThread() override { DestroyThread(); } 66 67 void RefreshStateAfterStop() override {} 68 69 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; } 70 71 lldb::RegisterContextSP 72 CreateRegisterContextForFrame(StackFrame *frame) override { 73 return nullptr; 74 } 75 76 bool CalculateStopInfo() override { return false; } 77 78 bool IsStillAtLastBreakpointHit() override { return true; } 79 }; 80 } // namespace 81 82 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) { 83 PlatformSP platform_sp; 84 TargetSP target_sp; 85 debugger_sp->GetTargetList().CreateTarget( 86 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); 87 88 return target_sp; 89 } 90 91 TEST_F(ThreadTest, SetStopInfo) { 92 ArchSpec arch("powerpc64-pc-linux"); 93 94 Platform::SetHostPlatform(platform_linux::PlatformLinux::CreateInstance( 95 true, &arch, /*debugger=*/nullptr, 96 /*metadata=*/nullptr)); 97 98 DebuggerSP debugger_sp = Debugger::CreateInstance(); 99 ASSERT_TRUE(debugger_sp); 100 101 TargetSP target_sp = CreateTarget(debugger_sp, arch); 102 ASSERT_TRUE(target_sp); 103 104 ListenerSP listener_sp(Listener::MakeListener("dummy")); 105 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 106 ASSERT_TRUE(process_sp); 107 108 DummyProcess *process = static_cast<DummyProcess *>(process_sp.get()); 109 110 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 111 ASSERT_TRUE(thread_sp); 112 113 StopInfoSP stopinfo_sp = 114 StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0); 115 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 116 117 /* 118 Should make stopinfo valid. 119 */ 120 process->GetModIDNonConstRef().BumpStopID(); 121 ASSERT_TRUE(stopinfo_sp->IsValid() == false); 122 123 thread_sp->SetStopInfo(stopinfo_sp); 124 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 125 } 126 127 TEST_F(ThreadTest, GetPrivateStopInfo) { 128 ArchSpec arch("powerpc64-pc-linux"); 129 130 Platform::SetHostPlatform(platform_linux::PlatformLinux::CreateInstance( 131 true, &arch, /*debugger=*/nullptr, 132 /*metadata=*/nullptr)); 133 134 DebuggerSP debugger_sp = Debugger::CreateInstance(); 135 ASSERT_TRUE(debugger_sp); 136 137 TargetSP target_sp = CreateTarget(debugger_sp, arch); 138 ASSERT_TRUE(target_sp); 139 140 ListenerSP listener_sp(Listener::MakeListener("dummy")); 141 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 142 ASSERT_TRUE(process_sp); 143 144 DummyProcess *process = static_cast<DummyProcess *>(process_sp.get()); 145 146 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 147 ASSERT_TRUE(thread_sp); 148 149 StopInfoSP stopinfo_sp = 150 StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0); 151 ASSERT_TRUE(stopinfo_sp); 152 153 thread_sp->SetStopInfo(stopinfo_sp); 154 155 /* 156 Should make stopinfo valid if thread is at last breakpoint hit. 157 */ 158 process->GetModIDNonConstRef().BumpStopID(); 159 ASSERT_TRUE(stopinfo_sp->IsValid() == false); 160 StopInfoSP new_stopinfo_sp = thread_sp->GetPrivateStopInfo(); 161 ASSERT_TRUE(new_stopinfo_sp && stopinfo_sp->IsValid() == true); 162 } 163