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