1 //===-- DebugNamesDWARFIndex.cpp -------------------------------*- C++ -*-===//
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 "Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.h"
10 #include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
11 #include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
12 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h"
13 #include "lldb/Utility/RegularExpression.h"
14 #include "lldb/Utility/Stream.h"
15 
16 using namespace lldb_private;
17 using namespace lldb;
18 
19 static llvm::DWARFDataExtractor ToLLVM(const DWARFDataExtractor &data) {
20   return llvm::DWARFDataExtractor(
21       llvm::StringRef(reinterpret_cast<const char *>(data.GetDataStart()),
22                       data.GetByteSize()),
23       data.GetByteOrder() == eByteOrderLittle, data.GetAddressByteSize());
24 }
25 
26 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>>
27 DebugNamesDWARFIndex::Create(Module &module, DWARFDataExtractor debug_names,
28                              DWARFDataExtractor debug_str,
29                              DWARFDebugInfo *debug_info) {
30   if (!debug_info) {
31     return llvm::make_error<llvm::StringError>("debug info null",
32                                                llvm::inconvertibleErrorCode());
33   }
34   auto index_up =
35       llvm::make_unique<DebugNames>(ToLLVM(debug_names), ToLLVM(debug_str));
36   if (llvm::Error E = index_up->extract())
37     return std::move(E);
38 
39   return std::unique_ptr<DebugNamesDWARFIndex>(new DebugNamesDWARFIndex(
40       module, std::move(index_up), debug_names, debug_str, *debug_info));
41 }
42 
43 llvm::DenseSet<dw_offset_t>
44 DebugNamesDWARFIndex::GetUnits(const DebugNames &debug_names) {
45   llvm::DenseSet<dw_offset_t> result;
46   for (const DebugNames::NameIndex &ni : debug_names) {
47     for (uint32_t cu = 0; cu < ni.getCUCount(); ++cu)
48       result.insert(ni.getCUOffset(cu));
49   }
50   return result;
51 }
52 
53 llvm::Optional<DIERef>
54 DebugNamesDWARFIndex::ToDIERef(const DebugNames::Entry &entry) {
55   llvm::Optional<uint64_t> cu_offset = entry.getCUOffset();
56   if (!cu_offset)
57     return llvm::None;
58 
59   DWARFUnit *cu = m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, *cu_offset);
60   if (!cu)
61     return llvm::None;
62 
63   // This initializes the DWO symbol file. It's not possible for
64   // GetDwoSymbolFile to call this automatically because of mutual recursion
65   // between this and DWARFDebugInfoEntry::GetAttributeValue.
66   cu->ExtractUnitDIEIfNeeded();
67   uint64_t die_bias = cu->GetDwoSymbolFile() ? 0 : *cu_offset;
68 
69   if (llvm::Optional<uint64_t> die_offset = entry.getDIEUnitOffset())
70     return DIERef(DIERef::Section::DebugInfo, *cu_offset, die_bias + *die_offset);
71 
72   return llvm::None;
73 }
74 
75 void DebugNamesDWARFIndex::Append(const DebugNames::Entry &entry,
76                                   DIEArray &offsets) {
77   if (llvm::Optional<DIERef> ref = ToDIERef(entry))
78     offsets.push_back(*ref);
79 }
80 
81 void DebugNamesDWARFIndex::MaybeLogLookupError(llvm::Error error,
82                                                const DebugNames::NameIndex &ni,
83                                                llvm::StringRef name) {
84   // Ignore SentinelErrors, log everything else.
85   LLDB_LOG_ERROR(
86       LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS),
87       handleErrors(std::move(error), [](const DebugNames::SentinelError &) {}),
88       "Failed to parse index entries for index at {1:x}, name {2}: {0}",
89       ni.getUnitOffset(), name);
90 }
91 
92 void DebugNamesDWARFIndex::GetGlobalVariables(ConstString basename,
93                                               DIEArray &offsets) {
94   m_fallback.GetGlobalVariables(basename, offsets);
95 
96   for (const DebugNames::Entry &entry :
97        m_debug_names_up->equal_range(basename.GetStringRef())) {
98     if (entry.tag() != DW_TAG_variable)
99       continue;
100 
101     Append(entry, offsets);
102   }
103 }
104 
105 void DebugNamesDWARFIndex::GetGlobalVariables(const RegularExpression &regex,
106                                               DIEArray &offsets) {
107   m_fallback.GetGlobalVariables(regex, offsets);
108 
109   for (const DebugNames::NameIndex &ni: *m_debug_names_up) {
110     for (DebugNames::NameTableEntry nte: ni) {
111       if (!regex.Execute(nte.getString()))
112         continue;
113 
114       uint32_t entry_offset = nte.getEntryOffset();
115       llvm::Expected<DebugNames::Entry> entry_or = ni.getEntry(&entry_offset);
116       for (; entry_or; entry_or = ni.getEntry(&entry_offset)) {
117         if (entry_or->tag() != DW_TAG_variable)
118           continue;
119 
120         Append(*entry_or, offsets);
121       }
122       MaybeLogLookupError(entry_or.takeError(), ni, nte.getString());
123     }
124   }
125 }
126 
127 void DebugNamesDWARFIndex::GetGlobalVariables(const DWARFUnit &cu,
128                                               DIEArray &offsets) {
129   m_fallback.GetGlobalVariables(cu, offsets);
130 
131   uint64_t cu_offset = cu.GetOffset();
132   for (const DebugNames::NameIndex &ni: *m_debug_names_up) {
133     for (DebugNames::NameTableEntry nte: ni) {
134       uint32_t entry_offset = nte.getEntryOffset();
135       llvm::Expected<DebugNames::Entry> entry_or = ni.getEntry(&entry_offset);
136       for (; entry_or; entry_or = ni.getEntry(&entry_offset)) {
137         if (entry_or->tag() != DW_TAG_variable)
138           continue;
139         if (entry_or->getCUOffset() != cu_offset)
140           continue;
141 
142         Append(*entry_or, offsets);
143       }
144       MaybeLogLookupError(entry_or.takeError(), ni, nte.getString());
145     }
146   }
147 }
148 
149 void DebugNamesDWARFIndex::GetCompleteObjCClass(ConstString class_name,
150                                                 bool must_be_implementation,
151                                                 DIEArray &offsets) {
152   m_fallback.GetCompleteObjCClass(class_name, must_be_implementation, offsets);
153 
154   // Keep a list of incomplete types as fallback for when we don't find the
155   // complete type.
156   DIEArray incomplete_types;
157 
158   for (const DebugNames::Entry &entry :
159        m_debug_names_up->equal_range(class_name.GetStringRef())) {
160     if (entry.tag() != DW_TAG_structure_type &&
161         entry.tag() != DW_TAG_class_type)
162       continue;
163 
164     llvm::Optional<DIERef> ref = ToDIERef(entry);
165     if (!ref)
166       continue;
167 
168     DWARFUnit *cu = m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo,
169                                                  *ref->unit_offset());
170     if (!cu || !cu->Supports_DW_AT_APPLE_objc_complete_type()) {
171       incomplete_types.push_back(*ref);
172       continue;
173     }
174 
175     // FIXME: We should return DWARFDIEs so we don't have to resolve it twice.
176     DWARFDIE die = m_debug_info.GetDIE(*ref);
177     if (!die)
178       continue;
179 
180     if (die.GetAttributeValueAsUnsigned(DW_AT_APPLE_objc_complete_type, 0)) {
181       // If we find the complete version we're done.
182       offsets.push_back(*ref);
183       return;
184     } else {
185       incomplete_types.push_back(*ref);
186     }
187   }
188 
189   offsets.insert(offsets.end(), incomplete_types.begin(),
190                  incomplete_types.end());
191 }
192 
193 void DebugNamesDWARFIndex::GetTypes(ConstString name, DIEArray &offsets) {
194   m_fallback.GetTypes(name, offsets);
195 
196   for (const DebugNames::Entry &entry :
197        m_debug_names_up->equal_range(name.GetStringRef())) {
198     if (isType(entry.tag()))
199       Append(entry, offsets);
200   }
201 }
202 
203 void DebugNamesDWARFIndex::GetTypes(const DWARFDeclContext &context,
204                                     DIEArray &offsets) {
205   m_fallback.GetTypes(context, offsets);
206 
207   for (const DebugNames::Entry &entry :
208        m_debug_names_up->equal_range(context[0].name)) {
209     if (entry.tag() == context[0].tag)
210       Append(entry, offsets);
211   }
212 }
213 
214 void DebugNamesDWARFIndex::GetNamespaces(ConstString name, DIEArray &offsets) {
215   m_fallback.GetNamespaces(name, offsets);
216 
217   for (const DebugNames::Entry &entry :
218        m_debug_names_up->equal_range(name.GetStringRef())) {
219     if (entry.tag() == DW_TAG_namespace)
220       Append(entry, offsets);
221   }
222 }
223 
224 void DebugNamesDWARFIndex::GetFunctions(
225     ConstString name, DWARFDebugInfo &info,
226     const CompilerDeclContext &parent_decl_ctx, uint32_t name_type_mask,
227     std::vector<DWARFDIE> &dies) {
228 
229   std::vector<DWARFDIE> v;
230   m_fallback.GetFunctions(name, info, parent_decl_ctx, name_type_mask, v);
231 
232   for (const DebugNames::Entry &entry :
233        m_debug_names_up->equal_range(name.GetStringRef())) {
234     Tag tag = entry.tag();
235     if (tag != DW_TAG_subprogram && tag != DW_TAG_inlined_subroutine)
236       continue;
237 
238     if (llvm::Optional<DIERef> ref = ToDIERef(entry))
239       ProcessFunctionDIE(name.GetStringRef(), *ref, info, parent_decl_ctx,
240                          name_type_mask, v);
241   }
242 
243   std::set<DWARFDebugInfoEntry *> seen;
244   for (DWARFDIE die : v)
245     if (seen.insert(die.GetDIE()).second)
246       dies.push_back(die);
247 }
248 
249 void DebugNamesDWARFIndex::GetFunctions(const RegularExpression &regex,
250                                         DIEArray &offsets) {
251   m_fallback.GetFunctions(regex, offsets);
252 
253   for (const DebugNames::NameIndex &ni: *m_debug_names_up) {
254     for (DebugNames::NameTableEntry nte: ni) {
255       if (!regex.Execute(nte.getString()))
256         continue;
257 
258       uint32_t entry_offset = nte.getEntryOffset();
259       llvm::Expected<DebugNames::Entry> entry_or = ni.getEntry(&entry_offset);
260       for (; entry_or; entry_or = ni.getEntry(&entry_offset)) {
261         Tag tag = entry_or->tag();
262         if (tag != DW_TAG_subprogram && tag != DW_TAG_inlined_subroutine)
263           continue;
264 
265         Append(*entry_or, offsets);
266       }
267       MaybeLogLookupError(entry_or.takeError(), ni, nte.getString());
268     }
269   }
270 }
271 
272 void DebugNamesDWARFIndex::Dump(Stream &s) {
273   m_fallback.Dump(s);
274 
275   std::string data;
276   llvm::raw_string_ostream os(data);
277   m_debug_names_up->dump(os);
278   s.PutCString(os.str());
279 }
280