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 SymbolFileDWARF_SymbolFileDWARF_h_
10 #define SymbolFileDWARF_SymbolFileDWARF_h_
11 
12 #include <list>
13 #include <map>
14 #include <mutex>
15 #include <set>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Threading.h"
21 
22 #include "lldb/Core/UniqueCStringMap.h"
23 #include "lldb/Core/dwarf.h"
24 #include "lldb/Expression/DWARFExpression.h"
25 #include "lldb/Symbol/DebugMacros.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Symbol/SymbolFile.h"
28 #include "lldb/Utility/ConstString.h"
29 #include "lldb/Utility/Flags.h"
30 #include "lldb/Utility/RangeMap.h"
31 #include "lldb/lldb-private.h"
32 
33 #include "DWARFContext.h"
34 #include "DWARFDataExtractor.h"
35 #include "DWARFDefines.h"
36 #include "DWARFIndex.h"
37 #include "UniqueDWARFASTType.h"
38 
39 // Forward Declarations for this DWARF plugin
40 class DebugMapModule;
41 class DWARFAbbreviationDeclaration;
42 class DWARFAbbreviationDeclarationSet;
43 class DWARFCompileUnit;
44 class DWARFDebugAbbrev;
45 class DWARFDebugAranges;
46 class DWARFDebugInfo;
47 class DWARFDebugInfoEntry;
48 class DWARFDebugLine;
49 class DWARFDebugRanges;
50 class DWARFDebugRngLists;
51 class DWARFDeclContext;
52 class DWARFFormValue;
53 class DWARFTypeUnit;
54 class SymbolFileDWARFDebugMap;
55 class SymbolFileDWARFDwo;
56 class SymbolFileDWARFDwp;
57 
58 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
59 
60 class SymbolFileDWARF : public lldb_private::SymbolFile,
61                         public lldb_private::UserID {
62 public:
63   friend class SymbolFileDWARFDebugMap;
64   friend class SymbolFileDWARFDwo;
65   friend class DebugMapModule;
66   friend class DWARFCompileUnit;
67   friend class DWARFDIE;
68   friend class DWARFASTParserClang;
69 
70   // Static Functions
71   static void Initialize();
72 
73   static void Terminate();
74 
75   static void DebuggerInitialize(lldb_private::Debugger &debugger);
76 
77   static lldb_private::ConstString GetPluginNameStatic();
78 
79   static const char *GetPluginDescriptionStatic();
80 
81   static lldb_private::SymbolFile *
82   CreateInstance(lldb::ObjectFileSP objfile_sp);
83 
84   static lldb_private::FileSpecList GetSymlinkPaths();
85 
86   // Constructors and Destructors
87 
88   SymbolFileDWARF(lldb::ObjectFileSP objfile_sp,
89                   lldb_private::SectionList *dwo_section_list);
90 
91   ~SymbolFileDWARF() override;
92 
93   uint32_t CalculateAbilities() override;
94 
95   void InitializeObject() override;
96 
97   // Compile Unit function calls
98 
99   lldb::LanguageType
100   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
101 
102   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
103 
104   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
105 
106   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
107 
108   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
109                          lldb_private::FileSpecList &support_files) override;
110 
111   bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override;
112 
113   size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
114 
115   bool ParseImportedModules(
116       const lldb_private::SymbolContext &sc,
117       std::vector<lldb_private::SourceModule> &imported_modules) override;
118 
119   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
120 
121   size_t
122   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
123 
124   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
125   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
126       lldb::user_id_t type_uid,
127       const lldb_private::ExecutionContext *exe_ctx) override;
128 
129   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
130 
131   lldb_private::Type *ResolveType(const DWARFDIE &die,
132                                   bool assert_not_being_parsed = true,
133                                   bool resolve_function_context = false);
134 
135   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
136 
137   lldb_private::CompilerDeclContext
138   GetDeclContextForUID(lldb::user_id_t uid) override;
139 
140   lldb_private::CompilerDeclContext
141   GetDeclContextContainingUID(lldb::user_id_t uid) override;
142 
143   void
144   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
145 
146   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
147                                 lldb::SymbolContextItem resolve_scope,
148                                 lldb_private::SymbolContext &sc) override;
149 
150   uint32_t
151   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
152                        bool check_inlines,
153                        lldb::SymbolContextItem resolve_scope,
154                        lldb_private::SymbolContextList &sc_list) override;
155 
156   uint32_t
157   FindGlobalVariables(lldb_private::ConstString name,
158                       const lldb_private::CompilerDeclContext *parent_decl_ctx,
159                       uint32_t max_matches,
160                       lldb_private::VariableList &variables) override;
161 
162   uint32_t FindGlobalVariables(const lldb_private::RegularExpression &regex,
163                                uint32_t max_matches,
164                                lldb_private::VariableList &variables) override;
165 
166   uint32_t
167   FindFunctions(lldb_private::ConstString name,
168                 const lldb_private::CompilerDeclContext *parent_decl_ctx,
169                 lldb::FunctionNameType name_type_mask, bool include_inlines,
170                 bool append, lldb_private::SymbolContextList &sc_list) override;
171 
172   uint32_t FindFunctions(const lldb_private::RegularExpression &regex,
173                          bool include_inlines, bool append,
174                          lldb_private::SymbolContextList &sc_list) override;
175 
176   void GetMangledNamesForFunction(
177       const std::string &scope_qualified_name,
178       std::vector<lldb_private::ConstString> &mangled_names) override;
179 
180   uint32_t
181   FindTypes(lldb_private::ConstString name,
182             const lldb_private::CompilerDeclContext *parent_decl_ctx,
183             bool append, uint32_t max_matches,
184             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
185             lldb_private::TypeMap &types) override;
186 
187   size_t FindTypes(const std::vector<lldb_private::CompilerContext> &context,
188                    bool append, lldb_private::TypeMap &types) override;
189 
190   size_t GetTypes(lldb_private::SymbolContextScope *sc_scope,
191                   lldb::TypeClass type_mask,
192                   lldb_private::TypeList &type_list) override;
193 
194   llvm::Expected<lldb_private::TypeSystem &>
195   GetTypeSystemForLanguage(lldb::LanguageType language) override;
196 
197   lldb_private::CompilerDeclContext FindNamespace(
198       lldb_private::ConstString name,
199       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
200 
201   void PreloadSymbols() override;
202 
203   std::recursive_mutex &GetModuleMutex() const override;
204 
205   // PluginInterface protocol
206   lldb_private::ConstString GetPluginName() override;
207 
208   uint32_t GetPluginVersion() override;
209 
210   const lldb_private::DWARFDataExtractor &get_debug_loc_data();
211   const lldb_private::DWARFDataExtractor &get_debug_loclists_data();
212 
213   DWARFDebugAbbrev *DebugAbbrev();
214 
215   const DWARFDebugAbbrev *DebugAbbrev() const;
216 
217   DWARFDebugInfo *DebugInfo();
218 
219   const DWARFDebugInfo *DebugInfo() const;
220 
221   DWARFDebugRanges *GetDebugRanges();
222   DWARFDebugRngLists *GetDebugRngLists();
223 
224   const lldb_private::DWARFDataExtractor &DebugLocData();
225 
226   static bool SupportedVersion(uint16_t version);
227 
228   DWARFDIE
229   GetDeclContextDIEContainingDIE(const DWARFDIE &die);
230 
231   bool
232   HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type);
233 
234   lldb_private::CompileUnit *
235   GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu);
236 
237   virtual size_t GetObjCMethodDIEOffsets(lldb_private::ConstString class_name,
238                                          DIEArray &method_die_offsets);
239 
240   bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu);
241 
242   lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
243 
244   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
245 
246   virtual lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
247 
248   virtual lldb_private::DWARFExpression::LocationListFormat
249   GetLocationListFormat() const;
250 
251   lldb::ModuleSP GetDWOModule(lldb_private::ConstString name);
252 
253   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
254       ExternalTypeModuleMap;
255 
256   /// Return the list of Clang modules imported by this SymbolFile.
257   const ExternalTypeModuleMap& getExternalTypeModules() const {
258       return m_external_type_modules;
259   }
260 
261   virtual DWARFDIE GetDIE(const DIERef &die_ref);
262 
263   DWARFDIE GetDIE(lldb::user_id_t uid);
264 
265   lldb::user_id_t GetUID(const DWARFBaseDIE &die) {
266     return GetUID(die.GetDIERef());
267   }
268 
269   lldb::user_id_t GetUID(const llvm::Optional<DIERef> &ref) {
270     return ref ? GetUID(*ref) : LLDB_INVALID_UID;
271   }
272 
273   lldb::user_id_t GetUID(DIERef ref);
274 
275   virtual std::unique_ptr<SymbolFileDWARFDwo>
276   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
277                                  const DWARFDebugInfoEntry &cu_die);
278 
279   // For regular SymbolFileDWARF instances the method returns nullptr,
280   // for the instances of the subclass SymbolFileDWARFDwo
281   // the method returns a pointer to the base compile unit.
282   virtual DWARFCompileUnit *GetBaseCompileUnit() { return nullptr; }
283 
284   virtual llvm::Optional<uint32_t> GetDwoNum() { return llvm::None; }
285 
286   static bool
287   DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
288                    const DWARFDIE &die);
289 
290   std::vector<lldb_private::CallEdge>
291   ParseCallEdgesInFunction(UserID func_id) override;
292 
293   void Dump(lldb_private::Stream &s) override;
294 
295   void DumpClangAST(lldb_private::Stream &s) override;
296 
297   lldb_private::DWARFContext &GetDWARFContext() { return m_context; }
298 
299   lldb_private::FileSpec GetFile(DWARFUnit &unit, size_t file_idx);
300 
301 protected:
302   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
303       DIEToTypePtr;
304   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
305       DIEToVariableSP;
306   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
307                          lldb::opaque_compiler_type_t>
308       DIEToClangType;
309   typedef llvm::DenseMap<lldb::opaque_compiler_type_t, lldb::user_id_t>
310       ClangTypeToDIE;
311 
312   struct DWARFDataSegment {
313     llvm::once_flag m_flag;
314     lldb_private::DWARFDataExtractor m_data;
315   };
316 
317   DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
318 
319   const lldb_private::DWARFDataExtractor &
320   GetCachedSectionData(lldb::SectionType sect_type,
321                        DWARFDataSegment &data_segment);
322 
323   virtual void LoadSectionData(lldb::SectionType sect_type,
324                                lldb_private::DWARFDataExtractor &data);
325 
326   bool DeclContextMatchesThisSymbolFile(
327       const lldb_private::CompilerDeclContext *decl_ctx);
328 
329   uint32_t CalculateNumCompileUnits() override;
330 
331   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
332 
333   lldb_private::TypeList &GetTypeList() override;
334 
335   virtual DWARFUnit *
336   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
337 
338   DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
339 
340   bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
341 
342   lldb_private::Function *ParseFunction(lldb_private::CompileUnit &comp_unit,
343                                         const DWARFDIE &die);
344 
345   size_t ParseBlocksRecursive(lldb_private::CompileUnit &comp_unit,
346                               lldb_private::Block *parent_block,
347                               const DWARFDIE &die,
348                               lldb::addr_t subprogram_low_pc, uint32_t depth);
349 
350   size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
351                     bool parse_siblings, bool parse_children);
352 
353   lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
354                          const DWARFDIE &die, bool *type_is_new);
355 
356   lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
357                                      bool assert_not_being_parsed);
358 
359   lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
360 
361   lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
362                                     const DWARFDIE &die,
363                                     const lldb::addr_t func_low_pc);
364 
365   size_t ParseVariables(const lldb_private::SymbolContext &sc,
366                         const DWARFDIE &orig_die,
367                         const lldb::addr_t func_low_pc, bool parse_siblings,
368                         bool parse_children,
369                         lldb_private::VariableList *cc_variable_list = nullptr);
370 
371   bool ClassOrStructIsVirtual(const DWARFDIE &die);
372 
373   // Given a die_offset, figure out the symbol context representing that die.
374   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
375                        lldb_private::SymbolContextList &sc_list);
376 
377   virtual lldb::TypeSP
378   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
379 
380   virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
381       const DWARFDIE &die, lldb_private::ConstString type_name,
382       bool must_be_implementation);
383 
384   lldb_private::Symbol *
385   GetObjCClassSymbol(lldb_private::ConstString objc_class_name);
386 
387   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
388                              bool resolve_function_context = false);
389 
390   void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
391     m_debug_map_module_wp = module_sp;
392   }
393 
394   SymbolFileDWARFDebugMap *GetDebugMapSymfile();
395 
396   DWARFDIE
397   FindBlockContainingSpecification(const DIERef &func_die_ref,
398                                    dw_offset_t spec_block_die_offset);
399 
400   DWARFDIE
401   FindBlockContainingSpecification(const DWARFDIE &die,
402                                    dw_offset_t spec_block_die_offset);
403 
404   virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
405 
406   bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2);
407 
408   bool ClassContainsSelector(const DWARFDIE &class_die,
409                              lldb_private::ConstString selector);
410 
411   bool FixupAddress(lldb_private::Address &addr);
412 
413   typedef std::set<lldb_private::Type *> TypeSet;
414 
415   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
416                 dw_offset_t max_die_offset, uint32_t type_mask,
417                 TypeSet &type_set);
418 
419   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
420                                         lldb_private::Variable *>
421       GlobalVariableMap;
422 
423   GlobalVariableMap &GetGlobalAranges();
424 
425   void UpdateExternalModuleListIfNeeded();
426 
427   virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
428 
429   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
430 
431   virtual DIEToClangType &GetForwardDeclDieToClangType() {
432     return m_forward_decl_die_to_clang_type;
433   }
434 
435   virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
436     return m_forward_decl_clang_type_to_die;
437   }
438 
439   void BuildCuTranslationTable();
440   llvm::Optional<uint32_t> GetDWARFUnitIndex(uint32_t cu_idx);
441 
442   struct DecodedUID {
443     SymbolFileDWARF &dwarf;
444     DIERef ref;
445   };
446   llvm::Optional<DecodedUID> DecodeUID(lldb::user_id_t uid);
447 
448   SymbolFileDWARFDwp *GetDwpSymbolFile();
449 
450   const lldb_private::FileSpecList &GetTypeUnitSupportFiles(DWARFTypeUnit &tu);
451 
452   lldb::ModuleWP m_debug_map_module_wp;
453   SymbolFileDWARFDebugMap *m_debug_map_symfile;
454 
455   llvm::once_flag m_dwp_symfile_once_flag;
456   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
457 
458   lldb_private::DWARFContext m_context;
459 
460   DWARFDataSegment m_data_debug_loc;
461   DWARFDataSegment m_data_debug_loclists;
462 
463   // The unique pointer items below are generated on demand if and when someone
464   // accesses them through a non const version of this class.
465   std::unique_ptr<DWARFDebugAbbrev> m_abbr;
466   std::unique_ptr<DWARFDebugInfo> m_info;
467   std::unique_ptr<GlobalVariableMap> m_global_aranges_up;
468 
469   typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
470       DebugMacrosMap;
471   DebugMacrosMap m_debug_macros_map;
472 
473   ExternalTypeModuleMap m_external_type_modules;
474   std::unique_ptr<lldb_private::DWARFIndex> m_index;
475   bool m_fetched_external_modules : 1;
476   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
477 
478   typedef std::set<lldb::user_id_t> DIERefSet;
479   typedef llvm::StringMap<DIERefSet> NameToOffsetMap;
480   NameToOffsetMap m_function_scope_qualified_name_map;
481   std::unique_ptr<DWARFDebugRanges> m_ranges;
482   std::unique_ptr<DWARFDebugRngLists> m_rnglists;
483   UniqueDWARFASTTypeMap m_unique_ast_type_map;
484   DIEToTypePtr m_die_to_type;
485   DIEToVariableSP m_die_to_variable_sp;
486   DIEToClangType m_forward_decl_die_to_clang_type;
487   ClangTypeToDIE m_forward_decl_clang_type_to_die;
488   llvm::DenseMap<dw_offset_t, lldb_private::FileSpecList>
489       m_type_unit_support_files;
490   std::vector<uint32_t> m_lldb_cu_to_dwarf_unit;
491 };
492 
493 #endif // SymbolFileDWARF_SymbolFileDWARF_h_
494