15ffd83dbSDimitry Andric //===-- ItaniumABILanguageRuntime.cpp -------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "ItaniumABILanguageRuntime.h"
100b57cec5SDimitry Andric 
115ffd83dbSDimitry Andric #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
120b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
130b57cec5SDimitry Andric #include "lldb/Core/Mangled.h"
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
160b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
170b57cec5SDimitry Andric #include "lldb/Core/ValueObjectMemory.h"
180b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
190b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
200b57cec5SDimitry Andric #include "lldb/Expression/FunctionCaller.h"
210b57cec5SDimitry Andric #include "lldb/Interpreter/CommandObject.h"
220b57cec5SDimitry Andric #include "lldb/Interpreter/CommandObjectMultiword.h"
230b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
260b57cec5SDimitry Andric #include "lldb/Symbol/TypeList.h"
270b57cec5SDimitry Andric #include "lldb/Target/Process.h"
280b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
290b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
300b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
310b57cec5SDimitry Andric #include "lldb/Target/Target.h"
320b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
330b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
3481ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
350b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
360b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
370b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric #include <vector>
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric using namespace lldb;
420b57cec5SDimitry Andric using namespace lldb_private;
430b57cec5SDimitry Andric 
445ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE_ADV(ItaniumABILanguageRuntime, CXXItaniumABI)
455ffd83dbSDimitry Andric 
460b57cec5SDimitry Andric static const char *vtable_demangled_prefix = "vtable for ";
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric char ItaniumABILanguageRuntime::ID = 0;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric bool ItaniumABILanguageRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
510b57cec5SDimitry Andric   const bool check_cxx = true;
520b57cec5SDimitry Andric   const bool check_objc = false;
530b57cec5SDimitry Andric   return in_value.GetCompilerType().IsPossibleDynamicType(nullptr, check_cxx,
540b57cec5SDimitry Andric                                                           check_objc);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
575f757f3fSDimitry Andric TypeAndOrName ItaniumABILanguageRuntime::GetTypeInfo(
585f757f3fSDimitry Andric     ValueObject &in_value, const VTableInfo &vtable_info) {
595f757f3fSDimitry Andric   if (vtable_info.addr.IsSectionOffset()) {
600b57cec5SDimitry Andric     // See if we have cached info for this type already
615f757f3fSDimitry Andric     TypeAndOrName type_info = GetDynamicTypeInfo(vtable_info.addr);
620b57cec5SDimitry Andric     if (type_info)
630b57cec5SDimitry Andric       return type_info;
640b57cec5SDimitry Andric 
655f757f3fSDimitry Andric     if (vtable_info.symbol) {
6681ad6265SDimitry Andric       Log *log = GetLog(LLDBLog::Object);
675f757f3fSDimitry Andric       llvm::StringRef symbol_name =
685f757f3fSDimitry Andric           vtable_info.symbol->GetMangled().GetDemangledName().GetStringRef();
699dba64beSDimitry Andric       LLDB_LOGF(log,
709dba64beSDimitry Andric                 "0x%16.16" PRIx64
710b57cec5SDimitry Andric                 ": static-type = '%s' has vtable symbol '%s'\n",
725f757f3fSDimitry Andric                 in_value.GetPointerValue(),
735f757f3fSDimitry Andric                 in_value.GetTypeName().GetCString(),
745f757f3fSDimitry Andric                 symbol_name.str().c_str());
750b57cec5SDimitry Andric       // We are a C++ class, that's good.  Get the class name and look it
760b57cec5SDimitry Andric       // up:
775f757f3fSDimitry Andric       llvm::StringRef class_name = symbol_name;
785f757f3fSDimitry Andric       class_name.consume_front(vtable_demangled_prefix);
790b57cec5SDimitry Andric       // We know the class name is absolute, so tell FindTypes that by
800b57cec5SDimitry Andric       // prefixing it with the root namespace:
810b57cec5SDimitry Andric       std::string lookup_name("::");
825f757f3fSDimitry Andric       lookup_name.append(class_name.data(), class_name.size());
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric       type_info.SetName(class_name);
855f757f3fSDimitry Andric       ConstString const_lookup_name(lookup_name);
860b57cec5SDimitry Andric       TypeList class_types;
875f757f3fSDimitry Andric       ModuleSP module_sp = vtable_info.symbol->CalculateSymbolContextModule();
880b57cec5SDimitry Andric       // First look in the module that the vtable symbol came from and
890b57cec5SDimitry Andric       // look for a single exact match.
905f757f3fSDimitry Andric       TypeResults results;
915f757f3fSDimitry Andric       TypeQuery query(const_lookup_name.GetStringRef(),
925f757f3fSDimitry Andric                       TypeQueryOptions::e_exact_match |
935f757f3fSDimitry Andric                           TypeQueryOptions::e_find_one);
945f757f3fSDimitry Andric       if (module_sp) {
955f757f3fSDimitry Andric         module_sp->FindTypes(query, results);
965f757f3fSDimitry Andric         TypeSP type_sp = results.GetFirstType();
975f757f3fSDimitry Andric         if (type_sp)
985f757f3fSDimitry Andric           class_types.Insert(type_sp);
995f757f3fSDimitry Andric       }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric       // If we didn't find a symbol, then move on to the entire module
1020b57cec5SDimitry Andric       // list in the target and get as many unique matches as possible
1035f757f3fSDimitry Andric       if (class_types.Empty()) {
1045f757f3fSDimitry Andric         query.SetFindOne(false);
1055f757f3fSDimitry Andric         m_process->GetTarget().GetImages().FindTypes(nullptr, query, results);
1065f757f3fSDimitry Andric         for (const auto &type_sp : results.GetTypeMap().Types())
1075f757f3fSDimitry Andric           class_types.Insert(type_sp);
1085f757f3fSDimitry Andric       }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric       lldb::TypeSP type_sp;
1119dba64beSDimitry Andric       if (class_types.Empty()) {
1129dba64beSDimitry Andric         LLDB_LOGF(log, "0x%16.16" PRIx64 ": is not dynamic\n",
1135f757f3fSDimitry Andric                   in_value.GetPointerValue());
1140b57cec5SDimitry Andric         return TypeAndOrName();
1150b57cec5SDimitry Andric       }
1169dba64beSDimitry Andric       if (class_types.GetSize() == 1) {
1170b57cec5SDimitry Andric         type_sp = class_types.GetTypeAtIndex(0);
1180b57cec5SDimitry Andric         if (type_sp) {
1195ffd83dbSDimitry Andric           if (TypeSystemClang::IsCXXClassType(
1200b57cec5SDimitry Andric                   type_sp->GetForwardCompilerType())) {
1219dba64beSDimitry Andric             LLDB_LOGF(
1229dba64beSDimitry Andric                 log,
1230b57cec5SDimitry Andric                 "0x%16.16" PRIx64
1240b57cec5SDimitry Andric                 ": static-type = '%s' has dynamic type: uid={0x%" PRIx64
1250b57cec5SDimitry Andric                 "}, type-name='%s'\n",
1265f757f3fSDimitry Andric                 in_value.GetPointerValue(), in_value.GetTypeName().AsCString(),
1270b57cec5SDimitry Andric                 type_sp->GetID(), type_sp->GetName().GetCString());
1280b57cec5SDimitry Andric             type_info.SetTypeSP(type_sp);
1290b57cec5SDimitry Andric           }
1300b57cec5SDimitry Andric         }
1319dba64beSDimitry Andric       } else {
1320b57cec5SDimitry Andric         size_t i;
1330b57cec5SDimitry Andric         if (log) {
1349dba64beSDimitry Andric           for (i = 0; i < class_types.GetSize(); i++) {
1350b57cec5SDimitry Andric             type_sp = class_types.GetTypeAtIndex(i);
1360b57cec5SDimitry Andric             if (type_sp) {
1379dba64beSDimitry Andric               LLDB_LOGF(
1389dba64beSDimitry Andric                   log,
1390b57cec5SDimitry Andric                   "0x%16.16" PRIx64
1400b57cec5SDimitry Andric                   ": static-type = '%s' has multiple matching dynamic "
1410b57cec5SDimitry Andric                   "types: uid={0x%" PRIx64 "}, type-name='%s'\n",
1425f757f3fSDimitry Andric                   in_value.GetPointerValue(),
1435f757f3fSDimitry Andric                   in_value.GetTypeName().AsCString(),
1440b57cec5SDimitry Andric                   type_sp->GetID(), type_sp->GetName().GetCString());
1450b57cec5SDimitry Andric             }
1460b57cec5SDimitry Andric           }
1470b57cec5SDimitry Andric         }
1480b57cec5SDimitry Andric 
1499dba64beSDimitry Andric         for (i = 0; i < class_types.GetSize(); i++) {
1500b57cec5SDimitry Andric           type_sp = class_types.GetTypeAtIndex(i);
1510b57cec5SDimitry Andric           if (type_sp) {
1525ffd83dbSDimitry Andric             if (TypeSystemClang::IsCXXClassType(
1530b57cec5SDimitry Andric                     type_sp->GetForwardCompilerType())) {
1549dba64beSDimitry Andric               LLDB_LOGF(
1559dba64beSDimitry Andric                   log,
1560b57cec5SDimitry Andric                   "0x%16.16" PRIx64 ": static-type = '%s' has multiple "
1570b57cec5SDimitry Andric                   "matching dynamic types, picking "
1589dba64beSDimitry Andric                   "this one: uid={0x%" PRIx64 "}, type-name='%s'\n",
1595f757f3fSDimitry Andric                   in_value.GetPointerValue(),
1605f757f3fSDimitry Andric                   in_value.GetTypeName().AsCString(),
1610b57cec5SDimitry Andric                   type_sp->GetID(), type_sp->GetName().GetCString());
1620b57cec5SDimitry Andric               type_info.SetTypeSP(type_sp);
1630b57cec5SDimitry Andric             }
1640b57cec5SDimitry Andric           }
1650b57cec5SDimitry Andric         }
1660b57cec5SDimitry Andric 
1679dba64beSDimitry Andric         if (log) {
1689dba64beSDimitry Andric           LLDB_LOGF(log,
1690b57cec5SDimitry Andric                     "0x%16.16" PRIx64
1700b57cec5SDimitry Andric                     ": static-type = '%s' has multiple matching dynamic "
1710b57cec5SDimitry Andric                     "types, didn't find a C++ match\n",
1725f757f3fSDimitry Andric                     in_value.GetPointerValue(),
1735f757f3fSDimitry Andric                     in_value.GetTypeName().AsCString());
1740b57cec5SDimitry Andric         }
1750b57cec5SDimitry Andric       }
1760b57cec5SDimitry Andric       if (type_info)
1775f757f3fSDimitry Andric         SetDynamicTypeInfo(vtable_info.addr, type_info);
1780b57cec5SDimitry Andric       return type_info;
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric   return TypeAndOrName();
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1845f757f3fSDimitry Andric llvm::Error ItaniumABILanguageRuntime::TypeHasVTable(CompilerType type) {
1855f757f3fSDimitry Andric   // Check to make sure the class has a vtable.
1865f757f3fSDimitry Andric   CompilerType original_type = type;
1875f757f3fSDimitry Andric   if (type.IsPointerOrReferenceType()) {
1885f757f3fSDimitry Andric     CompilerType pointee_type = type.GetPointeeType();
1895f757f3fSDimitry Andric     if (pointee_type)
1905f757f3fSDimitry Andric       type = pointee_type;
1915f757f3fSDimitry Andric   }
1925f757f3fSDimitry Andric 
1935f757f3fSDimitry Andric   // Make sure this is a class or a struct first by checking the type class
1945f757f3fSDimitry Andric   // bitfield that gets returned.
1955f757f3fSDimitry Andric   if ((type.GetTypeClass() & (eTypeClassStruct | eTypeClassClass)) == 0) {
1965f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
1975f757f3fSDimitry Andric         "type \"%s\" is not a class or struct or a pointer to one",
1985f757f3fSDimitry Andric         original_type.GetTypeName().AsCString("<invalid>"));
1995f757f3fSDimitry Andric   }
2005f757f3fSDimitry Andric 
2015f757f3fSDimitry Andric   // Check if the type has virtual functions by asking it if it is polymorphic.
2025f757f3fSDimitry Andric   if (!type.IsPolymorphicClass()) {
2035f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2045f757f3fSDimitry Andric         "type \"%s\" doesn't have a vtable",
2055f757f3fSDimitry Andric         type.GetTypeName().AsCString("<invalid>"));
2065f757f3fSDimitry Andric   }
2075f757f3fSDimitry Andric   return llvm::Error::success();
2085f757f3fSDimitry Andric }
2095f757f3fSDimitry Andric 
2105f757f3fSDimitry Andric // This function can accept both pointers or references to classes as well as
2115f757f3fSDimitry Andric // instances of classes. If you are using this function during dynamic type
2125f757f3fSDimitry Andric // detection, only valid ValueObjects that return true to
2135f757f3fSDimitry Andric // CouldHaveDynamicValue(...) should call this function and \a check_type
2145f757f3fSDimitry Andric // should be set to false. This function is also used by ValueObjectVTable
2155f757f3fSDimitry Andric // and is can pass in instances of classes which is not suitable for dynamic
2165f757f3fSDimitry Andric // type detection, these cases should pass true for \a check_type.
2175f757f3fSDimitry Andric llvm::Expected<LanguageRuntime::VTableInfo>
2185f757f3fSDimitry Andric  ItaniumABILanguageRuntime::GetVTableInfo(ValueObject &in_value,
2195f757f3fSDimitry Andric                                           bool check_type) {
2205f757f3fSDimitry Andric 
2215f757f3fSDimitry Andric   CompilerType type = in_value.GetCompilerType();
2225f757f3fSDimitry Andric   if (check_type) {
2235f757f3fSDimitry Andric     if (llvm::Error err = TypeHasVTable(type))
2245f757f3fSDimitry Andric       return std::move(err);
2255f757f3fSDimitry Andric   }
2265f757f3fSDimitry Andric   ExecutionContext exe_ctx(in_value.GetExecutionContextRef());
2275f757f3fSDimitry Andric   Process *process = exe_ctx.GetProcessPtr();
2285f757f3fSDimitry Andric   if (process == nullptr)
2295f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2305f757f3fSDimitry Andric                                    "invalid process");
2315f757f3fSDimitry Andric 
2325f757f3fSDimitry Andric   AddressType address_type;
2335f757f3fSDimitry Andric   lldb::addr_t original_ptr = LLDB_INVALID_ADDRESS;
2345f757f3fSDimitry Andric   if (type.IsPointerOrReferenceType())
2355f757f3fSDimitry Andric     original_ptr = in_value.GetPointerValue(&address_type);
2365f757f3fSDimitry Andric   else
2375f757f3fSDimitry Andric     original_ptr = in_value.GetAddressOf(/*scalar_is_load_address=*/true,
2385f757f3fSDimitry Andric                                          &address_type);
2395f757f3fSDimitry Andric   if (original_ptr == LLDB_INVALID_ADDRESS || address_type != eAddressTypeLoad)
2405f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2415f757f3fSDimitry Andric                                    "failed to get the address of the value");
2425f757f3fSDimitry Andric 
2435f757f3fSDimitry Andric   Status error;
2445f757f3fSDimitry Andric   lldb::addr_t vtable_load_addr =
2455f757f3fSDimitry Andric       process->ReadPointerFromMemory(original_ptr, error);
2465f757f3fSDimitry Andric 
2475f757f3fSDimitry Andric   if (!error.Success() || vtable_load_addr == LLDB_INVALID_ADDRESS)
2485f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2495f757f3fSDimitry Andric         "failed to read vtable pointer from memory at 0x%" PRIx64,
2505f757f3fSDimitry Andric         original_ptr);
2515f757f3fSDimitry Andric 
2525f757f3fSDimitry Andric   // The vtable load address can have authentication bits with
2535f757f3fSDimitry Andric   // AArch64 targets on Darwin.
2545f757f3fSDimitry Andric   vtable_load_addr = process->FixDataAddress(vtable_load_addr);
2555f757f3fSDimitry Andric 
2565f757f3fSDimitry Andric   // Find the symbol that contains the "vtable_load_addr" address
2575f757f3fSDimitry Andric   Address vtable_addr;
2585f757f3fSDimitry Andric   if (!process->GetTarget().ResolveLoadAddress(vtable_load_addr, vtable_addr))
2595f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2605f757f3fSDimitry Andric                                    "failed to resolve vtable pointer 0x%"
2615f757f3fSDimitry Andric                                    PRIx64 "to a section", vtable_load_addr);
2625f757f3fSDimitry Andric 
2635f757f3fSDimitry Andric   // Check our cache first to see if we already have this info
2645f757f3fSDimitry Andric   {
2655f757f3fSDimitry Andric     std::lock_guard<std::mutex> locker(m_mutex);
2665f757f3fSDimitry Andric     auto pos = m_vtable_info_map.find(vtable_addr);
2675f757f3fSDimitry Andric     if (pos != m_vtable_info_map.end())
2685f757f3fSDimitry Andric       return pos->second;
2695f757f3fSDimitry Andric   }
2705f757f3fSDimitry Andric 
2715f757f3fSDimitry Andric   Symbol *symbol = vtable_addr.CalculateSymbolContextSymbol();
2725f757f3fSDimitry Andric   if (symbol == nullptr)
2735f757f3fSDimitry Andric     return llvm::createStringError(std::errc::invalid_argument,
2745f757f3fSDimitry Andric                                    "no symbol found for 0x%" PRIx64,
2755f757f3fSDimitry Andric                                    vtable_load_addr);
2765f757f3fSDimitry Andric   llvm::StringRef name = symbol->GetMangled().GetDemangledName().GetStringRef();
2775f757f3fSDimitry Andric   if (name.starts_with(vtable_demangled_prefix)) {
2785f757f3fSDimitry Andric     VTableInfo info = {vtable_addr, symbol};
2795f757f3fSDimitry Andric     std::lock_guard<std::mutex> locker(m_mutex);
2805f757f3fSDimitry Andric     auto pos = m_vtable_info_map[vtable_addr] = info;
2815f757f3fSDimitry Andric     return info;
2825f757f3fSDimitry Andric   }
2835f757f3fSDimitry Andric   return llvm::createStringError(std::errc::invalid_argument,
2845f757f3fSDimitry Andric       "symbol found that contains 0x%" PRIx64 " is not a vtable symbol",
2855f757f3fSDimitry Andric       vtable_load_addr);
2865f757f3fSDimitry Andric }
2875f757f3fSDimitry Andric 
2880b57cec5SDimitry Andric bool ItaniumABILanguageRuntime::GetDynamicTypeAndAddress(
2890b57cec5SDimitry Andric     ValueObject &in_value, lldb::DynamicValueType use_dynamic,
2900b57cec5SDimitry Andric     TypeAndOrName &class_type_or_name, Address &dynamic_address,
2910b57cec5SDimitry Andric     Value::ValueType &value_type) {
2920b57cec5SDimitry Andric   // For Itanium, if the type has a vtable pointer in the object, it will be at
2930b57cec5SDimitry Andric   // offset 0 in the object.  That will point to the "address point" within the
2940b57cec5SDimitry Andric   // vtable (not the beginning of the vtable.)  We can then look up the symbol
2950b57cec5SDimitry Andric   // containing this "address point" and that symbol's name demangled will
2960b57cec5SDimitry Andric   // contain the full class name. The second pointer above the "address point"
2970b57cec5SDimitry Andric   // is the "offset_to_top".  We'll use that to get the start of the value
2980b57cec5SDimitry Andric   // object which holds the dynamic type.
2990b57cec5SDimitry Andric   //
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   class_type_or_name.Clear();
302fe6060f1SDimitry Andric   value_type = Value::ValueType::Scalar;
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   if (!CouldHaveDynamicValue(in_value))
3050b57cec5SDimitry Andric     return false;
3060b57cec5SDimitry Andric 
3075f757f3fSDimitry Andric   // Check if we have a vtable pointer in this value. If we don't it will
3085f757f3fSDimitry Andric   // return an error, else it will return a valid resolved address. We don't
3095f757f3fSDimitry Andric   // want GetVTableInfo to check the type since we accept void * as a possible
3105f757f3fSDimitry Andric   // dynamic type and that won't pass the type check. We already checked the
3115f757f3fSDimitry Andric   // type above in CouldHaveDynamicValue(...).
3125f757f3fSDimitry Andric   llvm::Expected<VTableInfo> vtable_info_or_err =
3135f757f3fSDimitry Andric       GetVTableInfo(in_value, /*check_type=*/false);
3145f757f3fSDimitry Andric   if (!vtable_info_or_err) {
3155f757f3fSDimitry Andric     llvm::consumeError(vtable_info_or_err.takeError());
3160b57cec5SDimitry Andric     return false;
3175f757f3fSDimitry Andric   }
3180b57cec5SDimitry Andric 
3195f757f3fSDimitry Andric   const VTableInfo &vtable_info = vtable_info_or_err.get();
3205f757f3fSDimitry Andric   class_type_or_name = GetTypeInfo(in_value, vtable_info);
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   if (!class_type_or_name)
3230b57cec5SDimitry Andric     return false;
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   CompilerType type = class_type_or_name.GetCompilerType();
3260b57cec5SDimitry Andric   // There can only be one type with a given name, so we've just found
3270b57cec5SDimitry Andric   // duplicate definitions, and this one will do as well as any other. We
3280b57cec5SDimitry Andric   // don't consider something to have a dynamic type if it is the same as
3290b57cec5SDimitry Andric   // the static type.  So compare against the value we were handed.
3300b57cec5SDimitry Andric   if (!type)
3310b57cec5SDimitry Andric     return true;
3320b57cec5SDimitry Andric 
3335ffd83dbSDimitry Andric   if (TypeSystemClang::AreTypesSame(in_value.GetCompilerType(), type)) {
3340b57cec5SDimitry Andric     // The dynamic type we found was the same type, so we don't have a
3350b57cec5SDimitry Andric     // dynamic type here...
3360b57cec5SDimitry Andric     return false;
3370b57cec5SDimitry Andric   }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   // The offset_to_top is two pointers above the vtable pointer.
3405f757f3fSDimitry Andric   Target &target = m_process->GetTarget();
3415f757f3fSDimitry Andric   const addr_t vtable_load_addr = vtable_info.addr.GetLoadAddress(&target);
3425f757f3fSDimitry Andric   if (vtable_load_addr == LLDB_INVALID_ADDRESS)
3430b57cec5SDimitry Andric     return false;
3445f757f3fSDimitry Andric   const uint32_t addr_byte_size = m_process->GetAddressByteSize();
3455f757f3fSDimitry Andric   const lldb::addr_t offset_to_top_location =
3465f757f3fSDimitry Andric       vtable_load_addr - 2 * addr_byte_size;
3475f757f3fSDimitry Andric   // Watch for underflow, offset_to_top_location should be less than
3485f757f3fSDimitry Andric   // vtable_load_addr
3495f757f3fSDimitry Andric   if (offset_to_top_location >= vtable_load_addr)
3505f757f3fSDimitry Andric     return false;
3515f757f3fSDimitry Andric   Status error;
3525f757f3fSDimitry Andric   const int64_t offset_to_top = m_process->ReadSignedIntegerFromMemory(
3530b57cec5SDimitry Andric       offset_to_top_location, addr_byte_size, INT64_MIN, error);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   if (offset_to_top == INT64_MIN)
3560b57cec5SDimitry Andric     return false;
3570b57cec5SDimitry Andric   // So the dynamic type is a value that starts at offset_to_top above
3580b57cec5SDimitry Andric   // the original address.
3595f757f3fSDimitry Andric   lldb::addr_t dynamic_addr = in_value.GetPointerValue() + offset_to_top;
3605f757f3fSDimitry Andric   if (!m_process->GetTarget().ResolveLoadAddress(
3610b57cec5SDimitry Andric           dynamic_addr, dynamic_address)) {
3620b57cec5SDimitry Andric     dynamic_address.SetRawAddress(dynamic_addr);
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric   return true;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric TypeAndOrName ItaniumABILanguageRuntime::FixUpDynamicType(
3680b57cec5SDimitry Andric     const TypeAndOrName &type_and_or_name, ValueObject &static_value) {
3690b57cec5SDimitry Andric   CompilerType static_type(static_value.GetCompilerType());
3700b57cec5SDimitry Andric   Flags static_type_flags(static_type.GetTypeInfo());
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   TypeAndOrName ret(type_and_or_name);
3730b57cec5SDimitry Andric   if (type_and_or_name.HasType()) {
3740b57cec5SDimitry Andric     // The type will always be the type of the dynamic object.  If our parent's
3750b57cec5SDimitry Andric     // type was a pointer, then our type should be a pointer to the type of the
3760b57cec5SDimitry Andric     // dynamic object.  If a reference, then the original type should be
3770b57cec5SDimitry Andric     // okay...
3780b57cec5SDimitry Andric     CompilerType orig_type = type_and_or_name.GetCompilerType();
3790b57cec5SDimitry Andric     CompilerType corrected_type = orig_type;
3800b57cec5SDimitry Andric     if (static_type_flags.AllSet(eTypeIsPointer))
3810b57cec5SDimitry Andric       corrected_type = orig_type.GetPointerType();
3820b57cec5SDimitry Andric     else if (static_type_flags.AllSet(eTypeIsReference))
3830b57cec5SDimitry Andric       corrected_type = orig_type.GetLValueReferenceType();
3840b57cec5SDimitry Andric     ret.SetCompilerType(corrected_type);
3850b57cec5SDimitry Andric   } else {
3860b57cec5SDimitry Andric     // If we are here we need to adjust our dynamic type name to include the
3870b57cec5SDimitry Andric     // correct & or * symbol
3880b57cec5SDimitry Andric     std::string corrected_name(type_and_or_name.GetName().GetCString());
3890b57cec5SDimitry Andric     if (static_type_flags.AllSet(eTypeIsPointer))
3900b57cec5SDimitry Andric       corrected_name.append(" *");
3910b57cec5SDimitry Andric     else if (static_type_flags.AllSet(eTypeIsReference))
3920b57cec5SDimitry Andric       corrected_name.append(" &");
3930b57cec5SDimitry Andric     // the parent type should be a correctly pointer'ed or referenc'ed type
3940b57cec5SDimitry Andric     ret.SetCompilerType(static_type);
3950b57cec5SDimitry Andric     ret.SetName(corrected_name.c_str());
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric   return ret;
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric // Static Functions
4010b57cec5SDimitry Andric LanguageRuntime *
4020b57cec5SDimitry Andric ItaniumABILanguageRuntime::CreateInstance(Process *process,
4030b57cec5SDimitry Andric                                           lldb::LanguageType language) {
4040b57cec5SDimitry Andric   // FIXME: We have to check the process and make sure we actually know that
4050b57cec5SDimitry Andric   // this process supports
4060b57cec5SDimitry Andric   // the Itanium ABI.
4070b57cec5SDimitry Andric   if (language == eLanguageTypeC_plus_plus ||
4080b57cec5SDimitry Andric       language == eLanguageTypeC_plus_plus_03 ||
4090b57cec5SDimitry Andric       language == eLanguageTypeC_plus_plus_11 ||
4100b57cec5SDimitry Andric       language == eLanguageTypeC_plus_plus_14)
4110b57cec5SDimitry Andric     return new ItaniumABILanguageRuntime(process);
4120b57cec5SDimitry Andric   else
4130b57cec5SDimitry Andric     return nullptr;
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric class CommandObjectMultiwordItaniumABI_Demangle : public CommandObjectParsed {
4170b57cec5SDimitry Andric public:
4180b57cec5SDimitry Andric   CommandObjectMultiwordItaniumABI_Demangle(CommandInterpreter &interpreter)
41906c3fb27SDimitry Andric       : CommandObjectParsed(
42006c3fb27SDimitry Andric             interpreter, "demangle", "Demangle a C++ mangled name.",
42106c3fb27SDimitry Andric             "language cplusplus demangle [<mangled-name> ...]") {
422*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeSymbol, eArgRepeatPlus);
4230b57cec5SDimitry Andric   }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   ~CommandObjectMultiwordItaniumABI_Demangle() override = default;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric protected:
4285f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
4290b57cec5SDimitry Andric     bool demangled_any = false;
4300b57cec5SDimitry Andric     bool error_any = false;
4310b57cec5SDimitry Andric     for (auto &entry : command.entries()) {
4329dba64beSDimitry Andric       if (entry.ref().empty())
4330b57cec5SDimitry Andric         continue;
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric       // the actual Mangled class should be strict about this, but on the
4360b57cec5SDimitry Andric       // command line if you're copying mangled names out of 'nm' on Darwin,
4370b57cec5SDimitry Andric       // they will come out with an extra underscore - be willing to strip this
4380b57cec5SDimitry Andric       // on behalf of the user.   This is the moral equivalent of the -_/-n
4390b57cec5SDimitry Andric       // options to c++filt
4409dba64beSDimitry Andric       auto name = entry.ref();
4415f757f3fSDimitry Andric       if (name.starts_with("__Z"))
4420b57cec5SDimitry Andric         name = name.drop_front();
4430b57cec5SDimitry Andric 
4449dba64beSDimitry Andric       Mangled mangled(name);
4450b57cec5SDimitry Andric       if (mangled.GuessLanguage() == lldb::eLanguageTypeC_plus_plus) {
4465ffd83dbSDimitry Andric         ConstString demangled(mangled.GetDisplayDemangledName());
4470b57cec5SDimitry Andric         demangled_any = true;
4489dba64beSDimitry Andric         result.AppendMessageWithFormat("%s ---> %s\n", entry.c_str(),
4490b57cec5SDimitry Andric                                        demangled.GetCString());
4500b57cec5SDimitry Andric       } else {
4510b57cec5SDimitry Andric         error_any = true;
4520b57cec5SDimitry Andric         result.AppendErrorWithFormat("%s is not a valid C++ mangled name\n",
4539dba64beSDimitry Andric                                      entry.ref().str().c_str());
4540b57cec5SDimitry Andric       }
4550b57cec5SDimitry Andric     }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric     result.SetStatus(
4580b57cec5SDimitry Andric         error_any ? lldb::eReturnStatusFailed
4590b57cec5SDimitry Andric                   : (demangled_any ? lldb::eReturnStatusSuccessFinishResult
4600b57cec5SDimitry Andric                                    : lldb::eReturnStatusSuccessFinishNoResult));
4610b57cec5SDimitry Andric   }
4620b57cec5SDimitry Andric };
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric class CommandObjectMultiwordItaniumABI : public CommandObjectMultiword {
4650b57cec5SDimitry Andric public:
4660b57cec5SDimitry Andric   CommandObjectMultiwordItaniumABI(CommandInterpreter &interpreter)
4670b57cec5SDimitry Andric       : CommandObjectMultiword(
4680b57cec5SDimitry Andric             interpreter, "cplusplus",
4690b57cec5SDimitry Andric             "Commands for operating on the C++ language runtime.",
4700b57cec5SDimitry Andric             "cplusplus <subcommand> [<subcommand-options>]") {
4710b57cec5SDimitry Andric     LoadSubCommand(
4720b57cec5SDimitry Andric         "demangle",
4730b57cec5SDimitry Andric         CommandObjectSP(
4740b57cec5SDimitry Andric             new CommandObjectMultiwordItaniumABI_Demangle(interpreter)));
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   ~CommandObjectMultiwordItaniumABI() override = default;
4780b57cec5SDimitry Andric };
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric void ItaniumABILanguageRuntime::Initialize() {
4810b57cec5SDimitry Andric   PluginManager::RegisterPlugin(
4820b57cec5SDimitry Andric       GetPluginNameStatic(), "Itanium ABI for the C++ language", CreateInstance,
4830b57cec5SDimitry Andric       [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
4840b57cec5SDimitry Andric         return CommandObjectSP(
4850b57cec5SDimitry Andric             new CommandObjectMultiwordItaniumABI(interpreter));
4860b57cec5SDimitry Andric       });
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric void ItaniumABILanguageRuntime::Terminate() {
4900b57cec5SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
4945ffd83dbSDimitry Andric     const BreakpointSP &bkpt, bool catch_bp, bool throw_bp) {
4950b57cec5SDimitry Andric   return CreateExceptionResolver(bkpt, catch_bp, throw_bp, false);
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric BreakpointResolverSP ItaniumABILanguageRuntime::CreateExceptionResolver(
4995ffd83dbSDimitry Andric     const BreakpointSP &bkpt, bool catch_bp, bool throw_bp,
5005ffd83dbSDimitry Andric     bool for_expressions) {
5010b57cec5SDimitry Andric   // One complication here is that most users DON'T want to stop at
5020b57cec5SDimitry Andric   // __cxa_allocate_expression, but until we can do anything better with
5030b57cec5SDimitry Andric   // predicting unwinding the expression parser does.  So we have two forms of
5040b57cec5SDimitry Andric   // the exception breakpoints, one for expressions that leaves out
5050b57cec5SDimitry Andric   // __cxa_allocate_exception, and one that includes it. The
5060b57cec5SDimitry Andric   // SetExceptionBreakpoints does the latter, the CreateExceptionBreakpoint in
5070b57cec5SDimitry Andric   // the runtime the former.
5080b57cec5SDimitry Andric   static const char *g_catch_name = "__cxa_begin_catch";
5090b57cec5SDimitry Andric   static const char *g_throw_name1 = "__cxa_throw";
5100b57cec5SDimitry Andric   static const char *g_throw_name2 = "__cxa_rethrow";
5110b57cec5SDimitry Andric   static const char *g_exception_throw_name = "__cxa_allocate_exception";
5120b57cec5SDimitry Andric   std::vector<const char *> exception_names;
5130b57cec5SDimitry Andric   exception_names.reserve(4);
5140b57cec5SDimitry Andric   if (catch_bp)
5150b57cec5SDimitry Andric     exception_names.push_back(g_catch_name);
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   if (throw_bp) {
5180b57cec5SDimitry Andric     exception_names.push_back(g_throw_name1);
5190b57cec5SDimitry Andric     exception_names.push_back(g_throw_name2);
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   if (for_expressions)
5230b57cec5SDimitry Andric     exception_names.push_back(g_exception_throw_name);
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverName(
5260b57cec5SDimitry Andric       bkpt, exception_names.data(), exception_names.size(),
5270b57cec5SDimitry Andric       eFunctionNameTypeBase, eLanguageTypeUnknown, 0, eLazyBoolNo));
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   return resolver_sp;
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric lldb::SearchFilterSP ItaniumABILanguageRuntime::CreateExceptionSearchFilter() {
5330b57cec5SDimitry Andric   Target &target = m_process->GetTarget();
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric   FileSpecList filter_modules;
5360b57cec5SDimitry Andric   if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple) {
5370b57cec5SDimitry Andric     // Limit the number of modules that are searched for these breakpoints for
5380b57cec5SDimitry Andric     // Apple binaries.
5399dba64beSDimitry Andric     filter_modules.EmplaceBack("libc++abi.dylib");
5409dba64beSDimitry Andric     filter_modules.EmplaceBack("libSystem.B.dylib");
541bdd1243dSDimitry Andric     filter_modules.EmplaceBack("libc++abi.1.0.dylib");
542bdd1243dSDimitry Andric     filter_modules.EmplaceBack("libc++abi.1.dylib");
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric   return target.GetSearchFilterForModuleList(&filter_modules);
5450b57cec5SDimitry Andric }
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric lldb::BreakpointSP ItaniumABILanguageRuntime::CreateExceptionBreakpoint(
5480b57cec5SDimitry Andric     bool catch_bp, bool throw_bp, bool for_expressions, bool is_internal) {
5490b57cec5SDimitry Andric   Target &target = m_process->GetTarget();
5500b57cec5SDimitry Andric   FileSpecList filter_modules;
5510b57cec5SDimitry Andric   BreakpointResolverSP exception_resolver_sp =
5520b57cec5SDimitry Andric       CreateExceptionResolver(nullptr, catch_bp, throw_bp, for_expressions);
5530b57cec5SDimitry Andric   SearchFilterSP filter_sp(CreateExceptionSearchFilter());
5540b57cec5SDimitry Andric   const bool hardware = false;
5550b57cec5SDimitry Andric   const bool resolve_indirect_functions = false;
5560b57cec5SDimitry Andric   return target.CreateBreakpoint(filter_sp, exception_resolver_sp, is_internal,
5570b57cec5SDimitry Andric                                  hardware, resolve_indirect_functions);
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric void ItaniumABILanguageRuntime::SetExceptionBreakpoints() {
5610b57cec5SDimitry Andric   if (!m_process)
5620b57cec5SDimitry Andric     return;
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric   const bool catch_bp = false;
5650b57cec5SDimitry Andric   const bool throw_bp = true;
5660b57cec5SDimitry Andric   const bool is_internal = true;
5670b57cec5SDimitry Andric   const bool for_expressions = true;
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   // For the exception breakpoints set by the Expression parser, we'll be a
5700b57cec5SDimitry Andric   // little more aggressive and stop at exception allocation as well.
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   if (m_cxx_exception_bp_sp) {
5730b57cec5SDimitry Andric     m_cxx_exception_bp_sp->SetEnabled(true);
5740b57cec5SDimitry Andric   } else {
5750b57cec5SDimitry Andric     m_cxx_exception_bp_sp = CreateExceptionBreakpoint(
5760b57cec5SDimitry Andric         catch_bp, throw_bp, for_expressions, is_internal);
5770b57cec5SDimitry Andric     if (m_cxx_exception_bp_sp)
5780b57cec5SDimitry Andric       m_cxx_exception_bp_sp->SetBreakpointKind("c++ exception");
5790b57cec5SDimitry Andric   }
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric void ItaniumABILanguageRuntime::ClearExceptionBreakpoints() {
5830b57cec5SDimitry Andric   if (!m_process)
5840b57cec5SDimitry Andric     return;
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   if (m_cxx_exception_bp_sp) {
5870b57cec5SDimitry Andric     m_cxx_exception_bp_sp->SetEnabled(false);
5880b57cec5SDimitry Andric   }
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric bool ItaniumABILanguageRuntime::ExceptionBreakpointsAreSet() {
5920b57cec5SDimitry Andric   return m_cxx_exception_bp_sp && m_cxx_exception_bp_sp->IsEnabled();
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric bool ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop(
5960b57cec5SDimitry Andric     lldb::StopInfoSP stop_reason) {
5970b57cec5SDimitry Andric   if (!m_process)
5980b57cec5SDimitry Andric     return false;
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric   if (!stop_reason || stop_reason->GetStopReason() != eStopReasonBreakpoint)
6010b57cec5SDimitry Andric     return false;
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   uint64_t break_site_id = stop_reason->GetValue();
6045f757f3fSDimitry Andric   return m_process->GetBreakpointSiteList().StopPointSiteContainsBreakpoint(
6050b57cec5SDimitry Andric       break_site_id, m_cxx_exception_bp_sp->GetID());
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric ValueObjectSP ItaniumABILanguageRuntime::GetExceptionObjectForThread(
6090b57cec5SDimitry Andric     ThreadSP thread_sp) {
6100b57cec5SDimitry Andric   if (!thread_sp->SafeToCallFunctions())
6110b57cec5SDimitry Andric     return {};
6120b57cec5SDimitry Andric 
613bdd1243dSDimitry Andric   TypeSystemClangSP scratch_ts_sp =
614e8d8bef9SDimitry Andric       ScratchTypeSystemClang::GetForTarget(m_process->GetTarget());
615bdd1243dSDimitry Andric   if (!scratch_ts_sp)
616480093f4SDimitry Andric     return {};
617480093f4SDimitry Andric 
6180b57cec5SDimitry Andric   CompilerType voidstar =
619bdd1243dSDimitry Andric       scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   DiagnosticManager diagnostics;
6220b57cec5SDimitry Andric   ExecutionContext exe_ctx;
6230b57cec5SDimitry Andric   EvaluateExpressionOptions options;
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric   options.SetUnwindOnError(true);
6260b57cec5SDimitry Andric   options.SetIgnoreBreakpoints(true);
6270b57cec5SDimitry Andric   options.SetStopOthers(true);
6280b57cec5SDimitry Andric   options.SetTimeout(m_process->GetUtilityExpressionTimeout());
6290b57cec5SDimitry Andric   options.SetTryAllThreads(false);
6300b57cec5SDimitry Andric   thread_sp->CalculateExecutionContext(exe_ctx);
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   const ModuleList &modules = m_process->GetTarget().GetImages();
6330b57cec5SDimitry Andric   SymbolContextList contexts;
6340b57cec5SDimitry Andric   SymbolContext context;
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   modules.FindSymbolsWithNameAndType(
6370b57cec5SDimitry Andric       ConstString("__cxa_current_exception_type"), eSymbolTypeCode, contexts);
6380b57cec5SDimitry Andric   contexts.GetContextAtIndex(0, context);
639480093f4SDimitry Andric   if (!context.symbol) {
640480093f4SDimitry Andric     return {};
641480093f4SDimitry Andric   }
6420b57cec5SDimitry Andric   Address addr = context.symbol->GetAddress();
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric   Status error;
6450b57cec5SDimitry Andric   FunctionCaller *function_caller =
6460b57cec5SDimitry Andric       m_process->GetTarget().GetFunctionCallerForLanguage(
6470b57cec5SDimitry Andric           eLanguageTypeC, voidstar, addr, ValueList(), "caller", error);
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric   ExpressionResults func_call_ret;
6500b57cec5SDimitry Andric   Value results;
6510b57cec5SDimitry Andric   func_call_ret = function_caller->ExecuteFunction(exe_ctx, nullptr, options,
6520b57cec5SDimitry Andric                                                    diagnostics, results);
6530b57cec5SDimitry Andric   if (func_call_ret != eExpressionCompleted || !error.Success()) {
6540b57cec5SDimitry Andric     return ValueObjectSP();
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   size_t ptr_size = m_process->GetAddressByteSize();
6580b57cec5SDimitry Andric   addr_t result_ptr = results.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
6590b57cec5SDimitry Andric   addr_t exception_addr =
6600b57cec5SDimitry Andric       m_process->ReadPointerFromMemory(result_ptr - ptr_size, error);
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   if (!error.Success()) {
6630b57cec5SDimitry Andric     return ValueObjectSP();
6640b57cec5SDimitry Andric   }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   lldb_private::formatters::InferiorSizedWord exception_isw(exception_addr,
6670b57cec5SDimitry Andric                                                             *m_process);
6680b57cec5SDimitry Andric   ValueObjectSP exception = ValueObject::CreateValueObjectFromData(
6690b57cec5SDimitry Andric       "exception", exception_isw.GetAsData(m_process->GetByteOrder()), exe_ctx,
6700b57cec5SDimitry Andric       voidstar);
67106c3fb27SDimitry Andric   ValueObjectSP dyn_exception
67206c3fb27SDimitry Andric       = exception->GetDynamicValue(eDynamicDontRunTarget);
67306c3fb27SDimitry Andric   // If we succeed in making a dynamic value, return that:
67406c3fb27SDimitry Andric   if (dyn_exception)
67506c3fb27SDimitry Andric      return dyn_exception;
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric   return exception;
6780b57cec5SDimitry Andric }
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric TypeAndOrName ItaniumABILanguageRuntime::GetDynamicTypeInfo(
6810b57cec5SDimitry Andric     const lldb_private::Address &vtable_addr) {
6825f757f3fSDimitry Andric   std::lock_guard<std::mutex> locker(m_mutex);
6830b57cec5SDimitry Andric   DynamicTypeCache::const_iterator pos = m_dynamic_type_map.find(vtable_addr);
6840b57cec5SDimitry Andric   if (pos == m_dynamic_type_map.end())
6850b57cec5SDimitry Andric     return TypeAndOrName();
6860b57cec5SDimitry Andric   else
6870b57cec5SDimitry Andric     return pos->second;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric void ItaniumABILanguageRuntime::SetDynamicTypeInfo(
6910b57cec5SDimitry Andric     const lldb_private::Address &vtable_addr, const TypeAndOrName &type_info) {
6925f757f3fSDimitry Andric   std::lock_guard<std::mutex> locker(m_mutex);
6930b57cec5SDimitry Andric   m_dynamic_type_map[vtable_addr] = type_info;
6940b57cec5SDimitry Andric }
695