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