LLDB mainline
DWARFDIE.cpp
Go to the documentation of this file.
1//===-- DWARFDIE.cpp ------------------------------------------------------===//
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#include "DWARFDIE.h"
10
11#include "DWARFASTParser.h"
12#include "DWARFDebugInfo.h"
13#include "DWARFDebugInfoEntry.h"
14#include "DWARFDeclContext.h"
15#include "DWARFUnit.h"
16
17#include "llvm/ADT/iterator.h"
18
19using namespace lldb_private;
20using namespace lldb_private::dwarf;
21
22namespace {
23
24/// Iterate through all DIEs elaborating (i.e. reachable by a chain of
25/// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
26/// convenience, the starting die is included in the sequence as the first
27/// item.
28class ElaboratingDIEIterator
29 : public llvm::iterator_facade_base<
30 ElaboratingDIEIterator, std::input_iterator_tag, DWARFDIE,
31 std::ptrdiff_t, DWARFDIE *, DWARFDIE *> {
32
33 // The operating invariant is: top of m_worklist contains the "current" item
34 // and the rest of the list are items yet to be visited. An empty worklist
35 // means we've reached the end.
36 // Infinite recursion is prevented by maintaining a list of seen DIEs.
37 // Container sizes are optimized for the case of following DW_AT_specification
38 // and DW_AT_abstract_origin just once.
39 llvm::SmallVector<DWARFDIE, 2> m_worklist;
40 llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;
41
42 void Next() {
43 assert(!m_worklist.empty() && "Incrementing end iterator?");
44
45 // Pop the current item from the list.
46 DWARFDIE die = m_worklist.back();
47 m_worklist.pop_back();
48
49 // And add back any items that elaborate it.
50 for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
51 if (DWARFDIE d = die.GetReferencedDIE(attr))
52 if (m_seen.insert(die.GetDIE()).second)
53 m_worklist.push_back(d);
54 }
55 }
56
57public:
58 /// An iterator starting at die d.
59 explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
60
61 /// End marker
62 ElaboratingDIEIterator() = default;
63
64 const DWARFDIE &operator*() const { return m_worklist.back(); }
65 ElaboratingDIEIterator &operator++() {
66 Next();
67 return *this;
68 }
69
70 friend bool operator==(const ElaboratingDIEIterator &a,
71 const ElaboratingDIEIterator &b) {
72 if (a.m_worklist.empty() || b.m_worklist.empty())
73 return a.m_worklist.empty() == b.m_worklist.empty();
74 return a.m_worklist.back() == b.m_worklist.back();
75 }
76};
77
78llvm::iterator_range<ElaboratingDIEIterator>
79elaborating_dies(const DWARFDIE &die) {
80 return llvm::make_range(ElaboratingDIEIterator(die),
81 ElaboratingDIEIterator());
82}
83} // namespace
84
87 if (IsValid())
88 return DWARFDIE(m_cu, m_die->GetParent());
89 else
90 return DWARFDIE();
91}
92
95 if (IsValid())
96 return DWARFDIE(m_cu, m_die->GetFirstChild());
97 else
98 return DWARFDIE();
99}
100
103 if (IsValid())
104 return DWARFDIE(m_cu, m_die->GetSibling());
105 else
106 return DWARFDIE();
107}
108
111 if (IsValid())
113 else
114 return {};
115}
116
118DWARFDIE::GetDIE(dw_offset_t die_offset) const {
119 if (IsValid())
120 return m_cu->GetDIE(die_offset);
121 else
122 return DWARFDIE();
123}
124
127 if (IsValid()) {
128 DWARFUnit *cu = GetCU();
129 const bool check_specification_or_abstract_origin = true;
130 DWARFFormValue form_value;
131 if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
132 check_specification_or_abstract_origin))
133 return form_value.Reference();
134 }
135 return DWARFDIE();
136}
137
140 if (!IsValid())
141 return DWARFDIE();
142
143 DWARFDIE result;
144 bool check_children = false;
145 bool match_addr_range = false;
146 switch (Tag()) {
147 case DW_TAG_class_type:
148 case DW_TAG_namespace:
149 case DW_TAG_structure_type:
150 case DW_TAG_common_block:
151 check_children = true;
152 break;
153 case DW_TAG_compile_unit:
154 case DW_TAG_module:
155 case DW_TAG_catch_block:
156 case DW_TAG_subprogram:
157 case DW_TAG_try_block:
158 case DW_TAG_partial_unit:
159 match_addr_range = true;
160 break;
161 case DW_TAG_lexical_block:
162 case DW_TAG_inlined_subroutine:
163 check_children = true;
164 match_addr_range = true;
165 break;
166 default:
167 break;
168 }
169
170 if (match_addr_range) {
171 DWARFRangeList ranges =
172 m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
173 if (ranges.FindEntryThatContains(address)) {
174 check_children = true;
175 switch (Tag()) {
176 default:
177 break;
178
179 case DW_TAG_inlined_subroutine: // Inlined Function
180 case DW_TAG_lexical_block: // Block { } in code
181 result = *this;
182 break;
183 }
184 } else {
185 check_children = false;
186 }
187 }
188
189 if (check_children) {
190 for (DWARFDIE child : children()) {
191 if (DWARFDIE child_result = child.LookupDeepestBlock(address))
192 return child_result;
193 }
194 }
195 return result;
196}
197
198const char *DWARFDIE::GetMangledName() const {
199 if (IsValid())
200 return m_die->GetMangledName(m_cu);
201 else
202 return nullptr;
203}
204
205const char *DWARFDIE::GetPubname() const {
206 if (IsValid())
207 return m_die->GetPubname(m_cu);
208 else
209 return nullptr;
210}
211
212// GetName
213//
214// Get value of the DW_AT_name attribute and place that value into the supplied
215// stream object. If the DIE is a NULL object "NULL" is placed into the stream,
216// and if no DW_AT_name attribute exists for the DIE then nothing is printed.
217void DWARFDIE::GetName(Stream &s) const {
218 if (!IsValid())
219 return;
220 if (GetDIE()->IsNULL()) {
221 s.PutCString("NULL");
222 return;
223 }
224 const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
225 if (!name)
226 return;
227 s.PutCString(name);
228}
229
230// AppendTypeName
231//
232// Follows the type name definition down through all needed tags to end up with
233// a fully qualified type name and dump the results to the supplied stream.
234// This is used to show the name of types given a type identifier.
236 if (!IsValid())
237 return;
238 if (GetDIE()->IsNULL()) {
239 s.PutCString("NULL");
240 return;
241 }
242 if (const char *name = GetPubname()) {
243 s.PutCString(name);
244 return;
245 }
246 switch (Tag()) {
247 case DW_TAG_array_type:
248 break; // print out a "[]" after printing the full type of the element
249 // below
250 case DW_TAG_base_type:
251 s.PutCString("base ");
252 break;
253 case DW_TAG_class_type:
254 s.PutCString("class ");
255 break;
256 case DW_TAG_const_type:
257 s.PutCString("const ");
258 break;
259 case DW_TAG_enumeration_type:
260 s.PutCString("enum ");
261 break;
262 case DW_TAG_file_type:
263 s.PutCString("file ");
264 break;
265 case DW_TAG_interface_type:
266 s.PutCString("interface ");
267 break;
268 case DW_TAG_packed_type:
269 s.PutCString("packed ");
270 break;
271 case DW_TAG_pointer_type:
272 break; // print out a '*' after printing the full type below
273 case DW_TAG_ptr_to_member_type:
274 break; // print out a '*' after printing the full type below
275 case DW_TAG_reference_type:
276 break; // print out a '&' after printing the full type below
277 case DW_TAG_restrict_type:
278 s.PutCString("restrict ");
279 break;
280 case DW_TAG_set_type:
281 s.PutCString("set ");
282 break;
283 case DW_TAG_shared_type:
284 s.PutCString("shared ");
285 break;
286 case DW_TAG_string_type:
287 s.PutCString("string ");
288 break;
289 case DW_TAG_structure_type:
290 s.PutCString("struct ");
291 break;
292 case DW_TAG_subrange_type:
293 s.PutCString("subrange ");
294 break;
295 case DW_TAG_subroutine_type:
296 s.PutCString("function ");
297 break;
298 case DW_TAG_thrown_type:
299 s.PutCString("thrown ");
300 break;
301 case DW_TAG_union_type:
302 s.PutCString("union ");
303 break;
304 case DW_TAG_unspecified_type:
305 s.PutCString("unspecified ");
306 break;
307 case DW_TAG_volatile_type:
308 s.PutCString("volatile ");
309 break;
310 case DW_TAG_LLVM_ptrauth_type: {
311 unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0);
312 bool isAddressDiscriminated = GetAttributeValueAsUnsigned(
313 DW_AT_LLVM_ptrauth_address_discriminated, 0);
314 unsigned extraDiscriminator =
315 GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0);
316 bool isaPointer =
317 GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0);
318 s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated,
319 extraDiscriminator, isaPointer);
320 break;
321 }
322 default:
323 return;
324 }
325
326 // Follow the DW_AT_type if possible
327 if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
328 next_die.AppendTypeName(s);
329
330 switch (Tag()) {
331 case DW_TAG_array_type:
332 s.PutCString("[]");
333 break;
334 case DW_TAG_pointer_type:
335 s.PutChar('*');
336 break;
337 case DW_TAG_ptr_to_member_type:
338 s.PutChar('*');
339 break;
340 case DW_TAG_reference_type:
341 s.PutChar('&');
342 break;
343 default:
344 break;
345 }
346}
347
349 if (IsValid())
350 return GetDWARF()->ResolveType(*this, true);
351 else
352 return nullptr;
353}
354
357 return dwarf->ResolveTypeUID(die, true);
358 return nullptr;
359}
360
361std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
362 if (!IsValid())
363 return {};
364
365 std::vector<DWARFDIE> result;
367 while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
368 result.push_back(std::move(parent));
369 parent = parent.GetParentDeclContextDIE();
370 }
371
372 return result;
373}
374
377 const dw_tag_t tag = Tag();
378 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
379 return;
380 DWARFDIE parent = GetParent();
381 if (parent)
382 parent.GetDeclContext(context);
383 switch (tag) {
384 case DW_TAG_module:
385 context.push_back({CompilerContextKind::Module, ConstString(GetName())});
386 break;
387 case DW_TAG_namespace:
388 context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
389 break;
390 case DW_TAG_structure_type:
391 context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
392 break;
393 case DW_TAG_union_type:
394 context.push_back({CompilerContextKind::Union, ConstString(GetName())});
395 break;
396 case DW_TAG_class_type:
397 context.push_back({CompilerContextKind::Class, ConstString(GetName())});
398 break;
399 case DW_TAG_enumeration_type:
400 context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
401 break;
402 case DW_TAG_subprogram:
403 context.push_back(
404 {CompilerContextKind::Function, ConstString(GetPubname())});
405 break;
406 case DW_TAG_variable:
407 context.push_back(
408 {CompilerContextKind::Variable, ConstString(GetPubname())});
409 break;
410 case DW_TAG_typedef:
411 context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
412 break;
413 default:
414 break;
415 }
416}
417
420 if (IsValid())
422 else
423 return DWARFDIE();
424}
425
427 const dw_tag_t tag = Tag();
428 return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
429 tag == DW_TAG_union_type;
430}
431
432bool DWARFDIE::IsMethod() const {
433 for (DWARFDIE d : elaborating_dies(*this))
434 if (d.GetParent().IsStructUnionOrClass())
435 return true;
436 return false;
437}
438
440 const char *&name, const char *&mangled, DWARFRangeList &ranges,
441 std::optional<int> &decl_file, std::optional<int> &decl_line,
442 std::optional<int> &decl_column, std::optional<int> &call_file,
443 std::optional<int> &call_line, std::optional<int> &call_column,
444 lldb_private::DWARFExpressionList *frame_base) const {
445 if (IsValid()) {
447 GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
448 call_file, call_line, call_column, frame_base);
449 } else
450 return false;
451}
452
453llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const {
454 return llvm::make_range(child_iterator(*this), child_iterator());
455}
dw_tag_t Tag() const
DWARFDebugInfoEntry * m_die
Definition: DWARFBaseDIE.h:122
bool IsValid() const
Definition: DWARFBaseDIE.h:46
DWARFUnit * m_cu
Definition: DWARFBaseDIE.h:121
DWARFUnit * GetCU() const
Definition: DWARFBaseDIE.h:55
SymbolFileDWARF * GetDWARF() const
uint64_t GetAttributeValueAsUnsigned(const dw_attr_t attr, uint64_t fail_value) const
bool IsMethod() const
Definition: DWARFDIE.cpp:432
std::vector< DWARFDIE > GetDeclContextDIEs() const
Definition: DWARFDIE.cpp:361
DWARFDebugInfoEntry * GetDIE() const
Definition: DWARFBaseDIE.h:57
DWARFDIE GetFirstChild() const
Definition: DWARFDIE.cpp:94
DWARFDIE GetParent() const
Definition: DWARFDIE.cpp:86
void GetDeclContext(llvm::SmallVectorImpl< lldb_private::CompilerContext > &context) const
Return this DIE's decl context as it is needed to look up types in Clang's -gmodules debug info forma...
Definition: DWARFDIE.cpp:375
const char * GetMangledName() const
Definition: DWARFDIE.cpp:198
DWARFDIE GetDIE(dw_offset_t die_offset) const
Definition: DWARFDIE.cpp:118
llvm::iterator_range< child_iterator > children() const
The range of all the children of this DIE.
Definition: DWARFDIE.cpp:453
DWARFDIE GetParentDeclContextDIE() const
Definition: DWARFDIE.cpp:419
bool IsStructUnionOrClass() const
Definition: DWARFDIE.cpp:426
lldb_private::Type * ResolveTypeUID(const DWARFDIE &die) const
Definition: DWARFDIE.cpp:355
DWARFDIE LookupDeepestBlock(lldb::addr_t file_addr) const
Definition: DWARFDIE.cpp:139
DWARFDIE GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:126
DWARFDIE GetReferencedDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:110
lldb_private::Type * ResolveType() const
Definition: DWARFDIE.cpp:348
const char * GetName() const
bool GetDIENamesAndRanges(const char *&name, const char *&mangled, DWARFRangeList &ranges, std::optional< int > &decl_file, std::optional< int > &decl_line, std::optional< int > &decl_column, std::optional< int > &call_file, std::optional< int > &call_line, std::optional< int > &call_column, lldb_private::DWARFExpressionList *frame_base) const
Definition: DWARFDIE.cpp:439
void AppendTypeName(lldb_private::Stream &s) const
Definition: DWARFDIE.cpp:235
const char * GetPubname() const
Definition: DWARFDIE.cpp:205
DWARFDIE GetSibling() const
Definition: DWARFDIE.cpp:102
const char * GetAttributeValueAsString(const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, bool check_specification_or_abstract_origin=false) const
dw_offset_t GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &formValue, dw_offset_t *end_attr_offset_ptr=nullptr, bool check_specification_or_abstract_origin=false) const
DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const
bool GetDIENamesAndRanges(DWARFUnit *cu, const char *&name, const char *&mangled, DWARFRangeList &rangeList, std::optional< int > &decl_file, std::optional< int > &decl_line, std::optional< int > &decl_column, std::optional< int > &call_file, std::optional< int > &call_line, std::optional< int > &call_column, lldb_private::DWARFExpressionList *frame_base=nullptr) const
const char * GetMangledName(const DWARFUnit *cu, bool substitute_name_allowed=true) const
DWARFDIE GetAttributeValueAsReference(const DWARFUnit *cu, const dw_attr_t attr, bool check_specification_or_abstract_origin=false) const
const char * GetPubname(const DWARFUnit *cu) const
DWARFDebugInfoEntry * GetFirstChild()
DWARFDebugInfoEntry * GetSibling()
DWARFRangeList GetAttributeAddressRanges(DWARFUnit *cu, bool check_hi_lo_pc, bool check_specification_or_abstract_origin=false) const
DWARFDebugInfoEntry * GetParent()
DWARFDIE Reference() const
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:642
lldb_private::Type * ResolveType(const DWARFDIE &die, bool assert_not_being_parsed=true, bool resolve_function_context=false)
A uniqued constant string class.
Definition: ConstString.h:40
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
const Entry * FindEntryThatContains(B addr) const
Definition: RangeMap.h:338
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
size_t PutChar(char ch)
Definition: Stream.cpp:104
uint64_t dw_offset_t
Definition: dwarf.h:31
llvm::dwarf::Tag dw_tag_t
Definition: dwarf.h:26
llvm::dwarf::Attribute dw_attr_t
Definition: dwarf.h:24
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
const Scalar operator*(Scalar lhs, Scalar rhs)
Definition: Scalar.cpp:557
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1016
uint64_t addr_t
Definition: lldb-types.h:79