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