15ffd83dbSDimitry Andric //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp-----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "AppleThreadPlanStepThroughObjCTrampoline.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "AppleObjCTrampolineHandler.h"
120b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
130b57cec5SDimitry Andric #include "lldb/Expression/FunctionCaller.h"
140b57cec5SDimitry Andric #include "lldb/Expression/UtilityFunction.h"
15349cc55cSDimitry Andric #include "lldb/Target/ABI.h"
160b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
170b57cec5SDimitry Andric #include "lldb/Target/Process.h"
180b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
190b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanRunToAddress.h"
200b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepOut.h"
2181ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
220b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #include <memory>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace lldb;
290b57cec5SDimitry Andric using namespace lldb_private;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric // ThreadPlanStepThroughObjCTrampoline constructor
320b57cec5SDimitry Andric 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)330b57cec5SDimitry Andric     AppleThreadPlanStepThroughObjCTrampoline(
345ffd83dbSDimitry Andric         Thread &thread, AppleObjCTrampolineHandler &trampoline_handler,
3581ad6265SDimitry Andric         ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr,
3681ad6265SDimitry Andric         lldb::addr_t sel_str_addr, llvm::StringRef sel_str)
370b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindGeneric,
380b57cec5SDimitry Andric                  "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
390b57cec5SDimitry Andric                  eVoteNoOpinion),
400b57cec5SDimitry Andric       m_trampoline_handler(trampoline_handler),
410b57cec5SDimitry Andric       m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values),
4281ad6265SDimitry Andric       m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(nullptr),
4381ad6265SDimitry Andric       m_sel_str_addr(sel_str_addr), m_sel_str(sel_str) {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric // Destructor
460b57cec5SDimitry Andric AppleThreadPlanStepThroughObjCTrampoline::
47fe6060f1SDimitry Andric     ~AppleThreadPlanStepThroughObjCTrampoline() = default;
480b57cec5SDimitry Andric 
DidPush()490b57cec5SDimitry Andric void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
500b57cec5SDimitry Andric   // Setting up the memory space for the called function text might require
510b57cec5SDimitry Andric   // allocations, i.e. a nested function call.  This needs to be done as a
520b57cec5SDimitry Andric   // PreResumeAction.
535ffd83dbSDimitry Andric   m_process.AddPreResumeAction(PreResumeInitializeFunctionCaller, (void *)this);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
InitializeFunctionCaller()560b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
570b57cec5SDimitry Andric   if (!m_func_sp) {
580b57cec5SDimitry Andric     DiagnosticManager diagnostics;
590b57cec5SDimitry Andric     m_args_addr =
605ffd83dbSDimitry Andric         m_trampoline_handler.SetupDispatchFunction(GetThread(), m_input_values);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric     if (m_args_addr == LLDB_INVALID_ADDRESS) {
630b57cec5SDimitry Andric       return false;
640b57cec5SDimitry Andric     }
650b57cec5SDimitry Andric     m_impl_function =
665ffd83dbSDimitry Andric         m_trampoline_handler.GetLookupImplementationFunctionCaller();
670b57cec5SDimitry Andric     ExecutionContext exc_ctx;
680b57cec5SDimitry Andric     EvaluateExpressionOptions options;
690b57cec5SDimitry Andric     options.SetUnwindOnError(true);
700b57cec5SDimitry Andric     options.SetIgnoreBreakpoints(true);
71fe6060f1SDimitry Andric     options.SetStopOthers(false);
725ffd83dbSDimitry Andric     GetThread().CalculateExecutionContext(exc_ctx);
730b57cec5SDimitry Andric     m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
740b57cec5SDimitry Andric         exc_ctx, m_args_addr, options, diagnostics);
750b57cec5SDimitry Andric     m_func_sp->SetOkayToDiscard(true);
765ffd83dbSDimitry Andric     PushPlan(m_func_sp);
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric   return true;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::
PreResumeInitializeFunctionCaller(void * void_myself)820b57cec5SDimitry Andric     PreResumeInitializeFunctionCaller(void *void_myself) {
830b57cec5SDimitry Andric   AppleThreadPlanStepThroughObjCTrampoline *myself =
840b57cec5SDimitry Andric       static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
850b57cec5SDimitry Andric   return myself->InitializeFunctionCaller();
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)880b57cec5SDimitry Andric void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
890b57cec5SDimitry Andric     Stream *s, lldb::DescriptionLevel level) {
900b57cec5SDimitry Andric   if (level == lldb::eDescriptionLevelBrief)
910b57cec5SDimitry Andric     s->Printf("Step through ObjC trampoline");
920b57cec5SDimitry Andric   else {
930b57cec5SDimitry Andric     s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: "
940b57cec5SDimitry Andric               "0x%" PRIx64 ", sel: 0x%" PRIx64,
950b57cec5SDimitry Andric               m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(),
960b57cec5SDimitry Andric               m_isa_addr, m_sel_addr);
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
ValidatePlan(Stream * error)1000b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
1010b57cec5SDimitry Andric   return true;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)1040b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
1050b57cec5SDimitry Andric     Event *event_ptr) {
1060b57cec5SDimitry Andric   // If we get asked to explain the stop it will be because something went
1070b57cec5SDimitry Andric   // wrong (like the implementation for selector function crashed...  We're
1080b57cec5SDimitry Andric   // going to figure out what to do about that, so we do explain the stop.
1090b57cec5SDimitry Andric   return true;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
GetPlanRunState()1120b57cec5SDimitry Andric lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
1130b57cec5SDimitry Andric   return eStateRunning;
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)1160b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
1170b57cec5SDimitry Andric   // First stage: we are still handling the "call a function to get the target
1180b57cec5SDimitry Andric   // of the dispatch"
1190b57cec5SDimitry Andric   if (m_func_sp) {
1200b57cec5SDimitry Andric     if (!m_func_sp->IsPlanComplete()) {
1210b57cec5SDimitry Andric       return false;
1220b57cec5SDimitry Andric     } else {
1230b57cec5SDimitry Andric       if (!m_func_sp->PlanSucceeded()) {
1240b57cec5SDimitry Andric         SetPlanComplete(false);
1250b57cec5SDimitry Andric         return true;
1260b57cec5SDimitry Andric       }
1270b57cec5SDimitry Andric       m_func_sp.reset();
1280b57cec5SDimitry Andric     }
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric 
13181ad6265SDimitry Andric   // Second stage, if all went well with the function calling,  get the
13281ad6265SDimitry Andric   // implementation function address, and queue up a "run to that address" plan.
13381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
13481ad6265SDimitry Andric 
1350b57cec5SDimitry Andric   if (!m_run_to_sp) {
1360b57cec5SDimitry Andric     Value target_addr_value;
1370b57cec5SDimitry Andric     ExecutionContext exc_ctx;
1385ffd83dbSDimitry Andric     GetThread().CalculateExecutionContext(exc_ctx);
1390b57cec5SDimitry Andric     m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
1400b57cec5SDimitry Andric                                           target_addr_value);
1410b57cec5SDimitry Andric     m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
1420b57cec5SDimitry Andric     lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
143349cc55cSDimitry Andric 
144349cc55cSDimitry Andric     if (ABISP abi_sp = GetThread().GetProcess()->GetABI()) {
145349cc55cSDimitry Andric       target_addr = abi_sp->FixCodeAddress(target_addr);
146349cc55cSDimitry Andric     }
1470b57cec5SDimitry Andric     Address target_so_addr;
1480b57cec5SDimitry Andric     target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
1490b57cec5SDimitry Andric     if (target_addr == 0) {
1509dba64beSDimitry Andric       LLDB_LOGF(log, "Got target implementation of 0x0, stopping.");
1510b57cec5SDimitry Andric       SetPlanComplete();
1520b57cec5SDimitry Andric       return true;
1530b57cec5SDimitry Andric     }
1545ffd83dbSDimitry Andric     if (m_trampoline_handler.AddrIsMsgForward(target_addr)) {
1559dba64beSDimitry Andric       LLDB_LOGF(log,
1560b57cec5SDimitry Andric                 "Implementation lookup returned msgForward function: 0x%" PRIx64
1570b57cec5SDimitry Andric                 ", stopping.",
1580b57cec5SDimitry Andric                 target_addr);
1590b57cec5SDimitry Andric 
1605ffd83dbSDimitry Andric       SymbolContext sc = GetThread().GetStackFrameAtIndex(0)->GetSymbolContext(
1610b57cec5SDimitry Andric           eSymbolContextEverything);
1620b57cec5SDimitry Andric       Status status;
1630b57cec5SDimitry Andric       const bool abort_other_plans = false;
1640b57cec5SDimitry Andric       const bool first_insn = true;
1650b57cec5SDimitry Andric       const uint32_t frame_idx = 0;
1665ffd83dbSDimitry Andric       m_run_to_sp = GetThread().QueueThreadPlanForStepOutNoShouldStop(
167fe6060f1SDimitry Andric           abort_other_plans, &sc, first_insn, false, eVoteNoOpinion,
1680b57cec5SDimitry Andric           eVoteNoOpinion, frame_idx, status);
1690b57cec5SDimitry Andric       if (m_run_to_sp && status.Success())
1700b57cec5SDimitry Andric         m_run_to_sp->SetPrivate(true);
1710b57cec5SDimitry Andric       return false;
1720b57cec5SDimitry Andric     }
1730b57cec5SDimitry Andric 
1749dba64beSDimitry Andric     LLDB_LOGF(log, "Running to ObjC method implementation: 0x%" PRIx64,
1750b57cec5SDimitry Andric               target_addr);
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric     ObjCLanguageRuntime *objc_runtime =
1780b57cec5SDimitry Andric         ObjCLanguageRuntime::Get(*GetThread().GetProcess());
1790b57cec5SDimitry Andric     assert(objc_runtime != nullptr);
18081ad6265SDimitry Andric     if (m_sel_str_addr != LLDB_INVALID_ADDRESS) {
18181ad6265SDimitry Andric       // Cache the string -> implementation and free the string in the target.
18281ad6265SDimitry Andric       Status dealloc_error =
18381ad6265SDimitry Andric           GetThread().GetProcess()->DeallocateMemory(m_sel_str_addr);
18481ad6265SDimitry Andric       // For now just log this:
18581ad6265SDimitry Andric       if (dealloc_error.Fail())
18681ad6265SDimitry Andric         LLDB_LOG(log, "Failed to deallocate the sel str at {0} - error: {1}",
18781ad6265SDimitry Andric                  m_sel_str_addr, dealloc_error);
18881ad6265SDimitry Andric       objc_runtime->AddToMethodCache(m_isa_addr, m_sel_str, target_addr);
18981ad6265SDimitry Andric       LLDB_LOG(log,
19081ad6265SDimitry Andric                "Adding \\{isa-addr={0}, sel-addr={1}\\} = addr={2} to cache.",
19181ad6265SDimitry Andric                m_isa_addr, m_sel_str, target_addr);
19281ad6265SDimitry Andric     } else {
1930b57cec5SDimitry Andric       objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
1949dba64beSDimitry Andric       LLDB_LOGF(log,
1959dba64beSDimitry Andric                 "Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
1960b57cec5SDimitry Andric                 "} = addr=0x%" PRIx64 " to cache.",
1970b57cec5SDimitry Andric                 m_isa_addr, m_sel_addr, target_addr);
19881ad6265SDimitry Andric     }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>(
201fe6060f1SDimitry Andric         GetThread(), target_so_addr, false);
2025ffd83dbSDimitry Andric     PushPlan(m_run_to_sp);
2030b57cec5SDimitry Andric     return false;
2045ffd83dbSDimitry Andric   } else if (GetThread().IsThreadPlanDone(m_run_to_sp.get())) {
2050b57cec5SDimitry Andric     // Third stage, work the run to target plan.
2060b57cec5SDimitry Andric     SetPlanComplete();
2070b57cec5SDimitry Andric     return true;
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   return false;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric // The base class MischiefManaged does some cleanup - so you have to call it in
2130b57cec5SDimitry Andric // your MischiefManaged derived class.
MischiefManaged()2140b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
2150b57cec5SDimitry Andric   return IsPlanComplete();
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
WillStop()2180b57cec5SDimitry Andric bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }
2195ffd83dbSDimitry Andric 
2205ffd83dbSDimitry Andric // Objective-C uses optimized dispatch functions for some common and seldom
2215ffd83dbSDimitry Andric // overridden methods.  For instance
2225ffd83dbSDimitry Andric //      [object respondsToSelector:];
2235ffd83dbSDimitry Andric // will get compiled to:
2245ffd83dbSDimitry Andric //      objc_opt_respondsToSelector(object);
2255ffd83dbSDimitry Andric // This checks whether the selector has been overridden, directly calling the
2265ffd83dbSDimitry Andric // implementation if it hasn't and calling objc_msgSend if it has.
2275ffd83dbSDimitry Andric //
2285ffd83dbSDimitry Andric // We need to get into the overridden implementation.  We'll do that by
2295ffd83dbSDimitry Andric // setting a breakpoint on objc_msgSend, and doing a "step out".  If we stop
2305ffd83dbSDimitry Andric // at objc_msgSend, we can step through to the target of the send, and see if
2315ffd83dbSDimitry Andric // that's a place we want to stop.
2325ffd83dbSDimitry Andric //
2335ffd83dbSDimitry Andric // A couple of complexities.  The checking code might call some other method,
2345ffd83dbSDimitry Andric // so we might see objc_msgSend more than once.  Also, these optimized dispatch
2355ffd83dbSDimitry Andric // functions might dispatch more than one message at a time (e.g. alloc followed
2365ffd83dbSDimitry Andric // by init.)  So we can't give up at the first objc_msgSend.
2375ffd83dbSDimitry Andric // That means among other things that we have to handle the "ShouldStopHere" -
2385ffd83dbSDimitry Andric // since we can't just return control to the plan that's controlling us on the
2395ffd83dbSDimitry Andric // first step.
2405ffd83dbSDimitry Andric 
2415ffd83dbSDimitry Andric AppleThreadPlanStepThroughDirectDispatch ::
AppleThreadPlanStepThroughDirectDispatch(Thread & thread,AppleObjCTrampolineHandler & handler,llvm::StringRef dispatch_func_name)2425ffd83dbSDimitry Andric     AppleThreadPlanStepThroughDirectDispatch(
2435ffd83dbSDimitry Andric         Thread &thread, AppleObjCTrampolineHandler &handler,
244fe6060f1SDimitry Andric         llvm::StringRef dispatch_func_name)
245fe6060f1SDimitry Andric     : ThreadPlanStepOut(thread, nullptr, true /* first instruction */, false,
246fe6060f1SDimitry Andric                         eVoteNoOpinion, eVoteNoOpinion,
2475ffd83dbSDimitry Andric                         0 /* Step out of zeroth frame */,
2485ffd83dbSDimitry Andric                         eLazyBoolNo /* Our parent plan will decide this
2495ffd83dbSDimitry Andric                                when we are done */
2505ffd83dbSDimitry Andric                         ,
2515ffd83dbSDimitry Andric                         true /* Run to branch for inline step out */,
2525ffd83dbSDimitry Andric                         false /* Don't gather the return value */),
2535ffd83dbSDimitry Andric       m_trampoline_handler(handler),
2545ffd83dbSDimitry Andric       m_dispatch_func_name(std::string(dispatch_func_name)),
255fe6060f1SDimitry Andric       m_at_msg_send(false) {
2565ffd83dbSDimitry Andric   // Set breakpoints on the dispatch functions:
2575ffd83dbSDimitry Andric   auto bkpt_callback = [&] (lldb::addr_t addr,
2585ffd83dbSDimitry Andric                             const AppleObjCTrampolineHandler
2595ffd83dbSDimitry Andric                                 ::DispatchFunction &dispatch) {
2605ffd83dbSDimitry Andric     m_msgSend_bkpts.push_back(GetTarget().CreateBreakpoint(addr,
2615ffd83dbSDimitry Andric                                                            true /* internal */,
2625ffd83dbSDimitry Andric                                                            false /* hard */));
2635ffd83dbSDimitry Andric     m_msgSend_bkpts.back()->SetThreadID(GetThread().GetID());
2645ffd83dbSDimitry Andric   };
2655ffd83dbSDimitry Andric   handler.ForEachDispatchFunction(bkpt_callback);
2665ffd83dbSDimitry Andric 
2675ffd83dbSDimitry Andric   // We'll set the step-out plan in the DidPush so it gets queued in the right
2685ffd83dbSDimitry Andric   // order.
2695ffd83dbSDimitry Andric 
270fe6060f1SDimitry Andric   if (GetThread().GetStepInAvoidsNoDebug())
2715ffd83dbSDimitry Andric     GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
2725ffd83dbSDimitry Andric   else
2735ffd83dbSDimitry Andric     GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
2745ffd83dbSDimitry Andric   // We only care about step in.  Our parent plan will figure out what to
2755ffd83dbSDimitry Andric   // do when we've stepped out again.
2765ffd83dbSDimitry Andric   GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
2775ffd83dbSDimitry Andric }
2785ffd83dbSDimitry Andric 
2795ffd83dbSDimitry Andric AppleThreadPlanStepThroughDirectDispatch::
~AppleThreadPlanStepThroughDirectDispatch()2805ffd83dbSDimitry Andric     ~AppleThreadPlanStepThroughDirectDispatch() {
2815ffd83dbSDimitry Andric     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
2825ffd83dbSDimitry Andric       GetTarget().RemoveBreakpointByID(bkpt_sp->GetID());
2835ffd83dbSDimitry Andric     }
2845ffd83dbSDimitry Andric }
2855ffd83dbSDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)2865ffd83dbSDimitry Andric void AppleThreadPlanStepThroughDirectDispatch::GetDescription(
2875ffd83dbSDimitry Andric     Stream *s, lldb::DescriptionLevel level) {
2885ffd83dbSDimitry Andric   switch (level) {
2895ffd83dbSDimitry Andric   case lldb::eDescriptionLevelBrief:
2905ffd83dbSDimitry Andric     s->PutCString("Step through ObjC direct dispatch function.");
2915ffd83dbSDimitry Andric     break;
2925ffd83dbSDimitry Andric   default:
2935ffd83dbSDimitry Andric     s->Printf("Step through ObjC direct dispatch '%s'  using breakpoints: ",
2945ffd83dbSDimitry Andric               m_dispatch_func_name.c_str());
2955ffd83dbSDimitry Andric     bool first = true;
2965ffd83dbSDimitry Andric     for (auto bkpt_sp : m_msgSend_bkpts) {
2975ffd83dbSDimitry Andric         if (!first) {
2985ffd83dbSDimitry Andric           s->PutCString(", ");
2995ffd83dbSDimitry Andric         }
3005ffd83dbSDimitry Andric         first = false;
3015ffd83dbSDimitry Andric         s->Printf("%d", bkpt_sp->GetID());
3025ffd83dbSDimitry Andric     }
3035ffd83dbSDimitry Andric     (*s) << ".";
3045ffd83dbSDimitry Andric     break;
3055ffd83dbSDimitry Andric   }
3065ffd83dbSDimitry Andric }
3075ffd83dbSDimitry Andric 
3085ffd83dbSDimitry Andric bool
DoPlanExplainsStop(Event * event_ptr)3095ffd83dbSDimitry Andric AppleThreadPlanStepThroughDirectDispatch::DoPlanExplainsStop(Event *event_ptr) {
3105ffd83dbSDimitry Andric   if (ThreadPlanStepOut::DoPlanExplainsStop(event_ptr))
3115ffd83dbSDimitry Andric     return true;
3125ffd83dbSDimitry Andric 
3135ffd83dbSDimitry Andric   StopInfoSP stop_info_sp = GetPrivateStopInfo();
3145ffd83dbSDimitry Andric 
3155ffd83dbSDimitry Andric   // Check if the breakpoint is one of ours msgSend dispatch breakpoints.
3165ffd83dbSDimitry Andric 
3175ffd83dbSDimitry Andric   StopReason stop_reason = eStopReasonNone;
3185ffd83dbSDimitry Andric   if (stop_info_sp)
3195ffd83dbSDimitry Andric     stop_reason = stop_info_sp->GetStopReason();
3205ffd83dbSDimitry Andric 
3215ffd83dbSDimitry Andric   // See if this is one of our msgSend breakpoints:
3225ffd83dbSDimitry Andric   if (stop_reason == eStopReasonBreakpoint) {
3235ffd83dbSDimitry Andric     ProcessSP process_sp = GetThread().GetProcess();
3245ffd83dbSDimitry Andric     uint64_t break_site_id = stop_info_sp->GetValue();
3255ffd83dbSDimitry Andric     BreakpointSiteSP site_sp
3265ffd83dbSDimitry Andric         = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
3275ffd83dbSDimitry Andric     // Some other plan might have deleted the site's last owner before this
3285ffd83dbSDimitry Andric     // got to us.  In which case, it wasn't our breakpoint...
3295ffd83dbSDimitry Andric     if (!site_sp)
3305ffd83dbSDimitry Andric       return false;
3315ffd83dbSDimitry Andric 
3325ffd83dbSDimitry Andric     for (BreakpointSP break_sp : m_msgSend_bkpts) {
3335ffd83dbSDimitry Andric       if (site_sp->IsBreakpointAtThisSite(break_sp->GetID())) {
3345ffd83dbSDimitry Andric         // If we aren't the only one with a breakpoint on this site, then we
3355ffd83dbSDimitry Andric         // should just stop and return control to the user.
336*5f757f3fSDimitry Andric         if (site_sp->GetNumberOfConstituents() > 1) {
3375ffd83dbSDimitry Andric           SetPlanComplete(true);
3385ffd83dbSDimitry Andric           return false;
3395ffd83dbSDimitry Andric         }
3405ffd83dbSDimitry Andric         m_at_msg_send = true;
3415ffd83dbSDimitry Andric         return true;
3425ffd83dbSDimitry Andric       }
3435ffd83dbSDimitry Andric     }
3445ffd83dbSDimitry Andric   }
3455ffd83dbSDimitry Andric 
3465ffd83dbSDimitry Andric   // We're done here.  If one of our sub-plans explained the stop, they
3475ffd83dbSDimitry Andric   // would have already answered true to PlanExplainsStop, and if they were
3485ffd83dbSDimitry Andric   // done, we'll get called to figure out what to do in ShouldStop...
3495ffd83dbSDimitry Andric   return false;
3505ffd83dbSDimitry Andric }
3515ffd83dbSDimitry Andric 
3525ffd83dbSDimitry Andric bool AppleThreadPlanStepThroughDirectDispatch
DoWillResume(lldb::StateType resume_state,bool current_plan)3535ffd83dbSDimitry Andric          ::DoWillResume(lldb::StateType resume_state, bool current_plan) {
3545ffd83dbSDimitry Andric   ThreadPlanStepOut::DoWillResume(resume_state, current_plan);
3555ffd83dbSDimitry Andric   m_at_msg_send = false;
3565ffd83dbSDimitry Andric   return true;
3575ffd83dbSDimitry Andric }
3585ffd83dbSDimitry Andric 
ShouldStop(Event * event_ptr)3595ffd83dbSDimitry Andric bool AppleThreadPlanStepThroughDirectDispatch::ShouldStop(Event *event_ptr) {
3605ffd83dbSDimitry Andric   // If step out plan finished, that means we didn't find our way into a method
3615ffd83dbSDimitry Andric   // implementation.  Either we went directly to the default implementation,
3625ffd83dbSDimitry Andric   // of the overridden implementation didn't have debug info.
3635ffd83dbSDimitry Andric   // So we should mark ourselves as done.
3645ffd83dbSDimitry Andric   const bool step_out_should_stop = ThreadPlanStepOut::ShouldStop(event_ptr);
3655ffd83dbSDimitry Andric   if (step_out_should_stop) {
3665ffd83dbSDimitry Andric     SetPlanComplete(true);
3675ffd83dbSDimitry Andric     return true;
3685ffd83dbSDimitry Andric   }
3695ffd83dbSDimitry Andric 
3705ffd83dbSDimitry Andric   // If we have a step through plan, then w're in the process of getting
3715ffd83dbSDimitry Andric   // through an ObjC msgSend.  If we arrived at the target function, then
3725ffd83dbSDimitry Andric   // check whether we have debug info, and if we do, stop.
37381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
3745ffd83dbSDimitry Andric 
3755ffd83dbSDimitry Andric   if (m_objc_step_through_sp && m_objc_step_through_sp->IsPlanComplete()) {
3765ffd83dbSDimitry Andric     // If the plan failed for some reason, we should probably just let the
3775ffd83dbSDimitry Andric     // step over plan get us out of here...  We don't need to do anything about
3785ffd83dbSDimitry Andric     // the step through plan, it is done and will get popped when we continue.
3795ffd83dbSDimitry Andric     if (!m_objc_step_through_sp->PlanSucceeded()) {
3805ffd83dbSDimitry Andric       LLDB_LOGF(log, "ObjC Step through plan failed.  Stepping out.");
3815ffd83dbSDimitry Andric     }
3825ffd83dbSDimitry Andric     Status error;
3835ffd83dbSDimitry Andric     if (InvokeShouldStopHereCallback(eFrameCompareYounger, error)) {
3845ffd83dbSDimitry Andric       SetPlanComplete(true);
3855ffd83dbSDimitry Andric       return true;
3865ffd83dbSDimitry Andric     }
3875ffd83dbSDimitry Andric     // If we didn't want to stop at this msgSend, there might be another so
3885ffd83dbSDimitry Andric     // we should just continue on with the step out and see if our breakpoint
3895ffd83dbSDimitry Andric     // triggers again.
3905ffd83dbSDimitry Andric     m_objc_step_through_sp.reset();
3915ffd83dbSDimitry Andric     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
3925ffd83dbSDimitry Andric       bkpt_sp->SetEnabled(true);
3935ffd83dbSDimitry Andric     }
3945ffd83dbSDimitry Andric     return false;
3955ffd83dbSDimitry Andric   }
3965ffd83dbSDimitry Andric 
3975ffd83dbSDimitry Andric   // If we hit an msgSend breakpoint, then we should queue the step through
3985ffd83dbSDimitry Andric   // plan:
3995ffd83dbSDimitry Andric 
4005ffd83dbSDimitry Andric   if (m_at_msg_send) {
4015ffd83dbSDimitry Andric     LanguageRuntime *objc_runtime
4025ffd83dbSDimitry Andric       = GetThread().GetProcess()->GetLanguageRuntime(eLanguageTypeObjC);
4035ffd83dbSDimitry Andric     // There's no way we could have gotten here without an ObjC language
4045ffd83dbSDimitry Andric     // runtime.
4055ffd83dbSDimitry Andric     assert(objc_runtime);
406fe6060f1SDimitry Andric     m_objc_step_through_sp =
407fe6060f1SDimitry Andric         objc_runtime->GetStepThroughTrampolinePlan(GetThread(), false);
4085ffd83dbSDimitry Andric     // If we failed to find the target for this dispatch, just keep going and
4095ffd83dbSDimitry Andric     // let the step out complete.
4105ffd83dbSDimitry Andric     if (!m_objc_step_through_sp) {
4115ffd83dbSDimitry Andric       LLDB_LOG(log, "Couldn't find target for message dispatch, continuing.");
4125ffd83dbSDimitry Andric       return false;
4135ffd83dbSDimitry Andric     }
4145ffd83dbSDimitry Andric     // Otherwise push the step through plan and continue.
4155ffd83dbSDimitry Andric     GetThread().QueueThreadPlan(m_objc_step_through_sp, false);
4165ffd83dbSDimitry Andric     for (BreakpointSP bkpt_sp : m_msgSend_bkpts) {
4175ffd83dbSDimitry Andric       bkpt_sp->SetEnabled(false);
4185ffd83dbSDimitry Andric     }
4195ffd83dbSDimitry Andric     return false;
4205ffd83dbSDimitry Andric   }
4215ffd83dbSDimitry Andric   return true;
4225ffd83dbSDimitry Andric }
4235ffd83dbSDimitry Andric 
MischiefManaged()4245ffd83dbSDimitry Andric bool AppleThreadPlanStepThroughDirectDispatch::MischiefManaged() {
4255ffd83dbSDimitry Andric   if (IsPlanComplete())
4265ffd83dbSDimitry Andric     return true;
4275ffd83dbSDimitry Andric   return ThreadPlanStepOut::MischiefManaged();
4285ffd83dbSDimitry Andric }
429