1 //===-- ProcessEventDataTest.cpp ------------------------------------------===//
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
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
;
26 class ProcessEventDataTest
: public ::testing::Test
{
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
{
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
{
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
{
54 bool DoUpdateThreadList(ThreadList
&old_thread_list
,
55 ThreadList
&new_thread_list
) override
{
58 llvm::StringRef
GetPluginName() override
{ return "Dummy"; }
60 ProcessModID
&GetModIDNonConstRef() { return m_mod_id
; }
63 class DummyThread
: public Thread
{
67 ~DummyThread() override
{ DestroyThread(); }
69 void RefreshStateAfterStop() override
{}
71 lldb::RegisterContextSP
GetRegisterContext() override
{ return nullptr; }
73 lldb::RegisterContextSP
74 CreateRegisterContextForFrame(StackFrame
*frame
) override
{
78 bool CalculateStopInfo() override
{ return false; }
81 class DummyStopInfo
: public StopInfo
{
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
{
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
++;
102 int m_should_stop_hit_count
= 0;
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
;
112 debugger_sp
->GetTargetList().CreateTarget(
113 *debugger_sp
, "", arch
, eLoadDependentsNo
, platform_sp
, 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) {
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
=
130 if (stopinfo_sp
== nullptr) {
134 thread_sp
->SetStopInfo(stopinfo_sp
);
137 process_sp
->GetThreadList().AddThread(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;
170 Should not hit ShouldStop if state is not eStateStopped
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;
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;
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)
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
++) {
231 // Does not want to stop but has valid StopInfo
232 thread_sp
= CreateThread(process_sp
, false, true);
234 // Wants to stop and has valid StopInfo
235 thread_sp
= CreateThread(process_sp
, true, true);
236 thread_sp
->SetResumeState(eStateSuspended
);
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);