AppleObjCTrampolineHandler.cpp revision 314564
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/ConstString.h" 21293116Semaste#include "lldb/Core/Debugger.h" 22293116Semaste#include "lldb/Core/Log.h" 23293116Semaste#include "lldb/Core/Module.h" 24293116Semaste#include "lldb/Core/StreamFile.h" 25293116Semaste#include "lldb/Core/Value.h" 26309124Sdim#include "lldb/Expression/DiagnosticManager.h" 27309124Sdim#include "lldb/Expression/FunctionCaller.h" 28293116Semaste#include "lldb/Expression/UserExpression.h" 29293116Semaste#include "lldb/Expression/UtilityFunction.h" 30293116Semaste#include "lldb/Host/FileSpec.h" 31293116Semaste#include "lldb/Symbol/ClangASTContext.h" 32293116Semaste#include "lldb/Symbol/Symbol.h" 33293116Semaste#include "lldb/Target/ABI.h" 34309124Sdim#include "lldb/Target/ExecutionContext.h" 35293116Semaste#include "lldb/Target/ObjCLanguageRuntime.h" 36293116Semaste#include "lldb/Target/Process.h" 37293116Semaste#include "lldb/Target/RegisterContext.h" 38293116Semaste#include "lldb/Target/Target.h" 39293116Semaste#include "lldb/Target/Thread.h" 40293116Semaste#include "lldb/Target/ThreadPlanRunToAddress.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\ 52293116Semasteextern \"C\" \n\ 53293116Semaste{ \n\ 54293116Semaste extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 55293116Semaste extern void *class_getMethodImplementation_stret(void *objc_class, void *sel); \n\ 56293116Semaste extern void * object_getClass (id object); \n\ 57293116Semaste extern void * sel_getUid(char *name); \n\ 58293116Semaste extern int printf(const char *format, ...); \n\ 59293116Semaste} \n\ 60293116Semasteextern \"C\" void * __lldb_objc_find_implementation_for_selector (void *object, \n\ 61293116Semaste void *sel, \n\ 62293116Semaste int is_stret, \n\ 63293116Semaste int is_super, \n\ 64293116Semaste int is_super2, \n\ 65293116Semaste int is_fixup, \n\ 66293116Semaste int is_fixed, \n\ 67293116Semaste int debug) \n\ 68293116Semaste{ \n\ 69293116Semaste struct __lldb_imp_return_struct \n\ 70293116Semaste { \n\ 71293116Semaste void *class_addr; \n\ 72293116Semaste void *sel_addr; \n\ 73293116Semaste void *impl_addr; \n\ 74293116Semaste }; \n\ 75293116Semaste \n\ 76293116Semaste struct __lldb_objc_class { \n\ 77293116Semaste void *isa; \n\ 78293116Semaste void *super_ptr; \n\ 79293116Semaste }; \n\ 80293116Semaste struct __lldb_objc_super { \n\ 81293116Semaste void *reciever; \n\ 82293116Semaste struct __lldb_objc_class *class_ptr; \n\ 83293116Semaste }; \n\ 84293116Semaste struct __lldb_msg_ref { \n\ 85293116Semaste void *dont_know; \n\ 86293116Semaste void *sel; \n\ 87293116Semaste }; \n\ 88293116Semaste \n\ 89293116Semaste struct __lldb_imp_return_struct return_struct; \n\ 90293116Semaste \n\ 91293116Semaste if (debug) \n\ 92293116Semaste printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \" \n\ 93293116Semaste \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 94293116Semaste object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed); \n\ 95293116Semaste if (is_super) \n\ 96293116Semaste { \n\ 97293116Semaste if (is_super2) \n\ 98293116Semaste { \n\ 99293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr; \n\ 100293116Semaste } \n\ 101293116Semaste else \n\ 102293116Semaste { \n\ 103293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; \n\ 104293116Semaste } \n\ 105293116Semaste } \n\ 106293116Semaste else \n\ 107293116Semaste { \n\ 108293116Semaste // This code seems a little funny, but has its reasons... \n\ 109293116Semaste // The call to [object class] is here because if this is a class, and has not been called into \n\ 110293116Semaste // yet, we need to do something to force the class to initialize itself. \n\ 111293116Semaste // Then the call to object_getClass will actually return the correct class, either the class \n\ 112293116Semaste // if object is a class instance, or the meta-class if it is a class pointer. \n\ 113293116Semaste void *class_ptr = (void *) [(id) object class]; \n\ 114293116Semaste return_struct.class_addr = (id) object_getClass((id) object); \n\ 115293116Semaste if (debug) \n\ 116293116Semaste { \n\ 117293116Semaste if (class_ptr == object) \n\ 118293116Semaste { \n\ 119293116Semaste printf (\"Found a class object, need to use the meta class %p -> %p\\n\", \n\ 120293116Semaste class_ptr, return_struct.class_addr); \n\ 121293116Semaste } \n\ 122293116Semaste else \n\ 123293116Semaste { \n\ 124293116Semaste printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 125293116Semaste class_ptr, return_struct.class_addr); \n\ 126293116Semaste } \n\ 127293116Semaste } \n\ 128293116Semaste } \n\ 129293116Semaste \n\ 130293116Semaste if (is_fixup) \n\ 131293116Semaste { \n\ 132293116Semaste if (is_fixed) \n\ 133293116Semaste { \n\ 134293116Semaste return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 135293116Semaste } \n\ 136293116Semaste else \n\ 137293116Semaste { \n\ 138293116Semaste char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 139293116Semaste return_struct.sel_addr = sel_getUid (sel_name); \n\ 140293116Semaste if (debug) \n\ 141293116Semaste printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\", \n\ 142293116Semaste return_struct.sel_addr, sel_name); \n\ 143293116Semaste } \n\ 144293116Semaste } \n\ 145293116Semaste else \n\ 146293116Semaste { \n\ 147293116Semaste return_struct.sel_addr = sel; \n\ 148293116Semaste } \n\ 149293116Semaste \n\ 150293116Semaste if (is_stret) \n\ 151293116Semaste { \n\ 152293116Semaste return_struct.impl_addr = class_getMethodImplementation_stret (return_struct.class_addr, \n\ 153293116Semaste return_struct.sel_addr); \n\ 154293116Semaste } \n\ 155293116Semaste else \n\ 156293116Semaste { \n\ 157293116Semaste return_struct.impl_addr = class_getMethodImplementation (return_struct.class_addr, \n\ 158293116Semaste return_struct.sel_addr); \n\ 159293116Semaste } \n\ 160293116Semaste if (debug) \n\ 161293116Semaste printf (\"\\n*** Returning implementation: %p.\\n\", return_struct.impl_addr); \n\ 162293116Semaste \n\ 163293116Semaste return return_struct.impl_addr; \n\ 164293116Semaste} \n\ 165293116Semaste"; 166314564Sdimconst char * 167314564Sdim AppleObjCTrampolineHandler::g_lookup_implementation_no_stret_function_code = 168314564Sdim " \n\ 169293116Semasteextern \"C\" \n\ 170293116Semaste{ \n\ 171293116Semaste extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 172293116Semaste extern void * object_getClass (id object); \n\ 173293116Semaste extern void * sel_getUid(char *name); \n\ 174293116Semaste extern int printf(const char *format, ...); \n\ 175293116Semaste} \n\ 176293116Semasteextern \"C\" void * __lldb_objc_find_implementation_for_selector (void *object, \n\ 177293116Semaste void *sel, \n\ 178293116Semaste int is_stret, \n\ 179293116Semaste int is_super, \n\ 180293116Semaste int is_super2, \n\ 181293116Semaste int is_fixup, \n\ 182293116Semaste int is_fixed, \n\ 183293116Semaste int debug) \n\ 184293116Semaste{ \n\ 185293116Semaste struct __lldb_imp_return_struct \n\ 186293116Semaste { \n\ 187293116Semaste void *class_addr; \n\ 188293116Semaste void *sel_addr; \n\ 189293116Semaste void *impl_addr; \n\ 190293116Semaste }; \n\ 191293116Semaste \n\ 192293116Semaste struct __lldb_objc_class { \n\ 193293116Semaste void *isa; \n\ 194293116Semaste void *super_ptr; \n\ 195293116Semaste }; \n\ 196293116Semaste struct __lldb_objc_super { \n\ 197293116Semaste void *reciever; \n\ 198293116Semaste struct __lldb_objc_class *class_ptr; \n\ 199293116Semaste }; \n\ 200293116Semaste struct __lldb_msg_ref { \n\ 201293116Semaste void *dont_know; \n\ 202293116Semaste void *sel; \n\ 203293116Semaste }; \n\ 204293116Semaste \n\ 205293116Semaste struct __lldb_imp_return_struct return_struct; \n\ 206293116Semaste \n\ 207293116Semaste if (debug) \n\ 208293116Semaste printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \" \n\ 209293116Semaste \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 210293116Semaste object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed); \n\ 211293116Semaste if (is_super) \n\ 212293116Semaste { \n\ 213293116Semaste if (is_super2) \n\ 214293116Semaste { \n\ 215293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr; \n\ 216293116Semaste } \n\ 217293116Semaste else \n\ 218293116Semaste { \n\ 219293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; \n\ 220293116Semaste } \n\ 221293116Semaste } \n\ 222293116Semaste else \n\ 223293116Semaste { \n\ 224293116Semaste // This code seems a little funny, but has its reasons... \n\ 225293116Semaste // The call to [object class] is here because if this is a class, and has not been called into \n\ 226293116Semaste // yet, we need to do something to force the class to initialize itself. \n\ 227293116Semaste // Then the call to object_getClass will actually return the correct class, either the class \n\ 228293116Semaste // if object is a class instance, or the meta-class if it is a class pointer. \n\ 229293116Semaste void *class_ptr = (void *) [(id) object class]; \n\ 230293116Semaste return_struct.class_addr = (id) object_getClass((id) object); \n\ 231293116Semaste if (debug) \n\ 232293116Semaste { \n\ 233293116Semaste if (class_ptr == object) \n\ 234293116Semaste { \n\ 235293116Semaste printf (\"Found a class object, need to return the meta class %p -> %p\\n\", \n\ 236293116Semaste class_ptr, return_struct.class_addr); \n\ 237293116Semaste } \n\ 238293116Semaste else \n\ 239293116Semaste { \n\ 240293116Semaste printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 241293116Semaste class_ptr, return_struct.class_addr); \n\ 242293116Semaste } \n\ 243293116Semaste } \n\ 244293116Semaste } \n\ 245293116Semaste \n\ 246293116Semaste if (is_fixup) \n\ 247293116Semaste { \n\ 248293116Semaste if (is_fixed) \n\ 249293116Semaste { \n\ 250293116Semaste return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 251293116Semaste } \n\ 252293116Semaste else \n\ 253293116Semaste { \n\ 254293116Semaste char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 255293116Semaste return_struct.sel_addr = sel_getUid (sel_name); \n\ 256293116Semaste if (debug) \n\ 257293116Semaste printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\", \n\ 258293116Semaste return_struct.sel_addr, sel_name); \n\ 259293116Semaste } \n\ 260293116Semaste } \n\ 261293116Semaste else \n\ 262293116Semaste { \n\ 263293116Semaste return_struct.sel_addr = sel; \n\ 264293116Semaste } \n\ 265293116Semaste \n\ 266293116Semaste return_struct.impl_addr = class_getMethodImplementation (return_struct.class_addr, \n\ 267293116Semaste return_struct.sel_addr); \n\ 268293116Semaste if (debug) \n\ 269293116Semaste printf (\"\\n*** Returning implementation: 0x%p.\\n\", return_struct.impl_addr); \n\ 270293116Semaste \n\ 271293116Semaste return return_struct.impl_addr; \n\ 272293116Semaste} \n\ 273293116Semaste"; 274293116Semaste 275314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::VTableRegion( 276314564Sdim AppleObjCVTables *owner, lldb::addr_t header_addr) 277314564Sdim : m_valid(true), m_owner(owner), m_header_addr(header_addr), 278314564Sdim m_code_start_addr(0), m_code_end_addr(0), m_next_region(0) { 279314564Sdim SetUpRegion(); 280293116Semaste} 281293116Semaste 282314564SdimAppleObjCTrampolineHandler::~AppleObjCTrampolineHandler() {} 283293116Semaste 284314564Sdimvoid AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::SetUpRegion() { 285314564Sdim // The header looks like: 286314564Sdim // 287314564Sdim // uint16_t headerSize 288314564Sdim // uint16_t descSize 289314564Sdim // uint32_t descCount 290314564Sdim // void * next 291314564Sdim // 292314564Sdim // First read in the header: 293293116Semaste 294314564Sdim char memory_buffer[16]; 295314564Sdim ProcessSP process_sp = m_owner->GetProcessSP(); 296314564Sdim if (!process_sp) 297314564Sdim return; 298314564Sdim DataExtractor data(memory_buffer, sizeof(memory_buffer), 299314564Sdim process_sp->GetByteOrder(), 300314564Sdim process_sp->GetAddressByteSize()); 301314564Sdim size_t actual_size = 8 + process_sp->GetAddressByteSize(); 302314564Sdim Error error; 303314564Sdim size_t bytes_read = 304314564Sdim process_sp->ReadMemory(m_header_addr, memory_buffer, actual_size, error); 305314564Sdim if (bytes_read != actual_size) { 306314564Sdim m_valid = false; 307314564Sdim return; 308314564Sdim } 309293116Semaste 310314564Sdim lldb::offset_t offset = 0; 311314564Sdim const uint16_t header_size = data.GetU16(&offset); 312314564Sdim const uint16_t descriptor_size = data.GetU16(&offset); 313314564Sdim const size_t num_descriptors = data.GetU32(&offset); 314293116Semaste 315314564Sdim m_next_region = data.GetPointer(&offset); 316314564Sdim 317314564Sdim // If the header size is 0, that means we've come in too early before this 318314564Sdim // data is set up. 319314564Sdim // Set ourselves as not valid, and continue. 320314564Sdim if (header_size == 0 || num_descriptors == 0) { 321314564Sdim m_valid = false; 322314564Sdim return; 323314564Sdim } 324314564Sdim 325314564Sdim // Now read in all the descriptors: 326314564Sdim // The descriptor looks like: 327314564Sdim // 328314564Sdim // uint32_t offset 329314564Sdim // uint32_t flags 330314564Sdim // 331314564Sdim // Where offset is either 0 - in which case it is unused, or 332314564Sdim // it is the offset of the vtable code from the beginning of the descriptor 333314564Sdim // record. 334314564Sdim // Below, we'll convert that into an absolute code address, since I don't want 335314564Sdim // to have 336314564Sdim // to compute it over and over. 337314564Sdim 338314564Sdim // Ingest the whole descriptor array: 339314564Sdim const lldb::addr_t desc_ptr = m_header_addr + header_size; 340314564Sdim const size_t desc_array_size = num_descriptors * descriptor_size; 341314564Sdim DataBufferSP data_sp(new DataBufferHeap(desc_array_size, '\0')); 342314564Sdim uint8_t *dst = (uint8_t *)data_sp->GetBytes(); 343314564Sdim 344314564Sdim DataExtractor desc_extractor(dst, desc_array_size, process_sp->GetByteOrder(), 345314564Sdim process_sp->GetAddressByteSize()); 346314564Sdim bytes_read = process_sp->ReadMemory(desc_ptr, dst, desc_array_size, error); 347314564Sdim if (bytes_read != desc_array_size) { 348314564Sdim m_valid = false; 349314564Sdim return; 350314564Sdim } 351314564Sdim 352314564Sdim // The actual code for the vtables will be laid out consecutively, so I also 353314564Sdim // compute the start and end of the whole code block. 354314564Sdim 355314564Sdim offset = 0; 356314564Sdim m_code_start_addr = 0; 357314564Sdim m_code_end_addr = 0; 358314564Sdim 359314564Sdim for (size_t i = 0; i < num_descriptors; i++) { 360314564Sdim lldb::addr_t start_offset = offset; 361314564Sdim uint32_t voffset = desc_extractor.GetU32(&offset); 362314564Sdim uint32_t flags = desc_extractor.GetU32(&offset); 363314564Sdim lldb::addr_t code_addr = desc_ptr + start_offset + voffset; 364314564Sdim m_descriptors.push_back(VTableDescriptor(flags, code_addr)); 365314564Sdim 366314564Sdim if (m_code_start_addr == 0 || code_addr < m_code_start_addr) 367314564Sdim m_code_start_addr = code_addr; 368314564Sdim if (code_addr > m_code_end_addr) 369314564Sdim m_code_end_addr = code_addr; 370314564Sdim 371314564Sdim offset = start_offset + descriptor_size; 372314564Sdim } 373314564Sdim // Finally, a little bird told me that all the vtable code blocks are the same 374314564Sdim // size. 375314564Sdim // Let's compute the blocks and if they are all the same add the size to the 376314564Sdim // code end address: 377314564Sdim lldb::addr_t code_size = 0; 378314564Sdim bool all_the_same = true; 379314564Sdim for (size_t i = 0; i < num_descriptors - 1; i++) { 380314564Sdim lldb::addr_t this_size = 381314564Sdim m_descriptors[i + 1].code_start - m_descriptors[i].code_start; 382314564Sdim if (code_size == 0) 383314564Sdim code_size = this_size; 384314564Sdim else { 385314564Sdim if (this_size != code_size) 386314564Sdim all_the_same = false; 387314564Sdim if (this_size > code_size) 388314564Sdim code_size = this_size; 389293116Semaste } 390314564Sdim } 391314564Sdim if (all_the_same) 392314564Sdim m_code_end_addr += code_size; 393293116Semaste} 394293116Semaste 395314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion:: 396314564Sdim AddressInRegion(lldb::addr_t addr, uint32_t &flags) { 397314564Sdim if (!IsValid()) 398293116Semaste return false; 399293116Semaste 400314564Sdim if (addr < m_code_start_addr || addr > m_code_end_addr) 401314564Sdim return false; 402314564Sdim 403314564Sdim std::vector<VTableDescriptor>::iterator pos, end = m_descriptors.end(); 404314564Sdim for (pos = m_descriptors.begin(); pos != end; pos++) { 405314564Sdim if (addr <= (*pos).code_start) { 406314564Sdim flags = (*pos).flags; 407314564Sdim return true; 408293116Semaste } 409314564Sdim } 410314564Sdim return false; 411293116Semaste} 412314564Sdim 413314564Sdimvoid AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::Dump( 414314564Sdim Stream &s) { 415314564Sdim s.Printf("Header addr: 0x%" PRIx64 " Code start: 0x%" PRIx64 416314564Sdim " Code End: 0x%" PRIx64 " Next: 0x%" PRIx64 "\n", 417314564Sdim m_header_addr, m_code_start_addr, m_code_end_addr, m_next_region); 418314564Sdim size_t num_elements = m_descriptors.size(); 419314564Sdim for (size_t i = 0; i < num_elements; i++) { 420314564Sdim s.Indent(); 421314564Sdim s.Printf("Code start: 0x%" PRIx64 " Flags: %d\n", 422314564Sdim m_descriptors[i].code_start, m_descriptors[i].flags); 423314564Sdim } 424293116Semaste} 425293116Semaste 426314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::AppleObjCVTables( 427314564Sdim const ProcessSP &process_sp, const ModuleSP &objc_module_sp) 428314564Sdim : m_process_wp(), m_trampoline_header(LLDB_INVALID_ADDRESS), 429314564Sdim m_trampolines_changed_bp_id(LLDB_INVALID_BREAK_ID), 430314564Sdim m_objc_module_sp(objc_module_sp) { 431314564Sdim if (process_sp) 432314564Sdim m_process_wp = process_sp; 433293116Semaste} 434293116Semaste 435314564SdimAppleObjCTrampolineHandler::AppleObjCVTables::~AppleObjCVTables() { 436314564Sdim ProcessSP process_sp = GetProcessSP(); 437314564Sdim if (process_sp) { 438314564Sdim if (m_trampolines_changed_bp_id != LLDB_INVALID_BREAK_ID) 439314564Sdim process_sp->GetTarget().RemoveBreakpointByID(m_trampolines_changed_bp_id); 440314564Sdim } 441314564Sdim} 442293116Semaste 443314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::InitializeVTableSymbols() { 444314564Sdim if (m_trampoline_header != LLDB_INVALID_ADDRESS) 445314564Sdim return true; 446314564Sdim 447314564Sdim ProcessSP process_sp = GetProcessSP(); 448314564Sdim if (process_sp) { 449314564Sdim Target &target = process_sp->GetTarget(); 450314564Sdim 451314564Sdim const ModuleList &target_modules = target.GetImages(); 452314564Sdim std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 453314564Sdim size_t num_modules = target_modules.GetSize(); 454314564Sdim if (!m_objc_module_sp) { 455314564Sdim for (size_t i = 0; i < num_modules; i++) { 456314564Sdim if (process_sp->GetObjCLanguageRuntime()->IsModuleObjCLibrary( 457314564Sdim target_modules.GetModuleAtIndexUnlocked(i))) { 458314564Sdim m_objc_module_sp = target_modules.GetModuleAtIndexUnlocked(i); 459314564Sdim break; 460293116Semaste } 461314564Sdim } 462314564Sdim } 463314564Sdim 464314564Sdim if (m_objc_module_sp) { 465314564Sdim ConstString trampoline_name("gdb_objc_trampolines"); 466314564Sdim const Symbol *trampoline_symbol = 467314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(trampoline_name, 468314564Sdim eSymbolTypeData); 469314564Sdim if (trampoline_symbol != NULL) { 470314564Sdim m_trampoline_header = trampoline_symbol->GetLoadAddress(&target); 471314564Sdim if (m_trampoline_header == LLDB_INVALID_ADDRESS) 472314564Sdim return false; 473314564Sdim 474314564Sdim // Next look up the "changed" symbol and set a breakpoint on that... 475314564Sdim ConstString changed_name("gdb_objc_trampolines_changed"); 476314564Sdim const Symbol *changed_symbol = 477314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(changed_name, 478314564Sdim eSymbolTypeCode); 479314564Sdim if (changed_symbol != NULL) { 480314564Sdim const Address changed_symbol_addr = changed_symbol->GetAddress(); 481314564Sdim if (!changed_symbol_addr.IsValid()) 482314564Sdim return false; 483314564Sdim 484314564Sdim lldb::addr_t changed_addr = 485314564Sdim changed_symbol_addr.GetOpcodeLoadAddress(&target); 486314564Sdim if (changed_addr != LLDB_INVALID_ADDRESS) { 487314564Sdim BreakpointSP trampolines_changed_bp_sp = 488314564Sdim target.CreateBreakpoint(changed_addr, true, false); 489314564Sdim if (trampolines_changed_bp_sp) { 490314564Sdim m_trampolines_changed_bp_id = trampolines_changed_bp_sp->GetID(); 491314564Sdim trampolines_changed_bp_sp->SetCallback(RefreshTrampolines, this, 492314564Sdim true); 493314564Sdim trampolines_changed_bp_sp->SetBreakpointKind( 494314564Sdim "objc-trampolines-changed"); 495314564Sdim return true; 496293116Semaste } 497314564Sdim } 498293116Semaste } 499314564Sdim } 500293116Semaste } 501314564Sdim } 502314564Sdim return false; 503293116Semaste} 504293116Semaste 505314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::RefreshTrampolines( 506314564Sdim void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, 507314564Sdim lldb::user_id_t break_loc_id) { 508314564Sdim AppleObjCVTables *vtable_handler = (AppleObjCVTables *)baton; 509314564Sdim if (vtable_handler->InitializeVTableSymbols()) { 510314564Sdim // The Update function is called with the address of an added region. So we 511314564Sdim // grab that address, and 512314564Sdim // feed it into ReadRegions. Of course, our friend the ABI will get the 513314564Sdim // values for us. 514314564Sdim ExecutionContext exe_ctx(context->exe_ctx_ref); 515314564Sdim Process *process = exe_ctx.GetProcessPtr(); 516314564Sdim const ABI *abi = process->GetABI().get(); 517314564Sdim 518314564Sdim ClangASTContext *clang_ast_context = 519314564Sdim process->GetTarget().GetScratchClangASTContext(); 520314564Sdim ValueList argument_values; 521314564Sdim Value input_value; 522314564Sdim CompilerType clang_void_ptr_type = 523314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 524314564Sdim 525314564Sdim input_value.SetValueType(Value::eValueTypeScalar); 526314564Sdim // input_value.SetContext (Value::eContextTypeClangType, 527314564Sdim // clang_void_ptr_type); 528314564Sdim input_value.SetCompilerType(clang_void_ptr_type); 529314564Sdim argument_values.PushValue(input_value); 530314564Sdim 531314564Sdim bool success = 532314564Sdim abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values); 533314564Sdim if (!success) 534314564Sdim return false; 535314564Sdim 536314564Sdim // Now get a pointer value from the zeroth argument. 537314564Sdim Error error; 538314564Sdim DataExtractor data; 539314564Sdim error = argument_values.GetValueAtIndex(0)->GetValueAsData(&exe_ctx, data, 540314564Sdim 0, NULL); 541314564Sdim lldb::offset_t offset = 0; 542314564Sdim lldb::addr_t region_addr = data.GetPointer(&offset); 543314564Sdim 544314564Sdim if (region_addr != 0) 545314564Sdim vtable_handler->ReadRegions(region_addr); 546314564Sdim } 547314564Sdim return false; 548293116Semaste} 549293116Semaste 550314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions() { 551314564Sdim // The no argument version reads the start region from the value of the 552314564Sdim // gdb_regions_header, and 553314564Sdim // gets started from there. 554314564Sdim 555314564Sdim m_regions.clear(); 556314564Sdim if (!InitializeVTableSymbols()) 557293116Semaste return false; 558314564Sdim Error error; 559314564Sdim ProcessSP process_sp = GetProcessSP(); 560314564Sdim if (process_sp) { 561314564Sdim lldb::addr_t region_addr = 562314564Sdim process_sp->ReadPointerFromMemory(m_trampoline_header, error); 563314564Sdim if (error.Success()) 564314564Sdim return ReadRegions(region_addr); 565314564Sdim } 566314564Sdim return false; 567293116Semaste} 568293116Semaste 569314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions( 570314564Sdim lldb::addr_t region_addr) { 571314564Sdim ProcessSP process_sp = GetProcessSP(); 572314564Sdim if (!process_sp) 573314564Sdim return false; 574314564Sdim 575314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 576314564Sdim 577314564Sdim // We aren't starting at the trampoline symbol. 578314564Sdim InitializeVTableSymbols(); 579314564Sdim lldb::addr_t next_region = region_addr; 580314564Sdim 581314564Sdim // Read in the sizes of the headers. 582314564Sdim while (next_region != 0) { 583314564Sdim m_regions.push_back(VTableRegion(this, next_region)); 584314564Sdim if (!m_regions.back().IsValid()) { 585314564Sdim m_regions.clear(); 586314564Sdim return false; 587293116Semaste } 588314564Sdim if (log) { 589314564Sdim StreamString s; 590314564Sdim m_regions.back().Dump(s); 591314564Sdim log->Printf("Read vtable region: \n%s", s.GetData()); 592293116Semaste } 593314564Sdim 594314564Sdim next_region = m_regions.back().GetNextRegionAddr(); 595314564Sdim } 596314564Sdim 597314564Sdim return true; 598293116Semaste} 599293116Semaste 600314564Sdimbool AppleObjCTrampolineHandler::AppleObjCVTables::IsAddressInVTables( 601314564Sdim lldb::addr_t addr, uint32_t &flags) { 602314564Sdim region_collection::iterator pos, end = m_regions.end(); 603314564Sdim for (pos = m_regions.begin(); pos != end; pos++) { 604314564Sdim if ((*pos).AddressInRegion(addr, flags)) 605314564Sdim return true; 606314564Sdim } 607314564Sdim return false; 608314564Sdim} 609314564Sdim 610293116Semasteconst AppleObjCTrampolineHandler::DispatchFunction 611314564Sdim AppleObjCTrampolineHandler::g_dispatch_functions[] = { 612314564Sdim // NAME STRET SUPER SUPER2 FIXUP TYPE 613314564Sdim {"objc_msgSend", false, false, false, DispatchFunction::eFixUpNone}, 614314564Sdim {"objc_msgSend_fixup", false, false, false, 615314564Sdim DispatchFunction::eFixUpToFix}, 616314564Sdim {"objc_msgSend_fixedup", false, false, false, 617314564Sdim DispatchFunction::eFixUpFixed}, 618314564Sdim {"objc_msgSend_stret", true, false, false, 619314564Sdim DispatchFunction::eFixUpNone}, 620314564Sdim {"objc_msgSend_stret_fixup", true, false, false, 621314564Sdim DispatchFunction::eFixUpToFix}, 622314564Sdim {"objc_msgSend_stret_fixedup", true, false, false, 623314564Sdim DispatchFunction::eFixUpFixed}, 624314564Sdim {"objc_msgSend_fpret", false, false, false, 625314564Sdim DispatchFunction::eFixUpNone}, 626314564Sdim {"objc_msgSend_fpret_fixup", false, false, false, 627314564Sdim DispatchFunction::eFixUpToFix}, 628314564Sdim {"objc_msgSend_fpret_fixedup", false, false, false, 629314564Sdim DispatchFunction::eFixUpFixed}, 630314564Sdim {"objc_msgSend_fp2ret", false, false, true, 631314564Sdim DispatchFunction::eFixUpNone}, 632314564Sdim {"objc_msgSend_fp2ret_fixup", false, false, true, 633314564Sdim DispatchFunction::eFixUpToFix}, 634314564Sdim {"objc_msgSend_fp2ret_fixedup", false, false, true, 635314564Sdim DispatchFunction::eFixUpFixed}, 636314564Sdim {"objc_msgSendSuper", false, true, false, DispatchFunction::eFixUpNone}, 637314564Sdim {"objc_msgSendSuper_stret", true, true, false, 638314564Sdim DispatchFunction::eFixUpNone}, 639314564Sdim {"objc_msgSendSuper2", false, true, true, DispatchFunction::eFixUpNone}, 640314564Sdim {"objc_msgSendSuper2_fixup", false, true, true, 641314564Sdim DispatchFunction::eFixUpToFix}, 642314564Sdim {"objc_msgSendSuper2_fixedup", false, true, true, 643314564Sdim DispatchFunction::eFixUpFixed}, 644314564Sdim {"objc_msgSendSuper2_stret", true, true, true, 645314564Sdim DispatchFunction::eFixUpNone}, 646314564Sdim {"objc_msgSendSuper2_stret_fixup", true, true, true, 647314564Sdim DispatchFunction::eFixUpToFix}, 648314564Sdim {"objc_msgSendSuper2_stret_fixedup", true, true, true, 649314564Sdim DispatchFunction::eFixUpFixed}, 650293116Semaste}; 651293116Semaste 652314564SdimAppleObjCTrampolineHandler::AppleObjCTrampolineHandler( 653314564Sdim const ProcessSP &process_sp, const ModuleSP &objc_module_sp) 654314564Sdim : m_process_wp(), m_objc_module_sp(objc_module_sp), 655314564Sdim m_lookup_implementation_function_code(nullptr), 656314564Sdim m_impl_fn_addr(LLDB_INVALID_ADDRESS), 657314564Sdim m_impl_stret_fn_addr(LLDB_INVALID_ADDRESS), 658314564Sdim m_msg_forward_addr(LLDB_INVALID_ADDRESS) { 659314564Sdim if (process_sp) 660314564Sdim m_process_wp = process_sp; 661314564Sdim // Look up the known resolution functions: 662314564Sdim 663314564Sdim ConstString get_impl_name("class_getMethodImplementation"); 664314564Sdim ConstString get_impl_stret_name("class_getMethodImplementation_stret"); 665314564Sdim ConstString msg_forward_name("_objc_msgForward"); 666314564Sdim ConstString msg_forward_stret_name("_objc_msgForward_stret"); 667314564Sdim 668314564Sdim Target *target = process_sp ? &process_sp->GetTarget() : NULL; 669314564Sdim const Symbol *class_getMethodImplementation = 670314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_name, 671314564Sdim eSymbolTypeCode); 672314564Sdim const Symbol *class_getMethodImplementation_stret = 673314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_stret_name, 674314564Sdim eSymbolTypeCode); 675314564Sdim const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType( 676314564Sdim msg_forward_name, eSymbolTypeCode); 677314564Sdim const Symbol *msg_forward_stret = 678314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(msg_forward_stret_name, 679314564Sdim eSymbolTypeCode); 680314564Sdim 681314564Sdim if (class_getMethodImplementation) 682314564Sdim m_impl_fn_addr = 683314564Sdim class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress( 684314564Sdim target); 685314564Sdim if (class_getMethodImplementation_stret) 686314564Sdim m_impl_stret_fn_addr = 687314564Sdim class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress( 688314564Sdim target); 689314564Sdim if (msg_forward) 690314564Sdim m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target); 691314564Sdim if (msg_forward_stret) 692314564Sdim m_msg_forward_stret_addr = 693314564Sdim msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target); 694314564Sdim 695314564Sdim // FIXME: Do some kind of logging here. 696314564Sdim if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) { 697314564Sdim // If we can't even find the ordinary get method implementation function, 698314564Sdim // then we aren't going to be able to 699314564Sdim // step through any method dispatches. Warn to that effect and get out of 700314564Sdim // here. 701314564Sdim if (process_sp->CanJIT()) { 702314564Sdim process_sp->GetTarget().GetDebugger().GetErrorFile()->Printf( 703314564Sdim "Could not find implementation lookup function \"%s\"" 704314564Sdim " step in through ObjC method dispatch will not work.\n", 705314564Sdim get_impl_name.AsCString()); 706293116Semaste } 707314564Sdim return; 708314564Sdim } else if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) { 709314564Sdim // It there is no stret return lookup function, assume that it is the same 710314564Sdim // as the straight lookup: 711314564Sdim m_impl_stret_fn_addr = m_impl_fn_addr; 712314564Sdim // Also we will use the version of the lookup code that doesn't rely on the 713314564Sdim // stret version of the function. 714314564Sdim m_lookup_implementation_function_code = 715314564Sdim g_lookup_implementation_no_stret_function_code; 716314564Sdim } else { 717314564Sdim m_lookup_implementation_function_code = 718314564Sdim g_lookup_implementation_with_stret_function_code; 719314564Sdim } 720314564Sdim 721314564Sdim // Look up the addresses for the objc dispatch functions and cache them. For 722314564Sdim // now I'm inspecting the symbol 723314564Sdim // names dynamically to figure out how to dispatch to them. If it becomes 724314564Sdim // more complicated than this we can 725314564Sdim // turn the g_dispatch_functions char * array into a template table, and 726314564Sdim // populate the DispatchFunction map 727314564Sdim // from there. 728314564Sdim 729314564Sdim for (size_t i = 0; i != llvm::array_lengthof(g_dispatch_functions); i++) { 730314564Sdim ConstString name_const_str(g_dispatch_functions[i].name); 731314564Sdim const Symbol *msgSend_symbol = 732314564Sdim m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str, 733314564Sdim eSymbolTypeCode); 734314564Sdim if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) { 735314564Sdim // FixMe: Make g_dispatch_functions static table of DispatchFunctions, and 736314564Sdim // have the map be address->index. 737314564Sdim // Problem is we also need to lookup the dispatch function. For now we 738314564Sdim // could have a side table of stret & non-stret 739314564Sdim // dispatch functions. If that's as complex as it gets, we're fine. 740314564Sdim 741314564Sdim lldb::addr_t sym_addr = 742314564Sdim msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); 743314564Sdim 744314564Sdim m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); 745293116Semaste } 746314564Sdim } 747293116Semaste 748314564Sdim // Build our vtable dispatch handler here: 749314564Sdim m_vtables_ap.reset(new AppleObjCVTables(process_sp, m_objc_module_sp)); 750314564Sdim if (m_vtables_ap.get()) 751314564Sdim m_vtables_ap->ReadRegions(); 752293116Semaste} 753293116Semaste 754293116Semastelldb::addr_t 755314564SdimAppleObjCTrampolineHandler::SetupDispatchFunction(Thread &thread, 756314564Sdim ValueList &dispatch_values) { 757314564Sdim ThreadSP thread_sp(thread.shared_from_this()); 758314564Sdim ExecutionContext exe_ctx(thread_sp); 759314564Sdim DiagnosticManager diagnostics; 760314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 761309124Sdim 762314564Sdim lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; 763314564Sdim FunctionCaller *impl_function_caller = nullptr; 764293116Semaste 765314564Sdim // Scope for mutex locker: 766314564Sdim { 767314564Sdim std::lock_guard<std::mutex> guard(m_impl_function_mutex); 768309124Sdim 769314564Sdim // First stage is to make the ClangUtility to hold our injected function: 770293116Semaste 771314564Sdim if (!m_impl_code.get()) { 772314564Sdim if (m_lookup_implementation_function_code != NULL) { 773314564Sdim Error error; 774314564Sdim m_impl_code.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage( 775314564Sdim m_lookup_implementation_function_code, eLanguageTypeObjC, 776314564Sdim g_lookup_implementation_function_name, error)); 777314564Sdim if (error.Fail()) { 778314564Sdim if (log) 779314564Sdim log->Printf( 780314564Sdim "Failed to get Utility Function for implementation lookup: %s.", 781314564Sdim error.AsCString()); 782314564Sdim m_impl_code.reset(); 783314564Sdim return args_addr; 784314564Sdim } 785309124Sdim 786314564Sdim if (!m_impl_code->Install(diagnostics, exe_ctx)) { 787314564Sdim if (log) { 788314564Sdim log->Printf("Failed to install implementation lookup."); 789314564Sdim diagnostics.Dump(log); 790314564Sdim } 791314564Sdim m_impl_code.reset(); 792314564Sdim return args_addr; 793293116Semaste } 794314564Sdim } else { 795314564Sdim if (log) 796314564Sdim log->Printf("No method lookup implementation code."); 797314564Sdim return LLDB_INVALID_ADDRESS; 798314564Sdim } 799309124Sdim 800314564Sdim // Next make the runner function for our implementation utility function. 801314564Sdim ClangASTContext *clang_ast_context = 802314564Sdim thread.GetProcess()->GetTarget().GetScratchClangASTContext(); 803314564Sdim CompilerType clang_void_ptr_type = 804314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 805314564Sdim Error error; 806309124Sdim 807314564Sdim impl_function_caller = m_impl_code->MakeFunctionCaller( 808314564Sdim clang_void_ptr_type, dispatch_values, thread_sp, error); 809314564Sdim if (error.Fail()) { 810293116Semaste if (log) 811314564Sdim log->Printf( 812314564Sdim "Error getting function caller for dispatch lookup: \"%s\".", 813314564Sdim error.AsCString()); 814293116Semaste return args_addr; 815314564Sdim } 816314564Sdim } else { 817314564Sdim impl_function_caller = m_impl_code->GetFunctionCaller(); 818293116Semaste } 819314564Sdim } 820309124Sdim 821314564Sdim diagnostics.Clear(); 822314564Sdim 823314564Sdim // Now write down the argument values for this particular call. This looks 824314564Sdim // like it might be a race condition 825314564Sdim // if other threads were calling into here, but actually it isn't because we 826314564Sdim // allocate a new args structure for 827314564Sdim // this call by passing args_addr = LLDB_INVALID_ADDRESS... 828314564Sdim 829314564Sdim if (!impl_function_caller->WriteFunctionArguments( 830314564Sdim exe_ctx, args_addr, dispatch_values, diagnostics)) { 831314564Sdim if (log) { 832314564Sdim log->Printf("Error writing function arguments."); 833314564Sdim diagnostics.Dump(log); 834314564Sdim } 835293116Semaste return args_addr; 836314564Sdim } 837314564Sdim 838314564Sdim return args_addr; 839293116Semaste} 840293116Semaste 841293116SemasteThreadPlanSP 842314564SdimAppleObjCTrampolineHandler::GetStepThroughDispatchPlan(Thread &thread, 843314564Sdim bool stop_others) { 844314564Sdim ThreadPlanSP ret_plan_sp; 845314564Sdim lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); 846314564Sdim 847314564Sdim DispatchFunction this_dispatch; 848314564Sdim bool found_it = false; 849314564Sdim 850314564Sdim // First step is to look and see if we are in one of the known ObjC dispatch 851314564Sdim // functions. We've already compiled 852314564Sdim // a table of same, so consult it. 853314564Sdim 854314564Sdim MsgsendMap::iterator pos; 855314564Sdim pos = m_msgSend_map.find(curr_pc); 856314564Sdim if (pos != m_msgSend_map.end()) { 857314564Sdim this_dispatch = g_dispatch_functions[(*pos).second]; 858314564Sdim found_it = true; 859314564Sdim } 860314564Sdim 861314564Sdim // Next check to see if we are in a vtable region: 862314564Sdim 863314564Sdim if (!found_it) { 864314564Sdim uint32_t flags; 865314564Sdim if (m_vtables_ap.get()) { 866314564Sdim found_it = m_vtables_ap->IsAddressInVTables(curr_pc, flags); 867314564Sdim if (found_it) { 868314564Sdim this_dispatch.name = "vtable"; 869314564Sdim this_dispatch.stret_return = 870314564Sdim (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) == 871314564Sdim AppleObjCVTables::eOBJC_TRAMPOLINE_STRET; 872314564Sdim this_dispatch.is_super = false; 873314564Sdim this_dispatch.is_super2 = false; 874314564Sdim this_dispatch.fixedup = DispatchFunction::eFixUpFixed; 875314564Sdim } 876293116Semaste } 877314564Sdim } 878314564Sdim 879314564Sdim if (found_it) { 880314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 881314564Sdim 882314564Sdim // We are decoding a method dispatch. 883314564Sdim // First job is to pull the arguments out: 884314564Sdim 885314564Sdim lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); 886314564Sdim 887314564Sdim const ABI *abi = NULL; 888314564Sdim ProcessSP process_sp(thread.CalculateProcess()); 889314564Sdim if (process_sp) 890314564Sdim abi = process_sp->GetABI().get(); 891314564Sdim if (abi == NULL) 892314564Sdim return ret_plan_sp; 893314564Sdim 894314564Sdim TargetSP target_sp(thread.CalculateTarget()); 895314564Sdim 896314564Sdim ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext(); 897314564Sdim ValueList argument_values; 898314564Sdim Value void_ptr_value; 899314564Sdim CompilerType clang_void_ptr_type = 900314564Sdim clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 901314564Sdim void_ptr_value.SetValueType(Value::eValueTypeScalar); 902314564Sdim // void_ptr_value.SetContext (Value::eContextTypeClangType, 903314564Sdim // clang_void_ptr_type); 904314564Sdim void_ptr_value.SetCompilerType(clang_void_ptr_type); 905314564Sdim 906314564Sdim int obj_index; 907314564Sdim int sel_index; 908314564Sdim 909314564Sdim // If this is a struct return dispatch, then the first argument is the 910314564Sdim // return struct pointer, and the object is the second, and the selector is 911314564Sdim // the third. 912314564Sdim // Otherwise the object is the first and the selector the second. 913314564Sdim if (this_dispatch.stret_return) { 914314564Sdim obj_index = 1; 915314564Sdim sel_index = 2; 916314564Sdim argument_values.PushValue(void_ptr_value); 917314564Sdim argument_values.PushValue(void_ptr_value); 918314564Sdim argument_values.PushValue(void_ptr_value); 919314564Sdim } else { 920314564Sdim obj_index = 0; 921314564Sdim sel_index = 1; 922314564Sdim argument_values.PushValue(void_ptr_value); 923314564Sdim argument_values.PushValue(void_ptr_value); 924293116Semaste } 925293116Semaste 926314564Sdim bool success = abi->GetArgumentValues(thread, argument_values); 927314564Sdim if (!success) 928314564Sdim return ret_plan_sp; 929293116Semaste 930314564Sdim lldb::addr_t obj_addr = 931314564Sdim argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong(); 932314564Sdim if (obj_addr == 0x0) { 933314564Sdim if (log) 934314564Sdim log->Printf( 935314564Sdim "Asked to step to dispatch to nil object, returning empty plan."); 936314564Sdim return ret_plan_sp; 937314564Sdim } 938314564Sdim 939314564Sdim ExecutionContext exe_ctx(thread.shared_from_this()); 940314564Sdim Process *process = exe_ctx.GetProcessPtr(); 941314564Sdim // isa_addr will store the class pointer that the method is being dispatched 942314564Sdim // to - so either the class 943314564Sdim // directly or the super class if this is one of the objc_msgSendSuper 944314564Sdim // flavors. That's mostly used to 945314564Sdim // look up the class/selector pair in our cache. 946314564Sdim 947314564Sdim lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS; 948314564Sdim lldb::addr_t sel_addr = 949314564Sdim argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong(); 950314564Sdim 951314564Sdim // Figure out the class this is being dispatched to and see if we've already 952314564Sdim // cached this method call, 953314564Sdim // If so we can push a run-to-address plan directly. Otherwise we have to 954314564Sdim // figure out where 955314564Sdim // the implementation lives. 956314564Sdim 957314564Sdim if (this_dispatch.is_super) { 958314564Sdim if (this_dispatch.is_super2) { 959314564Sdim // In the objc_msgSendSuper2 case, we don't get the object directly, we 960314564Sdim // get a structure containing 961314564Sdim // the object and the class to which the super message is being sent. 962314564Sdim // So we need to dig the super 963314564Sdim // out of the class and use that. 964314564Sdim 965314564Sdim Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 966314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 967314564Sdim super_value.ResolveValue(&exe_ctx); 968314564Sdim 969314564Sdim if (super_value.GetScalar().IsValid()) { 970314564Sdim 971314564Sdim // isa_value now holds the class pointer. The second word of the 972314564Sdim // class pointer is the super-class pointer: 973314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 974314564Sdim super_value.ResolveValue(&exe_ctx); 975314564Sdim if (super_value.GetScalar().IsValid()) 976314564Sdim isa_addr = super_value.GetScalar().ULongLong(); 977314564Sdim else { 978293116Semaste if (log) 979314564Sdim log->Printf("Failed to extract the super class value from the " 980314564Sdim "class in objc_super."); 981314564Sdim } 982314564Sdim } else { 983314564Sdim if (log) 984314564Sdim log->Printf("Failed to extract the class value from objc_super."); 985293116Semaste } 986314564Sdim } else { 987314564Sdim // In the objc_msgSendSuper case, we don't get the object directly, we 988314564Sdim // get a two element structure containing 989314564Sdim // the object and the super class to which the super message is being 990314564Sdim // sent. So the class we want is 991314564Sdim // the second element of this structure. 992293116Semaste 993314564Sdim Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 994314564Sdim super_value.GetScalar() += process->GetAddressByteSize(); 995314564Sdim super_value.ResolveValue(&exe_ctx); 996314564Sdim 997314564Sdim if (super_value.GetScalar().IsValid()) { 998314564Sdim isa_addr = super_value.GetScalar().ULongLong(); 999314564Sdim } else { 1000314564Sdim if (log) 1001314564Sdim log->Printf("Failed to extract the class value from objc_super."); 1002293116Semaste } 1003314564Sdim } 1004314564Sdim } else { 1005314564Sdim // In the direct dispatch case, the object->isa is the class pointer we 1006314564Sdim // want. 1007293116Semaste 1008314564Sdim // This is a little cheesy, but since object->isa is the first field, 1009314564Sdim // making the object value a load address value and resolving it will get 1010314564Sdim // the pointer sized data pointed to by that value... 1011293116Semaste 1012314564Sdim // Note, it isn't a fatal error not to be able to get the address from the 1013314564Sdim // object, since this might 1014314564Sdim // be a "tagged pointer" which isn't a real object, but rather some word 1015314564Sdim // length encoded dingus. 1016314564Sdim 1017314564Sdim Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); 1018314564Sdim 1019314564Sdim isa_value.SetValueType(Value::eValueTypeLoadAddress); 1020314564Sdim isa_value.ResolveValue(&exe_ctx); 1021314564Sdim if (isa_value.GetScalar().IsValid()) { 1022314564Sdim isa_addr = isa_value.GetScalar().ULongLong(); 1023314564Sdim } else { 1024314564Sdim if (log) 1025314564Sdim log->Printf("Failed to extract the isa value from object."); 1026314564Sdim } 1027293116Semaste } 1028314564Sdim 1029314564Sdim // Okay, we've got the address of the class for which we're resolving this, 1030314564Sdim // let's see if it's in our cache: 1031314564Sdim lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS; 1032314564Sdim 1033314564Sdim if (isa_addr != LLDB_INVALID_ADDRESS) { 1034314564Sdim if (log) { 1035314564Sdim log->Printf("Resolving call for class - 0x%" PRIx64 1036314564Sdim " and selector - 0x%" PRIx64, 1037314564Sdim isa_addr, sel_addr); 1038314564Sdim } 1039314564Sdim ObjCLanguageRuntime *objc_runtime = 1040314564Sdim thread.GetProcess()->GetObjCLanguageRuntime(); 1041314564Sdim assert(objc_runtime != NULL); 1042314564Sdim 1043314564Sdim impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sel_addr); 1044314564Sdim } 1045314564Sdim 1046314564Sdim if (impl_addr != LLDB_INVALID_ADDRESS) { 1047314564Sdim // Yup, it was in the cache, so we can run to that address directly. 1048314564Sdim 1049314564Sdim if (log) 1050314564Sdim log->Printf("Found implementation address in cache: 0x%" PRIx64, 1051314564Sdim impl_addr); 1052314564Sdim 1053314564Sdim ret_plan_sp.reset( 1054314564Sdim new ThreadPlanRunToAddress(thread, impl_addr, stop_others)); 1055314564Sdim } else { 1056314564Sdim // We haven't seen this class/selector pair yet. Look it up. 1057314564Sdim StreamString errors; 1058314564Sdim Address impl_code_address; 1059314564Sdim 1060314564Sdim ValueList dispatch_values; 1061314564Sdim 1062314564Sdim // We've will inject a little function in the target that takes the 1063314564Sdim // object, selector and some flags, 1064314564Sdim // and figures out the implementation. Looks like: 1065314564Sdim // void *__lldb_objc_find_implementation_for_selector (void *object, 1066314564Sdim // void *sel, 1067314564Sdim // int is_stret, 1068314564Sdim // int is_super, 1069314564Sdim // int is_super2, 1070314564Sdim // int is_fixup, 1071314564Sdim // int is_fixed, 1072314564Sdim // int debug) 1073314564Sdim // So set up the arguments for that call. 1074314564Sdim 1075314564Sdim dispatch_values.PushValue(*(argument_values.GetValueAtIndex(obj_index))); 1076314564Sdim dispatch_values.PushValue(*(argument_values.GetValueAtIndex(sel_index))); 1077314564Sdim 1078314564Sdim Value flag_value; 1079314564Sdim CompilerType clang_int_type = 1080314564Sdim clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( 1081314564Sdim lldb::eEncodingSint, 32); 1082314564Sdim flag_value.SetValueType(Value::eValueTypeScalar); 1083314564Sdim // flag_value.SetContext (Value::eContextTypeClangType, clang_int_type); 1084314564Sdim flag_value.SetCompilerType(clang_int_type); 1085314564Sdim 1086314564Sdim if (this_dispatch.stret_return) 1087314564Sdim flag_value.GetScalar() = 1; 1088314564Sdim else 1089314564Sdim flag_value.GetScalar() = 0; 1090314564Sdim dispatch_values.PushValue(flag_value); 1091314564Sdim 1092314564Sdim if (this_dispatch.is_super) 1093314564Sdim flag_value.GetScalar() = 1; 1094314564Sdim else 1095314564Sdim flag_value.GetScalar() = 0; 1096314564Sdim dispatch_values.PushValue(flag_value); 1097314564Sdim 1098314564Sdim if (this_dispatch.is_super2) 1099314564Sdim flag_value.GetScalar() = 1; 1100314564Sdim else 1101314564Sdim flag_value.GetScalar() = 0; 1102314564Sdim dispatch_values.PushValue(flag_value); 1103314564Sdim 1104314564Sdim switch (this_dispatch.fixedup) { 1105314564Sdim case DispatchFunction::eFixUpNone: 1106314564Sdim flag_value.GetScalar() = 0; 1107314564Sdim dispatch_values.PushValue(flag_value); 1108314564Sdim dispatch_values.PushValue(flag_value); 1109314564Sdim break; 1110314564Sdim case DispatchFunction::eFixUpFixed: 1111314564Sdim flag_value.GetScalar() = 1; 1112314564Sdim dispatch_values.PushValue(flag_value); 1113314564Sdim flag_value.GetScalar() = 1; 1114314564Sdim dispatch_values.PushValue(flag_value); 1115314564Sdim break; 1116314564Sdim case DispatchFunction::eFixUpToFix: 1117314564Sdim flag_value.GetScalar() = 1; 1118314564Sdim dispatch_values.PushValue(flag_value); 1119314564Sdim flag_value.GetScalar() = 0; 1120314564Sdim dispatch_values.PushValue(flag_value); 1121314564Sdim break; 1122314564Sdim } 1123314564Sdim if (log && log->GetVerbose()) 1124314564Sdim flag_value.GetScalar() = 1; 1125314564Sdim else 1126314564Sdim flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done. 1127314564Sdim dispatch_values.PushValue(flag_value); 1128314564Sdim 1129314564Sdim // The step through code might have to fill in the cache, so it is not 1130314564Sdim // safe to run only one thread. 1131314564Sdim // So we override the stop_others value passed in to us here: 1132314564Sdim const bool trampoline_stop_others = false; 1133314564Sdim ret_plan_sp.reset(new AppleThreadPlanStepThroughObjCTrampoline( 1134314564Sdim thread, this, dispatch_values, isa_addr, sel_addr, 1135314564Sdim trampoline_stop_others)); 1136314564Sdim if (log) { 1137314564Sdim StreamString s; 1138314564Sdim ret_plan_sp->GetDescription(&s, eDescriptionLevelFull); 1139314564Sdim log->Printf("Using ObjC step plan: %s.\n", s.GetData()); 1140314564Sdim } 1141314564Sdim } 1142314564Sdim } 1143314564Sdim 1144314564Sdim return ret_plan_sp; 1145293116Semaste} 1146293116Semaste 1147293116SemasteFunctionCaller * 1148314564SdimAppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller() { 1149314564Sdim return m_impl_code->GetFunctionCaller(); 1150293116Semaste} 1151