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(PlatformRemoteMacOSX::CreateInstance(true, &arch)); 146 147 DebuggerSP debugger_sp = Debugger::CreateInstance(); 148 ASSERT_TRUE(debugger_sp); 149 150 TargetSP target_sp = CreateTarget(debugger_sp, arch); 151 ASSERT_TRUE(target_sp); 152 153 ListenerSP listener_sp(Listener::MakeListener("dummy")); 154 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 155 ASSERT_TRUE(process_sp); 156 157 /* 158 Should hit ShouldStop if state is eStateStopped 159 */ 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); 168 169 /* 170 Should not hit ShouldStop if state is not eStateStopped 171 */ 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); 180 } 181 182 TEST_F(ProcessEventDataTest, ShouldStop) { 183 ArchSpec arch("x86_64-apple-macosx-"); 184 185 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch)); 186 187 DebuggerSP debugger_sp = Debugger::CreateInstance(); 188 ASSERT_TRUE(debugger_sp); 189 190 TargetSP target_sp = CreateTarget(debugger_sp, arch); 191 ASSERT_TRUE(target_sp); 192 193 ListenerSP listener_sp(Listener::MakeListener("dummy")); 194 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp); 195 ASSERT_TRUE(process_sp); 196 197 // wants to stop and has valid StopInfo 198 ThreadSP thread_sp = CreateThread(process_sp, true, true); 199 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); 203 /* 204 Should stop if thread has valid StopInfo and not suspended 205 */ 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); 210 211 /* 212 Should not stop if thread has valid StopInfo but was suspended 213 */ 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); 218 219 /* 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. 223 */ 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(); 228 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); 240 } 241 } 242 ASSERT_TRUE(process_sp->GetThreadList().GetSize() == 6); 243 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); 247 } 248