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