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