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