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