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 LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_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   /// LLVM RTTI support.
27   static char ID;
28 
29 public:
30   /// LLVM RTTI support.
31   /// \{
isA(const void * ClassID)32   bool isA(const void *ClassID) const override {
33     return ClassID == &ID || SymbolFile::isA(ClassID);
34   }
classof(const SymbolFile * obj)35   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
36   /// \}
37 
38   // Static Functions
39   static void Initialize();
40 
41   static void Terminate();
42 
43   static lldb_private::ConstString GetPluginNameStatic();
44 
45   static const char *GetPluginDescriptionStatic();
46 
47   static lldb_private::SymbolFile *
48   CreateInstance(lldb::ObjectFileSP objfile_sp);
49 
50   // Constructors and Destructors
51   SymbolFileDWARFDebugMap(lldb::ObjectFileSP objfile_sp);
52   ~SymbolFileDWARFDebugMap() override;
53 
54   uint32_t CalculateAbilities() override;
55   void InitializeObject() override;
56 
57   // Compile Unit function calls
58   lldb::LanguageType
59   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
60   lldb_private::XcodeSDK
61   ParseXcodeSDK(lldb_private::CompileUnit &comp_unit) override;
62   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
63   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
64   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
65 
66   bool ForEachExternalModule(
67       lldb_private::CompileUnit &, llvm::DenseSet<lldb_private::SymbolFile *> &,
68       llvm::function_ref<bool(lldb_private::Module &)>) override;
69 
70   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
71                          lldb_private::FileSpecList &support_files) override;
72 
73   bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override;
74 
75   size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
76 
77   bool ParseImportedModules(
78       const lldb_private::SymbolContext &sc,
79       std::vector<lldb_private::SourceModule> &imported_modules) override;
80   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
81   size_t
82   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
83 
84   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
85   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
86       lldb::user_id_t type_uid,
87       const lldb_private::ExecutionContext *exe_ctx) override;
88 
89   lldb_private::CompilerDeclContext
90   GetDeclContextForUID(lldb::user_id_t uid) override;
91   lldb_private::CompilerDeclContext
92   GetDeclContextContainingUID(lldb::user_id_t uid) override;
93   void
94   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
95 
96   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
97   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
98                                 lldb::SymbolContextItem resolve_scope,
99                                 lldb_private::SymbolContext &sc) override;
100   uint32_t ResolveSymbolContext(
101       const lldb_private::SourceLocationSpec &src_location_spec,
102       lldb::SymbolContextItem resolve_scope,
103       lldb_private::SymbolContextList &sc_list) override;
104   void
105   FindGlobalVariables(lldb_private::ConstString name,
106                       const lldb_private::CompilerDeclContext &parent_decl_ctx,
107                       uint32_t max_matches,
108                       lldb_private::VariableList &variables) override;
109   void FindGlobalVariables(const lldb_private::RegularExpression &regex,
110                            uint32_t max_matches,
111                            lldb_private::VariableList &variables) override;
112   void FindFunctions(lldb_private::ConstString name,
113                      const lldb_private::CompilerDeclContext &parent_decl_ctx,
114                      lldb::FunctionNameType name_type_mask,
115                      bool include_inlines,
116                      lldb_private::SymbolContextList &sc_list) override;
117   void FindFunctions(const lldb_private::RegularExpression &regex,
118                      bool include_inlines,
119                      lldb_private::SymbolContextList &sc_list) override;
120   void
121   FindTypes(lldb_private::ConstString name,
122             const lldb_private::CompilerDeclContext &parent_decl_ctx,
123             uint32_t max_matches,
124             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
125             lldb_private::TypeMap &types) override;
126   void
127   FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> context,
128             lldb_private::LanguageSet languages,
129             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
130             lldb_private::TypeMap &types) override;
131   lldb_private::CompilerDeclContext FindNamespace(
132       lldb_private::ConstString name,
133       const lldb_private::CompilerDeclContext &parent_decl_ctx) override;
134   void GetTypes(lldb_private::SymbolContextScope *sc_scope,
135                 lldb::TypeClass type_mask,
136                 lldb_private::TypeList &type_list) override;
137   std::vector<std::unique_ptr<lldb_private::CallEdge>>
138   ParseCallEdgesInFunction(lldb_private::UserID func_id) override;
139 
140   void DumpClangAST(lldb_private::Stream &s) override;
141 
142   // PluginInterface protocol
143   lldb_private::ConstString GetPluginName() override;
144 
145 protected:
146   enum { kHaveInitializedOSOs = (1 << 0), kNumFlags };
147 
148   friend class DebugMapModule;
149   friend class DWARFASTParserClang;
150   friend class DWARFCompileUnit;
151   friend class SymbolFileDWARF;
152   struct OSOInfo {
153     lldb::ModuleSP module_sp;
154 
OSOInfoOSOInfo155     OSOInfo() : module_sp() {}
156   };
157 
158   typedef std::shared_ptr<OSOInfo> OSOInfoSP;
159 
160   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
161                                         lldb::addr_t>
162       FileRangeMap;
163 
164   // Class specific types
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 = UINT32_MAX;
172     uint32_t last_symbol_index = UINT32_MAX;
173     uint32_t first_symbol_id = UINT32_MAX;
174     uint32_t last_symbol_id = UINT32_MAX;
175     FileRangeMap file_range_map;
176     bool file_range_map_valid = false;
177 
CompileUnitInfoCompileUnitInfo178     CompileUnitInfo()
179         : so_file(), oso_path(), oso_mod_time(), oso_sp(), compile_unit_sp(),
180 
181           file_range_map() {}
182 
183     const FileRangeMap &GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile);
184   };
185 
186   // Protected Member Functions
187   void InitOSO();
188 
189   uint32_t CalculateNumCompileUnits() override;
190   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
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   void PrivateFindGlobalVariables(
251       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, 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   // OSOEntry
277   class OSOEntry {
278   public:
279     OSOEntry() = default;
280 
OSOEntry(uint32_t exe_sym_idx,lldb::addr_t oso_file_addr)281     OSOEntry(uint32_t exe_sym_idx, lldb::addr_t oso_file_addr)
282         : m_exe_sym_idx(exe_sym_idx), m_oso_file_addr(oso_file_addr) {}
283 
GetExeSymbolIndex()284     uint32_t GetExeSymbolIndex() const { return m_exe_sym_idx; }
285 
286     bool operator<(const OSOEntry &rhs) const {
287       return m_exe_sym_idx < rhs.m_exe_sym_idx;
288     }
289 
GetOSOFileAddress()290     lldb::addr_t GetOSOFileAddress() const { return m_oso_file_addr; }
291 
SetOSOFileAddress(lldb::addr_t oso_file_addr)292     void SetOSOFileAddress(lldb::addr_t oso_file_addr) {
293       m_oso_file_addr = oso_file_addr;
294     }
295 
296   protected:
297     uint32_t m_exe_sym_idx = UINT32_MAX;
298     lldb::addr_t m_oso_file_addr = LLDB_INVALID_ADDRESS;
299   };
300 
301   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry>
302       DebugMap;
303 
304   // Member Variables
305   std::bitset<kNumFlags> m_flags;
306   std::vector<CompileUnitInfo> m_compile_unit_infos;
307   std::vector<uint32_t> m_func_indexes; // Sorted by address
308   std::vector<uint32_t> m_glob_indexes;
309   std::map<std::pair<lldb_private::ConstString, llvm::sys::TimePoint<>>,
310            OSOInfoSP>
311       m_oso_map;
312   UniqueDWARFASTTypeMap m_unique_ast_type_map;
313   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
314   DebugMap m_debug_map;
315 
316   // When an object file from the debug map gets parsed in
317   // SymbolFileDWARF, it needs to tell the debug map about the object
318   // files addresses by calling this function once for each N_FUN,
319   // N_GSYM and N_STSYM and after all entries in the debug map have
320   // been matched up, FinalizeOSOFileRanges() should be called.
321   bool AddOSOFileRange(CompileUnitInfo *cu_info, lldb::addr_t exe_file_addr,
322                        lldb::addr_t exe_byte_size, lldb::addr_t oso_file_addr,
323                        lldb::addr_t oso_byte_size);
324 
325   // Called after calling AddOSOFileRange() for each object file debug
326   // map entry to finalize the info for the unlinked compile unit.
327   void FinalizeOSOFileRanges(CompileUnitInfo *cu_info);
328 
329   /// Convert \a addr from a .o file address, to an executable address.
330   ///
331   /// \param[in] addr
332   ///     A section offset address from a .o file
333   ///
334   /// \return
335   ///     Returns true if \a addr was converted to be an executable
336   ///     section/offset address, false otherwise.
337   bool LinkOSOAddress(lldb_private::Address &addr);
338 
339   /// Convert a .o file "file address" to an executable "file address".
340   ///
341   /// \param[in] oso_symfile
342   ///     The DWARF symbol file that contains \a oso_file_addr
343   ///
344   /// \param[in] oso_file_addr
345   ///     A .o file "file address" to convert.
346   ///
347   /// \return
348   ///     LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
349   ///     linked executable, otherwise a valid "file address" from the
350   ///     linked executable that contains the debug map.
351   lldb::addr_t LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
352                                   lldb::addr_t oso_file_addr);
353 
354   /// Given a line table full of lines with "file addresses" that are
355   /// for a .o file represented by \a oso_symfile, link a new line table
356   /// and return it.
357   ///
358   /// \param[in] oso_symfile
359   ///     The DWARF symbol file that produced the \a line_table
360   ///
361   /// \param[in] line_table
362   ///     A pointer to the line table.
363   ///
364   /// \return
365   ///     Returns a valid line table full of linked addresses, or NULL
366   ///     if none of the line table addresses exist in the main
367   ///     executable.
368   lldb_private::LineTable *
369   LinkOSOLineTable(SymbolFileDWARF *oso_symfile,
370                    lldb_private::LineTable *line_table);
371 
372   size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data,
373                        DWARFDebugAranges *debug_aranges);
374 };
375 
376 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARFDEBUGMAP_H
377