]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / source / Plugins / LanguageRuntime / ObjC / AppleObjCRuntime / AppleThreadPlanStepThroughObjCTrampoline.cpp
1 //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project 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"
26
27 using namespace lldb;
28 using namespace lldb_private;
29
30 //----------------------------------------------------------------------
31 // ThreadPlanStepThroughObjCTrampoline constructor
32 //----------------------------------------------------------------------
33 AppleThreadPlanStepThroughObjCTrampoline::AppleThreadPlanStepThroughObjCTrampoline
34 (
35     Thread &thread, 
36     AppleObjCTrampolineHandler *trampoline_handler,
37     ValueList &input_values,
38     lldb::addr_t isa_addr,
39     lldb::addr_t sel_addr,
40     bool stop_others
41 ) :
42     ThreadPlan (ThreadPlan::eKindGeneric, 
43                 "MacOSX Step through ObjC Trampoline", 
44                 thread, 
45                 eVoteNoOpinion, 
46                 eVoteNoOpinion),
47     m_trampoline_handler (trampoline_handler),
48     m_args_addr (LLDB_INVALID_ADDRESS),
49     m_input_values (input_values),
50     m_isa_addr(isa_addr),
51     m_sel_addr(sel_addr),
52     m_impl_function (NULL),
53     m_stop_others (stop_others)
54 {
55     
56 }
57
58 //----------------------------------------------------------------------
59 // Destructor
60 //----------------------------------------------------------------------
61 AppleThreadPlanStepThroughObjCTrampoline::~AppleThreadPlanStepThroughObjCTrampoline()
62 {
63 }
64
65 void
66 AppleThreadPlanStepThroughObjCTrampoline::DidPush ()
67 {
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);
71 }
72
73 bool
74 AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller ()
75 {
76     if (!m_func_sp)
77     {
78         DiagnosticManager diagnostics;
79         m_args_addr = m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
80
81         if (m_args_addr == LLDB_INVALID_ADDRESS)
82         {
83             return false;
84         }
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);
95     }
96     return true;
97 }
98
99 bool
100 AppleThreadPlanStepThroughObjCTrampoline::PreResumeInitializeFunctionCaller(void *void_myself)
101 {
102     AppleThreadPlanStepThroughObjCTrampoline *myself = static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
103     return myself->InitializeFunctionCaller();
104 }
105
106 void
107 AppleThreadPlanStepThroughObjCTrampoline::GetDescription (Stream *s,
108                                                           lldb::DescriptionLevel level)
109 {
110     if (level == lldb::eDescriptionLevelBrief)
111         s->Printf("Step through ObjC trampoline");
112     else
113     {
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);
116     }
117 }
118                 
119 bool
120 AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan (Stream *error)
121 {
122     return true;
123 }
124
125 bool
126 AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop (Event *event_ptr)
127 {
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.
131     return true;
132 }
133
134 lldb::StateType
135 AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState ()
136 {
137     return eStateRunning;
138 }
139
140 bool
141 AppleThreadPlanStepThroughObjCTrampoline::ShouldStop (Event *event_ptr)
142 {
143     // First stage: we are still handling the "call a function to get the target of the dispatch"
144     if (m_func_sp)
145     {
146         if (!m_func_sp->IsPlanComplete())
147         {
148             return false;
149         }
150         else
151         {
152             if (!m_func_sp->PlanSucceeded())
153             {
154                 SetPlanComplete(false);
155                 return true;
156             }
157             m_func_sp.reset();
158         }
159     }
160     
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.
163     if (!m_run_to_sp) 
164     {
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)
175         {
176             if (log)
177                 log->Printf("Got target implementation of 0x0, stopping.");
178             SetPlanComplete();
179             return true;
180         }
181         if (m_trampoline_handler->AddrIsMsgForward(target_addr))
182         {
183             if (log)
184                 log->Printf ("Implementation lookup returned msgForward function: 0x%" PRIx64 ", stopping.", target_addr);
185
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,
191                                            &sc,
192                                            first_insn,
193                                            m_stop_others,
194                                            eVoteNoOpinion,
195                                            eVoteNoOpinion,
196                                            frame_idx);
197             m_run_to_sp->SetPrivate(true);
198             return false;
199         }
200         
201         if (log)
202             log->Printf("Running to ObjC method implementation: 0x%" PRIx64, target_addr);
203         
204         ObjCLanguageRuntime *objc_runtime = GetThread().GetProcess()->GetObjCLanguageRuntime();
205         assert (objc_runtime != NULL);
206         objc_runtime->AddToMethodCache (m_isa_addr, m_sel_addr, target_addr);
207         if (log)
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);
209
210         // Extract the target address from the value:
211         
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);
215         return false;
216     }
217     else if (m_thread.IsThreadPlanDone(m_run_to_sp.get()))
218     {
219         // Third stage, work the run to target plan.
220         SetPlanComplete();
221         return true;
222     }
223     return false;
224 }
225
226 // The base class MischiefManaged does some cleanup - so you have to call it
227 // in your MischiefManaged derived class.
228 bool
229 AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged ()
230 {
231     if (IsPlanComplete())
232         return true;
233     else
234         return false;
235 }
236
237 bool
238 AppleThreadPlanStepThroughObjCTrampoline::WillStop()
239 {
240     return true;
241 }