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