1 //===-- ManualDWARFIndex.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 "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/DataFileCache.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/Progress.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Utility/DataEncoder.h" 20 #include "lldb/Utility/DataExtractor.h" 21 #include "lldb/Utility/Stream.h" 22 #include "lldb/Utility/Timer.h" 23 #include "llvm/Support/FormatVariadic.h" 24 #include "llvm/Support/ThreadPool.h" 25 26 using namespace lldb_private; 27 using namespace lldb; 28 using namespace lldb_private::dwarf; 29 30 void ManualDWARFIndex::Index() { 31 if (m_indexed) 32 return; 33 m_indexed = true; 34 35 ElapsedTime elapsed(m_index_time); 36 LLDB_SCOPED_TIMERF("%p", static_cast<void *>(m_dwarf)); 37 if (LoadFromCache()) { 38 m_dwarf->SetDebugInfoIndexWasLoadedFromCache(); 39 return; 40 } 41 42 DWARFDebugInfo &main_info = m_dwarf->DebugInfo(); 43 SymbolFileDWARFDwo *dwp_dwarf = m_dwarf->GetDwpSymbolFile().get(); 44 DWARFDebugInfo *dwp_info = dwp_dwarf ? &dwp_dwarf->DebugInfo() : nullptr; 45 46 std::vector<DWARFUnit *> units_to_index; 47 units_to_index.reserve(main_info.GetNumUnits() + 48 (dwp_info ? dwp_info->GetNumUnits() : 0)); 49 50 // Process all units in the main file, as well as any type units in the dwp 51 // file. Type units in dwo files are handled when we reach the dwo file in 52 // IndexUnit. 53 for (size_t U = 0; U < main_info.GetNumUnits(); ++U) { 54 DWARFUnit *unit = main_info.GetUnitAtIndex(U); 55 if (unit && m_units_to_avoid.count(unit->GetOffset()) == 0) 56 units_to_index.push_back(unit); 57 } 58 if (dwp_info && dwp_info->ContainsTypeUnits()) { 59 for (size_t U = 0; U < dwp_info->GetNumUnits(); ++U) { 60 if (auto *tu = llvm::dyn_cast<DWARFTypeUnit>(dwp_info->GetUnitAtIndex(U))) 61 units_to_index.push_back(tu); 62 } 63 } 64 65 if (units_to_index.empty()) 66 return; 67 68 StreamString module_desc; 69 m_module.GetDescription(module_desc.AsRawOstream(), 70 lldb::eDescriptionLevelBrief); 71 72 // Include 2 passes per unit to index for extracting DIEs from the unit and 73 // indexing the unit, and then 8 extra entries for finalizing each index set. 74 const uint64_t total_progress = units_to_index.size() * 2 + 8; 75 Progress progress( 76 llvm::formatv("Manually indexing DWARF for {0}", module_desc.GetData()), 77 total_progress); 78 79 std::vector<IndexSet> sets(units_to_index.size()); 80 81 // Keep memory down by clearing DIEs for any units if indexing 82 // caused us to load the unit's DIEs. 83 std::vector<llvm::Optional<DWARFUnit::ScopedExtractDIEs>> clear_cu_dies( 84 units_to_index.size()); 85 auto parser_fn = [&](size_t cu_idx) { 86 IndexUnit(*units_to_index[cu_idx], dwp_dwarf, sets[cu_idx]); 87 progress.Increment(); 88 }; 89 90 auto extract_fn = [&](size_t cu_idx) { 91 clear_cu_dies[cu_idx] = units_to_index[cu_idx]->ExtractDIEsScoped(); 92 progress.Increment(); 93 }; 94 95 // Share one thread pool across operations to avoid the overhead of 96 // recreating the threads. 97 llvm::ThreadPool pool(llvm::optimal_concurrency(units_to_index.size())); 98 99 // Create a task runner that extracts dies for each DWARF unit in a 100 // separate thread. 101 // First figure out which units didn't have their DIEs already 102 // parsed and remember this. If no DIEs were parsed prior to this index 103 // function call, we are going to want to clear the CU dies after we are 104 // done indexing to make sure we don't pull in all DWARF dies, but we need 105 // to wait until all units have been indexed in case a DIE in one 106 // unit refers to another and the indexes accesses those DIEs. 107 for (size_t i = 0; i < units_to_index.size(); ++i) 108 pool.async(extract_fn, i); 109 pool.wait(); 110 111 // Now create a task runner that can index each DWARF unit in a 112 // separate thread so we can index quickly. 113 for (size_t i = 0; i < units_to_index.size(); ++i) 114 pool.async(parser_fn, i); 115 pool.wait(); 116 117 auto finalize_fn = [this, &sets, &progress](NameToDIE(IndexSet::*index)) { 118 NameToDIE &result = m_set.*index; 119 for (auto &set : sets) 120 result.Append(set.*index); 121 result.Finalize(); 122 progress.Increment(); 123 }; 124 125 pool.async(finalize_fn, &IndexSet::function_basenames); 126 pool.async(finalize_fn, &IndexSet::function_fullnames); 127 pool.async(finalize_fn, &IndexSet::function_methods); 128 pool.async(finalize_fn, &IndexSet::function_selectors); 129 pool.async(finalize_fn, &IndexSet::objc_class_selectors); 130 pool.async(finalize_fn, &IndexSet::globals); 131 pool.async(finalize_fn, &IndexSet::types); 132 pool.async(finalize_fn, &IndexSet::namespaces); 133 pool.wait(); 134 135 SaveToCache(); 136 } 137 138 void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, SymbolFileDWARFDwo *dwp, 139 IndexSet &set) { 140 Log *log = GetLog(DWARFLog::Lookups); 141 142 if (log) { 143 m_module.LogMessage( 144 log, "ManualDWARFIndex::IndexUnit for unit at .debug_info[0x%8.8x]", 145 unit.GetOffset()); 146 } 147 148 const LanguageType cu_language = SymbolFileDWARF::GetLanguage(unit); 149 150 IndexUnitImpl(unit, cu_language, set); 151 152 if (SymbolFileDWARFDwo *dwo_symbol_file = unit.GetDwoSymbolFile()) { 153 // Type units in a dwp file are indexed separately, so we just need to 154 // process the split unit here. However, if the split unit is in a dwo file, 155 // then we need to process type units here. 156 if (dwo_symbol_file == dwp) { 157 IndexUnitImpl(unit.GetNonSkeletonUnit(), cu_language, set); 158 } else { 159 DWARFDebugInfo &dwo_info = dwo_symbol_file->DebugInfo(); 160 for (size_t i = 0; i < dwo_info.GetNumUnits(); ++i) 161 IndexUnitImpl(*dwo_info.GetUnitAtIndex(i), cu_language, set); 162 } 163 } 164 } 165 166 void ManualDWARFIndex::IndexUnitImpl(DWARFUnit &unit, 167 const LanguageType cu_language, 168 IndexSet &set) { 169 for (const DWARFDebugInfoEntry &die : unit.dies()) { 170 const dw_tag_t tag = die.Tag(); 171 172 switch (tag) { 173 case DW_TAG_array_type: 174 case DW_TAG_base_type: 175 case DW_TAG_class_type: 176 case DW_TAG_constant: 177 case DW_TAG_enumeration_type: 178 case DW_TAG_inlined_subroutine: 179 case DW_TAG_namespace: 180 case DW_TAG_string_type: 181 case DW_TAG_structure_type: 182 case DW_TAG_subprogram: 183 case DW_TAG_subroutine_type: 184 case DW_TAG_typedef: 185 case DW_TAG_union_type: 186 case DW_TAG_unspecified_type: 187 case DW_TAG_variable: 188 break; 189 190 default: 191 continue; 192 } 193 194 DWARFAttributes attributes; 195 const char *name = nullptr; 196 const char *mangled_cstr = nullptr; 197 bool is_declaration = false; 198 // bool is_artificial = false; 199 bool has_address = false; 200 bool has_location_or_const_value = false; 201 bool is_global_or_static_variable = false; 202 203 DWARFFormValue specification_die_form; 204 const size_t num_attributes = die.GetAttributes(&unit, attributes); 205 if (num_attributes > 0) { 206 for (uint32_t i = 0; i < num_attributes; ++i) { 207 dw_attr_t attr = attributes.AttributeAtIndex(i); 208 DWARFFormValue form_value; 209 switch (attr) { 210 case DW_AT_name: 211 if (attributes.ExtractFormValueAtIndex(i, form_value)) 212 name = form_value.AsCString(); 213 break; 214 215 case DW_AT_declaration: 216 if (attributes.ExtractFormValueAtIndex(i, form_value)) 217 is_declaration = form_value.Unsigned() != 0; 218 break; 219 220 case DW_AT_MIPS_linkage_name: 221 case DW_AT_linkage_name: 222 if (attributes.ExtractFormValueAtIndex(i, form_value)) 223 mangled_cstr = form_value.AsCString(); 224 break; 225 226 case DW_AT_low_pc: 227 case DW_AT_high_pc: 228 case DW_AT_ranges: 229 has_address = true; 230 break; 231 232 case DW_AT_entry_pc: 233 has_address = true; 234 break; 235 236 case DW_AT_location: 237 case DW_AT_const_value: 238 has_location_or_const_value = true; 239 is_global_or_static_variable = die.IsGlobalOrStaticScopeVariable(); 240 241 break; 242 243 case DW_AT_specification: 244 if (attributes.ExtractFormValueAtIndex(i, form_value)) 245 specification_die_form = form_value; 246 break; 247 } 248 } 249 } 250 251 DIERef ref = *DWARFDIE(&unit, &die).GetDIERef(); 252 switch (tag) { 253 case DW_TAG_inlined_subroutine: 254 case DW_TAG_subprogram: 255 if (has_address) { 256 if (name) { 257 bool is_objc_method = false; 258 if (cu_language == eLanguageTypeObjC || 259 cu_language == eLanguageTypeObjC_plus_plus) { 260 ObjCLanguage::MethodName objc_method(name, true); 261 if (objc_method.IsValid(true)) { 262 is_objc_method = true; 263 ConstString class_name_with_category( 264 objc_method.GetClassNameWithCategory()); 265 ConstString objc_selector_name(objc_method.GetSelector()); 266 ConstString objc_fullname_no_category_name( 267 objc_method.GetFullNameWithoutCategory(true)); 268 ConstString class_name_no_category(objc_method.GetClassName()); 269 set.function_fullnames.Insert(ConstString(name), ref); 270 if (class_name_with_category) 271 set.objc_class_selectors.Insert(class_name_with_category, ref); 272 if (class_name_no_category && 273 class_name_no_category != class_name_with_category) 274 set.objc_class_selectors.Insert(class_name_no_category, ref); 275 if (objc_selector_name) 276 set.function_selectors.Insert(objc_selector_name, ref); 277 if (objc_fullname_no_category_name) 278 set.function_fullnames.Insert(objc_fullname_no_category_name, 279 ref); 280 } 281 } 282 // If we have a mangled name, then the DW_AT_name attribute is 283 // usually the method name without the class or any parameters 284 bool is_method = DWARFDIE(&unit, &die).IsMethod(); 285 286 if (is_method) 287 set.function_methods.Insert(ConstString(name), ref); 288 else 289 set.function_basenames.Insert(ConstString(name), ref); 290 291 if (!is_method && !mangled_cstr && !is_objc_method) 292 set.function_fullnames.Insert(ConstString(name), ref); 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), ref); 303 } 304 } 305 } 306 break; 307 308 case DW_TAG_array_type: 309 case DW_TAG_base_type: 310 case DW_TAG_class_type: 311 case DW_TAG_constant: 312 case DW_TAG_enumeration_type: 313 case DW_TAG_string_type: 314 case DW_TAG_structure_type: 315 case DW_TAG_subroutine_type: 316 case DW_TAG_typedef: 317 case DW_TAG_union_type: 318 case DW_TAG_unspecified_type: 319 if (name && !is_declaration) 320 set.types.Insert(ConstString(name), ref); 321 if (mangled_cstr && !is_declaration) 322 set.types.Insert(ConstString(mangled_cstr), ref); 323 break; 324 325 case DW_TAG_namespace: 326 if (name) 327 set.namespaces.Insert(ConstString(name), ref); 328 break; 329 330 case DW_TAG_variable: 331 if (name && has_location_or_const_value && is_global_or_static_variable) { 332 set.globals.Insert(ConstString(name), ref); 333 // Be sure to include variables by their mangled and demangled names if 334 // they have any since a variable can have a basename "i", a mangled 335 // named "_ZN12_GLOBAL__N_11iE" and a demangled mangled name 336 // "(anonymous namespace)::i"... 337 338 // Make sure our mangled name isn't the same string table entry as our 339 // name. If it starts with '_', then it is ok, else compare the string 340 // to make sure it isn't the same and we don't end up with duplicate 341 // entries 342 if (mangled_cstr && name != mangled_cstr && 343 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) { 344 set.globals.Insert(ConstString(mangled_cstr), ref); 345 } 346 } 347 break; 348 349 default: 350 continue; 351 } 352 } 353 } 354 355 void ManualDWARFIndex::GetGlobalVariables( 356 ConstString basename, llvm::function_ref<bool(DWARFDIE die)> callback) { 357 Index(); 358 m_set.globals.Find(basename, 359 DIERefCallback(callback, basename.GetStringRef())); 360 } 361 362 void ManualDWARFIndex::GetGlobalVariables( 363 const RegularExpression ®ex, 364 llvm::function_ref<bool(DWARFDIE die)> callback) { 365 Index(); 366 m_set.globals.Find(regex, DIERefCallback(callback, regex.GetText())); 367 } 368 369 void ManualDWARFIndex::GetGlobalVariables( 370 DWARFUnit &unit, llvm::function_ref<bool(DWARFDIE die)> callback) { 371 lldbassert(!unit.GetSymbolFileDWARF().GetDwoNum()); 372 Index(); 373 m_set.globals.FindAllEntriesForUnit(unit, DIERefCallback(callback)); 374 } 375 376 void ManualDWARFIndex::GetObjCMethods( 377 ConstString class_name, llvm::function_ref<bool(DWARFDIE die)> callback) { 378 Index(); 379 m_set.objc_class_selectors.Find( 380 class_name, DIERefCallback(callback, class_name.GetStringRef())); 381 } 382 383 void ManualDWARFIndex::GetCompleteObjCClass( 384 ConstString class_name, bool must_be_implementation, 385 llvm::function_ref<bool(DWARFDIE die)> callback) { 386 Index(); 387 m_set.types.Find(class_name, 388 DIERefCallback(callback, class_name.GetStringRef())); 389 } 390 391 void ManualDWARFIndex::GetTypes( 392 ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) { 393 Index(); 394 m_set.types.Find(name, DIERefCallback(callback, name.GetStringRef())); 395 } 396 397 void ManualDWARFIndex::GetTypes( 398 const DWARFDeclContext &context, 399 llvm::function_ref<bool(DWARFDIE die)> callback) { 400 Index(); 401 auto name = context[0].name; 402 m_set.types.Find(ConstString(name), 403 DIERefCallback(callback, llvm::StringRef(name))); 404 } 405 406 void ManualDWARFIndex::GetNamespaces( 407 ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) { 408 Index(); 409 m_set.namespaces.Find(name, DIERefCallback(callback, name.GetStringRef())); 410 } 411 412 void ManualDWARFIndex::GetFunctions( 413 ConstString name, SymbolFileDWARF &dwarf, 414 const CompilerDeclContext &parent_decl_ctx, uint32_t name_type_mask, 415 llvm::function_ref<bool(DWARFDIE die)> callback) { 416 Index(); 417 418 if (name_type_mask & eFunctionNameTypeFull) { 419 if (!m_set.function_fullnames.Find( 420 name, DIERefCallback( 421 [&](DWARFDIE die) { 422 if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx, 423 die)) 424 return true; 425 return callback(die); 426 }, 427 name.GetStringRef()))) 428 return; 429 } 430 if (name_type_mask & eFunctionNameTypeBase) { 431 if (!m_set.function_basenames.Find( 432 name, DIERefCallback( 433 [&](DWARFDIE die) { 434 if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx, 435 die)) 436 return true; 437 return callback(die); 438 }, 439 name.GetStringRef()))) 440 return; 441 } 442 443 if (name_type_mask & eFunctionNameTypeMethod && !parent_decl_ctx.IsValid()) { 444 if (!m_set.function_methods.Find( 445 name, DIERefCallback(callback, name.GetStringRef()))) 446 return; 447 } 448 449 if (name_type_mask & eFunctionNameTypeSelector && 450 !parent_decl_ctx.IsValid()) { 451 if (!m_set.function_selectors.Find( 452 name, DIERefCallback(callback, name.GetStringRef()))) 453 return; 454 } 455 } 456 457 void ManualDWARFIndex::GetFunctions( 458 const RegularExpression ®ex, 459 llvm::function_ref<bool(DWARFDIE die)> callback) { 460 Index(); 461 462 if (!m_set.function_basenames.Find(regex, 463 DIERefCallback(callback, regex.GetText()))) 464 return; 465 if (!m_set.function_fullnames.Find(regex, 466 DIERefCallback(callback, regex.GetText()))) 467 return; 468 } 469 470 void ManualDWARFIndex::Dump(Stream &s) { 471 s.Format("Manual DWARF index for ({0}) '{1:F}':", 472 m_module.GetArchitecture().GetArchitectureName(), 473 m_module.GetObjectFile()->GetFileSpec()); 474 s.Printf("\nFunction basenames:\n"); 475 m_set.function_basenames.Dump(&s); 476 s.Printf("\nFunction fullnames:\n"); 477 m_set.function_fullnames.Dump(&s); 478 s.Printf("\nFunction methods:\n"); 479 m_set.function_methods.Dump(&s); 480 s.Printf("\nFunction selectors:\n"); 481 m_set.function_selectors.Dump(&s); 482 s.Printf("\nObjective-C class selectors:\n"); 483 m_set.objc_class_selectors.Dump(&s); 484 s.Printf("\nGlobals and statics:\n"); 485 m_set.globals.Dump(&s); 486 s.Printf("\nTypes:\n"); 487 m_set.types.Dump(&s); 488 s.Printf("\nNamespaces:\n"); 489 m_set.namespaces.Dump(&s); 490 } 491 492 constexpr llvm::StringLiteral kIdentifierManualDWARFIndex("DIDX"); 493 // Define IDs for the different tables when encoding and decoding the 494 // ManualDWARFIndex NameToDIE objects so we can avoid saving any empty maps. 495 enum DataID { 496 kDataIDFunctionBasenames = 1u, 497 kDataIDFunctionFullnames, 498 kDataIDFunctionMethods, 499 kDataIDFunctionSelectors, 500 kDataIDFunctionObjcClassSelectors, 501 kDataIDGlobals, 502 kDataIDTypes, 503 kDataIDNamespaces, 504 kDataIDEnd = 255u, 505 506 }; 507 constexpr uint32_t CURRENT_CACHE_VERSION = 1; 508 509 bool ManualDWARFIndex::IndexSet::Decode(const DataExtractor &data, 510 lldb::offset_t *offset_ptr) { 511 StringTableReader strtab; 512 // We now decode the string table for all strings in the data cache file. 513 if (!strtab.Decode(data, offset_ptr)) 514 return false; 515 516 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4); 517 if (identifier != kIdentifierManualDWARFIndex) 518 return false; 519 const uint32_t version = data.GetU32(offset_ptr); 520 if (version != CURRENT_CACHE_VERSION) 521 return false; 522 523 bool done = false; 524 while (!done) { 525 switch (data.GetU8(offset_ptr)) { 526 default: 527 // If we got here, this is not expected, we expect the data IDs to match 528 // one of the values from the DataID enumeration. 529 return false; 530 case kDataIDFunctionBasenames: 531 if (!function_basenames.Decode(data, offset_ptr, strtab)) 532 return false; 533 break; 534 case kDataIDFunctionFullnames: 535 if (!function_fullnames.Decode(data, offset_ptr, strtab)) 536 return false; 537 break; 538 case kDataIDFunctionMethods: 539 if (!function_methods.Decode(data, offset_ptr, strtab)) 540 return false; 541 break; 542 case kDataIDFunctionSelectors: 543 if (!function_selectors.Decode(data, offset_ptr, strtab)) 544 return false; 545 break; 546 case kDataIDFunctionObjcClassSelectors: 547 if (!objc_class_selectors.Decode(data, offset_ptr, strtab)) 548 return false; 549 break; 550 case kDataIDGlobals: 551 if (!globals.Decode(data, offset_ptr, strtab)) 552 return false; 553 break; 554 case kDataIDTypes: 555 if (!types.Decode(data, offset_ptr, strtab)) 556 return false; 557 break; 558 case kDataIDNamespaces: 559 if (!namespaces.Decode(data, offset_ptr, strtab)) 560 return false; 561 break; 562 case kDataIDEnd: 563 // We got to the end of our NameToDIE encodings. 564 done = true; 565 break; 566 } 567 } 568 // Success! 569 return true; 570 } 571 572 void ManualDWARFIndex::IndexSet::Encode(DataEncoder &encoder) const { 573 ConstStringTable strtab; 574 575 // Encoder the DWARF index into a separate encoder first. This allows us 576 // gather all of the strings we willl need in "strtab" as we will need to 577 // write the string table out before the symbol table. 578 DataEncoder index_encoder(encoder.GetByteOrder(), 579 encoder.GetAddressByteSize()); 580 581 index_encoder.AppendData(kIdentifierManualDWARFIndex); 582 // Encode the data version. 583 index_encoder.AppendU32(CURRENT_CACHE_VERSION); 584 585 if (!function_basenames.IsEmpty()) { 586 index_encoder.AppendU8(kDataIDFunctionBasenames); 587 function_basenames.Encode(index_encoder, strtab); 588 } 589 if (!function_fullnames.IsEmpty()) { 590 index_encoder.AppendU8(kDataIDFunctionFullnames); 591 function_fullnames.Encode(index_encoder, strtab); 592 } 593 if (!function_methods.IsEmpty()) { 594 index_encoder.AppendU8(kDataIDFunctionMethods); 595 function_methods.Encode(index_encoder, strtab); 596 } 597 if (!function_selectors.IsEmpty()) { 598 index_encoder.AppendU8(kDataIDFunctionSelectors); 599 function_selectors.Encode(index_encoder, strtab); 600 } 601 if (!objc_class_selectors.IsEmpty()) { 602 index_encoder.AppendU8(kDataIDFunctionObjcClassSelectors); 603 objc_class_selectors.Encode(index_encoder, strtab); 604 } 605 if (!globals.IsEmpty()) { 606 index_encoder.AppendU8(kDataIDGlobals); 607 globals.Encode(index_encoder, strtab); 608 } 609 if (!types.IsEmpty()) { 610 index_encoder.AppendU8(kDataIDTypes); 611 types.Encode(index_encoder, strtab); 612 } 613 if (!namespaces.IsEmpty()) { 614 index_encoder.AppendU8(kDataIDNamespaces); 615 namespaces.Encode(index_encoder, strtab); 616 } 617 index_encoder.AppendU8(kDataIDEnd); 618 619 // Now that all strings have been gathered, we will emit the string table. 620 strtab.Encode(encoder); 621 // Followed the the symbol table data. 622 encoder.AppendData(index_encoder.GetData()); 623 } 624 625 bool ManualDWARFIndex::Decode(const DataExtractor &data, 626 lldb::offset_t *offset_ptr, 627 bool &signature_mismatch) { 628 signature_mismatch = false; 629 CacheSignature signature; 630 if (!signature.Decode(data, offset_ptr)) 631 return false; 632 if (CacheSignature(m_dwarf->GetObjectFile()) != signature) { 633 signature_mismatch = true; 634 return false; 635 } 636 IndexSet set; 637 if (!set.Decode(data, offset_ptr)) 638 return false; 639 m_set = std::move(set); 640 return true; 641 } 642 643 bool ManualDWARFIndex::Encode(DataEncoder &encoder) const { 644 CacheSignature signature(m_dwarf->GetObjectFile()); 645 if (!signature.Encode(encoder)) 646 return false; 647 m_set.Encode(encoder); 648 return true; 649 } 650 651 std::string ManualDWARFIndex::GetCacheKey() { 652 std::string key; 653 llvm::raw_string_ostream strm(key); 654 // DWARF Index can come from different object files for the same module. A 655 // module can have one object file as the main executable and might have 656 // another object file in a separate symbol file, or we might have a .dwo file 657 // that claims its module is the main executable. 658 ObjectFile *objfile = m_dwarf->GetObjectFile(); 659 strm << objfile->GetModule()->GetCacheKey() << "-dwarf-index-" 660 << llvm::format_hex(objfile->GetCacheHash(), 10); 661 return strm.str(); 662 } 663 664 bool ManualDWARFIndex::LoadFromCache() { 665 DataFileCache *cache = Module::GetIndexCache(); 666 if (!cache) 667 return false; 668 ObjectFile *objfile = m_dwarf->GetObjectFile(); 669 if (!objfile) 670 return false; 671 std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up = 672 cache->GetCachedData(GetCacheKey()); 673 if (!mem_buffer_up) 674 return false; 675 DataExtractor data(mem_buffer_up->getBufferStart(), 676 mem_buffer_up->getBufferSize(), 677 endian::InlHostByteOrder(), 678 objfile->GetAddressByteSize()); 679 bool signature_mismatch = false; 680 lldb::offset_t offset = 0; 681 const bool result = Decode(data, &offset, signature_mismatch); 682 if (signature_mismatch) 683 cache->RemoveCacheFile(GetCacheKey()); 684 return result; 685 } 686 687 void ManualDWARFIndex::SaveToCache() { 688 DataFileCache *cache = Module::GetIndexCache(); 689 if (!cache) 690 return; // Caching is not enabled. 691 ObjectFile *objfile = m_dwarf->GetObjectFile(); 692 if (!objfile) 693 return; 694 DataEncoder file(endian::InlHostByteOrder(), objfile->GetAddressByteSize()); 695 // Encode will return false if the object file doesn't have anything to make 696 // a signature from. 697 if (Encode(file)) { 698 if (cache->SetCachedData(GetCacheKey(), file.GetData())) 699 m_dwarf->SetDebugInfoIndexWasSavedToCache(); 700 } 701 } 702