LLDB mainline
AppleObjCRuntimeV2.cpp
Go to the documentation of this file.
1//===-- AppleObjCRuntimeV2.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
11
12#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Module.h"
16#include "lldb/Core/Section.h"
28#include "lldb/Symbol/Symbol.h"
31#include "lldb/Target/ABI.h"
36#include "lldb/Target/Process.h"
39#include "lldb/Target/Target.h"
40#include "lldb/Target/Thread.h"
43#include "lldb/Utility/Log.h"
44#include "lldb/Utility/Scalar.h"
45#include "lldb/Utility/Status.h"
46#include "lldb/Utility/Stream.h"
48#include "lldb/Utility/Timer.h"
52
54#include "AppleObjCDeclVendor.h"
55#include "AppleObjCRuntimeV2.h"
58
59#include "clang/AST/ASTContext.h"
60#include "clang/AST/DeclObjC.h"
61#include "clang/Basic/TargetInfo.h"
62#include "llvm/ADT/STLExtras.h"
63#include "llvm/ADT/ScopeExit.h"
64
65#include <cstdint>
66#include <memory>
67#include <string>
68#include <vector>
69
70using namespace lldb;
71using namespace lldb_private;
72
73namespace {
74struct RuntimeGlobalSymbolSpec {
75 ConstString name;
76
77 /// Whether to only return the address or also the value.
78 bool read_value = true;
79
80 /// A byte size of 0 means use the process pointer size.
81 uint8_t byte_size = 0;
82};
83
84struct RuntimeGlobalSymbolResult {
85 uint64_t value = LLDB_INVALID_ADDRESS;
86 bool success = false;
87};
88} // namespace
89
91
93 "__lldb_apple_objc_v2_get_dynamic_class_info";
94
95static const char *g_get_dynamic_class_info_body = R"(
96
97extern "C"
98{
99 size_t strlen(const char *);
100 char *strncpy (char * s1, const char * s2, size_t n);
101 int printf(const char * format, ...);
102}
103#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
104
105typedef struct _NXMapTable {
106 void *prototype;
107 unsigned num_classes;
108 unsigned num_buckets_minus_one;
109 void *buckets;
110} NXMapTable;
111
112#define NX_MAPNOTAKEY ((void *)(-1))
113
114typedef struct BucketInfo
115{
116 const char *name_ptr;
117 Class isa;
118} BucketInfo;
119
120struct ClassInfo
121{
122 Class isa;
123 uint32_t hash;
124} __attribute__((__packed__));
125
126uint32_t
127__lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr,
128 void *class_infos_ptr,
129 uint32_t class_infos_byte_size,
130 uint32_t should_log)
131{
132 DEBUG_PRINTF ("gdb_objc_realized_classes_ptr = %p\n", gdb_objc_realized_classes_ptr);
133 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
134 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
135 const NXMapTable *grc = (const NXMapTable *)gdb_objc_realized_classes_ptr;
136 if (grc)
137 {
138 const unsigned num_classes = grc->num_classes;
139 DEBUG_PRINTF ("num_classes = %u\n", grc->num_classes);
140 if (class_infos_ptr)
141 {
142 const unsigned num_buckets_minus_one = grc->num_buckets_minus_one;
143 DEBUG_PRINTF ("num_buckets_minus_one = %u\n", num_buckets_minus_one);
144
145 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
146 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
147
148 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
149 BucketInfo *buckets = (BucketInfo *)grc->buckets;
150
151 uint32_t idx = 0;
152 for (unsigned i=0; i<=num_buckets_minus_one; ++i)
153 {
154 if (buckets[i].name_ptr != NX_MAPNOTAKEY)
155 {
156 if (idx < max_class_infos)
157 {
158 const char *s = buckets[i].name_ptr;
159 uint32_t h = 5381;
160 for (unsigned char c = *s; c; c = *++s)
161 h = ((h << 5) + h) + c;
162 class_infos[idx].hash = h;
163 class_infos[idx].isa = buckets[i].isa;
164 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, buckets[i].name_ptr);
165 }
166 ++idx;
167 }
168 }
169 if (idx < max_class_infos)
170 {
171 class_infos[idx].isa = NULL;
172 class_infos[idx].hash = 0;
173 }
174 }
175 return num_classes;
176 }
177 return 0;
178}
179
180)";
181
183 "__lldb_apple_objc_v2_get_dynamic_class_info2";
184
185static const char *g_get_dynamic_class_info2_body = R"(
186
187extern "C" {
188 int printf(const char * format, ...);
189 void free(void *ptr);
190 Class* objc_copyRealizedClassList_nolock(unsigned int *outCount);
191 const char* objc_debug_class_getNameRaw(Class cls);
192}
193
194#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
195
196struct ClassInfo
197{
198 Class isa;
199 uint32_t hash;
200} __attribute__((__packed__));
201
202uint32_t
203__lldb_apple_objc_v2_get_dynamic_class_info2(void *gdb_objc_realized_classes_ptr,
204 void *class_infos_ptr,
205 uint32_t class_infos_byte_size,
206 uint32_t should_log)
207{
208 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
209 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
210
211 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
212 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
213
214 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
215
216 uint32_t count = 0;
217 Class* realized_class_list = objc_copyRealizedClassList_nolock(&count);
218 DEBUG_PRINTF ("count = %u\n", count);
219
220 uint32_t idx = 0;
221 for (uint32_t i=0; i<count; ++i)
222 {
223 if (idx < max_class_infos)
224 {
225 Class isa = realized_class_list[i];
226 const char *name_ptr = objc_debug_class_getNameRaw(isa);
227 if (!name_ptr)
228 continue;
229 const char *s = name_ptr;
230 uint32_t h = 5381;
231 for (unsigned char c = *s; c; c = *++s)
232 h = ((h << 5) + h) + c;
233 class_infos[idx].hash = h;
234 class_infos[idx].isa = isa;
235 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr);
236 }
237 idx++;
238 }
239
240 if (idx < max_class_infos)
241 {
242 class_infos[idx].isa = NULL;
243 class_infos[idx].hash = 0;
244 }
245
246 free(realized_class_list);
247 return count;
248}
249)";
250
252 "__lldb_apple_objc_v2_get_dynamic_class_info3";
253
254static const char *g_get_dynamic_class_info3_body = R"(
255
256extern "C" {
257 int printf(const char * format, ...);
258 void free(void *ptr);
259 size_t objc_getRealizedClassList_trylock(Class *buffer, size_t len);
260 const char* objc_debug_class_getNameRaw(Class cls);
261 const char* class_getName(Class cls);
262}
263
264#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
265
266struct ClassInfo
267{
268 Class isa;
269 uint32_t hash;
270} __attribute__((__packed__));
271
272uint32_t
273__lldb_apple_objc_v2_get_dynamic_class_info3(void *gdb_objc_realized_classes_ptr,
274 void *class_infos_ptr,
275 uint32_t class_infos_byte_size,
276 void *class_buffer,
277 uint32_t class_buffer_len,
278 uint32_t should_log)
279{
280 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
281 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
282
283 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
284 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
285
286 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
287
288 Class *realized_class_list = (Class*)class_buffer;
289
290 uint32_t count = objc_getRealizedClassList_trylock(realized_class_list,
291 class_buffer_len);
292 DEBUG_PRINTF ("count = %u\n", count);
293
294 uint32_t idx = 0;
295 for (uint32_t i=0; i<count; ++i)
296 {
297 if (idx < max_class_infos)
298 {
299 Class isa = realized_class_list[i];
300 const char *name_ptr = objc_debug_class_getNameRaw(isa);
301 if (!name_ptr) {
302 class_getName(isa); // Realize name of lazy classes.
303 name_ptr = objc_debug_class_getNameRaw(isa);
304 }
305 if (!name_ptr)
306 continue;
307 const char *s = name_ptr;
308 uint32_t h = 5381;
309 for (unsigned char c = *s; c; c = *++s)
310 h = ((h << 5) + h) + c;
311 class_infos[idx].hash = h;
312 class_infos[idx].isa = isa;
313 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr);
314 }
315 idx++;
316 }
317
318 if (idx < max_class_infos)
319 {
320 class_infos[idx].isa = NULL;
321 class_infos[idx].hash = 0;
322 }
323
324 return count;
325}
326)";
327
328// We'll substitute in class_getName or class_getNameRaw depending
329// on which is present.
330static const char *g_shared_cache_class_name_funcptr = R"(
331extern "C"
332{
333 const char *{0}(void *objc_class);
334 const char *(*class_name_lookup_func)(void *) = {1};
335}
336)";
337
339 "__lldb_apple_objc_v2_get_shared_cache_class_info";
340
342
343extern "C"
344{
345 size_t strlen(const char *);
346 char *strncpy (char * s1, const char * s2, size_t n);
347 int printf(const char * format, ...);
348}
349
350#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
351
352
353struct objc_classheader_t {
354 int32_t clsOffset;
355 int32_t hiOffset;
356};
357
358struct objc_classheader_v16_t {
359 uint64_t isDuplicate : 1,
360 objectCacheOffset : 47, // Offset from the shared cache base
361 dylibObjCIndex : 16;
362};
363
364struct objc_clsopt_t {
365 uint32_t capacity;
366 uint32_t occupied;
367 uint32_t shift;
368 uint32_t mask;
369 uint32_t zero;
370 uint32_t unused;
371 uint64_t salt;
372 uint32_t scramble[256];
373 uint8_t tab[0]; // tab[mask+1]
374 // uint8_t checkbytes[capacity];
375 // int32_t offset[capacity];
376 // objc_classheader_t clsOffsets[capacity];
377 // uint32_t duplicateCount;
378 // objc_classheader_t duplicateOffsets[duplicateCount];
379};
380
381struct objc_clsopt_v16_t {
382 uint32_t version;
383 uint32_t capacity;
384 uint32_t occupied;
385 uint32_t shift;
386 uint32_t mask;
387 uint32_t zero;
388 uint64_t salt;
389 uint32_t scramble[256];
390 uint8_t tab[0]; // tab[mask+1]
391 // uint8_t checkbytes[capacity];
392 // int32_t offset[capacity];
393 // objc_classheader_t clsOffsets[capacity];
394 // uint32_t duplicateCount;
395 // objc_classheader_t duplicateOffsets[duplicateCount];
396};
397
398struct objc_opt_t {
399 uint32_t version;
400 int32_t selopt_offset;
401 int32_t headeropt_offset;
402 int32_t clsopt_offset;
403};
404
405struct objc_opt_v14_t {
406 uint32_t version;
407 uint32_t flags;
408 int32_t selopt_offset;
409 int32_t headeropt_offset;
410 int32_t clsopt_offset;
411};
412
413struct objc_opt_v16_t {
414 uint32_t version;
415 uint32_t flags;
416 int32_t selopt_offset;
417 int32_t headeropt_ro_offset;
418 int32_t unused_clsopt_offset;
419 int32_t unused_protocolopt_offset;
420 int32_t headeropt_rw_offset;
421 int32_t unused_protocolopt2_offset;
422 int32_t largeSharedCachesClassOffset;
423 int32_t largeSharedCachesProtocolOffset;
424 uint64_t relativeMethodSelectorBaseAddressCacheOffset;
425};
426
427struct ClassInfo
428{
429 Class isa;
430 uint32_t hash;
431} __attribute__((__packed__));
432)";
433
435
436uint32_t
437__lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr,
438 void *shared_cache_base_ptr,
439 void *class_infos_ptr,
440 uint64_t *relative_selector_offset,
441 uint32_t class_infos_byte_size,
442 uint32_t *start_idx,
443 uint32_t should_log)
444{
445 *relative_selector_offset = 0;
446 uint32_t idx = 0;
447 DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr);
448 DEBUG_PRINTF ("shared_cache_base_ptr = %p\n", shared_cache_base_ptr);
449 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
450 DEBUG_PRINTF ("class_infos_byte_size = %u (%llu class infos)\n", class_infos_byte_size, (uint64_t)(class_infos_byte_size/sizeof(ClassInfo)));
451 DEBUG_PRINTF ("start_idx = %u\n", *start_idx);
452 if (objc_opt_ro_ptr)
453 {
454 const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr;
455 const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr;
456 const objc_opt_v16_t* objc_opt_v16 = (objc_opt_v16_t*)objc_opt_ro_ptr;
457 if (objc_opt->version >= 16)
458 {
459 *relative_selector_offset = objc_opt_v16->relativeMethodSelectorBaseAddressCacheOffset;
460 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v16->version);
461 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v16->flags);
462 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v16->selopt_offset);
463 DEBUG_PRINTF ("objc_opt->headeropt_ro_offset = %d\n", objc_opt_v16->headeropt_ro_offset);
464 DEBUG_PRINTF ("objc_opt->relativeMethodSelectorBaseAddressCacheOffset = %d\n", *relative_selector_offset);
465 }
466 else if (objc_opt->version >= 14)
467 {
468 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version);
469 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags);
470 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset);
471 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset);
472 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset);
473 }
474 else
475 {
476 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version);
477 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset);
478 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset);
479 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset);
480 }
481
482 if (objc_opt->version == 16)
483 {
484 int32_t large_offset = objc_opt_v16->largeSharedCachesClassOffset;
485 const objc_clsopt_v16_t* clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + large_offset);
486 // Work around a bug in some version shared cache builder where the offset overflows 2GiB (rdar://146432183).
487 uint32_t unsigned_offset = (uint32_t)large_offset;
488 if (unsigned_offset > 0x7fffffff && unsigned_offset < 0x82000000) {
489 clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + unsigned_offset);
490 DEBUG_PRINTF("warning: applying largeSharedCachesClassOffset overflow workaround!\n");
491 }
492 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
493
494 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
495
496 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
497
498 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
499 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
500 const objc_classheader_v16_t *classOffsets = (const objc_classheader_v16_t *)(offsets + clsopt->capacity);
501
502 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
503 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
504 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
505
506 const uint32_t original_start_idx = *start_idx;
507
508 // Always start at the start_idx here. If it's greater than the capacity,
509 // it will skip the loop entirely and go to the duplicate handling below.
510 for (uint32_t i=*start_idx; i<clsopt->capacity; ++i)
511 {
512 const uint64_t objectCacheOffset = classOffsets[i].objectCacheOffset;
513 DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset);
514
515 if (classOffsets[i].isDuplicate) {
516 DEBUG_PRINTF("isDuplicate = true\n");
517 continue; // duplicate
518 }
519
520 if (objectCacheOffset == 0) {
521 DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n");
522 continue; // invalid offset
523 }
524
525 if (class_infos && idx < max_class_infos)
526 {
527 class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset);
528
529 // Lookup the class name.
530 const char *name = class_name_lookup_func(class_infos[idx].isa);
531 DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
532
533 // Hash the class name so we don't have to read it.
534 const char *s = name;
535 uint32_t h = 5381;
536 for (unsigned char c = *s; c; c = *++s)
537 {
538 // class_getName demangles swift names and the hash must
539 // be calculated on the mangled name. hash==0 means lldb
540 // will fetch the mangled name and compute the hash in
541 // ParseClassInfoArray.
542 if (c == '.')
543 {
544 h = 0;
545 break;
546 }
547 h = ((h << 5) + h) + c;
548 }
549 class_infos[idx].hash = h;
550 }
551 else
552 {
553 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
554 *start_idx = i;
555 break;
556 }
557 ++idx;
558 }
559
560 if (idx < max_class_infos) {
561 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
562 const uint32_t duplicate_count = *duplicate_count_ptr;
563 const objc_classheader_v16_t *duplicateClassOffsets = (const objc_classheader_v16_t *)(&duplicate_count_ptr[1]);
564
565 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
566 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
567
568 const uint32_t duplicate_start_idx =
569 *start_idx < clsopt->capacity ?
570 0 :
571 *start_idx - clsopt->capacity;
572
573 for (uint32_t i=duplicate_start_idx; i<duplicate_count; ++i)
574 {
575 const uint64_t objectCacheOffset = duplicateClassOffsets[i].objectCacheOffset;
576 DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset);
577
578 if (duplicateClassOffsets[i].isDuplicate) {
579 DEBUG_PRINTF("isDuplicate = true\n");
580 continue; // duplicate
581 }
582
583 if (objectCacheOffset == 0) {
584 DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n");
585 continue; // invalid offset
586 }
587
588 if (class_infos && idx < max_class_infos)
589 {
590 class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset);
591
592 // Lookup the class name.
593 const char *name = class_name_lookup_func(class_infos[idx].isa);
594 DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
595
596 // Hash the class name so we don't have to read it.
597 const char *s = name;
598 uint32_t h = 5381;
599 for (unsigned char c = *s; c; c = *++s)
600 {
601 // class_getName demangles swift names and the hash must
602 // be calculated on the mangled name. hash==0 means lldb
603 // will fetch the mangled name and compute the hash in
604 // ParseClassInfoArray.
605 if (c == '.')
606 {
607 h = 0;
608 break;
609 }
610 h = ((h << 5) + h) + c;
611 }
612 class_infos[idx].hash = h;
613 } else {
614 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
615 *start_idx = i;
616 break;
617 }
618 ++idx;
619 }
620 }
621 // Always make sure start_idx gets updated. Otherwise we have an infinite
622 // loop if there are exactly max_class_infos number of classes.
623 if (*start_idx == original_start_idx) {
624 *start_idx = idx;
625 }
626 }
627 else if (objc_opt->version >= 12 && objc_opt->version <= 15)
628 {
629 const objc_clsopt_t* clsopt = NULL;
630 if (objc_opt->version >= 14)
631 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset);
632 else
633 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset);
634 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
635 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
636 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
637 int32_t invalidEntryOffset = 0;
638 // this is safe to do because the version field order is invariant
639 if (objc_opt->version == 12)
640 invalidEntryOffset = 16;
641 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
642 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
643 const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity);
644 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
645 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
646 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
647 DEBUG_PRINTF("invalidEntryOffset = %d\n", invalidEntryOffset);
648 for (uint32_t i=0; i<clsopt->capacity; ++i)
649 {
650 const int32_t clsOffset = classOffsets[i].clsOffset;
651 DEBUG_PRINTF("clsOffset[%u] = %u\n", i, clsOffset);
652 if (clsOffset & 1)
653 {
654 DEBUG_PRINTF("clsOffset & 1\n");
655 continue; // duplicate
656 }
657 else if (clsOffset == invalidEntryOffset)
658 {
659 DEBUG_PRINTF("clsOffset == invalidEntryOffset\n");
660 continue; // invalid offset
661 }
662
663 if (class_infos && idx < max_class_infos)
664 {
665 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
666 const char *name = class_name_lookup_func (class_infos[idx].isa);
667 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
668 // Hash the class name so we don't have to read it
669 const char *s = name;
670 uint32_t h = 5381;
671 for (unsigned char c = *s; c; c = *++s)
672 {
673 // class_getName demangles swift names and the hash must
674 // be calculated on the mangled name. hash==0 means lldb
675 // will fetch the mangled name and compute the hash in
676 // ParseClassInfoArray.
677 if (c == '.')
678 {
679 h = 0;
680 break;
681 }
682 h = ((h << 5) + h) + c;
683 }
684 class_infos[idx].hash = h;
685 }
686 else
687 {
688 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
689 }
690 ++idx;
691 }
692
693 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
694 const uint32_t duplicate_count = *duplicate_count_ptr;
695 const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]);
696 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
697 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
698 for (uint32_t i=0; i<duplicate_count; ++i)
699 {
700 const int32_t clsOffset = duplicateClassOffsets[i].clsOffset;
701 if (clsOffset & 1)
702 continue; // duplicate
703 else if (clsOffset == invalidEntryOffset)
704 continue; // invalid offset
705
706 if (class_infos && idx < max_class_infos)
707 {
708 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
709 const char *name = class_name_lookup_func (class_infos[idx].isa);
710 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
711 // Hash the class name so we don't have to read it
712 const char *s = name;
713 uint32_t h = 5381;
714 for (unsigned char c = *s; c; c = *++s)
715 {
716 // class_getName demangles swift names and the hash must
717 // be calculated on the mangled name. hash==0 means lldb
718 // will fetch the mangled name and compute the hash in
719 // ParseClassInfoArray.
720 if (c == '.')
721 {
722 h = 0;
723 break;
724 }
725 h = ((h << 5) + h) + c;
726 }
727 class_infos[idx].hash = h;
728 }
729 ++idx;
730 }
731 }
732 DEBUG_PRINTF ("%u class_infos\n", idx);
733 DEBUG_PRINTF ("done\n");
734 }
735 return idx;
736}
737
738
739)";
740
741static uint64_t
743 const ModuleSP &module_sp, Status &error,
744 bool read_value = true, uint8_t byte_size = 0,
745 uint64_t default_value = LLDB_INVALID_ADDRESS,
747 if (!process) {
748 error = Status::FromErrorString("no process");
749 return default_value;
750 }
751
752 if (!module_sp) {
753 error = Status::FromErrorString("no module");
754 return default_value;
755 }
756
757 if (!byte_size)
758 byte_size = process->GetAddressByteSize();
759 const Symbol *symbol =
760 module_sp->FindFirstSymbolWithNameAndType(name, lldb::eSymbolTypeData);
761
762 if (!symbol || !symbol->ValueIsAddress()) {
763 error = Status::FromErrorString("no symbol");
764 return default_value;
765 }
766
767 lldb::addr_t symbol_load_addr =
768 symbol->GetAddressRef().GetLoadAddress(&process->GetTarget());
769 if (symbol_load_addr == LLDB_INVALID_ADDRESS) {
770 error = Status::FromErrorString("symbol address invalid");
771 return default_value;
772 }
773
774 if (read_value)
775 return process->ReadUnsignedIntegerFromMemory(symbol_load_addr, byte_size,
776 default_value, error);
777 return symbol_load_addr;
778}
779
780/// Batched version of ExtractRuntimeGlobalSymbol. Resolves symbols and reads
781/// their values in a single batch using ReadUnsignedIntegersFromMemory.
782static llvm::SmallVector<RuntimeGlobalSymbolResult>
784 Process *process, const ModuleSP &module_sp,
785 llvm::ArrayRef<RuntimeGlobalSymbolSpec> specs) {
786
787 // Start out with all results in a failed state.
788 llvm::SmallVector<RuntimeGlobalSymbolResult> results(specs.size());
789
790 if (!process || !module_sp)
791 return results;
792
793 const uint8_t ptr_size = process->GetAddressByteSize();
794
795 // Phase 1: Resolve all symbols to addresses. Build a work list of entries
796 // that need their values read in Phase 2.
797 struct ReadEntry {
798 size_t result_idx;
799 lldb::addr_t addr;
800 uint8_t byte_size;
801 };
802 llvm::SmallVector<ReadEntry> work_list;
803 for (auto [i, spec] : llvm::enumerate(specs)) {
804 const uint8_t size = spec.byte_size ? spec.byte_size : ptr_size;
805 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
806 spec.name, lldb::eSymbolTypeData);
807
808 if (!symbol || !symbol->ValueIsAddress())
809 continue;
810
811 lldb::addr_t symbol_load_addr =
812 symbol->GetAddressRef().GetLoadAddress(&process->GetTarget());
813 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
814 continue;
815
816 if (!spec.read_value)
817 results[i] = {symbol_load_addr, true};
818 else
819 work_list.push_back({i, symbol_load_addr, size});
820 }
821
822 // Phase 2: Batch read values, grouping consecutive entries with the same
823 // byte size into a single ReadUnsignedIntegersFromMemory call.
824 llvm::stable_sort(work_list, [](const ReadEntry &a, const ReadEntry &b) {
825 return a.byte_size < b.byte_size;
826 });
827
828 for (size_t i = 0; i < work_list.size();) {
829 const uint8_t byte_size = work_list[i].byte_size;
830 const size_t group_start = i;
831
832 llvm::SmallVector<lldb::addr_t> addrs;
833 while (i < work_list.size() && work_list[i].byte_size == byte_size)
834 addrs.push_back(work_list[i++].addr);
835
836 auto read_values =
837 process->ReadUnsignedIntegersFromMemory(addrs, byte_size);
838
839 for (size_t j = 0; j < addrs.size(); ++j) {
840 size_t idx = work_list[group_start + j].result_idx;
841 if (read_values[j].has_value())
842 results[idx] = {*read_values[j], true};
843 }
844 }
845
846 return results;
847}
848
849static void RegisterObjCExceptionRecognizer(Process *process);
850
852 const ModuleSP &objc_module_sp)
853 : AppleObjCRuntime(process), m_objc_module_sp(objc_module_sp),
863 TaggedPointerVendorV2::CreateInstance(*this, objc_module_sp)),
866 static const ConstString g_gdb_object_getClass("gdb_object_getClass");
867 m_has_object_getClass = HasSymbol(g_gdb_object_getClass);
868 static const ConstString g_objc_copyRealizedClassList(
869 "_ZL33objc_copyRealizedClassList_nolockPj");
870 static const ConstString g_objc_getRealizedClassList_trylock(
871 "_objc_getRealizedClassList_trylock");
872 m_has_objc_copyRealizedClassList = HasSymbol(g_objc_copyRealizedClassList);
874 HasSymbol(g_objc_getRealizedClassList_trylock);
877}
878
881 if (auto process_sp = in_value.GetProcessSP()) {
882 assert(process_sp.get() == m_process);
883 if (auto descriptor_sp = GetNonKVOClassDescriptor(in_value)) {
884 LanguageType impl_lang = descriptor_sp->GetImplementationLanguage();
885 if (impl_lang != eLanguageTypeUnknown)
886 return process_sp->GetLanguageRuntime(impl_lang);
887 }
888 }
889 return nullptr;
890}
891
893 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
894 TypeAndOrName &class_type_or_name, Address &address,
895 Value::ValueType &value_type, llvm::ArrayRef<uint8_t> &local_buffer) {
896 // We should never get here with a null process...
897 assert(m_process != nullptr);
898
899 // The Runtime is attached to a particular process, you shouldn't pass in a
900 // value from another process. Note, however, the process might be NULL (e.g.
901 // if the value was made with SBTarget::EvaluateExpression...) in which case
902 // it is sufficient if the target's match:
903
904 Process *process = in_value.GetProcessSP().get();
905 if (process)
906 assert(process == m_process);
907 else
908 assert(in_value.GetTargetSP().get() == m_process->CalculateTarget().get());
909
910 class_type_or_name.Clear();
911 value_type = Value::ValueType::Scalar;
912
913 // Make sure we can have a dynamic value before starting...
914 if (CouldHaveDynamicValue(in_value)) {
915 // First job, pull out the address at 0 offset from the object That will
916 // be the ISA pointer.
917 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(in_value));
918 if (objc_class_sp) {
919 const addr_t object_ptr = in_value.GetPointerValue().address;
920 address.SetRawAddress(object_ptr);
921
922 ConstString class_name(objc_class_sp->GetClassName());
923 class_type_or_name.SetName(class_name);
924 TypeSP type_sp(objc_class_sp->GetType());
925 if (type_sp)
926 class_type_or_name.SetTypeSP(type_sp);
927 else {
928 type_sp = LookupInCompleteClassCache(class_name);
929 if (type_sp) {
930 objc_class_sp->SetType(type_sp);
931 class_type_or_name.SetTypeSP(type_sp);
932 } else {
933 // try to go for a CompilerType at least
934 if (auto *vendor = GetDeclVendor()) {
935 auto types = vendor->FindTypes(class_name, /*max_matches*/ 1);
936 if (!types.empty())
937 class_type_or_name.SetCompilerType(types.front());
938 }
939 }
940 }
941 }
942 }
943 return !class_type_or_name.IsEmpty();
944}
945
946// Static Functions
948 LanguageType language) {
949 // FIXME: This should be a MacOS or iOS process, and we need to look for the
950 // OBJC section to make
951 // sure we aren't using the V1 runtime.
952 if (language == eLanguageTypeObjC) {
953 ModuleSP objc_module_sp;
954
955 if (AppleObjCRuntime::GetObjCVersion(process, objc_module_sp) ==
957 return new AppleObjCRuntimeV2(process, objc_module_sp);
958 return nullptr;
959 }
960 return nullptr;
961}
962
965 false,
966 "verbose",
967 'v',
969 nullptr,
970 {},
971 0,
973 "Print ivar and method information in detail"}};
974
976public:
977 class CommandOptions : public Options {
978 public:
979 CommandOptions() : Options(), m_verbose(false, false) {}
980
981 ~CommandOptions() override = default;
982
983 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
984 ExecutionContext *execution_context) override {
986 const int short_option = m_getopt_table[option_idx].val;
987 switch (short_option) {
988 case 'v':
989 m_verbose.SetCurrentValue(true);
990 m_verbose.SetOptionWasSet();
991 break;
992
993 default:
995 "unrecognized short option '%c'", short_option);
996 break;
997 }
998
999 return error;
1000 }
1001
1002 void OptionParsingStarting(ExecutionContext *execution_context) override {
1003 m_verbose.Clear();
1004 }
1005
1006 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1007 return llvm::ArrayRef(g_objc_classtable_dump_options);
1008 }
1009
1011 };
1012
1014 : CommandObjectParsed(interpreter, "dump",
1015 "Dump information on Objective-C classes "
1016 "known to the current process.",
1017 "language objc class-table dump",
1018 eCommandRequiresProcess |
1019 eCommandProcessMustBeLaunched |
1020 eCommandProcessMustBePaused),
1021 m_options() {
1023 }
1024
1026
1027 Options *GetOptions() override { return &m_options; }
1028
1029protected:
1030 void DoExecute(Args &command, CommandReturnObject &result) override {
1031 std::unique_ptr<RegularExpression> regex_up;
1032 switch (command.GetArgumentCount()) {
1033 case 0:
1034 break;
1035 case 1: {
1036 regex_up =
1037 std::make_unique<RegularExpression>(command.GetArgumentAtIndex(0));
1038 if (!regex_up->IsValid()) {
1039 result.AppendError(
1040 "invalid argument - please provide a valid regular expression");
1042 return;
1043 }
1044 break;
1045 }
1046 default: {
1047 result.AppendError("please provide 0 or 1 arguments");
1049 return;
1050 }
1051 }
1052
1053 Process *process = m_exe_ctx.GetProcessPtr();
1054 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
1055 if (objc_runtime) {
1056 auto iterators_pair = objc_runtime->GetDescriptorIteratorPair();
1057 auto iterator = iterators_pair.first;
1058 auto &std_out = result.GetOutputStream();
1059 for (; iterator != iterators_pair.second; iterator++) {
1060 if (iterator->second) {
1061 const char *class_name =
1062 iterator->second->GetClassName().AsCString("<unknown>");
1063 if (regex_up && class_name &&
1064 !regex_up->Execute(llvm::StringRef(class_name)))
1065 continue;
1066 std_out.Printf("isa = 0x%" PRIx64, iterator->first);
1067 std_out.Printf(" name = %s", class_name);
1068 std_out.Printf(" instance size = %" PRIu64,
1069 iterator->second->GetInstanceSize());
1070 std_out.Printf(" num ivars = %" PRIuPTR,
1071 (uintptr_t)iterator->second->GetNumIVars());
1072 if (auto superclass = iterator->second->GetSuperclass()) {
1073 std_out.Printf(" superclass = %s",
1074 superclass->GetClassName().AsCString("<unknown>"));
1075 }
1076 std_out.Printf("\n");
1077 if (m_options.m_verbose) {
1078 for (size_t i = 0; i < iterator->second->GetNumIVars(); i++) {
1079 auto ivar = iterator->second->GetIVarAtIndex(i);
1080 std_out.Printf(
1081 " ivar name = %s type = %s size = %" PRIu64
1082 " offset = %" PRId32 "\n",
1083 ivar.m_name.AsCString("<unknown>"),
1084 ivar.m_type.GetDisplayTypeName().AsCString("<unknown>"),
1085 ivar.m_size, ivar.m_offset);
1086 }
1087
1088 iterator->second->Describe(
1089 nullptr,
1090 [&std_out](const char *name, const char *type) -> bool {
1091 std_out.Printf(" instance method name = %s type = %s\n",
1092 name, type);
1093 return false;
1094 },
1095 [&std_out](const char *name, const char *type) -> bool {
1096 std_out.Printf(" class method name = %s type = %s\n", name,
1097 type);
1098 return false;
1099 },
1100 nullptr);
1101 }
1102 } else {
1103 if (regex_up && !regex_up->Execute(llvm::StringRef()))
1104 continue;
1105 std_out.Printf("isa = 0x%" PRIx64 " has no associated class.\n",
1106 iterator->first);
1107 }
1108 }
1110 return;
1111 }
1112 result.AppendError("current process has no Objective-C runtime loaded");
1114 }
1115
1117};
1118
1120 : public CommandObjectParsed {
1121public:
1124 interpreter, "info", "Dump information on a tagged pointer.",
1125 "language objc tagged-pointer info",
1126 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
1127 eCommandProcessMustBePaused) {
1129 }
1130
1132
1133protected:
1134 void DoExecute(Args &command, CommandReturnObject &result) override {
1135 if (command.GetArgumentCount() == 0) {
1136 result.AppendError("this command requires arguments");
1138 return;
1139 }
1140
1141 Process *process = m_exe_ctx.GetProcessPtr();
1142 ExecutionContext exe_ctx(process);
1143
1144 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
1145 if (!objc_runtime) {
1146 result.AppendError("current process has no Objective-C runtime loaded");
1148 return;
1149 }
1150
1151 ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor =
1152 objc_runtime->GetTaggedPointerVendor();
1153 if (!tagged_ptr_vendor) {
1154 result.AppendError("current process has no tagged pointer support");
1156 return;
1157 }
1158
1159 for (size_t i = 0; i < command.GetArgumentCount(); i++) {
1160 const char *arg_str = command.GetArgumentAtIndex(i);
1161 if (!arg_str)
1162 continue;
1163
1164 Status error;
1166 &exe_ctx, arg_str, LLDB_INVALID_ADDRESS, &error);
1167 if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail()) {
1169 "could not convert '{0}' to a valid address\n", arg_str);
1171 return;
1172 }
1173
1174 if (!tagged_ptr_vendor->IsPossibleTaggedPointer(arg_addr)) {
1175 result.GetOutputStream().Format("{0:x16} is not tagged\n", arg_addr);
1176 continue;
1177 }
1178
1179 auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(arg_addr);
1180 if (!descriptor_sp) {
1182 "could not get class descriptor for {0:x16}\n", arg_addr);
1184 return;
1185 }
1186
1187 uint64_t info_bits = 0;
1188 uint64_t value_bits = 0;
1189 uint64_t payload = 0;
1190 if (descriptor_sp->GetTaggedPointerInfo(&info_bits, &value_bits,
1191 &payload)) {
1192 result.GetOutputStream().Format(
1193 "{0:x} is tagged\n"
1194 "\tpayload = {1:x16}\n"
1195 "\tvalue = {2:x16}\n"
1196 "\tinfo bits = {3:x16}\n"
1197 "\tclass = {4}\n",
1198 arg_addr, payload, value_bits, info_bits,
1199 descriptor_sp->GetClassName().AsCString("<unknown>"));
1200 } else {
1201 result.GetOutputStream().Format("{0:x16} is not tagged\n", arg_addr);
1202 }
1203 }
1204
1206 }
1207};
1208
1210public:
1213 interpreter, "class-table",
1214 "Commands for operating on the Objective-C class table.",
1215 "class-table <subcommand> [<subcommand-options>]") {
1217 "dump",
1219 }
1220
1222};
1223
1225public:
1228 interpreter, "tagged-pointer",
1229 "Commands for operating on Objective-C tagged pointers.",
1230 "tagged-pointer <subcommand> [<subcommand-options>]") {
1232 "info",
1235 }
1236
1238};
1239
1241public:
1244 interpreter, "objc",
1245 "Commands for operating on the Objective-C language runtime.",
1246 "objc <subcommand> [<subcommand-options>]") {
1247 LoadSubCommand("class-table",
1249 new CommandObjectMultiwordObjC_ClassTable(interpreter)));
1250 LoadSubCommand("tagged-pointer",
1252 interpreter)));
1253 }
1254
1255 ~CommandObjectMultiwordObjC() override = default;
1256};
1257
1260 GetPluginNameStatic(), "Apple Objective-C Language Runtime - Version 2",
1262 [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
1263 return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter));
1264 },
1266}
1267
1271
1274 bool catch_bp, bool throw_bp) {
1275 BreakpointResolverSP resolver_sp;
1276
1277 if (throw_bp)
1278 resolver_sp = std::make_shared<BreakpointResolverName>(
1279 bkpt, std::get<1>(GetExceptionThrowLocation()).AsCString(nullptr),
1280 eFunctionNameTypeBase, eLanguageTypeUnknown, Breakpoint::Exact, 0,
1281 /*offset_is_insn_count = */ false, eLazyBoolNo);
1282 // FIXME: We don't do catch breakpoints for ObjC yet.
1283 // Should there be some way for the runtime to specify what it can do in this
1284 // regard?
1285 return resolver_sp;
1286}
1287
1288llvm::Expected<std::unique_ptr<UtilityFunction>>
1290 ExecutionContext &exe_ctx) {
1291 char check_function_code[2048];
1292
1293 int len = 0;
1295 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
1296 extern "C" void *gdb_object_getClass(void *);
1297 extern "C" int printf(const char *format, ...);
1298 extern "C" void
1299 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
1300 if ($__lldb_arg_obj == (void *)0)
1301 return; // nil is ok
1302 if (!gdb_object_getClass($__lldb_arg_obj)) {
1303 *((volatile int *)0) = 'ocgc';
1304 } else if ($__lldb_arg_selector != (void *)0) {
1305 signed char $responds = (signed char)
1306 [(id)$__lldb_arg_obj respondsToSelector:
1307 (void *) $__lldb_arg_selector];
1308 if ($responds == (signed char) 0)
1309 *((volatile int *)0) = 'ocgc';
1310 }
1311 })",
1312 name.c_str());
1313 } else {
1314 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
1315 extern "C" void *gdb_class_getClass(void *);
1316 extern "C" int printf(const char *format, ...);
1317 extern "C" void
1318 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
1319 if ($__lldb_arg_obj == (void *)0)
1320 return; // nil is ok
1321 void **$isa_ptr = (void **)$__lldb_arg_obj;
1322 if (*$isa_ptr == (void *)0 ||
1323 !gdb_class_getClass(*$isa_ptr))
1324 *((volatile int *)0) = 'ocgc';
1325 else if ($__lldb_arg_selector != (void *)0) {
1326 signed char $responds = (signed char)
1327 [(id)$__lldb_arg_obj respondsToSelector:
1328 (void *) $__lldb_arg_selector];
1329 if ($responds == (signed char) 0)
1330 *((volatile int *)0) = 'ocgc';
1331 }
1332 })",
1333 name.c_str());
1334 }
1335
1336 assert(len < (int)sizeof(check_function_code));
1338
1339 return GetTargetRef().CreateUtilityFunction(check_function_code, name,
1340 eLanguageTypeC, exe_ctx);
1341}
1342
1344 const char *ivar_name) {
1345 uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
1346
1347 ConstString class_name = parent_ast_type.GetTypeName();
1348 if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) {
1349 // Make the objective C V2 mangled name for the ivar offset from the class
1350 // name and ivar name
1351 std::string buffer("OBJC_IVAR_$_");
1352 buffer.append(class_name.GetStringRef());
1353 buffer.push_back('.');
1354 buffer.append(ivar_name);
1355 ConstString ivar_const_str(buffer.c_str());
1356
1357 // Try to get the ivar offset address from the symbol table first using the
1358 // name we created above
1359 SymbolContextList sc_list;
1360 Target &target = m_process->GetTarget();
1361 target.GetImages().FindSymbolsWithNameAndType(ivar_const_str,
1362 eSymbolTypeObjCIVar, sc_list);
1364 addr_t ivar_offset_address = LLDB_INVALID_ADDRESS;
1366 Status error;
1367 SymbolContext ivar_offset_symbol;
1368 if (sc_list.GetSize() == 1 &&
1369 sc_list.GetContextAtIndex(0, ivar_offset_symbol)) {
1370 if (ivar_offset_symbol.symbol)
1371 ivar_offset_address =
1372 ivar_offset_symbol.symbol->GetLoadAddress(&target);
1373 }
1374
1375 // If we didn't get the ivar offset address from the symbol table, fall
1376 // back to getting it from the runtime
1377 if (ivar_offset_address == LLDB_INVALID_ADDRESS)
1378 ivar_offset_address = LookupRuntimeSymbol(ivar_const_str);
1379
1380 if (ivar_offset_address != LLDB_INVALID_ADDRESS)
1381 ivar_offset = m_process->ReadUnsignedIntegerFromMemory(
1382 ivar_offset_address, 4, LLDB_INVALID_IVAR_OFFSET, error);
1383 }
1384 return ivar_offset;
1385}
1386
1387// tagged pointers are special not-a-real-pointer values that contain both type
1388// and value information this routine attempts to check with as little
1389// computational effort as possible whether something could possibly be a
1390// tagged pointer - false positives are possible but false negatives shouldn't
1393 return false;
1394 return m_tagged_pointer_vendor_up->IsPossibleTaggedPointer(ptr);
1395}
1396
1397class RemoteNXMapTable {
1398public:
1399 RemoteNXMapTable() : m_end_iterator(*this, -1) {}
1400
1401 void Dump() {
1402 printf("RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n", m_load_addr);
1403 printf("RemoteNXMapTable.m_count = %u\n", m_count);
1404 printf("RemoteNXMapTable.m_num_buckets_minus_one = %u\n",
1405 m_num_buckets_minus_one);
1406 printf("RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n", m_buckets_ptr);
1407 }
1408
1409 bool ParseHeader(Process *process, lldb::addr_t load_addr) {
1410 m_process = process;
1411 m_load_addr = load_addr;
1412 m_map_pair_size = m_process->GetAddressByteSize() * 2;
1413 m_invalid_key =
1414 m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX;
1415 Status err;
1416
1417 // This currently holds true for all platforms we support, but we might
1418 // need to change this to use get the actually byte size of "unsigned" from
1419 // the target AST...
1420 const uint32_t unsigned_byte_size = sizeof(uint32_t);
1421 // Skip the prototype as we don't need it (const struct
1422 // +NXMapTablePrototype *prototype)
1423
1424 bool success = true;
1425 if (load_addr == LLDB_INVALID_ADDRESS)
1426 success = false;
1427 else {
1428 lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize();
1430 // unsigned count;
1431 m_count = m_process->ReadUnsignedIntegerFromMemory(
1432 cursor, unsigned_byte_size, 0, err);
1433 if (m_count) {
1434 cursor += unsigned_byte_size;
1435
1436 // unsigned nbBucketsMinusOne;
1437 m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory(
1438 cursor, unsigned_byte_size, 0, err);
1439 cursor += unsigned_byte_size;
1440
1441 // void *buckets;
1442 m_buckets_ptr = m_process->ReadPointerFromMemory(cursor, err);
1443
1444 success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS;
1445 }
1447
1448 if (!success) {
1449 m_count = 0;
1452 }
1453 return success;
1454 }
1456 // const_iterator mimics NXMapState and its code comes from NXInitMapState
1457 // and NXNextMapState.
1458 typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element;
1460 friend class const_iterator;
1461 class const_iterator {
1462 public:
1463 const_iterator(RemoteNXMapTable &parent, int index)
1464 : m_parent(parent), m_index(index) {
1466 }
1467
1468 const_iterator(const const_iterator &rhs)
1469 : m_parent(rhs.m_parent), m_index(rhs.m_index) {
1470 // AdvanceToValidIndex() has been called by rhs already.
1471 }
1472
1473 const_iterator &operator=(const const_iterator &rhs) {
1474 // AdvanceToValidIndex() has been called by rhs already.
1475 assert(&m_parent == &rhs.m_parent);
1476 m_index = rhs.m_index;
1477 return *this;
1478 }
1479
1480 bool operator==(const const_iterator &rhs) const {
1481 if (&m_parent != &rhs.m_parent)
1482 return false;
1483 if (m_index != rhs.m_index)
1484 return false;
1485
1486 return true;
1487 }
1488
1489 bool operator!=(const const_iterator &rhs) const {
1490 return !(operator==(rhs));
1491 }
1492
1493 const_iterator &operator++() {
1494 AdvanceToValidIndex();
1495 return *this;
1497
1498 element operator*() const {
1499 if (m_index == -1) {
1500 // TODO find a way to make this an error, but not an assert
1501 return element();
1502 }
1503
1504 lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1505 size_t map_pair_size = m_parent.m_map_pair_size;
1506 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1507
1508 Status err;
1509
1510 lldb::addr_t key =
1511 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1512 if (!err.Success())
1513 return element();
1514 lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory(
1515 pair_ptr + m_parent.m_process->GetAddressByteSize(), err);
1516 if (!err.Success())
1517 return element();
1518
1519 std::string key_string;
1521 m_parent.m_process->ReadCStringFromMemory(key, key_string, err);
1522 if (!err.Success())
1523 return element();
1524
1525 return element(ConstString(key_string.c_str()),
1528
1529 private:
1530 void AdvanceToValidIndex() {
1531 if (m_index == -1)
1532 return;
1534 const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1535 const size_t map_pair_size = m_parent.m_map_pair_size;
1536 const lldb::addr_t invalid_key = m_parent.m_invalid_key;
1537 Status err;
1538
1539 while (m_index--) {
1540 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1542 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1544 if (!err.Success()) {
1545 m_index = -1;
1546 return;
1547 }
1548
1549 if (key != invalid_key)
1550 return;
1552 }
1553 RemoteNXMapTable &m_parent;
1554 int m_index;
1555 };
1556
1557 const_iterator begin() {
1558 return const_iterator(*this, m_num_buckets_minus_one + 1);
1559 }
1560
1561 const_iterator end() { return m_end_iterator; }
1562
1563 uint32_t GetCount() const { return m_count; }
1564
1565 uint32_t GetBucketCount() const { return m_num_buckets_minus_one; }
1566
1567 lldb::addr_t GetBucketDataPointer() const { return m_buckets_ptr; }
1568
1569 lldb::addr_t GetTableLoadAddress() const { return m_load_addr; }
1570
1571private:
1572 // contents of _NXMapTable struct
1573 uint32_t m_count = 0;
1574 uint32_t m_num_buckets_minus_one = 0;
1575 lldb::addr_t m_buckets_ptr = LLDB_INVALID_ADDRESS;
1576 lldb_private::Process *m_process = nullptr;
1577 const_iterator m_end_iterator;
1579 size_t m_map_pair_size = 0;
1580 lldb::addr_t m_invalid_key = 0;
1581};
1582
1584
1586 const RemoteNXMapTable &hash_table) {
1587 m_count = hash_table.GetCount();
1589 m_buckets_ptr = hash_table.GetBucketDataPointer();
1590}
1591
1593 Process *process, AppleObjCRuntimeV2 *runtime,
1594 RemoteNXMapTable &hash_table) {
1595 if (!hash_table.ParseHeader(process, runtime->GetISAHashTablePointer())) {
1596 return false; // Failed to parse the header, no need to update anything
1597 }
1598
1599 // Check with out current signature and return true if the count, number of
1600 // buckets or the hash table address changes.
1601 if (m_count == hash_table.GetCount() &&
1602 m_num_buckets == hash_table.GetBucketCount() &&
1603 m_buckets_ptr == hash_table.GetBucketDataPointer()) {
1604 // Hash table hasn't changed
1605 return false;
1606 }
1607 // Hash table data has changed, we need to update
1608 return true;
1609}
1610
1613 ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp;
1614 if (auto *non_pointer_isa_cache = GetNonPointerIsaCache())
1615 class_descriptor_sp = non_pointer_isa_cache->GetClassDescriptor(isa);
1616 if (!class_descriptor_sp)
1617 class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa);
1618 return class_descriptor_sp;
1619}
1620
1623 ValueObjectSet seen;
1624 return GetClassDescriptorImpl(valobj, seen);
1625}
1626
1629 ValueObjectSet &seen) {
1630 seen.insert(&valobj);
1631
1632 ClassDescriptorSP objc_class_sp;
1633 if (valobj.IsBaseClass()) {
1634 ValueObject *parent = valobj.GetParent();
1635 // Fail if there's a cycle in our parent chain.
1636 if (!parent || seen.count(parent))
1637 return nullptr;
1638 if (ClassDescriptorSP parent_descriptor_sp =
1639 GetClassDescriptorImpl(*parent, seen))
1640 return parent_descriptor_sp->GetSuperclass();
1641 return nullptr;
1642 }
1643 // if we get an invalid VO (which might still happen when playing around with
1644 // pointers returned by the expression parser, don't consider this a valid
1645 // ObjC object)
1646 if (!valobj.GetCompilerType().IsValid())
1647 return objc_class_sp;
1648 addr_t isa_pointer = valobj.GetPointerValue().address;
1649
1650 // tagged pointer
1651 if (IsTaggedPointer(isa_pointer))
1652 return m_tagged_pointer_vendor_up->GetClassDescriptor(isa_pointer);
1653 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1654
1655 Process *process = exe_ctx.GetProcessPtr();
1656 if (!process)
1657 return objc_class_sp;
1658
1659 Status error;
1660 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
1661 if (isa == LLDB_INVALID_ADDRESS)
1662 return objc_class_sp;
1663
1664 objc_class_sp = GetClassDescriptorFromISA(isa);
1665 if (!objc_class_sp) {
1666 if (ABISP abi_sp = process->GetABI())
1667 isa = abi_sp->FixCodeAddress(isa);
1668 objc_class_sp = GetClassDescriptorFromISA(isa);
1669 }
1670
1671 if (isa && !objc_class_sp) {
1673 LLDB_LOGF(log,
1674 "0x%" PRIx64 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was "
1675 "not in class descriptor cache 0x%" PRIx64,
1676 isa_pointer, isa);
1677 }
1678 return objc_class_sp;
1679}
1684
1685 Process *process = GetProcess();
1686 ModuleSP objc_module_sp(GetObjCModule());
1687
1688 if (!objc_module_sp)
1689 return LLDB_INVALID_ADDRESS;
1690
1691 static ConstString g_gdb_objc_obfuscator(
1692 "objc_debug_taggedpointer_obfuscator");
1693
1694 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1695 g_gdb_objc_obfuscator, lldb::eSymbolTypeAny);
1696 if (symbol) {
1697 lldb::addr_t g_gdb_obj_obfuscator_ptr =
1698 symbol->GetLoadAddress(&process->GetTarget());
1699
1700 if (g_gdb_obj_obfuscator_ptr != LLDB_INVALID_ADDRESS) {
1701 Status error;
1703 process->ReadPointerFromMemory(g_gdb_obj_obfuscator_ptr, error);
1704 }
1705 }
1706 // If we don't have a correct value at this point, there must be no
1707 // obfuscation.
1710
1712}
1713
1716 Process *process = GetProcess();
1717
1718 ModuleSP objc_module_sp(GetObjCModule());
1719
1720 if (!objc_module_sp)
1721 return LLDB_INVALID_ADDRESS;
1722
1723 static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes");
1724
1725 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1726 g_gdb_objc_realized_classes, lldb::eSymbolTypeAny);
1727 if (symbol) {
1728 lldb::addr_t gdb_objc_realized_classes_ptr =
1729 symbol->GetLoadAddress(&process->GetTarget());
1730
1731 if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS) {
1732 Status error;
1734 gdb_objc_realized_classes_ptr, error);
1735 }
1736 }
1737 }
1738 return m_isa_hash_table_ptr;
1739}
1740
1741std::unique_ptr<AppleObjCRuntimeV2::SharedCacheImageHeaders>
1743 AppleObjCRuntimeV2 &runtime) {
1745 Process *process = runtime.GetProcess();
1746 ModuleSP objc_module_sp(runtime.GetObjCModule());
1747 if (!objc_module_sp || !process)
1748 return nullptr;
1749
1750 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1751 ConstString("objc_debug_headerInfoRWs"), lldb::eSymbolTypeAny);
1752 if (!symbol) {
1753 LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' unavailable. Some "
1754 "information concerning the shared cache may be unavailable");
1755 return nullptr;
1756 }
1757
1758 lldb::addr_t objc_debug_headerInfoRWs_addr =
1759 symbol->GetLoadAddress(&process->GetTarget());
1760 if (objc_debug_headerInfoRWs_addr == LLDB_INVALID_ADDRESS) {
1761 LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' was found but we were "
1762 "unable to get its load address");
1763 return nullptr;
1764 }
1765
1766 Status error;
1767 lldb::addr_t objc_debug_headerInfoRWs_ptr =
1768 process->ReadPointerFromMemory(objc_debug_headerInfoRWs_addr, error);
1769 if (error.Fail()) {
1770 LLDB_LOG(log,
1771 "Failed to read address of 'objc_debug_headerInfoRWs' at {0:x}",
1772 objc_debug_headerInfoRWs_addr);
1773 return nullptr;
1774 }
1775
1776 const size_t metadata_size =
1777 sizeof(uint32_t) + sizeof(uint32_t); // count + entsize
1778 DataBufferHeap metadata_buffer(metadata_size, '\0');
1779 process->ReadMemory(objc_debug_headerInfoRWs_ptr, metadata_buffer.GetBytes(),
1780 metadata_size, error);
1781 if (error.Fail()) {
1782 LLDB_LOG(log,
1783 "Unable to read metadata for 'objc_debug_headerInfoRWs' at {0:x}",
1784 objc_debug_headerInfoRWs_ptr);
1785 return nullptr;
1786 }
1787
1788 DataExtractor metadata_extractor(metadata_buffer.GetBytes(), metadata_size,
1789 process->GetByteOrder(),
1790 process->GetAddressByteSize());
1791 lldb::offset_t cursor = 0;
1792 uint32_t count = metadata_extractor.GetU32_unchecked(&cursor);
1793 uint32_t entsize = metadata_extractor.GetU32_unchecked(&cursor);
1794 if (count == 0 || entsize == 0) {
1795 LLDB_LOG(log,
1796 "'objc_debug_headerInfoRWs' had count {0} with entsize {1}. These "
1797 "should both be non-zero.",
1798 count, entsize);
1799 return nullptr;
1800 }
1801
1802 std::unique_ptr<SharedCacheImageHeaders> shared_cache_image_headers(
1803 new SharedCacheImageHeaders(runtime, objc_debug_headerInfoRWs_ptr, count,
1804 entsize));
1805 if (auto Err = shared_cache_image_headers->UpdateIfNeeded()) {
1806 LLDB_LOG_ERROR(log, std::move(Err),
1807 "Failed to update SharedCacheImageHeaders: {0}");
1808 return nullptr;
1809 }
1810
1811 return shared_cache_image_headers;
1812}
1813
1815 if (!m_needs_update)
1816 return llvm::Error::success();
1817
1818 Process *process = m_runtime.GetProcess();
1819 constexpr lldb::addr_t metadata_size =
1820 sizeof(uint32_t) + sizeof(uint32_t); // count + entsize
1821
1822 Status error;
1823 const lldb::addr_t first_header_addr = m_headerInfoRWs_ptr + metadata_size;
1824 DataBufferHeap header_buffer(m_entsize, '\0');
1825 lldb::offset_t cursor = 0;
1826 for (uint32_t i = 0; i < m_count; i++) {
1827 const lldb::addr_t header_addr = first_header_addr + (i * m_entsize);
1828 process->ReadMemory(header_addr, header_buffer.GetBytes(), m_entsize,
1829 error);
1830 if (error.Fail())
1831 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1832 "Failed to read memory from inferior when "
1833 "populating SharedCacheImageHeaders");
1834
1835 DataExtractor header_extractor(header_buffer.GetBytes(), m_entsize,
1836 process->GetByteOrder(),
1837 process->GetAddressByteSize());
1838 cursor = 0;
1839 bool is_loaded = false;
1840 if (m_entsize == 4) {
1841 uint32_t header = header_extractor.GetU32_unchecked(&cursor);
1842 if (header & 1)
1843 is_loaded = true;
1844 } else {
1845 uint64_t header = header_extractor.GetU64_unchecked(&cursor);
1846 if (header & 1)
1847 is_loaded = true;
1849
1850 if (is_loaded)
1851 m_loaded_images.set(i);
1852 else
1853 m_loaded_images.reset(i);
1854 }
1855 m_needs_update = false;
1856 m_version++;
1857 return llvm::Error::success();
1858}
1859
1861 uint16_t image_index) {
1862 if (image_index >= m_count)
1863 return false;
1864 if (auto Err = UpdateIfNeeded()) {
1866 LLDB_LOG_ERROR(log, std::move(Err),
1867 "Failed to update SharedCacheImageHeaders: {0}");
1868 }
1869 return m_loaded_images.test(image_index);
1870}
1871
1873 if (auto Err = UpdateIfNeeded()) {
1875 LLDB_LOG_ERROR(log, std::move(Err),
1876 "Failed to update SharedCacheImageHeaders: {0}");
1877 }
1878 return m_version;
1879}
1880
1881std::unique_ptr<UtilityFunction>
1883 ExecutionContext &exe_ctx, Helper helper, std::string code,
1884 std::string name) {
1886
1887 LLDB_LOG(log, "Creating utility function {0}", name);
1888
1889 TypeSystemClangSP scratch_ts_sp =
1891 if (!scratch_ts_sp)
1892 return {};
1893
1894 auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
1895 std::move(code), std::move(name), eLanguageTypeC, exe_ctx);
1896 if (!utility_fn_or_error) {
1898 log, utility_fn_or_error.takeError(),
1899 "Failed to get utility function for dynamic info extractor: {0}");
1900 return {};
1901 }
1902
1903 // Make some types for our arguments.
1904 CompilerType clang_uint32_t_type =
1905 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1906 CompilerType clang_void_pointer_type =
1907 scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
1908
1909 // Make the runner function for our implementation utility function.
1910 ValueList arguments;
1911 Value value;
1913 value.SetCompilerType(clang_void_pointer_type);
1914 arguments.PushValue(value);
1915 arguments.PushValue(value);
1917 value.SetCompilerType(clang_uint32_t_type);
1918 arguments.PushValue(value);
1919
1920 // objc_getRealizedClassList_trylock takes an additional buffer and length.
1922 value.SetCompilerType(clang_void_pointer_type);
1923 arguments.PushValue(value);
1924 value.SetCompilerType(clang_uint32_t_type);
1925 arguments.PushValue(value);
1926 }
1927
1928 arguments.PushValue(value);
1929
1930 std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error);
1931
1932 Status error;
1933 utility_fn->MakeFunctionCaller(clang_uint32_t_type, arguments,
1934 exe_ctx.GetThreadSP(), error);
1935
1936 if (error.Fail()) {
1937 LLDB_LOG(log,
1938 "Failed to make function caller for implementation lookup: {0}.",
1939 error.AsCString());
1940 return {};
1941 }
1942
1943 return utility_fn;
1944}
1956 return m_gdb_objc_realized_classes_helper.utility_function.get();
1957 }
1959 if (!m_objc_copyRealizedClassList_helper.utility_function)
1960 m_objc_copyRealizedClassList_helper.utility_function =
1961 GetClassInfoUtilityFunctionImpl(exe_ctx, helper,
1964 return m_objc_copyRealizedClassList_helper.utility_function.get();
1965 }
1966 case objc_getRealizedClassList_trylock: {
1967 if (!m_objc_getRealizedClassList_trylock_helper.utility_function)
1968 m_objc_getRealizedClassList_trylock_helper.utility_function =
1969 GetClassInfoUtilityFunctionImpl(exe_ctx, helper,
1972 return m_objc_getRealizedClassList_trylock_helper.utility_function.get();
1973 }
1974 }
1975 llvm_unreachable("Unexpected helper");
1976}
1977
1980 switch (helper) {
1981 case gdb_objc_realized_classes:
1982 return m_gdb_objc_realized_classes_helper.args;
1983 case objc_copyRealizedClassList:
1984 return m_objc_copyRealizedClassList_helper.args;
1985 case objc_getRealizedClassList_trylock:
1986 return m_objc_getRealizedClassList_trylock_helper.args;
1987 }
1988 llvm_unreachable("Unexpected helper");
1990
1993 ExecutionContext &exe_ctx) const {
1994 if (!m_runtime.m_has_objc_copyRealizedClassList &&
1995 !m_runtime.m_has_objc_getRealizedClassList_trylock)
1997
1998 if (Process *process = m_runtime.GetProcess()) {
1999 if (DynamicLoader *loader = process->GetDynamicLoader()) {
2000 if (loader->IsFullyInitialized()) {
2001 switch (exe_ctx.GetTargetRef().GetDynamicClassInfoHelper()) {
2003 [[fallthrough]];
2005 if (m_runtime.m_has_objc_getRealizedClassList_trylock)
2007 [[fallthrough]];
2009 if (m_runtime.m_has_objc_copyRealizedClassList)
2011 [[fallthrough]];
2014 }
2015 }
2016 }
2017 }
2018
2020}
2021
2022std::unique_ptr<UtilityFunction>
2026
2027 LLDB_LOG(log, "Creating utility function {0}",
2029
2030 TypeSystemClangSP scratch_ts_sp =
2032 if (!scratch_ts_sp)
2033 return {};
2034
2035 // If the inferior objc.dylib has the class_getNameRaw function, use that in
2036 // our jitted expression. Else fall back to the old class_getName.
2037 static ConstString g_class_getName_symbol_name("class_getName");
2038 static ConstString g_class_getNameRaw_symbol_name(
2039 "objc_debug_class_getNameRaw");
2040
2041 ConstString class_name_getter_function_name =
2042 m_runtime.HasSymbol(g_class_getNameRaw_symbol_name)
2043 ? g_class_getNameRaw_symbol_name
2044 : g_class_getName_symbol_name;
2045
2046 // Substitute in the correct class_getName / class_getNameRaw function name,
2047 // concatenate the two parts of our expression text. The format string has
2048 // two %s's, so provide the name twice.
2049 std::string shared_class_expression;
2050 llvm::raw_string_ostream(shared_class_expression) << llvm::formatv(
2051 g_shared_cache_class_name_funcptr, class_name_getter_function_name,
2052 class_name_getter_function_name);
2053
2054 shared_class_expression += g_get_shared_cache_class_info_definitions;
2055 shared_class_expression += g_get_shared_cache_class_info_body;
2056
2057 auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
2058 std::move(shared_class_expression), g_get_shared_cache_class_info_name,
2059 eLanguageTypeC, exe_ctx);
2060
2061 if (!utility_fn_or_error) {
2063 log, utility_fn_or_error.takeError(),
2064 "Failed to get utility function for shared class info extractor: {0}");
2065 return nullptr;
2066 }
2067
2068 // Make some types for our arguments.
2069 CompilerType clang_uint32_t_type =
2070 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
2071 CompilerType clang_void_pointer_type =
2072 scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
2073 CompilerType clang_uint64_t_pointer_type =
2074 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64)
2075 .GetPointerType();
2076 CompilerType clang_uint32_t_pointer_type =
2077 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32)
2078 .GetPointerType();
2079
2080 // Next make the function caller for our implementation utility function.
2081 ValueList arguments;
2082 Value value;
2084 value.SetCompilerType(clang_void_pointer_type);
2085 arguments.PushValue(value);
2086 arguments.PushValue(value);
2087 arguments.PushValue(value);
2090 value.SetCompilerType(clang_uint64_t_pointer_type);
2091 arguments.PushValue(value);
2092
2094 value.SetCompilerType(clang_uint32_t_type);
2095 arguments.PushValue(value);
2098 value.SetCompilerType(clang_uint32_t_pointer_type);
2099 arguments.PushValue(value);
2100
2102 value.SetCompilerType(clang_uint32_t_type);
2103 arguments.PushValue(value);
2104
2105 std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error);
2106
2107 Status error;
2108 utility_fn->MakeFunctionCaller(clang_uint32_t_type, arguments,
2109 exe_ctx.GetThreadSP(), error);
2110
2111 if (error.Fail()) {
2112 LLDB_LOG(log,
2113 "Failed to make function caller for implementation lookup: {0}.",
2114 error.AsCString());
2115 return {};
2116 }
2117
2118 return utility_fn;
2119}
2120
2123 ExecutionContext &exe_ctx) {
2124 if (!m_utility_function)
2125 m_utility_function = GetClassInfoUtilityFunctionImpl(exe_ctx);
2126 return m_utility_function.get();
2127}
2128
2129AppleObjCRuntimeV2::DescriptorMapUpdateResult
2131 RemoteNXMapTable &hash_table) {
2132 Process *process = m_runtime.GetProcess();
2133 if (process == nullptr)
2135
2136 uint32_t num_class_infos = 0;
2137
2139
2140 ExecutionContext exe_ctx;
2141
2142 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
2143
2144 if (!thread_sp)
2146
2147 if (!thread_sp->SafeToCallFunctions())
2149
2150 thread_sp->CalculateExecutionContext(exe_ctx);
2151 TypeSystemClangSP scratch_ts_sp =
2153
2154 if (!scratch_ts_sp)
2156
2157 Address function_address;
2158
2159 const uint32_t addr_size = process->GetAddressByteSize();
2160
2161 Status err;
2162
2163 // Compute which helper we're going to use for this update.
2164 const DynamicClassInfoExtractor::Helper helper = ComputeHelper(exe_ctx);
2165
2166 // Read the total number of classes from the hash table
2167 const uint32_t num_classes =
2169 ? hash_table.GetCount()
2170 : m_runtime.m_realized_class_generation_count;
2171 if (num_classes == 0) {
2172 LLDB_LOGF(log, "No dynamic classes found.");
2174 }
2175
2176 UtilityFunction *get_class_info_code =
2177 GetClassInfoUtilityFunction(exe_ctx, helper);
2178 if (!get_class_info_code) {
2179 // The callee will have already logged a useful error message.
2181 }
2182
2183 FunctionCaller *get_class_info_function =
2184 get_class_info_code->GetFunctionCaller();
2185
2186 if (!get_class_info_function) {
2187 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
2189 }
2190
2191 ValueList arguments = get_class_info_function->GetArgumentValues();
2192
2193 DiagnosticManager diagnostics;
2194
2195 const uint32_t class_info_byte_size = addr_size + 4;
2196 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
2197 lldb::addr_t class_infos_addr = process->AllocateMemory(
2198 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
2199
2200 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
2201 LLDB_LOGF(log,
2202 "unable to allocate %" PRIu32
2203 " bytes in process for shared cache read",
2204 class_infos_byte_size);
2206 }
2207
2208 llvm::scope_exit deallocate_class_infos([&] {
2209 // Deallocate the memory we allocated for the ClassInfo array
2210 if (class_infos_addr != LLDB_INVALID_ADDRESS)
2211 process->DeallocateMemory(class_infos_addr);
2212 });
2213
2214 lldb::addr_t class_buffer_addr = LLDB_INVALID_ADDRESS;
2215 const uint32_t class_byte_size = addr_size;
2216 const uint32_t class_buffer_len = num_classes;
2217 const uint32_t class_buffer_byte_size = class_buffer_len * class_byte_size;
2218 if (helper == Helper::objc_getRealizedClassList_trylock) {
2219 class_buffer_addr = process->AllocateMemory(
2220 class_buffer_byte_size, ePermissionsReadable | ePermissionsWritable,
2221 err);
2222 if (class_buffer_addr == LLDB_INVALID_ADDRESS) {
2223 LLDB_LOGF(log,
2224 "unable to allocate %" PRIu32
2225 " bytes in process for shared cache read",
2226 class_buffer_byte_size);
2228 }
2229 }
2230
2231 llvm::scope_exit deallocate_class_buffer([&] {
2232 // Deallocate the memory we allocated for the Class array
2233 if (class_buffer_addr != LLDB_INVALID_ADDRESS)
2234 process->DeallocateMemory(class_buffer_addr);
2235 });
2236
2237 std::lock_guard<std::mutex> guard(m_mutex);
2238
2239 // Fill in our function argument values
2240 uint32_t index = 0;
2241 arguments.GetValueAtIndex(index++)->GetScalar() =
2242 hash_table.GetTableLoadAddress();
2243 arguments.GetValueAtIndex(index++)->GetScalar() = class_infos_addr;
2244 arguments.GetValueAtIndex(index++)->GetScalar() = class_infos_byte_size;
2245
2246 if (class_buffer_addr != LLDB_INVALID_ADDRESS) {
2247 arguments.GetValueAtIndex(index++)->GetScalar() = class_buffer_addr;
2248 arguments.GetValueAtIndex(index++)->GetScalar() = class_buffer_byte_size;
2249 }
2250
2251 // Only dump the runtime classes from the expression evaluation if the log is
2252 // verbose:
2253 Log *type_log = GetLog(LLDBLog::Types);
2254 bool dump_log = type_log && type_log->GetVerbose();
2255
2256 arguments.GetValueAtIndex(index++)->GetScalar() = dump_log ? 1 : 0;
2257
2258 bool success = false;
2259
2260 diagnostics.Clear();
2261
2262 // Write our function arguments into the process so we can run our function
2263 if (get_class_info_function->WriteFunctionArguments(
2264 exe_ctx, GetClassInfoArgs(helper), arguments, diagnostics)) {
2265 EvaluateExpressionOptions options;
2266 options.SetUnwindOnError(true);
2267 options.SetTryAllThreads(false);
2268 options.SetStopOthers(true);
2269 options.SetIgnoreBreakpoints(true);
2270 options.SetTimeout(process->GetUtilityExpressionTimeout());
2271 options.SetIsForUtilityExpr(true);
2272
2273 CompilerType clang_uint32_t_type =
2274 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
2275
2276 Value return_value;
2278 return_value.SetCompilerType(clang_uint32_t_type);
2279 return_value.GetScalar() = 0;
2280
2281 diagnostics.Clear();
2282
2283 // Run the function
2284 ExpressionResults results = get_class_info_function->ExecuteFunction(
2285 exe_ctx, &GetClassInfoArgs(helper), options, diagnostics, return_value);
2287 if (results == eExpressionCompleted) {
2288 // The result is the number of ClassInfo structures that were filled in
2289 num_class_infos = return_value.GetScalar().ULong();
2290 LLDB_LOG(log, "Discovered {0} Objective-C classes", num_class_infos);
2291 if (num_class_infos > 0) {
2292 // Read the ClassInfo structures
2293 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
2294 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(),
2295 buffer.GetByteSize(),
2296 err) == buffer.GetByteSize()) {
2297 DataExtractor class_infos_data(buffer.GetBytes(),
2298 buffer.GetByteSize(),
2299 process->GetByteOrder(), addr_size);
2300 m_runtime.ParseClassInfoArray(class_infos_data, num_class_infos);
2301 }
2302 }
2303 success = true;
2304 } else {
2305 if (log) {
2306 LLDB_LOGF(log, "Error evaluating our find class name function.");
2307 diagnostics.Dump(log);
2308 }
2309 }
2310 } else {
2311 if (log) {
2312 LLDB_LOGF(log, "Error writing function arguments.");
2313 diagnostics.Dump(log);
2314 }
2315 }
2316
2317 return DescriptorMapUpdateResult(success, false, num_class_infos);
2318}
2319
2320uint32_t AppleObjCRuntimeV2::ParseClassInfoArray(const DataExtractor &data,
2321 uint32_t num_class_infos) {
2322 // Parses an array of "num_class_infos" packed ClassInfo structures:
2323 //
2324 // struct ClassInfo
2325 // {
2326 // Class isa;
2327 // uint32_t hash;
2328 // } __attribute__((__packed__));
2329
2330 Log *log = GetLog(LLDBLog::Types);
2331 bool should_log = log && log->GetVerbose();
2332
2333 uint32_t num_parsed = 0;
2334
2335 // Iterate through all ClassInfo structures
2336 lldb::offset_t offset = 0;
2337 for (uint32_t i = 0; i < num_class_infos; ++i) {
2338 ObjCISA isa = data.GetAddress(&offset);
2339
2340 if (isa == 0) {
2341 if (should_log)
2342 LLDB_LOGF(
2343 log, "AppleObjCRuntimeV2 found NULL isa, ignoring this class info");
2344 continue;
2345 }
2346 // Check if we already know about this ISA, if we do, the info will never
2347 // change, so we can just skip it.
2348 if (ISAIsCached(isa)) {
2349 if (should_log)
2350 LLDB_LOGF(log,
2351 "AppleObjCRuntimeV2 found cached isa=0x%" PRIx64
2352 ", ignoring this class info",
2353 isa);
2354 offset += 4;
2355 } else {
2356 // Read the 32 bit hash for the class name
2357 const uint32_t name_hash = data.GetU32(&offset);
2358 ClassDescriptorSP descriptor_sp(
2359 new ClassDescriptorV2(*this, isa, nullptr));
2360
2361 // The code in g_get_shared_cache_class_info_body sets the value of the
2362 // hash to 0 to signal a demangled symbol. We use class_getName() in that
2363 // code to find the class name, but this returns a demangled name for
2364 // Swift symbols. For those symbols, recompute the hash here by reading
2365 // their name from the runtime.
2366 if (name_hash)
2367 AddClass(isa, descriptor_sp, name_hash);
2368 else
2369 AddClass(isa, descriptor_sp,
2370 descriptor_sp->GetClassName().AsCString(nullptr));
2371 num_parsed++;
2372 if (should_log)
2373 LLDB_LOGF(log,
2374 "AppleObjCRuntimeV2 added isa=0x%" PRIx64
2375 ", hash=0x%8.8x, name=%s",
2376 isa, name_hash,
2377 descriptor_sp->GetClassName().AsCString("<unknown>"));
2378 }
2379 }
2380 if (should_log)
2381 LLDB_LOGF(log, "AppleObjCRuntimeV2 parsed %" PRIu32 " class infos",
2382 num_parsed);
2383 return num_parsed;
2384}
2385
2387 if (!m_objc_module_sp)
2388 return false;
2389 if (const Symbol *symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType(
2390 Name, lldb::eSymbolTypeCode)) {
2391 if (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())
2392 return true;
2393 }
2394 return false;
2395}
2396
2397AppleObjCRuntimeV2::DescriptorMapUpdateResult
2399 Process *process = m_runtime.GetProcess();
2400 if (process == nullptr)
2402
2404
2405 ExecutionContext exe_ctx;
2406
2407 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
2408
2409 if (!thread_sp)
2411
2412 if (!thread_sp->SafeToCallFunctions())
2414
2415 thread_sp->CalculateExecutionContext(exe_ctx);
2416 TypeSystemClangSP scratch_ts_sp =
2418
2419 if (!scratch_ts_sp)
2421
2422 Address function_address;
2423
2424 const uint32_t addr_size = process->GetAddressByteSize();
2425
2426 Status err;
2427
2428 uint32_t num_class_infos = 0;
2429
2430 const lldb::addr_t objc_opt_ptr = m_runtime.GetSharedCacheReadOnlyAddress();
2431 const lldb::addr_t shared_cache_base_addr =
2432 m_runtime.GetSharedCacheBaseAddress();
2433
2434 if (objc_opt_ptr == LLDB_INVALID_ADDRESS ||
2435 shared_cache_base_addr == LLDB_INVALID_ADDRESS)
2437
2438 // The number of entries to pre-allocate room for.
2439 // Each entry is (addrsize + 4) bytes
2440 const uint32_t max_num_classes_in_buffer = 212992;
2441
2442 UtilityFunction *get_class_info_code = GetClassInfoUtilityFunction(exe_ctx);
2443 if (!get_class_info_code) {
2444 // The callee will have already logged a useful error message.
2446 }
2447
2448 FunctionCaller *get_shared_cache_class_info_function =
2449 get_class_info_code->GetFunctionCaller();
2450
2451 if (!get_shared_cache_class_info_function) {
2452 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
2454 }
2455
2456 ValueList arguments =
2457 get_shared_cache_class_info_function->GetArgumentValues();
2458
2459 DiagnosticManager diagnostics;
2460
2461 const uint32_t class_info_byte_size = addr_size + 4;
2462 const uint32_t class_infos_byte_size =
2463 max_num_classes_in_buffer * class_info_byte_size;
2464 lldb::addr_t class_infos_addr = process->AllocateMemory(
2465 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
2466 const uint32_t relative_selector_offset_addr_size = 64;
2467 lldb::addr_t relative_selector_offset_addr =
2468 process->AllocateMemory(relative_selector_offset_addr_size,
2469 ePermissionsReadable | ePermissionsWritable, err);
2470 constexpr uint32_t class_info_start_idx_byte_size = sizeof(uint32_t);
2471 lldb::addr_t class_info_start_idx_addr =
2472 process->AllocateMemory(class_info_start_idx_byte_size,
2473 ePermissionsReadable | ePermissionsWritable, err);
2474
2475 if (class_infos_addr == LLDB_INVALID_ADDRESS ||
2476 relative_selector_offset_addr == LLDB_INVALID_ADDRESS ||
2477 class_info_start_idx_addr == LLDB_INVALID_ADDRESS) {
2478 LLDB_LOGF(log,
2479 "unable to allocate %" PRIu32
2480 " bytes in process for shared cache read",
2481 class_infos_byte_size);
2483 }
2484
2485 const uint32_t start_idx_init_value = 0;
2486 size_t bytes_written = process->WriteMemory(
2487 class_info_start_idx_addr, &start_idx_init_value, sizeof(uint32_t), err);
2488 if (bytes_written != sizeof(uint32_t)) {
2489 LLDB_LOGF(log,
2490 "unable to write %" PRIu32
2491 " bytes in process for shared cache read",
2492 class_infos_byte_size);
2494 }
2495
2496 std::lock_guard<std::mutex> guard(m_mutex);
2497
2498 // Fill in our function argument values
2499 arguments.GetValueAtIndex(0)->GetScalar() = objc_opt_ptr;
2500 arguments.GetValueAtIndex(1)->GetScalar() = shared_cache_base_addr;
2501 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_addr;
2502 arguments.GetValueAtIndex(3)->GetScalar() = relative_selector_offset_addr;
2503 arguments.GetValueAtIndex(4)->GetScalar() = class_infos_byte_size;
2504 arguments.GetValueAtIndex(5)->GetScalar() = class_info_start_idx_addr;
2505 // Only dump the runtime classes from the expression evaluation if the log is
2506 // verbose:
2507 Log *type_log = GetLog(LLDBLog::Types);
2508 bool dump_log = type_log && type_log->GetVerbose();
2509
2510 arguments.GetValueAtIndex(6)->GetScalar() = dump_log ? 1 : 0;
2511
2512 bool success = false;
2513
2514 diagnostics.Clear();
2515
2516 // Write our function arguments into the process so we can run our function
2517 if (get_shared_cache_class_info_function->WriteFunctionArguments(
2518 exe_ctx, m_args, arguments, diagnostics)) {
2519 EvaluateExpressionOptions options;
2520 options.SetUnwindOnError(true);
2521 options.SetTryAllThreads(false);
2522 options.SetStopOthers(true);
2523 options.SetIgnoreBreakpoints(true);
2524 options.SetTimeout(process->GetUtilityExpressionTimeout());
2525 options.SetIsForUtilityExpr(true);
2526
2527 CompilerType clang_uint32_t_type =
2528 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
2529
2530 Value return_value;
2532 return_value.SetCompilerType(clang_uint32_t_type);
2533 return_value.GetScalar() = 0;
2534
2535 diagnostics.Clear();
2536
2537 uint32_t num_class_infos_read = 0;
2538 bool already_read_relative_selector_offset = false;
2539
2540 do {
2541 // Run the function.
2542 ExpressionResults results =
2543 get_shared_cache_class_info_function->ExecuteFunction(
2544 exe_ctx, &m_args, options, diagnostics, return_value);
2545
2546 if (results == eExpressionCompleted) {
2547 // The result is the number of ClassInfo structures that were filled in.
2548 num_class_infos_read = return_value.GetScalar().ULong();
2549 num_class_infos += num_class_infos_read;
2550 LLDB_LOG(log, "Discovered {0} Objective-C classes in the shared cache",
2551 num_class_infos_read);
2552 if (num_class_infos_read > 0) {
2553 success = true;
2554
2555 // Read the relative selector offset. This only needs to occur once no
2556 // matter how many times the function is called.
2557 if (!already_read_relative_selector_offset) {
2558 DataBufferHeap relative_selector_offset_buffer(64, 0);
2559 if (process->ReadMemory(
2560 relative_selector_offset_addr,
2561 relative_selector_offset_buffer.GetBytes(),
2562 relative_selector_offset_buffer.GetByteSize(),
2563 err) == relative_selector_offset_buffer.GetByteSize()) {
2564 DataExtractor relative_selector_offset_data(
2565 relative_selector_offset_buffer.GetBytes(),
2566 relative_selector_offset_buffer.GetByteSize(),
2567 process->GetByteOrder(), addr_size);
2568 lldb::offset_t offset = 0;
2569 uint64_t relative_selector_offset =
2570 relative_selector_offset_data.GetU64(&offset);
2571 if (relative_selector_offset > 0) {
2572 // The offset is relative to the objc_opt struct.
2573 m_runtime.SetRelativeSelectorBaseAddr(objc_opt_ptr +
2574 relative_selector_offset);
2575 }
2576 }
2577 already_read_relative_selector_offset = true;
2578 }
2579
2580 // Read the ClassInfo structures
2581 DataBufferHeap class_infos_buffer(
2582 num_class_infos_read * class_info_byte_size, 0);
2583 if (process->ReadMemory(class_infos_addr,
2584 class_infos_buffer.GetBytes(),
2585 class_infos_buffer.GetByteSize(),
2586 err) == class_infos_buffer.GetByteSize()) {
2587 DataExtractor class_infos_data(class_infos_buffer.GetBytes(),
2588 class_infos_buffer.GetByteSize(),
2589 process->GetByteOrder(), addr_size);
2590
2591 m_runtime.ParseClassInfoArray(class_infos_data,
2592 num_class_infos_read);
2593 }
2594 }
2595 } else if (log) {
2596 LLDB_LOGF(log, "Error evaluating our find class name function.");
2597 diagnostics.Dump(log);
2598 break;
2599 }
2600 } while (num_class_infos_read == max_num_classes_in_buffer);
2601 } else if (log) {
2602 LLDB_LOGF(log, "Error writing function arguments.");
2603 diagnostics.Dump(log);
2604 }
2605
2606 LLDB_LOG(log, "Processed {0} Objective-C classes total from the shared cache",
2607 num_class_infos);
2608 // Cleanup memory we allocated in the process.
2609 process->DeallocateMemory(relative_selector_offset_addr);
2610 process->DeallocateMemory(class_info_start_idx_addr);
2611 process->DeallocateMemory(class_infos_addr);
2612
2613 return DescriptorMapUpdateResult(success, false, num_class_infos);
2614}
2617 Process *process = GetProcess();
2618
2619 if (process) {
2620 ModuleSP objc_module_sp(GetObjCModule());
2621
2622 if (objc_module_sp) {
2623 ObjectFile *objc_object = objc_module_sp->GetObjectFile();
2624
2625 if (objc_object) {
2626 SectionList *section_list = objc_module_sp->GetSectionList();
2627
2628 if (section_list) {
2629 SectionSP text_segment_sp(
2630 section_list->FindSectionByName(ConstString("__TEXT")));
2631
2632 if (text_segment_sp) {
2633 SectionSP objc_opt_section_sp(
2634 text_segment_sp->GetChildren().FindSectionByName(
2635 ConstString("__objc_opt_ro")));
2636
2637 if (objc_opt_section_sp) {
2638 return objc_opt_section_sp->GetLoadBaseAddress(
2639 &process->GetTarget());
2640 }
2641 }
2642 }
2643 }
2644 }
2645 }
2646 return LLDB_INVALID_ADDRESS;
2647}
2648
2650 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
2651 if (!info)
2652 return LLDB_INVALID_ADDRESS;
2653
2654 StructuredData::Dictionary *info_dict = info->GetAsDictionary();
2655 if (!info_dict)
2656 return LLDB_INVALID_ADDRESS;
2657
2659 info_dict->GetValueForKey("shared_cache_base_address");
2660 if (!value)
2661 return LLDB_INVALID_ADDRESS;
2662
2663 return value->GetUnsignedIntegerValue(LLDB_INVALID_ADDRESS);
2664}
2665
2668
2670
2671 // Else we need to check with our process to see when the map was updated.
2672 Process *process = GetProcess();
2673
2674 if (process) {
2675 RemoteNXMapTable hash_table;
2676
2677 // Update the process stop ID that indicates the last time we updated the
2678 // map, whether it was successful or not.
2680
2681 // Ask the runtime is the realized class generation count changed. Unlike
2682 // the hash table, this accounts for lazily named classes.
2683 const bool class_count_changed = RealizedClassGenerationCountChanged();
2684
2685 if (!m_hash_signature.NeedsUpdate(process, this, hash_table) &&
2686 !class_count_changed)
2687 return;
2688
2689 m_hash_signature.UpdateSignature(hash_table);
2690
2691 // Grab the dynamically loaded Objective-C classes from memory.
2692 DescriptorMapUpdateResult dynamic_update_result =
2693 m_dynamic_class_info_extractor.UpdateISAToDescriptorMap(hash_table);
2694
2695 // Now get the objc classes that are baked into the Objective-C runtime in
2696 // the shared cache, but only once per process as this data never changes
2697 if (!m_loaded_objc_opt) {
2698 // it is legitimately possible for the shared cache to be empty - in that
2699 // case, the dynamic hash table will contain all the class information we
2700 // need; the situation we're trying to detect is one where we aren't
2701 // seeing class information from the runtime - in order to detect that
2702 // vs. just the shared cache being empty or sparsely populated, we set an
2703 // arbitrary (very low) threshold for the number of classes that we want
2704 // to see in a "good" scenario - anything below that is suspicious
2705 // (Foundation alone has thousands of classes)
2706 const uint32_t num_classes_to_warn_at = 500;
2707
2708 DescriptorMapUpdateResult shared_cache_update_result =
2709 m_shared_cache_class_info_extractor.UpdateISAToDescriptorMap();
2711 LLDB_LOGF(log,
2712 "attempted to read objc class data - results: "
2713 "[dynamic_update]: ran: %s, retry: %s, count: %" PRIu32
2714 " [shared_cache_update]: ran: %s, retry: %s, count: %" PRIu32,
2715 dynamic_update_result.m_update_ran ? "yes" : "no",
2716 dynamic_update_result.m_retry_update ? "yes" : "no",
2717 dynamic_update_result.m_num_found,
2718 shared_cache_update_result.m_update_ran ? "yes" : "no",
2719 shared_cache_update_result.m_retry_update ? "yes" : "no",
2720 shared_cache_update_result.m_num_found);
2721
2722 // warn if:
2723 // - we could not run either expression
2724 // - we found fewer than num_classes_to_warn_at classes total
2725 if (dynamic_update_result.m_retry_update ||
2726 shared_cache_update_result.m_retry_update)
2728 else if ((!shared_cache_update_result.m_update_ran) ||
2729 (!dynamic_update_result.m_update_ran))
2732 else if (dynamic_update_result.m_num_found +
2733 shared_cache_update_result.m_num_found <
2734 num_classes_to_warn_at)
2736 else
2737 m_loaded_objc_opt = true;
2738 }
2739 } else {
2741 }
2742}
2743
2745 Process *process = GetProcess();
2746 if (!process)
2747 return false;
2748
2749 Status error;
2750 uint64_t objc_debug_realized_class_generation_count =
2752 process, ConstString("objc_debug_realized_class_generation_count"),
2753 GetObjCModule(), error);
2754 if (error.Fail())
2755 return false;
2756
2758 objc_debug_realized_class_generation_count)
2759 return false;
2760
2762 LLDB_LOG(log,
2763 "objc_debug_realized_class_generation_count changed from {0} to {1}",
2765 objc_debug_realized_class_generation_count);
2766
2768 objc_debug_realized_class_generation_count;
2769
2770 return true;
2771}
2772
2773static bool DoesProcessHaveSharedCache(Process &process) {
2774 PlatformSP platform_sp = process.GetTarget().GetPlatform();
2775 if (!platform_sp)
2776 return true; // this should not happen
2777
2778 llvm::StringRef platform_plugin_name_sr = platform_sp->GetPluginName();
2779 if (platform_plugin_name_sr.ends_with("-simulator"))
2780 return false;
2781
2782 return true;
2784
2786 SharedCacheWarningReason reason) {
2788 // Simulators do not have the objc_opt_ro class table so don't actually
2789 // complain to the user
2790 return;
2791 }
2792
2793 Debugger &debugger(GetProcess()->GetTarget().GetDebugger());
2794 switch (reason) {
2796 Debugger::ReportWarning("could not find Objective-C class data in "
2797 "the process. This may reduce the quality of type "
2798 "information available\n",
2799 debugger.GetID(), &m_no_classes_cached_warning);
2800 break;
2803 "could not execute support code to read "
2804 "Objective-C class data in the process. This may "
2805 "reduce the quality of type information available\n",
2806 debugger.GetID(), &m_no_classes_cached_warning);
2807 break;
2810 "could not execute support code to read Objective-C class data because "
2811 "it's not yet safe to do so, and will be retried later\n",
2812 debugger.GetID(), nullptr);
2813 break;
2814 }
2815}
2816
2818 if (!m_objc_module_sp)
2819 return;
2821 ObjectFile *object_file = m_objc_module_sp->GetObjectFile();
2822 if (!object_file)
2823 return;
2824
2825 if (!object_file->IsInMemory())
2826 return;
2828 if (!GetProcess()->IsLiveDebugSession())
2829 return;
2830
2831 Target &target = GetProcess()->GetTarget();
2832 Debugger &debugger = target.GetDebugger();
2833
2834 std::string buffer;
2835 llvm::raw_string_ostream os(buffer);
2836
2837 os << "libobjc.A.dylib is being read from process memory. This "
2838 "indicates that LLDB could not ";
2839 if (PlatformSP platform_sp = target.GetPlatform()) {
2840 if (platform_sp->IsHost()) {
2841 os << "read from the host's in-memory shared cache";
2842 } else {
2843 os << "find the on-disk shared cache for this device";
2844 }
2845 } else {
2846 os << "read from the shared cache";
2847 }
2848 os << ". This will likely reduce debugging performance\n";
2849
2850 Debugger::ReportWarning(buffer, debugger.GetID(),
2852}
2853
2855 if (!m_decl_vendor_up)
2856 m_decl_vendor_up = std::make_unique<AppleObjCDeclVendor>(*this);
2857
2858 return m_decl_vendor_up.get();
2859}
2860
2863
2864 const char *name_cstr = name.AsCString(nullptr);
2865
2866 if (name_cstr) {
2867 llvm::StringRef name_strref(name_cstr);
2868
2869 llvm::StringRef ivar_prefix("OBJC_IVAR_$_");
2870 llvm::StringRef class_prefix("OBJC_CLASS_$_");
2871
2872 if (name_strref.starts_with(ivar_prefix)) {
2873 llvm::StringRef ivar_skipped_prefix =
2874 name_strref.substr(ivar_prefix.size());
2875 std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar =
2876 ivar_skipped_prefix.split('.');
2877
2878 if (!class_and_ivar.first.empty() && !class_and_ivar.second.empty()) {
2879 const ConstString class_name_cs(class_and_ivar.first);
2880 ClassDescriptorSP descriptor =
2882
2883 if (descriptor) {
2884 const ConstString ivar_name_cs(class_and_ivar.second);
2885 const char *ivar_name_cstr = ivar_name_cs.AsCString(nullptr);
2886
2887 auto ivar_func = [&ret,
2888 ivar_name_cstr](const char *name, const char *type,
2889 lldb::addr_t offset_addr,
2890 uint64_t size) -> lldb::addr_t {
2891 if (!strcmp(name, ivar_name_cstr)) {
2892 ret = offset_addr;
2893 return true;
2894 }
2895 return false;
2896 };
2897
2898 descriptor->Describe(
2899 std::function<void(ObjCISA)>(nullptr),
2900 std::function<bool(const char *, const char *)>(nullptr),
2901 std::function<bool(const char *, const char *)>(nullptr),
2902 ivar_func);
2903 }
2904 }
2905 } else if (name_strref.starts_with(class_prefix)) {
2906 llvm::StringRef class_skipped_prefix =
2907 name_strref.substr(class_prefix.size());
2908 const ConstString class_name_cs(class_skipped_prefix);
2909 ClassDescriptorSP descriptor =
2910 GetClassDescriptorFromClassName(class_name_cs);
2911
2912 if (descriptor)
2913 ret = descriptor->GetISA();
2914 }
2915 }
2916
2917 return ret;
2918}
2919
2920AppleObjCRuntimeV2::NonPointerISACache *
2922 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2923 Process *process(runtime.GetProcess());
2924
2925 Log *log = GetLog(LLDBLog::Types);
2926
2927 // Batch read all the ISA-related symbols in one go.
2928 enum ISASymbol {
2929 kMagicMask,
2930 kMagicValue,
2931 kClassMask,
2932 kIndexedMagicMask,
2933 kIndexedMagicValue,
2934 kIndexedIndexMask,
2935 kIndexedIndexShift,
2936 kIndexedClasses,
2937 kISASymbolCount,
2938 };
2939 llvm::SmallVector<RuntimeGlobalSymbolSpec> specs = {
2940 {ConstString("objc_debug_isa_magic_mask")},
2941 {ConstString("objc_debug_isa_magic_value")},
2942 {ConstString("objc_debug_isa_class_mask")},
2943 {ConstString("objc_debug_indexed_isa_magic_mask")},
2944 {ConstString("objc_debug_indexed_isa_magic_value")},
2945 {ConstString("objc_debug_indexed_isa_index_mask")},
2946 {ConstString("objc_debug_indexed_isa_index_shift")},
2947 {ConstString("objc_indexed_classes"), /*read_value=*/false},
2948 };
2949 assert(specs.size() == kISASymbolCount);
2950
2951 auto results =
2952 ExtractRuntimeGlobalSymbolsBatched(process, objc_module_sp, specs);
2953
2954 // Check required symbols.
2955 if (!results[kMagicMask].success || !results[kMagicValue].success ||
2956 !results[kClassMask].success)
2957 return nullptr;
2958
2959 auto objc_debug_isa_magic_mask = results[kMagicMask].value;
2960 auto objc_debug_isa_magic_value = results[kMagicValue].value;
2961 auto objc_debug_isa_class_mask = results[kClassMask].value;
2962
2963 if (log)
2964 log->PutCString("AOCRT::NPI: Found all the non-indexed ISA masks");
2965
2966 // Check optional indexed ISA symbols.
2967 bool foundError = !results[kIndexedMagicMask].success ||
2968 !results[kIndexedMagicValue].success ||
2969 !results[kIndexedIndexMask].success ||
2970 !results[kIndexedIndexShift].success ||
2971 !results[kIndexedClasses].success;
2972
2973 auto objc_debug_indexed_isa_magic_mask = results[kIndexedMagicMask].value;
2974 auto objc_debug_indexed_isa_magic_value = results[kIndexedMagicValue].value;
2975 auto objc_debug_indexed_isa_index_mask = results[kIndexedIndexMask].value;
2976 auto objc_debug_indexed_isa_index_shift = results[kIndexedIndexShift].value;
2977 auto objc_indexed_classes = results[kIndexedClasses].value;
2978
2979 if (log && !foundError)
2980 log->PutCString("AOCRT::NPI: Found all the indexed ISA masks");
2981
2982 // we might want to have some rules to outlaw these other values (e.g if the
2983 // mask is zero but the value is non-zero, ...)
2984
2985 return new NonPointerISACache(
2986 runtime, objc_module_sp, objc_debug_isa_class_mask,
2987 objc_debug_isa_magic_mask, objc_debug_isa_magic_value,
2988 objc_debug_indexed_isa_magic_mask, objc_debug_indexed_isa_magic_value,
2989 objc_debug_indexed_isa_index_mask, objc_debug_indexed_isa_index_shift,
2990 foundError ? 0 : objc_indexed_classes);
2991}
2992
2995 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2996 Process *process(runtime.GetProcess());
2997
2998 // Batch read all tagged pointer symbols in one go.
2999 enum TaggedPtrSymbol {
3000 kMask,
3001 kSlotShift,
3002 kSlotMask,
3003 kPayloadLshift,
3004 kPayloadRshift,
3005 kClasses,
3006 kExtMask,
3007 kExtSlotShift,
3008 kExtSlotMask,
3009 kExtClasses,
3010 kExtPayloadLshift,
3011 kExtPayloadRshift,
3012 kTaggedPtrSymbolCount,
3013 };
3014 llvm::SmallVector<RuntimeGlobalSymbolSpec> specs = {
3015 {ConstString("objc_debug_taggedpointer_mask")},
3016 {ConstString("objc_debug_taggedpointer_slot_shift"), true, 4},
3017 {ConstString("objc_debug_taggedpointer_slot_mask"), true, 4},
3018 {ConstString("objc_debug_taggedpointer_payload_lshift"), true, 4},
3019 {ConstString("objc_debug_taggedpointer_payload_rshift"), true, 4},
3020 {ConstString("objc_debug_taggedpointer_classes"), false},
3021 {ConstString("objc_debug_taggedpointer_ext_mask")},
3022 {ConstString("objc_debug_taggedpointer_ext_slot_shift"), true, 4},
3023 {ConstString("objc_debug_taggedpointer_ext_slot_mask"), true, 4},
3024 {ConstString("objc_debug_taggedpointer_ext_classes"), false},
3025 {ConstString("objc_debug_taggedpointer_ext_payload_lshift"), true, 4},
3026 {ConstString("objc_debug_taggedpointer_ext_payload_rshift"), true, 4},
3027 };
3028 assert(specs.size() == kTaggedPtrSymbolCount);
3029
3030 auto results =
3031 ExtractRuntimeGlobalSymbolsBatched(process, objc_module_sp, specs);
3032
3033 // Check required symbols.
3034 bool required_success =
3035 results[kMask].success && results[kSlotShift].success &&
3036 results[kSlotMask].success && results[kPayloadLshift].success &&
3037 results[kPayloadRshift].success && results[kClasses].success;
3038
3039 if (!required_success)
3040 return new TaggedPointerVendorLegacy(runtime);
3041
3042 auto objc_debug_taggedpointer_mask = results[kMask].value;
3043 auto objc_debug_taggedpointer_slot_shift = results[kSlotShift].value;
3044 auto objc_debug_taggedpointer_slot_mask = results[kSlotMask].value;
3045 auto objc_debug_taggedpointer_payload_lshift = results[kPayloadLshift].value;
3046 auto objc_debug_taggedpointer_payload_rshift = results[kPayloadRshift].value;
3047 auto objc_debug_taggedpointer_classes = results[kClasses].value;
3048
3049 // Check if extended symbols are all present.
3050 bool extended_success =
3051 results[kExtMask].success && results[kExtSlotShift].success &&
3052 results[kExtSlotMask].success && results[kExtClasses].success &&
3053 results[kExtPayloadLshift].success && results[kExtPayloadRshift].success;
3054
3055 if (extended_success) {
3056 auto objc_debug_taggedpointer_ext_mask = results[kExtMask].value;
3057 auto objc_debug_taggedpointer_ext_slot_shift = results[kExtSlotShift].value;
3058 auto objc_debug_taggedpointer_ext_slot_mask = results[kExtSlotMask].value;
3059 auto objc_debug_taggedpointer_ext_classes = results[kExtClasses].value;
3060 auto objc_debug_taggedpointer_ext_payload_lshift =
3061 results[kExtPayloadLshift].value;
3062 auto objc_debug_taggedpointer_ext_payload_rshift =
3063 results[kExtPayloadRshift].value;
3064
3065 return new TaggedPointerVendorExtended(
3066 runtime, objc_debug_taggedpointer_mask,
3067 objc_debug_taggedpointer_ext_mask, objc_debug_taggedpointer_slot_shift,
3068 objc_debug_taggedpointer_ext_slot_shift,
3069 objc_debug_taggedpointer_slot_mask,
3070 objc_debug_taggedpointer_ext_slot_mask,
3071 objc_debug_taggedpointer_payload_lshift,
3072 objc_debug_taggedpointer_payload_rshift,
3073 objc_debug_taggedpointer_ext_payload_lshift,
3074 objc_debug_taggedpointer_ext_payload_rshift,
3075 objc_debug_taggedpointer_classes, objc_debug_taggedpointer_ext_classes);
3076 }
3077
3078 // we might want to have some rules to outlaw these values (e.g if the
3079 // table's address is zero)
3080
3082 runtime, objc_debug_taggedpointer_mask,
3083 objc_debug_taggedpointer_slot_shift, objc_debug_taggedpointer_slot_mask,
3084 objc_debug_taggedpointer_payload_lshift,
3085 objc_debug_taggedpointer_payload_rshift,
3086 objc_debug_taggedpointer_classes);
3087}
3088
3090 lldb::addr_t ptr) {
3091 return (ptr & 1);
3092}
3093
3096 lldb::addr_t ptr) {
3097 if (!IsPossibleTaggedPointer(ptr))
3099
3100 uint32_t foundation_version = m_runtime.GetFoundationVersion();
3101
3102 if (foundation_version == LLDB_INVALID_MODULE_VERSION)
3104
3105 uint64_t class_bits = (ptr & 0xE) >> 1;
3106 ConstString name;
3107
3108 static ConstString g_NSAtom("NSAtom");
3109 static ConstString g_NSNumber("NSNumber");
3110 static ConstString g_NSDateTS("NSDateTS");
3111 static ConstString g_NSManagedObject("NSManagedObject");
3112 static ConstString g_NSDate("NSDate");
3113
3114 if (foundation_version >= 900) {
3115 switch (class_bits) {
3116 case 0:
3117 name = g_NSAtom;
3118 break;
3119 case 3:
3120 name = g_NSNumber;
3121 break;
3122 case 4:
3123 name = g_NSDateTS;
3124 break;
3125 case 5:
3126 name = g_NSManagedObject;
3127 break;
3128 case 6:
3129 name = g_NSDate;
3130 break;
3131 default:
3133 }
3134 } else {
3135 switch (class_bits) {
3136 case 1:
3137 name = g_NSNumber;
3138 break;
3139 case 5:
3140 name = g_NSManagedObject;
3141 break;
3142 case 6:
3143 name = g_NSDate;
3144 break;
3145 case 7:
3146 name = g_NSDateTS;
3147 break;
3148 default:
3150 }
3151 }
3152
3153 lldb::addr_t unobfuscated = ptr ^ m_runtime.GetTaggedPointerObfuscator();
3154 return ClassDescriptorSP(new ClassDescriptorV2Tagged(name, unobfuscated));
3155}
3156
3159 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
3160 uint32_t objc_debug_taggedpointer_slot_shift,
3161 uint32_t objc_debug_taggedpointer_slot_mask,
3162 uint32_t objc_debug_taggedpointer_payload_lshift,
3163 uint32_t objc_debug_taggedpointer_payload_rshift,
3164 lldb::addr_t objc_debug_taggedpointer_classes)
3165 : TaggedPointerVendorV2(runtime), m_cache(),
3166 m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask),
3167 m_objc_debug_taggedpointer_slot_shift(
3168 objc_debug_taggedpointer_slot_shift),
3169 m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask),
3170 m_objc_debug_taggedpointer_payload_lshift(
3171 objc_debug_taggedpointer_payload_lshift),
3172 m_objc_debug_taggedpointer_payload_rshift(
3173 objc_debug_taggedpointer_payload_rshift),
3174 m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes) {}
3175
3178 return (ptr & m_objc_debug_taggedpointer_mask) != 0;
3179}
3180
3183 lldb::addr_t ptr) {
3184 ClassDescriptorSP actual_class_descriptor_sp;
3185 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
3186
3187 if (!IsPossibleTaggedPointer(unobfuscated))
3189
3190 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) &
3191 m_objc_debug_taggedpointer_slot_mask;
3192
3193 CacheIterator iterator = m_cache.find(slot), end = m_cache.end();
3194 if (iterator != end) {
3195 actual_class_descriptor_sp = iterator->second;
3196 } else {
3197 Process *process(m_runtime.GetProcess());
3198 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
3199 m_objc_debug_taggedpointer_classes;
3200 Status error;
3201 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
3202 if (error.Fail() || slot_data == 0 ||
3203 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
3204 return nullptr;
3205 actual_class_descriptor_sp =
3206 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
3207 if (!actual_class_descriptor_sp) {
3208 if (ABISP abi_sp = process->GetABI()) {
3209 ObjCISA fixed_isa = abi_sp->FixCodeAddress((ObjCISA)slot_data);
3210 actual_class_descriptor_sp =
3211 m_runtime.GetClassDescriptorFromISA(fixed_isa);
3212 }
3213 }
3214 if (!actual_class_descriptor_sp)
3216 m_cache[slot] = actual_class_descriptor_sp;
3217 }
3218
3219 uint64_t data_payload =
3220 ((unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
3221 m_objc_debug_taggedpointer_payload_rshift);
3222 int64_t data_payload_signed =
3223 ((int64_t)(unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
3224 m_objc_debug_taggedpointer_payload_rshift);
3226 actual_class_descriptor_sp, data_payload, data_payload_signed));
3227}
3230 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
3231 uint64_t objc_debug_taggedpointer_ext_mask,
3232 uint32_t objc_debug_taggedpointer_slot_shift,
3233 uint32_t objc_debug_taggedpointer_ext_slot_shift,
3234 uint32_t objc_debug_taggedpointer_slot_mask,
3235 uint32_t objc_debug_taggedpointer_ext_slot_mask,
3236 uint32_t objc_debug_taggedpointer_payload_lshift,
3237 uint32_t objc_debug_taggedpointer_payload_rshift,
3238 uint32_t objc_debug_taggedpointer_ext_payload_lshift,
3239 uint32_t objc_debug_taggedpointer_ext_payload_rshift,
3240 lldb::addr_t objc_debug_taggedpointer_classes,
3241 lldb::addr_t objc_debug_taggedpointer_ext_classes)
3243 runtime, objc_debug_taggedpointer_mask,
3244 objc_debug_taggedpointer_slot_shift,
3245 objc_debug_taggedpointer_slot_mask,
3246 objc_debug_taggedpointer_payload_lshift,
3247 objc_debug_taggedpointer_payload_rshift,
3248 objc_debug_taggedpointer_classes),
3249 m_ext_cache(),
3250 m_objc_debug_taggedpointer_ext_mask(objc_debug_taggedpointer_ext_mask),
3252 objc_debug_taggedpointer_ext_slot_shift),
3254 objc_debug_taggedpointer_ext_slot_mask),
3256 objc_debug_taggedpointer_ext_payload_lshift),
3258 objc_debug_taggedpointer_ext_payload_rshift),
3260 objc_debug_taggedpointer_ext_classes) {}
3261
3264 if (!IsPossibleTaggedPointer(ptr))
3265 return false;
3266
3267 if (m_objc_debug_taggedpointer_ext_mask == 0)
3268 return false;
3269
3270 return ((ptr & m_objc_debug_taggedpointer_ext_mask) ==
3271 m_objc_debug_taggedpointer_ext_mask);
3272}
3273
3276 lldb::addr_t ptr) {
3277 ClassDescriptorSP actual_class_descriptor_sp;
3278 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
3279
3280 if (!IsPossibleTaggedPointer(unobfuscated))
3282
3283 if (!IsPossibleExtendedTaggedPointer(unobfuscated))
3285
3286 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_ext_slot_shift) &
3287 m_objc_debug_taggedpointer_ext_slot_mask;
3288
3289 CacheIterator iterator = m_ext_cache.find(slot), end = m_ext_cache.end();
3290 if (iterator != end) {
3291 actual_class_descriptor_sp = iterator->second;
3292 } else {
3293 Process *process(m_runtime.GetProcess());
3294 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
3295 m_objc_debug_taggedpointer_ext_classes;
3296 Status error;
3297 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
3298 if (error.Fail() || slot_data == 0 ||
3299 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
3300 return nullptr;
3301 actual_class_descriptor_sp =
3302 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
3303 if (!actual_class_descriptor_sp)
3305 m_ext_cache[slot] = actual_class_descriptor_sp;
3307
3308 uint64_t data_payload = (((uint64_t)unobfuscated
3309 << m_objc_debug_taggedpointer_ext_payload_lshift) >>
3310 m_objc_debug_taggedpointer_ext_payload_rshift);
3311 int64_t data_payload_signed =
3312 ((int64_t)((uint64_t)unobfuscated
3313 << m_objc_debug_taggedpointer_ext_payload_lshift) >>
3314 m_objc_debug_taggedpointer_ext_payload_rshift);
3315
3317 actual_class_descriptor_sp, data_payload, data_payload_signed));
3318}
3319
3321 AppleObjCRuntimeV2 &runtime, const ModuleSP &objc_module_sp,
3322 uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask,
3323 uint64_t objc_debug_isa_magic_value,
3324 uint64_t objc_debug_indexed_isa_magic_mask,
3325 uint64_t objc_debug_indexed_isa_magic_value,
3326 uint64_t objc_debug_indexed_isa_index_mask,
3327 uint64_t objc_debug_indexed_isa_index_shift,
3328 lldb::addr_t objc_indexed_classes)
3329 : m_runtime(runtime), m_cache(), m_objc_module_wp(objc_module_sp),
3330 m_objc_debug_isa_class_mask(objc_debug_isa_class_mask),
3331 m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask),
3332 m_objc_debug_isa_magic_value(objc_debug_isa_magic_value),
3333 m_objc_debug_indexed_isa_magic_mask(objc_debug_indexed_isa_magic_mask),
3334 m_objc_debug_indexed_isa_magic_value(objc_debug_indexed_isa_magic_value),
3335 m_objc_debug_indexed_isa_index_mask(objc_debug_indexed_isa_index_mask),
3336 m_objc_debug_indexed_isa_index_shift(objc_debug_indexed_isa_index_shift),
3337 m_objc_indexed_classes(objc_indexed_classes), m_indexed_isa_cache() {}
3338
3341 ObjCISA real_isa = 0;
3342 if (!EvaluateNonPointerISA(isa, real_isa))
3344 auto cache_iter = m_cache.find(real_isa);
3345 if (cache_iter != m_cache.end())
3346 return cache_iter->second;
3347 auto descriptor_sp =
3348 m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(real_isa);
3349 if (descriptor_sp) // cache only positive matches since the table might grow
3350 m_cache[real_isa] = descriptor_sp;
3351 return descriptor_sp;
3352}
3353
3355 ObjCISA isa, ObjCISA &ret_isa) {
3356 Log *log = GetLog(LLDBLog::Types);
3357
3358 LLDB_LOGF(log, "AOCRT::NPI Evaluate(isa = 0x%" PRIx64 ")", (uint64_t)isa);
3359
3360 if ((isa & ~m_objc_debug_isa_class_mask) == 0)
3361 return false;
3362
3363 // If all of the indexed ISA variables are set, then its possible that this
3364 // ISA is indexed, and we should first try to get its value using the index.
3365 // Note, we check these variables first as the ObjC runtime will set at least
3366 // one of their values to 0 if they aren't needed.
3367 if (m_objc_debug_indexed_isa_magic_mask &&
3368 m_objc_debug_indexed_isa_magic_value &&
3369 m_objc_debug_indexed_isa_index_mask &&
3370 m_objc_debug_indexed_isa_index_shift && m_objc_indexed_classes) {
3371 if ((isa & ~m_objc_debug_indexed_isa_index_mask) == 0)
3372 return false;
3373
3374 if ((isa & m_objc_debug_indexed_isa_magic_mask) ==
3375 m_objc_debug_indexed_isa_magic_value) {
3376 // Magic bits are correct, so try extract the index.
3377 uintptr_t index = (isa & m_objc_debug_indexed_isa_index_mask) >>
3378 m_objc_debug_indexed_isa_index_shift;
3379 // If the index is out of bounds of the length of the array then check if
3380 // the array has been updated. If that is the case then we should try
3381 // read the count again, and update the cache if the count has been
3382 // updated.
3383 if (index > m_indexed_isa_cache.size()) {
3384 LLDB_LOGF(log,
3385 "AOCRT::NPI (index = %" PRIu64
3386 ") exceeds cache (size = %" PRIu64 ")",
3387 (uint64_t)index, (uint64_t)m_indexed_isa_cache.size());
3388
3389 Process *process(m_runtime.GetProcess());
3390
3391 ModuleSP objc_module_sp(m_objc_module_wp.lock());
3392 if (!objc_module_sp)
3393 return false;
3394
3395 Status error;
3396 auto objc_indexed_classes_count = ExtractRuntimeGlobalSymbol(
3397 process, ConstString("objc_indexed_classes_count"), objc_module_sp,
3398 error);
3399 if (error.Fail())
3400 return false;
3401
3402 LLDB_LOGF(log, "AOCRT::NPI (new class count = %" PRIu64 ")",
3403 (uint64_t)objc_indexed_classes_count);
3404
3405 if (objc_indexed_classes_count > m_indexed_isa_cache.size()) {
3406 // Read the class entries we don't have. We should just read all of
3407 // them instead of just the one we need as then we can cache those we
3408 // may need later.
3409 auto num_new_classes =
3410 objc_indexed_classes_count - m_indexed_isa_cache.size();
3411 const uint32_t addr_size = process->GetAddressByteSize();
3412 DataBufferHeap buffer(num_new_classes * addr_size, 0);
3413
3414 lldb::addr_t last_read_class =
3415 m_objc_indexed_classes + (m_indexed_isa_cache.size() * addr_size);
3416 size_t bytes_read = process->ReadMemory(
3417 last_read_class, buffer.GetBytes(), buffer.GetByteSize(), error);
3418 if (error.Fail() || bytes_read != buffer.GetByteSize())
3419 return false;
3420
3421 LLDB_LOGF(log, "AOCRT::NPI (read new classes count = %" PRIu64 ")",
3422 (uint64_t)num_new_classes);
3423
3424 // Append the new entries to the existing cache.
3425 DataExtractor data(buffer.GetBytes(), buffer.GetByteSize(),
3426 process->GetByteOrder(),
3427 process->GetAddressByteSize());
3428
3429 lldb::offset_t offset = 0;
3430 for (unsigned i = 0; i != num_new_classes; ++i)
3431 m_indexed_isa_cache.push_back(data.GetAddress(&offset));
3433 }
3434
3435 // If the index is still out of range then this isn't a pointer.
3436 if (index >= m_indexed_isa_cache.size())
3437 return false;
3438
3439 LLDB_LOGF(log, "AOCRT::NPI Evaluate(ret_isa = 0x%" PRIx64 ")",
3440 (uint64_t)m_indexed_isa_cache[index]);
3442 ret_isa = m_indexed_isa_cache[index];
3443 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
3444 }
3445
3446 return false;
3447 }
3448
3449 // Definitely not an indexed ISA, so try to use a mask to extract the pointer
3450 // from the ISA.
3451 if ((isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value) {
3452 ret_isa = isa & m_objc_debug_isa_class_mask;
3453 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
3454 }
3455 return false;
3456}
3457
3461 std::make_shared<AppleObjCTypeEncodingParser>(*this);
3462 return m_encoding_to_type_sp;
3463}
3464
3467 ObjCISA ret = isa;
3468
3469 if (auto *non_pointer_isa_cache = GetNonPointerIsaCache())
3470 non_pointer_isa_cache->EvaluateNonPointerISA(isa, ret);
3471
3472 return ret;
3473}
3474
3477 return true;
3478
3479 static ConstString g_dunder_kCFBooleanFalse("__kCFBooleanFalse");
3480 static ConstString g_dunder_kCFBooleanTrue("__kCFBooleanTrue");
3481 static ConstString g_kCFBooleanFalse("kCFBooleanFalse");
3482 static ConstString g_kCFBooleanTrue("kCFBooleanTrue");
3483
3484 std::function<lldb::addr_t(ConstString, ConstString)> get_symbol =
3485 [this](ConstString sym, ConstString real_sym) -> lldb::addr_t {
3486 SymbolContextList sc_list;
3488 sym, lldb::eSymbolTypeData, sc_list);
3489 if (sc_list.GetSize() == 1) {
3490 SymbolContext sc;
3491 sc_list.GetContextAtIndex(0, sc);
3492 if (sc.symbol)
3493 return sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
3496 real_sym, lldb::eSymbolTypeData, sc_list);
3497 if (sc_list.GetSize() != 1)
3498 return LLDB_INVALID_ADDRESS;
3499
3501 sc_list.GetContextAtIndex(0, sc);
3502 if (!sc.symbol)
3503 return LLDB_INVALID_ADDRESS;
3504
3505 lldb::addr_t addr = sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
3506 Status error;
3507 addr = GetProcess()->ReadPointerFromMemory(addr, error);
3508 if (error.Fail())
3509 return LLDB_INVALID_ADDRESS;
3510 return addr;
3512
3513 lldb::addr_t false_addr = get_symbol(g_dunder_kCFBooleanFalse, g_kCFBooleanFalse);
3514 lldb::addr_t true_addr = get_symbol(g_dunder_kCFBooleanTrue, g_kCFBooleanTrue);
3515
3516 return (m_CFBoolean_values = {false_addr, true_addr}).operator bool();
3517}
3518
3520 lldb::addr_t &cf_false) {
3522 cf_true = m_CFBoolean_values->second;
3523 cf_false = m_CFBoolean_values->first;
3524 } else
3525 this->AppleObjCRuntime::GetValuesForGlobalCFBooleans(cf_true, cf_false);
3526}
3527
3528void AppleObjCRuntimeV2::ModulesDidLoad(const ModuleList &module_list) {
3531 m_shared_cache_image_headers_up->SetNeedsUpdate();
3533
3538 }
3540 return m_shared_cache_image_headers_up->IsImageLoaded(image_index);
3541
3542 return false;
3543}
3544
3549 }
3551 return m_shared_cache_image_headers_up->GetVersion();
3552
3553 return std::nullopt;
3554}
3555
3558 auto dict_up = std::make_unique<StructuredData::Dictionary>();
3559 dict_up->AddItem("Objective-C runtime version",
3560 std::make_unique<StructuredData::UnsignedInteger>(2));
3561 return dict_up;
3562}
3563
3564#pragma mark Frame recognizers
3565
3566class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame {
3567public:
3568 ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp) {
3569 ThreadSP thread_sp = frame_sp->GetThread();
3570 ProcessSP process_sp = thread_sp->GetProcess();
3571
3572 const lldb::ABISP &abi = process_sp->GetABI();
3573 if (!abi)
3574 return;
3576 TypeSystemClangSP scratch_ts_sp =
3577 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
3578 if (!scratch_ts_sp)
3579 return;
3580 CompilerType voidstar =
3581 scratch_ts_sp->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
3582
3583 ValueList args;
3584 Value input_value;
3585 input_value.SetCompilerType(voidstar);
3586 args.PushValue(input_value);
3587
3588 if (!abi->GetArgumentValues(*thread_sp, args))
3589 return;
3590
3591 addr_t exception_addr = args.GetValueAtIndex(0)->GetScalar().ULongLong();
3592
3593 Value value(exception_addr);
3594 value.SetCompilerType(voidstar);
3595 exception = ValueObjectConstResult::Create(frame_sp.get(), value,
3596 ConstString("exception"));
3598 *exception, eValueTypeVariableArgument);
3599 exception = exception->GetDynamicValue(eDynamicDontRunTarget);
3600
3601 m_arguments = std::make_shared<ValueObjectList>();
3602 m_arguments->Append(exception);
3603
3604 m_stop_desc = "hit Objective-C exception";
3605 }
3606
3608
3609 lldb::ValueObjectSP GetExceptionObject() override { return exception; }
3610};
3611
3612class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer {
3614 RecognizeFrame(lldb::StackFrameSP frame) override {
3616 new ObjCExceptionRecognizedStackFrame(frame));
3617 };
3618 std::string GetName() override {
3619 return "ObjC Exception Throw StackFrame Recognizer";
3620 }
3621};
3622
3623static void RegisterObjCExceptionRecognizer(Process *process) {
3624 FileSpec module;
3625 ConstString function;
3626 std::tie(module, function) = AppleObjCRuntime::GetExceptionThrowLocation();
3627 std::vector<ConstString> symbols = {function};
3628
3630 StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
3632 /*first_instruction_only*/ true);
3633}
static const char * g_get_dynamic_class_info_name
static void RegisterObjCExceptionRecognizer(Process *process)
static llvm::SmallVector< RuntimeGlobalSymbolResult > ExtractRuntimeGlobalSymbolsBatched(Process *process, const ModuleSP &module_sp, llvm::ArrayRef< RuntimeGlobalSymbolSpec > specs)
Batched version of ExtractRuntimeGlobalSymbol.
static const char * g_get_dynamic_class_info3_body
static const char * g_get_dynamic_class_info2_body
static const char * g_get_shared_cache_class_info_name
static const char * g_get_dynamic_class_info3_name
static uint64_t ExtractRuntimeGlobalSymbol(Process *process, ConstString name, const ModuleSP &module_sp, Status &error, bool read_value=true, uint8_t byte_size=0, uint64_t default_value=LLDB_INVALID_ADDRESS, SymbolType sym_type=lldb::eSymbolTypeData)
static constexpr OptionDefinition g_objc_classtable_dump_options[]
static const char * g_get_shared_cache_class_info_body
static const char * g_shared_cache_class_name_funcptr
static const char * g_get_dynamic_class_info_body
static const char * g_get_dynamic_class_info2_name
static const char * g_get_shared_cache_class_info_definitions
static bool DoesProcessHaveSharedCache(Process &process)
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
#define LLDB_LOGF(log,...)
Definition Log.h:378
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
#define LLDB_SCOPED_TIMER()
Definition Timer.h:83
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition XcodeSDK.cpp:21
CommandObjectMultiwordObjC_ClassTable(CommandInterpreter &interpreter)
~CommandObjectMultiwordObjC_ClassTable() override=default
~CommandObjectMultiwordObjC_TaggedPointer_Info() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter &interpreter)
CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter &interpreter)
~CommandObjectMultiwordObjC_TaggedPointer() override=default
~CommandObjectMultiwordObjC() override=default
CommandObjectMultiwordObjC(CommandInterpreter &interpreter)
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
~CommandObjectObjC_ClassTable_Dump() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectObjC_ClassTable_Dump(CommandInterpreter &interpreter)
const_iterator(RemoteNXMapTable &parent, int index)
lldb_private::Process * m_process
bool ParseHeader(Process *process, lldb::addr_t load_addr)
std::pair< ConstString, ObjCLanguageRuntime::ObjCISA > element
lldb::addr_t GetTableLoadAddress() const
uint32_t GetCount() const
lldb::addr_t GetBucketDataPointer() const
uint32_t GetBucketCount() const
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
void SetRawAddress(lldb::addr_t addr)
Definition Address.h:447
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
UtilityFunction * GetClassInfoUtilityFunction(ExecutionContext &exe_ctx, Helper helper)
Helper ComputeHelper(ExecutionContext &exe_ctx) const
Compute which helper to use.
DescriptorMapUpdateResult UpdateISAToDescriptorMap(RemoteNXMapTable &hash_table)
std::unique_ptr< UtilityFunction > GetClassInfoUtilityFunctionImpl(ExecutionContext &exe_ctx, Helper helper, std::string code, std::string name)
void UpdateSignature(const RemoteNXMapTable &hash_table)
bool NeedsUpdate(Process *process, AppleObjCRuntimeV2 *runtime, RemoteNXMapTable &hash_table)
NonPointerISACache(AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp, uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask, uint64_t objc_debug_isa_magic_value, uint64_t objc_debug_indexed_isa_magic_mask, uint64_t objc_debug_indexed_isa_magic_value, uint64_t objc_debug_indexed_isa_index_mask, uint64_t objc_debug_indexed_isa_index_shift, lldb::addr_t objc_indexed_classes)
bool EvaluateNonPointerISA(ObjCISA isa, ObjCISA &ret_isa)
ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(ObjCISA isa)
static NonPointerISACache * CreateInstance(AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp)
std::map< ObjCISA, ObjCLanguageRuntime::ClassDescriptorSP > m_cache
UtilityFunction * GetClassInfoUtilityFunction(ExecutionContext &exe_ctx)
std::unique_ptr< UtilityFunction > GetClassInfoUtilityFunctionImpl(ExecutionContext &exe_ctx)
static std::unique_ptr< SharedCacheImageHeaders > CreateSharedCacheImageHeaders(AppleObjCRuntimeV2 &runtime)
ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(lldb::addr_t ptr) override
TaggedPointerVendorExtended(AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask, uint64_t objc_debug_taggedpointer_ext_mask, uint32_t objc_debug_taggedpointer_slot_shift, uint32_t objc_debug_taggedpointer_ext_slot_shift, uint32_t objc_debug_taggedpointer_slot_mask, uint32_t objc_debug_taggedpointer_ext_slot_mask, uint32_t objc_debug_taggedpointer_payload_lshift, uint32_t objc_debug_taggedpointer_payload_rshift, uint32_t objc_debug_taggedpointer_ext_payload_lshift, uint32_t objc_debug_taggedpointer_ext_payload_rshift, lldb::addr_t objc_debug_taggedpointer_classes, lldb::addr_t objc_debug_taggedpointer_ext_classes)
ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(lldb::addr_t ptr) override
ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(lldb::addr_t ptr) override
TaggedPointerVendorRuntimeAssisted(AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask, uint32_t objc_debug_taggedpointer_slot_shift, uint32_t objc_debug_taggedpointer_slot_mask, uint32_t objc_debug_taggedpointer_payload_lshift, uint32_t objc_debug_taggedpointer_payload_rshift, lldb::addr_t objc_debug_taggedpointer_classes)
static TaggedPointerVendorV2 * CreateInstance(AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp)
llvm::Expected< std::unique_ptr< UtilityFunction > > CreateObjectChecker(std::string name, ExecutionContext &exe_ctx) override
bool IsTaggedPointer(lldb::addr_t ptr) override
uint32_t ParseClassInfoArray(const lldb_private::DataExtractor &data, uint32_t num_class_infos)
llvm::SmallPtrSet< ValueObject *, 8 > ValueObjectSet
bool RealizedClassGenerationCountChanged()
Update the generation count of realized classes.
static llvm::StringRef GetPluginNameStatic()
EncodingToTypeSP GetEncodingToType() override
AppleObjCRuntimeV2(Process *process, const lldb::ModuleSP &objc_module_sp)
bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name, Address &address, Value::ValueType &value_type, llvm::ArrayRef< uint8_t > &local_buffer) override
This call should return true if it could set the name and/or the type Sets address to the address of ...
NonPointerISACache * GetNonPointerIsaCache()
size_t GetByteOffsetForIvar(CompilerType &parent_ast_type, const char *ivar_name) override
StructuredData::ObjectSP GetLanguageSpecificData(SymbolContext sc) override
Language runtime plugins can use this API to report language-specific runtime information about this ...
SharedCacheClassInfoExtractor m_shared_cache_class_info_extractor
DynamicClassInfoExtractor m_dynamic_class_info_extractor
ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa) override
std::unique_ptr< SharedCacheImageHeaders > m_shared_cache_image_headers_up
lldb::addr_t LookupRuntimeSymbol(ConstString name) override
ClassDescriptorSP GetClassDescriptor(ValueObject &valobj) override
void ModulesDidLoad(const ModuleList &module_list) override
Called when modules have been loaded in the process.
bool IsSharedCacheImageLoaded(uint16_t image_index)
std::optional< std::pair< lldb::addr_t, lldb::addr_t > > m_CFBoolean_values
std::unique_ptr< DeclVendor > m_decl_vendor_up
std::unique_ptr< TaggedPointerVendor > m_tagged_pointer_vendor_up
std::unique_ptr< NonPointerISACache > m_non_pointer_isa_cache_up
void GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true, lldb::addr_t &cf_false) override
lldb::BreakpointResolverSP CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bool catch_bp, bool throw_bp) override
LanguageRuntime * GetPreferredLanguageRuntime(ValueObject &in_value) override
Return the preferred language runtime instance, which in most cases will be the current instance.
std::optional< uint64_t > GetSharedCacheImageHeaderVersion()
ClassDescriptorSP GetClassDescriptorImpl(ValueObject &valobj, ValueObjectSet &seen)
void WarnIfNoClassesCached(SharedCacheWarningReason reason)
static lldb_private::LanguageRuntime * CreateInstance(Process *process, lldb::LanguageType language)
virtual void GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true, lldb::addr_t &cf_false)
static std::tuple< FileSpec, ConstString > GetExceptionThrowLocation()
static ObjCRuntimeVersions GetObjCVersion(Process *process, lldb::ModuleSP &objc_module_sp)
void ModulesDidLoad(const ModuleList &module_list) override
Called when modules have been loaded in the process.
bool CouldHaveDynamicValue(ValueObject &in_value) override
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
CommandObjectMultiword(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormatv(const char *format, Args &&...args)
Generic representation of a type in a programming language.
ConstString GetTypeName(bool BaseOnly=false) const
A uniqued constant string class.
Definition ConstString.h:40
bool IsEmpty() const
Test for empty string.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const
A class to manage flag bits.
Definition Debugger.h:100
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
A plug-in interface definition class for dynamic loaders.
void SetUnwindOnError(bool unwind=false)
Definition Target.h:390
void SetTryAllThreads(bool try_others=true)
Definition Target.h:423
void SetTimeout(const Timeout< std::micro > &timeout)
Definition Target.h:411
void SetStopOthers(bool stop_others=true)
Definition Target.h:427
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:394
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::ThreadSP & GetThreadSP() const
Get accessor to get the thread shared pointer.
Target & GetTargetRef() const
Returns a reference to the target object.
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:250
ValueList GetArgumentValues() const
lldb::ExpressionResults ExecuteFunction(ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager, Value &results)
Run the function this FunctionCaller was created with.
bool WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, DiagnosticManager &diagnostic_manager)
Insert the default function argument struct.
void PutCString(const char *cstr)
Definition Log.cpp:145
bool GetVerbose() const
Definition Log.cpp:300
A collection class for Module objects.
Definition ModuleList.h:125
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr)=0
virtual ObjCLanguageRuntime::ClassDescriptorSP GetClassDescriptor(lldb::addr_t ptr)=0
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp)
std::pair< ISAToDescriptorIterator, ISAToDescriptorIterator > GetDescriptorIteratorPair(bool update_if_needed=true)
virtual TaggedPointerVendor * GetTaggedPointerVendor()
lldb::TypeSP LookupInCompleteClassCache(ConstString &name)
ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value)
virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa)
static ObjCLanguageRuntime * Get(Process &process)
virtual ClassDescriptorSP GetClassDescriptorFromClassName(ConstString class_name)
static lldb::BreakpointPreconditionSP GetBreakpointExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
std::shared_ptr< EncodingToType > EncodingToTypeSP
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
bool IsInMemory() const
Returns true if the object file exists only in memory.
Definition ObjectFile.h:685
A command line option parsing protocol class.
Definition Options.h:58
std::vector< Option > m_getopt_table
Definition Options.h:198
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
std::chrono::seconds GetUtilityExpressionTimeout() const
Definition Process.cpp:325
A plug-in interface definition class for debugging a process.
Definition Process.h:355
ThreadList & GetThreadList()
Definition Process.h:2312
lldb::addr_t AllocateMemory(size_t size, uint32_t permissions, Status &error)
The public interface to allocating memory in the process.
Definition Process.cpp:2561
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:1911
llvm::SmallVector< std::optional< uint64_t > > ReadUnsignedIntegersFromMemory(llvm::ArrayRef< lldb::addr_t > addresses, unsigned byte_size)
Use Process::ReadMemoryRanges to efficiently read multiple unsigned integers from memory at once.
Definition Process.cpp:2328
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3770
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
Definition Process.cpp:2316
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2377
Status DeallocateMemory(lldb::addr_t ptr)
The public interface to deallocating memory in the process.
Definition Process.cpp:2620
uint32_t GetAddressByteSize() const
Definition Process.cpp:3774
uint32_t GetStopID() const
Definition Process.h:1487
size_t WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, Status &error)
Write memory to a process.
Definition Process.cpp:2419
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
Definition Process.cpp:2962
const lldb::ABISP & GetABI()
Definition Process.cpp:1459
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1251
Process * m_process
Definition Runtime.h:29
Process * GetProcess()
Definition Runtime.h:22
Target & GetTargetRef()
Definition Runtime.h:23
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
unsigned long ULong(unsigned long fail_value=0) const
Definition Scalar.cpp:357
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
Definition Section.cpp:556
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference symbol_mangling, bool first_instruction_only=true)
Add a new recognizer that triggers on a given symbol name.
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Success() const
Test for success condition.
Definition Status.cpp:303
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:367
ObjectSP GetValueForKey(llvm::StringRef key) const
std::shared_ptr< Object > ObjectSP
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Symbol * symbol
The Symbol for a given query.
lldb::addr_t GetLoadAddress(Target *target) const
Definition Symbol.cpp:504
bool ValueIsAddress() const
Definition Symbol.cpp:165
Address & GetAddressRef()
Definition Symbol.h:73
DynamicClassInfoHelper GetDynamicClassInfoHelper() const
Definition Target.cpp:4911
Debugger & GetDebugger() const
Definition Target.h:1240
StackFrameRecognizerManager & GetFrameRecognizerManager()
Definition Target.h:1718
llvm::Expected< std::unique_ptr< UtilityFunction > > CreateUtilityFunction(std::string expression, std::string name, lldb::LanguageType language, ExecutionContext &exe_ctx)
Creates and installs a UtilityFunction for the given language.
Definition Target.cpp:2755
lldb::PlatformSP GetPlatform()
Definition Target.h:1694
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1157
lldb::ThreadSP GetExpressionExecutionThread()
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
Definition Type.h:779
void SetName(ConstString type_name)
Definition Type.cpp:902
void SetCompilerType(CompilerType compiler_type)
Definition Type.cpp:922
void SetTypeSP(lldb::TypeSP type_sp)
Definition Type.cpp:914
"lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that provides a function that i...
FunctionCaller * GetFunctionCaller()
void PushValue(const Value &value)
Definition Value.cpp:694
Value * GetValueAtIndex(size_t idx)
Definition Value.cpp:698
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS)
static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type)
lldb::ProcessSP GetProcessSP() const
virtual bool IsBaseClass()
lldb::TargetSP GetTargetSP() const
CompilerType GetCompilerType()
virtual ValueObject * GetParent()
const ExecutionContextRef & GetExecutionContextRef() const
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
ValueType
Type that describes Value::m_value.
Definition Value.h:41
@ Scalar
A raw scalar value.
Definition Value.h:45
void SetCompilerType(const CompilerType &compiler_type)
Definition Value.cpp:276
void SetValueType(ValueType value_type)
Definition Value.h:89
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define UINT64_MAX
#define LLDB_INVALID_MODULE_VERSION
#define LLDB_OPT_SET_ALL
#define UNUSED_IF_ASSERT_DISABLED(x)
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
#define LLDB_INVALID_IVAR_OFFSET
llvm::Error exception(const char *s=nullptr)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
const Scalar operator*(Scalar lhs, Scalar rhs)
Definition Scalar.cpp:574
bool operator!=(const Address &lhs, const Address &rhs)
Definition Address.cpp:1016
@ eDynamicClassInfoHelperCopyRealizedClassList
Definition Target.h:79
@ eDynamicClassInfoHelperGetRealizedClassList
Definition Target.h:80
@ eDynamicClassInfoHelperAuto
Definition Target.h:77
@ eDynamicClassInfoHelperRealizedClassesStruct
Definition Target.h:78
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Platform > PlatformSP
uint64_t offset_t
Definition lldb-types.h:85
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eLanguageTypeObjC
Objective-C.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::Process > ProcessSP
SymbolType
Symbol types.
@ eSymbolTypeObjCIVar
@ eEncodingUint
unsigned integer
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult
@ eArgTypeRegularExpression
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::StackFrameRecognizer > StackFrameRecognizerSP
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
@ eDynamicDontRunTarget
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition SBAddress.cpp:60
std::shared_ptr< lldb_private::Module > ModuleSP
@ eValueTypeVariableArgument
function argument variables
static DescriptorMapUpdateResult Success(uint32_t found)
static lldb::addr_t ToRawAddress(const ExecutionContext *exe_ctx, llvm::StringRef s, lldb::addr_t fail_value, Status *error_ptr)
As for ToAddress but do not remove non-address bits from the result.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47