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 "DWARFIndex.h"
42 #include "UniqueDWARFASTType.h"
43 
44 //----------------------------------------------------------------------
45 // Forward Declarations for this DWARF plugin
46 //----------------------------------------------------------------------
47 class DebugMapModule;
48 class DWARFAbbreviationDeclaration;
49 class DWARFAbbreviationDeclarationSet;
50 class DWARFileUnit;
51 class DWARFDebugAbbrev;
52 class DWARFDebugAranges;
53 class DWARFDebugInfo;
54 class DWARFDebugInfoEntry;
55 class DWARFDebugLine;
56 class DWARFDebugRangesBase;
57 class DWARFDeclContext;
58 class DWARFDIECollection;
59 class DWARFFormValue;
60 class SymbolFileDWARFDebugMap;
61 class SymbolFileDWARFDwo;
62 class SymbolFileDWARFDwp;
63 
64 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
65 
66 class SymbolFileDWARF : public lldb_private::SymbolFile,
67                         public lldb_private::UserID {
68 public:
69   friend class SymbolFileDWARFDebugMap;
70   friend class SymbolFileDWARFDwo;
71   friend class DebugMapModule;
72   friend struct DIERef;
73   friend class DWARFUnit;
74   friend class DWARFDIE;
75   friend class DWARFASTParserClang;
76   friend class DWARFASTParserGo;
77   friend class DWARFASTParserJava;
78   friend class DWARFASTParserOCaml;
79 
80   //------------------------------------------------------------------
81   // Static Functions
82   //------------------------------------------------------------------
83   static void Initialize();
84 
85   static void Terminate();
86 
87   static void DebuggerInitialize(lldb_private::Debugger &debugger);
88 
89   static lldb_private::ConstString GetPluginNameStatic();
90 
91   static const char *GetPluginDescriptionStatic();
92 
93   static lldb_private::SymbolFile *
94   CreateInstance(lldb_private::ObjectFile *obj_file);
95 
96   //------------------------------------------------------------------
97   // Constructors and Destructors
98   //------------------------------------------------------------------
99 
100   SymbolFileDWARF(lldb_private::ObjectFile *ofile);
101 
102   ~SymbolFileDWARF() override;
103 
104   uint32_t CalculateAbilities() override;
105 
106   void InitializeObject() override;
107 
108   //------------------------------------------------------------------
109   // Compile Unit function calls
110   //------------------------------------------------------------------
111 
112   uint32_t GetNumCompileUnits() override;
113 
114   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
115 
116   lldb::LanguageType
117   ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) override;
118 
119   size_t
120   ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc) override;
121 
122   bool
123   ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc) override;
124 
125   bool
126   ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc) override;
127 
128   bool ParseCompileUnitSupportFiles(
129       const lldb_private::SymbolContext &sc,
130       lldb_private::FileSpecList &support_files) override;
131 
132   bool
133   ParseCompileUnitIsOptimized(const lldb_private::SymbolContext &sc) override;
134 
135   bool ParseImportedModules(
136       const lldb_private::SymbolContext &sc,
137       std::vector<lldb_private::ConstString> &imported_modules) override;
138 
139   size_t ParseFunctionBlocks(const lldb_private::SymbolContext &sc) override;
140 
141   size_t ParseTypes(const lldb_private::SymbolContext &sc) override;
142 
143   size_t
144   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
145 
146   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
147 
148   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
149 
150   lldb_private::Type *ResolveType(const DWARFDIE &die,
151                                   bool assert_not_being_parsed = true,
152                                   bool resolve_function_context = false);
153 
154   SymbolFileDWARF *GetDWARFForUID(lldb::user_id_t uid);
155 
156   DWARFDIE
157   GetDIEFromUID(lldb::user_id_t uid);
158 
159   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
160 
161   lldb_private::CompilerDeclContext
162   GetDeclContextForUID(lldb::user_id_t uid) override;
163 
164   lldb_private::CompilerDeclContext
165   GetDeclContextContainingUID(lldb::user_id_t uid) override;
166 
167   void
168   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
169 
170   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
171                                 lldb::SymbolContextItem resolve_scope,
172                                 lldb_private::SymbolContext &sc) override;
173 
174   uint32_t
175   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
176                        bool check_inlines,
177                        lldb::SymbolContextItem 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                       uint32_t max_matches,
184                       lldb_private::VariableList &variables) override;
185 
186   uint32_t FindGlobalVariables(const lldb_private::RegularExpression &regex,
187                                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                 lldb::FunctionNameType name_type_mask, bool include_inlines,
194                 bool append, 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                   lldb::TypeClass 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   std::recursive_mutex &GetModuleMutex() const override;
232 
233   //------------------------------------------------------------------
234   // PluginInterface protocol
235   //------------------------------------------------------------------
236   lldb_private::ConstString GetPluginName() override;
237 
238   uint32_t GetPluginVersion() override;
239 
240   const lldb_private::DWARFDataExtractor &get_debug_abbrev_data();
241   const lldb_private::DWARFDataExtractor &get_debug_addr_data();
242   const lldb_private::DWARFDataExtractor &get_debug_aranges_data();
243   const lldb_private::DWARFDataExtractor &get_debug_frame_data();
244   const lldb_private::DWARFDataExtractor &get_debug_info_data();
245   const lldb_private::DWARFDataExtractor &get_debug_line_data();
246   const lldb_private::DWARFDataExtractor &get_debug_line_str_data();
247   const lldb_private::DWARFDataExtractor &get_debug_macro_data();
248   const lldb_private::DWARFDataExtractor &get_debug_loc_data();
249   const lldb_private::DWARFDataExtractor &get_debug_loclists_data();
250   const lldb_private::DWARFDataExtractor &get_debug_ranges_data();
251   const lldb_private::DWARFDataExtractor &get_debug_rnglists_data();
252   const lldb_private::DWARFDataExtractor &get_debug_str_data();
253   const lldb_private::DWARFDataExtractor &get_debug_str_offsets_data();
254   const lldb_private::DWARFDataExtractor &get_debug_types_data();
255   const lldb_private::DWARFDataExtractor &get_apple_names_data();
256   const lldb_private::DWARFDataExtractor &get_apple_types_data();
257   const lldb_private::DWARFDataExtractor &get_apple_namespaces_data();
258   const lldb_private::DWARFDataExtractor &get_apple_objc_data();
259   const lldb_private::DWARFDataExtractor &get_gnu_debugaltlink();
260 
261   DWARFDebugAbbrev *DebugAbbrev();
262 
263   const DWARFDebugAbbrev *DebugAbbrev() const;
264 
265   DWARFDebugInfo *DebugInfo();
266 
267   const DWARFDebugInfo *DebugInfo() const;
268 
269   DWARFDebugRangesBase *DebugRanges();
270 
271   const DWARFDebugRangesBase *DebugRanges() const;
272 
273   const lldb_private::DWARFDataExtractor &DebugLocData();
274 
275   static bool SupportedVersion(uint16_t version);
276 
277   DWARFDIE
278   GetDeclContextDIEContainingDIE(const DWARFDIE &die);
279 
280   bool
281   HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type);
282 
283   lldb_private::CompileUnit *
284   GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
285                               uint32_t cu_idx = UINT32_MAX);
286 
287   virtual size_t GetObjCMethodDIEOffsets(lldb_private::ConstString class_name,
288                                          DIEArray &method_die_offsets);
289 
290   bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu);
291 
292   lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
293 
294   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
295 
296   virtual lldb::CompUnitSP ParseCompileUnit(DWARFUnit *dwarf_cu,
297                                             uint32_t cu_idx);
298 
299   virtual lldb_private::DWARFExpression::LocationListFormat
300   GetLocationListFormat() const;
301 
302   lldb::ModuleSP GetDWOModule(lldb_private::ConstString name);
303 
304   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
305       ExternalTypeModuleMap;
306 
307   /// Return the list of Clang modules imported by this SymbolFile.
308   const ExternalTypeModuleMap& getExternalTypeModules() const {
309       return m_external_type_modules;
310   }
311 
312   virtual DWARFDIE GetDIE(const DIERef &die_ref);
313 
314   virtual std::unique_ptr<SymbolFileDWARFDwo>
315   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
316                                  const DWARFDebugInfoEntry &cu_die);
317 
318   // For regular SymbolFileDWARF instances the method returns nullptr,
319   // for the instances of the subclass SymbolFileDWARFDwo
320   // the method returns a pointer to the base compile unit.
321   virtual DWARFUnit *GetBaseCompileUnit();
322 
323   static bool
324   DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
325                    const DWARFDIE &die);
326 
327   std::vector<lldb_private::CallEdge>
328   ParseCallEdgesInFunction(UserID func_id) override;
329 
330   void Dump(lldb_private::Stream &s) override;
331 
332 protected:
333   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
334       DIEToTypePtr;
335   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
336       DIEToVariableSP;
337   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
338                          lldb::opaque_compiler_type_t>
339       DIEToClangType;
340   typedef llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> ClangTypeToDIE;
341 
342   struct DWARFDataSegment {
343     llvm::once_flag m_flag;
344     lldb_private::DWARFDataExtractor m_data;
345   };
346 
347   DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
348 
349   const lldb_private::DWARFDataExtractor &
350   GetCachedSectionData(lldb::SectionType sect_type,
351                        DWARFDataSegment &data_segment);
352 
353   virtual void LoadSectionData(lldb::SectionType sect_type,
354                                lldb_private::DWARFDataExtractor &data);
355 
356   bool DeclContextMatchesThisSymbolFile(
357       const lldb_private::CompilerDeclContext *decl_ctx);
358 
359   virtual DWARFUnit *
360   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
361 
362   DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
363 
364   bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
365 
366   lldb_private::Function *
367   ParseCompileUnitFunction(const lldb_private::SymbolContext &sc,
368                            const DWARFDIE &die);
369 
370   size_t ParseFunctionBlocks(const lldb_private::SymbolContext &sc,
371                              lldb_private::Block *parent_block,
372                              const DWARFDIE &die,
373                              lldb::addr_t subprogram_low_pc, uint32_t depth);
374 
375   size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
376                     bool parse_siblings, bool parse_children);
377 
378   lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
379                          const DWARFDIE &die, bool *type_is_new);
380 
381   lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
382                                      bool assert_not_being_parsed);
383 
384   lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
385 
386   lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
387                                     const DWARFDIE &die,
388                                     const lldb::addr_t func_low_pc);
389 
390   size_t ParseVariables(const lldb_private::SymbolContext &sc,
391                         const DWARFDIE &orig_die,
392                         const lldb::addr_t func_low_pc, bool parse_siblings,
393                         bool parse_children,
394                         lldb_private::VariableList *cc_variable_list = NULL);
395 
396   bool ClassOrStructIsVirtual(const DWARFDIE &die);
397 
398   // Given a die_offset, figure out the symbol context representing that die.
399   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
400                        lldb_private::SymbolContextList &sc_list);
401 
402   virtual lldb::TypeSP
403   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
404 
405   virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
406       const DWARFDIE &die, const lldb_private::ConstString &type_name,
407       bool must_be_implementation);
408 
409   lldb_private::Symbol *
410   GetObjCClassSymbol(const lldb_private::ConstString &objc_class_name);
411 
412   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
413                              bool resolve_function_context = false);
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   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
441                 dw_offset_t max_die_offset, uint32_t type_mask,
442                 TypeSet &type_set);
443 
444   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
445                                         lldb_private::Variable *>
446       GlobalVariableMap;
447 
448   GlobalVariableMap &GetGlobalAranges();
449 
450   void UpdateExternalModuleListIfNeeded();
451 
452   virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
453 
454   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
455 
456   virtual DIEToClangType &GetForwardDeclDieToClangType() {
457     return m_forward_decl_die_to_clang_type;
458   }
459 
460   virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
461     return m_forward_decl_clang_type_to_die;
462   }
463 
464   SymbolFileDWARFDwp *GetDwpSymbolFile();
465 
466   lldb::ModuleWP m_debug_map_module_wp;
467   SymbolFileDWARFDebugMap *m_debug_map_symfile;
468 
469   llvm::once_flag m_dwp_symfile_once_flag;
470   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
471 
472   lldb_private::DWARFDataExtractor m_dwarf_data;
473 
474   DWARFDataSegment m_data_debug_abbrev;
475   DWARFDataSegment m_data_debug_addr;
476   DWARFDataSegment m_data_debug_aranges;
477   DWARFDataSegment m_data_debug_frame;
478   DWARFDataSegment m_data_debug_info;
479   DWARFDataSegment m_data_debug_line;
480   DWARFDataSegment m_data_debug_line_str;
481   DWARFDataSegment m_data_debug_macro;
482   DWARFDataSegment m_data_debug_loc;
483   DWARFDataSegment m_data_debug_loclists;
484   DWARFDataSegment m_data_debug_ranges;
485   DWARFDataSegment m_data_debug_rnglists;
486   DWARFDataSegment m_data_debug_str;
487   DWARFDataSegment m_data_debug_str_offsets;
488   DWARFDataSegment m_data_debug_types;
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   DWARFDataSegment m_data_gnu_debugaltlink;
494 
495   // The unique pointer items below are generated on demand if and when someone
496   // accesses
497   // them through a non const version of this class.
498   std::unique_ptr<DWARFDebugAbbrev> m_abbr;
499   std::unique_ptr<DWARFDebugInfo> m_info;
500   std::unique_ptr<DWARFDebugLine> m_line;
501   std::unique_ptr<GlobalVariableMap> m_global_aranges_ap;
502 
503   typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
504       DebugMacrosMap;
505   DebugMacrosMap m_debug_macros_map;
506 
507   ExternalTypeModuleMap m_external_type_modules;
508   std::unique_ptr<lldb_private::DWARFIndex> m_index;
509   bool m_fetched_external_modules : 1;
510   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
511 
512   typedef std::shared_ptr<std::set<DIERef>> DIERefSetSP;
513   typedef std::unordered_map<std::string, DIERefSetSP> NameToOffsetMap;
514   NameToOffsetMap m_function_scope_qualified_name_map;
515   std::unique_ptr<DWARFDebugRangesBase> m_ranges;
516   UniqueDWARFASTTypeMap m_unique_ast_type_map;
517   DIEToTypePtr m_die_to_type;
518   DIEToVariableSP m_die_to_variable_sp;
519   DIEToClangType m_forward_decl_die_to_clang_type;
520   ClangTypeToDIE m_forward_decl_clang_type_to_die;
521 };
522 
523 #endif // SymbolFileDWARF_SymbolFileDWARF_h_
524