AppleObjCTrampolineHandler.cpp revision 293116
1293116Semaste//===-- AppleObjCTrampolineHandler.cpp ----------------------------*- C++ -*-===// 2293116Semaste// 3293116Semaste// The LLVM Compiler Infrastructure 4293116Semaste// 5293116Semaste// This file is distributed under the University of Illinois Open Source 6293116Semaste// License. See LICENSE.TXT for details. 7293116Semaste// 8293116Semaste//===----------------------------------------------------------------------===// 9293116Semaste 10293116Semaste#include "AppleObjCTrampolineHandler.h" 11293116Semaste 12293116Semaste// C Includes 13293116Semaste// C++ Includes 14293116Semaste// Other libraries and framework includes 15293116Semaste// Project includes 16293116Semaste#include "AppleThreadPlanStepThroughObjCTrampoline.h" 17293116Semaste 18293116Semaste#include "lldb/Breakpoint/StoppointCallbackContext.h" 19293116Semaste#include "lldb/Core/ConstString.h" 20293116Semaste#include "lldb/Core/Debugger.h" 21293116Semaste#include "lldb/Core/Log.h" 22293116Semaste#include "lldb/Core/Module.h" 23293116Semaste#include "lldb/Core/StreamFile.h" 24293116Semaste#include "lldb/Core/Value.h" 25293116Semaste#include "lldb/Expression/UserExpression.h" 26293116Semaste#include "lldb/Expression/FunctionCaller.h" 27293116Semaste#include "lldb/Expression/UtilityFunction.h" 28293116Semaste#include "lldb/Host/FileSpec.h" 29293116Semaste#include "lldb/Symbol/ClangASTContext.h" 30293116Semaste#include "lldb/Symbol/Symbol.h" 31293116Semaste#include "lldb/Target/ABI.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/ExecutionContext.h" 38293116Semaste#include "lldb/Target/ThreadPlanRunToAddress.h" 39293116Semaste 40293116Semaste#include "llvm/ADT/STLExtras.h" 41293116Semaste 42293116Semasteusing namespace lldb; 43293116Semasteusing namespace lldb_private; 44293116Semaste 45293116Semasteconst char *AppleObjCTrampolineHandler::g_lookup_implementation_function_name = "__lldb_objc_find_implementation_for_selector"; 46293116Semasteconst char *AppleObjCTrampolineHandler::g_lookup_implementation_function_code = NULL; 47293116Semasteconst char *AppleObjCTrampolineHandler::g_lookup_implementation_with_stret_function_code = " \n\ 48293116Semasteextern \"C\" \n\ 49293116Semaste{ \n\ 50293116Semaste extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 51293116Semaste extern void *class_getMethodImplementation_stret(void *objc_class, void *sel); \n\ 52293116Semaste extern void * object_getClass (id object); \n\ 53293116Semaste extern void * sel_getUid(char *name); \n\ 54293116Semaste extern int printf(const char *format, ...); \n\ 55293116Semaste} \n\ 56293116Semasteextern \"C\" void * __lldb_objc_find_implementation_for_selector (void *object, \n\ 57293116Semaste void *sel, \n\ 58293116Semaste int is_stret, \n\ 59293116Semaste int is_super, \n\ 60293116Semaste int is_super2, \n\ 61293116Semaste int is_fixup, \n\ 62293116Semaste int is_fixed, \n\ 63293116Semaste int debug) \n\ 64293116Semaste{ \n\ 65293116Semaste struct __lldb_imp_return_struct \n\ 66293116Semaste { \n\ 67293116Semaste void *class_addr; \n\ 68293116Semaste void *sel_addr; \n\ 69293116Semaste void *impl_addr; \n\ 70293116Semaste }; \n\ 71293116Semaste \n\ 72293116Semaste struct __lldb_objc_class { \n\ 73293116Semaste void *isa; \n\ 74293116Semaste void *super_ptr; \n\ 75293116Semaste }; \n\ 76293116Semaste struct __lldb_objc_super { \n\ 77293116Semaste void *reciever; \n\ 78293116Semaste struct __lldb_objc_class *class_ptr; \n\ 79293116Semaste }; \n\ 80293116Semaste struct __lldb_msg_ref { \n\ 81293116Semaste void *dont_know; \n\ 82293116Semaste void *sel; \n\ 83293116Semaste }; \n\ 84293116Semaste \n\ 85293116Semaste struct __lldb_imp_return_struct return_struct; \n\ 86293116Semaste \n\ 87293116Semaste if (debug) \n\ 88293116Semaste printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \" \n\ 89293116Semaste \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 90293116Semaste object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed); \n\ 91293116Semaste if (is_super) \n\ 92293116Semaste { \n\ 93293116Semaste if (is_super2) \n\ 94293116Semaste { \n\ 95293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr; \n\ 96293116Semaste } \n\ 97293116Semaste else \n\ 98293116Semaste { \n\ 99293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; \n\ 100293116Semaste } \n\ 101293116Semaste } \n\ 102293116Semaste else \n\ 103293116Semaste { \n\ 104293116Semaste // This code seems a little funny, but has its reasons... \n\ 105293116Semaste // The call to [object class] is here because if this is a class, and has not been called into \n\ 106293116Semaste // yet, we need to do something to force the class to initialize itself. \n\ 107293116Semaste // Then the call to object_getClass will actually return the correct class, either the class \n\ 108293116Semaste // if object is a class instance, or the meta-class if it is a class pointer. \n\ 109293116Semaste void *class_ptr = (void *) [(id) object class]; \n\ 110293116Semaste return_struct.class_addr = (id) object_getClass((id) object); \n\ 111293116Semaste if (debug) \n\ 112293116Semaste { \n\ 113293116Semaste if (class_ptr == object) \n\ 114293116Semaste { \n\ 115293116Semaste printf (\"Found a class object, need to use the meta class %p -> %p\\n\", \n\ 116293116Semaste class_ptr, return_struct.class_addr); \n\ 117293116Semaste } \n\ 118293116Semaste else \n\ 119293116Semaste { \n\ 120293116Semaste printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 121293116Semaste class_ptr, return_struct.class_addr); \n\ 122293116Semaste } \n\ 123293116Semaste } \n\ 124293116Semaste } \n\ 125293116Semaste \n\ 126293116Semaste if (is_fixup) \n\ 127293116Semaste { \n\ 128293116Semaste if (is_fixed) \n\ 129293116Semaste { \n\ 130293116Semaste return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 131293116Semaste } \n\ 132293116Semaste else \n\ 133293116Semaste { \n\ 134293116Semaste char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 135293116Semaste return_struct.sel_addr = sel_getUid (sel_name); \n\ 136293116Semaste if (debug) \n\ 137293116Semaste printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\", \n\ 138293116Semaste return_struct.sel_addr, sel_name); \n\ 139293116Semaste } \n\ 140293116Semaste } \n\ 141293116Semaste else \n\ 142293116Semaste { \n\ 143293116Semaste return_struct.sel_addr = sel; \n\ 144293116Semaste } \n\ 145293116Semaste \n\ 146293116Semaste if (is_stret) \n\ 147293116Semaste { \n\ 148293116Semaste return_struct.impl_addr = class_getMethodImplementation_stret (return_struct.class_addr, \n\ 149293116Semaste return_struct.sel_addr); \n\ 150293116Semaste } \n\ 151293116Semaste else \n\ 152293116Semaste { \n\ 153293116Semaste return_struct.impl_addr = class_getMethodImplementation (return_struct.class_addr, \n\ 154293116Semaste return_struct.sel_addr); \n\ 155293116Semaste } \n\ 156293116Semaste if (debug) \n\ 157293116Semaste printf (\"\\n*** Returning implementation: %p.\\n\", return_struct.impl_addr); \n\ 158293116Semaste \n\ 159293116Semaste return return_struct.impl_addr; \n\ 160293116Semaste} \n\ 161293116Semaste"; 162293116Semasteconst char *AppleObjCTrampolineHandler::g_lookup_implementation_no_stret_function_code = " \n\ 163293116Semasteextern \"C\" \n\ 164293116Semaste{ \n\ 165293116Semaste extern void *class_getMethodImplementation(void *objc_class, void *sel); \n\ 166293116Semaste extern void * object_getClass (id object); \n\ 167293116Semaste extern void * sel_getUid(char *name); \n\ 168293116Semaste extern int printf(const char *format, ...); \n\ 169293116Semaste} \n\ 170293116Semasteextern \"C\" void * __lldb_objc_find_implementation_for_selector (void *object, \n\ 171293116Semaste void *sel, \n\ 172293116Semaste int is_stret, \n\ 173293116Semaste int is_super, \n\ 174293116Semaste int is_super2, \n\ 175293116Semaste int is_fixup, \n\ 176293116Semaste int is_fixed, \n\ 177293116Semaste int debug) \n\ 178293116Semaste{ \n\ 179293116Semaste struct __lldb_imp_return_struct \n\ 180293116Semaste { \n\ 181293116Semaste void *class_addr; \n\ 182293116Semaste void *sel_addr; \n\ 183293116Semaste void *impl_addr; \n\ 184293116Semaste }; \n\ 185293116Semaste \n\ 186293116Semaste struct __lldb_objc_class { \n\ 187293116Semaste void *isa; \n\ 188293116Semaste void *super_ptr; \n\ 189293116Semaste }; \n\ 190293116Semaste struct __lldb_objc_super { \n\ 191293116Semaste void *reciever; \n\ 192293116Semaste struct __lldb_objc_class *class_ptr; \n\ 193293116Semaste }; \n\ 194293116Semaste struct __lldb_msg_ref { \n\ 195293116Semaste void *dont_know; \n\ 196293116Semaste void *sel; \n\ 197293116Semaste }; \n\ 198293116Semaste \n\ 199293116Semaste struct __lldb_imp_return_struct return_struct; \n\ 200293116Semaste \n\ 201293116Semaste if (debug) \n\ 202293116Semaste printf (\"\\n*** Called with obj: 0x%p sel: 0x%p is_stret: %d is_super: %d, \" \n\ 203293116Semaste \"is_super2: %d, is_fixup: %d, is_fixed: %d\\n\", \n\ 204293116Semaste object, sel, is_stret, is_super, is_super2, is_fixup, is_fixed); \n\ 205293116Semaste if (is_super) \n\ 206293116Semaste { \n\ 207293116Semaste if (is_super2) \n\ 208293116Semaste { \n\ 209293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr->super_ptr; \n\ 210293116Semaste } \n\ 211293116Semaste else \n\ 212293116Semaste { \n\ 213293116Semaste return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; \n\ 214293116Semaste } \n\ 215293116Semaste } \n\ 216293116Semaste else \n\ 217293116Semaste { \n\ 218293116Semaste // This code seems a little funny, but has its reasons... \n\ 219293116Semaste // The call to [object class] is here because if this is a class, and has not been called into \n\ 220293116Semaste // yet, we need to do something to force the class to initialize itself. \n\ 221293116Semaste // Then the call to object_getClass will actually return the correct class, either the class \n\ 222293116Semaste // if object is a class instance, or the meta-class if it is a class pointer. \n\ 223293116Semaste void *class_ptr = (void *) [(id) object class]; \n\ 224293116Semaste return_struct.class_addr = (id) object_getClass((id) object); \n\ 225293116Semaste if (debug) \n\ 226293116Semaste { \n\ 227293116Semaste if (class_ptr == object) \n\ 228293116Semaste { \n\ 229293116Semaste printf (\"Found a class object, need to return the meta class %p -> %p\\n\", \n\ 230293116Semaste class_ptr, return_struct.class_addr); \n\ 231293116Semaste } \n\ 232293116Semaste else \n\ 233293116Semaste { \n\ 234293116Semaste printf (\"[object class] returned: %p object_getClass: %p.\\n\", \n\ 235293116Semaste class_ptr, return_struct.class_addr); \n\ 236293116Semaste } \n\ 237293116Semaste } \n\ 238293116Semaste } \n\ 239293116Semaste \n\ 240293116Semaste if (is_fixup) \n\ 241293116Semaste { \n\ 242293116Semaste if (is_fixed) \n\ 243293116Semaste { \n\ 244293116Semaste return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; \n\ 245293116Semaste } \n\ 246293116Semaste else \n\ 247293116Semaste { \n\ 248293116Semaste char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; \n\ 249293116Semaste return_struct.sel_addr = sel_getUid (sel_name); \n\ 250293116Semaste if (debug) \n\ 251293116Semaste printf (\"\\n*** Got fixed up selector: %p for name %s.\\n\", \n\ 252293116Semaste return_struct.sel_addr, sel_name); \n\ 253293116Semaste } \n\ 254293116Semaste } \n\ 255293116Semaste else \n\ 256293116Semaste { \n\ 257293116Semaste return_struct.sel_addr = sel; \n\ 258293116Semaste } \n\ 259293116Semaste \n\ 260293116Semaste return_struct.impl_addr = class_getMethodImplementation (return_struct.class_addr, \n\ 261293116Semaste return_struct.sel_addr); \n\ 262293116Semaste if (debug) \n\ 263293116Semaste printf (\"\\n*** Returning implementation: 0x%p.\\n\", return_struct.impl_addr); \n\ 264293116Semaste \n\ 265293116Semaste return return_struct.impl_addr; \n\ 266293116Semaste} \n\ 267293116Semaste"; 268293116Semaste 269293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::VTableRegion(AppleObjCVTables *owner, lldb::addr_t header_addr) : 270293116Semaste m_valid (true), 271293116Semaste m_owner(owner), 272293116Semaste m_header_addr (header_addr), 273293116Semaste m_code_start_addr(0), 274293116Semaste m_code_end_addr (0), 275293116Semaste m_next_region (0) 276293116Semaste{ 277293116Semaste SetUpRegion (); 278293116Semaste} 279293116Semaste 280293116SemasteAppleObjCTrampolineHandler::~AppleObjCTrampolineHandler() 281293116Semaste{ 282293116Semaste} 283293116Semaste 284293116Semastevoid 285293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::SetUpRegion() 286293116Semaste{ 287293116Semaste // The header looks like: 288293116Semaste // 289293116Semaste // uint16_t headerSize 290293116Semaste // uint16_t descSize 291293116Semaste // uint32_t descCount 292293116Semaste // void * next 293293116Semaste // 294293116Semaste // First read in the header: 295293116Semaste 296293116Semaste char memory_buffer[16]; 297293116Semaste ProcessSP process_sp = m_owner->GetProcessSP(); 298293116Semaste if (!process_sp) 299293116Semaste return; 300293116Semaste DataExtractor data(memory_buffer, sizeof(memory_buffer), 301293116Semaste process_sp->GetByteOrder(), 302293116Semaste process_sp->GetAddressByteSize()); 303293116Semaste size_t actual_size = 8 + process_sp->GetAddressByteSize(); 304293116Semaste Error error; 305293116Semaste size_t bytes_read = process_sp->ReadMemory (m_header_addr, memory_buffer, actual_size, error); 306293116Semaste if (bytes_read != actual_size) 307293116Semaste { 308293116Semaste m_valid = false; 309293116Semaste return; 310293116Semaste } 311293116Semaste 312293116Semaste lldb::offset_t offset = 0; 313293116Semaste const uint16_t header_size = data.GetU16(&offset); 314293116Semaste const uint16_t descriptor_size = data.GetU16(&offset); 315293116Semaste const size_t num_descriptors = data.GetU32(&offset); 316293116Semaste 317293116Semaste m_next_region = data.GetPointer(&offset); 318293116Semaste 319293116Semaste // If the header size is 0, that means we've come in too early before this data is set up. 320293116Semaste // Set ourselves as not valid, and continue. 321293116Semaste if (header_size == 0 || num_descriptors == 0) 322293116Semaste { 323293116Semaste m_valid = false; 324293116Semaste return; 325293116Semaste } 326293116Semaste 327293116Semaste // Now read in all the descriptors: 328293116Semaste // The descriptor looks like: 329293116Semaste // 330293116Semaste // uint32_t offset 331293116Semaste // uint32_t flags 332293116Semaste // 333293116Semaste // Where offset is either 0 - in which case it is unused, or 334293116Semaste // it is the offset of the vtable code from the beginning of the descriptor record. 335293116Semaste // Below, we'll convert that into an absolute code address, since I don't want to have 336293116Semaste // to compute it over and over. 337293116Semaste 338293116Semaste // Ingest the whole descriptor array: 339293116Semaste const lldb::addr_t desc_ptr = m_header_addr + header_size; 340293116Semaste const size_t desc_array_size = num_descriptors * descriptor_size; 341293116Semaste DataBufferSP data_sp(new DataBufferHeap (desc_array_size, '\0')); 342293116Semaste uint8_t* dst = (uint8_t*)data_sp->GetBytes(); 343293116Semaste 344293116Semaste DataExtractor desc_extractor (dst, desc_array_size, 345293116Semaste process_sp->GetByteOrder(), 346293116Semaste process_sp->GetAddressByteSize()); 347293116Semaste bytes_read = process_sp->ReadMemory(desc_ptr, dst, desc_array_size, error); 348293116Semaste if (bytes_read != desc_array_size) 349293116Semaste { 350293116Semaste m_valid = false; 351293116Semaste return; 352293116Semaste } 353293116Semaste 354293116Semaste // The actual code for the vtables will be laid out consecutively, so I also 355293116Semaste // compute the start and end of the whole code block. 356293116Semaste 357293116Semaste offset = 0; 358293116Semaste m_code_start_addr = 0; 359293116Semaste m_code_end_addr = 0; 360293116Semaste 361293116Semaste for (size_t i = 0; i < num_descriptors; i++) 362293116Semaste { 363293116Semaste lldb::addr_t start_offset = offset; 364293116Semaste uint32_t voffset = desc_extractor.GetU32 (&offset); 365293116Semaste uint32_t flags = desc_extractor.GetU32 (&offset); 366293116Semaste lldb::addr_t code_addr = desc_ptr + start_offset + voffset; 367293116Semaste m_descriptors.push_back (VTableDescriptor(flags, code_addr)); 368293116Semaste 369293116Semaste if (m_code_start_addr == 0 || code_addr < m_code_start_addr) 370293116Semaste m_code_start_addr = code_addr; 371293116Semaste if (code_addr > m_code_end_addr) 372293116Semaste m_code_end_addr = code_addr; 373293116Semaste 374293116Semaste offset = start_offset + descriptor_size; 375293116Semaste } 376293116Semaste // Finally, a little bird told me that all the vtable code blocks are the same size. 377293116Semaste // Let's compute the blocks and if they are all the same add the size to the code end address: 378293116Semaste lldb::addr_t code_size = 0; 379293116Semaste bool all_the_same = true; 380293116Semaste for (size_t i = 0; i < num_descriptors - 1; i++) 381293116Semaste { 382293116Semaste lldb::addr_t this_size = m_descriptors[i + 1].code_start - m_descriptors[i].code_start; 383293116Semaste if (code_size == 0) 384293116Semaste code_size = this_size; 385293116Semaste else 386293116Semaste { 387293116Semaste if (this_size != code_size) 388293116Semaste all_the_same = false; 389293116Semaste if (this_size > code_size) 390293116Semaste code_size = this_size; 391293116Semaste } 392293116Semaste } 393293116Semaste if (all_the_same) 394293116Semaste m_code_end_addr += code_size; 395293116Semaste} 396293116Semaste 397293116Semastebool 398293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::AddressInRegion (lldb::addr_t addr, uint32_t &flags) 399293116Semaste{ 400293116Semaste if (!IsValid()) 401293116Semaste return false; 402293116Semaste 403293116Semaste if (addr < m_code_start_addr || addr > m_code_end_addr) 404293116Semaste return false; 405293116Semaste 406293116Semaste std::vector<VTableDescriptor>::iterator pos, end = m_descriptors.end(); 407293116Semaste for (pos = m_descriptors.begin(); pos != end; pos++) 408293116Semaste { 409293116Semaste if (addr <= (*pos).code_start) 410293116Semaste { 411293116Semaste flags = (*pos).flags; 412293116Semaste return true; 413293116Semaste } 414293116Semaste } 415293116Semaste return false; 416293116Semaste} 417293116Semaste 418293116Semastevoid 419293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::Dump (Stream &s) 420293116Semaste{ 421293116Semaste s.Printf ("Header addr: 0x%" PRIx64 " Code start: 0x%" PRIx64 " Code End: 0x%" PRIx64 " Next: 0x%" PRIx64 "\n", 422293116Semaste m_header_addr, m_code_start_addr, m_code_end_addr, m_next_region); 423293116Semaste size_t num_elements = m_descriptors.size(); 424293116Semaste for (size_t i = 0; i < num_elements; i++) 425293116Semaste { 426293116Semaste s.Indent(); 427293116Semaste s.Printf ("Code start: 0x%" PRIx64 " Flags: %d\n", m_descriptors[i].code_start, m_descriptors[i].flags); 428293116Semaste } 429293116Semaste} 430293116Semaste 431293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::AppleObjCVTables (const ProcessSP &process_sp, 432293116Semaste const ModuleSP &objc_module_sp) : 433293116Semaste m_process_wp (), 434293116Semaste m_trampoline_header (LLDB_INVALID_ADDRESS), 435293116Semaste m_trampolines_changed_bp_id (LLDB_INVALID_BREAK_ID), 436293116Semaste m_objc_module_sp (objc_module_sp) 437293116Semaste{ 438293116Semaste if (process_sp) 439293116Semaste m_process_wp = process_sp; 440293116Semaste} 441293116Semaste 442293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::~AppleObjCVTables() 443293116Semaste{ 444293116Semaste ProcessSP process_sp = GetProcessSP (); 445293116Semaste if (process_sp) 446293116Semaste { 447293116Semaste if (m_trampolines_changed_bp_id != LLDB_INVALID_BREAK_ID) 448293116Semaste process_sp->GetTarget().RemoveBreakpointByID (m_trampolines_changed_bp_id); 449293116Semaste } 450293116Semaste} 451293116Semaste 452293116Semastebool 453293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::InitializeVTableSymbols () 454293116Semaste{ 455293116Semaste if (m_trampoline_header != LLDB_INVALID_ADDRESS) 456293116Semaste return true; 457293116Semaste 458293116Semaste ProcessSP process_sp = GetProcessSP (); 459293116Semaste if (process_sp) 460293116Semaste { 461293116Semaste Target &target = process_sp->GetTarget(); 462293116Semaste 463293116Semaste const ModuleList &target_modules = target.GetImages(); 464293116Semaste Mutex::Locker modules_locker(target_modules.GetMutex()); 465293116Semaste size_t num_modules = target_modules.GetSize(); 466293116Semaste if (!m_objc_module_sp) 467293116Semaste { 468293116Semaste for (size_t i = 0; i < num_modules; i++) 469293116Semaste { 470293116Semaste if (process_sp->GetObjCLanguageRuntime()->IsModuleObjCLibrary (target_modules.GetModuleAtIndexUnlocked(i))) 471293116Semaste { 472293116Semaste m_objc_module_sp = target_modules.GetModuleAtIndexUnlocked(i); 473293116Semaste break; 474293116Semaste } 475293116Semaste } 476293116Semaste } 477293116Semaste 478293116Semaste if (m_objc_module_sp) 479293116Semaste { 480293116Semaste ConstString trampoline_name ("gdb_objc_trampolines"); 481293116Semaste const Symbol *trampoline_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (trampoline_name, 482293116Semaste eSymbolTypeData); 483293116Semaste if (trampoline_symbol != NULL) 484293116Semaste { 485293116Semaste m_trampoline_header = trampoline_symbol->GetLoadAddress(&target); 486293116Semaste if (m_trampoline_header == LLDB_INVALID_ADDRESS) 487293116Semaste return false; 488293116Semaste 489293116Semaste // Next look up the "changed" symbol and set a breakpoint on that... 490293116Semaste ConstString changed_name ("gdb_objc_trampolines_changed"); 491293116Semaste const Symbol *changed_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (changed_name, 492293116Semaste eSymbolTypeCode); 493293116Semaste if (changed_symbol != NULL) 494293116Semaste { 495293116Semaste const Address changed_symbol_addr = changed_symbol->GetAddress(); 496293116Semaste if (!changed_symbol_addr.IsValid()) 497293116Semaste return false; 498293116Semaste 499293116Semaste lldb::addr_t changed_addr = changed_symbol_addr.GetOpcodeLoadAddress (&target); 500293116Semaste if (changed_addr != LLDB_INVALID_ADDRESS) 501293116Semaste { 502293116Semaste BreakpointSP trampolines_changed_bp_sp = target.CreateBreakpoint (changed_addr, true, false); 503293116Semaste if (trampolines_changed_bp_sp) 504293116Semaste { 505293116Semaste m_trampolines_changed_bp_id = trampolines_changed_bp_sp->GetID(); 506293116Semaste trampolines_changed_bp_sp->SetCallback (RefreshTrampolines, this, true); 507293116Semaste trampolines_changed_bp_sp->SetBreakpointKind ("objc-trampolines-changed"); 508293116Semaste return true; 509293116Semaste } 510293116Semaste } 511293116Semaste } 512293116Semaste } 513293116Semaste } 514293116Semaste } 515293116Semaste return false; 516293116Semaste} 517293116Semaste 518293116Semastebool 519293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::RefreshTrampolines (void *baton, 520293116Semaste StoppointCallbackContext *context, 521293116Semaste lldb::user_id_t break_id, 522293116Semaste lldb::user_id_t break_loc_id) 523293116Semaste{ 524293116Semaste AppleObjCVTables *vtable_handler = (AppleObjCVTables *) baton; 525293116Semaste if (vtable_handler->InitializeVTableSymbols()) 526293116Semaste { 527293116Semaste // The Update function is called with the address of an added region. So we grab that address, and 528293116Semaste // feed it into ReadRegions. Of course, our friend the ABI will get the values for us. 529293116Semaste ExecutionContext exe_ctx (context->exe_ctx_ref); 530293116Semaste Process *process = exe_ctx.GetProcessPtr(); 531293116Semaste const ABI *abi = process->GetABI().get(); 532293116Semaste 533293116Semaste ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext(); 534293116Semaste ValueList argument_values; 535293116Semaste Value input_value; 536293116Semaste CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 537293116Semaste 538293116Semaste input_value.SetValueType (Value::eValueTypeScalar); 539293116Semaste //input_value.SetContext (Value::eContextTypeClangType, clang_void_ptr_type); 540293116Semaste input_value.SetCompilerType (clang_void_ptr_type); 541293116Semaste argument_values.PushValue(input_value); 542293116Semaste 543293116Semaste bool success = abi->GetArgumentValues (exe_ctx.GetThreadRef(), argument_values); 544293116Semaste if (!success) 545293116Semaste return false; 546293116Semaste 547293116Semaste // Now get a pointer value from the zeroth argument. 548293116Semaste Error error; 549293116Semaste DataExtractor data; 550293116Semaste error = argument_values.GetValueAtIndex(0)->GetValueAsData (&exe_ctx, 551293116Semaste data, 552293116Semaste 0, 553293116Semaste NULL); 554293116Semaste lldb::offset_t offset = 0; 555293116Semaste lldb::addr_t region_addr = data.GetPointer(&offset); 556293116Semaste 557293116Semaste if (region_addr != 0) 558293116Semaste vtable_handler->ReadRegions(region_addr); 559293116Semaste } 560293116Semaste return false; 561293116Semaste} 562293116Semaste 563293116Semastebool 564293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions () 565293116Semaste{ 566293116Semaste // The no argument version reads the start region from the value of the gdb_regions_header, and 567293116Semaste // gets started from there. 568293116Semaste 569293116Semaste m_regions.clear(); 570293116Semaste if (!InitializeVTableSymbols()) 571293116Semaste return false; 572293116Semaste Error error; 573293116Semaste ProcessSP process_sp = GetProcessSP (); 574293116Semaste if (process_sp) 575293116Semaste { 576293116Semaste lldb::addr_t region_addr = process_sp->ReadPointerFromMemory (m_trampoline_header, error); 577293116Semaste if (error.Success()) 578293116Semaste return ReadRegions (region_addr); 579293116Semaste } 580293116Semaste return false; 581293116Semaste} 582293116Semaste 583293116Semastebool 584293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions (lldb::addr_t region_addr) 585293116Semaste{ 586293116Semaste ProcessSP process_sp = GetProcessSP (); 587293116Semaste if (!process_sp) 588293116Semaste return false; 589293116Semaste 590293116Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 591293116Semaste 592293116Semaste // We aren't starting at the trampoline symbol. 593293116Semaste InitializeVTableSymbols (); 594293116Semaste lldb::addr_t next_region = region_addr; 595293116Semaste 596293116Semaste // Read in the sizes of the headers. 597293116Semaste while (next_region != 0) 598293116Semaste { 599293116Semaste m_regions.push_back (VTableRegion(this, next_region)); 600293116Semaste if (!m_regions.back().IsValid()) 601293116Semaste { 602293116Semaste m_regions.clear(); 603293116Semaste return false; 604293116Semaste } 605293116Semaste if (log) 606293116Semaste { 607293116Semaste StreamString s; 608293116Semaste m_regions.back().Dump(s); 609293116Semaste log->Printf("Read vtable region: \n%s", s.GetData()); 610293116Semaste } 611293116Semaste 612293116Semaste next_region = m_regions.back().GetNextRegionAddr(); 613293116Semaste } 614293116Semaste 615293116Semaste return true; 616293116Semaste} 617293116Semaste 618293116Semastebool 619293116SemasteAppleObjCTrampolineHandler::AppleObjCVTables::IsAddressInVTables (lldb::addr_t addr, uint32_t &flags) 620293116Semaste{ 621293116Semaste region_collection::iterator pos, end = m_regions.end(); 622293116Semaste for (pos = m_regions.begin(); pos != end; pos++) 623293116Semaste { 624293116Semaste if ((*pos).AddressInRegion (addr, flags)) 625293116Semaste return true; 626293116Semaste } 627293116Semaste return false; 628293116Semaste} 629293116Semaste 630293116Semasteconst AppleObjCTrampolineHandler::DispatchFunction 631293116SemasteAppleObjCTrampolineHandler::g_dispatch_functions[] = 632293116Semaste{ 633293116Semaste // NAME STRET SUPER SUPER2 FIXUP TYPE 634293116Semaste {"objc_msgSend", false, false, false, DispatchFunction::eFixUpNone }, 635293116Semaste {"objc_msgSend_fixup", false, false, false, DispatchFunction::eFixUpToFix }, 636293116Semaste {"objc_msgSend_fixedup", false, false, false, DispatchFunction::eFixUpFixed }, 637293116Semaste {"objc_msgSend_stret", true, false, false, DispatchFunction::eFixUpNone }, 638293116Semaste {"objc_msgSend_stret_fixup", true, false, false, DispatchFunction::eFixUpToFix }, 639293116Semaste {"objc_msgSend_stret_fixedup", true, false, false, DispatchFunction::eFixUpFixed }, 640293116Semaste {"objc_msgSend_fpret", false, false, false, DispatchFunction::eFixUpNone }, 641293116Semaste {"objc_msgSend_fpret_fixup", false, false, false, DispatchFunction::eFixUpToFix }, 642293116Semaste {"objc_msgSend_fpret_fixedup", false, false, false, DispatchFunction::eFixUpFixed }, 643293116Semaste {"objc_msgSend_fp2ret", false, false, true, DispatchFunction::eFixUpNone }, 644293116Semaste {"objc_msgSend_fp2ret_fixup", false, false, true, DispatchFunction::eFixUpToFix }, 645293116Semaste {"objc_msgSend_fp2ret_fixedup", false, false, true, DispatchFunction::eFixUpFixed }, 646293116Semaste {"objc_msgSendSuper", false, true, false, DispatchFunction::eFixUpNone }, 647293116Semaste {"objc_msgSendSuper_stret", true, true, false, DispatchFunction::eFixUpNone }, 648293116Semaste {"objc_msgSendSuper2", false, true, true, DispatchFunction::eFixUpNone }, 649293116Semaste {"objc_msgSendSuper2_fixup", false, true, true, DispatchFunction::eFixUpToFix }, 650293116Semaste {"objc_msgSendSuper2_fixedup", false, true, true, DispatchFunction::eFixUpFixed }, 651293116Semaste {"objc_msgSendSuper2_stret", true, true, true, DispatchFunction::eFixUpNone }, 652293116Semaste {"objc_msgSendSuper2_stret_fixup", true, true, true, DispatchFunction::eFixUpToFix }, 653293116Semaste {"objc_msgSendSuper2_stret_fixedup", true, true, true, DispatchFunction::eFixUpFixed }, 654293116Semaste}; 655293116Semaste 656293116SemasteAppleObjCTrampolineHandler::AppleObjCTrampolineHandler (const ProcessSP &process_sp, 657293116Semaste const ModuleSP &objc_module_sp) : 658293116Semaste m_process_wp (), 659293116Semaste m_objc_module_sp (objc_module_sp), 660293116Semaste m_impl_fn_addr (LLDB_INVALID_ADDRESS), 661293116Semaste m_impl_stret_fn_addr (LLDB_INVALID_ADDRESS), 662293116Semaste m_msg_forward_addr (LLDB_INVALID_ADDRESS) 663293116Semaste{ 664293116Semaste if (process_sp) 665293116Semaste m_process_wp = process_sp; 666293116Semaste // Look up the known resolution functions: 667293116Semaste 668293116Semaste ConstString get_impl_name("class_getMethodImplementation"); 669293116Semaste ConstString get_impl_stret_name("class_getMethodImplementation_stret"); 670293116Semaste ConstString msg_forward_name("_objc_msgForward"); 671293116Semaste ConstString msg_forward_stret_name("_objc_msgForward_stret"); 672293116Semaste 673293116Semaste Target *target = process_sp ? &process_sp->GetTarget() : NULL; 674293116Semaste const Symbol *class_getMethodImplementation = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_name, eSymbolTypeCode); 675293116Semaste const Symbol *class_getMethodImplementation_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_stret_name, eSymbolTypeCode); 676293116Semaste const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType (msg_forward_name, eSymbolTypeCode); 677293116Semaste const Symbol *msg_forward_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (msg_forward_stret_name, eSymbolTypeCode); 678293116Semaste 679293116Semaste if (class_getMethodImplementation) 680293116Semaste m_impl_fn_addr = class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress (target); 681293116Semaste if (class_getMethodImplementation_stret) 682293116Semaste m_impl_stret_fn_addr = class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress (target); 683293116Semaste if (msg_forward) 684293116Semaste m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target); 685293116Semaste if (msg_forward_stret) 686293116Semaste m_msg_forward_stret_addr = msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target); 687293116Semaste 688293116Semaste // FIXME: Do some kind of logging here. 689293116Semaste if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) 690293116Semaste { 691293116Semaste // If we can't even find the ordinary get method implementation function, then we aren't going to be able to 692293116Semaste // step through any method dispatches. Warn to that effect and get out of here. 693293116Semaste if (process_sp->CanJIT()) 694293116Semaste { 695293116Semaste process_sp->GetTarget().GetDebugger().GetErrorFile()->Printf ("Could not find implementation lookup function \"%s\"" 696293116Semaste " step in through ObjC method dispatch will not work.\n", 697293116Semaste get_impl_name.AsCString()); 698293116Semaste } 699293116Semaste return; 700293116Semaste } 701293116Semaste else if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) 702293116Semaste { 703293116Semaste // It there is no stret return lookup function, assume that it is the same as the straight lookup: 704293116Semaste m_impl_stret_fn_addr = m_impl_fn_addr; 705293116Semaste // Also we will use the version of the lookup code that doesn't rely on the stret version of the function. 706293116Semaste g_lookup_implementation_function_code = g_lookup_implementation_no_stret_function_code; 707293116Semaste } 708293116Semaste else 709293116Semaste { 710293116Semaste g_lookup_implementation_function_code = g_lookup_implementation_with_stret_function_code; 711293116Semaste } 712293116Semaste 713293116Semaste // Look up the addresses for the objc dispatch functions and cache them. For now I'm inspecting the symbol 714293116Semaste // names dynamically to figure out how to dispatch to them. If it becomes more complicated than this we can 715293116Semaste // turn the g_dispatch_functions char * array into a template table, and populate the DispatchFunction map 716293116Semaste // from there. 717293116Semaste 718293116Semaste for (size_t i = 0; i != llvm::array_lengthof(g_dispatch_functions); i++) 719293116Semaste { 720293116Semaste ConstString name_const_str(g_dispatch_functions[i].name); 721293116Semaste const Symbol *msgSend_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (name_const_str, eSymbolTypeCode); 722293116Semaste if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) 723293116Semaste { 724293116Semaste // FixMe: Make g_dispatch_functions static table of DispatchFunctions, and have the map be address->index. 725293116Semaste // Problem is we also need to lookup the dispatch function. For now we could have a side table of stret & non-stret 726293116Semaste // dispatch functions. If that's as complex as it gets, we're fine. 727293116Semaste 728293116Semaste lldb::addr_t sym_addr = msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); 729293116Semaste 730293116Semaste m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); 731293116Semaste } 732293116Semaste } 733293116Semaste 734293116Semaste // Build our vtable dispatch handler here: 735293116Semaste m_vtables_ap.reset(new AppleObjCVTables(process_sp, m_objc_module_sp)); 736293116Semaste if (m_vtables_ap.get()) 737293116Semaste m_vtables_ap->ReadRegions(); 738293116Semaste} 739293116Semaste 740293116Semastelldb::addr_t 741293116SemasteAppleObjCTrampolineHandler::SetupDispatchFunction (Thread &thread, ValueList &dispatch_values) 742293116Semaste{ 743293116Semaste ExecutionContext exe_ctx (thread.shared_from_this()); 744293116Semaste StreamString errors; 745293116Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 746293116Semaste lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; 747293116Semaste FunctionCaller *impl_function_caller = nullptr; 748293116Semaste 749293116Semaste // Scope for mutex locker: 750293116Semaste { 751293116Semaste Mutex::Locker locker(m_impl_function_mutex); 752293116Semaste 753293116Semaste // First stage is to make the ClangUtility to hold our injected function: 754293116Semaste 755293116Semaste if (!m_impl_code.get()) 756293116Semaste { 757293116Semaste if (g_lookup_implementation_function_code != NULL) 758293116Semaste { 759293116Semaste Error error; 760293116Semaste m_impl_code.reset (exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage (g_lookup_implementation_function_code, 761293116Semaste eLanguageTypeObjC, 762293116Semaste g_lookup_implementation_function_name, 763293116Semaste error)); 764293116Semaste if (error.Fail()) 765293116Semaste { 766293116Semaste if (log) 767293116Semaste log->Printf ("Failed to get Utility Function for implementation lookup: %s.", error.AsCString()); 768293116Semaste m_impl_code.reset(); 769293116Semaste return args_addr; 770293116Semaste } 771293116Semaste 772293116Semaste if (!m_impl_code->Install(errors, exe_ctx)) 773293116Semaste { 774293116Semaste if (log) 775293116Semaste log->Printf ("Failed to install implementation lookup: %s.", errors.GetData()); 776293116Semaste m_impl_code.reset(); 777293116Semaste return args_addr; 778293116Semaste } 779293116Semaste } 780293116Semaste else 781293116Semaste { 782293116Semaste if (log) 783293116Semaste log->Printf("No method lookup implementation code."); 784293116Semaste errors.Printf ("No method lookup implementation code found."); 785293116Semaste return LLDB_INVALID_ADDRESS; 786293116Semaste } 787293116Semaste 788293116Semaste 789293116Semaste // Next make the runner function for our implementation utility function. 790293116Semaste ClangASTContext *clang_ast_context = thread.GetProcess()->GetTarget().GetScratchClangASTContext(); 791293116Semaste CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 792293116Semaste Error error; 793293116Semaste 794293116Semaste impl_function_caller = m_impl_code->MakeFunctionCaller(clang_void_ptr_type, 795293116Semaste dispatch_values, 796293116Semaste error); 797293116Semaste if (error.Fail()) 798293116Semaste { 799293116Semaste if (log) 800293116Semaste log->Printf ("Error getting function caller for dispatch lookup: \"%s\".", error.AsCString()); 801293116Semaste return args_addr; 802293116Semaste } 803293116Semaste } 804293116Semaste else 805293116Semaste { 806293116Semaste impl_function_caller = m_impl_code->GetFunctionCaller(); 807293116Semaste } 808293116Semaste } 809293116Semaste 810293116Semaste errors.Clear(); 811293116Semaste 812293116Semaste // Now write down the argument values for this particular call. This looks like it might be a race condition 813293116Semaste // if other threads were calling into here, but actually it isn't because we allocate a new args structure for 814293116Semaste // this call by passing args_addr = LLDB_INVALID_ADDRESS... 815293116Semaste 816293116Semaste if (impl_function_caller->WriteFunctionArguments (exe_ctx, args_addr, dispatch_values, errors)) 817293116Semaste { 818293116Semaste if (log) 819293116Semaste log->Printf ("Error writing function arguments: \"%s\".", errors.GetData()); 820293116Semaste return args_addr; 821293116Semaste } 822293116Semaste 823293116Semaste return args_addr; 824293116Semaste} 825293116Semaste 826293116SemasteThreadPlanSP 827293116SemasteAppleObjCTrampolineHandler::GetStepThroughDispatchPlan (Thread &thread, bool stop_others) 828293116Semaste{ 829293116Semaste ThreadPlanSP ret_plan_sp; 830293116Semaste lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); 831293116Semaste 832293116Semaste DispatchFunction this_dispatch; 833293116Semaste bool found_it = false; 834293116Semaste 835293116Semaste // First step is to look and see if we are in one of the known ObjC dispatch functions. We've already compiled 836293116Semaste // a table of same, so consult it. 837293116Semaste 838293116Semaste MsgsendMap::iterator pos; 839293116Semaste pos = m_msgSend_map.find (curr_pc); 840293116Semaste if (pos != m_msgSend_map.end()) 841293116Semaste { 842293116Semaste this_dispatch = g_dispatch_functions[(*pos).second]; 843293116Semaste found_it = true; 844293116Semaste } 845293116Semaste 846293116Semaste // Next check to see if we are in a vtable region: 847293116Semaste 848293116Semaste if (!found_it) 849293116Semaste { 850293116Semaste uint32_t flags; 851293116Semaste if (m_vtables_ap.get()) 852293116Semaste { 853293116Semaste found_it = m_vtables_ap->IsAddressInVTables (curr_pc, flags); 854293116Semaste if (found_it) 855293116Semaste { 856293116Semaste this_dispatch.name = "vtable"; 857293116Semaste this_dispatch.stret_return 858293116Semaste = (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) == AppleObjCVTables::eOBJC_TRAMPOLINE_STRET; 859293116Semaste this_dispatch.is_super = false; 860293116Semaste this_dispatch.is_super2 = false; 861293116Semaste this_dispatch.fixedup = DispatchFunction::eFixUpFixed; 862293116Semaste } 863293116Semaste } 864293116Semaste } 865293116Semaste 866293116Semaste if (found_it) 867293116Semaste { 868293116Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 869293116Semaste 870293116Semaste // We are decoding a method dispatch. 871293116Semaste // First job is to pull the arguments out: 872293116Semaste 873293116Semaste lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); 874293116Semaste 875293116Semaste const ABI *abi = NULL; 876293116Semaste ProcessSP process_sp (thread.CalculateProcess()); 877293116Semaste if (process_sp) 878293116Semaste abi = process_sp->GetABI().get(); 879293116Semaste if (abi == NULL) 880293116Semaste return ret_plan_sp; 881293116Semaste 882293116Semaste TargetSP target_sp (thread.CalculateTarget()); 883293116Semaste 884293116Semaste ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext(); 885293116Semaste ValueList argument_values; 886293116Semaste Value void_ptr_value; 887293116Semaste CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 888293116Semaste void_ptr_value.SetValueType (Value::eValueTypeScalar); 889293116Semaste //void_ptr_value.SetContext (Value::eContextTypeClangType, clang_void_ptr_type); 890293116Semaste void_ptr_value.SetCompilerType (clang_void_ptr_type); 891293116Semaste 892293116Semaste int obj_index; 893293116Semaste int sel_index; 894293116Semaste 895293116Semaste // If this is a struct return dispatch, then the first argument is the 896293116Semaste // return struct pointer, and the object is the second, and the selector is the third. 897293116Semaste // Otherwise the object is the first and the selector the second. 898293116Semaste if (this_dispatch.stret_return) 899293116Semaste { 900293116Semaste obj_index = 1; 901293116Semaste sel_index = 2; 902293116Semaste argument_values.PushValue(void_ptr_value); 903293116Semaste argument_values.PushValue(void_ptr_value); 904293116Semaste argument_values.PushValue(void_ptr_value); 905293116Semaste } 906293116Semaste else 907293116Semaste { 908293116Semaste obj_index = 0; 909293116Semaste sel_index = 1; 910293116Semaste argument_values.PushValue(void_ptr_value); 911293116Semaste argument_values.PushValue(void_ptr_value); 912293116Semaste } 913293116Semaste 914293116Semaste 915293116Semaste bool success = abi->GetArgumentValues (thread, argument_values); 916293116Semaste if (!success) 917293116Semaste return ret_plan_sp; 918293116Semaste 919293116Semaste lldb::addr_t obj_addr = argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong(); 920293116Semaste if (obj_addr == 0x0) 921293116Semaste { 922293116Semaste if (log) 923293116Semaste log->Printf("Asked to step to dispatch to nil object, returning empty plan."); 924293116Semaste return ret_plan_sp; 925293116Semaste } 926293116Semaste 927293116Semaste ExecutionContext exe_ctx (thread.shared_from_this()); 928293116Semaste Process *process = exe_ctx.GetProcessPtr(); 929293116Semaste // isa_addr will store the class pointer that the method is being dispatched to - so either the class 930293116Semaste // directly or the super class if this is one of the objc_msgSendSuper flavors. That's mostly used to 931293116Semaste // look up the class/selector pair in our cache. 932293116Semaste 933293116Semaste lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS; 934293116Semaste lldb::addr_t sel_addr = argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong(); 935293116Semaste 936293116Semaste // Figure out the class this is being dispatched to and see if we've already cached this method call, 937293116Semaste // If so we can push a run-to-address plan directly. Otherwise we have to figure out where 938293116Semaste // the implementation lives. 939293116Semaste 940293116Semaste if (this_dispatch.is_super) 941293116Semaste { 942293116Semaste if (this_dispatch.is_super2) 943293116Semaste { 944293116Semaste // In the objc_msgSendSuper2 case, we don't get the object directly, we get a structure containing 945293116Semaste // the object and the class to which the super message is being sent. So we need to dig the super 946293116Semaste // out of the class and use that. 947293116Semaste 948293116Semaste Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 949293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 950293116Semaste super_value.ResolveValue (&exe_ctx); 951293116Semaste 952293116Semaste if (super_value.GetScalar().IsValid()) 953293116Semaste { 954293116Semaste 955293116Semaste // isa_value now holds the class pointer. The second word of the class pointer is the super-class pointer: 956293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 957293116Semaste super_value.ResolveValue (&exe_ctx); 958293116Semaste if (super_value.GetScalar().IsValid()) 959293116Semaste isa_addr = super_value.GetScalar().ULongLong(); 960293116Semaste else 961293116Semaste { 962293116Semaste if (log) 963293116Semaste log->Printf("Failed to extract the super class value from the class in objc_super."); 964293116Semaste } 965293116Semaste } 966293116Semaste else 967293116Semaste { 968293116Semaste if (log) 969293116Semaste log->Printf("Failed to extract the class value from objc_super."); 970293116Semaste } 971293116Semaste } 972293116Semaste else 973293116Semaste { 974293116Semaste // In the objc_msgSendSuper case, we don't get the object directly, we get a two element structure containing 975293116Semaste // the object and the super class to which the super message is being sent. So the class we want is 976293116Semaste // the second element of this structure. 977293116Semaste 978293116Semaste Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 979293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 980293116Semaste super_value.ResolveValue (&exe_ctx); 981293116Semaste 982293116Semaste if (super_value.GetScalar().IsValid()) 983293116Semaste { 984293116Semaste isa_addr = super_value.GetScalar().ULongLong(); 985293116Semaste } 986293116Semaste else 987293116Semaste { 988293116Semaste if (log) 989293116Semaste log->Printf("Failed to extract the class value from objc_super."); 990293116Semaste } 991293116Semaste } 992293116Semaste } 993293116Semaste else 994293116Semaste { 995293116Semaste // In the direct dispatch case, the object->isa is the class pointer we want. 996293116Semaste 997293116Semaste // This is a little cheesy, but since object->isa is the first field, 998293116Semaste // making the object value a load address value and resolving it will get 999293116Semaste // the pointer sized data pointed to by that value... 1000293116Semaste 1001293116Semaste // Note, it isn't a fatal error not to be able to get the address from the object, since this might 1002293116Semaste // be a "tagged pointer" which isn't a real object, but rather some word length encoded dingus. 1003293116Semaste 1004293116Semaste Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); 1005293116Semaste 1006293116Semaste isa_value.SetValueType(Value::eValueTypeLoadAddress); 1007293116Semaste isa_value.ResolveValue(&exe_ctx); 1008293116Semaste if (isa_value.GetScalar().IsValid()) 1009293116Semaste { 1010293116Semaste isa_addr = isa_value.GetScalar().ULongLong(); 1011293116Semaste } 1012293116Semaste else 1013293116Semaste { 1014293116Semaste if (log) 1015293116Semaste log->Printf("Failed to extract the isa value from object."); 1016293116Semaste } 1017293116Semaste 1018293116Semaste } 1019293116Semaste 1020293116Semaste // Okay, we've got the address of the class for which we're resolving this, let's see if it's in our cache: 1021293116Semaste lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS; 1022293116Semaste 1023293116Semaste if (isa_addr != LLDB_INVALID_ADDRESS) 1024293116Semaste { 1025293116Semaste if (log) 1026293116Semaste { 1027293116Semaste log->Printf("Resolving call for class - 0x%" PRIx64 " and selector - 0x%" PRIx64, 1028293116Semaste isa_addr, sel_addr); 1029293116Semaste } 1030293116Semaste ObjCLanguageRuntime *objc_runtime = thread.GetProcess()->GetObjCLanguageRuntime (); 1031293116Semaste assert(objc_runtime != NULL); 1032293116Semaste 1033293116Semaste impl_addr = objc_runtime->LookupInMethodCache (isa_addr, sel_addr); 1034293116Semaste } 1035293116Semaste 1036293116Semaste if (impl_addr != LLDB_INVALID_ADDRESS) 1037293116Semaste { 1038293116Semaste // Yup, it was in the cache, so we can run to that address directly. 1039293116Semaste 1040293116Semaste if (log) 1041293116Semaste log->Printf ("Found implementation address in cache: 0x%" PRIx64, impl_addr); 1042293116Semaste 1043293116Semaste ret_plan_sp.reset (new ThreadPlanRunToAddress (thread, impl_addr, stop_others)); 1044293116Semaste } 1045293116Semaste else 1046293116Semaste { 1047293116Semaste // We haven't seen this class/selector pair yet. Look it up. 1048293116Semaste StreamString errors; 1049293116Semaste Address impl_code_address; 1050293116Semaste 1051293116Semaste ValueList dispatch_values; 1052293116Semaste 1053293116Semaste // We've will inject a little function in the target that takes the object, selector and some flags, 1054293116Semaste // and figures out the implementation. Looks like: 1055293116Semaste // void *__lldb_objc_find_implementation_for_selector (void *object, 1056293116Semaste // void *sel, 1057293116Semaste // int is_stret, 1058293116Semaste // int is_super, 1059293116Semaste // int is_super2, 1060293116Semaste // int is_fixup, 1061293116Semaste // int is_fixed, 1062293116Semaste // int debug) 1063293116Semaste // So set up the arguments for that call. 1064293116Semaste 1065293116Semaste dispatch_values.PushValue (*(argument_values.GetValueAtIndex(obj_index))); 1066293116Semaste dispatch_values.PushValue (*(argument_values.GetValueAtIndex(sel_index))); 1067293116Semaste 1068293116Semaste Value flag_value; 1069293116Semaste CompilerType clang_int_type = clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingSint, 32); 1070293116Semaste flag_value.SetValueType (Value::eValueTypeScalar); 1071293116Semaste //flag_value.SetContext (Value::eContextTypeClangType, clang_int_type); 1072293116Semaste flag_value.SetCompilerType (clang_int_type); 1073293116Semaste 1074293116Semaste if (this_dispatch.stret_return) 1075293116Semaste flag_value.GetScalar() = 1; 1076293116Semaste else 1077293116Semaste flag_value.GetScalar() = 0; 1078293116Semaste dispatch_values.PushValue (flag_value); 1079293116Semaste 1080293116Semaste if (this_dispatch.is_super) 1081293116Semaste flag_value.GetScalar() = 1; 1082293116Semaste else 1083293116Semaste flag_value.GetScalar() = 0; 1084293116Semaste dispatch_values.PushValue (flag_value); 1085293116Semaste 1086293116Semaste if (this_dispatch.is_super2) 1087293116Semaste flag_value.GetScalar() = 1; 1088293116Semaste else 1089293116Semaste flag_value.GetScalar() = 0; 1090293116Semaste dispatch_values.PushValue (flag_value); 1091293116Semaste 1092293116Semaste switch (this_dispatch.fixedup) 1093293116Semaste { 1094293116Semaste case DispatchFunction::eFixUpNone: 1095293116Semaste flag_value.GetScalar() = 0; 1096293116Semaste dispatch_values.PushValue (flag_value); 1097293116Semaste dispatch_values.PushValue (flag_value); 1098293116Semaste break; 1099293116Semaste case DispatchFunction::eFixUpFixed: 1100293116Semaste flag_value.GetScalar() = 1; 1101293116Semaste dispatch_values.PushValue (flag_value); 1102293116Semaste flag_value.GetScalar() = 1; 1103293116Semaste dispatch_values.PushValue (flag_value); 1104293116Semaste break; 1105293116Semaste case DispatchFunction::eFixUpToFix: 1106293116Semaste flag_value.GetScalar() = 1; 1107293116Semaste dispatch_values.PushValue (flag_value); 1108293116Semaste flag_value.GetScalar() = 0; 1109293116Semaste dispatch_values.PushValue (flag_value); 1110293116Semaste break; 1111293116Semaste } 1112293116Semaste if (log && log->GetVerbose()) 1113293116Semaste flag_value.GetScalar() = 1; 1114293116Semaste else 1115293116Semaste flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done. 1116293116Semaste dispatch_values.PushValue (flag_value); 1117293116Semaste 1118293116Semaste 1119293116Semaste // The step through code might have to fill in the cache, so it is not safe to run only one thread. 1120293116Semaste // So we override the stop_others value passed in to us here: 1121293116Semaste const bool trampoline_stop_others = false; 1122293116Semaste ret_plan_sp.reset (new AppleThreadPlanStepThroughObjCTrampoline (thread, 1123293116Semaste this, 1124293116Semaste dispatch_values, 1125293116Semaste isa_addr, 1126293116Semaste sel_addr, 1127293116Semaste trampoline_stop_others)); 1128293116Semaste if (log) 1129293116Semaste { 1130293116Semaste StreamString s; 1131293116Semaste ret_plan_sp->GetDescription(&s, eDescriptionLevelFull); 1132293116Semaste log->Printf("Using ObjC step plan: %s.\n", s.GetData()); 1133293116Semaste } 1134293116Semaste } 1135293116Semaste } 1136293116Semaste 1137293116Semaste return ret_plan_sp; 1138293116Semaste} 1139293116Semaste 1140293116SemasteFunctionCaller * 1141293116SemasteAppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller () 1142293116Semaste{ 1143293116Semaste return m_impl_code->GetFunctionCaller(); 1144293116Semaste} 1145