1 //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp
2 //--------------------------*- C++ -*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // C Includes
12 // C++ Includes
13 // Other libraries and framework includes
14 // Project includes
15 #include "AppleThreadPlanStepThroughObjCTrampoline.h"
16 #include "AppleObjCTrampolineHandler.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Expression/DiagnosticManager.h"
19 #include "lldb/Expression/FunctionCaller.h"
20 #include "lldb/Expression/UtilityFunction.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Target/ThreadPlanRunToAddress.h"
26 #include "lldb/Target/ThreadPlanStepOut.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
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,
38         bool stop_others)
39     : ThreadPlan(ThreadPlan::eKindGeneric,
40                  "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
41                  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) {}
46 
47 //----------------------------------------------------------------------
48 // Destructor
49 //----------------------------------------------------------------------
50 AppleThreadPlanStepThroughObjCTrampoline::
51     ~AppleThreadPlanStepThroughObjCTrampoline() {}
52 
53 void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
54   // Setting up the memory space for the called function text might require
55   // allocations,
56   // i.e. a nested function call.  This needs to be done as a PreResumeAction.
57   m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller,
58                                             (void *)this);
59 }
60 
61 bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
62   if (!m_func_sp) {
63     DiagnosticManager diagnostics;
64     m_args_addr =
65         m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
66 
67     if (m_args_addr == LLDB_INVALID_ADDRESS) {
68       return false;
69     }
70     m_impl_function =
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);
82   }
83   return true;
84 }
85 
86 bool AppleThreadPlanStepThroughObjCTrampoline::
87     PreResumeInitializeFunctionCaller(void *void_myself) {
88   AppleThreadPlanStepThroughObjCTrampoline *myself =
89       static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
90   return myself->InitializeFunctionCaller();
91 }
92 
93 void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
94     Stream *s, lldb::DescriptionLevel level) {
95   if (level == lldb::eDescriptionLevelBrief)
96     s->Printf("Step through ObjC trampoline");
97   else {
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);
102   }
103 }
104 
105 bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
106   return true;
107 }
108 
109 bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
110     Event *event_ptr) {
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
114   // to figure out what to do about that, so we do explain the stop.
115   return true;
116 }
117 
118 lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
119   return eStateRunning;
120 }
121 
122 bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
123   // First stage: we are still handling the "call a function to get the target
124   // of the dispatch"
125   if (m_func_sp) {
126     if (!m_func_sp->IsPlanComplete()) {
127       return false;
128     } else {
129       if (!m_func_sp->PlanSucceeded()) {
130         SetPlanComplete(false);
131         return true;
132       }
133       m_func_sp.reset();
134     }
135   }
136 
137   // Second stage, if all went well with the function calling, then fetch the
138   // target address, and
139   // queue up a "run to that address" plan.
140   if (!m_run_to_sp) {
141     Value target_addr_value;
142     ExecutionContext exc_ctx;
143     m_thread.CalculateExecutionContext(exc_ctx);
144     m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
145                                           target_addr_value);
146     m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
147     lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
148     Address target_so_addr;
149     target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
150     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
151     if (target_addr == 0) {
152       if (log)
153         log->Printf("Got target implementation of 0x0, stopping.");
154       SetPlanComplete();
155       return true;
156     }
157     if (m_trampoline_handler->AddrIsMsgForward(target_addr)) {
158       if (log)
159         log->Printf(
160             "Implementation lookup returned msgForward function: 0x%" PRIx64
161             ", stopping.",
162             target_addr);
163 
164       SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(
165           eSymbolContextEverything);
166       const bool abort_other_plans = false;
167       const bool first_insn = true;
168       const uint32_t frame_idx = 0;
169       m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
170           abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion,
171           eVoteNoOpinion, frame_idx);
172       m_run_to_sp->SetPrivate(true);
173       return false;
174     }
175 
176     if (log)
177       log->Printf("Running to ObjC method implementation: 0x%" PRIx64,
178                   target_addr);
179 
180     ObjCLanguageRuntime *objc_runtime =
181         GetThread().GetProcess()->GetObjCLanguageRuntime();
182     assert(objc_runtime != NULL);
183     objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
184     if (log)
185       log->Printf("Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
186                   "} = addr=0x%" PRIx64 " to cache.",
187                   m_isa_addr, m_sel_addr, target_addr);
188 
189     // Extract the target address from the value:
190 
191     m_run_to_sp.reset(
192         new ThreadPlanRunToAddress(m_thread, target_so_addr, m_stop_others));
193     m_thread.QueueThreadPlan(m_run_to_sp, false);
194     m_run_to_sp->SetPrivate(true);
195     return false;
196   } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
197     // Third stage, work the run to target plan.
198     SetPlanComplete();
199     return true;
200   }
201   return false;
202 }
203 
204 // The base class MischiefManaged does some cleanup - so you have to call it
205 // in your MischiefManaged derived class.
206 bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
207   if (IsPlanComplete())
208     return true;
209   else
210     return false;
211 }
212 
213 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }
214