[AArch64,ELF] Restrict MOVZ/MOVK to non-PIC large code model (#70178)
[llvm-project.git] / lldb / unittests / Process / ProcessEventDataTest.cpp
blobdd159496cd881db04a256783b2e4efae50e39ae6
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 //===----------------------------------------------------------------------===//
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"
21 using namespace lldb_private;
22 using namespace lldb_private::repro;
23 using namespace lldb;
25 namespace {
26 class ProcessEventDataTest : public ::testing::Test {
27 public:
28 void SetUp() override {
29 FileSystem::Initialize();
30 HostInfo::Initialize();
31 PlatformMacOSX::Initialize();
33 void TearDown() override {
34 PlatformMacOSX::Terminate();
35 HostInfo::Terminate();
36 FileSystem::Terminate();
40 class DummyProcess : public Process {
41 public:
42 DummyProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
43 : Process(target_sp, listener_sp) {}
45 bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override {
46 return true;
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;
54 bool DoUpdateThreadList(ThreadList &old_thread_list,
55 ThreadList &new_thread_list) override {
56 return false;
58 llvm::StringRef GetPluginName() override { return "Dummy"; }
60 ProcessModID &GetModIDNonConstRef() { return m_mod_id; }
63 class DummyThread : public Thread {
64 public:
65 using Thread::Thread;
67 ~DummyThread() override { DestroyThread(); }
69 void RefreshStateAfterStop() override {}
71 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; }
73 lldb::RegisterContextSP
74 CreateRegisterContextForFrame(StackFrame *frame) override {
75 return nullptr;
78 bool CalculateStopInfo() override { return false; }
81 class DummyStopInfo : public StopInfo {
82 public:
83 DummyStopInfo(Thread &thread, uint64_t value) : StopInfo(thread, value) {}
85 bool ShouldStop(Event *event_ptr) override { return m_should_stop; }
87 StopReason GetStopReason() const override { return m_stop_reason; }
89 bool m_should_stop = true;
90 StopReason m_stop_reason = eStopReasonBreakpoint;
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;
102 int m_should_stop_hit_count = 0;
104 } // namespace
106 typedef std::shared_ptr<Process::ProcessEventData> ProcessEventDataSP;
107 typedef std::shared_ptr<Event> EventSP;
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);
115 return target_sp;
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;
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;
134 thread_sp->SetStopInfo(stopinfo_sp);
137 process_sp->GetThreadList().AddThread(thread_sp);
139 return thread_sp;
142 TEST_F(ProcessEventDataTest, DoOnRemoval) {
143 ArchSpec arch("x86_64-apple-macosx-");
145 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
147 DebuggerSP debugger_sp = Debugger::CreateInstance();
148 ASSERT_TRUE(debugger_sp);
150 TargetSP target_sp = CreateTarget(debugger_sp, arch);
151 ASSERT_TRUE(target_sp);
153 ListenerSP listener_sp(Listener::MakeListener("dummy"));
154 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
155 ASSERT_TRUE(process_sp);
158 Should hit ShouldStop if state is eStateStopped
160 ProcessEventDataSP event_data_sp =
161 std::make_shared<DummyProcessEventData>(process_sp, eStateStopped);
162 EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
163 event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
164 event_data_sp->DoOnRemoval(event_sp.get());
165 bool result = static_cast<DummyProcessEventData *>(event_data_sp.get())
166 ->m_should_stop_hit_count == 1;
167 ASSERT_TRUE(result);
170 Should not hit ShouldStop if state is not eStateStopped
172 event_data_sp =
173 std::make_shared<DummyProcessEventData>(process_sp, eStateStepping);
174 event_sp = std::make_shared<Event>(0, event_data_sp);
175 event_data_sp->SetUpdateStateOnRemoval(event_sp.get());
176 event_data_sp->DoOnRemoval(event_sp.get());
177 result = static_cast<DummyProcessEventData *>(event_data_sp.get())
178 ->m_should_stop_hit_count == 0;
179 ASSERT_TRUE(result);
182 TEST_F(ProcessEventDataTest, ShouldStop) {
183 ArchSpec arch("x86_64-apple-macosx-");
185 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
187 DebuggerSP debugger_sp = Debugger::CreateInstance();
188 ASSERT_TRUE(debugger_sp);
190 TargetSP target_sp = CreateTarget(debugger_sp, arch);
191 ASSERT_TRUE(target_sp);
193 ListenerSP listener_sp(Listener::MakeListener("dummy"));
194 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
195 ASSERT_TRUE(process_sp);
197 // wants to stop and has valid StopInfo
198 ThreadSP thread_sp = CreateThread(process_sp, true, true);
200 ProcessEventDataSP event_data_sp =
201 std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
202 EventSP event_sp = std::make_shared<Event>(0, event_data_sp);
204 Should stop if thread has valid StopInfo and not suspended
206 bool found_valid_stopinfo = false;
207 bool should_stop =
208 event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
209 ASSERT_TRUE(should_stop == true && found_valid_stopinfo == true);
212 Should not stop if thread has valid StopInfo but was suspended
214 found_valid_stopinfo = false;
215 thread_sp->SetResumeState(eStateSuspended);
216 should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
217 ASSERT_TRUE(should_stop == false && found_valid_stopinfo == false);
220 Should not stop, thread-reason of stop does not want to stop in its
221 callback and suspended thread who wants to (from previous stop)
222 must be ignored.
224 event_data_sp =
225 std::make_shared<Process::ProcessEventData>(process_sp, eStateStopped);
226 event_sp = std::make_shared<Event>(0, event_data_sp);
227 process_sp->GetThreadList().Clear();
229 for (int i = 0; i < 6; i++) {
230 if (i == 2) {
231 // Does not want to stop but has valid StopInfo
232 thread_sp = CreateThread(process_sp, false, true);
233 } else if (i == 5) {
234 // Wants to stop and has valid StopInfo
235 thread_sp = CreateThread(process_sp, true, true);
236 thread_sp->SetResumeState(eStateSuspended);
237 } else {
238 // Thread has no StopInfo i.e is not the reason of stop
239 thread_sp = CreateThread(process_sp, false, false);
242 ASSERT_TRUE(process_sp->GetThreadList().GetSize() == 6);
244 found_valid_stopinfo = false;
245 should_stop = event_data_sp->ShouldStop(event_sp.get(), found_valid_stopinfo);
246 ASSERT_TRUE(should_stop == false && found_valid_stopinfo == true);