1 //===-- ManualDWARFIndex.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/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" 20 21 using namespace lldb_private; 22 using namespace lldb; 23 24 void ManualDWARFIndex::Index() { 25 if (!m_debug_info) 26 return; 27 28 DWARFDebugInfo &debug_info = *m_debug_info; 29 m_debug_info = nullptr; 30 31 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 32 Timer scoped_timer(func_cat, "%p", static_cast<void *>(&debug_info)); 33 34 std::vector<DWARFUnit *> units_to_index; 35 units_to_index.reserve(debug_info.GetNumCompileUnits()); 36 for (size_t U = 0; U < debug_info.GetNumCompileUnits(); ++U) { 37 DWARFUnit *unit = debug_info.GetCompileUnitAtIndex(U); 38 if (unit && m_units_to_avoid.count(unit->GetOffset()) == 0) 39 units_to_index.push_back(unit); 40 } 41 if (units_to_index.empty()) 42 return; 43 44 std::vector<IndexSet> sets(units_to_index.size()); 45 46 // Keep memory down by clearing DIEs for any compile units if indexing 47 // caused us to load the compile 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]); 52 }; 53 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(); 56 }; 57 58 // Create a task runner that extracts dies for each DWARF compile unit in a 59 // separate thread 60 // First figure out which compile 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 compile units have been indexed in case a DIE in one 65 // compile unit refers to another and the indexes accesses those DIEs. 66 TaskMapOverInt(0, units_to_index.size(), extract_fn); 67 68 // Now create a task runner that can index each DWARF compile unit in a 69 // separate thread so we can index quickly. 70 71 TaskMapOverInt(0, units_to_index.size(), parser_fn); 72 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); 77 result.Finalize(); 78 }; 79 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); }); 88 } 89 90 void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, IndexSet &set) { 91 assert(!unit.GetSymbolFileDWARF()->GetBaseCompileUnit() && 92 "DWARFUnit associated with .dwo or .dwp should not be indexed directly"); 93 94 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS); 95 96 if (log) { 97 m_module.LogMessage( 98 log, "ManualDWARFIndex::IndexUnit for compile unit at .debug_info[0x%8.8x]", 99 unit.GetOffset()); 100 } 101 102 const LanguageType cu_language = unit.GetLanguageType(); 103 DWARFFormValue::FixedFormSizes fixed_form_sizes = unit.GetFixedFormSizes(); 104 105 IndexUnitImpl(unit, cu_language, fixed_form_sizes, unit.GetOffset(), set); 106 107 SymbolFileDWARFDwo *dwo_symbol_file = unit.GetDwoSymbolFile(); 108 if (dwo_symbol_file && dwo_symbol_file->GetCompileUnit()) { 109 IndexUnitImpl(*dwo_symbol_file->GetCompileUnit(), cu_language, 110 fixed_form_sizes, unit.GetOffset(), set); 111 } 112 } 113 114 void ManualDWARFIndex::IndexUnitImpl( 115 DWARFUnit &unit, const LanguageType cu_language, 116 const DWARFFormValue::FixedFormSizes &fixed_form_sizes, 117 const dw_offset_t cu_offset, IndexSet &set) { 118 for (const DWARFDebugInfoEntry &die : unit.dies()) { 119 const dw_tag_t tag = die.Tag(); 120 121 switch (tag) { 122 case DW_TAG_array_type: 123 case DW_TAG_base_type: 124 case DW_TAG_class_type: 125 case DW_TAG_constant: 126 case DW_TAG_enumeration_type: 127 case DW_TAG_inlined_subroutine: 128 case DW_TAG_namespace: 129 case DW_TAG_string_type: 130 case DW_TAG_structure_type: 131 case DW_TAG_subprogram: 132 case DW_TAG_subroutine_type: 133 case DW_TAG_typedef: 134 case DW_TAG_union_type: 135 case DW_TAG_unspecified_type: 136 case DW_TAG_variable: 137 break; 138 139 default: 140 continue; 141 } 142 143 DWARFAttributes attributes; 144 const char *name = NULL; 145 const char *mangled_cstr = NULL; 146 bool is_declaration = false; 147 // bool is_artificial = false; 148 bool has_address = false; 149 bool has_location_or_const_value = false; 150 bool is_global_or_static_variable = false; 151 152 DWARFFormValue specification_die_form; 153 const size_t num_attributes = 154 die.GetAttributes(&unit, fixed_form_sizes, attributes); 155 if (num_attributes > 0) { 156 for (uint32_t i = 0; i < num_attributes; ++i) { 157 dw_attr_t attr = attributes.AttributeAtIndex(i); 158 DWARFFormValue form_value; 159 switch (attr) { 160 case DW_AT_name: 161 if (attributes.ExtractFormValueAtIndex(i, form_value)) 162 name = form_value.AsCString(); 163 break; 164 165 case DW_AT_declaration: 166 if (attributes.ExtractFormValueAtIndex(i, form_value)) 167 is_declaration = form_value.Unsigned() != 0; 168 break; 169 170 // case DW_AT_artificial: 171 // if (attributes.ExtractFormValueAtIndex(i, 172 // form_value)) 173 // is_artificial = form_value.Unsigned() != 0; 174 // break; 175 176 case DW_AT_MIPS_linkage_name: 177 case DW_AT_linkage_name: 178 if (attributes.ExtractFormValueAtIndex(i, form_value)) 179 mangled_cstr = form_value.AsCString(); 180 break; 181 182 case DW_AT_low_pc: 183 case DW_AT_high_pc: 184 case DW_AT_ranges: 185 has_address = true; 186 break; 187 188 case DW_AT_entry_pc: 189 has_address = true; 190 break; 191 192 case DW_AT_location: 193 case DW_AT_const_value: 194 has_location_or_const_value = true; 195 if (tag == DW_TAG_variable) { 196 const DWARFDebugInfoEntry *parent_die = die.GetParent(); 197 while (parent_die != NULL) { 198 switch (parent_die->Tag()) { 199 case DW_TAG_subprogram: 200 case DW_TAG_lexical_block: 201 case DW_TAG_inlined_subroutine: 202 // Even if this is a function level static, we don't add it. We 203 // could theoretically add these if we wanted to by 204 // introspecting into the DW_AT_location and seeing if the 205 // location describes a hard coded address, but we don't want 206 // the performance penalty of that right now. 207 is_global_or_static_variable = false; 208 // if (attributes.ExtractFormValueAtIndex(dwarf, i, 209 // form_value)) { 210 // // If we have valid block data, then we have location 211 // // expression bytesthat are fixed (not a location list). 212 // const uint8_t *block_data = form_value.BlockData(); 213 // if (block_data) { 214 // uint32_t block_length = form_value.Unsigned(); 215 // if (block_length == 1 + 216 // attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) { 217 // if (block_data[0] == DW_OP_addr) 218 // add_die = true; 219 // } 220 // } 221 // } 222 parent_die = NULL; // Terminate the while loop. 223 break; 224 225 case DW_TAG_compile_unit: 226 case DW_TAG_partial_unit: 227 is_global_or_static_variable = true; 228 parent_die = NULL; // Terminate the while loop. 229 break; 230 231 default: 232 parent_die = 233 parent_die->GetParent(); // Keep going in the while loop. 234 break; 235 } 236 } 237 } 238 break; 239 240 case DW_AT_specification: 241 if (attributes.ExtractFormValueAtIndex(i, form_value)) 242 specification_die_form = form_value; 243 break; 244 } 245 } 246 } 247 248 switch (tag) { 249 case DW_TAG_inlined_subroutine: 250 case DW_TAG_subprogram: 251 if (has_address) { 252 if (name) { 253 ObjCLanguage::MethodName objc_method(name, true); 254 if (objc_method.IsValid(true)) { 255 ConstString objc_class_name_with_category( 256 objc_method.GetClassNameWithCategory()); 257 ConstString objc_selector_name(objc_method.GetSelector()); 258 ConstString objc_fullname_no_category_name( 259 objc_method.GetFullNameWithoutCategory(true)); 260 ConstString objc_class_name_no_category(objc_method.GetClassName()); 261 set.function_fullnames.Insert(ConstString(name), 262 DIERef(cu_offset, die.GetOffset())); 263 if (objc_class_name_with_category) 264 set.objc_class_selectors.Insert( 265 objc_class_name_with_category, 266 DIERef(cu_offset, die.GetOffset())); 267 if (objc_class_name_no_category && 268 objc_class_name_no_category != objc_class_name_with_category) 269 set.objc_class_selectors.Insert( 270 objc_class_name_no_category, 271 DIERef(cu_offset, die.GetOffset())); 272 if (objc_selector_name) 273 set.function_selectors.Insert(objc_selector_name, 274 DIERef(cu_offset, die.GetOffset())); 275 if (objc_fullname_no_category_name) 276 set.function_fullnames.Insert(objc_fullname_no_category_name, 277 DIERef(cu_offset, die.GetOffset())); 278 } 279 // If we have a mangled name, then the DW_AT_name attribute is 280 // usually the method name without the class or any parameters 281 bool is_method = DWARFDIE(&unit, &die).IsMethod(); 282 283 if (is_method) 284 set.function_methods.Insert(ConstString(name), 285 DIERef(cu_offset, die.GetOffset())); 286 else 287 set.function_basenames.Insert(ConstString(name), 288 DIERef(cu_offset, die.GetOffset())); 289 290 if (!is_method && !mangled_cstr && !objc_method.IsValid(true)) 291 set.function_fullnames.Insert(ConstString(name), 292 DIERef(cu_offset, die.GetOffset())); 293 } 294 if (mangled_cstr) { 295 // Make sure our mangled name isn't the same string table entry as 296 // our name. If it starts with '_', then it is ok, else compare the 297 // string to make sure it isn't the same and we don't end up with 298 // duplicate entries 299 if (name && name != mangled_cstr && 300 ((mangled_cstr[0] == '_') || 301 (::strcmp(name, mangled_cstr) != 0))) { 302 set.function_fullnames.Insert(ConstString(mangled_cstr), 303 DIERef(cu_offset, die.GetOffset())); 304 } 305 } 306 } 307 break; 308 309 case DW_TAG_array_type: 310 case DW_TAG_base_type: 311 case DW_TAG_class_type: 312 case DW_TAG_constant: 313 case DW_TAG_enumeration_type: 314 case DW_TAG_string_type: 315 case DW_TAG_structure_type: 316 case DW_TAG_subroutine_type: 317 case DW_TAG_typedef: 318 case DW_TAG_union_type: 319 case DW_TAG_unspecified_type: 320 if (name && !is_declaration) 321 set.types.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset())); 322 if (mangled_cstr && !is_declaration) 323 set.types.Insert(ConstString(mangled_cstr), 324 DIERef(cu_offset, die.GetOffset())); 325 break; 326 327 case DW_TAG_namespace: 328 if (name) 329 set.namespaces.Insert(ConstString(name), 330 DIERef(cu_offset, die.GetOffset())); 331 break; 332 333 case DW_TAG_variable: 334 if (name && has_location_or_const_value && is_global_or_static_variable) { 335 set.globals.Insert(ConstString(name), 336 DIERef(cu_offset, die.GetOffset())); 337 // Be sure to include variables by their mangled and demangled names if 338 // they have any since a variable can have a basename "i", a mangled 339 // named "_ZN12_GLOBAL__N_11iE" and a demangled mangled name 340 // "(anonymous namespace)::i"... 341 342 // Make sure our mangled name isn't the same string table entry as our 343 // name. If it starts with '_', then it is ok, else compare the string 344 // to make sure it isn't the same and we don't end up with duplicate 345 // entries 346 if (mangled_cstr && name != mangled_cstr && 347 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) { 348 set.globals.Insert(ConstString(mangled_cstr), 349 DIERef(cu_offset, die.GetOffset())); 350 } 351 } 352 break; 353 354 default: 355 continue; 356 } 357 } 358 } 359 360 void ManualDWARFIndex::GetGlobalVariables(ConstString basename, DIEArray &offsets) { 361 Index(); 362 m_set.globals.Find(basename, offsets); 363 } 364 365 void ManualDWARFIndex::GetGlobalVariables(const RegularExpression ®ex, 366 DIEArray &offsets) { 367 Index(); 368 m_set.globals.Find(regex, offsets); 369 } 370 371 void ManualDWARFIndex::GetGlobalVariables(const DWARFUnit &cu, 372 DIEArray &offsets) { 373 Index(); 374 m_set.globals.FindAllEntriesForCompileUnit(cu.GetOffset(), offsets); 375 } 376 377 void ManualDWARFIndex::GetObjCMethods(ConstString class_name, 378 DIEArray &offsets) { 379 Index(); 380 m_set.objc_class_selectors.Find(class_name, offsets); 381 } 382 383 void ManualDWARFIndex::GetCompleteObjCClass(ConstString class_name, 384 bool must_be_implementation, 385 DIEArray &offsets) { 386 Index(); 387 m_set.types.Find(class_name, offsets); 388 } 389 390 void ManualDWARFIndex::GetTypes(ConstString name, DIEArray &offsets) { 391 Index(); 392 m_set.types.Find(name, offsets); 393 } 394 395 void ManualDWARFIndex::GetTypes(const DWARFDeclContext &context, 396 DIEArray &offsets) { 397 Index(); 398 m_set.types.Find(ConstString(context[0].name), offsets); 399 } 400 401 void ManualDWARFIndex::GetNamespaces(ConstString name, DIEArray &offsets) { 402 Index(); 403 m_set.namespaces.Find(name, offsets); 404 } 405 406 void ManualDWARFIndex::GetFunctions(ConstString name, DWARFDebugInfo &info, 407 const CompilerDeclContext &parent_decl_ctx, 408 uint32_t name_type_mask, 409 std::vector<DWARFDIE> &dies) { 410 Index(); 411 412 if (name_type_mask & eFunctionNameTypeFull) { 413 DIEArray offsets; 414 m_set.function_basenames.Find(name, offsets); 415 m_set.function_methods.Find(name, offsets); 416 m_set.function_fullnames.Find(name, offsets); 417 for (const DIERef &die_ref: offsets) { 418 DWARFDIE die = info.GetDIE(die_ref); 419 if (!die) 420 continue; 421 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die)) 422 dies.push_back(die); 423 } 424 } 425 if (name_type_mask & eFunctionNameTypeBase) { 426 DIEArray offsets; 427 m_set.function_basenames.Find(name, offsets); 428 for (const DIERef &die_ref: offsets) { 429 DWARFDIE die = info.GetDIE(die_ref); 430 if (!die) 431 continue; 432 if (SymbolFileDWARF::DIEInDeclContext(&parent_decl_ctx, die)) 433 dies.push_back(die); 434 } 435 offsets.clear(); 436 } 437 438 if (name_type_mask & eFunctionNameTypeMethod && !parent_decl_ctx.IsValid()) { 439 DIEArray offsets; 440 m_set.function_methods.Find(name, offsets); 441 for (const DIERef &die_ref: offsets) { 442 if (DWARFDIE die = info.GetDIE(die_ref)) 443 dies.push_back(die); 444 } 445 } 446 447 if (name_type_mask & eFunctionNameTypeSelector && 448 !parent_decl_ctx.IsValid()) { 449 DIEArray offsets; 450 m_set.function_selectors.Find(name, offsets); 451 for (const DIERef &die_ref: offsets) { 452 if (DWARFDIE die = info.GetDIE(die_ref)) 453 dies.push_back(die); 454 } 455 } 456 } 457 458 void ManualDWARFIndex::GetFunctions(const RegularExpression ®ex, 459 DIEArray &offsets) { 460 Index(); 461 462 m_set.function_basenames.Find(regex, offsets); 463 m_set.function_fullnames.Find(regex, offsets); 464 } 465 466 void ManualDWARFIndex::Dump(Stream &s) { 467 s.Format("Manual DWARF index for ({0}) '{1:F}':", 468 m_module.GetArchitecture().GetArchitectureName(), 469 m_module.GetObjectFile()->GetFileSpec()); 470 s.Printf("\nFunction basenames:\n"); 471 m_set.function_basenames.Dump(&s); 472 s.Printf("\nFunction fullnames:\n"); 473 m_set.function_fullnames.Dump(&s); 474 s.Printf("\nFunction methods:\n"); 475 m_set.function_methods.Dump(&s); 476 s.Printf("\nFunction selectors:\n"); 477 m_set.function_selectors.Dump(&s); 478 s.Printf("\nObjective-C class selectors:\n"); 479 m_set.objc_class_selectors.Dump(&s); 480 s.Printf("\nGlobals and statics:\n"); 481 m_set.globals.Dump(&s); 482 s.Printf("\nTypes:\n"); 483 m_set.types.Dump(&s); 484 s.Printf("\nNamespaces:\n"); 485 m_set.namespaces.Dump(&s); 486 } 487