xref: /llvm-project/lldb/unittests/Thread/ThreadTest.cpp (revision 2634ec6ce9007f2406545ca28b4c72961f1e8f67)
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   PlatformSP platform_sp;
87   TargetSP target_sp;
88   debugger_sp->GetTargetList().CreateTarget(
89       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
90 
91   return target_sp;
92 }
93 
94 TEST_F(ThreadTest, SetStopInfo) {
95   ArchSpec arch("powerpc64-pc-linux");
96 
97   Platform::SetHostPlatform(
98       platform_linux::PlatformLinux::CreateInstance(true, &arch));
99 
100   DebuggerSP debugger_sp = Debugger::CreateInstance();
101   ASSERT_TRUE(debugger_sp);
102 
103   TargetSP target_sp = CreateTarget(debugger_sp, arch);
104   ASSERT_TRUE(target_sp);
105 
106   ListenerSP listener_sp(Listener::MakeListener("dummy"));
107   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
108   ASSERT_TRUE(process_sp);
109 
110   DummyProcess *process = static_cast<DummyProcess *>(process_sp.get());
111 
112   ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
113   ASSERT_TRUE(thread_sp);
114 
115   StopInfoSP stopinfo_sp =
116       StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0);
117   ASSERT_TRUE(stopinfo_sp->IsValid() == true);
118 
119   /*
120    Should make stopinfo valid.
121    */
122   process->GetModIDNonConstRef().BumpStopID();
123   ASSERT_TRUE(stopinfo_sp->IsValid() == false);
124 
125   thread_sp->SetStopInfo(stopinfo_sp);
126   ASSERT_TRUE(stopinfo_sp->IsValid() == true);
127 }
128 
129 TEST_F(ThreadTest, GetPrivateStopInfo) {
130   ArchSpec arch("powerpc64-pc-linux");
131 
132   Platform::SetHostPlatform(
133       platform_linux::PlatformLinux::CreateInstance(true, &arch));
134 
135   DebuggerSP debugger_sp = Debugger::CreateInstance();
136   ASSERT_TRUE(debugger_sp);
137 
138   TargetSP target_sp = CreateTarget(debugger_sp, arch);
139   ASSERT_TRUE(target_sp);
140 
141   ListenerSP listener_sp(Listener::MakeListener("dummy"));
142   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
143   ASSERT_TRUE(process_sp);
144 
145   DummyProcess *process = static_cast<DummyProcess *>(process_sp.get());
146 
147   ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
148   ASSERT_TRUE(thread_sp);
149 
150   StopInfoSP stopinfo_sp =
151       StopInfo::CreateStopReasonWithBreakpointSiteID(*thread_sp.get(), 0);
152   ASSERT_TRUE(stopinfo_sp);
153 
154   thread_sp->SetStopInfo(stopinfo_sp);
155 
156   /*
157    Should make stopinfo valid if thread is at last breakpoint hit.
158    */
159   process->GetModIDNonConstRef().BumpStopID();
160   ASSERT_TRUE(stopinfo_sp->IsValid() == false);
161   StopInfoSP new_stopinfo_sp = thread_sp->GetPrivateStopInfo();
162   ASSERT_TRUE(new_stopinfo_sp && stopinfo_sp->IsValid() == true);
163 }
164