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