af630eee72654750083ee1fd4c511b1323f4336d
[openbsd] /
1 //===-- AppleThreadPlanStepThroughObjCTrampoline.cpp
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "AppleThreadPlanStepThroughObjCTrampoline.h"
10
11 #include "AppleObjCTrampolineHandler.h"
12 #include "lldb/Expression/DiagnosticManager.h"
13 #include "lldb/Expression/FunctionCaller.h"
14 #include "lldb/Expression/UtilityFunction.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Target/ThreadPlanRunToAddress.h"
19 #include "lldb/Target/ThreadPlanStepOut.h"
20 #include "lldb/Utility/Log.h"
21
22 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
23
24 #include <memory>
25
26 using namespace lldb;
27 using namespace lldb_private;
28
29 // ThreadPlanStepThroughObjCTrampoline constructor
30 AppleThreadPlanStepThroughObjCTrampoline::
31     AppleThreadPlanStepThroughObjCTrampoline(
32         Thread &thread, AppleObjCTrampolineHandler *trampoline_handler,
33         ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr,
34         bool stop_others)
35     : ThreadPlan(ThreadPlan::eKindGeneric,
36                  "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion,
37                  eVoteNoOpinion),
38       m_trampoline_handler(trampoline_handler),
39       m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values),
40       m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(nullptr),
41       m_stop_others(stop_others) {}
42
43 // Destructor
44 AppleThreadPlanStepThroughObjCTrampoline::
45     ~AppleThreadPlanStepThroughObjCTrampoline() {}
46
47 void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
48   // Setting up the memory space for the called function text might require
49   // allocations, i.e. a nested function call.  This needs to be done as a
50   // PreResumeAction.
51   m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller,
52                                             (void *)this);
53 }
54
55 bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
56   if (!m_func_sp) {
57     DiagnosticManager diagnostics;
58     m_args_addr =
59         m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values);
60
61     if (m_args_addr == LLDB_INVALID_ADDRESS) {
62       return false;
63     }
64     m_impl_function =
65         m_trampoline_handler->GetLookupImplementationFunctionCaller();
66     ExecutionContext exc_ctx;
67     EvaluateExpressionOptions options;
68     options.SetUnwindOnError(true);
69     options.SetIgnoreBreakpoints(true);
70     options.SetStopOthers(m_stop_others);
71     m_thread.CalculateExecutionContext(exc_ctx);
72     m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
73         exc_ctx, m_args_addr, options, diagnostics);
74     m_func_sp->SetOkayToDiscard(true);
75     m_thread.QueueThreadPlan(m_func_sp, false);
76   }
77   return true;
78 }
79
80 bool AppleThreadPlanStepThroughObjCTrampoline::
81     PreResumeInitializeFunctionCaller(void *void_myself) {
82   AppleThreadPlanStepThroughObjCTrampoline *myself =
83       static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself);
84   return myself->InitializeFunctionCaller();
85 }
86
87 void AppleThreadPlanStepThroughObjCTrampoline::GetDescription(
88     Stream *s, lldb::DescriptionLevel level) {
89   if (level == lldb::eDescriptionLevelBrief)
90     s->Printf("Step through ObjC trampoline");
91   else {
92     s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: "
93               "0x%" PRIx64 ", sel: 0x%" PRIx64,
94               m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(),
95               m_isa_addr, m_sel_addr);
96   }
97 }
98
99 bool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) {
100   return true;
101 }
102
103 bool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop(
104     Event *event_ptr) {
105   // If we get asked to explain the stop it will be because something went
106   // wrong (like the implementation for selector function crashed...  We're
107   // going to figure out what to do about that, so we do explain the stop.
108   return true;
109 }
110
111 lldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() {
112   return eStateRunning;
113 }
114
115 bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
116   // First stage: we are still handling the "call a function to get the target
117   // of the dispatch"
118   if (m_func_sp) {
119     if (!m_func_sp->IsPlanComplete()) {
120       return false;
121     } else {
122       if (!m_func_sp->PlanSucceeded()) {
123         SetPlanComplete(false);
124         return true;
125       }
126       m_func_sp.reset();
127     }
128   }
129
130   // Second stage, if all went well with the function calling, then fetch the
131   // target address, and queue up a "run to that address" plan.
132   if (!m_run_to_sp) {
133     Value target_addr_value;
134     ExecutionContext exc_ctx;
135     m_thread.CalculateExecutionContext(exc_ctx);
136     m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
137                                           target_addr_value);
138     m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
139     lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong();
140     Address target_so_addr;
141     target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr());
142     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
143     if (target_addr == 0) {
144       LLDB_LOGF(log, "Got target implementation of 0x0, stopping.");
145       SetPlanComplete();
146       return true;
147     }
148     if (m_trampoline_handler->AddrIsMsgForward(target_addr)) {
149       LLDB_LOGF(log,
150                 "Implementation lookup returned msgForward function: 0x%" PRIx64
151                 ", stopping.",
152                 target_addr);
153
154       SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(
155           eSymbolContextEverything);
156       Status status;
157       const bool abort_other_plans = false;
158       const bool first_insn = true;
159       const uint32_t frame_idx = 0;
160       m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
161           abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion,
162           eVoteNoOpinion, frame_idx, status);
163       if (m_run_to_sp && status.Success())
164         m_run_to_sp->SetPrivate(true);
165       return false;
166     }
167
168     LLDB_LOGF(log, "Running to ObjC method implementation: 0x%" PRIx64,
169               target_addr);
170
171     ObjCLanguageRuntime *objc_runtime =
172         ObjCLanguageRuntime::Get(*GetThread().GetProcess());
173     assert(objc_runtime != nullptr);
174     objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr);
175     LLDB_LOGF(log,
176               "Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64
177               "} = addr=0x%" PRIx64 " to cache.",
178               m_isa_addr, m_sel_addr, target_addr);
179
180     // Extract the target address from the value:
181
182     m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>(
183         m_thread, target_so_addr, m_stop_others);
184     m_thread.QueueThreadPlan(m_run_to_sp, false);
185     m_run_to_sp->SetPrivate(true);
186     return false;
187   } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
188     // Third stage, work the run to target plan.
189     SetPlanComplete();
190     return true;
191   }
192   return false;
193 }
194
195 // The base class MischiefManaged does some cleanup - so you have to call it in
196 // your MischiefManaged derived class.
197 bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() {
198   return IsPlanComplete();
199 }
200
201 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; }