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