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 if (log) 145 log->Printf("Got target implementation of 0x0, stopping."); 146 SetPlanComplete(); 147 return true; 148 } 149 if (m_trampoline_handler->AddrIsMsgForward(target_addr)) { 150 if (log) 151 log->Printf( 152 "Implementation lookup returned msgForward function: 0x%" PRIx64 153 ", stopping.", 154 target_addr); 155 156 SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext( 157 eSymbolContextEverything); 158 Status status; 159 const bool abort_other_plans = false; 160 const bool first_insn = true; 161 const uint32_t frame_idx = 0; 162 m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop( 163 abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion, 164 eVoteNoOpinion, frame_idx, status); 165 if (m_run_to_sp && status.Success()) 166 m_run_to_sp->SetPrivate(true); 167 return false; 168 } 169 170 if (log) 171 log->Printf("Running to ObjC method implementation: 0x%" PRIx64, 172 target_addr); 173 174 ObjCLanguageRuntime *objc_runtime = 175 ObjCLanguageRuntime::Get(*GetThread().GetProcess()); 176 assert(objc_runtime != nullptr); 177 objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr); 178 if (log) 179 log->Printf("Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64 180 "} = addr=0x%" PRIx64 " to cache.", 181 m_isa_addr, m_sel_addr, target_addr); 182 183 // Extract the target address from the value: 184 185 m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>( 186 m_thread, target_so_addr, m_stop_others); 187 m_thread.QueueThreadPlan(m_run_to_sp, false); 188 m_run_to_sp->SetPrivate(true); 189 return false; 190 } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) { 191 // Third stage, work the run to target plan. 192 SetPlanComplete(); 193 return true; 194 } 195 return false; 196 } 197 198 // The base class MischiefManaged does some cleanup - so you have to call it in 199 // your MischiefManaged derived class. 200 bool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() { 201 return IsPlanComplete(); 202 } 203 204 bool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; } 205