xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===-- SymbolFilePDB.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_PDB_SYMBOLFILEPDB_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_PDB_SYMBOLFILEPDB_H
11 
12 #include "lldb/Core/UniqueCStringMap.h"
13 #include "lldb/Symbol/SymbolFile.h"
14 #include "lldb/Symbol/VariableList.h"
15 #include "lldb/Utility/UserID.h"
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/DebugInfo/PDB/IPDBSession.h"
19 #include "llvm/DebugInfo/PDB/PDB.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
21 #include <optional>
22 
23 class PDBASTParser;
24 
25 class SymbolFilePDB : public lldb_private::SymbolFileCommon {
26   /// LLVM RTTI support.
27   static char ID;
28 
29 public:
30   /// LLVM RTTI support.
31   /// \{
32   bool isA(const void *ClassID) const override {
33     return ClassID == &ID || SymbolFileCommon::isA(ClassID);
34   }
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 void DebuggerInitialize(lldb_private::Debugger &debugger);
44 
45   static llvm::StringRef GetPluginNameStatic() { return "pdb"; }
46 
47   static llvm::StringRef GetPluginDescriptionStatic();
48 
49   static lldb_private::SymbolFile *
50   CreateInstance(lldb::ObjectFileSP objfile_sp);
51 
52   // Constructors and Destructors
53   SymbolFilePDB(lldb::ObjectFileSP objfile_sp);
54 
55   ~SymbolFilePDB() override;
56 
57   uint32_t CalculateAbilities() override;
58 
59   void InitializeObject() override;
60 
61   // Compile Unit function calls
62 
63   lldb::LanguageType
64   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
65 
66   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
67 
68   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
69 
70   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
71 
72   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
73                          lldb_private::SupportFileList &support_files) 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 
81   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
82 
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   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
92 
93   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
94 
95   lldb_private::CompilerDeclContext
96   GetDeclContextForUID(lldb::user_id_t uid) override;
97 
98   lldb_private::CompilerDeclContext
99   GetDeclContextContainingUID(lldb::user_id_t uid) override;
100 
101   void
102   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
103 
104   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
105                                 lldb::SymbolContextItem resolve_scope,
106                                 lldb_private::SymbolContext &sc) override;
107 
108   uint32_t ResolveSymbolContext(
109       const lldb_private::SourceLocationSpec &src_location_spec,
110       lldb::SymbolContextItem resolve_scope,
111       lldb_private::SymbolContextList &sc_list) override;
112 
113   void
114   FindGlobalVariables(lldb_private::ConstString name,
115                       const lldb_private::CompilerDeclContext &parent_decl_ctx,
116                       uint32_t max_matches,
117                       lldb_private::VariableList &variables) override;
118 
119   void FindGlobalVariables(const lldb_private::RegularExpression &regex,
120                            uint32_t max_matches,
121                            lldb_private::VariableList &variables) override;
122 
123   void FindFunctions(const lldb_private::Module::LookupInfo &lookup_info,
124                      const lldb_private::CompilerDeclContext &parent_decl_ctx,
125                      bool include_inlines,
126                      lldb_private::SymbolContextList &sc_list) override;
127 
128   void FindFunctions(const lldb_private::RegularExpression &regex,
129                      bool include_inlines,
130                      lldb_private::SymbolContextList &sc_list) override;
131 
132   void GetMangledNamesForFunction(
133       const std::string &scope_qualified_name,
134       std::vector<lldb_private::ConstString> &mangled_names) override;
135 
136   void AddSymbols(lldb_private::Symtab &symtab) override;
137   void FindTypes(const lldb_private::TypeQuery &match,
138                  lldb_private::TypeResults &results) override;
139   void FindTypesByRegex(const lldb_private::RegularExpression &regex,
140                         uint32_t max_matches, lldb_private::TypeMap &types);
141 
142   void GetTypes(lldb_private::SymbolContextScope *sc_scope,
143                 lldb::TypeClass type_mask,
144                 lldb_private::TypeList &type_list) override;
145 
146   llvm::Expected<lldb::TypeSystemSP>
147   GetTypeSystemForLanguage(lldb::LanguageType language) override;
148 
149   lldb_private::CompilerDeclContext
150   FindNamespace(lldb_private::ConstString name,
151                 const lldb_private::CompilerDeclContext &parent_decl_ctx,
152                 bool only_root_namespaces) override;
153 
154   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
155 
156   llvm::pdb::IPDBSession &GetPDBSession();
157 
158   const llvm::pdb::IPDBSession &GetPDBSession() const;
159 
160   void DumpClangAST(lldb_private::Stream &s) override;
161 
162 private:
163   struct SecContribInfo {
164     uint32_t Offset;
165     uint32_t Size;
166     uint32_t CompilandId;
167   };
168   using SecContribsMap = std::map<uint32_t, std::vector<SecContribInfo>>;
169 
170   uint32_t CalculateNumCompileUnits() override;
171 
172   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
173 
174   lldb::CompUnitSP ParseCompileUnitForUID(uint32_t id,
175                                           uint32_t index = UINT32_MAX);
176 
177   bool ParseCompileUnitLineTable(lldb_private::CompileUnit &comp_unit,
178                                  uint32_t match_line);
179 
180   void BuildSupportFileIdToSupportFileIndexMap(
181       const llvm::pdb::PDBSymbolCompiland &pdb_compiland,
182       llvm::DenseMap<uint32_t, uint32_t> &index_map) const;
183 
184   void FindTypesByName(llvm::StringRef name,
185                        const lldb_private::CompilerDeclContext &parent_decl_ctx,
186                        uint32_t max_matches, lldb_private::TypeMap &types);
187 
188   std::string GetMangledForPDBData(const llvm::pdb::PDBSymbolData &pdb_data);
189 
190   lldb::VariableSP
191   ParseVariableForPDBData(const lldb_private::SymbolContext &sc,
192                           const llvm::pdb::PDBSymbolData &pdb_data);
193 
194   size_t ParseVariables(const lldb_private::SymbolContext &sc,
195                         const llvm::pdb::PDBSymbol &pdb_data,
196                         lldb_private::VariableList *variable_list = nullptr);
197 
198   lldb::CompUnitSP
199   GetCompileUnitContainsAddress(const lldb_private::Address &so_addr);
200 
201   typedef std::vector<lldb_private::Type *> TypeCollection;
202 
203   void GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
204                             uint32_t type_mask,
205                             TypeCollection &type_collection);
206 
207   lldb_private::Function *
208   ParseCompileUnitFunctionForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func,
209                                      lldb_private::CompileUnit &comp_unit);
210 
211   void GetCompileUnitIndex(const llvm::pdb::PDBSymbolCompiland &pdb_compiland,
212                            uint32_t &index);
213 
214   PDBASTParser *GetPDBAstParser();
215 
216   std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
217   GetPDBCompilandByUID(uint32_t uid);
218 
219   lldb_private::Mangled
220   GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func);
221 
222   bool ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
223                        bool include_inlines,
224                        lldb_private::SymbolContextList &sc_list);
225 
226   bool ResolveFunction(uint32_t uid, bool include_inlines,
227                        lldb_private::SymbolContextList &sc_list);
228 
229   void CacheFunctionNames();
230 
231   bool DeclContextMatchesThisSymbolFile(
232       const lldb_private::CompilerDeclContext &decl_ctx);
233 
234   uint32_t GetCompilandId(const llvm::pdb::PDBSymbolData &data);
235 
236   llvm::DenseMap<uint32_t, lldb::CompUnitSP> m_comp_units;
237   llvm::DenseMap<uint32_t, lldb::TypeSP> m_types;
238   llvm::DenseMap<uint32_t, lldb::VariableSP> m_variables;
239   llvm::DenseMap<uint64_t, std::string> m_public_names;
240 
241   SecContribsMap m_sec_contribs;
242 
243   std::vector<lldb::TypeSP> m_builtin_types;
244   std::unique_ptr<llvm::pdb::IPDBSession> m_session_up;
245   std::unique_ptr<llvm::pdb::PDBSymbolExe> m_global_scope_up;
246 
247   lldb_private::UniqueCStringMap<uint32_t> m_func_full_names;
248   lldb_private::UniqueCStringMap<uint32_t> m_func_base_names;
249   lldb_private::UniqueCStringMap<uint32_t> m_func_method_names;
250 };
251 
252 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_PDB_SYMBOLFILEPDB_H
253