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