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 <memory>
17 #include <map>
18 #include <vector>
19 
20 // Other libraries and framework includes
21 #include "clang/AST/ExternalASTSource.h"
22 #include "llvm/ADT/DenseMap.h"
23 
24 #include "lldb/Core/ClangForward.h"
25 #include "lldb/Core/ConstString.h"
26 #include "lldb/Core/dwarf.h"
27 #include "lldb/Core/DataExtractor.h"
28 #include "lldb/Core/Flags.h"
29 #include "lldb/Core/UniqueCStringMap.h"
30 #include "lldb/Symbol/SymbolFile.h"
31 #include "lldb/Symbol/SymbolContext.h"
32 
33 // Project includes
34 #include "DWARFDefines.h"
35 #include "HashedNameToDIE.h"
36 #include "NameToDIE.h"
37 #include "UniqueDWARFASTType.h"
38 
39 
40 //----------------------------------------------------------------------
41 // Forward Declarations for this DWARF plugin
42 //----------------------------------------------------------------------
43 class DWARFAbbreviationDeclaration;
44 class DWARFAbbreviationDeclarationSet;
45 class DWARFCompileUnit;
46 class DWARFDebugAbbrev;
47 class DWARFDebugAranges;
48 class DWARFDebugInfo;
49 class DWARFDebugInfoEntry;
50 class DWARFDebugLine;
51 class DWARFDebugPubnames;
52 class DWARFDebugRanges;
53 class DWARFDIECollection;
54 class DWARFFormValue;
55 class SymbolFileDWARFDebugMap;
56 
57 class SymbolFileDWARF : public lldb_private::SymbolFile
58 {
59 public:
60     friend class SymbolFileDWARFDebugMap;
61 
62     //------------------------------------------------------------------
63     // Static Functions
64     //------------------------------------------------------------------
65     static void
66     Initialize();
67 
68     static void
69     Terminate();
70 
71     static const char *
72     GetPluginNameStatic();
73 
74     static const char *
75     GetPluginDescriptionStatic();
76 
77     static lldb_private::SymbolFile*
78     CreateInstance (lldb_private::ObjectFile* obj_file);
79     //------------------------------------------------------------------
80     // Constructors and Destructors
81     //------------------------------------------------------------------
82                             SymbolFileDWARF(lldb_private::ObjectFile* ofile);
83     virtual                 ~SymbolFileDWARF();
84 
85     virtual uint32_t        GetAbilities ();
86     virtual void            InitializeObject();
87 
88     //------------------------------------------------------------------
89     // Compile Unit function calls
90     //------------------------------------------------------------------
91     virtual uint32_t        GetNumCompileUnits();
92     virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index);
93 
94     virtual size_t          ParseCompileUnitFunctions (const lldb_private::SymbolContext& sc);
95     virtual bool            ParseCompileUnitLineTable (const lldb_private::SymbolContext& sc);
96     virtual bool            ParseCompileUnitSupportFiles (const lldb_private::SymbolContext& sc, lldb_private::FileSpecList& support_files);
97     virtual size_t          ParseFunctionBlocks (const lldb_private::SymbolContext& sc);
98     virtual size_t          ParseTypes (const lldb_private::SymbolContext& sc);
99     virtual size_t          ParseVariablesForContext (const lldb_private::SymbolContext& sc);
100 
101     virtual lldb_private::Type* ResolveTypeUID(lldb::user_id_t type_uid);
102     virtual lldb::clang_type_t ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_opaque_type);
103 
104     virtual lldb_private::Type* ResolveType (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed = true);
105     virtual clang::DeclContext* GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid);
106     virtual clang::DeclContext* GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid);
107 
108     virtual uint32_t        ResolveSymbolContext (const lldb_private::Address& so_addr, uint32_t resolve_scope, lldb_private::SymbolContext& sc);
109     virtual uint32_t        ResolveSymbolContext (const lldb_private::FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, lldb_private::SymbolContextList& sc_list);
110     virtual uint32_t        FindGlobalVariables(const lldb_private::ConstString &name, bool append, uint32_t max_matches, lldb_private::VariableList& variables);
111     virtual uint32_t        FindGlobalVariables(const lldb_private::RegularExpression& regex, bool append, uint32_t max_matches, lldb_private::VariableList& variables);
112     virtual uint32_t        FindFunctions(const lldb_private::ConstString &name, uint32_t name_type_mask, bool append, lldb_private::SymbolContextList& sc_list);
113     virtual uint32_t        FindFunctions(const lldb_private::RegularExpression& regex, bool append, lldb_private::SymbolContextList& sc_list);
114     virtual uint32_t        FindTypes (const lldb_private::SymbolContext& sc, const lldb_private::ConstString &name, bool append, uint32_t max_matches, lldb_private::TypeList& types);
115     virtual lldb_private::TypeList *
116                             GetTypeList ();
117     virtual lldb_private::ClangASTContext &
118                             GetClangASTContext ();
119 
120     virtual lldb_private::ClangNamespaceDecl
121             FindNamespace (const lldb_private::SymbolContext& sc,
122                            const lldb_private::ConstString &name);
123 
124 
125     //------------------------------------------------------------------
126     // ClangASTContext callbacks for external source lookups.
127     //------------------------------------------------------------------
128     static void
129     CompleteTagDecl (void *baton, clang::TagDecl *);
130 
131     static void
132     CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *);
133 
134     static void
135     FindExternalVisibleDeclsByName (void *baton,
136                                     const clang::DeclContext *DC,
137                                     clang::DeclarationName Name,
138                                     llvm::SmallVectorImpl <clang::NamedDecl *> *results);
139 
140     //------------------------------------------------------------------
141     // PluginInterface protocol
142     //------------------------------------------------------------------
143     virtual const char *
144     GetPluginName();
145 
146     virtual const char *
147     GetShortPluginName();
148 
149     virtual uint32_t
150     GetPluginVersion();
151 
152     // Approach 2 - count + accessor
153     // Index compile units would scan the initial compile units and register
154     // them with the module. This would only be done on demand if and only if
155     // the compile units were needed.
156     //virtual size_t        GetCompUnitCount() = 0;
157     //virtual CompUnitSP    GetCompUnitAtIndex(size_t cu_idx) = 0;
158 
159     const lldb_private::DataExtractor&      get_debug_abbrev_data ();
160     const lldb_private::DataExtractor&      get_debug_aranges_data ();
161     const lldb_private::DataExtractor&      get_debug_frame_data ();
162     const lldb_private::DataExtractor&      get_debug_info_data ();
163     const lldb_private::DataExtractor&      get_debug_line_data ();
164     const lldb_private::DataExtractor&      get_debug_loc_data ();
165     const lldb_private::DataExtractor&      get_debug_ranges_data ();
166     const lldb_private::DataExtractor&      get_debug_str_data ();
167     const lldb_private::DataExtractor&      get_apple_names_data ();
168     const lldb_private::DataExtractor&      get_apple_types_data ();
169 
170     DWARFDebugAbbrev*       DebugAbbrev();
171     const DWARFDebugAbbrev* DebugAbbrev() const;
172 
173     DWARFDebugInfo*         DebugInfo();
174     const DWARFDebugInfo*   DebugInfo() const;
175 
176     DWARFDebugRanges*       DebugRanges();
177     const DWARFDebugRanges* DebugRanges() const;
178 
179     const lldb_private::DataExtractor&
180     GetCachedSectionData (uint32_t got_flag,
181                           lldb::SectionType sect_type,
182                           lldb_private::DataExtractor &data);
183 
184     static bool
185     SupportedVersion(uint16_t version);
186 
187     clang::DeclContext *
188     GetClangDeclContextForDIE (const lldb_private::SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die);
189 
190     clang::DeclContext *
191     GetClangDeclContextForDIEOffset (const lldb_private::SymbolContext &sc, dw_offset_t die_offset);
192 
193     clang::DeclContext *
194     GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die);
195 
196     clang::DeclContext *
197     GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset);
198 
199     const DWARFDebugInfoEntry *
200     GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die);
201 
202     void
203     SearchDeclContext (const clang::DeclContext *decl_context,
204                        const char *name,
205                        llvm::SmallVectorImpl <clang::NamedDecl *> *results);
206 
207     lldb_private::Flags&
208     GetFlags ()
209     {
210         return m_flags;
211     }
212 
213     const lldb_private::Flags&
214     GetFlags () const
215     {
216         return m_flags;
217     }
218 
219     bool
220     HasForwardDeclForClangType (lldb::clang_type_t clang_type);
221 
222 protected:
223 
224     enum
225     {
226         flagsGotDebugAbbrevData     = (1 << 0),
227         flagsGotDebugArangesData    = (1 << 1),
228         flagsGotDebugFrameData      = (1 << 2),
229         flagsGotDebugInfoData       = (1 << 3),
230         flagsGotDebugLineData       = (1 << 4),
231         flagsGotDebugLocData        = (1 << 5),
232         flagsGotDebugMacInfoData    = (1 << 6),
233         flagsGotDebugPubNamesData   = (1 << 7),
234         flagsGotDebugPubTypesData   = (1 << 8),
235         flagsGotDebugRangesData     = (1 << 9),
236         flagsGotDebugStrData        = (1 << 10),
237         flagsGotDebugNamesData      = (1 << 11),
238         flagsGotDebugTypesData      = (1 << 12)
239     };
240 
241     DISALLOW_COPY_AND_ASSIGN (SymbolFileDWARF);
242     bool                    ParseCompileUnit (DWARFCompileUnit* cu, lldb::CompUnitSP& compile_unit_sp);
243     DWARFCompileUnit*       GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid);
244     DWARFCompileUnit*       GetNextUnparsedDWARFCompileUnit(DWARFCompileUnit* prev_cu);
245     lldb_private::CompileUnit*      GetCompUnitForDWARFCompUnit(DWARFCompileUnit* cu, uint32_t cu_idx = UINT32_MAX);
246     bool                    GetFunction (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* func_die, lldb_private::SymbolContext& sc);
247     lldb_private::Function *        ParseCompileUnitFunction (const lldb_private::SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die);
248     size_t                  ParseFunctionBlocks (const lldb_private::SymbolContext& sc,
249                                                  lldb_private::Block *parent_block,
250                                                  DWARFCompileUnit* dwarf_cu,
251                                                  const DWARFDebugInfoEntry *die,
252                                                  lldb::addr_t subprogram_low_pc,
253                                                  uint32_t depth);
254     size_t                  ParseTypes (const lldb_private::SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool parse_siblings, bool parse_children);
255     lldb::TypeSP            ParseType (const lldb_private::SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new);
256 
257     lldb::VariableSP        ParseVariableDIE(
258                                 const lldb_private::SymbolContext& sc,
259                                 DWARFCompileUnit* dwarf_cu,
260                                 const DWARFDebugInfoEntry *die,
261                                 const lldb::addr_t func_low_pc);
262 
263     size_t                  ParseVariables(
264                                 const lldb_private::SymbolContext& sc,
265                                 DWARFCompileUnit* dwarf_cu,
266                                 const lldb::addr_t func_low_pc,
267                                 const DWARFDebugInfoEntry *die,
268                                 bool parse_siblings,
269                                 bool parse_children,
270                                 lldb_private::VariableList* cc_variable_list = NULL);
271 
272     size_t                  ParseChildMembers(
273                                 const lldb_private::SymbolContext& sc,
274                                 DWARFCompileUnit* dwarf_cu,
275                                 const DWARFDebugInfoEntry *die,
276                                 lldb::clang_type_t class_clang_type,
277                                 const lldb::LanguageType class_language,
278                                 std::vector<clang::CXXBaseSpecifier *>& base_classes,
279                                 std::vector<int>& member_accessibilities,
280                                 DWARFDIECollection& member_function_dies,
281                                 lldb::AccessType &default_accessibility,
282                                 bool &is_a_class);
283 
284     size_t                  ParseChildParameters(
285                                 const lldb_private::SymbolContext& sc,
286                                 clang::DeclContext *containing_decl_ctx,
287                                 lldb::TypeSP& type_sp,
288                                 DWARFCompileUnit* dwarf_cu,
289                                 const DWARFDebugInfoEntry *parent_die,
290                                 bool skip_artificial,
291                                 bool &is_static,
292                                 lldb_private::TypeList* type_list,
293                                 std::vector<lldb::clang_type_t>& function_args,
294                                 std::vector<clang::ParmVarDecl*>& function_param_decls,
295                                 unsigned &type_quals);
296 
297     size_t                  ParseChildEnumerators(
298                                 const lldb_private::SymbolContext& sc,
299                                 lldb::clang_type_t enumerator_qual_type,
300                                 uint32_t enumerator_byte_size,
301                                 DWARFCompileUnit* dwarf_cu,
302                                 const DWARFDebugInfoEntry *enum_die);
303 
304     void                    ParseChildArrayInfo(
305                                 const lldb_private::SymbolContext& sc,
306                                 DWARFCompileUnit* dwarf_cu,
307                                 const DWARFDebugInfoEntry *parent_die,
308                                 int64_t& first_index,
309                                 std::vector<uint64_t>& element_orders,
310                                 uint32_t& byte_stride,
311                                 uint32_t& bit_stride);
312 
313     uint32_t                ResolveFunctions (
314                                 const DIEArray &die_offsets,
315                                 lldb_private::SymbolContextList& sc_list,
316                                 const lldb_private::ConstString &name,
317                                 uint32_t name_type_mask);
318 
319     void                    FindFunctions(
320                                 const lldb_private::ConstString &name,
321                                 const NameToDIE &name_to_die,
322                                 lldb_private::SymbolContextList& sc_list);
323 
324     void                    FindFunctions (
325                                 const lldb_private::RegularExpression &regex,
326                                 const NameToDIE &name_to_die,
327                                 lldb_private::SymbolContextList& sc_list);
328 
329     lldb::TypeSP            FindDefinitionTypeForDIE (
330                                 DWARFCompileUnit* cu,
331                                 const DWARFDebugInfoEntry *die,
332                                 const lldb_private::ConstString &type_name);
333 
334     lldb::TypeSP            GetTypeForDIE (DWARFCompileUnit *cu,
335                                            const DWARFDebugInfoEntry* die);
336 
337     uint32_t                FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, lldb_private::TypeList& types);
338 
339     void                    Index();
340 
341     void                    DumpIndexes();
342 
343     void                    SetDebugMapSymfile (SymbolFileDWARFDebugMap *debug_map_symfile)
344                             {
345                                 m_debug_map_symfile = debug_map_symfile;
346                             }
347 
348     const DWARFDebugInfoEntry *
349                             FindBlockContainingSpecification (dw_offset_t func_die_offset,
350                                                               dw_offset_t spec_block_die_offset,
351                                                               DWARFCompileUnit **dwarf_cu_handle);
352 
353     const DWARFDebugInfoEntry *
354                             FindBlockContainingSpecification (DWARFCompileUnit* dwarf_cu,
355                                                               const DWARFDebugInfoEntry *die,
356                                                               dw_offset_t spec_block_die_offset,
357                                                               DWARFCompileUnit **dwarf_cu_handle);
358 
359     clang::NamespaceDecl *
360     ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die);
361 
362     UniqueDWARFASTTypeMap &
363     GetUniqueDWARFASTTypeMap ();
364 
365     void                    LinkDeclContextToDIE (clang::DeclContext *decl_ctx,
366                                                   const DWARFDebugInfoEntry *die)
367                             {
368                                 m_die_to_decl_ctx[die] = decl_ctx;
369                                 m_decl_ctx_to_die[decl_ctx] = die;
370                             }
371 
372     void
373     ReportError (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
374     void
375     ReportWarning (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
376 
377     SymbolFileDWARFDebugMap *       m_debug_map_symfile;
378     clang::TranslationUnitDecl *    m_clang_tu_decl;
379     lldb_private::Flags             m_flags;
380     lldb_private::DataExtractor     m_dwarf_data;
381     lldb_private::DataExtractor     m_data_debug_abbrev;
382     lldb_private::DataExtractor     m_data_debug_aranges;
383     lldb_private::DataExtractor     m_data_debug_frame;
384     lldb_private::DataExtractor     m_data_debug_info;
385     lldb_private::DataExtractor     m_data_debug_line;
386     lldb_private::DataExtractor     m_data_debug_loc;
387     lldb_private::DataExtractor     m_data_debug_ranges;
388     lldb_private::DataExtractor     m_data_debug_str;
389     lldb_private::DataExtractor     m_data_apple_names;
390     lldb_private::DataExtractor     m_data_apple_types;
391 
392     // The auto_ptr items below are generated on demand if and when someone accesses
393     // them through a non const version of this class.
394     std::auto_ptr<DWARFDebugAbbrev>     m_abbr;
395     std::auto_ptr<DWARFDebugInfo>       m_info;
396     std::auto_ptr<DWARFDebugLine>       m_line;
397     HashedNameToDIE::MemoryTable        m_apple_names;
398     HashedNameToDIE::MemoryTable        m_apple_types;
399     NameToDIE                           m_function_basename_index;  // All concrete functions
400     NameToDIE                           m_function_fullname_index;  // All concrete functions
401     NameToDIE                           m_function_method_index;    // All inlined functions
402     NameToDIE                           m_function_selector_index;  // All method names for functions of classes
403     NameToDIE                           m_objc_class_selectors_index; // Given a class name, find all selectors for the class
404     NameToDIE                           m_global_index;                 // Global and static variables
405     NameToDIE                           m_type_index;                  // All type DIE offsets
406     NameToDIE                           m_namespace_index;              // All type DIE offsets
407     bool m_indexed:1,
408          m_is_external_ast_source:1;
409 
410     std::auto_ptr<DWARFDebugRanges>     m_ranges;
411     UniqueDWARFASTTypeMap m_unique_ast_type_map;
412     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, clang::DeclContext *> DIEToDeclContextMap;
413     typedef llvm::DenseMap<const clang::DeclContext *, const DWARFDebugInfoEntry *> DeclContextToDIEMap;
414     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *> DIEToTypePtr;
415     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP> DIEToVariableSP;
416     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::clang_type_t> DIEToClangType;
417     typedef llvm::DenseMap<lldb::clang_type_t, const DWARFDebugInfoEntry *> ClangTypeToDIE;
418     DIEToDeclContextMap m_die_to_decl_ctx;
419     DeclContextToDIEMap m_decl_ctx_to_die;
420     DIEToTypePtr m_die_to_type;
421     DIEToVariableSP m_die_to_variable_sp;
422     DIEToClangType m_forward_decl_die_to_clang_type;
423     ClangTypeToDIE m_forward_decl_clang_type_to_die;
424 };
425 
426 #endif  // SymbolFileDWARF_SymbolFileDWARF_h_
427