1 //===-- ProcessEventDataTest.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 "Plugins/Platform/MacOSX/PlatformMacOSX.h"
10 #include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.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/Target/Thread.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/Event.h"
19 #include "lldb/Utility/Reproducer.h"
20 #include "gtest/gtest.h"
21
22 using namespace lldb_private;
23 using namespace lldb_private::repro;
24 using namespace lldb;
25
26 namespace {
27 class ProcessEventDataTest : public ::testing::Test {
28 public:
SetUp()29 void SetUp() override {
30 llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None));
31 FileSystem::Initialize();
32 HostInfo::Initialize();
33 PlatformMacOSX::Initialize();
34 }
TearDown()35 void TearDown() override {
36 PlatformMacOSX::Terminate();
37 HostInfo::Terminate();
38 FileSystem::Terminate();
39 Reproducer::Terminate();
40 }
41 };
42
43 class DummyProcess : public Process {
44 public:
45 using Process::Process;
46
CanDebug(lldb::TargetSP target,bool plugin_specified_by_name)47 virtual bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) {
48 return true;
49 }
DoDestroy()50 virtual Status DoDestroy() { return {}; }
RefreshStateAfterStop()51 virtual void RefreshStateAfterStop() {}
DoReadMemory(lldb::addr_t vm_addr,void * buf,size_t size,Status & error)52 virtual size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
53 Status &error) {
54 return 0;
55 }
UpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)56 virtual bool UpdateThreadList(ThreadList &old_thread_list,
57 ThreadList &new_thread_list) {
58 return false;
59 }
GetPluginName()60 virtual ConstString GetPluginName() { return ConstString("Dummy"); }
GetPluginVersion()61 virtual uint32_t GetPluginVersion() { return 0; }
62
GetModIDNonConstRef()63 ProcessModID &GetModIDNonConstRef() { return m_mod_id; }
64 };
65
66 class DummyThread : public Thread {
67 public:
68 using Thread::Thread;
69
~DummyThread()70 ~DummyThread() override { DestroyThread(); }
71
RefreshStateAfterStop()72 void RefreshStateAfterStop() override {}
73
GetRegisterContext()74 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; }
75
76 lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)77 CreateRegisterContextForFrame(StackFrame *frame) override {
78 return nullptr;
79 }
80
CalculateStopInfo()81 bool CalculateStopInfo() override { return false; }
82 };
83
84 class DummyStopInfo : public StopInfo {
85 public:
DummyStopInfo(Thread & thread,uint64_t value)86 DummyStopInfo(Thread &thread, uint64_t value)
87 : StopInfo(thread, value), m_should_stop(true),
88 m_stop_reason(eStopReasonBreakpoint) {}
89
ShouldStop(Event * event_ptr)90 bool ShouldStop(Event *event_ptr) override { return m_should_stop; }
91
GetStopReason() const92 StopReason GetStopReason() const override { return m_stop_reason; }
93
94 bool m_should_stop;
95 StopReason m_stop_reason;
96 };
97
98 class DummyProcessEventData : public Process::ProcessEventData {
99 public:
DummyProcessEventData(ProcessSP & process_sp,StateType state)100 DummyProcessEventData(ProcessSP &process_sp, StateType state)
101 : ProcessEventData(process_sp, state), m_should_stop_hit_count(0) {}
ShouldStop(Event * event_ptr,bool & found_valid_stopinfo)102 bool ShouldStop(Event *event_ptr, bool &found_valid_stopinfo) override {
103 m_should_stop_hit_count++;
104 return false;
105 }
106
107 int m_should_stop_hit_count;
108 };
109 } // namespace
110
111 typedef std::shared_ptr<Process::ProcessEventData> ProcessEventDataSP;
112 typedef std::shared_ptr<Event> EventSP;
113
CreateTarget(DebuggerSP & debugger_sp,ArchSpec & arch)114 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) {
115 Status error;
116 PlatformSP platform_sp;
117 TargetSP target_sp;
118 error = debugger_sp->GetTargetList().CreateTarget(
119 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
120
121 if (target_sp) {
122 debugger_sp->GetTargetList().SetSelectedTarget(target_sp.get());
123 }
124
125 return target_sp;
126 }
127
CreateThread(ProcessSP & process_sp,bool should_stop,bool has_valid_stopinfo)128 ThreadSP CreateThread(ProcessSP &process_sp, bool should_stop,
129 bool has_valid_stopinfo) {
130 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
131 if (thread_sp == nullptr) {
132 return nullptr;
133 }
134
135 if (has_valid_stopinfo) {
136 StopInfoSP stopinfo_sp =
137 std::make_shared<DummyStopInfo>(*thread_sp.get(), 0);
138 static_cast<DummyStopInfo *>(stopinfo_sp.get())->m_should_stop =
139 should_stop;
140 if (stopinfo_sp == nullptr) {
141 return nullptr;
142 }
143
144 thread_sp->SetStopInfo(stopinfo_sp);
145 }
146
147 process_sp->GetThreadList().AddThread(thread_sp);
148
149 return thread_sp;
150 }
151
TEST_F(ProcessEventDataTest,DoOnRemoval)152 TEST_F(ProcessEventDataTest, DoOnRemoval) {
153 ArchSpec arch("x86_64-apple-macosx-");
154
155 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
156
157 DebuggerSP debugger_sp = Debugger::CreateInstance();
158 ASSERT_TRUE(debugger_sp);
159
160 TargetSP target_sp = CreateTarget(debugger_sp, arch);
161 ASSERT_TRUE(target_sp);
162
163 ListenerSP listener_sp(Listener::MakeListener("dummy"));
164 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
165 ASSERT_TRUE(process_sp);
166
167 /*
168 Should hit ShouldStop if state is eStateStopped
169 */
170 ProcessEventDataSP event_data_sp =
171 std::make_shared<DummyProcessEventData>(process_sp, eStateStopped);
172 EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
173 event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
174 event_data_sp->DoOnRemoval(event_sp.get());
175 bool result = static_cast<DummyProcessEventData *>(event_data_sp.get())
176 ->m_should_stop_hit_count == 1;
177 ASSERT_TRUE(result);
178
179 /*
180 Should not hit ShouldStop if state is not eStateStopped
181 */
182 event_data_sp =
183 std::make_shared<DummyProcessEventData>(process_sp, eStateStepping);
184 event_sp = std::make_shared<Event>(0, event_data_sp);
185 event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
186 event_data_sp->DoOnRemoval(event_sp.get());
187 result = static_cast<DummyProcessEventData *>(event_data_sp.get())
188 ->m_should_stop_hit_count == 0;
189 ASSERT_TRUE(result);
190 }
191
TEST_F(ProcessEventDataTest,ShouldStop)192 TEST_F(ProcessEventDataTest, ShouldStop) {
193 ArchSpec arch("x86_64-apple-macosx-");
194
195 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
196
197 DebuggerSP debugger_sp = Debugger::CreateInstance();
198 ASSERT_TRUE(debugger_sp);
199
200 TargetSP target_sp = CreateTarget(debugger_sp, arch);
201 ASSERT_TRUE(target_sp);
202
203 ListenerSP listener_sp(Listener::MakeListener("dummy"));
204 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
205 ASSERT_TRUE(process_sp);
206
207 // wants to stop and has valid StopInfo
208 ThreadSP thread_sp = CreateThread(process_sp, true, true);
209
210 ProcessEventDataSP event_data_sp =
211 std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
212 EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
213 /*
214 Should stop if thread has valid StopInfo and not suspended
215 */
216 bool found_valid_stopinfo = false;
217 bool should_stop =
218 event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
219 ASSERT_TRUE(should_stop == true && found_valid_stopinfo == true);
220
221 /*
222 Should not stop if thread has valid StopInfo but was suspended
223 */
224 found_valid_stopinfo = false;
225 thread_sp->SetResumeState(eStateSuspended);
226 should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
227 ASSERT_TRUE(should_stop == false && found_valid_stopinfo == false);
228
229 /*
230 Should not stop, thread-reason of stop does not want to stop in its
231 callback and suspended thread who wants to (from previous stop)
232 must be ignored.
233 */
234 event_data_sp =
235 std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
236 event_sp = std::make_shared<Event>(0, event_data_sp);
237 process_sp->GetThreadList().Clear();
238
239 for (int i = 0; i < 6; i++) {
240 if (i == 2) {
241 // Does not want to stop but has valid StopInfo
242 thread_sp = CreateThread(process_sp, false, true);
243 } else if (i == 5) {
244 // Wants to stop and has valid StopInfo
245 thread_sp = CreateThread(process_sp, true, true);
246 thread_sp->SetResumeState(eStateSuspended);
247 } else {
248 // Thread has no StopInfo i.e is not the reason of stop
249 thread_sp = CreateThread(process_sp, false, false);
250 }
251 }
252 ASSERT_TRUE(process_sp->GetThreadList().GetSize() == 6);
253
254 found_valid_stopinfo = false;
255 should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
256 ASSERT_TRUE(should_stop == false && found_valid_stopinfo == true);
257 }
258