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: 29 void SetUp() override { 30 llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None)); 31 FileSystem::Initialize(); 32 HostInfo::Initialize(); 33 PlatformMacOSX::Initialize(); 34 } 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 47 bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override { 48 return true; 49 } 50 Status DoDestroy() override { return {}; } 51 void RefreshStateAfterStop() override {} 52 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 53 Status &error) override { 54 return 0; 55 } 56 bool DoUpdateThreadList(ThreadList &old_thread_list, 57 ThreadList &new_thread_list) override { 58 return false; 59 } 60 llvm::StringRef GetPluginName() override { return "Dummy"; } 61 62 ProcessModID &GetModIDNonConstRef() { return m_mod_id; } 63 }; 64 65 class DummyThread : public Thread { 66 public: 67 using Thread::Thread; 68 69 ~DummyThread() override { DestroyThread(); } 70 71 void RefreshStateAfterStop() override {} 72 73 lldb::RegisterContextSP GetRegisterContext() override { return nullptr; } 74 75 lldb::RegisterContextSP 76 CreateRegisterContextForFrame(StackFrame *frame) override { 77 return nullptr; 78 } 79 80 bool CalculateStopInfo() override { return false; } 81 }; 82 83 class DummyStopInfo : public StopInfo { 84 public: 85 DummyStopInfo(Thread &thread, uint64_t value) 86 : StopInfo(thread, value), m_should_stop(true), 87 m_stop_reason(eStopReasonBreakpoint) {} 88 89 bool ShouldStop(Event *event_ptr) override { return m_should_stop; } 90 91 StopReason GetStopReason() const override { return m_stop_reason; } 92 93 bool m_should_stop; 94 StopReason m_stop_reason; 95 }; 96 97 class DummyProcessEventData : public Process::ProcessEventData { 98 public: 99 DummyProcessEventData(ProcessSP &process_sp, StateType state) 100 : ProcessEventData(process_sp, state), m_should_stop_hit_count(0) {} 101 bool ShouldStop(Event *event_ptr, bool &found_valid_stopinfo) override { 102 m_should_stop_hit_count++; 103 return false; 104 } 105 106 int m_should_stop_hit_count; 107 }; 108 } // namespace 109 110 typedef std::shared_ptr<Process::ProcessEventData> ProcessEventDataSP; 111 typedef std::shared_ptr<Event> EventSP; 112 113 TargetSP CreateTarget(DebuggerSP &debugger_sp, ArchSpec &arch) { 114 PlatformSP platform_sp; 115 TargetSP target_sp; 116 debugger_sp->GetTargetList().CreateTarget( 117 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); 118 119 return target_sp; 120 } 121 122 ThreadSP CreateThread(ProcessSP &process_sp, bool should_stop, 123 bool has_valid_stopinfo) { 124 ThreadSP thread_sp = std::make_shared<DummyThread>(*process_sp.get(), 0); 125 if (thread_sp == nullptr) { 126 return nullptr; 127 } 128 129 if (has_valid_stopinfo) { 130 StopInfoSP stopinfo_sp = 131 std::make_shared<DummyStopInfo>(*thread_sp.get(), 0); 132 static_cast<DummyStopInfo *>(stopinfo_sp.get())->m_should_stop = 133 should_stop; 134 if (stopinfo_sp == nullptr) { 135 return nullptr; 136 } 137 138 thread_sp->SetStopInfo(stopinfo_sp); 139 } 140 141 process_sp->GetThreadList().AddThread(thread_sp); 142 143 return thread_sp; 144 } 145 146 TEST_F(ProcessEventDataTest, DoOnRemoval) { 147 ArchSpec arch("x86_64-apple-macosx-"); 148 149 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch)); 150 151 DebuggerSP debugger_sp = Debugger::CreateInstance(); 152 ASSERT_TRUE(debugger_sp); 153 154 TargetSP target_sp = CreateTarget(debugger_sp, arch); 155 ASSERT_TRUE(target_sp); 156 157 ListenerSP listener_sp(Listener::MakeListener("dummy")); 158 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 159 ASSERT_TRUE(process_sp); 160 161 /* 162 Should hit ShouldStop if state is eStateStopped 163 */ 164 ProcessEventDataSP event_data_sp = 165 std::make_shared<DummyProcessEventData>(process_sp, eStateStopped); 166 EventSP event_sp = std::make_shared<Event>(0, event_data_sp); 167 event_data_sp->SetUpdateStateOnRemoval(event_sp.get()); 168 event_data_sp->DoOnRemoval(event_sp.get()); 169 bool result = static_cast<DummyProcessEventData *>(event_data_sp.get()) 170 ->m_should_stop_hit_count == 1; 171 ASSERT_TRUE(result); 172 173 /* 174 Should not hit ShouldStop if state is not eStateStopped 175 */ 176 event_data_sp = 177 std::make_shared<DummyProcessEventData>(process_sp, eStateStepping); 178 event_sp = std::make_shared<Event>(0, event_data_sp); 179 event_data_sp->SetUpdateStateOnRemoval(event_sp.get()); 180 event_data_sp->DoOnRemoval(event_sp.get()); 181 result = static_cast<DummyProcessEventData *>(event_data_sp.get()) 182 ->m_should_stop_hit_count == 0; 183 ASSERT_TRUE(result); 184 } 185 186 TEST_F(ProcessEventDataTest, ShouldStop) { 187 ArchSpec arch("x86_64-apple-macosx-"); 188 189 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch)); 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