1 //===-- ManualDWARFIndex.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Plugins/SymbolFile/DWARF/ManualDWARFIndex.h"
11 #include "Plugins/Language/ObjC/ObjCLanguage.h"
12 #include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
13 #include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
14 #include "Plugins/SymbolFile/DWARF/LogChannelDWARF.h"
15 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Host/TaskPool.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Utility/Stream.h"
20 #include "lldb/Utility/Timer.h"
22 using namespace lldb_private;
25 void ManualDWARFIndex::Index() {
29 DWARFDebugInfo &debug_info = *m_debug_info;
30 m_debug_info = nullptr;
32 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
33 Timer scoped_timer(func_cat, "%p", static_cast<void *>(&debug_info));
35 std::vector<DWARFUnit *> units_to_index;
36 units_to_index.reserve(debug_info.GetNumCompileUnits());
37 for (size_t U = 0; U < debug_info.GetNumCompileUnits(); ++U) {
38 DWARFUnit *unit = debug_info.GetCompileUnitAtIndex(U);
39 if (unit && m_units_to_avoid.count(unit->GetOffset()) == 0)
40 units_to_index.push_back(unit);
42 if (units_to_index.empty())
45 std::vector<IndexSet> sets(units_to_index.size());
47 //----------------------------------------------------------------------
48 // Keep memory down by clearing DIEs for any compile units if indexing
49 // caused us to load the compile unit's DIEs.
50 //----------------------------------------------------------------------
51 std::vector<llvm::Optional<DWARFUnit::ScopedExtractDIEs>> clear_cu_dies(
52 units_to_index.size());
53 auto parser_fn = [&](size_t cu_idx) {
54 IndexUnit(*units_to_index[cu_idx], sets[cu_idx]);
57 auto extract_fn = [&units_to_index, &clear_cu_dies](size_t cu_idx) {
58 clear_cu_dies[cu_idx] = units_to_index[cu_idx]->ExtractDIEsScoped();
61 // Create a task runner that extracts dies for each DWARF compile unit in a
63 //----------------------------------------------------------------------
64 // First figure out which compile units didn't have their DIEs already
65 // parsed and remember this. If no DIEs were parsed prior to this index
66 // function call, we are going to want to clear the CU dies after we are
67 // done indexing to make sure we don't pull in all DWARF dies, but we need
68 // to wait until all compile units have been indexed in case a DIE in one
69 // compile unit refers to another and the indexes accesses those DIEs.
70 //----------------------------------------------------------------------
71 TaskMapOverInt(0, units_to_index.size(), extract_fn);
73 // Now create a task runner that can index each DWARF compile unit in a
74 // separate thread so we can index quickly.
76 TaskMapOverInt(0, units_to_index.size(), parser_fn);
78 auto finalize_fn = [this, &sets](NameToDIE(IndexSet::*index)) {
79 NameToDIE &result = m_set.*index;
80 for (auto &set : sets)
81 result.Append(set.*index);
85 TaskPool::RunTasks([&]() { finalize_fn(&IndexSet::function_basenames); },
86 [&]() { finalize_fn(&IndexSet::function_fullnames); },
87 [&]() { finalize_fn(&IndexSet::function_methods); },
88 [&]() { finalize_fn(&IndexSet::function_selectors); },
89 [&]() { finalize_fn(&IndexSet::objc_class_selectors); },
90 [&]() { finalize_fn(&IndexSet::globals); },
91 [&]() { finalize_fn(&IndexSet::types); },
92 [&]() { finalize_fn(&IndexSet::namespaces); });
95 void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, IndexSet &set) {
96 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS);
100 log, "ManualDWARFIndex::IndexUnit for compile unit at .debug_info[0x%8.8x]",
104 const LanguageType cu_language = unit.GetLanguageType();
105 DWARFFormValue::FixedFormSizes fixed_form_sizes = unit.GetFixedFormSizes();
107 IndexUnitImpl(unit, cu_language, fixed_form_sizes, unit.GetOffset(), set);
109 SymbolFileDWARFDwo *dwo_symbol_file = unit.GetDwoSymbolFile();
110 if (dwo_symbol_file && dwo_symbol_file->GetCompileUnit()) {
111 IndexUnitImpl(*dwo_symbol_file->GetCompileUnit(), cu_language,
112 fixed_form_sizes, unit.GetOffset(), set);
116 void ManualDWARFIndex::IndexUnitImpl(
117 DWARFUnit &unit, const LanguageType cu_language,
118 const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
119 const dw_offset_t cu_offset, IndexSet &set) {
120 for (const DWARFDebugInfoEntry &die : unit.dies()) {
121 const dw_tag_t tag = die.Tag();
124 case DW_TAG_array_type:
125 case DW_TAG_base_type:
126 case DW_TAG_class_type:
127 case DW_TAG_constant:
128 case DW_TAG_enumeration_type:
129 case DW_TAG_inlined_subroutine:
130 case DW_TAG_namespace:
131 case DW_TAG_string_type:
132 case DW_TAG_structure_type:
133 case DW_TAG_subprogram:
134 case DW_TAG_subroutine_type:
136 case DW_TAG_union_type:
137 case DW_TAG_unspecified_type:
138 case DW_TAG_variable:
145 DWARFAttributes attributes;
146 const char *name = NULL;
147 const char *mangled_cstr = NULL;
148 bool is_declaration = false;
149 // bool is_artificial = false;
150 bool has_address = false;
151 bool has_location_or_const_value = false;
152 bool is_global_or_static_variable = false;
154 DWARFFormValue specification_die_form;
155 const size_t num_attributes =
156 die.GetAttributes(&unit, fixed_form_sizes, attributes);
157 if (num_attributes > 0) {
158 for (uint32_t i = 0; i < num_attributes; ++i) {
159 dw_attr_t attr = attributes.AttributeAtIndex(i);
160 DWARFFormValue form_value;
163 if (attributes.ExtractFormValueAtIndex(i, form_value))
164 name = form_value.AsCString();
167 case DW_AT_declaration:
168 if (attributes.ExtractFormValueAtIndex(i, form_value))
169 is_declaration = form_value.Unsigned() != 0;
172 // case DW_AT_artificial:
173 // if (attributes.ExtractFormValueAtIndex(i,
175 // is_artificial = form_value.Unsigned() != 0;
178 case DW_AT_MIPS_linkage_name:
179 case DW_AT_linkage_name:
180 if (attributes.ExtractFormValueAtIndex(i, form_value))
181 mangled_cstr = form_value.AsCString();
195 case DW_AT_const_value:
196 has_location_or_const_value = true;
197 if (tag == DW_TAG_variable) {
198 const DWARFDebugInfoEntry *parent_die = die.GetParent();
199 while (parent_die != NULL) {
200 switch (parent_die->Tag()) {
201 case DW_TAG_subprogram:
202 case DW_TAG_lexical_block:
203 case DW_TAG_inlined_subroutine:
204 // Even if this is a function level static, we don't add it. We
205 // could theoretically add these if we wanted to by
206 // introspecting into the DW_AT_location and seeing if the
207 // location describes a hard coded address, but we don't want
208 // the performance penalty of that right now.
209 is_global_or_static_variable = false;
210 // if (attributes.ExtractFormValueAtIndex(dwarf, i,
212 // // If we have valid block data, then we have location
213 // // expression bytesthat are fixed (not a location list).
214 // const uint8_t *block_data = form_value.BlockData();
216 // uint32_t block_length = form_value.Unsigned();
217 // if (block_length == 1 +
218 // attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) {
219 // if (block_data[0] == DW_OP_addr)
224 parent_die = NULL; // Terminate the while loop.
227 case DW_TAG_compile_unit:
228 case DW_TAG_partial_unit:
229 is_global_or_static_variable = true;
230 parent_die = NULL; // Terminate the while loop.
235 parent_die->GetParent(); // Keep going in the while loop.
242 case DW_AT_specification:
243 if (attributes.ExtractFormValueAtIndex(i, form_value))
244 specification_die_form = form_value;
251 case DW_TAG_inlined_subroutine:
252 case DW_TAG_subprogram:
255 ObjCLanguage::MethodName objc_method(name, true);
256 if (objc_method.IsValid(true)) {
257 ConstString objc_class_name_with_category(
258 objc_method.GetClassNameWithCategory());
259 ConstString objc_selector_name(objc_method.GetSelector());
260 ConstString objc_fullname_no_category_name(
261 objc_method.GetFullNameWithoutCategory(true));
262 ConstString objc_class_name_no_category(objc_method.GetClassName());
263 set.function_fullnames.Insert(ConstString(name),
264 DIERef(cu_offset, die.GetOffset()));
265 if (objc_class_name_with_category)
266 set.objc_class_selectors.Insert(
267 objc_class_name_with_category,
268 DIERef(cu_offset, die.GetOffset()));
269 if (objc_class_name_no_category &&
270 objc_class_name_no_category != objc_class_name_with_category)
271 set.objc_class_selectors.Insert(
272 objc_class_name_no_category,
273 DIERef(cu_offset, die.GetOffset()));
274 if (objc_selector_name)
275 set.function_selectors.Insert(objc_selector_name,
276 DIERef(cu_offset, die.GetOffset()));
277 if (objc_fullname_no_category_name)
278 set.function_fullnames.Insert(objc_fullname_no_category_name,
279 DIERef(cu_offset, die.GetOffset()));
281 // If we have a mangled name, then the DW_AT_name attribute is
282 // usually the method name without the class or any parameters
283 bool is_method = DWARFDIE(&unit, &die).IsMethod();
286 set.function_methods.Insert(ConstString(name),
287 DIERef(cu_offset, die.GetOffset()));
289 set.function_basenames.Insert(ConstString(name),
290 DIERef(cu_offset, die.GetOffset()));
292 if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
293 set.function_fullnames.Insert(ConstString(name),
294 DIERef(cu_offset, die.GetOffset()));
297 // Make sure our mangled name isn't the same string table entry as
298 // our name. If it starts with '_', then it is ok, else compare the
299 // string to make sure it isn't the same and we don't end up with
301 if (name && name != mangled_cstr &&
302 ((mangled_cstr[0] == '_') ||
303 (::strcmp(name, mangled_cstr) != 0))) {
304 set.function_fullnames.Insert(ConstString(mangled_cstr),
305 DIERef(cu_offset, die.GetOffset()));
311 case DW_TAG_array_type:
312 case DW_TAG_base_type:
313 case DW_TAG_class_type:
314 case DW_TAG_constant:
315 case DW_TAG_enumeration_type:
316 case DW_TAG_string_type:
317 case DW_TAG_structure_type:
318 case DW_TAG_subroutine_type:
320 case DW_TAG_union_type:
321 case DW_TAG_unspecified_type:
322 if (name && !is_declaration)
323 set.types.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset()));
324 if (mangled_cstr && !is_declaration)
325 set.types.Insert(ConstString(mangled_cstr),
326 DIERef(cu_offset, die.GetOffset()));
329 case DW_TAG_namespace:
331 set.namespaces.Insert(ConstString(name),
332 DIERef(cu_offset, die.GetOffset()));
335 case DW_TAG_variable:
336 if (name && has_location_or_const_value && is_global_or_static_variable) {
337 set.globals.Insert(ConstString(name),
338 DIERef(cu_offset, die.GetOffset()));
339 // Be sure to include variables by their mangled and demangled names if
340 // they have any since a variable can have a basename "i", a mangled
341 // named "_ZN12_GLOBAL__N_11iE" and a demangled mangled name
342 // "(anonymous namespace)::i"...
344 // Make sure our mangled name isn't the same string table entry as our
345 // name. If it starts with '_', then it is ok, else compare the string
346 // to make sure it isn't the same and we don't end up with duplicate
348 if (mangled_cstr && name != mangled_cstr &&
349 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) {
350 set.globals.Insert(ConstString(mangled_cstr),
351 DIERef(cu_offset, die.GetOffset()));
362 void ManualDWARFIndex::GetGlobalVariables(ConstString basename, DIEArray &offsets) {
364 m_set.globals.Find(basename, offsets);
367 void ManualDWARFIndex::GetGlobalVariables(const RegularExpression ®ex,
370 m_set.globals.Find(regex, offsets);
373 void ManualDWARFIndex::GetGlobalVariables(const DWARFUnit &cu,
376 m_set.globals.FindAllEntriesForCompileUnit(cu.GetOffset(), offsets);
379 void ManualDWARFIndex::GetObjCMethods(ConstString class_name,
382 m_set.objc_class_selectors.Find(class_name, offsets);
385 void ManualDWARFIndex::GetCompleteObjCClass(ConstString class_name,
386 bool must_be_implementation,
389 m_set.types.Find(class_name, offsets);
392 void ManualDWARFIndex::GetTypes(ConstString name, DIEArray &offsets) {
394 m_set.types.Find(name, offsets);
397 void ManualDWARFIndex::GetTypes(const DWARFDeclContext &context,
400 m_set.types.Find(ConstString(context[0].name), offsets);
403 void ManualDWARFIndex::GetNamespaces(ConstString name, DIEArray &offsets) {
405 m_set.namespaces.Find(name, offsets);
408 void ManualDWARFIndex::GetFunctions(ConstString name, DWARFDebugInfo &info,
409 const CompilerDeclContext &parent_decl_ctx,
410 uint32_t name_type_mask,
411 std::vector<DWARFDIE> &dies) {
414 if (name_type_mask & eFunctionNameTypeFull) {
416 m_set.function_basenames.Find(name, offsets);
417 m_set.function_methods.Find(name, offsets);
418 m_set.function_fullnames.Find(name, offsets);
419 for (const DIERef &die_ref: offsets) {
420 DWARFDIE die = info.GetDIE(die_ref);
423 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die))
427 if (name_type_mask & eFunctionNameTypeBase) {
429 m_set.function_basenames.Find(name, offsets);
430 for (const DIERef &die_ref: offsets) {
431 DWARFDIE die = info.GetDIE(die_ref);
434 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die))
440 if (name_type_mask & eFunctionNameTypeMethod && !parent_decl_ctx.IsValid()) {
442 m_set.function_methods.Find(name, offsets);
443 for (const DIERef &die_ref: offsets) {
444 if (DWARFDIE die = info.GetDIE(die_ref))
449 if (name_type_mask & eFunctionNameTypeSelector &&
450 !parent_decl_ctx.IsValid()) {
452 m_set.function_selectors.Find(name, offsets);
453 for (const DIERef &die_ref: offsets) {
454 if (DWARFDIE die = info.GetDIE(die_ref))
460 void ManualDWARFIndex::GetFunctions(const RegularExpression ®ex,
464 m_set.function_basenames.Find(regex, offsets);
465 m_set.function_fullnames.Find(regex, offsets);
468 void ManualDWARFIndex::Dump(Stream &s) {
469 s.Format("Manual DWARF index for ({0}) '{1:F}':",
470 m_module.GetArchitecture().GetArchitectureName(),
471 m_module.GetObjectFile()->GetFileSpec());
472 s.Printf("\nFunction basenames:\n");
473 m_set.function_basenames.Dump(&s);
474 s.Printf("\nFunction fullnames:\n");
475 m_set.function_fullnames.Dump(&s);
476 s.Printf("\nFunction methods:\n");
477 m_set.function_methods.Dump(&s);
478 s.Printf("\nFunction selectors:\n");
479 m_set.function_selectors.Dump(&s);
480 s.Printf("\nObjective-C class selectors:\n");
481 m_set.objc_class_selectors.Dump(&s);
482 s.Printf("\nGlobals and statics:\n");
483 m_set.globals.Dump(&s);
484 s.Printf("\nTypes:\n");
485 m_set.types.Dump(&s);
486 s.Printf("\nNamespaces:\n");
487 m_set.namespaces.Dump(&s);