xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFBaseDIE.cpp (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1 //===-- DWARFBaseDIE.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 "DWARFBaseDIE.h"
10 
11 #include "DWARFUnit.h"
12 #include "DWARFDebugInfoEntry.h"
13 #include "SymbolFileDWARF.h"
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/Log.h"
18 #include <optional>
19 
20 using namespace lldb_private;
21 using namespace lldb_private::plugin::dwarf;
22 
23 std::optional<DIERef> DWARFBaseDIE::GetDIERef() const {
24   if (!IsValid())
25     return std::nullopt;
26 
27   return DIERef(m_cu->GetSymbolFileDWARF().GetFileIndex(),
28                 m_cu->GetDebugSection(), m_die->GetOffset());
29 }
30 
31 dw_tag_t DWARFBaseDIE::Tag() const {
32   if (m_die)
33     return m_die->Tag();
34   else
35     return llvm::dwarf::DW_TAG_null;
36 }
37 
38 const char *DWARFBaseDIE::GetTagAsCString() const {
39   return DW_TAG_value_to_name(Tag());
40 }
41 
42 const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr,
43                                                 const char *fail_value) const {
44   if (IsValid())
45     return m_die->GetAttributeValueAsString(GetCU(), attr, fail_value);
46   else
47     return fail_value;
48 }
49 
50 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr,
51                                                uint64_t fail_value) const {
52   if (IsValid())
53     return m_die->GetAttributeValueAsUnsigned(GetCU(), attr, fail_value);
54   else
55     return fail_value;
56 }
57 
58 std::optional<uint64_t>
59 DWARFBaseDIE::GetAttributeValueAsOptionalUnsigned(const dw_attr_t attr) const {
60   if (IsValid())
61     return m_die->GetAttributeValueAsOptionalUnsigned(GetCU(), attr);
62   return std::nullopt;
63 }
64 
65 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
66                                               uint64_t fail_value) const {
67   if (IsValid())
68     return m_die->GetAttributeValueAsAddress(GetCU(), attr, fail_value);
69   else
70     return fail_value;
71 }
72 
73 lldb::user_id_t DWARFBaseDIE::GetID() const {
74   const std::optional<DIERef> &ref = this->GetDIERef();
75   if (ref)
76     return ref->get_id();
77 
78   return LLDB_INVALID_UID;
79 }
80 
81 const char *DWARFBaseDIE::GetName() const {
82   if (IsValid())
83     return m_die->GetName(m_cu);
84   else
85     return nullptr;
86 }
87 
88 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
89   SymbolFileDWARF *dwarf = GetDWARF();
90   if (dwarf)
91     return dwarf->GetObjectFile()->GetModule();
92   else
93     return lldb::ModuleSP();
94 }
95 
96 dw_offset_t DWARFBaseDIE::GetOffset() const {
97   if (IsValid())
98     return m_die->GetOffset();
99   else
100     return DW_INVALID_OFFSET;
101 }
102 
103 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
104   if (m_cu)
105     return &m_cu->GetSymbolFileDWARF();
106   else
107     return nullptr;
108 }
109 
110 bool DWARFBaseDIE::HasChildren() const {
111   return m_die && m_die->HasChildren();
112 }
113 
114 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const {
115   return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
116 }
117 
118 DWARFAttributes DWARFBaseDIE::GetAttributes(Recurse recurse) const {
119   if (IsValid())
120     return m_die->GetAttributes(m_cu, recurse);
121   return DWARFAttributes();
122 }
123 
124 namespace lldb_private::plugin {
125 namespace dwarf {
126 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
127   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
128 }
129 
130 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
131   return !(lhs == rhs);
132 }
133 } // namespace dwarf
134 } // namespace lldb_private::plugin
135 
136 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
137   // Clients must check if this DIE is valid before calling this function.
138   assert(IsValid());
139   return m_cu->GetData();
140 }
141