1dda28197Spatrick //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp-----------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "AppleThreadPlanStepThroughObjCTrampoline.h"
10061da546Spatrick 
11061da546Spatrick #include "AppleObjCTrampolineHandler.h"
12061da546Spatrick #include "lldb/Expression/DiagnosticManager.h"
13061da546Spatrick #include "lldb/Expression/FunctionCaller.h"
14061da546Spatrick #include "lldb/Expression/UtilityFunction.h"
15*f6aab3d8Srobert #include "lldb/Target/ABI.h"
16061da546Spatrick #include "lldb/Target/ExecutionContext.h"
17061da546Spatrick #include "lldb/Target/Process.h"
18061da546Spatrick #include "lldb/Target/Thread.h"
19061da546Spatrick #include "lldb/Target/ThreadPlanRunToAddress.h"
20061da546Spatrick #include "lldb/Target/ThreadPlanStepOut.h"
21*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
22061da546Spatrick #include "lldb/Utility/Log.h"
23061da546Spatrick 
24061da546Spatrick #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
25061da546Spatrick 
26061da546Spatrick #include <memory>
27061da546Spatrick 
28061da546Spatrick using namespace lldb;
29061da546Spatrick using namespace lldb_private;
30061da546Spatrick 
31061da546Spatrick // ThreadPlanStepThroughObjCTrampoline constructor
32061da546Spatrick AppleThreadPlanStepThroughObjCTrampoline::
AppleThreadPlanStepThroughObjCTrampoline(Thread & thread,AppleObjCTrampolineHandler & trampoline_handler,ValueList & input_values,lldb::addr_t isa_addr,lldb::addr_t sel_addr,lldb::addr_t sel_str_addr,llvm::StringRef sel_str)33061da546Spatrick     AppleThreadPlanStepThroughObjCTrampoline(
34dda28197Spatrick         Thread &thread, AppleObjCTrampolineHandler &trampoline_handler,
35*f6aab3d8Srobert         ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr,
36*f6aab3d8Srobert         lldb::addr_t sel_str_addr, llvm::StringRef sel_str)
37061da546Spatrick     : ThreadPlan(ThreadPlan::eKindGeneric,
38061da546Spatrick                  "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
39061da546Spatrick                  eVoteNoOpinion),
40061da546Spatrick       m_trampoline_handler(trampoline_handler),
41061da546Spatrick       m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values),
42*f6aab3d8Srobert       m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(nullptr),
43*f6aab3d8Srobert       m_sel_str_addr(sel_str_addr), m_sel_str(sel_str) {}
44061da546Spatrick 
45061da546Spatrick // Destructor
46061da546Spatrick AppleThreadPlanStepThroughObjCTrampoline::
47be691f3bSpatrick     ~AppleThreadPlanStepThroughObjCTrampoline() = default;
48061da546Spatrick 
DidPush()49061da546Spatrick void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
50061da546Spatrick   // Setting up the memory space for the called function text might require
51061da546Spatrick   // allocations, i.e. a nested function call.  This needs to be done as a
52061da546Spatrick   // PreResumeAction.
53dda28197Spatrick   m_process.AddPreResumeAction(PreResumeInitializeFunctionCaller, (void *)this);
54061da546Spatrick }
55061da546Spatrick 
InitializeFunctionCaller()56061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
57061da546Spatrick   if (!m_func_sp) {
58061da546Spatrick     DiagnosticManager diagnostics;
59061da546Spatrick     m_args_addr =
60dda28197Spatrick         m_trampoline_handler.SetupDispatchFunction(GetThread(), m_input_values);
61061da546Spatrick 
62061da546Spatrick     if (m_args_addr == LLDB_INVALID_ADDRESS) {
63061da546Spatrick       return false;
64061da546Spatrick     }
65061da546Spatrick     m_impl_function =
66dda28197Spatrick         m_trampoline_handler.GetLookupImplementationFunctionCaller();
67061da546Spatrick     ExecutionContext exc_ctx;
68061da546Spatrick     EvaluateExpressionOptions options;
69061da546Spatrick     options.SetUnwindOnError(true);
70061da546Spatrick     options.SetIgnoreBreakpoints(true);
71be691f3bSpatrick     options.SetStopOthers(false);
72dda28197Spatrick     GetThread().CalculateExecutionContext(exc_ctx);
73061da546Spatrick     m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
74061da546Spatrick         exc_ctx, m_args_addr, options, diagnostics);
75061da546Spatrick     m_func_sp->SetOkayToDiscard(true);
76dda28197Spatrick     PushPlan(m_func_sp);
77061da546Spatrick   }
78061da546Spatrick   return true;
79061da546Spatrick }
80061da546Spatrick 
81061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::
PreResumeInitializeFunctionCaller(void * void_myself)82061da546Spatrick     PreResumeInitializeFunctionCaller(void *void_myself) {
83061da546Spatrick   AppleThreadPlanStepThroughObjCTrampoline *myself =
84061da546Spatrick       static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
85061da546Spatrick   return myself->InitializeFunctionCaller();
86061da546Spatrick }
87061da546Spatrick 
GetDescription(Stream * s,lldb::DescriptionLevel level)88061da546Spatrick void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
89061da546Spatrick     Stream *s, lldb::DescriptionLevel level) {
90061da546Spatrick   if (level == lldb::eDescriptionLevelBrief)
91061da546Spatrick     s->Printf("Step through ObjC trampoline");
92061da546Spatrick   else {
93061da546Spatrick     s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: "
94061da546Spatrick               "0x%" PRIx64 ", sel: 0x%" PRIx64,
95061da546Spatrick               m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(),
96061da546Spatrick               m_isa_addr, m_sel_addr);
97061da546Spatrick   }
98061da546Spatrick }
99061da546Spatrick 
ValidatePlan(Stream * error)100061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
101061da546Spatrick   return true;
102061da546Spatrick }
103061da546Spatrick 
DoPlanExplainsStop(Event * event_ptr)104061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
105061da546Spatrick     Event *event_ptr) {
106061da546Spatrick   // If we get asked to explain the stop it will be because something went
107061da546Spatrick   // wrong (like the implementation for selector function crashed...  We're
108061da546Spatrick   // going to figure out what to do about that, so we do explain the stop.
109061da546Spatrick   return true;
110061da546Spatrick }
111061da546Spatrick 
GetPlanRunState()112061da546Spatrick lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
113061da546Spatrick   return eStateRunning;
114061da546Spatrick }
115061da546Spatrick 
ShouldStop(Event * event_ptr)116061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
117061da546Spatrick   // First stage: we are still handling the "call a function to get the target
118061da546Spatrick   // of the dispatch"
119061da546Spatrick   if (m_func_sp) {
120061da546Spatrick     if (!m_func_sp->IsPlanComplete()) {
121061da546Spatrick       return false;
122061da546Spatrick     } else {
123061da546Spatrick       if (!m_func_sp->PlanSucceeded()) {
124061da546Spatrick         SetPlanComplete(false);
125061da546Spatrick         return true;
126061da546Spatrick       }
127061da546Spatrick       m_func_sp.reset();
128061da546Spatrick     }
129061da546Spatrick   }
130061da546Spatrick 
131*f6aab3d8Srobert   // Second stage, if all went well with the function calling,  get the
132*f6aab3d8Srobert   // implementation function address, and queue up a "run to that address" plan.
133*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Step);
134*f6aab3d8Srobert 
135061da546Spatrick   if (!m_run_to_sp) {
136061da546Spatrick     Value target_addr_value;
137061da546Spatrick     ExecutionContext exc_ctx;
138dda28197Spatrick     GetThread().CalculateExecutionContext(exc_ctx);
139061da546Spatrick     m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
140061da546Spatrick                                           target_addr_value);
141061da546Spatrick     m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
142061da546Spatrick     lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
143*f6aab3d8Srobert 
144*f6aab3d8Srobert     if (ABISP abi_sp = GetThread().GetProcess()->GetABI()) {
145*f6aab3d8Srobert       target_addr = abi_sp->FixCodeAddress(target_addr);
146*f6aab3d8Srobert     }
147061da546Spatrick     Address target_so_addr;
148061da546Spatrick     target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
149061da546Spatrick     if (target_addr == 0) {
150061da546Spatrick       LLDB_LOGF(log, "Got target implementation of 0x0, stopping.");
151061da546Spatrick       SetPlanComplete();
152061da546Spatrick       return true;
153061da546Spatrick     }
154dda28197Spatrick     if (m_trampoline_handler.AddrIsMsgForward(target_addr)) {
155061da546Spatrick       LLDB_LOGF(log,
156061da546Spatrick                 "Implementation lookup returned msgForward function: 0x%" PRIx64
157061da546Spatrick                 ", stopping.",
158061da546Spatrick                 target_addr);
159061da546Spatrick 
160dda28197Spatrick       SymbolContext sc = GetThread().GetStackFrameAtIndex(0)->GetSymbolContext(
161061da546Spatrick           eSymbolContextEverything);
162061da546Spatrick       Status status;
163061da546Spatrick       const bool abort_other_plans = false;
164061da546Spatrick       const bool first_insn = true;
165061da546Spatrick       const uint32_t frame_idx = 0;
166dda28197Spatrick       m_run_to_sp = GetThread().QueueThreadPlanForStepOutNoShouldStop(
167be691f3bSpatrick           abort_other_plans, &sc, first_insn, false, eVoteNoOpinion,
168061da546Spatrick           eVoteNoOpinion, frame_idx, status);
169061da546Spatrick       if (m_run_to_sp && status.Success())
170061da546Spatrick         m_run_to_sp->SetPrivate(true);
171061da546Spatrick       return false;
172061da546Spatrick     }
173061da546Spatrick 
174061da546Spatrick     LLDB_LOGF(log, "Running to ObjC method implementation: 0x%" PRIx64,
175061da546Spatrick               target_addr);
176061da546Spatrick 
177061da546Spatrick     ObjCLanguageRuntime *objc_runtime =
178061da546Spatrick         ObjCLanguageRuntime::Get(*GetThread().GetProcess());
179061da546Spatrick     assert(objc_runtime != nullptr);
180*f6aab3d8Srobert     if (m_sel_str_addr != LLDB_INVALID_ADDRESS) {
181*f6aab3d8Srobert       // Cache the string -> implementation and free the string in the target.
182*f6aab3d8Srobert       Status dealloc_error =
183*f6aab3d8Srobert           GetThread().GetProcess()->DeallocateMemory(m_sel_str_addr);
184*f6aab3d8Srobert       // For now just log this:
185*f6aab3d8Srobert       if (dealloc_error.Fail())
186*f6aab3d8Srobert         LLDB_LOG(log, "Failed to deallocate the sel str at {0} - error: {1}",
187*f6aab3d8Srobert                  m_sel_str_addr, dealloc_error);
188*f6aab3d8Srobert       objc_runtime->AddToMethodCache(m_isa_addr, m_sel_str, target_addr);
189*f6aab3d8Srobert       LLDB_LOG(log,
190*f6aab3d8Srobert                "Adding \\{isa-addr={0}, sel-addr={1}\\} = addr={2} to cache.",
191*f6aab3d8Srobert                m_isa_addr, m_sel_str, target_addr);
192*f6aab3d8Srobert     } else {
193061da546Spatrick       objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
194061da546Spatrick       LLDB_LOGF(log,
195061da546Spatrick                 "Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
196061da546Spatrick                 "} = addr=0x%" PRIx64 " to cache.",
197061da546Spatrick                 m_isa_addr, m_sel_addr, target_addr);
198*f6aab3d8Srobert     }
199061da546Spatrick 
200061da546Spatrick     m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>(
201be691f3bSpatrick         GetThread(), target_so_addr, false);
202dda28197Spatrick     PushPlan(m_run_to_sp);
203061da546Spatrick     return false;
204dda28197Spatrick   } else if (GetThread().IsThreadPlanDone(m_run_to_sp.get())) {
205061da546Spatrick     // Third stage, work the run to target plan.
206061da546Spatrick     SetPlanComplete();
207061da546Spatrick     return true;
208061da546Spatrick   }
209061da546Spatrick   return false;
210061da546Spatrick }
211061da546Spatrick 
212061da546Spatrick // The base class MischiefManaged does some cleanup - so you have to call it in
213061da546Spatrick // your MischiefManaged derived class.
MischiefManaged()214061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
215061da546Spatrick   return IsPlanComplete();
216061da546Spatrick }
217061da546Spatrick 
WillStop()218061da546Spatrick bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }
219dda28197Spatrick 
220dda28197Spatrick // Objective-C uses optimized dispatch functions for some common and seldom
221dda28197Spatrick // overridden methods.  For instance
222dda28197Spatrick //      [object respondsToSelector:];
223dda28197Spatrick // will get compiled to:
224dda28197Spatrick //      objc_opt_respondsToSelector(object);
225dda28197Spatrick // This checks whether the selector has been overridden, directly calling the
226dda28197Spatrick // implementation if it hasn't and calling objc_msgSend if it has.
227dda28197Spatrick //
228dda28197Spatrick // We need to get into the overridden implementation.  We'll do that by
229dda28197Spatrick // setting a breakpoint on objc_msgSend, and doing a "step out".  If we stop
230dda28197Spatrick // at objc_msgSend, we can step through to the target of the send, and see if
231dda28197Spatrick // that's a place we want to stop.
232dda28197Spatrick //
233dda28197Spatrick // A couple of complexities.  The checking code might call some other method,
234dda28197Spatrick // so we might see objc_msgSend more than once.  Also, these optimized dispatch
235dda28197Spatrick // functions might dispatch more than one message at a time (e.g. alloc followed
236dda28197Spatrick // by init.)  So we can't give up at the first objc_msgSend.
237dda28197Spatrick // That means among other things that we have to handle the "ShouldStopHere" -
238dda28197Spatrick // since we can't just return control to the plan that's controlling us on the
239dda28197Spatrick // first step.
240dda28197Spatrick 
241dda28197Spatrick AppleThreadPlanStepThroughDirectDispatch ::
AppleThreadPlanStepThroughDirectDispatch(Thread & thread,AppleObjCTrampolineHandler & handler,llvm::StringRef dispatch_func_name)242dda28197Spatrick     AppleThreadPlanStepThroughDirectDispatch(
243dda28197Spatrick         Thread &thread, AppleObjCTrampolineHandler &handler,
244be691f3bSpatrick         llvm::StringRef dispatch_func_name)
245be691f3bSpatrick     : ThreadPlanStepOut(thread, nullptr, true /* first instruction */, false,
246be691f3bSpatrick                         eVoteNoOpinion, eVoteNoOpinion,
247dda28197Spatrick                         0 /* Step out of zeroth frame */,
248dda28197Spatrick                         eLazyBoolNo /* Our parent plan will decide this
249dda28197Spatrick                                when we are done */
250dda28197Spatrick                         ,
251dda28197Spatrick                         true /* Run to branch for inline step out */,
252dda28197Spatrick                         false /* Don't gather the return value */),
253dda28197Spatrick       m_trampoline_handler(handler),
254dda28197Spatrick       m_dispatch_func_name(std::string(dispatch_func_name)),
255be691f3bSpatrick       m_at_msg_send(false) {
256dda28197Spatrick   // Set breakpoints on the dispatch functions:
257dda28197Spatrick   auto bkpt_callback = [&] (lldb::addr_t addr,
258dda28197Spatrick                             const AppleObjCTrampolineHandler
259dda28197Spatrick                                 ::DispatchFunction &dispatch) {
260dda28197Spatrick     m_msgSend_bkpts.push_back(GetTarget().CreateBreakpoint(addr,
261dda28197Spatrick                                                            true /* internal */,
262dda28197Spatrick                                                            false /* hard */));
263dda28197Spatrick     m_msgSend_bkpts.back()->SetThreadID(GetThread().GetID());
264dda28197Spatrick   };
265dda28197Spatrick   handler.ForEachDispatchFunction(bkpt_callback);
266dda28197Spatrick 
267dda28197Spatrick   // We'll set the step-out plan in the DidPush so it gets queued in the right
268dda28197Spatrick   // order.
269dda28197Spatrick 
270be691f3bSpatrick   if (GetThread().GetStepInAvoidsNoDebug())
271dda28197Spatrick     GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
272dda28197Spatrick   else
273dda28197Spatrick     GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
274dda28197Spatrick   // We only care about step in.  Our parent plan will figure out what to
275dda28197Spatrick   // do when we've stepped out again.
276dda28197Spatrick   GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
277dda28197Spatrick }
278dda28197Spatrick 
279dda28197Spatrick AppleThreadPlanStepThroughDirectDispatch::
~AppleThreadPlanStepThroughDirectDispatch()280dda28197Spatrick     ~AppleThreadPlanStepThroughDirectDispatch() {
281dda28197Spatrick     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
282dda28197Spatrick       GetTarget().RemoveBreakpointByID(bkpt_sp->GetID());
283dda28197Spatrick     }
284dda28197Spatrick }
285dda28197Spatrick 
GetDescription(Stream * s,lldb::DescriptionLevel level)286dda28197Spatrick void AppleThreadPlanStepThroughDirectDispatch::GetDescription(
287dda28197Spatrick     Stream *s, lldb::DescriptionLevel level) {
288dda28197Spatrick   switch (level) {
289dda28197Spatrick   case lldb::eDescriptionLevelBrief:
290dda28197Spatrick     s->PutCString("Step through ObjC direct dispatch function.");
291dda28197Spatrick     break;
292dda28197Spatrick   default:
293dda28197Spatrick     s->Printf("Step through ObjC direct dispatch '%s'  using breakpoints: ",
294dda28197Spatrick               m_dispatch_func_name.c_str());
295dda28197Spatrick     bool first = true;
296dda28197Spatrick     for (auto bkpt_sp : m_msgSend_bkpts) {
297dda28197Spatrick         if (!first) {
298dda28197Spatrick           s->PutCString(", ");
299dda28197Spatrick         }
300dda28197Spatrick         first = false;
301dda28197Spatrick         s->Printf("%d", bkpt_sp->GetID());
302dda28197Spatrick     }
303dda28197Spatrick     (*s) << ".";
304dda28197Spatrick     break;
305dda28197Spatrick   }
306dda28197Spatrick }
307dda28197Spatrick 
308dda28197Spatrick bool
DoPlanExplainsStop(Event * event_ptr)309dda28197Spatrick AppleThreadPlanStepThroughDirectDispatch::DoPlanExplainsStop(Event *event_ptr) {
310dda28197Spatrick   if (ThreadPlanStepOut::DoPlanExplainsStop(event_ptr))
311dda28197Spatrick     return true;
312dda28197Spatrick 
313dda28197Spatrick   StopInfoSP stop_info_sp = GetPrivateStopInfo();
314dda28197Spatrick 
315dda28197Spatrick   // Check if the breakpoint is one of ours msgSend dispatch breakpoints.
316dda28197Spatrick 
317dda28197Spatrick   StopReason stop_reason = eStopReasonNone;
318dda28197Spatrick   if (stop_info_sp)
319dda28197Spatrick     stop_reason = stop_info_sp->GetStopReason();
320dda28197Spatrick 
321dda28197Spatrick   // See if this is one of our msgSend breakpoints:
322dda28197Spatrick   if (stop_reason == eStopReasonBreakpoint) {
323dda28197Spatrick     ProcessSP process_sp = GetThread().GetProcess();
324dda28197Spatrick     uint64_t break_site_id = stop_info_sp->GetValue();
325dda28197Spatrick     BreakpointSiteSP site_sp
326dda28197Spatrick         = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
327dda28197Spatrick     // Some other plan might have deleted the site's last owner before this
328dda28197Spatrick     // got to us.  In which case, it wasn't our breakpoint...
329dda28197Spatrick     if (!site_sp)
330dda28197Spatrick       return false;
331dda28197Spatrick 
332dda28197Spatrick     for (BreakpointSP break_sp : m_msgSend_bkpts) {
333dda28197Spatrick       if (site_sp->IsBreakpointAtThisSite(break_sp->GetID())) {
334dda28197Spatrick         // If we aren't the only one with a breakpoint on this site, then we
335dda28197Spatrick         // should just stop and return control to the user.
336dda28197Spatrick         if (site_sp->GetNumberOfOwners() > 1) {
337dda28197Spatrick           SetPlanComplete(true);
338dda28197Spatrick           return false;
339dda28197Spatrick         }
340dda28197Spatrick         m_at_msg_send = true;
341dda28197Spatrick         return true;
342dda28197Spatrick       }
343dda28197Spatrick     }
344dda28197Spatrick   }
345dda28197Spatrick 
346dda28197Spatrick   // We're done here.  If one of our sub-plans explained the stop, they
347dda28197Spatrick   // would have already answered true to PlanExplainsStop, and if they were
348dda28197Spatrick   // done, we'll get called to figure out what to do in ShouldStop...
349dda28197Spatrick   return false;
350dda28197Spatrick }
351dda28197Spatrick 
352dda28197Spatrick bool AppleThreadPlanStepThroughDirectDispatch
DoWillResume(lldb::StateType resume_state,bool current_plan)353dda28197Spatrick          ::DoWillResume(lldb::StateType resume_state, bool current_plan) {
354dda28197Spatrick   ThreadPlanStepOut::DoWillResume(resume_state, current_plan);
355dda28197Spatrick   m_at_msg_send = false;
356dda28197Spatrick   return true;
357dda28197Spatrick }
358dda28197Spatrick 
ShouldStop(Event * event_ptr)359dda28197Spatrick bool AppleThreadPlanStepThroughDirectDispatch::ShouldStop(Event *event_ptr) {
360dda28197Spatrick   // If step out plan finished, that means we didn't find our way into a method
361dda28197Spatrick   // implementation.  Either we went directly to the default implementation,
362dda28197Spatrick   // of the overridden implementation didn't have debug info.
363dda28197Spatrick   // So we should mark ourselves as done.
364dda28197Spatrick   const bool step_out_should_stop = ThreadPlanStepOut::ShouldStop(event_ptr);
365dda28197Spatrick   if (step_out_should_stop) {
366dda28197Spatrick     SetPlanComplete(true);
367dda28197Spatrick     return true;
368dda28197Spatrick   }
369dda28197Spatrick 
370dda28197Spatrick   // If we have a step through plan, then w're in the process of getting
371dda28197Spatrick   // through an ObjC msgSend.  If we arrived at the target function, then
372dda28197Spatrick   // check whether we have debug info, and if we do, stop.
373*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Step);
374dda28197Spatrick 
375dda28197Spatrick   if (m_objc_step_through_sp && m_objc_step_through_sp->IsPlanComplete()) {
376dda28197Spatrick     // If the plan failed for some reason, we should probably just let the
377dda28197Spatrick     // step over plan get us out of here...  We don't need to do anything about
378dda28197Spatrick     // the step through plan, it is done and will get popped when we continue.
379dda28197Spatrick     if (!m_objc_step_through_sp->PlanSucceeded()) {
380dda28197Spatrick       LLDB_LOGF(log, "ObjC Step through plan failed.  Stepping out.");
381dda28197Spatrick     }
382dda28197Spatrick     Status error;
383dda28197Spatrick     if (InvokeShouldStopHereCallback(eFrameCompareYounger, error)) {
384dda28197Spatrick       SetPlanComplete(true);
385dda28197Spatrick       return true;
386dda28197Spatrick     }
387dda28197Spatrick     // If we didn't want to stop at this msgSend, there might be another so
388dda28197Spatrick     // we should just continue on with the step out and see if our breakpoint
389dda28197Spatrick     // triggers again.
390dda28197Spatrick     m_objc_step_through_sp.reset();
391dda28197Spatrick     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
392dda28197Spatrick       bkpt_sp->SetEnabled(true);
393dda28197Spatrick     }
394dda28197Spatrick     return false;
395dda28197Spatrick   }
396dda28197Spatrick 
397dda28197Spatrick   // If we hit an msgSend breakpoint, then we should queue the step through
398dda28197Spatrick   // plan:
399dda28197Spatrick 
400dda28197Spatrick   if (m_at_msg_send) {
401dda28197Spatrick     LanguageRuntime *objc_runtime
402dda28197Spatrick       = GetThread().GetProcess()->GetLanguageRuntime(eLanguageTypeObjC);
403dda28197Spatrick     // There's no way we could have gotten here without an ObjC language
404dda28197Spatrick     // runtime.
405dda28197Spatrick     assert(objc_runtime);
406be691f3bSpatrick     m_objc_step_through_sp =
407be691f3bSpatrick         objc_runtime->GetStepThroughTrampolinePlan(GetThread(), false);
408dda28197Spatrick     // If we failed to find the target for this dispatch, just keep going and
409dda28197Spatrick     // let the step out complete.
410dda28197Spatrick     if (!m_objc_step_through_sp) {
411dda28197Spatrick       LLDB_LOG(log, "Couldn't find target for message dispatch, continuing.");
412dda28197Spatrick       return false;
413dda28197Spatrick     }
414dda28197Spatrick     // Otherwise push the step through plan and continue.
415dda28197Spatrick     GetThread().QueueThreadPlan(m_objc_step_through_sp, false);
416dda28197Spatrick     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
417dda28197Spatrick       bkpt_sp->SetEnabled(false);
418dda28197Spatrick     }
419dda28197Spatrick     return false;
420dda28197Spatrick   }
421dda28197Spatrick   return true;
422dda28197Spatrick }
423dda28197Spatrick 
MischiefManaged()424dda28197Spatrick bool AppleThreadPlanStepThroughDirectDispatch::MischiefManaged() {
425dda28197Spatrick   if (IsPlanComplete())
426dda28197Spatrick     return true;
427dda28197Spatrick   return ThreadPlanStepOut::MischiefManaged();
428dda28197Spatrick }
429