1 //===-- DWARFDIE.cpp --------------------------------------------*- C++ -*-===//
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<lldb::user_id_t, 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.GetID()).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 file_addr) const {
145 SymbolFileDWARF *dwarf = GetDWARF();
146 DWARFUnit *cu = GetCU();
147 DWARFDebugInfoEntry *function_die = nullptr;
148 DWARFDebugInfoEntry *block_die = nullptr;
149 if (m_die->LookupAddress(file_addr, cu, &function_die, &block_die)) {
150 if (block_die && block_die != function_die) {
151 if (cu->ContainsDIEOffset(block_die->GetOffset()))
152 return DWARFDIE(cu, block_die);
154 return DWARFDIE(dwarf->DebugInfo()->GetUnit(DIERef(
155 cu->GetSymbolFileDWARF().GetDwoNum(),
156 cu->GetDebugSection(), block_die->GetOffset())),
164 const char *DWARFDIE::GetMangledName() const {
166 return m_die->GetMangledName(m_cu);
171 const char *DWARFDIE::GetPubname() const {
173 return m_die->GetPubname(m_cu);
178 const char *DWARFDIE::GetQualifiedName(std::string &storage) const {
180 return m_die->GetQualifiedName(m_cu, storage);
187 // Get value of the DW_AT_name attribute and place that value into the supplied
188 // stream object. If the DIE is a NULL object "NULL" is placed into the stream,
189 // and if no DW_AT_name attribute exists for the DIE then nothing is printed.
190 void DWARFDIE::GetName(Stream &s) const {
193 if (GetDIE()->IsNULL()) {
194 s.PutCString("NULL");
197 const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
205 // Follows the type name definition down through all needed tags to end up with
206 // a fully qualified type name and dump the results to the supplied stream.
207 // This is used to show the name of types given a type identifier.
208 void DWARFDIE::AppendTypeName(Stream &s) const {
211 if (GetDIE()->IsNULL()) {
212 s.PutCString("NULL");
215 if (const char *name = GetPubname()) {
220 case DW_TAG_array_type:
221 break; // print out a "[]" after printing the full type of the element
223 case DW_TAG_base_type:
224 s.PutCString("base ");
226 case DW_TAG_class_type:
227 s.PutCString("class ");
229 case DW_TAG_const_type:
230 s.PutCString("const ");
232 case DW_TAG_enumeration_type:
233 s.PutCString("enum ");
235 case DW_TAG_file_type:
236 s.PutCString("file ");
238 case DW_TAG_interface_type:
239 s.PutCString("interface ");
241 case DW_TAG_packed_type:
242 s.PutCString("packed ");
244 case DW_TAG_pointer_type:
245 break; // print out a '*' after printing the full type below
246 case DW_TAG_ptr_to_member_type:
247 break; // print out a '*' after printing the full type below
248 case DW_TAG_reference_type:
249 break; // print out a '&' after printing the full type below
250 case DW_TAG_restrict_type:
251 s.PutCString("restrict ");
253 case DW_TAG_set_type:
254 s.PutCString("set ");
256 case DW_TAG_shared_type:
257 s.PutCString("shared ");
259 case DW_TAG_string_type:
260 s.PutCString("string ");
262 case DW_TAG_structure_type:
263 s.PutCString("struct ");
265 case DW_TAG_subrange_type:
266 s.PutCString("subrange ");
268 case DW_TAG_subroutine_type:
269 s.PutCString("function ");
271 case DW_TAG_thrown_type:
272 s.PutCString("thrown ");
274 case DW_TAG_union_type:
275 s.PutCString("union ");
277 case DW_TAG_unspecified_type:
278 s.PutCString("unspecified ");
280 case DW_TAG_volatile_type:
281 s.PutCString("volatile ");
287 // Follow the DW_AT_type if possible
288 if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
289 next_die.AppendTypeName(s);
292 case DW_TAG_array_type:
295 case DW_TAG_pointer_type:
298 case DW_TAG_ptr_to_member_type:
301 case DW_TAG_reference_type:
309 lldb_private::Type *DWARFDIE::ResolveType() const {
311 return GetDWARF()->ResolveType(*this, true);
316 lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
317 if (SymbolFileDWARF *dwarf = GetDWARF())
318 return dwarf->ResolveTypeUID(die, true);
322 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
326 std::vector<DWARFDIE> result;
327 DWARFDIE parent = GetParentDeclContextDIE();
328 while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
329 result.push_back(std::move(parent));
330 parent = parent.GetParentDeclContextDIE();
336 void DWARFDIE::GetDWARFDeclContext(DWARFDeclContext &dwarf_decl_ctx) const {
338 dwarf_decl_ctx.SetLanguage(GetLanguage());
339 m_die->GetDWARFDeclContext(GetCU(), dwarf_decl_ctx);
341 dwarf_decl_ctx.Clear();
345 void DWARFDIE::GetDeclContext(std::vector<CompilerContext> &context) const {
346 const dw_tag_t tag = Tag();
347 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
349 DWARFDIE parent = GetParent();
351 parent.GetDeclContext(context);
355 CompilerContext(CompilerContextKind::Module, ConstString(GetName())));
357 case DW_TAG_namespace:
358 context.push_back(CompilerContext(CompilerContextKind::Namespace,
359 ConstString(GetName())));
361 case DW_TAG_structure_type:
362 context.push_back(CompilerContext(CompilerContextKind::Structure,
363 ConstString(GetName())));
365 case DW_TAG_union_type:
367 CompilerContext(CompilerContextKind::Union, ConstString(GetName())));
369 case DW_TAG_class_type:
371 CompilerContext(CompilerContextKind::Class, ConstString(GetName())));
373 case DW_TAG_enumeration_type:
374 context.push_back(CompilerContext(CompilerContextKind::Enumeration,
375 ConstString(GetName())));
377 case DW_TAG_subprogram:
378 context.push_back(CompilerContext(CompilerContextKind::Function,
379 ConstString(GetPubname())));
381 case DW_TAG_variable:
382 context.push_back(CompilerContext(CompilerContextKind::Variable,
383 ConstString(GetPubname())));
387 CompilerContext(CompilerContextKind::Typedef, ConstString(GetName())));
395 DWARFDIE::GetParentDeclContextDIE() const {
397 return m_die->GetParentDeclContextDIE(m_cu);
402 bool DWARFDIE::IsStructUnionOrClass() const {
403 const dw_tag_t tag = Tag();
404 return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
405 tag == DW_TAG_union_type;
408 bool DWARFDIE::IsMethod() const {
409 for (DWARFDIE d : elaborating_dies(*this))
410 if (d.GetParent().IsStructUnionOrClass())
416 DWARFDIE::GetContainingDWOModuleDIE() const {
418 DWARFDIE top_module_die;
419 // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
421 for (DWARFDIE parent = GetParent(); parent.IsValid();
422 parent = parent.GetParent()) {
423 const dw_tag_t tag = parent.Tag();
424 if (tag == DW_TAG_module)
425 top_module_die = parent;
426 else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
430 return top_module_die;
435 lldb::ModuleSP DWARFDIE::GetContainingDWOModule() const {
437 DWARFDIE dwo_module_die = GetContainingDWOModuleDIE();
439 if (dwo_module_die) {
440 const char *module_name = dwo_module_die.GetName();
442 return GetDWARF()->GetDWOModule(lldb_private::ConstString(module_name));
445 return lldb::ModuleSP();
448 bool DWARFDIE::GetDIENamesAndRanges(
449 const char *&name, const char *&mangled, DWARFRangeList &ranges,
450 int &decl_file, int &decl_line, int &decl_column, int &call_file,
451 int &call_line, int &call_column,
452 lldb_private::DWARFExpression *frame_base) const {
454 return m_die->GetDIENamesAndRanges(
455 GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
456 call_file, call_line, call_column, frame_base);
461 CompilerDecl DWARFDIE::GetDecl() const {
462 DWARFASTParser *dwarf_ast = GetDWARFParser();
464 return dwarf_ast->GetDeclForUIDFromDWARF(*this);
466 return CompilerDecl();
469 CompilerDeclContext DWARFDIE::GetDeclContext() const {
470 DWARFASTParser *dwarf_ast = GetDWARFParser();
472 return dwarf_ast->GetDeclContextForUIDFromDWARF(*this);
474 return CompilerDeclContext();
477 CompilerDeclContext DWARFDIE::GetContainingDeclContext() const {
478 DWARFASTParser *dwarf_ast = GetDWARFParser();
480 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(*this);
482 return CompilerDeclContext();