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