1314564Sdim//===-- AppleThreadPlanStepThroughObjCTrampoline.cpp 2293116Semaste// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6293116Semaste// 7293116Semaste//===----------------------------------------------------------------------===// 8293116Semaste 9293116Semaste#include "AppleThreadPlanStepThroughObjCTrampoline.h" 10353358Sdim 11293116Semaste#include "AppleObjCTrampolineHandler.h" 12309124Sdim#include "lldb/Expression/DiagnosticManager.h" 13293116Semaste#include "lldb/Expression/FunctionCaller.h" 14293116Semaste#include "lldb/Expression/UtilityFunction.h" 15293116Semaste#include "lldb/Target/ExecutionContext.h" 16309124Sdim#include "lldb/Target/Process.h" 17309124Sdim#include "lldb/Target/Thread.h" 18293116Semaste#include "lldb/Target/ThreadPlanRunToAddress.h" 19293116Semaste#include "lldb/Target/ThreadPlanStepOut.h" 20321369Sdim#include "lldb/Utility/Log.h" 21293116Semaste 22353358Sdim#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" 23353358Sdim 24353358Sdim#include <memory> 25353358Sdim 26293116Semasteusing namespace lldb; 27293116Semasteusing namespace lldb_private; 28293116Semaste 29293116Semaste// ThreadPlanStepThroughObjCTrampoline constructor 30314564SdimAppleThreadPlanStepThroughObjCTrampoline:: 31314564Sdim AppleThreadPlanStepThroughObjCTrampoline( 32314564Sdim Thread &thread, AppleObjCTrampolineHandler *trampoline_handler, 33314564Sdim ValueList &input_values, lldb::addr_t isa_addr, lldb::addr_t sel_addr, 34314564Sdim bool stop_others) 35314564Sdim : ThreadPlan(ThreadPlan::eKindGeneric, 36314564Sdim "MacOSX Step through ObjC Trampoline", thread, eVoteNoOpinion, 37314564Sdim eVoteNoOpinion), 38314564Sdim m_trampoline_handler(trampoline_handler), 39314564Sdim m_args_addr(LLDB_INVALID_ADDRESS), m_input_values(input_values), 40353358Sdim m_isa_addr(isa_addr), m_sel_addr(sel_addr), m_impl_function(nullptr), 41314564Sdim m_stop_others(stop_others) {} 42293116Semaste 43293116Semaste// Destructor 44314564SdimAppleThreadPlanStepThroughObjCTrampoline:: 45314564Sdim ~AppleThreadPlanStepThroughObjCTrampoline() {} 46293116Semaste 47314564Sdimvoid AppleThreadPlanStepThroughObjCTrampoline::DidPush() { 48314564Sdim // Setting up the memory space for the called function text might require 49341825Sdim // allocations, i.e. a nested function call. This needs to be done as a 50341825Sdim // PreResumeAction. 51314564Sdim m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller, 52314564Sdim (void *)this); 53293116Semaste} 54293116Semaste 55314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() { 56314564Sdim if (!m_func_sp) { 57314564Sdim DiagnosticManager diagnostics; 58314564Sdim m_args_addr = 59314564Sdim m_trampoline_handler->SetupDispatchFunction(m_thread, m_input_values); 60309124Sdim 61314564Sdim if (m_args_addr == LLDB_INVALID_ADDRESS) { 62314564Sdim return false; 63293116Semaste } 64314564Sdim m_impl_function = 65314564Sdim m_trampoline_handler->GetLookupImplementationFunctionCaller(); 66314564Sdim ExecutionContext exc_ctx; 67314564Sdim EvaluateExpressionOptions options; 68314564Sdim options.SetUnwindOnError(true); 69314564Sdim options.SetIgnoreBreakpoints(true); 70314564Sdim options.SetStopOthers(m_stop_others); 71314564Sdim m_thread.CalculateExecutionContext(exc_ctx); 72314564Sdim m_func_sp = m_impl_function->GetThreadPlanToCallFunction( 73314564Sdim exc_ctx, m_args_addr, options, diagnostics); 74314564Sdim m_func_sp->SetOkayToDiscard(true); 75314564Sdim m_thread.QueueThreadPlan(m_func_sp, false); 76314564Sdim } 77314564Sdim return true; 78293116Semaste} 79293116Semaste 80314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline:: 81314564Sdim PreResumeInitializeFunctionCaller(void *void_myself) { 82314564Sdim AppleThreadPlanStepThroughObjCTrampoline *myself = 83314564Sdim static_cast<AppleThreadPlanStepThroughObjCTrampoline *>(void_myself); 84314564Sdim return myself->InitializeFunctionCaller(); 85293116Semaste} 86293116Semaste 87314564Sdimvoid AppleThreadPlanStepThroughObjCTrampoline::GetDescription( 88314564Sdim Stream *s, lldb::DescriptionLevel level) { 89314564Sdim if (level == lldb::eDescriptionLevelBrief) 90314564Sdim s->Printf("Step through ObjC trampoline"); 91314564Sdim else { 92314564Sdim s->Printf("Stepping to implementation of ObjC method - obj: 0x%llx, isa: " 93314564Sdim "0x%" PRIx64 ", sel: 0x%" PRIx64, 94314564Sdim m_input_values.GetValueAtIndex(0)->GetScalar().ULongLong(), 95314564Sdim m_isa_addr, m_sel_addr); 96314564Sdim } 97293116Semaste} 98314564Sdim 99314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::ValidatePlan(Stream *error) { 100314564Sdim return true; 101293116Semaste} 102293116Semaste 103314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::DoPlanExplainsStop( 104314564Sdim Event *event_ptr) { 105314564Sdim // If we get asked to explain the stop it will be because something went 106314564Sdim // wrong (like the implementation for selector function crashed... We're 107341825Sdim // going to figure out what to do about that, so we do explain the stop. 108314564Sdim return true; 109293116Semaste} 110293116Semaste 111314564Sdimlldb::StateType AppleThreadPlanStepThroughObjCTrampoline::GetPlanRunState() { 112314564Sdim return eStateRunning; 113293116Semaste} 114293116Semaste 115314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) { 116314564Sdim // First stage: we are still handling the "call a function to get the target 117314564Sdim // of the dispatch" 118314564Sdim if (m_func_sp) { 119314564Sdim if (!m_func_sp->IsPlanComplete()) { 120314564Sdim return false; 121314564Sdim } else { 122314564Sdim if (!m_func_sp->PlanSucceeded()) { 123314564Sdim SetPlanComplete(false); 124314564Sdim return true; 125314564Sdim } 126314564Sdim m_func_sp.reset(); 127293116Semaste } 128314564Sdim } 129293116Semaste 130314564Sdim // Second stage, if all went well with the function calling, then fetch the 131341825Sdim // target address, and queue up a "run to that address" plan. 132314564Sdim if (!m_run_to_sp) { 133314564Sdim Value target_addr_value; 134314564Sdim ExecutionContext exc_ctx; 135314564Sdim m_thread.CalculateExecutionContext(exc_ctx); 136314564Sdim m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr, 137314564Sdim target_addr_value); 138314564Sdim m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr); 139314564Sdim lldb::addr_t target_addr = target_addr_value.GetScalar().ULongLong(); 140314564Sdim Address target_so_addr; 141314564Sdim target_so_addr.SetOpcodeLoadAddress(target_addr, exc_ctx.GetTargetPtr()); 142314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 143314564Sdim if (target_addr == 0) { 144360784Sdim LLDB_LOGF(log, "Got target implementation of 0x0, stopping."); 145314564Sdim SetPlanComplete(); 146314564Sdim return true; 147314564Sdim } 148314564Sdim if (m_trampoline_handler->AddrIsMsgForward(target_addr)) { 149360784Sdim LLDB_LOGF(log, 150360784Sdim "Implementation lookup returned msgForward function: 0x%" PRIx64 151360784Sdim ", stopping.", 152360784Sdim target_addr); 153293116Semaste 154314564Sdim SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext( 155314564Sdim eSymbolContextEverything); 156344779Sdim Status status; 157314564Sdim const bool abort_other_plans = false; 158314564Sdim const bool first_insn = true; 159314564Sdim const uint32_t frame_idx = 0; 160314564Sdim m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop( 161314564Sdim abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion, 162344779Sdim eVoteNoOpinion, frame_idx, status); 163344779Sdim if (m_run_to_sp && status.Success()) 164344779Sdim m_run_to_sp->SetPrivate(true); 165314564Sdim return false; 166293116Semaste } 167314564Sdim 168360784Sdim LLDB_LOGF(log, "Running to ObjC method implementation: 0x%" PRIx64, 169360784Sdim target_addr); 170314564Sdim 171314564Sdim ObjCLanguageRuntime *objc_runtime = 172353358Sdim ObjCLanguageRuntime::Get(*GetThread().GetProcess()); 173353358Sdim assert(objc_runtime != nullptr); 174314564Sdim objc_runtime->AddToMethodCache(m_isa_addr, m_sel_addr, target_addr); 175360784Sdim LLDB_LOGF(log, 176360784Sdim "Adding {isa-addr=0x%" PRIx64 ", sel-addr=0x%" PRIx64 177360784Sdim "} = addr=0x%" PRIx64 " to cache.", 178360784Sdim m_isa_addr, m_sel_addr, target_addr); 179314564Sdim 180314564Sdim // Extract the target address from the value: 181314564Sdim 182353358Sdim m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>( 183353358Sdim m_thread, target_so_addr, m_stop_others); 184314564Sdim m_thread.QueueThreadPlan(m_run_to_sp, false); 185314564Sdim m_run_to_sp->SetPrivate(true); 186293116Semaste return false; 187314564Sdim } else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) { 188314564Sdim // Third stage, work the run to target plan. 189314564Sdim SetPlanComplete(); 190314564Sdim return true; 191314564Sdim } 192314564Sdim return false; 193293116Semaste} 194293116Semaste 195341825Sdim// The base class MischiefManaged does some cleanup - so you have to call it in 196341825Sdim// your MischiefManaged derived class. 197314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::MischiefManaged() { 198344779Sdim return IsPlanComplete(); 199293116Semaste} 200293116Semaste 201314564Sdimbool AppleThreadPlanStepThroughObjCTrampoline::WillStop() { return true; } 202