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