1 //===-- DWARFUnit.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef SymbolFileDWARF_DWARFUnit_h_
11 #define SymbolFileDWARF_DWARFUnit_h_
14 #include "DWARFDebugInfoEntry.h"
15 #include "lldb/lldb-enumerations.h"
16 #include "llvm/Support/RWMutex.h"
20 class DWARFCompileUnit;
22 class SymbolFileDWARF;
23 class SymbolFileDWARFDwo;
25 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
36 using die_iterator_range =
37 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
42 void ExtractUnitDIEIfNeeded();
43 void ExtractDIEsIfNeeded();
45 class ScopedExtractDIEs {
48 bool m_clear_dies = false;
49 ScopedExtractDIEs(DWARFUnit *cu);
51 DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs);
52 ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
53 ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
55 ScopedExtractDIEs ExtractDIEsScoped();
57 DWARFDIE LookupAddress(const dw_addr_t address);
58 size_t AppendDIEsWithTag(const dw_tag_t tag,
59 DWARFDIECollection &matching_dies,
60 uint32_t depth = UINT32_MAX) const;
61 bool Verify(lldb_private::Stream *s) const;
62 virtual void Dump(lldb_private::Stream *s) const = 0;
63 //------------------------------------------------------------------
64 /// Get the data that contains the DIE information for this unit.
66 /// This will return the correct bytes that contain the data for
67 /// this DWARFUnit. It could be .debug_info or .debug_types
68 /// depending on where the data for this unit originates.
71 /// The correct data for the DIE information in this unit.
72 //------------------------------------------------------------------
73 virtual const lldb_private::DWARFDataExtractor &GetData() const = 0;
74 //------------------------------------------------------------------
75 /// Get the size in bytes of the compile unit header.
78 /// Byte size of the compile unit header
79 //------------------------------------------------------------------
80 virtual uint32_t GetHeaderByteSize() const = 0;
81 // Offset of the initial length field.
82 dw_offset_t GetOffset() const { return m_offset; }
83 lldb::user_id_t GetID() const;
84 //------------------------------------------------------------------
85 /// Get the size in bytes of the length field in the header.
87 /// In DWARF32 this is just 4 bytes, and DWARF64 it is 12 where 4
88 /// are 0xFFFFFFFF followed by the actual 64 bit length.
91 /// Byte size of the compile unit header length field
92 //------------------------------------------------------------------
93 size_t GetLengthByteSize() const { return IsDWARF64() ? 12 : 4; }
95 bool ContainsDIEOffset(dw_offset_t die_offset) const {
96 return die_offset >= GetFirstDIEOffset() &&
97 die_offset < GetNextCompileUnitOffset();
99 dw_offset_t GetFirstDIEOffset() const {
100 return m_offset + GetHeaderByteSize();
102 dw_offset_t GetNextCompileUnitOffset() const;
103 // Size of the CU data (without initial length and without header).
104 size_t GetDebugInfoSize() const;
105 // Size of the CU data incl. header but without initial length.
106 uint32_t GetLength() const { return m_length; }
107 uint16_t GetVersion() const { return m_version; }
108 const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
109 dw_offset_t GetAbbrevOffset() const;
110 uint8_t GetAddressByteSize() const { return m_addr_size; }
111 dw_addr_t GetBaseAddress() const { return m_base_addr; }
112 dw_addr_t GetAddrBase() const { return m_addr_base; }
113 dw_addr_t GetRangesBase() const { return m_ranges_base; }
114 dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }
115 void SetAddrBase(dw_addr_t addr_base);
116 void SetRangesBase(dw_addr_t ranges_base);
117 void SetBaseObjOffset(dw_offset_t base_obj_offset);
118 void SetStrOffsetsBase(dw_offset_t str_offsets_base);
119 void BuildAddressRangeTable(SymbolFileDWARF *dwarf,
120 DWARFDebugAranges *debug_aranges);
122 lldb::ByteOrder GetByteOrder() const;
124 lldb_private::TypeSystem *GetTypeSystem();
126 const DWARFDebugAranges &GetFunctionAranges();
128 DWARFFormValue::FixedFormSizes GetFixedFormSizes();
130 void SetBaseAddress(dw_addr_t base_addr);
132 DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
134 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
136 DWARFDIE GetDIE(dw_offset_t die_offset);
138 static uint8_t GetAddressByteSize(const DWARFUnit *cu);
140 static bool IsDWARF64(const DWARFUnit *cu);
142 static uint8_t GetDefaultAddressSize();
144 void *GetUserData() const;
146 void SetUserData(void *d);
148 bool Supports_DW_AT_APPLE_objc_complete_type();
150 bool DW_AT_decl_file_attributes_are_invalid();
152 bool Supports_unnamed_objc_bitfields();
154 SymbolFileDWARF *GetSymbolFileDWARF() const;
156 DWARFProducer GetProducer();
158 uint32_t GetProducerVersionMajor();
160 uint32_t GetProducerVersionMinor();
162 uint32_t GetProducerVersionUpdate();
164 static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
166 lldb::LanguageType GetLanguageType();
168 bool IsDWARF64() const { return m_is_dwarf64; }
170 bool GetIsOptimized();
172 SymbolFileDWARFDwo *GetDwoSymbolFile() const;
174 dw_offset_t GetBaseObjOffset() const;
176 die_iterator_range dies() {
177 ExtractDIEsIfNeeded();
178 return die_iterator_range(m_die_array.begin(), m_die_array.end());
182 DWARFUnit(SymbolFileDWARF *dwarf);
184 SymbolFileDWARF *m_dwarf = nullptr;
185 std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;
186 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
187 void *m_user_data = nullptr;
188 // The compile unit debug information entry item
189 DWARFDebugInfoEntry::collection m_die_array;
190 mutable llvm::sys::RWMutex m_die_array_mutex;
191 // It is used for tracking of ScopedExtractDIEs instances.
192 mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
193 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
194 // as someone called ExtractDIEsIfNeeded().
195 std::atomic<bool> m_cancel_scopes;
196 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
197 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
198 // would possibly move in memory after later ExtractDIEsIfNeeded().
199 DWARFDebugInfoEntry m_first_die;
200 llvm::sys::RWMutex m_first_die_mutex;
201 // A table similar to the .debug_aranges table, but this one points to the
202 // exact DW_TAG_subprogram DIEs
203 std::unique_ptr<DWARFDebugAranges> m_func_aranges_ap;
204 dw_addr_t m_base_addr = 0;
205 dw_offset_t m_length = 0;
206 uint16_t m_version = 0;
207 uint8_t m_addr_size = 0;
208 uint8_t m_unit_type = 0;
209 uint64_t m_dwo_id = 0;
210 DWARFProducer m_producer = eProducerInvalid;
211 uint32_t m_producer_version_major = 0;
212 uint32_t m_producer_version_minor = 0;
213 uint32_t m_producer_version_update = 0;
214 lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown;
215 bool m_is_dwarf64 = false;
216 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate;
217 dw_addr_t m_addr_base = 0; // Value of DW_AT_addr_base
218 dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base
219 // If this is a dwo compile unit this is the offset of the base compile unit
220 // in the main object file
221 dw_offset_t m_base_obj_offset = DW_INVALID_OFFSET;
222 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
223 // Offset of the initial length field.
224 dw_offset_t m_offset;
227 void ParseProducerInfo();
228 void ExtractDIEsRWLocked();
229 void ClearDIEsRWLocked();
231 // Get the DWARF unit DWARF debug informration entry. Parse the single DIE
233 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
234 ExtractUnitDIEIfNeeded();
235 // m_first_die_mutex is not required as m_first_die is never cleared.
241 // Get all DWARF debug informration entries. Parse all DIEs if needed.
242 const DWARFDebugInfoEntry *DIEPtr() {
243 ExtractDIEsIfNeeded();
244 if (m_die_array.empty())
246 return &m_die_array[0];
249 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
250 void ExtractDIEsEndCheck(lldb::offset_t offset) const;
252 DISALLOW_COPY_AND_ASSIGN(DWARFUnit);
255 #endif // SymbolFileDWARF_DWARFUnit_h_