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