1254721Semaste//===-- SymbolFile.h --------------------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#ifndef liblldb_SymbolFile_h_
10254721Semaste#define liblldb_SymbolFile_h_
11254721Semaste
12254721Semaste#include "lldb/Core/PluginInterface.h"
13296417Sdim#include "lldb/Symbol/CompilerDecl.h"
14296417Sdim#include "lldb/Symbol/CompilerDeclContext.h"
15314564Sdim#include "lldb/Symbol/CompilerType.h"
16344779Sdim#include "lldb/Symbol/Function.h"
17353358Sdim#include "lldb/Symbol/SourceModule.h"
18254721Semaste#include "lldb/Symbol/Type.h"
19360784Sdim#include "lldb/Symbol/TypeList.h"
20360784Sdim#include "lldb/Symbol/TypeSystem.h"
21314564Sdim#include "lldb/lldb-private.h"
22309124Sdim#include "llvm/ADT/DenseSet.h"
23360784Sdim#include "llvm/Support/Errc.h"
24309124Sdim
25344779Sdim#include <mutex>
26344779Sdim
27344779Sdim#if defined(LLDB_CONFIGURATION_DEBUG)
28344779Sdim#define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock())
29344779Sdim#else
30344779Sdim#define ASSERT_MODULE_LOCK(expr) ((void)0)
31344779Sdim#endif
32344779Sdim
33254721Semastenamespace lldb_private {
34254721Semaste
35314564Sdimclass SymbolFile : public PluginInterface {
36360784Sdim  /// LLVM RTTI support.
37360784Sdim  static char ID;
38360784Sdim
39254721Semastepublic:
40360784Sdim  /// LLVM RTTI support.
41360784Sdim  /// \{
42360784Sdim  virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
43360784Sdim  static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
44360784Sdim  /// \}
45360784Sdim
46314564Sdim  // Symbol file ability bits.
47314564Sdim  //
48341825Sdim  // Each symbol file can claim to support one or more symbol file abilities.
49341825Sdim  // These get returned from SymbolFile::GetAbilities(). These help us to
50341825Sdim  // determine which plug-in will be best to load the debug information found
51341825Sdim  // in files.
52314564Sdim  enum Abilities {
53314564Sdim    CompileUnits = (1u << 0),
54314564Sdim    LineTables = (1u << 1),
55314564Sdim    Functions = (1u << 2),
56314564Sdim    Blocks = (1u << 3),
57314564Sdim    GlobalVariables = (1u << 4),
58314564Sdim    LocalVariables = (1u << 5),
59314564Sdim    VariableTypes = (1u << 6),
60314564Sdim    kAllAbilities = ((1u << 7) - 1u)
61314564Sdim  };
62254721Semaste
63360784Sdim  static SymbolFile *FindPlugin(lldb::ObjectFileSP objfile_sp);
64254721Semaste
65314564Sdim  // Constructors and Destructors
66360784Sdim  SymbolFile(lldb::ObjectFileSP objfile_sp)
67360784Sdim      : m_objfile_sp(std::move(objfile_sp)), m_abilities(0),
68360784Sdim        m_calculated_abilities(false) {}
69254721Semaste
70314564Sdim  ~SymbolFile() override {}
71254721Semaste
72314564Sdim  /// Get a mask of what this symbol file supports for the object file
73314564Sdim  /// that it was constructed with.
74314564Sdim  ///
75314564Sdim  /// Each symbol file gets to respond with a mask of abilities that
76314564Sdim  /// it supports for each object file. This happens when we are
77314564Sdim  /// trying to figure out which symbol file plug-in will get used
78314564Sdim  /// for a given object file. The plug-in that responds with the
79314564Sdim  /// best mix of "SymbolFile::Abilities" bits set, will get chosen to
80314564Sdim  /// be the symbol file parser. This allows each plug-in to check for
81314564Sdim  /// sections that contain data a symbol file plug-in would need. For
82314564Sdim  /// example the DWARF plug-in requires DWARF sections in a file that
83314564Sdim  /// contain debug information. If the DWARF plug-in doesn't find
84314564Sdim  /// these sections, it won't respond with many ability bits set, and
85314564Sdim  /// we will probably fall back to the symbol table SymbolFile plug-in
86314564Sdim  /// which uses any information in the symbol table. Also, plug-ins
87314564Sdim  /// might check for some specific symbols in a symbol table in the
88314564Sdim  /// case where the symbol table contains debug information (STABS
89314564Sdim  /// and COFF). Not a lot of work should happen in these functions
90314564Sdim  /// as the plug-in might not get selected due to another plug-in
91314564Sdim  /// having more abilities. Any initialization work should be saved
92314564Sdim  /// for "void SymbolFile::InitializeObject()" which will get called
93314564Sdim  /// on the SymbolFile object with the best set of abilities.
94314564Sdim  ///
95353358Sdim  /// \return
96314564Sdim  ///     A uint32_t mask containing bits from the SymbolFile::Abilities
97314564Sdim  ///     enumeration. Any bits that are set represent an ability that
98314564Sdim  ///     this symbol plug-in can parse from the object file.
99314564Sdim  uint32_t GetAbilities() {
100314564Sdim    if (!m_calculated_abilities) {
101314564Sdim      m_abilities = CalculateAbilities();
102314564Sdim      m_calculated_abilities = true;
103254721Semaste    }
104254721Semaste
105314564Sdim    return m_abilities;
106314564Sdim  }
107254721Semaste
108314564Sdim  virtual uint32_t CalculateAbilities() = 0;
109296417Sdim
110344779Sdim  /// Symbols file subclasses should override this to return the Module that
111344779Sdim  /// owns the TypeSystem that this symbol file modifies type information in.
112344779Sdim  virtual std::recursive_mutex &GetModuleMutex() const;
113344779Sdim
114314564Sdim  /// Initialize the SymbolFile object.
115314564Sdim  ///
116314564Sdim  /// The SymbolFile object with the best set of abilities (detected
117314564Sdim  /// in "uint32_t SymbolFile::GetAbilities()) will have this function
118314564Sdim  /// called if it is chosen to parse an object file. More complete
119314564Sdim  /// initialization can happen in this function which will get called
120314564Sdim  /// prior to any other functions in the SymbolFile protocol.
121314564Sdim  virtual void InitializeObject() {}
122296417Sdim
123314564Sdim  // Compile Unit function calls
124314564Sdim  // Approach 1 - iterator
125360784Sdim  uint32_t GetNumCompileUnits();
126360784Sdim  lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx);
127296417Sdim
128360784Sdim  Symtab *GetSymtab();
129360784Sdim
130344779Sdim  virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0;
131344779Sdim  virtual size_t ParseFunctions(CompileUnit &comp_unit) = 0;
132344779Sdim  virtual bool ParseLineTable(CompileUnit &comp_unit) = 0;
133344779Sdim  virtual bool ParseDebugMacros(CompileUnit &comp_unit) = 0;
134360784Sdim
135360784Sdim  /// Apply a lambda to each external lldb::Module referenced by this
136360784Sdim  /// \p comp_unit. Recursively also descends into the referenced external
137360784Sdim  /// modules of any encountered compilation unit.
138360784Sdim  ///
139360784Sdim  /// \param comp_unit
140360784Sdim  ///     When this SymbolFile consists of multiple auxilliary
141360784Sdim  ///     SymbolFiles, for example, a Darwin debug map that references
142360784Sdim  ///     multiple .o files, comp_unit helps choose the auxilliary
143360784Sdim  ///     file. In most other cases comp_unit's symbol file is
144360784Sdim  ///     identiacal with *this.
145360784Sdim  ///
146360784Sdim  /// \param[in] lambda
147360784Sdim  ///     The lambda that should be applied to every function. The lambda can
148360784Sdim  ///     return true if the iteration should be aborted earlier.
149360784Sdim  ///
150360784Sdim  /// \param visited_symbol_files
151360784Sdim  ///     A set of SymbolFiles that were already visited to avoid
152360784Sdim  ///     visiting one file more than once.
153360784Sdim  ///
154360784Sdim  /// \return
155360784Sdim  ///     If the lambda early-exited, this function returns true to
156360784Sdim  ///     propagate the early exit.
157360784Sdim  virtual bool ForEachExternalModule(
158360784Sdim      lldb_private::CompileUnit &comp_unit,
159360784Sdim      llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
160360784Sdim      llvm::function_ref<bool(Module &)> lambda) {
161360784Sdim    return false;
162360784Sdim  }
163344779Sdim  virtual bool ParseSupportFiles(CompileUnit &comp_unit,
164344779Sdim                                 FileSpecList &support_files) = 0;
165344779Sdim  virtual size_t ParseTypes(CompileUnit &comp_unit) = 0;
166344779Sdim  virtual bool ParseIsOptimized(CompileUnit &comp_unit) { return false; }
167344779Sdim
168314564Sdim  virtual bool
169314564Sdim  ParseImportedModules(const SymbolContext &sc,
170353358Sdim                       std::vector<SourceModule> &imported_modules) = 0;
171344779Sdim  virtual size_t ParseBlocksRecursive(Function &func) = 0;
172314564Sdim  virtual size_t ParseVariablesForContext(const SymbolContext &sc) = 0;
173314564Sdim  virtual Type *ResolveTypeUID(lldb::user_id_t type_uid) = 0;
174344779Sdim
175344779Sdim
176344779Sdim  /// The characteristics of an array type.
177344779Sdim  struct ArrayInfo {
178353358Sdim    int64_t first_index = 0;
179344779Sdim    llvm::SmallVector<uint64_t, 1> element_orders;
180353358Sdim    uint32_t byte_stride = 0;
181353358Sdim    uint32_t bit_stride = 0;
182344779Sdim  };
183344779Sdim  /// If \c type_uid points to an array type, return its characteristics.
184344779Sdim  /// To support variable-length array types, this function takes an
185344779Sdim  /// optional \p ExtecutionContext. If \c exe_ctx is non-null, the
186344779Sdim  /// dynamic characteristics for that context are returned.
187344779Sdim  virtual llvm::Optional<ArrayInfo>
188344779Sdim  GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,
189344779Sdim                            const lldb_private::ExecutionContext *exe_ctx) = 0;
190344779Sdim
191314564Sdim  virtual bool CompleteType(CompilerType &compiler_type) = 0;
192314564Sdim  virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {}
193314564Sdim  virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) {
194314564Sdim    return CompilerDecl();
195314564Sdim  }
196314564Sdim  virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) {
197314564Sdim    return CompilerDeclContext();
198314564Sdim  }
199314564Sdim  virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) {
200314564Sdim    return CompilerDeclContext();
201314564Sdim  }
202314564Sdim  virtual uint32_t ResolveSymbolContext(const Address &so_addr,
203344779Sdim                                        lldb::SymbolContextItem resolve_scope,
204314564Sdim                                        SymbolContext &sc) = 0;
205314564Sdim  virtual uint32_t ResolveSymbolContext(const FileSpec &file_spec,
206314564Sdim                                        uint32_t line, bool check_inlines,
207344779Sdim                                        lldb::SymbolContextItem resolve_scope,
208314564Sdim                                        SymbolContextList &sc_list);
209344779Sdim
210344779Sdim  virtual void DumpClangAST(Stream &s) {}
211360784Sdim  virtual void
212353358Sdim  FindGlobalVariables(ConstString name,
213341825Sdim                      const CompilerDeclContext *parent_decl_ctx,
214314564Sdim                      uint32_t max_matches, VariableList &variables);
215360784Sdim  virtual void FindGlobalVariables(const RegularExpression &regex,
216360784Sdim                                   uint32_t max_matches,
217360784Sdim                                   VariableList &variables);
218360784Sdim  virtual void FindFunctions(ConstString name,
219360784Sdim                             const CompilerDeclContext *parent_decl_ctx,
220360784Sdim                             lldb::FunctionNameType name_type_mask,
221360784Sdim                             bool include_inlines, SymbolContextList &sc_list);
222360784Sdim  virtual void FindFunctions(const RegularExpression &regex,
223360784Sdim                             bool include_inlines, SymbolContextList &sc_list);
224360784Sdim  virtual void
225353358Sdim  FindTypes(ConstString name, const CompilerDeclContext *parent_decl_ctx,
226360784Sdim            uint32_t max_matches,
227314564Sdim            llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
228314564Sdim            TypeMap &types);
229254721Semaste
230360784Sdim  /// Find types specified by a CompilerContextPattern.
231360784Sdim  /// \param languages
232360784Sdim  ///     Only return results in these languages.
233360784Sdim  /// \param searched_symbol_files
234360784Sdim  ///     Prevents one file from being visited multiple times.
235314564Sdim  virtual void
236360784Sdim  FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
237360784Sdim            llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
238360784Sdim            TypeMap &types);
239360784Sdim
240360784Sdim  virtual void
241314564Sdim  GetMangledNamesForFunction(const std::string &scope_qualified_name,
242314564Sdim                             std::vector<ConstString> &mangled_names);
243276479Sdim
244360784Sdim  virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope,
245360784Sdim                        lldb::TypeClass type_mask,
246360784Sdim                        lldb_private::TypeList &type_list) = 0;
247360784Sdim
248321369Sdim  virtual void PreloadSymbols();
249321369Sdim
250360784Sdim  virtual llvm::Expected<lldb_private::TypeSystem &>
251314564Sdim  GetTypeSystemForLanguage(lldb::LanguageType language);
252276479Sdim
253314564Sdim  virtual CompilerDeclContext
254353358Sdim  FindNamespace(ConstString name,
255314564Sdim                const CompilerDeclContext *parent_decl_ctx) {
256314564Sdim    return CompilerDeclContext();
257314564Sdim  }
258314564Sdim
259360784Sdim  ObjectFile *GetObjectFile() { return m_objfile_sp.get(); }
260360784Sdim  const ObjectFile *GetObjectFile() const { return m_objfile_sp.get(); }
261360784Sdim  ObjectFile *GetMainObjectFile();
262314564Sdim
263360784Sdim  virtual std::vector<std::unique_ptr<CallEdge>>
264360784Sdim  ParseCallEdgesInFunction(UserID func_id) {
265344779Sdim    return {};
266344779Sdim  }
267344779Sdim
268344779Sdim  virtual void AddSymbols(Symtab &symtab) {}
269344779Sdim
270314564Sdim  /// Notify the SymbolFile that the file addresses in the Sections
271314564Sdim  /// for this module have been changed.
272360784Sdim  virtual void SectionFileAddressesChanged();
273314564Sdim
274353358Sdim  struct RegisterInfoResolver {
275353358Sdim    virtual ~RegisterInfoResolver(); // anchor
276353358Sdim
277353358Sdim    virtual const RegisterInfo *ResolveName(llvm::StringRef name) const = 0;
278353358Sdim    virtual const RegisterInfo *ResolveNumber(lldb::RegisterKind kind,
279353358Sdim                                              uint32_t number) const = 0;
280353358Sdim  };
281353358Sdim  virtual lldb::UnwindPlanSP
282353358Sdim  GetUnwindPlan(const Address &address, const RegisterInfoResolver &resolver) {
283353358Sdim    return nullptr;
284353358Sdim  }
285353358Sdim
286360784Sdim  /// Return the number of stack bytes taken up by the parameters to this
287360784Sdim  /// function.
288360784Sdim  virtual llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) {
289360784Sdim    return llvm::createStringError(make_error_code(llvm::errc::not_supported),
290360784Sdim                                   "Operation not supported.");
291360784Sdim  }
292341825Sdim
293360784Sdim  virtual void Dump(Stream &s);
294360784Sdim
295254721Semasteprotected:
296344779Sdim  void AssertModuleLock();
297360784Sdim  virtual uint32_t CalculateNumCompileUnits() = 0;
298360784Sdim  virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0;
299360784Sdim  virtual TypeList &GetTypeList() { return m_type_list; }
300344779Sdim
301360784Sdim  void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp);
302360784Sdim
303360784Sdim  lldb::ObjectFileSP m_objfile_sp; // Keep a reference to the object file in
304360784Sdim                                   // case it isn't the same as the module
305360784Sdim                                   // object file (debug symbols in a separate
306360784Sdim                                   // file)
307360784Sdim  llvm::Optional<std::vector<lldb::CompUnitSP>> m_compile_units;
308360784Sdim  TypeList m_type_list;
309360784Sdim  Symtab *m_symtab = nullptr;
310314564Sdim  uint32_t m_abilities;
311314564Sdim  bool m_calculated_abilities;
312296417Sdim
313254721Semasteprivate:
314314564Sdim  DISALLOW_COPY_AND_ASSIGN(SymbolFile);
315254721Semaste};
316254721Semaste
317254721Semaste} // namespace lldb_private
318254721Semaste
319296417Sdim#endif // liblldb_SymbolFile_h_
320