1 //===-- ManualDWARFIndex.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 //===----------------------------------------------------------------------===//
9 #include "Plugins/SymbolFile/DWARF/ManualDWARFIndex.h"
10 #include "Plugins/Language/ObjC/ObjCLanguage.h"
11 #include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
12 #include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
13 #include "Plugins/SymbolFile/DWARF/LogChannelDWARF.h"
14 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Host/TaskPool.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/Timer.h"
21 using namespace lldb_private;
24 void ManualDWARFIndex::Index() {
28 DWARFDebugInfo &debug_info = *m_debug_info;
29 m_debug_info = nullptr;
31 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
32 Timer scoped_timer(func_cat, "%p", static_cast<void *>(&debug_info));
34 std::vector<DWARFUnit *> units_to_index;
35 units_to_index.reserve(debug_info.GetNumUnits());
36 for (size_t U = 0; U < debug_info.GetNumUnits(); ++U) {
37 DWARFUnit *unit = debug_info.GetUnitAtIndex(U);
38 if (unit && m_units_to_avoid.count(unit->GetOffset()) == 0)
39 units_to_index.push_back(unit);
41 if (units_to_index.empty())
44 std::vector<IndexSet> sets(units_to_index.size());
46 // Keep memory down by clearing DIEs for any units if indexing
47 // caused us to load the unit's DIEs.
48 std::vector<llvm::Optional<DWARFUnit::ScopedExtractDIEs>> clear_cu_dies(
49 units_to_index.size());
50 auto parser_fn = [&](size_t cu_idx) {
51 IndexUnit(*units_to_index[cu_idx], sets[cu_idx]);
54 auto extract_fn = [&units_to_index, &clear_cu_dies](size_t cu_idx) {
55 clear_cu_dies[cu_idx] = units_to_index[cu_idx]->ExtractDIEsScoped();
58 // Create a task runner that extracts dies for each DWARF unit in a
60 // First figure out which units didn't have their DIEs already
61 // parsed and remember this. If no DIEs were parsed prior to this index
62 // function call, we are going to want to clear the CU dies after we are
63 // done indexing to make sure we don't pull in all DWARF dies, but we need
64 // to wait until all units have been indexed in case a DIE in one
65 // unit refers to another and the indexes accesses those DIEs.
66 TaskMapOverInt(0, units_to_index.size(), extract_fn);
68 // Now create a task runner that can index each DWARF unit in a
69 // separate thread so we can index quickly.
71 TaskMapOverInt(0, units_to_index.size(), parser_fn);
73 auto finalize_fn = [this, &sets](NameToDIE(IndexSet::*index)) {
74 NameToDIE &result = m_set.*index;
75 for (auto &set : sets)
76 result.Append(set.*index);
80 TaskPool::RunTasks([&]() { finalize_fn(&IndexSet::function_basenames); },
81 [&]() { finalize_fn(&IndexSet::function_fullnames); },
82 [&]() { finalize_fn(&IndexSet::function_methods); },
83 [&]() { finalize_fn(&IndexSet::function_selectors); },
84 [&]() { finalize_fn(&IndexSet::objc_class_selectors); },
85 [&]() { finalize_fn(&IndexSet::globals); },
86 [&]() { finalize_fn(&IndexSet::types); },
87 [&]() { finalize_fn(&IndexSet::namespaces); });
90 void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, IndexSet &set) {
92 !unit.GetSymbolFileDWARF().GetBaseCompileUnit() &&
93 "DWARFUnit associated with .dwo or .dwp should not be indexed directly");
95 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS);
99 log, "ManualDWARFIndex::IndexUnit for unit at .debug_info[0x%8.8x]",
103 const LanguageType cu_language = unit.GetLanguageType();
105 IndexUnitImpl(unit, cu_language, set);
107 if (SymbolFileDWARFDwo *dwo_symbol_file = unit.GetDwoSymbolFile()) {
108 DWARFDebugInfo &dwo_info = *dwo_symbol_file->DebugInfo();
109 for (size_t i = 0; i < dwo_info.GetNumUnits(); ++i)
110 IndexUnitImpl(*dwo_info.GetUnitAtIndex(i), cu_language, set);
114 void ManualDWARFIndex::IndexUnitImpl(DWARFUnit &unit,
115 const LanguageType cu_language,
117 for (const DWARFDebugInfoEntry &die : unit.dies()) {
118 const dw_tag_t tag = die.Tag();
121 case DW_TAG_array_type:
122 case DW_TAG_base_type:
123 case DW_TAG_class_type:
124 case DW_TAG_constant:
125 case DW_TAG_enumeration_type:
126 case DW_TAG_inlined_subroutine:
127 case DW_TAG_namespace:
128 case DW_TAG_string_type:
129 case DW_TAG_structure_type:
130 case DW_TAG_subprogram:
131 case DW_TAG_subroutine_type:
133 case DW_TAG_union_type:
134 case DW_TAG_unspecified_type:
135 case DW_TAG_variable:
142 DWARFAttributes attributes;
143 const char *name = nullptr;
144 const char *mangled_cstr = nullptr;
145 bool is_declaration = false;
146 // bool is_artificial = false;
147 bool has_address = false;
148 bool has_location_or_const_value = false;
149 bool is_global_or_static_variable = false;
151 DWARFFormValue specification_die_form;
152 const size_t num_attributes = die.GetAttributes(&unit, attributes);
153 if (num_attributes > 0) {
154 for (uint32_t i = 0; i < num_attributes; ++i) {
155 dw_attr_t attr = attributes.AttributeAtIndex(i);
156 DWARFFormValue form_value;
159 if (attributes.ExtractFormValueAtIndex(i, form_value))
160 name = form_value.AsCString();
163 case DW_AT_declaration:
164 if (attributes.ExtractFormValueAtIndex(i, form_value))
165 is_declaration = form_value.Unsigned() != 0;
168 // case DW_AT_artificial:
169 // if (attributes.ExtractFormValueAtIndex(i,
171 // is_artificial = form_value.Unsigned() != 0;
174 case DW_AT_MIPS_linkage_name:
175 case DW_AT_linkage_name:
176 if (attributes.ExtractFormValueAtIndex(i, form_value))
177 mangled_cstr = form_value.AsCString();
191 case DW_AT_const_value:
192 has_location_or_const_value = true;
193 if (tag == DW_TAG_variable) {
194 const DWARFDebugInfoEntry *parent_die = die.GetParent();
195 while (parent_die != nullptr) {
196 switch (parent_die->Tag()) {
197 case DW_TAG_subprogram:
198 case DW_TAG_lexical_block:
199 case DW_TAG_inlined_subroutine:
200 // Even if this is a function level static, we don't add it. We
201 // could theoretically add these if we wanted to by
202 // introspecting into the DW_AT_location and seeing if the
203 // location describes a hard coded address, but we don't want
204 // the performance penalty of that right now.
205 is_global_or_static_variable = false;
206 // if (attributes.ExtractFormValueAtIndex(dwarf, i,
208 // // If we have valid block data, then we have location
209 // // expression bytesthat are fixed (not a location list).
210 // const uint8_t *block_data = form_value.BlockData();
212 // uint32_t block_length = form_value.Unsigned();
213 // if (block_length == 1 +
214 // attributes.UnitAtIndex(i)->GetAddressByteSize()) {
215 // if (block_data[0] == DW_OP_addr)
220 parent_die = nullptr; // Terminate the while loop.
223 case DW_TAG_compile_unit:
224 case DW_TAG_partial_unit:
225 is_global_or_static_variable = true;
226 parent_die = nullptr; // Terminate the while loop.
231 parent_die->GetParent(); // Keep going in the while loop.
238 case DW_AT_specification:
239 if (attributes.ExtractFormValueAtIndex(i, form_value))
240 specification_die_form = form_value;
246 DIERef ref = *DWARFDIE(&unit, &die).GetDIERef();
248 case DW_TAG_inlined_subroutine:
249 case DW_TAG_subprogram:
252 bool is_objc_method = false;
253 if (cu_language == eLanguageTypeObjC ||
254 cu_language == eLanguageTypeObjC_plus_plus) {
255 ObjCLanguage::MethodName objc_method(name, true);
256 if (objc_method.IsValid(true)) {
257 is_objc_method = true;
258 ConstString class_name_with_category(
259 objc_method.GetClassNameWithCategory());
260 ConstString objc_selector_name(objc_method.GetSelector());
261 ConstString objc_fullname_no_category_name(
262 objc_method.GetFullNameWithoutCategory(true));
263 ConstString class_name_no_category(objc_method.GetClassName());
264 set.function_fullnames.Insert(ConstString(name), ref);
265 if (class_name_with_category)
266 set.objc_class_selectors.Insert(class_name_with_category, ref);
267 if (class_name_no_category &&
268 class_name_no_category != class_name_with_category)
269 set.objc_class_selectors.Insert(class_name_no_category, ref);
270 if (objc_selector_name)
271 set.function_selectors.Insert(objc_selector_name, ref);
272 if (objc_fullname_no_category_name)
273 set.function_fullnames.Insert(objc_fullname_no_category_name,
277 // If we have a mangled name, then the DW_AT_name attribute is
278 // usually the method name without the class or any parameters
279 bool is_method = DWARFDIE(&unit, &die).IsMethod();
282 set.function_methods.Insert(ConstString(name), ref);
284 set.function_basenames.Insert(ConstString(name), ref);
286 if (!is_method && !mangled_cstr && !is_objc_method)
287 set.function_fullnames.Insert(ConstString(name), ref);
290 // Make sure our mangled name isn't the same string table entry as
291 // our name. If it starts with '_', then it is ok, else compare the
292 // string to make sure it isn't the same and we don't end up with
294 if (name && name != mangled_cstr &&
295 ((mangled_cstr[0] == '_') ||
296 (::strcmp(name, mangled_cstr) != 0))) {
297 set.function_fullnames.Insert(ConstString(mangled_cstr), ref);
303 case DW_TAG_array_type:
304 case DW_TAG_base_type:
305 case DW_TAG_class_type:
306 case DW_TAG_constant:
307 case DW_TAG_enumeration_type:
308 case DW_TAG_string_type:
309 case DW_TAG_structure_type:
310 case DW_TAG_subroutine_type:
312 case DW_TAG_union_type:
313 case DW_TAG_unspecified_type:
314 if (name && !is_declaration)
315 set.types.Insert(ConstString(name), ref);
316 if (mangled_cstr && !is_declaration)
317 set.types.Insert(ConstString(mangled_cstr), ref);
320 case DW_TAG_namespace:
322 set.namespaces.Insert(ConstString(name), ref);
325 case DW_TAG_variable:
326 if (name && has_location_or_const_value && is_global_or_static_variable) {
327 set.globals.Insert(ConstString(name), ref);
328 // Be sure to include variables by their mangled and demangled names if
329 // they have any since a variable can have a basename "i", a mangled
330 // named "_ZN12_GLOBAL__N_11iE" and a demangled mangled name
331 // "(anonymous namespace)::i"...
333 // Make sure our mangled name isn't the same string table entry as our
334 // name. If it starts with '_', then it is ok, else compare the string
335 // to make sure it isn't the same and we don't end up with duplicate
337 if (mangled_cstr && name != mangled_cstr &&
338 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) {
339 set.globals.Insert(ConstString(mangled_cstr), ref);
350 void ManualDWARFIndex::GetGlobalVariables(ConstString basename, DIEArray &offsets) {
352 m_set.globals.Find(basename, offsets);
355 void ManualDWARFIndex::GetGlobalVariables(const RegularExpression ®ex,
358 m_set.globals.Find(regex, offsets);
361 void ManualDWARFIndex::GetGlobalVariables(const DWARFUnit &unit,
364 m_set.globals.FindAllEntriesForUnit(unit, offsets);
367 void ManualDWARFIndex::GetObjCMethods(ConstString class_name,
370 m_set.objc_class_selectors.Find(class_name, offsets);
373 void ManualDWARFIndex::GetCompleteObjCClass(ConstString class_name,
374 bool must_be_implementation,
377 m_set.types.Find(class_name, offsets);
380 void ManualDWARFIndex::GetTypes(ConstString name, DIEArray &offsets) {
382 m_set.types.Find(name, offsets);
385 void ManualDWARFIndex::GetTypes(const DWARFDeclContext &context,
388 m_set.types.Find(ConstString(context[0].name), offsets);
391 void ManualDWARFIndex::GetNamespaces(ConstString name, DIEArray &offsets) {
393 m_set.namespaces.Find(name, offsets);
396 void ManualDWARFIndex::GetFunctions(ConstString name, SymbolFileDWARF &dwarf,
397 const CompilerDeclContext &parent_decl_ctx,
398 uint32_t name_type_mask,
399 std::vector<DWARFDIE> &dies) {
402 if (name_type_mask & eFunctionNameTypeFull) {
404 m_set.function_basenames.Find(name, offsets);
405 m_set.function_methods.Find(name, offsets);
406 m_set.function_fullnames.Find(name, offsets);
407 for (const DIERef &die_ref: offsets) {
408 DWARFDIE die = dwarf.GetDIE(die_ref);
411 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die))
415 if (name_type_mask & eFunctionNameTypeBase) {
417 m_set.function_basenames.Find(name, offsets);
418 for (const DIERef &die_ref: offsets) {
419 DWARFDIE die = dwarf.GetDIE(die_ref);
422 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die))
428 if (name_type_mask & eFunctionNameTypeMethod && !parent_decl_ctx.IsValid()) {
430 m_set.function_methods.Find(name, offsets);
431 for (const DIERef &die_ref: offsets) {
432 if (DWARFDIE die = dwarf.GetDIE(die_ref))
437 if (name_type_mask & eFunctionNameTypeSelector &&
438 !parent_decl_ctx.IsValid()) {
440 m_set.function_selectors.Find(name, offsets);
441 for (const DIERef &die_ref: offsets) {
442 if (DWARFDIE die = dwarf.GetDIE(die_ref))
448 void ManualDWARFIndex::GetFunctions(const RegularExpression ®ex,
452 m_set.function_basenames.Find(regex, offsets);
453 m_set.function_fullnames.Find(regex, offsets);
456 void ManualDWARFIndex::Dump(Stream &s) {
457 s.Format("Manual DWARF index for ({0}) '{1:F}':",
458 m_module.GetArchitecture().GetArchitectureName(),
459 m_module.GetObjectFile()->GetFileSpec());
460 s.Printf("\nFunction basenames:\n");
461 m_set.function_basenames.Dump(&s);
462 s.Printf("\nFunction fullnames:\n");
463 m_set.function_fullnames.Dump(&s);
464 s.Printf("\nFunction methods:\n");
465 m_set.function_methods.Dump(&s);
466 s.Printf("\nFunction selectors:\n");
467 m_set.function_selectors.Dump(&s);
468 s.Printf("\nObjective-C class selectors:\n");
469 m_set.objc_class_selectors.Dump(&s);
470 s.Printf("\nGlobals and statics:\n");
471 m_set.globals.Dump(&s);
472 s.Printf("\nTypes:\n");
473 m_set.types.Dump(&s);
474 s.Printf("\nNamespaces:\n");
475 m_set.namespaces.Dump(&s);