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   bool ForEachExternalModule(
57       lldb_private::CompileUnit &, llvm::DenseSet<lldb_private::SymbolFile *> &,
58       llvm::function_ref<bool(lldb_private::Module &)>) 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   void
118   FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> context,
119             lldb_private::LanguageSet languages,
120             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
121             lldb_private::TypeMap &types) override;
122   lldb_private::CompilerDeclContext FindNamespace(
123       lldb_private::ConstString name,
124       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
125   void GetTypes(lldb_private::SymbolContextScope *sc_scope,
126                 lldb::TypeClass type_mask,
127                 lldb_private::TypeList &type_list) override;
128   std::vector<lldb_private::CallEdge>
129   ParseCallEdgesInFunction(lldb_private::UserID func_id) override;
130 
131   void DumpClangAST(lldb_private::Stream &s) override;
132 
133   // PluginInterface protocol
134   lldb_private::ConstString GetPluginName() override;
135 
136   uint32_t GetPluginVersion() override;
137 
138 protected:
139   enum { kHaveInitializedOSOs = (1 << 0), kNumFlags };
140 
141   friend class DebugMapModule;
142   friend class DWARFASTParserClang;
143   friend class DWARFCompileUnit;
144   friend class SymbolFileDWARF;
145   struct OSOInfo {
146     lldb::ModuleSP module_sp;
147 
148     OSOInfo() : module_sp() {}
149   };
150 
151   typedef std::shared_ptr<OSOInfo> OSOInfoSP;
152 
153   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
154                                         lldb::addr_t>
155       FileRangeMap;
156 
157   // Class specific types
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   // Protected Member Functions
181   void InitOSO();
182 
183   uint32_t CalculateNumCompileUnits() override;
184   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
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   CompileUnitInfo *GetCompUnitInfo(const lldb_private::CompileUnit &comp_unit);
196 
197   size_t GetCompUnitInfosForModule(const lldb_private::Module *oso_module,
198                                    std::vector<CompileUnitInfo *> &cu_infos);
199 
200   lldb_private::Module *
201   GetModuleByCompUnitInfo(CompileUnitInfo *comp_unit_info);
202 
203   lldb_private::Module *GetModuleByOSOIndex(uint32_t oso_idx);
204 
205   lldb_private::ObjectFile *
206   GetObjectFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
207 
208   lldb_private::ObjectFile *GetObjectFileByOSOIndex(uint32_t oso_idx);
209 
210   uint32_t GetCompUnitInfoIndex(const CompileUnitInfo *comp_unit_info);
211 
212   SymbolFileDWARF *GetSymbolFile(const lldb_private::SymbolContext &sc);
213   SymbolFileDWARF *GetSymbolFile(const lldb_private::CompileUnit &comp_unit);
214 
215   SymbolFileDWARF *GetSymbolFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
216 
217   SymbolFileDWARF *GetSymbolFileByOSOIndex(uint32_t oso_idx);
218 
219   // If closure returns "false", iteration continues.  If it returns
220   // "true", iteration terminates.
221   void ForEachSymbolFile(std::function<bool(SymbolFileDWARF *)> closure) {
222     for (uint32_t oso_idx = 0, num_oso_idxs = m_compile_unit_infos.size();
223          oso_idx < num_oso_idxs; ++oso_idx) {
224       if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) {
225         if (closure(oso_dwarf))
226           return;
227       }
228     }
229   }
230 
231   CompileUnitInfo *GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,
232                                                         uint32_t *oso_idx_ptr);
233 
234   CompileUnitInfo *GetCompileUnitInfoForSymbolWithID(lldb::user_id_t symbol_id,
235                                                      uint32_t *oso_idx_ptr);
236 
237   static int
238   SymbolContainsSymbolWithIndex(uint32_t *symbol_idx_ptr,
239                                 const CompileUnitInfo *comp_unit_info);
240 
241   static int SymbolContainsSymbolWithID(lldb::user_id_t *symbol_idx_ptr,
242                                         const CompileUnitInfo *comp_unit_info);
243 
244   void PrivateFindGlobalVariables(
245       lldb_private::ConstString name,
246       const lldb_private::CompilerDeclContext *parent_decl_ctx,
247       const std::vector<uint32_t> &name_symbol_indexes, uint32_t max_matches,
248       lldb_private::VariableList &variables);
249 
250   void SetCompileUnit(SymbolFileDWARF *oso_dwarf,
251                       const lldb::CompUnitSP &cu_sp);
252 
253   lldb::CompUnitSP GetCompileUnit(SymbolFileDWARF *oso_dwarf);
254 
255   CompileUnitInfo *GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf);
256 
257   lldb::TypeSP
258   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
259 
260   bool Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF *skip_dwarf_oso);
261 
262   lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
263       const DWARFDIE &die, lldb_private::ConstString type_name,
264       bool must_be_implementation);
265 
266   UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap() {
267     return m_unique_ast_type_map;
268   }
269 
270   // OSOEntry
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   // Member Variables
300   std::bitset<kNumFlags> m_flags;
301   std::vector<CompileUnitInfo> m_compile_unit_infos;
302   std::vector<uint32_t> m_func_indexes; // Sorted by address
303   std::vector<uint32_t> m_glob_indexes;
304   std::map<std::pair<lldb_private::ConstString, llvm::sys::TimePoint<>>,
305            OSOInfoSP>
306       m_oso_map;
307   UniqueDWARFASTTypeMap m_unique_ast_type_map;
308   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
309   DebugMap m_debug_map;
310 
311   // When an object file from the debug map gets parsed in
312   // SymbolFileDWARF, it needs to tell the debug map about the object
313   // files addresses by calling this function once for each N_FUN,
314   // N_GSYM and N_STSYM and after all entries in the debug map have
315   // been matched up, FinalizeOSOFileRanges() should be called.
316   bool AddOSOFileRange(CompileUnitInfo *cu_info, lldb::addr_t exe_file_addr,
317                        lldb::addr_t exe_byte_size, lldb::addr_t oso_file_addr,
318                        lldb::addr_t oso_byte_size);
319 
320   // Called after calling AddOSOFileRange() for each object file debug
321   // map entry to finalize the info for the unlinked compile unit.
322   void FinalizeOSOFileRanges(CompileUnitInfo *cu_info);
323 
324   /// Convert \a addr from a .o file address, to an executable address.
325   ///
326   /// \param[in] addr
327   ///     A section offset address from a .o file
328   ///
329   /// \return
330   ///     Returns true if \a addr was converted to be an executable
331   ///     section/offset address, false otherwise.
332   bool LinkOSOAddress(lldb_private::Address &addr);
333 
334   /// Convert a .o file "file address" to an executable "file address".
335   ///
336   /// \param[in] oso_symfile
337   ///     The DWARF symbol file that contains \a oso_file_addr
338   ///
339   /// \param[in] oso_file_addr
340   ///     A .o file "file address" to convert.
341   ///
342   /// \return
343   ///     LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
344   ///     linked executable, otherwise a valid "file address" from the
345   ///     linked executable that contains the debug map.
346   lldb::addr_t LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
347                                   lldb::addr_t oso_file_addr);
348 
349   /// Given a line table full of lines with "file addresses" that are
350   /// for a .o file represented by \a oso_symfile, link a new line table
351   /// and return it.
352   ///
353   /// \param[in] oso_symfile
354   ///     The DWARF symbol file that produced the \a line_table
355   ///
356   /// \param[in] line_table
357   ///     A pointer to the line table.
358   ///
359   /// \return
360   ///     Returns a valid line table full of linked addresses, or NULL
361   ///     if none of the line table addresses exist in the main
362   ///     executable.
363   lldb_private::LineTable *
364   LinkOSOLineTable(SymbolFileDWARF *oso_symfile,
365                    lldb_private::LineTable *line_table);
366 
367   size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data,
368                        DWARFDebugAranges *debug_aranges);
369 };
370 
371 #endif // #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
372