1 //===-- DWARFUnit.h ---------------------------------------------*- 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 #ifndef SymbolFileDWARF_DWARFUnit_h_ 10 #define SymbolFileDWARF_DWARFUnit_h_ 11 12 #include "DWARFDIE.h" 13 #include "DWARFDebugInfoEntry.h" 14 #include "lldb/lldb-enumerations.h" 15 #include "llvm/Support/RWMutex.h" 16 #include <atomic> 17 18 class DWARFUnit; 19 class DWARFCompileUnit; 20 class NameToDIE; 21 class SymbolFileDWARF; 22 class SymbolFileDWARFDwo; 23 24 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP; 25 26 enum DWARFProducer { 27 eProducerInvalid = 0, 28 eProducerClang, 29 eProducerGCC, 30 eProducerLLVMGCC, 31 eProcucerOther 32 }; 33 34 class DWARFUnit { 35 using die_iterator_range = 36 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>; 37 38 public: 39 virtual ~DWARFUnit(); 40 41 void ExtractUnitDIEIfNeeded(); 42 void ExtractDIEsIfNeeded(); 43 44 class ScopedExtractDIEs { 45 DWARFUnit *m_cu; 46 public: 47 bool m_clear_dies = false; 48 ScopedExtractDIEs(DWARFUnit *cu); 49 ~ScopedExtractDIEs(); 50 DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs); 51 ScopedExtractDIEs(ScopedExtractDIEs &&rhs); 52 ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs); 53 }; 54 ScopedExtractDIEs ExtractDIEsScoped(); 55 56 DWARFDIE LookupAddress(const dw_addr_t address); 57 size_t AppendDIEsWithTag(const dw_tag_t tag, std::vector<DWARFDIE> &dies, 58 uint32_t depth = UINT32_MAX) const; 59 bool Verify(lldb_private::Stream *s) const; 60 virtual void Dump(lldb_private::Stream *s) const = 0; 61 //------------------------------------------------------------------ 62 /// Get the data that contains the DIE information for this unit. 63 /// 64 /// This will return the correct bytes that contain the data for 65 /// this DWARFUnit. It could be .debug_info or .debug_types 66 /// depending on where the data for this unit originates. 67 /// 68 /// \return 69 /// The correct data for the DIE information in this unit. 70 //------------------------------------------------------------------ 71 virtual const lldb_private::DWARFDataExtractor &GetData() const = 0; 72 //------------------------------------------------------------------ 73 /// Get the size in bytes of the compile unit header. 74 /// 75 /// \return 76 /// Byte size of the compile unit header 77 //------------------------------------------------------------------ 78 virtual uint32_t GetHeaderByteSize() const = 0; 79 // Offset of the initial length field. 80 dw_offset_t GetOffset() const { return m_offset; } 81 lldb::user_id_t GetID() const; 82 //------------------------------------------------------------------ 83 /// Get the size in bytes of the length field in the header. 84 /// 85 /// In DWARF32 this is just 4 bytes 86 /// 87 /// \return 88 /// Byte size of the compile unit header length field 89 //------------------------------------------------------------------ 90 size_t GetLengthByteSize() const { return 4; } 91 92 bool ContainsDIEOffset(dw_offset_t die_offset) const { 93 return die_offset >= GetFirstDIEOffset() && 94 die_offset < GetNextCompileUnitOffset(); 95 } 96 dw_offset_t GetFirstDIEOffset() const { 97 return m_offset + GetHeaderByteSize(); 98 } 99 dw_offset_t GetNextCompileUnitOffset() const; 100 // Size of the CU data (without initial length and without header). 101 size_t GetDebugInfoSize() const; 102 // Size of the CU data incl. header but without initial length. 103 uint32_t GetLength() const { return m_length; } 104 uint16_t GetVersion() const { return m_version; } 105 const DWARFAbbreviationDeclarationSet *GetAbbreviations() const; 106 dw_offset_t GetAbbrevOffset() const; 107 uint8_t GetAddressByteSize() const { return m_addr_size; } 108 dw_addr_t GetBaseAddress() const { return m_base_addr; } 109 dw_addr_t GetAddrBase() const { return m_addr_base; } 110 dw_addr_t GetRangesBase() const { return m_ranges_base; } 111 dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; } 112 void SetAddrBase(dw_addr_t addr_base); 113 void SetRangesBase(dw_addr_t ranges_base); 114 void SetBaseObjOffset(dw_offset_t base_obj_offset); 115 void SetStrOffsetsBase(dw_offset_t str_offsets_base); 116 void BuildAddressRangeTable(SymbolFileDWARF *dwarf, 117 DWARFDebugAranges *debug_aranges); 118 119 lldb::ByteOrder GetByteOrder() const; 120 121 lldb_private::TypeSystem *GetTypeSystem(); 122 123 const DWARFDebugAranges &GetFunctionAranges(); 124 125 DWARFFormValue::FixedFormSizes GetFixedFormSizes(); 126 127 void SetBaseAddress(dw_addr_t base_addr); 128 129 DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); } 130 131 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); } 132 133 DWARFDIE GetDIE(dw_offset_t die_offset); 134 135 static uint8_t GetAddressByteSize(const DWARFUnit *cu); 136 137 static uint8_t GetDefaultAddressSize(); 138 139 void *GetUserData() const; 140 141 void SetUserData(void *d); 142 143 bool Supports_DW_AT_APPLE_objc_complete_type(); 144 145 bool DW_AT_decl_file_attributes_are_invalid(); 146 147 bool Supports_unnamed_objc_bitfields(); 148 149 SymbolFileDWARF *GetSymbolFileDWARF() const; 150 151 DWARFProducer GetProducer(); 152 153 uint32_t GetProducerVersionMajor(); 154 155 uint32_t GetProducerVersionMinor(); 156 157 uint32_t GetProducerVersionUpdate(); 158 159 static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val); 160 161 lldb::LanguageType GetLanguageType(); 162 163 bool GetIsOptimized(); 164 165 const lldb_private::FileSpec &GetCompilationDirectory(); 166 lldb_private::FileSpec::Style GetPathStyle(); 167 168 SymbolFileDWARFDwo *GetDwoSymbolFile() const; 169 170 dw_offset_t GetBaseObjOffset() const; 171 172 die_iterator_range dies() { 173 ExtractDIEsIfNeeded(); 174 return die_iterator_range(m_die_array.begin(), m_die_array.end()); 175 } 176 177 protected: 178 DWARFUnit(SymbolFileDWARF *dwarf); 179 180 SymbolFileDWARF *m_dwarf = nullptr; 181 std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file; 182 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr; 183 void *m_user_data = nullptr; 184 // The compile unit debug information entry item 185 DWARFDebugInfoEntry::collection m_die_array; 186 mutable llvm::sys::RWMutex m_die_array_mutex; 187 // It is used for tracking of ScopedExtractDIEs instances. 188 mutable llvm::sys::RWMutex m_die_array_scoped_mutex; 189 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked() 190 // as someone called ExtractDIEsIfNeeded(). 191 std::atomic<bool> m_cancel_scopes; 192 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE. 193 // But the first element of m_die_array after ExtractUnitDIEIfNeeded() 194 // would possibly move in memory after later ExtractDIEsIfNeeded(). 195 DWARFDebugInfoEntry m_first_die; 196 llvm::sys::RWMutex m_first_die_mutex; 197 // A table similar to the .debug_aranges table, but this one points to the 198 // exact DW_TAG_subprogram DIEs 199 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up; 200 dw_addr_t m_base_addr = 0; 201 dw_offset_t m_length = 0; 202 uint16_t m_version = 0; 203 uint8_t m_addr_size = 0; 204 uint8_t m_unit_type = 0; 205 uint64_t m_dwo_id = 0; 206 DWARFProducer m_producer = eProducerInvalid; 207 uint32_t m_producer_version_major = 0; 208 uint32_t m_producer_version_minor = 0; 209 uint32_t m_producer_version_update = 0; 210 lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown; 211 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate; 212 llvm::Optional<lldb_private::FileSpec> m_comp_dir; 213 dw_addr_t m_addr_base = 0; // Value of DW_AT_addr_base 214 dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base 215 // If this is a dwo compile unit this is the offset of the base compile unit 216 // in the main object file 217 dw_offset_t m_base_obj_offset = DW_INVALID_OFFSET; 218 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base. 219 // Offset of the initial length field. 220 dw_offset_t m_offset; 221 222 private: 223 void ParseProducerInfo(); 224 void ExtractDIEsRWLocked(); 225 void ClearDIEsRWLocked(); 226 227 // Get the DWARF unit DWARF debug informration entry. Parse the single DIE 228 // if needed. 229 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() { 230 ExtractUnitDIEIfNeeded(); 231 // m_first_die_mutex is not required as m_first_die is never cleared. 232 if (!m_first_die) 233 return NULL; 234 return &m_first_die; 235 } 236 237 // Get all DWARF debug informration entries. Parse all DIEs if needed. 238 const DWARFDebugInfoEntry *DIEPtr() { 239 ExtractDIEsIfNeeded(); 240 if (m_die_array.empty()) 241 return NULL; 242 return &m_die_array[0]; 243 } 244 245 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die); 246 247 void ComputeCompDirAndGuessPathStyle(); 248 249 DISALLOW_COPY_AND_ASSIGN(DWARFUnit); 250 }; 251 252 #endif // SymbolFileDWARF_DWARFUnit_h_ 253