AppleObjCTrampolineHandler.cpp revision 309124
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" 25309124Sdim#include "lldb/Expression/DiagnosticManager.h" 26309124Sdim#include "lldb/Expression/FunctionCaller.h" 27293116Semaste#include "lldb/Expression/UserExpression.h" 28293116Semaste#include "lldb/Expression/UtilityFunction.h" 29293116Semaste#include "lldb/Host/FileSpec.h" 30293116Semaste#include "lldb/Symbol/ClangASTContext.h" 31293116Semaste#include "lldb/Symbol/Symbol.h" 32293116Semaste#include "lldb/Target/ABI.h" 33309124Sdim#include "lldb/Target/ExecutionContext.h" 34293116Semaste#include "lldb/Target/ObjCLanguageRuntime.h" 35293116Semaste#include "lldb/Target/Process.h" 36293116Semaste#include "lldb/Target/RegisterContext.h" 37293116Semaste#include "lldb/Target/Target.h" 38293116Semaste#include "lldb/Target/Thread.h" 39293116Semaste#include "lldb/Target/ThreadPlanRunToAddress.h" 40293116Semaste 41293116Semaste#include "llvm/ADT/STLExtras.h" 42293116Semaste 43293116Semasteusing namespace lldb; 44293116Semasteusing namespace lldb_private; 45293116Semaste 46293116Semasteconst char *AppleObjCTrampolineHandler::g_lookup_implementation_function_name = "__lldb_objc_find_implementation_for_selector"; 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(); 464309124Sdim std::lock_guard<std::recursive_mutex> guard(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), 660309124Sdim m_lookup_implementation_function_code(nullptr), 661293116Semaste m_impl_fn_addr (LLDB_INVALID_ADDRESS), 662293116Semaste m_impl_stret_fn_addr (LLDB_INVALID_ADDRESS), 663293116Semaste m_msg_forward_addr (LLDB_INVALID_ADDRESS) 664293116Semaste{ 665293116Semaste if (process_sp) 666293116Semaste m_process_wp = process_sp; 667293116Semaste // Look up the known resolution functions: 668293116Semaste 669293116Semaste ConstString get_impl_name("class_getMethodImplementation"); 670293116Semaste ConstString get_impl_stret_name("class_getMethodImplementation_stret"); 671293116Semaste ConstString msg_forward_name("_objc_msgForward"); 672293116Semaste ConstString msg_forward_stret_name("_objc_msgForward_stret"); 673293116Semaste 674293116Semaste Target *target = process_sp ? &process_sp->GetTarget() : NULL; 675293116Semaste const Symbol *class_getMethodImplementation = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_name, eSymbolTypeCode); 676293116Semaste const Symbol *class_getMethodImplementation_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_stret_name, eSymbolTypeCode); 677293116Semaste const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType (msg_forward_name, eSymbolTypeCode); 678293116Semaste const Symbol *msg_forward_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (msg_forward_stret_name, eSymbolTypeCode); 679293116Semaste 680293116Semaste if (class_getMethodImplementation) 681293116Semaste m_impl_fn_addr = class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress (target); 682293116Semaste if (class_getMethodImplementation_stret) 683293116Semaste m_impl_stret_fn_addr = class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress (target); 684293116Semaste if (msg_forward) 685293116Semaste m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target); 686293116Semaste if (msg_forward_stret) 687293116Semaste m_msg_forward_stret_addr = msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target); 688293116Semaste 689293116Semaste // FIXME: Do some kind of logging here. 690293116Semaste if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) 691293116Semaste { 692293116Semaste // If we can't even find the ordinary get method implementation function, then we aren't going to be able to 693293116Semaste // step through any method dispatches. Warn to that effect and get out of here. 694293116Semaste if (process_sp->CanJIT()) 695293116Semaste { 696293116Semaste process_sp->GetTarget().GetDebugger().GetErrorFile()->Printf ("Could not find implementation lookup function \"%s\"" 697293116Semaste " step in through ObjC method dispatch will not work.\n", 698293116Semaste get_impl_name.AsCString()); 699293116Semaste } 700293116Semaste return; 701293116Semaste } 702293116Semaste else if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) 703293116Semaste { 704293116Semaste // It there is no stret return lookup function, assume that it is the same as the straight lookup: 705293116Semaste m_impl_stret_fn_addr = m_impl_fn_addr; 706293116Semaste // Also we will use the version of the lookup code that doesn't rely on the stret version of the function. 707309124Sdim m_lookup_implementation_function_code = g_lookup_implementation_no_stret_function_code; 708293116Semaste } 709293116Semaste else 710293116Semaste { 711309124Sdim m_lookup_implementation_function_code = g_lookup_implementation_with_stret_function_code; 712293116Semaste } 713293116Semaste 714293116Semaste // Look up the addresses for the objc dispatch functions and cache them. For now I'm inspecting the symbol 715293116Semaste // names dynamically to figure out how to dispatch to them. If it becomes more complicated than this we can 716293116Semaste // turn the g_dispatch_functions char * array into a template table, and populate the DispatchFunction map 717293116Semaste // from there. 718293116Semaste 719293116Semaste for (size_t i = 0; i != llvm::array_lengthof(g_dispatch_functions); i++) 720293116Semaste { 721293116Semaste ConstString name_const_str(g_dispatch_functions[i].name); 722293116Semaste const Symbol *msgSend_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (name_const_str, eSymbolTypeCode); 723293116Semaste if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) 724293116Semaste { 725293116Semaste // FixMe: Make g_dispatch_functions static table of DispatchFunctions, and have the map be address->index. 726293116Semaste // Problem is we also need to lookup the dispatch function. For now we could have a side table of stret & non-stret 727293116Semaste // dispatch functions. If that's as complex as it gets, we're fine. 728293116Semaste 729293116Semaste lldb::addr_t sym_addr = msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); 730293116Semaste 731293116Semaste m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); 732293116Semaste } 733293116Semaste } 734293116Semaste 735293116Semaste // Build our vtable dispatch handler here: 736293116Semaste m_vtables_ap.reset(new AppleObjCVTables(process_sp, m_objc_module_sp)); 737293116Semaste if (m_vtables_ap.get()) 738293116Semaste m_vtables_ap->ReadRegions(); 739293116Semaste} 740293116Semaste 741293116Semastelldb::addr_t 742309124SdimAppleObjCTrampolineHandler::SetupDispatchFunction(Thread &thread, ValueList &dispatch_values) 743293116Semaste{ 744309124Sdim ThreadSP thread_sp(thread.shared_from_this()); 745309124Sdim ExecutionContext exe_ctx (thread_sp); 746309124Sdim DiagnosticManager diagnostics; 747309124Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 748309124Sdim 749293116Semaste lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; 750293116Semaste FunctionCaller *impl_function_caller = nullptr; 751293116Semaste 752293116Semaste // Scope for mutex locker: 753293116Semaste { 754309124Sdim std::lock_guard<std::mutex> guard(m_impl_function_mutex); 755309124Sdim 756293116Semaste // First stage is to make the ClangUtility to hold our injected function: 757293116Semaste 758293116Semaste if (!m_impl_code.get()) 759293116Semaste { 760309124Sdim if (m_lookup_implementation_function_code != NULL) 761293116Semaste { 762293116Semaste Error error; 763309124Sdim m_impl_code.reset (exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage (m_lookup_implementation_function_code, 764293116Semaste eLanguageTypeObjC, 765293116Semaste g_lookup_implementation_function_name, 766293116Semaste error)); 767293116Semaste if (error.Fail()) 768293116Semaste { 769293116Semaste if (log) 770293116Semaste log->Printf ("Failed to get Utility Function for implementation lookup: %s.", error.AsCString()); 771293116Semaste m_impl_code.reset(); 772293116Semaste return args_addr; 773293116Semaste } 774309124Sdim 775309124Sdim if (!m_impl_code->Install(diagnostics, exe_ctx)) 776293116Semaste { 777293116Semaste if (log) 778309124Sdim { 779309124Sdim log->Printf("Failed to install implementation lookup."); 780309124Sdim diagnostics.Dump(log); 781309124Sdim } 782293116Semaste m_impl_code.reset(); 783293116Semaste return args_addr; 784293116Semaste } 785293116Semaste } 786293116Semaste else 787293116Semaste { 788293116Semaste if (log) 789293116Semaste log->Printf("No method lookup implementation code."); 790293116Semaste return LLDB_INVALID_ADDRESS; 791293116Semaste } 792293116Semaste 793293116Semaste // Next make the runner function for our implementation utility function. 794293116Semaste ClangASTContext *clang_ast_context = thread.GetProcess()->GetTarget().GetScratchClangASTContext(); 795293116Semaste CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 796293116Semaste Error error; 797293116Semaste 798293116Semaste impl_function_caller = m_impl_code->MakeFunctionCaller(clang_void_ptr_type, 799293116Semaste dispatch_values, 800309124Sdim thread_sp, 801293116Semaste error); 802293116Semaste if (error.Fail()) 803293116Semaste { 804293116Semaste if (log) 805293116Semaste log->Printf ("Error getting function caller for dispatch lookup: \"%s\".", error.AsCString()); 806293116Semaste return args_addr; 807293116Semaste } 808293116Semaste } 809293116Semaste else 810293116Semaste { 811293116Semaste impl_function_caller = m_impl_code->GetFunctionCaller(); 812293116Semaste } 813293116Semaste } 814309124Sdim 815309124Sdim diagnostics.Clear(); 816309124Sdim 817293116Semaste // Now write down the argument values for this particular call. This looks like it might be a race condition 818293116Semaste // if other threads were calling into here, but actually it isn't because we allocate a new args structure for 819293116Semaste // this call by passing args_addr = LLDB_INVALID_ADDRESS... 820293116Semaste 821309124Sdim if (!impl_function_caller->WriteFunctionArguments(exe_ctx, args_addr, dispatch_values, diagnostics)) 822293116Semaste { 823293116Semaste if (log) 824309124Sdim { 825309124Sdim log->Printf("Error writing function arguments."); 826309124Sdim diagnostics.Dump(log); 827309124Sdim } 828293116Semaste return args_addr; 829293116Semaste } 830309124Sdim 831293116Semaste return args_addr; 832293116Semaste} 833293116Semaste 834293116SemasteThreadPlanSP 835293116SemasteAppleObjCTrampolineHandler::GetStepThroughDispatchPlan (Thread &thread, bool stop_others) 836293116Semaste{ 837293116Semaste ThreadPlanSP ret_plan_sp; 838293116Semaste lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); 839293116Semaste 840293116Semaste DispatchFunction this_dispatch; 841293116Semaste bool found_it = false; 842293116Semaste 843293116Semaste // First step is to look and see if we are in one of the known ObjC dispatch functions. We've already compiled 844293116Semaste // a table of same, so consult it. 845293116Semaste 846293116Semaste MsgsendMap::iterator pos; 847293116Semaste pos = m_msgSend_map.find (curr_pc); 848293116Semaste if (pos != m_msgSend_map.end()) 849293116Semaste { 850293116Semaste this_dispatch = g_dispatch_functions[(*pos).second]; 851293116Semaste found_it = true; 852293116Semaste } 853293116Semaste 854293116Semaste // Next check to see if we are in a vtable region: 855293116Semaste 856293116Semaste if (!found_it) 857293116Semaste { 858293116Semaste uint32_t flags; 859293116Semaste if (m_vtables_ap.get()) 860293116Semaste { 861293116Semaste found_it = m_vtables_ap->IsAddressInVTables (curr_pc, flags); 862293116Semaste if (found_it) 863293116Semaste { 864293116Semaste this_dispatch.name = "vtable"; 865293116Semaste this_dispatch.stret_return 866293116Semaste = (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) == AppleObjCVTables::eOBJC_TRAMPOLINE_STRET; 867293116Semaste this_dispatch.is_super = false; 868293116Semaste this_dispatch.is_super2 = false; 869293116Semaste this_dispatch.fixedup = DispatchFunction::eFixUpFixed; 870293116Semaste } 871293116Semaste } 872293116Semaste } 873293116Semaste 874293116Semaste if (found_it) 875293116Semaste { 876293116Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 877293116Semaste 878293116Semaste // We are decoding a method dispatch. 879293116Semaste // First job is to pull the arguments out: 880293116Semaste 881293116Semaste lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); 882293116Semaste 883293116Semaste const ABI *abi = NULL; 884293116Semaste ProcessSP process_sp (thread.CalculateProcess()); 885293116Semaste if (process_sp) 886293116Semaste abi = process_sp->GetABI().get(); 887293116Semaste if (abi == NULL) 888293116Semaste return ret_plan_sp; 889293116Semaste 890293116Semaste TargetSP target_sp (thread.CalculateTarget()); 891293116Semaste 892293116Semaste ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext(); 893293116Semaste ValueList argument_values; 894293116Semaste Value void_ptr_value; 895293116Semaste CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); 896293116Semaste void_ptr_value.SetValueType (Value::eValueTypeScalar); 897293116Semaste //void_ptr_value.SetContext (Value::eContextTypeClangType, clang_void_ptr_type); 898293116Semaste void_ptr_value.SetCompilerType (clang_void_ptr_type); 899293116Semaste 900293116Semaste int obj_index; 901293116Semaste int sel_index; 902293116Semaste 903293116Semaste // If this is a struct return dispatch, then the first argument is the 904293116Semaste // return struct pointer, and the object is the second, and the selector is the third. 905293116Semaste // Otherwise the object is the first and the selector the second. 906293116Semaste if (this_dispatch.stret_return) 907293116Semaste { 908293116Semaste obj_index = 1; 909293116Semaste sel_index = 2; 910293116Semaste argument_values.PushValue(void_ptr_value); 911293116Semaste argument_values.PushValue(void_ptr_value); 912293116Semaste argument_values.PushValue(void_ptr_value); 913293116Semaste } 914293116Semaste else 915293116Semaste { 916293116Semaste obj_index = 0; 917293116Semaste sel_index = 1; 918293116Semaste argument_values.PushValue(void_ptr_value); 919293116Semaste argument_values.PushValue(void_ptr_value); 920293116Semaste } 921293116Semaste 922293116Semaste 923293116Semaste bool success = abi->GetArgumentValues (thread, argument_values); 924293116Semaste if (!success) 925293116Semaste return ret_plan_sp; 926293116Semaste 927293116Semaste lldb::addr_t obj_addr = argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong(); 928293116Semaste if (obj_addr == 0x0) 929293116Semaste { 930293116Semaste if (log) 931293116Semaste log->Printf("Asked to step to dispatch to nil object, returning empty plan."); 932293116Semaste return ret_plan_sp; 933293116Semaste } 934293116Semaste 935293116Semaste ExecutionContext exe_ctx (thread.shared_from_this()); 936293116Semaste Process *process = exe_ctx.GetProcessPtr(); 937293116Semaste // isa_addr will store the class pointer that the method is being dispatched to - so either the class 938293116Semaste // directly or the super class if this is one of the objc_msgSendSuper flavors. That's mostly used to 939293116Semaste // look up the class/selector pair in our cache. 940293116Semaste 941293116Semaste lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS; 942293116Semaste lldb::addr_t sel_addr = argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong(); 943293116Semaste 944293116Semaste // Figure out the class this is being dispatched to and see if we've already cached this method call, 945293116Semaste // If so we can push a run-to-address plan directly. Otherwise we have to figure out where 946293116Semaste // the implementation lives. 947293116Semaste 948293116Semaste if (this_dispatch.is_super) 949293116Semaste { 950293116Semaste if (this_dispatch.is_super2) 951293116Semaste { 952293116Semaste // In the objc_msgSendSuper2 case, we don't get the object directly, we get a structure containing 953293116Semaste // the object and the class to which the super message is being sent. So we need to dig the super 954293116Semaste // out of the class and use that. 955293116Semaste 956293116Semaste Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 957293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 958293116Semaste super_value.ResolveValue (&exe_ctx); 959293116Semaste 960293116Semaste if (super_value.GetScalar().IsValid()) 961293116Semaste { 962293116Semaste 963293116Semaste // isa_value now holds the class pointer. The second word of the class pointer is the super-class pointer: 964293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 965293116Semaste super_value.ResolveValue (&exe_ctx); 966293116Semaste if (super_value.GetScalar().IsValid()) 967293116Semaste isa_addr = super_value.GetScalar().ULongLong(); 968293116Semaste else 969293116Semaste { 970293116Semaste if (log) 971293116Semaste log->Printf("Failed to extract the super class value from the class in objc_super."); 972293116Semaste } 973293116Semaste } 974293116Semaste else 975293116Semaste { 976293116Semaste if (log) 977293116Semaste log->Printf("Failed to extract the class value from objc_super."); 978293116Semaste } 979293116Semaste } 980293116Semaste else 981293116Semaste { 982293116Semaste // In the objc_msgSendSuper case, we don't get the object directly, we get a two element structure containing 983293116Semaste // the object and the super class to which the super message is being sent. So the class we want is 984293116Semaste // the second element of this structure. 985293116Semaste 986293116Semaste Value super_value(*(argument_values.GetValueAtIndex(obj_index))); 987293116Semaste super_value.GetScalar() += process->GetAddressByteSize(); 988293116Semaste super_value.ResolveValue (&exe_ctx); 989293116Semaste 990293116Semaste if (super_value.GetScalar().IsValid()) 991293116Semaste { 992293116Semaste isa_addr = super_value.GetScalar().ULongLong(); 993293116Semaste } 994293116Semaste else 995293116Semaste { 996293116Semaste if (log) 997293116Semaste log->Printf("Failed to extract the class value from objc_super."); 998293116Semaste } 999293116Semaste } 1000293116Semaste } 1001293116Semaste else 1002293116Semaste { 1003293116Semaste // In the direct dispatch case, the object->isa is the class pointer we want. 1004293116Semaste 1005293116Semaste // This is a little cheesy, but since object->isa is the first field, 1006293116Semaste // making the object value a load address value and resolving it will get 1007293116Semaste // the pointer sized data pointed to by that value... 1008293116Semaste 1009293116Semaste // Note, it isn't a fatal error not to be able to get the address from the object, since this might 1010293116Semaste // be a "tagged pointer" which isn't a real object, but rather some word length encoded dingus. 1011293116Semaste 1012293116Semaste Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); 1013293116Semaste 1014293116Semaste isa_value.SetValueType(Value::eValueTypeLoadAddress); 1015293116Semaste isa_value.ResolveValue(&exe_ctx); 1016293116Semaste if (isa_value.GetScalar().IsValid()) 1017293116Semaste { 1018293116Semaste isa_addr = isa_value.GetScalar().ULongLong(); 1019293116Semaste } 1020293116Semaste else 1021293116Semaste { 1022293116Semaste if (log) 1023293116Semaste log->Printf("Failed to extract the isa value from object."); 1024293116Semaste } 1025293116Semaste 1026293116Semaste } 1027293116Semaste 1028293116Semaste // Okay, we've got the address of the class for which we're resolving this, let's see if it's in our cache: 1029293116Semaste lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS; 1030293116Semaste 1031293116Semaste if (isa_addr != LLDB_INVALID_ADDRESS) 1032293116Semaste { 1033293116Semaste if (log) 1034293116Semaste { 1035293116Semaste log->Printf("Resolving call for class - 0x%" PRIx64 " and selector - 0x%" PRIx64, 1036293116Semaste isa_addr, sel_addr); 1037293116Semaste } 1038293116Semaste ObjCLanguageRuntime *objc_runtime = thread.GetProcess()->GetObjCLanguageRuntime (); 1039293116Semaste assert(objc_runtime != NULL); 1040293116Semaste 1041293116Semaste impl_addr = objc_runtime->LookupInMethodCache (isa_addr, sel_addr); 1042293116Semaste } 1043293116Semaste 1044293116Semaste if (impl_addr != LLDB_INVALID_ADDRESS) 1045293116Semaste { 1046293116Semaste // Yup, it was in the cache, so we can run to that address directly. 1047293116Semaste 1048293116Semaste if (log) 1049293116Semaste log->Printf ("Found implementation address in cache: 0x%" PRIx64, impl_addr); 1050293116Semaste 1051293116Semaste ret_plan_sp.reset (new ThreadPlanRunToAddress (thread, impl_addr, stop_others)); 1052293116Semaste } 1053293116Semaste else 1054293116Semaste { 1055293116Semaste // We haven't seen this class/selector pair yet. Look it up. 1056293116Semaste StreamString errors; 1057293116Semaste Address impl_code_address; 1058293116Semaste 1059293116Semaste ValueList dispatch_values; 1060293116Semaste 1061293116Semaste // We've will inject a little function in the target that takes the object, selector and some flags, 1062293116Semaste // and figures out the implementation. Looks like: 1063293116Semaste // void *__lldb_objc_find_implementation_for_selector (void *object, 1064293116Semaste // void *sel, 1065293116Semaste // int is_stret, 1066293116Semaste // int is_super, 1067293116Semaste // int is_super2, 1068293116Semaste // int is_fixup, 1069293116Semaste // int is_fixed, 1070293116Semaste // int debug) 1071293116Semaste // So set up the arguments for that call. 1072293116Semaste 1073293116Semaste dispatch_values.PushValue (*(argument_values.GetValueAtIndex(obj_index))); 1074293116Semaste dispatch_values.PushValue (*(argument_values.GetValueAtIndex(sel_index))); 1075293116Semaste 1076293116Semaste Value flag_value; 1077293116Semaste CompilerType clang_int_type = clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingSint, 32); 1078293116Semaste flag_value.SetValueType (Value::eValueTypeScalar); 1079293116Semaste //flag_value.SetContext (Value::eContextTypeClangType, clang_int_type); 1080293116Semaste flag_value.SetCompilerType (clang_int_type); 1081293116Semaste 1082293116Semaste if (this_dispatch.stret_return) 1083293116Semaste flag_value.GetScalar() = 1; 1084293116Semaste else 1085293116Semaste flag_value.GetScalar() = 0; 1086293116Semaste dispatch_values.PushValue (flag_value); 1087293116Semaste 1088293116Semaste if (this_dispatch.is_super) 1089293116Semaste flag_value.GetScalar() = 1; 1090293116Semaste else 1091293116Semaste flag_value.GetScalar() = 0; 1092293116Semaste dispatch_values.PushValue (flag_value); 1093293116Semaste 1094293116Semaste if (this_dispatch.is_super2) 1095293116Semaste flag_value.GetScalar() = 1; 1096293116Semaste else 1097293116Semaste flag_value.GetScalar() = 0; 1098293116Semaste dispatch_values.PushValue (flag_value); 1099293116Semaste 1100293116Semaste switch (this_dispatch.fixedup) 1101293116Semaste { 1102293116Semaste case DispatchFunction::eFixUpNone: 1103293116Semaste flag_value.GetScalar() = 0; 1104293116Semaste dispatch_values.PushValue (flag_value); 1105293116Semaste dispatch_values.PushValue (flag_value); 1106293116Semaste break; 1107293116Semaste case DispatchFunction::eFixUpFixed: 1108293116Semaste flag_value.GetScalar() = 1; 1109293116Semaste dispatch_values.PushValue (flag_value); 1110293116Semaste flag_value.GetScalar() = 1; 1111293116Semaste dispatch_values.PushValue (flag_value); 1112293116Semaste break; 1113293116Semaste case DispatchFunction::eFixUpToFix: 1114293116Semaste flag_value.GetScalar() = 1; 1115293116Semaste dispatch_values.PushValue (flag_value); 1116293116Semaste flag_value.GetScalar() = 0; 1117293116Semaste dispatch_values.PushValue (flag_value); 1118293116Semaste break; 1119293116Semaste } 1120293116Semaste if (log && log->GetVerbose()) 1121293116Semaste flag_value.GetScalar() = 1; 1122293116Semaste else 1123293116Semaste flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done. 1124293116Semaste dispatch_values.PushValue (flag_value); 1125293116Semaste 1126293116Semaste 1127293116Semaste // The step through code might have to fill in the cache, so it is not safe to run only one thread. 1128293116Semaste // So we override the stop_others value passed in to us here: 1129293116Semaste const bool trampoline_stop_others = false; 1130293116Semaste ret_plan_sp.reset (new AppleThreadPlanStepThroughObjCTrampoline (thread, 1131293116Semaste this, 1132293116Semaste dispatch_values, 1133293116Semaste isa_addr, 1134293116Semaste sel_addr, 1135293116Semaste trampoline_stop_others)); 1136293116Semaste if (log) 1137293116Semaste { 1138293116Semaste StreamString s; 1139293116Semaste ret_plan_sp->GetDescription(&s, eDescriptionLevelFull); 1140293116Semaste log->Printf("Using ObjC step plan: %s.\n", s.GetData()); 1141293116Semaste } 1142293116Semaste } 1143293116Semaste } 1144293116Semaste 1145293116Semaste return ret_plan_sp; 1146293116Semaste} 1147293116Semaste 1148293116SemasteFunctionCaller * 1149293116SemasteAppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller () 1150293116Semaste{ 1151293116Semaste return m_impl_code->GetFunctionCaller(); 1152293116Semaste} 1153