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