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