xref: /llvm-project/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h (revision 28d14904c00b74154b8dfa71d5b062a7e590c44c)
1 //===-- SymbolFileDWARF.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H
11 
12 #include <list>
13 #include <map>
14 #include <mutex>
15 #include <optional>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Support/Threading.h"
22 
23 #include "lldb/Core/UniqueCStringMap.h"
24 #include "lldb/Core/dwarf.h"
25 #include "lldb/Expression/DWARFExpressionList.h"
26 #include "lldb/Symbol/DebugMacros.h"
27 #include "lldb/Symbol/SymbolContext.h"
28 #include "lldb/Symbol/SymbolFile.h"
29 #include "lldb/Target/Statistics.h"
30 #include "lldb/Utility/ConstString.h"
31 #include "lldb/Utility/Flags.h"
32 #include "lldb/Utility/RangeMap.h"
33 #include "lldb/Utility/StructuredData.h"
34 #include "lldb/lldb-private.h"
35 
36 #include "DWARFContext.h"
37 #include "DWARFDataExtractor.h"
38 #include "DWARFDefines.h"
39 #include "DWARFIndex.h"
40 #include "UniqueDWARFASTType.h"
41 
42 class DWARFASTParserClang;
43 
44 namespace llvm {
45 class DWARFDebugAbbrev;
46 } // namespace llvm
47 
48 namespace lldb_private::plugin {
49 namespace dwarf {
50 // Forward Declarations for this DWARF plugin
51 class DebugMapModule;
52 class DWARFCompileUnit;
53 class DWARFDebugAranges;
54 class DWARFDebugInfo;
55 class DWARFDebugInfoEntry;
56 class DWARFDebugLine;
57 class DWARFDeclContext;
58 class DWARFFormValue;
59 class DWARFTypeUnit;
60 class SymbolFileDWARFDebugMap;
61 class SymbolFileDWARFDwo;
62 class SymbolFileDWARFDwp;
63 
64 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
65 
66 class SymbolFileDWARF : public SymbolFileCommon {
67   /// LLVM RTTI support.
68   static char ID;
69 
70 public:
71   /// LLVM RTTI support.
72   /// \{
73   bool isA(const void *ClassID) const override {
74     return ClassID == &ID || SymbolFileCommon::isA(ClassID);
75   }
76   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
77   /// \}
78 
79   friend class SymbolFileDWARFDebugMap;
80   friend class SymbolFileDWARFDwo;
81   friend class DebugMapModule;
82   friend class DWARFCompileUnit;
83   friend class DWARFDIE;
84   friend class DWARFASTParser;
85 
86   // Static Functions
87   static void Initialize();
88 
89   static void Terminate();
90 
91   static void DebuggerInitialize(Debugger &debugger);
92 
93   static llvm::StringRef GetPluginNameStatic() { return "dwarf"; }
94 
95   static llvm::StringRef GetPluginDescriptionStatic();
96 
97   static SymbolFile *CreateInstance(lldb::ObjectFileSP objfile_sp);
98 
99   // Constructors and Destructors
100 
101   SymbolFileDWARF(lldb::ObjectFileSP objfile_sp, SectionList *dwo_section_list);
102 
103   ~SymbolFileDWARF() override;
104 
105   uint32_t CalculateAbilities() override;
106 
107   void InitializeObject() override;
108 
109   // Compile Unit function calls
110 
111   lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override;
112 
113   XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) override;
114 
115   size_t ParseFunctions(CompileUnit &comp_unit) override;
116 
117   bool ParseLineTable(CompileUnit &comp_unit) override;
118 
119   bool ParseDebugMacros(CompileUnit &comp_unit) override;
120 
121   bool ForEachExternalModule(CompileUnit &, llvm::DenseSet<SymbolFile *> &,
122                              llvm::function_ref<bool(Module &)>) override;
123 
124   bool ParseSupportFiles(CompileUnit &comp_unit,
125                          SupportFileList &support_files) override;
126 
127   bool ParseIsOptimized(CompileUnit &comp_unit) override;
128 
129   size_t ParseTypes(CompileUnit &comp_unit) override;
130 
131   bool
132   ParseImportedModules(const SymbolContext &sc,
133                        std::vector<SourceModule> &imported_modules) override;
134 
135   size_t ParseBlocksRecursive(Function &func) override;
136 
137   size_t ParseVariablesForContext(const SymbolContext &sc) override;
138 
139   std::optional<ArrayInfo>
140   GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,
141                             const ExecutionContext *exe_ctx) override;
142 
143   bool CompleteType(CompilerType &compiler_type) override;
144 
145   Type *ResolveType(const DWARFDIE &die, bool assert_not_being_parsed = true,
146                     bool resolve_function_context = false);
147 
148   CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
149 
150   CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) override;
151 
152   CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
153 
154   std::vector<CompilerContext>
155   GetCompilerContextForUID(lldb::user_id_t uid) override;
156 
157   void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
158 
159   uint32_t ResolveSymbolContext(const Address &so_addr,
160                                 lldb::SymbolContextItem resolve_scope,
161                                 SymbolContext &sc) override;
162 
163   Status CalculateFrameVariableError(StackFrame &frame) override;
164 
165   uint32_t ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
166                                 lldb::SymbolContextItem resolve_scope,
167                                 SymbolContextList &sc_list) override;
168 
169   void FindGlobalVariables(ConstString name,
170                            const CompilerDeclContext &parent_decl_ctx,
171                            uint32_t max_matches,
172                            VariableList &variables) override;
173 
174   void FindGlobalVariables(const RegularExpression &regex, uint32_t max_matches,
175                            VariableList &variables) override;
176 
177   void FindFunctions(const Module::LookupInfo &lookup_info,
178                      const CompilerDeclContext &parent_decl_ctx,
179                      bool include_inlines, SymbolContextList &sc_list) override;
180 
181   void FindFunctions(const RegularExpression &regex, bool include_inlines,
182                      SymbolContextList &sc_list) override;
183 
184   void
185   GetMangledNamesForFunction(const std::string &scope_qualified_name,
186                              std::vector<ConstString> &mangled_names) override;
187 
188   uint64_t GetDebugInfoSize(bool load_all_debug_info = false) override;
189 
190   void FindTypes(const lldb_private::TypeQuery &match,
191                  lldb_private::TypeResults &results) override;
192 
193   void GetTypes(SymbolContextScope *sc_scope, lldb::TypeClass type_mask,
194                 TypeList &type_list) override;
195 
196   llvm::Expected<lldb::TypeSystemSP>
197   GetTypeSystemForLanguage(lldb::LanguageType language) override;
198 
199   CompilerDeclContext FindNamespace(ConstString name,
200                                     const CompilerDeclContext &parent_decl_ctx,
201                                     bool only_root_namespaces) override;
202 
203   void PreloadSymbols() override;
204 
205   std::recursive_mutex &GetModuleMutex() const override;
206 
207   // PluginInterface protocol
208   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
209 
210   llvm::DWARFDebugAbbrev *DebugAbbrev();
211 
212   DWARFDebugInfo &DebugInfo();
213 
214   static bool SupportedVersion(uint16_t version);
215 
216   DWARFDIE
217   GetDeclContextDIEContainingDIE(const DWARFDIE &die);
218 
219   bool HasForwardDeclForCompilerType(const CompilerType &compiler_type);
220 
221   CompileUnit *GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu);
222 
223   virtual void GetObjCMethods(ConstString class_name,
224                               llvm::function_ref<bool(DWARFDIE die)> callback);
225 
226   DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
227 
228   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
229 
230   lldb::ModuleSP GetExternalModule(ConstString name);
231 
232   typedef std::map<ConstString, lldb::ModuleSP> ExternalTypeModuleMap;
233 
234   /// Return the list of Clang modules imported by this SymbolFile.
235   const ExternalTypeModuleMap &getExternalTypeModules() const {
236     return m_external_type_modules;
237   }
238 
239   /// Given a DIERef, find the correct SymbolFileDWARF.
240   ///
241   /// A DIERef contains a file index that can uniquely identify a N_OSO file for
242   /// DWARF in .o files on mac, or a .dwo or .dwp file index for split DWARF.
243   /// Calling this function will find the correct symbol file to use so that
244   /// further lookups can be done on the correct symbol file so that the DIE
245   /// offset makes sense in the DIERef.
246   virtual SymbolFileDWARF *GetDIERefSymbolFile(const DIERef &die_ref);
247 
248   virtual DWARFDIE GetDIE(const DIERef &die_ref);
249 
250   DWARFDIE GetDIE(lldb::user_id_t uid);
251 
252   std::shared_ptr<SymbolFileDWARFDwo>
253   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
254                                  const DWARFDebugInfoEntry &cu_die);
255 
256   /// If this is a DWARF object with a single CU, return its DW_AT_dwo_id.
257   std::optional<uint64_t> GetDWOId();
258 
259   /// Given a DWO DWARFUnit, find the corresponding skeleton DWARFUnit
260   /// in the main symbol file. DWP files can have their DWARFUnits
261   /// parsed without the skeleton compile units having been parsed, so
262   /// sometimes we need to find the skeleton compile unit for a DWO
263   /// DWARFUnit so we can fill in this link. Currently unless the
264   /// skeleton compile unit has been parsed _and_ the Unit DIE has been
265   /// parsed, the DWO unit will not have a backward link setup correctly
266   /// which was causing crashes due to an assertion that was firing
267   /// in SymbolFileDWARF::GetCompUnitForDWARFCompUnit().
268   DWARFUnit *GetSkeletonUnit(DWARFUnit *dwo_unit);
269 
270   static bool DIEInDeclContext(const CompilerDeclContext &parent_decl_ctx,
271                                const DWARFDIE &die,
272                                bool only_root_namespaces = false);
273 
274   std::vector<std::unique_ptr<CallEdge>>
275   ParseCallEdgesInFunction(UserID func_id) override;
276 
277   void Dump(Stream &s) override;
278 
279   void DumpClangAST(Stream &s) override;
280 
281   /// List separate dwo files.
282   bool GetSeparateDebugInfo(StructuredData::Dictionary &d,
283                             bool errors_only) override;
284 
285   DWARFContext &GetDWARFContext() { return m_context; }
286 
287   const std::shared_ptr<SymbolFileDWARFDwo> &GetDwpSymbolFile();
288 
289   FileSpec GetFile(DWARFUnit &unit, size_t file_idx);
290 
291   static llvm::Expected<lldb::TypeSystemSP> GetTypeSystem(DWARFUnit &unit);
292 
293   static DWARFASTParser *GetDWARFParser(DWARFUnit &unit);
294 
295   // CompilerDecl related functions
296 
297   static CompilerDecl GetDecl(const DWARFDIE &die);
298 
299   static CompilerDeclContext GetDeclContext(const DWARFDIE &die);
300 
301   static CompilerDeclContext GetContainingDeclContext(const DWARFDIE &die);
302 
303   static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
304 
305   static lldb::LanguageType GetLanguage(DWARFUnit &unit);
306   /// Same as GetLanguage() but reports all C++ versions as C++ (no version).
307   static lldb::LanguageType GetLanguageFamily(DWARFUnit &unit);
308 
309   StatsDuration::Duration GetDebugInfoParseTime() override {
310     return m_parse_time;
311   }
312   StatsDuration::Duration GetDebugInfoIndexTime() override;
313 
314   StatsDuration &GetDebugInfoParseTimeRef() { return m_parse_time; }
315 
316   void ResetStatistics() override;
317 
318   virtual lldb::offset_t
319   GetVendorDWARFOpcodeSize(const DataExtractor &data,
320                            const lldb::offset_t data_offset,
321                            const uint8_t op) const {
322     return LLDB_INVALID_OFFSET;
323   }
324 
325   virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
326                                       lldb::offset_t &offset,
327                                       std::vector<Value> &stack) const {
328     return false;
329   }
330 
331   ConstString ConstructFunctionDemangledName(const DWARFDIE &die);
332 
333   std::optional<uint64_t> GetFileIndex() const { return m_file_index; }
334   void SetFileIndex(std::optional<uint64_t> file_index) {
335     m_file_index = file_index;
336   }
337 
338   virtual llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &GetDIEToType();
339 
340   virtual llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> &
341   GetForwardDeclCompilerTypeToDIE();
342 
343   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
344       DIEToVariableSP;
345 
346   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
347 
348   virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
349 
350   bool ClassOrStructIsVirtual(const DWARFDIE &die);
351 
352   SymbolFileDWARFDebugMap *GetDebugMapSymfile();
353 
354   virtual DWARFDIE FindDefinitionDIE(const DWARFDIE &die);
355 
356   virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
357       const DWARFDIE &die, ConstString type_name, bool must_be_implementation);
358 
359   Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
360 
361   Type *ResolveTypeUID(const DWARFDIE &die, bool assert_not_being_parsed);
362 
363   Type *ResolveTypeUID(const DIERef &die_ref);
364 
365   /// Returns the DWARFIndex for this symbol, if it exists.
366   DWARFIndex *getIndex() { return m_index.get(); }
367 
368 protected:
369   SymbolFileDWARF(const SymbolFileDWARF &) = delete;
370   const SymbolFileDWARF &operator=(const SymbolFileDWARF &) = delete;
371 
372   virtual void LoadSectionData(lldb::SectionType sect_type,
373                                DWARFDataExtractor &data);
374 
375   bool DeclContextMatchesThisSymbolFile(const CompilerDeclContext &decl_ctx);
376 
377   uint32_t CalculateNumCompileUnits() override;
378 
379   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
380 
381   TypeList &GetTypeList() override;
382 
383   lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
384 
385   virtual DWARFCompileUnit *GetDWARFCompileUnit(CompileUnit *comp_unit);
386 
387   DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
388 
389   bool GetFunction(const DWARFDIE &die, SymbolContext &sc);
390 
391   Function *ParseFunction(CompileUnit &comp_unit, const DWARFDIE &die);
392 
393   size_t ParseBlocksRecursive(CompileUnit &comp_unit, Block *parent_block,
394                               DWARFDIE die, lldb::addr_t subprogram_low_pc);
395 
396   size_t ParseTypes(const SymbolContext &sc, const DWARFDIE &die,
397                     bool parse_siblings, bool parse_children);
398 
399   lldb::TypeSP ParseType(const SymbolContext &sc, const DWARFDIE &die,
400                          bool *type_is_new);
401 
402   bool ParseSupportFiles(DWARFUnit &dwarf_cu, const lldb::ModuleSP &module,
403                          SupportFileList &support_files);
404 
405   lldb::VariableSP ParseVariableDIE(const SymbolContext &sc,
406                                     const DWARFDIE &die,
407                                     const lldb::addr_t func_low_pc);
408   lldb::VariableSP ParseVariableDIECached(const SymbolContext &sc,
409                                           const DWARFDIE &die);
410 
411   void ParseAndAppendGlobalVariable(const SymbolContext &sc,
412                                     const DWARFDIE &die,
413                                     VariableList &cc_variable_list);
414 
415   size_t ParseVariablesInFunctionContext(const SymbolContext &sc,
416                                          const DWARFDIE &die,
417                                          const lldb::addr_t func_low_pc);
418 
419   size_t ParseVariablesInFunctionContextRecursive(const SymbolContext &sc,
420                                                   const DWARFDIE &die,
421                                                   lldb::addr_t func_low_pc,
422                                                   DIEArray &accumulator);
423 
424   size_t PopulateBlockVariableList(VariableList &variable_list,
425                                    const SymbolContext &sc,
426                                    llvm::ArrayRef<DIERef> variable_dies,
427                                    lldb::addr_t func_low_pc);
428 
429   DIEArray MergeBlockAbstractParameters(const DWARFDIE &block_die,
430                                         DIEArray &&variable_dies);
431 
432   // Given a die_offset, figure out the symbol context representing that die.
433   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
434                        SymbolContextList &sc_list);
435 
436   /// Resolve functions and (possibly) blocks for the given file address and a
437   /// compile unit. The compile unit comes from the sc argument and it must be
438   /// set. The results of the lookup (if any) are written back to the symbol
439   /// context.
440   void ResolveFunctionAndBlock(lldb::addr_t file_vm_addr, bool lookup_block,
441                                SymbolContext &sc);
442 
443   Symbol *GetObjCClassSymbol(ConstString objc_class_name);
444 
445   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
446                              bool resolve_function_context = false);
447 
448   void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
449     m_debug_map_module_wp = module_sp;
450   }
451 
452   DWARFDIE
453   FindBlockContainingSpecification(const DIERef &func_die_ref,
454                                    dw_offset_t spec_block_die_offset);
455 
456   DWARFDIE
457   FindBlockContainingSpecification(const DWARFDIE &die,
458                                    dw_offset_t spec_block_die_offset);
459 
460   bool ClassContainsSelector(const DWARFDIE &class_die, ConstString selector);
461 
462   /// Parse call site entries (DW_TAG_call_site), including any nested call site
463   /// parameters (DW_TAG_call_site_parameter).
464   std::vector<std::unique_ptr<CallEdge>>
465   CollectCallEdges(lldb::ModuleSP module, DWARFDIE function_die);
466 
467   /// If this symbol file is linked to by a debug map (see
468   /// SymbolFileDWARFDebugMap), and \p file_addr is a file address relative to
469   /// an object file, adjust \p file_addr so that it is relative to the main
470   /// binary. Returns the adjusted address, or \p file_addr if no adjustment is
471   /// needed, on success and LLDB_INVALID_ADDRESS otherwise.
472   lldb::addr_t FixupAddress(lldb::addr_t file_addr);
473 
474   bool FixupAddress(Address &addr);
475 
476   typedef llvm::SetVector<Type *> TypeSet;
477 
478   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
479                 dw_offset_t max_die_offset, uint32_t type_mask,
480                 TypeSet &type_set);
481 
482   typedef RangeDataVector<lldb::addr_t, lldb::addr_t, Variable *>
483       GlobalVariableMap;
484 
485   GlobalVariableMap &GetGlobalAranges();
486 
487   void UpdateExternalModuleListIfNeeded();
488 
489   void BuildCuTranslationTable();
490   std::optional<uint32_t> GetDWARFUnitIndex(uint32_t cu_idx);
491 
492   void FindDwpSymbolFile();
493 
494   const SupportFileList *GetTypeUnitSupportFiles(DWARFTypeUnit &tu);
495 
496   void InitializeFirstCodeAddressRecursive(const SectionList &section_list);
497 
498   void InitializeFirstCodeAddress();
499 
500   void
501   GetCompileOptions(std::unordered_map<lldb::CompUnitSP, Args> &args) override;
502 
503   lldb::ModuleWP m_debug_map_module_wp;
504   SymbolFileDWARFDebugMap *m_debug_map_symfile;
505 
506   llvm::once_flag m_dwp_symfile_once_flag;
507   std::shared_ptr<SymbolFileDWARFDwo> m_dwp_symfile;
508 
509   DWARFContext m_context;
510 
511   llvm::once_flag m_info_once_flag;
512   std::unique_ptr<DWARFDebugInfo> m_info;
513 
514   std::unique_ptr<llvm::DWARFDebugAbbrev> m_abbr;
515   std::unique_ptr<GlobalVariableMap> m_global_aranges_up;
516 
517   typedef std::unordered_map<lldb::offset_t, DebugMacrosSP> DebugMacrosMap;
518   DebugMacrosMap m_debug_macros_map;
519 
520   ExternalTypeModuleMap m_external_type_modules;
521   std::unique_ptr<DWARFIndex> m_index;
522   bool m_fetched_external_modules : 1;
523 
524   typedef std::set<DIERef> DIERefSet;
525   typedef llvm::StringMap<DIERefSet> NameToOffsetMap;
526   NameToOffsetMap m_function_scope_qualified_name_map;
527   UniqueDWARFASTTypeMap m_unique_ast_type_map;
528   // A map from DIE to lldb_private::Type. For record type, the key might be
529   // either declaration DIE or definition DIE.
530   llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> m_die_to_type;
531   DIEToVariableSP m_die_to_variable_sp;
532   // A map from CompilerType to the struct/class/union/enum DIE (might be a
533   // declaration or a definition) that is used to construct it.
534   llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef>
535       m_forward_decl_compiler_type_to_die;
536   llvm::DenseMap<dw_offset_t, std::unique_ptr<SupportFileList>>
537       m_type_unit_support_files;
538   std::vector<uint32_t> m_lldb_cu_to_dwarf_unit;
539   /// DWARF does not provide a good way for traditional (concatenating) linkers
540   /// to invalidate debug info describing dead-stripped code. These linkers will
541   /// keep the debug info but resolve any addresses referring to such code as
542   /// zero (BFD) or a small positive integer (zero + relocation addend -- GOLD).
543   /// Try to filter out this debug info by comparing it to the lowest code
544   /// address in the module.
545   lldb::addr_t m_first_code_address = LLDB_INVALID_ADDRESS;
546   StatsDuration m_parse_time;
547   std::atomic_flag m_dwo_warning_issued = ATOMIC_FLAG_INIT;
548   /// If this DWARF file a .DWO file or a DWARF .o file on mac when
549   /// no dSYM file is being used, this file index will be set to a
550   /// valid value that can be used in DIERef objects which will contain
551   /// an index that identifies the .DWO or .o file.
552   std::optional<uint64_t> m_file_index;
553 };
554 } // namespace dwarf
555 } // namespace lldb_private::plugin
556 
557 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H
558