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/DWARFDebugAbbrev.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
18#include "llvm/Support/RWMutex.h"
19#include <atomic>
20#include <optional>
21
22namespace lldb_private::plugin {
23namespace dwarf {
24class DWARFUnit;
25class DWARFCompileUnit;
26class NameToDIE;
27class SymbolFileDWARF;
28class SymbolFileDWARFDwo;
29
30typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
31
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 void SetSkeletonUnit(DWARFUnit *skeleton_unit);
174
176
178
180
182
184
185 llvm::VersionTuple GetProducerVersion();
186
187 uint64_t GetDWARFLanguageType();
188
189 bool GetIsOptimized();
190
192 const FileSpec &GetAbsolutePath();
193 FileSpec GetFile(size_t file_idx);
195
196 SymbolFileDWARFDwo *GetDwoSymbolFile(bool load_all_debug_info = true);
197
200 return die_iterator_range(m_die_array.begin(), m_die_array.end());
201 }
202
204
205 uint8_t GetUnitType() const { return m_header.getUnitType(); }
206 bool IsTypeUnit() const { return m_header.isTypeUnit(); }
207 /// Note that this check only works for DWARF5+.
208 bool IsSkeletonUnit() const {
209 return GetUnitType() == llvm::dwarf::DW_UT_skeleton;
210 }
211
212 std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const;
213
214 /// Return a list of address ranges resulting from a (possibly encoded)
215 /// range list starting at a given offset in the appropriate ranges section.
216 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset);
217
218 /// Return a list of address ranges retrieved from an encoded range
219 /// list whose offset is found via a table lookup given an index (DWARF v5
220 /// and later).
221 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index);
222
223 /// Return a rangelist's offset based on an index. The index designates
224 /// an entry in the rangelist table's offset array and is supplied by
225 /// DW_FORM_rnglistx.
226 llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index);
227
228 std::optional<uint64_t> GetLoclistOffset(uint32_t Index) {
230 return std::nullopt;
231
232 std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(
234 if (!Offset)
235 return std::nullopt;
236 return *Offset + m_loclists_base;
237 }
238
239 /// Return the location table for parsing the given location list data. The
240 /// format is chosen according to the unit type. Never returns null.
241 std::unique_ptr<llvm::DWARFLocationTable>
242 GetLocationTable(const DataExtractor &data) const;
243
245
246 /// Returns true if any DIEs in the unit match any DW_TAG values in \a tags.
247 ///
248 /// \param[in] tags
249 /// An array of dw_tag_t values to check all abbrevitions for.
250 ///
251 /// \returns
252 /// True if any DIEs match any tag in \a tags, false otherwise.
253 bool HasAny(llvm::ArrayRef<dw_tag_t> tags);
254
255 /// Get the fission .dwo file specific error for this compile unit.
256 ///
257 /// The skeleton compile unit only can have a DWO error. Any other type
258 /// of DWARFUnit will not have a valid DWO error.
259 ///
260 /// \returns
261 /// A valid DWO error if there is a problem with anything in the
262 /// locating or parsing inforamtion in the .dwo file
263 const Status &GetDwoError() const { return m_dwo_error; }
264
265 /// Set the fission .dwo file specific error for this compile unit.
266 ///
267 /// This helps tracks issues that arise when trying to locate or parse a
268 /// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other
269 /// .dwo errors can be stored in each compile unit so the issues can be
270 /// communicated to the user.
272
273protected:
275 const llvm::DWARFUnitHeader &header,
276 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
277 DIERef::Section section, bool is_dwo);
278
280 const DWARFDataExtractor &data,
281 lldb::offset_t *offset_ptr);
282
283 // Get the DWARF unit DWARF debug information entry. Parse the single DIE
284 // if needed.
287 // m_first_die_mutex is not required as m_first_die is never cleared.
288 if (!m_first_die)
289 return nullptr;
290 return &m_first_die;
291 }
292
293 // Get all DWARF debug informration entries. Parse all DIEs if needed.
296 if (m_die_array.empty())
297 return nullptr;
298 return &m_die_array[0];
299 }
300
301 const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();
302
304
306 std::shared_ptr<DWARFUnit> m_dwo;
307 llvm::DWARFUnitHeader m_header;
308 const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
310 // If this is a DWO file, we have a backlink to our skeleton compile unit.
312 // The compile unit debug information entry item
314 mutable llvm::sys::RWMutex m_die_array_mutex;
315 // It is used for tracking of ScopedExtractDIEs instances.
316 mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
317 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
318 // as someone called ExtractDIEsIfNeeded().
319 std::atomic<bool> m_cancel_scopes;
320 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
321 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
322 // would possibly move in memory after later ExtractDIEsIfNeeded().
324 llvm::sys::RWMutex m_first_die_mutex;
325 // A table similar to the .debug_aranges table, but this one points to the
326 // exact DW_TAG_subprogram DIEs
327 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
330 llvm::VersionTuple m_producer_version;
331 std::optional<uint64_t> m_language_type;
333 std::optional<FileSpec> m_comp_dir;
334 std::optional<FileSpec> m_file_spec;
335 std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base.
336 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.
337 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base.
338 std::optional<uint64_t> m_gnu_addr_base;
339 std::optional<uint64_t> m_gnu_ranges_base;
340
341 /// Value of DW_AT_stmt_list.
343
344 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
345
346 std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;
348 std::optional<llvm::DWARFListTableHeader> m_loclist_table_header;
349
353 /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
354 std::optional<uint64_t> m_dwo_id;
355 /// If we get an error when trying to load a .dwo file, save that error here.
356 /// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found
357 /// but DWO ID doesn't match, etc.
359
360private:
361 void ParseProducerInfo();
362 void ExtractDIEsRWLocked();
363 void ClearDIEsRWLocked();
364
365 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
367
369 void ComputeAbsolutePath();
370
371 DWARFUnit(const DWARFUnit &) = delete;
372 const DWARFUnit &operator=(const DWARFUnit &) = delete;
373};
374} // namespace dwarf
375} // namespace lldb_private::plugin
376
377#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:41
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:44
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:335
bool IsSkeletonUnit() const
Note that this check only works for DWARF5+.
Definition: DWARFUnit.h:208
std::unique_ptr< llvm::DWARFLocationTable > GetLocationTable(const DataExtractor &data) const
Return the location table for parsing the given location list data.
Definition: DWARFUnit.cpp:522
std::optional< FileSpec > m_file_spec
Definition: DWARFUnit.h:334
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:346
lldb_private::CompileUnit * m_lldb_cu
Definition: DWARFUnit.h:309
const DWARFDebugInfoEntry * GetUnitDIEPtrOnly()
Definition: DWARFUnit.h:285
llvm::sys::RWMutex m_die_array_mutex
Definition: DWARFUnit.h:314
dw_offset_t m_line_table_offset
Value of DW_AT_stmt_list.
Definition: DWARFUnit.h:342
llvm::sys::RWMutex m_die_array_scoped_mutex
Definition: DWARFUnit.h:316
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
Definition: DWARFUnit.cpp:608
std::atomic< bool > m_cancel_scopes
Definition: DWARFUnit.h:319
Status m_dwo_error
If we get an error when trying to load a .dwo file, save that error here.
Definition: DWARFUnit.h:358
void SetLoclistsBase(dw_addr_t loclists_base)
Definition: DWARFUnit.cpp:490
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition: DWARFUnit.h:181
DIERef::Section GetDebugSection() const
Definition: DWARFUnit.h:203
const DWARFDebugAranges & GetFunctionAranges()
Definition: DWARFUnit.cpp:907
const DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
Definition: DWARFUnit.cpp:996
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:928
DWARFCompileUnit * GetSkeletonUnit()
Get the skeleton compile unit for a DWO file.
Definition: DWARFUnit.cpp:710
dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const
Definition: DWARFUnit.cpp:612
const DWARFDebugInfoEntry * DIEPtr()
Definition: DWARFUnit.h:294
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:333
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:263
std::optional< uint64_t > m_language_type
Definition: DWARFUnit.h:331
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:671
DWARFDebugInfoEntry::collection m_die_array
Definition: DWARFUnit.h:313
void SetBaseAddress(dw_addr_t base_addr)
Definition: DWARFUnit.cpp:636
std::optional< uint64_t > m_gnu_ranges_base
Definition: DWARFUnit.h:339
const llvm::DWARFAbbreviationDeclarationSet * m_abbrevs
Definition: DWARFUnit.h:308
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:350
dw_offset_t GetNextUnitOffset() const
Definition: DWARFUnit.h:114
DWARFDataExtractor GetLocationData() const
Definition: DWARFUnit.cpp:532
const std::optional< llvm::DWARFDebugRnglistTable > & GetRnglistTable()
Definition: DWARFUnit.cpp:570
llvm::Expected< uint64_t > GetRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
Definition: DWARFUnit.cpp:586
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
Definition: DWARFUnit.h:327
void SetSkeletonUnit(DWARFUnit *skeleton_unit)
Definition: DWARFUnit.cpp:721
std::optional< uint64_t > GetLoclistOffset(uint32_t Index)
Definition: DWARFUnit.h:228
dw_addr_t m_ranges_base
Value of DW_AT_rnglists_base.
Definition: DWARFUnit.h:337
virtual void Dump(Stream *s) const =0
dw_addr_t m_loclists_base
Value of DW_AT_loclists_base.
Definition: DWARFUnit.h:336
void SetRangesBase(dw_addr_t ranges_base)
Definition: DWARFUnit.cpp:563
uint32_t GetHeaderByteSize() const
Get the size in bytes of the unit header.
Definition: DWARFUnit.cpp:1002
void SetAddrBase(dw_addr_t addr_base)
Definition: DWARFUnit.cpp:459
llvm::Expected< DWARFRangeList > 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:1069
std::optional< llvm::DWARFListTableHeader > m_loclist_table_header
Definition: DWARFUnit.h:348
llvm::VersionTuple GetProducerVersion()
Definition: DWARFUnit.cpp:788
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:354
std::shared_ptr< DWARFUnit > m_dwo
Definition: DWARFUnit.h:306
std::optional< uint64_t > m_gnu_addr_base
Definition: DWARFUnit.h:338
llvm::VersionTuple m_producer_version
Definition: DWARFUnit.h:330
std::optional< uint64_t > GetStringOffsetSectionItem(uint32_t index) const
Definition: DWARFUnit.cpp:1018
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:650
lldb::ByteOrder GetByteOrder() const
Definition: DWARFUnit.cpp:632
void AddUnitDIE(const DWARFDebugInfoEntry &cu_die)
Definition: DWARFUnit.cpp:380
void SetDwoError(const Status &error)
Set the fission .dwo file specific error for this compile unit.
Definition: DWARFUnit.h:271
llvm::sys::RWMutex m_first_die_mutex
Definition: DWARFUnit.h:324
llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, const DWARFDataExtractor &data, lldb::offset_t *offset_ptr)
SymbolFileDWARFDwo * GetDwoSymbolFile(bool load_all_debug_info=true)
Definition: DWARFUnit.cpp:899
const FileSpec & GetCompilationDirectory()
Definition: DWARFUnit.cpp:826
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:374
llvm::DWARFUnitHeader m_header
Definition: DWARFUnit.h:307
const llvm::DWARFAbbreviationDeclarationSet * GetAbbreviations() const
Definition: DWARFUnit.cpp:446
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:1076
llvm::Expected< DWARFRangeList > 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:1024
DWARFDataExtractor GetRnglistData() const
Definition: DWARFUnit.cpp:546
FileSpec GetFile(size_t file_idx)
Definition: DWARFUnit.cpp:838
uint64_t dw_offset_t
Definition: dwarf.h:31
#define DW_INVALID_OFFSET
Definition: dwarf.h:36
uint64_t dw_addr_t
Definition: dwarf.h:27
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition: DWARFUnit.h:30
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