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