• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- AppleObjCRuntime.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "AppleObjCRuntime.h"
10 #include "AppleObjCRuntimeV1.h"
11 #include "AppleObjCRuntimeV2.h"
12 #include "AppleObjCTrampolineHandler.h"
13 #include "Plugins/Language/ObjC/NSString.h"
14 #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
15 #include "Plugins/Process/Utility/HistoryThread.h"
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleList.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Core/ValueObjectConstResult.h"
23 #include "lldb/DataFormatters/FormattersHelpers.h"
24 #include "lldb/Expression/DiagnosticManager.h"
25 #include "lldb/Expression/FunctionCaller.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Utility/ConstString.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Scalar.h"
36 #include "lldb/Utility/Status.h"
37 #include "lldb/Utility/StreamString.h"
38 #include "clang/AST/Type.h"
39 
40 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
41 
42 #include <vector>
43 
44 using namespace lldb;
45 using namespace lldb_private;
46 
47 LLDB_PLUGIN_DEFINE(AppleObjCRuntime)
48 
49 char AppleObjCRuntime::ID = 0;
50 
~AppleObjCRuntime()51 AppleObjCRuntime::~AppleObjCRuntime() {}
52 
AppleObjCRuntime(Process * process)53 AppleObjCRuntime::AppleObjCRuntime(Process *process)
54     : ObjCLanguageRuntime(process), m_read_objc_library(false),
55       m_objc_trampoline_handler_up(), m_Foundation_major() {
56   ReadObjCLibraryIfNeeded(process->GetTarget().GetImages());
57 }
58 
Initialize()59 void AppleObjCRuntime::Initialize() {
60   AppleObjCRuntimeV2::Initialize();
61   AppleObjCRuntimeV1::Initialize();
62 }
63 
Terminate()64 void AppleObjCRuntime::Terminate() {
65   AppleObjCRuntimeV2::Terminate();
66   AppleObjCRuntimeV1::Terminate();
67 }
68 
GetObjectDescription(Stream & str,ValueObject & valobj)69 bool AppleObjCRuntime::GetObjectDescription(Stream &str, ValueObject &valobj) {
70   CompilerType compiler_type(valobj.GetCompilerType());
71   bool is_signed;
72   // ObjC objects can only be pointers (or numbers that actually represents
73   // pointers but haven't been typecast, because reasons..)
74   if (!compiler_type.IsIntegerType(is_signed) && !compiler_type.IsPointerType())
75     return false;
76 
77   // Make the argument list: we pass one arg, the address of our pointer, to
78   // the print function.
79   Value val;
80 
81   if (!valobj.ResolveValue(val.GetScalar()))
82     return false;
83 
84   // Value Objects may not have a process in their ExecutionContextRef.  But we
85   // need to have one in the ref we pass down to eventually call description.
86   // Get it from the target if it isn't present.
87   ExecutionContext exe_ctx;
88   if (valobj.GetProcessSP()) {
89     exe_ctx = ExecutionContext(valobj.GetExecutionContextRef());
90   } else {
91     exe_ctx.SetContext(valobj.GetTargetSP(), true);
92     if (!exe_ctx.HasProcessScope())
93       return false;
94   }
95   return GetObjectDescription(str, val, exe_ctx.GetBestExecutionContextScope());
96 }
GetObjectDescription(Stream & strm,Value & value,ExecutionContextScope * exe_scope)97 bool AppleObjCRuntime::GetObjectDescription(Stream &strm, Value &value,
98                                             ExecutionContextScope *exe_scope) {
99   if (!m_read_objc_library)
100     return false;
101 
102   ExecutionContext exe_ctx;
103   exe_scope->CalculateExecutionContext(exe_ctx);
104   Process *process = exe_ctx.GetProcessPtr();
105   if (!process)
106     return false;
107 
108   // We need other parts of the exe_ctx, but the processes have to match.
109   assert(m_process == process);
110 
111   // Get the function address for the print function.
112   const Address *function_address = GetPrintForDebuggerAddr();
113   if (!function_address)
114     return false;
115 
116   Target *target = exe_ctx.GetTargetPtr();
117   CompilerType compiler_type = value.GetCompilerType();
118   if (compiler_type) {
119     if (!TypeSystemClang::IsObjCObjectPointerType(compiler_type)) {
120       strm.Printf("Value doesn't point to an ObjC object.\n");
121       return false;
122     }
123   } else {
124     // If it is not a pointer, see if we can make it into a pointer.
125     TypeSystemClang *ast_context =
126         ScratchTypeSystemClang::GetForTarget(*target);
127     if (!ast_context)
128       return false;
129 
130     CompilerType opaque_type = ast_context->GetBasicType(eBasicTypeObjCID);
131     if (!opaque_type)
132       opaque_type = ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
133     // value.SetContext(Value::eContextTypeClangType, opaque_type_ptr);
134     value.SetCompilerType(opaque_type);
135   }
136 
137   ValueList arg_value_list;
138   arg_value_list.PushValue(value);
139 
140   // This is the return value:
141   TypeSystemClang *ast_context = ScratchTypeSystemClang::GetForTarget(*target);
142   if (!ast_context)
143     return false;
144 
145   CompilerType return_compiler_type = ast_context->GetCStringType(true);
146   Value ret;
147   //    ret.SetContext(Value::eContextTypeClangType, return_compiler_type);
148   ret.SetCompilerType(return_compiler_type);
149 
150   if (exe_ctx.GetFramePtr() == nullptr) {
151     Thread *thread = exe_ctx.GetThreadPtr();
152     if (thread == nullptr) {
153       exe_ctx.SetThreadSP(process->GetThreadList().GetSelectedThread());
154       thread = exe_ctx.GetThreadPtr();
155     }
156     if (thread) {
157       exe_ctx.SetFrameSP(thread->GetSelectedFrame());
158     }
159   }
160 
161   // Now we're ready to call the function:
162 
163   DiagnosticManager diagnostics;
164   lldb::addr_t wrapper_struct_addr = LLDB_INVALID_ADDRESS;
165 
166   if (!m_print_object_caller_up) {
167     Status error;
168     m_print_object_caller_up.reset(
169         exe_scope->CalculateTarget()->GetFunctionCallerForLanguage(
170             eLanguageTypeObjC, return_compiler_type, *function_address,
171             arg_value_list, "objc-object-description", error));
172     if (error.Fail()) {
173       m_print_object_caller_up.reset();
174       strm.Printf("Could not get function runner to call print for debugger "
175                   "function: %s.",
176                   error.AsCString());
177       return false;
178     }
179     m_print_object_caller_up->InsertFunction(exe_ctx, wrapper_struct_addr,
180                                              diagnostics);
181   } else {
182     m_print_object_caller_up->WriteFunctionArguments(
183         exe_ctx, wrapper_struct_addr, arg_value_list, diagnostics);
184   }
185 
186   EvaluateExpressionOptions options;
187   options.SetUnwindOnError(true);
188   options.SetTryAllThreads(true);
189   options.SetStopOthers(true);
190   options.SetIgnoreBreakpoints(true);
191   options.SetTimeout(process->GetUtilityExpressionTimeout());
192   options.SetIsForUtilityExpr(true);
193 
194   ExpressionResults results = m_print_object_caller_up->ExecuteFunction(
195       exe_ctx, &wrapper_struct_addr, options, diagnostics, ret);
196   if (results != eExpressionCompleted) {
197     strm.Printf("Error evaluating Print Object function: %d.\n", results);
198     return false;
199   }
200 
201   addr_t result_ptr = ret.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
202 
203   char buf[512];
204   size_t cstr_len = 0;
205   size_t full_buffer_len = sizeof(buf) - 1;
206   size_t curr_len = full_buffer_len;
207   while (curr_len == full_buffer_len) {
208     Status error;
209     curr_len = process->ReadCStringFromMemory(result_ptr + cstr_len, buf,
210                                               sizeof(buf), error);
211     strm.Write(buf, curr_len);
212     cstr_len += curr_len;
213   }
214   return cstr_len > 0;
215 }
216 
GetObjCModule()217 lldb::ModuleSP AppleObjCRuntime::GetObjCModule() {
218   ModuleSP module_sp(m_objc_module_wp.lock());
219   if (module_sp)
220     return module_sp;
221 
222   Process *process = GetProcess();
223   if (process) {
224     const ModuleList &modules = process->GetTarget().GetImages();
225     for (uint32_t idx = 0; idx < modules.GetSize(); idx++) {
226       module_sp = modules.GetModuleAtIndex(idx);
227       if (AppleObjCRuntime::AppleIsModuleObjCLibrary(module_sp)) {
228         m_objc_module_wp = module_sp;
229         return module_sp;
230       }
231     }
232   }
233   return ModuleSP();
234 }
235 
GetPrintForDebuggerAddr()236 Address *AppleObjCRuntime::GetPrintForDebuggerAddr() {
237   if (!m_PrintForDebugger_addr) {
238     const ModuleList &modules = m_process->GetTarget().GetImages();
239 
240     SymbolContextList contexts;
241     SymbolContext context;
242 
243     modules.FindSymbolsWithNameAndType(ConstString("_NSPrintForDebugger"),
244                                         eSymbolTypeCode, contexts);
245     if (contexts.IsEmpty()) {
246       modules.FindSymbolsWithNameAndType(ConstString("_CFPrintForDebugger"),
247                                          eSymbolTypeCode, contexts);
248       if (contexts.IsEmpty())
249         return nullptr;
250     }
251 
252     contexts.GetContextAtIndex(0, context);
253 
254     m_PrintForDebugger_addr =
255         std::make_unique<Address>(context.symbol->GetAddress());
256   }
257 
258   return m_PrintForDebugger_addr.get();
259 }
260 
CouldHaveDynamicValue(ValueObject & in_value)261 bool AppleObjCRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
262   return in_value.GetCompilerType().IsPossibleDynamicType(
263       nullptr,
264       false, // do not check C++
265       true); // check ObjC
266 }
267 
GetDynamicTypeAndAddress(ValueObject & in_value,lldb::DynamicValueType use_dynamic,TypeAndOrName & class_type_or_name,Address & address,Value::ValueType & value_type)268 bool AppleObjCRuntime::GetDynamicTypeAndAddress(
269     ValueObject &in_value, lldb::DynamicValueType use_dynamic,
270     TypeAndOrName &class_type_or_name, Address &address,
271     Value::ValueType &value_type) {
272   return false;
273 }
274 
275 TypeAndOrName
FixUpDynamicType(const TypeAndOrName & type_and_or_name,ValueObject & static_value)276 AppleObjCRuntime::FixUpDynamicType(const TypeAndOrName &type_and_or_name,
277                                    ValueObject &static_value) {
278   CompilerType static_type(static_value.GetCompilerType());
279   Flags static_type_flags(static_type.GetTypeInfo());
280 
281   TypeAndOrName ret(type_and_or_name);
282   if (type_and_or_name.HasType()) {
283     // The type will always be the type of the dynamic object.  If our parent's
284     // type was a pointer, then our type should be a pointer to the type of the
285     // dynamic object.  If a reference, then the original type should be
286     // okay...
287     CompilerType orig_type = type_and_or_name.GetCompilerType();
288     CompilerType corrected_type = orig_type;
289     if (static_type_flags.AllSet(eTypeIsPointer))
290       corrected_type = orig_type.GetPointerType();
291     ret.SetCompilerType(corrected_type);
292   } else {
293     // If we are here we need to adjust our dynamic type name to include the
294     // correct & or * symbol
295     std::string corrected_name(type_and_or_name.GetName().GetCString());
296     if (static_type_flags.AllSet(eTypeIsPointer))
297       corrected_name.append(" *");
298     // the parent type should be a correctly pointer'ed or referenc'ed type
299     ret.SetCompilerType(static_type);
300     ret.SetName(corrected_name.c_str());
301   }
302   return ret;
303 }
304 
AppleIsModuleObjCLibrary(const ModuleSP & module_sp)305 bool AppleObjCRuntime::AppleIsModuleObjCLibrary(const ModuleSP &module_sp) {
306   if (module_sp) {
307     const FileSpec &module_file_spec = module_sp->GetFileSpec();
308     static ConstString ObjCName("libobjc.A.dylib");
309 
310     if (module_file_spec) {
311       if (module_file_spec.GetFilename() == ObjCName)
312         return true;
313     }
314   }
315   return false;
316 }
317 
318 // we use the version of Foundation to make assumptions about the ObjC runtime
319 // on a target
GetFoundationVersion()320 uint32_t AppleObjCRuntime::GetFoundationVersion() {
321   if (!m_Foundation_major.hasValue()) {
322     const ModuleList &modules = m_process->GetTarget().GetImages();
323     for (uint32_t idx = 0; idx < modules.GetSize(); idx++) {
324       lldb::ModuleSP module_sp = modules.GetModuleAtIndex(idx);
325       if (!module_sp)
326         continue;
327       if (strcmp(module_sp->GetFileSpec().GetFilename().AsCString(""),
328                  "Foundation") == 0) {
329         m_Foundation_major = module_sp->GetVersion().getMajor();
330         return *m_Foundation_major;
331       }
332     }
333     return LLDB_INVALID_MODULE_VERSION;
334   } else
335     return m_Foundation_major.getValue();
336 }
337 
GetValuesForGlobalCFBooleans(lldb::addr_t & cf_true,lldb::addr_t & cf_false)338 void AppleObjCRuntime::GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true,
339                                                     lldb::addr_t &cf_false) {
340   cf_true = cf_false = LLDB_INVALID_ADDRESS;
341 }
342 
IsModuleObjCLibrary(const ModuleSP & module_sp)343 bool AppleObjCRuntime::IsModuleObjCLibrary(const ModuleSP &module_sp) {
344   return AppleIsModuleObjCLibrary(module_sp);
345 }
346 
ReadObjCLibrary(const ModuleSP & module_sp)347 bool AppleObjCRuntime::ReadObjCLibrary(const ModuleSP &module_sp) {
348   // Maybe check here and if we have a handler already, and the UUID of this
349   // module is the same as the one in the current module, then we don't have to
350   // reread it?
351   m_objc_trampoline_handler_up = std::make_unique<AppleObjCTrampolineHandler>(
352       m_process->shared_from_this(), module_sp);
353   if (m_objc_trampoline_handler_up != nullptr) {
354     m_read_objc_library = true;
355     return true;
356   } else
357     return false;
358 }
359 
GetStepThroughTrampolinePlan(Thread & thread,bool stop_others)360 ThreadPlanSP AppleObjCRuntime::GetStepThroughTrampolinePlan(Thread &thread,
361                                                             bool stop_others) {
362   ThreadPlanSP thread_plan_sp;
363   if (m_objc_trampoline_handler_up)
364     thread_plan_sp = m_objc_trampoline_handler_up->GetStepThroughDispatchPlan(
365         thread, stop_others);
366   return thread_plan_sp;
367 }
368 
369 // Static Functions
370 ObjCLanguageRuntime::ObjCRuntimeVersions
GetObjCVersion(Process * process,ModuleSP & objc_module_sp)371 AppleObjCRuntime::GetObjCVersion(Process *process, ModuleSP &objc_module_sp) {
372   if (!process)
373     return ObjCRuntimeVersions::eObjC_VersionUnknown;
374 
375   Target &target = process->GetTarget();
376   if (target.GetArchitecture().GetTriple().getVendor() !=
377       llvm::Triple::VendorType::Apple)
378     return ObjCRuntimeVersions::eObjC_VersionUnknown;
379 
380   const ModuleList &target_modules = target.GetImages();
381   std::lock_guard<std::recursive_mutex> gaurd(target_modules.GetMutex());
382 
383   size_t num_images = target_modules.GetSize();
384   for (size_t i = 0; i < num_images; i++) {
385     ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
386     // One tricky bit here is that we might get called as part of the initial
387     // module loading, but before all the pre-run libraries get winnowed from
388     // the module list.  So there might actually be an old and incorrect ObjC
389     // library sitting around in the list, and we don't want to look at that.
390     // That's why we call IsLoadedInTarget.
391 
392     if (AppleIsModuleObjCLibrary(module_sp) &&
393         module_sp->IsLoadedInTarget(&target)) {
394       objc_module_sp = module_sp;
395       ObjectFile *ofile = module_sp->GetObjectFile();
396       if (!ofile)
397         return ObjCRuntimeVersions::eObjC_VersionUnknown;
398 
399       SectionList *sections = module_sp->GetSectionList();
400       if (!sections)
401         return ObjCRuntimeVersions::eObjC_VersionUnknown;
402       SectionSP v1_telltale_section_sp =
403           sections->FindSectionByName(ConstString("__OBJC"));
404       if (v1_telltale_section_sp) {
405         return ObjCRuntimeVersions::eAppleObjC_V1;
406       }
407       return ObjCRuntimeVersions::eAppleObjC_V2;
408     }
409   }
410 
411   return ObjCRuntimeVersions::eObjC_VersionUnknown;
412 }
413 
SetExceptionBreakpoints()414 void AppleObjCRuntime::SetExceptionBreakpoints() {
415   const bool catch_bp = false;
416   const bool throw_bp = true;
417   const bool is_internal = true;
418 
419   if (!m_objc_exception_bp_sp) {
420     m_objc_exception_bp_sp = LanguageRuntime::CreateExceptionBreakpoint(
421         m_process->GetTarget(), GetLanguageType(), catch_bp, throw_bp,
422         is_internal);
423     if (m_objc_exception_bp_sp)
424       m_objc_exception_bp_sp->SetBreakpointKind("ObjC exception");
425   } else
426     m_objc_exception_bp_sp->SetEnabled(true);
427 }
428 
ClearExceptionBreakpoints()429 void AppleObjCRuntime::ClearExceptionBreakpoints() {
430   if (!m_process)
431     return;
432 
433   if (m_objc_exception_bp_sp.get()) {
434     m_objc_exception_bp_sp->SetEnabled(false);
435   }
436 }
437 
ExceptionBreakpointsAreSet()438 bool AppleObjCRuntime::ExceptionBreakpointsAreSet() {
439   return m_objc_exception_bp_sp && m_objc_exception_bp_sp->IsEnabled();
440 }
441 
ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason)442 bool AppleObjCRuntime::ExceptionBreakpointsExplainStop(
443     lldb::StopInfoSP stop_reason) {
444   if (!m_process)
445     return false;
446 
447   if (!stop_reason || stop_reason->GetStopReason() != eStopReasonBreakpoint)
448     return false;
449 
450   uint64_t break_site_id = stop_reason->GetValue();
451   return m_process->GetBreakpointSiteList().BreakpointSiteContainsBreakpoint(
452       break_site_id, m_objc_exception_bp_sp->GetID());
453 }
454 
CalculateHasNewLiteralsAndIndexing()455 bool AppleObjCRuntime::CalculateHasNewLiteralsAndIndexing() {
456   if (!m_process)
457     return false;
458 
459   Target &target(m_process->GetTarget());
460 
461   static ConstString s_method_signature(
462       "-[NSDictionary objectForKeyedSubscript:]");
463   static ConstString s_arclite_method_signature(
464       "__arclite_objectForKeyedSubscript");
465 
466   SymbolContextList sc_list;
467 
468   target.GetImages().FindSymbolsWithNameAndType(s_method_signature,
469                                                 eSymbolTypeCode, sc_list);
470   if (sc_list.IsEmpty())
471     target.GetImages().FindSymbolsWithNameAndType(s_arclite_method_signature,
472                                                   eSymbolTypeCode, sc_list);
473   return !sc_list.IsEmpty();
474 }
475 
CreateExceptionSearchFilter()476 lldb::SearchFilterSP AppleObjCRuntime::CreateExceptionSearchFilter() {
477   Target &target = m_process->GetTarget();
478 
479   FileSpecList filter_modules;
480   if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple) {
481     filter_modules.Append(std::get<0>(GetExceptionThrowLocation()));
482   }
483   return target.GetSearchFilterForModuleList(&filter_modules);
484 }
485 
GetExceptionObjectForThread(ThreadSP thread_sp)486 ValueObjectSP AppleObjCRuntime::GetExceptionObjectForThread(
487     ThreadSP thread_sp) {
488   auto *cpp_runtime = m_process->GetLanguageRuntime(eLanguageTypeC_plus_plus);
489   if (!cpp_runtime) return ValueObjectSP();
490   auto cpp_exception = cpp_runtime->GetExceptionObjectForThread(thread_sp);
491   if (!cpp_exception) return ValueObjectSP();
492 
493   auto descriptor = GetClassDescriptor(*cpp_exception);
494   if (!descriptor || !descriptor->IsValid()) return ValueObjectSP();
495 
496   while (descriptor) {
497     ConstString class_name(descriptor->GetClassName());
498     if (class_name == "NSException")
499       return cpp_exception;
500     descriptor = descriptor->GetSuperclass();
501   }
502 
503   return ValueObjectSP();
504 }
505 
506 /// Utility method for error handling in GetBacktraceThreadFromException.
507 /// \param msg The message to add to the log.
508 /// \return An invalid ThreadSP to be returned from
509 ///         GetBacktraceThreadFromException.
510 LLVM_NODISCARD
FailExceptionParsing(llvm::StringRef msg)511 static ThreadSP FailExceptionParsing(llvm::StringRef msg) {
512   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_LANGUAGE));
513   LLDB_LOG(log, "Failed getting backtrace from exception: {0}", msg);
514   return ThreadSP();
515 }
516 
GetBacktraceThreadFromException(lldb::ValueObjectSP exception_sp)517 ThreadSP AppleObjCRuntime::GetBacktraceThreadFromException(
518     lldb::ValueObjectSP exception_sp) {
519   ValueObjectSP reserved_dict =
520       exception_sp->GetChildMemberWithName(ConstString("reserved"), true);
521   if (!reserved_dict)
522     return FailExceptionParsing("Failed to get 'reserved' member.");
523 
524   reserved_dict = reserved_dict->GetSyntheticValue();
525   if (!reserved_dict)
526     return FailExceptionParsing("Failed to get synthetic value.");
527 
528   TypeSystemClang *clang_ast_context =
529       ScratchTypeSystemClang::GetForTarget(*exception_sp->GetTargetSP());
530   if (!clang_ast_context)
531     return FailExceptionParsing("Failed to get scratch AST.");
532   CompilerType objc_id =
533       clang_ast_context->GetBasicType(lldb::eBasicTypeObjCID);
534   ValueObjectSP return_addresses;
535 
536   auto objc_object_from_address = [&exception_sp, &objc_id](uint64_t addr,
537                                                             const char *name) {
538     Value value(addr);
539     value.SetCompilerType(objc_id);
540     auto object = ValueObjectConstResult::Create(
541         exception_sp->GetTargetSP().get(), value, ConstString(name));
542     object = object->GetDynamicValue(eDynamicDontRunTarget);
543     return object;
544   };
545 
546   for (size_t idx = 0; idx < reserved_dict->GetNumChildren(); idx++) {
547     ValueObjectSP dict_entry = reserved_dict->GetChildAtIndex(idx, true);
548 
549     DataExtractor data;
550     data.SetAddressByteSize(dict_entry->GetProcessSP()->GetAddressByteSize());
551     Status error;
552     dict_entry->GetData(data, error);
553     if (error.Fail()) return ThreadSP();
554 
555     lldb::offset_t data_offset = 0;
556     auto dict_entry_key = data.GetAddress(&data_offset);
557     auto dict_entry_value = data.GetAddress(&data_offset);
558 
559     auto key_nsstring = objc_object_from_address(dict_entry_key, "key");
560     StreamString key_summary;
561     if (lldb_private::formatters::NSStringSummaryProvider(
562             *key_nsstring, key_summary, TypeSummaryOptions()) &&
563         !key_summary.Empty()) {
564       if (key_summary.GetString() == "\"callStackReturnAddresses\"") {
565         return_addresses = objc_object_from_address(dict_entry_value,
566                                                     "callStackReturnAddresses");
567         break;
568       }
569     }
570   }
571 
572   if (!return_addresses)
573     return FailExceptionParsing("Failed to get return addresses.");
574   auto frames_value =
575       return_addresses->GetChildMemberWithName(ConstString("_frames"), true);
576   if (!frames_value)
577     return FailExceptionParsing("Failed to get frames_value.");
578   addr_t frames_addr = frames_value->GetValueAsUnsigned(0);
579   auto count_value =
580       return_addresses->GetChildMemberWithName(ConstString("_cnt"), true);
581   if (!count_value)
582     return FailExceptionParsing("Failed to get count_value.");
583   size_t count = count_value->GetValueAsUnsigned(0);
584   auto ignore_value =
585       return_addresses->GetChildMemberWithName(ConstString("_ignore"), true);
586   if (!ignore_value)
587     return FailExceptionParsing("Failed to get ignore_value.");
588   size_t ignore = ignore_value->GetValueAsUnsigned(0);
589 
590   size_t ptr_size = m_process->GetAddressByteSize();
591   std::vector<lldb::addr_t> pcs;
592   for (size_t idx = 0; idx < count; idx++) {
593     Status error;
594     addr_t pc = m_process->ReadPointerFromMemory(
595         frames_addr + (ignore + idx) * ptr_size, error);
596     pcs.push_back(pc);
597   }
598 
599   if (pcs.empty())
600     return FailExceptionParsing("Failed to get PC list.");
601 
602   ThreadSP new_thread_sp(new HistoryThread(*m_process, 0, pcs));
603   m_process->GetExtendedThreadList().AddThread(new_thread_sp);
604   return new_thread_sp;
605 }
606 
607 std::tuple<FileSpec, ConstString>
GetExceptionThrowLocation()608 AppleObjCRuntime::GetExceptionThrowLocation() {
609   return std::make_tuple(
610       FileSpec("libobjc.A.dylib"), ConstString("objc_exception_throw"));
611 }
612 
ReadObjCLibraryIfNeeded(const ModuleList & module_list)613 void AppleObjCRuntime::ReadObjCLibraryIfNeeded(const ModuleList &module_list) {
614   if (!HasReadObjCLibrary()) {
615     std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
616 
617     size_t num_modules = module_list.GetSize();
618     for (size_t i = 0; i < num_modules; i++) {
619       auto mod = module_list.GetModuleAtIndex(i);
620       if (IsModuleObjCLibrary(mod)) {
621         ReadObjCLibrary(mod);
622         break;
623       }
624     }
625   }
626 }
627 
ModulesDidLoad(const ModuleList & module_list)628 void AppleObjCRuntime::ModulesDidLoad(const ModuleList &module_list) {
629   ReadObjCLibraryIfNeeded(module_list);
630 }
631