1 //===-- AppleObjCRuntimeV2.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
17 // Other libraries and framework includes
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclObjC.h"
22 #include "lldb/lldb-enumerations.h"
23 #include "lldb/Core/ClangForward.h"
24 #include "lldb/Symbol/CompilerType.h"
26 #include "lldb/Core/ClangForward.h"
27 #include "lldb/Core/ConstString.h"
28 #include "lldb/Core/Debugger.h"
29 #include "lldb/Core/Error.h"
30 #include "lldb/Core/Log.h"
31 #include "lldb/Core/Module.h"
32 #include "lldb/Core/PluginManager.h"
33 #include "lldb/Core/Scalar.h"
34 #include "lldb/Core/Section.h"
35 #include "lldb/Core/Stream.h"
36 #include "lldb/Core/StreamString.h"
37 #include "lldb/Core/Timer.h"
38 #include "lldb/Core/ValueObjectVariable.h"
39 #include "lldb/Expression/FunctionCaller.h"
40 #include "lldb/Expression/UtilityFunction.h"
41 #include "lldb/Host/StringConvert.h"
42 #include "lldb/Interpreter/CommandObject.h"
43 #include "lldb/Interpreter/CommandObjectMultiword.h"
44 #include "lldb/Interpreter/CommandReturnObject.h"
45 #include "lldb/Symbol/ClangASTContext.h"
46 #include "lldb/Symbol/ObjectFile.h"
47 #include "lldb/Symbol/Symbol.h"
48 #include "lldb/Symbol/TypeList.h"
49 #include "lldb/Symbol/VariableList.h"
50 #include "lldb/Target/ExecutionContext.h"
51 #include "lldb/Target/Platform.h"
52 #include "lldb/Target/Process.h"
53 #include "lldb/Target/RegisterContext.h"
54 #include "lldb/Target/Target.h"
55 #include "lldb/Target/Thread.h"
57 #include "AppleObjCRuntimeV2.h"
58 #include "AppleObjCClassDescriptorV2.h"
59 #include "AppleObjCTypeEncodingParser.h"
60 #include "AppleObjCDeclVendor.h"
61 #include "AppleObjCTrampolineHandler.h"
63 #if defined(__APPLE__)
64 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
68 using namespace lldb_private;
70 // 2 second timeout when running utility functions
71 #define UTILITY_FUNCTION_TIMEOUT_USEC 2*1000*1000
73 static const char *g_get_dynamic_class_info_name = "__lldb_apple_objc_v2_get_dynamic_class_info";
74 // Testing using the new C++11 raw string literals. If this breaks GCC then we will
75 // need to revert to the code above...
76 static const char *g_get_dynamic_class_info_body = R"(
80 size_t strlen(const char *);
81 char *strncpy (char * s1, const char * s2, size_t n);
82 int printf(const char * format, ...);
84 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
85 #ifdef ENABLE_DEBUG_PRINTF
86 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
88 #define DEBUG_PRINTF(fmt, ...)
91 typedef struct _NXMapTable {
94 unsigned num_buckets_minus_one;
98 #define NX_MAPNOTAKEY ((void *)(-1))
100 typedef struct BucketInfo
102 const char *name_ptr;
110 } __attribute__((__packed__));
113 __lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr,
114 void *class_infos_ptr,
115 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 static const char *g_get_shared_cache_class_info_name = "__lldb_apple_objc_v2_get_shared_cache_class_info";
161 // Testing using the new C++11 raw string literals. If this breaks GCC then we will
162 // need to revert to the code above...
163 static const char *g_get_shared_cache_class_info_body = R"(
167 const char *class_getName(void *objc_class);
168 size_t strlen(const char *);
169 char *strncpy (char * s1, const char * s2, size_t n);
170 int printf(const char * format, ...);
173 // #define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
174 #ifdef ENABLE_DEBUG_PRINTF
175 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
177 #define DEBUG_PRINTF(fmt, ...)
181 struct objc_classheader_t {
186 struct objc_clsopt_t {
194 uint32_t scramble[256];
195 uint8_t tab[0]; // tab[mask+1]
196 // uint8_t checkbytes[capacity];
197 // int32_t offset[capacity];
198 // objc_classheader_t clsOffsets[capacity];
199 // uint32_t duplicateCount;
200 // objc_classheader_t duplicateOffsets[duplicateCount];
205 int32_t selopt_offset;
206 int32_t headeropt_offset;
207 int32_t clsopt_offset;
210 struct objc_opt_v14_t {
213 int32_t selopt_offset;
214 int32_t headeropt_offset;
215 int32_t clsopt_offset;
222 } __attribute__((__packed__));
225 __lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr,
226 void *class_infos_ptr,
227 uint32_t class_infos_byte_size)
230 DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr);
231 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
232 DEBUG_PRINTF ("class_infos_byte_size = %u (%" PRIu64 " class infos)\n", class_infos_byte_size, (size_t)(class_infos_byte_size/sizeof(ClassInfo)));
235 const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr;
236 const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr;
237 const bool is_v14_format = objc_opt->version >= 14;
240 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version);
241 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags);
242 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset);
243 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset);
244 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset);
248 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version);
249 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset);
250 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset);
251 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset);
253 if (objc_opt->version == 12 || objc_opt->version == 13 || objc_opt->version == 14)
255 const objc_clsopt_t* clsopt = NULL;
257 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset);
259 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset);
260 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
261 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
262 int32_t invalidEntryOffset = 0;
263 // this is safe to do because the version field order is invariant
264 if (objc_opt->version == 12)
265 invalidEntryOffset = 16;
266 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
267 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
268 const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity);
269 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
270 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
271 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
272 for (uint32_t i=0; i<clsopt->capacity; ++i)
274 const int32_t clsOffset = classOffsets[i].clsOffset;
276 continue; // duplicate
277 else if (clsOffset == invalidEntryOffset)
278 continue; // invalid offset
280 if (class_infos && idx < max_class_infos)
282 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
283 const char *name = class_getName (class_infos[idx].isa);
284 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
285 // Hash the class name so we don't have to read it
286 const char *s = name;
288 for (unsigned char c = *s; c; c = *++s)
289 h = ((h << 5) + h) + c;
290 class_infos[idx].hash = h;
295 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
296 const uint32_t duplicate_count = *duplicate_count_ptr;
297 const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]);
298 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
299 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
300 for (uint32_t i=0; i<duplicate_count; ++i)
302 const int32_t clsOffset = duplicateClassOffsets[i].clsOffset;
304 continue; // duplicate
305 else if (clsOffset == invalidEntryOffset)
306 continue; // invalid offset
308 if (class_infos && idx < max_class_infos)
310 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
311 const char *name = class_getName (class_infos[idx].isa);
312 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
313 // Hash the class name so we don't have to read it
314 const char *s = name;
316 for (unsigned char c = *s; c; c = *++s)
317 h = ((h << 5) + h) + c;
318 class_infos[idx].hash = h;
323 DEBUG_PRINTF ("%u class_infos\n", idx);
324 DEBUG_PRINTF ("done\n");
333 ExtractRuntimeGlobalSymbol (Process* process,
335 const ModuleSP &module_sp,
337 bool read_value = true,
338 uint8_t byte_size = 0,
339 uint64_t default_value = LLDB_INVALID_ADDRESS,
340 SymbolType sym_type = lldb::eSymbolTypeData)
344 error.SetErrorString("no process");
345 return default_value;
349 error.SetErrorString("no module");
350 return default_value;
353 byte_size = process->GetAddressByteSize();
354 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(name, lldb::eSymbolTypeData);
355 if (symbol && symbol->ValueIsAddress())
357 lldb::addr_t symbol_load_addr = symbol->GetAddressRef().GetLoadAddress(&process->GetTarget());
358 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
361 return process->ReadUnsignedIntegerFromMemory(symbol_load_addr, byte_size, default_value, error);
363 return symbol_load_addr;
367 error.SetErrorString("symbol address invalid");
368 return default_value;
373 error.SetErrorString("no symbol");
374 return default_value;
378 AppleObjCRuntimeV2::AppleObjCRuntimeV2 (Process *process,
379 const ModuleSP &objc_module_sp) :
380 AppleObjCRuntime (process),
381 m_get_class_info_code(),
382 m_get_class_info_args (LLDB_INVALID_ADDRESS),
383 m_get_class_info_args_mutex (Mutex::eMutexTypeNormal),
384 m_get_shared_cache_class_info_code(),
385 m_get_shared_cache_class_info_args (LLDB_INVALID_ADDRESS),
386 m_get_shared_cache_class_info_args_mutex (Mutex::eMutexTypeNormal),
388 m_isa_hash_table_ptr (LLDB_INVALID_ADDRESS),
390 m_has_object_getClass (false),
391 m_loaded_objc_opt (false),
392 m_non_pointer_isa_cache_ap(NonPointerISACache::CreateInstance(*this,objc_module_sp)),
393 m_tagged_pointer_vendor_ap(TaggedPointerVendorV2::CreateInstance(*this,objc_module_sp)),
394 m_encoding_to_type_sp(),
395 m_noclasses_warning_emitted(false)
397 static const ConstString g_gdb_object_getClass("gdb_object_getClass");
398 m_has_object_getClass = (objc_module_sp->FindFirstSymbolWithNameAndType(g_gdb_object_getClass, eSymbolTypeCode) != NULL);
402 AppleObjCRuntimeV2::GetDynamicTypeAndAddress (ValueObject &in_value,
403 DynamicValueType use_dynamic,
404 TypeAndOrName &class_type_or_name,
406 Value::ValueType &value_type)
408 // The Runtime is attached to a particular process, you shouldn't pass in a value from another process.
409 assert (in_value.GetProcessSP().get() == m_process);
410 assert (m_process != NULL);
412 class_type_or_name.Clear();
413 value_type = Value::ValueType::eValueTypeScalar;
415 // Make sure we can have a dynamic value before starting...
416 if (CouldHaveDynamicValue (in_value))
418 // First job, pull out the address at 0 offset from the object That will be the ISA pointer.
419 ClassDescriptorSP objc_class_sp (GetNonKVOClassDescriptor (in_value));
422 const addr_t object_ptr = in_value.GetPointerValue();
423 address.SetRawAddress(object_ptr);
425 ConstString class_name (objc_class_sp->GetClassName());
426 class_type_or_name.SetName(class_name);
427 TypeSP type_sp (objc_class_sp->GetType());
429 class_type_or_name.SetTypeSP (type_sp);
432 type_sp = LookupInCompleteClassCache (class_name);
435 objc_class_sp->SetType (type_sp);
436 class_type_or_name.SetTypeSP (type_sp);
440 // try to go for a CompilerType at least
441 DeclVendor* vendor = GetDeclVendor();
444 std::vector<clang::NamedDecl*> decls;
445 if (vendor->FindDecls(class_name, false, 1, decls) && decls.size())
446 class_type_or_name.SetCompilerType(ClangASTContext::GetTypeForDecl(decls[0]));
452 return class_type_or_name.IsEmpty() == false;
455 //------------------------------------------------------------------
457 //------------------------------------------------------------------
459 AppleObjCRuntimeV2::CreateInstance (Process *process, LanguageType language)
461 // FIXME: This should be a MacOS or iOS process, and we need to look for the OBJC section to make
462 // sure we aren't using the V1 runtime.
463 if (language == eLanguageTypeObjC)
465 ModuleSP objc_module_sp;
467 if (AppleObjCRuntime::GetObjCVersion (process, objc_module_sp) == ObjCRuntimeVersions::eAppleObjC_V2)
468 return new AppleObjCRuntimeV2 (process, objc_module_sp);
476 class CommandObjectObjC_ClassTable_Dump : public CommandObjectParsed
479 CommandObjectObjC_ClassTable_Dump (CommandInterpreter &interpreter) :
480 CommandObjectParsed (interpreter,
482 "Dump information on Objective-C classes known to the current process.",
483 "language objc class-table dump",
484 eCommandRequiresProcess |
485 eCommandProcessMustBeLaunched |
486 eCommandProcessMustBePaused )
490 ~CommandObjectObjC_ClassTable_Dump() override = default;
494 DoExecute(Args& command, CommandReturnObject &result) override
496 Process *process = m_exe_ctx.GetProcessPtr();
497 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
500 auto iterators_pair = objc_runtime->GetDescriptorIteratorPair();
501 auto iterator = iterators_pair.first;
502 for(; iterator != iterators_pair.second; iterator++)
504 result.GetOutputStream().Printf("isa = 0x%" PRIx64, iterator->first);
505 if (iterator->second)
507 result.GetOutputStream().Printf(" name = %s", iterator->second->GetClassName().AsCString("<unknown>"));
508 result.GetOutputStream().Printf(" instance size = %" PRIu64, iterator->second->GetInstanceSize());
509 result.GetOutputStream().Printf(" num ivars = %" PRIuPTR, (uintptr_t)iterator->second->GetNumIVars());
510 if (auto superclass = iterator->second->GetSuperclass())
512 result.GetOutputStream().Printf(" superclass = %s", superclass->GetClassName().AsCString("<unknown>"));
514 result.GetOutputStream().Printf("\n");
518 result.GetOutputStream().Printf(" has no associated class.\n");
521 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
526 result.AppendError("current process has no Objective-C runtime loaded");
527 result.SetStatus(lldb::eReturnStatusFailed);
533 class CommandObjectMultiwordObjC_TaggedPointer_Info : public CommandObjectParsed
536 CommandObjectMultiwordObjC_TaggedPointer_Info (CommandInterpreter &interpreter) :
537 CommandObjectParsed (interpreter,
539 "Dump information on a tagged pointer.",
540 "language objc tagged-pointer info",
541 eCommandRequiresProcess |
542 eCommandProcessMustBeLaunched |
543 eCommandProcessMustBePaused )
545 CommandArgumentEntry arg;
546 CommandArgumentData index_arg;
548 // Define the first (and only) variant of this arg.
549 index_arg.arg_type = eArgTypeAddress;
550 index_arg.arg_repetition = eArgRepeatPlus;
552 // There is only one variant this argument could be; put it into the argument entry.
553 arg.push_back (index_arg);
555 // Push the data for the first argument into the m_arguments vector.
556 m_arguments.push_back (arg);
559 ~CommandObjectMultiwordObjC_TaggedPointer_Info() override = default;
563 DoExecute(Args& command, CommandReturnObject &result) override
565 if (command.GetArgumentCount() == 0)
567 result.AppendError("this command requires arguments");
568 result.SetStatus(lldb::eReturnStatusFailed);
572 Process *process = m_exe_ctx.GetProcessPtr();
573 ExecutionContext exe_ctx(process);
574 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
577 ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor = objc_runtime->GetTaggedPointerVendor();
578 if (tagged_ptr_vendor)
581 i < command.GetArgumentCount();
584 const char *arg_str = command.GetArgumentAtIndex(i);
588 lldb::addr_t arg_addr = Args::StringToAddress(&exe_ctx, arg_str, LLDB_INVALID_ADDRESS, &error);
589 if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail())
591 auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(arg_addr);
594 uint64_t info_bits = 0;
595 uint64_t value_bits = 0;
596 uint64_t payload = 0;
597 if (descriptor_sp->GetTaggedPointerInfo(&info_bits, &value_bits, &payload))
599 result.GetOutputStream().Printf("0x%" PRIx64 " is tagged.\n\tpayload = 0x%" PRIx64 "\n\tvalue = 0x%" PRIx64 "\n\tinfo bits = 0x%" PRIx64 "\n\tclass = %s\n",
604 descriptor_sp->GetClassName().AsCString("<unknown>"));
608 result.GetOutputStream().Printf("0x%" PRIx64 " is not tagged.\n", (uint64_t)arg_addr);
614 result.AppendError("current process has no tagged pointer support");
615 result.SetStatus(lldb::eReturnStatusFailed);
618 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
623 result.AppendError("current process has no Objective-C runtime loaded");
624 result.SetStatus(lldb::eReturnStatusFailed);
630 class CommandObjectMultiwordObjC_ClassTable : public CommandObjectMultiword
633 CommandObjectMultiwordObjC_ClassTable (CommandInterpreter &interpreter) :
634 CommandObjectMultiword (interpreter,
636 "A set of commands for operating on the Objective-C class table.",
637 "class-table <subcommand> [<subcommand-options>]")
639 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectObjC_ClassTable_Dump (interpreter)));
642 ~CommandObjectMultiwordObjC_ClassTable() override = default;
645 class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword
649 CommandObjectMultiwordObjC_TaggedPointer (CommandInterpreter &interpreter) :
650 CommandObjectMultiword (interpreter,
652 "A set of commands for operating on Objective-C tagged pointers.",
653 "class-table <subcommand> [<subcommand-options>]")
655 LoadSubCommand ("info", CommandObjectSP (new CommandObjectMultiwordObjC_TaggedPointer_Info (interpreter)));
658 ~CommandObjectMultiwordObjC_TaggedPointer() override = default;
661 class CommandObjectMultiwordObjC : public CommandObjectMultiword
664 CommandObjectMultiwordObjC (CommandInterpreter &interpreter) :
665 CommandObjectMultiword (interpreter,
667 "A set of commands for operating on the Objective-C Language Runtime.",
668 "objc <subcommand> [<subcommand-options>]")
670 LoadSubCommand ("class-table", CommandObjectSP (new CommandObjectMultiwordObjC_ClassTable (interpreter)));
671 LoadSubCommand ("tagged-pointer", CommandObjectSP (new CommandObjectMultiwordObjC_TaggedPointer (interpreter)));
674 ~CommandObjectMultiwordObjC() override = default;
678 AppleObjCRuntimeV2::Initialize()
680 PluginManager::RegisterPlugin (GetPluginNameStatic(),
681 "Apple Objective C Language Runtime - Version 2",
683 [] (CommandInterpreter& interpreter) -> lldb::CommandObjectSP {
684 return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter));
689 AppleObjCRuntimeV2::Terminate()
691 PluginManager::UnregisterPlugin (CreateInstance);
694 lldb_private::ConstString
695 AppleObjCRuntimeV2::GetPluginNameStatic()
697 static ConstString g_name("apple-objc-v2");
701 //------------------------------------------------------------------
702 // PluginInterface protocol
703 //------------------------------------------------------------------
704 lldb_private::ConstString
705 AppleObjCRuntimeV2::GetPluginName()
707 return GetPluginNameStatic();
711 AppleObjCRuntimeV2::GetPluginVersion()
717 AppleObjCRuntimeV2::CreateExceptionResolver (Breakpoint *bkpt, bool catch_bp, bool throw_bp)
719 BreakpointResolverSP resolver_sp;
722 resolver_sp.reset (new BreakpointResolverName (bkpt,
723 "objc_exception_throw",
724 eFunctionNameTypeBase,
725 eLanguageTypeUnknown,
728 // FIXME: We don't do catch breakpoints for ObjC yet.
729 // Should there be some way for the runtime to specify what it can do in this regard?
734 AppleObjCRuntimeV2::CreateObjectChecker(const char *name)
736 char check_function_code[2048];
739 if (m_has_object_getClass)
741 len = ::snprintf (check_function_code,
742 sizeof(check_function_code),
743 "extern \"C\" void *gdb_object_getClass(void *); \n"
744 "extern \"C\" int printf(const char *format, ...); \n"
745 "extern \"C\" void \n"
746 "%s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) \n"
748 " if ($__lldb_arg_obj == (void *)0) \n"
749 " return; // nil is ok \n"
750 " if (!gdb_object_getClass($__lldb_arg_obj)) \n"
751 " *((volatile int *)0) = 'ocgc'; \n"
752 " else if ($__lldb_arg_selector != (void *)0) \n"
754 " signed char responds = (signed char) [(id) $__lldb_arg_obj \n"
755 " respondsToSelector: \n"
756 " (struct objc_selector *) $__lldb_arg_selector]; \n"
757 " if (responds == (signed char) 0) \n"
758 " *((volatile int *)0) = 'ocgc'; \n"
765 len = ::snprintf (check_function_code,
766 sizeof(check_function_code),
767 "extern \"C\" void *gdb_class_getClass(void *); \n"
768 "extern \"C\" int printf(const char *format, ...); \n"
769 "extern \"C\" void \n"
770 "%s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) \n"
772 " if ($__lldb_arg_obj == (void *)0) \n"
773 " return; // nil is ok \n"
774 " void **$isa_ptr = (void **)$__lldb_arg_obj; \n"
775 " if (*$isa_ptr == (void *)0 || !gdb_class_getClass(*$isa_ptr)) \n"
776 " *((volatile int *)0) = 'ocgc'; \n"
777 " else if ($__lldb_arg_selector != (void *)0) \n"
779 " signed char responds = (signed char) [(id) $__lldb_arg_obj \n"
780 " respondsToSelector: \n"
781 " (struct objc_selector *) $__lldb_arg_selector]; \n"
782 " if (responds == (signed char) 0) \n"
783 " *((volatile int *)0) = 'ocgc'; \n"
789 assert (len < (int)sizeof(check_function_code));
792 return GetTargetRef().GetUtilityFunctionForLanguage(check_function_code, eLanguageTypeObjC, name, error);
796 AppleObjCRuntimeV2::GetByteOffsetForIvar (CompilerType &parent_ast_type, const char *ivar_name)
798 uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
800 const char *class_name = parent_ast_type.GetConstTypeName().AsCString();
801 if (class_name && class_name[0] && ivar_name && ivar_name[0])
803 //----------------------------------------------------------------------
804 // Make the objective C V2 mangled name for the ivar offset from the
805 // class name and ivar name
806 //----------------------------------------------------------------------
807 std::string buffer("OBJC_IVAR_$_");
808 buffer.append (class_name);
809 buffer.push_back ('.');
810 buffer.append (ivar_name);
811 ConstString ivar_const_str (buffer.c_str());
813 //----------------------------------------------------------------------
814 // Try to get the ivar offset address from the symbol table first using
815 // the name we created above
816 //----------------------------------------------------------------------
817 SymbolContextList sc_list;
818 Target &target = m_process->GetTarget();
819 target.GetImages().FindSymbolsWithNameAndType(ivar_const_str, eSymbolTypeObjCIVar, sc_list);
821 addr_t ivar_offset_address = LLDB_INVALID_ADDRESS;
824 SymbolContext ivar_offset_symbol;
825 if (sc_list.GetSize() == 1 && sc_list.GetContextAtIndex(0, ivar_offset_symbol))
827 if (ivar_offset_symbol.symbol)
828 ivar_offset_address = ivar_offset_symbol.symbol->GetLoadAddress (&target);
831 //----------------------------------------------------------------------
832 // If we didn't get the ivar offset address from the symbol table, fall
833 // back to getting it from the runtime
834 //----------------------------------------------------------------------
835 if (ivar_offset_address == LLDB_INVALID_ADDRESS)
836 ivar_offset_address = LookupRuntimeSymbol(ivar_const_str);
838 if (ivar_offset_address != LLDB_INVALID_ADDRESS)
839 ivar_offset = m_process->ReadUnsignedIntegerFromMemory (ivar_offset_address,
841 LLDB_INVALID_IVAR_OFFSET,
847 // tagged pointers are special not-a-real-pointer values that contain both type and value information
848 // this routine attempts to check with as little computational effort as possible whether something
849 // could possibly be a tagged pointer - false positives are possible but false negatives shouldn't
851 AppleObjCRuntimeV2::IsTaggedPointer(addr_t ptr)
853 if (!m_tagged_pointer_vendor_ap)
855 return m_tagged_pointer_vendor_ap->IsPossibleTaggedPointer(ptr);
858 class RemoteNXMapTable
861 RemoteNXMapTable () :
863 m_num_buckets_minus_one (0),
864 m_buckets_ptr (LLDB_INVALID_ADDRESS),
866 m_end_iterator (*this, -1),
867 m_load_addr (LLDB_INVALID_ADDRESS),
876 printf ("RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n", m_load_addr);
877 printf ("RemoteNXMapTable.m_count = %u\n", m_count);
878 printf ("RemoteNXMapTable.m_num_buckets_minus_one = %u\n", m_num_buckets_minus_one);
879 printf ("RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n", m_buckets_ptr);
883 ParseHeader (Process* process, lldb::addr_t load_addr)
886 m_load_addr = load_addr;
887 m_map_pair_size = m_process->GetAddressByteSize() * 2;
888 m_invalid_key = m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX;
891 // This currently holds true for all platforms we support, but we might
892 // need to change this to use get the actually byte size of "unsigned"
893 // from the target AST...
894 const uint32_t unsigned_byte_size = sizeof(uint32_t);
895 // Skip the prototype as we don't need it (const struct +NXMapTablePrototype *prototype)
898 if (load_addr == LLDB_INVALID_ADDRESS)
902 lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize();
905 m_count = m_process->ReadUnsignedIntegerFromMemory(cursor, unsigned_byte_size, 0, err);
908 cursor += unsigned_byte_size;
910 // unsigned nbBucketsMinusOne;
911 m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory(cursor, unsigned_byte_size, 0, err);
912 cursor += unsigned_byte_size;
915 m_buckets_ptr = m_process->ReadPointerFromMemory(cursor, err);
917 success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS;
924 m_num_buckets_minus_one = 0;
925 m_buckets_ptr = LLDB_INVALID_ADDRESS;
930 // const_iterator mimics NXMapState and its code comes from NXInitMapState and NXNextMapState.
931 typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element;
933 friend class const_iterator;
937 const_iterator (RemoteNXMapTable &parent, int index) : m_parent(parent), m_index(index)
939 AdvanceToValidIndex();
942 const_iterator (const const_iterator &rhs) : m_parent(rhs.m_parent), m_index(rhs.m_index)
944 // AdvanceToValidIndex() has been called by rhs already.
947 const_iterator &operator=(const const_iterator &rhs)
949 // AdvanceToValidIndex() has been called by rhs already.
950 assert (&m_parent == &rhs.m_parent);
951 m_index = rhs.m_index;
955 bool operator==(const const_iterator &rhs) const
957 if (&m_parent != &rhs.m_parent)
959 if (m_index != rhs.m_index)
965 bool operator!=(const const_iterator &rhs) const
967 return !(operator==(rhs));
970 const_iterator &operator++()
972 AdvanceToValidIndex();
976 const element operator*() const
980 // TODO find a way to make this an error, but not an assert
984 lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
985 size_t map_pair_size = m_parent.m_map_pair_size;
986 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
990 lldb::addr_t key = m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
993 lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory(pair_ptr + m_parent.m_process->GetAddressByteSize(), err);
997 std::string key_string;
999 m_parent.m_process->ReadCStringFromMemory(key, key_string, err);
1003 return element(ConstString(key_string.c_str()), (ObjCLanguageRuntime::ObjCISA)value);
1007 void AdvanceToValidIndex ()
1012 const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1013 const size_t map_pair_size = m_parent.m_map_pair_size;
1014 const lldb::addr_t invalid_key = m_parent.m_invalid_key;
1019 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1020 lldb::addr_t key = m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1028 if (key != invalid_key)
1032 RemoteNXMapTable &m_parent;
1036 const_iterator begin ()
1038 return const_iterator(*this, m_num_buckets_minus_one + 1);
1041 const_iterator end ()
1043 return m_end_iterator;
1053 GetBucketCount () const
1055 return m_num_buckets_minus_one;
1059 GetBucketDataPointer () const
1061 return m_buckets_ptr;
1065 GetTableLoadAddress() const
1071 // contents of _NXMapTable struct
1073 uint32_t m_num_buckets_minus_one;
1074 lldb::addr_t m_buckets_ptr;
1075 lldb_private::Process *m_process;
1076 const_iterator m_end_iterator;
1077 lldb::addr_t m_load_addr;
1078 size_t m_map_pair_size;
1079 lldb::addr_t m_invalid_key;
1082 AppleObjCRuntimeV2::HashTableSignature::HashTableSignature() :
1090 AppleObjCRuntimeV2::HashTableSignature::UpdateSignature (const RemoteNXMapTable &hash_table)
1092 m_count = hash_table.GetCount();
1093 m_num_buckets = hash_table.GetBucketCount();
1094 m_buckets_ptr = hash_table.GetBucketDataPointer();
1098 AppleObjCRuntimeV2::HashTableSignature::NeedsUpdate (Process *process, AppleObjCRuntimeV2 *runtime, RemoteNXMapTable &hash_table)
1100 if (!hash_table.ParseHeader(process, runtime->GetISAHashTablePointer ()))
1102 return false; // Failed to parse the header, no need to update anything
1105 // Check with out current signature and return true if the count,
1106 // number of buckets or the hash table address changes.
1107 if (m_count == hash_table.GetCount() &&
1108 m_num_buckets == hash_table.GetBucketCount() &&
1109 m_buckets_ptr == hash_table.GetBucketDataPointer())
1111 // Hash table hasn't changed
1114 // Hash table data has changed, we need to update
1118 ObjCLanguageRuntime::ClassDescriptorSP
1119 AppleObjCRuntimeV2::GetClassDescriptorFromISA (ObjCISA isa)
1121 ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp;
1122 if (m_non_pointer_isa_cache_ap.get())
1123 class_descriptor_sp = m_non_pointer_isa_cache_ap->GetClassDescriptor(isa);
1124 if (!class_descriptor_sp)
1125 class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa);
1126 return class_descriptor_sp;
1129 ObjCLanguageRuntime::ClassDescriptorSP
1130 AppleObjCRuntimeV2::GetClassDescriptor (ValueObject& valobj)
1132 ClassDescriptorSP objc_class_sp;
1133 if (valobj.IsBaseClass())
1135 ValueObject *parent = valobj.GetParent();
1136 // if I am my own parent, bail out of here fast..
1137 if (parent && parent != &valobj)
1139 ClassDescriptorSP parent_descriptor_sp = GetClassDescriptor(*parent);
1140 if (parent_descriptor_sp)
1141 return parent_descriptor_sp->GetSuperclass();
1145 // if we get an invalid VO (which might still happen when playing around
1146 // with pointers returned by the expression parser, don't consider this
1147 // a valid ObjC object)
1148 if (valobj.GetCompilerType().IsValid())
1150 addr_t isa_pointer = valobj.GetPointerValue();
1153 if (IsTaggedPointer(isa_pointer))
1155 return m_tagged_pointer_vendor_ap->GetClassDescriptor(isa_pointer);
1159 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
1161 Process *process = exe_ctx.GetProcessPtr();
1165 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
1166 if (isa != LLDB_INVALID_ADDRESS)
1168 objc_class_sp = GetClassDescriptorFromISA (isa);
1169 if (isa && !objc_class_sp)
1171 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1173 log->Printf("0x%" PRIx64 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was not in class descriptor cache 0x%" PRIx64,
1181 return objc_class_sp;
1185 AppleObjCRuntimeV2::GetISAHashTablePointer ()
1187 if (m_isa_hash_table_ptr == LLDB_INVALID_ADDRESS)
1189 Process *process = GetProcess();
1191 ModuleSP objc_module_sp(GetObjCModule());
1193 if (!objc_module_sp)
1194 return LLDB_INVALID_ADDRESS;
1196 static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes");
1198 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(g_gdb_objc_realized_classes, lldb::eSymbolTypeAny);
1201 lldb::addr_t gdb_objc_realized_classes_ptr = symbol->GetLoadAddress(&process->GetTarget());
1203 if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS)
1206 m_isa_hash_table_ptr = process->ReadPointerFromMemory(gdb_objc_realized_classes_ptr, error);
1210 return m_isa_hash_table_ptr;
1214 AppleObjCRuntimeV2::UpdateISAToDescriptorMapDynamic(RemoteNXMapTable &hash_table)
1216 Process *process = GetProcess();
1218 if (process == NULL)
1221 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1223 ExecutionContext exe_ctx;
1225 ThreadSP thread_sp = process->GetThreadList().GetSelectedThread();
1230 thread_sp->CalculateExecutionContext(exe_ctx);
1231 ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
1236 Address function_address;
1238 StreamString errors;
1240 const uint32_t addr_size = process->GetAddressByteSize();
1244 // Read the total number of classes from the hash table
1245 const uint32_t num_classes = hash_table.GetCount();
1246 if (num_classes == 0)
1249 log->Printf ("No dynamic classes found in gdb_objc_realized_classes.");
1253 // Make some types for our arguments
1254 CompilerType clang_uint32_t_type = ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1255 CompilerType clang_void_pointer_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1257 ValueList arguments;
1258 FunctionCaller *get_class_info_function = nullptr;
1260 if (!m_get_class_info_code.get())
1263 m_get_class_info_code.reset (GetTargetRef().GetUtilityFunctionForLanguage (g_get_dynamic_class_info_body,
1265 g_get_dynamic_class_info_name,
1270 log->Printf ("Failed to get Utility Function for implementation lookup: %s", error.AsCString());
1271 m_get_class_info_code.reset();
1277 if (!m_get_class_info_code->Install(errors, exe_ctx))
1280 log->Printf ("Failed to install implementation lookup: %s.", errors.GetData());
1281 m_get_class_info_code.reset();
1284 if (!m_get_class_info_code.get())
1287 // Next make the runner function for our implementation utility function.
1289 value.SetValueType (Value::eValueTypeScalar);
1290 value.SetCompilerType (clang_void_pointer_type);
1291 arguments.PushValue (value);
1292 arguments.PushValue (value);
1294 value.SetValueType (Value::eValueTypeScalar);
1295 value.SetCompilerType (clang_uint32_t_type);
1296 arguments.PushValue (value);
1298 get_class_info_function = m_get_class_info_code->MakeFunctionCaller(clang_uint32_t_type,
1305 log->Printf("Failed to make function caller for implementation lookup: %s.", error.AsCString());
1311 get_class_info_function = m_get_class_info_code->GetFunctionCaller();
1312 if (!get_class_info_function)
1315 log->Printf ("Failed to get implementation lookup function caller: %s.", errors.GetData());
1318 arguments = get_class_info_function->GetArgumentValues();
1323 const uint32_t class_info_byte_size = addr_size + 4;
1324 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1325 lldb::addr_t class_infos_addr = process->AllocateMemory(class_infos_byte_size,
1326 ePermissionsReadable | ePermissionsWritable,
1329 if (class_infos_addr == LLDB_INVALID_ADDRESS)
1332 Mutex::Locker locker(m_get_class_info_args_mutex);
1334 // Fill in our function argument values
1335 arguments.GetValueAtIndex(0)->GetScalar() = hash_table.GetTableLoadAddress();
1336 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1337 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1339 bool success = false;
1343 // Write our function arguments into the process so we can run our function
1344 if (get_class_info_function->WriteFunctionArguments (exe_ctx,
1345 m_get_class_info_args,
1349 EvaluateExpressionOptions options;
1350 options.SetUnwindOnError(true);
1351 options.SetTryAllThreads(false);
1352 options.SetStopOthers(true);
1353 options.SetIgnoreBreakpoints(true);
1354 options.SetTimeoutUsec(UTILITY_FUNCTION_TIMEOUT_USEC);
1357 return_value.SetValueType (Value::eValueTypeScalar);
1358 //return_value.SetContext (Value::eContextTypeClangType, clang_uint32_t_type);
1359 return_value.SetCompilerType (clang_uint32_t_type);
1360 return_value.GetScalar() = 0;
1365 ExpressionResults results = get_class_info_function->ExecuteFunction (exe_ctx,
1366 &m_get_class_info_args,
1371 if (results == eExpressionCompleted)
1373 // The result is the number of ClassInfo structures that were filled in
1374 uint32_t num_class_infos = return_value.GetScalar().ULong();
1376 log->Printf("Discovered %u ObjC classes\n",num_class_infos);
1377 if (num_class_infos > 0)
1379 // Read the ClassInfo structures
1380 DataBufferHeap buffer (num_class_infos * class_info_byte_size, 0);
1381 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(), buffer.GetByteSize(), err) == buffer.GetByteSize())
1383 DataExtractor class_infos_data (buffer.GetBytes(),
1384 buffer.GetByteSize(),
1385 process->GetByteOrder(),
1387 ParseClassInfoArray (class_infos_data, num_class_infos);
1395 log->Printf("Error evaluating our find class name function: %s.\n", errors.GetData());
1401 log->Printf ("Error writing function arguments: \"%s\".", errors.GetData());
1404 // Deallocate the memory we allocated for the ClassInfo array
1405 process->DeallocateMemory(class_infos_addr);
1411 AppleObjCRuntimeV2::ParseClassInfoArray (const DataExtractor &data, uint32_t num_class_infos)
1413 // Parses an array of "num_class_infos" packed ClassInfo structures:
1419 // } __attribute__((__packed__));
1421 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1423 uint32_t num_parsed = 0;
1425 // Iterate through all ClassInfo structures
1426 lldb::offset_t offset = 0;
1427 for (uint32_t i=0; i<num_class_infos; ++i)
1429 ObjCISA isa = data.GetPointer(&offset);
1434 log->Printf("AppleObjCRuntimeV2 found NULL isa, ignoring this class info");
1437 // Check if we already know about this ISA, if we do, the info will
1438 // never change, so we can just skip it.
1439 if (ISAIsCached(isa))
1445 // Read the 32 bit hash for the class name
1446 const uint32_t name_hash = data.GetU32(&offset);
1447 ClassDescriptorSP descriptor_sp (new ClassDescriptorV2(*this, isa, NULL));
1448 AddClass (isa, descriptor_sp, name_hash);
1450 if (log && log->GetVerbose())
1451 log->Printf("AppleObjCRuntimeV2 added isa=0x%" PRIx64 ", hash=0x%8.8x, name=%s", isa, name_hash,descriptor_sp->GetClassName().AsCString("<unknown>"));
1457 AppleObjCRuntimeV2::DescriptorMapUpdateResult
1458 AppleObjCRuntimeV2::UpdateISAToDescriptorMapSharedCache()
1460 Process *process = GetProcess();
1462 if (process == NULL)
1463 return DescriptorMapUpdateResult::Fail();
1465 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1467 ExecutionContext exe_ctx;
1469 ThreadSP thread_sp = process->GetThreadList().GetSelectedThread();
1472 return DescriptorMapUpdateResult::Fail();
1474 thread_sp->CalculateExecutionContext(exe_ctx);
1475 ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
1478 return DescriptorMapUpdateResult::Fail();
1480 Address function_address;
1482 StreamString errors;
1484 const uint32_t addr_size = process->GetAddressByteSize();
1488 const lldb::addr_t objc_opt_ptr = GetSharedCacheReadOnlyAddress();
1490 if (objc_opt_ptr == LLDB_INVALID_ADDRESS)
1491 return DescriptorMapUpdateResult::Fail();
1493 // Read the total number of classes from the hash table
1494 const uint32_t num_classes = 128*1024;
1495 if (num_classes == 0)
1498 log->Printf ("No dynamic classes found in gdb_objc_realized_classes_addr.");
1499 return DescriptorMapUpdateResult::Fail();
1502 // Make some types for our arguments
1503 CompilerType clang_uint32_t_type = ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1504 CompilerType clang_void_pointer_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1506 ValueList arguments;
1507 FunctionCaller *get_shared_cache_class_info_function = nullptr;
1509 if (!m_get_shared_cache_class_info_code.get())
1512 m_get_shared_cache_class_info_code.reset (GetTargetRef().GetUtilityFunctionForLanguage (g_get_shared_cache_class_info_body,
1514 g_get_shared_cache_class_info_name,
1519 log->Printf ("Failed to get Utility function for implementation lookup: %s.", error.AsCString());
1520 m_get_shared_cache_class_info_code.reset();
1526 if (!m_get_shared_cache_class_info_code->Install(errors, exe_ctx))
1529 log->Printf ("Failed to install implementation lookup: %s.", errors.GetData());
1530 m_get_shared_cache_class_info_code.reset();
1534 if (!m_get_shared_cache_class_info_code.get())
1535 return DescriptorMapUpdateResult::Fail();
1537 // Next make the function caller for our implementation utility function.
1539 value.SetValueType (Value::eValueTypeScalar);
1540 //value.SetContext (Value::eContextTypeClangType, clang_void_pointer_type);
1541 value.SetCompilerType (clang_void_pointer_type);
1542 arguments.PushValue (value);
1543 arguments.PushValue (value);
1545 value.SetValueType (Value::eValueTypeScalar);
1546 //value.SetContext (Value::eContextTypeClangType, clang_uint32_t_type);
1547 value.SetCompilerType (clang_uint32_t_type);
1548 arguments.PushValue (value);
1550 get_shared_cache_class_info_function = m_get_shared_cache_class_info_code->MakeFunctionCaller(clang_uint32_t_type,
1554 if (get_shared_cache_class_info_function == nullptr)
1555 return DescriptorMapUpdateResult::Fail();
1560 get_shared_cache_class_info_function = m_get_shared_cache_class_info_code->GetFunctionCaller();
1561 if (get_shared_cache_class_info_function == nullptr)
1562 return DescriptorMapUpdateResult::Fail();
1563 arguments = get_shared_cache_class_info_function->GetArgumentValues();
1568 const uint32_t class_info_byte_size = addr_size + 4;
1569 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1570 lldb::addr_t class_infos_addr = process->AllocateMemory (class_infos_byte_size,
1571 ePermissionsReadable | ePermissionsWritable,
1574 if (class_infos_addr == LLDB_INVALID_ADDRESS)
1575 return DescriptorMapUpdateResult::Fail();
1577 Mutex::Locker locker(m_get_shared_cache_class_info_args_mutex);
1579 // Fill in our function argument values
1580 arguments.GetValueAtIndex(0)->GetScalar() = objc_opt_ptr;
1581 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1582 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1584 bool success = false;
1585 bool any_found = false;
1589 // Write our function arguments into the process so we can run our function
1590 if (get_shared_cache_class_info_function->WriteFunctionArguments (exe_ctx,
1591 m_get_shared_cache_class_info_args,
1595 EvaluateExpressionOptions options;
1596 options.SetUnwindOnError(true);
1597 options.SetTryAllThreads(false);
1598 options.SetStopOthers(true);
1599 options.SetIgnoreBreakpoints(true);
1600 options.SetTimeoutUsec(UTILITY_FUNCTION_TIMEOUT_USEC);
1603 return_value.SetValueType (Value::eValueTypeScalar);
1604 //return_value.SetContext (Value::eContextTypeClangType, clang_uint32_t_type);
1605 return_value.SetCompilerType (clang_uint32_t_type);
1606 return_value.GetScalar() = 0;
1611 ExpressionResults results = get_shared_cache_class_info_function->ExecuteFunction (exe_ctx,
1612 &m_get_shared_cache_class_info_args,
1617 if (results == eExpressionCompleted)
1619 // The result is the number of ClassInfo structures that were filled in
1620 uint32_t num_class_infos = return_value.GetScalar().ULong();
1622 log->Printf("Discovered %u ObjC classes in shared cache\n",num_class_infos);
1623 #ifdef LLDB_CONFIGURATION_DEBUG
1624 assert (num_class_infos <= num_classes);
1626 if (num_class_infos > 0)
1628 if (num_class_infos > num_classes)
1630 num_class_infos = num_classes;
1639 // Read the ClassInfo structures
1640 DataBufferHeap buffer (num_class_infos * class_info_byte_size, 0);
1641 if (process->ReadMemory(class_infos_addr,
1643 buffer.GetByteSize(),
1644 err) == buffer.GetByteSize())
1646 DataExtractor class_infos_data (buffer.GetBytes(),
1647 buffer.GetByteSize(),
1648 process->GetByteOrder(),
1651 any_found = (ParseClassInfoArray (class_infos_data, num_class_infos) > 0);
1662 log->Printf("Error evaluating our find class name function: %s.\n", errors.GetData());
1668 log->Printf ("Error writing function arguments: \"%s\".", errors.GetData());
1671 // Deallocate the memory we allocated for the ClassInfo array
1672 process->DeallocateMemory(class_infos_addr);
1674 return DescriptorMapUpdateResult(success, any_found);
1678 AppleObjCRuntimeV2::UpdateISAToDescriptorMapFromMemory (RemoteNXMapTable &hash_table)
1680 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1682 Process *process = GetProcess();
1684 if (process == NULL)
1687 uint32_t num_map_table_isas = 0;
1689 ModuleSP objc_module_sp(GetObjCModule());
1693 for (RemoteNXMapTable::element elt : hash_table)
1695 ++num_map_table_isas;
1697 if (ISAIsCached(elt.second))
1700 ClassDescriptorSP descriptor_sp = ClassDescriptorSP(new ClassDescriptorV2(*this, elt.second, elt.first.AsCString()));
1702 if (log && log->GetVerbose())
1703 log->Printf("AppleObjCRuntimeV2 added (ObjCISA)0x%" PRIx64 " (%s) from dynamic table to isa->descriptor cache", elt.second, elt.first.AsCString());
1705 AddClass (elt.second, descriptor_sp, elt.first.AsCString());
1709 return num_map_table_isas > 0;
1713 AppleObjCRuntimeV2::GetSharedCacheReadOnlyAddress()
1715 Process *process = GetProcess();
1719 ModuleSP objc_module_sp(GetObjCModule());
1723 ObjectFile *objc_object = objc_module_sp->GetObjectFile();
1727 SectionList *section_list = objc_module_sp->GetSectionList();
1731 SectionSP text_segment_sp (section_list->FindSectionByName(ConstString("__TEXT")));
1733 if (text_segment_sp)
1735 SectionSP objc_opt_section_sp (text_segment_sp->GetChildren().FindSectionByName(ConstString("__objc_opt_ro")));
1737 if (objc_opt_section_sp)
1739 return objc_opt_section_sp->GetLoadBaseAddress(&process->GetTarget());
1746 return LLDB_INVALID_ADDRESS;
1750 AppleObjCRuntimeV2::UpdateISAToDescriptorMapIfNeeded()
1752 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1754 // Else we need to check with our process to see when the map was updated.
1755 Process *process = GetProcess();
1759 RemoteNXMapTable hash_table;
1761 // Update the process stop ID that indicates the last time we updated the
1762 // map, whether it was successful or not.
1763 m_isa_to_descriptor_stop_id = process->GetStopID();
1765 if (!m_hash_signature.NeedsUpdate(process, this, hash_table))
1768 m_hash_signature.UpdateSignature (hash_table);
1770 // Grab the dynamically loaded objc classes from the hash table in memory
1771 UpdateISAToDescriptorMapDynamic(hash_table);
1773 // Now get the objc classes that are baked into the Objective C runtime
1774 // in the shared cache, but only once per process as this data never
1776 if (!m_loaded_objc_opt)
1778 DescriptorMapUpdateResult shared_cache_update_result = UpdateISAToDescriptorMapSharedCache();
1779 if (!shared_cache_update_result.any_found)
1780 WarnIfNoClassesCached ();
1782 m_loaded_objc_opt = true;
1787 m_isa_to_descriptor_stop_id = UINT32_MAX;
1792 AppleObjCRuntimeV2::WarnIfNoClassesCached ()
1794 if (m_noclasses_warning_emitted)
1797 #if defined(__APPLE__)
1799 m_process->GetTarget().GetPlatform() &&
1800 m_process->GetTarget().GetPlatform()->GetPluginName() == PlatformiOSSimulator::GetPluginNameStatic())
1802 // the iOS simulator does not have the objc_opt_ro class table
1803 // so don't actually complain to the user
1804 m_noclasses_warning_emitted = true;
1809 Debugger &debugger(GetProcess()->GetTarget().GetDebugger());
1811 if (debugger.GetAsyncOutputStream())
1813 debugger.GetAsyncOutputStream()->PutCString("warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.\n");
1814 m_noclasses_warning_emitted = true;
1818 // TODO: should we have a transparent_kvo parameter here to say if we
1819 // want to replace the KVO swizzled class with the actual user-level type?
1821 AppleObjCRuntimeV2::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa)
1823 if (isa == g_objc_Tagged_ISA)
1825 static const ConstString g_objc_tagged_isa_name ("_lldb_Tagged_ObjC_ISA");
1826 return g_objc_tagged_isa_name;
1828 if (isa == g_objc_Tagged_ISA_NSAtom)
1830 static const ConstString g_objc_tagged_isa_nsatom_name ("NSAtom");
1831 return g_objc_tagged_isa_nsatom_name;
1833 if (isa == g_objc_Tagged_ISA_NSNumber)
1835 static const ConstString g_objc_tagged_isa_nsnumber_name ("NSNumber");
1836 return g_objc_tagged_isa_nsnumber_name;
1838 if (isa == g_objc_Tagged_ISA_NSDateTS)
1840 static const ConstString g_objc_tagged_isa_nsdatets_name ("NSDateTS");
1841 return g_objc_tagged_isa_nsdatets_name;
1843 if (isa == g_objc_Tagged_ISA_NSManagedObject)
1845 static const ConstString g_objc_tagged_isa_nsmanagedobject_name ("NSManagedObject");
1846 return g_objc_tagged_isa_nsmanagedobject_name;
1848 if (isa == g_objc_Tagged_ISA_NSDate)
1850 static const ConstString g_objc_tagged_isa_nsdate_name ("NSDate");
1851 return g_objc_tagged_isa_nsdate_name;
1853 return ObjCLanguageRuntime::GetActualTypeName(isa);
1857 AppleObjCRuntimeV2::GetDeclVendor()
1859 if (!m_decl_vendor_ap.get())
1860 m_decl_vendor_ap.reset(new AppleObjCDeclVendor(*this));
1862 return m_decl_vendor_ap.get();
1866 AppleObjCRuntimeV2::LookupRuntimeSymbol (const ConstString &name)
1868 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
1870 const char *name_cstr = name.AsCString();
1874 llvm::StringRef name_strref(name_cstr);
1876 static const llvm::StringRef ivar_prefix("OBJC_IVAR_$_");
1877 static const llvm::StringRef class_prefix("OBJC_CLASS_$_");
1879 if (name_strref.startswith(ivar_prefix))
1881 llvm::StringRef ivar_skipped_prefix = name_strref.substr(ivar_prefix.size());
1882 std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar = ivar_skipped_prefix.split('.');
1884 if (class_and_ivar.first.size() && class_and_ivar.second.size())
1886 const ConstString class_name_cs(class_and_ivar.first);
1887 ClassDescriptorSP descriptor = ObjCLanguageRuntime::GetClassDescriptorFromClassName(class_name_cs);
1891 const ConstString ivar_name_cs(class_and_ivar.second);
1892 const char *ivar_name_cstr = ivar_name_cs.AsCString();
1894 auto ivar_func = [&ret, ivar_name_cstr](const char *name, const char *type, lldb::addr_t offset_addr, uint64_t size) -> lldb::addr_t
1896 if (!strcmp(name, ivar_name_cstr))
1904 descriptor->Describe(std::function<void (ObjCISA)>(nullptr),
1905 std::function<bool (const char *, const char *)>(nullptr),
1906 std::function<bool (const char *, const char *)>(nullptr),
1911 else if (name_strref.startswith(class_prefix))
1913 llvm::StringRef class_skipped_prefix = name_strref.substr(class_prefix.size());
1914 const ConstString class_name_cs(class_skipped_prefix);
1915 ClassDescriptorSP descriptor = GetClassDescriptorFromClassName(class_name_cs);
1918 ret = descriptor->GetISA();
1925 AppleObjCRuntimeV2::NonPointerISACache*
1926 AppleObjCRuntimeV2::NonPointerISACache::CreateInstance (AppleObjCRuntimeV2& runtime, const lldb::ModuleSP& objc_module_sp)
1928 Process* process(runtime.GetProcess());
1932 auto objc_debug_isa_magic_mask = ExtractRuntimeGlobalSymbol(process,
1933 ConstString("objc_debug_isa_magic_mask"),
1939 auto objc_debug_isa_magic_value = ExtractRuntimeGlobalSymbol(process,
1940 ConstString("objc_debug_isa_magic_value"),
1946 auto objc_debug_isa_class_mask = ExtractRuntimeGlobalSymbol(process,
1947 ConstString("objc_debug_isa_class_mask"),
1953 // we might want to have some rules to outlaw these other values (e.g if the mask is zero but the value is non-zero, ...)
1955 return new NonPointerISACache(runtime,
1956 objc_debug_isa_class_mask,
1957 objc_debug_isa_magic_mask,
1958 objc_debug_isa_magic_value);
1961 AppleObjCRuntimeV2::TaggedPointerVendorV2*
1962 AppleObjCRuntimeV2::TaggedPointerVendorV2::CreateInstance (AppleObjCRuntimeV2& runtime, const lldb::ModuleSP& objc_module_sp)
1964 Process* process(runtime.GetProcess());
1968 auto objc_debug_taggedpointer_mask = ExtractRuntimeGlobalSymbol(process,
1969 ConstString("objc_debug_taggedpointer_mask"),
1973 return new TaggedPointerVendorLegacy(runtime);
1975 auto objc_debug_taggedpointer_slot_shift = ExtractRuntimeGlobalSymbol(process,
1976 ConstString("objc_debug_taggedpointer_slot_shift"),
1982 return new TaggedPointerVendorLegacy(runtime);
1984 auto objc_debug_taggedpointer_slot_mask = ExtractRuntimeGlobalSymbol(process,
1985 ConstString("objc_debug_taggedpointer_slot_mask"),
1991 return new TaggedPointerVendorLegacy(runtime);
1993 auto objc_debug_taggedpointer_payload_lshift = ExtractRuntimeGlobalSymbol(process,
1994 ConstString("objc_debug_taggedpointer_payload_lshift"),
2000 return new TaggedPointerVendorLegacy(runtime);
2002 auto objc_debug_taggedpointer_payload_rshift = ExtractRuntimeGlobalSymbol(process,
2003 ConstString("objc_debug_taggedpointer_payload_rshift"),
2009 return new TaggedPointerVendorLegacy(runtime);
2011 auto objc_debug_taggedpointer_classes = ExtractRuntimeGlobalSymbol(process,
2012 ConstString("objc_debug_taggedpointer_classes"),
2017 return new TaggedPointerVendorLegacy(runtime);
2020 // we might want to have some rules to outlaw these values (e.g if the table's address is zero)
2022 return new TaggedPointerVendorRuntimeAssisted(runtime,
2023 objc_debug_taggedpointer_mask,
2024 objc_debug_taggedpointer_slot_shift,
2025 objc_debug_taggedpointer_slot_mask,
2026 objc_debug_taggedpointer_payload_lshift,
2027 objc_debug_taggedpointer_payload_rshift,
2028 objc_debug_taggedpointer_classes);
2032 AppleObjCRuntimeV2::TaggedPointerVendorLegacy::IsPossibleTaggedPointer (lldb::addr_t ptr)
2037 ObjCLanguageRuntime::ClassDescriptorSP
2038 AppleObjCRuntimeV2::TaggedPointerVendorLegacy::GetClassDescriptor (lldb::addr_t ptr)
2040 if (!IsPossibleTaggedPointer(ptr))
2041 return ObjCLanguageRuntime::ClassDescriptorSP();
2043 uint32_t foundation_version = m_runtime.GetFoundationVersion();
2045 if (foundation_version == LLDB_INVALID_MODULE_VERSION)
2046 return ObjCLanguageRuntime::ClassDescriptorSP();
2048 uint64_t class_bits = (ptr & 0xE) >> 1;
2051 // TODO: make a table
2052 if (foundation_version >= 900)
2057 name = ConstString("NSAtom");
2060 name = ConstString("NSNumber");
2063 name = ConstString("NSDateTS");
2066 name = ConstString("NSManagedObject");
2069 name = ConstString("NSDate");
2072 return ObjCLanguageRuntime::ClassDescriptorSP();
2080 name = ConstString("NSNumber");
2083 name = ConstString("NSManagedObject");
2086 name = ConstString("NSDate");
2089 name = ConstString("NSDateTS");
2092 return ObjCLanguageRuntime::ClassDescriptorSP();
2095 return ClassDescriptorSP(new ClassDescriptorV2Tagged(name,ptr));
2098 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::TaggedPointerVendorRuntimeAssisted (AppleObjCRuntimeV2& runtime,
2099 uint64_t objc_debug_taggedpointer_mask,
2100 uint32_t objc_debug_taggedpointer_slot_shift,
2101 uint32_t objc_debug_taggedpointer_slot_mask,
2102 uint32_t objc_debug_taggedpointer_payload_lshift,
2103 uint32_t objc_debug_taggedpointer_payload_rshift,
2104 lldb::addr_t objc_debug_taggedpointer_classes) :
2105 TaggedPointerVendorV2(runtime),
2107 m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask),
2108 m_objc_debug_taggedpointer_slot_shift(objc_debug_taggedpointer_slot_shift),
2109 m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask),
2110 m_objc_debug_taggedpointer_payload_lshift(objc_debug_taggedpointer_payload_lshift),
2111 m_objc_debug_taggedpointer_payload_rshift(objc_debug_taggedpointer_payload_rshift),
2112 m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes)
2117 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::IsPossibleTaggedPointer (lldb::addr_t ptr)
2119 return (ptr & m_objc_debug_taggedpointer_mask) != 0;
2122 ObjCLanguageRuntime::ClassDescriptorSP
2123 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::GetClassDescriptor (lldb::addr_t ptr)
2125 ClassDescriptorSP actual_class_descriptor_sp;
2126 uint64_t data_payload;
2128 if (!IsPossibleTaggedPointer(ptr))
2129 return ObjCLanguageRuntime::ClassDescriptorSP();
2131 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) & m_objc_debug_taggedpointer_slot_mask;
2133 CacheIterator iterator = m_cache.find(slot),
2134 end = m_cache.end();
2135 if (iterator != end)
2137 actual_class_descriptor_sp = iterator->second;
2141 Process* process(m_runtime.GetProcess());
2142 uintptr_t slot_ptr = slot*process->GetAddressByteSize()+m_objc_debug_taggedpointer_classes;
2144 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
2145 if (error.Fail() || slot_data == 0 || slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
2147 actual_class_descriptor_sp = m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
2148 if (!actual_class_descriptor_sp)
2149 return ObjCLanguageRuntime::ClassDescriptorSP();
2150 m_cache[slot] = actual_class_descriptor_sp;
2153 data_payload = (((uint64_t)ptr << m_objc_debug_taggedpointer_payload_lshift) >> m_objc_debug_taggedpointer_payload_rshift);
2155 return ClassDescriptorSP(new ClassDescriptorV2Tagged(actual_class_descriptor_sp,data_payload));
2158 AppleObjCRuntimeV2::NonPointerISACache::NonPointerISACache (AppleObjCRuntimeV2& runtime,
2159 uint64_t objc_debug_isa_class_mask,
2160 uint64_t objc_debug_isa_magic_mask,
2161 uint64_t objc_debug_isa_magic_value) :
2164 m_objc_debug_isa_class_mask(objc_debug_isa_class_mask),
2165 m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask),
2166 m_objc_debug_isa_magic_value(objc_debug_isa_magic_value)
2170 ObjCLanguageRuntime::ClassDescriptorSP
2171 AppleObjCRuntimeV2::NonPointerISACache::GetClassDescriptor (ObjCISA isa)
2173 ObjCISA real_isa = 0;
2174 if (EvaluateNonPointerISA(isa, real_isa) == false)
2175 return ObjCLanguageRuntime::ClassDescriptorSP();
2176 auto cache_iter = m_cache.find(real_isa);
2177 if (cache_iter != m_cache.end())
2178 return cache_iter->second;
2179 auto descriptor_sp = m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(real_isa);
2180 if (descriptor_sp) // cache only positive matches since the table might grow
2181 m_cache[real_isa] = descriptor_sp;
2182 return descriptor_sp;
2186 AppleObjCRuntimeV2::NonPointerISACache::EvaluateNonPointerISA (ObjCISA isa, ObjCISA& ret_isa)
2188 if ( (isa & ~m_objc_debug_isa_class_mask) == 0)
2190 if ( (isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value)
2192 ret_isa = isa & m_objc_debug_isa_class_mask;
2193 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
2198 ObjCLanguageRuntime::EncodingToTypeSP
2199 AppleObjCRuntimeV2::GetEncodingToType ()
2201 if (!m_encoding_to_type_sp)
2202 m_encoding_to_type_sp.reset(new AppleObjCTypeEncodingParser(*this));
2203 return m_encoding_to_type_sp;
2206 lldb_private::AppleObjCRuntime::ObjCISA
2207 AppleObjCRuntimeV2::GetPointerISA (ObjCISA isa)
2211 if (m_non_pointer_isa_cache_ap)
2212 m_non_pointer_isa_cache_ap->EvaluateNonPointerISA(isa, ret);