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