1 //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp
2 //--------------------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
13 // Other libraries and framework includes
15 #include "AppleThreadPlanStepThroughObjCTrampoline.h"
16 #include "AppleObjCTrampolineHandler.h"
17 #include "lldb/Expression/DiagnosticManager.h"
18 #include "lldb/Expression/FunctionCaller.h"
19 #include "lldb/Expression/UtilityFunction.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/ThreadPlanRunToAddress.h"
25 #include "lldb/Target/ThreadPlanStepOut.h"
26 #include "lldb/Utility/Log.h"
29 using namespace lldb_private;
31 //----------------------------------------------------------------------
32 // ThreadPlanStepThroughObjCTrampoline constructor
33 //----------------------------------------------------------------------
34 AppleThreadPlanStepThroughObjCTrampoline::
35 AppleThreadPlanStepThroughObjCTrampoline(
36 Thread &thread, AppleObjCTrampolineHandler *trampoline_handler,
37 ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr,
39 : ThreadPlan(ThreadPlan::eKindGeneric,
40 "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
42 m_trampoline_handler(trampoline_handler),
43 m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values),
44 m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(NULL),
45 m_stop_others(stop_others) {}
47 //----------------------------------------------------------------------
49 //----------------------------------------------------------------------
50 AppleThreadPlanStepThroughObjCTrampoline::
51 ~AppleThreadPlanStepThroughObjCTrampoline() {}
53 void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
54 // Setting up the memory space for the called function text might require
55 // allocations, i.e. a nested function call. This needs to be done as a
57 m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller,
61 bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
63 DiagnosticManager diagnostics;
65 m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
67 if (m_args_addr == LLDB_INVALID_ADDRESS) {
71 m_trampoline_handler->GetLookupImplementationFunctionCaller();
72 ExecutionContext exc_ctx;
73 EvaluateExpressionOptions options;
74 options.SetUnwindOnError(true);
75 options.SetIgnoreBreakpoints(true);
76 options.SetStopOthers(m_stop_others);
77 m_thread.CalculateExecutionContext(exc_ctx);
78 m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
79 exc_ctx, m_args_addr, options, diagnostics);
80 m_func_sp->SetOkayToDiscard(true);
81 m_thread.QueueThreadPlan(m_func_sp, false);
86 bool AppleThreadPlanStepThroughObjCTrampoline::
87 PreResumeInitializeFunctionCaller(void *void_myself) {
88 AppleThreadPlanStepThroughObjCTrampoline *myself =
89 static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
90 return myself->InitializeFunctionCaller();
93 void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
94 Stream *s, lldb::DescriptionLevel level) {
95 if (level == lldb::eDescriptionLevelBrief)
96 s->Printf("Step through ObjC trampoline");
98 s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: "
99 "0x%" PRIx64 ", sel: 0x%" PRIx64,
100 m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(),
101 m_isa_addr, m_sel_addr);
105 bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
109 bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
111 // If we get asked to explain the stop it will be because something went
112 // wrong (like the implementation for selector function crashed... We're
113 // going to figure out what to do about that, so we do explain the stop.
117 lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
118 return eStateRunning;
121 bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
122 // First stage: we are still handling the "call a function to get the target
125 if (!m_func_sp->IsPlanComplete()) {
128 if (!m_func_sp->PlanSucceeded()) {
129 SetPlanComplete(false);
136 // Second stage, if all went well with the function calling, then fetch the
137 // target address, and queue up a "run to that address" plan.
139 Value target_addr_value;
140 ExecutionContext exc_ctx;
141 m_thread.CalculateExecutionContext(exc_ctx);
142 m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
144 m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
145 lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
146 Address target_so_addr;
147 target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
148 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
149 if (target_addr == 0) {
151 log->Printf("Got target implementation of 0x0, stopping.");
155 if (m_trampoline_handler->AddrIsMsgForward(target_addr)) {
158 "Implementation lookup returned msgForward function: 0x%" PRIx64
162 SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(
163 eSymbolContextEverything);
164 const bool abort_other_plans = false;
165 const bool first_insn = true;
166 const uint32_t frame_idx = 0;
167 m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
168 abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion,
169 eVoteNoOpinion, frame_idx);
170 m_run_to_sp->SetPrivate(true);
175 log->Printf("Running to ObjC method implementation: 0x%" PRIx64,
178 ObjCLanguageRuntime *objc_runtime =
179 GetThread().GetProcess()->GetObjCLanguageRuntime();
180 assert(objc_runtime != NULL);
181 objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
183 log->Printf("Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
184 "} = addr=0x%" PRIx64 " to cache.",
185 m_isa_addr, m_sel_addr, target_addr);
187 // Extract the target address from the value:
190 new ThreadPlanRunToAddress(m_thread, target_so_addr, m_stop_others));
191 m_thread.QueueThreadPlan(m_run_to_sp, false);
192 m_run_to_sp->SetPrivate(true);
194 } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
195 // Third stage, work the run to target plan.
202 // The base class MischiefManaged does some cleanup - so you have to call it in
203 // your MischiefManaged derived class.
204 bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
205 if (IsPlanComplete())
211 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }