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