1 //===-- DWARFBaseDIE.cpp ---------------------------------------*- C++ -*-===// 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 "DWARFBaseDIE.h" 10 11 #include "DWARFUnit.h" 12 #include "DWARFDebugInfoEntry.h" 13 #include "SymbolFileDWARF.h" 14 15 #include "lldb/Core/Module.h" 16 #include "lldb/Symbol/ObjectFile.h" 17 #include "lldb/Utility/Log.h" 18 19 using namespace lldb_private; 20 21 llvm::Optional<DIERef> DWARFBaseDIE::GetDIERef() const { 22 if (!IsValid()) 23 return llvm::None; 24 25 return DIERef(m_cu->GetSymbolFileDWARF().GetDwoNum(), m_cu->GetDebugSection(), 26 m_die->GetOffset()); 27 } 28 29 dw_tag_t DWARFBaseDIE::Tag() const { 30 if (m_die) 31 return m_die->Tag(); 32 else 33 return llvm::dwarf::DW_TAG_null; 34 } 35 36 const char *DWARFBaseDIE::GetTagAsCString() const { 37 return lldb_private::DW_TAG_value_to_name(Tag()); 38 } 39 40 const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr, 41 const char *fail_value) const { 42 if (IsValid()) 43 return m_die->GetAttributeValueAsString(GetCU(), attr, fail_value); 44 else 45 return fail_value; 46 } 47 48 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr, 49 uint64_t fail_value) const { 50 if (IsValid()) 51 return m_die->GetAttributeValueAsUnsigned(GetCU(), attr, fail_value); 52 else 53 return fail_value; 54 } 55 56 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr, 57 uint64_t fail_value) const { 58 if (IsValid()) 59 return m_die->GetAttributeValueAsAddress(GetCU(), attr, fail_value); 60 else 61 return fail_value; 62 } 63 64 lldb::user_id_t DWARFBaseDIE::GetID() const { 65 if (IsValid()) 66 return GetDWARF()->GetUID(*this); 67 return LLDB_INVALID_UID; 68 } 69 70 const char *DWARFBaseDIE::GetName() const { 71 if (IsValid()) 72 return m_die->GetName(m_cu); 73 else 74 return nullptr; 75 } 76 77 lldb::LanguageType DWARFBaseDIE::GetLanguage() const { 78 if (IsValid()) 79 return m_cu->GetLanguageType(); 80 else 81 return lldb::eLanguageTypeUnknown; 82 } 83 84 lldb::ModuleSP DWARFBaseDIE::GetModule() const { 85 SymbolFileDWARF *dwarf = GetDWARF(); 86 if (dwarf) 87 return dwarf->GetObjectFile()->GetModule(); 88 else 89 return lldb::ModuleSP(); 90 } 91 92 dw_offset_t DWARFBaseDIE::GetOffset() const { 93 if (IsValid()) 94 return m_die->GetOffset(); 95 else 96 return DW_INVALID_OFFSET; 97 } 98 99 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const { 100 if (m_cu) 101 return &m_cu->GetSymbolFileDWARF(); 102 else 103 return nullptr; 104 } 105 106 llvm::Expected<lldb_private::TypeSystem &> DWARFBaseDIE::GetTypeSystem() const { 107 if (!m_cu) 108 return llvm::make_error<llvm::StringError>( 109 "Unable to get TypeSystem, no compilation unit available", 110 llvm::inconvertibleErrorCode()); 111 return m_cu->GetTypeSystem(); 112 } 113 114 DWARFASTParser *DWARFBaseDIE::GetDWARFParser() const { 115 auto type_system_or_err = GetTypeSystem(); 116 if (auto err = type_system_or_err.takeError()) { 117 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 118 std::move(err), "Unable to get DWARFASTParser"); 119 return nullptr; 120 } 121 return type_system_or_err->GetDWARFParser(); 122 } 123 124 bool DWARFBaseDIE::HasChildren() const { 125 return m_die && m_die->HasChildren(); 126 } 127 128 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const { 129 return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu); 130 } 131 132 size_t DWARFBaseDIE::GetAttributes(DWARFAttributes &attributes, 133 uint32_t depth) const { 134 if (IsValid()) 135 return m_die->GetAttributes(m_cu, attributes, depth); 136 if (depth == 0) 137 attributes.Clear(); 138 return 0; 139 } 140 141 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) { 142 return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU(); 143 } 144 145 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) { 146 return !(lhs == rhs); 147 } 148 149 const DWARFDataExtractor &DWARFBaseDIE::GetData() const { 150 // Clients must check if this DIE is valid before calling this function. 151 assert(IsValid()); 152 return m_cu->GetData(); 153 } 154