1 //===-- ItaniumABILanguageRuntime.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 "ItaniumABILanguageRuntime.h"
10 
11 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/Mangled.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Core/ValueObject.h"
17 #include "lldb/Core/ValueObjectMemory.h"
18 #include "lldb/DataFormatters/FormattersHelpers.h"
19 #include "lldb/Expression/DiagnosticManager.h"
20 #include "lldb/Expression/FunctionCaller.h"
21 #include "lldb/Interpreter/CommandObject.h"
22 #include "lldb/Interpreter/CommandObjectMultiword.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Symbol/Symbol.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/TypeList.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/SectionLoadList.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/LLDBLog.h"
35 #include "lldb/Utility/Log.h"
36 #include "lldb/Utility/Scalar.h"
37 #include "lldb/Utility/Status.h"
38 
39 #include <vector>
40 
41 using namespace lldb;
42 using namespace lldb_private;
43 
44 LLDB_PLUGIN_DEFINE_ADV(ItaniumABILanguageRuntime, CXXItaniumABI)
45 
46 static const char *vtable_demangled_prefix = "vtable for ";
47 
48 char ItaniumABILanguageRuntime::ID = 0;
49 
CouldHaveDynamicValue(ValueObject & in_value)50 bool ItaniumABILanguageRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
51   const bool check_cxx = true;
52   const bool check_objc = false;
53   return in_value.GetCompilerType().IsPossibleDynamicType(nullptr, check_cxx,
54                                                           check_objc);
55 }
56 
GetTypeInfo(ValueObject & in_value,const VTableInfo & vtable_info)57 TypeAndOrName ItaniumABILanguageRuntime::GetTypeInfo(
58     ValueObject &in_value, const VTableInfo &vtable_info) {
59   if (vtable_info.addr.IsSectionOffset()) {
60     // See if we have cached info for this type already
61     TypeAndOrName type_info = GetDynamicTypeInfo(vtable_info.addr);
62     if (type_info)
63       return type_info;
64 
65     if (vtable_info.symbol) {
66       Log *log = GetLog(LLDBLog::Object);
67       llvm::StringRef symbol_name =
68           vtable_info.symbol->GetMangled().GetDemangledName().GetStringRef();
69       LLDB_LOGF(log,
70                 "0x%16.16" PRIx64
71                 ": static-type = '%s' has vtable symbol '%s'\n",
72                 in_value.GetPointerValue(),
73                 in_value.GetTypeName().GetCString(),
74                 symbol_name.str().c_str());
75       // We are a C++ class, that's good.  Get the class name and look it
76       // up:
77       llvm::StringRef class_name = symbol_name;
78       class_name.consume_front(vtable_demangled_prefix);
79       // We know the class name is absolute, so tell FindTypes that by
80       // prefixing it with the root namespace:
81       std::string lookup_name("::");
82       lookup_name.append(class_name.data(), class_name.size());
83 
84       type_info.SetName(class_name);
85       ConstString const_lookup_name(lookup_name);
86       TypeList class_types;
87       ModuleSP module_sp = vtable_info.symbol->CalculateSymbolContextModule();
88       // First look in the module that the vtable symbol came from and
89       // look for a single exact match.
90       TypeResults results;
91       TypeQuery query(const_lookup_name.GetStringRef(),
92                       TypeQueryOptions::e_exact_match |
93                           TypeQueryOptions::e_find_one);
94       if (module_sp) {
95         module_sp->FindTypes(query, results);
96         TypeSP type_sp = results.GetFirstType();
97         if (type_sp)
98           class_types.Insert(type_sp);
99       }
100 
101       // If we didn't find a symbol, then move on to the entire module
102       // list in the target and get as many unique matches as possible
103       if (class_types.Empty()) {
104         query.SetFindOne(false);
105         m_process->GetTarget().GetImages().FindTypes(nullptr, query, results);
106         for (const auto &type_sp : results.GetTypeMap().Types())
107           class_types.Insert(type_sp);
108       }
109 
110       lldb::TypeSP type_sp;
111       if (class_types.Empty()) {
112         LLDB_LOGF(log, "0x%16.16" PRIx64 ": is not dynamic\n",
113                   in_value.GetPointerValue());
114         return TypeAndOrName();
115       }
116       if (class_types.GetSize() == 1) {
117         type_sp = class_types.GetTypeAtIndex(0);
118         if (type_sp) {
119           if (TypeSystemClang::IsCXXClassType(
120                   type_sp->GetForwardCompilerType())) {
121             LLDB_LOGF(
122                 log,
123                 "0x%16.16" PRIx64
124                 ": static-type = '%s' has dynamic type: uid={0x%" PRIx64
125                 "}, type-name='%s'\n",
126                 in_value.GetPointerValue(), in_value.GetTypeName().AsCString(),
127                 type_sp->GetID(), type_sp->GetName().GetCString());
128             type_info.SetTypeSP(type_sp);
129           }
130         }
131       } else {
132         size_t i;
133         if (log) {
134           for (i = 0; i < class_types.GetSize(); i++) {
135             type_sp = class_types.GetTypeAtIndex(i);
136             if (type_sp) {
137               LLDB_LOGF(
138                   log,
139                   "0x%16.16" PRIx64
140                   ": static-type = '%s' has multiple matching dynamic "
141                   "types: uid={0x%" PRIx64 "}, type-name='%s'\n",
142                   in_value.GetPointerValue(),
143                   in_value.GetTypeName().AsCString(),
144                   type_sp->GetID(), type_sp->GetName().GetCString());
145             }
146           }
147         }
148 
149         for (i = 0; i < class_types.GetSize(); i++) {
150           type_sp = class_types.GetTypeAtIndex(i);
151           if (type_sp) {
152             if (TypeSystemClang::IsCXXClassType(
153                     type_sp->GetForwardCompilerType())) {
154               LLDB_LOGF(
155                   log,
156                   "0x%16.16" PRIx64 ": static-type = '%s' has multiple "
157                   "matching dynamic types, picking "
158                   "this one: uid={0x%" PRIx64 "}, type-name='%s'\n",
159                   in_value.GetPointerValue(),
160                   in_value.GetTypeName().AsCString(),
161                   type_sp->GetID(), type_sp->GetName().GetCString());
162               type_info.SetTypeSP(type_sp);
163             }
164           }
165         }
166 
167         if (log) {
168           LLDB_LOGF(log,
169                     "0x%16.16" PRIx64
170                     ": static-type = '%s' has multiple matching dynamic "
171                     "types, didn't find a C++ match\n",
172                     in_value.GetPointerValue(),
173                     in_value.GetTypeName().AsCString());
174         }
175       }
176       if (type_info)
177         SetDynamicTypeInfo(vtable_info.addr, type_info);
178       return type_info;
179     }
180   }
181   return TypeAndOrName();
182 }
183 
TypeHasVTable(CompilerType type)184 llvm::Error ItaniumABILanguageRuntime::TypeHasVTable(CompilerType type) {
185   // Check to make sure the class has a vtable.
186   CompilerType original_type = type;
187   if (type.IsPointerOrReferenceType()) {
188     CompilerType pointee_type = type.GetPointeeType();
189     if (pointee_type)
190       type = pointee_type;
191   }
192 
193   // Make sure this is a class or a struct first by checking the type class
194   // bitfield that gets returned.
195   if ((type.GetTypeClass() & (eTypeClassStruct | eTypeClassClass)) == 0) {
196     return llvm::createStringError(std::errc::invalid_argument,
197         "type \"%s\" is not a class or struct or a pointer to one",
198         original_type.GetTypeName().AsCString("<invalid>"));
199   }
200 
201   // Check if the type has virtual functions by asking it if it is polymorphic.
202   if (!type.IsPolymorphicClass()) {
203     return llvm::createStringError(std::errc::invalid_argument,
204         "type \"%s\" doesn't have a vtable",
205         type.GetTypeName().AsCString("<invalid>"));
206   }
207   return llvm::Error::success();
208 }
209 
210 // This function can accept both pointers or references to classes as well as
211 // instances of classes. If you are using this function during dynamic type
212 // detection, only valid ValueObjects that return true to
213 // CouldHaveDynamicValue(...) should call this function and \a check_type
214 // should be set to false. This function is also used by ValueObjectVTable
215 // and is can pass in instances of classes which is not suitable for dynamic
216 // type detection, these cases should pass true for \a check_type.
217 llvm::Expected<LanguageRuntime::VTableInfo>
GetVTableInfo(ValueObject & in_value,bool check_type)218  ItaniumABILanguageRuntime::GetVTableInfo(ValueObject &in_value,
219                                           bool check_type) {
220 
221   CompilerType type = in_value.GetCompilerType();
222   if (check_type) {
223     if (llvm::Error err = TypeHasVTable(type))
224       return std::move(err);
225   }
226   ExecutionContext exe_ctx(in_value.GetExecutionContextRef());
227   Process *process = exe_ctx.GetProcessPtr();
228   if (process == nullptr)
229     return llvm::createStringError(std::errc::invalid_argument,
230                                    "invalid process");
231 
232   AddressType address_type;
233   lldb::addr_t original_ptr = LLDB_INVALID_ADDRESS;
234   if (type.IsPointerOrReferenceType())
235     original_ptr = in_value.GetPointerValue(&address_type);
236   else
237     original_ptr = in_value.GetAddressOf(/*scalar_is_load_address=*/true,
238                                          &address_type);
239   if (original_ptr == LLDB_INVALID_ADDRESS || address_type != eAddressTypeLoad)
240     return llvm::createStringError(std::errc::invalid_argument,
241                                    "failed to get the address of the value");
242 
243   Status error;
244   lldb::addr_t vtable_load_addr =
245       process->ReadPointerFromMemory(original_ptr, error);
246 
247   if (!error.Success() || vtable_load_addr == LLDB_INVALID_ADDRESS)
248     return llvm::createStringError(std::errc::invalid_argument,
249         "failed to read vtable pointer from memory at 0x%" PRIx64,
250         original_ptr);
251 
252   // The vtable load address can have authentication bits with
253   // AArch64 targets on Darwin.
254   vtable_load_addr = process->FixDataAddress(vtable_load_addr);
255 
256   // Find the symbol that contains the "vtable_load_addr" address
257   Address vtable_addr;
258   if (!process->GetTarget().ResolveLoadAddress(vtable_load_addr, vtable_addr))
259     return llvm::createStringError(std::errc::invalid_argument,
260                                    "failed to resolve vtable pointer 0x%"
261                                    PRIx64 "to a section", vtable_load_addr);
262 
263   // Check our cache first to see if we already have this info
264   {
265     std::lock_guard<std::mutex> locker(m_mutex);
266     auto pos = m_vtable_info_map.find(vtable_addr);
267     if (pos != m_vtable_info_map.end())
268       return pos->second;
269   }
270 
271   Symbol *symbol = vtable_addr.CalculateSymbolContextSymbol();
272   if (symbol == nullptr)
273     return llvm::createStringError(std::errc::invalid_argument,
274                                    "no symbol found for 0x%" PRIx64,
275                                    vtable_load_addr);
276   llvm::StringRef name = symbol->GetMangled().GetDemangledName().GetStringRef();
277   if (name.starts_with(vtable_demangled_prefix)) {
278     VTableInfo info = {vtable_addr, symbol};
279     std::lock_guard<std::mutex> locker(m_mutex);
280     auto pos = m_vtable_info_map[vtable_addr] = info;
281     return info;
282   }
283   return llvm::createStringError(std::errc::invalid_argument,
284       "symbol found that contains 0x%" PRIx64 " is not a vtable symbol",
285       vtable_load_addr);
286 }
287 
GetDynamicTypeAndAddress(ValueObject & in_value,lldb::DynamicValueType use_dynamic,TypeAndOrName & class_type_or_name,Address & dynamic_address,Value::ValueType & value_type)288 bool ItaniumABILanguageRuntime::GetDynamicTypeAndAddress(
289     ValueObject &in_value, lldb::DynamicValueType use_dynamic,
290     TypeAndOrName &class_type_or_name, Address &dynamic_address,
291     Value::ValueType &value_type) {
292   // For Itanium, if the type has a vtable pointer in the object, it will be at
293   // offset 0 in the object.  That will point to the "address point" within the
294   // vtable (not the beginning of the vtable.)  We can then look up the symbol
295   // containing this "address point" and that symbol's name demangled will
296   // contain the full class name. The second pointer above the "address point"
297   // is the "offset_to_top".  We'll use that to get the start of the value
298   // object which holds the dynamic type.
299   //
300 
301   class_type_or_name.Clear();
302   value_type = Value::ValueType::Scalar;
303 
304   if (!CouldHaveDynamicValue(in_value))
305     return false;
306 
307   // Check if we have a vtable pointer in this value. If we don't it will
308   // return an error, else it will return a valid resolved address. We don't
309   // want GetVTableInfo to check the type since we accept void * as a possible
310   // dynamic type and that won't pass the type check. We already checked the
311   // type above in CouldHaveDynamicValue(...).
312   llvm::Expected<VTableInfo> vtable_info_or_err =
313       GetVTableInfo(in_value, /*check_type=*/false);
314   if (!vtable_info_or_err) {
315     llvm::consumeError(vtable_info_or_err.takeError());
316     return false;
317   }
318 
319   const VTableInfo &vtable_info = vtable_info_or_err.get();
320   class_type_or_name = GetTypeInfo(in_value, vtable_info);
321 
322   if (!class_type_or_name)
323     return false;
324 
325   CompilerType type = class_type_or_name.GetCompilerType();
326   // There can only be one type with a given name, so we've just found
327   // duplicate definitions, and this one will do as well as any other. We
328   // don't consider something to have a dynamic type if it is the same as
329   // the static type.  So compare against the value we were handed.
330   if (!type)
331     return true;
332 
333   if (TypeSystemClang::AreTypesSame(in_value.GetCompilerType(), type)) {
334     // The dynamic type we found was the same type, so we don't have a
335     // dynamic type here...
336     return false;
337   }
338 
339   // The offset_to_top is two pointers above the vtable pointer.
340   Target &target = m_process->GetTarget();
341   const addr_t vtable_load_addr = vtable_info.addr.GetLoadAddress(&target);
342   if (vtable_load_addr == LLDB_INVALID_ADDRESS)
343     return false;
344   const uint32_t addr_byte_size = m_process->GetAddressByteSize();
345   const lldb::addr_t offset_to_top_location =
346       vtable_load_addr - 2 * addr_byte_size;
347   // Watch for underflow, offset_to_top_location should be less than
348   // vtable_load_addr
349   if (offset_to_top_location >= vtable_load_addr)
350     return false;
351   Status error;
352   const int64_t offset_to_top = m_process->ReadSignedIntegerFromMemory(
353       offset_to_top_location, addr_byte_size, INT64_MIN, error);
354 
355   if (offset_to_top == INT64_MIN)
356     return false;
357   // So the dynamic type is a value that starts at offset_to_top above
358   // the original address.
359   lldb::addr_t dynamic_addr = in_value.GetPointerValue() + offset_to_top;
360   if (!m_process->GetTarget().ResolveLoadAddress(
361           dynamic_addr, dynamic_address)) {
362     dynamic_address.SetRawAddress(dynamic_addr);
363   }
364   return true;
365 }
366 
FixUpDynamicType(const TypeAndOrName & type_and_or_name,ValueObject & static_value)367 TypeAndOrName ItaniumABILanguageRuntime::FixUpDynamicType(
368     const TypeAndOrName &type_and_or_name, ValueObject &static_value) {
369   CompilerType static_type(static_value.GetCompilerType());
370   Flags static_type_flags(static_type.GetTypeInfo());
371 
372   TypeAndOrName ret(type_and_or_name);
373   if (type_and_or_name.HasType()) {
374     // The type will always be the type of the dynamic object.  If our parent's
375     // type was a pointer, then our type should be a pointer to the type of the
376     // dynamic object.  If a reference, then the original type should be
377     // okay...
378     CompilerType orig_type = type_and_or_name.GetCompilerType();
379     CompilerType corrected_type = orig_type;
380     if (static_type_flags.AllSet(eTypeIsPointer))
381       corrected_type = orig_type.GetPointerType();
382     else if (static_type_flags.AllSet(eTypeIsReference))
383       corrected_type = orig_type.GetLValueReferenceType();
384     ret.SetCompilerType(corrected_type);
385   } else {
386     // If we are here we need to adjust our dynamic type name to include the
387     // correct & or * symbol
388     std::string corrected_name(type_and_or_name.GetName().GetCString());
389     if (static_type_flags.AllSet(eTypeIsPointer))
390       corrected_name.append(" *");
391     else if (static_type_flags.AllSet(eTypeIsReference))
392       corrected_name.append(" &");
393     // the parent type should be a correctly pointer'ed or referenc'ed type
394     ret.SetCompilerType(static_type);
395     ret.SetName(corrected_name.c_str());
396   }
397   return ret;
398 }
399 
400 // Static Functions
401 LanguageRuntime *
CreateInstance(Process * process,lldb::LanguageType language)402 ItaniumABILanguageRuntime::CreateInstance(Process *process,
403                                           lldb::LanguageType language) {
404   // FIXME: We have to check the process and make sure we actually know that
405   // this process supports
406   // the Itanium ABI.
407   if (language == eLanguageTypeC_plus_plus ||
408       language == eLanguageTypeC_plus_plus_03 ||
409       language == eLanguageTypeC_plus_plus_11 ||
410       language == eLanguageTypeC_plus_plus_14)
411     return new ItaniumABILanguageRuntime(process);
412   else
413     return nullptr;
414 }
415 
416 class CommandObjectMultiwordItaniumABI_Demangle : public CommandObjectParsed {
417 public:
CommandObjectMultiwordItaniumABI_Demangle(CommandInterpreter & interpreter)418   CommandObjectMultiwordItaniumABI_Demangle(CommandInterpreter &interpreter)
419       : CommandObjectParsed(
420             interpreter, "demangle", "Demangle a C++ mangled name.",
421             "language cplusplus demangle [<mangled-name> ...]") {
422     CommandArgumentEntry arg;
423     CommandArgumentData index_arg;
424 
425     // Define the first (and only) variant of this arg.
426     index_arg.arg_type = eArgTypeSymbol;
427     index_arg.arg_repetition = eArgRepeatPlus;
428 
429     // There is only one variant this argument could be; put it into the
430     // argument entry.
431     arg.push_back(index_arg);
432 
433     // Push the data for the first argument into the m_arguments vector.
434     m_arguments.push_back(arg);
435   }
436 
437   ~CommandObjectMultiwordItaniumABI_Demangle() override = default;
438 
439 protected:
DoExecute(Args & command,CommandReturnObject & result)440   void DoExecute(Args &command, CommandReturnObject &result) override {
441     bool demangled_any = false;
442     bool error_any = false;
443     for (auto &entry : command.entries()) {
444       if (entry.ref().empty())
445         continue;
446 
447       // the actual Mangled class should be strict about this, but on the
448       // command line if you're copying mangled names out of 'nm' on Darwin,
449       // they will come out with an extra underscore - be willing to strip this
450       // on behalf of the user.   This is the moral equivalent of the -_/-n
451       // options to c++filt
452       auto name = entry.ref();
453       if (name.starts_with("__Z"))
454         name = name.drop_front();
455 
456       Mangled mangled(name);
457       if (mangled.GuessLanguage() == lldb::eLanguageTypeC_plus_plus) {
458         ConstString demangled(mangled.GetDisplayDemangledName());
459         demangled_any = true;
460         result.AppendMessageWithFormat("%s ---> %s\n", entry.c_str(),
461                                        demangled.GetCString());
462       } else {
463         error_any = true;
464         result.AppendErrorWithFormat("%s is not a valid C++ mangled name\n",
465                                      entry.ref().str().c_str());
466       }
467     }
468 
469     result.SetStatus(
470         error_any ? lldb::eReturnStatusFailed
471                   : (demangled_any ? lldb::eReturnStatusSuccessFinishResult
472                                    : lldb::eReturnStatusSuccessFinishNoResult));
473   }
474 };
475 
476 class CommandObjectMultiwordItaniumABI : public CommandObjectMultiword {
477 public:
CommandObjectMultiwordItaniumABI(CommandInterpreter & interpreter)478   CommandObjectMultiwordItaniumABI(CommandInterpreter &interpreter)
479       : CommandObjectMultiword(
480             interpreter, "cplusplus",
481             "Commands for operating on the C++ language runtime.",
482             "cplusplus <subcommand> [<subcommand-options>]") {
483     LoadSubCommand(
484         "demangle",
485         CommandObjectSP(
486             new CommandObjectMultiwordItaniumABI_Demangle(interpreter)));
487   }
488 
489   ~CommandObjectMultiwordItaniumABI() override = default;
490 };
491 
Initialize()492 void ItaniumABILanguageRuntime::Initialize() {
493   PluginManager::RegisterPlugin(
494       GetPluginNameStatic(), "Itanium ABI for the C++ language", CreateInstance,
495       [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
496         return CommandObjectSP(
497             new CommandObjectMultiwordItaniumABI(interpreter));
498       });
499 }
500 
Terminate()501 void ItaniumABILanguageRuntime::Terminate() {
502   PluginManager::UnregisterPlugin(CreateInstance);
503 }
504 
CreateExceptionResolver(const BreakpointSP & bkpt,bool catch_bp,bool throw_bp)505 BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
506     const BreakpointSP &bkpt, bool catch_bp, bool throw_bp) {
507   return CreateExceptionResolver(bkpt, catch_bp, throw_bp, false);
508 }
509 
CreateExceptionResolver(const BreakpointSP & bkpt,bool catch_bp,bool throw_bp,bool for_expressions)510 BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
511     const BreakpointSP &bkpt, bool catch_bp, bool throw_bp,
512     bool for_expressions) {
513   // One complication here is that most users DON'T want to stop at
514   // __cxa_allocate_expression, but until we can do anything better with
515   // predicting unwinding the expression parser does.  So we have two forms of
516   // the exception breakpoints, one for expressions that leaves out
517   // __cxa_allocate_exception, and one that includes it. The
518   // SetExceptionBreakpoints does the latter, the CreateExceptionBreakpoint in
519   // the runtime the former.
520   static const char *g_catch_name = "__cxa_begin_catch";
521   static const char *g_throw_name1 = "__cxa_throw";
522   static const char *g_throw_name2 = "__cxa_rethrow";
523   static const char *g_exception_throw_name = "__cxa_allocate_exception";
524   std::vector<const char *> exception_names;
525   exception_names.reserve(4);
526   if (catch_bp)
527     exception_names.push_back(g_catch_name);
528 
529   if (throw_bp) {
530     exception_names.push_back(g_throw_name1);
531     exception_names.push_back(g_throw_name2);
532   }
533 
534   if (for_expressions)
535     exception_names.push_back(g_exception_throw_name);
536 
537   BreakpointResolverSP resolver_sp(new BreakpointResolverName(
538       bkpt, exception_names.data(), exception_names.size(),
539       eFunctionNameTypeBase, eLanguageTypeUnknown, 0, eLazyBoolNo));
540 
541   return resolver_sp;
542 }
543 
CreateExceptionSearchFilter()544 lldb::SearchFilterSP ItaniumABILanguageRuntime::CreateExceptionSearchFilter() {
545   Target &target = m_process->GetTarget();
546 
547   FileSpecList filter_modules;
548   if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple) {
549     // Limit the number of modules that are searched for these breakpoints for
550     // Apple binaries.
551     filter_modules.EmplaceBack("libc++abi.dylib");
552     filter_modules.EmplaceBack("libSystem.B.dylib");
553     filter_modules.EmplaceBack("libc++abi.1.0.dylib");
554     filter_modules.EmplaceBack("libc++abi.1.dylib");
555   }
556   return target.GetSearchFilterForModuleList(&filter_modules);
557 }
558 
CreateExceptionBreakpoint(bool catch_bp,bool throw_bp,bool for_expressions,bool is_internal)559 lldb::BreakpointSP ItaniumABILanguageRuntime::CreateExceptionBreakpoint(
560     bool catch_bp, bool throw_bp, bool for_expressions, bool is_internal) {
561   Target &target = m_process->GetTarget();
562   FileSpecList filter_modules;
563   BreakpointResolverSP exception_resolver_sp =
564       CreateExceptionResolver(nullptr, catch_bp, throw_bp, for_expressions);
565   SearchFilterSP filter_sp(CreateExceptionSearchFilter());
566   const bool hardware = false;
567   const bool resolve_indirect_functions = false;
568   return target.CreateBreakpoint(filter_sp, exception_resolver_sp, is_internal,
569                                  hardware, resolve_indirect_functions);
570 }
571 
SetExceptionBreakpoints()572 void ItaniumABILanguageRuntime::SetExceptionBreakpoints() {
573   if (!m_process)
574     return;
575 
576   const bool catch_bp = false;
577   const bool throw_bp = true;
578   const bool is_internal = true;
579   const bool for_expressions = true;
580 
581   // For the exception breakpoints set by the Expression parser, we'll be a
582   // little more aggressive and stop at exception allocation as well.
583 
584   if (m_cxx_exception_bp_sp) {
585     m_cxx_exception_bp_sp->SetEnabled(true);
586   } else {
587     m_cxx_exception_bp_sp = CreateExceptionBreakpoint(
588         catch_bp, throw_bp, for_expressions, is_internal);
589     if (m_cxx_exception_bp_sp)
590       m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception");
591   }
592 }
593 
ClearExceptionBreakpoints()594 void ItaniumABILanguageRuntime::ClearExceptionBreakpoints() {
595   if (!m_process)
596     return;
597 
598   if (m_cxx_exception_bp_sp) {
599     m_cxx_exception_bp_sp->SetEnabled(false);
600   }
601 }
602 
ExceptionBreakpointsAreSet()603 bool ItaniumABILanguageRuntime::ExceptionBreakpointsAreSet() {
604   return m_cxx_exception_bp_sp && m_cxx_exception_bp_sp->IsEnabled();
605 }
606 
ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason)607 bool ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop(
608     lldb::StopInfoSP stop_reason) {
609   if (!m_process)
610     return false;
611 
612   if (!stop_reason || stop_reason->GetStopReason() != eStopReasonBreakpoint)
613     return false;
614 
615   uint64_t break_site_id = stop_reason->GetValue();
616   return m_process->GetBreakpointSiteList().StopPointSiteContainsBreakpoint(
617       break_site_id, m_cxx_exception_bp_sp->GetID());
618 }
619 
GetExceptionObjectForThread(ThreadSP thread_sp)620 ValueObjectSP ItaniumABILanguageRuntime::GetExceptionObjectForThread(
621     ThreadSP thread_sp) {
622   if (!thread_sp->SafeToCallFunctions())
623     return {};
624 
625   TypeSystemClangSP scratch_ts_sp =
626       ScratchTypeSystemClang::GetForTarget(m_process->GetTarget());
627   if (!scratch_ts_sp)
628     return {};
629 
630   CompilerType voidstar =
631       scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
632 
633   DiagnosticManager diagnostics;
634   ExecutionContext exe_ctx;
635   EvaluateExpressionOptions options;
636 
637   options.SetUnwindOnError(true);
638   options.SetIgnoreBreakpoints(true);
639   options.SetStopOthers(true);
640   options.SetTimeout(m_process->GetUtilityExpressionTimeout());
641   options.SetTryAllThreads(false);
642   thread_sp->CalculateExecutionContext(exe_ctx);
643 
644   const ModuleList &modules = m_process->GetTarget().GetImages();
645   SymbolContextList contexts;
646   SymbolContext context;
647 
648   modules.FindSymbolsWithNameAndType(
649       ConstString("__cxa_current_exception_type"), eSymbolTypeCode, contexts);
650   contexts.GetContextAtIndex(0, context);
651   if (!context.symbol) {
652     return {};
653   }
654   Address addr = context.symbol->GetAddress();
655 
656   Status error;
657   FunctionCaller *function_caller =
658       m_process->GetTarget().GetFunctionCallerForLanguage(
659           eLanguageTypeC, voidstar, addr, ValueList(), "caller", error);
660 
661   ExpressionResults func_call_ret;
662   Value results;
663   func_call_ret = function_caller->ExecuteFunction(exe_ctx, nullptr, options,
664                                                    diagnostics, results);
665   if (func_call_ret != eExpressionCompleted || !error.Success()) {
666     return ValueObjectSP();
667   }
668 
669   size_t ptr_size = m_process->GetAddressByteSize();
670   addr_t result_ptr = results.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
671   addr_t exception_addr =
672       m_process->ReadPointerFromMemory(result_ptr - ptr_size, error);
673 
674   if (!error.Success()) {
675     return ValueObjectSP();
676   }
677 
678   lldb_private::formatters::InferiorSizedWord exception_isw(exception_addr,
679                                                             *m_process);
680   ValueObjectSP exception = ValueObject::CreateValueObjectFromData(
681       "exception", exception_isw.GetAsData(m_process->GetByteOrder()), exe_ctx,
682       voidstar);
683   ValueObjectSP dyn_exception
684       = exception->GetDynamicValue(eDynamicDontRunTarget);
685   // If we succeed in making a dynamic value, return that:
686   if (dyn_exception)
687      return dyn_exception;
688 
689   return exception;
690 }
691 
GetDynamicTypeInfo(const lldb_private::Address & vtable_addr)692 TypeAndOrName ItaniumABILanguageRuntime::GetDynamicTypeInfo(
693     const lldb_private::Address &vtable_addr) {
694   std::lock_guard<std::mutex> locker(m_mutex);
695   DynamicTypeCache::const_iterator pos = m_dynamic_type_map.find(vtable_addr);
696   if (pos == m_dynamic_type_map.end())
697     return TypeAndOrName();
698   else
699     return pos->second;
700 }
701 
SetDynamicTypeInfo(const lldb_private::Address & vtable_addr,const TypeAndOrName & type_info)702 void ItaniumABILanguageRuntime::SetDynamicTypeInfo(
703     const lldb_private::Address &vtable_addr, const TypeAndOrName &type_info) {
704   std::lock_guard<std::mutex> locker(m_mutex);
705   m_dynamic_type_map[vtable_addr] = type_info;
706 }
707