]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / LanguageRuntime / ObjC / AppleObjCRuntime / AppleThreadPlanStepThroughObjCTrampoline.cpp
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/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"
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, i.e. a nested function call.  This needs to be done as a
56   // 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 to figure out what to do about that, so we do explain the stop.
114   return true;
115 }
116
117 lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
118   return eStateRunning;
119 }
120
121 bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
122   // First stage: we are still handling the "call a function to get the target
123   // of the dispatch"
124   if (m_func_sp) {
125     if (!m_func_sp->IsPlanComplete()) {
126       return false;
127     } else {
128       if (!m_func_sp->PlanSucceeded()) {
129         SetPlanComplete(false);
130         return true;
131       }
132       m_func_sp.reset();
133     }
134   }
135
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.
138   if (!m_run_to_sp) {
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,
143                                           target_addr_value);
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) {
150       if (log)
151         log->Printf("Got target implementation of 0x0, stopping.");
152       SetPlanComplete();
153       return true;
154     }
155     if (m_trampoline_handler->AddrIsMsgForward(target_addr)) {
156       if (log)
157         log->Printf(
158             "Implementation lookup returned msgForward function: 0x%" PRIx64
159             ", stopping.",
160             target_addr);
161
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);
171       return false;
172     }
173
174     if (log)
175       log->Printf("Running to ObjC method implementation: 0x%" PRIx64,
176                   target_addr);
177
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);
182     if (log)
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);
186
187     // Extract the target address from the value:
188
189     m_run_to_sp.reset(
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);
193     return false;
194   } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
195     // Third stage, work the run to target plan.
196     SetPlanComplete();
197     return true;
198   }
199   return false;
200 }
201
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())
206     return true;
207   else
208     return false;
209 }
210
211 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }