1 //===-- SymbolFileDWARFDebugMap.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_SymbolFileDWARFDebugMap_h_
11 #define SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
12 
13 
14 #include <vector>
15 #include <bitset>
16 
17 #include "clang/AST/CharUnits.h"
18 
19 #include "lldb/Core/RangeMap.h"
20 #include "lldb/Symbol/SymbolFile.h"
21 
22 #include "UniqueDWARFASTType.h"
23 
24 class SymbolFileDWARF;
25 class DWARFCompileUnit;
26 class DWARFDebugInfoEntry;
27 class DWARFDeclContext;
28 class DebugMapModule;
29 
30 class SymbolFileDWARFDebugMap : public lldb_private::SymbolFile
31 {
32 public:
33 
34     //------------------------------------------------------------------
35     // Static Functions
36     //------------------------------------------------------------------
37     static void
38     Initialize();
39 
40     static void
41     Terminate();
42 
43     static lldb_private::ConstString
44     GetPluginNameStatic();
45 
46     static const char *
47     GetPluginDescriptionStatic();
48 
49     static lldb_private::SymbolFile *
50     CreateInstance (lldb_private::ObjectFile* obj_file);
51 
52     //------------------------------------------------------------------
53     // Constructors and Destructors
54     //------------------------------------------------------------------
55                             SymbolFileDWARFDebugMap (lldb_private::ObjectFile* ofile);
56     virtual               ~ SymbolFileDWARFDebugMap ();
57 
58     virtual uint32_t        CalculateAbilities ();
59 
60     virtual void            InitializeObject();
61 
62     //------------------------------------------------------------------
63     // Compile Unit function calls
64     //------------------------------------------------------------------
65     virtual uint32_t        GetNumCompileUnits ();
66     virtual lldb::CompUnitSP ParseCompileUnitAtIndex (uint32_t index);
67 
68     virtual lldb::LanguageType ParseCompileUnitLanguage (const lldb_private::SymbolContext& sc);
69     virtual size_t          ParseCompileUnitFunctions (const lldb_private::SymbolContext& sc);
70     virtual bool            ParseCompileUnitLineTable (const lldb_private::SymbolContext& sc);
71     virtual bool            ParseCompileUnitSupportFiles (const lldb_private::SymbolContext& sc, lldb_private::FileSpecList &support_files);
72     virtual size_t          ParseFunctionBlocks (const lldb_private::SymbolContext& sc);
73     virtual size_t          ParseTypes (const lldb_private::SymbolContext& sc);
74     virtual size_t          ParseVariablesForContext (const lldb_private::SymbolContext& sc);
75 
76     virtual lldb_private::Type* ResolveTypeUID (lldb::user_id_t type_uid);
77     virtual clang::DeclContext* GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid);
78     virtual clang::DeclContext* GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid);
79     virtual lldb::clang_type_t  ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type);
80     virtual uint32_t        ResolveSymbolContext (const lldb_private::Address& so_addr, uint32_t resolve_scope, lldb_private::SymbolContext& sc);
81     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);
82     virtual uint32_t        FindGlobalVariables (const lldb_private::ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, lldb_private::VariableList& variables);
83     virtual uint32_t        FindGlobalVariables (const lldb_private::RegularExpression& regex, bool append, uint32_t max_matches, lldb_private::VariableList& variables);
84     virtual uint32_t        FindFunctions (const lldb_private::ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, lldb_private::SymbolContextList& sc_list);
85     virtual uint32_t        FindFunctions (const lldb_private::RegularExpression& regex, bool include_inlines, bool append, lldb_private::SymbolContextList& sc_list);
86     virtual uint32_t        FindTypes (const lldb_private::SymbolContext& sc, const lldb_private::ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, lldb_private::TypeList& types);
87     virtual lldb_private::ClangNamespaceDecl
88                             FindNamespace (const lldb_private::SymbolContext& sc,
89                                            const lldb_private::ConstString &name,
90                                            const lldb_private::ClangNamespaceDecl *parent_namespace_decl);
91     virtual size_t          GetTypes (lldb_private::SymbolContextScope *sc_scope,
92                                       uint32_t type_mask,
93                                       lldb_private::TypeList &type_list);
94 
95 
96     //------------------------------------------------------------------
97     // ClangASTContext callbacks for external source lookups.
98     //------------------------------------------------------------------
99     static void
100     CompleteTagDecl (void *baton, clang::TagDecl *);
101 
102     static void
103     CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *);
104 
105     static bool
106     LayoutRecordType (void *baton,
107                       const clang::RecordDecl *record_decl,
108                       uint64_t &size,
109                       uint64_t &alignment,
110                       llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
111                       llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
112                       llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets);
113 
114 
115     //------------------------------------------------------------------
116     // PluginInterface protocol
117     //------------------------------------------------------------------
118     virtual lldb_private::ConstString
119     GetPluginName();
120 
121     virtual uint32_t
122     GetPluginVersion();
123 
124 protected:
125     enum
126     {
127         kHaveInitializedOSOs = (1 << 0),
128         kNumFlags
129     };
130 
131     friend class SymbolFileDWARF;
132     friend class DebugMapModule;
133     struct OSOInfo
134     {
135         lldb::ModuleSP module_sp;
136 
137         OSOInfo() :
138             module_sp ()
139         {
140         }
141     };
142 
143     typedef std::shared_ptr<OSOInfo> OSOInfoSP;
144 
145     typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t> FileRangeMap;
146 
147     //------------------------------------------------------------------
148     // Class specific types
149     //------------------------------------------------------------------
150     struct CompileUnitInfo
151     {
152         lldb_private::FileSpec so_file;
153         lldb_private::ConstString oso_path;
154         lldb_private::TimeValue oso_mod_time;
155         OSOInfoSP oso_sp;
156         lldb::CompUnitSP compile_unit_sp;
157         uint32_t first_symbol_index;
158         uint32_t last_symbol_index;
159         uint32_t first_symbol_id;
160         uint32_t last_symbol_id;
161         FileRangeMap file_range_map;
162         bool file_range_map_valid;
163 
164 
165         CompileUnitInfo() :
166             so_file (),
167             oso_path (),
168             oso_mod_time (),
169             oso_sp (),
170             compile_unit_sp (),
171             first_symbol_index (UINT32_MAX),
172             last_symbol_index (UINT32_MAX),
173             first_symbol_id (UINT32_MAX),
174             last_symbol_id (UINT32_MAX),
175             file_range_map (),
176             file_range_map_valid (false)
177         {
178         }
179 
180         const FileRangeMap &
181         GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile);
182     };
183 
184     //------------------------------------------------------------------
185     // Protected Member Functions
186     //------------------------------------------------------------------
187     void
188     InitOSO ();
189 
190     static uint32_t
191     GetOSOIndexFromUserID (lldb::user_id_t uid)
192     {
193         return (uint32_t)((uid >> 32ull) - 1ull);
194     }
195 
196     static SymbolFileDWARF *
197     GetSymbolFileAsSymbolFileDWARF (SymbolFile *sym_file);
198 
199     bool
200     GetFileSpecForSO (uint32_t oso_idx, lldb_private::FileSpec &file_spec);
201 
202     CompileUnitInfo *
203     GetCompUnitInfo (const lldb_private::SymbolContext& sc);
204 
205     size_t
206     GetCompUnitInfosForModule (const lldb_private::Module *oso_module,
207                                std::vector<CompileUnitInfo *>& cu_infos);
208 
209     lldb_private::Module *
210     GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info);
211 
212     lldb_private::Module *
213     GetModuleByOSOIndex (uint32_t oso_idx);
214 
215     lldb_private::ObjectFile *
216     GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info);
217 
218     lldb_private::ObjectFile *
219     GetObjectFileByOSOIndex (uint32_t oso_idx);
220 
221     uint32_t
222     GetCompUnitInfoIndex (const CompileUnitInfo *comp_unit_info);
223 
224     SymbolFileDWARF *
225     GetSymbolFile (const lldb_private::SymbolContext& sc);
226 
227     SymbolFileDWARF *
228     GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info);
229 
230     SymbolFileDWARF *
231     GetSymbolFileByOSOIndex (uint32_t oso_idx);
232 
233     CompileUnitInfo *
234     GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr);
235 
236     CompileUnitInfo *
237     GetCompileUnitInfoForSymbolWithID (lldb::user_id_t symbol_id, uint32_t *oso_idx_ptr);
238 
239     static int
240     SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info);
241 
242     static int
243     SymbolContainsSymbolWithID (lldb::user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info);
244 
245     uint32_t
246     PrivateFindGlobalVariables (const lldb_private::ConstString &name,
247                                 const lldb_private::ClangNamespaceDecl *namespace_decl,
248                                 const std::vector<uint32_t> &name_symbol_indexes,
249                                 uint32_t max_matches,
250                                 lldb_private::VariableList& variables);
251 
252 
253     void
254     SetCompileUnit (SymbolFileDWARF *oso_dwarf, const lldb::CompUnitSP &cu_sp);
255 
256     lldb::CompUnitSP
257     GetCompileUnit (SymbolFileDWARF *oso_dwarf);
258 
259     CompileUnitInfo *
260     GetCompileUnitInfo (SymbolFileDWARF *oso_dwarf);
261 
262     lldb::TypeSP
263     FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &die_decl_ctx);
264 
265     bool
266     Supports_DW_AT_APPLE_objc_complete_type (SymbolFileDWARF *skip_dwarf_oso);
267 
268     lldb::TypeSP
269     FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
270                                           const lldb_private::ConstString &type_name,
271                                           bool must_be_implementation);
272 
273 
274     UniqueDWARFASTTypeMap &
275     GetUniqueDWARFASTTypeMap ()
276     {
277         return m_unique_ast_type_map;
278     }
279 
280 
281     //------------------------------------------------------------------
282     // OSOEntry
283     //------------------------------------------------------------------
284     class OSOEntry
285     {
286     public:
287 
288         OSOEntry () :
289         m_exe_sym_idx (UINT32_MAX),
290         m_oso_file_addr (LLDB_INVALID_ADDRESS)
291         {
292         }
293 
294         OSOEntry (uint32_t exe_sym_idx,
295                   lldb::addr_t oso_file_addr) :
296         m_exe_sym_idx (exe_sym_idx),
297         m_oso_file_addr (oso_file_addr)
298         {
299         }
300 
301         uint32_t
302         GetExeSymbolIndex () const
303         {
304             return m_exe_sym_idx;
305         }
306 
307         bool
308         operator < (const OSOEntry &rhs) const
309         {
310             return m_exe_sym_idx < rhs.m_exe_sym_idx;
311         }
312 
313         lldb::addr_t
314         GetOSOFileAddress () const
315         {
316             return m_oso_file_addr;
317         }
318 
319         void
320         SetOSOFileAddress (lldb::addr_t oso_file_addr)
321         {
322             m_oso_file_addr = oso_file_addr;
323         }
324     protected:
325         uint32_t m_exe_sym_idx;
326         lldb::addr_t m_oso_file_addr;
327     };
328 
329     typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry> DebugMap;
330 
331     //------------------------------------------------------------------
332     // Member Variables
333     //------------------------------------------------------------------
334     std::bitset<kNumFlags> m_flags;
335     std::vector<CompileUnitInfo> m_compile_unit_infos;
336     std::vector<uint32_t> m_func_indexes;   // Sorted by address
337     std::vector<uint32_t> m_glob_indexes;
338     std::map<lldb_private::ConstString, OSOInfoSP> m_oso_map;
339     UniqueDWARFASTTypeMap m_unique_ast_type_map;
340     lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
341     DebugMap m_debug_map;
342 
343     //------------------------------------------------------------------
344     // When an object file from the debug map gets parsed in
345     // SymbolFileDWARF, it needs to tell the debug map about the object
346     // files addresses by calling this function once for each N_FUN,
347     // N_GSYM and N_STSYM and after all entries in the debug map have
348     // been matched up, FinalizeOSOFileRanges() should be called.
349     //------------------------------------------------------------------
350     bool
351     AddOSOFileRange (CompileUnitInfo *cu_info,
352                      lldb::addr_t exe_file_addr,
353                      lldb::addr_t oso_file_addr,
354                      lldb::addr_t oso_byte_size);
355 
356     //------------------------------------------------------------------
357     // Called after calling AddOSOFileRange() for each object file debug
358     // map entry to finalize the info for the unlinked compile unit.
359     //------------------------------------------------------------------
360     void
361     FinalizeOSOFileRanges (CompileUnitInfo *cu_info);
362 
363     //------------------------------------------------------------------
364     /// Convert \a addr from a .o file address, to an executable address.
365     ///
366     /// @param[in] addr
367     ///     A section offset address from a .o file
368     ///
369     /// @return
370     ///     Returns true if \a addr was converted to be an executable
371     ///     section/offset address, false otherwise.
372     //------------------------------------------------------------------
373     bool
374     LinkOSOAddress (lldb_private::Address &addr);
375 
376     //------------------------------------------------------------------
377     /// Convert a .o file "file address" to an executable "file address".
378     ///
379     /// @param[in] oso_symfile
380     ///     The DWARF symbol file that contains \a oso_file_addr
381     ///
382     /// @param[in] oso_file_addr
383     ///     A .o file "file address" to convert.
384     ///
385     /// @return
386     ///     LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
387     ///     linked executable, otherwise a valid "file address" from the
388     ///     linked executable that contains the debug map.
389     //------------------------------------------------------------------
390     lldb::addr_t
391     LinkOSOFileAddress (SymbolFileDWARF *oso_symfile, lldb::addr_t oso_file_addr);
392 
393     //------------------------------------------------------------------
394     /// Given a line table full of lines with "file adresses" that are
395     /// for a .o file represented by \a oso_symfile, link a new line table
396     /// and return it.
397     ///
398     /// @param[in] oso_symfile
399     ///     The DWARF symbol file that produced the \a line_table
400     ///
401     /// @param[in] addr
402     ///     A section offset address from a .o file
403     ///
404     /// @return
405     ///     Returns a valid line table full of linked addresses, or NULL
406     ///     if none of the line table adresses exist in the main
407     ///     executable.
408     //------------------------------------------------------------------
409     lldb_private::LineTable *
410     LinkOSOLineTable (SymbolFileDWARF *oso_symfile,
411                       lldb_private::LineTable *line_table);
412 };
413 
414 #endif // #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
415