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 "lldb/Utility/Reproducer.h" 18 #include "gtest/gtest.h" 19 20 using namespace lldb_private; 21 using namespace lldb_private::repro; 22 using namespace lldb; 23 24 namespace { 25 class ThreadTest : public ::testing::Test { 26 public: 27 void SetUp() override { 28 llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None)); 29 FileSystem::Initialize(); 30 HostInfo::Initialize(); 31 platform_linux::PlatformLinux::Initialize(); 32 } 33 void TearDown() override { 34 platform_linux::PlatformLinux::Terminate(); 35 HostInfo::Terminate(); 36 FileSystem::Terminate(); 37 Reproducer::Terminate(); 38 } 39 }; 40 41 class DummyProcess : public Process { 42 public: 43 using Process::Process; 44 45 virtual bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) { 46 return true; 47 } 48 virtual Status DoDestroy() { return {}; } 49 virtual void RefreshStateAfterStop() {} 50 virtual size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 51 Status &error) { 52 return 0; 53 } 54 virtual bool UpdateThreadList(ThreadList &old_thread_list, 55 ThreadList &new_thread_list) { 56 return false; 57 } 58 virtual ConstString GetPluginName() { return ConstString("Dummy"); } 59 virtual uint32_t GetPluginVersion() { return 0; } 60 61 ProcessModID &GetModIDNonConstRef() { return m_mod_id; } 62 }; 63 64 class DummyThread : public Thread { 65 public: 66 using Thread::Thread; 67 68 ~DummyThread() override { DestroyThread(); } 69 70 void RefreshStateAfterStop() override {} 71 72 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; } 73 74 lldb::RegisterContextSP 75 CreateRegisterContextForFrame(StackFrame *frame) override { 76 return nullptr; 77 } 78 79 bool CalculateStopInfo() override { return false; } 80 81 bool IsStillAtLastBreakpointHit() override { return true; } 82 }; 83 } // namespace 84 85 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) { 86 Status error; 87 PlatformSP platform_sp; 88 TargetSP target_sp; 89 error = debugger_sp->GetTargetList().CreateTarget( 90 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); 91 92 if (target_sp) { 93 debugger_sp->GetTargetList().SetSelectedTarget(target_sp.get()); 94 } 95 96 return target_sp; 97 } 98 99 TEST_F(ThreadTest, SetStopInfo) { 100 ArchSpec arch("powerpc64-pc-linux"); 101 102 Platform::SetHostPlatform( 103 platform_linux::PlatformLinux::CreateInstance(true, &arch)); 104 105 DebuggerSP debugger_sp = Debugger::CreateInstance(); 106 ASSERT_TRUE(debugger_sp); 107 108 TargetSP target_sp = CreateTarget(debugger_sp, arch); 109 ASSERT_TRUE(target_sp); 110 111 ListenerSP listener_sp(Listener::MakeListener("dummy")); 112 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 113 ASSERT_TRUE(process_sp); 114 115 DummyProcess *process = static_cast<DummyProcess *>(process_sp.get()); 116 117 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 118 ASSERT_TRUE(thread_sp); 119 120 StopInfoSP stopinfo_sp = 121 StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0); 122 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 123 124 /* 125 Should make stopinfo valid. 126 */ 127 process->GetModIDNonConstRef().BumpStopID(); 128 ASSERT_TRUE(stopinfo_sp->IsValid() == false); 129 130 thread_sp->SetStopInfo(stopinfo_sp); 131 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 132 } 133 134 TEST_F(ThreadTest, GetPrivateStopInfo) { 135 ArchSpec arch("powerpc64-pc-linux"); 136 137 Platform::SetHostPlatform( 138 platform_linux::PlatformLinux::CreateInstance(true, &arch)); 139 140 DebuggerSP debugger_sp = Debugger::CreateInstance(); 141 ASSERT_TRUE(debugger_sp); 142 143 TargetSP target_sp = CreateTarget(debugger_sp, arch); 144 ASSERT_TRUE(target_sp); 145 146 ListenerSP listener_sp(Listener::MakeListener("dummy")); 147 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 148 ASSERT_TRUE(process_sp); 149 150 DummyProcess *process = static_cast<DummyProcess *>(process_sp.get()); 151 152 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 153 ASSERT_TRUE(thread_sp); 154 155 StopInfoSP stopinfo_sp = 156 StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0); 157 ASSERT_TRUE(stopinfo_sp); 158 159 thread_sp->SetStopInfo(stopinfo_sp); 160 161 /* 162 Should make stopinfo valid if thread is at last breakpoint hit. 163 */ 164 process->GetModIDNonConstRef().BumpStopID(); 165 ASSERT_TRUE(stopinfo_sp->IsValid() == false); 166 StopInfoSP new_stopinfo_sp = thread_sp->GetPrivateStopInfo(); 167 ASSERT_TRUE(new_stopinfo_sp && stopinfo_sp->IsValid() == true); 168 } 169