xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Symbol/SymbolFile.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1 //===-- SymbolFile.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_SYMBOL_SYMBOLFILE_H
10 #define LLDB_SYMBOL_SYMBOLFILE_H
11 
12 #include "lldb/Core/PluginInterface.h"
13 #include "lldb/Core/SourceLocationSpec.h"
14 #include "lldb/Symbol/CompilerDecl.h"
15 #include "lldb/Symbol/CompilerDeclContext.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/SourceModule.h"
19 #include "lldb/Symbol/Type.h"
20 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Symbol/TypeSystem.h"
22 #include "lldb/Target/Statistics.h"
23 #include "lldb/Utility/XcodeSDK.h"
24 #include "lldb/lldb-private.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/Support/Errc.h"
27 
28 #include <mutex>
29 
30 #if defined(LLDB_CONFIGURATION_DEBUG)
31 #define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock())
32 #else
33 #define ASSERT_MODULE_LOCK(expr) ((void)0)
34 #endif
35 
36 namespace lldb_private {
37 
38 class SymbolFile : public PluginInterface {
39   /// LLVM RTTI support.
40   static char ID;
41 
42 public:
43   /// LLVM RTTI support.
44   /// \{
45   virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
46   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
47   /// \}
48 
49   // Symbol file ability bits.
50   //
51   // Each symbol file can claim to support one or more symbol file abilities.
52   // These get returned from SymbolFile::GetAbilities(). These help us to
53   // determine which plug-in will be best to load the debug information found
54   // in files.
55   enum Abilities {
56     CompileUnits = (1u << 0),
57     LineTables = (1u << 1),
58     Functions = (1u << 2),
59     Blocks = (1u << 3),
60     GlobalVariables = (1u << 4),
61     LocalVariables = (1u << 5),
62     VariableTypes = (1u << 6),
63     kAllAbilities = ((1u << 7) - 1u)
64   };
65 
66   static SymbolFile *FindPlugin(lldb::ObjectFileSP objfile_sp);
67 
68   // Constructors and Destructors
69   SymbolFile(lldb::ObjectFileSP objfile_sp)
70       : m_objfile_sp(std::move(objfile_sp)), m_abilities(0),
71         m_calculated_abilities(false) {}
72 
73   ~SymbolFile() override = default;
74 
75   /// Get a mask of what this symbol file supports for the object file
76   /// that it was constructed with.
77   ///
78   /// Each symbol file gets to respond with a mask of abilities that
79   /// it supports for each object file. This happens when we are
80   /// trying to figure out which symbol file plug-in will get used
81   /// for a given object file. The plug-in that responds with the
82   /// best mix of "SymbolFile::Abilities" bits set, will get chosen to
83   /// be the symbol file parser. This allows each plug-in to check for
84   /// sections that contain data a symbol file plug-in would need. For
85   /// example the DWARF plug-in requires DWARF sections in a file that
86   /// contain debug information. If the DWARF plug-in doesn't find
87   /// these sections, it won't respond with many ability bits set, and
88   /// we will probably fall back to the symbol table SymbolFile plug-in
89   /// which uses any information in the symbol table. Also, plug-ins
90   /// might check for some specific symbols in a symbol table in the
91   /// case where the symbol table contains debug information (STABS
92   /// and COFF). Not a lot of work should happen in these functions
93   /// as the plug-in might not get selected due to another plug-in
94   /// having more abilities. Any initialization work should be saved
95   /// for "void SymbolFile::InitializeObject()" which will get called
96   /// on the SymbolFile object with the best set of abilities.
97   ///
98   /// \return
99   ///     A uint32_t mask containing bits from the SymbolFile::Abilities
100   ///     enumeration. Any bits that are set represent an ability that
101   ///     this symbol plug-in can parse from the object file.
102   uint32_t GetAbilities() {
103     if (!m_calculated_abilities) {
104       m_abilities = CalculateAbilities();
105       m_calculated_abilities = true;
106     }
107 
108     return m_abilities;
109   }
110 
111   virtual uint32_t CalculateAbilities() = 0;
112 
113   /// Symbols file subclasses should override this to return the Module that
114   /// owns the TypeSystem that this symbol file modifies type information in.
115   virtual std::recursive_mutex &GetModuleMutex() const;
116 
117   /// Initialize the SymbolFile object.
118   ///
119   /// The SymbolFile object with the best set of abilities (detected
120   /// in "uint32_t SymbolFile::GetAbilities()) will have this function
121   /// called if it is chosen to parse an object file. More complete
122   /// initialization can happen in this function which will get called
123   /// prior to any other functions in the SymbolFile protocol.
124   virtual void InitializeObject() {}
125 
126   // Compile Unit function calls
127   // Approach 1 - iterator
128   uint32_t GetNumCompileUnits();
129   lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx);
130 
131   Symtab *GetSymtab();
132 
133   virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0;
134   /// Return the Xcode SDK comp_unit was compiled against.
135   virtual XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) { return {}; }
136   virtual size_t ParseFunctions(CompileUnit &comp_unit) = 0;
137   virtual bool ParseLineTable(CompileUnit &comp_unit) = 0;
138   virtual bool ParseDebugMacros(CompileUnit &comp_unit) = 0;
139 
140   /// Apply a lambda to each external lldb::Module referenced by this
141   /// \p comp_unit. Recursively also descends into the referenced external
142   /// modules of any encountered compilation unit.
143   ///
144   /// This function can be used to traverse Clang -gmodules debug
145   /// information, which is stored in DWARF files separate from the
146   /// object files.
147   ///
148   /// \param comp_unit
149   ///     When this SymbolFile consists of multiple auxilliary
150   ///     SymbolFiles, for example, a Darwin debug map that references
151   ///     multiple .o files, comp_unit helps choose the auxilliary
152   ///     file. In most other cases comp_unit's symbol file is
153   ///     identical with *this.
154   ///
155   /// \param[in] lambda
156   ///     The lambda that should be applied to every function. The lambda can
157   ///     return true if the iteration should be aborted earlier.
158   ///
159   /// \param visited_symbol_files
160   ///     A set of SymbolFiles that were already visited to avoid
161   ///     visiting one file more than once.
162   ///
163   /// \return
164   ///     If the lambda early-exited, this function returns true to
165   ///     propagate the early exit.
166   virtual bool ForEachExternalModule(
167       lldb_private::CompileUnit &comp_unit,
168       llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
169       llvm::function_ref<bool(Module &)> lambda) {
170     return false;
171   }
172   virtual bool ParseSupportFiles(CompileUnit &comp_unit,
173                                  FileSpecList &support_files) = 0;
174   virtual size_t ParseTypes(CompileUnit &comp_unit) = 0;
175   virtual bool ParseIsOptimized(CompileUnit &comp_unit) { return false; }
176 
177   virtual bool
178   ParseImportedModules(const SymbolContext &sc,
179                        std::vector<SourceModule> &imported_modules) = 0;
180   virtual size_t ParseBlocksRecursive(Function &func) = 0;
181   virtual size_t ParseVariablesForContext(const SymbolContext &sc) = 0;
182   virtual Type *ResolveTypeUID(lldb::user_id_t type_uid) = 0;
183 
184   /// The characteristics of an array type.
185   struct ArrayInfo {
186     int64_t first_index = 0;
187     llvm::SmallVector<uint64_t, 1> element_orders;
188     uint32_t byte_stride = 0;
189     uint32_t bit_stride = 0;
190   };
191   /// If \c type_uid points to an array type, return its characteristics.
192   /// To support variable-length array types, this function takes an
193   /// optional \p ExecutionContext. If \c exe_ctx is non-null, the
194   /// dynamic characteristics for that context are returned.
195   virtual llvm::Optional<ArrayInfo>
196   GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,
197                             const lldb_private::ExecutionContext *exe_ctx) = 0;
198 
199   virtual bool CompleteType(CompilerType &compiler_type) = 0;
200   virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {}
201   virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) {
202     return CompilerDecl();
203   }
204   virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) {
205     return CompilerDeclContext();
206   }
207   virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) {
208     return CompilerDeclContext();
209   }
210   virtual uint32_t ResolveSymbolContext(const Address &so_addr,
211                                         lldb::SymbolContextItem resolve_scope,
212                                         SymbolContext &sc) = 0;
213   virtual uint32_t
214   ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
215                        lldb::SymbolContextItem resolve_scope,
216                        SymbolContextList &sc_list);
217 
218   virtual void DumpClangAST(Stream &s) {}
219   virtual void FindGlobalVariables(ConstString name,
220                                    const CompilerDeclContext &parent_decl_ctx,
221                                    uint32_t max_matches,
222                                    VariableList &variables);
223   virtual void FindGlobalVariables(const RegularExpression &regex,
224                                    uint32_t max_matches,
225                                    VariableList &variables);
226   virtual void FindFunctions(ConstString name,
227                              const CompilerDeclContext &parent_decl_ctx,
228                              lldb::FunctionNameType name_type_mask,
229                              bool include_inlines, SymbolContextList &sc_list);
230   virtual void FindFunctions(const RegularExpression &regex,
231                              bool include_inlines, SymbolContextList &sc_list);
232   virtual void
233   FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx,
234             uint32_t max_matches,
235             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
236             TypeMap &types);
237 
238   /// Find types specified by a CompilerContextPattern.
239   /// \param languages
240   ///     Only return results in these languages.
241   /// \param searched_symbol_files
242   ///     Prevents one file from being visited multiple times.
243   virtual void
244   FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
245             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
246             TypeMap &types);
247 
248   virtual void
249   GetMangledNamesForFunction(const std::string &scope_qualified_name,
250                              std::vector<ConstString> &mangled_names);
251 
252   virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope,
253                         lldb::TypeClass type_mask,
254                         lldb_private::TypeList &type_list) = 0;
255 
256   virtual void PreloadSymbols();
257 
258   virtual llvm::Expected<lldb_private::TypeSystem &>
259   GetTypeSystemForLanguage(lldb::LanguageType language);
260 
261   virtual CompilerDeclContext
262   FindNamespace(ConstString name, const CompilerDeclContext &parent_decl_ctx) {
263     return CompilerDeclContext();
264   }
265 
266   ObjectFile *GetObjectFile() { return m_objfile_sp.get(); }
267   const ObjectFile *GetObjectFile() const { return m_objfile_sp.get(); }
268   ObjectFile *GetMainObjectFile();
269 
270   virtual std::vector<std::unique_ptr<CallEdge>>
271   ParseCallEdgesInFunction(UserID func_id) {
272     return {};
273   }
274 
275   virtual void AddSymbols(Symtab &symtab) {}
276 
277   /// Notify the SymbolFile that the file addresses in the Sections
278   /// for this module have been changed.
279   virtual void SectionFileAddressesChanged();
280 
281   struct RegisterInfoResolver {
282     virtual ~RegisterInfoResolver(); // anchor
283 
284     virtual const RegisterInfo *ResolveName(llvm::StringRef name) const = 0;
285     virtual const RegisterInfo *ResolveNumber(lldb::RegisterKind kind,
286                                               uint32_t number) const = 0;
287   };
288   virtual lldb::UnwindPlanSP
289   GetUnwindPlan(const Address &address, const RegisterInfoResolver &resolver) {
290     return nullptr;
291   }
292 
293   /// Return the number of stack bytes taken up by the parameters to this
294   /// function.
295   virtual llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) {
296     return llvm::createStringError(make_error_code(llvm::errc::not_supported),
297                                    "Operation not supported.");
298   }
299 
300   virtual void Dump(Stream &s);
301 
302   /// Metrics gathering functions
303 
304   /// Return the size in bytes of all debug information in the symbol file.
305   ///
306   /// If the debug information is contained in sections of an ObjectFile, then
307   /// this call should add the size of all sections that contain debug
308   /// information. Symbols the symbol tables are not considered debug
309   /// information for this call to make it easy and quick for this number to be
310   /// calculated. If the symbol file is all debug information, the size of the
311   /// entire file should be returned. The default implementation of this
312   /// function will iterate over all sections in a module and add up their
313   /// debug info only section byte sizes.
314   virtual uint64_t GetDebugInfoSize();
315 
316   /// Return the time taken to parse the debug information.
317   ///
318   /// \returns 0.0 if no information has been parsed or if there is
319   /// no computational cost to parsing the debug information.
320   virtual StatsDuration GetDebugInfoParseTime() { return StatsDuration(0.0); }
321 
322   /// Return the time it took to index the debug information in the object
323   /// file.
324   ///
325   /// \returns 0.0 if the file doesn't need to be indexed or if it
326   /// hasn't been indexed yet, or a valid duration if it has.
327   virtual StatsDuration GetDebugInfoIndexTime() { return StatsDuration(0.0); }
328 
329 protected:
330   void AssertModuleLock();
331   virtual uint32_t CalculateNumCompileUnits() = 0;
332   virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0;
333   virtual TypeList &GetTypeList() { return m_type_list; }
334 
335   void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp);
336 
337   lldb::ObjectFileSP m_objfile_sp; // Keep a reference to the object file in
338                                    // case it isn't the same as the module
339                                    // object file (debug symbols in a separate
340                                    // file)
341   llvm::Optional<std::vector<lldb::CompUnitSP>> m_compile_units;
342   TypeList m_type_list;
343   Symtab *m_symtab = nullptr;
344   uint32_t m_abilities;
345   bool m_calculated_abilities;
346 
347 private:
348   SymbolFile(const SymbolFile &) = delete;
349   const SymbolFile &operator=(const SymbolFile &) = delete;
350 };
351 
352 } // namespace lldb_private
353 
354 #endif // LLDB_SYMBOL_SYMBOLFILE_H
355