xref: /llvm-project/lldb/unittests/Process/ProcessEventDataTest.cpp (revision 2d53527e9c64c70c24e1abba74fa0a8c8b3392b1)
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 "gtest/gtest.h"
20 
21 using namespace lldb_private;
22 using namespace lldb_private::repro;
23 using namespace lldb;
24 
25 namespace {
26 class ProcessEventDataTest : public ::testing::Test {
27 public:
28   void SetUp() override {
29     FileSystem::Initialize();
30     HostInfo::Initialize();
31     PlatformMacOSX::Initialize();
32   }
33   void TearDown() override {
34     PlatformMacOSX::Terminate();
35     HostInfo::Terminate();
36     FileSystem::Terminate();
37   }
38 };
39 
40 class DummyProcess : public Process {
41 public:
42   DummyProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
43       : Process(target_sp, listener_sp) {}
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 
81 class DummyStopInfo : public StopInfo {
82 public:
83   DummyStopInfo(Thread &thread, uint64_t value) : StopInfo(thread, value) {}
84 
85   bool ShouldStop(Event *event_ptr) override { return m_should_stop; }
86 
87   StopReason GetStopReason() const override { return m_stop_reason; }
88 
89   bool m_should_stop = true;
90   StopReason m_stop_reason = eStopReasonBreakpoint;
91 };
92 
93 class DummyProcessEventData : public Process::ProcessEventData {
94 public:
95   DummyProcessEventData(ProcessSP &process_sp, StateType state)
96       : ProcessEventData(process_sp, state) {}
97   bool ShouldStop(Event *event_ptr, bool &found_valid_stopinfo) override {
98     m_should_stop_hit_count++;
99     return false;
100   }
101 
102   int m_should_stop_hit_count = 0;
103 };
104 } // namespace
105 
106 typedef std::shared_ptr<Process::ProcessEventData> ProcessEventDataSP;
107 typedef std::shared_ptr<Event> EventSP;
108 
109 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) {
110   PlatformSP platform_sp;
111   TargetSP target_sp;
112   debugger_sp->GetTargetList().CreateTarget(
113       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
114 
115   return target_sp;
116 }
117 
118 ThreadSP CreateThread(ProcessSP &process_sp, bool should_stop,
119                       bool has_valid_stopinfo) {
120   ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0);
121   if (thread_sp == nullptr) {
122     return nullptr;
123   }
124 
125   if (has_valid_stopinfo) {
126     StopInfoSP stopinfo_sp =
127         std::make_shared<DummyStopInfo>(*thread_sp.get(), 0);
128     static_cast<DummyStopInfo *>(stopinfo_sp.get())->m_should_stop =
129         should_stop;
130     if (stopinfo_sp == nullptr) {
131       return nullptr;
132     }
133 
134     thread_sp->SetStopInfo(stopinfo_sp);
135   }
136 
137   process_sp->GetThreadList().AddThread(thread_sp);
138 
139   return thread_sp;
140 }
141 
142 TEST_F(ProcessEventDataTest, DoOnRemoval) {
143   ArchSpec arch("x86_64-apple-macosx-");
144 
145   Platform::SetHostPlatform(
146       PlatformRemoteMacOSX::CreateInstance(true, &arch, /*debugger=*/nullptr,
147                                            /*metadata=*/nullptr));
148 
149   DebuggerSP debugger_sp = Debugger::CreateInstance();
150   ASSERT_TRUE(debugger_sp);
151 
152   TargetSP target_sp = CreateTarget(debugger_sp, arch);
153   ASSERT_TRUE(target_sp);
154 
155   ListenerSP listener_sp(Listener::MakeListener("dummy"));
156   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
157   ASSERT_TRUE(process_sp);
158 
159   /*
160    Should hit ShouldStop if state is eStateStopped
161    */
162   ProcessEventDataSP event_data_sp =
163       std::make_shared<DummyProcessEventData>(process_sp, eStateStopped);
164   EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
165   event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
166   event_data_sp->DoOnRemoval(event_sp.get());
167   bool result = static_cast<DummyProcessEventData *>(event_data_sp.get())
168                     ->m_should_stop_hit_count == 1;
169   ASSERT_TRUE(result);
170 
171   /*
172    Should not hit ShouldStop if state is not eStateStopped
173    */
174   event_data_sp =
175       std::make_shared<DummyProcessEventData>(process_sp, eStateStepping);
176   event_sp = std::make_shared<Event>(0, event_data_sp);
177   event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
178   event_data_sp->DoOnRemoval(event_sp.get());
179   result = static_cast<DummyProcessEventData *>(event_data_sp.get())
180                ->m_should_stop_hit_count == 0;
181   ASSERT_TRUE(result);
182 }
183 
184 TEST_F(ProcessEventDataTest, ShouldStop) {
185   ArchSpec arch("x86_64-apple-macosx-");
186 
187   Platform::SetHostPlatform(
188       PlatformRemoteMacOSX::CreateInstance(true, &arch, /*debugger=*/nullptr,
189                                            /*metadata=*/nullptr));
190 
191   DebuggerSP debugger_sp = Debugger::CreateInstance();
192   ASSERT_TRUE(debugger_sp);
193 
194   TargetSP target_sp = CreateTarget(debugger_sp, arch);
195   ASSERT_TRUE(target_sp);
196 
197   ListenerSP listener_sp(Listener::MakeListener("dummy"));
198   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
199   ASSERT_TRUE(process_sp);
200 
201   // wants to stop and has valid StopInfo
202   ThreadSP thread_sp = CreateThread(process_sp, true, true);
203 
204   ProcessEventDataSP event_data_sp =
205       std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
206   EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
207   /*
208    Should stop if thread has valid StopInfo and not suspended
209    */
210   bool found_valid_stopinfo = false;
211   bool should_stop =
212       event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
213   ASSERT_TRUE(should_stop == true && found_valid_stopinfo == true);
214 
215   /*
216    Should not stop if thread has valid StopInfo but was suspended
217    */
218   found_valid_stopinfo = false;
219   thread_sp->SetResumeState(eStateSuspended);
220   should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
221   ASSERT_TRUE(should_stop == false && found_valid_stopinfo == false);
222 
223   /*
224    Should not stop, thread-reason of stop does not want to stop in its
225    callback and suspended thread who wants to (from previous stop)
226    must be ignored.
227    */
228   event_data_sp =
229       std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
230   event_sp = std::make_shared<Event>(0, event_data_sp);
231   process_sp->GetThreadList().Clear();
232 
233   for (int i = 0; i < 6; i++) {
234     if (i == 2) {
235       // Does not want to stop but has valid StopInfo
236       thread_sp = CreateThread(process_sp, false, true);
237     } else if (i == 5) {
238       // Wants to stop and has valid StopInfo
239       thread_sp = CreateThread(process_sp, true, true);
240       thread_sp->SetResumeState(eStateSuspended);
241     } else {
242       // Thread has no StopInfo i.e is not the reason of stop
243       thread_sp = CreateThread(process_sp, false, false);
244     }
245   }
246   ASSERT_TRUE(process_sp->GetThreadList().GetSize() == 6);
247 
248   found_valid_stopinfo = false;
249   should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
250   ASSERT_TRUE(should_stop == false && found_valid_stopinfo == true);
251 }
252