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 bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override { 46 return true; 47 } 48 Status DoDestroy() override { return {}; } 49 void RefreshStateAfterStop() override {} 50 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 51 Status &error) override { 52 return 0; 53 } 54 bool DoUpdateThreadList(ThreadList &old_thread_list, 55 ThreadList &new_thread_list) override { 56 return false; 57 } 58 llvm::StringRef GetPluginName() override { return "Dummy"; } 59 60 ProcessModID &GetModIDNonConstRef() { return m_mod_id; } 61 }; 62 63 class DummyThread : public Thread { 64 public: 65 using Thread::Thread; 66 67 ~DummyThread() override { DestroyThread(); } 68 69 void RefreshStateAfterStop() override {} 70 71 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; } 72 73 lldb::RegisterContextSP 74 CreateRegisterContextForFrame(StackFrame *frame) override { 75 return nullptr; 76 } 77 78 bool CalculateStopInfo() override { return false; } 79 80 bool IsStillAtLastBreakpointHit() override { return true; } 81 }; 82 } // namespace 83 84 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) { 85 PlatformSP platform_sp; 86 TargetSP target_sp; 87 debugger_sp->GetTargetList().CreateTarget( 88 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); 89 90 return target_sp; 91 } 92 93 TEST_F(ThreadTest, SetStopInfo) { 94 ArchSpec arch("powerpc64-pc-linux"); 95 96 Platform::SetHostPlatform( 97 platform_linux::PlatformLinux::CreateInstance(true, &arch)); 98 99 DebuggerSP debugger_sp = Debugger::CreateInstance(); 100 ASSERT_TRUE(debugger_sp); 101 102 TargetSP target_sp = CreateTarget(debugger_sp, arch); 103 ASSERT_TRUE(target_sp); 104 105 ListenerSP listener_sp(Listener::MakeListener("dummy")); 106 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 107 ASSERT_TRUE(process_sp); 108 109 DummyProcess *process = static_cast<DummyProcess *>(process_sp.get()); 110 111 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 112 ASSERT_TRUE(thread_sp); 113 114 StopInfoSP stopinfo_sp = 115 StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0); 116 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 117 118 /* 119 Should make stopinfo valid. 120 */ 121 process->GetModIDNonConstRef().BumpStopID(); 122 ASSERT_TRUE(stopinfo_sp->IsValid() == false); 123 124 thread_sp->SetStopInfo(stopinfo_sp); 125 ASSERT_TRUE(stopinfo_sp->IsValid() == true); 126 } 127 128 TEST_F(ThreadTest, GetPrivateStopInfo) { 129 ArchSpec arch("powerpc64-pc-linux"); 130 131 Platform::SetHostPlatform( 132 platform_linux::PlatformLinux::CreateInstance(true, &arch)); 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