1 //===-- AppleThreadPlanStepThroughObjCTrampoline.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 "AppleThreadPlanStepThroughObjCTrampoline.h"
11 #include "AppleObjCTrampolineHandler.h"
12 #include "lldb/Expression/DiagnosticManager.h"
13 #include "lldb/Expression/FunctionCaller.h"
14 #include "lldb/Expression/UtilityFunction.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Target/ThreadPlanRunToAddress.h"
19 #include "lldb/Target/ThreadPlanStepOut.h"
20 #include "lldb/Utility/Log.h"
22 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
27 using namespace lldb_private;
29 // ThreadPlanStepThroughObjCTrampoline constructor
30 AppleThreadPlanStepThroughObjCTrampoline::
31 AppleThreadPlanStepThroughObjCTrampoline(
32 Thread &thread, AppleObjCTrampolineHandler *trampoline_handler,
33 ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr,
35 : ThreadPlan(ThreadPlan::eKindGeneric,
36 "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
38 m_trampoline_handler(trampoline_handler),
39 m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values),
40 m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(nullptr),
41 m_stop_others(stop_others) {}
44 AppleThreadPlanStepThroughObjCTrampoline::
45 ~AppleThreadPlanStepThroughObjCTrampoline() {}
47 void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
48 // Setting up the memory space for the called function text might require
49 // allocations, i.e. a nested function call. This needs to be done as a
51 m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller,
55 bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
57 DiagnosticManager diagnostics;
59 m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
61 if (m_args_addr == LLDB_INVALID_ADDRESS) {
65 m_trampoline_handler->GetLookupImplementationFunctionCaller();
66 ExecutionContext exc_ctx;
67 EvaluateExpressionOptions options;
68 options.SetUnwindOnError(true);
69 options.SetIgnoreBreakpoints(true);
70 options.SetStopOthers(m_stop_others);
71 m_thread.CalculateExecutionContext(exc_ctx);
72 m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
73 exc_ctx, m_args_addr, options, diagnostics);
74 m_func_sp->SetOkayToDiscard(true);
75 m_thread.QueueThreadPlan(m_func_sp, false);
80 bool AppleThreadPlanStepThroughObjCTrampoline::
81 PreResumeInitializeFunctionCaller(void *void_myself) {
82 AppleThreadPlanStepThroughObjCTrampoline *myself =
83 static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
84 return myself->InitializeFunctionCaller();
87 void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
88 Stream *s, lldb::DescriptionLevel level) {
89 if (level == lldb::eDescriptionLevelBrief)
90 s->Printf("Step through ObjC trampoline");
92 s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: "
93 "0x%" PRIx64 ", sel: 0x%" PRIx64,
94 m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(),
95 m_isa_addr, m_sel_addr);
99 bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
103 bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
105 // If we get asked to explain the stop it will be because something went
106 // wrong (like the implementation for selector function crashed... We're
107 // going to figure out what to do about that, so we do explain the stop.
111 lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
112 return eStateRunning;
115 bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
116 // First stage: we are still handling the "call a function to get the target
119 if (!m_func_sp->IsPlanComplete()) {
122 if (!m_func_sp->PlanSucceeded()) {
123 SetPlanComplete(false);
130 // Second stage, if all went well with the function calling, then fetch the
131 // target address, and queue up a "run to that address" plan.
133 Value target_addr_value;
134 ExecutionContext exc_ctx;
135 m_thread.CalculateExecutionContext(exc_ctx);
136 m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
138 m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
139 lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
140 Address target_so_addr;
141 target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
142 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
143 if (target_addr == 0) {
144 LLDB_LOGF(log, "Got target implementation of 0x0, stopping.");
148 if (m_trampoline_handler->AddrIsMsgForward(target_addr)) {
150 "Implementation lookup returned msgForward function: 0x%" PRIx64
154 SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(
155 eSymbolContextEverything);
157 const bool abort_other_plans = false;
158 const bool first_insn = true;
159 const uint32_t frame_idx = 0;
160 m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
161 abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion,
162 eVoteNoOpinion, frame_idx, status);
163 if (m_run_to_sp && status.Success())
164 m_run_to_sp->SetPrivate(true);
168 LLDB_LOGF(log, "Running to ObjC method implementation: 0x%" PRIx64,
171 ObjCLanguageRuntime *objc_runtime =
172 ObjCLanguageRuntime::Get(*GetThread().GetProcess());
173 assert(objc_runtime != nullptr);
174 objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
176 "Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
177 "} = addr=0x%" PRIx64 " to cache.",
178 m_isa_addr, m_sel_addr, target_addr);
180 // Extract the target address from the value:
182 m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>(
183 m_thread, target_so_addr, m_stop_others);
184 m_thread.QueueThreadPlan(m_run_to_sp, false);
185 m_run_to_sp->SetPrivate(true);
187 } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
188 // Third stage, work the run to target plan.
195 // The base class MischiefManaged does some cleanup - so you have to call it in
196 // your MischiefManaged derived class.
197 bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
198 return IsPlanComplete();
201 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }