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"
17#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
20#include "llvm/Support/Mutex.h"
21#include "llvm/Support/RWMutex.h"
22#include <atomic>
23#include <optional>
24
25namespace lldb_private::plugin {
26namespace dwarf {
27class DWARFUnit;
29class NameToDIE;
30class SymbolFileDWARF;
32
33typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
34
42
45 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
46
47public:
48 static llvm::Expected<DWARFUnitSP>
50 const DWARFDataExtractor &debug_info, DIERef::Section section,
51 lldb::offset_t *offset_ptr);
52 virtual ~DWARFUnit();
53
54 bool IsDWOUnit() { return m_is_dwo; }
55 /// Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's
56 /// DW_AT_dwo_id or DW_AT_GNU_dwo_id for DWARF4 and earlier.
57 std::optional<uint64_t> GetDWOId();
58 /// Get the DWO ID from the DWARFUnitHeader only. DWARF5 skeleton units have
59 /// the DWO ID in the compile unit header and we sometimes only want to access
60 /// this cheap value without causing the more expensive attribute fetches that
61 /// GetDWOId() uses.
62 std::optional<uint64_t> GetHeaderDWOId() { return m_header.getDWOId(); }
66
80
81 bool Verify(Stream *s) const;
82 virtual void Dump(Stream *s) const = 0;
83 /// Get the data that contains the DIE information for this unit.
84 ///
85 /// This will return the correct bytes that contain the data for
86 /// this DWARFUnit. It could be .debug_info or .debug_types
87 /// depending on where the data for this unit originates.
88 ///
89 /// \return
90 /// The correct data for the DIE information in this unit.
91 const DWARFDataExtractor &GetData() const;
92
93 /// Get the size in bytes of the unit header.
94 ///
95 /// \return
96 /// Byte size of the unit header
97 uint32_t GetHeaderByteSize() const;
98
99 // Offset of the initial length field.
100 dw_offset_t GetOffset() const { return m_header.getOffset(); }
101 /// Get the size in bytes of the length field in the header.
102 ///
103 /// In DWARF32 this is just 4 bytes
104 ///
105 /// \return
106 /// Byte size of the compile unit header length field
107 size_t GetLengthByteSize() const { return 4; }
108
109 bool ContainsDIEOffset(dw_offset_t die_offset) const {
110 return die_offset >= GetFirstDIEOffset() &&
111 die_offset < GetNextUnitOffset();
112 }
114 return GetOffset() + GetHeaderByteSize();
115 }
116 dw_offset_t GetNextUnitOffset() const { return m_header.getNextUnitOffset(); }
117 // Size of the CU data (without initial length and without header).
118 size_t GetDebugInfoSize() const;
119 // Size of the CU data incl. header but without initial length.
120 dw_offset_t GetLength() const { return m_header.getLength(); }
121 uint16_t GetVersion() const override { return m_header.getVersion(); }
122 const llvm::dwarf::FormParams &GetFormParams() const {
123 return m_header.getFormParams();
124 }
125 const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
127 uint8_t GetAddressByteSize() const override {
128 return m_header.getAddressByteSize();
129 }
130 dw_addr_t GetAddrBase() const { return m_addr_base.value_or(0); }
131 dw_addr_t GetBaseAddress() const override { return m_base_addr; }
135 void SetAddrBase(dw_addr_t addr_base);
136 void SetLoclistsBase(dw_addr_t loclists_base);
137 void SetRangesBase(dw_addr_t ranges_base);
138 void SetStrOffsetsBase(dw_offset_t str_offsets_base);
139 virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;
140
141 dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const override;
142
144
146
147 void SetBaseAddress(dw_addr_t base_addr);
148
150
151 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
152
153 DWARFDIE GetDIE(dw_offset_t die_offset);
154
155 /// Returns the AT_Name of the DIE at `die_offset`, if it exists, without
156 /// parsing the entire compile unit. An empty is string is returned upon
157 /// error or if the attribute is not present.
158 llvm::StringRef PeekDIEName(dw_offset_t die_offset);
159
160 llvm::Expected<std::pair<uint64_t, bool>>
161 GetDIEBitSizeAndSign(uint64_t relative_die_offset) const override;
162
164 const lldb::offset_t data_offset,
165 const uint8_t op) const override;
166
167 virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
168 lldb::offset_t &offset,
169 RegisterContext *reg_ctx,
170 lldb::RegisterKind reg_kind,
171 std::vector<Value> &stack) const override;
172
173 bool ParseDWARFLocationList(const DataExtractor &data,
174 DWARFExpressionList &loc_list) const;
175
177
178 static uint8_t GetAddressByteSize(const DWARFUnit *cu);
179
180 static uint8_t GetDefaultAddressSize();
181
183
185
186 /// Get the skeleton compile unit for a DWO file.
187 ///
188 /// We need to keep track of the skeleton compile unit for a DWO file so
189 /// we can access it. Sometimes this value is cached when the skeleton
190 /// compile unit is first parsed, but if a .dwp file parses all of the
191 /// DWARFUnits in the file, the skeleton compile unit might not have been
192 /// parsed yet, to there might not be a backlink. This accessor handles
193 /// both cases correctly and avoids crashes.
195
196 bool LinkToSkeletonUnit(DWARFUnit &skeleton_unit);
197
199
201
203
204 llvm::VersionTuple GetProducerVersion();
205
206 uint64_t GetDWARFLanguageType();
207
208 bool GetIsOptimized();
209
211 const FileSpec &GetAbsolutePath();
212 FileSpec GetFile(size_t file_idx);
214
215 SymbolFileDWARFDwo *GetDwoSymbolFile(bool load_all_debug_info = true);
216
221
223
224 uint8_t GetUnitType() const { return m_header.getUnitType(); }
225 bool IsTypeUnit() const { return m_header.isTypeUnit(); }
226 /// Note that this check only works for DWARF5+.
227 bool IsSkeletonUnit() const {
228 return GetUnitType() == llvm::dwarf::DW_UT_skeleton;
229 }
230
231 std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const;
232
233 /// Return a list of address ranges resulting from a (possibly encoded)
234 /// range list starting at a given offset in the appropriate ranges section.
235 llvm::Expected<llvm::DWARFAddressRangesVector>
237
238 /// Return a list of address ranges retrieved from an encoded range
239 /// list whose offset is found via a table lookup given an index (DWARF v5
240 /// and later).
241 llvm::Expected<llvm::DWARFAddressRangesVector>
242 FindRnglistFromIndex(uint32_t index);
243
244 /// Return a rangelist's offset based on an index. The index designates
245 /// an entry in the rangelist table's offset array and is supplied by
246 /// DW_FORM_rnglistx.
247 llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index);
248
249 std::optional<uint64_t> GetLoclistOffset(uint32_t Index) {
251 return std::nullopt;
252
253 std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(
254 m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(), Index);
255 if (!Offset)
256 return std::nullopt;
257 return *Offset + m_loclists_base;
258 }
259
260 /// Return the location table for parsing the given location list data. The
261 /// format is chosen according to the unit type. Never returns null.
262 std::unique_ptr<llvm::DWARFLocationTable>
263 GetLocationTable(const DataExtractor &data) const;
264
266
267 /// Returns true if any DIEs in the unit match any DW_TAG values in \a tags.
268 ///
269 /// \param[in] tags
270 /// An array of dw_tag_t values to check all abbrevitions for.
271 ///
272 /// \returns
273 /// True if any DIEs match any tag in \a tags, false otherwise.
274 bool HasAny(llvm::ArrayRef<dw_tag_t> tags);
275
276 /// Get the fission .dwo file specific error for this compile unit.
277 ///
278 /// The skeleton compile unit only can have a DWO error. Any other type
279 /// of DWARFUnit will not have a valid DWO error.
280 ///
281 /// \returns
282 /// A valid DWO error if there is a problem with anything in the
283 /// locating or parsing inforamtion in the .dwo file
284 const Status &GetDwoError() const { return m_dwo_error; }
285
286 /// Set the fission .dwo file specific error for this compile unit.
287 ///
288 /// This helps tracks issues that arise when trying to locate or parse a
289 /// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other
290 /// .dwo errors can be stored in each compile unit so the issues can be
291 /// communicated to the user.
292 void SetDwoError(Status &&error) { m_dwo_error = std::move(error); }
293
294protected:
296 const llvm::DWARFUnitHeader &header,
297 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
298 DIERef::Section section, bool is_dwo);
299
301 const DWARFDataExtractor &data,
302 lldb::offset_t *offset_ptr);
303
304 // Get the DWARF unit DWARF debug information entry. Parse the single DIE
305 // if needed.
308 // m_first_die_mutex is not required as m_first_die is never cleared.
309 if (!m_first_die)
310 return nullptr;
311 return &m_first_die;
312 }
313
314 // Get all DWARF debug informration entries. Parse all DIEs if needed.
317 if (m_die_array.empty())
318 return nullptr;
319 return &m_die_array[0];
320 }
321
322 const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();
323
325
327 std::shared_ptr<DWARFUnit> m_dwo;
328 llvm::DWARFUnitHeader m_header;
329 const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
331 // If this is a DWO file, we have a backlink to our skeleton compile unit.
332 std::atomic<DWARFUnit *> m_skeleton_unit = nullptr;
333 // The compile unit debug information entry item
335 mutable llvm::sys::RWMutex m_die_array_mutex;
336 // It is used for tracking of ScopedExtractDIEs instances.
337 mutable llvm::sys::Mutex m_die_array_scoped_mutex;
339 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
340 // as someone called ExtractDIEsIfNeeded().
341 std::atomic<bool> m_cancel_scopes;
342 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
343 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
344 // would possibly move in memory after later ExtractDIEsIfNeeded().
346 llvm::sys::RWMutex m_first_die_mutex;
347 // A table similar to the .debug_aranges table, but this one points to the
348 // exact DW_TAG_subprogram DIEs
349 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
352 llvm::VersionTuple m_producer_version;
353 std::optional<uint64_t> m_language_type;
355 std::optional<FileSpec> m_comp_dir;
356 std::optional<FileSpec> m_file_spec;
357 std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base.
358 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.
359 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base.
360 std::optional<uint64_t> m_gnu_addr_base;
361 std::optional<uint64_t> m_gnu_ranges_base;
362
363 /// Value of DW_AT_stmt_list.
365
366 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
367
368 std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;
370 std::optional<llvm::DWARFListTableHeader> m_loclist_table_header;
371
375 /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
376 std::optional<uint64_t> m_dwo_id;
377 /// If we get an error when trying to load a .dwo file, save that error here.
378 /// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found
379 /// but DWO ID doesn't match, etc.
381
382private:
383 void ParseProducerInfo();
384 void ExtractDIEsRWLocked();
385 void ClearDIEsRWLocked();
386
387 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
389
391 void ComputeAbsolutePath();
392
393 DWARFUnit(const DWARFUnit &) = delete;
394 const DWARFUnit &operator=(const DWARFUnit &) = delete;
395};
396} // namespace dwarf
397} // namespace lldb_private::plugin
398
399#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
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
An data extractor class.
A file utility class.
Definition FileSpec.h:57
llvm::sys::path::Style Style
Definition FileSpec.h:59
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
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:357
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...
bool IsSkeletonUnit() const
Note that this check only works for DWARF5+.
Definition DWARFUnit.h:227
std::unique_ptr< llvm::DWARFLocationTable > GetLocationTable(const DataExtractor &data) const
Return the location table for parsing the given location list data.
std::optional< FileSpec > m_file_spec
Definition DWARFUnit.h:356
size_t GetLengthByteSize() const
Get the size in bytes of the length field in the header.
Definition DWARFUnit.h:107
std::optional< uint64_t > GetHeaderDWOId()
Get the DWO ID from the DWARFUnitHeader only.
Definition DWARFUnit.h:62
std::optional< llvm::DWARFDebugRnglistTable > m_rnglist_table
Definition DWARFUnit.h:368
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes, lldb::offset_t &offset, RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, std::vector< Value > &stack) const override
lldb_private::CompileUnit * m_lldb_cu
Definition DWARFUnit.h:330
const DWARFDebugInfoEntry * GetUnitDIEPtrOnly()
Definition DWARFUnit.h:306
bool LinkToSkeletonUnit(DWARFUnit &skeleton_unit)
dw_offset_t m_line_table_offset
Value of DW_AT_stmt_list.
Definition DWARFUnit.h:364
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
llvm::iterator_range< DWARFDebugInfoEntry::collection::iterator > die_iterator_range
Definition DWARFUnit.h:44
Status m_dwo_error
If we get an error when trying to load a .dwo file, save that error here.
Definition DWARFUnit.h:380
void SetLoclistsBase(dw_addr_t loclists_base)
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition DWARFUnit.h:200
DIERef::Section GetDebugSection() const
Definition DWARFUnit.h:222
const DWARFDebugAranges & GetFunctionAranges()
dw_addr_t GetBaseAddress() const override
Definition DWARFUnit.h:131
const DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, const DWARFDataExtractor &debug_info, DIERef::Section section, lldb::offset_t *offset_ptr)
DWARFCompileUnit * GetSkeletonUnit()
Get the skeleton compile unit for a DWO file.
const DWARFDebugInfoEntry * DIEPtr()
Definition DWARFUnit.h:315
bool ContainsDIEOffset(dw_offset_t die_offset) const
Definition DWARFUnit.h:109
std::optional< FileSpec > m_comp_dir
Definition DWARFUnit.h:355
dw_offset_t GetFirstDIEOffset() const
Definition DWARFUnit.h:113
const Status & GetDwoError() const
Get the fission .dwo file specific error for this compile unit.
Definition DWARFUnit.h:284
std::optional< uint64_t > m_language_type
Definition DWARFUnit.h:353
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.
DWARFDebugInfoEntry::collection m_die_array
Definition DWARFUnit.h:334
void SetBaseAddress(dw_addr_t base_addr)
std::optional< uint64_t > m_gnu_ranges_base
Definition DWARFUnit.h:361
const llvm::DWARFAbbreviationDeclarationSet * m_abbrevs
Definition DWARFUnit.h:329
void SetLLDBCompUnit(lldb_private::CompileUnit *cu)
Definition DWARFUnit.h:184
DWARFUnit(const DWARFUnit &)=delete
llvm::Expected< std::pair< uint64_t, bool > > GetDIEBitSizeAndSign(uint64_t relative_die_offset) const override
lldb_private::CompileUnit * GetLLDBCompUnit() const
Definition DWARFUnit.h:182
uint8_t GetAddressByteSize() const override
Definition DWARFUnit.h:127
dw_offset_t GetNextUnitOffset() const
Definition DWARFUnit.h:116
DWARFDataExtractor GetLocationData() const
const std::optional< llvm::DWARFDebugRnglistTable > & GetRnglistTable()
llvm::Expected< uint64_t > GetRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
Definition DWARFUnit.h:349
std::optional< uint64_t > GetLoclistOffset(uint32_t Index)
Definition DWARFUnit.h:249
dw_addr_t m_ranges_base
Value of DW_AT_rnglists_base.
Definition DWARFUnit.h:359
virtual void Dump(Stream *s) const =0
dw_addr_t m_loclists_base
Value of DW_AT_loclists_base.
Definition DWARFUnit.h:358
lldb::offset_t GetVendorDWARFOpcodeSize(const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op) const override
void SetRangesBase(dw_addr_t ranges_base)
uint32_t GetHeaderByteSize() const
Get the size in bytes of the unit header.
bool ParseDWARFLocationList(const DataExtractor &data, DWARFExpressionList &loc_list) const
void SetAddrBase(dw_addr_t addr_base)
void SetDwoError(Status &&error)
Set the fission .dwo file specific error for this compile unit.
Definition DWARFUnit.h:292
std::optional< llvm::DWARFListTableHeader > m_loclist_table_header
Definition DWARFUnit.h:370
llvm::VersionTuple GetProducerVersion()
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:376
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...
std::shared_ptr< DWARFUnit > m_dwo
Definition DWARFUnit.h:327
std::optional< uint64_t > m_gnu_addr_base
Definition DWARFUnit.h:360
DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, const llvm::DWARFUnitHeader &header, const llvm::DWARFAbbreviationDeclarationSet &abbrevs, DIERef::Section section, bool is_dwo)
Definition DWARFUnit.cpp:37
std::optional< uint64_t > GetStringOffsetSectionItem(uint32_t index) const
DWARFDIE GetDIE(dw_offset_t die_offset)
lldb::ByteOrder GetByteOrder() const
void AddUnitDIE(const DWARFDebugInfoEntry &cu_die)
llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, const DWARFDataExtractor &data, lldb::offset_t *offset_ptr)
SymbolFileDWARFDwo * GetDwoSymbolFile(bool load_all_debug_info=true)
uint16_t GetVersion() const override
Definition DWARFUnit.h:121
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_...
const llvm::DWARFAbbreviationDeclarationSet * GetAbbreviations() const
bool HasAny(llvm::ArrayRef< dw_tag_t > tags)
Returns true if any DIEs in the unit match any DW_TAG values in tags.
const llvm::dwarf::FormParams & GetFormParams() const
Definition DWARFUnit.h:122
std::atomic< DWARFUnit * > m_skeleton_unit
Definition DWARFUnit.h:332
dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const override
DWARFDataExtractor GetRnglistData() const
FileSpec GetFile(size_t file_idx)
uint64_t dw_offset_t
Definition dwarf.h:24
#define DW_INVALID_OFFSET
Definition dwarf.h:29
uint64_t dw_addr_t
Definition dwarf.h:20
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition DWARFUnit.h:33
uint64_t offset_t
Definition lldb-types.h:85
ByteOrder
Byte ordering definitions.
uint64_t user_id_t
Definition lldb-types.h:82
RegisterKind
Register numbering types.
UserID(lldb::user_id_t uid=LLDB_INVALID_UID)
Construct with optional user ID.
Definition UserID.h:33