Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
39644 views
//===-- ManualDWARFIndex.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Plugins/SymbolFile/DWARF/ManualDWARFIndex.h"9#include "Plugins/Language/ObjC/ObjCLanguage.h"10#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"11#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"12#include "Plugins/SymbolFile/DWARF/LogChannelDWARF.h"13#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h"14#include "lldb/Core/DataFileCache.h"15#include "lldb/Core/Debugger.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#include <optional>2627using namespace lldb_private;28using namespace lldb;29using namespace lldb_private::dwarf;30using namespace lldb_private::plugin::dwarf;3132void ManualDWARFIndex::Index() {33if (m_indexed)34return;35m_indexed = true;3637ElapsedTime elapsed(m_index_time);38LLDB_SCOPED_TIMERF("%p", static_cast<void *>(m_dwarf));39if (LoadFromCache()) {40m_dwarf->SetDebugInfoIndexWasLoadedFromCache();41return;42}4344DWARFDebugInfo &main_info = m_dwarf->DebugInfo();45SymbolFileDWARFDwo *dwp_dwarf = m_dwarf->GetDwpSymbolFile().get();46DWARFDebugInfo *dwp_info = dwp_dwarf ? &dwp_dwarf->DebugInfo() : nullptr;4748std::vector<DWARFUnit *> units_to_index;49units_to_index.reserve(main_info.GetNumUnits() +50(dwp_info ? dwp_info->GetNumUnits() : 0));5152// Process all units in the main file, as well as any type units in the dwp53// file. Type units in dwo files are handled when we reach the dwo file in54// IndexUnit.55for (size_t U = 0; U < main_info.GetNumUnits(); ++U) {56DWARFUnit *unit = main_info.GetUnitAtIndex(U);57if (unit && m_units_to_avoid.count(unit->GetOffset()) == 0)58units_to_index.push_back(unit);59}60if (dwp_info && dwp_info->ContainsTypeUnits()) {61for (size_t U = 0; U < dwp_info->GetNumUnits(); ++U) {62if (auto *tu =63llvm::dyn_cast<DWARFTypeUnit>(dwp_info->GetUnitAtIndex(U))) {64if (!m_type_sigs_to_avoid.contains(tu->GetTypeHash()))65units_to_index.push_back(tu);66}67}68}6970if (units_to_index.empty())71return;7273StreamString module_desc;74m_module.GetDescription(module_desc.AsRawOstream(),75lldb::eDescriptionLevelBrief);7677// Include 2 passes per unit to index for extracting DIEs from the unit and78// indexing the unit, and then 8 extra entries for finalizing each index set.79const uint64_t total_progress = units_to_index.size() * 2 + 8;80Progress progress("Manually indexing DWARF", module_desc.GetData(),81total_progress);8283std::vector<IndexSet> sets(units_to_index.size());8485// Keep memory down by clearing DIEs for any units if indexing86// caused us to load the unit's DIEs.87std::vector<std::optional<DWARFUnit::ScopedExtractDIEs>> clear_cu_dies(88units_to_index.size());89auto parser_fn = [&](size_t cu_idx) {90IndexUnit(*units_to_index[cu_idx], dwp_dwarf, sets[cu_idx]);91progress.Increment();92};9394auto extract_fn = [&](size_t cu_idx) {95clear_cu_dies[cu_idx] = units_to_index[cu_idx]->ExtractDIEsScoped();96progress.Increment();97};9899// Share one thread pool across operations to avoid the overhead of100// recreating the threads.101llvm::ThreadPoolTaskGroup task_group(Debugger::GetThreadPool());102103// Create a task runner that extracts dies for each DWARF unit in a104// separate thread.105// First figure out which units didn't have their DIEs already106// parsed and remember this. If no DIEs were parsed prior to this index107// function call, we are going to want to clear the CU dies after we are108// done indexing to make sure we don't pull in all DWARF dies, but we need109// to wait until all units have been indexed in case a DIE in one110// unit refers to another and the indexes accesses those DIEs.111for (size_t i = 0; i < units_to_index.size(); ++i)112task_group.async(extract_fn, i);113task_group.wait();114115// Now create a task runner that can index each DWARF unit in a116// separate thread so we can index quickly.117for (size_t i = 0; i < units_to_index.size(); ++i)118task_group.async(parser_fn, i);119task_group.wait();120121auto finalize_fn = [this, &sets, &progress](NameToDIE(IndexSet::*index)) {122NameToDIE &result = m_set.*index;123for (auto &set : sets)124result.Append(set.*index);125result.Finalize();126progress.Increment();127};128129task_group.async(finalize_fn, &IndexSet::function_basenames);130task_group.async(finalize_fn, &IndexSet::function_fullnames);131task_group.async(finalize_fn, &IndexSet::function_methods);132task_group.async(finalize_fn, &IndexSet::function_selectors);133task_group.async(finalize_fn, &IndexSet::objc_class_selectors);134task_group.async(finalize_fn, &IndexSet::globals);135task_group.async(finalize_fn, &IndexSet::types);136task_group.async(finalize_fn, &IndexSet::namespaces);137task_group.wait();138139SaveToCache();140}141142void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, SymbolFileDWARFDwo *dwp,143IndexSet &set) {144Log *log = GetLog(DWARFLog::Lookups);145146if (log) {147m_module.LogMessage(148log, "ManualDWARFIndex::IndexUnit for unit at .debug_info[{0:x16}]",149unit.GetOffset());150}151152const LanguageType cu_language = SymbolFileDWARF::GetLanguage(unit);153154// First check if the unit has a DWO ID. If it does then we only want to index155// the .dwo file or nothing at all. If we have a compile unit where we can't156// locate the .dwo/.dwp file we don't want to index anything from the skeleton157// compile unit because it is usally has no children unless158// -fsplit-dwarf-inlining was used at compile time. This option will add a159// copy of all DW_TAG_subprogram and any contained DW_TAG_inline_subroutine160// DIEs so that symbolication will still work in the absence of the .dwo/.dwp161// file, but the functions have no return types and all arguments and locals162// have been removed. So we don't want to index any of these hacked up163// function types. Types can still exist in the skeleton compile unit DWARF164// though as some functions have template parameter types and other things165// that cause extra copies of types to be included, but we should find these166// types in the .dwo file only as methods could have return types removed and167// we don't have to index incomplete types from the skeleton compile unit.168if (unit.GetDWOId()) {169// Index the .dwo or dwp instead of the skeleton unit.170if (SymbolFileDWARFDwo *dwo_symbol_file = unit.GetDwoSymbolFile()) {171// Type units in a dwp file are indexed separately, so we just need to172// process the split unit here. However, if the split unit is in a dwo173// file, then we need to process type units here.174if (dwo_symbol_file == dwp) {175IndexUnitImpl(unit.GetNonSkeletonUnit(), cu_language, set);176} else {177DWARFDebugInfo &dwo_info = dwo_symbol_file->DebugInfo();178for (size_t i = 0; i < dwo_info.GetNumUnits(); ++i)179IndexUnitImpl(*dwo_info.GetUnitAtIndex(i), cu_language, set);180}181return;182}183// This was a DWARF5 skeleton CU and the .dwo file couldn't be located.184if (unit.GetVersion() >= 5 && unit.IsSkeletonUnit())185return;186187// Either this is a DWARF 4 + fission CU with the .dwo file188// missing, or it's a -gmodules pch or pcm. Try to detect the189// latter by checking whether the first DIE is a DW_TAG_module.190// If it's a pch/pcm, continue indexing it.191if (unit.GetDIE(unit.GetFirstDIEOffset()).GetFirstChild().Tag() !=192llvm::dwarf::DW_TAG_module)193return;194}195// We have a normal compile unit which we want to index.196IndexUnitImpl(unit, cu_language, set);197}198199void ManualDWARFIndex::IndexUnitImpl(DWARFUnit &unit,200const LanguageType cu_language,201IndexSet &set) {202for (const DWARFDebugInfoEntry &die : unit.dies()) {203const dw_tag_t tag = die.Tag();204205switch (tag) {206case DW_TAG_array_type:207case DW_TAG_base_type:208case DW_TAG_class_type:209case DW_TAG_constant:210case DW_TAG_enumeration_type:211case DW_TAG_inlined_subroutine:212case DW_TAG_namespace:213case DW_TAG_imported_declaration:214case DW_TAG_string_type:215case DW_TAG_structure_type:216case DW_TAG_subprogram:217case DW_TAG_subroutine_type:218case DW_TAG_typedef:219case DW_TAG_union_type:220case DW_TAG_unspecified_type:221case DW_TAG_variable:222break;223224default:225continue;226}227228const char *name = nullptr;229const char *mangled_cstr = nullptr;230bool is_declaration = false;231bool has_address = false;232bool has_location_or_const_value = false;233bool is_global_or_static_variable = false;234235DWARFFormValue specification_die_form;236DWARFAttributes attributes = die.GetAttributes(&unit);237for (size_t i = 0; i < attributes.Size(); ++i) {238dw_attr_t attr = attributes.AttributeAtIndex(i);239DWARFFormValue form_value;240switch (attr) {241default:242break;243case DW_AT_name:244if (attributes.ExtractFormValueAtIndex(i, form_value))245name = form_value.AsCString();246break;247248case DW_AT_declaration:249if (attributes.ExtractFormValueAtIndex(i, form_value))250is_declaration = form_value.Unsigned() != 0;251break;252253case DW_AT_MIPS_linkage_name:254case DW_AT_linkage_name:255if (attributes.ExtractFormValueAtIndex(i, form_value))256mangled_cstr = form_value.AsCString();257break;258259case DW_AT_low_pc:260case DW_AT_high_pc:261case DW_AT_ranges:262has_address = true;263break;264265case DW_AT_entry_pc:266has_address = true;267break;268269case DW_AT_location:270case DW_AT_const_value:271has_location_or_const_value = true;272is_global_or_static_variable = die.IsGlobalOrStaticScopeVariable();273274break;275276case DW_AT_specification:277if (attributes.ExtractFormValueAtIndex(i, form_value))278specification_die_form = form_value;279break;280}281}282283DIERef ref = *DWARFDIE(&unit, &die).GetDIERef();284switch (tag) {285case DW_TAG_inlined_subroutine:286case DW_TAG_subprogram:287if (has_address) {288if (name) {289bool is_objc_method = false;290if (cu_language == eLanguageTypeObjC ||291cu_language == eLanguageTypeObjC_plus_plus) {292std::optional<const ObjCLanguage::MethodName> objc_method =293ObjCLanguage::MethodName::Create(name, true);294if (objc_method) {295is_objc_method = true;296ConstString class_name_with_category(297objc_method->GetClassNameWithCategory());298ConstString objc_selector_name(objc_method->GetSelector());299ConstString objc_fullname_no_category_name(300objc_method->GetFullNameWithoutCategory().c_str());301ConstString class_name_no_category(objc_method->GetClassName());302set.function_fullnames.Insert(ConstString(name), ref);303if (class_name_with_category)304set.objc_class_selectors.Insert(class_name_with_category, ref);305if (class_name_no_category &&306class_name_no_category != class_name_with_category)307set.objc_class_selectors.Insert(class_name_no_category, ref);308if (objc_selector_name)309set.function_selectors.Insert(objc_selector_name, ref);310if (objc_fullname_no_category_name)311set.function_fullnames.Insert(objc_fullname_no_category_name,312ref);313}314}315// If we have a mangled name, then the DW_AT_name attribute is316// usually the method name without the class or any parameters317bool is_method = DWARFDIE(&unit, &die).IsMethod();318319if (is_method)320set.function_methods.Insert(ConstString(name), ref);321else322set.function_basenames.Insert(ConstString(name), ref);323324if (!is_method && !mangled_cstr && !is_objc_method)325set.function_fullnames.Insert(ConstString(name), ref);326}327if (mangled_cstr) {328// Make sure our mangled name isn't the same string table entry as329// our name. If it starts with '_', then it is ok, else compare the330// string to make sure it isn't the same and we don't end up with331// duplicate entries332if (name && name != mangled_cstr &&333((mangled_cstr[0] == '_') ||334(::strcmp(name, mangled_cstr) != 0))) {335set.function_fullnames.Insert(ConstString(mangled_cstr), ref);336}337}338}339break;340341case DW_TAG_array_type:342case DW_TAG_base_type:343case DW_TAG_class_type:344case DW_TAG_constant:345case DW_TAG_enumeration_type:346case DW_TAG_string_type:347case DW_TAG_structure_type:348case DW_TAG_subroutine_type:349case DW_TAG_typedef:350case DW_TAG_union_type:351case DW_TAG_unspecified_type:352if (name && !is_declaration)353set.types.Insert(ConstString(name), ref);354if (mangled_cstr && !is_declaration)355set.types.Insert(ConstString(mangled_cstr), ref);356break;357358case DW_TAG_namespace:359case DW_TAG_imported_declaration:360if (name)361set.namespaces.Insert(ConstString(name), ref);362break;363364case DW_TAG_variable:365if (name && has_location_or_const_value && is_global_or_static_variable) {366set.globals.Insert(ConstString(name), ref);367// Be sure to include variables by their mangled and demangled names if368// they have any since a variable can have a basename "i", a mangled369// named "_ZN12_GLOBAL__N_11iE" and a demangled mangled name370// "(anonymous namespace)::i"...371372// Make sure our mangled name isn't the same string table entry as our373// name. If it starts with '_', then it is ok, else compare the string374// to make sure it isn't the same and we don't end up with duplicate375// entries376if (mangled_cstr && name != mangled_cstr &&377((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) {378set.globals.Insert(ConstString(mangled_cstr), ref);379}380}381break;382383default:384continue;385}386}387}388389void ManualDWARFIndex::GetGlobalVariables(390ConstString basename, llvm::function_ref<bool(DWARFDIE die)> callback) {391Index();392m_set.globals.Find(basename,393DIERefCallback(callback, basename.GetStringRef()));394}395396void ManualDWARFIndex::GetGlobalVariables(397const RegularExpression ®ex,398llvm::function_ref<bool(DWARFDIE die)> callback) {399Index();400m_set.globals.Find(regex, DIERefCallback(callback, regex.GetText()));401}402403void ManualDWARFIndex::GetGlobalVariables(404DWARFUnit &unit, llvm::function_ref<bool(DWARFDIE die)> callback) {405Index();406m_set.globals.FindAllEntriesForUnit(unit, DIERefCallback(callback));407}408409void ManualDWARFIndex::GetObjCMethods(410ConstString class_name, llvm::function_ref<bool(DWARFDIE die)> callback) {411Index();412m_set.objc_class_selectors.Find(413class_name, DIERefCallback(callback, class_name.GetStringRef()));414}415416void ManualDWARFIndex::GetCompleteObjCClass(417ConstString class_name, bool must_be_implementation,418llvm::function_ref<bool(DWARFDIE die)> callback) {419Index();420m_set.types.Find(class_name,421DIERefCallback(callback, class_name.GetStringRef()));422}423424void ManualDWARFIndex::GetTypes(425ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) {426Index();427m_set.types.Find(name, DIERefCallback(callback, name.GetStringRef()));428}429430void ManualDWARFIndex::GetTypes(431const DWARFDeclContext &context,432llvm::function_ref<bool(DWARFDIE die)> callback) {433Index();434auto name = context[0].name;435m_set.types.Find(ConstString(name),436DIERefCallback(callback, llvm::StringRef(name)));437}438439void ManualDWARFIndex::GetNamespaces(440ConstString name, llvm::function_ref<bool(DWARFDIE die)> callback) {441Index();442m_set.namespaces.Find(name, DIERefCallback(callback, name.GetStringRef()));443}444445void ManualDWARFIndex::GetFunctions(446const Module::LookupInfo &lookup_info, SymbolFileDWARF &dwarf,447const CompilerDeclContext &parent_decl_ctx,448llvm::function_ref<bool(DWARFDIE die)> callback) {449Index();450ConstString name = lookup_info.GetLookupName();451FunctionNameType name_type_mask = lookup_info.GetNameTypeMask();452453if (name_type_mask & eFunctionNameTypeFull) {454if (!m_set.function_fullnames.Find(455name, DIERefCallback(456[&](DWARFDIE die) {457if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx,458die))459return true;460return callback(die);461},462name.GetStringRef())))463return;464}465if (name_type_mask & eFunctionNameTypeBase) {466if (!m_set.function_basenames.Find(467name, DIERefCallback(468[&](DWARFDIE die) {469if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx,470die))471return true;472return callback(die);473},474name.GetStringRef())))475return;476}477478if (name_type_mask & eFunctionNameTypeMethod && !parent_decl_ctx.IsValid()) {479if (!m_set.function_methods.Find(480name, DIERefCallback(callback, name.GetStringRef())))481return;482}483484if (name_type_mask & eFunctionNameTypeSelector &&485!parent_decl_ctx.IsValid()) {486if (!m_set.function_selectors.Find(487name, DIERefCallback(callback, name.GetStringRef())))488return;489}490}491492void ManualDWARFIndex::GetFunctions(493const RegularExpression ®ex,494llvm::function_ref<bool(DWARFDIE die)> callback) {495Index();496497if (!m_set.function_basenames.Find(regex,498DIERefCallback(callback, regex.GetText())))499return;500if (!m_set.function_fullnames.Find(regex,501DIERefCallback(callback, regex.GetText())))502return;503}504505void ManualDWARFIndex::Dump(Stream &s) {506s.Format("Manual DWARF index for ({0}) '{1:F}':",507m_module.GetArchitecture().GetArchitectureName(),508m_module.GetObjectFile()->GetFileSpec());509s.Printf("\nFunction basenames:\n");510m_set.function_basenames.Dump(&s);511s.Printf("\nFunction fullnames:\n");512m_set.function_fullnames.Dump(&s);513s.Printf("\nFunction methods:\n");514m_set.function_methods.Dump(&s);515s.Printf("\nFunction selectors:\n");516m_set.function_selectors.Dump(&s);517s.Printf("\nObjective-C class selectors:\n");518m_set.objc_class_selectors.Dump(&s);519s.Printf("\nGlobals and statics:\n");520m_set.globals.Dump(&s);521s.Printf("\nTypes:\n");522m_set.types.Dump(&s);523s.Printf("\nNamespaces:\n");524m_set.namespaces.Dump(&s);525}526527constexpr llvm::StringLiteral kIdentifierManualDWARFIndex("DIDX");528// Define IDs for the different tables when encoding and decoding the529// ManualDWARFIndex NameToDIE objects so we can avoid saving any empty maps.530enum DataID {531kDataIDFunctionBasenames = 1u,532kDataIDFunctionFullnames,533kDataIDFunctionMethods,534kDataIDFunctionSelectors,535kDataIDFunctionObjcClassSelectors,536kDataIDGlobals,537kDataIDTypes,538kDataIDNamespaces,539kDataIDEnd = 255u,540541};542543// Version 2 changes the encoding of DIERef objects used in the DWARF manual544// index name tables. See DIERef class for details.545constexpr uint32_t CURRENT_CACHE_VERSION = 2;546547bool ManualDWARFIndex::IndexSet::Decode(const DataExtractor &data,548lldb::offset_t *offset_ptr) {549StringTableReader strtab;550// We now decode the string table for all strings in the data cache file.551if (!strtab.Decode(data, offset_ptr))552return false;553554llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);555if (identifier != kIdentifierManualDWARFIndex)556return false;557const uint32_t version = data.GetU32(offset_ptr);558if (version != CURRENT_CACHE_VERSION)559return false;560561bool done = false;562while (!done) {563switch (data.GetU8(offset_ptr)) {564default:565// If we got here, this is not expected, we expect the data IDs to match566// one of the values from the DataID enumeration.567return false;568case kDataIDFunctionBasenames:569if (!function_basenames.Decode(data, offset_ptr, strtab))570return false;571break;572case kDataIDFunctionFullnames:573if (!function_fullnames.Decode(data, offset_ptr, strtab))574return false;575break;576case kDataIDFunctionMethods:577if (!function_methods.Decode(data, offset_ptr, strtab))578return false;579break;580case kDataIDFunctionSelectors:581if (!function_selectors.Decode(data, offset_ptr, strtab))582return false;583break;584case kDataIDFunctionObjcClassSelectors:585if (!objc_class_selectors.Decode(data, offset_ptr, strtab))586return false;587break;588case kDataIDGlobals:589if (!globals.Decode(data, offset_ptr, strtab))590return false;591break;592case kDataIDTypes:593if (!types.Decode(data, offset_ptr, strtab))594return false;595break;596case kDataIDNamespaces:597if (!namespaces.Decode(data, offset_ptr, strtab))598return false;599break;600case kDataIDEnd:601// We got to the end of our NameToDIE encodings.602done = true;603break;604}605}606// Success!607return true;608}609610void ManualDWARFIndex::IndexSet::Encode(DataEncoder &encoder) const {611ConstStringTable strtab;612613// Encoder the DWARF index into a separate encoder first. This allows us614// gather all of the strings we willl need in "strtab" as we will need to615// write the string table out before the symbol table.616DataEncoder index_encoder(encoder.GetByteOrder(),617encoder.GetAddressByteSize());618619index_encoder.AppendData(kIdentifierManualDWARFIndex);620// Encode the data version.621index_encoder.AppendU32(CURRENT_CACHE_VERSION);622623if (!function_basenames.IsEmpty()) {624index_encoder.AppendU8(kDataIDFunctionBasenames);625function_basenames.Encode(index_encoder, strtab);626}627if (!function_fullnames.IsEmpty()) {628index_encoder.AppendU8(kDataIDFunctionFullnames);629function_fullnames.Encode(index_encoder, strtab);630}631if (!function_methods.IsEmpty()) {632index_encoder.AppendU8(kDataIDFunctionMethods);633function_methods.Encode(index_encoder, strtab);634}635if (!function_selectors.IsEmpty()) {636index_encoder.AppendU8(kDataIDFunctionSelectors);637function_selectors.Encode(index_encoder, strtab);638}639if (!objc_class_selectors.IsEmpty()) {640index_encoder.AppendU8(kDataIDFunctionObjcClassSelectors);641objc_class_selectors.Encode(index_encoder, strtab);642}643if (!globals.IsEmpty()) {644index_encoder.AppendU8(kDataIDGlobals);645globals.Encode(index_encoder, strtab);646}647if (!types.IsEmpty()) {648index_encoder.AppendU8(kDataIDTypes);649types.Encode(index_encoder, strtab);650}651if (!namespaces.IsEmpty()) {652index_encoder.AppendU8(kDataIDNamespaces);653namespaces.Encode(index_encoder, strtab);654}655index_encoder.AppendU8(kDataIDEnd);656657// Now that all strings have been gathered, we will emit the string table.658strtab.Encode(encoder);659// Followed by the symbol table data.660encoder.AppendData(index_encoder.GetData());661}662663bool ManualDWARFIndex::Decode(const DataExtractor &data,664lldb::offset_t *offset_ptr,665bool &signature_mismatch) {666signature_mismatch = false;667CacheSignature signature;668if (!signature.Decode(data, offset_ptr))669return false;670if (CacheSignature(m_dwarf->GetObjectFile()) != signature) {671signature_mismatch = true;672return false;673}674IndexSet set;675if (!set.Decode(data, offset_ptr))676return false;677m_set = std::move(set);678return true;679}680681bool ManualDWARFIndex::Encode(DataEncoder &encoder) const {682CacheSignature signature(m_dwarf->GetObjectFile());683if (!signature.Encode(encoder))684return false;685m_set.Encode(encoder);686return true;687}688689std::string ManualDWARFIndex::GetCacheKey() {690std::string key;691llvm::raw_string_ostream strm(key);692// DWARF Index can come from different object files for the same module. A693// module can have one object file as the main executable and might have694// another object file in a separate symbol file, or we might have a .dwo file695// that claims its module is the main executable.696ObjectFile *objfile = m_dwarf->GetObjectFile();697strm << objfile->GetModule()->GetCacheKey() << "-dwarf-index-"698<< llvm::format_hex(objfile->GetCacheHash(), 10);699return strm.str();700}701702bool ManualDWARFIndex::LoadFromCache() {703DataFileCache *cache = Module::GetIndexCache();704if (!cache)705return false;706ObjectFile *objfile = m_dwarf->GetObjectFile();707if (!objfile)708return false;709std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =710cache->GetCachedData(GetCacheKey());711if (!mem_buffer_up)712return false;713DataExtractor data(mem_buffer_up->getBufferStart(),714mem_buffer_up->getBufferSize(),715endian::InlHostByteOrder(),716objfile->GetAddressByteSize());717bool signature_mismatch = false;718lldb::offset_t offset = 0;719const bool result = Decode(data, &offset, signature_mismatch);720if (signature_mismatch)721cache->RemoveCacheFile(GetCacheKey());722return result;723}724725void ManualDWARFIndex::SaveToCache() {726DataFileCache *cache = Module::GetIndexCache();727if (!cache)728return; // Caching is not enabled.729ObjectFile *objfile = m_dwarf->GetObjectFile();730if (!objfile)731return;732DataEncoder file(endian::InlHostByteOrder(), objfile->GetAddressByteSize());733// Encode will return false if the object file doesn't have anything to make734// a signature from.735if (Encode(file)) {736if (cache->SetCachedData(GetCacheKey(), file.GetData()))737m_dwarf->SetDebugInfoIndexWasSavedToCache();738}739}740741742