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