15ffd83dbSDimitry Andric //===-- AppleObjCTrampolineHandler.cpp ------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "AppleObjCTrampolineHandler.h"
100b57cec5SDimitry Andric #include "AppleThreadPlanStepThroughObjCTrampoline.h"
110b57cec5SDimitry Andric
125ffd83dbSDimitry Andric #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
140b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
150b57cec5SDimitry Andric #include "lldb/Core/Module.h"
160b57cec5SDimitry Andric #include "lldb/Core/Value.h"
170b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
180b57cec5SDimitry Andric #include "lldb/Expression/FunctionCaller.h"
190b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h"
200b57cec5SDimitry Andric #include "lldb/Expression/UtilityFunction.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
220b57cec5SDimitry Andric #include "lldb/Target/ABI.h"
230b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
240b57cec5SDimitry Andric #include "lldb/Target/Process.h"
250b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
260b57cec5SDimitry Andric #include "lldb/Target/Target.h"
270b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
280b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanRunToAddress.h"
290b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
300b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
3181ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
320b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
3581ad6265SDimitry Andric #include "llvm/ADT/ScopeExit.h"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric #include <memory>
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric using namespace lldb;
420b57cec5SDimitry Andric using namespace lldb_private;
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric const char *AppleObjCTrampolineHandler::g_lookup_implementation_function_name =
450b57cec5SDimitry Andric "__lldb_objc_find_implementation_for_selector";
460b57cec5SDimitry Andric const char *AppleObjCTrampolineHandler::
470b57cec5SDimitry Andric g_lookup_implementation_with_stret_function_code =
4881ad6265SDimitry Andric R"(
4981ad6265SDimitry Andric if (is_stret) {
5081ad6265SDimitry Andric return_struct.impl_addr =
5181ad6265SDimitry Andric class_getMethodImplementation_stret (return_struct.class_addr,
5281ad6265SDimitry Andric return_struct.sel_addr);
5381ad6265SDimitry Andric } else {
5481ad6265SDimitry Andric return_struct.impl_addr =
5581ad6265SDimitry Andric class_getMethodImplementation (return_struct.class_addr,
5681ad6265SDimitry Andric return_struct.sel_addr);
5781ad6265SDimitry Andric }
5881ad6265SDimitry Andric if (debug)
5981ad6265SDimitry Andric printf ("\n*** Returning implementation: %p.\n",
6081ad6265SDimitry Andric return_struct.impl_addr);
6181ad6265SDimitry Andric
6281ad6265SDimitry Andric return return_struct.impl_addr;
6381ad6265SDimitry Andric }
6481ad6265SDimitry Andric )";
650b57cec5SDimitry Andric const char *
660b57cec5SDimitry Andric AppleObjCTrampolineHandler::g_lookup_implementation_no_stret_function_code =
6781ad6265SDimitry Andric R"(
6881ad6265SDimitry Andric return_struct.impl_addr =
6981ad6265SDimitry Andric class_getMethodImplementation (return_struct.class_addr,
7081ad6265SDimitry Andric return_struct.sel_addr);
7181ad6265SDimitry Andric if (debug)
7281ad6265SDimitry Andric printf ("\n*** getMethodImpletation for addr: 0x%p sel: 0x%p result: 0x%p.\n",
7381ad6265SDimitry Andric return_struct.class_addr, return_struct.sel_addr, return_struct.impl_addr);
7481ad6265SDimitry Andric
7581ad6265SDimitry Andric return return_struct.impl_addr;
7681ad6265SDimitry Andric }
7781ad6265SDimitry Andric )";
7881ad6265SDimitry Andric
7981ad6265SDimitry Andric const char
8081ad6265SDimitry Andric *AppleObjCTrampolineHandler::g_lookup_implementation_function_common_code =
8181ad6265SDimitry Andric R"(
8281ad6265SDimitry Andric extern "C"
8381ad6265SDimitry Andric {
8481ad6265SDimitry Andric extern void *class_getMethodImplementation(void *objc_class, void *sel);
8581ad6265SDimitry Andric extern void *class_getMethodImplementation_stret(void *objc_class, void *sel);
8681ad6265SDimitry Andric extern void * object_getClass (id object);
8781ad6265SDimitry Andric extern void * sel_getUid(char *name);
8881ad6265SDimitry Andric extern int printf(const char *format, ...);
8981ad6265SDimitry Andric }
9081ad6265SDimitry Andric extern "C" void *
9181ad6265SDimitry Andric __lldb_objc_find_implementation_for_selector (void *object,
9281ad6265SDimitry Andric void *sel,
9381ad6265SDimitry Andric int is_str_ptr,
9481ad6265SDimitry Andric int is_stret,
9581ad6265SDimitry Andric int is_super,
9681ad6265SDimitry Andric int is_super2,
9781ad6265SDimitry Andric int is_fixup,
9881ad6265SDimitry Andric int is_fixed,
9981ad6265SDimitry Andric int debug)
10081ad6265SDimitry Andric {
10181ad6265SDimitry Andric struct __lldb_imp_return_struct {
10281ad6265SDimitry Andric void *class_addr;
10381ad6265SDimitry Andric void *sel_addr;
10481ad6265SDimitry Andric void *impl_addr;
10581ad6265SDimitry Andric };
10681ad6265SDimitry Andric
10781ad6265SDimitry Andric struct __lldb_objc_class {
10881ad6265SDimitry Andric void *isa;
10981ad6265SDimitry Andric void *super_ptr;
11081ad6265SDimitry Andric };
11181ad6265SDimitry Andric struct __lldb_objc_super {
11281ad6265SDimitry Andric void *receiver;
11381ad6265SDimitry Andric struct __lldb_objc_class *class_ptr;
11481ad6265SDimitry Andric };
11581ad6265SDimitry Andric struct __lldb_msg_ref {
11681ad6265SDimitry Andric void *dont_know;
11781ad6265SDimitry Andric void *sel;
11881ad6265SDimitry Andric };
11981ad6265SDimitry Andric
12081ad6265SDimitry Andric struct __lldb_imp_return_struct return_struct;
12181ad6265SDimitry Andric
12281ad6265SDimitry Andric if (debug)
12381ad6265SDimitry Andric printf ("\n*** Called with obj: %p sel: %p is_str_ptr: %d "
12481ad6265SDimitry Andric "is_stret: %d is_super: %d, "
12581ad6265SDimitry Andric "is_super2: %d, is_fixup: %d, is_fixed: %d\n",
12681ad6265SDimitry Andric object, sel, is_str_ptr, is_stret,
12781ad6265SDimitry Andric is_super, is_super2, is_fixup, is_fixed);
12881ad6265SDimitry Andric
12981ad6265SDimitry Andric if (is_str_ptr) {
13081ad6265SDimitry Andric if (debug)
13181ad6265SDimitry Andric printf("*** Turning string: '%s'", sel);
13281ad6265SDimitry Andric sel = sel_getUid((char *)sel);
13381ad6265SDimitry Andric if (debug)
13481ad6265SDimitry Andric printf("*** into sel to %p", sel);
13581ad6265SDimitry Andric }
13681ad6265SDimitry Andric if (is_super) {
13781ad6265SDimitry Andric if (is_super2) {
13881ad6265SDimitry Andric return_struct.class_addr
13981ad6265SDimitry Andric = ((__lldb_objc_super *) object)->class_ptr->super_ptr;
14081ad6265SDimitry Andric } else {
14181ad6265SDimitry Andric return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr;
14281ad6265SDimitry Andric }
14381ad6265SDimitry Andric if (debug)
14481ad6265SDimitry Andric printf("*** Super, class addr: %p\n", return_struct.class_addr);
14581ad6265SDimitry Andric } else {
14681ad6265SDimitry Andric // This code seems a little funny, but has its reasons...
14781ad6265SDimitry Andric // The call to [object class] is here because if this is a class, and has
14881ad6265SDimitry Andric // not been called into yet, we need to do something to force the class to
14981ad6265SDimitry Andric // initialize itself.
15081ad6265SDimitry Andric // Then the call to object_getClass will actually return the correct class,
15181ad6265SDimitry Andric // either the class if object is a class instance, or the meta-class if it
15281ad6265SDimitry Andric // is a class pointer.
15381ad6265SDimitry Andric void *class_ptr = (void *) [(id) object class];
15481ad6265SDimitry Andric return_struct.class_addr = (id) object_getClass((id) object);
15581ad6265SDimitry Andric if (debug) {
15681ad6265SDimitry Andric if (class_ptr == object) {
15781ad6265SDimitry Andric printf ("Found a class object, need to return the meta class %p -> %p\n",
15881ad6265SDimitry Andric class_ptr, return_struct.class_addr);
15981ad6265SDimitry Andric } else {
16081ad6265SDimitry Andric printf ("[object class] returned: %p object_getClass: %p.\n",
16181ad6265SDimitry Andric class_ptr, return_struct.class_addr);
16281ad6265SDimitry Andric }
16381ad6265SDimitry Andric }
16481ad6265SDimitry Andric }
16581ad6265SDimitry Andric
16681ad6265SDimitry Andric if (is_fixup) {
16781ad6265SDimitry Andric if (is_fixed) {
16881ad6265SDimitry Andric return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel;
16981ad6265SDimitry Andric } else {
17081ad6265SDimitry Andric char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel;
17181ad6265SDimitry Andric return_struct.sel_addr = sel_getUid (sel_name);
17281ad6265SDimitry Andric if (debug)
17381ad6265SDimitry Andric printf ("\n*** Got fixed up selector: %p for name %s.\n",
17481ad6265SDimitry Andric return_struct.sel_addr, sel_name);
17581ad6265SDimitry Andric }
17681ad6265SDimitry Andric } else {
17781ad6265SDimitry Andric return_struct.sel_addr = sel;
17881ad6265SDimitry Andric }
17981ad6265SDimitry Andric )";
1800b57cec5SDimitry Andric
VTableRegion(AppleObjCVTables * owner,lldb::addr_t header_addr)1810b57cec5SDimitry Andric AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::VTableRegion(
1820b57cec5SDimitry Andric AppleObjCVTables *owner, lldb::addr_t header_addr)
18381ad6265SDimitry Andric : m_valid(true), m_owner(owner), m_header_addr(header_addr) {
1840b57cec5SDimitry Andric SetUpRegion();
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
187fe6060f1SDimitry Andric AppleObjCTrampolineHandler::~AppleObjCTrampolineHandler() = default;
1880b57cec5SDimitry Andric
SetUpRegion()1890b57cec5SDimitry Andric void AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::SetUpRegion() {
1900b57cec5SDimitry Andric // The header looks like:
1910b57cec5SDimitry Andric //
1920b57cec5SDimitry Andric // uint16_t headerSize
1930b57cec5SDimitry Andric // uint16_t descSize
1940b57cec5SDimitry Andric // uint32_t descCount
1950b57cec5SDimitry Andric // void * next
1960b57cec5SDimitry Andric //
1970b57cec5SDimitry Andric // First read in the header:
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric char memory_buffer[16];
2000b57cec5SDimitry Andric ProcessSP process_sp = m_owner->GetProcessSP();
2010b57cec5SDimitry Andric if (!process_sp)
2020b57cec5SDimitry Andric return;
2030b57cec5SDimitry Andric DataExtractor data(memory_buffer, sizeof(memory_buffer),
2040b57cec5SDimitry Andric process_sp->GetByteOrder(),
2050b57cec5SDimitry Andric process_sp->GetAddressByteSize());
2060b57cec5SDimitry Andric size_t actual_size = 8 + process_sp->GetAddressByteSize();
2070b57cec5SDimitry Andric Status error;
2080b57cec5SDimitry Andric size_t bytes_read =
2090b57cec5SDimitry Andric process_sp->ReadMemory(m_header_addr, memory_buffer, actual_size, error);
2100b57cec5SDimitry Andric if (bytes_read != actual_size) {
2110b57cec5SDimitry Andric m_valid = false;
2120b57cec5SDimitry Andric return;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric lldb::offset_t offset = 0;
2160b57cec5SDimitry Andric const uint16_t header_size = data.GetU16(&offset);
2170b57cec5SDimitry Andric const uint16_t descriptor_size = data.GetU16(&offset);
2180b57cec5SDimitry Andric const size_t num_descriptors = data.GetU32(&offset);
2190b57cec5SDimitry Andric
2205ffd83dbSDimitry Andric m_next_region = data.GetAddress(&offset);
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric // If the header size is 0, that means we've come in too early before this
2230b57cec5SDimitry Andric // data is set up.
2240b57cec5SDimitry Andric // Set ourselves as not valid, and continue.
2250b57cec5SDimitry Andric if (header_size == 0 || num_descriptors == 0) {
2260b57cec5SDimitry Andric m_valid = false;
2270b57cec5SDimitry Andric return;
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric // Now read in all the descriptors:
2310b57cec5SDimitry Andric // The descriptor looks like:
2320b57cec5SDimitry Andric //
2330b57cec5SDimitry Andric // uint32_t offset
2340b57cec5SDimitry Andric // uint32_t flags
2350b57cec5SDimitry Andric //
2360b57cec5SDimitry Andric // Where offset is either 0 - in which case it is unused, or it is
2370b57cec5SDimitry Andric // the offset of the vtable code from the beginning of the
2380b57cec5SDimitry Andric // descriptor record. Below, we'll convert that into an absolute
2390b57cec5SDimitry Andric // code address, since I don't want to have to compute it over and
2400b57cec5SDimitry Andric // over.
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric // Ingest the whole descriptor array:
2430b57cec5SDimitry Andric const lldb::addr_t desc_ptr = m_header_addr + header_size;
2440b57cec5SDimitry Andric const size_t desc_array_size = num_descriptors * descriptor_size;
24581ad6265SDimitry Andric WritableDataBufferSP data_sp(new DataBufferHeap(desc_array_size, '\0'));
2460b57cec5SDimitry Andric uint8_t *dst = (uint8_t *)data_sp->GetBytes();
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric DataExtractor desc_extractor(dst, desc_array_size, process_sp->GetByteOrder(),
2490b57cec5SDimitry Andric process_sp->GetAddressByteSize());
2500b57cec5SDimitry Andric bytes_read = process_sp->ReadMemory(desc_ptr, dst, desc_array_size, error);
2510b57cec5SDimitry Andric if (bytes_read != desc_array_size) {
2520b57cec5SDimitry Andric m_valid = false;
2530b57cec5SDimitry Andric return;
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric // The actual code for the vtables will be laid out consecutively, so I also
2570b57cec5SDimitry Andric // compute the start and end of the whole code block.
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric offset = 0;
2600b57cec5SDimitry Andric m_code_start_addr = 0;
2610b57cec5SDimitry Andric m_code_end_addr = 0;
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric for (size_t i = 0; i < num_descriptors; i++) {
2640b57cec5SDimitry Andric lldb::addr_t start_offset = offset;
2650b57cec5SDimitry Andric uint32_t voffset = desc_extractor.GetU32(&offset);
2660b57cec5SDimitry Andric uint32_t flags = desc_extractor.GetU32(&offset);
2670b57cec5SDimitry Andric lldb::addr_t code_addr = desc_ptr + start_offset + voffset;
2680b57cec5SDimitry Andric m_descriptors.push_back(VTableDescriptor(flags, code_addr));
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric if (m_code_start_addr == 0 || code_addr < m_code_start_addr)
2710b57cec5SDimitry Andric m_code_start_addr = code_addr;
2720b57cec5SDimitry Andric if (code_addr > m_code_end_addr)
2730b57cec5SDimitry Andric m_code_end_addr = code_addr;
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric offset = start_offset + descriptor_size;
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric // Finally, a little bird told me that all the vtable code blocks
2780b57cec5SDimitry Andric // are the same size. Let's compute the blocks and if they are all
2790b57cec5SDimitry Andric // the same add the size to the code end address:
2800b57cec5SDimitry Andric lldb::addr_t code_size = 0;
2810b57cec5SDimitry Andric bool all_the_same = true;
2820b57cec5SDimitry Andric for (size_t i = 0; i < num_descriptors - 1; i++) {
2830b57cec5SDimitry Andric lldb::addr_t this_size =
2840b57cec5SDimitry Andric m_descriptors[i + 1].code_start - m_descriptors[i].code_start;
2850b57cec5SDimitry Andric if (code_size == 0)
2860b57cec5SDimitry Andric code_size = this_size;
2870b57cec5SDimitry Andric else {
2880b57cec5SDimitry Andric if (this_size != code_size)
2890b57cec5SDimitry Andric all_the_same = false;
2900b57cec5SDimitry Andric if (this_size > code_size)
2910b57cec5SDimitry Andric code_size = this_size;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric if (all_the_same)
2950b57cec5SDimitry Andric m_code_end_addr += code_size;
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::
AddressInRegion(lldb::addr_t addr,uint32_t & flags)2990b57cec5SDimitry Andric AddressInRegion(lldb::addr_t addr, uint32_t &flags) {
3000b57cec5SDimitry Andric if (!IsValid())
3010b57cec5SDimitry Andric return false;
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric if (addr < m_code_start_addr || addr > m_code_end_addr)
3040b57cec5SDimitry Andric return false;
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric std::vector<VTableDescriptor>::iterator pos, end = m_descriptors.end();
3070b57cec5SDimitry Andric for (pos = m_descriptors.begin(); pos != end; pos++) {
3080b57cec5SDimitry Andric if (addr <= (*pos).code_start) {
3090b57cec5SDimitry Andric flags = (*pos).flags;
3100b57cec5SDimitry Andric return true;
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric return false;
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric
Dump(Stream & s)3160b57cec5SDimitry Andric void AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::Dump(
3170b57cec5SDimitry Andric Stream &s) {
3180b57cec5SDimitry Andric s.Printf("Header addr: 0x%" PRIx64 " Code start: 0x%" PRIx64
3190b57cec5SDimitry Andric " Code End: 0x%" PRIx64 " Next: 0x%" PRIx64 "\n",
3200b57cec5SDimitry Andric m_header_addr, m_code_start_addr, m_code_end_addr, m_next_region);
3210b57cec5SDimitry Andric size_t num_elements = m_descriptors.size();
3220b57cec5SDimitry Andric for (size_t i = 0; i < num_elements; i++) {
3230b57cec5SDimitry Andric s.Indent();
3240b57cec5SDimitry Andric s.Printf("Code start: 0x%" PRIx64 " Flags: %d\n",
3250b57cec5SDimitry Andric m_descriptors[i].code_start, m_descriptors[i].flags);
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
AppleObjCVTables(const ProcessSP & process_sp,const ModuleSP & objc_module_sp)3290b57cec5SDimitry Andric AppleObjCTrampolineHandler::AppleObjCVTables::AppleObjCVTables(
3300b57cec5SDimitry Andric const ProcessSP &process_sp, const ModuleSP &objc_module_sp)
3310b57cec5SDimitry Andric : m_process_wp(), m_trampoline_header(LLDB_INVALID_ADDRESS),
3320b57cec5SDimitry Andric m_trampolines_changed_bp_id(LLDB_INVALID_BREAK_ID),
3330b57cec5SDimitry Andric m_objc_module_sp(objc_module_sp) {
3340b57cec5SDimitry Andric if (process_sp)
3350b57cec5SDimitry Andric m_process_wp = process_sp;
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric
~AppleObjCVTables()3380b57cec5SDimitry Andric AppleObjCTrampolineHandler::AppleObjCVTables::~AppleObjCVTables() {
3390b57cec5SDimitry Andric ProcessSP process_sp = GetProcessSP();
3400b57cec5SDimitry Andric if (process_sp) {
3410b57cec5SDimitry Andric if (m_trampolines_changed_bp_id != LLDB_INVALID_BREAK_ID)
3420b57cec5SDimitry Andric process_sp->GetTarget().RemoveBreakpointByID(m_trampolines_changed_bp_id);
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric
InitializeVTableSymbols()3460b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::InitializeVTableSymbols() {
3470b57cec5SDimitry Andric if (m_trampoline_header != LLDB_INVALID_ADDRESS)
3480b57cec5SDimitry Andric return true;
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric ProcessSP process_sp = GetProcessSP();
3510b57cec5SDimitry Andric if (process_sp) {
3520b57cec5SDimitry Andric Target &target = process_sp->GetTarget();
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric if (!m_objc_module_sp) {
355e8d8bef9SDimitry Andric for (ModuleSP module_sp : target.GetImages().Modules()) {
3560b57cec5SDimitry Andric if (ObjCLanguageRuntime::Get(*process_sp)
357e8d8bef9SDimitry Andric ->IsModuleObjCLibrary(module_sp)) {
358e8d8bef9SDimitry Andric m_objc_module_sp = module_sp;
3590b57cec5SDimitry Andric break;
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric if (m_objc_module_sp) {
3650b57cec5SDimitry Andric ConstString trampoline_name("gdb_objc_trampolines");
3660b57cec5SDimitry Andric const Symbol *trampoline_symbol =
3670b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(trampoline_name,
3680b57cec5SDimitry Andric eSymbolTypeData);
3690b57cec5SDimitry Andric if (trampoline_symbol != nullptr) {
3700b57cec5SDimitry Andric m_trampoline_header = trampoline_symbol->GetLoadAddress(&target);
3710b57cec5SDimitry Andric if (m_trampoline_header == LLDB_INVALID_ADDRESS)
3720b57cec5SDimitry Andric return false;
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric // Next look up the "changed" symbol and set a breakpoint on that...
3750b57cec5SDimitry Andric ConstString changed_name("gdb_objc_trampolines_changed");
3760b57cec5SDimitry Andric const Symbol *changed_symbol =
3770b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(changed_name,
3780b57cec5SDimitry Andric eSymbolTypeCode);
3790b57cec5SDimitry Andric if (changed_symbol != nullptr) {
3800b57cec5SDimitry Andric const Address changed_symbol_addr = changed_symbol->GetAddress();
3810b57cec5SDimitry Andric if (!changed_symbol_addr.IsValid())
3820b57cec5SDimitry Andric return false;
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric lldb::addr_t changed_addr =
3850b57cec5SDimitry Andric changed_symbol_addr.GetOpcodeLoadAddress(&target);
3860b57cec5SDimitry Andric if (changed_addr != LLDB_INVALID_ADDRESS) {
3870b57cec5SDimitry Andric BreakpointSP trampolines_changed_bp_sp =
3880b57cec5SDimitry Andric target.CreateBreakpoint(changed_addr, true, false);
3890b57cec5SDimitry Andric if (trampolines_changed_bp_sp) {
3900b57cec5SDimitry Andric m_trampolines_changed_bp_id = trampolines_changed_bp_sp->GetID();
3910b57cec5SDimitry Andric trampolines_changed_bp_sp->SetCallback(RefreshTrampolines, this,
3920b57cec5SDimitry Andric true);
3930b57cec5SDimitry Andric trampolines_changed_bp_sp->SetBreakpointKind(
3940b57cec5SDimitry Andric "objc-trampolines-changed");
3950b57cec5SDimitry Andric return true;
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric }
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric return false;
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric
RefreshTrampolines(void * baton,StoppointCallbackContext * context,lldb::user_id_t break_id,lldb::user_id_t break_loc_id)4050b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::RefreshTrampolines(
4060b57cec5SDimitry Andric void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
4070b57cec5SDimitry Andric lldb::user_id_t break_loc_id) {
4080b57cec5SDimitry Andric AppleObjCVTables *vtable_handler = (AppleObjCVTables *)baton;
4090b57cec5SDimitry Andric if (vtable_handler->InitializeVTableSymbols()) {
4100b57cec5SDimitry Andric // The Update function is called with the address of an added region. So we
4110b57cec5SDimitry Andric // grab that address, and
4120b57cec5SDimitry Andric // feed it into ReadRegions. Of course, our friend the ABI will get the
4130b57cec5SDimitry Andric // values for us.
4140b57cec5SDimitry Andric ExecutionContext exe_ctx(context->exe_ctx_ref);
4150b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
4160b57cec5SDimitry Andric const ABI *abi = process->GetABI().get();
4170b57cec5SDimitry Andric
418*bdd1243dSDimitry Andric TypeSystemClangSP scratch_ts_sp =
419e8d8bef9SDimitry Andric ScratchTypeSystemClang::GetForTarget(process->GetTarget());
420*bdd1243dSDimitry Andric if (!scratch_ts_sp)
421480093f4SDimitry Andric return false;
422480093f4SDimitry Andric
4230b57cec5SDimitry Andric ValueList argument_values;
4240b57cec5SDimitry Andric Value input_value;
4250b57cec5SDimitry Andric CompilerType clang_void_ptr_type =
426*bdd1243dSDimitry Andric scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
4270b57cec5SDimitry Andric
428fe6060f1SDimitry Andric input_value.SetValueType(Value::ValueType::Scalar);
4290b57cec5SDimitry Andric // input_value.SetContext (Value::eContextTypeClangType,
4300b57cec5SDimitry Andric // clang_void_ptr_type);
4310b57cec5SDimitry Andric input_value.SetCompilerType(clang_void_ptr_type);
4320b57cec5SDimitry Andric argument_values.PushValue(input_value);
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric bool success =
4350b57cec5SDimitry Andric abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values);
4360b57cec5SDimitry Andric if (!success)
4370b57cec5SDimitry Andric return false;
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric // Now get a pointer value from the zeroth argument.
4400b57cec5SDimitry Andric Status error;
4410b57cec5SDimitry Andric DataExtractor data;
4420b57cec5SDimitry Andric error = argument_values.GetValueAtIndex(0)->GetValueAsData(&exe_ctx, data,
4439dba64beSDimitry Andric nullptr);
4440b57cec5SDimitry Andric lldb::offset_t offset = 0;
4455ffd83dbSDimitry Andric lldb::addr_t region_addr = data.GetAddress(&offset);
4460b57cec5SDimitry Andric
4470b57cec5SDimitry Andric if (region_addr != 0)
4480b57cec5SDimitry Andric vtable_handler->ReadRegions(region_addr);
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric return false;
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric
ReadRegions()4530b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions() {
4540b57cec5SDimitry Andric // The no argument version reads the start region from the value of
4550b57cec5SDimitry Andric // the gdb_regions_header, and gets started from there.
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric m_regions.clear();
4580b57cec5SDimitry Andric if (!InitializeVTableSymbols())
4590b57cec5SDimitry Andric return false;
4600b57cec5SDimitry Andric Status error;
4610b57cec5SDimitry Andric ProcessSP process_sp = GetProcessSP();
4620b57cec5SDimitry Andric if (process_sp) {
4630b57cec5SDimitry Andric lldb::addr_t region_addr =
4640b57cec5SDimitry Andric process_sp->ReadPointerFromMemory(m_trampoline_header, error);
4650b57cec5SDimitry Andric if (error.Success())
4660b57cec5SDimitry Andric return ReadRegions(region_addr);
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric return false;
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric
ReadRegions(lldb::addr_t region_addr)4710b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions(
4720b57cec5SDimitry Andric lldb::addr_t region_addr) {
4730b57cec5SDimitry Andric ProcessSP process_sp = GetProcessSP();
4740b57cec5SDimitry Andric if (!process_sp)
4750b57cec5SDimitry Andric return false;
4760b57cec5SDimitry Andric
47781ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andric // We aren't starting at the trampoline symbol.
4800b57cec5SDimitry Andric InitializeVTableSymbols();
4810b57cec5SDimitry Andric lldb::addr_t next_region = region_addr;
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric // Read in the sizes of the headers.
4840b57cec5SDimitry Andric while (next_region != 0) {
4850b57cec5SDimitry Andric m_regions.push_back(VTableRegion(this, next_region));
4860b57cec5SDimitry Andric if (!m_regions.back().IsValid()) {
4870b57cec5SDimitry Andric m_regions.clear();
4880b57cec5SDimitry Andric return false;
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric if (log) {
4910b57cec5SDimitry Andric StreamString s;
4920b57cec5SDimitry Andric m_regions.back().Dump(s);
4939dba64beSDimitry Andric LLDB_LOGF(log, "Read vtable region: \n%s", s.GetData());
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric
4960b57cec5SDimitry Andric next_region = m_regions.back().GetNextRegionAddr();
4970b57cec5SDimitry Andric }
4980b57cec5SDimitry Andric
4990b57cec5SDimitry Andric return true;
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric
IsAddressInVTables(lldb::addr_t addr,uint32_t & flags)5020b57cec5SDimitry Andric bool AppleObjCTrampolineHandler::AppleObjCVTables::IsAddressInVTables(
5030b57cec5SDimitry Andric lldb::addr_t addr, uint32_t &flags) {
5040b57cec5SDimitry Andric region_collection::iterator pos, end = m_regions.end();
5050b57cec5SDimitry Andric for (pos = m_regions.begin(); pos != end; pos++) {
5060b57cec5SDimitry Andric if ((*pos).AddressInRegion(addr, flags))
5070b57cec5SDimitry Andric return true;
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric return false;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric
5120b57cec5SDimitry Andric const AppleObjCTrampolineHandler::DispatchFunction
5130b57cec5SDimitry Andric AppleObjCTrampolineHandler::g_dispatch_functions[] = {
5140b57cec5SDimitry Andric // NAME STRET SUPER SUPER2 FIXUP TYPE
5150b57cec5SDimitry Andric {"objc_msgSend", false, false, false, DispatchFunction::eFixUpNone},
5160b57cec5SDimitry Andric {"objc_msgSend_fixup", false, false, false,
5170b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5180b57cec5SDimitry Andric {"objc_msgSend_fixedup", false, false, false,
5190b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5200b57cec5SDimitry Andric {"objc_msgSend_stret", true, false, false,
5210b57cec5SDimitry Andric DispatchFunction::eFixUpNone},
5220b57cec5SDimitry Andric {"objc_msgSend_stret_fixup", true, false, false,
5230b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5240b57cec5SDimitry Andric {"objc_msgSend_stret_fixedup", true, false, false,
5250b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5260b57cec5SDimitry Andric {"objc_msgSend_fpret", false, false, false,
5270b57cec5SDimitry Andric DispatchFunction::eFixUpNone},
5280b57cec5SDimitry Andric {"objc_msgSend_fpret_fixup", false, false, false,
5290b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5300b57cec5SDimitry Andric {"objc_msgSend_fpret_fixedup", false, false, false,
5310b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5320b57cec5SDimitry Andric {"objc_msgSend_fp2ret", false, false, true,
5330b57cec5SDimitry Andric DispatchFunction::eFixUpNone},
5340b57cec5SDimitry Andric {"objc_msgSend_fp2ret_fixup", false, false, true,
5350b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5360b57cec5SDimitry Andric {"objc_msgSend_fp2ret_fixedup", false, false, true,
5370b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5380b57cec5SDimitry Andric {"objc_msgSendSuper", false, true, false, DispatchFunction::eFixUpNone},
5390b57cec5SDimitry Andric {"objc_msgSendSuper_stret", true, true, false,
5400b57cec5SDimitry Andric DispatchFunction::eFixUpNone},
5410b57cec5SDimitry Andric {"objc_msgSendSuper2", false, true, true, DispatchFunction::eFixUpNone},
5420b57cec5SDimitry Andric {"objc_msgSendSuper2_fixup", false, true, true,
5430b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5440b57cec5SDimitry Andric {"objc_msgSendSuper2_fixedup", false, true, true,
5450b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5460b57cec5SDimitry Andric {"objc_msgSendSuper2_stret", true, true, true,
5470b57cec5SDimitry Andric DispatchFunction::eFixUpNone},
5480b57cec5SDimitry Andric {"objc_msgSendSuper2_stret_fixup", true, true, true,
5490b57cec5SDimitry Andric DispatchFunction::eFixUpToFix},
5500b57cec5SDimitry Andric {"objc_msgSendSuper2_stret_fixedup", true, true, true,
5510b57cec5SDimitry Andric DispatchFunction::eFixUpFixed},
5520b57cec5SDimitry Andric };
5530b57cec5SDimitry Andric
5545ffd83dbSDimitry Andric // This is the table of ObjC "accelerated dispatch" functions. They are a set
5555ffd83dbSDimitry Andric // of objc methods that are "seldom overridden" and so the compiler replaces the
5565ffd83dbSDimitry Andric // objc_msgSend with a call to one of the dispatch functions. That will check
5575ffd83dbSDimitry Andric // whether the method has been overridden, and directly call the Foundation
5585ffd83dbSDimitry Andric // implementation if not.
5595ffd83dbSDimitry Andric // This table is supposed to be complete. If ones get added in the future, we
5605ffd83dbSDimitry Andric // will have to add them to the table.
5615ffd83dbSDimitry Andric const char *AppleObjCTrampolineHandler::g_opt_dispatch_names[] = {
5625ffd83dbSDimitry Andric "objc_alloc",
5635ffd83dbSDimitry Andric "objc_autorelease",
5645ffd83dbSDimitry Andric "objc_release",
5655ffd83dbSDimitry Andric "objc_retain",
5665ffd83dbSDimitry Andric "objc_alloc_init",
5675ffd83dbSDimitry Andric "objc_allocWithZone",
5685ffd83dbSDimitry Andric "objc_opt_class",
5695ffd83dbSDimitry Andric "objc_opt_isKindOfClass",
5705ffd83dbSDimitry Andric "objc_opt_new",
5715ffd83dbSDimitry Andric "objc_opt_respondsToSelector",
5725ffd83dbSDimitry Andric "objc_opt_self",
5735ffd83dbSDimitry Andric };
5745ffd83dbSDimitry Andric
AppleObjCTrampolineHandler(const ProcessSP & process_sp,const ModuleSP & objc_module_sp)5750b57cec5SDimitry Andric AppleObjCTrampolineHandler::AppleObjCTrampolineHandler(
5760b57cec5SDimitry Andric const ProcessSP &process_sp, const ModuleSP &objc_module_sp)
5770b57cec5SDimitry Andric : m_process_wp(), m_objc_module_sp(objc_module_sp),
5780b57cec5SDimitry Andric m_impl_fn_addr(LLDB_INVALID_ADDRESS),
5790b57cec5SDimitry Andric m_impl_stret_fn_addr(LLDB_INVALID_ADDRESS),
580fcaf7f86SDimitry Andric m_msg_forward_addr(LLDB_INVALID_ADDRESS),
581fcaf7f86SDimitry Andric m_msg_forward_stret_addr(LLDB_INVALID_ADDRESS) {
5820b57cec5SDimitry Andric if (process_sp)
5830b57cec5SDimitry Andric m_process_wp = process_sp;
5840b57cec5SDimitry Andric // Look up the known resolution functions:
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric ConstString get_impl_name("class_getMethodImplementation");
5870b57cec5SDimitry Andric ConstString get_impl_stret_name("class_getMethodImplementation_stret");
5880b57cec5SDimitry Andric ConstString msg_forward_name("_objc_msgForward");
5890b57cec5SDimitry Andric ConstString msg_forward_stret_name("_objc_msgForward_stret");
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric Target *target = process_sp ? &process_sp->GetTarget() : nullptr;
5920b57cec5SDimitry Andric const Symbol *class_getMethodImplementation =
5930b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_name,
5940b57cec5SDimitry Andric eSymbolTypeCode);
5950b57cec5SDimitry Andric const Symbol *class_getMethodImplementation_stret =
5960b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_stret_name,
5970b57cec5SDimitry Andric eSymbolTypeCode);
5980b57cec5SDimitry Andric const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType(
5990b57cec5SDimitry Andric msg_forward_name, eSymbolTypeCode);
6000b57cec5SDimitry Andric const Symbol *msg_forward_stret =
6010b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(msg_forward_stret_name,
6020b57cec5SDimitry Andric eSymbolTypeCode);
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric if (class_getMethodImplementation)
6050b57cec5SDimitry Andric m_impl_fn_addr =
6060b57cec5SDimitry Andric class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress(
6070b57cec5SDimitry Andric target);
6080b57cec5SDimitry Andric if (class_getMethodImplementation_stret)
6090b57cec5SDimitry Andric m_impl_stret_fn_addr =
6100b57cec5SDimitry Andric class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress(
6110b57cec5SDimitry Andric target);
6120b57cec5SDimitry Andric if (msg_forward)
6130b57cec5SDimitry Andric m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target);
6140b57cec5SDimitry Andric if (msg_forward_stret)
6150b57cec5SDimitry Andric m_msg_forward_stret_addr =
6160b57cec5SDimitry Andric msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target);
6170b57cec5SDimitry Andric
6180b57cec5SDimitry Andric // FIXME: Do some kind of logging here.
6190b57cec5SDimitry Andric if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) {
6200b57cec5SDimitry Andric // If we can't even find the ordinary get method implementation function,
6210b57cec5SDimitry Andric // then we aren't going to be able to
6220b57cec5SDimitry Andric // step through any method dispatches. Warn to that effect and get out of
6230b57cec5SDimitry Andric // here.
6240b57cec5SDimitry Andric if (process_sp->CanJIT()) {
6259dba64beSDimitry Andric process_sp->GetTarget().GetDebugger().GetErrorStream().Printf(
6260b57cec5SDimitry Andric "Could not find implementation lookup function \"%s\""
6270b57cec5SDimitry Andric " step in through ObjC method dispatch will not work.\n",
6280b57cec5SDimitry Andric get_impl_name.AsCString());
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric return;
63181ad6265SDimitry Andric }
63281ad6265SDimitry Andric
63381ad6265SDimitry Andric // We will either set the implementation to the _stret or non_stret version,
63481ad6265SDimitry Andric // so either way it's safe to start filling the m_lookup_..._code here.
63581ad6265SDimitry Andric m_lookup_implementation_function_code.assign(
63681ad6265SDimitry Andric g_lookup_implementation_function_common_code);
63781ad6265SDimitry Andric
63881ad6265SDimitry Andric if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) {
6390b57cec5SDimitry Andric // It there is no stret return lookup function, assume that it is the same
6400b57cec5SDimitry Andric // as the straight lookup:
6410b57cec5SDimitry Andric m_impl_stret_fn_addr = m_impl_fn_addr;
6420b57cec5SDimitry Andric // Also we will use the version of the lookup code that doesn't rely on the
6430b57cec5SDimitry Andric // stret version of the function.
64481ad6265SDimitry Andric m_lookup_implementation_function_code.append(
64581ad6265SDimitry Andric g_lookup_implementation_no_stret_function_code);
6460b57cec5SDimitry Andric } else {
64781ad6265SDimitry Andric m_lookup_implementation_function_code.append(
64881ad6265SDimitry Andric g_lookup_implementation_with_stret_function_code);
6490b57cec5SDimitry Andric }
6500b57cec5SDimitry Andric
6510b57cec5SDimitry Andric // Look up the addresses for the objc dispatch functions and cache
6520b57cec5SDimitry Andric // them. For now I'm inspecting the symbol names dynamically to
6530b57cec5SDimitry Andric // figure out how to dispatch to them. If it becomes more
6540b57cec5SDimitry Andric // complicated than this we can turn the g_dispatch_functions char *
6550b57cec5SDimitry Andric // array into a template table, and populate the DispatchFunction
6560b57cec5SDimitry Andric // map from there.
6570b57cec5SDimitry Andric
658*bdd1243dSDimitry Andric for (size_t i = 0; i != std::size(g_dispatch_functions); i++) {
6590b57cec5SDimitry Andric ConstString name_const_str(g_dispatch_functions[i].name);
6600b57cec5SDimitry Andric const Symbol *msgSend_symbol =
6610b57cec5SDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str,
6620b57cec5SDimitry Andric eSymbolTypeCode);
6630b57cec5SDimitry Andric if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) {
6640b57cec5SDimitry Andric // FIXME: Make g_dispatch_functions static table of
6650b57cec5SDimitry Andric // DispatchFunctions, and have the map be address->index.
6660b57cec5SDimitry Andric // Problem is we also need to lookup the dispatch function. For
6670b57cec5SDimitry Andric // now we could have a side table of stret & non-stret dispatch
6680b57cec5SDimitry Andric // functions. If that's as complex as it gets, we're fine.
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric lldb::addr_t sym_addr =
6710b57cec5SDimitry Andric msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target);
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i));
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric
6775ffd83dbSDimitry Andric // Similarly, cache the addresses of the "optimized dispatch" function.
678*bdd1243dSDimitry Andric for (size_t i = 0; i != std::size(g_opt_dispatch_names); i++) {
6795ffd83dbSDimitry Andric ConstString name_const_str(g_opt_dispatch_names[i]);
6805ffd83dbSDimitry Andric const Symbol *msgSend_symbol =
6815ffd83dbSDimitry Andric m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str,
6825ffd83dbSDimitry Andric eSymbolTypeCode);
6835ffd83dbSDimitry Andric if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) {
6845ffd83dbSDimitry Andric lldb::addr_t sym_addr =
6855ffd83dbSDimitry Andric msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target);
6865ffd83dbSDimitry Andric
6875ffd83dbSDimitry Andric m_opt_dispatch_map.emplace(sym_addr, i);
6885ffd83dbSDimitry Andric }
6895ffd83dbSDimitry Andric }
6905ffd83dbSDimitry Andric
6910b57cec5SDimitry Andric // Build our vtable dispatch handler here:
6925ffd83dbSDimitry Andric m_vtables_up =
6935ffd83dbSDimitry Andric std::make_unique<AppleObjCVTables>(process_sp, m_objc_module_sp);
6940b57cec5SDimitry Andric if (m_vtables_up)
6950b57cec5SDimitry Andric m_vtables_up->ReadRegions();
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric lldb::addr_t
SetupDispatchFunction(Thread & thread,ValueList & dispatch_values)6990b57cec5SDimitry Andric AppleObjCTrampolineHandler::SetupDispatchFunction(Thread &thread,
7000b57cec5SDimitry Andric ValueList &dispatch_values) {
7010b57cec5SDimitry Andric ThreadSP thread_sp(thread.shared_from_this());
7020b57cec5SDimitry Andric ExecutionContext exe_ctx(thread_sp);
70381ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
7060b57cec5SDimitry Andric FunctionCaller *impl_function_caller = nullptr;
7070b57cec5SDimitry Andric
7080b57cec5SDimitry Andric // Scope for mutex locker:
7090b57cec5SDimitry Andric {
7100b57cec5SDimitry Andric std::lock_guard<std::mutex> guard(m_impl_function_mutex);
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric // First stage is to make the ClangUtility to hold our injected function:
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andric if (!m_impl_code) {
71581ad6265SDimitry Andric if (!m_lookup_implementation_function_code.empty()) {
716e8d8bef9SDimitry Andric auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
717e8d8bef9SDimitry Andric m_lookup_implementation_function_code,
718e8d8bef9SDimitry Andric g_lookup_implementation_function_name, eLanguageTypeC, exe_ctx);
719e8d8bef9SDimitry Andric if (!utility_fn_or_error) {
720e8d8bef9SDimitry Andric LLDB_LOG_ERROR(
721e8d8bef9SDimitry Andric log, utility_fn_or_error.takeError(),
722e8d8bef9SDimitry Andric "Failed to get Utility Function for implementation lookup: {0}.");
7230b57cec5SDimitry Andric return args_addr;
7240b57cec5SDimitry Andric }
725e8d8bef9SDimitry Andric m_impl_code = std::move(*utility_fn_or_error);
7260b57cec5SDimitry Andric } else {
7279dba64beSDimitry Andric LLDB_LOGF(log, "No method lookup implementation code.");
7280b57cec5SDimitry Andric return LLDB_INVALID_ADDRESS;
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric
7310b57cec5SDimitry Andric // Next make the runner function for our implementation utility function.
732*bdd1243dSDimitry Andric TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget(
733e8d8bef9SDimitry Andric thread.GetProcess()->GetTarget());
734*bdd1243dSDimitry Andric if (!scratch_ts_sp)
735480093f4SDimitry Andric return LLDB_INVALID_ADDRESS;
736480093f4SDimitry Andric
7370b57cec5SDimitry Andric CompilerType clang_void_ptr_type =
738*bdd1243dSDimitry Andric scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
7390b57cec5SDimitry Andric Status error;
7400b57cec5SDimitry Andric
7410b57cec5SDimitry Andric impl_function_caller = m_impl_code->MakeFunctionCaller(
7420b57cec5SDimitry Andric clang_void_ptr_type, dispatch_values, thread_sp, error);
7430b57cec5SDimitry Andric if (error.Fail()) {
7449dba64beSDimitry Andric LLDB_LOGF(log,
7450b57cec5SDimitry Andric "Error getting function caller for dispatch lookup: \"%s\".",
7460b57cec5SDimitry Andric error.AsCString());
7470b57cec5SDimitry Andric return args_addr;
7480b57cec5SDimitry Andric }
7490b57cec5SDimitry Andric } else {
7500b57cec5SDimitry Andric impl_function_caller = m_impl_code->GetFunctionCaller();
7510b57cec5SDimitry Andric }
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric
7540b57cec5SDimitry Andric // Now write down the argument values for this particular call.
7550b57cec5SDimitry Andric // This looks like it might be a race condition if other threads
7560b57cec5SDimitry Andric // were calling into here, but actually it isn't because we allocate
7570b57cec5SDimitry Andric // a new args structure for this call by passing args_addr =
7580b57cec5SDimitry Andric // LLDB_INVALID_ADDRESS...
7590b57cec5SDimitry Andric
760e8d8bef9SDimitry Andric DiagnosticManager diagnostics;
7610b57cec5SDimitry Andric if (!impl_function_caller->WriteFunctionArguments(
7620b57cec5SDimitry Andric exe_ctx, args_addr, dispatch_values, diagnostics)) {
7630b57cec5SDimitry Andric if (log) {
7649dba64beSDimitry Andric LLDB_LOGF(log, "Error writing function arguments.");
7650b57cec5SDimitry Andric diagnostics.Dump(log);
7660b57cec5SDimitry Andric }
7670b57cec5SDimitry Andric return args_addr;
7680b57cec5SDimitry Andric }
7690b57cec5SDimitry Andric
7700b57cec5SDimitry Andric return args_addr;
7710b57cec5SDimitry Andric }
7720b57cec5SDimitry Andric
7735ffd83dbSDimitry Andric const AppleObjCTrampolineHandler::DispatchFunction *
FindDispatchFunction(lldb::addr_t addr)7745ffd83dbSDimitry Andric AppleObjCTrampolineHandler::FindDispatchFunction(lldb::addr_t addr) {
7755ffd83dbSDimitry Andric MsgsendMap::iterator pos;
7765ffd83dbSDimitry Andric pos = m_msgSend_map.find(addr);
7775ffd83dbSDimitry Andric if (pos != m_msgSend_map.end()) {
7785ffd83dbSDimitry Andric return &g_dispatch_functions[(*pos).second];
7795ffd83dbSDimitry Andric }
7805ffd83dbSDimitry Andric return nullptr;
7815ffd83dbSDimitry Andric }
7825ffd83dbSDimitry Andric
ForEachDispatchFunction(std::function<void (lldb::addr_t,const DispatchFunction &)> callback)783fcaf7f86SDimitry Andric void AppleObjCTrampolineHandler::ForEachDispatchFunction(
784fcaf7f86SDimitry Andric std::function<void(lldb::addr_t, const DispatchFunction &)> callback) {
7855ffd83dbSDimitry Andric for (auto elem : m_msgSend_map) {
7865ffd83dbSDimitry Andric callback(elem.first, g_dispatch_functions[elem.second]);
7875ffd83dbSDimitry Andric }
7885ffd83dbSDimitry Andric }
7895ffd83dbSDimitry Andric
7900b57cec5SDimitry Andric ThreadPlanSP
GetStepThroughDispatchPlan(Thread & thread,bool stop_others)7910b57cec5SDimitry Andric AppleObjCTrampolineHandler::GetStepThroughDispatchPlan(Thread &thread,
7920b57cec5SDimitry Andric bool stop_others) {
7930b57cec5SDimitry Andric ThreadPlanSP ret_plan_sp;
7940b57cec5SDimitry Andric lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
7950b57cec5SDimitry Andric
79604eeddc0SDimitry Andric DispatchFunction vtable_dispatch = {"vtable", false, false, false,
79704eeddc0SDimitry Andric DispatchFunction::eFixUpFixed};
79881ad6265SDimitry Andric // The selector specific stubs are a wrapper for objc_msgSend. They don't get
79981ad6265SDimitry Andric // passed a SEL, but instead the selector string is encoded in the stub
80081ad6265SDimitry Andric // name, in the form:
80181ad6265SDimitry Andric // objc_msgSend$SelectorName
80281ad6265SDimitry Andric // and the stub figures out the uniqued selector. If we find ourselves in
80381ad6265SDimitry Andric // one of these stubs, we strip off the selector string and pass that to the
80481ad6265SDimitry Andric // implementation finder function, which looks up the SEL (you have to do this
80581ad6265SDimitry Andric // in process) and passes that to the runtime lookup function.
80681ad6265SDimitry Andric DispatchFunction sel_stub_dispatch = {"sel-specific-stub", false, false,
80781ad6265SDimitry Andric false, DispatchFunction::eFixUpNone};
8080b57cec5SDimitry Andric
80981ad6265SDimitry Andric // First step is to see if we're in a selector-specific dispatch stub.
81081ad6265SDimitry Andric // Those are of the form _objc_msgSend$<SELECTOR>, so see if the current
81181ad6265SDimitry Andric // function has that name:
81281ad6265SDimitry Andric Address func_addr;
81381ad6265SDimitry Andric Target &target = thread.GetProcess()->GetTarget();
81481ad6265SDimitry Andric llvm::StringRef sym_name;
81581ad6265SDimitry Andric const DispatchFunction *this_dispatch = nullptr;
81681ad6265SDimitry Andric
81781ad6265SDimitry Andric if (target.ResolveLoadAddress(curr_pc, func_addr)) {
81881ad6265SDimitry Andric Symbol *curr_sym = func_addr.CalculateSymbolContextSymbol();
81981ad6265SDimitry Andric if (curr_sym)
82081ad6265SDimitry Andric sym_name = curr_sym->GetName().GetStringRef();
82181ad6265SDimitry Andric
82281ad6265SDimitry Andric if (!sym_name.empty() && !sym_name.consume_front("objc_msgSend$"))
82381ad6265SDimitry Andric sym_name = {};
82481ad6265SDimitry Andric else
82581ad6265SDimitry Andric this_dispatch = &sel_stub_dispatch;
82681ad6265SDimitry Andric }
82781ad6265SDimitry Andric bool in_selector_stub = !sym_name.empty();
82881ad6265SDimitry Andric // Second step is to look and see if we are in one of the known ObjC
8290b57cec5SDimitry Andric // dispatch functions. We've already compiled a table of same, so
8300b57cec5SDimitry Andric // consult it.
8310b57cec5SDimitry Andric
83281ad6265SDimitry Andric if (!in_selector_stub)
83381ad6265SDimitry Andric this_dispatch = FindDispatchFunction(curr_pc);
8340b57cec5SDimitry Andric
8350b57cec5SDimitry Andric // Next check to see if we are in a vtable region:
8360b57cec5SDimitry Andric
8375ffd83dbSDimitry Andric if (!this_dispatch && m_vtables_up) {
8380b57cec5SDimitry Andric uint32_t flags;
8395ffd83dbSDimitry Andric if (m_vtables_up->IsAddressInVTables(curr_pc, flags)) {
8405ffd83dbSDimitry Andric vtable_dispatch.stret_return =
8410b57cec5SDimitry Andric (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) ==
8420b57cec5SDimitry Andric AppleObjCVTables::eOBJC_TRAMPOLINE_STRET;
8435ffd83dbSDimitry Andric this_dispatch = &vtable_dispatch;
8440b57cec5SDimitry Andric }
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric
84781ad6265SDimitry Andric // Since we set this_dispatch in both the vtable & sel specific stub cases
84881ad6265SDimitry Andric // this if will be used for all three of those cases.
8495ffd83dbSDimitry Andric if (this_dispatch) {
85081ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
8510b57cec5SDimitry Andric
8520b57cec5SDimitry Andric // We are decoding a method dispatch. First job is to pull the
85381ad6265SDimitry Andric // arguments out. If we are in a regular stub, we get self & selector,
85481ad6265SDimitry Andric // but if we are in a selector-specific stub, we'll have to get that from
85581ad6265SDimitry Andric // the string sym_name.
8560b57cec5SDimitry Andric
8570b57cec5SDimitry Andric lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
8580b57cec5SDimitry Andric
8590b57cec5SDimitry Andric const ABI *abi = nullptr;
8600b57cec5SDimitry Andric ProcessSP process_sp(thread.CalculateProcess());
8610b57cec5SDimitry Andric if (process_sp)
8620b57cec5SDimitry Andric abi = process_sp->GetABI().get();
8630b57cec5SDimitry Andric if (abi == nullptr)
8640b57cec5SDimitry Andric return ret_plan_sp;
8650b57cec5SDimitry Andric
8660b57cec5SDimitry Andric TargetSP target_sp(thread.CalculateTarget());
8670b57cec5SDimitry Andric
868*bdd1243dSDimitry Andric TypeSystemClangSP scratch_ts_sp =
869e8d8bef9SDimitry Andric ScratchTypeSystemClang::GetForTarget(*target_sp);
870*bdd1243dSDimitry Andric if (!scratch_ts_sp)
871480093f4SDimitry Andric return ret_plan_sp;
872480093f4SDimitry Andric
8730b57cec5SDimitry Andric ValueList argument_values;
8740b57cec5SDimitry Andric Value void_ptr_value;
8750b57cec5SDimitry Andric CompilerType clang_void_ptr_type =
876*bdd1243dSDimitry Andric scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
877fe6060f1SDimitry Andric void_ptr_value.SetValueType(Value::ValueType::Scalar);
8780b57cec5SDimitry Andric // void_ptr_value.SetContext (Value::eContextTypeClangType,
8790b57cec5SDimitry Andric // clang_void_ptr_type);
8800b57cec5SDimitry Andric void_ptr_value.SetCompilerType(clang_void_ptr_type);
8810b57cec5SDimitry Andric
8820b57cec5SDimitry Andric int obj_index;
8830b57cec5SDimitry Andric int sel_index;
8840b57cec5SDimitry Andric
88581ad6265SDimitry Andric // If this is a selector-specific stub then just push one value, 'cause
88681ad6265SDimitry Andric // we only get the object.
8870b57cec5SDimitry Andric // If this is a struct return dispatch, then the first argument is
8880b57cec5SDimitry Andric // the return struct pointer, and the object is the second, and
88981ad6265SDimitry Andric // the selector is the third.
89081ad6265SDimitry Andric // Otherwise the object is the first and the selector the second.
89181ad6265SDimitry Andric if (in_selector_stub) {
89281ad6265SDimitry Andric obj_index = 0;
89381ad6265SDimitry Andric sel_index = 1;
89481ad6265SDimitry Andric argument_values.PushValue(void_ptr_value);
89581ad6265SDimitry Andric } else if (this_dispatch->stret_return) {
8960b57cec5SDimitry Andric obj_index = 1;
8970b57cec5SDimitry Andric sel_index = 2;
8980b57cec5SDimitry Andric argument_values.PushValue(void_ptr_value);
8990b57cec5SDimitry Andric argument_values.PushValue(void_ptr_value);
9000b57cec5SDimitry Andric argument_values.PushValue(void_ptr_value);
9010b57cec5SDimitry Andric } else {
9020b57cec5SDimitry Andric obj_index = 0;
9030b57cec5SDimitry Andric sel_index = 1;
9040b57cec5SDimitry Andric argument_values.PushValue(void_ptr_value);
9050b57cec5SDimitry Andric argument_values.PushValue(void_ptr_value);
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric
9080b57cec5SDimitry Andric bool success = abi->GetArgumentValues(thread, argument_values);
9090b57cec5SDimitry Andric if (!success)
9100b57cec5SDimitry Andric return ret_plan_sp;
9110b57cec5SDimitry Andric
9120b57cec5SDimitry Andric lldb::addr_t obj_addr =
9130b57cec5SDimitry Andric argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong();
9140b57cec5SDimitry Andric if (obj_addr == 0x0) {
9159dba64beSDimitry Andric LLDB_LOGF(
9169dba64beSDimitry Andric log,
9170b57cec5SDimitry Andric "Asked to step to dispatch to nil object, returning empty plan.");
9180b57cec5SDimitry Andric return ret_plan_sp;
9190b57cec5SDimitry Andric }
9200b57cec5SDimitry Andric
9210b57cec5SDimitry Andric ExecutionContext exe_ctx(thread.shared_from_this());
9220b57cec5SDimitry Andric // isa_addr will store the class pointer that the method is being
9230b57cec5SDimitry Andric // dispatched to - so either the class directly or the super class
9240b57cec5SDimitry Andric // if this is one of the objc_msgSendSuper flavors. That's mostly
9250b57cec5SDimitry Andric // used to look up the class/selector pair in our cache.
9260b57cec5SDimitry Andric
9270b57cec5SDimitry Andric lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS;
92881ad6265SDimitry Andric lldb::addr_t sel_addr = LLDB_INVALID_ADDRESS;
92981ad6265SDimitry Andric // If we are not in a selector stub, get the sel address from the arguments.
93081ad6265SDimitry Andric if (!in_selector_stub)
93181ad6265SDimitry Andric sel_addr =
9320b57cec5SDimitry Andric argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong();
9330b57cec5SDimitry Andric
9340b57cec5SDimitry Andric // Figure out the class this is being dispatched to and see if
9350b57cec5SDimitry Andric // we've already cached this method call, If so we can push a
9360b57cec5SDimitry Andric // run-to-address plan directly. Otherwise we have to figure out
9370b57cec5SDimitry Andric // where the implementation lives.
9380b57cec5SDimitry Andric
9395ffd83dbSDimitry Andric if (this_dispatch->is_super) {
9405ffd83dbSDimitry Andric if (this_dispatch->is_super2) {
9410b57cec5SDimitry Andric // In the objc_msgSendSuper2 case, we don't get the object
9420b57cec5SDimitry Andric // directly, we get a structure containing the object and the
9430b57cec5SDimitry Andric // class to which the super message is being sent. So we need
9440b57cec5SDimitry Andric // to dig the super out of the class and use that.
9450b57cec5SDimitry Andric
9460b57cec5SDimitry Andric Value super_value(*(argument_values.GetValueAtIndex(obj_index)));
94781ad6265SDimitry Andric super_value.GetScalar() += process_sp->GetAddressByteSize();
9480b57cec5SDimitry Andric super_value.ResolveValue(&exe_ctx);
9490b57cec5SDimitry Andric
9500b57cec5SDimitry Andric if (super_value.GetScalar().IsValid()) {
9510b57cec5SDimitry Andric
9520b57cec5SDimitry Andric // isa_value now holds the class pointer. The second word of the
9530b57cec5SDimitry Andric // class pointer is the super-class pointer:
95481ad6265SDimitry Andric super_value.GetScalar() += process_sp->GetAddressByteSize();
9550b57cec5SDimitry Andric super_value.ResolveValue(&exe_ctx);
9560b57cec5SDimitry Andric if (super_value.GetScalar().IsValid())
9570b57cec5SDimitry Andric isa_addr = super_value.GetScalar().ULongLong();
9580b57cec5SDimitry Andric else {
9599dba64beSDimitry Andric LLDB_LOGF(log, "Failed to extract the super class value from the "
9600b57cec5SDimitry Andric "class in objc_super.");
9610b57cec5SDimitry Andric }
9620b57cec5SDimitry Andric } else {
9639dba64beSDimitry Andric LLDB_LOGF(log, "Failed to extract the class value from objc_super.");
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric } else {
9660b57cec5SDimitry Andric // In the objc_msgSendSuper case, we don't get the object
9670b57cec5SDimitry Andric // directly, we get a two element structure containing the
9680b57cec5SDimitry Andric // object and the super class to which the super message is
9690b57cec5SDimitry Andric // being sent. So the class we want is the second element of
9700b57cec5SDimitry Andric // this structure.
9710b57cec5SDimitry Andric
9720b57cec5SDimitry Andric Value super_value(*(argument_values.GetValueAtIndex(obj_index)));
97381ad6265SDimitry Andric super_value.GetScalar() += process_sp->GetAddressByteSize();
9740b57cec5SDimitry Andric super_value.ResolveValue(&exe_ctx);
9750b57cec5SDimitry Andric
9760b57cec5SDimitry Andric if (super_value.GetScalar().IsValid()) {
9770b57cec5SDimitry Andric isa_addr = super_value.GetScalar().ULongLong();
9780b57cec5SDimitry Andric } else {
9799dba64beSDimitry Andric LLDB_LOGF(log, "Failed to extract the class value from objc_super.");
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric }
9820b57cec5SDimitry Andric } else {
9830b57cec5SDimitry Andric // In the direct dispatch case, the object->isa is the class pointer we
9840b57cec5SDimitry Andric // want.
9850b57cec5SDimitry Andric
9860b57cec5SDimitry Andric // This is a little cheesy, but since object->isa is the first field,
9870b57cec5SDimitry Andric // making the object value a load address value and resolving it will get
9880b57cec5SDimitry Andric // the pointer sized data pointed to by that value...
9890b57cec5SDimitry Andric
9900b57cec5SDimitry Andric // Note, it isn't a fatal error not to be able to get the
9910b57cec5SDimitry Andric // address from the object, since this might be a "tagged
9920b57cec5SDimitry Andric // pointer" which isn't a real object, but rather some word
9930b57cec5SDimitry Andric // length encoded dingus.
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric Value isa_value(*(argument_values.GetValueAtIndex(obj_index)));
9960b57cec5SDimitry Andric
997fe6060f1SDimitry Andric isa_value.SetValueType(Value::ValueType::LoadAddress);
9980b57cec5SDimitry Andric isa_value.ResolveValue(&exe_ctx);
9990b57cec5SDimitry Andric if (isa_value.GetScalar().IsValid()) {
10000b57cec5SDimitry Andric isa_addr = isa_value.GetScalar().ULongLong();
10010b57cec5SDimitry Andric } else {
10029dba64beSDimitry Andric LLDB_LOGF(log, "Failed to extract the isa value from object.");
10030b57cec5SDimitry Andric }
10040b57cec5SDimitry Andric }
10050b57cec5SDimitry Andric
10060b57cec5SDimitry Andric // Okay, we've got the address of the class for which we're resolving this,
10070b57cec5SDimitry Andric // let's see if it's in our cache:
10080b57cec5SDimitry Andric lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS;
100981ad6265SDimitry Andric // If this is a regular dispatch, look up the sel in our addr to sel cache:
10100b57cec5SDimitry Andric if (isa_addr != LLDB_INVALID_ADDRESS) {
10110b57cec5SDimitry Andric ObjCLanguageRuntime *objc_runtime =
10120b57cec5SDimitry Andric ObjCLanguageRuntime::Get(*thread.GetProcess());
10130b57cec5SDimitry Andric assert(objc_runtime != nullptr);
101481ad6265SDimitry Andric if (!in_selector_stub) {
101581ad6265SDimitry Andric LLDB_LOG(log, "Resolving call for class - {0} and selector - {1}",
101681ad6265SDimitry Andric isa_addr, sel_addr);
10170b57cec5SDimitry Andric impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sel_addr);
101881ad6265SDimitry Andric } else {
101981ad6265SDimitry Andric LLDB_LOG(log, "Resolving call for class - {0} and selector - {1}",
102081ad6265SDimitry Andric isa_addr, sym_name);
102181ad6265SDimitry Andric impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sym_name);
10220b57cec5SDimitry Andric }
102381ad6265SDimitry Andric }
102481ad6265SDimitry Andric // If it is a selector-specific stub dispatch, look in the string cache:
10250b57cec5SDimitry Andric
10260b57cec5SDimitry Andric if (impl_addr != LLDB_INVALID_ADDRESS) {
10270b57cec5SDimitry Andric // Yup, it was in the cache, so we can run to that address directly.
10280b57cec5SDimitry Andric
10299dba64beSDimitry Andric LLDB_LOGF(log, "Found implementation address in cache: 0x%" PRIx64,
10300b57cec5SDimitry Andric impl_addr);
10310b57cec5SDimitry Andric
10320b57cec5SDimitry Andric ret_plan_sp = std::make_shared<ThreadPlanRunToAddress>(thread, impl_addr,
10330b57cec5SDimitry Andric stop_others);
10340b57cec5SDimitry Andric } else {
10350b57cec5SDimitry Andric // We haven't seen this class/selector pair yet. Look it up.
10360b57cec5SDimitry Andric StreamString errors;
10370b57cec5SDimitry Andric Address impl_code_address;
10380b57cec5SDimitry Andric
10390b57cec5SDimitry Andric ValueList dispatch_values;
10400b57cec5SDimitry Andric
10410b57cec5SDimitry Andric // We've will inject a little function in the target that takes the
104281ad6265SDimitry Andric // object, selector/selector string and some flags,
10430b57cec5SDimitry Andric // and figures out the implementation. Looks like:
10440b57cec5SDimitry Andric // void *__lldb_objc_find_implementation_for_selector (void *object,
10450b57cec5SDimitry Andric // void *sel,
104681ad6265SDimitry Andric // int
104781ad6265SDimitry Andric // is_str_ptr,
10480b57cec5SDimitry Andric // int is_stret,
10490b57cec5SDimitry Andric // int is_super,
10500b57cec5SDimitry Andric // int is_super2,
10510b57cec5SDimitry Andric // int is_fixup,
10520b57cec5SDimitry Andric // int is_fixed,
10530b57cec5SDimitry Andric // int debug)
105481ad6265SDimitry Andric // If we don't have an actual SEL, but rather a string version of the
105581ad6265SDimitry Andric // selector WE injected, set is_str_ptr to true, and sel to the address
105681ad6265SDimitry Andric // of the string.
10570b57cec5SDimitry Andric // So set up the arguments for that call.
10580b57cec5SDimitry Andric
10590b57cec5SDimitry Andric dispatch_values.PushValue(*(argument_values.GetValueAtIndex(obj_index)));
106081ad6265SDimitry Andric lldb::addr_t sel_str_addr = LLDB_INVALID_ADDRESS;
106181ad6265SDimitry Andric if (!in_selector_stub) {
106281ad6265SDimitry Andric // If we don't have a selector string, push the selector from arguments.
106381ad6265SDimitry Andric dispatch_values.PushValue(
106481ad6265SDimitry Andric *(argument_values.GetValueAtIndex(sel_index)));
106581ad6265SDimitry Andric } else {
106681ad6265SDimitry Andric // Otherwise, inject the string into the target, and push that value for
106781ad6265SDimitry Andric // the sel argument.
106881ad6265SDimitry Andric Status error;
106981ad6265SDimitry Andric sel_str_addr = process_sp->AllocateMemory(
107081ad6265SDimitry Andric sym_name.size() + 1, ePermissionsReadable | ePermissionsWritable,
107181ad6265SDimitry Andric error);
107281ad6265SDimitry Andric if (sel_str_addr == LLDB_INVALID_ADDRESS || error.Fail()) {
107381ad6265SDimitry Andric LLDB_LOG(log,
107481ad6265SDimitry Andric "Could not allocate memory for selector string {0}: {1}",
107581ad6265SDimitry Andric sym_name, error);
107681ad6265SDimitry Andric return ret_plan_sp;
107781ad6265SDimitry Andric }
107881ad6265SDimitry Andric process_sp->WriteMemory(sel_str_addr, sym_name.str().c_str(),
107981ad6265SDimitry Andric sym_name.size() + 1, error);
108081ad6265SDimitry Andric if (error.Fail()) {
108181ad6265SDimitry Andric LLDB_LOG(log, "Could not write string to address {0}", sel_str_addr);
108281ad6265SDimitry Andric return ret_plan_sp;
108381ad6265SDimitry Andric }
108481ad6265SDimitry Andric Value sel_ptr_value(void_ptr_value);
108581ad6265SDimitry Andric sel_ptr_value.GetScalar() = sel_str_addr;
108681ad6265SDimitry Andric dispatch_values.PushValue(sel_ptr_value);
108781ad6265SDimitry Andric }
10880b57cec5SDimitry Andric
10890b57cec5SDimitry Andric Value flag_value;
10900b57cec5SDimitry Andric CompilerType clang_int_type =
1091*bdd1243dSDimitry Andric scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(
10920b57cec5SDimitry Andric lldb::eEncodingSint, 32);
1093fe6060f1SDimitry Andric flag_value.SetValueType(Value::ValueType::Scalar);
10940b57cec5SDimitry Andric // flag_value.SetContext (Value::eContextTypeClangType, clang_int_type);
10950b57cec5SDimitry Andric flag_value.SetCompilerType(clang_int_type);
10960b57cec5SDimitry Andric
109781ad6265SDimitry Andric if (in_selector_stub)
109881ad6265SDimitry Andric flag_value.GetScalar() = 1;
109981ad6265SDimitry Andric else
110081ad6265SDimitry Andric flag_value.GetScalar() = 0;
110181ad6265SDimitry Andric dispatch_values.PushValue(flag_value);
110281ad6265SDimitry Andric
11035ffd83dbSDimitry Andric if (this_dispatch->stret_return)
11040b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11050b57cec5SDimitry Andric else
11060b57cec5SDimitry Andric flag_value.GetScalar() = 0;
11070b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11080b57cec5SDimitry Andric
11095ffd83dbSDimitry Andric if (this_dispatch->is_super)
11100b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11110b57cec5SDimitry Andric else
11120b57cec5SDimitry Andric flag_value.GetScalar() = 0;
11130b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11140b57cec5SDimitry Andric
11155ffd83dbSDimitry Andric if (this_dispatch->is_super2)
11160b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11170b57cec5SDimitry Andric else
11180b57cec5SDimitry Andric flag_value.GetScalar() = 0;
11190b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11200b57cec5SDimitry Andric
11215ffd83dbSDimitry Andric switch (this_dispatch->fixedup) {
11220b57cec5SDimitry Andric case DispatchFunction::eFixUpNone:
11230b57cec5SDimitry Andric flag_value.GetScalar() = 0;
11240b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11250b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11260b57cec5SDimitry Andric break;
11270b57cec5SDimitry Andric case DispatchFunction::eFixUpFixed:
11280b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11290b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11300b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11310b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11320b57cec5SDimitry Andric break;
11330b57cec5SDimitry Andric case DispatchFunction::eFixUpToFix:
11340b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11350b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11360b57cec5SDimitry Andric flag_value.GetScalar() = 0;
11370b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11380b57cec5SDimitry Andric break;
11390b57cec5SDimitry Andric }
11400b57cec5SDimitry Andric if (log && log->GetVerbose())
11410b57cec5SDimitry Andric flag_value.GetScalar() = 1;
11420b57cec5SDimitry Andric else
11430b57cec5SDimitry Andric flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done.
11440b57cec5SDimitry Andric dispatch_values.PushValue(flag_value);
11450b57cec5SDimitry Andric
11460b57cec5SDimitry Andric ret_plan_sp = std::make_shared<AppleThreadPlanStepThroughObjCTrampoline>(
114781ad6265SDimitry Andric thread, *this, dispatch_values, isa_addr, sel_addr, sel_str_addr,
114881ad6265SDimitry Andric sym_name);
11490b57cec5SDimitry Andric if (log) {
11500b57cec5SDimitry Andric StreamString s;
11510b57cec5SDimitry Andric ret_plan_sp->GetDescription(&s, eDescriptionLevelFull);
11529dba64beSDimitry Andric LLDB_LOGF(log, "Using ObjC step plan: %s.\n", s.GetData());
11530b57cec5SDimitry Andric }
11540b57cec5SDimitry Andric }
11550b57cec5SDimitry Andric }
11560b57cec5SDimitry Andric
11575ffd83dbSDimitry Andric // Finally, check if we have hit an "optimized dispatch" function. This will
11585ffd83dbSDimitry Andric // either directly call the base implementation or dispatch an objc_msgSend
11595ffd83dbSDimitry Andric // if the method has been overridden. So we just do a "step in/step out",
11605ffd83dbSDimitry Andric // setting a breakpoint on objc_msgSend, and if we hit the msgSend, we
11615ffd83dbSDimitry Andric // will automatically step in again. That's the job of the
11625ffd83dbSDimitry Andric // AppleThreadPlanStepThroughDirectDispatch.
11635ffd83dbSDimitry Andric if (!this_dispatch && !ret_plan_sp) {
11645ffd83dbSDimitry Andric MsgsendMap::iterator pos;
11655ffd83dbSDimitry Andric pos = m_opt_dispatch_map.find(curr_pc);
11665ffd83dbSDimitry Andric if (pos != m_opt_dispatch_map.end()) {
11675ffd83dbSDimitry Andric const char *opt_name = g_opt_dispatch_names[(*pos).second];
11685ffd83dbSDimitry Andric ret_plan_sp = std::make_shared<AppleThreadPlanStepThroughDirectDispatch>(
1169fe6060f1SDimitry Andric thread, *this, opt_name);
11705ffd83dbSDimitry Andric }
11715ffd83dbSDimitry Andric }
11725ffd83dbSDimitry Andric
11730b57cec5SDimitry Andric return ret_plan_sp;
11740b57cec5SDimitry Andric }
11750b57cec5SDimitry Andric
11760b57cec5SDimitry Andric FunctionCaller *
GetLookupImplementationFunctionCaller()11770b57cec5SDimitry Andric AppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller() {
11780b57cec5SDimitry Andric return m_impl_code->GetFunctionCaller();
11790b57cec5SDimitry Andric }
1180