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