AppleObjCTrampolineHandler.cpp revision 341825
1314564Sdim//===-- AppleObjCTrampolineHandler.cpp ----------------------------*- C++ 2314564Sdim//-*-===// 3293116Semaste// 4293116Semaste// The LLVM Compiler Infrastructure 5293116Semaste// 6293116Semaste// This file is distributed under the University of Illinois Open Source 7293116Semaste// License. See LICENSE.TXT for details. 8293116Semaste// 9293116Semaste//===----------------------------------------------------------------------===// 10293116Semaste 11293116Semaste#include "AppleObjCTrampolineHandler.h" 12293116Semaste 13293116Semaste// C Includes 14293116Semaste// C++ Includes 15293116Semaste// Other libraries and framework includes 16293116Semaste// Project includes 17293116Semaste#include "AppleThreadPlanStepThroughObjCTrampoline.h" 18293116Semaste 19293116Semaste#include "lldb/Breakpoint/StoppointCallbackContext.h" 20293116Semaste#include "lldb/Core/Debugger.h" 21293116Semaste#include "lldb/Core/Module.h" 22293116Semaste#include "lldb/Core/StreamFile.h" 23293116Semaste#include "lldb/Core/Value.h" 24309124Sdim#include "lldb/Expression/DiagnosticManager.h" 25309124Sdim#include "lldb/Expression/FunctionCaller.h" 26293116Semaste#include "lldb/Expression/UserExpression.h" 27293116Semaste#include "lldb/Expression/UtilityFunction.h" 28293116Semaste#include "lldb/Symbol/ClangASTContext.h" 29293116Semaste#include "lldb/Symbol/Symbol.h" 30293116Semaste#include "lldb/Target/ABI.h" 31309124Sdim#include "lldb/Target/ExecutionContext.h" 32293116Semaste#include "lldb/Target/ObjCLanguageRuntime.h" 33293116Semaste#include "lldb/Target/Process.h" 34293116Semaste#include "lldb/Target/RegisterContext.h" 35293116Semaste#include "lldb/Target/Target.h" 36293116Semaste#include "lldb/Target/Thread.h" 37293116Semaste#include "lldb/Target/ThreadPlanRunToAddress.h" 38321369Sdim#include "lldb/Utility/ConstString.h" 39321369Sdim#include "lldb/Utility/FileSpec.h" 40321369Sdim#include "lldb/Utility/Log.h" 41293116Semaste 42293116Semaste#include "llvm/ADT/STLExtras.h" 43293116Semaste 44293116Semasteusing namespace lldb; 45293116Semasteusing namespace lldb_private; 46293116Semaste 47314564Sdimconst char *AppleObjCTrampolineHandler::g_lookup_implementation_function_name = 48314564Sdim "__lldb_objc_find_implementation_for_selector"; 49314564Sdimconst char *AppleObjCTrampolineHandler:: 50314564Sdim g_lookup_implementation_with_stret_function_code = 51314564Sdim " \n\ 52341825Sdimextern \"C\" \n\ 53341825Sdim{ \n\ 54341825Sdim extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 55341825Sdim extern void *class_getMethodImplementation_stret(void *objc_class, \n\ 56341825Sdim void *sel); \n\ 57341825Sdim extern void * object_getClass (id object); \n\ 58341825Sdim extern void * sel_getUid(char *name); \n\ 59341825Sdim extern int printf(const char *format, ...); \n\ 60341825Sdim} \n\ 61341825Sdimextern \"C\" void * __lldb_objc_find_implementation_for_selector ( \n\ 62341825Sdim void *object, \n\ 63341825Sdim void *sel, \n\ 64341825Sdim int is_stret, \n\ 65341825Sdim int is_super, \n\ 66341825Sdim int is_super2, \n\ 67341825Sdim int is_fixup, \n\ 68341825Sdim int is_fixed, \n\ 69341825Sdim int debug) \n\ 70341825Sdim{ \n\ 71341825Sdim struct __lldb_imp_return_struct \n\ 72341825Sdim { \n\ 73341825Sdim void *class_addr; \n\ 74341825Sdim void *sel_addr; \n\ 75341825Sdim void *impl_addr; \n\ 76341825Sdim }; \n\ 77341825Sdim \n\ 78341825Sdim struct __lldb_objc_class { \n\ 79341825Sdim void *isa; \n\ 80341825Sdim void *super_ptr; \n\ 81341825Sdim }; \n\ 82341825Sdim struct __lldb_objc_super { \n\ 83341825Sdim void *reciever; \n\ 84341825Sdim struct __lldb_objc_class *class_ptr; \n\ 85341825Sdim }; \n\ 86341825Sdim struct __lldb_msg_ref { \n\ 87341825Sdim void *dont_know; \n\ 88341825Sdim void *sel; \n\ 89341825Sdim }; \n\ 90341825Sdim \n\ 91341825Sdim struct __lldb_imp_return_struct return_struct; \n\ 92341825Sdim \n\ 93341825Sdim if (debug) \n\ 94341825Sdim printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \"\n\ 95341825Sdim \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 96341825Sdim object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed);\n\ 97341825Sdim if (is_super) \n\ 98341825Sdim { \n\ 99341825Sdim if (is_super2) \n\ 100341825Sdim { \n\ 101341825Sdim return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr;\n\ 102341825Sdim } \n\ 103341825Sdim else \n\ 104341825Sdim { \n\ 105341825Sdim return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr;\n\ 106341825Sdim } \n\ 107341825Sdim } \n\ 108341825Sdim else \n\ 109341825Sdim { \n\ 110341825Sdim // This code seems a little funny, but has its reasons... \n\ 111341825Sdim \n\ 112341825Sdim // The call to [object class] is here because if this is a \n\ 113341825Sdim // class, and has not been called into yet, we need to do \n\ 114341825Sdim // something to force the class to initialize itself. \n\ 115341825Sdim // Then the call to object_getClass will actually return the \n\ 116341825Sdim // correct class, either the class if object is a class \n\ 117341825Sdim // instance, or the meta-class if it is a class pointer. \n\ 118341825Sdim void *class_ptr = (void *) [(id) object class]; \n\ 119341825Sdim return_struct.class_addr = (id) object_getClass((id) object); \n\ 120341825Sdim if (debug) \n\ 121341825Sdim { \n\ 122341825Sdim if (class_ptr == object) \n\ 123341825Sdim { \n\ 124341825Sdim printf (\"Found a class object, need to use the meta class %p -> %p\\n\",\n\ 125341825Sdim class_ptr, return_struct.class_addr); \n\ 126341825Sdim } \n\ 127341825Sdim else \n\ 128341825Sdim { \n\ 129341825Sdim printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 130341825Sdim class_ptr, return_struct.class_addr); \n\ 131341825Sdim } \n\ 132341825Sdim } \n\ 133341825Sdim } \n\ 134341825Sdim \n\ 135341825Sdim if (is_fixup) \n\ 136341825Sdim { \n\ 137341825Sdim if (is_fixed) \n\ 138341825Sdim { \n\ 139341825Sdim return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 140341825Sdim } \n\ 141341825Sdim else \n\ 142341825Sdim { \n\ 143341825Sdim char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 144341825Sdim return_struct.sel_addr = sel_getUid (sel_name); \n\ 145341825Sdim if (debug) \n\ 146341825Sdim printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\",\n\ 147341825Sdim return_struct.sel_addr, sel_name); \n\ 148341825Sdim } \n\ 149341825Sdim } \n\ 150341825Sdim else \n\ 151341825Sdim { \n\ 152341825Sdim return_struct.sel_addr = sel; \n\ 153341825Sdim } \n\ 154341825Sdim \n\ 155341825Sdim if (is_stret) \n\ 156341825Sdim { \n\ 157341825Sdim return_struct.impl_addr = \n\ 158341825Sdim class_getMethodImplementation_stret (return_struct.class_addr, \n\ 159341825Sdim return_struct.sel_addr); \n\ 160341825Sdim } \n\ 161341825Sdim else \n\ 162341825Sdim { \n\ 163341825Sdim return_struct.impl_addr = \n\ 164341825Sdim class_getMethodImplementation (return_struct.class_addr, \n\ 165341825Sdim return_struct.sel_addr); \n\ 166341825Sdim } \n\ 167341825Sdim if (debug) \n\ 168341825Sdim printf (\"\\n*** Returning implementation: %p.\\n\", \n\ 169341825Sdim return_struct.impl_addr); \n\ 170341825Sdim \n\ 171341825Sdim return return_struct.impl_addr; \n\ 172341825Sdim} \n\ 173293116Semaste"; 174314564Sdimconst char * 175314564Sdim AppleObjCTrampolineHandler::g_lookup_implementation_no_stret_function_code = 176314564Sdim " \n\ 177341825Sdimextern \"C\" \n\ 178341825Sdim{ \n\ 179341825Sdim extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 180341825Sdim extern void * object_getClass (id object); \n\ 181341825Sdim extern void * sel_getUid(char *name); \n\ 182341825Sdim extern int printf(const char *format, ...); \n\ 183341825Sdim} \n\ 184293116Semasteextern \"C\" void * __lldb_objc_find_implementation_for_selector (void *object, \n\ 185341825Sdim void *sel, \n\ 186341825Sdim int is_stret, \n\ 187341825Sdim int is_super, \n\ 188341825Sdim int is_super2, \n\ 189341825Sdim int is_fixup, \n\ 190341825Sdim int is_fixed, \n\ 191341825Sdim int debug) \n\ 192341825Sdim{ \n\ 193341825Sdim struct __lldb_imp_return_struct \n\ 194341825Sdim { \n\ 195341825Sdim void *class_addr; \n\ 196341825Sdim void *sel_addr; \n\ 197341825Sdim void *impl_addr; \n\ 198341825Sdim }; \n\ 199341825Sdim \n\ 200341825Sdim struct __lldb_objc_class { \n\ 201341825Sdim void *isa; \n\ 202341825Sdim void *super_ptr; \n\ 203341825Sdim }; \n\ 204341825Sdim struct __lldb_objc_super { \n\ 205341825Sdim void *reciever; \n\ 206341825Sdim struct __lldb_objc_class *class_ptr; \n\ 207341825Sdim }; \n\ 208341825Sdim struct __lldb_msg_ref { \n\ 209341825Sdim void *dont_know; \n\ 210341825Sdim void *sel; \n\ 211341825Sdim }; \n\ 212341825Sdim \n\ 213341825Sdim struct __lldb_imp_return_struct return_struct; \n\ 214341825Sdim \n\ 215341825Sdim if (debug) \n\ 216293116Semaste printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \" \n\ 217341825Sdim \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 218293116Semaste object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed); \n\ 219341825Sdim if (is_super) \n\ 220341825Sdim { \n\ 221341825Sdim if (is_super2) \n\ 222341825Sdim { \n\ 223293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr; \n\ 224341825Sdim } \n\ 225341825Sdim else \n\ 226341825Sdim { \n\ 227293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; \n\ 228341825Sdim } \n\ 229341825Sdim } \n\ 230341825Sdim else \n\ 231341825Sdim { \n\ 232341825Sdim // This code seems a little funny, but has its reasons... \n\ 233293116Semaste // The call to [object class] is here because if this is a class, and has not been called into \n\ 234293116Semaste // yet, we need to do something to force the class to initialize itself. \n\ 235293116Semaste // Then the call to object_getClass will actually return the correct class, either the class \n\ 236293116Semaste // if object is a class instance, or the meta-class if it is a class pointer. \n\ 237341825Sdim void *class_ptr = (void *) [(id) object class]; \n\ 238341825Sdim return_struct.class_addr = (id) object_getClass((id) object); \n\ 239341825Sdim if (debug) \n\ 240341825Sdim { \n\ 241341825Sdim if (class_ptr == object) \n\ 242341825Sdim { \n\ 243293116Semaste printf (\"Found a class object, need to return the meta class %p -> %p\\n\", \n\ 244341825Sdim class_ptr, return_struct.class_addr); \n\ 245341825Sdim } \n\ 246341825Sdim else \n\ 247341825Sdim { \n\ 248293116Semaste printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 249341825Sdim class_ptr, return_struct.class_addr); \n\ 250341825Sdim } \n\ 251341825Sdim } \n\ 252341825Sdim } \n\ 253341825Sdim \n\ 254341825Sdim if (is_fixup) \n\ 255341825Sdim { \n\ 256341825Sdim if (is_fixed) \n\ 257341825Sdim { \n\ 258341825Sdim return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 259341825Sdim } \n\ 260341825Sdim else \n\ 261341825Sdim { \n\ 262341825Sdim char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 263341825Sdim return_struct.sel_addr = sel_getUid (sel_name); \n\ 264341825Sdim if (debug) \n\ 265341825Sdim printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\",\n\ 266341825Sdim return_struct.sel_addr, sel_name); \n\ 267341825Sdim } \n\ 268341825Sdim } \n\ 269341825Sdim else \n\ 270341825Sdim { \n\ 271341825Sdim return_struct.sel_addr = sel; \n\ 272341825Sdim } \n\ 273341825Sdim \n\ 274341825Sdim return_struct.impl_addr = \n\ 275341825Sdim class_getMethodImplementation (return_struct.class_addr, \n\ 276341825Sdim return_struct.sel_addr); \n\ 277341825Sdim if (debug) \n\ 278341825Sdim printf (\"\\n*** Returning implementation: 0x%p.\\n\", \n\ 279341825Sdim return_struct.impl_addr); \n\ 280341825Sdim \n\ 281341825Sdim return return_struct.impl_addr; \n\ 282341825Sdim} \n\ 283293116Semaste"; 284293116Semaste 285314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::VTableRegion( 286314564Sdim AppleObjCVTables *owner, lldb::addr_t header_addr) 287314564Sdim : m_valid(true), m_owner(owner), m_header_addr(header_addr), 288314564Sdim m_code_start_addr(0), m_code_end_addr(0), m_next_region(0) { 289314564Sdim SetUpRegion(); 290293116Semaste} 291293116Semaste 292314564SdimAppleObjCTrampolineHandler::~AppleObjCTrampolineHandler() {} 293293116Semaste 294314564Sdimvoid AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::SetUpRegion() { 295314564Sdim // The header looks like: 296314564Sdim // 297314564Sdim // uint16_t headerSize 298314564Sdim // uint16_t descSize 299314564Sdim // uint32_t descCount 300314564Sdim // void * next 301314564Sdim // 302314564Sdim // First read in the header: 303293116Semaste 304314564Sdim char memory_buffer[16]; 305314564Sdim ProcessSP process_sp = m_owner->GetProcessSP(); 306314564Sdim if (!process_sp) 307314564Sdim return; 308314564Sdim DataExtractor data(memory_buffer, sizeof(memory_buffer), 309314564Sdim process_sp->GetByteOrder(), 310314564Sdim process_sp->GetAddressByteSize()); 311314564Sdim size_t actual_size = 8 + process_sp->GetAddressByteSize(); 312321369Sdim Status error; 313314564Sdim size_t bytes_read = 314314564Sdim process_sp->ReadMemory(m_header_addr, memory_buffer, actual_size, error); 315314564Sdim if (bytes_read != actual_size) { 316314564Sdim m_valid = false; 317314564Sdim return; 318314564Sdim } 319293116Semaste 320314564Sdim lldb::offset_t offset = 0; 321314564Sdim const uint16_t header_size = data.GetU16(&offset); 322314564Sdim const uint16_t descriptor_size = data.GetU16(&offset); 323314564Sdim const size_t num_descriptors = data.GetU32(&offset); 324293116Semaste 325314564Sdim m_next_region = data.GetPointer(&offset); 326314564Sdim 327314564Sdim // If the header size is 0, that means we've come in too early before this 328314564Sdim // data is set up. 329314564Sdim // Set ourselves as not valid, and continue. 330314564Sdim if (header_size == 0 || num_descriptors == 0) { 331314564Sdim m_valid = false; 332314564Sdim return; 333314564Sdim } 334314564Sdim 335314564Sdim // Now read in all the descriptors: 336314564Sdim // The descriptor looks like: 337314564Sdim // 338314564Sdim // uint32_t offset 339314564Sdim // uint32_t flags 340314564Sdim // 341341825Sdim // Where offset is either 0 - in which case it is unused, or it is 342341825Sdim // the offset of the vtable code from the beginning of the 343341825Sdim // descriptor record. Below, we'll convert that into an absolute 344341825Sdim // code address, since I don't want to have to compute it over and 345341825Sdim // over. 346314564Sdim 347314564Sdim // Ingest the whole descriptor array: 348314564Sdim const lldb::addr_t desc_ptr = m_header_addr + header_size; 349314564Sdim const size_t desc_array_size = num_descriptors * descriptor_size; 350314564Sdim DataBufferSP data_sp(new DataBufferHeap(desc_array_size, '\0')); 351314564Sdim uint8_t *dst = (uint8_t *)data_sp->GetBytes(); 352314564Sdim 353314564Sdim DataExtractor desc_extractor(dst, desc_array_size, process_sp->GetByteOrder(), 354314564Sdim process_sp->GetAddressByteSize()); 355314564Sdim bytes_read = process_sp->ReadMemory(desc_ptr, dst, desc_array_size, error); 356314564Sdim if (bytes_read != desc_array_size) { 357314564Sdim m_valid = false; 358314564Sdim return; 359314564Sdim } 360314564Sdim 361314564Sdim // The actual code for the vtables will be laid out consecutively, so I also 362314564Sdim // compute the start and end of the whole code block. 363314564Sdim 364314564Sdim offset = 0; 365314564Sdim m_code_start_addr = 0; 366314564Sdim m_code_end_addr = 0; 367314564Sdim 368314564Sdim for (size_t i = 0; i < num_descriptors; i++) { 369314564Sdim lldb::addr_t start_offset = offset; 370314564Sdim uint32_t voffset = desc_extractor.GetU32(&offset); 371314564Sdim uint32_t flags = desc_extractor.GetU32(&offset); 372314564Sdim lldb::addr_t code_addr = desc_ptr + start_offset + voffset; 373314564Sdim m_descriptors.push_back(VTableDescriptor(flags, code_addr)); 374314564Sdim 375314564Sdim if (m_code_start_addr == 0 || code_addr < m_code_start_addr) 376314564Sdim m_code_start_addr = code_addr; 377314564Sdim if (code_addr > m_code_end_addr) 378314564Sdim m_code_end_addr = code_addr; 379314564Sdim 380314564Sdim offset = start_offset + descriptor_size; 381314564Sdim } 382341825Sdim // Finally, a little bird told me that all the vtable code blocks 383341825Sdim // are the same size. Let's compute the blocks and if they are all 384341825Sdim // the same add the size to the code end address: 385314564Sdim lldb::addr_t code_size = 0; 386314564Sdim bool all_the_same = true; 387314564Sdim for (size_t i = 0; i < num_descriptors - 1; i++) { 388314564Sdim lldb::addr_t this_size = 389314564Sdim m_descriptors[i + 1].code_start - m_descriptors[i].code_start; 390314564Sdim if (code_size == 0) 391314564Sdim code_size = this_size; 392314564Sdim else { 393314564Sdim if (this_size != code_size) 394314564Sdim all_the_same = false; 395314564Sdim if (this_size > code_size) 396314564Sdim code_size = this_size; 397293116Semaste } 398314564Sdim } 399314564Sdim if (all_the_same) 400314564Sdim m_code_end_addr += code_size; 401293116Semaste} 402293116Semaste 403314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion:: 404314564Sdim AddressInRegion(lldb::addr_t addr, uint32_t &flags) { 405314564Sdim if (!IsValid()) 406293116Semaste return false; 407293116Semaste 408314564Sdim if (addr < m_code_start_addr || addr > m_code_end_addr) 409314564Sdim return false; 410314564Sdim 411314564Sdim std::vector<VTableDescriptor>::iterator pos, end = m_descriptors.end(); 412314564Sdim for (pos = m_descriptors.begin(); pos != end; pos++) { 413314564Sdim if (addr <= (*pos).code_start) { 414314564Sdim flags = (*pos).flags; 415314564Sdim return true; 416293116Semaste } 417314564Sdim } 418314564Sdim return false; 419293116Semaste} 420314564Sdim 421314564Sdimvoid AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::Dump( 422314564Sdim Stream &s) { 423314564Sdim s.Printf("Header addr: 0x%" PRIx64 " Code start: 0x%" PRIx64 424314564Sdim " Code End: 0x%" PRIx64 " Next: 0x%" PRIx64 "\n", 425314564Sdim m_header_addr, m_code_start_addr, m_code_end_addr, m_next_region); 426314564Sdim size_t num_elements = m_descriptors.size(); 427314564Sdim for (size_t i = 0; i < num_elements; i++) { 428314564Sdim s.Indent(); 429314564Sdim s.Printf("Code start: 0x%" PRIx64 " Flags: %d\n", 430314564Sdim m_descriptors[i].code_start, m_descriptors[i].flags); 431314564Sdim } 432293116Semaste} 433293116Semaste 434314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::AppleObjCVTables( 435314564Sdim const ProcessSP &process_sp, const ModuleSP &objc_module_sp) 436314564Sdim : m_process_wp(), m_trampoline_header(LLDB_INVALID_ADDRESS), 437314564Sdim m_trampolines_changed_bp_id(LLDB_INVALID_BREAK_ID), 438314564Sdim m_objc_module_sp(objc_module_sp) { 439314564Sdim if (process_sp) 440314564Sdim m_process_wp = process_sp; 441293116Semaste} 442293116Semaste 443314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::~AppleObjCVTables() { 444314564Sdim ProcessSP process_sp = GetProcessSP(); 445314564Sdim if (process_sp) { 446314564Sdim if (m_trampolines_changed_bp_id != LLDB_INVALID_BREAK_ID) 447314564Sdim process_sp->GetTarget().RemoveBreakpointByID(m_trampolines_changed_bp_id); 448314564Sdim } 449314564Sdim} 450293116Semaste 451314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::InitializeVTableSymbols() { 452314564Sdim if (m_trampoline_header != LLDB_INVALID_ADDRESS) 453314564Sdim return true; 454314564Sdim 455314564Sdim ProcessSP process_sp = GetProcessSP(); 456314564Sdim if (process_sp) { 457314564Sdim Target &target = process_sp->GetTarget(); 458314564Sdim 459314564Sdim const ModuleList &target_modules = target.GetImages(); 460314564Sdim std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 461314564Sdim size_t num_modules = target_modules.GetSize(); 462314564Sdim if (!m_objc_module_sp) { 463314564Sdim for (size_t i = 0; i < num_modules; i++) { 464314564Sdim if (process_sp->GetObjCLanguageRuntime()->IsModuleObjCLibrary( 465314564Sdim target_modules.GetModuleAtIndexUnlocked(i))) { 466314564Sdim m_objc_module_sp = target_modules.GetModuleAtIndexUnlocked(i); 467314564Sdim break; 468293116Semaste } 469314564Sdim } 470314564Sdim } 471314564Sdim 472314564Sdim if (m_objc_module_sp) { 473314564Sdim ConstString trampoline_name("gdb_objc_trampolines"); 474314564Sdim const Symbol *trampoline_symbol = 475314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(trampoline_name, 476314564Sdim eSymbolTypeData); 477314564Sdim if (trampoline_symbol != NULL) { 478314564Sdim m_trampoline_header = trampoline_symbol->GetLoadAddress(&target); 479314564Sdim if (m_trampoline_header == LLDB_INVALID_ADDRESS) 480314564Sdim return false; 481314564Sdim 482314564Sdim // Next look up the "changed" symbol and set a breakpoint on that... 483314564Sdim ConstString changed_name("gdb_objc_trampolines_changed"); 484314564Sdim const Symbol *changed_symbol = 485314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(changed_name, 486314564Sdim eSymbolTypeCode); 487314564Sdim if (changed_symbol != NULL) { 488314564Sdim const Address changed_symbol_addr = changed_symbol->GetAddress(); 489314564Sdim if (!changed_symbol_addr.IsValid()) 490314564Sdim return false; 491314564Sdim 492314564Sdim lldb::addr_t changed_addr = 493314564Sdim changed_symbol_addr.GetOpcodeLoadAddress(&target); 494314564Sdim if (changed_addr != LLDB_INVALID_ADDRESS) { 495314564Sdim BreakpointSP trampolines_changed_bp_sp = 496314564Sdim target.CreateBreakpoint(changed_addr, true, false); 497314564Sdim if (trampolines_changed_bp_sp) { 498314564Sdim m_trampolines_changed_bp_id = trampolines_changed_bp_sp->GetID(); 499314564Sdim trampolines_changed_bp_sp->SetCallback(RefreshTrampolines, this, 500314564Sdim true); 501314564Sdim trampolines_changed_bp_sp->SetBreakpointKind( 502314564Sdim "objc-trampolines-changed"); 503314564Sdim return true; 504293116Semaste } 505314564Sdim } 506293116Semaste } 507314564Sdim } 508293116Semaste } 509314564Sdim } 510314564Sdim return false; 511293116Semaste} 512293116Semaste 513314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::RefreshTrampolines( 514314564Sdim void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, 515314564Sdim lldb::user_id_t break_loc_id) { 516314564Sdim AppleObjCVTables *vtable_handler = (AppleObjCVTables *)baton; 517314564Sdim if (vtable_handler->InitializeVTableSymbols()) { 518314564Sdim // The Update function is called with the address of an added region. So we 519314564Sdim // grab that address, and 520314564Sdim // feed it into ReadRegions. Of course, our friend the ABI will get the 521314564Sdim // values for us. 522314564Sdim ExecutionContext exe_ctx(context->exe_ctx_ref); 523314564Sdim Process *process = exe_ctx.GetProcessPtr(); 524314564Sdim const ABI *abi = process->GetABI().get(); 525314564Sdim 526314564Sdim ClangASTContext *clang_ast_context = 527314564Sdim process->GetTarget().GetScratchClangASTContext(); 528314564Sdim ValueList argument_values; 529314564Sdim Value input_value; 530314564Sdim CompilerType clang_void_ptr_type = 531314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 532314564Sdim 533314564Sdim input_value.SetValueType(Value::eValueTypeScalar); 534314564Sdim // input_value.SetContext (Value::eContextTypeClangType, 535314564Sdim // clang_void_ptr_type); 536314564Sdim input_value.SetCompilerType(clang_void_ptr_type); 537314564Sdim argument_values.PushValue(input_value); 538314564Sdim 539314564Sdim bool success = 540314564Sdim abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values); 541314564Sdim if (!success) 542314564Sdim return false; 543314564Sdim 544314564Sdim // Now get a pointer value from the zeroth argument. 545321369Sdim Status error; 546314564Sdim DataExtractor data; 547314564Sdim error = argument_values.GetValueAtIndex(0)->GetValueAsData(&exe_ctx, data, 548314564Sdim 0, NULL); 549314564Sdim lldb::offset_t offset = 0; 550314564Sdim lldb::addr_t region_addr = data.GetPointer(&offset); 551314564Sdim 552314564Sdim if (region_addr != 0) 553314564Sdim vtable_handler->ReadRegions(region_addr); 554314564Sdim } 555314564Sdim return false; 556293116Semaste} 557293116Semaste 558314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions() { 559341825Sdim // The no argument version reads the start region from the value of 560341825Sdim // the gdb_regions_header, and gets started from there. 561314564Sdim 562314564Sdim m_regions.clear(); 563314564Sdim if (!InitializeVTableSymbols()) 564293116Semaste return false; 565321369Sdim Status error; 566314564Sdim ProcessSP process_sp = GetProcessSP(); 567314564Sdim if (process_sp) { 568314564Sdim lldb::addr_t region_addr = 569314564Sdim process_sp->ReadPointerFromMemory(m_trampoline_header, error); 570314564Sdim if (error.Success()) 571314564Sdim return ReadRegions(region_addr); 572314564Sdim } 573314564Sdim return false; 574293116Semaste} 575293116Semaste 576314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions( 577314564Sdim lldb::addr_t region_addr) { 578314564Sdim ProcessSP process_sp = GetProcessSP(); 579314564Sdim if (!process_sp) 580314564Sdim return false; 581314564Sdim 582314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 583314564Sdim 584314564Sdim // We aren't starting at the trampoline symbol. 585314564Sdim InitializeVTableSymbols(); 586314564Sdim lldb::addr_t next_region = region_addr; 587314564Sdim 588314564Sdim // Read in the sizes of the headers. 589314564Sdim while (next_region != 0) { 590314564Sdim m_regions.push_back(VTableRegion(this, next_region)); 591314564Sdim if (!m_regions.back().IsValid()) { 592314564Sdim m_regions.clear(); 593314564Sdim return false; 594293116Semaste } 595314564Sdim if (log) { 596314564Sdim StreamString s; 597314564Sdim m_regions.back().Dump(s); 598314564Sdim log->Printf("Read vtable region: \n%s", s.GetData()); 599293116Semaste } 600314564Sdim 601314564Sdim next_region = m_regions.back().GetNextRegionAddr(); 602314564Sdim } 603314564Sdim 604314564Sdim return true; 605293116Semaste} 606293116Semaste 607314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::IsAddressInVTables( 608314564Sdim lldb::addr_t addr, uint32_t &flags) { 609314564Sdim region_collection::iterator pos, end = m_regions.end(); 610314564Sdim for (pos = m_regions.begin(); pos != end; pos++) { 611314564Sdim if ((*pos).AddressInRegion(addr, flags)) 612314564Sdim return true; 613314564Sdim } 614314564Sdim return false; 615314564Sdim} 616314564Sdim 617293116Semasteconst AppleObjCTrampolineHandler::DispatchFunction 618314564Sdim AppleObjCTrampolineHandler::g_dispatch_functions[] = { 619314564Sdim // NAME STRET SUPER SUPER2 FIXUP TYPE 620314564Sdim {"objc_msgSend", false, false, false, DispatchFunction::eFixUpNone}, 621314564Sdim {"objc_msgSend_fixup", false, false, false, 622314564Sdim DispatchFunction::eFixUpToFix}, 623314564Sdim {"objc_msgSend_fixedup", false, false, false, 624314564Sdim DispatchFunction::eFixUpFixed}, 625314564Sdim {"objc_msgSend_stret", true, false, false, 626314564Sdim DispatchFunction::eFixUpNone}, 627314564Sdim {"objc_msgSend_stret_fixup", true, false, false, 628314564Sdim DispatchFunction::eFixUpToFix}, 629314564Sdim {"objc_msgSend_stret_fixedup", true, false, false, 630314564Sdim DispatchFunction::eFixUpFixed}, 631314564Sdim {"objc_msgSend_fpret", false, false, false, 632314564Sdim DispatchFunction::eFixUpNone}, 633314564Sdim {"objc_msgSend_fpret_fixup", false, false, false, 634314564Sdim DispatchFunction::eFixUpToFix}, 635314564Sdim {"objc_msgSend_fpret_fixedup", false, false, false, 636314564Sdim DispatchFunction::eFixUpFixed}, 637314564Sdim {"objc_msgSend_fp2ret", false, false, true, 638314564Sdim DispatchFunction::eFixUpNone}, 639314564Sdim {"objc_msgSend_fp2ret_fixup", false, false, true, 640314564Sdim DispatchFunction::eFixUpToFix}, 641314564Sdim {"objc_msgSend_fp2ret_fixedup", false, false, true, 642314564Sdim DispatchFunction::eFixUpFixed}, 643314564Sdim {"objc_msgSendSuper", false, true, false, DispatchFunction::eFixUpNone}, 644314564Sdim {"objc_msgSendSuper_stret", true, true, false, 645314564Sdim DispatchFunction::eFixUpNone}, 646314564Sdim {"objc_msgSendSuper2", false, true, true, DispatchFunction::eFixUpNone}, 647314564Sdim {"objc_msgSendSuper2_fixup", false, true, true, 648314564Sdim DispatchFunction::eFixUpToFix}, 649314564Sdim {"objc_msgSendSuper2_fixedup", false, true, true, 650314564Sdim DispatchFunction::eFixUpFixed}, 651314564Sdim {"objc_msgSendSuper2_stret", true, true, true, 652314564Sdim DispatchFunction::eFixUpNone}, 653314564Sdim {"objc_msgSendSuper2_stret_fixup", true, true, true, 654314564Sdim DispatchFunction::eFixUpToFix}, 655314564Sdim {"objc_msgSendSuper2_stret_fixedup", true, true, true, 656314564Sdim DispatchFunction::eFixUpFixed}, 657293116Semaste}; 658293116Semaste 659314564SdimAppleObjCTrampolineHandler::AppleObjCTrampolineHandler( 660314564Sdim const ProcessSP &process_sp, const ModuleSP &objc_module_sp) 661314564Sdim : m_process_wp(), m_objc_module_sp(objc_module_sp), 662314564Sdim m_lookup_implementation_function_code(nullptr), 663314564Sdim m_impl_fn_addr(LLDB_INVALID_ADDRESS), 664314564Sdim m_impl_stret_fn_addr(LLDB_INVALID_ADDRESS), 665314564Sdim m_msg_forward_addr(LLDB_INVALID_ADDRESS) { 666314564Sdim if (process_sp) 667314564Sdim m_process_wp = process_sp; 668314564Sdim // Look up the known resolution functions: 669314564Sdim 670314564Sdim ConstString get_impl_name("class_getMethodImplementation"); 671314564Sdim ConstString get_impl_stret_name("class_getMethodImplementation_stret"); 672314564Sdim ConstString msg_forward_name("_objc_msgForward"); 673314564Sdim ConstString msg_forward_stret_name("_objc_msgForward_stret"); 674314564Sdim 675314564Sdim Target *target = process_sp ? &process_sp->GetTarget() : NULL; 676314564Sdim const Symbol *class_getMethodImplementation = 677314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_name, 678314564Sdim eSymbolTypeCode); 679314564Sdim const Symbol *class_getMethodImplementation_stret = 680314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_stret_name, 681314564Sdim eSymbolTypeCode); 682314564Sdim const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType( 683314564Sdim msg_forward_name, eSymbolTypeCode); 684314564Sdim const Symbol *msg_forward_stret = 685314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(msg_forward_stret_name, 686314564Sdim eSymbolTypeCode); 687314564Sdim 688314564Sdim if (class_getMethodImplementation) 689314564Sdim m_impl_fn_addr = 690314564Sdim class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress( 691314564Sdim target); 692314564Sdim if (class_getMethodImplementation_stret) 693314564Sdim m_impl_stret_fn_addr = 694314564Sdim class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress( 695314564Sdim target); 696314564Sdim if (msg_forward) 697314564Sdim m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target); 698314564Sdim if (msg_forward_stret) 699314564Sdim m_msg_forward_stret_addr = 700314564Sdim msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target); 701314564Sdim 702314564Sdim // FIXME: Do some kind of logging here. 703314564Sdim if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) { 704314564Sdim // If we can't even find the ordinary get method implementation function, 705314564Sdim // then we aren't going to be able to 706314564Sdim // step through any method dispatches. Warn to that effect and get out of 707314564Sdim // here. 708314564Sdim if (process_sp->CanJIT()) { 709314564Sdim process_sp->GetTarget().GetDebugger().GetErrorFile()->Printf( 710314564Sdim "Could not find implementation lookup function \"%s\"" 711314564Sdim " step in through ObjC method dispatch will not work.\n", 712314564Sdim get_impl_name.AsCString()); 713293116Semaste } 714314564Sdim return; 715314564Sdim } else if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) { 716314564Sdim // It there is no stret return lookup function, assume that it is the same 717314564Sdim // as the straight lookup: 718314564Sdim m_impl_stret_fn_addr = m_impl_fn_addr; 719314564Sdim // Also we will use the version of the lookup code that doesn't rely on the 720314564Sdim // stret version of the function. 721314564Sdim m_lookup_implementation_function_code = 722314564Sdim g_lookup_implementation_no_stret_function_code; 723314564Sdim } else { 724314564Sdim m_lookup_implementation_function_code = 725314564Sdim g_lookup_implementation_with_stret_function_code; 726314564Sdim } 727314564Sdim 728341825Sdim // Look up the addresses for the objc dispatch functions and cache 729341825Sdim // them. For now I'm inspecting the symbol names dynamically to 730341825Sdim // figure out how to dispatch to them. If it becomes more 731341825Sdim // complicated than this we can turn the g_dispatch_functions char * 732341825Sdim // array into a template table, and populate the DispatchFunction 733341825Sdim // map from there. 734314564Sdim 735314564Sdim for (size_t i = 0; i != llvm::array_lengthof(g_dispatch_functions); i++) { 736314564Sdim ConstString name_const_str(g_dispatch_functions[i].name); 737314564Sdim const Symbol *msgSend_symbol = 738314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str, 739314564Sdim eSymbolTypeCode); 740314564Sdim if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) { 741341825Sdim // FIXME: Make g_dispatch_functions static table of 742341825Sdim // DispatchFunctions, and have the map be address->index. 743341825Sdim // Problem is we also need to lookup the dispatch function. For 744341825Sdim // now we could have a side table of stret & non-stret dispatch 745341825Sdim // functions. If that's as complex as it gets, we're fine. 746314564Sdim 747314564Sdim lldb::addr_t sym_addr = 748314564Sdim msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); 749314564Sdim 750314564Sdim m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); 751293116Semaste } 752314564Sdim } 753293116Semaste 754314564Sdim // Build our vtable dispatch handler here: 755314564Sdim m_vtables_ap.reset(new AppleObjCVTables(process_sp, m_objc_module_sp)); 756314564Sdim if (m_vtables_ap.get()) 757314564Sdim m_vtables_ap->ReadRegions(); 758293116Semaste} 759293116Semaste 760293116Semastelldb::addr_t 761314564SdimAppleObjCTrampolineHandler::SetupDispatchFunction(Thread &thread, 762314564Sdim ValueList &dispatch_values) { 763314564Sdim ThreadSP thread_sp(thread.shared_from_this()); 764314564Sdim ExecutionContext exe_ctx(thread_sp); 765314564Sdim DiagnosticManager diagnostics; 766314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 767309124Sdim 768314564Sdim lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; 769314564Sdim FunctionCaller *impl_function_caller = nullptr; 770293116Semaste 771314564Sdim // Scope for mutex locker: 772314564Sdim { 773314564Sdim std::lock_guard<std::mutex> guard(m_impl_function_mutex); 774309124Sdim 775314564Sdim // First stage is to make the ClangUtility to hold our injected function: 776293116Semaste 777314564Sdim if (!m_impl_code.get()) { 778314564Sdim if (m_lookup_implementation_function_code != NULL) { 779321369Sdim Status error; 780314564Sdim m_impl_code.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage( 781314564Sdim m_lookup_implementation_function_code, eLanguageTypeObjC, 782314564Sdim g_lookup_implementation_function_name, error)); 783314564Sdim if (error.Fail()) { 784314564Sdim if (log) 785314564Sdim log->Printf( 786314564Sdim "Failed to get Utility Function for implementation lookup: %s.", 787314564Sdim error.AsCString()); 788314564Sdim m_impl_code.reset(); 789314564Sdim return args_addr; 790314564Sdim } 791309124Sdim 792314564Sdim if (!m_impl_code->Install(diagnostics, exe_ctx)) { 793314564Sdim if (log) { 794314564Sdim log->Printf("Failed to install implementation lookup."); 795314564Sdim diagnostics.Dump(log); 796314564Sdim } 797314564Sdim m_impl_code.reset(); 798314564Sdim return args_addr; 799293116Semaste } 800314564Sdim } else { 801314564Sdim if (log) 802314564Sdim log->Printf("No method lookup implementation code."); 803314564Sdim return LLDB_INVALID_ADDRESS; 804314564Sdim } 805309124Sdim 806314564Sdim // Next make the runner function for our implementation utility function. 807314564Sdim ClangASTContext *clang_ast_context = 808314564Sdim thread.GetProcess()->GetTarget().GetScratchClangASTContext(); 809314564Sdim CompilerType clang_void_ptr_type = 810314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 811321369Sdim Status error; 812309124Sdim 813314564Sdim impl_function_caller = m_impl_code->MakeFunctionCaller( 814314564Sdim clang_void_ptr_type, dispatch_values, thread_sp, error); 815314564Sdim if (error.Fail()) { 816293116Semaste if (log) 817314564Sdim log->Printf( 818314564Sdim "Error getting function caller for dispatch lookup: \"%s\".", 819314564Sdim error.AsCString()); 820293116Semaste return args_addr; 821314564Sdim } 822314564Sdim } else { 823314564Sdim impl_function_caller = m_impl_code->GetFunctionCaller(); 824293116Semaste } 825314564Sdim } 826309124Sdim 827314564Sdim diagnostics.Clear(); 828314564Sdim 829341825Sdim // Now write down the argument values for this particular call. 830341825Sdim // This looks like it might be a race condition if other threads 831341825Sdim // were calling into here, but actually it isn't because we allocate 832341825Sdim // a new args structure for this call by passing args_addr = 833341825Sdim // LLDB_INVALID_ADDRESS... 834314564Sdim 835314564Sdim if (!impl_function_caller->WriteFunctionArguments( 836314564Sdim exe_ctx, args_addr, dispatch_values, diagnostics)) { 837314564Sdim if (log) { 838314564Sdim log->Printf("Error writing function arguments."); 839314564Sdim diagnostics.Dump(log); 840314564Sdim } 841293116Semaste return args_addr; 842314564Sdim } 843314564Sdim 844314564Sdim return args_addr; 845293116Semaste} 846293116Semaste 847293116SemasteThreadPlanSP 848314564SdimAppleObjCTrampolineHandler::GetStepThroughDispatchPlan(Thread &thread, 849314564Sdim bool stop_others) { 850314564Sdim ThreadPlanSP ret_plan_sp; 851314564Sdim lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); 852314564Sdim 853314564Sdim DispatchFunction this_dispatch; 854314564Sdim bool found_it = false; 855314564Sdim 856341825Sdim // First step is to look and see if we are in one of the known ObjC 857341825Sdim // dispatch functions. We've already compiled a table of same, so 858341825Sdim // consult it. 859314564Sdim 860314564Sdim MsgsendMap::iterator pos; 861314564Sdim pos = m_msgSend_map.find(curr_pc); 862314564Sdim if (pos != m_msgSend_map.end()) { 863314564Sdim this_dispatch = g_dispatch_functions[(*pos).second]; 864314564Sdim found_it = true; 865314564Sdim } 866314564Sdim 867314564Sdim // Next check to see if we are in a vtable region: 868314564Sdim 869314564Sdim if (!found_it) { 870314564Sdim uint32_t flags; 871314564Sdim if (m_vtables_ap.get()) { 872314564Sdim found_it = m_vtables_ap->IsAddressInVTables(curr_pc, flags); 873314564Sdim if (found_it) { 874314564Sdim this_dispatch.name = "vtable"; 875314564Sdim this_dispatch.stret_return = 876314564Sdim (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) == 877314564Sdim AppleObjCVTables::eOBJC_TRAMPOLINE_STRET; 878314564Sdim this_dispatch.is_super = false; 879314564Sdim this_dispatch.is_super2 = false; 880314564Sdim this_dispatch.fixedup = DispatchFunction::eFixUpFixed; 881314564Sdim } 882293116Semaste } 883314564Sdim } 884314564Sdim 885314564Sdim if (found_it) { 886314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 887314564Sdim 888341825Sdim // We are decoding a method dispatch. First job is to pull the 889341825Sdim // arguments out: 890314564Sdim 891314564Sdim lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); 892314564Sdim 893314564Sdim const ABI *abi = NULL; 894314564Sdim ProcessSP process_sp(thread.CalculateProcess()); 895314564Sdim if (process_sp) 896314564Sdim abi = process_sp->GetABI().get(); 897314564Sdim if (abi == NULL) 898314564Sdim return ret_plan_sp; 899314564Sdim 900314564Sdim TargetSP target_sp(thread.CalculateTarget()); 901314564Sdim 902314564Sdim ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext(); 903314564Sdim ValueList argument_values; 904314564Sdim Value void_ptr_value; 905314564Sdim CompilerType clang_void_ptr_type = 906314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 907314564Sdim void_ptr_value.SetValueType(Value::eValueTypeScalar); 908314564Sdim // void_ptr_value.SetContext (Value::eContextTypeClangType, 909314564Sdim // clang_void_ptr_type); 910314564Sdim void_ptr_value.SetCompilerType(clang_void_ptr_type); 911314564Sdim 912314564Sdim int obj_index; 913314564Sdim int sel_index; 914314564Sdim 915341825Sdim // If this is a struct return dispatch, then the first argument is 916341825Sdim // the return struct pointer, and the object is the second, and 917341825Sdim // the selector is the third. Otherwise the object is the first 918341825Sdim // and the selector the second. 919314564Sdim if (this_dispatch.stret_return) { 920314564Sdim obj_index = 1; 921314564Sdim sel_index = 2; 922314564Sdim argument_values.PushValue(void_ptr_value); 923314564Sdim argument_values.PushValue(void_ptr_value); 924314564Sdim argument_values.PushValue(void_ptr_value); 925314564Sdim } else { 926314564Sdim obj_index = 0; 927314564Sdim sel_index = 1; 928314564Sdim argument_values.PushValue(void_ptr_value); 929314564Sdim argument_values.PushValue(void_ptr_value); 930293116Semaste } 931293116Semaste 932314564Sdim bool success = abi->GetArgumentValues(thread, argument_values); 933314564Sdim if (!success) 934314564Sdim return ret_plan_sp; 935293116Semaste 936314564Sdim lldb::addr_t obj_addr = 937314564Sdim argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong(); 938314564Sdim if (obj_addr == 0x0) { 939314564Sdim if (log) 940314564Sdim log->Printf( 941314564Sdim "Asked to step to dispatch to nil object, returning empty plan."); 942314564Sdim return ret_plan_sp; 943314564Sdim } 944314564Sdim 945314564Sdim ExecutionContext exe_ctx(thread.shared_from_this()); 946314564Sdim Process *process = exe_ctx.GetProcessPtr(); 947341825Sdim // isa_addr will store the class pointer that the method is being 948341825Sdim // dispatched to - so either the class directly or the super class 949341825Sdim // if this is one of the objc_msgSendSuper flavors. That's mostly 950341825Sdim // used to look up the class/selector pair in our cache. 951314564Sdim 952314564Sdim lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS; 953314564Sdim lldb::addr_t sel_addr = 954314564Sdim argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong(); 955314564Sdim 956341825Sdim // Figure out the class this is being dispatched to and see if 957341825Sdim // we've already cached this method call, If so we can push a 958341825Sdim // run-to-address plan directly. Otherwise we have to figure out 959341825Sdim // where the implementation lives. 960314564Sdim 961314564Sdim if (this_dispatch.is_super) { 962314564Sdim if (this_dispatch.is_super2) { 963341825Sdim // In the objc_msgSendSuper2 case, we don't get the object 964341825Sdim // directly, we get a structure containing the object and the 965341825Sdim // class to which the super message is being sent. So we need 966341825Sdim // to dig the super out of the class and use that. 967314564Sdim 968314564Sdim Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 969314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 970314564Sdim super_value.ResolveValue(&exe_ctx); 971314564Sdim 972314564Sdim if (super_value.GetScalar().IsValid()) { 973314564Sdim 974314564Sdim // isa_value now holds the class pointer. The second word of the 975314564Sdim // class pointer is the super-class pointer: 976314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 977314564Sdim super_value.ResolveValue(&exe_ctx); 978314564Sdim if (super_value.GetScalar().IsValid()) 979314564Sdim isa_addr = super_value.GetScalar().ULongLong(); 980314564Sdim else { 981293116Semaste if (log) 982314564Sdim log->Printf("Failed to extract the super class value from the " 983314564Sdim "class in objc_super."); 984314564Sdim } 985314564Sdim } else { 986314564Sdim if (log) 987314564Sdim log->Printf("Failed to extract the class value from objc_super."); 988293116Semaste } 989314564Sdim } else { 990341825Sdim // In the objc_msgSendSuper case, we don't get the object 991341825Sdim // directly, we get a two element structure containing the 992341825Sdim // object and the super class to which the super message is 993341825Sdim // being sent. So the class we want is the second element of 994341825Sdim // this structure. 995293116Semaste 996314564Sdim Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 997314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 998314564Sdim super_value.ResolveValue(&exe_ctx); 999314564Sdim 1000314564Sdim if (super_value.GetScalar().IsValid()) { 1001314564Sdim isa_addr = super_value.GetScalar().ULongLong(); 1002314564Sdim } else { 1003314564Sdim if (log) 1004314564Sdim log->Printf("Failed to extract the class value from objc_super."); 1005293116Semaste } 1006314564Sdim } 1007314564Sdim } else { 1008314564Sdim // In the direct dispatch case, the object->isa is the class pointer we 1009314564Sdim // want. 1010293116Semaste 1011314564Sdim // This is a little cheesy, but since object->isa is the first field, 1012314564Sdim // making the object value a load address value and resolving it will get 1013314564Sdim // the pointer sized data pointed to by that value... 1014293116Semaste 1015341825Sdim // Note, it isn't a fatal error not to be able to get the 1016341825Sdim // address from the object, since this might be a "tagged 1017341825Sdim // pointer" which isn't a real object, but rather some word 1018314564Sdim // length encoded dingus. 1019314564Sdim 1020314564Sdim Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); 1021314564Sdim 1022314564Sdim isa_value.SetValueType(Value::eValueTypeLoadAddress); 1023314564Sdim isa_value.ResolveValue(&exe_ctx); 1024314564Sdim if (isa_value.GetScalar().IsValid()) { 1025314564Sdim isa_addr = isa_value.GetScalar().ULongLong(); 1026314564Sdim } else { 1027314564Sdim if (log) 1028314564Sdim log->Printf("Failed to extract the isa value from object."); 1029314564Sdim } 1030293116Semaste } 1031314564Sdim 1032314564Sdim // Okay, we've got the address of the class for which we're resolving this, 1033314564Sdim // let's see if it's in our cache: 1034314564Sdim lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS; 1035314564Sdim 1036314564Sdim if (isa_addr != LLDB_INVALID_ADDRESS) { 1037314564Sdim if (log) { 1038314564Sdim log->Printf("Resolving call for class - 0x%" PRIx64 1039314564Sdim " and selector - 0x%" PRIx64, 1040314564Sdim isa_addr, sel_addr); 1041314564Sdim } 1042314564Sdim ObjCLanguageRuntime *objc_runtime = 1043314564Sdim thread.GetProcess()->GetObjCLanguageRuntime(); 1044314564Sdim assert(objc_runtime != NULL); 1045314564Sdim 1046314564Sdim impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sel_addr); 1047314564Sdim } 1048314564Sdim 1049314564Sdim if (impl_addr != LLDB_INVALID_ADDRESS) { 1050314564Sdim // Yup, it was in the cache, so we can run to that address directly. 1051314564Sdim 1052314564Sdim if (log) 1053314564Sdim log->Printf("Found implementation address in cache: 0x%" PRIx64, 1054314564Sdim impl_addr); 1055314564Sdim 1056314564Sdim ret_plan_sp.reset( 1057314564Sdim new ThreadPlanRunToAddress(thread, impl_addr, stop_others)); 1058314564Sdim } else { 1059314564Sdim // We haven't seen this class/selector pair yet. Look it up. 1060314564Sdim StreamString errors; 1061314564Sdim Address impl_code_address; 1062314564Sdim 1063314564Sdim ValueList dispatch_values; 1064314564Sdim 1065314564Sdim // We've will inject a little function in the target that takes the 1066314564Sdim // object, selector and some flags, 1067314564Sdim // and figures out the implementation. Looks like: 1068314564Sdim // void *__lldb_objc_find_implementation_for_selector (void *object, 1069314564Sdim // void *sel, 1070314564Sdim // int is_stret, 1071314564Sdim // int is_super, 1072314564Sdim // int is_super2, 1073314564Sdim // int is_fixup, 1074314564Sdim // int is_fixed, 1075314564Sdim // int debug) 1076314564Sdim // So set up the arguments for that call. 1077314564Sdim 1078314564Sdim dispatch_values.PushValue(*(argument_values.GetValueAtIndex(obj_index))); 1079314564Sdim dispatch_values.PushValue(*(argument_values.GetValueAtIndex(sel_index))); 1080314564Sdim 1081314564Sdim Value flag_value; 1082314564Sdim CompilerType clang_int_type = 1083314564Sdim clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( 1084314564Sdim lldb::eEncodingSint, 32); 1085314564Sdim flag_value.SetValueType(Value::eValueTypeScalar); 1086314564Sdim // flag_value.SetContext (Value::eContextTypeClangType, clang_int_type); 1087314564Sdim flag_value.SetCompilerType(clang_int_type); 1088314564Sdim 1089314564Sdim if (this_dispatch.stret_return) 1090314564Sdim flag_value.GetScalar() = 1; 1091314564Sdim else 1092314564Sdim flag_value.GetScalar() = 0; 1093314564Sdim dispatch_values.PushValue(flag_value); 1094314564Sdim 1095314564Sdim if (this_dispatch.is_super) 1096314564Sdim flag_value.GetScalar() = 1; 1097314564Sdim else 1098314564Sdim flag_value.GetScalar() = 0; 1099314564Sdim dispatch_values.PushValue(flag_value); 1100314564Sdim 1101314564Sdim if (this_dispatch.is_super2) 1102314564Sdim flag_value.GetScalar() = 1; 1103314564Sdim else 1104314564Sdim flag_value.GetScalar() = 0; 1105314564Sdim dispatch_values.PushValue(flag_value); 1106314564Sdim 1107314564Sdim switch (this_dispatch.fixedup) { 1108314564Sdim case DispatchFunction::eFixUpNone: 1109314564Sdim flag_value.GetScalar() = 0; 1110314564Sdim dispatch_values.PushValue(flag_value); 1111314564Sdim dispatch_values.PushValue(flag_value); 1112314564Sdim break; 1113314564Sdim case DispatchFunction::eFixUpFixed: 1114314564Sdim flag_value.GetScalar() = 1; 1115314564Sdim dispatch_values.PushValue(flag_value); 1116314564Sdim flag_value.GetScalar() = 1; 1117314564Sdim dispatch_values.PushValue(flag_value); 1118314564Sdim break; 1119314564Sdim case DispatchFunction::eFixUpToFix: 1120314564Sdim flag_value.GetScalar() = 1; 1121314564Sdim dispatch_values.PushValue(flag_value); 1122314564Sdim flag_value.GetScalar() = 0; 1123314564Sdim dispatch_values.PushValue(flag_value); 1124314564Sdim break; 1125314564Sdim } 1126314564Sdim if (log && log->GetVerbose()) 1127314564Sdim flag_value.GetScalar() = 1; 1128314564Sdim else 1129314564Sdim flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done. 1130314564Sdim dispatch_values.PushValue(flag_value); 1131314564Sdim 1132341825Sdim // The step through code might have to fill in the cache, so it 1133341825Sdim // is not safe to run only one thread. So we override the 1134341825Sdim // stop_others value passed in to us here: 1135314564Sdim const bool trampoline_stop_others = false; 1136314564Sdim ret_plan_sp.reset(new AppleThreadPlanStepThroughObjCTrampoline( 1137314564Sdim thread, this, dispatch_values, isa_addr, sel_addr, 1138314564Sdim trampoline_stop_others)); 1139314564Sdim if (log) { 1140314564Sdim StreamString s; 1141314564Sdim ret_plan_sp->GetDescription(&s, eDescriptionLevelFull); 1142314564Sdim log->Printf("Using ObjC step plan: %s.\n", s.GetData()); 1143314564Sdim } 1144314564Sdim } 1145314564Sdim } 1146314564Sdim 1147314564Sdim return ret_plan_sp; 1148293116Semaste} 1149293116Semaste 1150293116SemasteFunctionCaller * 1151314564SdimAppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller() { 1152314564Sdim return m_impl_code->GetFunctionCaller(); 1153293116Semaste} 1154