1 //===-- DWARFDIE.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 #include "DWARFASTParser.h"
12 #include "DWARFDebugInfo.h"
13 #include "DWARFDebugInfoEntry.h"
14 #include "DWARFDeclContext.h"
15 #include "DWARFUnit.h"
17 using namespace lldb_private;
21 /// Iterate through all DIEs elaborating (i.e. reachable by a chain of
22 /// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
23 /// convenience, the starting die is included in the sequence as the first
25 class ElaboratingDIEIterator
26 : public std::iterator<std::input_iterator_tag, DWARFDIE> {
28 // The operating invariant is: top of m_worklist contains the "current" item
29 // and the rest of the list are items yet to be visited. An empty worklist
30 // means we've reached the end.
31 // Infinite recursion is prevented by maintaining a list of seen DIEs.
32 // Container sizes are optimized for the case of following DW_AT_specification
33 // and DW_AT_abstract_origin just once.
34 llvm::SmallVector<DWARFDIE, 2> m_worklist;
35 llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;
38 assert(!m_worklist.empty() && "Incrementing end iterator?");
40 // Pop the current item from the list.
41 DWARFDIE die = m_worklist.back();
42 m_worklist.pop_back();
44 // And add back any items that elaborate it.
45 for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
46 if (DWARFDIE d = die.GetReferencedDIE(attr))
47 if (m_seen.insert(die.GetDIE()).second)
48 m_worklist.push_back(d);
53 /// An iterator starting at die d.
54 explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
57 ElaboratingDIEIterator() {}
59 const DWARFDIE &operator*() const { return m_worklist.back(); }
60 ElaboratingDIEIterator &operator++() {
64 ElaboratingDIEIterator operator++(int) {
65 ElaboratingDIEIterator I = *this;
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();
76 friend bool operator!=(const ElaboratingDIEIterator &a,
77 const ElaboratingDIEIterator &b) {
82 llvm::iterator_range<ElaboratingDIEIterator>
83 elaborating_dies(const DWARFDIE &die) {
84 return llvm::make_range(ElaboratingDIEIterator(die),
85 ElaboratingDIEIterator());
90 DWARFDIE::GetParent() const {
92 return DWARFDIE(m_cu, m_die->GetParent());
98 DWARFDIE::GetFirstChild() const {
100 return DWARFDIE(m_cu, m_die->GetFirstChild());
106 DWARFDIE::GetSibling() const {
108 return DWARFDIE(m_cu, m_die->GetSibling());
114 DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const {
116 return m_die->GetAttributeValueAsReference(GetCU(), attr);
122 DWARFDIE::GetDIE(dw_offset_t die_offset) const {
124 return m_cu->GetDIE(die_offset);
130 DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const {
132 DWARFUnit *cu = GetCU();
133 const bool check_specification_or_abstract_origin = true;
134 DWARFFormValue form_value;
135 if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
136 check_specification_or_abstract_origin))
137 return form_value.Reference();
143 DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
148 bool check_children = false;
149 bool match_addr_range = false;
151 case DW_TAG_class_type:
152 case DW_TAG_namespace:
153 case DW_TAG_structure_type:
154 case DW_TAG_common_block:
155 check_children = true;
157 case DW_TAG_compile_unit:
159 case DW_TAG_catch_block:
160 case DW_TAG_subprogram:
161 case DW_TAG_try_block:
162 case DW_TAG_partial_unit:
163 match_addr_range = true;
165 case DW_TAG_lexical_block:
166 case DW_TAG_inlined_subroutine:
167 check_children = true;
168 match_addr_range = true;
174 if (match_addr_range) {
175 DWARFRangeList ranges;
176 if (m_die->GetAttributeAddressRanges(m_cu, ranges,
177 /*check_hi_lo_pc=*/true) &&
178 ranges.FindEntryThatContains(address)) {
179 check_children = true;
184 case DW_TAG_inlined_subroutine: // Inlined Function
185 case DW_TAG_lexical_block: // Block { } in code
190 check_children = false;
194 if (check_children) {
195 for (DWARFDIE child = GetFirstChild(); child; child = child.GetSibling()) {
196 if (DWARFDIE child_result = child.LookupDeepestBlock(address))
203 const char *DWARFDIE::GetMangledName() const {
205 return m_die->GetMangledName(m_cu);
210 const char *DWARFDIE::GetPubname() const {
212 return m_die->GetPubname(m_cu);
217 const char *DWARFDIE::GetQualifiedName(std::string &storage) const {
219 return m_die->GetQualifiedName(m_cu, storage);
226 // Get value of the DW_AT_name attribute and place that value into the supplied
227 // stream object. If the DIE is a NULL object "NULL" is placed into the stream,
228 // and if no DW_AT_name attribute exists for the DIE then nothing is printed.
229 void DWARFDIE::GetName(Stream &s) const {
232 if (GetDIE()->IsNULL()) {
233 s.PutCString("NULL");
236 const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
244 // Follows the type name definition down through all needed tags to end up with
245 // a fully qualified type name and dump the results to the supplied stream.
246 // This is used to show the name of types given a type identifier.
247 void DWARFDIE::AppendTypeName(Stream &s) const {
250 if (GetDIE()->IsNULL()) {
251 s.PutCString("NULL");
254 if (const char *name = GetPubname()) {
259 case DW_TAG_array_type:
260 break; // print out a "[]" after printing the full type of the element
262 case DW_TAG_base_type:
263 s.PutCString("base ");
265 case DW_TAG_class_type:
266 s.PutCString("class ");
268 case DW_TAG_const_type:
269 s.PutCString("const ");
271 case DW_TAG_enumeration_type:
272 s.PutCString("enum ");
274 case DW_TAG_file_type:
275 s.PutCString("file ");
277 case DW_TAG_interface_type:
278 s.PutCString("interface ");
280 case DW_TAG_packed_type:
281 s.PutCString("packed ");
283 case DW_TAG_pointer_type:
284 break; // print out a '*' after printing the full type below
285 case DW_TAG_ptr_to_member_type:
286 break; // print out a '*' after printing the full type below
287 case DW_TAG_reference_type:
288 break; // print out a '&' after printing the full type below
289 case DW_TAG_restrict_type:
290 s.PutCString("restrict ");
292 case DW_TAG_set_type:
293 s.PutCString("set ");
295 case DW_TAG_shared_type:
296 s.PutCString("shared ");
298 case DW_TAG_string_type:
299 s.PutCString("string ");
301 case DW_TAG_structure_type:
302 s.PutCString("struct ");
304 case DW_TAG_subrange_type:
305 s.PutCString("subrange ");
307 case DW_TAG_subroutine_type:
308 s.PutCString("function ");
310 case DW_TAG_thrown_type:
311 s.PutCString("thrown ");
313 case DW_TAG_union_type:
314 s.PutCString("union ");
316 case DW_TAG_unspecified_type:
317 s.PutCString("unspecified ");
319 case DW_TAG_volatile_type:
320 s.PutCString("volatile ");
326 // Follow the DW_AT_type if possible
327 if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
328 next_die.AppendTypeName(s);
331 case DW_TAG_array_type:
334 case DW_TAG_pointer_type:
337 case DW_TAG_ptr_to_member_type:
340 case DW_TAG_reference_type:
348 lldb_private::Type *DWARFDIE::ResolveType() const {
350 return GetDWARF()->ResolveType(*this, true);
355 lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
356 if (SymbolFileDWARF *dwarf = GetDWARF())
357 return dwarf->ResolveTypeUID(die, true);
361 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
365 std::vector<DWARFDIE> result;
366 DWARFDIE parent = GetParentDeclContextDIE();
367 while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
368 result.push_back(std::move(parent));
369 parent = parent.GetParentDeclContextDIE();
375 void DWARFDIE::GetDeclContext(
376 llvm::SmallVectorImpl<lldb_private::CompilerContext> &context) const {
377 const dw_tag_t tag = Tag();
378 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
380 DWARFDIE parent = GetParent();
382 parent.GetDeclContext(context);
385 context.push_back({CompilerContextKind::Module, ConstString(GetName())});
387 case DW_TAG_namespace:
388 context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
390 case DW_TAG_structure_type:
391 context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
393 case DW_TAG_union_type:
394 context.push_back({CompilerContextKind::Union, ConstString(GetName())});
396 case DW_TAG_class_type:
397 context.push_back({CompilerContextKind::Class, ConstString(GetName())});
399 case DW_TAG_enumeration_type:
400 context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
402 case DW_TAG_subprogram:
404 {CompilerContextKind::Function, ConstString(GetPubname())});
406 case DW_TAG_variable:
408 {CompilerContextKind::Variable, ConstString(GetPubname())});
411 context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
419 DWARFDIE::GetParentDeclContextDIE() const {
421 return m_die->GetParentDeclContextDIE(m_cu);
426 bool DWARFDIE::IsStructUnionOrClass() const {
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;
432 bool DWARFDIE::IsMethod() const {
433 for (DWARFDIE d : elaborating_dies(*this))
434 if (d.GetParent().IsStructUnionOrClass())
439 bool DWARFDIE::GetDIENamesAndRanges(
440 const char *&name, const char *&mangled, DWARFRangeList &ranges,
441 int &decl_file, int &decl_line, int &decl_column, int &call_file,
442 int &call_line, int &call_column,
443 lldb_private::DWARFExpression *frame_base) const {
445 return m_die->GetDIENamesAndRanges(
446 GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
447 call_file, call_line, call_column, frame_base);