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;
173 /*check_hi_lo_pc=*/true) &&
174 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 s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated,
320 extraDiscriminator, isaPointer);
321 break;
322 }
323 default:
324 return;
325 }
326
327 // Follow the DW_AT_type if possible
328 if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
329 next_die.AppendTypeName(s);
330
331 switch (Tag()) {
332 case DW_TAG_array_type:
333 s.PutCString("[]");
334 break;
335 case DW_TAG_pointer_type:
336 s.PutChar('*');
337 break;
338 case DW_TAG_ptr_to_member_type:
339 s.PutChar('*');
340 break;
341 case DW_TAG_reference_type:
342 s.PutChar('&');
343 break;
344 default:
345 break;
346 }
347}
348
350 if (IsValid())
351 return GetDWARF()->ResolveType(*this, true);
352 else
353 return nullptr;
354}
355
358 return dwarf->ResolveTypeUID(die, true);
359 return nullptr;
360}
361
362std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
363 if (!IsValid())
364 return {};
365
366 std::vector<DWARFDIE> result;
368 while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
369 result.push_back(std::move(parent));
370 parent = parent.GetParentDeclContextDIE();
371 }
372
373 return result;
374}
375
378 const dw_tag_t tag = Tag();
379 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
380 return;
381 DWARFDIE parent = GetParent();
382 if (parent)
383 parent.GetDeclContext(context);
384 switch (tag) {
385 case DW_TAG_module:
386 context.push_back({CompilerContextKind::Module, ConstString(GetName())});
387 break;
388 case DW_TAG_namespace:
389 context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
390 break;
391 case DW_TAG_structure_type:
392 context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
393 break;
394 case DW_TAG_union_type:
395 context.push_back({CompilerContextKind::Union, ConstString(GetName())});
396 break;
397 case DW_TAG_class_type:
398 context.push_back({CompilerContextKind::Class, ConstString(GetName())});
399 break;
400 case DW_TAG_enumeration_type:
401 context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
402 break;
403 case DW_TAG_subprogram:
404 context.push_back(
405 {CompilerContextKind::Function, ConstString(GetPubname())});
406 break;
407 case DW_TAG_variable:
408 context.push_back(
409 {CompilerContextKind::Variable, ConstString(GetPubname())});
410 break;
411 case DW_TAG_typedef:
412 context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
413 break;
414 default:
415 break;
416 }
417}
418
421 if (IsValid())
423 else
424 return DWARFDIE();
425}
426
428 const dw_tag_t tag = Tag();
429 return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
430 tag == DW_TAG_union_type;
431}
432
433bool DWARFDIE::IsMethod() const {
434 for (DWARFDIE d : elaborating_dies(*this))
435 if (d.GetParent().IsStructUnionOrClass())
436 return true;
437 return false;
438}
439
441 const char *&name, const char *&mangled, DWARFRangeList &ranges,
442 int &decl_file, int &decl_line, int &decl_column, int &call_file,
443 int &call_line, 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:123
bool IsValid() const
Definition: DWARFBaseDIE.h:46
DWARFUnit * m_cu
Definition: DWARFBaseDIE.h:122
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:433
std::vector< DWARFDIE > GetDeclContextDIEs() const
Definition: DWARFDIE.cpp:362
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:376
const char * GetMangledName() const
Definition: DWARFDIE.cpp:199
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:420
bool GetDIENamesAndRanges(const char *&name, const char *&mangled, DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, int &call_file, int &call_line, int &call_column, lldb_private::DWARFExpressionList *frame_base) const
Definition: DWARFDIE.cpp:440
bool IsStructUnionOrClass() const
Definition: DWARFDIE.cpp:427
lldb_private::Type * ResolveTypeUID(const DWARFDIE &die) const
Definition: DWARFDIE.cpp:356
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:349
const char * GetName() const
void AppendTypeName(lldb_private::Stream &s) const
Definition: DWARFDIE.cpp:236
const char * GetPubname() const
Definition: DWARFDIE.cpp:206
DWARFDIE GetSibling() const
Definition: DWARFDIE.cpp:102
size_t GetAttributeAddressRanges(DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc, bool check_specification_or_abstract_origin=false) const
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
const char * GetMangledName(const DWARFUnit *cu, bool substitute_name_allowed=true) const
bool GetDIENamesAndRanges(DWARFUnit *cu, const char *&name, const char *&mangled, DWARFRangeList &rangeList, int &decl_file, int &decl_line, int &decl_column, int &call_file, int &call_line, int &call_column, lldb_private::DWARFExpressionList *frame_base=nullptr) 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()
DWARFDebugInfoEntry * GetParent()
DWARFDIE Reference() const
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:641
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:39
"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
llvm::dwarf::Tag dw_tag_t
Definition: dwarf.h:28
A class that represents a running process on the host machine.
const Scalar operator*(Scalar lhs, Scalar rhs)
Definition: Scalar.cpp:556
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1016
uint64_t addr_t
Definition: lldb-types.h:83