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   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   virtual DWARFDIE GetDIE(const DIERef &die_ref);
297 
298   virtual std::unique_ptr<SymbolFileDWARFDwo>
299   GetDwoSymbolFileForCompileUnit(DWARFCompileUnit &dwarf_cu,
300                                  const DWARFDebugInfoEntry &cu_die);
301 
302 protected:
303   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
304       DIEToTypePtr;
305   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
306       DIEToVariableSP;
307   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
308                          lldb::opaque_compiler_type_t>
309       DIEToClangType;
310   typedef llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> ClangTypeToDIE;
311 
312   struct DWARFDataSegment {
313     llvm::once_flag m_flag;
314     lldb_private::DWARFDataExtractor m_data;
315   };
316 
317   DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
318 
319   const lldb_private::DWARFDataExtractor &
320   GetCachedSectionData(lldb::SectionType sect_type,
321                        DWARFDataSegment &data_segment);
322 
323   virtual void LoadSectionData(lldb::SectionType sect_type,
324                                lldb_private::DWARFDataExtractor &data);
325 
326   bool DeclContextMatchesThisSymbolFile(
327       const lldb_private::CompilerDeclContext *decl_ctx);
328 
329   bool
330   DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
331                    const DWARFDIE &die);
332 
333   virtual DWARFCompileUnit *
334   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
335 
336   DWARFCompileUnit *GetNextUnparsedDWARFCompileUnit(DWARFCompileUnit *prev_cu);
337 
338   bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
339 
340   lldb_private::Function *
341   ParseCompileUnitFunction(const lldb_private::SymbolContext &sc,
342                            const DWARFDIE &die);
343 
344   size_t ParseFunctionBlocks(const lldb_private::SymbolContext &sc,
345                              lldb_private::Block *parent_block,
346                              const DWARFDIE &die,
347                              lldb::addr_t subprogram_low_pc, uint32_t depth);
348 
349   size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
350                     bool parse_siblings, bool parse_children);
351 
352   lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
353                          const DWARFDIE &die, bool *type_is_new);
354 
355   lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
356                                      bool assert_not_being_parsed);
357 
358   lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
359 
360   lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
361                                     const DWARFDIE &die,
362                                     const lldb::addr_t func_low_pc);
363 
364   size_t ParseVariables(const lldb_private::SymbolContext &sc,
365                         const DWARFDIE &orig_die,
366                         const lldb::addr_t func_low_pc, bool parse_siblings,
367                         bool parse_children,
368                         lldb_private::VariableList *cc_variable_list = NULL);
369 
370   bool ClassOrStructIsVirtual(const DWARFDIE &die);
371 
372   // Given a die_offset, figure out the symbol context representing that die.
373   bool ResolveFunction(const DIERef &die_ref, bool include_inlines,
374                        lldb_private::SymbolContextList &sc_list);
375 
376   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
377                        lldb_private::SymbolContextList &sc_list);
378 
379   void FindFunctions(const lldb_private::ConstString &name,
380                      const NameToDIE &name_to_die, bool include_inlines,
381                      lldb_private::SymbolContextList &sc_list);
382 
383   void FindFunctions(const lldb_private::RegularExpression &regex,
384                      const NameToDIE &name_to_die, bool include_inlines,
385                      lldb_private::SymbolContextList &sc_list);
386 
387   void FindFunctions(const lldb_private::RegularExpression &regex,
388                      const DWARFMappedHash::MemoryTable &memory_table,
389                      bool include_inlines,
390                      lldb_private::SymbolContextList &sc_list);
391 
392   virtual lldb::TypeSP
393   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
394 
395   lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
396       const DWARFDIE &die, const lldb_private::ConstString &type_name,
397       bool must_be_implementation);
398 
399   lldb::TypeSP
400   FindCompleteObjCDefinitionType(const lldb_private::ConstString &type_name,
401                                  bool header_definition_ok);
402 
403   lldb_private::Symbol *
404   GetObjCClassSymbol(const lldb_private::ConstString &objc_class_name);
405 
406   void ParseFunctions(const DIEArray &die_offsets, bool include_inlines,
407                       lldb_private::SymbolContextList &sc_list);
408 
409   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
410                              bool resolve_function_context = false);
411 
412   void Index();
413 
414   void DumpIndexes();
415 
416   void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
417     m_debug_map_module_wp = module_sp;
418   }
419 
420   SymbolFileDWARFDebugMap *GetDebugMapSymfile();
421 
422   DWARFDIE
423   FindBlockContainingSpecification(const DIERef &func_die_ref,
424                                    dw_offset_t spec_block_die_offset);
425 
426   DWARFDIE
427   FindBlockContainingSpecification(const DWARFDIE &die,
428                                    dw_offset_t spec_block_die_offset);
429 
430   virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
431 
432   bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2);
433 
434   bool ClassContainsSelector(const DWARFDIE &class_die,
435                              const lldb_private::ConstString &selector);
436 
437   bool FixupAddress(lldb_private::Address &addr);
438 
439   typedef std::set<lldb_private::Type *> TypeSet;
440 
441   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
442       ExternalTypeModuleMap;
443 
444   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
445                 dw_offset_t max_die_offset, uint32_t type_mask,
446                 TypeSet &type_set);
447 
448   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
449                                         lldb_private::Variable *>
450       GlobalVariableMap;
451 
452   GlobalVariableMap &GetGlobalAranges();
453 
454   void UpdateExternalModuleListIfNeeded();
455 
456   virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
457 
458   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
459 
460   virtual DIEToClangType &GetForwardDeclDieToClangType() {
461     return m_forward_decl_die_to_clang_type;
462   }
463 
464   virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
465     return m_forward_decl_clang_type_to_die;
466   }
467 
468   SymbolFileDWARFDwp *GetDwpSymbolFile();
469 
470   lldb::ModuleWP m_debug_map_module_wp;
471   SymbolFileDWARFDebugMap *m_debug_map_symfile;
472 
473   llvm::once_flag m_dwp_symfile_once_flag;
474   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
475 
476   lldb_private::DWARFDataExtractor m_dwarf_data;
477 
478   DWARFDataSegment m_data_debug_abbrev;
479   DWARFDataSegment m_data_debug_addr;
480   DWARFDataSegment m_data_debug_aranges;
481   DWARFDataSegment m_data_debug_frame;
482   DWARFDataSegment m_data_debug_info;
483   DWARFDataSegment m_data_debug_line;
484   DWARFDataSegment m_data_debug_macro;
485   DWARFDataSegment m_data_debug_loc;
486   DWARFDataSegment m_data_debug_ranges;
487   DWARFDataSegment m_data_debug_str;
488   DWARFDataSegment m_data_debug_str_offsets;
489   DWARFDataSegment m_data_apple_names;
490   DWARFDataSegment m_data_apple_types;
491   DWARFDataSegment m_data_apple_namespaces;
492   DWARFDataSegment m_data_apple_objc;
493 
494   // The unique pointer items below are generated on demand if and when someone
495   // accesses
496   // them through a non const version of this class.
497   std::unique_ptr<DWARFDebugAbbrev> m_abbr;
498   std::unique_ptr<DWARFDebugInfo> m_info;
499   std::unique_ptr<DWARFDebugLine> m_line;
500   std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_names_ap;
501   std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_types_ap;
502   std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_namespaces_ap;
503   std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_objc_ap;
504   std::unique_ptr<GlobalVariableMap> m_global_aranges_ap;
505 
506   typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
507       DebugMacrosMap;
508   DebugMacrosMap m_debug_macros_map;
509 
510   ExternalTypeModuleMap m_external_type_modules;
511   NameToDIE m_function_basename_index; // All concrete functions
512   NameToDIE m_function_fullname_index; // All concrete functions
513   NameToDIE m_function_method_index;   // All inlined functions
514   NameToDIE
515       m_function_selector_index; // All method names for functions of classes
516   NameToDIE m_objc_class_selectors_index; // Given a class name, find all
517                                           // selectors for the class
518   NameToDIE m_global_index;               // Global and static variables
519   NameToDIE m_type_index;                 // All type DIE offsets
520   NameToDIE m_namespace_index;            // All type DIE offsets
521   bool m_indexed : 1, m_using_apple_tables : 1, m_fetched_external_modules : 1;
522   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
523 
524   typedef std::shared_ptr<std::set<DIERef>> DIERefSetSP;
525   typedef std::unordered_map<std::string, DIERefSetSP> NameToOffsetMap;
526   NameToOffsetMap m_function_scope_qualified_name_map;
527   std::unique_ptr<DWARFDebugRanges> m_ranges;
528   UniqueDWARFASTTypeMap m_unique_ast_type_map;
529   DIEToTypePtr m_die_to_type;
530   DIEToVariableSP m_die_to_variable_sp;
531   DIEToClangType m_forward_decl_die_to_clang_type;
532   ClangTypeToDIE m_forward_decl_clang_type_to_die;
533 };
534 
535 #endif // SymbolFileDWARF_SymbolFileDWARF_h_
536