/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- AppleObjCTrampolineHandler.cpp ------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "AppleObjCTrampolineHandler.h" |
10 | | #include "AppleThreadPlanStepThroughObjCTrampoline.h" |
11 | | |
12 | | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
13 | | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
14 | | #include "lldb/Core/Debugger.h" |
15 | | #include "lldb/Core/Module.h" |
16 | | #include "lldb/Core/Value.h" |
17 | | #include "lldb/Expression/DiagnosticManager.h" |
18 | | #include "lldb/Expression/FunctionCaller.h" |
19 | | #include "lldb/Expression/UserExpression.h" |
20 | | #include "lldb/Expression/UtilityFunction.h" |
21 | | #include "lldb/Symbol/Symbol.h" |
22 | | #include "lldb/Target/ABI.h" |
23 | | #include "lldb/Target/ExecutionContext.h" |
24 | | #include "lldb/Target/Process.h" |
25 | | #include "lldb/Target/RegisterContext.h" |
26 | | #include "lldb/Target/Target.h" |
27 | | #include "lldb/Target/Thread.h" |
28 | | #include "lldb/Target/ThreadPlanRunToAddress.h" |
29 | | #include "lldb/Utility/ConstString.h" |
30 | | #include "lldb/Utility/FileSpec.h" |
31 | | #include "lldb/Utility/LLDBLog.h" |
32 | | #include "lldb/Utility/Log.h" |
33 | | |
34 | | #include "llvm/ADT/STLExtras.h" |
35 | | #include "llvm/ADT/ScopeExit.h" |
36 | | |
37 | | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
38 | | |
39 | | #include <memory> |
40 | | |
41 | | using namespace lldb; |
42 | | using namespace lldb_private; |
43 | | |
44 | | const char *AppleObjCTrampolineHandler::g_lookup_implementation_function_name = |
45 | | "__lldb_objc_find_implementation_for_selector"; |
46 | | const char *AppleObjCTrampolineHandler:: |
47 | | g_lookup_implementation_with_stret_function_code = |
48 | | R"( |
49 | | if (is_stret) { |
50 | | return_struct.impl_addr = |
51 | | class_getMethodImplementation_stret (return_struct.class_addr, |
52 | | return_struct.sel_addr); |
53 | | } else { |
54 | | return_struct.impl_addr = |
55 | | class_getMethodImplementation (return_struct.class_addr, |
56 | | return_struct.sel_addr); |
57 | | } |
58 | | if (debug) |
59 | | printf ("\n*** Returning implementation: %p.\n", |
60 | | return_struct.impl_addr); |
61 | | |
62 | | return return_struct.impl_addr; |
63 | | } |
64 | | )"; |
65 | | const char * |
66 | | AppleObjCTrampolineHandler::g_lookup_implementation_no_stret_function_code = |
67 | | R"( |
68 | | return_struct.impl_addr = |
69 | | class_getMethodImplementation (return_struct.class_addr, |
70 | | return_struct.sel_addr); |
71 | | if (debug) |
72 | | printf ("\n*** getMethodImpletation for addr: 0x%p sel: 0x%p result: 0x%p.\n", |
73 | | return_struct.class_addr, return_struct.sel_addr, return_struct.impl_addr); |
74 | | |
75 | | return return_struct.impl_addr; |
76 | | } |
77 | | )"; |
78 | | |
79 | | const char |
80 | | *AppleObjCTrampolineHandler::g_lookup_implementation_function_common_code = |
81 | | R"( |
82 | | extern "C" |
83 | | { |
84 | | extern void *class_getMethodImplementation(void *objc_class, void *sel); |
85 | | extern void *class_getMethodImplementation_stret(void *objc_class, void *sel); |
86 | | extern void * object_getClass (id object); |
87 | | extern void * sel_getUid(char *name); |
88 | | extern int printf(const char *format, ...); |
89 | | } |
90 | | extern "C" void * |
91 | | __lldb_objc_find_implementation_for_selector (void *object, |
92 | | void *sel, |
93 | | int is_str_ptr, |
94 | | int is_stret, |
95 | | int is_super, |
96 | | int is_super2, |
97 | | int is_fixup, |
98 | | int is_fixed, |
99 | | int debug) |
100 | | { |
101 | | struct __lldb_imp_return_struct { |
102 | | void *class_addr; |
103 | | void *sel_addr; |
104 | | void *impl_addr; |
105 | | }; |
106 | | |
107 | | struct __lldb_objc_class { |
108 | | void *isa; |
109 | | void *super_ptr; |
110 | | }; |
111 | | struct __lldb_objc_super { |
112 | | void *receiver; |
113 | | struct __lldb_objc_class *class_ptr; |
114 | | }; |
115 | | struct __lldb_msg_ref { |
116 | | void *dont_know; |
117 | | void *sel; |
118 | | }; |
119 | | |
120 | | struct __lldb_imp_return_struct return_struct; |
121 | | |
122 | | if (debug) |
123 | | printf ("\n*** Called with obj: %p sel: %p is_str_ptr: %d " |
124 | | "is_stret: %d is_super: %d, " |
125 | | "is_super2: %d, is_fixup: %d, is_fixed: %d\n", |
126 | | object, sel, is_str_ptr, is_stret, |
127 | | is_super, is_super2, is_fixup, is_fixed); |
128 | | |
129 | | if (is_str_ptr) { |
130 | | if (debug) |
131 | | printf("*** Turning string: '%s'", sel); |
132 | | sel = sel_getUid((char *)sel); |
133 | | if (debug) |
134 | | printf("*** into sel to %p", sel); |
135 | | } |
136 | | if (is_super) { |
137 | | if (is_super2) { |
138 | | return_struct.class_addr |
139 | | = ((__lldb_objc_super *) object)->class_ptr->super_ptr; |
140 | | } else { |
141 | | return_struct.class_addr = ((__lldb_objc_super *) object)->class_ptr; |
142 | | } |
143 | | if (debug) |
144 | | printf("*** Super, class addr: %p\n", return_struct.class_addr); |
145 | | } else { |
146 | | // This code seems a little funny, but has its reasons... |
147 | | // The call to [object class] is here because if this is a class, and has |
148 | | // not been called into yet, we need to do something to force the class to |
149 | | // initialize itself. |
150 | | // Then the call to object_getClass will actually return the correct class, |
151 | | // either the class if object is a class instance, or the meta-class if it |
152 | | // is a class pointer. |
153 | | void *class_ptr = (void *) [(id) object class]; |
154 | | return_struct.class_addr = (id) object_getClass((id) object); |
155 | | if (debug) { |
156 | | if (class_ptr == object) { |
157 | | printf ("Found a class object, need to return the meta class %p -> %p\n", |
158 | | class_ptr, return_struct.class_addr); |
159 | | } else { |
160 | | printf ("[object class] returned: %p object_getClass: %p.\n", |
161 | | class_ptr, return_struct.class_addr); |
162 | | } |
163 | | } |
164 | | } |
165 | | |
166 | | if (is_fixup) { |
167 | | if (is_fixed) { |
168 | | return_struct.sel_addr = ((__lldb_msg_ref *) sel)->sel; |
169 | | } else { |
170 | | char *sel_name = (char *) ((__lldb_msg_ref *) sel)->sel; |
171 | | return_struct.sel_addr = sel_getUid (sel_name); |
172 | | if (debug) |
173 | | printf ("\n*** Got fixed up selector: %p for name %s.\n", |
174 | | return_struct.sel_addr, sel_name); |
175 | | } |
176 | | } else { |
177 | | return_struct.sel_addr = sel; |
178 | | } |
179 | | )"; |
180 | | |
181 | | AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::VTableRegion( |
182 | | AppleObjCVTables *owner, lldb::addr_t header_addr) |
183 | 0 | : m_valid(true), m_owner(owner), m_header_addr(header_addr) { |
184 | 0 | SetUpRegion(); |
185 | 0 | } |
186 | | |
187 | 2.11k | AppleObjCTrampolineHandler::~AppleObjCTrampolineHandler() = default; |
188 | | |
189 | 0 | void AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::SetUpRegion() { |
190 | | // The header looks like: |
191 | | // |
192 | | // uint16_t headerSize |
193 | | // uint16_t descSize |
194 | | // uint32_t descCount |
195 | | // void * next |
196 | | // |
197 | | // First read in the header: |
198 | |
|
199 | 0 | char memory_buffer[16]; |
200 | 0 | ProcessSP process_sp = m_owner->GetProcessSP(); |
201 | 0 | if (!process_sp) |
202 | 0 | return; |
203 | 0 | DataExtractor data(memory_buffer, sizeof(memory_buffer), |
204 | 0 | process_sp->GetByteOrder(), |
205 | 0 | process_sp->GetAddressByteSize()); |
206 | 0 | size_t actual_size = 8 + process_sp->GetAddressByteSize(); |
207 | 0 | Status error; |
208 | 0 | size_t bytes_read = |
209 | 0 | process_sp->ReadMemory(m_header_addr, memory_buffer, actual_size, error); |
210 | 0 | if (bytes_read != actual_size) { |
211 | 0 | m_valid = false; |
212 | 0 | return; |
213 | 0 | } |
214 | | |
215 | 0 | lldb::offset_t offset = 0; |
216 | 0 | const uint16_t header_size = data.GetU16(&offset); |
217 | 0 | const uint16_t descriptor_size = data.GetU16(&offset); |
218 | 0 | const size_t num_descriptors = data.GetU32(&offset); |
219 | |
|
220 | 0 | m_next_region = data.GetAddress(&offset); |
221 | | |
222 | | // If the header size is 0, that means we've come in too early before this |
223 | | // data is set up. |
224 | | // Set ourselves as not valid, and continue. |
225 | 0 | if (header_size == 0 || num_descriptors == 0) { |
226 | 0 | m_valid = false; |
227 | 0 | return; |
228 | 0 | } |
229 | | |
230 | | // Now read in all the descriptors: |
231 | | // The descriptor looks like: |
232 | | // |
233 | | // uint32_t offset |
234 | | // uint32_t flags |
235 | | // |
236 | | // Where offset is either 0 - in which case it is unused, or it is |
237 | | // the offset of the vtable code from the beginning of the |
238 | | // descriptor record. Below, we'll convert that into an absolute |
239 | | // code address, since I don't want to have to compute it over and |
240 | | // over. |
241 | | |
242 | | // Ingest the whole descriptor array: |
243 | 0 | const lldb::addr_t desc_ptr = m_header_addr + header_size; |
244 | 0 | const size_t desc_array_size = num_descriptors * descriptor_size; |
245 | 0 | WritableDataBufferSP data_sp(new DataBufferHeap(desc_array_size, '\0')); |
246 | 0 | uint8_t *dst = (uint8_t *)data_sp->GetBytes(); |
247 | |
|
248 | 0 | DataExtractor desc_extractor(dst, desc_array_size, process_sp->GetByteOrder(), |
249 | 0 | process_sp->GetAddressByteSize()); |
250 | 0 | bytes_read = process_sp->ReadMemory(desc_ptr, dst, desc_array_size, error); |
251 | 0 | if (bytes_read != desc_array_size) { |
252 | 0 | m_valid = false; |
253 | 0 | return; |
254 | 0 | } |
255 | | |
256 | | // The actual code for the vtables will be laid out consecutively, so I also |
257 | | // compute the start and end of the whole code block. |
258 | | |
259 | 0 | offset = 0; |
260 | 0 | m_code_start_addr = 0; |
261 | 0 | m_code_end_addr = 0; |
262 | |
|
263 | 0 | for (size_t i = 0; i < num_descriptors; i++) { |
264 | 0 | lldb::addr_t start_offset = offset; |
265 | 0 | uint32_t voffset = desc_extractor.GetU32(&offset); |
266 | 0 | uint32_t flags = desc_extractor.GetU32(&offset); |
267 | 0 | lldb::addr_t code_addr = desc_ptr + start_offset + voffset; |
268 | 0 | m_descriptors.push_back(VTableDescriptor(flags, code_addr)); |
269 | |
|
270 | 0 | if (m_code_start_addr == 0 || code_addr < m_code_start_addr) |
271 | 0 | m_code_start_addr = code_addr; |
272 | 0 | if (code_addr > m_code_end_addr) |
273 | 0 | m_code_end_addr = code_addr; |
274 | |
|
275 | 0 | offset = start_offset + descriptor_size; |
276 | 0 | } |
277 | | // Finally, a little bird told me that all the vtable code blocks |
278 | | // are the same size. Let's compute the blocks and if they are all |
279 | | // the same add the size to the code end address: |
280 | 0 | lldb::addr_t code_size = 0; |
281 | 0 | bool all_the_same = true; |
282 | 0 | for (size_t i = 0; i < num_descriptors - 1; i++) { |
283 | 0 | lldb::addr_t this_size = |
284 | 0 | m_descriptors[i + 1].code_start - m_descriptors[i].code_start; |
285 | 0 | if (code_size == 0) |
286 | 0 | code_size = this_size; |
287 | 0 | else { |
288 | 0 | if (this_size != code_size) |
289 | 0 | all_the_same = false; |
290 | 0 | if (this_size > code_size) |
291 | 0 | code_size = this_size; |
292 | 0 | } |
293 | 0 | } |
294 | 0 | if (all_the_same) |
295 | 0 | m_code_end_addr += code_size; |
296 | 0 | } |
297 | | |
298 | | bool AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion:: |
299 | 0 | AddressInRegion(lldb::addr_t addr, uint32_t &flags) { |
300 | 0 | if (!IsValid()) |
301 | 0 | return false; |
302 | | |
303 | 0 | if (addr < m_code_start_addr || addr > m_code_end_addr) |
304 | 0 | return false; |
305 | | |
306 | 0 | std::vector<VTableDescriptor>::iterator pos, end = m_descriptors.end(); |
307 | 0 | for (pos = m_descriptors.begin(); pos != end; pos++) { |
308 | 0 | if (addr <= (*pos).code_start) { |
309 | 0 | flags = (*pos).flags; |
310 | 0 | return true; |
311 | 0 | } |
312 | 0 | } |
313 | 0 | return false; |
314 | 0 | } |
315 | | |
316 | | void AppleObjCTrampolineHandler::AppleObjCVTables::VTableRegion::Dump( |
317 | 0 | Stream &s) { |
318 | 0 | s.Printf("Header addr: 0x%" PRIx64 " Code start: 0x%" PRIx64 |
319 | 0 | " Code End: 0x%" PRIx64 " Next: 0x%" PRIx64 "\n", |
320 | 0 | m_header_addr, m_code_start_addr, m_code_end_addr, m_next_region); |
321 | 0 | size_t num_elements = m_descriptors.size(); |
322 | 0 | for (size_t i = 0; i < num_elements; i++) { |
323 | 0 | s.Indent(); |
324 | 0 | s.Printf("Code start: 0x%" PRIx64 " Flags: %d\n", |
325 | 0 | m_descriptors[i].code_start, m_descriptors[i].flags); |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | AppleObjCTrampolineHandler::AppleObjCVTables::AppleObjCVTables( |
330 | | const ProcessSP &process_sp, const ModuleSP &objc_module_sp) |
331 | 2.11k | : m_process_wp(), m_trampoline_header(LLDB_INVALID_ADDRESS), |
332 | 2.11k | m_trampolines_changed_bp_id(LLDB_INVALID_BREAK_ID), |
333 | 2.11k | m_objc_module_sp(objc_module_sp) { |
334 | 2.11k | if (process_sp) |
335 | 2.11k | m_process_wp = process_sp; |
336 | 2.11k | } |
337 | | |
338 | 2.11k | AppleObjCTrampolineHandler::AppleObjCVTables::~AppleObjCVTables() { |
339 | 2.11k | ProcessSP process_sp = GetProcessSP(); |
340 | 2.11k | if (process_sp) { |
341 | 2.11k | if (m_trampolines_changed_bp_id != LLDB_INVALID_BREAK_ID) |
342 | 0 | process_sp->GetTarget().RemoveBreakpointByID(m_trampolines_changed_bp_id); |
343 | 2.11k | } |
344 | 2.11k | } |
345 | | |
346 | 2.11k | bool AppleObjCTrampolineHandler::AppleObjCVTables::InitializeVTableSymbols() { |
347 | 2.11k | if (m_trampoline_header != LLDB_INVALID_ADDRESS) |
348 | 0 | return true; |
349 | | |
350 | 2.11k | ProcessSP process_sp = GetProcessSP(); |
351 | 2.11k | if (process_sp) { |
352 | 2.11k | Target &target = process_sp->GetTarget(); |
353 | | |
354 | 2.11k | if (!m_objc_module_sp) { |
355 | 0 | for (ModuleSP module_sp : target.GetImages().Modules()) { |
356 | 0 | if (ObjCLanguageRuntime::Get(*process_sp) |
357 | 0 | ->IsModuleObjCLibrary(module_sp)) { |
358 | 0 | m_objc_module_sp = module_sp; |
359 | 0 | break; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | 2.11k | if (m_objc_module_sp) { |
365 | 2.11k | ConstString trampoline_name("gdb_objc_trampolines"); |
366 | 2.11k | const Symbol *trampoline_symbol = |
367 | 2.11k | m_objc_module_sp->FindFirstSymbolWithNameAndType(trampoline_name, |
368 | 2.11k | eSymbolTypeData); |
369 | 2.11k | if (trampoline_symbol != nullptr) { |
370 | 0 | m_trampoline_header = trampoline_symbol->GetLoadAddress(&target); |
371 | 0 | if (m_trampoline_header == LLDB_INVALID_ADDRESS) |
372 | 0 | return false; |
373 | | |
374 | | // Next look up the "changed" symbol and set a breakpoint on that... |
375 | 0 | ConstString changed_name("gdb_objc_trampolines_changed"); |
376 | 0 | const Symbol *changed_symbol = |
377 | 0 | m_objc_module_sp->FindFirstSymbolWithNameAndType(changed_name, |
378 | 0 | eSymbolTypeCode); |
379 | 0 | if (changed_symbol != nullptr) { |
380 | 0 | const Address changed_symbol_addr = changed_symbol->GetAddress(); |
381 | 0 | if (!changed_symbol_addr.IsValid()) |
382 | 0 | return false; |
383 | | |
384 | 0 | lldb::addr_t changed_addr = |
385 | 0 | changed_symbol_addr.GetOpcodeLoadAddress(&target); |
386 | 0 | if (changed_addr != LLDB_INVALID_ADDRESS) { |
387 | 0 | BreakpointSP trampolines_changed_bp_sp = |
388 | 0 | target.CreateBreakpoint(changed_addr, true, false); |
389 | 0 | if (trampolines_changed_bp_sp) { |
390 | 0 | m_trampolines_changed_bp_id = trampolines_changed_bp_sp->GetID(); |
391 | 0 | trampolines_changed_bp_sp->SetCallback(RefreshTrampolines, this, |
392 | 0 | true); |
393 | 0 | trampolines_changed_bp_sp->SetBreakpointKind( |
394 | 0 | "objc-trampolines-changed"); |
395 | 0 | return true; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | } |
399 | 0 | } |
400 | 2.11k | } |
401 | 2.11k | } |
402 | 2.11k | return false; |
403 | 2.11k | } |
404 | | |
405 | | bool AppleObjCTrampolineHandler::AppleObjCVTables::RefreshTrampolines( |
406 | | void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, |
407 | 0 | lldb::user_id_t break_loc_id) { |
408 | 0 | AppleObjCVTables *vtable_handler = (AppleObjCVTables *)baton; |
409 | 0 | if (vtable_handler->InitializeVTableSymbols()) { |
410 | | // The Update function is called with the address of an added region. So we |
411 | | // grab that address, and |
412 | | // feed it into ReadRegions. Of course, our friend the ABI will get the |
413 | | // values for us. |
414 | 0 | ExecutionContext exe_ctx(context->exe_ctx_ref); |
415 | 0 | Process *process = exe_ctx.GetProcessPtr(); |
416 | 0 | const ABI *abi = process->GetABI().get(); |
417 | |
|
418 | 0 | TypeSystemClangSP scratch_ts_sp = |
419 | 0 | ScratchTypeSystemClang::GetForTarget(process->GetTarget()); |
420 | 0 | if (!scratch_ts_sp) |
421 | 0 | return false; |
422 | | |
423 | 0 | ValueList argument_values; |
424 | 0 | Value input_value; |
425 | 0 | CompilerType clang_void_ptr_type = |
426 | 0 | scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType(); |
427 | |
|
428 | 0 | input_value.SetValueType(Value::ValueType::Scalar); |
429 | | // input_value.SetContext (Value::eContextTypeClangType, |
430 | | // clang_void_ptr_type); |
431 | 0 | input_value.SetCompilerType(clang_void_ptr_type); |
432 | 0 | argument_values.PushValue(input_value); |
433 | |
|
434 | 0 | bool success = |
435 | 0 | abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values); |
436 | 0 | if (!success) |
437 | 0 | return false; |
438 | | |
439 | | // Now get a pointer value from the zeroth argument. |
440 | 0 | Status error; |
441 | 0 | DataExtractor data; |
442 | 0 | error = argument_values.GetValueAtIndex(0)->GetValueAsData(&exe_ctx, data, |
443 | 0 | nullptr); |
444 | 0 | lldb::offset_t offset = 0; |
445 | 0 | lldb::addr_t region_addr = data.GetAddress(&offset); |
446 | |
|
447 | 0 | if (region_addr != 0) |
448 | 0 | vtable_handler->ReadRegions(region_addr); |
449 | 0 | } |
450 | 0 | return false; |
451 | 0 | } |
452 | | |
453 | 2.11k | bool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions() { |
454 | | // The no argument version reads the start region from the value of |
455 | | // the gdb_regions_header, and gets started from there. |
456 | | |
457 | 2.11k | m_regions.clear(); |
458 | 2.11k | if (!InitializeVTableSymbols()) |
459 | 2.11k | return false; |
460 | 0 | Status error; |
461 | 0 | ProcessSP process_sp = GetProcessSP(); |
462 | 0 | if (process_sp) { |
463 | 0 | lldb::addr_t region_addr = |
464 | 0 | process_sp->ReadPointerFromMemory(m_trampoline_header, error); |
465 | 0 | if (error.Success()) |
466 | 0 | return ReadRegions(region_addr); |
467 | 0 | } |
468 | 0 | return false; |
469 | 0 | } |
470 | | |
471 | | bool AppleObjCTrampolineHandler::AppleObjCVTables::ReadRegions( |
472 | 0 | lldb::addr_t region_addr) { |
473 | 0 | ProcessSP process_sp = GetProcessSP(); |
474 | 0 | if (!process_sp) |
475 | 0 | return false; |
476 | | |
477 | 0 | Log *log = GetLog(LLDBLog::Step); |
478 | | |
479 | | // We aren't starting at the trampoline symbol. |
480 | 0 | InitializeVTableSymbols(); |
481 | 0 | lldb::addr_t next_region = region_addr; |
482 | | |
483 | | // Read in the sizes of the headers. |
484 | 0 | while (next_region != 0) { |
485 | 0 | m_regions.push_back(VTableRegion(this, next_region)); |
486 | 0 | if (!m_regions.back().IsValid()) { |
487 | 0 | m_regions.clear(); |
488 | 0 | return false; |
489 | 0 | } |
490 | 0 | if (log) { |
491 | 0 | StreamString s; |
492 | 0 | m_regions.back().Dump(s); |
493 | 0 | LLDB_LOGF(log, "Read vtable region: \n%s", s.GetData()); |
494 | 0 | } |
495 | |
|
496 | 0 | next_region = m_regions.back().GetNextRegionAddr(); |
497 | 0 | } |
498 | | |
499 | 0 | return true; |
500 | 0 | } |
501 | | |
502 | | bool AppleObjCTrampolineHandler::AppleObjCVTables::IsAddressInVTables( |
503 | 363 | lldb::addr_t addr, uint32_t &flags) { |
504 | 363 | region_collection::iterator pos, end = m_regions.end(); |
505 | 363 | for (pos = m_regions.begin(); pos != end; pos++0 ) { |
506 | 0 | if ((*pos).AddressInRegion(addr, flags)) |
507 | 0 | return true; |
508 | 0 | } |
509 | 363 | return false; |
510 | 363 | } |
511 | | |
512 | | const AppleObjCTrampolineHandler::DispatchFunction |
513 | | AppleObjCTrampolineHandler::g_dispatch_functions[] = { |
514 | | // NAME STRET SUPER SUPER2 FIXUP TYPE |
515 | | {"objc_msgSend", false, false, false, DispatchFunction::eFixUpNone}, |
516 | | {"objc_msgSend_fixup", false, false, false, |
517 | | DispatchFunction::eFixUpToFix}, |
518 | | {"objc_msgSend_fixedup", false, false, false, |
519 | | DispatchFunction::eFixUpFixed}, |
520 | | {"objc_msgSend_stret", true, false, false, |
521 | | DispatchFunction::eFixUpNone}, |
522 | | {"objc_msgSend_stret_fixup", true, false, false, |
523 | | DispatchFunction::eFixUpToFix}, |
524 | | {"objc_msgSend_stret_fixedup", true, false, false, |
525 | | DispatchFunction::eFixUpFixed}, |
526 | | {"objc_msgSend_fpret", false, false, false, |
527 | | DispatchFunction::eFixUpNone}, |
528 | | {"objc_msgSend_fpret_fixup", false, false, false, |
529 | | DispatchFunction::eFixUpToFix}, |
530 | | {"objc_msgSend_fpret_fixedup", false, false, false, |
531 | | DispatchFunction::eFixUpFixed}, |
532 | | {"objc_msgSend_fp2ret", false, false, true, |
533 | | DispatchFunction::eFixUpNone}, |
534 | | {"objc_msgSend_fp2ret_fixup", false, false, true, |
535 | | DispatchFunction::eFixUpToFix}, |
536 | | {"objc_msgSend_fp2ret_fixedup", false, false, true, |
537 | | DispatchFunction::eFixUpFixed}, |
538 | | {"objc_msgSendSuper", false, true, false, DispatchFunction::eFixUpNone}, |
539 | | {"objc_msgSendSuper_stret", true, true, false, |
540 | | DispatchFunction::eFixUpNone}, |
541 | | {"objc_msgSendSuper2", false, true, true, DispatchFunction::eFixUpNone}, |
542 | | {"objc_msgSendSuper2_fixup", false, true, true, |
543 | | DispatchFunction::eFixUpToFix}, |
544 | | {"objc_msgSendSuper2_fixedup", false, true, true, |
545 | | DispatchFunction::eFixUpFixed}, |
546 | | {"objc_msgSendSuper2_stret", true, true, true, |
547 | | DispatchFunction::eFixUpNone}, |
548 | | {"objc_msgSendSuper2_stret_fixup", true, true, true, |
549 | | DispatchFunction::eFixUpToFix}, |
550 | | {"objc_msgSendSuper2_stret_fixedup", true, true, true, |
551 | | DispatchFunction::eFixUpFixed}, |
552 | | }; |
553 | | |
554 | | // This is the table of ObjC "accelerated dispatch" functions. They are a set |
555 | | // of objc methods that are "seldom overridden" and so the compiler replaces the |
556 | | // objc_msgSend with a call to one of the dispatch functions. That will check |
557 | | // whether the method has been overridden, and directly call the Foundation |
558 | | // implementation if not. |
559 | | // This table is supposed to be complete. If ones get added in the future, we |
560 | | // will have to add them to the table. |
561 | | const char *AppleObjCTrampolineHandler::g_opt_dispatch_names[] = { |
562 | | "objc_alloc", |
563 | | "objc_autorelease", |
564 | | "objc_release", |
565 | | "objc_retain", |
566 | | "objc_alloc_init", |
567 | | "objc_allocWithZone", |
568 | | "objc_opt_class", |
569 | | "objc_opt_isKindOfClass", |
570 | | "objc_opt_new", |
571 | | "objc_opt_respondsToSelector", |
572 | | "objc_opt_self", |
573 | | }; |
574 | | |
575 | | AppleObjCTrampolineHandler::AppleObjCTrampolineHandler( |
576 | | const ProcessSP &process_sp, const ModuleSP &objc_module_sp) |
577 | 2.11k | : m_process_wp(), m_objc_module_sp(objc_module_sp), |
578 | | m_impl_fn_addr(LLDB_INVALID_ADDRESS), |
579 | | m_impl_stret_fn_addr(LLDB_INVALID_ADDRESS), |
580 | | m_msg_forward_addr(LLDB_INVALID_ADDRESS), |
581 | 2.11k | m_msg_forward_stret_addr(LLDB_INVALID_ADDRESS) { |
582 | 2.11k | if (process_sp) |
583 | 2.11k | m_process_wp = process_sp; |
584 | | // Look up the known resolution functions: |
585 | | |
586 | 2.11k | ConstString get_impl_name("class_getMethodImplementation"); |
587 | 2.11k | ConstString get_impl_stret_name("class_getMethodImplementation_stret"); |
588 | 2.11k | ConstString msg_forward_name("_objc_msgForward"); |
589 | 2.11k | ConstString msg_forward_stret_name("_objc_msgForward_stret"); |
590 | | |
591 | 2.11k | Target *target = process_sp ? &process_sp->GetTarget() : nullptr0 ; |
592 | 2.11k | const Symbol *class_getMethodImplementation = |
593 | 2.11k | m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_name, |
594 | 2.11k | eSymbolTypeCode); |
595 | 2.11k | const Symbol *class_getMethodImplementation_stret = |
596 | 2.11k | m_objc_module_sp->FindFirstSymbolWithNameAndType(get_impl_stret_name, |
597 | 2.11k | eSymbolTypeCode); |
598 | 2.11k | const Symbol *msg_forward = m_objc_module_sp->FindFirstSymbolWithNameAndType( |
599 | 2.11k | msg_forward_name, eSymbolTypeCode); |
600 | 2.11k | const Symbol *msg_forward_stret = |
601 | 2.11k | m_objc_module_sp->FindFirstSymbolWithNameAndType(msg_forward_stret_name, |
602 | 2.11k | eSymbolTypeCode); |
603 | | |
604 | 2.11k | if (class_getMethodImplementation) |
605 | 2.11k | m_impl_fn_addr = |
606 | 2.11k | class_getMethodImplementation->GetAddress().GetOpcodeLoadAddress( |
607 | 2.11k | target); |
608 | 2.11k | if (class_getMethodImplementation_stret) |
609 | 2.11k | m_impl_stret_fn_addr = |
610 | 2.11k | class_getMethodImplementation_stret->GetAddress().GetOpcodeLoadAddress( |
611 | 2.11k | target); |
612 | 2.11k | if (msg_forward) |
613 | 2.11k | m_msg_forward_addr = msg_forward->GetAddress().GetOpcodeLoadAddress(target); |
614 | 2.11k | if (msg_forward_stret) |
615 | 2.11k | m_msg_forward_stret_addr = |
616 | 2.11k | msg_forward_stret->GetAddress().GetOpcodeLoadAddress(target); |
617 | | |
618 | | // FIXME: Do some kind of logging here. |
619 | 2.11k | if (m_impl_fn_addr == LLDB_INVALID_ADDRESS) { |
620 | | // If we can't even find the ordinary get method implementation function, |
621 | | // then we aren't going to be able to |
622 | | // step through any method dispatches. Warn to that effect and get out of |
623 | | // here. |
624 | 0 | if (process_sp->CanJIT()) { |
625 | 0 | process_sp->GetTarget().GetDebugger().GetErrorStream().Printf( |
626 | 0 | "Could not find implementation lookup function \"%s\"" |
627 | 0 | " step in through ObjC method dispatch will not work.\n", |
628 | 0 | get_impl_name.AsCString()); |
629 | 0 | } |
630 | 0 | return; |
631 | 0 | } |
632 | | |
633 | | // We will either set the implementation to the _stret or non_stret version, |
634 | | // so either way it's safe to start filling the m_lookup_..._code here. |
635 | 2.11k | m_lookup_implementation_function_code.assign( |
636 | 2.11k | g_lookup_implementation_function_common_code); |
637 | | |
638 | 2.11k | if (m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) { |
639 | | // It there is no stret return lookup function, assume that it is the same |
640 | | // as the straight lookup: |
641 | 0 | m_impl_stret_fn_addr = m_impl_fn_addr; |
642 | | // Also we will use the version of the lookup code that doesn't rely on the |
643 | | // stret version of the function. |
644 | 0 | m_lookup_implementation_function_code.append( |
645 | 0 | g_lookup_implementation_no_stret_function_code); |
646 | 2.11k | } else { |
647 | 2.11k | m_lookup_implementation_function_code.append( |
648 | 2.11k | g_lookup_implementation_with_stret_function_code); |
649 | 2.11k | } |
650 | | |
651 | | // Look up the addresses for the objc dispatch functions and cache |
652 | | // them. For now I'm inspecting the symbol names dynamically to |
653 | | // figure out how to dispatch to them. If it becomes more |
654 | | // complicated than this we can turn the g_dispatch_functions char * |
655 | | // array into a template table, and populate the DispatchFunction |
656 | | // map from there. |
657 | | |
658 | 44.3k | for (size_t i = 0; i != std::size(g_dispatch_functions); i++42.2k ) { |
659 | 42.2k | ConstString name_const_str(g_dispatch_functions[i].name); |
660 | 42.2k | const Symbol *msgSend_symbol = |
661 | 42.2k | m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str, |
662 | 42.2k | eSymbolTypeCode); |
663 | 42.2k | if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) { |
664 | | // FIXME: Make g_dispatch_functions static table of |
665 | | // DispatchFunctions, and have the map be address->index. |
666 | | // Problem is we also need to lookup the dispatch function. For |
667 | | // now we could have a side table of stret & non-stret dispatch |
668 | | // functions. If that's as complex as it gets, we're fine. |
669 | | |
670 | 42.2k | lldb::addr_t sym_addr = |
671 | 42.2k | msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); |
672 | | |
673 | 42.2k | m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); |
674 | 42.2k | } |
675 | 42.2k | } |
676 | | |
677 | | // Similarly, cache the addresses of the "optimized dispatch" function. |
678 | 25.3k | for (size_t i = 0; i != std::size(g_opt_dispatch_names); i++23.2k ) { |
679 | 23.2k | ConstString name_const_str(g_opt_dispatch_names[i]); |
680 | 23.2k | const Symbol *msgSend_symbol = |
681 | 23.2k | m_objc_module_sp->FindFirstSymbolWithNameAndType(name_const_str, |
682 | 23.2k | eSymbolTypeCode); |
683 | 23.2k | if (msgSend_symbol && msgSend_symbol->ValueIsAddress()) { |
684 | 23.2k | lldb::addr_t sym_addr = |
685 | 23.2k | msgSend_symbol->GetAddressRef().GetOpcodeLoadAddress(target); |
686 | | |
687 | 23.2k | m_opt_dispatch_map.emplace(sym_addr, i); |
688 | 23.2k | } |
689 | 23.2k | } |
690 | | |
691 | | // Build our vtable dispatch handler here: |
692 | 2.11k | m_vtables_up = |
693 | 2.11k | std::make_unique<AppleObjCVTables>(process_sp, m_objc_module_sp); |
694 | 2.11k | if (m_vtables_up) |
695 | 2.11k | m_vtables_up->ReadRegions(); |
696 | 2.11k | } |
697 | | |
698 | | lldb::addr_t |
699 | | AppleObjCTrampolineHandler::SetupDispatchFunction(Thread &thread, |
700 | 27 | ValueList &dispatch_values) { |
701 | 27 | ThreadSP thread_sp(thread.shared_from_this()); |
702 | 27 | ExecutionContext exe_ctx(thread_sp); |
703 | 27 | Log *log = GetLog(LLDBLog::Step); |
704 | | |
705 | 27 | lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; |
706 | 27 | FunctionCaller *impl_function_caller = nullptr; |
707 | | |
708 | | // Scope for mutex locker: |
709 | 27 | { |
710 | 27 | std::lock_guard<std::mutex> guard(m_impl_function_mutex); |
711 | | |
712 | | // First stage is to make the ClangUtility to hold our injected function: |
713 | | |
714 | 27 | if (!m_impl_code) { |
715 | 5 | if (!m_lookup_implementation_function_code.empty()) { |
716 | 5 | auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction( |
717 | 5 | m_lookup_implementation_function_code, |
718 | 5 | g_lookup_implementation_function_name, eLanguageTypeC, exe_ctx); |
719 | 5 | if (!utility_fn_or_error) { |
720 | 0 | LLDB_LOG_ERROR( |
721 | 0 | log, utility_fn_or_error.takeError(), |
722 | 0 | "Failed to get Utility Function for implementation lookup: {0}."); |
723 | 0 | return args_addr; |
724 | 0 | } |
725 | 5 | m_impl_code = std::move(*utility_fn_or_error); |
726 | 5 | } else { |
727 | 0 | LLDB_LOGF(log, "No method lookup implementation code."); |
728 | 0 | return LLDB_INVALID_ADDRESS; |
729 | 0 | } |
730 | | |
731 | | // Next make the runner function for our implementation utility function. |
732 | 5 | TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget( |
733 | 5 | thread.GetProcess()->GetTarget()); |
734 | 5 | if (!scratch_ts_sp) |
735 | 0 | return LLDB_INVALID_ADDRESS; |
736 | | |
737 | 5 | CompilerType clang_void_ptr_type = |
738 | 5 | scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType(); |
739 | 5 | Status error; |
740 | | |
741 | 5 | impl_function_caller = m_impl_code->MakeFunctionCaller( |
742 | 5 | clang_void_ptr_type, dispatch_values, thread_sp, error); |
743 | 5 | if (error.Fail()) { |
744 | 0 | LLDB_LOGF(log, |
745 | 0 | "Error getting function caller for dispatch lookup: \"%s\".", |
746 | 0 | error.AsCString()); |
747 | 0 | return args_addr; |
748 | 0 | } |
749 | 22 | } else { |
750 | 22 | impl_function_caller = m_impl_code->GetFunctionCaller(); |
751 | 22 | } |
752 | 27 | } |
753 | | |
754 | | // Now write down the argument values for this particular call. |
755 | | // This looks like it might be a race condition if other threads |
756 | | // were calling into here, but actually it isn't because we allocate |
757 | | // a new args structure for this call by passing args_addr = |
758 | | // LLDB_INVALID_ADDRESS... |
759 | | |
760 | 27 | DiagnosticManager diagnostics; |
761 | 27 | if (!impl_function_caller->WriteFunctionArguments( |
762 | 27 | exe_ctx, args_addr, dispatch_values, diagnostics)) { |
763 | 0 | if (log) { |
764 | 0 | LLDB_LOGF(log, "Error writing function arguments."); |
765 | 0 | diagnostics.Dump(log); |
766 | 0 | } |
767 | 0 | return args_addr; |
768 | 0 | } |
769 | | |
770 | 27 | return args_addr; |
771 | 27 | } |
772 | | |
773 | | const AppleObjCTrampolineHandler::DispatchFunction * |
774 | 399 | AppleObjCTrampolineHandler::FindDispatchFunction(lldb::addr_t addr) { |
775 | 399 | MsgsendMap::iterator pos; |
776 | 399 | pos = m_msgSend_map.find(addr); |
777 | 399 | if (pos != m_msgSend_map.end()) { |
778 | 36 | return &g_dispatch_functions[(*pos).second]; |
779 | 36 | } |
780 | 363 | return nullptr; |
781 | 399 | } |
782 | | |
783 | | void AppleObjCTrampolineHandler::ForEachDispatchFunction( |
784 | 10 | std::function<void(lldb::addr_t, const DispatchFunction &)> callback) { |
785 | 200 | for (auto elem : m_msgSend_map) { |
786 | 200 | callback(elem.first, g_dispatch_functions[elem.second]); |
787 | 200 | } |
788 | 10 | } |
789 | | |
790 | | ThreadPlanSP |
791 | | AppleObjCTrampolineHandler::GetStepThroughDispatchPlan(Thread &thread, |
792 | 399 | bool stop_others) { |
793 | 399 | ThreadPlanSP ret_plan_sp; |
794 | 399 | lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); |
795 | | |
796 | 399 | DispatchFunction vtable_dispatch = {"vtable", false, false, false, |
797 | 399 | DispatchFunction::eFixUpFixed}; |
798 | | // The selector specific stubs are a wrapper for objc_msgSend. They don't get |
799 | | // passed a SEL, but instead the selector string is encoded in the stub |
800 | | // name, in the form: |
801 | | // objc_msgSend$SelectorName |
802 | | // and the stub figures out the uniqued selector. If we find ourselves in |
803 | | // one of these stubs, we strip off the selector string and pass that to the |
804 | | // implementation finder function, which looks up the SEL (you have to do this |
805 | | // in process) and passes that to the runtime lookup function. |
806 | 399 | DispatchFunction sel_stub_dispatch = {"sel-specific-stub", false, false, |
807 | 399 | false, DispatchFunction::eFixUpNone}; |
808 | | |
809 | | // First step is to see if we're in a selector-specific dispatch stub. |
810 | | // Those are of the form _objc_msgSend$<SELECTOR>, so see if the current |
811 | | // function has that name: |
812 | 399 | Address func_addr; |
813 | 399 | Target &target = thread.GetProcess()->GetTarget(); |
814 | 399 | llvm::StringRef sym_name; |
815 | 399 | const DispatchFunction *this_dispatch = nullptr; |
816 | | |
817 | 399 | if (target.ResolveLoadAddress(curr_pc, func_addr)) { |
818 | 399 | Symbol *curr_sym = func_addr.CalculateSymbolContextSymbol(); |
819 | 399 | if (curr_sym) |
820 | 399 | sym_name = curr_sym->GetName().GetStringRef(); |
821 | | |
822 | 399 | if (!sym_name.empty() && !sym_name.consume_front("objc_msgSend$")) |
823 | 399 | sym_name = {}; |
824 | 0 | else |
825 | 0 | this_dispatch = &sel_stub_dispatch; |
826 | 399 | } |
827 | 399 | bool in_selector_stub = !sym_name.empty(); |
828 | | // Second step is to look and see if we are in one of the known ObjC |
829 | | // dispatch functions. We've already compiled a table of same, so |
830 | | // consult it. |
831 | | |
832 | 399 | if (!in_selector_stub) |
833 | 399 | this_dispatch = FindDispatchFunction(curr_pc); |
834 | | |
835 | | // Next check to see if we are in a vtable region: |
836 | | |
837 | 399 | if (!this_dispatch && m_vtables_up363 ) { |
838 | 363 | uint32_t flags; |
839 | 363 | if (m_vtables_up->IsAddressInVTables(curr_pc, flags)) { |
840 | 0 | vtable_dispatch.stret_return = |
841 | 0 | (flags & AppleObjCVTables::eOBJC_TRAMPOLINE_STRET) == |
842 | 0 | AppleObjCVTables::eOBJC_TRAMPOLINE_STRET; |
843 | 0 | this_dispatch = &vtable_dispatch; |
844 | 0 | } |
845 | 363 | } |
846 | | |
847 | | // Since we set this_dispatch in both the vtable & sel specific stub cases |
848 | | // this if will be used for all three of those cases. |
849 | 399 | if (this_dispatch) { |
850 | 36 | Log *log = GetLog(LLDBLog::Step); |
851 | | |
852 | | // We are decoding a method dispatch. First job is to pull the |
853 | | // arguments out. If we are in a regular stub, we get self & selector, |
854 | | // but if we are in a selector-specific stub, we'll have to get that from |
855 | | // the string sym_name. |
856 | | |
857 | 36 | lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); |
858 | | |
859 | 36 | const ABI *abi = nullptr; |
860 | 36 | ProcessSP process_sp(thread.CalculateProcess()); |
861 | 36 | if (process_sp) |
862 | 36 | abi = process_sp->GetABI().get(); |
863 | 36 | if (abi == nullptr) |
864 | 0 | return ret_plan_sp; |
865 | | |
866 | 36 | TargetSP target_sp(thread.CalculateTarget()); |
867 | | |
868 | 36 | TypeSystemClangSP scratch_ts_sp = |
869 | 36 | ScratchTypeSystemClang::GetForTarget(*target_sp); |
870 | 36 | if (!scratch_ts_sp) |
871 | 0 | return ret_plan_sp; |
872 | | |
873 | 36 | ValueList argument_values; |
874 | 36 | Value void_ptr_value; |
875 | 36 | CompilerType clang_void_ptr_type = |
876 | 36 | scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType(); |
877 | 36 | void_ptr_value.SetValueType(Value::ValueType::Scalar); |
878 | | // void_ptr_value.SetContext (Value::eContextTypeClangType, |
879 | | // clang_void_ptr_type); |
880 | 36 | void_ptr_value.SetCompilerType(clang_void_ptr_type); |
881 | | |
882 | 36 | int obj_index; |
883 | 36 | int sel_index; |
884 | | |
885 | | // If this is a selector-specific stub then just push one value, 'cause |
886 | | // we only get the object. |
887 | | // If this is a struct return dispatch, then the first argument is |
888 | | // the return struct pointer, and the object is the second, and |
889 | | // the selector is the third. |
890 | | // Otherwise the object is the first and the selector the second. |
891 | 36 | if (in_selector_stub) { |
892 | 0 | obj_index = 0; |
893 | 0 | sel_index = 1; |
894 | 0 | argument_values.PushValue(void_ptr_value); |
895 | 36 | } else if (this_dispatch->stret_return) { |
896 | 0 | obj_index = 1; |
897 | 0 | sel_index = 2; |
898 | 0 | argument_values.PushValue(void_ptr_value); |
899 | 0 | argument_values.PushValue(void_ptr_value); |
900 | 0 | argument_values.PushValue(void_ptr_value); |
901 | 36 | } else { |
902 | 36 | obj_index = 0; |
903 | 36 | sel_index = 1; |
904 | 36 | argument_values.PushValue(void_ptr_value); |
905 | 36 | argument_values.PushValue(void_ptr_value); |
906 | 36 | } |
907 | | |
908 | 36 | bool success = abi->GetArgumentValues(thread, argument_values); |
909 | 36 | if (!success) |
910 | 0 | return ret_plan_sp; |
911 | | |
912 | 36 | lldb::addr_t obj_addr = |
913 | 36 | argument_values.GetValueAtIndex(obj_index)->GetScalar().ULongLong(); |
914 | 36 | if (obj_addr == 0x0) { |
915 | 2 | LLDB_LOGF( |
916 | 2 | log, |
917 | 2 | "Asked to step to dispatch to nil object, returning empty plan."); |
918 | 2 | return ret_plan_sp; |
919 | 2 | } |
920 | | |
921 | 34 | ExecutionContext exe_ctx(thread.shared_from_this()); |
922 | | // isa_addr will store the class pointer that the method is being |
923 | | // dispatched to - so either the class directly or the super class |
924 | | // if this is one of the objc_msgSendSuper flavors. That's mostly |
925 | | // used to look up the class/selector pair in our cache. |
926 | | |
927 | 34 | lldb::addr_t isa_addr = LLDB_INVALID_ADDRESS; |
928 | 34 | lldb::addr_t sel_addr = LLDB_INVALID_ADDRESS; |
929 | | // If we are not in a selector stub, get the sel address from the arguments. |
930 | 34 | if (!in_selector_stub) |
931 | 34 | sel_addr = |
932 | 34 | argument_values.GetValueAtIndex(sel_index)->GetScalar().ULongLong(); |
933 | | |
934 | | // Figure out the class this is being dispatched to and see if |
935 | | // we've already cached this method call, If so we can push a |
936 | | // run-to-address plan directly. Otherwise we have to figure out |
937 | | // where the implementation lives. |
938 | | |
939 | 34 | if (this_dispatch->is_super) { |
940 | 8 | if (this_dispatch->is_super2) { |
941 | | // In the objc_msgSendSuper2 case, we don't get the object |
942 | | // directly, we get a structure containing the object and the |
943 | | // class to which the super message is being sent. So we need |
944 | | // to dig the super out of the class and use that. |
945 | | |
946 | 8 | Value super_value(*(argument_values.GetValueAtIndex(obj_index))); |
947 | 8 | super_value.GetScalar() += process_sp->GetAddressByteSize(); |
948 | 8 | super_value.ResolveValue(&exe_ctx); |
949 | | |
950 | 8 | if (super_value.GetScalar().IsValid()) { |
951 | | |
952 | | // isa_value now holds the class pointer. The second word of the |
953 | | // class pointer is the super-class pointer: |
954 | 8 | super_value.GetScalar() += process_sp->GetAddressByteSize(); |
955 | 8 | super_value.ResolveValue(&exe_ctx); |
956 | 8 | if (super_value.GetScalar().IsValid()) |
957 | 8 | isa_addr = super_value.GetScalar().ULongLong(); |
958 | 0 | else { |
959 | 0 | LLDB_LOGF(log, "Failed to extract the super class value from the " |
960 | 0 | "class in objc_super."); |
961 | 0 | } |
962 | 8 | } else { |
963 | 0 | LLDB_LOGF(log, "Failed to extract the class value from objc_super."); |
964 | 0 | } |
965 | 8 | } else { |
966 | | // In the objc_msgSendSuper case, we don't get the object |
967 | | // directly, we get a two element structure containing the |
968 | | // object and the super class to which the super message is |
969 | | // being sent. So the class we want is the second element of |
970 | | // this structure. |
971 | |
|
972 | 0 | Value super_value(*(argument_values.GetValueAtIndex(obj_index))); |
973 | 0 | super_value.GetScalar() += process_sp->GetAddressByteSize(); |
974 | 0 | super_value.ResolveValue(&exe_ctx); |
975 | |
|
976 | 0 | if (super_value.GetScalar().IsValid()) { |
977 | 0 | isa_addr = super_value.GetScalar().ULongLong(); |
978 | 0 | } else { |
979 | 0 | LLDB_LOGF(log, "Failed to extract the class value from objc_super."); |
980 | 0 | } |
981 | 0 | } |
982 | 26 | } else { |
983 | | // In the direct dispatch case, the object->isa is the class pointer we |
984 | | // want. |
985 | | |
986 | | // This is a little cheesy, but since object->isa is the first field, |
987 | | // making the object value a load address value and resolving it will get |
988 | | // the pointer sized data pointed to by that value... |
989 | | |
990 | | // Note, it isn't a fatal error not to be able to get the |
991 | | // address from the object, since this might be a "tagged |
992 | | // pointer" which isn't a real object, but rather some word |
993 | | // length encoded dingus. |
994 | | |
995 | 26 | Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); |
996 | | |
997 | 26 | isa_value.SetValueType(Value::ValueType::LoadAddress); |
998 | 26 | isa_value.ResolveValue(&exe_ctx); |
999 | 26 | if (isa_value.GetScalar().IsValid()) { |
1000 | 26 | isa_addr = isa_value.GetScalar().ULongLong(); |
1001 | 26 | } else { |
1002 | 0 | LLDB_LOGF(log, "Failed to extract the isa value from object."); |
1003 | 0 | } |
1004 | 26 | } |
1005 | | |
1006 | | // Okay, we've got the address of the class for which we're resolving this, |
1007 | | // let's see if it's in our cache: |
1008 | 34 | lldb::addr_t impl_addr = LLDB_INVALID_ADDRESS; |
1009 | | // If this is a regular dispatch, look up the sel in our addr to sel cache: |
1010 | 34 | if (isa_addr != LLDB_INVALID_ADDRESS) { |
1011 | 34 | ObjCLanguageRuntime *objc_runtime = |
1012 | 34 | ObjCLanguageRuntime::Get(*thread.GetProcess()); |
1013 | 34 | assert(objc_runtime != nullptr); |
1014 | 34 | if (!in_selector_stub) { |
1015 | 34 | LLDB_LOG(log, "Resolving call for class - {0} and selector - {1}", |
1016 | 34 | isa_addr, sel_addr); |
1017 | 34 | impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sel_addr); |
1018 | 34 | } else { |
1019 | 0 | LLDB_LOG(log, "Resolving call for class - {0} and selector - {1}", |
1020 | 0 | isa_addr, sym_name); |
1021 | 0 | impl_addr = objc_runtime->LookupInMethodCache(isa_addr, sym_name); |
1022 | 0 | } |
1023 | 34 | } |
1024 | | // If it is a selector-specific stub dispatch, look in the string cache: |
1025 | | |
1026 | 34 | if (impl_addr != LLDB_INVALID_ADDRESS) { |
1027 | | // Yup, it was in the cache, so we can run to that address directly. |
1028 | | |
1029 | 7 | LLDB_LOGF(log, "Found implementation address in cache: 0x%" PRIx64, |
1030 | 7 | impl_addr); |
1031 | | |
1032 | 7 | ret_plan_sp = std::make_shared<ThreadPlanRunToAddress>(thread, impl_addr, |
1033 | 7 | stop_others); |
1034 | 27 | } else { |
1035 | | // We haven't seen this class/selector pair yet. Look it up. |
1036 | 27 | StreamString errors; |
1037 | 27 | Address impl_code_address; |
1038 | | |
1039 | 27 | ValueList dispatch_values; |
1040 | | |
1041 | | // We've will inject a little function in the target that takes the |
1042 | | // object, selector/selector string and some flags, |
1043 | | // and figures out the implementation. Looks like: |
1044 | | // void *__lldb_objc_find_implementation_for_selector (void *object, |
1045 | | // void *sel, |
1046 | | // int |
1047 | | // is_str_ptr, |
1048 | | // int is_stret, |
1049 | | // int is_super, |
1050 | | // int is_super2, |
1051 | | // int is_fixup, |
1052 | | // int is_fixed, |
1053 | | // int debug) |
1054 | | // If we don't have an actual SEL, but rather a string version of the |
1055 | | // selector WE injected, set is_str_ptr to true, and sel to the address |
1056 | | // of the string. |
1057 | | // So set up the arguments for that call. |
1058 | | |
1059 | 27 | dispatch_values.PushValue(*(argument_values.GetValueAtIndex(obj_index))); |
1060 | 27 | lldb::addr_t sel_str_addr = LLDB_INVALID_ADDRESS; |
1061 | 27 | if (!in_selector_stub) { |
1062 | | // If we don't have a selector string, push the selector from arguments. |
1063 | 27 | dispatch_values.PushValue( |
1064 | 27 | *(argument_values.GetValueAtIndex(sel_index))); |
1065 | 27 | } else { |
1066 | | // Otherwise, inject the string into the target, and push that value for |
1067 | | // the sel argument. |
1068 | 0 | Status error; |
1069 | 0 | sel_str_addr = process_sp->AllocateMemory( |
1070 | 0 | sym_name.size() + 1, ePermissionsReadable | ePermissionsWritable, |
1071 | 0 | error); |
1072 | 0 | if (sel_str_addr == LLDB_INVALID_ADDRESS || error.Fail()) { |
1073 | 0 | LLDB_LOG(log, |
1074 | 0 | "Could not allocate memory for selector string {0}: {1}", |
1075 | 0 | sym_name, error); |
1076 | 0 | return ret_plan_sp; |
1077 | 0 | } |
1078 | 0 | process_sp->WriteMemory(sel_str_addr, sym_name.str().c_str(), |
1079 | 0 | sym_name.size() + 1, error); |
1080 | 0 | if (error.Fail()) { |
1081 | 0 | LLDB_LOG(log, "Could not write string to address {0}", sel_str_addr); |
1082 | 0 | return ret_plan_sp; |
1083 | 0 | } |
1084 | 0 | Value sel_ptr_value(void_ptr_value); |
1085 | 0 | sel_ptr_value.GetScalar() = sel_str_addr; |
1086 | 0 | dispatch_values.PushValue(sel_ptr_value); |
1087 | 0 | } |
1088 | | |
1089 | 27 | Value flag_value; |
1090 | 27 | CompilerType clang_int_type = |
1091 | 27 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize( |
1092 | 27 | lldb::eEncodingSint, 32); |
1093 | 27 | flag_value.SetValueType(Value::ValueType::Scalar); |
1094 | | // flag_value.SetContext (Value::eContextTypeClangType, clang_int_type); |
1095 | 27 | flag_value.SetCompilerType(clang_int_type); |
1096 | | |
1097 | 27 | if (in_selector_stub) |
1098 | 0 | flag_value.GetScalar() = 1; |
1099 | 27 | else |
1100 | 27 | flag_value.GetScalar() = 0; |
1101 | 27 | dispatch_values.PushValue(flag_value); |
1102 | | |
1103 | 27 | if (this_dispatch->stret_return) |
1104 | 0 | flag_value.GetScalar() = 1; |
1105 | 27 | else |
1106 | 27 | flag_value.GetScalar() = 0; |
1107 | 27 | dispatch_values.PushValue(flag_value); |
1108 | | |
1109 | 27 | if (this_dispatch->is_super) |
1110 | 4 | flag_value.GetScalar() = 1; |
1111 | 23 | else |
1112 | 23 | flag_value.GetScalar() = 0; |
1113 | 27 | dispatch_values.PushValue(flag_value); |
1114 | | |
1115 | 27 | if (this_dispatch->is_super2) |
1116 | 4 | flag_value.GetScalar() = 1; |
1117 | 23 | else |
1118 | 23 | flag_value.GetScalar() = 0; |
1119 | 27 | dispatch_values.PushValue(flag_value); |
1120 | | |
1121 | 27 | switch (this_dispatch->fixedup) { |
1122 | 27 | case DispatchFunction::eFixUpNone: |
1123 | 27 | flag_value.GetScalar() = 0; |
1124 | 27 | dispatch_values.PushValue(flag_value); |
1125 | 27 | dispatch_values.PushValue(flag_value); |
1126 | 27 | break; |
1127 | 0 | case DispatchFunction::eFixUpFixed: |
1128 | 0 | flag_value.GetScalar() = 1; |
1129 | 0 | dispatch_values.PushValue(flag_value); |
1130 | 0 | flag_value.GetScalar() = 1; |
1131 | 0 | dispatch_values.PushValue(flag_value); |
1132 | 0 | break; |
1133 | 0 | case DispatchFunction::eFixUpToFix: |
1134 | 0 | flag_value.GetScalar() = 1; |
1135 | 0 | dispatch_values.PushValue(flag_value); |
1136 | 0 | flag_value.GetScalar() = 0; |
1137 | 0 | dispatch_values.PushValue(flag_value); |
1138 | 0 | break; |
1139 | 27 | } |
1140 | 27 | if (log && log->GetVerbose()0 ) |
1141 | 0 | flag_value.GetScalar() = 1; |
1142 | 27 | else |
1143 | 27 | flag_value.GetScalar() = 0; // FIXME - Set to 0 when debugging is done. |
1144 | 27 | dispatch_values.PushValue(flag_value); |
1145 | | |
1146 | 27 | ret_plan_sp = std::make_shared<AppleThreadPlanStepThroughObjCTrampoline>( |
1147 | 27 | thread, *this, dispatch_values, isa_addr, sel_addr, sel_str_addr, |
1148 | 27 | sym_name); |
1149 | 27 | if (log) { |
1150 | 0 | StreamString s; |
1151 | 0 | ret_plan_sp->GetDescription(&s, eDescriptionLevelFull); |
1152 | 0 | LLDB_LOGF(log, "Using ObjC step plan: %s.\n", s.GetData()); |
1153 | 0 | } |
1154 | 27 | } |
1155 | 34 | } |
1156 | | |
1157 | | // Finally, check if we have hit an "optimized dispatch" function. This will |
1158 | | // either directly call the base implementation or dispatch an objc_msgSend |
1159 | | // if the method has been overridden. So we just do a "step in/step out", |
1160 | | // setting a breakpoint on objc_msgSend, and if we hit the msgSend, we |
1161 | | // will automatically step in again. That's the job of the |
1162 | | // AppleThreadPlanStepThroughDirectDispatch. |
1163 | 397 | if (!this_dispatch && !ret_plan_sp363 ) { |
1164 | 363 | MsgsendMap::iterator pos; |
1165 | 363 | pos = m_opt_dispatch_map.find(curr_pc); |
1166 | 363 | if (pos != m_opt_dispatch_map.end()) { |
1167 | 10 | const char *opt_name = g_opt_dispatch_names[(*pos).second]; |
1168 | 10 | ret_plan_sp = std::make_shared<AppleThreadPlanStepThroughDirectDispatch>( |
1169 | 10 | thread, *this, opt_name); |
1170 | 10 | } |
1171 | 363 | } |
1172 | | |
1173 | 397 | return ret_plan_sp; |
1174 | 399 | } |
1175 | | |
1176 | | FunctionCaller * |
1177 | 27 | AppleObjCTrampolineHandler::GetLookupImplementationFunctionCaller() { |
1178 | 27 | return m_impl_code->GetFunctionCaller(); |
1179 | 27 | } |