1 //===-- AppleObjCRuntimeV2.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclObjC.h"
18 #include "lldb/Host/OptionParser.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/lldb-enumerations.h"
22 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
23 #include "lldb/Core/Debugger.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/Section.h"
27 #include "lldb/Core/ValueObjectConstResult.h"
28 #include "lldb/Core/ValueObjectVariable.h"
29 #include "lldb/Expression/DiagnosticManager.h"
30 #include "lldb/Expression/FunctionCaller.h"
31 #include "lldb/Expression/UtilityFunction.h"
32 #include "lldb/Interpreter/CommandObject.h"
33 #include "lldb/Interpreter/CommandObjectMultiword.h"
34 #include "lldb/Interpreter/CommandReturnObject.h"
35 #include "lldb/Interpreter/OptionArgParser.h"
36 #include "lldb/Interpreter/OptionValueBoolean.h"
37 #include "lldb/Symbol/ObjectFile.h"
38 #include "lldb/Symbol/Symbol.h"
39 #include "lldb/Symbol/TypeList.h"
40 #include "lldb/Symbol/VariableList.h"
41 #include "lldb/Target/ABI.h"
42 #include "lldb/Target/ExecutionContext.h"
43 #include "lldb/Target/Platform.h"
44 #include "lldb/Target/Process.h"
45 #include "lldb/Target/RegisterContext.h"
46 #include "lldb/Target/StackFrameRecognizer.h"
47 #include "lldb/Target/Target.h"
48 #include "lldb/Target/Thread.h"
49 #include "lldb/Utility/ConstString.h"
50 #include "lldb/Utility/Log.h"
51 #include "lldb/Utility/Scalar.h"
52 #include "lldb/Utility/Status.h"
53 #include "lldb/Utility/Stream.h"
54 #include "lldb/Utility/StreamString.h"
55 #include "lldb/Utility/Timer.h"
57 #include "AppleObjCClassDescriptorV2.h"
58 #include "AppleObjCDeclVendor.h"
59 #include "AppleObjCRuntimeV2.h"
60 #include "AppleObjCTrampolineHandler.h"
61 #include "AppleObjCTypeEncodingParser.h"
63 #include "clang/AST/ASTContext.h"
64 #include "clang/AST/DeclObjC.h"
65 #include "clang/Basic/TargetInfo.h"
67 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
72 using namespace lldb_private;
74 char AppleObjCRuntimeV2::ID = 0;
76 static const char *g_get_dynamic_class_info_name =
77 "__lldb_apple_objc_v2_get_dynamic_class_info";
78 // Testing using the new C++11 raw string literals. If this breaks GCC then we
79 // will need to revert to the code above...
80 static const char *g_get_dynamic_class_info_body = R"(
84 size_t strlen(const char *);
85 char *strncpy (char * s1, const char * s2, size_t n);
86 int printf(const char * format, ...);
88 #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
90 typedef struct _NXMapTable {
93 unsigned num_buckets_minus_one;
97 #define NX_MAPNOTAKEY ((void *)(-1))
99 typedef struct BucketInfo
101 const char *name_ptr;
109 } __attribute__((__packed__));
112 __lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr,
113 void *class_infos_ptr,
114 uint32_t class_infos_byte_size,
117 DEBUG_PRINTF ("gdb_objc_realized_classes_ptr = %p\n", gdb_objc_realized_classes_ptr);
118 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
119 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
120 const NXMapTable *grc = (const NXMapTable *)gdb_objc_realized_classes_ptr;
123 const unsigned num_classes = grc->num_classes;
126 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
127 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
128 BucketInfo *buckets = (BucketInfo *)grc->buckets;
131 for (unsigned i=0; i<=grc->num_buckets_minus_one; ++i)
133 if (buckets[i].name_ptr != NX_MAPNOTAKEY)
135 if (idx < max_class_infos)
137 const char *s = buckets[i].name_ptr;
139 for (unsigned char c = *s; c; c = *++s)
140 h = ((h << 5) + h) + c;
141 class_infos[idx].hash = h;
142 class_infos[idx].isa = buckets[i].isa;
147 if (idx < max_class_infos)
149 class_infos[idx].isa = NULL;
150 class_infos[idx].hash = 0;
160 // We'll substitute in class_getName or class_getNameRaw depending
161 // on which is present.
162 static const char *g_shared_cache_class_name_funcptr = R"(
165 const char *%s(void *objc_class);
166 const char *(*class_name_lookup_func)(void *) = %s;
170 static const char *g_get_shared_cache_class_info_name =
171 "__lldb_apple_objc_v2_get_shared_cache_class_info";
172 // Testing using the new C++11 raw string literals. If this breaks GCC then we
173 // will need to revert to the code above...
174 static const char *g_get_shared_cache_class_info_body = R"(
178 size_t strlen(const char *);
179 char *strncpy (char * s1, const char * s2, size_t n);
180 int printf(const char * format, ...);
183 #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
186 struct objc_classheader_t {
191 struct objc_clsopt_t {
199 uint32_t scramble[256];
200 uint8_t tab[0]; // tab[mask+1]
201 // uint8_t checkbytes[capacity];
202 // int32_t offset[capacity];
203 // objc_classheader_t clsOffsets[capacity];
204 // uint32_t duplicateCount;
205 // objc_classheader_t duplicateOffsets[duplicateCount];
210 int32_t selopt_offset;
211 int32_t headeropt_offset;
212 int32_t clsopt_offset;
215 struct objc_opt_v14_t {
218 int32_t selopt_offset;
219 int32_t headeropt_offset;
220 int32_t clsopt_offset;
227 } __attribute__((__packed__));
230 __lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr,
231 void *class_infos_ptr,
232 uint32_t class_infos_byte_size,
236 DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr);
237 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
238 DEBUG_PRINTF ("class_infos_byte_size = %u (%llu class infos)\n", class_infos_byte_size, (uint64_t)(class_infos_byte_size/sizeof(ClassInfo)));
241 const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr;
242 const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr;
243 const bool is_v14_format = objc_opt->version >= 14;
246 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version);
247 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags);
248 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset);
249 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset);
250 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset);
254 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version);
255 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset);
256 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset);
257 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset);
259 if (objc_opt->version == 12 || objc_opt->version == 13 || objc_opt->version == 14 || objc_opt->version == 15)
261 const objc_clsopt_t* clsopt = NULL;
263 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset);
265 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset);
266 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
267 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
268 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
269 int32_t invalidEntryOffset = 0;
270 // this is safe to do because the version field order is invariant
271 if (objc_opt->version == 12)
272 invalidEntryOffset = 16;
273 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
274 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
275 const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity);
276 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
277 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
278 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
279 DEBUG_PRINTF("invalidEntryOffset = %d\n", invalidEntryOffset);
280 for (uint32_t i=0; i<clsopt->capacity; ++i)
282 const int32_t clsOffset = classOffsets[i].clsOffset;
283 DEBUG_PRINTF("clsOffset[%u] = %u\n", i, clsOffset);
286 DEBUG_PRINTF("clsOffset & 1\n");
287 continue; // duplicate
289 else if (clsOffset == invalidEntryOffset)
291 DEBUG_PRINTF("clsOffset == invalidEntryOffset\n");
292 continue; // invalid offset
295 if (class_infos && idx < max_class_infos)
297 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
298 const char *name = class_name_lookup_func (class_infos[idx].isa);
299 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
300 // Hash the class name so we don't have to read it
301 const char *s = name;
303 for (unsigned char c = *s; c; c = *++s)
305 // class_getName demangles swift names and the hash must
306 // be calculated on the mangled name. hash==0 means lldb
307 // will fetch the mangled name and compute the hash in
308 // ParseClassInfoArray.
314 h = ((h << 5) + h) + c;
316 class_infos[idx].hash = h;
320 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
325 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
326 const uint32_t duplicate_count = *duplicate_count_ptr;
327 const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]);
328 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
329 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
330 for (uint32_t i=0; i<duplicate_count; ++i)
332 const int32_t clsOffset = duplicateClassOffsets[i].clsOffset;
334 continue; // duplicate
335 else if (clsOffset == invalidEntryOffset)
336 continue; // invalid offset
338 if (class_infos && idx < max_class_infos)
340 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
341 const char *name = class_name_lookup_func (class_infos[idx].isa);
342 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
343 // Hash the class name so we don't have to read it
344 const char *s = name;
346 for (unsigned char c = *s; c; c = *++s)
348 // class_getName demangles swift names and the hash must
349 // be calculated on the mangled name. hash==0 means lldb
350 // will fetch the mangled name and compute the hash in
351 // ParseClassInfoArray.
357 h = ((h << 5) + h) + c;
359 class_infos[idx].hash = h;
364 DEBUG_PRINTF ("%u class_infos\n", idx);
365 DEBUG_PRINTF ("done\n");
374 ExtractRuntimeGlobalSymbol(Process *process, ConstString name,
375 const ModuleSP &module_sp, Status &error,
376 bool read_value = true, uint8_t byte_size = 0,
377 uint64_t default_value = LLDB_INVALID_ADDRESS,
378 SymbolType sym_type = lldb::eSymbolTypeData) {
380 error.SetErrorString("no process");
381 return default_value;
384 error.SetErrorString("no module");
385 return default_value;
388 byte_size = process->GetAddressByteSize();
389 const Symbol *symbol =
390 module_sp->FindFirstSymbolWithNameAndType(name, lldb::eSymbolTypeData);
391 if (symbol && symbol->ValueIsAddress()) {
392 lldb::addr_t symbol_load_addr =
393 symbol->GetAddressRef().GetLoadAddress(&process->GetTarget());
394 if (symbol_load_addr != LLDB_INVALID_ADDRESS) {
396 return process->ReadUnsignedIntegerFromMemory(
397 symbol_load_addr, byte_size, default_value, error);
399 return symbol_load_addr;
401 error.SetErrorString("symbol address invalid");
402 return default_value;
405 error.SetErrorString("no symbol");
406 return default_value;
410 static void RegisterObjCExceptionRecognizer();
412 AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
413 const ModuleSP &objc_module_sp)
414 : AppleObjCRuntime(process), m_get_class_info_code(),
415 m_get_class_info_args(LLDB_INVALID_ADDRESS),
416 m_get_class_info_args_mutex(), m_get_shared_cache_class_info_code(),
417 m_get_shared_cache_class_info_args(LLDB_INVALID_ADDRESS),
418 m_get_shared_cache_class_info_args_mutex(), m_decl_vendor_up(),
419 m_tagged_pointer_obfuscator(LLDB_INVALID_ADDRESS),
420 m_isa_hash_table_ptr(LLDB_INVALID_ADDRESS), m_hash_signature(),
421 m_has_object_getClass(false), m_loaded_objc_opt(false),
422 m_non_pointer_isa_cache_up(
423 NonPointerISACache::CreateInstance(*this, objc_module_sp)),
424 m_tagged_pointer_vendor_up(
425 TaggedPointerVendorV2::CreateInstance(*this, objc_module_sp)),
426 m_encoding_to_type_sp(), m_noclasses_warning_emitted(false),
427 m_CFBoolean_values() {
428 static const ConstString g_gdb_object_getClass("gdb_object_getClass");
429 m_has_object_getClass =
430 (objc_module_sp->FindFirstSymbolWithNameAndType(
431 g_gdb_object_getClass, eSymbolTypeCode) != nullptr);
432 RegisterObjCExceptionRecognizer();
435 bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress(
436 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
437 TypeAndOrName &class_type_or_name, Address &address,
438 Value::ValueType &value_type) {
439 // We should never get here with a null process...
440 assert(m_process != nullptr);
442 // The Runtime is attached to a particular process, you shouldn't pass in a
443 // value from another process. Note, however, the process might be NULL (e.g.
444 // if the value was made with SBTarget::EvaluateExpression...) in which case
445 // it is sufficient if the target's match:
447 Process *process = in_value.GetProcessSP().get();
449 assert(process == m_process);
451 assert(in_value.GetTargetSP().get() == m_process->CalculateTarget().get());
453 class_type_or_name.Clear();
454 value_type = Value::ValueType::eValueTypeScalar;
456 // Make sure we can have a dynamic value before starting...
457 if (CouldHaveDynamicValue(in_value)) {
458 // First job, pull out the address at 0 offset from the object That will
459 // be the ISA pointer.
460 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(in_value));
462 const addr_t object_ptr = in_value.GetPointerValue();
463 address.SetRawAddress(object_ptr);
465 ConstString class_name(objc_class_sp->GetClassName());
466 class_type_or_name.SetName(class_name);
467 TypeSP type_sp(objc_class_sp->GetType());
469 class_type_or_name.SetTypeSP(type_sp);
471 type_sp = LookupInCompleteClassCache(class_name);
473 objc_class_sp->SetType(type_sp);
474 class_type_or_name.SetTypeSP(type_sp);
476 // try to go for a CompilerType at least
477 if (auto *vendor = GetDeclVendor()) {
478 auto types = vendor->FindTypes(class_name, /*max_matches*/ 1);
480 class_type_or_name.SetCompilerType(types.front());
486 return !class_type_or_name.IsEmpty();
490 LanguageRuntime *AppleObjCRuntimeV2::CreateInstance(Process *process,
491 LanguageType language) {
492 // FIXME: This should be a MacOS or iOS process, and we need to look for the
493 // OBJC section to make
494 // sure we aren't using the V1 runtime.
495 if (language == eLanguageTypeObjC) {
496 ModuleSP objc_module_sp;
498 if (AppleObjCRuntime::GetObjCVersion(process, objc_module_sp) ==
499 ObjCRuntimeVersions::eAppleObjC_V2)
500 return new AppleObjCRuntimeV2(process, objc_module_sp);
507 static constexpr OptionDefinition g_objc_classtable_dump_options[] = {
508 {LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument,
509 nullptr, {}, 0, eArgTypeNone,
510 "Print ivar and method information in detail"}};
512 class CommandObjectObjC_ClassTable_Dump : public CommandObjectParsed {
514 class CommandOptions : public Options {
516 CommandOptions() : Options(), m_verbose(false, false) {}
518 ~CommandOptions() override = default;
520 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
521 ExecutionContext *execution_context) override {
523 const int short_option = m_getopt_table[option_idx].val;
524 switch (short_option) {
526 m_verbose.SetCurrentValue(true);
527 m_verbose.SetOptionWasSet();
531 error.SetErrorStringWithFormat("unrecognized short option '%c'",
539 void OptionParsingStarting(ExecutionContext *execution_context) override {
543 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
544 return llvm::makeArrayRef(g_objc_classtable_dump_options);
547 OptionValueBoolean m_verbose;
550 CommandObjectObjC_ClassTable_Dump(CommandInterpreter &interpreter)
551 : CommandObjectParsed(
552 interpreter, "dump", "Dump information on Objective-C classes "
553 "known to the current process.",
554 "language objc class-table dump",
555 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
556 eCommandProcessMustBePaused),
558 CommandArgumentEntry arg;
559 CommandArgumentData index_arg;
561 // Define the first (and only) variant of this arg.
562 index_arg.arg_type = eArgTypeRegularExpression;
563 index_arg.arg_repetition = eArgRepeatOptional;
565 // There is only one variant this argument could be; put it into the
567 arg.push_back(index_arg);
569 // Push the data for the first argument into the m_arguments vector.
570 m_arguments.push_back(arg);
573 ~CommandObjectObjC_ClassTable_Dump() override = default;
575 Options *GetOptions() override { return &m_options; }
578 bool DoExecute(Args &command, CommandReturnObject &result) override {
579 std::unique_ptr<RegularExpression> regex_up;
580 switch (command.GetArgumentCount()) {
584 regex_up = std::make_unique<RegularExpression>(
585 llvm::StringRef::withNullAsEmpty(command.GetArgumentAtIndex(0)));
586 if (!regex_up->IsValid()) {
588 "invalid argument - please provide a valid regular expression");
589 result.SetStatus(lldb::eReturnStatusFailed);
595 result.AppendError("please provide 0 or 1 arguments");
596 result.SetStatus(lldb::eReturnStatusFailed);
601 Process *process = m_exe_ctx.GetProcessPtr();
602 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
604 auto iterators_pair = objc_runtime->GetDescriptorIteratorPair();
605 auto iterator = iterators_pair.first;
606 auto &std_out = result.GetOutputStream();
607 for (; iterator != iterators_pair.second; iterator++) {
608 if (iterator->second) {
609 const char *class_name =
610 iterator->second->GetClassName().AsCString("<unknown>");
611 if (regex_up && class_name &&
612 !regex_up->Execute(llvm::StringRef(class_name)))
614 std_out.Printf("isa = 0x%" PRIx64, iterator->first);
615 std_out.Printf(" name = %s", class_name);
616 std_out.Printf(" instance size = %" PRIu64,
617 iterator->second->GetInstanceSize());
618 std_out.Printf(" num ivars = %" PRIuPTR,
619 (uintptr_t)iterator->second->GetNumIVars());
620 if (auto superclass = iterator->second->GetSuperclass()) {
621 std_out.Printf(" superclass = %s",
622 superclass->GetClassName().AsCString("<unknown>"));
624 std_out.Printf("\n");
625 if (m_options.m_verbose) {
626 for (size_t i = 0; i < iterator->second->GetNumIVars(); i++) {
627 auto ivar = iterator->second->GetIVarAtIndex(i);
629 " ivar name = %s type = %s size = %" PRIu64
630 " offset = %" PRId32 "\n",
631 ivar.m_name.AsCString("<unknown>"),
632 ivar.m_type.GetDisplayTypeName().AsCString("<unknown>"),
633 ivar.m_size, ivar.m_offset);
635 iterator->second->Describe(
637 [&std_out](const char *name, const char *type) -> bool {
638 std_out.Printf(" instance method name = %s type = %s\n",
642 [&std_out](const char *name, const char *type) -> bool {
643 std_out.Printf(" class method name = %s type = %s\n", name,
650 if (regex_up && !regex_up->Execute(llvm::StringRef()))
652 std_out.Printf("isa = 0x%" PRIx64 " has no associated class.\n",
656 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
659 result.AppendError("current process has no Objective-C runtime loaded");
660 result.SetStatus(lldb::eReturnStatusFailed);
665 CommandOptions m_options;
668 class CommandObjectMultiwordObjC_TaggedPointer_Info
669 : public CommandObjectParsed {
671 CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter &interpreter)
672 : CommandObjectParsed(
673 interpreter, "info", "Dump information on a tagged pointer.",
674 "language objc tagged-pointer info",
675 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
676 eCommandProcessMustBePaused) {
677 CommandArgumentEntry arg;
678 CommandArgumentData index_arg;
680 // Define the first (and only) variant of this arg.
681 index_arg.arg_type = eArgTypeAddress;
682 index_arg.arg_repetition = eArgRepeatPlus;
684 // There is only one variant this argument could be; put it into the
686 arg.push_back(index_arg);
688 // Push the data for the first argument into the m_arguments vector.
689 m_arguments.push_back(arg);
692 ~CommandObjectMultiwordObjC_TaggedPointer_Info() override = default;
695 bool DoExecute(Args &command, CommandReturnObject &result) override {
696 if (command.GetArgumentCount() == 0) {
697 result.AppendError("this command requires arguments");
698 result.SetStatus(lldb::eReturnStatusFailed);
702 Process *process = m_exe_ctx.GetProcessPtr();
703 ExecutionContext exe_ctx(process);
704 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
706 ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor =
707 objc_runtime->GetTaggedPointerVendor();
708 if (tagged_ptr_vendor) {
709 for (size_t i = 0; i < command.GetArgumentCount(); i++) {
710 const char *arg_str = command.GetArgumentAtIndex(i);
714 lldb::addr_t arg_addr = OptionArgParser::ToAddress(
715 &exe_ctx, arg_str, LLDB_INVALID_ADDRESS, &error);
716 if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail())
718 auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(arg_addr);
721 uint64_t info_bits = 0;
722 uint64_t value_bits = 0;
723 uint64_t payload = 0;
724 if (descriptor_sp->GetTaggedPointerInfo(&info_bits, &value_bits,
726 result.GetOutputStream().Printf(
727 "0x%" PRIx64 " is tagged.\n\tpayload = 0x%" PRIx64
728 "\n\tvalue = 0x%" PRIx64 "\n\tinfo bits = 0x%" PRIx64
730 (uint64_t)arg_addr, payload, value_bits, info_bits,
731 descriptor_sp->GetClassName().AsCString("<unknown>"));
733 result.GetOutputStream().Printf("0x%" PRIx64 " is not tagged.\n",
738 result.AppendError("current process has no tagged pointer support");
739 result.SetStatus(lldb::eReturnStatusFailed);
742 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
745 result.AppendError("current process has no Objective-C runtime loaded");
746 result.SetStatus(lldb::eReturnStatusFailed);
752 class CommandObjectMultiwordObjC_ClassTable : public CommandObjectMultiword {
754 CommandObjectMultiwordObjC_ClassTable(CommandInterpreter &interpreter)
755 : CommandObjectMultiword(
756 interpreter, "class-table",
757 "Commands for operating on the Objective-C class table.",
758 "class-table <subcommand> [<subcommand-options>]") {
761 CommandObjectSP(new CommandObjectObjC_ClassTable_Dump(interpreter)));
764 ~CommandObjectMultiwordObjC_ClassTable() override = default;
767 class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword {
769 CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter &interpreter)
770 : CommandObjectMultiword(
771 interpreter, "tagged-pointer",
772 "Commands for operating on Objective-C tagged pointers.",
773 "class-table <subcommand> [<subcommand-options>]") {
777 new CommandObjectMultiwordObjC_TaggedPointer_Info(interpreter)));
780 ~CommandObjectMultiwordObjC_TaggedPointer() override = default;
783 class CommandObjectMultiwordObjC : public CommandObjectMultiword {
785 CommandObjectMultiwordObjC(CommandInterpreter &interpreter)
786 : CommandObjectMultiword(
788 "Commands for operating on the Objective-C language runtime.",
789 "objc <subcommand> [<subcommand-options>]") {
790 LoadSubCommand("class-table",
792 new CommandObjectMultiwordObjC_ClassTable(interpreter)));
793 LoadSubCommand("tagged-pointer",
794 CommandObjectSP(new CommandObjectMultiwordObjC_TaggedPointer(
798 ~CommandObjectMultiwordObjC() override = default;
801 void AppleObjCRuntimeV2::Initialize() {
802 PluginManager::RegisterPlugin(
803 GetPluginNameStatic(), "Apple Objective-C Language Runtime - Version 2",
805 [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
806 return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter));
808 GetBreakpointExceptionPrecondition);
811 void AppleObjCRuntimeV2::Terminate() {
812 PluginManager::UnregisterPlugin(CreateInstance);
815 lldb_private::ConstString AppleObjCRuntimeV2::GetPluginNameStatic() {
816 static ConstString g_name("apple-objc-v2");
820 // PluginInterface protocol
821 lldb_private::ConstString AppleObjCRuntimeV2::GetPluginName() {
822 return GetPluginNameStatic();
825 uint32_t AppleObjCRuntimeV2::GetPluginVersion() { return 1; }
828 AppleObjCRuntimeV2::CreateExceptionResolver(const BreakpointSP &bkpt,
829 bool catch_bp, bool throw_bp) {
830 BreakpointResolverSP resolver_sp;
833 resolver_sp = std::make_shared<BreakpointResolverName>(
834 bkpt, std::get<1>(GetExceptionThrowLocation()).AsCString(),
835 eFunctionNameTypeBase, eLanguageTypeUnknown, Breakpoint::Exact, 0,
837 // FIXME: We don't do catch breakpoints for ObjC yet.
838 // Should there be some way for the runtime to specify what it can do in this
843 UtilityFunction *AppleObjCRuntimeV2::CreateObjectChecker(const char *name) {
844 char check_function_code[2048];
847 if (m_has_object_getClass) {
848 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
849 extern "C" void *gdb_object_getClass(void *);
850 extern "C" int printf(const char *format, ...);
852 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
853 if ($__lldb_arg_obj == (void *)0)
855 if (!gdb_object_getClass($__lldb_arg_obj)) {
856 *((volatile int *)0) = 'ocgc';
857 } else if ($__lldb_arg_selector != (void *)0) {
858 signed char $responds = (signed char)
859 [(id)$__lldb_arg_obj respondsToSelector:
860 (void *) $__lldb_arg_selector];
861 if ($responds == (signed char) 0)
862 *((volatile int *)0) = 'ocgc';
866 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
867 extern "C" void *gdb_class_getClass(void *);
868 extern "C" int printf(const char *format, ...);
870 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
871 if ($__lldb_arg_obj == (void *)0)
873 void **$isa_ptr = (void **)$__lldb_arg_obj;
874 if (*$isa_ptr == (void *)0 ||
875 !gdb_class_getClass(*$isa_ptr))
876 *((volatile int *)0) = 'ocgc';
877 else if ($__lldb_arg_selector != (void *)0) {
878 signed char $responds = (signed char)
879 [(id)$__lldb_arg_obj respondsToSelector:
880 (void *) $__lldb_arg_selector];
881 if ($responds == (signed char) 0)
882 *((volatile int *)0) = 'ocgc';
887 assert(len < (int)sizeof(check_function_code));
888 UNUSED_IF_ASSERT_DISABLED(len);
891 return GetTargetRef().GetUtilityFunctionForLanguage(
892 check_function_code, eLanguageTypeObjC, name, error);
895 size_t AppleObjCRuntimeV2::GetByteOffsetForIvar(CompilerType &parent_ast_type,
896 const char *ivar_name) {
897 uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
899 ConstString class_name = parent_ast_type.GetTypeName();
900 if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) {
901 // Make the objective C V2 mangled name for the ivar offset from the class
902 // name and ivar name
903 std::string buffer("OBJC_IVAR_$_");
904 buffer.append(class_name.AsCString());
905 buffer.push_back('.');
906 buffer.append(ivar_name);
907 ConstString ivar_const_str(buffer.c_str());
909 // Try to get the ivar offset address from the symbol table first using the
910 // name we created above
911 SymbolContextList sc_list;
912 Target &target = m_process->GetTarget();
913 target.GetImages().FindSymbolsWithNameAndType(ivar_const_str,
914 eSymbolTypeObjCIVar, sc_list);
916 addr_t ivar_offset_address = LLDB_INVALID_ADDRESS;
919 SymbolContext ivar_offset_symbol;
920 if (sc_list.GetSize() == 1 &&
921 sc_list.GetContextAtIndex(0, ivar_offset_symbol)) {
922 if (ivar_offset_symbol.symbol)
923 ivar_offset_address =
924 ivar_offset_symbol.symbol->GetLoadAddress(&target);
927 // If we didn't get the ivar offset address from the symbol table, fall
928 // back to getting it from the runtime
929 if (ivar_offset_address == LLDB_INVALID_ADDRESS)
930 ivar_offset_address = LookupRuntimeSymbol(ivar_const_str);
932 if (ivar_offset_address != LLDB_INVALID_ADDRESS)
933 ivar_offset = m_process->ReadUnsignedIntegerFromMemory(
934 ivar_offset_address, 4, LLDB_INVALID_IVAR_OFFSET, error);
939 // tagged pointers are special not-a-real-pointer values that contain both type
940 // and value information this routine attempts to check with as little
941 // computational effort as possible whether something could possibly be a
942 // tagged pointer - false positives are possible but false negatives shouldn't
943 bool AppleObjCRuntimeV2::IsTaggedPointer(addr_t ptr) {
944 if (!m_tagged_pointer_vendor_up)
946 return m_tagged_pointer_vendor_up->IsPossibleTaggedPointer(ptr);
949 class RemoteNXMapTable {
952 : m_count(0), m_num_buckets_minus_one(0),
953 m_buckets_ptr(LLDB_INVALID_ADDRESS), m_process(nullptr),
954 m_end_iterator(*this, -1), m_load_addr(LLDB_INVALID_ADDRESS),
955 m_map_pair_size(0), m_invalid_key(0) {}
958 printf("RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n", m_load_addr);
959 printf("RemoteNXMapTable.m_count = %u\n", m_count);
960 printf("RemoteNXMapTable.m_num_buckets_minus_one = %u\n",
961 m_num_buckets_minus_one);
962 printf("RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n", m_buckets_ptr);
965 bool ParseHeader(Process *process, lldb::addr_t load_addr) {
967 m_load_addr = load_addr;
968 m_map_pair_size = m_process->GetAddressByteSize() * 2;
970 m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX;
973 // This currently holds true for all platforms we support, but we might
974 // need to change this to use get the actually byte size of "unsigned" from
976 const uint32_t unsigned_byte_size = sizeof(uint32_t);
977 // Skip the prototype as we don't need it (const struct
978 // +NXMapTablePrototype *prototype)
981 if (load_addr == LLDB_INVALID_ADDRESS)
984 lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize();
987 m_count = m_process->ReadUnsignedIntegerFromMemory(
988 cursor, unsigned_byte_size, 0, err);
990 cursor += unsigned_byte_size;
992 // unsigned nbBucketsMinusOne;
993 m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory(
994 cursor, unsigned_byte_size, 0, err);
995 cursor += unsigned_byte_size;
998 m_buckets_ptr = m_process->ReadPointerFromMemory(cursor, err);
1000 success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS;
1006 m_num_buckets_minus_one = 0;
1007 m_buckets_ptr = LLDB_INVALID_ADDRESS;
1012 // const_iterator mimics NXMapState and its code comes from NXInitMapState
1013 // and NXNextMapState.
1014 typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element;
1016 friend class const_iterator;
1017 class const_iterator {
1019 const_iterator(RemoteNXMapTable &parent, int index)
1020 : m_parent(parent), m_index(index) {
1021 AdvanceToValidIndex();
1024 const_iterator(const const_iterator &rhs)
1025 : m_parent(rhs.m_parent), m_index(rhs.m_index) {
1026 // AdvanceToValidIndex() has been called by rhs already.
1029 const_iterator &operator=(const const_iterator &rhs) {
1030 // AdvanceToValidIndex() has been called by rhs already.
1031 assert(&m_parent == &rhs.m_parent);
1032 m_index = rhs.m_index;
1036 bool operator==(const const_iterator &rhs) const {
1037 if (&m_parent != &rhs.m_parent)
1039 if (m_index != rhs.m_index)
1045 bool operator!=(const const_iterator &rhs) const {
1046 return !(operator==(rhs));
1049 const_iterator &operator++() {
1050 AdvanceToValidIndex();
1054 const element operator*() const {
1055 if (m_index == -1) {
1056 // TODO find a way to make this an error, but not an assert
1060 lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1061 size_t map_pair_size = m_parent.m_map_pair_size;
1062 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1067 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1070 lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory(
1071 pair_ptr + m_parent.m_process->GetAddressByteSize(), err);
1075 std::string key_string;
1077 m_parent.m_process->ReadCStringFromMemory(key, key_string, err);
1081 return element(ConstString(key_string.c_str()),
1082 (ObjCLanguageRuntime::ObjCISA)value);
1086 void AdvanceToValidIndex() {
1090 const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1091 const size_t map_pair_size = m_parent.m_map_pair_size;
1092 const lldb::addr_t invalid_key = m_parent.m_invalid_key;
1096 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1098 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1100 if (!err.Success()) {
1105 if (key != invalid_key)
1109 RemoteNXMapTable &m_parent;
1113 const_iterator begin() {
1114 return const_iterator(*this, m_num_buckets_minus_one + 1);
1117 const_iterator end() { return m_end_iterator; }
1119 uint32_t GetCount() const { return m_count; }
1121 uint32_t GetBucketCount() const { return m_num_buckets_minus_one; }
1123 lldb::addr_t GetBucketDataPointer() const { return m_buckets_ptr; }
1125 lldb::addr_t GetTableLoadAddress() const { return m_load_addr; }
1128 // contents of _NXMapTable struct
1130 uint32_t m_num_buckets_minus_one;
1131 lldb::addr_t m_buckets_ptr;
1132 lldb_private::Process *m_process;
1133 const_iterator m_end_iterator;
1134 lldb::addr_t m_load_addr;
1135 size_t m_map_pair_size;
1136 lldb::addr_t m_invalid_key;
1139 AppleObjCRuntimeV2::HashTableSignature::HashTableSignature()
1140 : m_count(0), m_num_buckets(0), m_buckets_ptr(0) {}
1142 void AppleObjCRuntimeV2::HashTableSignature::UpdateSignature(
1143 const RemoteNXMapTable &hash_table) {
1144 m_count = hash_table.GetCount();
1145 m_num_buckets = hash_table.GetBucketCount();
1146 m_buckets_ptr = hash_table.GetBucketDataPointer();
1149 bool AppleObjCRuntimeV2::HashTableSignature::NeedsUpdate(
1150 Process *process, AppleObjCRuntimeV2 *runtime,
1151 RemoteNXMapTable &hash_table) {
1152 if (!hash_table.ParseHeader(process, runtime->GetISAHashTablePointer())) {
1153 return false; // Failed to parse the header, no need to update anything
1156 // Check with out current signature and return true if the count, number of
1157 // buckets or the hash table address changes.
1158 if (m_count == hash_table.GetCount() &&
1159 m_num_buckets == hash_table.GetBucketCount() &&
1160 m_buckets_ptr == hash_table.GetBucketDataPointer()) {
1161 // Hash table hasn't changed
1164 // Hash table data has changed, we need to update
1168 ObjCLanguageRuntime::ClassDescriptorSP
1169 AppleObjCRuntimeV2::GetClassDescriptorFromISA(ObjCISA isa) {
1170 ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp;
1171 if (m_non_pointer_isa_cache_up)
1172 class_descriptor_sp = m_non_pointer_isa_cache_up->GetClassDescriptor(isa);
1173 if (!class_descriptor_sp)
1174 class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa);
1175 return class_descriptor_sp;
1178 static std::pair<bool, ConstString> ObjCGetClassNameRaw(
1179 AppleObjCRuntime::ObjCISA isa,
1181 StreamString expr_string;
1182 std::string input = std::to_string(isa);
1183 expr_string.Printf("(const char *)objc_debug_class_getNameRaw(%s)",
1186 ValueObjectSP result_sp;
1187 EvaluateExpressionOptions eval_options;
1188 eval_options.SetLanguage(lldb::eLanguageTypeObjC);
1189 eval_options.SetResultIsInternal(true);
1190 eval_options.SetGenerateDebugInfo(true);
1191 eval_options.SetTimeout(process->GetUtilityExpressionTimeout());
1192 auto eval_result = process->GetTarget().EvaluateExpression(
1193 expr_string.GetData(),
1194 process->GetThreadList().GetSelectedThread()->GetSelectedFrame().get(),
1195 result_sp, eval_options);
1196 ConstString type_name(result_sp->GetSummaryAsCString());
1197 return std::make_pair(eval_result == eExpressionCompleted, type_name);
1200 ObjCLanguageRuntime::ClassDescriptorSP
1201 AppleObjCRuntimeV2::GetClassDescriptor(ValueObject &valobj) {
1202 ClassDescriptorSP objc_class_sp;
1203 if (valobj.IsBaseClass()) {
1204 ValueObject *parent = valobj.GetParent();
1205 // if I am my own parent, bail out of here fast..
1206 if (parent && parent != &valobj) {
1207 ClassDescriptorSP parent_descriptor_sp = GetClassDescriptor(*parent);
1208 if (parent_descriptor_sp)
1209 return parent_descriptor_sp->GetSuperclass();
1213 // if we get an invalid VO (which might still happen when playing around with
1214 // pointers returned by the expression parser, don't consider this a valid
1216 if (!valobj.GetCompilerType().IsValid())
1217 return objc_class_sp;
1218 addr_t isa_pointer = valobj.GetPointerValue();
1221 if (IsTaggedPointer(isa_pointer))
1222 return m_tagged_pointer_vendor_up->GetClassDescriptor(isa_pointer);
1223 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1225 Process *process = exe_ctx.GetProcessPtr();
1227 return objc_class_sp;
1230 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
1231 if (isa == LLDB_INVALID_ADDRESS)
1232 return objc_class_sp;
1234 objc_class_sp = GetClassDescriptorFromISA(isa);
1237 return objc_class_sp;
1239 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS |
1240 LIBLLDB_LOG_TYPES));
1243 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was "
1244 "not in class descriptor cache 0x%" PRIx64,
1248 ClassDescriptorSP descriptor_sp(new ClassDescriptorV2(*this, isa, nullptr));
1249 auto resolved = ObjCGetClassNameRaw(isa, process);
1250 if (resolved.first == true) {
1251 AddClass(isa, descriptor_sp, resolved.second.AsCString());
1252 objc_class_sp = descriptor_sp;
1254 return objc_class_sp;
1257 lldb::addr_t AppleObjCRuntimeV2::GetTaggedPointerObfuscator() {
1258 if (m_tagged_pointer_obfuscator != LLDB_INVALID_ADDRESS)
1259 return m_tagged_pointer_obfuscator;
1262 Process *process = GetProcess();
1263 ModuleSP objc_module_sp(GetObjCModule());
1265 if (!objc_module_sp)
1266 return LLDB_INVALID_ADDRESS;
1268 static ConstString g_gdb_objc_obfuscator("objc_debug_taggedpointer_obfuscator");
1270 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1271 g_gdb_objc_obfuscator, lldb::eSymbolTypeAny);
1273 lldb::addr_t g_gdb_obj_obfuscator_ptr =
1274 symbol->GetLoadAddress(&process->GetTarget());
1276 if (g_gdb_obj_obfuscator_ptr != LLDB_INVALID_ADDRESS) {
1278 m_tagged_pointer_obfuscator = process->ReadPointerFromMemory(
1279 g_gdb_obj_obfuscator_ptr, error);
1282 // If we don't have a correct value at this point, there must be no obfuscation.
1283 if (m_tagged_pointer_obfuscator == LLDB_INVALID_ADDRESS)
1284 m_tagged_pointer_obfuscator = 0;
1286 return m_tagged_pointer_obfuscator;
1289 lldb::addr_t AppleObjCRuntimeV2::GetISAHashTablePointer() {
1290 if (m_isa_hash_table_ptr == LLDB_INVALID_ADDRESS) {
1291 Process *process = GetProcess();
1293 ModuleSP objc_module_sp(GetObjCModule());
1295 if (!objc_module_sp)
1296 return LLDB_INVALID_ADDRESS;
1298 static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes");
1300 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1301 g_gdb_objc_realized_classes, lldb::eSymbolTypeAny);
1303 lldb::addr_t gdb_objc_realized_classes_ptr =
1304 symbol->GetLoadAddress(&process->GetTarget());
1306 if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS) {
1308 m_isa_hash_table_ptr = process->ReadPointerFromMemory(
1309 gdb_objc_realized_classes_ptr, error);
1313 return m_isa_hash_table_ptr;
1316 AppleObjCRuntimeV2::DescriptorMapUpdateResult
1317 AppleObjCRuntimeV2::UpdateISAToDescriptorMapDynamic(
1318 RemoteNXMapTable &hash_table) {
1319 Process *process = GetProcess();
1321 if (process == nullptr)
1322 return DescriptorMapUpdateResult::Fail();
1324 uint32_t num_class_infos = 0;
1326 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1328 ExecutionContext exe_ctx;
1330 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
1333 return DescriptorMapUpdateResult::Fail();
1335 thread_sp->CalculateExecutionContext(exe_ctx);
1336 TypeSystemClang *ast = TypeSystemClang::GetScratch(process->GetTarget());
1339 return DescriptorMapUpdateResult::Fail();
1341 Address function_address;
1343 DiagnosticManager diagnostics;
1345 const uint32_t addr_size = process->GetAddressByteSize();
1349 // Read the total number of classes from the hash table
1350 const uint32_t num_classes = hash_table.GetCount();
1351 if (num_classes == 0) {
1352 LLDB_LOGF(log, "No dynamic classes found in gdb_objc_realized_classes.");
1353 return DescriptorMapUpdateResult::Success(0);
1356 // Make some types for our arguments
1357 CompilerType clang_uint32_t_type =
1358 ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1359 CompilerType clang_void_pointer_type =
1360 ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1362 ValueList arguments;
1363 FunctionCaller *get_class_info_function = nullptr;
1365 if (!m_get_class_info_code) {
1367 m_get_class_info_code.reset(GetTargetRef().GetUtilityFunctionForLanguage(
1368 g_get_dynamic_class_info_body, eLanguageTypeObjC,
1369 g_get_dynamic_class_info_name, error));
1372 "Failed to get Utility Function for implementation lookup: %s",
1374 m_get_class_info_code.reset();
1376 diagnostics.Clear();
1378 if (!m_get_class_info_code->Install(diagnostics, exe_ctx)) {
1380 LLDB_LOGF(log, "Failed to install implementation lookup");
1381 diagnostics.Dump(log);
1383 m_get_class_info_code.reset();
1386 if (!m_get_class_info_code)
1387 return DescriptorMapUpdateResult::Fail();
1389 // Next make the runner function for our implementation utility function.
1391 value.SetValueType(Value::eValueTypeScalar);
1392 value.SetCompilerType(clang_void_pointer_type);
1393 arguments.PushValue(value);
1394 arguments.PushValue(value);
1396 value.SetValueType(Value::eValueTypeScalar);
1397 value.SetCompilerType(clang_uint32_t_type);
1398 arguments.PushValue(value);
1399 arguments.PushValue(value);
1401 get_class_info_function = m_get_class_info_code->MakeFunctionCaller(
1402 clang_uint32_t_type, arguments, thread_sp, error);
1406 "Failed to make function caller for implementation lookup: %s.",
1408 return DescriptorMapUpdateResult::Fail();
1411 get_class_info_function = m_get_class_info_code->GetFunctionCaller();
1412 if (!get_class_info_function) {
1414 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
1415 diagnostics.Dump(log);
1418 return DescriptorMapUpdateResult::Fail();
1420 arguments = get_class_info_function->GetArgumentValues();
1423 diagnostics.Clear();
1425 const uint32_t class_info_byte_size = addr_size + 4;
1426 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1427 lldb::addr_t class_infos_addr = process->AllocateMemory(
1428 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
1430 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
1432 "unable to allocate %" PRIu32
1433 " bytes in process for shared cache read",
1434 class_infos_byte_size);
1435 return DescriptorMapUpdateResult::Fail();
1438 std::lock_guard<std::mutex> guard(m_get_class_info_args_mutex);
1440 // Fill in our function argument values
1441 arguments.GetValueAtIndex(0)->GetScalar() = hash_table.GetTableLoadAddress();
1442 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1443 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1445 // Only dump the runtime classes from the expression evaluation if the log is
1447 Log *type_log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES);
1448 bool dump_log = type_log && type_log->GetVerbose();
1450 arguments.GetValueAtIndex(3)->GetScalar() = dump_log ? 1 : 0;
1452 bool success = false;
1454 diagnostics.Clear();
1456 // Write our function arguments into the process so we can run our function
1457 if (get_class_info_function->WriteFunctionArguments(
1458 exe_ctx, m_get_class_info_args, arguments, diagnostics)) {
1459 EvaluateExpressionOptions options;
1460 options.SetUnwindOnError(true);
1461 options.SetTryAllThreads(false);
1462 options.SetStopOthers(true);
1463 options.SetIgnoreBreakpoints(true);
1464 options.SetTimeout(process->GetUtilityExpressionTimeout());
1465 options.SetIsForUtilityExpr(true);
1468 return_value.SetValueType(Value::eValueTypeScalar);
1469 return_value.SetCompilerType(clang_uint32_t_type);
1470 return_value.GetScalar() = 0;
1472 diagnostics.Clear();
1475 ExpressionResults results = get_class_info_function->ExecuteFunction(
1476 exe_ctx, &m_get_class_info_args, options, diagnostics, return_value);
1478 if (results == eExpressionCompleted) {
1479 // The result is the number of ClassInfo structures that were filled in
1480 num_class_infos = return_value.GetScalar().ULong();
1481 LLDB_LOGF(log, "Discovered %u ObjC classes\n", num_class_infos);
1482 if (num_class_infos > 0) {
1483 // Read the ClassInfo structures
1484 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
1485 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(),
1486 buffer.GetByteSize(),
1487 err) == buffer.GetByteSize()) {
1488 DataExtractor class_infos_data(buffer.GetBytes(),
1489 buffer.GetByteSize(),
1490 process->GetByteOrder(), addr_size);
1491 ParseClassInfoArray(class_infos_data, num_class_infos);
1497 LLDB_LOGF(log, "Error evaluating our find class name function.");
1498 diagnostics.Dump(log);
1503 LLDB_LOGF(log, "Error writing function arguments.");
1504 diagnostics.Dump(log);
1508 // Deallocate the memory we allocated for the ClassInfo array
1509 process->DeallocateMemory(class_infos_addr);
1511 return DescriptorMapUpdateResult(success, num_class_infos);
1514 uint32_t AppleObjCRuntimeV2::ParseClassInfoArray(const DataExtractor &data,
1515 uint32_t num_class_infos) {
1516 // Parses an array of "num_class_infos" packed ClassInfo structures:
1522 // } __attribute__((__packed__));
1524 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
1525 bool should_log = log && log->GetVerbose();
1527 uint32_t num_parsed = 0;
1529 // Iterate through all ClassInfo structures
1530 lldb::offset_t offset = 0;
1531 for (uint32_t i = 0; i < num_class_infos; ++i) {
1532 ObjCISA isa = data.GetAddress(&offset);
1537 log, "AppleObjCRuntimeV2 found NULL isa, ignoring this class info");
1540 // Check if we already know about this ISA, if we do, the info will never
1541 // change, so we can just skip it.
1542 if (ISAIsCached(isa)) {
1545 "AppleObjCRuntimeV2 found cached isa=0x%" PRIx64
1546 ", ignoring this class info",
1550 // Read the 32 bit hash for the class name
1551 const uint32_t name_hash = data.GetU32(&offset);
1552 ClassDescriptorSP descriptor_sp(
1553 new ClassDescriptorV2(*this, isa, nullptr));
1555 // The code in g_get_shared_cache_class_info_body sets the value of the hash
1556 // to 0 to signal a demangled symbol. We use class_getName() in that code to
1557 // find the class name, but this returns a demangled name for Swift symbols.
1558 // For those symbols, recompute the hash here by reading their name from the
1561 AddClass(isa, descriptor_sp, name_hash);
1563 AddClass(isa, descriptor_sp, descriptor_sp->GetClassName().AsCString(nullptr));
1567 "AppleObjCRuntimeV2 added isa=0x%" PRIx64
1568 ", hash=0x%8.8x, name=%s",
1570 descriptor_sp->GetClassName().AsCString("<unknown>"));
1574 LLDB_LOGF(log, "AppleObjCRuntimeV2 parsed %" PRIu32 " class infos",
1579 AppleObjCRuntimeV2::DescriptorMapUpdateResult
1580 AppleObjCRuntimeV2::UpdateISAToDescriptorMapSharedCache() {
1581 Process *process = GetProcess();
1583 if (process == nullptr)
1584 return DescriptorMapUpdateResult::Fail();
1586 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1588 ExecutionContext exe_ctx;
1590 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
1593 return DescriptorMapUpdateResult::Fail();
1595 thread_sp->CalculateExecutionContext(exe_ctx);
1596 TypeSystemClang *ast = TypeSystemClang::GetScratch(process->GetTarget());
1599 return DescriptorMapUpdateResult::Fail();
1601 Address function_address;
1603 DiagnosticManager diagnostics;
1605 const uint32_t addr_size = process->GetAddressByteSize();
1609 uint32_t num_class_infos = 0;
1611 const lldb::addr_t objc_opt_ptr = GetSharedCacheReadOnlyAddress();
1613 if (objc_opt_ptr == LLDB_INVALID_ADDRESS)
1614 return DescriptorMapUpdateResult::Fail();
1616 const uint32_t num_classes = 128 * 1024;
1618 // Make some types for our arguments
1619 CompilerType clang_uint32_t_type =
1620 ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1621 CompilerType clang_void_pointer_type =
1622 ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1624 ValueList arguments;
1625 FunctionCaller *get_shared_cache_class_info_function = nullptr;
1627 if (!m_get_shared_cache_class_info_code) {
1630 // If the inferior objc.dylib has the class_getNameRaw function,
1631 // use that in our jitted expression. Else fall back to the old
1633 static ConstString g_class_getName_symbol_name("class_getName");
1634 static ConstString g_class_getNameRaw_symbol_name("objc_debug_class_getNameRaw");
1635 ConstString class_name_getter_function_name = g_class_getName_symbol_name;
1637 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
1639 const ModuleList &images = process->GetTarget().GetImages();
1640 std::lock_guard<std::recursive_mutex> guard(images.GetMutex());
1641 for (size_t i = 0; i < images.GetSize(); ++i) {
1642 lldb::ModuleSP mod_sp = images.GetModuleAtIndexUnlocked(i);
1643 if (objc_runtime->IsModuleObjCLibrary(mod_sp)) {
1644 const Symbol *symbol =
1645 mod_sp->FindFirstSymbolWithNameAndType(g_class_getNameRaw_symbol_name,
1646 lldb::eSymbolTypeCode);
1648 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
1649 class_name_getter_function_name = g_class_getNameRaw_symbol_name;
1655 // Substitute in the correct class_getName / class_getNameRaw function name,
1656 // concatenate the two parts of our expression text. The format string
1657 // has two %s's, so provide the name twice.
1658 std::string shared_class_expression;
1659 llvm::raw_string_ostream(shared_class_expression) << llvm::format(
1660 g_shared_cache_class_name_funcptr,
1661 class_name_getter_function_name.AsCString(),
1662 class_name_getter_function_name.AsCString());
1664 shared_class_expression += g_get_shared_cache_class_info_body;
1666 m_get_shared_cache_class_info_code.reset(
1667 GetTargetRef().GetUtilityFunctionForLanguage(
1668 shared_class_expression.c_str(), eLanguageTypeObjC,
1669 g_get_shared_cache_class_info_name, error));
1672 "Failed to get Utility function for implementation lookup: %s.",
1674 m_get_shared_cache_class_info_code.reset();
1676 diagnostics.Clear();
1678 if (!m_get_shared_cache_class_info_code->Install(diagnostics, exe_ctx)) {
1680 LLDB_LOGF(log, "Failed to install implementation lookup.");
1681 diagnostics.Dump(log);
1683 m_get_shared_cache_class_info_code.reset();
1687 if (!m_get_shared_cache_class_info_code)
1688 return DescriptorMapUpdateResult::Fail();
1690 // Next make the function caller for our implementation utility function.
1692 value.SetValueType(Value::eValueTypeScalar);
1693 value.SetCompilerType(clang_void_pointer_type);
1694 arguments.PushValue(value);
1695 arguments.PushValue(value);
1697 value.SetValueType(Value::eValueTypeScalar);
1698 value.SetCompilerType(clang_uint32_t_type);
1699 arguments.PushValue(value);
1700 arguments.PushValue(value);
1702 get_shared_cache_class_info_function =
1703 m_get_shared_cache_class_info_code->MakeFunctionCaller(
1704 clang_uint32_t_type, arguments, thread_sp, error);
1706 if (get_shared_cache_class_info_function == nullptr)
1707 return DescriptorMapUpdateResult::Fail();
1710 get_shared_cache_class_info_function =
1711 m_get_shared_cache_class_info_code->GetFunctionCaller();
1712 if (get_shared_cache_class_info_function == nullptr)
1713 return DescriptorMapUpdateResult::Fail();
1714 arguments = get_shared_cache_class_info_function->GetArgumentValues();
1717 diagnostics.Clear();
1719 const uint32_t class_info_byte_size = addr_size + 4;
1720 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1721 lldb::addr_t class_infos_addr = process->AllocateMemory(
1722 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
1724 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
1726 "unable to allocate %" PRIu32
1727 " bytes in process for shared cache read",
1728 class_infos_byte_size);
1729 return DescriptorMapUpdateResult::Fail();
1732 std::lock_guard<std::mutex> guard(m_get_shared_cache_class_info_args_mutex);
1734 // Fill in our function argument values
1735 arguments.GetValueAtIndex(0)->GetScalar() = objc_opt_ptr;
1736 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1737 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1738 // Only dump the runtime classes from the expression evaluation if the log is
1740 Log *type_log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES);
1741 bool dump_log = type_log && type_log->GetVerbose();
1743 arguments.GetValueAtIndex(3)->GetScalar() = dump_log ? 1 : 0;
1745 bool success = false;
1747 diagnostics.Clear();
1749 // Write our function arguments into the process so we can run our function
1750 if (get_shared_cache_class_info_function->WriteFunctionArguments(
1751 exe_ctx, m_get_shared_cache_class_info_args, arguments,
1753 EvaluateExpressionOptions options;
1754 options.SetUnwindOnError(true);
1755 options.SetTryAllThreads(false);
1756 options.SetStopOthers(true);
1757 options.SetIgnoreBreakpoints(true);
1758 options.SetTimeout(process->GetUtilityExpressionTimeout());
1759 options.SetIsForUtilityExpr(true);
1762 return_value.SetValueType(Value::eValueTypeScalar);
1763 return_value.SetCompilerType(clang_uint32_t_type);
1764 return_value.GetScalar() = 0;
1766 diagnostics.Clear();
1769 ExpressionResults results =
1770 get_shared_cache_class_info_function->ExecuteFunction(
1771 exe_ctx, &m_get_shared_cache_class_info_args, options, diagnostics,
1774 if (results == eExpressionCompleted) {
1775 // The result is the number of ClassInfo structures that were filled in
1776 num_class_infos = return_value.GetScalar().ULong();
1777 LLDB_LOGF(log, "Discovered %u ObjC classes in shared cache\n",
1779 assert(num_class_infos <= num_classes);
1780 if (num_class_infos > 0) {
1781 if (num_class_infos > num_classes) {
1782 num_class_infos = num_classes;
1789 // Read the ClassInfo structures
1790 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
1791 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(),
1792 buffer.GetByteSize(),
1793 err) == buffer.GetByteSize()) {
1794 DataExtractor class_infos_data(buffer.GetBytes(),
1795 buffer.GetByteSize(),
1796 process->GetByteOrder(), addr_size);
1798 ParseClassInfoArray(class_infos_data, num_class_infos);
1805 LLDB_LOGF(log, "Error evaluating our find class name function.");
1806 diagnostics.Dump(log);
1811 LLDB_LOGF(log, "Error writing function arguments.");
1812 diagnostics.Dump(log);
1816 // Deallocate the memory we allocated for the ClassInfo array
1817 process->DeallocateMemory(class_infos_addr);
1819 return DescriptorMapUpdateResult(success, num_class_infos);
1822 bool AppleObjCRuntimeV2::UpdateISAToDescriptorMapFromMemory(
1823 RemoteNXMapTable &hash_table) {
1824 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1826 Process *process = GetProcess();
1828 if (process == nullptr)
1831 uint32_t num_map_table_isas = 0;
1833 ModuleSP objc_module_sp(GetObjCModule());
1835 if (objc_module_sp) {
1836 for (RemoteNXMapTable::element elt : hash_table) {
1837 ++num_map_table_isas;
1839 if (ISAIsCached(elt.second))
1842 ClassDescriptorSP descriptor_sp = ClassDescriptorSP(
1843 new ClassDescriptorV2(*this, elt.second, elt.first.AsCString()));
1845 if (log && log->GetVerbose())
1847 "AppleObjCRuntimeV2 added (ObjCISA)0x%" PRIx64
1848 " (%s) from dynamic table to isa->descriptor cache",
1849 elt.second, elt.first.AsCString());
1851 AddClass(elt.second, descriptor_sp, elt.first.AsCString());
1855 return num_map_table_isas > 0;
1858 lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheReadOnlyAddress() {
1859 Process *process = GetProcess();
1862 ModuleSP objc_module_sp(GetObjCModule());
1864 if (objc_module_sp) {
1865 ObjectFile *objc_object = objc_module_sp->GetObjectFile();
1868 SectionList *section_list = objc_module_sp->GetSectionList();
1871 SectionSP text_segment_sp(
1872 section_list->FindSectionByName(ConstString("__TEXT")));
1874 if (text_segment_sp) {
1875 SectionSP objc_opt_section_sp(
1876 text_segment_sp->GetChildren().FindSectionByName(
1877 ConstString("__objc_opt_ro")));
1879 if (objc_opt_section_sp) {
1880 return objc_opt_section_sp->GetLoadBaseAddress(
1881 &process->GetTarget());
1888 return LLDB_INVALID_ADDRESS;
1891 void AppleObjCRuntimeV2::UpdateISAToDescriptorMapIfNeeded() {
1892 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1894 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1895 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1897 // Else we need to check with our process to see when the map was updated.
1898 Process *process = GetProcess();
1901 RemoteNXMapTable hash_table;
1903 // Update the process stop ID that indicates the last time we updated the
1904 // map, whether it was successful or not.
1905 m_isa_to_descriptor_stop_id = process->GetStopID();
1907 if (!m_hash_signature.NeedsUpdate(process, this, hash_table))
1910 m_hash_signature.UpdateSignature(hash_table);
1912 // Grab the dynamically loaded objc classes from the hash table in memory
1913 DescriptorMapUpdateResult dynamic_update_result =
1914 UpdateISAToDescriptorMapDynamic(hash_table);
1916 // Now get the objc classes that are baked into the Objective-C runtime in
1917 // the shared cache, but only once per process as this data never changes
1918 if (!m_loaded_objc_opt) {
1919 // it is legitimately possible for the shared cache to be empty - in that
1920 // case, the dynamic hash table will contain all the class information we
1921 // need; the situation we're trying to detect is one where we aren't
1922 // seeing class information from the runtime - in order to detect that
1923 // vs. just the shared cache being empty or sparsely populated, we set an
1924 // arbitrary (very low) threshold for the number of classes that we want
1925 // to see in a "good" scenario - anything below that is suspicious
1926 // (Foundation alone has thousands of classes)
1927 const uint32_t num_classes_to_warn_at = 500;
1929 DescriptorMapUpdateResult shared_cache_update_result =
1930 UpdateISAToDescriptorMapSharedCache();
1933 "attempted to read objc class data - results: "
1934 "[dynamic_update]: ran: %s, count: %" PRIu32
1935 " [shared_cache_update]: ran: %s, count: %" PRIu32,
1936 dynamic_update_result.m_update_ran ? "yes" : "no",
1937 dynamic_update_result.m_num_found,
1938 shared_cache_update_result.m_update_ran ? "yes" : "no",
1939 shared_cache_update_result.m_num_found);
1942 // - we could not run either expression
1943 // - we found fewer than num_classes_to_warn_at classes total
1944 if ((!shared_cache_update_result.m_update_ran) ||
1945 (!dynamic_update_result.m_update_ran))
1946 WarnIfNoClassesCached(
1947 SharedCacheWarningReason::eExpressionExecutionFailure);
1948 else if (dynamic_update_result.m_num_found +
1949 shared_cache_update_result.m_num_found <
1950 num_classes_to_warn_at)
1951 WarnIfNoClassesCached(SharedCacheWarningReason::eNotEnoughClassesRead);
1953 m_loaded_objc_opt = true;
1956 m_isa_to_descriptor_stop_id = UINT32_MAX;
1960 static bool DoesProcessHaveSharedCache(Process &process) {
1961 PlatformSP platform_sp = process.GetTarget().GetPlatform();
1963 return true; // this should not happen
1965 ConstString platform_plugin_name = platform_sp->GetPluginName();
1966 if (platform_plugin_name) {
1967 llvm::StringRef platform_plugin_name_sr =
1968 platform_plugin_name.GetStringRef();
1969 if (platform_plugin_name_sr.endswith("-simulator"))
1976 void AppleObjCRuntimeV2::WarnIfNoClassesCached(
1977 SharedCacheWarningReason reason) {
1978 if (m_noclasses_warning_emitted)
1981 if (GetProcess() && !DoesProcessHaveSharedCache(*GetProcess())) {
1982 // Simulators do not have the objc_opt_ro class table so don't actually
1983 // complain to the user
1984 m_noclasses_warning_emitted = true;
1988 Debugger &debugger(GetProcess()->GetTarget().GetDebugger());
1989 if (auto stream = debugger.GetAsyncOutputStream()) {
1991 case SharedCacheWarningReason::eNotEnoughClassesRead:
1992 stream->PutCString("warning: could not find Objective-C class data in "
1993 "the process. This may reduce the quality of type "
1994 "information available.\n");
1995 m_noclasses_warning_emitted = true;
1997 case SharedCacheWarningReason::eExpressionExecutionFailure:
1998 stream->PutCString("warning: could not execute support code to read "
1999 "Objective-C class data in the process. This may "
2000 "reduce the quality of type information available.\n");
2001 m_noclasses_warning_emitted = true;
2007 DeclVendor *AppleObjCRuntimeV2::GetDeclVendor() {
2008 if (!m_decl_vendor_up)
2009 m_decl_vendor_up = std::make_unique<AppleObjCDeclVendor>(*this);
2011 return m_decl_vendor_up.get();
2014 lldb::addr_t AppleObjCRuntimeV2::LookupRuntimeSymbol(ConstString name) {
2015 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
2017 const char *name_cstr = name.AsCString();
2020 llvm::StringRef name_strref(name_cstr);
2022 llvm::StringRef ivar_prefix("OBJC_IVAR_$_");
2023 llvm::StringRef class_prefix("OBJC_CLASS_$_");
2025 if (name_strref.startswith(ivar_prefix)) {
2026 llvm::StringRef ivar_skipped_prefix =
2027 name_strref.substr(ivar_prefix.size());
2028 std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar =
2029 ivar_skipped_prefix.split('.');
2031 if (class_and_ivar.first.size() && class_and_ivar.second.size()) {
2032 const ConstString class_name_cs(class_and_ivar.first);
2033 ClassDescriptorSP descriptor =
2034 ObjCLanguageRuntime::GetClassDescriptorFromClassName(class_name_cs);
2037 const ConstString ivar_name_cs(class_and_ivar.second);
2038 const char *ivar_name_cstr = ivar_name_cs.AsCString();
2040 auto ivar_func = [&ret, ivar_name_cstr](
2041 const char *name, const char *type, lldb::addr_t offset_addr,
2042 uint64_t size) -> lldb::addr_t {
2043 if (!strcmp(name, ivar_name_cstr)) {
2050 descriptor->Describe(
2051 std::function<void(ObjCISA)>(nullptr),
2052 std::function<bool(const char *, const char *)>(nullptr),
2053 std::function<bool(const char *, const char *)>(nullptr),
2057 } else if (name_strref.startswith(class_prefix)) {
2058 llvm::StringRef class_skipped_prefix =
2059 name_strref.substr(class_prefix.size());
2060 const ConstString class_name_cs(class_skipped_prefix);
2061 ClassDescriptorSP descriptor =
2062 GetClassDescriptorFromClassName(class_name_cs);
2065 ret = descriptor->GetISA();
2072 AppleObjCRuntimeV2::NonPointerISACache *
2073 AppleObjCRuntimeV2::NonPointerISACache::CreateInstance(
2074 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2075 Process *process(runtime.GetProcess());
2079 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
2081 auto objc_debug_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2082 process, ConstString("objc_debug_isa_magic_mask"), objc_module_sp, error);
2086 auto objc_debug_isa_magic_value = ExtractRuntimeGlobalSymbol(
2087 process, ConstString("objc_debug_isa_magic_value"), objc_module_sp,
2092 auto objc_debug_isa_class_mask = ExtractRuntimeGlobalSymbol(
2093 process, ConstString("objc_debug_isa_class_mask"), objc_module_sp, error);
2098 log->PutCString("AOCRT::NPI: Found all the non-indexed ISA masks");
2100 bool foundError = false;
2101 auto objc_debug_indexed_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2102 process, ConstString("objc_debug_indexed_isa_magic_mask"), objc_module_sp,
2104 foundError |= error.Fail();
2106 auto objc_debug_indexed_isa_magic_value = ExtractRuntimeGlobalSymbol(
2107 process, ConstString("objc_debug_indexed_isa_magic_value"),
2108 objc_module_sp, error);
2109 foundError |= error.Fail();
2111 auto objc_debug_indexed_isa_index_mask = ExtractRuntimeGlobalSymbol(
2112 process, ConstString("objc_debug_indexed_isa_index_mask"), objc_module_sp,
2114 foundError |= error.Fail();
2116 auto objc_debug_indexed_isa_index_shift = ExtractRuntimeGlobalSymbol(
2117 process, ConstString("objc_debug_indexed_isa_index_shift"),
2118 objc_module_sp, error);
2119 foundError |= error.Fail();
2121 auto objc_indexed_classes =
2122 ExtractRuntimeGlobalSymbol(process, ConstString("objc_indexed_classes"),
2123 objc_module_sp, error, false);
2124 foundError |= error.Fail();
2127 log->PutCString("AOCRT::NPI: Found all the indexed ISA masks");
2129 // we might want to have some rules to outlaw these other values (e.g if the
2130 // mask is zero but the value is non-zero, ...)
2132 return new NonPointerISACache(
2133 runtime, objc_module_sp, objc_debug_isa_class_mask,
2134 objc_debug_isa_magic_mask, objc_debug_isa_magic_value,
2135 objc_debug_indexed_isa_magic_mask, objc_debug_indexed_isa_magic_value,
2136 objc_debug_indexed_isa_index_mask, objc_debug_indexed_isa_index_shift,
2137 foundError ? 0 : objc_indexed_classes);
2140 AppleObjCRuntimeV2::TaggedPointerVendorV2 *
2141 AppleObjCRuntimeV2::TaggedPointerVendorV2::CreateInstance(
2142 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2143 Process *process(runtime.GetProcess());
2147 auto objc_debug_taggedpointer_mask = ExtractRuntimeGlobalSymbol(
2148 process, ConstString("objc_debug_taggedpointer_mask"), objc_module_sp,
2151 return new TaggedPointerVendorLegacy(runtime);
2153 auto objc_debug_taggedpointer_slot_shift = ExtractRuntimeGlobalSymbol(
2154 process, ConstString("objc_debug_taggedpointer_slot_shift"),
2155 objc_module_sp, error, true, 4);
2157 return new TaggedPointerVendorLegacy(runtime);
2159 auto objc_debug_taggedpointer_slot_mask = ExtractRuntimeGlobalSymbol(
2160 process, ConstString("objc_debug_taggedpointer_slot_mask"),
2161 objc_module_sp, error, true, 4);
2163 return new TaggedPointerVendorLegacy(runtime);
2165 auto objc_debug_taggedpointer_payload_lshift = ExtractRuntimeGlobalSymbol(
2166 process, ConstString("objc_debug_taggedpointer_payload_lshift"),
2167 objc_module_sp, error, true, 4);
2169 return new TaggedPointerVendorLegacy(runtime);
2171 auto objc_debug_taggedpointer_payload_rshift = ExtractRuntimeGlobalSymbol(
2172 process, ConstString("objc_debug_taggedpointer_payload_rshift"),
2173 objc_module_sp, error, true, 4);
2175 return new TaggedPointerVendorLegacy(runtime);
2177 auto objc_debug_taggedpointer_classes = ExtractRuntimeGlobalSymbol(
2178 process, ConstString("objc_debug_taggedpointer_classes"), objc_module_sp,
2181 return new TaggedPointerVendorLegacy(runtime);
2183 // try to detect the "extended tagged pointer" variables - if any are
2184 // missing, use the non-extended vendor
2186 auto objc_debug_taggedpointer_ext_mask = ExtractRuntimeGlobalSymbol(
2187 process, ConstString("objc_debug_taggedpointer_ext_mask"),
2188 objc_module_sp, error);
2192 auto objc_debug_taggedpointer_ext_slot_shift = ExtractRuntimeGlobalSymbol(
2193 process, ConstString("objc_debug_taggedpointer_ext_slot_shift"),
2194 objc_module_sp, error, true, 4);
2198 auto objc_debug_taggedpointer_ext_slot_mask = ExtractRuntimeGlobalSymbol(
2199 process, ConstString("objc_debug_taggedpointer_ext_slot_mask"),
2200 objc_module_sp, error, true, 4);
2204 auto objc_debug_taggedpointer_ext_classes = ExtractRuntimeGlobalSymbol(
2205 process, ConstString("objc_debug_taggedpointer_ext_classes"),
2206 objc_module_sp, error, false);
2210 auto objc_debug_taggedpointer_ext_payload_lshift =
2211 ExtractRuntimeGlobalSymbol(
2212 process, ConstString("objc_debug_taggedpointer_ext_payload_lshift"),
2213 objc_module_sp, error, true, 4);
2217 auto objc_debug_taggedpointer_ext_payload_rshift =
2218 ExtractRuntimeGlobalSymbol(
2219 process, ConstString("objc_debug_taggedpointer_ext_payload_rshift"),
2220 objc_module_sp, error, true, 4);
2224 return new TaggedPointerVendorExtended(
2225 runtime, objc_debug_taggedpointer_mask,
2226 objc_debug_taggedpointer_ext_mask, objc_debug_taggedpointer_slot_shift,
2227 objc_debug_taggedpointer_ext_slot_shift,
2228 objc_debug_taggedpointer_slot_mask,
2229 objc_debug_taggedpointer_ext_slot_mask,
2230 objc_debug_taggedpointer_payload_lshift,
2231 objc_debug_taggedpointer_payload_rshift,
2232 objc_debug_taggedpointer_ext_payload_lshift,
2233 objc_debug_taggedpointer_ext_payload_rshift,
2234 objc_debug_taggedpointer_classes, objc_debug_taggedpointer_ext_classes);
2237 // we might want to have some rules to outlaw these values (e.g if the
2238 // table's address is zero)
2240 return new TaggedPointerVendorRuntimeAssisted(
2241 runtime, objc_debug_taggedpointer_mask,
2242 objc_debug_taggedpointer_slot_shift, objc_debug_taggedpointer_slot_mask,
2243 objc_debug_taggedpointer_payload_lshift,
2244 objc_debug_taggedpointer_payload_rshift,
2245 objc_debug_taggedpointer_classes);
2248 bool AppleObjCRuntimeV2::TaggedPointerVendorLegacy::IsPossibleTaggedPointer(
2253 ObjCLanguageRuntime::ClassDescriptorSP
2254 AppleObjCRuntimeV2::TaggedPointerVendorLegacy::GetClassDescriptor(
2256 if (!IsPossibleTaggedPointer(ptr))
2257 return ObjCLanguageRuntime::ClassDescriptorSP();
2259 uint32_t foundation_version = m_runtime.GetFoundationVersion();
2261 if (foundation_version == LLDB_INVALID_MODULE_VERSION)
2262 return ObjCLanguageRuntime::ClassDescriptorSP();
2264 uint64_t class_bits = (ptr & 0xE) >> 1;
2267 static ConstString g_NSAtom("NSAtom");
2268 static ConstString g_NSNumber("NSNumber");
2269 static ConstString g_NSDateTS("NSDateTS");
2270 static ConstString g_NSManagedObject("NSManagedObject");
2271 static ConstString g_NSDate("NSDate");
2273 if (foundation_version >= 900) {
2274 switch (class_bits) {
2285 name = g_NSManagedObject;
2291 return ObjCLanguageRuntime::ClassDescriptorSP();
2294 switch (class_bits) {
2299 name = g_NSManagedObject;
2308 return ObjCLanguageRuntime::ClassDescriptorSP();
2312 lldb::addr_t unobfuscated = ptr ^ m_runtime.GetTaggedPointerObfuscator();
2313 return ClassDescriptorSP(new ClassDescriptorV2Tagged(name, unobfuscated));
2316 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
2317 TaggedPointerVendorRuntimeAssisted(
2318 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
2319 uint32_t objc_debug_taggedpointer_slot_shift,
2320 uint32_t objc_debug_taggedpointer_slot_mask,
2321 uint32_t objc_debug_taggedpointer_payload_lshift,
2322 uint32_t objc_debug_taggedpointer_payload_rshift,
2323 lldb::addr_t objc_debug_taggedpointer_classes)
2324 : TaggedPointerVendorV2(runtime), m_cache(),
2325 m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask),
2326 m_objc_debug_taggedpointer_slot_shift(
2327 objc_debug_taggedpointer_slot_shift),
2328 m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask),
2329 m_objc_debug_taggedpointer_payload_lshift(
2330 objc_debug_taggedpointer_payload_lshift),
2331 m_objc_debug_taggedpointer_payload_rshift(
2332 objc_debug_taggedpointer_payload_rshift),
2333 m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes) {}
2335 bool AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
2336 IsPossibleTaggedPointer(lldb::addr_t ptr) {
2337 return (ptr & m_objc_debug_taggedpointer_mask) != 0;
2340 ObjCLanguageRuntime::ClassDescriptorSP
2341 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(
2343 ClassDescriptorSP actual_class_descriptor_sp;
2344 uint64_t data_payload;
2345 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
2347 if (!IsPossibleTaggedPointer(unobfuscated))
2348 return ObjCLanguageRuntime::ClassDescriptorSP();
2350 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) &
2351 m_objc_debug_taggedpointer_slot_mask;
2353 CacheIterator iterator = m_cache.find(slot), end = m_cache.end();
2354 if (iterator != end) {
2355 actual_class_descriptor_sp = iterator->second;
2357 Process *process(m_runtime.GetProcess());
2358 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
2359 m_objc_debug_taggedpointer_classes;
2361 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
2362 if (error.Fail() || slot_data == 0 ||
2363 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
2365 actual_class_descriptor_sp =
2366 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
2367 if (!actual_class_descriptor_sp)
2368 return ObjCLanguageRuntime::ClassDescriptorSP();
2369 m_cache[slot] = actual_class_descriptor_sp;
2373 (((uint64_t)unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
2374 m_objc_debug_taggedpointer_payload_rshift);
2376 return ClassDescriptorSP(
2377 new ClassDescriptorV2Tagged(actual_class_descriptor_sp, data_payload));
2380 AppleObjCRuntimeV2::TaggedPointerVendorExtended::TaggedPointerVendorExtended(
2381 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
2382 uint64_t objc_debug_taggedpointer_ext_mask,
2383 uint32_t objc_debug_taggedpointer_slot_shift,
2384 uint32_t objc_debug_taggedpointer_ext_slot_shift,
2385 uint32_t objc_debug_taggedpointer_slot_mask,
2386 uint32_t objc_debug_taggedpointer_ext_slot_mask,
2387 uint32_t objc_debug_taggedpointer_payload_lshift,
2388 uint32_t objc_debug_taggedpointer_payload_rshift,
2389 uint32_t objc_debug_taggedpointer_ext_payload_lshift,
2390 uint32_t objc_debug_taggedpointer_ext_payload_rshift,
2391 lldb::addr_t objc_debug_taggedpointer_classes,
2392 lldb::addr_t objc_debug_taggedpointer_ext_classes)
2393 : TaggedPointerVendorRuntimeAssisted(
2394 runtime, objc_debug_taggedpointer_mask,
2395 objc_debug_taggedpointer_slot_shift,
2396 objc_debug_taggedpointer_slot_mask,
2397 objc_debug_taggedpointer_payload_lshift,
2398 objc_debug_taggedpointer_payload_rshift,
2399 objc_debug_taggedpointer_classes),
2401 m_objc_debug_taggedpointer_ext_mask(objc_debug_taggedpointer_ext_mask),
2402 m_objc_debug_taggedpointer_ext_slot_shift(
2403 objc_debug_taggedpointer_ext_slot_shift),
2404 m_objc_debug_taggedpointer_ext_slot_mask(
2405 objc_debug_taggedpointer_ext_slot_mask),
2406 m_objc_debug_taggedpointer_ext_payload_lshift(
2407 objc_debug_taggedpointer_ext_payload_lshift),
2408 m_objc_debug_taggedpointer_ext_payload_rshift(
2409 objc_debug_taggedpointer_ext_payload_rshift),
2410 m_objc_debug_taggedpointer_ext_classes(
2411 objc_debug_taggedpointer_ext_classes) {}
2413 bool AppleObjCRuntimeV2::TaggedPointerVendorExtended::
2414 IsPossibleExtendedTaggedPointer(lldb::addr_t ptr) {
2415 if (!IsPossibleTaggedPointer(ptr))
2418 if (m_objc_debug_taggedpointer_ext_mask == 0)
2421 return ((ptr & m_objc_debug_taggedpointer_ext_mask) ==
2422 m_objc_debug_taggedpointer_ext_mask);
2425 ObjCLanguageRuntime::ClassDescriptorSP
2426 AppleObjCRuntimeV2::TaggedPointerVendorExtended::GetClassDescriptor(
2428 ClassDescriptorSP actual_class_descriptor_sp;
2429 uint64_t data_payload;
2430 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
2432 if (!IsPossibleTaggedPointer(unobfuscated))
2433 return ObjCLanguageRuntime::ClassDescriptorSP();
2435 if (!IsPossibleExtendedTaggedPointer(unobfuscated))
2436 return this->TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(ptr);
2438 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_ext_slot_shift) &
2439 m_objc_debug_taggedpointer_ext_slot_mask;
2441 CacheIterator iterator = m_ext_cache.find(slot), end = m_ext_cache.end();
2442 if (iterator != end) {
2443 actual_class_descriptor_sp = iterator->second;
2445 Process *process(m_runtime.GetProcess());
2446 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
2447 m_objc_debug_taggedpointer_ext_classes;
2449 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
2450 if (error.Fail() || slot_data == 0 ||
2451 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
2453 actual_class_descriptor_sp =
2454 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
2455 if (!actual_class_descriptor_sp)
2456 return ObjCLanguageRuntime::ClassDescriptorSP();
2457 m_ext_cache[slot] = actual_class_descriptor_sp;
2461 (((uint64_t)unobfuscated << m_objc_debug_taggedpointer_ext_payload_lshift) >>
2462 m_objc_debug_taggedpointer_ext_payload_rshift);
2464 return ClassDescriptorSP(
2465 new ClassDescriptorV2Tagged(actual_class_descriptor_sp, data_payload));
2468 AppleObjCRuntimeV2::NonPointerISACache::NonPointerISACache(
2469 AppleObjCRuntimeV2 &runtime, const ModuleSP &objc_module_sp,
2470 uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask,
2471 uint64_t objc_debug_isa_magic_value,
2472 uint64_t objc_debug_indexed_isa_magic_mask,
2473 uint64_t objc_debug_indexed_isa_magic_value,
2474 uint64_t objc_debug_indexed_isa_index_mask,
2475 uint64_t objc_debug_indexed_isa_index_shift,
2476 lldb::addr_t objc_indexed_classes)
2477 : m_runtime(runtime), m_cache(), m_objc_module_wp(objc_module_sp),
2478 m_objc_debug_isa_class_mask(objc_debug_isa_class_mask),
2479 m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask),
2480 m_objc_debug_isa_magic_value(objc_debug_isa_magic_value),
2481 m_objc_debug_indexed_isa_magic_mask(objc_debug_indexed_isa_magic_mask),
2482 m_objc_debug_indexed_isa_magic_value(objc_debug_indexed_isa_magic_value),
2483 m_objc_debug_indexed_isa_index_mask(objc_debug_indexed_isa_index_mask),
2484 m_objc_debug_indexed_isa_index_shift(objc_debug_indexed_isa_index_shift),
2485 m_objc_indexed_classes(objc_indexed_classes), m_indexed_isa_cache() {}
2487 ObjCLanguageRuntime::ClassDescriptorSP
2488 AppleObjCRuntimeV2::NonPointerISACache::GetClassDescriptor(ObjCISA isa) {
2489 ObjCISA real_isa = 0;
2490 if (!EvaluateNonPointerISA(isa, real_isa))
2491 return ObjCLanguageRuntime::ClassDescriptorSP();
2492 auto cache_iter = m_cache.find(real_isa);
2493 if (cache_iter != m_cache.end())
2494 return cache_iter->second;
2495 auto descriptor_sp =
2496 m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(real_isa);
2497 if (descriptor_sp) // cache only positive matches since the table might grow
2498 m_cache[real_isa] = descriptor_sp;
2499 return descriptor_sp;
2502 bool AppleObjCRuntimeV2::NonPointerISACache::EvaluateNonPointerISA(
2503 ObjCISA isa, ObjCISA &ret_isa) {
2504 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
2506 LLDB_LOGF(log, "AOCRT::NPI Evaluate(isa = 0x%" PRIx64 ")", (uint64_t)isa);
2508 if ((isa & ~m_objc_debug_isa_class_mask) == 0)
2511 // If all of the indexed ISA variables are set, then its possible that this
2512 // ISA is indexed, and we should first try to get its value using the index.
2513 // Note, we check these variables first as the ObjC runtime will set at least
2514 // one of their values to 0 if they aren't needed.
2515 if (m_objc_debug_indexed_isa_magic_mask &&
2516 m_objc_debug_indexed_isa_magic_value &&
2517 m_objc_debug_indexed_isa_index_mask &&
2518 m_objc_debug_indexed_isa_index_shift && m_objc_indexed_classes) {
2519 if ((isa & ~m_objc_debug_indexed_isa_index_mask) == 0)
2522 if ((isa & m_objc_debug_indexed_isa_magic_mask) ==
2523 m_objc_debug_indexed_isa_magic_value) {
2524 // Magic bits are correct, so try extract the index.
2525 uintptr_t index = (isa & m_objc_debug_indexed_isa_index_mask) >>
2526 m_objc_debug_indexed_isa_index_shift;
2527 // If the index is out of bounds of the length of the array then check if
2528 // the array has been updated. If that is the case then we should try
2529 // read the count again, and update the cache if the count has been
2531 if (index > m_indexed_isa_cache.size()) {
2533 "AOCRT::NPI (index = %" PRIu64
2534 ") exceeds cache (size = %" PRIu64 ")",
2535 (uint64_t)index, (uint64_t)m_indexed_isa_cache.size());
2537 Process *process(m_runtime.GetProcess());
2539 ModuleSP objc_module_sp(m_objc_module_wp.lock());
2540 if (!objc_module_sp)
2544 auto objc_indexed_classes_count = ExtractRuntimeGlobalSymbol(
2545 process, ConstString("objc_indexed_classes_count"), objc_module_sp,
2550 LLDB_LOGF(log, "AOCRT::NPI (new class count = %" PRIu64 ")",
2551 (uint64_t)objc_indexed_classes_count);
2553 if (objc_indexed_classes_count > m_indexed_isa_cache.size()) {
2554 // Read the class entries we don't have. We should just read all of
2555 // them instead of just the one we need as then we can cache those we
2557 auto num_new_classes =
2558 objc_indexed_classes_count - m_indexed_isa_cache.size();
2559 const uint32_t addr_size = process->GetAddressByteSize();
2560 DataBufferHeap buffer(num_new_classes * addr_size, 0);
2562 lldb::addr_t last_read_class =
2563 m_objc_indexed_classes + (m_indexed_isa_cache.size() * addr_size);
2564 size_t bytes_read = process->ReadMemory(
2565 last_read_class, buffer.GetBytes(), buffer.GetByteSize(), error);
2566 if (error.Fail() || bytes_read != buffer.GetByteSize())
2569 LLDB_LOGF(log, "AOCRT::NPI (read new classes count = %" PRIu64 ")",
2570 (uint64_t)num_new_classes);
2572 // Append the new entries to the existing cache.
2573 DataExtractor data(buffer.GetBytes(), buffer.GetByteSize(),
2574 process->GetByteOrder(),
2575 process->GetAddressByteSize());
2577 lldb::offset_t offset = 0;
2578 for (unsigned i = 0; i != num_new_classes; ++i)
2579 m_indexed_isa_cache.push_back(data.GetAddress(&offset));
2583 // If the index is still out of range then this isn't a pointer.
2584 if (index > m_indexed_isa_cache.size())
2587 LLDB_LOGF(log, "AOCRT::NPI Evaluate(ret_isa = 0x%" PRIx64 ")",
2588 (uint64_t)m_indexed_isa_cache[index]);
2590 ret_isa = m_indexed_isa_cache[index];
2591 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
2597 // Definitely not an indexed ISA, so try to use a mask to extract the pointer
2599 if ((isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value) {
2600 ret_isa = isa & m_objc_debug_isa_class_mask;
2601 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
2606 ObjCLanguageRuntime::EncodingToTypeSP AppleObjCRuntimeV2::GetEncodingToType() {
2607 if (!m_encoding_to_type_sp)
2608 m_encoding_to_type_sp =
2609 std::make_shared<AppleObjCTypeEncodingParser>(*this);
2610 return m_encoding_to_type_sp;
2613 lldb_private::AppleObjCRuntime::ObjCISA
2614 AppleObjCRuntimeV2::GetPointerISA(ObjCISA isa) {
2617 if (m_non_pointer_isa_cache_up)
2618 m_non_pointer_isa_cache_up->EvaluateNonPointerISA(isa, ret);
2623 bool AppleObjCRuntimeV2::GetCFBooleanValuesIfNeeded() {
2624 if (m_CFBoolean_values)
2627 static ConstString g_kCFBooleanFalse("__kCFBooleanFalse");
2628 static ConstString g_kCFBooleanTrue("__kCFBooleanTrue");
2630 std::function<lldb::addr_t(ConstString)> get_symbol =
2631 [this](ConstString sym) -> lldb::addr_t {
2632 SymbolContextList sc_list;
2633 GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
2634 sym, lldb::eSymbolTypeData, sc_list);
2635 if (sc_list.GetSize() == 1) {
2637 sc_list.GetContextAtIndex(0, sc);
2639 return sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
2642 return LLDB_INVALID_ADDRESS;
2645 lldb::addr_t false_addr = get_symbol(g_kCFBooleanFalse);
2646 lldb::addr_t true_addr = get_symbol(g_kCFBooleanTrue);
2648 return (m_CFBoolean_values = {false_addr, true_addr}).operator bool();
2651 void AppleObjCRuntimeV2::GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true,
2652 lldb::addr_t &cf_false) {
2653 if (GetCFBooleanValuesIfNeeded()) {
2654 cf_true = m_CFBoolean_values->second;
2655 cf_false = m_CFBoolean_values->first;
2657 this->AppleObjCRuntime::GetValuesForGlobalCFBooleans(cf_true, cf_false);
2660 #pragma mark Frame recognizers
2662 class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame {
2664 ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp) {
2665 ThreadSP thread_sp = frame_sp->GetThread();
2666 ProcessSP process_sp = thread_sp->GetProcess();
2668 const lldb::ABISP &abi = process_sp->GetABI();
2671 TypeSystemClang *clang_ast_context =
2672 TypeSystemClang::GetScratch(process_sp->GetTarget());
2673 if (!clang_ast_context)
2675 CompilerType voidstar =
2676 clang_ast_context->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
2680 input_value.SetCompilerType(voidstar);
2681 args.PushValue(input_value);
2683 if (!abi->GetArgumentValues(*thread_sp, args)) return;
2685 addr_t exception_addr = args.GetValueAtIndex(0)->GetScalar().ULongLong();
2687 Value value(exception_addr);
2688 value.SetCompilerType(voidstar);
2689 exception = ValueObjectConstResult::Create(frame_sp.get(), value,
2690 ConstString("exception"));
2691 exception = ValueObjectRecognizerSynthesizedValue::Create(
2692 *exception, eValueTypeVariableArgument);
2693 exception = exception->GetDynamicValue(eDynamicDontRunTarget);
2695 m_arguments = ValueObjectListSP(new ValueObjectList());
2696 m_arguments->Append(exception);
2698 m_stop_desc = "hit Objective-C exception";
2701 ValueObjectSP exception;
2703 lldb::ValueObjectSP GetExceptionObject() override { return exception; }
2706 class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer {
2707 lldb::RecognizedStackFrameSP
2708 RecognizeFrame(lldb::StackFrameSP frame) override {
2709 return lldb::RecognizedStackFrameSP(
2710 new ObjCExceptionRecognizedStackFrame(frame));
2714 static void RegisterObjCExceptionRecognizer() {
2715 static llvm::once_flag g_once_flag;
2716 llvm::call_once(g_once_flag, []() {
2718 ConstString function;
2719 std::tie(module, function) = AppleObjCRuntime::GetExceptionThrowLocation();
2720 std::vector<ConstString> symbols = {function};
2721 StackFrameRecognizerManager::AddRecognizer(
2722 StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
2723 module.GetFilename(), symbols,
2724 /*first_instruction_only*/ true);