LLDB mainline
DWARFUnit.h
Go to the documentation of this file.
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 LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
10#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
11
12#include "DWARFDIE.h"
13#include "DWARFDebugInfoEntry.h"
16#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
19#include "llvm/Support/RWMutex.h"
20#include <atomic>
21#include <optional>
22
23namespace lldb_private::plugin {
24namespace dwarf {
25class DWARFUnit;
26class DWARFCompileUnit;
27class NameToDIE;
28class SymbolFileDWARF;
29class SymbolFileDWARFDwo;
30
31typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
32
39};
40
41class DWARFUnit : public UserID {
43 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
44
45public:
46 static llvm::Expected<DWARFUnitSP>
48 const DWARFDataExtractor &debug_info, DIERef::Section section,
49 lldb::offset_t *offset_ptr);
50 virtual ~DWARFUnit();
51
52 bool IsDWOUnit() { return m_is_dwo; }
53 /// Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's
54 /// DW_AT_dwo_id or DW_AT_GNU_dwo_id for DWARF4 and earlier.
55 std::optional<uint64_t> GetDWOId();
56 /// Get the DWO ID from the DWARFUnitHeader only. DWARF5 skeleton units have
57 /// the DWO ID in the compile unit header and we sometimes only want to access
58 /// this cheap value without causing the more expensive attribute fetches that
59 /// GetDWOId() uses.
60 std::optional<uint64_t> GetHeaderDWOId() { return m_header.getDWOId(); }
64
67
68 public:
69 bool m_clear_dies = false;
76 };
78
79 bool Verify(Stream *s) const;
80 virtual void Dump(Stream *s) const = 0;
81 /// Get the data that contains the DIE information for this unit.
82 ///
83 /// This will return the correct bytes that contain the data for
84 /// this DWARFUnit. It could be .debug_info or .debug_types
85 /// depending on where the data for this unit originates.
86 ///
87 /// \return
88 /// The correct data for the DIE information in this unit.
89 const DWARFDataExtractor &GetData() const;
90
91 /// Get the size in bytes of the unit header.
92 ///
93 /// \return
94 /// Byte size of the unit header
95 uint32_t GetHeaderByteSize() const;
96
97 // Offset of the initial length field.
98 dw_offset_t GetOffset() const { return m_header.getOffset(); }
99 /// Get the size in bytes of the length field in the header.
100 ///
101 /// In DWARF32 this is just 4 bytes
102 ///
103 /// \return
104 /// Byte size of the compile unit header length field
105 size_t GetLengthByteSize() const { return 4; }
106
107 bool ContainsDIEOffset(dw_offset_t die_offset) const {
108 return die_offset >= GetFirstDIEOffset() &&
109 die_offset < GetNextUnitOffset();
110 }
112 return GetOffset() + GetHeaderByteSize();
113 }
114 dw_offset_t GetNextUnitOffset() const { return m_header.getNextUnitOffset(); }
115 // Size of the CU data (without initial length and without header).
116 size_t GetDebugInfoSize() const;
117 // Size of the CU data incl. header but without initial length.
118 dw_offset_t GetLength() const { return m_header.getLength(); }
119 uint16_t GetVersion() const { return m_header.getVersion(); }
120 const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
122 uint8_t GetAddressByteSize() const { return m_header.getAddressByteSize(); }
123 dw_addr_t GetAddrBase() const { return m_addr_base.value_or(0); }
128 void SetAddrBase(dw_addr_t addr_base);
129 void SetLoclistsBase(dw_addr_t loclists_base);
130 void SetRangesBase(dw_addr_t ranges_base);
131 void SetStrOffsetsBase(dw_offset_t str_offsets_base);
132 virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;
133
134 dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const;
135
137
139
140 void SetBaseAddress(dw_addr_t base_addr);
141
143
144 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
145
146 DWARFDIE GetDIE(dw_offset_t die_offset);
147
148 /// Returns the AT_Name of the DIE at `die_offset`, if it exists, without
149 /// parsing the entire compile unit. An empty is string is returned upon
150 /// error or if the attribute is not present.
151 llvm::StringRef PeekDIEName(dw_offset_t die_offset);
152
154
155 static uint8_t GetAddressByteSize(const DWARFUnit *cu);
156
157 static uint8_t GetDefaultAddressSize();
158
160
162
163 /// Get the skeleton compile unit for a DWO file.
164 ///
165 /// We need to keep track of the skeleton compile unit for a DWO file so
166 /// we can access it. Sometimes this value is cached when the skeleton
167 /// compile unit is first parsed, but if a .dwp file parses all of the
168 /// DWARFUnits in the file, the skeleton compile unit might not have been
169 /// parsed yet, to there might not be a backlink. This accessor handles
170 /// both cases correctly and avoids crashes.
172
173 bool LinkToSkeletonUnit(DWARFUnit &skeleton_unit);
174
176
178
180
181 llvm::VersionTuple GetProducerVersion();
182
183 uint64_t GetDWARFLanguageType();
184
185 bool GetIsOptimized();
186
188 const FileSpec &GetAbsolutePath();
189 FileSpec GetFile(size_t file_idx);
191
192 SymbolFileDWARFDwo *GetDwoSymbolFile(bool load_all_debug_info = true);
193
196 return die_iterator_range(m_die_array.begin(), m_die_array.end());
197 }
198
200
201 uint8_t GetUnitType() const { return m_header.getUnitType(); }
202 bool IsTypeUnit() const { return m_header.isTypeUnit(); }
203 /// Note that this check only works for DWARF5+.
204 bool IsSkeletonUnit() const {
205 return GetUnitType() == llvm::dwarf::DW_UT_skeleton;
206 }
207
208 std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const;
209
210 /// Return a list of address ranges resulting from a (possibly encoded)
211 /// range list starting at a given offset in the appropriate ranges section.
212 llvm::Expected<llvm::DWARFAddressRangesVector>
214
215 /// Return a list of address ranges retrieved from an encoded range
216 /// list whose offset is found via a table lookup given an index (DWARF v5
217 /// and later).
218 llvm::Expected<llvm::DWARFAddressRangesVector>
219 FindRnglistFromIndex(uint32_t index);
220
221 /// Return a rangelist's offset based on an index. The index designates
222 /// an entry in the rangelist table's offset array and is supplied by
223 /// DW_FORM_rnglistx.
224 llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index);
225
226 std::optional<uint64_t> GetLoclistOffset(uint32_t Index) {
228 return std::nullopt;
229
230 std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(
232 if (!Offset)
233 return std::nullopt;
234 return *Offset + m_loclists_base;
235 }
236
237 /// Return the location table for parsing the given location list data. The
238 /// format is chosen according to the unit type. Never returns null.
239 std::unique_ptr<llvm::DWARFLocationTable>
240 GetLocationTable(const DataExtractor &data) const;
241
243
244 /// Returns true if any DIEs in the unit match any DW_TAG values in \a tags.
245 ///
246 /// \param[in] tags
247 /// An array of dw_tag_t values to check all abbrevitions for.
248 ///
249 /// \returns
250 /// True if any DIEs match any tag in \a tags, false otherwise.
251 bool HasAny(llvm::ArrayRef<dw_tag_t> tags);
252
253 /// Get the fission .dwo file specific error for this compile unit.
254 ///
255 /// The skeleton compile unit only can have a DWO error. Any other type
256 /// of DWARFUnit will not have a valid DWO error.
257 ///
258 /// \returns
259 /// A valid DWO error if there is a problem with anything in the
260 /// locating or parsing inforamtion in the .dwo file
261 const Status &GetDwoError() const { return m_dwo_error; }
262
263 /// Set the fission .dwo file specific error for this compile unit.
264 ///
265 /// This helps tracks issues that arise when trying to locate or parse a
266 /// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other
267 /// .dwo errors can be stored in each compile unit so the issues can be
268 /// communicated to the user.
269 void SetDwoError(Status &&error) { m_dwo_error = std::move(error); }
270
271protected:
273 const llvm::DWARFUnitHeader &header,
274 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
275 DIERef::Section section, bool is_dwo);
276
278 const DWARFDataExtractor &data,
279 lldb::offset_t *offset_ptr);
280
281 // Get the DWARF unit DWARF debug information entry. Parse the single DIE
282 // if needed.
285 // m_first_die_mutex is not required as m_first_die is never cleared.
286 if (!m_first_die)
287 return nullptr;
288 return &m_first_die;
289 }
290
291 // Get all DWARF debug informration entries. Parse all DIEs if needed.
294 if (m_die_array.empty())
295 return nullptr;
296 return &m_die_array[0];
297 }
298
299 const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();
300
302
304 std::shared_ptr<DWARFUnit> m_dwo;
305 llvm::DWARFUnitHeader m_header;
306 const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
308 // If this is a DWO file, we have a backlink to our skeleton compile unit.
309 std::atomic<DWARFUnit *> m_skeleton_unit = nullptr;
310 // The compile unit debug information entry item
312 mutable llvm::sys::RWMutex m_die_array_mutex;
313 // It is used for tracking of ScopedExtractDIEs instances.
314 mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
315 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
316 // as someone called ExtractDIEsIfNeeded().
317 std::atomic<bool> m_cancel_scopes;
318 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
319 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
320 // would possibly move in memory after later ExtractDIEsIfNeeded().
322 llvm::sys::RWMutex m_first_die_mutex;
323 // A table similar to the .debug_aranges table, but this one points to the
324 // exact DW_TAG_subprogram DIEs
325 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
328 llvm::VersionTuple m_producer_version;
329 std::optional<uint64_t> m_language_type;
331 std::optional<FileSpec> m_comp_dir;
332 std::optional<FileSpec> m_file_spec;
333 std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base.
334 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.
335 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base.
336 std::optional<uint64_t> m_gnu_addr_base;
337 std::optional<uint64_t> m_gnu_ranges_base;
338
339 /// Value of DW_AT_stmt_list.
341
342 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
343
344 std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;
346 std::optional<llvm::DWARFListTableHeader> m_loclist_table_header;
347
351 /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
352 std::optional<uint64_t> m_dwo_id;
353 /// If we get an error when trying to load a .dwo file, save that error here.
354 /// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found
355 /// but DWO ID doesn't match, etc.
357
358private:
359 void ParseProducerInfo();
360 void ExtractDIEsRWLocked();
361 void ClearDIEsRWLocked();
362
363 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
365
367 void ComputeAbsolutePath();
368
369 DWARFUnit(const DWARFUnit &) = delete;
370 const DWARFUnit &operator=(const DWARFUnit &) = delete;
371};
372} // namespace dwarf
373} // namespace lldb_private::plugin
374
375#endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
static llvm::raw_ostream & error(Stream &strm)
A class that describes a compilation unit.
Definition: CompileUnit.h:43
llvm::DataExtractor GetAsLLVM() const
An data extractor class.
Definition: DataExtractor.h:48
A file utility class.
Definition: FileSpec.h:56
llvm::sys::path::Style Style
Definition: FileSpec.h:58
An error handling class.
Definition: Status.h:118
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
const DWARFDataExtractor & getOrLoadLocListsData()
DWARFDebugInfoEntry objects assume that they are living in one big vector and do pointer arithmetic o...
std::vector< DWARFDebugInfoEntry > collection
ScopedExtractDIEs(const ScopedExtractDIEs &)=delete
const ScopedExtractDIEs & operator=(const ScopedExtractDIEs &)=delete
const DWARFUnit & operator=(const DWARFUnit &)=delete
std::optional< dw_addr_t > m_addr_base
Value of DW_AT_addr_base.
Definition: DWARFUnit.h:333
llvm::Expected< llvm::DWARFAddressRangesVector > FindRnglistFromOffset(dw_offset_t offset)
Return a list of address ranges resulting from a (possibly encoded) range list starting at a given of...
Definition: DWARFUnit.cpp:1017
bool IsSkeletonUnit() const
Note that this check only works for DWARF5+.
Definition: DWARFUnit.h:204
std::unique_ptr< llvm::DWARFLocationTable > GetLocationTable(const DataExtractor &data) const
Return the location table for parsing the given location list data.
Definition: DWARFUnit.cpp:526
std::optional< FileSpec > m_file_spec
Definition: DWARFUnit.h:332
size_t GetLengthByteSize() const
Get the size in bytes of the length field in the header.
Definition: DWARFUnit.h:105
std::optional< uint64_t > GetHeaderDWOId()
Get the DWO ID from the DWARFUnitHeader only.
Definition: DWARFUnit.h:60
std::optional< llvm::DWARFDebugRnglistTable > m_rnglist_table
Definition: DWARFUnit.h:344
lldb_private::CompileUnit * m_lldb_cu
Definition: DWARFUnit.h:307
const DWARFDebugInfoEntry * GetUnitDIEPtrOnly()
Definition: DWARFUnit.h:283
llvm::sys::RWMutex m_die_array_mutex
Definition: DWARFUnit.h:312
bool LinkToSkeletonUnit(DWARFUnit &skeleton_unit)
Definition: DWARFUnit.cpp:728
dw_offset_t m_line_table_offset
Value of DW_AT_stmt_list.
Definition: DWARFUnit.h:340
llvm::sys::RWMutex m_die_array_scoped_mutex
Definition: DWARFUnit.h:314
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
Definition: DWARFUnit.cpp:612
std::atomic< bool > m_cancel_scopes
Definition: DWARFUnit.h:317
Status m_dwo_error
If we get an error when trying to load a .dwo file, save that error here.
Definition: DWARFUnit.h:356
void SetLoclistsBase(dw_addr_t loclists_base)
Definition: DWARFUnit.cpp:494
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition: DWARFUnit.h:177
DIERef::Section GetDebugSection() const
Definition: DWARFUnit.h:199
const DWARFDebugAranges & GetFunctionAranges()
Definition: DWARFUnit.cpp:900
const DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
Definition: DWARFUnit.cpp:989
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, const DWARFDataExtractor &debug_info, DIERef::Section section, lldb::offset_t *offset_ptr)
Definition: DWARFUnit.cpp:921
DWARFCompileUnit * GetSkeletonUnit()
Get the skeleton compile unit for a DWO file.
Definition: DWARFUnit.cpp:714
dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const
Definition: DWARFUnit.cpp:616
const DWARFDebugInfoEntry * DIEPtr()
Definition: DWARFUnit.h:292
bool ContainsDIEOffset(dw_offset_t die_offset) const
Definition: DWARFUnit.h:107
llvm::iterator_range< DWARFDebugInfoEntry::collection::iterator > die_iterator_range
Definition: DWARFUnit.h:43
std::optional< FileSpec > m_comp_dir
Definition: DWARFUnit.h:331
dw_offset_t GetFirstDIEOffset() const
Definition: DWARFUnit.h:111
const Status & GetDwoError() const
Get the fission .dwo file specific error for this compile unit.
Definition: DWARFUnit.h:261
std::optional< uint64_t > m_language_type
Definition: DWARFUnit.h:329
llvm::StringRef PeekDIEName(dw_offset_t die_offset)
Returns the AT_Name of the DIE at die_offset, if it exists, without parsing the entire compile unit.
Definition: DWARFUnit.cpp:675
DWARFDebugInfoEntry::collection m_die_array
Definition: DWARFUnit.h:311
void SetBaseAddress(dw_addr_t base_addr)
Definition: DWARFUnit.cpp:640
std::optional< uint64_t > m_gnu_ranges_base
Definition: DWARFUnit.h:337
const llvm::DWARFAbbreviationDeclarationSet * m_abbrevs
Definition: DWARFUnit.h:306
void SetLLDBCompUnit(lldb_private::CompileUnit *cu)
Definition: DWARFUnit.h:161
DWARFUnit(const DWARFUnit &)=delete
lldb_private::CompileUnit * GetLLDBCompUnit() const
Definition: DWARFUnit.h:159
const DIERef::Section m_section
Definition: DWARFUnit.h:348
dw_offset_t GetNextUnitOffset() const
Definition: DWARFUnit.h:114
DWARFDataExtractor GetLocationData() const
Definition: DWARFUnit.cpp:536
const std::optional< llvm::DWARFDebugRnglistTable > & GetRnglistTable()
Definition: DWARFUnit.cpp:574
llvm::Expected< uint64_t > GetRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
Definition: DWARFUnit.cpp:590
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
Definition: DWARFUnit.h:325
std::optional< uint64_t > GetLoclistOffset(uint32_t Index)
Definition: DWARFUnit.h:226
dw_addr_t m_ranges_base
Value of DW_AT_rnglists_base.
Definition: DWARFUnit.h:335
virtual void Dump(Stream *s) const =0
dw_addr_t m_loclists_base
Value of DW_AT_loclists_base.
Definition: DWARFUnit.h:334
void SetRangesBase(dw_addr_t ranges_base)
Definition: DWARFUnit.cpp:567
uint32_t GetHeaderByteSize() const
Get the size in bytes of the unit header.
Definition: DWARFUnit.cpp:995
void SetAddrBase(dw_addr_t addr_base)
Definition: DWARFUnit.cpp:463
void SetDwoError(Status &&error)
Set the fission .dwo file specific error for this compile unit.
Definition: DWARFUnit.h:269
std::optional< llvm::DWARFListTableHeader > m_loclist_table_header
Definition: DWARFUnit.h:346
llvm::VersionTuple GetProducerVersion()
Definition: DWARFUnit.cpp:781
virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges)=0
std::optional< uint64_t > m_dwo_id
Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
Definition: DWARFUnit.h:352
llvm::Expected< llvm::DWARFAddressRangesVector > FindRnglistFromIndex(uint32_t index)
Return a list of address ranges retrieved from an encoded range list whose offset is found via a tabl...
Definition: DWARFUnit.cpp:1057
std::shared_ptr< DWARFUnit > m_dwo
Definition: DWARFUnit.h:304
std::optional< uint64_t > m_gnu_addr_base
Definition: DWARFUnit.h:336
llvm::VersionTuple m_producer_version
Definition: DWARFUnit.h:328
std::optional< uint64_t > GetStringOffsetSectionItem(uint32_t index) const
Definition: DWARFUnit.cpp:1011
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:654
lldb::ByteOrder GetByteOrder() const
Definition: DWARFUnit.cpp:636
void AddUnitDIE(const DWARFDebugInfoEntry &cu_die)
Definition: DWARFUnit.cpp:384
llvm::sys::RWMutex m_first_die_mutex
Definition: DWARFUnit.h:322
llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, const DWARFDataExtractor &data, lldb::offset_t *offset_ptr)
SymbolFileDWARFDwo * GetDwoSymbolFile(bool load_all_debug_info=true)
Definition: DWARFUnit.cpp:892
const FileSpec & GetCompilationDirectory()
Definition: DWARFUnit.cpp:819
std::optional< uint64_t > GetDWOId()
Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's DW_AT_dwo_id or DW_AT_GNU_...
Definition: DWARFUnit.cpp:378
llvm::DWARFUnitHeader m_header
Definition: DWARFUnit.h:305
const llvm::DWARFAbbreviationDeclarationSet * GetAbbreviations() const
Definition: DWARFUnit.cpp:450
bool HasAny(llvm::ArrayRef< dw_tag_t > tags)
Returns true if any DIEs in the unit match any DW_TAG values in tags.
Definition: DWARFUnit.cpp:1064
std::atomic< DWARFUnit * > m_skeleton_unit
Definition: DWARFUnit.h:309
DWARFDataExtractor GetRnglistData() const
Definition: DWARFUnit.cpp:550
FileSpec GetFile(size_t file_idx)
Definition: DWARFUnit.cpp:831
uint64_t dw_offset_t
Definition: dwarf.h:30
#define DW_INVALID_OFFSET
Definition: dwarf.h:35
uint64_t dw_addr_t
Definition: dwarf.h:26
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition: DWARFUnit.h:31
uint64_t offset_t
Definition: lldb-types.h:85
ByteOrder
Byte ordering definitions.
uint64_t user_id_t
Definition: lldb-types.h:82
A mix in class that contains a generic user ID.
Definition: UserID.h:31