1 //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "AppleThreadPlanStepThroughObjCTrampoline.h"
15 #include "AppleObjCTrampolineHandler.h"
16 #include "lldb/Core/Log.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"
28 using namespace lldb_private;
30 //----------------------------------------------------------------------
31 // ThreadPlanStepThroughObjCTrampoline constructor
32 //----------------------------------------------------------------------
33 AppleThreadPlanStepThroughObjCTrampoline::AppleThreadPlanStepThroughObjCTrampoline
36 AppleObjCTrampolineHandler *trampoline_handler,
37 ValueList &input_values,
38 lldb::addr_t isa_addr,
39 lldb::addr_t sel_addr,
42 ThreadPlan (ThreadPlan::eKindGeneric,
43 "MacOSX Step through ObjC Trampoline",
47 m_trampoline_handler (trampoline_handler),
48 m_args_addr (LLDB_INVALID_ADDRESS),
49 m_input_values (input_values),
52 m_impl_function (NULL),
53 m_stop_others (stop_others)
58 //----------------------------------------------------------------------
60 //----------------------------------------------------------------------
61 AppleThreadPlanStepThroughObjCTrampoline::~AppleThreadPlanStepThroughObjCTrampoline()
66 AppleThreadPlanStepThroughObjCTrampoline::DidPush ()
68 // Setting up the memory space for the called function text might require allocations,
69 // i.e. a nested function call. This needs to be done as a PreResumeAction.
70 m_thread.GetProcess()->AddPreResumeAction (PreResumeInitializeFunctionCaller, (void *) this);
74 AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller ()
78 DiagnosticManager diagnostics;
79 m_args_addr = m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
81 if (m_args_addr == LLDB_INVALID_ADDRESS)
85 m_impl_function = m_trampoline_handler->GetLookupImplementationFunctionCaller();
86 ExecutionContext exc_ctx;
87 EvaluateExpressionOptions options;
88 options.SetUnwindOnError(true);
89 options.SetIgnoreBreakpoints(true);
90 options.SetStopOthers(m_stop_others);
91 m_thread.CalculateExecutionContext(exc_ctx);
92 m_func_sp = m_impl_function->GetThreadPlanToCallFunction(exc_ctx, m_args_addr, options, diagnostics);
93 m_func_sp->SetOkayToDiscard(true);
94 m_thread.QueueThreadPlan(m_func_sp, false);
100 AppleThreadPlanStepThroughObjCTrampoline::PreResumeInitializeFunctionCaller(void *void_myself)
102 AppleThreadPlanStepThroughObjCTrampoline *myself = static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
103 return myself->InitializeFunctionCaller();
107 AppleThreadPlanStepThroughObjCTrampoline::GetDescription (Stream *s,
108 lldb::DescriptionLevel level)
110 if (level == lldb::eDescriptionLevelBrief)
111 s->Printf("Step through ObjC trampoline");
114 s->Printf ("Stepping to implementation of ObjC method - obj: 0x%llx, isa: 0x%" PRIx64 ", sel: 0x%" PRIx64,
115 m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(), m_isa_addr, m_sel_addr);
120 AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan (Stream *error)
126 AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop (Event *event_ptr)
128 // If we get asked to explain the stop it will be because something went
129 // wrong (like the implementation for selector function crashed... We're going
130 // to figure out what to do about that, so we do explain the stop.
135 AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState ()
137 return eStateRunning;
141 AppleThreadPlanStepThroughObjCTrampoline::ShouldStop (Event *event_ptr)
143 // First stage: we are still handling the "call a function to get the target of the dispatch"
146 if (!m_func_sp->IsPlanComplete())
152 if (!m_func_sp->PlanSucceeded())
154 SetPlanComplete(false);
161 // Second stage, if all went well with the function calling, then fetch the target address, and
162 // queue up a "run to that address" plan.
165 Value target_addr_value;
166 ExecutionContext exc_ctx;
167 m_thread.CalculateExecutionContext(exc_ctx);
168 m_impl_function->FetchFunctionResults (exc_ctx, m_args_addr, target_addr_value);
169 m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
170 lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
171 Address target_so_addr;
172 target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
173 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
174 if (target_addr == 0)
177 log->Printf("Got target implementation of 0x0, stopping.");
181 if (m_trampoline_handler->AddrIsMsgForward(target_addr))
184 log->Printf ("Implementation lookup returned msgForward function: 0x%" PRIx64 ", stopping.", target_addr);
186 SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(eSymbolContextEverything);
187 const bool abort_other_plans = false;
188 const bool first_insn = true;
189 const uint32_t frame_idx = 0;
190 m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (abort_other_plans,
197 m_run_to_sp->SetPrivate(true);
202 log->Printf("Running to ObjC method implementation: 0x%" PRIx64, target_addr);
204 ObjCLanguageRuntime *objc_runtime = GetThread().GetProcess()->GetObjCLanguageRuntime();
205 assert (objc_runtime != NULL);
206 objc_runtime->AddToMethodCache (m_isa_addr, m_sel_addr, target_addr);
208 log->Printf("Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64 "} = addr=0x%" PRIx64 " to cache.", m_isa_addr, m_sel_addr, target_addr);
210 // Extract the target address from the value:
212 m_run_to_sp.reset(new ThreadPlanRunToAddress(m_thread, target_so_addr, m_stop_others));
213 m_thread.QueueThreadPlan(m_run_to_sp, false);
214 m_run_to_sp->SetPrivate(true);
217 else if (m_thread.IsThreadPlanDone(m_run_to_sp.get()))
219 // Third stage, work the run to target plan.
226 // The base class MischiefManaged does some cleanup - so you have to call it
227 // in your MischiefManaged derived class.
229 AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged ()
231 if (IsPlanComplete())
238 AppleThreadPlanStepThroughObjCTrampoline::WillStop()