1 //===-- SymbolFileDWARF.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 "SymbolFileDWARF.h"
10 
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/Threading.h"
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleList.h"
17 #include "lldb/Core/ModuleSpec.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/Value.h"
22 #include "lldb/Utility/ArchSpec.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Scalar.h"
25 #include "lldb/Utility/StreamString.h"
26 #include "lldb/Utility/Timer.h"
27 
28 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
29 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
30 
31 #include "lldb/Host/FileSystem.h"
32 #include "lldb/Host/Host.h"
33 
34 #include "lldb/Interpreter/OptionValueFileSpecList.h"
35 #include "lldb/Interpreter/OptionValueProperties.h"
36 
37 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
38 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
39 #include "lldb/Symbol/Block.h"
40 #include "lldb/Symbol/CompileUnit.h"
41 #include "lldb/Symbol/CompilerDecl.h"
42 #include "lldb/Symbol/CompilerDeclContext.h"
43 #include "lldb/Symbol/DebugMacros.h"
44 #include "lldb/Symbol/LineTable.h"
45 #include "lldb/Symbol/LocateSymbolFile.h"
46 #include "lldb/Symbol/ObjectFile.h"
47 #include "lldb/Symbol/SymbolFile.h"
48 #include "lldb/Symbol/TypeMap.h"
49 #include "lldb/Symbol/TypeSystem.h"
50 #include "lldb/Symbol/VariableList.h"
51 
52 #include "lldb/Target/Language.h"
53 #include "lldb/Target/Target.h"
54 
55 #include "AppleDWARFIndex.h"
56 #include "DWARFASTParser.h"
57 #include "DWARFASTParserClang.h"
58 #include "DWARFCompileUnit.h"
59 #include "DWARFDebugAbbrev.h"
60 #include "DWARFDebugAranges.h"
61 #include "DWARFDebugInfo.h"
62 #include "DWARFDebugMacro.h"
63 #include "DWARFDebugRanges.h"
64 #include "DWARFDeclContext.h"
65 #include "DWARFFormValue.h"
66 #include "DWARFTypeUnit.h"
67 #include "DWARFUnit.h"
68 #include "DebugNamesDWARFIndex.h"
69 #include "LogChannelDWARF.h"
70 #include "ManualDWARFIndex.h"
71 #include "SymbolFileDWARFDebugMap.h"
72 #include "SymbolFileDWARFDwo.h"
73 
74 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
75 #include "llvm/Support/FileSystem.h"
76 
77 #include <algorithm>
78 #include <map>
79 #include <memory>
80 
81 #include <ctype.h>
82 #include <string.h>
83 
84 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
85 
86 #ifdef ENABLE_DEBUG_PRINTF
87 #include <stdio.h>
88 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
89 #else
90 #define DEBUG_PRINTF(fmt, ...)
91 #endif
92 
93 using namespace lldb;
94 using namespace lldb_private;
95 
96 LLDB_PLUGIN_DEFINE(SymbolFileDWARF)
97 
98 char SymbolFileDWARF::ID;
99 
100 // static inline bool
101 // child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
102 //{
103 //    switch (tag)
104 //    {
105 //    default:
106 //        break;
107 //    case DW_TAG_subprogram:
108 //    case DW_TAG_inlined_subroutine:
109 //    case DW_TAG_class_type:
110 //    case DW_TAG_structure_type:
111 //    case DW_TAG_union_type:
112 //        return true;
113 //    }
114 //    return false;
115 //}
116 //
117 
118 namespace {
119 
120 #define LLDB_PROPERTIES_symbolfiledwarf
121 #include "SymbolFileDWARFProperties.inc"
122 
123 enum {
124 #define LLDB_PROPERTIES_symbolfiledwarf
125 #include "SymbolFileDWARFPropertiesEnum.inc"
126 };
127 
128 class PluginProperties : public Properties {
129 public:
GetSettingName()130   static ConstString GetSettingName() {
131     return SymbolFileDWARF::GetPluginNameStatic();
132   }
133 
PluginProperties()134   PluginProperties() {
135     m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
136     m_collection_sp->Initialize(g_symbolfiledwarf_properties);
137   }
138 
IgnoreFileIndexes() const139   bool IgnoreFileIndexes() const {
140     return m_collection_sp->GetPropertyAtIndexAsBoolean(
141         nullptr, ePropertyIgnoreIndexes, false);
142   }
143 };
144 
145 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
146 
GetGlobalPluginProperties()147 static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
148   static const auto g_settings_sp(std::make_shared<PluginProperties>());
149   return g_settings_sp;
150 }
151 
152 } // namespace
153 
154 static const llvm::DWARFDebugLine::LineTable *
ParseLLVMLineTable(lldb_private::DWARFContext & context,llvm::DWARFDebugLine & line,dw_offset_t line_offset,dw_offset_t unit_offset)155 ParseLLVMLineTable(lldb_private::DWARFContext &context,
156                    llvm::DWARFDebugLine &line, dw_offset_t line_offset,
157                    dw_offset_t unit_offset) {
158   Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
159 
160   llvm::DWARFDataExtractor data = context.getOrLoadLineData().GetAsLLVM();
161   llvm::DWARFContext &ctx = context.GetAsLLVM();
162   llvm::Expected<const llvm::DWARFDebugLine::LineTable *> line_table =
163       line.getOrParseLineTable(
164           data, line_offset, ctx, nullptr, [&](llvm::Error e) {
165             LLDB_LOG_ERROR(
166                 log, std::move(e),
167                 "SymbolFileDWARF::ParseLineTable failed to parse: {0}");
168           });
169 
170   if (!line_table) {
171     LLDB_LOG_ERROR(log, line_table.takeError(),
172                    "SymbolFileDWARF::ParseLineTable failed to parse: {0}");
173     return nullptr;
174   }
175   return *line_table;
176 }
177 
ParseLLVMLineTablePrologue(lldb_private::DWARFContext & context,llvm::DWARFDebugLine::Prologue & prologue,dw_offset_t line_offset,dw_offset_t unit_offset)178 static bool ParseLLVMLineTablePrologue(lldb_private::DWARFContext &context,
179                                        llvm::DWARFDebugLine::Prologue &prologue,
180                                        dw_offset_t line_offset,
181                                        dw_offset_t unit_offset) {
182   Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
183   bool success = true;
184   llvm::DWARFDataExtractor data = context.getOrLoadLineData().GetAsLLVM();
185   llvm::DWARFContext &ctx = context.GetAsLLVM();
186   uint64_t offset = line_offset;
187   llvm::Error error = prologue.parse(
188       data, &offset,
189       [&](llvm::Error e) {
190         success = false;
191         LLDB_LOG_ERROR(log, std::move(e),
192                        "SymbolFileDWARF::ParseSupportFiles failed to parse "
193                        "line table prologue: {0}");
194       },
195       ctx, nullptr);
196   if (error) {
197     LLDB_LOG_ERROR(log, std::move(error),
198                    "SymbolFileDWARF::ParseSupportFiles failed to parse line "
199                    "table prologue: {0}");
200     return false;
201   }
202   return success;
203 }
204 
205 static llvm::Optional<std::string>
GetFileByIndex(const llvm::DWARFDebugLine::Prologue & prologue,size_t idx,llvm::StringRef compile_dir,FileSpec::Style style)206 GetFileByIndex(const llvm::DWARFDebugLine::Prologue &prologue, size_t idx,
207                llvm::StringRef compile_dir, FileSpec::Style style) {
208   // Try to get an absolute path first.
209   std::string abs_path;
210   auto absolute = llvm::DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath;
211   if (prologue.getFileNameByIndex(idx, compile_dir, absolute, abs_path, style))
212     return std::move(abs_path);
213 
214   // Otherwise ask for a relative path.
215   std::string rel_path;
216   auto relative = llvm::DILineInfoSpecifier::FileLineInfoKind::RawValue;
217   if (!prologue.getFileNameByIndex(idx, compile_dir, relative, rel_path, style))
218     return {};
219   return std::move(rel_path);
220 }
221 
222 static FileSpecList
ParseSupportFilesFromPrologue(const lldb::ModuleSP & module,const llvm::DWARFDebugLine::Prologue & prologue,FileSpec::Style style,llvm::StringRef compile_dir={})223 ParseSupportFilesFromPrologue(const lldb::ModuleSP &module,
224                               const llvm::DWARFDebugLine::Prologue &prologue,
225                               FileSpec::Style style,
226                               llvm::StringRef compile_dir = {}) {
227   FileSpecList support_files;
228   size_t first_file = 0;
229   if (prologue.getVersion() <= 4) {
230     // File index 0 is not valid before DWARF v5. Add a dummy entry to ensure
231     // support file list indices match those we get from the debug info and line
232     // tables.
233     support_files.Append(FileSpec());
234     first_file = 1;
235   }
236 
237   const size_t number_of_files = prologue.FileNames.size();
238   for (size_t idx = first_file; idx <= number_of_files; ++idx) {
239     std::string remapped_file;
240     if (auto file_path = GetFileByIndex(prologue, idx, compile_dir, style))
241       if (!module->RemapSourceFile(llvm::StringRef(*file_path), remapped_file))
242         remapped_file = std::move(*file_path);
243 
244     // Unconditionally add an entry, so the indices match up.
245     support_files.EmplaceBack(remapped_file, style);
246   }
247 
248   return support_files;
249 }
250 
Initialize()251 void SymbolFileDWARF::Initialize() {
252   LogChannelDWARF::Initialize();
253   PluginManager::RegisterPlugin(GetPluginNameStatic(),
254                                 GetPluginDescriptionStatic(), CreateInstance,
255                                 DebuggerInitialize);
256   SymbolFileDWARFDebugMap::Initialize();
257 }
258 
DebuggerInitialize(Debugger & debugger)259 void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
260   if (!PluginManager::GetSettingForSymbolFilePlugin(
261           debugger, PluginProperties::GetSettingName())) {
262     const bool is_global_setting = true;
263     PluginManager::CreateSettingForSymbolFilePlugin(
264         debugger, GetGlobalPluginProperties()->GetValueProperties(),
265         ConstString("Properties for the dwarf symbol-file plug-in."),
266         is_global_setting);
267   }
268 }
269 
Terminate()270 void SymbolFileDWARF::Terminate() {
271   SymbolFileDWARFDebugMap::Terminate();
272   PluginManager::UnregisterPlugin(CreateInstance);
273   LogChannelDWARF::Terminate();
274 }
275 
GetPluginNameStatic()276 lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
277   static ConstString g_name("dwarf");
278   return g_name;
279 }
280 
GetPluginDescriptionStatic()281 const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
282   return "DWARF and DWARF3 debug symbol file reader.";
283 }
284 
CreateInstance(ObjectFileSP objfile_sp)285 SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFileSP objfile_sp) {
286   return new SymbolFileDWARF(std::move(objfile_sp),
287                              /*dwo_section_list*/ nullptr);
288 }
289 
GetTypeList()290 TypeList &SymbolFileDWARF::GetTypeList() {
291   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
292   if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
293     return debug_map_symfile->GetTypeList();
294   return SymbolFile::GetTypeList();
295 }
GetTypes(const DWARFDIE & die,dw_offset_t min_die_offset,dw_offset_t max_die_offset,uint32_t type_mask,TypeSet & type_set)296 void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
297                                dw_offset_t max_die_offset, uint32_t type_mask,
298                                TypeSet &type_set) {
299   if (die) {
300     const dw_offset_t die_offset = die.GetOffset();
301 
302     if (die_offset >= max_die_offset)
303       return;
304 
305     if (die_offset >= min_die_offset) {
306       const dw_tag_t tag = die.Tag();
307 
308       bool add_type = false;
309 
310       switch (tag) {
311       case DW_TAG_array_type:
312         add_type = (type_mask & eTypeClassArray) != 0;
313         break;
314       case DW_TAG_unspecified_type:
315       case DW_TAG_base_type:
316         add_type = (type_mask & eTypeClassBuiltin) != 0;
317         break;
318       case DW_TAG_class_type:
319         add_type = (type_mask & eTypeClassClass) != 0;
320         break;
321       case DW_TAG_structure_type:
322         add_type = (type_mask & eTypeClassStruct) != 0;
323         break;
324       case DW_TAG_union_type:
325         add_type = (type_mask & eTypeClassUnion) != 0;
326         break;
327       case DW_TAG_enumeration_type:
328         add_type = (type_mask & eTypeClassEnumeration) != 0;
329         break;
330       case DW_TAG_subroutine_type:
331       case DW_TAG_subprogram:
332       case DW_TAG_inlined_subroutine:
333         add_type = (type_mask & eTypeClassFunction) != 0;
334         break;
335       case DW_TAG_pointer_type:
336         add_type = (type_mask & eTypeClassPointer) != 0;
337         break;
338       case DW_TAG_rvalue_reference_type:
339       case DW_TAG_reference_type:
340         add_type = (type_mask & eTypeClassReference) != 0;
341         break;
342       case DW_TAG_typedef:
343         add_type = (type_mask & eTypeClassTypedef) != 0;
344         break;
345       case DW_TAG_ptr_to_member_type:
346         add_type = (type_mask & eTypeClassMemberPointer) != 0;
347         break;
348       default:
349         break;
350       }
351 
352       if (add_type) {
353         const bool assert_not_being_parsed = true;
354         Type *type = ResolveTypeUID(die, assert_not_being_parsed);
355         if (type)
356           type_set.insert(type);
357       }
358     }
359 
360     for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
361          child_die = child_die.GetSibling()) {
362       GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
363     }
364   }
365 }
366 
GetTypes(SymbolContextScope * sc_scope,TypeClass type_mask,TypeList & type_list)367 void SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
368                                TypeClass type_mask, TypeList &type_list)
369 
370 {
371   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
372   TypeSet type_set;
373 
374   CompileUnit *comp_unit = nullptr;
375   if (sc_scope)
376     comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
377 
378   const auto &get = [&](DWARFUnit *unit) {
379     if (!unit)
380       return;
381     unit = &unit->GetNonSkeletonUnit();
382     GetTypes(unit->DIE(), unit->GetOffset(), unit->GetNextUnitOffset(),
383              type_mask, type_set);
384   };
385   if (comp_unit) {
386     get(GetDWARFCompileUnit(comp_unit));
387   } else {
388     DWARFDebugInfo &info = DebugInfo();
389     const size_t num_cus = info.GetNumUnits();
390     for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx)
391       get(info.GetUnitAtIndex(cu_idx));
392   }
393 
394   std::set<CompilerType> compiler_type_set;
395   for (Type *type : type_set) {
396     CompilerType compiler_type = type->GetForwardCompilerType();
397     if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
398       compiler_type_set.insert(compiler_type);
399       type_list.Insert(type->shared_from_this());
400     }
401   }
402 }
403 
404 // Gets the first parent that is a lexical block, function or inlined
405 // subroutine, or compile unit.
406 DWARFDIE
GetParentSymbolContextDIE(const DWARFDIE & child_die)407 SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
408   DWARFDIE die;
409   for (die = child_die.GetParent(); die; die = die.GetParent()) {
410     dw_tag_t tag = die.Tag();
411 
412     switch (tag) {
413     case DW_TAG_compile_unit:
414     case DW_TAG_partial_unit:
415     case DW_TAG_subprogram:
416     case DW_TAG_inlined_subroutine:
417     case DW_TAG_lexical_block:
418       return die;
419     default:
420       break;
421     }
422   }
423   return DWARFDIE();
424 }
425 
SymbolFileDWARF(ObjectFileSP objfile_sp,SectionList * dwo_section_list)426 SymbolFileDWARF::SymbolFileDWARF(ObjectFileSP objfile_sp,
427                                  SectionList *dwo_section_list)
428     : SymbolFile(std::move(objfile_sp)),
429       UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
430                                   // when this class parses .o files to
431                                   // contain the .o file index/ID
432       m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
433       m_context(m_objfile_sp->GetModule()->GetSectionList(), dwo_section_list),
434       m_fetched_external_modules(false),
435       m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
436 
~SymbolFileDWARF()437 SymbolFileDWARF::~SymbolFileDWARF() {}
438 
GetDWARFMachOSegmentName()439 static ConstString GetDWARFMachOSegmentName() {
440   static ConstString g_dwarf_section_name("__DWARF");
441   return g_dwarf_section_name;
442 }
443 
GetUniqueDWARFASTTypeMap()444 UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
445   SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
446   if (debug_map_symfile)
447     return debug_map_symfile->GetUniqueDWARFASTTypeMap();
448   else
449     return m_unique_ast_type_map;
450 }
451 
452 llvm::Expected<TypeSystem &>
GetTypeSystemForLanguage(LanguageType language)453 SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
454   if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
455     return debug_map_symfile->GetTypeSystemForLanguage(language);
456 
457   auto type_system_or_err =
458       m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
459   if (type_system_or_err) {
460     type_system_or_err->SetSymbolFile(this);
461   }
462   return type_system_or_err;
463 }
464 
InitializeObject()465 void SymbolFileDWARF::InitializeObject() {
466   Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
467 
468   if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
469     DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
470     LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
471     LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
472     LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
473     LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
474 
475     m_index = AppleDWARFIndex::Create(
476         *GetObjectFile()->GetModule(), apple_names, apple_namespaces,
477         apple_types, apple_objc, m_context.getOrLoadStrData());
478 
479     if (m_index)
480       return;
481 
482     DWARFDataExtractor debug_names;
483     LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
484     if (debug_names.GetByteSize() > 0) {
485       llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
486           DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
487                                        debug_names,
488                                        m_context.getOrLoadStrData(), *this);
489       if (index_or) {
490         m_index = std::move(*index_or);
491         return;
492       }
493       LLDB_LOG_ERROR(log, index_or.takeError(),
494                      "Unable to read .debug_names data: {0}");
495     }
496   }
497 
498   m_index =
499       std::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(), *this);
500 }
501 
SupportedVersion(uint16_t version)502 bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
503   return version >= 2 && version <= 5;
504 }
505 
CalculateAbilities()506 uint32_t SymbolFileDWARF::CalculateAbilities() {
507   uint32_t abilities = 0;
508   if (m_objfile_sp != nullptr) {
509     const Section *section = nullptr;
510     const SectionList *section_list = m_objfile_sp->GetSectionList();
511     if (section_list == nullptr)
512       return 0;
513 
514     uint64_t debug_abbrev_file_size = 0;
515     uint64_t debug_info_file_size = 0;
516     uint64_t debug_line_file_size = 0;
517 
518     section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
519 
520     if (section)
521       section_list = &section->GetChildren();
522 
523     section =
524         section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
525     if (section != nullptr) {
526       debug_info_file_size = section->GetFileSize();
527 
528       section =
529           section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
530               .get();
531       if (section)
532         debug_abbrev_file_size = section->GetFileSize();
533 
534       DWARFDebugAbbrev *abbrev = DebugAbbrev();
535       if (abbrev) {
536         std::set<dw_form_t> invalid_forms;
537         abbrev->GetUnsupportedForms(invalid_forms);
538         if (!invalid_forms.empty()) {
539           StreamString error;
540           error.Printf("unsupported DW_FORM value%s:",
541                        invalid_forms.size() > 1 ? "s" : "");
542           for (auto form : invalid_forms)
543             error.Printf(" %#x", form);
544           m_objfile_sp->GetModule()->ReportWarning(
545               "%s", error.GetString().str().c_str());
546           return 0;
547         }
548       }
549 
550       section =
551           section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
552               .get();
553       if (section)
554         debug_line_file_size = section->GetFileSize();
555     } else {
556       const char *symfile_dir_cstr =
557           m_objfile_sp->GetFileSpec().GetDirectory().GetCString();
558       if (symfile_dir_cstr) {
559         if (strcasestr(symfile_dir_cstr, ".dsym")) {
560           if (m_objfile_sp->GetType() == ObjectFile::eTypeDebugInfo) {
561             // We have a dSYM file that didn't have a any debug info. If the
562             // string table has a size of 1, then it was made from an
563             // executable with no debug info, or from an executable that was
564             // stripped.
565             section =
566                 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
567                     .get();
568             if (section && section->GetFileSize() == 1) {
569               m_objfile_sp->GetModule()->ReportWarning(
570                   "empty dSYM file detected, dSYM was created with an "
571                   "executable with no debug info.");
572             }
573           }
574         }
575       }
576     }
577 
578     if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
579       abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
580                    LocalVariables | VariableTypes;
581 
582     if (debug_line_file_size > 0)
583       abilities |= LineTables;
584   }
585   return abilities;
586 }
587 
LoadSectionData(lldb::SectionType sect_type,DWARFDataExtractor & data)588 void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
589                                       DWARFDataExtractor &data) {
590   ModuleSP module_sp(m_objfile_sp->GetModule());
591   const SectionList *section_list = module_sp->GetSectionList();
592   if (!section_list)
593     return;
594 
595   SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
596   if (!section_sp)
597     return;
598 
599   data.Clear();
600   m_objfile_sp->ReadSectionData(section_sp.get(), data);
601 }
602 
DebugAbbrev()603 DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
604   if (m_abbr)
605     return m_abbr.get();
606 
607   const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
608   if (debug_abbrev_data.GetByteSize() == 0)
609     return nullptr;
610 
611   auto abbr = std::make_unique<DWARFDebugAbbrev>();
612   llvm::Error error = abbr->parse(debug_abbrev_data);
613   if (error) {
614     Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
615     LLDB_LOG_ERROR(log, std::move(error),
616                    "Unable to read .debug_abbrev section: {0}");
617     return nullptr;
618   }
619 
620   m_abbr = std::move(abbr);
621   return m_abbr.get();
622 }
623 
DebugInfo()624 DWARFDebugInfo &SymbolFileDWARF::DebugInfo() {
625   llvm::call_once(m_info_once_flag, [&] {
626     static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
627     Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
628                        static_cast<void *>(this));
629     m_info = std::make_unique<DWARFDebugInfo>(*this, m_context);
630   });
631   return *m_info;
632 }
633 
GetDWARFCompileUnit(CompileUnit * comp_unit)634 DWARFCompileUnit *SymbolFileDWARF::GetDWARFCompileUnit(CompileUnit *comp_unit) {
635   if (!comp_unit)
636     return nullptr;
637 
638   // The compile unit ID is the index of the DWARF unit.
639   DWARFUnit *dwarf_cu = DebugInfo().GetUnitAtIndex(comp_unit->GetID());
640   if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
641     dwarf_cu->SetUserData(comp_unit);
642 
643   // It must be DWARFCompileUnit when it created a CompileUnit.
644   return llvm::cast_or_null<DWARFCompileUnit>(dwarf_cu);
645 }
646 
GetDebugRanges()647 DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
648   if (!m_ranges) {
649     static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
650     Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
651                        static_cast<void *>(this));
652 
653     if (m_context.getOrLoadRangesData().GetByteSize() > 0)
654       m_ranges = std::make_unique<DWARFDebugRanges>();
655 
656     if (m_ranges)
657       m_ranges->Extract(m_context);
658   }
659   return m_ranges.get();
660 }
661 
662 /// Make an absolute path out of \p file_spec and remap it using the
663 /// module's source remapping dictionary.
MakeAbsoluteAndRemap(FileSpec & file_spec,DWARFUnit & dwarf_cu,const ModuleSP & module_sp)664 static void MakeAbsoluteAndRemap(FileSpec &file_spec, DWARFUnit &dwarf_cu,
665                                  const ModuleSP &module_sp) {
666   if (!file_spec)
667     return;
668   // If we have a full path to the compile unit, we don't need to
669   // resolve the file.  This can be expensive e.g. when the source
670   // files are NFS mounted.
671   file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
672 
673   std::string remapped_file;
674   if (module_sp->RemapSourceFile(file_spec.GetPath(), remapped_file))
675     file_spec.SetFile(remapped_file, FileSpec::Style::native);
676 }
677 
ParseCompileUnit(DWARFCompileUnit & dwarf_cu)678 lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
679   CompUnitSP cu_sp;
680   CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
681   if (comp_unit) {
682     // We already parsed this compile unit, had out a shared pointer to it
683     cu_sp = comp_unit->shared_from_this();
684   } else {
685     if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
686       // Let the debug map create the compile unit
687       cu_sp = m_debug_map_symfile->GetCompileUnit(this);
688       dwarf_cu.SetUserData(cu_sp.get());
689     } else {
690       ModuleSP module_sp(m_objfile_sp->GetModule());
691       if (module_sp) {
692         const DWARFBaseDIE cu_die =
693             dwarf_cu.GetNonSkeletonUnit().GetUnitDIEOnly();
694         if (cu_die) {
695           FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
696           MakeAbsoluteAndRemap(cu_file_spec, dwarf_cu, module_sp);
697 
698           LanguageType cu_language = SymbolFileDWARF::LanguageTypeFromDWARF(
699               cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
700 
701           bool is_optimized = dwarf_cu.GetNonSkeletonUnit().GetIsOptimized();
702           BuildCuTranslationTable();
703           cu_sp = std::make_shared<CompileUnit>(
704               module_sp, &dwarf_cu, cu_file_spec,
705               *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
706               is_optimized ? eLazyBoolYes : eLazyBoolNo);
707 
708           dwarf_cu.SetUserData(cu_sp.get());
709 
710           SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
711         }
712       }
713     }
714   }
715   return cu_sp;
716 }
717 
BuildCuTranslationTable()718 void SymbolFileDWARF::BuildCuTranslationTable() {
719   if (!m_lldb_cu_to_dwarf_unit.empty())
720     return;
721 
722   DWARFDebugInfo &info = DebugInfo();
723   if (!info.ContainsTypeUnits()) {
724     // We can use a 1-to-1 mapping. No need to build a translation table.
725     return;
726   }
727   for (uint32_t i = 0, num = info.GetNumUnits(); i < num; ++i) {
728     if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info.GetUnitAtIndex(i))) {
729       cu->SetID(m_lldb_cu_to_dwarf_unit.size());
730       m_lldb_cu_to_dwarf_unit.push_back(i);
731     }
732   }
733 }
734 
GetDWARFUnitIndex(uint32_t cu_idx)735 llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
736   BuildCuTranslationTable();
737   if (m_lldb_cu_to_dwarf_unit.empty())
738     return cu_idx;
739   if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
740     return llvm::None;
741   return m_lldb_cu_to_dwarf_unit[cu_idx];
742 }
743 
CalculateNumCompileUnits()744 uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
745   BuildCuTranslationTable();
746   return m_lldb_cu_to_dwarf_unit.empty() ? DebugInfo().GetNumUnits()
747                                          : m_lldb_cu_to_dwarf_unit.size();
748 }
749 
ParseCompileUnitAtIndex(uint32_t cu_idx)750 CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
751   ASSERT_MODULE_LOCK(this);
752   if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
753     if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
754             DebugInfo().GetUnitAtIndex(*dwarf_idx)))
755       return ParseCompileUnit(*dwarf_cu);
756   }
757   return {};
758 }
759 
ParseFunction(CompileUnit & comp_unit,const DWARFDIE & die)760 Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
761                                          const DWARFDIE &die) {
762   ASSERT_MODULE_LOCK(this);
763   if (!die.IsValid())
764     return nullptr;
765 
766   auto type_system_or_err = GetTypeSystemForLanguage(GetLanguage(*die.GetCU()));
767   if (auto err = type_system_or_err.takeError()) {
768     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
769                    std::move(err), "Unable to parse function");
770     return nullptr;
771   }
772   DWARFASTParser *dwarf_ast = type_system_or_err->GetDWARFParser();
773   if (!dwarf_ast)
774     return nullptr;
775 
776   return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
777 }
778 
FixupAddress(lldb::addr_t file_addr)779 lldb::addr_t SymbolFileDWARF::FixupAddress(lldb::addr_t file_addr) {
780   SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
781   if (debug_map_symfile)
782     return debug_map_symfile->LinkOSOFileAddress(this, file_addr);
783   return file_addr;
784 }
785 
FixupAddress(Address & addr)786 bool SymbolFileDWARF::FixupAddress(Address &addr) {
787   SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
788   if (debug_map_symfile) {
789     return debug_map_symfile->LinkOSOAddress(addr);
790   }
791   // This is a normal DWARF file, no address fixups need to happen
792   return true;
793 }
ParseLanguage(CompileUnit & comp_unit)794 lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
795   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
796   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
797   if (dwarf_cu)
798     return GetLanguage(*dwarf_cu);
799   else
800     return eLanguageTypeUnknown;
801 }
802 
ParseXcodeSDK(CompileUnit & comp_unit)803 XcodeSDK SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
804   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
805   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
806   if (!dwarf_cu)
807     return {};
808   const DWARFBaseDIE cu_die = dwarf_cu->GetNonSkeletonUnit().GetUnitDIEOnly();
809   if (!cu_die)
810     return {};
811   const char *sdk = cu_die.GetAttributeValueAsString(DW_AT_APPLE_sdk, nullptr);
812   if (!sdk)
813     return {};
814   const char *sysroot =
815       cu_die.GetAttributeValueAsString(DW_AT_LLVM_sysroot, "");
816   // Register the sysroot path remapping with the module belonging to
817   // the CU as well as the one belonging to the symbol file. The two
818   // would be different if this is an OSO object and module is the
819   // corresponding debug map, in which case both should be updated.
820   ModuleSP module_sp = comp_unit.GetModule();
821   if (module_sp)
822     module_sp->RegisterXcodeSDK(sdk, sysroot);
823 
824   ModuleSP local_module_sp = m_objfile_sp->GetModule();
825   if (local_module_sp && local_module_sp != module_sp)
826     local_module_sp->RegisterXcodeSDK(sdk, sysroot);
827 
828   return {sdk};
829 }
830 
ParseFunctions(CompileUnit & comp_unit)831 size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
832   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
833   Timer scoped_timer(func_cat, "SymbolFileDWARF::ParseFunctions");
834   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
835   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
836   if (!dwarf_cu)
837     return 0;
838 
839   size_t functions_added = 0;
840   dwarf_cu = &dwarf_cu->GetNonSkeletonUnit();
841   for (DWARFDebugInfoEntry &entry : dwarf_cu->dies()) {
842     if (entry.Tag() != DW_TAG_subprogram)
843       continue;
844 
845     DWARFDIE die(dwarf_cu, &entry);
846     if (comp_unit.FindFunctionByUID(die.GetID()))
847       continue;
848     if (ParseFunction(comp_unit, die))
849       ++functions_added;
850   }
851   // FixupTypes();
852   return functions_added;
853 }
854 
ForEachExternalModule(CompileUnit & comp_unit,llvm::DenseSet<lldb_private::SymbolFile * > & visited_symbol_files,llvm::function_ref<bool (Module &)> lambda)855 bool SymbolFileDWARF::ForEachExternalModule(
856     CompileUnit &comp_unit,
857     llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
858     llvm::function_ref<bool(Module &)> lambda) {
859   // Only visit each symbol file once.
860   if (!visited_symbol_files.insert(this).second)
861     return false;
862 
863   UpdateExternalModuleListIfNeeded();
864   for (auto &p : m_external_type_modules) {
865     ModuleSP module = p.second;
866     if (!module)
867       continue;
868 
869     // Invoke the action and potentially early-exit.
870     if (lambda(*module))
871       return true;
872 
873     for (std::size_t i = 0; i < module->GetNumCompileUnits(); ++i) {
874       auto cu = module->GetCompileUnitAtIndex(i);
875       bool early_exit = cu->ForEachExternalModule(visited_symbol_files, lambda);
876       if (early_exit)
877         return true;
878     }
879   }
880   return false;
881 }
882 
ParseSupportFiles(CompileUnit & comp_unit,FileSpecList & support_files)883 bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
884                                         FileSpecList &support_files) {
885   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
886   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
887   if (!dwarf_cu)
888     return false;
889 
890   dw_offset_t offset = dwarf_cu->GetLineTableOffset();
891   if (offset == DW_INVALID_OFFSET)
892     return false;
893 
894   llvm::DWARFDebugLine::Prologue prologue;
895   if (!ParseLLVMLineTablePrologue(m_context, prologue, offset,
896                                   dwarf_cu->GetOffset()))
897     return false;
898 
899   comp_unit.SetSupportFiles(ParseSupportFilesFromPrologue(
900       comp_unit.GetModule(), prologue, dwarf_cu->GetPathStyle(),
901       dwarf_cu->GetCompilationDirectory().GetCString()));
902 
903   return true;
904 }
905 
GetFile(DWARFUnit & unit,size_t file_idx)906 FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) {
907   if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) {
908     if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu))
909       return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx);
910     return FileSpec();
911   }
912 
913   auto &tu = llvm::cast<DWARFTypeUnit>(unit);
914   return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx);
915 }
916 
917 const FileSpecList &
GetTypeUnitSupportFiles(DWARFTypeUnit & tu)918 SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) {
919   static FileSpecList empty_list;
920 
921   dw_offset_t offset = tu.GetLineTableOffset();
922   if (offset == DW_INVALID_OFFSET ||
923       offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() ||
924       offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey())
925     return empty_list;
926 
927   // Many type units can share a line table, so parse the support file list
928   // once, and cache it based on the offset field.
929   auto iter_bool = m_type_unit_support_files.try_emplace(offset);
930   FileSpecList &list = iter_bool.first->second;
931   if (iter_bool.second) {
932     uint64_t line_table_offset = offset;
933     llvm::DWARFDataExtractor data = m_context.getOrLoadLineData().GetAsLLVM();
934     llvm::DWARFContext &ctx = m_context.GetAsLLVM();
935     llvm::DWARFDebugLine::Prologue prologue;
936     auto report = [](llvm::Error error) {
937       Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
938       LLDB_LOG_ERROR(log, std::move(error),
939                      "SymbolFileDWARF::GetTypeUnitSupportFiles failed to parse "
940                      "the line table prologue");
941     };
942     llvm::Error error = prologue.parse(data, &line_table_offset, report, ctx);
943     if (error) {
944       report(std::move(error));
945     } else {
946       list = ParseSupportFilesFromPrologue(GetObjectFile()->GetModule(),
947                                            prologue, tu.GetPathStyle());
948     }
949   }
950   return list;
951 }
952 
ParseIsOptimized(CompileUnit & comp_unit)953 bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
954   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
955   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
956   if (dwarf_cu)
957     return dwarf_cu->GetIsOptimized();
958   return false;
959 }
960 
ParseImportedModules(const lldb_private::SymbolContext & sc,std::vector<SourceModule> & imported_modules)961 bool SymbolFileDWARF::ParseImportedModules(
962     const lldb_private::SymbolContext &sc,
963     std::vector<SourceModule> &imported_modules) {
964   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
965   assert(sc.comp_unit);
966   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
967   if (!dwarf_cu)
968     return false;
969   if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
970           sc.comp_unit->GetLanguage()))
971     return false;
972   UpdateExternalModuleListIfNeeded();
973 
974   const DWARFDIE die = dwarf_cu->DIE();
975   if (!die)
976     return false;
977 
978   for (DWARFDIE child_die = die.GetFirstChild(); child_die;
979        child_die = child_die.GetSibling()) {
980     if (child_die.Tag() != DW_TAG_imported_declaration)
981       continue;
982 
983     DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
984     if (module_die.Tag() != DW_TAG_module)
985       continue;
986 
987     if (const char *name =
988             module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
989       SourceModule module;
990       module.path.push_back(ConstString(name));
991 
992       DWARFDIE parent_die = module_die;
993       while ((parent_die = parent_die.GetParent())) {
994         if (parent_die.Tag() != DW_TAG_module)
995           break;
996         if (const char *name =
997                 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
998           module.path.push_back(ConstString(name));
999       }
1000       std::reverse(module.path.begin(), module.path.end());
1001       if (const char *include_path = module_die.GetAttributeValueAsString(
1002               DW_AT_LLVM_include_path, nullptr)) {
1003         FileSpec include_spec(include_path, dwarf_cu->GetPathStyle());
1004         MakeAbsoluteAndRemap(include_spec, *dwarf_cu, m_objfile_sp->GetModule());
1005         module.search_path = ConstString(include_spec.GetPath());
1006       }
1007       if (const char *sysroot = dwarf_cu->DIE().GetAttributeValueAsString(
1008               DW_AT_LLVM_sysroot, nullptr))
1009         module.sysroot = ConstString(sysroot);
1010       imported_modules.push_back(module);
1011     }
1012   }
1013   return true;
1014 }
1015 
ParseLineTable(CompileUnit & comp_unit)1016 bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
1017   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1018   if (comp_unit.GetLineTable() != nullptr)
1019     return true;
1020 
1021   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
1022   if (!dwarf_cu)
1023     return false;
1024 
1025   dw_offset_t offset = dwarf_cu->GetLineTableOffset();
1026   if (offset == DW_INVALID_OFFSET)
1027     return false;
1028 
1029   llvm::DWARFDebugLine line;
1030   const llvm::DWARFDebugLine::LineTable *line_table =
1031       ParseLLVMLineTable(m_context, line, offset, dwarf_cu->GetOffset());
1032 
1033   if (!line_table)
1034     return false;
1035 
1036   // FIXME: Rather than parsing the whole line table and then copying it over
1037   // into LLDB, we should explore using a callback to populate the line table
1038   // while we parse to reduce memory usage.
1039   std::vector<std::unique_ptr<LineSequence>> sequences;
1040   // The Sequences view contains only valid line sequences. Don't iterate over
1041   // the Rows directly.
1042   for (const llvm::DWARFDebugLine::Sequence &seq : line_table->Sequences) {
1043     std::unique_ptr<LineSequence> sequence =
1044         LineTable::CreateLineSequenceContainer();
1045     for (unsigned idx = seq.FirstRowIndex; idx < seq.LastRowIndex; ++idx) {
1046       const llvm::DWARFDebugLine::Row &row = line_table->Rows[idx];
1047       LineTable::AppendLineEntryToSequence(
1048           sequence.get(), row.Address.Address, row.Line, row.Column, row.File,
1049           row.IsStmt, row.BasicBlock, row.PrologueEnd, row.EpilogueBegin,
1050           row.EndSequence);
1051     }
1052     sequences.push_back(std::move(sequence));
1053   }
1054 
1055   std::unique_ptr<LineTable> line_table_up =
1056       std::make_unique<LineTable>(&comp_unit, std::move(sequences));
1057 
1058   if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile()) {
1059     // We have an object file that has a line table with addresses that are not
1060     // linked. We need to link the line table and convert the addresses that
1061     // are relative to the .o file into addresses for the main executable.
1062     comp_unit.SetLineTable(
1063         debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
1064   } else {
1065     comp_unit.SetLineTable(line_table_up.release());
1066   }
1067 
1068   return true;
1069 }
1070 
1071 lldb_private::DebugMacrosSP
ParseDebugMacros(lldb::offset_t * offset)1072 SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
1073   auto iter = m_debug_macros_map.find(*offset);
1074   if (iter != m_debug_macros_map.end())
1075     return iter->second;
1076 
1077   const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
1078   if (debug_macro_data.GetByteSize() == 0)
1079     return DebugMacrosSP();
1080 
1081   lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
1082   m_debug_macros_map[*offset] = debug_macros_sp;
1083 
1084   const DWARFDebugMacroHeader &header =
1085       DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
1086   DWARFDebugMacroEntry::ReadMacroEntries(
1087       debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
1088       offset, this, debug_macros_sp);
1089 
1090   return debug_macros_sp;
1091 }
1092 
ParseDebugMacros(CompileUnit & comp_unit)1093 bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
1094   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1095 
1096   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
1097   if (dwarf_cu == nullptr)
1098     return false;
1099 
1100   const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
1101   if (!dwarf_cu_die)
1102     return false;
1103 
1104   lldb::offset_t sect_offset =
1105       dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
1106   if (sect_offset == DW_INVALID_OFFSET)
1107     sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
1108                                                            DW_INVALID_OFFSET);
1109   if (sect_offset == DW_INVALID_OFFSET)
1110     return false;
1111 
1112   comp_unit.SetDebugMacros(ParseDebugMacros(&sect_offset));
1113 
1114   return true;
1115 }
1116 
ParseBlocksRecursive(lldb_private::CompileUnit & comp_unit,Block * parent_block,const DWARFDIE & orig_die,addr_t subprogram_low_pc,uint32_t depth)1117 size_t SymbolFileDWARF::ParseBlocksRecursive(
1118     lldb_private::CompileUnit &comp_unit, Block *parent_block,
1119     const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) {
1120   size_t blocks_added = 0;
1121   DWARFDIE die = orig_die;
1122   while (die) {
1123     dw_tag_t tag = die.Tag();
1124 
1125     switch (tag) {
1126     case DW_TAG_inlined_subroutine:
1127     case DW_TAG_subprogram:
1128     case DW_TAG_lexical_block: {
1129       Block *block = nullptr;
1130       if (tag == DW_TAG_subprogram) {
1131         // Skip any DW_TAG_subprogram DIEs that are inside of a normal or
1132         // inlined functions. These will be parsed on their own as separate
1133         // entities.
1134 
1135         if (depth > 0)
1136           break;
1137 
1138         block = parent_block;
1139       } else {
1140         BlockSP block_sp(new Block(die.GetID()));
1141         parent_block->AddChild(block_sp);
1142         block = block_sp.get();
1143       }
1144       DWARFRangeList ranges;
1145       const char *name = nullptr;
1146       const char *mangled_name = nullptr;
1147 
1148       int decl_file = 0;
1149       int decl_line = 0;
1150       int decl_column = 0;
1151       int call_file = 0;
1152       int call_line = 0;
1153       int call_column = 0;
1154       if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file,
1155                                    decl_line, decl_column, call_file, call_line,
1156                                    call_column, nullptr)) {
1157         if (tag == DW_TAG_subprogram) {
1158           assert(subprogram_low_pc == LLDB_INVALID_ADDRESS);
1159           subprogram_low_pc = ranges.GetMinRangeBase(0);
1160         } else if (tag == DW_TAG_inlined_subroutine) {
1161           // We get called here for inlined subroutines in two ways. The first
1162           // time is when we are making the Function object for this inlined
1163           // concrete instance.  Since we're creating a top level block at
1164           // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS.  So we
1165           // need to adjust the containing address. The second time is when we
1166           // are parsing the blocks inside the function that contains the
1167           // inlined concrete instance.  Since these will be blocks inside the
1168           // containing "real" function the offset will be for that function.
1169           if (subprogram_low_pc == LLDB_INVALID_ADDRESS) {
1170             subprogram_low_pc = ranges.GetMinRangeBase(0);
1171           }
1172         }
1173 
1174         const size_t num_ranges = ranges.GetSize();
1175         for (size_t i = 0; i < num_ranges; ++i) {
1176           const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
1177           const addr_t range_base = range.GetRangeBase();
1178           if (range_base >= subprogram_low_pc)
1179             block->AddRange(Block::Range(range_base - subprogram_low_pc,
1180                                          range.GetByteSize()));
1181           else {
1182             GetObjectFile()->GetModule()->ReportError(
1183                 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64
1184                 ") which has a base that is less than the function's low PC "
1185                 "0x%" PRIx64 ". Please file a bug and attach the file at the "
1186                 "start of this error message",
1187                 block->GetID(), range_base, range.GetRangeEnd(),
1188                 subprogram_low_pc);
1189           }
1190         }
1191         block->FinalizeRanges();
1192 
1193         if (tag != DW_TAG_subprogram &&
1194             (name != nullptr || mangled_name != nullptr)) {
1195           std::unique_ptr<Declaration> decl_up;
1196           if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1197             decl_up = std::make_unique<Declaration>(
1198                 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
1199                 decl_line, decl_column);
1200 
1201           std::unique_ptr<Declaration> call_up;
1202           if (call_file != 0 || call_line != 0 || call_column != 0)
1203             call_up = std::make_unique<Declaration>(
1204                 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file),
1205                 call_line, call_column);
1206 
1207           block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(),
1208                                         call_up.get());
1209         }
1210 
1211         ++blocks_added;
1212 
1213         if (die.HasChildren()) {
1214           blocks_added +=
1215               ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(),
1216                                    subprogram_low_pc, depth + 1);
1217         }
1218       }
1219     } break;
1220     default:
1221       break;
1222     }
1223 
1224     // Only parse siblings of the block if we are not at depth zero. A depth of
1225     // zero indicates we are currently parsing the top level DW_TAG_subprogram
1226     // DIE
1227 
1228     if (depth == 0)
1229       die.Clear();
1230     else
1231       die = die.GetSibling();
1232   }
1233   return blocks_added;
1234 }
1235 
ClassOrStructIsVirtual(const DWARFDIE & parent_die)1236 bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) {
1237   if (parent_die) {
1238     for (DWARFDIE die = parent_die.GetFirstChild(); die;
1239          die = die.GetSibling()) {
1240       dw_tag_t tag = die.Tag();
1241       bool check_virtuality = false;
1242       switch (tag) {
1243       case DW_TAG_inheritance:
1244       case DW_TAG_subprogram:
1245         check_virtuality = true;
1246         break;
1247       default:
1248         break;
1249       }
1250       if (check_virtuality) {
1251         if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0)
1252           return true;
1253       }
1254     }
1255   }
1256   return false;
1257 }
1258 
ParseDeclsForContext(CompilerDeclContext decl_ctx)1259 void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) {
1260   auto *type_system = decl_ctx.GetTypeSystem();
1261   if (type_system != nullptr)
1262     type_system->GetDWARFParser()->EnsureAllDIEsInDeclContextHaveBeenParsed(
1263         decl_ctx);
1264 }
1265 
GetUID(DIERef ref)1266 user_id_t SymbolFileDWARF::GetUID(DIERef ref) {
1267   if (GetDebugMapSymfile())
1268     return GetID() | ref.die_offset();
1269 
1270   lldbassert(GetDwoNum().getValueOr(0) <= 0x3fffffff);
1271   return user_id_t(GetDwoNum().getValueOr(0)) << 32 | ref.die_offset() |
1272          lldb::user_id_t(GetDwoNum().hasValue()) << 62 |
1273          lldb::user_id_t(ref.section() == DIERef::Section::DebugTypes) << 63;
1274 }
1275 
1276 llvm::Optional<SymbolFileDWARF::DecodedUID>
DecodeUID(lldb::user_id_t uid)1277 SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
1278   // This method can be called without going through the symbol vendor so we
1279   // need to lock the module.
1280   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1281   // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we
1282   // must make sure we use the correct DWARF file when resolving things. On
1283   // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple
1284   // SymbolFileDWARF classes, one for each .o file. We can often end up with
1285   // references to other DWARF objects and we must be ready to receive a
1286   // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF
1287   // instance.
1288   if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
1289     SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
1290         debug_map->GetOSOIndexFromUserID(uid));
1291     return DecodedUID{
1292         *dwarf, {llvm::None, DIERef::Section::DebugInfo, dw_offset_t(uid)}};
1293   }
1294   dw_offset_t die_offset = uid;
1295   if (die_offset == DW_INVALID_OFFSET)
1296     return llvm::None;
1297 
1298   DIERef::Section section =
1299       uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
1300 
1301   llvm::Optional<uint32_t> dwo_num;
1302   bool dwo_valid = uid >> 62 & 1;
1303   if (dwo_valid)
1304     dwo_num = uid >> 32 & 0x3fffffff;
1305 
1306   return DecodedUID{*this, {dwo_num, section, die_offset}};
1307 }
1308 
1309 DWARFDIE
GetDIE(lldb::user_id_t uid)1310 SymbolFileDWARF::GetDIE(lldb::user_id_t uid) {
1311   // This method can be called without going through the symbol vendor so we
1312   // need to lock the module.
1313   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1314 
1315   llvm::Optional<DecodedUID> decoded = DecodeUID(uid);
1316 
1317   if (decoded)
1318     return decoded->dwarf.GetDIE(decoded->ref);
1319 
1320   return DWARFDIE();
1321 }
1322 
GetDeclForUID(lldb::user_id_t type_uid)1323 CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) {
1324   // This method can be called without going through the symbol vendor so we
1325   // need to lock the module.
1326   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1327   // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1328   // SymbolFileDWARF::GetDIE(). See comments inside the
1329   // SymbolFileDWARF::GetDIE() for details.
1330   if (DWARFDIE die = GetDIE(type_uid))
1331     return GetDecl(die);
1332   return CompilerDecl();
1333 }
1334 
1335 CompilerDeclContext
GetDeclContextForUID(lldb::user_id_t type_uid)1336 SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) {
1337   // This method can be called without going through the symbol vendor so we
1338   // need to lock the module.
1339   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1340   // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1341   // SymbolFileDWARF::GetDIE(). See comments inside the
1342   // SymbolFileDWARF::GetDIE() for details.
1343   if (DWARFDIE die = GetDIE(type_uid))
1344     return GetDeclContext(die);
1345   return CompilerDeclContext();
1346 }
1347 
1348 CompilerDeclContext
GetDeclContextContainingUID(lldb::user_id_t type_uid)1349 SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1350   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1351   // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1352   // SymbolFileDWARF::GetDIE(). See comments inside the
1353   // SymbolFileDWARF::GetDIE() for details.
1354   if (DWARFDIE die = GetDIE(type_uid))
1355     return GetContainingDeclContext(die);
1356   return CompilerDeclContext();
1357 }
1358 
ResolveTypeUID(lldb::user_id_t type_uid)1359 Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
1360   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1361   // Anytime we have a lldb::user_id_t, we must get the DIE by calling
1362   // SymbolFileDWARF::GetDIE(). See comments inside the
1363   // SymbolFileDWARF::GetDIE() for details.
1364   if (DWARFDIE type_die = GetDIE(type_uid))
1365     return type_die.ResolveType();
1366   else
1367     return nullptr;
1368 }
1369 
1370 llvm::Optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)1371 SymbolFileDWARF::GetDynamicArrayInfoForUID(
1372     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1373   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1374   if (DWARFDIE type_die = GetDIE(type_uid))
1375     return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx);
1376   else
1377     return llvm::None;
1378 }
1379 
ResolveTypeUID(const DIERef & die_ref)1380 Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) {
1381   return ResolveType(GetDIE(die_ref), true);
1382 }
1383 
ResolveTypeUID(const DWARFDIE & die,bool assert_not_being_parsed)1384 Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
1385                                       bool assert_not_being_parsed) {
1386   if (die) {
1387     Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1388     if (log)
1389       GetObjectFile()->GetModule()->LogMessage(
1390           log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1391           die.GetOffset(), die.GetTagAsCString(), die.GetName());
1392 
1393     // We might be coming in in the middle of a type tree (a class within a
1394     // class, an enum within a class), so parse any needed parent DIEs before
1395     // we get to this one...
1396     DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die);
1397     if (decl_ctx_die) {
1398       if (log) {
1399         switch (decl_ctx_die.Tag()) {
1400         case DW_TAG_structure_type:
1401         case DW_TAG_union_type:
1402         case DW_TAG_class_type: {
1403           // Get the type, which could be a forward declaration
1404           if (log)
1405             GetObjectFile()->GetModule()->LogMessage(
1406                 log,
1407                 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' "
1408                 "resolve parent forward type for 0x%8.8x",
1409                 die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1410                 decl_ctx_die.GetOffset());
1411         } break;
1412 
1413         default:
1414           break;
1415         }
1416       }
1417     }
1418     return ResolveType(die);
1419   }
1420   return nullptr;
1421 }
1422 
1423 // This function is used when SymbolFileDWARFDebugMap owns a bunch of
1424 // SymbolFileDWARF objects to detect if this DWARF file is the one that can
1425 // resolve a compiler_type.
HasForwardDeclForClangType(const CompilerType & compiler_type)1426 bool SymbolFileDWARF::HasForwardDeclForClangType(
1427     const CompilerType &compiler_type) {
1428   CompilerType compiler_type_no_qualifiers =
1429       ClangUtil::RemoveFastQualifiers(compiler_type);
1430   if (GetForwardDeclClangTypeToDie().count(
1431           compiler_type_no_qualifiers.GetOpaqueQualType())) {
1432     return true;
1433   }
1434   TypeSystem *type_system = compiler_type.GetTypeSystem();
1435 
1436   TypeSystemClang *clang_type_system =
1437       llvm::dyn_cast_or_null<TypeSystemClang>(type_system);
1438   if (!clang_type_system)
1439     return false;
1440   DWARFASTParserClang *ast_parser =
1441       static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1442   return ast_parser->GetClangASTImporter().CanImport(compiler_type);
1443 }
1444 
CompleteType(CompilerType & compiler_type)1445 bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
1446   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1447 
1448   TypeSystemClang *clang_type_system =
1449       llvm::dyn_cast_or_null<TypeSystemClang>(compiler_type.GetTypeSystem());
1450   if (clang_type_system) {
1451     DWARFASTParserClang *ast_parser =
1452         static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
1453     if (ast_parser &&
1454         ast_parser->GetClangASTImporter().CanImport(compiler_type))
1455       return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
1456   }
1457 
1458   // We have a struct/union/class/enum that needs to be fully resolved.
1459   CompilerType compiler_type_no_qualifiers =
1460       ClangUtil::RemoveFastQualifiers(compiler_type);
1461   auto die_it = GetForwardDeclClangTypeToDie().find(
1462       compiler_type_no_qualifiers.GetOpaqueQualType());
1463   if (die_it == GetForwardDeclClangTypeToDie().end()) {
1464     // We have already resolved this type...
1465     return true;
1466   }
1467 
1468   DWARFDIE dwarf_die = GetDIE(die_it->getSecond());
1469   if (dwarf_die) {
1470     // Once we start resolving this type, remove it from the forward
1471     // declaration map in case anyone child members or other types require this
1472     // type to get resolved. The type will get resolved when all of the calls
1473     // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done.
1474     GetForwardDeclClangTypeToDie().erase(die_it);
1475 
1476     Type *type = GetDIEToType().lookup(dwarf_die.GetDIE());
1477 
1478     Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
1479                                           DWARF_LOG_TYPE_COMPLETION));
1480     if (log)
1481       GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1482           log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
1483           dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1484           type->GetName().AsCString());
1485     assert(compiler_type);
1486     if (DWARFASTParser *dwarf_ast = GetDWARFParser(*dwarf_die.GetCU()))
1487       return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
1488   }
1489   return false;
1490 }
1491 
ResolveType(const DWARFDIE & die,bool assert_not_being_parsed,bool resolve_function_context)1492 Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
1493                                    bool assert_not_being_parsed,
1494                                    bool resolve_function_context) {
1495   if (die) {
1496     Type *type = GetTypeForDIE(die, resolve_function_context).get();
1497 
1498     if (assert_not_being_parsed) {
1499       if (type != DIE_IS_BEING_PARSED)
1500         return type;
1501 
1502       GetObjectFile()->GetModule()->ReportError(
1503           "Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1504           die.GetOffset(), die.GetTagAsCString(), die.GetName());
1505 
1506     } else
1507       return type;
1508   }
1509   return nullptr;
1510 }
1511 
1512 CompileUnit *
GetCompUnitForDWARFCompUnit(DWARFCompileUnit & dwarf_cu)1513 SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
1514   if (dwarf_cu.IsDWOUnit()) {
1515     DWARFCompileUnit *non_dwo_cu =
1516         static_cast<DWARFCompileUnit *>(dwarf_cu.GetUserData());
1517     assert(non_dwo_cu);
1518     return non_dwo_cu->GetSymbolFileDWARF().GetCompUnitForDWARFCompUnit(
1519         *non_dwo_cu);
1520   }
1521   // Check if the symbol vendor already knows about this compile unit?
1522   if (dwarf_cu.GetUserData() == nullptr) {
1523     // The symbol vendor doesn't know about this compile unit, we need to parse
1524     // and add it to the symbol vendor object.
1525     return ParseCompileUnit(dwarf_cu).get();
1526   }
1527   return static_cast<CompileUnit *>(dwarf_cu.GetUserData());
1528 }
1529 
GetObjCMethods(ConstString class_name,llvm::function_ref<bool (DWARFDIE die)> callback)1530 void SymbolFileDWARF::GetObjCMethods(
1531     ConstString class_name, llvm::function_ref<bool(DWARFDIE die)> callback) {
1532   m_index->GetObjCMethods(class_name, callback);
1533 }
1534 
GetFunction(const DWARFDIE & die,SymbolContext & sc)1535 bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) {
1536   sc.Clear(false);
1537 
1538   if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) {
1539     // Check if the symbol vendor already knows about this compile unit?
1540     sc.comp_unit =
1541         GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU()));
1542 
1543     sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
1544     if (sc.function == nullptr)
1545       sc.function = ParseFunction(*sc.comp_unit, die);
1546 
1547     if (sc.function) {
1548       sc.module_sp = sc.function->CalculateSymbolContextModule();
1549       return true;
1550     }
1551   }
1552 
1553   return false;
1554 }
1555 
GetExternalModule(ConstString name)1556 lldb::ModuleSP SymbolFileDWARF::GetExternalModule(ConstString name) {
1557   UpdateExternalModuleListIfNeeded();
1558   const auto &pos = m_external_type_modules.find(name);
1559   if (pos != m_external_type_modules.end())
1560     return pos->second;
1561   else
1562     return lldb::ModuleSP();
1563 }
1564 
1565 DWARFDIE
GetDIE(const DIERef & die_ref)1566 SymbolFileDWARF::GetDIE(const DIERef &die_ref) {
1567   if (die_ref.dwo_num()) {
1568     SymbolFileDWARF *dwarf = *die_ref.dwo_num() == 0x3fffffff
1569                                  ? m_dwp_symfile.get()
1570                                  : this->DebugInfo()
1571                                        .GetUnitAtIndex(*die_ref.dwo_num())
1572                                        ->GetDwoSymbolFile();
1573     return dwarf->DebugInfo().GetDIE(die_ref);
1574   }
1575 
1576   return DebugInfo().GetDIE(die_ref);
1577 }
1578 
1579 /// Return the DW_AT_(GNU_)dwo_name.
GetDWOName(DWARFCompileUnit & dwarf_cu,const DWARFDebugInfoEntry & cu_die)1580 static const char *GetDWOName(DWARFCompileUnit &dwarf_cu,
1581                               const DWARFDebugInfoEntry &cu_die) {
1582   const char *dwo_name =
1583       cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
1584   if (!dwo_name)
1585     dwo_name =
1586         cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_dwo_name, nullptr);
1587   return dwo_name;
1588 }
1589 
1590 /// Return the DW_AT_(GNU_)dwo_id.
1591 /// FIXME: Technically 0 is a valid hash.
GetDWOId(DWARFCompileUnit & dwarf_cu,const DWARFDebugInfoEntry & cu_die)1592 static uint64_t GetDWOId(DWARFCompileUnit &dwarf_cu,
1593                          const DWARFDebugInfoEntry &cu_die) {
1594   uint64_t dwo_id =
1595       cu_die.GetAttributeValueAsUnsigned(&dwarf_cu, DW_AT_GNU_dwo_id, 0);
1596   if (!dwo_id)
1597     dwo_id = cu_die.GetAttributeValueAsUnsigned(&dwarf_cu, DW_AT_dwo_id, 0);
1598   return dwo_id;
1599 }
1600 
GetDWOId()1601 llvm::Optional<uint64_t> SymbolFileDWARF::GetDWOId() {
1602   if (GetNumCompileUnits() == 1) {
1603     if (auto comp_unit = GetCompileUnitAtIndex(0))
1604       if (DWARFCompileUnit *cu = GetDWARFCompileUnit(comp_unit.get()))
1605         if (DWARFDebugInfoEntry *cu_die = cu->DIE().GetDIE())
1606           if (uint64_t dwo_id = ::GetDWOId(*cu, *cu_die))
1607             return dwo_id;
1608   }
1609   return {};
1610 }
1611 
1612 std::shared_ptr<SymbolFileDWARFDwo>
GetDwoSymbolFileForCompileUnit(DWARFUnit & unit,const DWARFDebugInfoEntry & cu_die)1613 SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
1614     DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) {
1615   // If this is a Darwin-style debug map (non-.dSYM) symbol file,
1616   // never attempt to load ELF-style DWO files since the -gmodules
1617   // support uses the same DWO machanism to specify full debug info
1618   // files for modules. This is handled in
1619   // UpdateExternalModuleListIfNeeded().
1620   if (GetDebugMapSymfile())
1621     return nullptr;
1622 
1623   DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit);
1624   // Only compile units can be split into two parts.
1625   if (!dwarf_cu)
1626     return nullptr;
1627 
1628   const char *dwo_name = GetDWOName(*dwarf_cu, cu_die);
1629   if (!dwo_name)
1630     return nullptr;
1631 
1632   if (std::shared_ptr<SymbolFileDWARFDwo> dwp_sp = GetDwpSymbolFile())
1633     return dwp_sp;
1634 
1635   FileSpec dwo_file(dwo_name);
1636   FileSystem::Instance().Resolve(dwo_file);
1637   if (dwo_file.IsRelative()) {
1638     const char *comp_dir =
1639         cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr);
1640     if (!comp_dir)
1641       return nullptr;
1642 
1643     dwo_file.SetFile(comp_dir, FileSpec::Style::native);
1644     FileSystem::Instance().Resolve(dwo_file);
1645     dwo_file.AppendPathComponent(dwo_name);
1646   }
1647 
1648   if (!FileSystem::Instance().Exists(dwo_file))
1649     return nullptr;
1650 
1651   const lldb::offset_t file_offset = 0;
1652   DataBufferSP dwo_file_data_sp;
1653   lldb::offset_t dwo_file_data_offset = 0;
1654   ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin(
1655       GetObjectFile()->GetModule(), &dwo_file, file_offset,
1656       FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp,
1657       dwo_file_data_offset);
1658   if (dwo_obj_file == nullptr)
1659     return nullptr;
1660 
1661   return std::make_shared<SymbolFileDWARFDwo>(*this, dwo_obj_file,
1662                                               dwarf_cu->GetID());
1663 }
1664 
UpdateExternalModuleListIfNeeded()1665 void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
1666   if (m_fetched_external_modules)
1667     return;
1668   m_fetched_external_modules = true;
1669   DWARFDebugInfo &debug_info = DebugInfo();
1670 
1671   // Follow DWO skeleton unit breadcrumbs.
1672   const uint32_t num_compile_units = GetNumCompileUnits();
1673   for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
1674     auto *dwarf_cu =
1675         llvm::dyn_cast<DWARFCompileUnit>(debug_info.GetUnitAtIndex(cu_idx));
1676     if (!dwarf_cu)
1677       continue;
1678 
1679     const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly();
1680     if (!die || die.HasChildren() || !die.GetDIE())
1681       continue;
1682 
1683     const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr);
1684     if (!name)
1685       continue;
1686 
1687     ConstString const_name(name);
1688     ModuleSP &module_sp = m_external_type_modules[const_name];
1689     if (module_sp)
1690       continue;
1691 
1692     const char *dwo_path = GetDWOName(*dwarf_cu, *die.GetDIE());
1693     if (!dwo_path)
1694       continue;
1695 
1696     ModuleSpec dwo_module_spec;
1697     dwo_module_spec.GetFileSpec().SetFile(dwo_path, FileSpec::Style::native);
1698     if (dwo_module_spec.GetFileSpec().IsRelative()) {
1699       const char *comp_dir =
1700           die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr);
1701       if (comp_dir) {
1702         dwo_module_spec.GetFileSpec().SetFile(comp_dir,
1703                                               FileSpec::Style::native);
1704         FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec());
1705         dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path);
1706       }
1707     }
1708     dwo_module_spec.GetArchitecture() =
1709         m_objfile_sp->GetModule()->GetArchitecture();
1710 
1711     // When LLDB loads "external" modules it looks at the presence of
1712     // DW_AT_dwo_name. However, when the already created module
1713     // (corresponding to .dwo itself) is being processed, it will see
1714     // the presence of DW_AT_dwo_name (which contains the name of dwo
1715     // file) and will try to call ModuleList::GetSharedModule
1716     // again. In some cases (i.e., for empty files) Clang 4.0
1717     // generates a *.dwo file which has DW_AT_dwo_name, but no
1718     // DW_AT_comp_dir. In this case the method
1719     // ModuleList::GetSharedModule will fail and the warning will be
1720     // printed. However, as one can notice in this case we don't
1721     // actually need to try to load the already loaded module
1722     // (corresponding to .dwo) so we simply skip it.
1723     if (m_objfile_sp->GetFileSpec().GetFileNameExtension() == ".dwo" &&
1724         llvm::StringRef(m_objfile_sp->GetFileSpec().GetPath())
1725             .endswith(dwo_module_spec.GetFileSpec().GetPath())) {
1726       continue;
1727     }
1728 
1729     Status error = ModuleList::GetSharedModule(dwo_module_spec, module_sp,
1730                                                nullptr, nullptr, nullptr);
1731     if (!module_sp) {
1732       GetObjectFile()->GetModule()->ReportWarning(
1733           "0x%8.8x: unable to locate module needed for external types: "
1734           "%s\nerror: %s\nDebugging will be degraded due to missing "
1735           "types. Rebuilding the project will regenerate the needed "
1736           "module files.",
1737           die.GetOffset(), dwo_module_spec.GetFileSpec().GetPath().c_str(),
1738           error.AsCString("unknown error"));
1739       continue;
1740     }
1741 
1742     // Verify the DWO hash.
1743     // FIXME: Technically "0" is a valid hash.
1744     uint64_t dwo_id = ::GetDWOId(*dwarf_cu, *die.GetDIE());
1745     if (!dwo_id)
1746       continue;
1747 
1748     auto *dwo_symfile =
1749         llvm::dyn_cast_or_null<SymbolFileDWARF>(module_sp->GetSymbolFile());
1750     if (!dwo_symfile)
1751       continue;
1752     llvm::Optional<uint64_t> dwo_dwo_id = dwo_symfile->GetDWOId();
1753     if (!dwo_dwo_id)
1754       continue;
1755 
1756     if (dwo_id != dwo_dwo_id) {
1757       GetObjectFile()->GetModule()->ReportWarning(
1758           "0x%8.8x: Module %s is out-of-date (hash mismatch). Type information "
1759           "from this module may be incomplete or inconsistent with the rest of "
1760           "the program. Rebuilding the project will regenerate the needed "
1761           "module files.",
1762           die.GetOffset(), dwo_module_spec.GetFileSpec().GetPath().c_str());
1763     }
1764   }
1765 }
1766 
GetGlobalAranges()1767 SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() {
1768   if (!m_global_aranges_up) {
1769     m_global_aranges_up = std::make_unique<GlobalVariableMap>();
1770 
1771     ModuleSP module_sp = GetObjectFile()->GetModule();
1772     if (module_sp) {
1773       const size_t num_cus = module_sp->GetNumCompileUnits();
1774       for (size_t i = 0; i < num_cus; ++i) {
1775         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
1776         if (cu_sp) {
1777           VariableListSP globals_sp = cu_sp->GetVariableList(true);
1778           if (globals_sp) {
1779             const size_t num_globals = globals_sp->GetSize();
1780             for (size_t g = 0; g < num_globals; ++g) {
1781               VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
1782               if (var_sp && !var_sp->GetLocationIsConstantValueData()) {
1783                 const DWARFExpression &location = var_sp->LocationExpression();
1784                 Value location_result;
1785                 Status error;
1786                 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr,
1787                                       nullptr, location_result, &error)) {
1788                   if (location_result.GetValueType() ==
1789                       Value::eValueTypeFileAddress) {
1790                     lldb::addr_t file_addr =
1791                         location_result.GetScalar().ULongLong();
1792                     lldb::addr_t byte_size = 1;
1793                     if (var_sp->GetType())
1794                       byte_size =
1795                           var_sp->GetType()->GetByteSize(nullptr).getValueOr(0);
1796                     m_global_aranges_up->Append(GlobalVariableMap::Entry(
1797                         file_addr, byte_size, var_sp.get()));
1798                   }
1799                 }
1800               }
1801             }
1802           }
1803         }
1804       }
1805     }
1806     m_global_aranges_up->Sort();
1807   }
1808   return *m_global_aranges_up;
1809 }
1810 
ResolveFunctionAndBlock(lldb::addr_t file_vm_addr,bool lookup_block,SymbolContext & sc)1811 void SymbolFileDWARF::ResolveFunctionAndBlock(lldb::addr_t file_vm_addr,
1812                                               bool lookup_block,
1813                                               SymbolContext &sc) {
1814   assert(sc.comp_unit);
1815   DWARFCompileUnit &cu =
1816       GetDWARFCompileUnit(sc.comp_unit)->GetNonSkeletonUnit();
1817   DWARFDIE function_die = cu.LookupAddress(file_vm_addr);
1818   DWARFDIE block_die;
1819   if (function_die) {
1820     sc.function = sc.comp_unit->FindFunctionByUID(function_die.GetID()).get();
1821     if (sc.function == nullptr)
1822       sc.function = ParseFunction(*sc.comp_unit, function_die);
1823 
1824     if (sc.function && lookup_block)
1825       block_die = function_die.LookupDeepestBlock(file_vm_addr);
1826   }
1827 
1828   if (!sc.function || ! lookup_block)
1829     return;
1830 
1831   Block &block = sc.function->GetBlock(true);
1832   if (block_die)
1833     sc.block = block.FindBlockByID(block_die.GetID());
1834   else
1835     sc.block = block.FindBlockByID(function_die.GetID());
1836 }
1837 
ResolveSymbolContext(const Address & so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)1838 uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
1839                                                SymbolContextItem resolve_scope,
1840                                                SymbolContext &sc) {
1841   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1842   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1843   Timer scoped_timer(func_cat,
1844                      "SymbolFileDWARF::"
1845                      "ResolveSymbolContext (so_addr = { "
1846                      "section = %p, offset = 0x%" PRIx64
1847                      " }, resolve_scope = 0x%8.8x)",
1848                      static_cast<void *>(so_addr.GetSection().get()),
1849                      so_addr.GetOffset(), resolve_scope);
1850   uint32_t resolved = 0;
1851   if (resolve_scope &
1852       (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock |
1853        eSymbolContextLineEntry | eSymbolContextVariable)) {
1854     lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1855 
1856     DWARFDebugInfo &debug_info = DebugInfo();
1857     llvm::Expected<DWARFDebugAranges &> aranges =
1858         debug_info.GetCompileUnitAranges();
1859     if (!aranges) {
1860       Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
1861       LLDB_LOG_ERROR(log, aranges.takeError(),
1862                      "SymbolFileDWARF::ResolveSymbolContext failed to get cu "
1863                      "aranges.  {0}");
1864       return 0;
1865     }
1866 
1867     const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr);
1868     if (cu_offset == DW_INVALID_OFFSET) {
1869       // Global variables are not in the compile unit address ranges. The only
1870       // way to currently find global variables is to iterate over the
1871       // .debug_pubnames or the __apple_names table and find all items in there
1872       // that point to DW_TAG_variable DIEs and then find the address that
1873       // matches.
1874       if (resolve_scope & eSymbolContextVariable) {
1875         GlobalVariableMap &map = GetGlobalAranges();
1876         const GlobalVariableMap::Entry *entry =
1877             map.FindEntryThatContains(file_vm_addr);
1878         if (entry && entry->data) {
1879           Variable *variable = entry->data;
1880           SymbolContextScope *scc = variable->GetSymbolContextScope();
1881           if (scc) {
1882             scc->CalculateSymbolContext(&sc);
1883             sc.variable = variable;
1884           }
1885           return sc.GetResolvedMask();
1886         }
1887       }
1888     } else {
1889       uint32_t cu_idx = DW_INVALID_INDEX;
1890       if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>(
1891               debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, cu_offset,
1892                                          &cu_idx))) {
1893         sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
1894         if (sc.comp_unit) {
1895           resolved |= eSymbolContextCompUnit;
1896 
1897           bool force_check_line_table = false;
1898           if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
1899             ResolveFunctionAndBlock(file_vm_addr,
1900                                     resolve_scope & eSymbolContextBlock, sc);
1901             if (sc.function)
1902               resolved |= eSymbolContextFunction;
1903             else {
1904               // We might have had a compile unit that had discontiguous address
1905               // ranges where the gaps are symbols that don't have any debug
1906               // info. Discontiguous compile unit address ranges should only
1907               // happen when there aren't other functions from other compile
1908               // units in these gaps. This helps keep the size of the aranges
1909               // down.
1910               force_check_line_table = true;
1911             }
1912             if (sc.block)
1913               resolved |= eSymbolContextBlock;
1914           }
1915 
1916           if ((resolve_scope & eSymbolContextLineEntry) ||
1917               force_check_line_table) {
1918             LineTable *line_table = sc.comp_unit->GetLineTable();
1919             if (line_table != nullptr) {
1920               // And address that makes it into this function should be in terms
1921               // of this debug file if there is no debug map, or it will be an
1922               // address in the .o file which needs to be fixed up to be in
1923               // terms of the debug map executable. Either way, calling
1924               // FixupAddress() will work for us.
1925               Address exe_so_addr(so_addr);
1926               if (FixupAddress(exe_so_addr)) {
1927                 if (line_table->FindLineEntryByAddress(exe_so_addr,
1928                                                        sc.line_entry)) {
1929                   resolved |= eSymbolContextLineEntry;
1930                 }
1931               }
1932             }
1933           }
1934 
1935           if (force_check_line_table && !(resolved & eSymbolContextLineEntry)) {
1936             // We might have had a compile unit that had discontiguous address
1937             // ranges where the gaps are symbols that don't have any debug info.
1938             // Discontiguous compile unit address ranges should only happen when
1939             // there aren't other functions from other compile units in these
1940             // gaps. This helps keep the size of the aranges down.
1941             sc.comp_unit = nullptr;
1942             resolved &= ~eSymbolContextCompUnit;
1943           }
1944         } else {
1945           GetObjectFile()->GetModule()->ReportWarning(
1946               "0x%8.8x: compile unit %u failed to create a valid "
1947               "lldb_private::CompileUnit class.",
1948               cu_offset, cu_idx);
1949         }
1950       }
1951     }
1952   }
1953   return resolved;
1954 }
1955 
ResolveSymbolContext(const FileSpec & file_spec,uint32_t line,bool check_inlines,SymbolContextItem resolve_scope,SymbolContextList & sc_list)1956 uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec,
1957                                                uint32_t line,
1958                                                bool check_inlines,
1959                                                SymbolContextItem resolve_scope,
1960                                                SymbolContextList &sc_list) {
1961   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1962   const uint32_t prev_size = sc_list.GetSize();
1963   if (resolve_scope & eSymbolContextCompUnit) {
1964     for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus;
1965          ++cu_idx) {
1966       CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get();
1967       if (!dc_cu)
1968         continue;
1969 
1970       bool file_spec_matches_cu_file_spec =
1971           FileSpec::Match(file_spec, dc_cu->GetPrimaryFile());
1972       if (check_inlines || file_spec_matches_cu_file_spec) {
1973         SymbolContext sc(m_objfile_sp->GetModule());
1974         sc.comp_unit = dc_cu;
1975         uint32_t file_idx = UINT32_MAX;
1976 
1977         // If we are looking for inline functions only and we don't find it
1978         // in the support files, we are done.
1979         if (check_inlines) {
1980           file_idx =
1981               sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true);
1982           if (file_idx == UINT32_MAX)
1983             continue;
1984         }
1985 
1986         if (line != 0) {
1987           LineTable *line_table = sc.comp_unit->GetLineTable();
1988 
1989           if (line_table != nullptr && line != 0) {
1990             // We will have already looked up the file index if we are
1991             // searching for inline entries.
1992             if (!check_inlines)
1993               file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex(
1994                   1, file_spec, true);
1995 
1996             if (file_idx != UINT32_MAX) {
1997               uint32_t found_line;
1998               uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex(
1999                   0, file_idx, line, false, &sc.line_entry);
2000               found_line = sc.line_entry.line;
2001 
2002               while (line_idx != UINT32_MAX) {
2003                 sc.function = nullptr;
2004                 sc.block = nullptr;
2005                 if (resolve_scope &
2006                     (eSymbolContextFunction | eSymbolContextBlock)) {
2007                   const lldb::addr_t file_vm_addr =
2008                       sc.line_entry.range.GetBaseAddress().GetFileAddress();
2009                   if (file_vm_addr != LLDB_INVALID_ADDRESS) {
2010                     ResolveFunctionAndBlock(
2011                         file_vm_addr, resolve_scope & eSymbolContextBlock, sc);
2012                   }
2013                 }
2014 
2015                 sc_list.Append(sc);
2016                 line_idx = line_table->FindLineEntryIndexByFileIndex(
2017                     line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2018               }
2019             }
2020           } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2021             // only append the context if we aren't looking for inline call
2022             // sites by file and line and if the file spec matches that of
2023             // the compile unit
2024             sc_list.Append(sc);
2025           }
2026         } else if (file_spec_matches_cu_file_spec && !check_inlines) {
2027           // only append the context if we aren't looking for inline call
2028           // sites by file and line and if the file spec matches that of
2029           // the compile unit
2030           sc_list.Append(sc);
2031         }
2032 
2033         if (!check_inlines)
2034           break;
2035       }
2036     }
2037   }
2038   return sc_list.GetSize() - prev_size;
2039 }
2040 
PreloadSymbols()2041 void SymbolFileDWARF::PreloadSymbols() {
2042   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2043   m_index->Preload();
2044 }
2045 
GetModuleMutex() const2046 std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const {
2047   lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
2048   if (module_sp)
2049     return module_sp->GetMutex();
2050   return GetObjectFile()->GetModule()->GetMutex();
2051 }
2052 
DeclContextMatchesThisSymbolFile(const lldb_private::CompilerDeclContext & decl_ctx)2053 bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile(
2054     const lldb_private::CompilerDeclContext &decl_ctx) {
2055   if (!decl_ctx.IsValid()) {
2056     // Invalid namespace decl which means we aren't matching only things in
2057     // this symbol file, so return true to indicate it matches this symbol
2058     // file.
2059     return true;
2060   }
2061 
2062   TypeSystem *decl_ctx_type_system = decl_ctx.GetTypeSystem();
2063   auto type_system_or_err = GetTypeSystemForLanguage(
2064       decl_ctx_type_system->GetMinimumLanguage(nullptr));
2065   if (auto err = type_system_or_err.takeError()) {
2066     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
2067                    std::move(err),
2068                    "Unable to match namespace decl using TypeSystem");
2069     return false;
2070   }
2071 
2072   if (decl_ctx_type_system == &type_system_or_err.get())
2073     return true; // The type systems match, return true
2074 
2075   // The namespace AST was valid, and it does not match...
2076   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2077 
2078   if (log)
2079     GetObjectFile()->GetModule()->LogMessage(
2080         log, "Valid namespace does not match symbol file");
2081 
2082   return false;
2083 }
2084 
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,VariableList & variables)2085 void SymbolFileDWARF::FindGlobalVariables(
2086     ConstString name, const CompilerDeclContext &parent_decl_ctx,
2087     uint32_t max_matches, VariableList &variables) {
2088   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2089   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2090 
2091   if (log)
2092     GetObjectFile()->GetModule()->LogMessage(
2093         log,
2094         "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2095         "parent_decl_ctx=%p, max_matches=%u, variables)",
2096         name.GetCString(), static_cast<const void *>(&parent_decl_ctx),
2097         max_matches);
2098 
2099   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2100     return;
2101 
2102   // Remember how many variables are in the list before we search.
2103   const uint32_t original_size = variables.GetSize();
2104 
2105   llvm::StringRef basename;
2106   llvm::StringRef context;
2107   bool name_is_mangled = (bool)Mangled(name);
2108 
2109   if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(),
2110                                                       context, basename))
2111     basename = name.GetStringRef();
2112 
2113   // Loop invariant: Variables up to this index have been checked for context
2114   // matches.
2115   uint32_t pruned_idx = original_size;
2116 
2117   SymbolContext sc;
2118   m_index->GetGlobalVariables(ConstString(basename), [&](DWARFDIE die) {
2119     if (!sc.module_sp)
2120       sc.module_sp = m_objfile_sp->GetModule();
2121     assert(sc.module_sp);
2122 
2123     if (die.Tag() != DW_TAG_variable)
2124       return true;
2125 
2126     auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2127     if (!dwarf_cu)
2128       return true;
2129     sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2130 
2131     if (parent_decl_ctx) {
2132       if (DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU())) {
2133         CompilerDeclContext actual_parent_decl_ctx =
2134             dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
2135         if (!actual_parent_decl_ctx ||
2136             actual_parent_decl_ctx != parent_decl_ctx)
2137           return true;
2138       }
2139     }
2140 
2141     ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2142     while (pruned_idx < variables.GetSize()) {
2143       VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx);
2144       if (name_is_mangled ||
2145           var_sp->GetName().GetStringRef().contains(name.GetStringRef()))
2146         ++pruned_idx;
2147       else
2148         variables.RemoveVariableAtIndex(pruned_idx);
2149     }
2150 
2151     return variables.GetSize() - original_size < max_matches;
2152   });
2153 
2154   // Return the number of variable that were appended to the list
2155   const uint32_t num_matches = variables.GetSize() - original_size;
2156   if (log && num_matches > 0) {
2157     GetObjectFile()->GetModule()->LogMessage(
2158         log,
2159         "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", "
2160         "parent_decl_ctx=%p, max_matches=%u, variables) => %u",
2161         name.GetCString(), static_cast<const void *>(&parent_decl_ctx),
2162         max_matches, num_matches);
2163   }
2164 }
2165 
FindGlobalVariables(const RegularExpression & regex,uint32_t max_matches,VariableList & variables)2166 void SymbolFileDWARF::FindGlobalVariables(const RegularExpression &regex,
2167                                           uint32_t max_matches,
2168                                           VariableList &variables) {
2169   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2170   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2171 
2172   if (log) {
2173     GetObjectFile()->GetModule()->LogMessage(
2174         log,
2175         "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", "
2176         "max_matches=%u, variables)",
2177         regex.GetText().str().c_str(), max_matches);
2178   }
2179 
2180   // Remember how many variables are in the list before we search.
2181   const uint32_t original_size = variables.GetSize();
2182 
2183   SymbolContext sc;
2184   m_index->GetGlobalVariables(regex, [&](DWARFDIE die) {
2185     if (!sc.module_sp)
2186       sc.module_sp = m_objfile_sp->GetModule();
2187     assert(sc.module_sp);
2188 
2189     DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU());
2190     if (!dwarf_cu)
2191       return true;
2192     sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2193 
2194     ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables);
2195 
2196     return variables.GetSize() - original_size < max_matches;
2197   });
2198 }
2199 
ResolveFunction(const DWARFDIE & orig_die,bool include_inlines,SymbolContextList & sc_list)2200 bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
2201                                       bool include_inlines,
2202                                       SymbolContextList &sc_list) {
2203   SymbolContext sc;
2204 
2205   if (!orig_die)
2206     return false;
2207 
2208   // If we were passed a die that is not a function, just return false...
2209   if (!(orig_die.Tag() == DW_TAG_subprogram ||
2210         (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine)))
2211     return false;
2212 
2213   DWARFDIE die = orig_die;
2214   DWARFDIE inlined_die;
2215   if (die.Tag() == DW_TAG_inlined_subroutine) {
2216     inlined_die = die;
2217 
2218     while (true) {
2219       die = die.GetParent();
2220 
2221       if (die) {
2222         if (die.Tag() == DW_TAG_subprogram)
2223           break;
2224       } else
2225         break;
2226     }
2227   }
2228   assert(die && die.Tag() == DW_TAG_subprogram);
2229   if (GetFunction(die, sc)) {
2230     Address addr;
2231     // Parse all blocks if needed
2232     if (inlined_die) {
2233       Block &function_block = sc.function->GetBlock(true);
2234       sc.block = function_block.FindBlockByID(inlined_die.GetID());
2235       if (sc.block == nullptr)
2236         sc.block = function_block.FindBlockByID(inlined_die.GetOffset());
2237       if (sc.block == nullptr || !sc.block->GetStartAddress(addr))
2238         addr.Clear();
2239     } else {
2240       sc.block = nullptr;
2241       addr = sc.function->GetAddressRange().GetBaseAddress();
2242     }
2243 
2244 
2245     if (auto section_sp = addr.GetSection()) {
2246       if (section_sp->GetPermissions() & ePermissionsExecutable) {
2247         sc_list.Append(sc);
2248         return true;
2249       }
2250     }
2251   }
2252 
2253   return false;
2254 }
2255 
DIEInDeclContext(const CompilerDeclContext & decl_ctx,const DWARFDIE & die)2256 bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext &decl_ctx,
2257                                        const DWARFDIE &die) {
2258   // If we have no parent decl context to match this DIE matches, and if the
2259   // parent decl context isn't valid, we aren't trying to look for any
2260   // particular decl context so any die matches.
2261   if (!decl_ctx.IsValid())
2262     return true;
2263 
2264   if (die) {
2265     if (DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU())) {
2266       if (CompilerDeclContext actual_decl_ctx =
2267               dwarf_ast->GetDeclContextContainingUIDFromDWARF(die))
2268         return decl_ctx.IsContainedInLookup(actual_decl_ctx);
2269     }
2270   }
2271   return false;
2272 }
2273 
FindFunctions(ConstString name,const CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,bool include_inlines,SymbolContextList & sc_list)2274 void SymbolFileDWARF::FindFunctions(ConstString name,
2275                                     const CompilerDeclContext &parent_decl_ctx,
2276                                     FunctionNameType name_type_mask,
2277                                     bool include_inlines,
2278                                     SymbolContextList &sc_list) {
2279   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2280   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2281   Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')",
2282                      name.AsCString());
2283 
2284   // eFunctionNameTypeAuto should be pre-resolved by a call to
2285   // Module::LookupInfo::LookupInfo()
2286   assert((name_type_mask & eFunctionNameTypeAuto) == 0);
2287 
2288   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2289 
2290   if (log) {
2291     GetObjectFile()->GetModule()->LogMessage(
2292         log,
2293         "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, sc_list)",
2294         name.GetCString(), name_type_mask);
2295   }
2296 
2297   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2298     return;
2299 
2300   // If name is empty then we won't find anything.
2301   if (name.IsEmpty())
2302     return;
2303 
2304   // Remember how many sc_list are in the list before we search in case we are
2305   // appending the results to a variable list.
2306 
2307   const uint32_t original_size = sc_list.GetSize();
2308 
2309   llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2310 
2311   m_index->GetFunctions(name, *this, parent_decl_ctx, name_type_mask,
2312                         [&](DWARFDIE die) {
2313                           if (resolved_dies.insert(die.GetDIE()).second)
2314                             ResolveFunction(die, include_inlines, sc_list);
2315                           return true;
2316                         });
2317 
2318   // Return the number of variable that were appended to the list
2319   const uint32_t num_matches = sc_list.GetSize() - original_size;
2320 
2321   if (log && num_matches > 0) {
2322     GetObjectFile()->GetModule()->LogMessage(
2323         log,
2324         "SymbolFileDWARF::FindFunctions (name=\"%s\", "
2325         "name_type_mask=0x%x, include_inlines=%d, sc_list) => %u",
2326         name.GetCString(), name_type_mask, include_inlines,
2327         num_matches);
2328   }
2329 }
2330 
FindFunctions(const RegularExpression & regex,bool include_inlines,SymbolContextList & sc_list)2331 void SymbolFileDWARF::FindFunctions(const RegularExpression &regex,
2332                                     bool include_inlines,
2333                                     SymbolContextList &sc_list) {
2334   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2335   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2336   Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')",
2337                      regex.GetText().str().c_str());
2338 
2339   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2340 
2341   if (log) {
2342     GetObjectFile()->GetModule()->LogMessage(
2343         log, "SymbolFileDWARF::FindFunctions (regex=\"%s\", sc_list)",
2344         regex.GetText().str().c_str());
2345   }
2346 
2347   llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies;
2348   m_index->GetFunctions(regex, [&](DWARFDIE die) {
2349     if (resolved_dies.insert(die.GetDIE()).second)
2350       ResolveFunction(die, include_inlines, sc_list);
2351     return true;
2352   });
2353 }
2354 
GetMangledNamesForFunction(const std::string & scope_qualified_name,std::vector<ConstString> & mangled_names)2355 void SymbolFileDWARF::GetMangledNamesForFunction(
2356     const std::string &scope_qualified_name,
2357     std::vector<ConstString> &mangled_names) {
2358   DWARFDebugInfo &info = DebugInfo();
2359   uint32_t num_comp_units = info.GetNumUnits();
2360   for (uint32_t i = 0; i < num_comp_units; i++) {
2361     DWARFUnit *cu = info.GetUnitAtIndex(i);
2362     if (cu == nullptr)
2363       continue;
2364 
2365     SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile();
2366     if (dwo)
2367       dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names);
2368   }
2369 
2370   for (DIERef die_ref :
2371        m_function_scope_qualified_name_map.lookup(scope_qualified_name)) {
2372     DWARFDIE die = GetDIE(die_ref);
2373     mangled_names.push_back(ConstString(die.GetMangledName()));
2374   }
2375 }
2376 
FindTypes(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,TypeMap & types)2377 void SymbolFileDWARF::FindTypes(
2378     ConstString name, const CompilerDeclContext &parent_decl_ctx,
2379     uint32_t max_matches,
2380     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
2381     TypeMap &types) {
2382   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2383   // Make sure we haven't already searched this SymbolFile before.
2384   if (!searched_symbol_files.insert(this).second)
2385     return;
2386 
2387   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2388 
2389   if (log) {
2390     if (parent_decl_ctx)
2391       GetObjectFile()->GetModule()->LogMessage(
2392           log,
2393           "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2394           "%p (\"%s\"), max_matches=%u, type_list)",
2395           name.GetCString(), static_cast<const void *>(&parent_decl_ctx),
2396           parent_decl_ctx.GetName().AsCString("<NULL>"), max_matches);
2397     else
2398       GetObjectFile()->GetModule()->LogMessage(
2399           log,
2400           "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = "
2401           "NULL, max_matches=%u, type_list)",
2402           name.GetCString(), max_matches);
2403   }
2404 
2405   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2406     return;
2407 
2408   m_index->GetTypes(name, [&](DWARFDIE die) {
2409     if (!DIEInDeclContext(parent_decl_ctx, die))
2410       return true; // The containing decl contexts don't match
2411 
2412     Type *matching_type = ResolveType(die, true, true);
2413     if (!matching_type)
2414       return true;
2415 
2416     // We found a type pointer, now find the shared pointer form our type
2417     // list
2418     types.InsertUnique(matching_type->shared_from_this());
2419     return types.GetSize() < max_matches;
2420   });
2421 
2422   // Next search through the reachable Clang modules. This only applies for
2423   // DWARF objects compiled with -gmodules that haven't been processed by
2424   // dsymutil.
2425   if (types.GetSize() < max_matches) {
2426     UpdateExternalModuleListIfNeeded();
2427 
2428     for (const auto &pair : m_external_type_modules)
2429       if (ModuleSP external_module_sp = pair.second)
2430         if (SymbolFile *sym_file = external_module_sp->GetSymbolFile())
2431           sym_file->FindTypes(name, parent_decl_ctx, max_matches,
2432                               searched_symbol_files, types);
2433   }
2434 
2435   if (log && types.GetSize()) {
2436     if (parent_decl_ctx) {
2437       GetObjectFile()->GetModule()->LogMessage(
2438           log,
2439           "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2440           "= %p (\"%s\"), max_matches=%u, type_list) => %u",
2441           name.GetCString(), static_cast<const void *>(&parent_decl_ctx),
2442           parent_decl_ctx.GetName().AsCString("<NULL>"), max_matches,
2443           types.GetSize());
2444     } else {
2445       GetObjectFile()->GetModule()->LogMessage(
2446           log,
2447           "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx "
2448           "= NULL, max_matches=%u, type_list) => %u",
2449           name.GetCString(), max_matches, types.GetSize());
2450     }
2451   }
2452 }
2453 
FindTypes(llvm::ArrayRef<CompilerContext> pattern,LanguageSet languages,llvm::DenseSet<SymbolFile * > & searched_symbol_files,TypeMap & types)2454 void SymbolFileDWARF::FindTypes(
2455     llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
2456     llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
2457   // Make sure we haven't already searched this SymbolFile before.
2458   if (!searched_symbol_files.insert(this).second)
2459     return;
2460 
2461   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2462   if (pattern.empty())
2463     return;
2464 
2465   ConstString name = pattern.back().name;
2466 
2467   if (!name)
2468     return;
2469 
2470   m_index->GetTypes(name, [&](DWARFDIE die) {
2471     if (!languages[GetLanguage(*die.GetCU())])
2472       return true;
2473 
2474     llvm::SmallVector<CompilerContext, 4> die_context;
2475     die.GetDeclContext(die_context);
2476     if (!contextMatches(die_context, pattern))
2477       return true;
2478 
2479     if (Type *matching_type = ResolveType(die, true, true)) {
2480       // We found a type pointer, now find the shared pointer form our type
2481       // list.
2482       types.InsertUnique(matching_type->shared_from_this());
2483     }
2484     return true;
2485   });
2486 
2487   // Next search through the reachable Clang modules. This only applies for
2488   // DWARF objects compiled with -gmodules that haven't been processed by
2489   // dsymutil.
2490   UpdateExternalModuleListIfNeeded();
2491 
2492   for (const auto &pair : m_external_type_modules)
2493     if (ModuleSP external_module_sp = pair.second)
2494       external_module_sp->FindTypes(pattern, languages, searched_symbol_files,
2495                                     types);
2496 }
2497 
2498 CompilerDeclContext
FindNamespace(ConstString name,const CompilerDeclContext & parent_decl_ctx)2499 SymbolFileDWARF::FindNamespace(ConstString name,
2500                                const CompilerDeclContext &parent_decl_ctx) {
2501   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
2502   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2503 
2504   if (log) {
2505     GetObjectFile()->GetModule()->LogMessage(
2506         log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
2507         name.GetCString());
2508   }
2509 
2510   CompilerDeclContext namespace_decl_ctx;
2511 
2512   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
2513     return namespace_decl_ctx;
2514 
2515   m_index->GetNamespaces(name, [&](DWARFDIE die) {
2516     if (!DIEInDeclContext(parent_decl_ctx, die))
2517       return true; // The containing decl contexts don't match
2518 
2519     DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU());
2520     if (!dwarf_ast)
2521       return true;
2522 
2523     namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die);
2524     return !namespace_decl_ctx.IsValid();
2525   });
2526 
2527   if (log && namespace_decl_ctx) {
2528     GetObjectFile()->GetModule()->LogMessage(
2529         log,
2530         "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => "
2531         "CompilerDeclContext(%p/%p) \"%s\"",
2532         name.GetCString(),
2533         static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()),
2534         static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()),
2535         namespace_decl_ctx.GetName().AsCString("<NULL>"));
2536   }
2537 
2538   return namespace_decl_ctx;
2539 }
2540 
GetTypeForDIE(const DWARFDIE & die,bool resolve_function_context)2541 TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die,
2542                                       bool resolve_function_context) {
2543   TypeSP type_sp;
2544   if (die) {
2545     Type *type_ptr = GetDIEToType().lookup(die.GetDIE());
2546     if (type_ptr == nullptr) {
2547       SymbolContextScope *scope;
2548       if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()))
2549         scope = GetCompUnitForDWARFCompUnit(*dwarf_cu);
2550       else
2551         scope = GetObjectFile()->GetModule().get();
2552       assert(scope);
2553       SymbolContext sc(scope);
2554       const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE();
2555       while (parent_die != nullptr) {
2556         if (parent_die->Tag() == DW_TAG_subprogram)
2557           break;
2558         parent_die = parent_die->GetParent();
2559       }
2560       SymbolContext sc_backup = sc;
2561       if (resolve_function_context && parent_die != nullptr &&
2562           !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc))
2563         sc = sc_backup;
2564 
2565       type_sp = ParseType(sc, die, nullptr);
2566     } else if (type_ptr != DIE_IS_BEING_PARSED) {
2567       // Grab the existing type from the master types lists
2568       type_sp = type_ptr->shared_from_this();
2569     }
2570   }
2571   return type_sp;
2572 }
2573 
2574 DWARFDIE
GetDeclContextDIEContainingDIE(const DWARFDIE & orig_die)2575 SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) {
2576   if (orig_die) {
2577     DWARFDIE die = orig_die;
2578 
2579     while (die) {
2580       // If this is the original DIE that we are searching for a declaration
2581       // for, then don't look in the cache as we don't want our own decl
2582       // context to be our decl context...
2583       if (orig_die != die) {
2584         switch (die.Tag()) {
2585         case DW_TAG_compile_unit:
2586         case DW_TAG_partial_unit:
2587         case DW_TAG_namespace:
2588         case DW_TAG_structure_type:
2589         case DW_TAG_union_type:
2590         case DW_TAG_class_type:
2591         case DW_TAG_lexical_block:
2592         case DW_TAG_subprogram:
2593           return die;
2594         case DW_TAG_inlined_subroutine: {
2595           DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2596           if (abs_die) {
2597             return abs_die;
2598           }
2599           break;
2600         }
2601         default:
2602           break;
2603         }
2604       }
2605 
2606       DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification);
2607       if (spec_die) {
2608         DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die);
2609         if (decl_ctx_die)
2610           return decl_ctx_die;
2611       }
2612 
2613       DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin);
2614       if (abs_die) {
2615         DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die);
2616         if (decl_ctx_die)
2617           return decl_ctx_die;
2618       }
2619 
2620       die = die.GetParent();
2621     }
2622   }
2623   return DWARFDIE();
2624 }
2625 
GetObjCClassSymbol(ConstString objc_class_name)2626 Symbol *SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) {
2627   Symbol *objc_class_symbol = nullptr;
2628   if (m_objfile_sp) {
2629     Symtab *symtab = m_objfile_sp->GetSymtab();
2630     if (symtab) {
2631       objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
2632           objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo,
2633           Symtab::eVisibilityAny);
2634     }
2635   }
2636   return objc_class_symbol;
2637 }
2638 
2639 // Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If
2640 // they don't then we can end up looking through all class types for a complete
2641 // type and never find the full definition. We need to know if this attribute
2642 // is supported, so we determine this here and cache th result. We also need to
2643 // worry about the debug map
2644 // DWARF file
2645 // if we are doing darwin DWARF in .o file debugging.
Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit * cu)2646 bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu) {
2647   if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
2648     m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
2649     if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
2650       m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2651     else {
2652       DWARFDebugInfo &debug_info = DebugInfo();
2653       const uint32_t num_compile_units = GetNumCompileUnits();
2654       for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
2655         DWARFUnit *dwarf_cu = debug_info.GetUnitAtIndex(cu_idx);
2656         if (dwarf_cu != cu &&
2657             dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) {
2658           m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
2659           break;
2660         }
2661       }
2662     }
2663     if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo &&
2664         GetDebugMapSymfile())
2665       return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this);
2666   }
2667   return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
2668 }
2669 
2670 // This function can be used when a DIE is found that is a forward declaration
2671 // DIE and we want to try and find a type that has the complete definition.
FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE & die,ConstString type_name,bool must_be_implementation)2672 TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE(
2673     const DWARFDIE &die, ConstString type_name, bool must_be_implementation) {
2674 
2675   TypeSP type_sp;
2676 
2677   if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name)))
2678     return type_sp;
2679 
2680   m_index->GetCompleteObjCClass(
2681       type_name, must_be_implementation, [&](DWARFDIE type_die) {
2682         bool try_resolving_type = false;
2683 
2684         // Don't try and resolve the DIE we are looking for with the DIE
2685         // itself!
2686         if (type_die != die) {
2687           switch (type_die.Tag()) {
2688           case DW_TAG_class_type:
2689           case DW_TAG_structure_type:
2690             try_resolving_type = true;
2691             break;
2692           default:
2693             break;
2694           }
2695         }
2696         if (!try_resolving_type)
2697           return true;
2698 
2699         if (must_be_implementation &&
2700             type_die.Supports_DW_AT_APPLE_objc_complete_type())
2701           try_resolving_type = type_die.GetAttributeValueAsUnsigned(
2702               DW_AT_APPLE_objc_complete_type, 0);
2703         if (!try_resolving_type)
2704           return true;
2705 
2706         Type *resolved_type = ResolveType(type_die, false, true);
2707         if (!resolved_type || resolved_type == DIE_IS_BEING_PARSED)
2708           return true;
2709 
2710         DEBUG_PRINTF(
2711             "resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64
2712             " (cu 0x%8.8" PRIx64 ")\n",
2713             die.GetID(),
2714             m_objfile_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
2715             type_die.GetID(), type_cu->GetID());
2716 
2717         if (die)
2718           GetDIEToType()[die.GetDIE()] = resolved_type;
2719         type_sp = resolved_type->shared_from_this();
2720         return false;
2721       });
2722   return type_sp;
2723 }
2724 
2725 // This function helps to ensure that the declaration contexts match for two
2726 // different DIEs. Often times debug information will refer to a forward
2727 // declaration of a type (the equivalent of "struct my_struct;". There will
2728 // often be a declaration of that type elsewhere that has the full definition.
2729 // When we go looking for the full type "my_struct", we will find one or more
2730 // matches in the accelerator tables and we will then need to make sure the
2731 // type was in the same declaration context as the original DIE. This function
2732 // can efficiently compare two DIEs and will return true when the declaration
2733 // context matches, and false when they don't.
DIEDeclContextsMatch(const DWARFDIE & die1,const DWARFDIE & die2)2734 bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1,
2735                                            const DWARFDIE &die2) {
2736   if (die1 == die2)
2737     return true;
2738 
2739   std::vector<DWARFDIE> decl_ctx_1;
2740   std::vector<DWARFDIE> decl_ctx_2;
2741   // The declaration DIE stack is a stack of the declaration context DIEs all
2742   // the way back to the compile unit. If a type "T" is declared inside a class
2743   // "B", and class "B" is declared inside a class "A" and class "A" is in a
2744   // namespace "lldb", and the namespace is in a compile unit, there will be a
2745   // stack of DIEs:
2746   //
2747   //   [0] DW_TAG_class_type for "B"
2748   //   [1] DW_TAG_class_type for "A"
2749   //   [2] DW_TAG_namespace  for "lldb"
2750   //   [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file.
2751   //
2752   // We grab both contexts and make sure that everything matches all the way
2753   // back to the compiler unit.
2754 
2755   // First lets grab the decl contexts for both DIEs
2756   decl_ctx_1 = die1.GetDeclContextDIEs();
2757   decl_ctx_2 = die2.GetDeclContextDIEs();
2758   // Make sure the context arrays have the same size, otherwise we are done
2759   const size_t count1 = decl_ctx_1.size();
2760   const size_t count2 = decl_ctx_2.size();
2761   if (count1 != count2)
2762     return false;
2763 
2764   // Make sure the DW_TAG values match all the way back up the compile unit. If
2765   // they don't, then we are done.
2766   DWARFDIE decl_ctx_die1;
2767   DWARFDIE decl_ctx_die2;
2768   size_t i;
2769   for (i = 0; i < count1; i++) {
2770     decl_ctx_die1 = decl_ctx_1[i];
2771     decl_ctx_die2 = decl_ctx_2[i];
2772     if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag())
2773       return false;
2774   }
2775 #ifndef NDEBUG
2776 
2777   // Make sure the top item in the decl context die array is always
2778   // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then
2779   // something went wrong in the DWARFDIE::GetDeclContextDIEs()
2780   // function.
2781   dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag();
2782   UNUSED_IF_ASSERT_DISABLED(cu_tag);
2783   assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit);
2784 
2785 #endif
2786   // Always skip the compile unit when comparing by only iterating up to "count
2787   // - 1". Here we compare the names as we go.
2788   for (i = 0; i < count1 - 1; i++) {
2789     decl_ctx_die1 = decl_ctx_1[i];
2790     decl_ctx_die2 = decl_ctx_2[i];
2791     const char *name1 = decl_ctx_die1.GetName();
2792     const char *name2 = decl_ctx_die2.GetName();
2793     // If the string was from a DW_FORM_strp, then the pointer will often be
2794     // the same!
2795     if (name1 == name2)
2796       continue;
2797 
2798     // Name pointers are not equal, so only compare the strings if both are not
2799     // NULL.
2800     if (name1 && name2) {
2801       // If the strings don't compare, we are done...
2802       if (strcmp(name1, name2) != 0)
2803         return false;
2804     } else {
2805       // One name was NULL while the other wasn't
2806       return false;
2807     }
2808   }
2809   // We made it through all of the checks and the declaration contexts are
2810   // equal.
2811   return true;
2812 }
2813 
FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext & dwarf_decl_ctx)2814 TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(
2815     const DWARFDeclContext &dwarf_decl_ctx) {
2816   TypeSP type_sp;
2817 
2818   const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
2819   if (dwarf_decl_ctx_count > 0) {
2820     const ConstString type_name(dwarf_decl_ctx[0].name);
2821     const dw_tag_t tag = dwarf_decl_ctx[0].tag;
2822 
2823     if (type_name) {
2824       Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
2825                                             DWARF_LOG_LOOKUPS));
2826       if (log) {
2827         GetObjectFile()->GetModule()->LogMessage(
2828             log,
2829             "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%"
2830             "s, qualified-name='%s')",
2831             DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2832             dwarf_decl_ctx.GetQualifiedName());
2833       }
2834 
2835       // Get the type system that we are looking to find a type for. We will
2836       // use this to ensure any matches we find are in a language that this
2837       // type system supports
2838       const LanguageType language = dwarf_decl_ctx.GetLanguage();
2839       TypeSystem *type_system = nullptr;
2840       if (language != eLanguageTypeUnknown) {
2841         auto type_system_or_err = GetTypeSystemForLanguage(language);
2842         if (auto err = type_system_or_err.takeError()) {
2843           LLDB_LOG_ERROR(
2844               lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
2845               std::move(err), "Cannot get TypeSystem for language {}",
2846               Language::GetNameForLanguageType(language));
2847         } else {
2848           type_system = &type_system_or_err.get();
2849         }
2850       }
2851 
2852       m_index->GetTypes(dwarf_decl_ctx, [&](DWARFDIE type_die) {
2853         // Make sure type_die's langauge matches the type system we are
2854         // looking for. We don't want to find a "Foo" type from Java if we
2855         // are looking for a "Foo" type for C, C++, ObjC, or ObjC++.
2856         if (type_system &&
2857             !type_system->SupportsLanguage(GetLanguage(*type_die.GetCU())))
2858           return true;
2859         bool try_resolving_type = false;
2860 
2861         // Don't try and resolve the DIE we are looking for with the DIE
2862         // itself!
2863         const dw_tag_t type_tag = type_die.Tag();
2864         // Make sure the tags match
2865         if (type_tag == tag) {
2866           // The tags match, lets try resolving this type
2867           try_resolving_type = true;
2868         } else {
2869           // The tags don't match, but we need to watch our for a forward
2870           // declaration for a struct and ("struct foo") ends up being a
2871           // class ("class foo { ... };") or vice versa.
2872           switch (type_tag) {
2873           case DW_TAG_class_type:
2874             // We had a "class foo", see if we ended up with a "struct foo
2875             // { ... };"
2876             try_resolving_type = (tag == DW_TAG_structure_type);
2877             break;
2878           case DW_TAG_structure_type:
2879             // We had a "struct foo", see if we ended up with a "class foo
2880             // { ... };"
2881             try_resolving_type = (tag == DW_TAG_class_type);
2882             break;
2883           default:
2884             // Tags don't match, don't event try to resolve using this type
2885             // whose name matches....
2886             break;
2887           }
2888         }
2889 
2890         if (!try_resolving_type) {
2891           if (log) {
2892             std::string qualified_name;
2893             type_die.GetQualifiedName(qualified_name);
2894             GetObjectFile()->GetModule()->LogMessage(
2895                 log,
2896                 "SymbolFileDWARF::"
2897                 "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2898                 "qualified-name='%s') ignoring die=0x%8.8x (%s)",
2899                 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2900                 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2901                 qualified_name.c_str());
2902           }
2903           return true;
2904         }
2905 
2906         DWARFDeclContext type_dwarf_decl_ctx = GetDWARFDeclContext(type_die);
2907 
2908         if (log) {
2909           GetObjectFile()->GetModule()->LogMessage(
2910               log,
2911               "SymbolFileDWARF::"
2912               "FindDefinitionTypeForDWARFDeclContext(tag=%s, "
2913               "qualified-name='%s') trying die=0x%8.8x (%s)",
2914               DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
2915               dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(),
2916               type_dwarf_decl_ctx.GetQualifiedName());
2917         }
2918 
2919         // Make sure the decl contexts match all the way up
2920         if (dwarf_decl_ctx != type_dwarf_decl_ctx)
2921           return true;
2922 
2923         Type *resolved_type = ResolveType(type_die, false);
2924         if (!resolved_type || resolved_type == DIE_IS_BEING_PARSED)
2925           return true;
2926 
2927         type_sp = resolved_type->shared_from_this();
2928         return false;
2929       });
2930     }
2931   }
2932   return type_sp;
2933 }
2934 
ParseType(const SymbolContext & sc,const DWARFDIE & die,bool * type_is_new_ptr)2935 TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die,
2936                                   bool *type_is_new_ptr) {
2937   if (!die)
2938     return {};
2939 
2940   auto type_system_or_err = GetTypeSystemForLanguage(GetLanguage(*die.GetCU()));
2941   if (auto err = type_system_or_err.takeError()) {
2942     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
2943                    std::move(err), "Unable to parse type");
2944     return {};
2945   }
2946 
2947   DWARFASTParser *dwarf_ast = type_system_or_err->GetDWARFParser();
2948   if (!dwarf_ast)
2949     return {};
2950 
2951   TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, type_is_new_ptr);
2952   if (type_sp) {
2953     GetTypeList().Insert(type_sp);
2954 
2955     if (die.Tag() == DW_TAG_subprogram) {
2956       std::string scope_qualified_name(GetDeclContextForUID(die.GetID())
2957                                            .GetScopeQualifiedName()
2958                                            .AsCString(""));
2959       if (scope_qualified_name.size()) {
2960         m_function_scope_qualified_name_map[scope_qualified_name].insert(
2961             *die.GetDIERef());
2962       }
2963     }
2964   }
2965 
2966   return type_sp;
2967 }
2968 
ParseTypes(const SymbolContext & sc,const DWARFDIE & orig_die,bool parse_siblings,bool parse_children)2969 size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc,
2970                                    const DWARFDIE &orig_die,
2971                                    bool parse_siblings, bool parse_children) {
2972   size_t types_added = 0;
2973   DWARFDIE die = orig_die;
2974 
2975   while (die) {
2976     const dw_tag_t tag = die.Tag();
2977     bool type_is_new = false;
2978 
2979     Tag dwarf_tag = static_cast<Tag>(tag);
2980 
2981     // TODO: Currently ParseTypeFromDWARF(...) which is called by ParseType(...)
2982     // does not handle DW_TAG_subrange_type. It is not clear if this is a bug or
2983     // not.
2984     if (isType(dwarf_tag) && tag != DW_TAG_subrange_type)
2985       ParseType(sc, die, &type_is_new);
2986 
2987     if (type_is_new)
2988       ++types_added;
2989 
2990     if (parse_children && die.HasChildren()) {
2991       if (die.Tag() == DW_TAG_subprogram) {
2992         SymbolContext child_sc(sc);
2993         child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get();
2994         types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true);
2995       } else
2996         types_added += ParseTypes(sc, die.GetFirstChild(), true, true);
2997     }
2998 
2999     if (parse_siblings)
3000       die = die.GetSibling();
3001     else
3002       die.Clear();
3003   }
3004   return types_added;
3005 }
3006 
ParseBlocksRecursive(Function & func)3007 size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) {
3008   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
3009   CompileUnit *comp_unit = func.GetCompileUnit();
3010   lldbassert(comp_unit);
3011 
3012   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit);
3013   if (!dwarf_cu)
3014     return 0;
3015 
3016   size_t functions_added = 0;
3017   const dw_offset_t function_die_offset = func.GetID();
3018   DWARFDIE function_die =
3019       dwarf_cu->GetNonSkeletonUnit().GetDIE(function_die_offset);
3020   if (function_die) {
3021     ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die,
3022                          LLDB_INVALID_ADDRESS, 0);
3023   }
3024 
3025   return functions_added;
3026 }
3027 
ParseTypes(CompileUnit & comp_unit)3028 size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) {
3029   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
3030   size_t types_added = 0;
3031   DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
3032   if (dwarf_cu) {
3033     DWARFDIE dwarf_cu_die = dwarf_cu->DIE();
3034     if (dwarf_cu_die && dwarf_cu_die.HasChildren()) {
3035       SymbolContext sc;
3036       sc.comp_unit = &comp_unit;
3037       types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true);
3038     }
3039   }
3040 
3041   return types_added;
3042 }
3043 
ParseVariablesForContext(const SymbolContext & sc)3044 size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
3045   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
3046   if (sc.comp_unit != nullptr) {
3047     if (sc.function) {
3048       DWARFDIE function_die = GetDIE(sc.function->GetID());
3049 
3050       const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress(
3051           DW_AT_low_pc, LLDB_INVALID_ADDRESS);
3052       if (func_lo_pc != LLDB_INVALID_ADDRESS) {
3053         const size_t num_variables = ParseVariables(
3054             sc, function_die.GetFirstChild(), func_lo_pc, true, true);
3055 
3056         // Let all blocks know they have parse all their variables
3057         sc.function->GetBlock(false).SetDidParseVariables(true, true);
3058         return num_variables;
3059       }
3060     } else if (sc.comp_unit) {
3061       DWARFUnit *dwarf_cu = DebugInfo().GetUnitAtIndex(sc.comp_unit->GetID());
3062 
3063       if (dwarf_cu == nullptr)
3064         return 0;
3065 
3066       uint32_t vars_added = 0;
3067       VariableListSP variables(sc.comp_unit->GetVariableList(false));
3068 
3069       if (variables.get() == nullptr) {
3070         variables = std::make_shared<VariableList>();
3071         sc.comp_unit->SetVariableList(variables);
3072 
3073         m_index->GetGlobalVariables(
3074             dwarf_cu->GetNonSkeletonUnit(), [&](DWARFDIE die) {
3075               VariableSP var_sp(
3076                   ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS));
3077               if (var_sp) {
3078                 variables->AddVariableIfUnique(var_sp);
3079                 ++vars_added;
3080               }
3081               return true;
3082             });
3083       }
3084       return vars_added;
3085     }
3086   }
3087   return 0;
3088 }
3089 
ParseVariableDIE(const SymbolContext & sc,const DWARFDIE & die,const lldb::addr_t func_low_pc)3090 VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
3091                                              const DWARFDIE &die,
3092                                              const lldb::addr_t func_low_pc) {
3093   if (die.GetDWARF() != this)
3094     return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc);
3095 
3096   if (!die)
3097     return nullptr;
3098 
3099   if (VariableSP var_sp = GetDIEToVariable()[die.GetDIE()])
3100     return var_sp; // Already been parsed!
3101 
3102   const dw_tag_t tag = die.Tag();
3103   ModuleSP module = GetObjectFile()->GetModule();
3104 
3105   if (tag != DW_TAG_variable && tag != DW_TAG_constant &&
3106       (tag != DW_TAG_formal_parameter || !sc.function))
3107     return nullptr;
3108 
3109   DWARFAttributes attributes;
3110   const size_t num_attributes = die.GetAttributes(attributes);
3111   DWARFDIE spec_die;
3112   VariableSP var_sp;
3113   const char *name = nullptr;
3114   const char *mangled = nullptr;
3115   Declaration decl;
3116   DWARFFormValue type_die_form;
3117   DWARFExpression location;
3118   bool is_external = false;
3119   bool is_artificial = false;
3120   DWARFFormValue const_value_form, location_form;
3121   Variable::RangeList scope_ranges;
3122 
3123   for (size_t i = 0; i < num_attributes; ++i) {
3124     dw_attr_t attr = attributes.AttributeAtIndex(i);
3125     DWARFFormValue form_value;
3126 
3127     if (!attributes.ExtractFormValueAtIndex(i, form_value))
3128       continue;
3129     switch (attr) {
3130     case DW_AT_decl_file:
3131       decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3132           form_value.Unsigned()));
3133       break;
3134     case DW_AT_decl_line:
3135       decl.SetLine(form_value.Unsigned());
3136       break;
3137     case DW_AT_decl_column:
3138       decl.SetColumn(form_value.Unsigned());
3139       break;
3140     case DW_AT_name:
3141       name = form_value.AsCString();
3142       break;
3143     case DW_AT_linkage_name:
3144     case DW_AT_MIPS_linkage_name:
3145       mangled = form_value.AsCString();
3146       break;
3147     case DW_AT_type:
3148       type_die_form = form_value;
3149       break;
3150     case DW_AT_external:
3151       is_external = form_value.Boolean();
3152       break;
3153     case DW_AT_const_value:
3154       const_value_form = form_value;
3155       break;
3156     case DW_AT_location:
3157       location_form = form_value;
3158       break;
3159     case DW_AT_specification:
3160       spec_die = form_value.Reference();
3161       break;
3162     case DW_AT_start_scope:
3163       // TODO: Implement this.
3164       break;
3165     case DW_AT_artificial:
3166       is_artificial = form_value.Boolean();
3167       break;
3168     case DW_AT_declaration:
3169     case DW_AT_description:
3170     case DW_AT_endianity:
3171     case DW_AT_segment:
3172     case DW_AT_visibility:
3173     default:
3174     case DW_AT_abstract_origin:
3175     case DW_AT_sibling:
3176       break;
3177     }
3178   }
3179 
3180   // Prefer DW_AT_location over DW_AT_const_value. Both can be emitted e.g.
3181   // for static constexpr member variables -- DW_AT_const_value will be
3182   // present in the class declaration and DW_AT_location in the DIE defining
3183   // the member.
3184   bool location_is_const_value_data = false;
3185   bool has_explicit_location = false;
3186   bool use_type_size_for_value = false;
3187   if (location_form.IsValid()) {
3188     has_explicit_location = true;
3189     if (DWARFFormValue::IsBlockForm(location_form.Form())) {
3190       const DWARFDataExtractor &data = die.GetData();
3191 
3192       uint32_t block_offset = location_form.BlockData() - data.GetDataStart();
3193       uint32_t block_length = location_form.Unsigned();
3194       location = DWARFExpression(
3195           module, DataExtractor(data, block_offset, block_length), die.GetCU());
3196     } else {
3197       DataExtractor data = die.GetCU()->GetLocationData();
3198       dw_offset_t offset = location_form.Unsigned();
3199       if (location_form.Form() == DW_FORM_loclistx)
3200         offset = die.GetCU()->GetLoclistOffset(offset).getValueOr(-1);
3201       if (data.ValidOffset(offset)) {
3202         data = DataExtractor(data, offset, data.GetByteSize() - offset);
3203         location = DWARFExpression(module, data, die.GetCU());
3204         assert(func_low_pc != LLDB_INVALID_ADDRESS);
3205         location.SetLocationListAddresses(
3206             location_form.GetUnit()->GetBaseAddress(), func_low_pc);
3207       }
3208     }
3209   } else if (const_value_form.IsValid()) {
3210     location_is_const_value_data = true;
3211     // The constant value will be either a block, a data value or a
3212     // string.
3213     const DWARFDataExtractor &debug_info_data = die.GetData();
3214     if (DWARFFormValue::IsBlockForm(const_value_form.Form())) {
3215       // Retrieve the value as a block expression.
3216       uint32_t block_offset =
3217           const_value_form.BlockData() - debug_info_data.GetDataStart();
3218       uint32_t block_length = const_value_form.Unsigned();
3219       location = DWARFExpression(
3220           module, DataExtractor(debug_info_data, block_offset, block_length),
3221           die.GetCU());
3222     } else if (DWARFFormValue::IsDataForm(const_value_form.Form())) {
3223       // Constant value size does not have to match the size of the
3224       // variable. We will fetch the size of the type after we create
3225       // it.
3226       use_type_size_for_value = true;
3227     } else if (const char *str = const_value_form.AsCString()) {
3228       uint32_t string_length = strlen(str) + 1;
3229       location = DWARFExpression(
3230           module,
3231           DataExtractor(str, string_length, die.GetCU()->GetByteOrder(),
3232                         die.GetCU()->GetAddressByteSize()),
3233           die.GetCU());
3234     }
3235   }
3236 
3237   const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die);
3238   const dw_tag_t parent_tag = die.GetParent().Tag();
3239   bool is_static_member = (parent_tag == DW_TAG_compile_unit ||
3240                            parent_tag == DW_TAG_partial_unit) &&
3241                           (parent_context_die.Tag() == DW_TAG_class_type ||
3242                            parent_context_die.Tag() == DW_TAG_structure_type);
3243 
3244   ValueType scope = eValueTypeInvalid;
3245 
3246   const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die);
3247   SymbolContextScope *symbol_context_scope = nullptr;
3248 
3249   bool has_explicit_mangled = mangled != nullptr;
3250   if (!mangled) {
3251     // LLDB relies on the mangled name (DW_TAG_linkage_name or
3252     // DW_AT_MIPS_linkage_name) to generate fully qualified names
3253     // of global variables with commands like "frame var j". For
3254     // example, if j were an int variable holding a value 4 and
3255     // declared in a namespace B which in turn is contained in a
3256     // namespace A, the command "frame var j" returns
3257     //   "(int) A::B::j = 4".
3258     // If the compiler does not emit a linkage name, we should be
3259     // able to generate a fully qualified name from the
3260     // declaration context.
3261     if ((parent_tag == DW_TAG_compile_unit ||
3262          parent_tag == DW_TAG_partial_unit) &&
3263         Language::LanguageIsCPlusPlus(GetLanguage(*die.GetCU())))
3264       mangled =
3265           GetDWARFDeclContext(die).GetQualifiedNameAsConstString().GetCString();
3266   }
3267 
3268   if (tag == DW_TAG_formal_parameter)
3269     scope = eValueTypeVariableArgument;
3270   else {
3271     // DWARF doesn't specify if a DW_TAG_variable is a local, global
3272     // or static variable, so we have to do a little digging:
3273     // 1) DW_AT_linkage_name implies static lifetime (but may be missing)
3274     // 2) An empty DW_AT_location is an (optimized-out) static lifetime var.
3275     // 3) DW_AT_location containing a DW_OP_addr implies static lifetime.
3276     // Clang likes to combine small global variables into the same symbol
3277     // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
3278     // so we need to look through the whole expression.
3279     bool is_static_lifetime =
3280         has_explicit_mangled || (has_explicit_location && !location.IsValid());
3281     // Check if the location has a DW_OP_addr with any address value...
3282     lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
3283     if (!location_is_const_value_data) {
3284       bool op_error = false;
3285       location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error);
3286       if (op_error) {
3287         StreamString strm;
3288         location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0,
3289                                         nullptr);
3290         GetObjectFile()->GetModule()->ReportError(
3291             "0x%8.8x: %s has an invalid location: %s", die.GetOffset(),
3292             die.GetTagAsCString(), strm.GetData());
3293       }
3294       if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
3295         is_static_lifetime = true;
3296     }
3297     SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
3298     if (debug_map_symfile)
3299       // Set the module of the expression to the linked module
3300       // instead of the oject file so the relocated address can be
3301       // found there.
3302       location.SetModule(debug_map_symfile->GetObjectFile()->GetModule());
3303 
3304     if (is_static_lifetime) {
3305       if (is_external)
3306         scope = eValueTypeVariableGlobal;
3307       else
3308         scope = eValueTypeVariableStatic;
3309 
3310       if (debug_map_symfile) {
3311         // When leaving the DWARF in the .o files on darwin, when we have a
3312         // global variable that wasn't initialized, the .o file might not
3313         // have allocated a virtual address for the global variable. In
3314         // this case it will have created a symbol for the global variable
3315         // that is undefined/data and external and the value will be the
3316         // byte size of the variable. When we do the address map in
3317         // SymbolFileDWARFDebugMap we rely on having an address, we need to
3318         // do some magic here so we can get the correct address for our
3319         // global variable. The address for all of these entries will be
3320         // zero, and there will be an undefined symbol in this object file,
3321         // and the executable will have a matching symbol with a good
3322         // address. So here we dig up the correct address and replace it in
3323         // the location for the variable, and set the variable's symbol
3324         // context scope to be that of the main executable so the file
3325         // address will resolve correctly.
3326         bool linked_oso_file_addr = false;
3327         if (is_external && location_DW_OP_addr == 0) {
3328           // we have a possible uninitialized extern global
3329           ConstString const_name(mangled ? mangled : name);
3330           ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
3331           if (debug_map_objfile) {
3332             Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
3333             if (debug_map_symtab) {
3334               Symbol *exe_symbol =
3335                   debug_map_symtab->FindFirstSymbolWithNameAndType(
3336                       const_name, eSymbolTypeData, Symtab::eDebugYes,
3337                       Symtab::eVisibilityExtern);
3338               if (exe_symbol) {
3339                 if (exe_symbol->ValueIsAddress()) {
3340                   const addr_t exe_file_addr =
3341                       exe_symbol->GetAddressRef().GetFileAddress();
3342                   if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3343                     if (location.Update_DW_OP_addr(exe_file_addr)) {
3344                       linked_oso_file_addr = true;
3345                       symbol_context_scope = exe_symbol;
3346                     }
3347                   }
3348                 }
3349               }
3350             }
3351           }
3352         }
3353 
3354         if (!linked_oso_file_addr) {
3355           // The DW_OP_addr is not zero, but it contains a .o file address
3356           // which needs to be linked up correctly.
3357           const lldb::addr_t exe_file_addr =
3358               debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
3359           if (exe_file_addr != LLDB_INVALID_ADDRESS) {
3360             // Update the file address for this variable
3361             location.Update_DW_OP_addr(exe_file_addr);
3362           } else {
3363             // Variable didn't make it into the final executable
3364             return var_sp;
3365           }
3366         }
3367       }
3368     } else {
3369       if (location_is_const_value_data &&
3370           die.GetDIE()->IsGlobalOrStaticScopeVariable())
3371         scope = eValueTypeVariableStatic;
3372       else {
3373         scope = eValueTypeVariableLocal;
3374         if (debug_map_symfile) {
3375           // We need to check for TLS addresses that we need to fixup
3376           if (location.ContainsThreadLocalStorage()) {
3377             location.LinkThreadLocalStorage(
3378                 debug_map_symfile->GetObjectFile()->GetModule(),
3379                 [this, debug_map_symfile](
3380                     lldb::addr_t unlinked_file_addr) -> lldb::addr_t {
3381                   return debug_map_symfile->LinkOSOFileAddress(
3382                       this, unlinked_file_addr);
3383                 });
3384             scope = eValueTypeVariableThreadLocal;
3385           }
3386         }
3387       }
3388     }
3389   }
3390 
3391   if (symbol_context_scope == nullptr) {
3392     switch (parent_tag) {
3393     case DW_TAG_subprogram:
3394     case DW_TAG_inlined_subroutine:
3395     case DW_TAG_lexical_block:
3396       if (sc.function) {
3397         symbol_context_scope =
3398             sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
3399         if (symbol_context_scope == nullptr)
3400           symbol_context_scope = sc.function;
3401       }
3402       break;
3403 
3404     default:
3405       symbol_context_scope = sc.comp_unit;
3406       break;
3407     }
3408   }
3409 
3410   if (symbol_context_scope) {
3411     auto type_sp = std::make_shared<SymbolFileType>(
3412         *this, GetUID(type_die_form.Reference()));
3413 
3414     if (use_type_size_for_value && type_sp->GetType())
3415       location.UpdateValue(
3416           const_value_form.Unsigned(),
3417           type_sp->GetType()->GetByteSize(nullptr).getValueOr(0),
3418           die.GetCU()->GetAddressByteSize());
3419 
3420     var_sp = std::make_shared<Variable>(
3421         die.GetID(), name, mangled, type_sp, scope, symbol_context_scope,
3422         scope_ranges, &decl, location, is_external, is_artificial,
3423         location_is_const_value_data, is_static_member);
3424   } else {
3425     // Not ready to parse this variable yet. It might be a global or static
3426     // variable that is in a function scope and the function in the symbol
3427     // context wasn't filled in yet
3428     return var_sp;
3429   }
3430   // Cache var_sp even if NULL (the variable was just a specification or was
3431   // missing vital information to be able to be displayed in the debugger
3432   // (missing location due to optimization, etc)) so we don't re-parse this
3433   // DIE over and over later...
3434   GetDIEToVariable()[die.GetDIE()] = var_sp;
3435   if (spec_die)
3436     GetDIEToVariable()[spec_die.GetDIE()] = var_sp;
3437 
3438   return var_sp;
3439 }
3440 
3441 DWARFDIE
FindBlockContainingSpecification(const DIERef & func_die_ref,dw_offset_t spec_block_die_offset)3442 SymbolFileDWARF::FindBlockContainingSpecification(
3443     const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) {
3444   // Give the concrete function die specified by "func_die_offset", find the
3445   // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3446   // to "spec_block_die_offset"
3447   return FindBlockContainingSpecification(DebugInfo().GetDIE(func_die_ref),
3448                                           spec_block_die_offset);
3449 }
3450 
3451 DWARFDIE
FindBlockContainingSpecification(const DWARFDIE & die,dw_offset_t spec_block_die_offset)3452 SymbolFileDWARF::FindBlockContainingSpecification(
3453     const DWARFDIE &die, dw_offset_t spec_block_die_offset) {
3454   if (die) {
3455     switch (die.Tag()) {
3456     case DW_TAG_subprogram:
3457     case DW_TAG_inlined_subroutine:
3458     case DW_TAG_lexical_block: {
3459       if (die.GetReferencedDIE(DW_AT_specification).GetOffset() ==
3460           spec_block_die_offset)
3461         return die;
3462 
3463       if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() ==
3464           spec_block_die_offset)
3465         return die;
3466     } break;
3467     default:
3468       break;
3469     }
3470 
3471     // Give the concrete function die specified by "func_die_offset", find the
3472     // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
3473     // to "spec_block_die_offset"
3474     for (DWARFDIE child_die = die.GetFirstChild(); child_die;
3475          child_die = child_die.GetSibling()) {
3476       DWARFDIE result_die =
3477           FindBlockContainingSpecification(child_die, spec_block_die_offset);
3478       if (result_die)
3479         return result_die;
3480     }
3481   }
3482 
3483   return DWARFDIE();
3484 }
3485 
ParseVariables(const SymbolContext & sc,const DWARFDIE & orig_die,const lldb::addr_t func_low_pc,bool parse_siblings,bool parse_children,VariableList * cc_variable_list)3486 size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc,
3487                                        const DWARFDIE &orig_die,
3488                                        const lldb::addr_t func_low_pc,
3489                                        bool parse_siblings, bool parse_children,
3490                                        VariableList *cc_variable_list) {
3491   if (!orig_die)
3492     return 0;
3493 
3494   VariableListSP variable_list_sp;
3495 
3496   size_t vars_added = 0;
3497   DWARFDIE die = orig_die;
3498   while (die) {
3499     dw_tag_t tag = die.Tag();
3500 
3501     // Check to see if we have already parsed this variable or constant?
3502     VariableSP var_sp = GetDIEToVariable()[die.GetDIE()];
3503     if (var_sp) {
3504       if (cc_variable_list)
3505         cc_variable_list->AddVariableIfUnique(var_sp);
3506     } else {
3507       // We haven't already parsed it, lets do that now.
3508       if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) ||
3509           (tag == DW_TAG_formal_parameter && sc.function)) {
3510         if (variable_list_sp.get() == nullptr) {
3511           DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die);
3512           dw_tag_t parent_tag = sc_parent_die.Tag();
3513           switch (parent_tag) {
3514           case DW_TAG_compile_unit:
3515           case DW_TAG_partial_unit:
3516             if (sc.comp_unit != nullptr) {
3517               variable_list_sp = sc.comp_unit->GetVariableList(false);
3518               if (variable_list_sp.get() == nullptr) {
3519                 variable_list_sp = std::make_shared<VariableList>();
3520               }
3521             } else {
3522               GetObjectFile()->GetModule()->ReportError(
3523                   "parent 0x%8.8" PRIx64 " %s with no valid compile unit in "
3524                   "symbol context for 0x%8.8" PRIx64 " %s.\n",
3525                   sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(),
3526                   orig_die.GetID(), orig_die.GetTagAsCString());
3527             }
3528             break;
3529 
3530           case DW_TAG_subprogram:
3531           case DW_TAG_inlined_subroutine:
3532           case DW_TAG_lexical_block:
3533             if (sc.function != nullptr) {
3534               // Check to see if we already have parsed the variables for the
3535               // given scope
3536 
3537               Block *block = sc.function->GetBlock(true).FindBlockByID(
3538                   sc_parent_die.GetID());
3539               if (block == nullptr) {
3540                 // This must be a specification or abstract origin with a
3541                 // concrete block counterpart in the current function. We need
3542                 // to find the concrete block so we can correctly add the
3543                 // variable to it
3544                 const DWARFDIE concrete_block_die =
3545                     FindBlockContainingSpecification(
3546                         GetDIE(sc.function->GetID()),
3547                         sc_parent_die.GetOffset());
3548                 if (concrete_block_die)
3549                   block = sc.function->GetBlock(true).FindBlockByID(
3550                       concrete_block_die.GetID());
3551               }
3552 
3553               if (block != nullptr) {
3554                 const bool can_create = false;
3555                 variable_list_sp = block->GetBlockVariableList(can_create);
3556                 if (variable_list_sp.get() == nullptr) {
3557                   variable_list_sp = std::make_shared<VariableList>();
3558                   block->SetVariableList(variable_list_sp);
3559                 }
3560               }
3561             }
3562             break;
3563 
3564           default:
3565             GetObjectFile()->GetModule()->ReportError(
3566                 "didn't find appropriate parent DIE for variable list for "
3567                 "0x%8.8" PRIx64 " %s.\n",
3568                 orig_die.GetID(), orig_die.GetTagAsCString());
3569             break;
3570           }
3571         }
3572 
3573         if (variable_list_sp) {
3574           VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc));
3575           if (var_sp) {
3576             variable_list_sp->AddVariableIfUnique(var_sp);
3577             if (cc_variable_list)
3578               cc_variable_list->AddVariableIfUnique(var_sp);
3579             ++vars_added;
3580           }
3581         }
3582       }
3583     }
3584 
3585     bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram);
3586 
3587     if (!skip_children && parse_children && die.HasChildren()) {
3588       vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true,
3589                                    true, cc_variable_list);
3590     }
3591 
3592     if (parse_siblings)
3593       die = die.GetSibling();
3594     else
3595       die.Clear();
3596   }
3597   return vars_added;
3598 }
3599 
3600 /// Collect call site parameters in a DW_TAG_call_site DIE.
3601 static CallSiteParameterArray
CollectCallSiteParameters(ModuleSP module,DWARFDIE call_site_die)3602 CollectCallSiteParameters(ModuleSP module, DWARFDIE call_site_die) {
3603   CallSiteParameterArray parameters;
3604   for (DWARFDIE child = call_site_die.GetFirstChild(); child.IsValid();
3605        child = child.GetSibling()) {
3606     if (child.Tag() != DW_TAG_call_site_parameter &&
3607         child.Tag() != DW_TAG_GNU_call_site_parameter)
3608       continue;
3609 
3610     llvm::Optional<DWARFExpression> LocationInCallee;
3611     llvm::Optional<DWARFExpression> LocationInCaller;
3612 
3613     DWARFAttributes attributes;
3614     const size_t num_attributes = child.GetAttributes(attributes);
3615 
3616     // Parse the location at index \p attr_index within this call site parameter
3617     // DIE, or return None on failure.
3618     auto parse_simple_location =
3619         [&](int attr_index) -> llvm::Optional<DWARFExpression> {
3620       DWARFFormValue form_value;
3621       if (!attributes.ExtractFormValueAtIndex(attr_index, form_value))
3622         return {};
3623       if (!DWARFFormValue::IsBlockForm(form_value.Form()))
3624         return {};
3625       auto data = child.GetData();
3626       uint32_t block_offset = form_value.BlockData() - data.GetDataStart();
3627       uint32_t block_length = form_value.Unsigned();
3628       return DWARFExpression(module,
3629                              DataExtractor(data, block_offset, block_length),
3630                              child.GetCU());
3631     };
3632 
3633     for (size_t i = 0; i < num_attributes; ++i) {
3634       dw_attr_t attr = attributes.AttributeAtIndex(i);
3635       if (attr == DW_AT_location)
3636         LocationInCallee = parse_simple_location(i);
3637       if (attr == DW_AT_call_value || attr == DW_AT_GNU_call_site_value)
3638         LocationInCaller = parse_simple_location(i);
3639     }
3640 
3641     if (LocationInCallee && LocationInCaller) {
3642       CallSiteParameter param = {*LocationInCallee, *LocationInCaller};
3643       parameters.push_back(param);
3644     }
3645   }
3646   return parameters;
3647 }
3648 
3649 /// Collect call graph edges present in a function DIE.
3650 std::vector<std::unique_ptr<lldb_private::CallEdge>>
CollectCallEdges(ModuleSP module,DWARFDIE function_die)3651 SymbolFileDWARF::CollectCallEdges(ModuleSP module, DWARFDIE function_die) {
3652   // Check if the function has a supported call site-related attribute.
3653   // TODO: In the future it may be worthwhile to support call_all_source_calls.
3654   bool has_call_edges =
3655       function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0) ||
3656       function_die.GetAttributeValueAsUnsigned(DW_AT_GNU_all_call_sites, 0);
3657   if (!has_call_edges)
3658     return {};
3659 
3660   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3661   LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}",
3662            function_die.GetPubname());
3663 
3664   // Scan the DIE for TAG_call_site entries.
3665   // TODO: A recursive scan of all blocks in the subprogram is needed in order
3666   // to be DWARF5-compliant. This may need to be done lazily to be performant.
3667   // For now, assume that all entries are nested directly under the subprogram
3668   // (this is the kind of DWARF LLVM produces) and parse them eagerly.
3669   std::vector<std::unique_ptr<CallEdge>> call_edges;
3670   for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid();
3671        child = child.GetSibling()) {
3672     if (child.Tag() != DW_TAG_call_site && child.Tag() != DW_TAG_GNU_call_site)
3673       continue;
3674 
3675     llvm::Optional<DWARFDIE> call_origin;
3676     llvm::Optional<DWARFExpression> call_target;
3677     addr_t return_pc = LLDB_INVALID_ADDRESS;
3678     addr_t call_inst_pc = LLDB_INVALID_ADDRESS;
3679     addr_t low_pc = LLDB_INVALID_ADDRESS;
3680     bool tail_call = false;
3681 
3682     // Second DW_AT_low_pc may come from DW_TAG_subprogram referenced by
3683     // DW_TAG_GNU_call_site's DW_AT_abstract_origin overwriting our 'low_pc'.
3684     // So do not inherit attributes from DW_AT_abstract_origin.
3685     DWARFAttributes attributes;
3686     const size_t num_attributes =
3687         child.GetAttributes(attributes, DWARFDIE::Recurse::no);
3688     for (size_t i = 0; i < num_attributes; ++i) {
3689       DWARFFormValue form_value;
3690       if (!attributes.ExtractFormValueAtIndex(i, form_value)) {
3691         LLDB_LOG(log, "CollectCallEdges: Could not extract TAG_call_site form");
3692         break;
3693       }
3694 
3695       dw_attr_t attr = attributes.AttributeAtIndex(i);
3696 
3697       if (attr == DW_AT_call_tail_call || attr == DW_AT_GNU_tail_call)
3698         tail_call = form_value.Boolean();
3699 
3700       // Extract DW_AT_call_origin (the call target's DIE).
3701       if (attr == DW_AT_call_origin || attr == DW_AT_abstract_origin) {
3702         call_origin = form_value.Reference();
3703         if (!call_origin->IsValid()) {
3704           LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}",
3705                    function_die.GetPubname());
3706           break;
3707         }
3708       }
3709 
3710       if (attr == DW_AT_low_pc)
3711         low_pc = form_value.Address();
3712 
3713       // Extract DW_AT_call_return_pc (the PC the call returns to) if it's
3714       // available. It should only ever be unavailable for tail call edges, in
3715       // which case use LLDB_INVALID_ADDRESS.
3716       if (attr == DW_AT_call_return_pc)
3717         return_pc = form_value.Address();
3718 
3719       // Extract DW_AT_call_pc (the PC at the call/branch instruction). It
3720       // should only ever be unavailable for non-tail calls, in which case use
3721       // LLDB_INVALID_ADDRESS.
3722       if (attr == DW_AT_call_pc)
3723         call_inst_pc = form_value.Address();
3724 
3725       // Extract DW_AT_call_target (the location of the address of the indirect
3726       // call).
3727       if (attr == DW_AT_call_target || attr == DW_AT_GNU_call_site_target) {
3728         if (!DWARFFormValue::IsBlockForm(form_value.Form())) {
3729           LLDB_LOG(log,
3730                    "CollectCallEdges: AT_call_target does not have block form");
3731           break;
3732         }
3733 
3734         auto data = child.GetData();
3735         uint32_t block_offset = form_value.BlockData() - data.GetDataStart();
3736         uint32_t block_length = form_value.Unsigned();
3737         call_target = DWARFExpression(
3738             module, DataExtractor(data, block_offset, block_length),
3739             child.GetCU());
3740       }
3741     }
3742     if (!call_origin && !call_target) {
3743       LLDB_LOG(log, "CollectCallEdges: call site without any call target");
3744       continue;
3745     }
3746 
3747     addr_t caller_address;
3748     CallEdge::AddrType caller_address_type;
3749     if (return_pc != LLDB_INVALID_ADDRESS) {
3750       caller_address = return_pc;
3751       caller_address_type = CallEdge::AddrType::AfterCall;
3752     } else if (low_pc != LLDB_INVALID_ADDRESS) {
3753       caller_address = low_pc;
3754       caller_address_type = CallEdge::AddrType::AfterCall;
3755     } else if (call_inst_pc != LLDB_INVALID_ADDRESS) {
3756       caller_address = call_inst_pc;
3757       caller_address_type = CallEdge::AddrType::Call;
3758     } else {
3759       LLDB_LOG(log, "CollectCallEdges: No caller address");
3760       continue;
3761     }
3762     // Adjust any PC forms. It needs to be fixed up if the main executable
3763     // contains a debug map (i.e. pointers to object files), because we need a
3764     // file address relative to the executable's text section.
3765     caller_address = FixupAddress(caller_address);
3766 
3767     // Extract call site parameters.
3768     CallSiteParameterArray parameters =
3769         CollectCallSiteParameters(module, child);
3770 
3771     std::unique_ptr<CallEdge> edge;
3772     if (call_origin) {
3773       LLDB_LOG(log,
3774                "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x}) "
3775                "(call-PC: {2:x})",
3776                call_origin->GetPubname(), return_pc, call_inst_pc);
3777       edge = std::make_unique<DirectCallEdge>(
3778           call_origin->GetMangledName(), caller_address_type, caller_address,
3779           tail_call, std::move(parameters));
3780     } else {
3781       if (log) {
3782         StreamString call_target_desc;
3783         call_target->GetDescription(&call_target_desc, eDescriptionLevelBrief,
3784                                     LLDB_INVALID_ADDRESS, nullptr);
3785         LLDB_LOG(log, "CollectCallEdges: Found indirect call target: {0}",
3786                  call_target_desc.GetString());
3787       }
3788       edge = std::make_unique<IndirectCallEdge>(
3789           *call_target, caller_address_type, caller_address, tail_call,
3790           std::move(parameters));
3791     }
3792 
3793     if (log && parameters.size()) {
3794       for (const CallSiteParameter &param : parameters) {
3795         StreamString callee_loc_desc, caller_loc_desc;
3796         param.LocationInCallee.GetDescription(&callee_loc_desc,
3797                                               eDescriptionLevelBrief,
3798                                               LLDB_INVALID_ADDRESS, nullptr);
3799         param.LocationInCaller.GetDescription(&caller_loc_desc,
3800                                               eDescriptionLevelBrief,
3801                                               LLDB_INVALID_ADDRESS, nullptr);
3802         LLDB_LOG(log, "CollectCallEdges: \tparam: {0} => {1}",
3803                  callee_loc_desc.GetString(), caller_loc_desc.GetString());
3804       }
3805     }
3806 
3807     call_edges.push_back(std::move(edge));
3808   }
3809   return call_edges;
3810 }
3811 
3812 std::vector<std::unique_ptr<lldb_private::CallEdge>>
ParseCallEdgesInFunction(UserID func_id)3813 SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) {
3814   // ParseCallEdgesInFunction must be called at the behest of an exclusively
3815   // locked lldb::Function instance. Storage for parsed call edges is owned by
3816   // the lldb::Function instance: locking at the SymbolFile level would be too
3817   // late, because the act of storing results from ParseCallEdgesInFunction
3818   // would be racy.
3819   DWARFDIE func_die = GetDIE(func_id.GetID());
3820   if (func_die.IsValid())
3821     return CollectCallEdges(GetObjectFile()->GetModule(), func_die);
3822   return {};
3823 }
3824 
3825 // PluginInterface protocol
GetPluginName()3826 ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); }
3827 
GetPluginVersion()3828 uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; }
3829 
Dump(lldb_private::Stream & s)3830 void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
3831   SymbolFile::Dump(s);
3832   m_index->Dump(s);
3833 }
3834 
DumpClangAST(Stream & s)3835 void SymbolFileDWARF::DumpClangAST(Stream &s) {
3836   auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
3837   if (!ts_or_err)
3838     return;
3839   TypeSystemClang *clang =
3840       llvm::dyn_cast_or_null<TypeSystemClang>(&ts_or_err.get());
3841   if (!clang)
3842     return;
3843   clang->Dump(s);
3844 }
3845 
GetDebugMapSymfile()3846 SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
3847   if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) {
3848     lldb::ModuleSP module_sp(m_debug_map_module_wp.lock());
3849     if (module_sp) {
3850       m_debug_map_symfile =
3851           static_cast<SymbolFileDWARFDebugMap *>(module_sp->GetSymbolFile());
3852     }
3853   }
3854   return m_debug_map_symfile;
3855 }
3856 
GetDwpSymbolFile()3857 const std::shared_ptr<SymbolFileDWARFDwo> &SymbolFileDWARF::GetDwpSymbolFile() {
3858   llvm::call_once(m_dwp_symfile_once_flag, [this]() {
3859     ModuleSpec module_spec;
3860     module_spec.GetFileSpec() = m_objfile_sp->GetFileSpec();
3861     module_spec.GetSymbolFileSpec() =
3862         FileSpec(m_objfile_sp->GetModule()->GetFileSpec().GetPath() + ".dwp");
3863 
3864     FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
3865     FileSpec dwp_filespec =
3866         Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
3867     if (FileSystem::Instance().Exists(dwp_filespec)) {
3868       DataBufferSP dwp_file_data_sp;
3869       lldb::offset_t dwp_file_data_offset = 0;
3870       ObjectFileSP dwp_obj_file = ObjectFile::FindPlugin(
3871           GetObjectFile()->GetModule(), &dwp_filespec, 0,
3872           FileSystem::Instance().GetByteSize(dwp_filespec), dwp_file_data_sp,
3873           dwp_file_data_offset);
3874       if (!dwp_obj_file)
3875         return;
3876       m_dwp_symfile =
3877           std::make_shared<SymbolFileDWARFDwo>(*this, dwp_obj_file, 0x3fffffff);
3878     }
3879   });
3880   return m_dwp_symfile;
3881 }
3882 
GetTypeSystem(DWARFUnit & unit)3883 llvm::Expected<TypeSystem &> SymbolFileDWARF::GetTypeSystem(DWARFUnit &unit) {
3884   return unit.GetSymbolFileDWARF().GetTypeSystemForLanguage(GetLanguage(unit));
3885 }
3886 
GetDWARFParser(DWARFUnit & unit)3887 DWARFASTParser *SymbolFileDWARF::GetDWARFParser(DWARFUnit &unit) {
3888   auto type_system_or_err = GetTypeSystem(unit);
3889   if (auto err = type_system_or_err.takeError()) {
3890     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
3891                    std::move(err), "Unable to get DWARFASTParser");
3892     return nullptr;
3893   }
3894   return type_system_or_err->GetDWARFParser();
3895 }
3896 
GetDecl(const DWARFDIE & die)3897 CompilerDecl SymbolFileDWARF::GetDecl(const DWARFDIE &die) {
3898   if (DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU()))
3899     return dwarf_ast->GetDeclForUIDFromDWARF(die);
3900   return CompilerDecl();
3901 }
3902 
GetDeclContext(const DWARFDIE & die)3903 CompilerDeclContext SymbolFileDWARF::GetDeclContext(const DWARFDIE &die) {
3904   if (DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU()))
3905     return dwarf_ast->GetDeclContextForUIDFromDWARF(die);
3906   return CompilerDeclContext();
3907 }
3908 
3909 CompilerDeclContext
GetContainingDeclContext(const DWARFDIE & die)3910 SymbolFileDWARF::GetContainingDeclContext(const DWARFDIE &die) {
3911   if (DWARFASTParser *dwarf_ast = GetDWARFParser(*die.GetCU()))
3912     return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die);
3913   return CompilerDeclContext();
3914 }
3915 
GetDWARFDeclContext(const DWARFDIE & die)3916 DWARFDeclContext SymbolFileDWARF::GetDWARFDeclContext(const DWARFDIE &die) {
3917   if (!die.IsValid())
3918     return {};
3919   DWARFDeclContext dwarf_decl_ctx =
3920       die.GetDIE()->GetDWARFDeclContext(die.GetCU());
3921   dwarf_decl_ctx.SetLanguage(GetLanguage(*die.GetCU()));
3922   return dwarf_decl_ctx;
3923 }
3924 
LanguageTypeFromDWARF(uint64_t val)3925 LanguageType SymbolFileDWARF::LanguageTypeFromDWARF(uint64_t val) {
3926   // Note: user languages between lo_user and hi_user must be handled
3927   // explicitly here.
3928   switch (val) {
3929   case DW_LANG_Mips_Assembler:
3930     return eLanguageTypeMipsAssembler;
3931   case DW_LANG_GOOGLE_RenderScript:
3932     return eLanguageTypeExtRenderScript;
3933   default:
3934     return static_cast<LanguageType>(val);
3935   }
3936 }
3937 
GetLanguage(DWARFUnit & unit)3938 LanguageType SymbolFileDWARF::GetLanguage(DWARFUnit &unit) {
3939   return LanguageTypeFromDWARF(unit.GetDWARFLanguageType());
3940 }
3941