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_Plugins_SymbolFile_PDB_SymbolFilePDB_h_
10 #define lldb_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 
22 class PDBASTParser;
23 
24 class SymbolFilePDB : public lldb_private::SymbolFile {
25 public:
26   // Static Functions
27   static void Initialize();
28 
29   static void Terminate();
30 
31   static void DebuggerInitialize(lldb_private::Debugger &debugger);
32 
33   static lldb_private::ConstString GetPluginNameStatic();
34 
35   static const char *GetPluginDescriptionStatic();
36 
37   static lldb_private::SymbolFile *
38   CreateInstance(lldb::ObjectFileSP objfile_sp);
39 
40   // Constructors and Destructors
41   SymbolFilePDB(lldb::ObjectFileSP objfile_sp);
42 
43   ~SymbolFilePDB() override;
44 
45   uint32_t CalculateAbilities() override;
46 
47   void InitializeObject() override;
48 
49   // Compile Unit function calls
50 
51   lldb::LanguageType
52   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
53 
54   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
55 
56   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
57 
58   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
59 
60   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
61                          lldb_private::FileSpecList &support_files) override;
62 
63   size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
64 
65   bool ParseImportedModules(
66       const lldb_private::SymbolContext &sc,
67       std::vector<lldb_private::SourceModule> &imported_modules) override;
68 
69   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
70 
71   size_t
72   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
73 
74   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
75   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
76       lldb::user_id_t type_uid,
77       const lldb_private::ExecutionContext *exe_ctx) override;
78 
79   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
80 
81   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
82 
83   lldb_private::CompilerDeclContext
84   GetDeclContextForUID(lldb::user_id_t uid) override;
85 
86   lldb_private::CompilerDeclContext
87   GetDeclContextContainingUID(lldb::user_id_t uid) override;
88 
89   void
90   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
91 
92   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
93                                 lldb::SymbolContextItem resolve_scope,
94                                 lldb_private::SymbolContext &sc) override;
95 
96   uint32_t
97   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
98                        bool check_inlines,
99                        lldb::SymbolContextItem resolve_scope,
100                        lldb_private::SymbolContextList &sc_list) override;
101 
102   void
103   FindGlobalVariables(lldb_private::ConstString name,
104                       const lldb_private::CompilerDeclContext *parent_decl_ctx,
105                       uint32_t max_matches,
106                       lldb_private::VariableList &variables) override;
107 
108   void FindGlobalVariables(const lldb_private::RegularExpression &regex,
109                            uint32_t max_matches,
110                            lldb_private::VariableList &variables) override;
111 
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 
118   void FindFunctions(const lldb_private::RegularExpression &regex,
119                      bool include_inlines,
120                      lldb_private::SymbolContextList &sc_list) override;
121 
122   void GetMangledNamesForFunction(
123       const std::string &scope_qualified_name,
124       std::vector<lldb_private::ConstString> &mangled_names) override;
125 
126   void AddSymbols(lldb_private::Symtab &symtab) override;
127 
128   void
129   FindTypes(lldb_private::ConstString name,
130             const lldb_private::CompilerDeclContext *parent_decl_ctx,
131             uint32_t max_matches,
132             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
133             lldb_private::TypeMap &types) override;
134 
135   void FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
136                  lldb_private::LanguageSet languages,
137                  lldb_private::TypeMap &types) override;
138 
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_private::TypeSystem &>
147   GetTypeSystemForLanguage(lldb::LanguageType language) override;
148 
149   lldb_private::CompilerDeclContext FindNamespace(
150       lldb_private::ConstString name,
151       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
152 
153   lldb_private::ConstString GetPluginName() override;
154 
155   uint32_t GetPluginVersion() override;
156 
157   llvm::pdb::IPDBSession &GetPDBSession();
158 
159   const llvm::pdb::IPDBSession &GetPDBSession() const;
160 
161   void DumpClangAST(lldb_private::Stream &s) override;
162 
163 private:
164   struct SecContribInfo {
165     uint32_t Offset;
166     uint32_t Size;
167     uint32_t CompilandId;
168   };
169   using SecContribsMap = std::map<uint32_t, std::vector<SecContribInfo>>;
170 
171   uint32_t CalculateNumCompileUnits() override;
172 
173   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
174 
175   lldb::CompUnitSP ParseCompileUnitForUID(uint32_t id,
176                                           uint32_t index = UINT32_MAX);
177 
178   bool ParseCompileUnitLineTable(lldb_private::CompileUnit &comp_unit,
179                                  uint32_t match_line);
180 
181   void BuildSupportFileIdToSupportFileIndexMap(
182       const llvm::pdb::PDBSymbolCompiland &pdb_compiland,
183       llvm::DenseMap<uint32_t, uint32_t> &index_map) const;
184 
185   void FindTypesByName(llvm::StringRef name,
186                        const lldb_private::CompilerDeclContext *parent_decl_ctx,
187                        uint32_t max_matches, lldb_private::TypeMap &types);
188 
189   std::string GetMangledForPDBData(const llvm::pdb::PDBSymbolData &pdb_data);
190 
191   lldb::VariableSP
192   ParseVariableForPDBData(const lldb_private::SymbolContext &sc,
193                           const llvm::pdb::PDBSymbolData &pdb_data);
194 
195   size_t ParseVariables(const lldb_private::SymbolContext &sc,
196                         const llvm::pdb::PDBSymbol &pdb_data,
197                         lldb_private::VariableList *variable_list = nullptr);
198 
199   lldb::CompUnitSP
200   GetCompileUnitContainsAddress(const lldb_private::Address &so_addr);
201 
202   typedef std::vector<lldb_private::Type *> TypeCollection;
203 
204   void GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
205                             uint32_t type_mask,
206                             TypeCollection &type_collection);
207 
208   lldb_private::Function *
209   ParseCompileUnitFunctionForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func,
210                                      lldb_private::CompileUnit &comp_unit);
211 
212   void GetCompileUnitIndex(const llvm::pdb::PDBSymbolCompiland &pdb_compiland,
213                            uint32_t &index);
214 
215   PDBASTParser *GetPDBAstParser();
216 
217   std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
218   GetPDBCompilandByUID(uint32_t uid);
219 
220   lldb_private::Mangled
221   GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func);
222 
223   bool ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
224                        bool include_inlines,
225                        lldb_private::SymbolContextList &sc_list);
226 
227   bool ResolveFunction(uint32_t uid, bool include_inlines,
228                        lldb_private::SymbolContextList &sc_list);
229 
230   void CacheFunctionNames();
231 
232   bool DeclContextMatchesThisSymbolFile(
233       const lldb_private::CompilerDeclContext *decl_ctx);
234 
235   uint32_t GetCompilandId(const llvm::pdb::PDBSymbolData &data);
236 
237   llvm::DenseMap<uint32_t, lldb::CompUnitSP> m_comp_units;
238   llvm::DenseMap<uint32_t, lldb::TypeSP> m_types;
239   llvm::DenseMap<uint32_t, lldb::VariableSP> m_variables;
240   llvm::DenseMap<uint64_t, std::string> m_public_names;
241 
242   SecContribsMap m_sec_contribs;
243 
244   std::vector<lldb::TypeSP> m_builtin_types;
245   std::unique_ptr<llvm::pdb::IPDBSession> m_session_up;
246   std::unique_ptr<llvm::pdb::PDBSymbolExe> m_global_scope_up;
247 
248   lldb_private::UniqueCStringMap<uint32_t> m_func_full_names;
249   lldb_private::UniqueCStringMap<uint32_t> m_func_base_names;
250   lldb_private::UniqueCStringMap<uint32_t> m_func_method_names;
251 };
252 
253 #endif // lldb_Plugins_SymbolFile_PDB_SymbolFilePDB_h_
254