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