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