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