blob: f298749f10d472576dbad09a63caaee79800f6e6 [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFilePDB.h"
11
Zachary Turner42dff792016-04-15 00:21:26 +000012#include "clang/Lex/Lexer.h"
13
Zachary Turner74e08ca2016-03-02 22:05:52 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/LineTable.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000021#include "lldb/Symbol/SymbolVendor.h"
Aaron Smithec40f812018-01-23 20:35:19 +000022#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000023#include "lldb/Symbol/TypeMap.h"
Aaron Smithcab0d232018-05-23 01:52:42 +000024#include "lldb/Symbol/Variable.h"
Aaron Smith86e94342017-12-22 05:26:50 +000025#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000026
Pavel Labathb8d8c622016-05-09 11:07:43 +000027#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000028#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000029#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
30#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000031#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000033#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000035#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000036#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
37#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000038#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000039#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
41#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
42#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000043#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000044#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
46#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
47
Aaron Smith7ac1c782018-02-09 05:31:28 +000048#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner42dff792016-04-15 00:21:26 +000049#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
50
51#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000052
Aaron Smith10a02572018-01-13 06:58:18 +000053using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000054using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000055using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057namespace {
58lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
59 switch (lang) {
60 case PDB_Lang::Cpp:
61 return lldb::LanguageType::eLanguageTypeC_plus_plus;
62 case PDB_Lang::C:
63 return lldb::LanguageType::eLanguageTypeC;
64 default:
65 return lldb::LanguageType::eLanguageTypeUnknown;
66 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
70 uint32_t addr_length) {
71 return ((requested_line == 0 || actual_line == requested_line) &&
72 addr_length > 0);
73}
Aaron Smithc8316ed2018-03-22 03:44:51 +000074} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076void SymbolFilePDB::Initialize() {
77 PluginManager::RegisterPlugin(GetPluginNameStatic(),
78 GetPluginDescriptionStatic(), CreateInstance,
79 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082void SymbolFilePDB::Terminate() {
83 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
87
88lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
89 static ConstString g_name("pdb");
90 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000091}
92
Kate Stoneb9c1b512016-09-06 20:57:50 +000093const char *SymbolFilePDB::GetPluginDescriptionStatic() {
94 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000095}
96
97lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000098SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
99 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000100}
101
102SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000103 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
104 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000109 uint32_t abilities = 0;
110 if (!m_obj_file)
111 return 0;
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (!m_session_up) {
114 // Lazily load and match the PDB file, but only do this once.
115 std::string exePath = m_obj_file->GetFileSpec().GetPath();
116 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
117 m_session_up);
118 if (error) {
119 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000120 auto module_sp = m_obj_file->GetModule();
121 if (!module_sp)
122 return 0;
123 // See if any symbol file is specified through `--symfile` option.
124 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
125 if (!symfile)
126 return 0;
127 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000128 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000129 if (error) {
130 llvm::consumeError(std::move(error));
131 return 0;
132 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000135 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000136 return 0;
137
138 auto enum_tables_up = m_session_up->getEnumTables();
139 if (!enum_tables_up)
140 return 0;
141 while (auto table_up = enum_tables_up->getNext()) {
142 if (table_up->getItemCount() == 0)
143 continue;
144 auto type = table_up->getTableType();
145 switch (type) {
146 case PDB_TableType::Symbols:
147 // This table represents a store of symbols with types listed in
148 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000149 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
150 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000151 break;
152 case PDB_TableType::LineNumbers:
153 abilities |= LineTables;
154 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000155 default:
156 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000157 }
158 }
159 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162void SymbolFilePDB::InitializeObject() {
163 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000164 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000166 if (!m_global_scope_up)
167 m_global_scope_up = m_session_up->getGlobalScope();
168 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 TypeSystem *type_system =
171 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
172 ClangASTContext *clang_type_system =
173 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000174 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
176 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179uint32_t SymbolFilePDB::GetNumCompileUnits() {
180 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000181 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
182 if (!compilands)
183 return 0;
184
185 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000186 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
187 // found as a child of the global scope (PDB executable). Usually, such
188 // compilands contain `thunk` symbols in which we are not interested for
189 // now. However we still count them in the compiland list. If we perform
190 // any compiland related activity, like finding symbols through
191 // llvm::pdb::IPDBSession methods, such compilands will all be searched
192 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000196 // PDB. Ignore this special compile unit for our purposes, if it is there.
197 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000198 auto last_compiland_up =
199 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
200 lldbassert(last_compiland_up.get());
201 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 if (name == "* Linker *")
203 --m_cached_compile_unit_count;
204 }
205 return m_cached_compile_unit_count;
206}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000207
Aaron Smith10a02572018-01-13 06:58:18 +0000208void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000209 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000210 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
211 if (!results_up)
212 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000213 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000214 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000215 auto compiland_up = results_up->getChildAtIndex(cu_idx);
216 if (!compiland_up)
217 continue;
218 if (compiland_up->getSymIndexId() == uid) {
219 index = cu_idx;
220 return;
221 }
222 }
223 index = UINT32_MAX;
224 return;
225}
226
227std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
228SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
229 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
230}
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000233 if (index >= GetNumCompileUnits())
234 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235
Aaron Smith10a02572018-01-13 06:58:18 +0000236 // Assuming we always retrieve same compilands listed in same order through
237 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
238 // compile unit makes no sense.
239 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
240 if (!results)
241 return CompUnitSP();
242 auto compiland_up = results->getChildAtIndex(index);
243 if (!compiland_up)
244 return CompUnitSP();
245 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000246}
247
248lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
250 // What fields should I expect to be filled out on the SymbolContext? Is it
251 // safe to assume that `sc.comp_unit` is valid?
252 if (!sc.comp_unit)
253 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000254
Aaron Smith10a02572018-01-13 06:58:18 +0000255 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
256 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000258 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 if (!details)
260 return lldb::eLanguageTypeUnknown;
261 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000262}
263
Aaron Smithc8316ed2018-03-22 03:44:51 +0000264lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
265 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000266 lldbassert(sc.comp_unit && sc.module_sp.get());
267
Aaron Smithe664b5d2018-03-19 21:14:19 +0000268 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000269 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000270 return nullptr;
271
Aaron Smithe664b5d2018-03-19 21:14:19 +0000272 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000273 AddressRange func_range =
274 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000275 if (!func_range.GetBaseAddress().IsValid())
276 return nullptr;
277
Aaron Smithc8316ed2018-03-22 03:44:51 +0000278 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000279 if (!func_type)
280 return nullptr;
281
Aaron Smithe664b5d2018-03-19 21:14:19 +0000282 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000283
Aaron Smith7ac1c782018-02-09 05:31:28 +0000284 Mangled mangled = GetMangledForPDBFunc(pdb_func);
285
Aaron Smithc8316ed2018-03-22 03:44:51 +0000286 FunctionSP func_sp =
287 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
288 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000289
290 sc.comp_unit->AddFunction(func_sp);
291 return func_sp.get();
292}
293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294size_t SymbolFilePDB::ParseCompileUnitFunctions(
295 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000296 lldbassert(sc.comp_unit);
297 size_t func_added = 0;
298 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
299 if (!compiland_up)
300 return 0;
301 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
302 if (!results_up)
303 return 0;
304 while (auto pdb_func_up = results_up->getNext()) {
305 auto func_sp =
306 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
307 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000308 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000309 ++func_added;
310 }
311 }
312 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000313}
314
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315bool SymbolFilePDB::ParseCompileUnitLineTable(
316 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000317 lldbassert(sc.comp_unit);
318 if (sc.comp_unit->GetLineTable())
319 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000321}
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323bool SymbolFilePDB::ParseCompileUnitDebugMacros(
324 const lldb_private::SymbolContext &sc) {
325 // PDB doesn't contain information about macros
326 return false;
327}
328
329bool SymbolFilePDB::ParseCompileUnitSupportFiles(
330 const lldb_private::SymbolContext &sc,
331 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000332 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000335 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
336 // second time seems like a waste. Unfortunately, there's no good way around
337 // this short of a moderate refactor since SymbolVendor depends on being able
338 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000339 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
340 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000341 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000342 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000344 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345
346 while (auto file = files->getNext()) {
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000347 FileSpec spec(file->getFileName(), false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000348 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 }
350 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000351}
352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353bool SymbolFilePDB::ParseImportedModules(
354 const lldb_private::SymbolContext &sc,
355 std::vector<lldb_private::ConstString> &imported_modules) {
356 // PDB does not yet support module debug info
357 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000358}
359
Aaron Smithc8316ed2018-03-22 03:44:51 +0000360static size_t ParseFunctionBlocksForPDBSymbol(
361 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
362 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
363 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000364 assert(pdb_symbol && parent_block);
365
366 size_t num_added = 0;
367 switch (pdb_symbol->getSymTag()) {
368 case PDB_SymType::Block:
369 case PDB_SymType::Function: {
370 Block *block = nullptr;
371 auto &raw_sym = pdb_symbol->getRawSymbol();
372 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
373 if (pdb_func->hasNoInlineAttribute())
374 break;
375 if (is_top_parent)
376 block = parent_block;
377 else
378 break;
379 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
380 auto uid = pdb_symbol->getSymIndexId();
381 if (parent_block->FindBlockByID(uid))
382 break;
383 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
384 break;
385
386 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
387 parent_block->AddChild(block_sp);
388 block = block_sp.get();
389 } else
390 llvm_unreachable("Unexpected PDB symbol!");
391
Aaron Smithc8316ed2018-03-22 03:44:51 +0000392 block->AddRange(Block::Range(
393 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000394 block->FinalizeRanges();
395 ++num_added;
396
397 auto results_up = pdb_symbol->findAllChildren();
398 if (!results_up)
399 break;
400 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000401 num_added += ParseFunctionBlocksForPDBSymbol(
402 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000403 }
404 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000405 default:
406 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000407 }
408 return num_added;
409}
410
Zachary Turner74e08ca2016-03-02 22:05:52 +0000411size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000413 lldbassert(sc.comp_unit && sc.function);
414 size_t num_added = 0;
415 auto uid = sc.function->GetID();
416 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
417 if (!pdb_func_up)
418 return 0;
419 Block &parent_block = sc.function->GetBlock(false);
420 num_added =
421 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
422 pdb_func_up.get(), &parent_block, true);
423 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000424}
425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000427 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000428 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000429 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000430
431 size_t num_added = 0;
432 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
433 if (!compiland)
434 return 0;
435
436 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
437 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000438 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
439 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000440 for (auto tag : tags_to_search) {
441 results = raw_sym.findAllChildren(tag);
442 if (!results || results->getChildCount() == 0)
443 continue;
444 while (auto symbol = results->getNext()) {
445 switch (symbol->getSymTag()) {
446 case PDB_SymType::Enum:
447 case PDB_SymType::UDT:
448 case PDB_SymType::Typedef:
449 break;
450 default:
451 continue;
452 }
453
454 // This should cause the type to get cached and stored in the `m_types`
455 // lookup.
456 if (!ResolveTypeUID(symbol->getSymIndexId()))
457 continue;
458
459 ++num_added;
460 }
Aaron Smithec40f812018-01-23 20:35:19 +0000461 }
Aaron Smith66b84072018-03-14 04:05:27 +0000462 };
Aaron Smithec40f812018-01-23 20:35:19 +0000463
Aaron Smith66b84072018-03-14 04:05:27 +0000464 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000465 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
466 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000467 if (!pdb_func)
468 return 0;
469 ParseTypesByTagFn(*pdb_func);
470 } else {
471 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000472
Aaron Smith66b84072018-03-14 04:05:27 +0000473 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000474 // Unfortunately, PDB has no compiland information for each global type. We
475 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000476 static bool parse_all_global_types = false;
477 if (!parse_all_global_types) {
478 ParseTypesByTagFn(*m_global_scope_up);
479 parse_all_global_types = true;
480 }
Aaron Smithec40f812018-01-23 20:35:19 +0000481 }
482 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483}
484
485size_t
486SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000487 if (!sc.comp_unit)
488 return 0;
489
490 size_t num_added = 0;
491 if (sc.function) {
492 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
493 sc.function->GetID());
494 if (!pdb_func)
495 return 0;
496
497 num_added += ParseVariables(sc, *pdb_func);
498 sc.function->GetBlock(false).SetDidParseVariables(true, true);
499 } else if (sc.comp_unit) {
500 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
501 if (!compiland)
502 return 0;
503
504 if (sc.comp_unit->GetVariableList(false))
505 return 0;
506
507 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
508 if (results && results->getChildCount()) {
509 while (auto result = results->getNext()) {
510 auto cu_id = result->getCompilandId();
511 // FIXME: We are not able to determine variable's compile unit.
512 if (cu_id == 0)
513 continue;
514
515 if (cu_id == sc.comp_unit->GetID())
516 num_added += ParseVariables(sc, *result);
517 }
518 }
519
520 // FIXME: A `file static` or `global constant` variable appears both in
521 // compiland's children and global scope's children with unexpectedly
522 // different symbol's Id making it ambiguous.
523
524 // FIXME: 'local constant', for example, const char var[] = "abc", declared
525 // in a function scope, can't be found in PDB.
526
527 // Parse variables in this compiland.
528 num_added += ParseVariables(sc, *compiland);
529 }
530
531 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532}
533
534lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
535 auto find_result = m_types.find(type_uid);
536 if (find_result != m_types.end())
537 return find_result->second.get();
538
539 TypeSystem *type_system =
540 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
541 ClangASTContext *clang_type_system =
542 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
543 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000544 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 PDBASTParser *pdb =
546 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
547 if (!pdb)
548 return nullptr;
549
550 auto pdb_type = m_session_up->getSymbolById(type_uid);
551 if (pdb_type == nullptr)
552 return nullptr;
553
554 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000555 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000556 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000557 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000558 if (type_list)
559 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000560 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000562}
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
565 // TODO: Implement this
566 return false;
567}
568
569lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
570 return lldb_private::CompilerDecl();
571}
572
573lldb_private::CompilerDeclContext
574SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
575 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000576 // improve this later but it's not easy because PDB doesn't provide a high
577 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 return *m_tu_decl_ctx_up;
579}
580
581lldb_private::CompilerDeclContext
582SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
583 return *m_tu_decl_ctx_up;
584}
585
586void SymbolFilePDB::ParseDeclsForContext(
587 lldb_private::CompilerDeclContext decl_ctx) {}
588
589uint32_t
590SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
591 uint32_t resolve_scope,
592 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000593 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000594 if (resolve_scope & eSymbolContextCompUnit ||
595 resolve_scope & eSymbolContextVariable ||
596 resolve_scope & eSymbolContextFunction ||
597 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000598 resolve_scope & eSymbolContextLineEntry) {
599 addr_t file_vm_addr = so_addr.GetFileAddress();
600 auto symbol_up =
601 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
602 if (!symbol_up)
603 return 0;
604
605 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
606 if (!cu_sp) {
607 if (resolved_flags | eSymbolContextVariable) {
608 // TODO: Resolve variables
609 }
610 return 0;
611 }
612 sc.comp_unit = cu_sp.get();
613 resolved_flags |= eSymbolContextCompUnit;
614 lldbassert(sc.module_sp == cu_sp->GetModule());
615
616 switch (symbol_up->getSymTag()) {
617 case PDB_SymType::Function:
618 if (resolve_scope & eSymbolContextFunction) {
619 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
620 assert(pdb_func);
621 auto func_uid = pdb_func->getSymIndexId();
622 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
623 if (sc.function == nullptr)
Aaron Smithe664b5d2018-03-19 21:14:19 +0000624 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000625 if (sc.function) {
626 resolved_flags |= eSymbolContextFunction;
627 if (resolve_scope & eSymbolContextBlock) {
628 Block &block = sc.function->GetBlock(true);
629 sc.block = block.FindBlockByID(sc.function->GetID());
630 if (sc.block)
631 resolved_flags |= eSymbolContextBlock;
632 }
633 }
634 }
635 break;
636 default:
637 break;
638 }
639
640 if (resolve_scope & eSymbolContextLineEntry) {
641 if (auto *line_table = sc.comp_unit->GetLineTable()) {
642 Address addr(so_addr);
643 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
644 resolved_flags |= eSymbolContextLineEntry;
645 }
646 }
647 }
648 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649}
650
651uint32_t SymbolFilePDB::ResolveSymbolContext(
652 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
653 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000654 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 if (resolve_scope & lldb::eSymbolContextCompUnit) {
656 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000657 // file. For example, if `file_spec` is <vector>, then this should return
658 // all source files and header files that reference <vector>, either
659 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 auto compilands = m_session_up->findCompilandsForSourceFile(
661 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
662
Aaron Smith10a02572018-01-13 06:58:18 +0000663 if (!compilands)
664 return 0;
665
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000666 // For each one, either find its previously parsed data or parse it afresh
667 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000669 // If we're not checking inlines, then don't add line information for
670 // this file unless the FileSpec matches. For inline functions, we don't
671 // have to match the FileSpec since they could be defined in headers
672 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000674 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000675 if (source_file.empty())
676 continue;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000677 FileSpec this_spec(source_file, false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000678 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
679 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 continue;
681 }
682
683 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000684 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000685 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000686 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 sc.comp_unit = cu.get();
688 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689
690 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000691 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000692 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
693 eSymbolContextLineEntry)) {
694 bool has_line_table = ParseCompileUnitLineTable(sc, line);
695
696 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
697 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000698 // compile unit. This is not normal for `line` = 0. So just assert
699 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000700 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000701
702 // Current compiland does not have the requested line. Search next.
703 continue;
704 }
705
706 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
707 if (!has_line_table)
708 continue;
709
710 auto *line_table = sc.comp_unit->GetLineTable();
711 lldbassert(line_table);
712
713 uint32_t num_line_entries = line_table->GetSize();
714 // Skip the terminal line entry.
715 --num_line_entries;
716
Adrian Prantl05097242018-04-30 16:49:04 +0000717 // If `line `!= 0, see if we can resolve function for each line entry
718 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000719 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
720 ++line_idx) {
721 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
722 continue;
723
724 auto file_vm_addr =
725 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000726 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000727 continue;
728
Aaron Smithc8316ed2018-03-22 03:44:51 +0000729 auto symbol_up = m_session_up->findSymbolByAddress(
730 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000731 if (symbol_up) {
732 auto func_uid = symbol_up->getSymIndexId();
733 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
734 if (sc.function == nullptr) {
735 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
736 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000737 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000738 }
739 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
740 Block &block = sc.function->GetBlock(true);
741 sc.block = block.FindBlockByID(sc.function->GetID());
742 }
743 }
744 sc_list.Append(sc);
745 }
746 } else if (has_line_table) {
747 // We can parse line table for the compile unit. But no query to
748 // resolve function or block. We append `sc` to the list anyway.
749 sc_list.Append(sc);
750 }
751 } else {
752 // No query for line entry, function or block. But we have a valid
753 // compile unit, append `sc` to the list.
754 sc_list.Append(sc);
755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 }
757 }
Aaron Smith10a02572018-01-13 06:58:18 +0000758 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759}
760
Aaron Smithcab0d232018-05-23 01:52:42 +0000761std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
762 std::string decorated_name;
763 auto vm_addr = pdb_data.getVirtualAddress();
764 if (vm_addr != LLDB_INVALID_ADDRESS && vm_addr) {
765 auto result_up =
766 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol);
767 if (result_up) {
768 while (auto symbol_up = result_up->getNext()) {
769 if (symbol_up->getRawSymbol().getVirtualAddress() == vm_addr) {
770 decorated_name = symbol_up->getRawSymbol().getName();
771 break;
772 }
773 }
774 }
775 }
776 if (!decorated_name.empty())
777 return decorated_name;
778
779 return std::string();
780}
781
782VariableSP SymbolFilePDB::ParseVariableForPDBData(
783 const lldb_private::SymbolContext &sc,
784 const llvm::pdb::PDBSymbolData &pdb_data) {
785 VariableSP var_sp;
786 uint32_t var_uid = pdb_data.getSymIndexId();
787 auto result = m_variables.find(var_uid);
788 if (result != m_variables.end())
789 return result->second;
790
791 ValueType scope = eValueTypeInvalid;
792 bool is_static_member = false;
793 bool is_external = false;
794 bool is_artificial = false;
795
796 switch (pdb_data.getDataKind()) {
797 case PDB_DataKind::Global:
798 scope = eValueTypeVariableGlobal;
799 is_external = true;
800 break;
801 case PDB_DataKind::Local:
802 scope = eValueTypeVariableLocal;
803 break;
804 case PDB_DataKind::FileStatic:
805 scope = eValueTypeVariableStatic;
806 break;
807 case PDB_DataKind::StaticMember:
808 is_static_member = true;
809 scope = eValueTypeVariableStatic;
810 break;
811 case PDB_DataKind::Member:
812 scope = eValueTypeVariableStatic;
813 break;
814 case PDB_DataKind::Param:
815 scope = eValueTypeVariableArgument;
816 break;
817 case PDB_DataKind::Constant:
818 scope = eValueTypeConstResult;
819 break;
820 default:
821 break;
822 }
823
824 switch (pdb_data.getLocationType()) {
825 case PDB_LocType::TLS:
826 scope = eValueTypeVariableThreadLocal;
827 break;
828 case PDB_LocType::RegRel: {
829 // It is a `this` pointer.
830 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
831 scope = eValueTypeVariableArgument;
832 is_artificial = true;
833 }
834 } break;
835 default:
836 break;
837 }
838
839 Declaration decl;
840 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
841 if (auto lines = pdb_data.getLineNumbers()) {
842 if (auto first_line = lines->getNext()) {
843 uint32_t src_file_id = first_line->getSourceFileId();
844 auto src_file = m_session_up->getSourceFileById(src_file_id);
845 if (src_file) {
846 FileSpec spec(src_file->getFileName(), /*resolve_path*/ false);
847 decl.SetFile(spec);
848 decl.SetColumn(first_line->getColumnNumber());
849 decl.SetLine(first_line->getLineNumber());
850 }
851 }
852 }
853 }
854
855 Variable::RangeList ranges;
856 SymbolContextScope *context_scope = sc.comp_unit;
857 if (scope == eValueTypeVariableLocal) {
858 if (sc.function) {
859 context_scope = sc.function->GetBlock(true).FindBlockByID(
860 pdb_data.getClassParentId());
861 if (context_scope == nullptr)
862 context_scope = sc.function;
863 }
864 }
865
866 SymbolFileTypeSP type_sp =
867 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
868
869 auto var_name = pdb_data.getName();
870 auto mangled = GetMangledForPDBData(pdb_data);
871 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
872
873 DWARFExpression location(nullptr);
874
875 var_sp = std::make_shared<Variable>(
876 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
877 ranges, &decl, location, is_external, is_artificial, is_static_member);
878
879 m_variables.insert(std::make_pair(var_uid, var_sp));
880 return var_sp;
881}
882
883size_t
884SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
885 const llvm::pdb::PDBSymbol &pdb_symbol,
886 lldb_private::VariableList *variable_list) {
887 size_t num_added = 0;
888
889 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
890 VariableListSP local_variable_list_sp;
891
892 auto result = m_variables.find(pdb_data->getSymIndexId());
893 if (result != m_variables.end()) {
894 if (variable_list)
895 variable_list->AddVariableIfUnique(result->second);
896 } else {
897 // Prepare right VariableList for this variable.
898 if (auto lexical_parent = pdb_data->getLexicalParent()) {
899 switch (lexical_parent->getSymTag()) {
900 case PDB_SymType::Exe:
901 assert(sc.comp_unit);
902 LLVM_FALLTHROUGH;
903 case PDB_SymType::Compiland: {
904 if (sc.comp_unit) {
905 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
906 if (!local_variable_list_sp) {
907 local_variable_list_sp = std::make_shared<VariableList>();
908 sc.comp_unit->SetVariableList(local_variable_list_sp);
909 }
910 }
911 } break;
912 case PDB_SymType::Block:
913 case PDB_SymType::Function: {
914 if (sc.function) {
915 Block *block = sc.function->GetBlock(true).FindBlockByID(
916 lexical_parent->getSymIndexId());
917 if (block) {
918 local_variable_list_sp = block->GetBlockVariableList(false);
919 if (!local_variable_list_sp) {
920 local_variable_list_sp = std::make_shared<VariableList>();
921 block->SetVariableList(local_variable_list_sp);
922 }
923 }
924 }
925 } break;
926 default:
927 break;
928 }
929 }
930
931 if (local_variable_list_sp) {
932 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
933 local_variable_list_sp->AddVariableIfUnique(var_sp);
934 if (variable_list)
935 variable_list->AddVariableIfUnique(var_sp);
936 ++num_added;
937 }
938 }
939 }
940 }
941
942 if (auto results = pdb_symbol.findAllChildren()) {
943 while (auto result = results->getNext())
944 num_added += ParseVariables(sc, *result, variable_list);
945 }
946
947 return num_added;
948}
949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950uint32_t SymbolFilePDB::FindGlobalVariables(
951 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +0000952 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000954 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
955 return 0;
956 if (name.IsEmpty())
957 return 0;
958
959 auto results =
960 m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(),
961 PDB_NameSearchFlags::NS_CaseSensitive);
962 if (!results)
963 return 0;
964
965 uint32_t matches = 0;
966 size_t old_size = variables.GetSize();
967 while (auto result = results->getNext()) {
968 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
969 if (max_matches > 0 && matches >= max_matches)
970 break;
971
972 SymbolContext sc;
973 sc.module_sp = m_obj_file->GetModule();
974 lldbassert(sc.module_sp.get());
975
976 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
977 // FIXME: We are not able to determine the compile unit.
978 if (sc.comp_unit == nullptr)
979 continue;
980
981 ParseVariables(sc, *pdb_data, &variables);
982 matches = variables.GetSize() - old_size;
983 }
984
985 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986}
987
988uint32_t
989SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +0000990 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000992 if (!regex.IsValid())
993 return 0;
994 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
995 if (!results)
996 return 0;
997
998 uint32_t matches = 0;
999 size_t old_size = variables.GetSize();
1000 while (auto pdb_data = results->getNext()) {
1001 if (max_matches > 0 && matches >= max_matches)
1002 break;
1003
1004 auto var_name = pdb_data->getName();
1005 if (var_name.empty())
1006 continue;
1007 if (!regex.Execute(var_name))
1008 continue;
1009 SymbolContext sc;
1010 sc.module_sp = m_obj_file->GetModule();
1011 lldbassert(sc.module_sp.get());
1012
1013 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
1014 // FIXME: We are not able to determine the compile unit.
1015 if (sc.comp_unit == nullptr)
1016 continue;
1017
1018 ParseVariables(sc, *pdb_data, &variables);
1019 matches = variables.GetSize() - old_size;
1020 }
1021
1022 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023}
1024
Aaron Smithe664b5d2018-03-19 21:14:19 +00001025bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001026 bool include_inlines,
1027 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001028 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001029 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001030 if (!sc.comp_unit)
1031 return false;
1032 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001033 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001034 if (!sc.function)
1035 return false;
1036
1037 sc_list.Append(sc);
1038 return true;
1039}
1040
1041bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1042 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001043 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001044 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1045 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001046 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001047}
1048
1049void SymbolFilePDB::CacheFunctionNames() {
1050 if (!m_func_full_names.IsEmpty())
1051 return;
1052
1053 std::map<uint64_t, uint32_t> addr_ids;
1054
1055 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1056 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001057 if (pdb_func_up->isCompilerGenerated())
1058 continue;
1059
Aaron Smith7ac1c782018-02-09 05:31:28 +00001060 auto name = pdb_func_up->getName();
1061 auto demangled_name = pdb_func_up->getUndecoratedName();
1062 if (name.empty() && demangled_name.empty())
1063 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001064
Aaron Smithf76fe682018-03-07 03:16:50 +00001065 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001066 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1067 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1068
1069 if (auto parent = pdb_func_up->getClassParent()) {
1070
1071 // PDB have symbols for class/struct methods or static methods in Enum
1072 // Class. We won't bother to check if the parent is UDT or Enum here.
1073 m_func_method_names.Append(ConstString(name), uid);
1074
1075 ConstString cstr_name(name);
1076
Adrian Prantl05097242018-04-30 16:49:04 +00001077 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1078 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1079 // not have inforamtion of this, we extract base names and cache them
1080 // by our own effort.
Aaron Smith7ac1c782018-02-09 05:31:28 +00001081 llvm::StringRef basename;
1082 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
1083 if (cpp_method.IsValid()) {
1084 llvm::StringRef context;
1085 basename = cpp_method.GetBasename();
1086 if (basename.empty())
1087 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
1088 context, basename);
1089 }
1090
1091 if (!basename.empty())
1092 m_func_base_names.Append(ConstString(basename), uid);
1093 else {
1094 m_func_base_names.Append(ConstString(name), uid);
1095 }
1096
1097 if (!demangled_name.empty())
1098 m_func_full_names.Append(ConstString(demangled_name), uid);
1099
1100 } else {
1101 // Handle not-method symbols.
1102
1103 // The function name might contain namespace, or its lexical scope. It
1104 // is not safe to get its base name by applying same scheme as we deal
1105 // with the method names.
1106 // FIXME: Remove namespace if function is static in a scope.
1107 m_func_base_names.Append(ConstString(name), uid);
1108
1109 if (name == "main") {
1110 m_func_full_names.Append(ConstString(name), uid);
1111
1112 if (!demangled_name.empty() && name != demangled_name) {
1113 m_func_full_names.Append(ConstString(demangled_name), uid);
1114 m_func_base_names.Append(ConstString(demangled_name), uid);
1115 }
1116 } else if (!demangled_name.empty()) {
1117 m_func_full_names.Append(ConstString(demangled_name), uid);
1118 } else {
1119 m_func_full_names.Append(ConstString(name), uid);
1120 }
1121 }
1122 }
1123 }
1124
1125 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001126 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001127 while (auto pub_sym_up = results_up->getNext()) {
1128 if (!pub_sym_up->isFunction())
1129 continue;
1130 auto name = pub_sym_up->getName();
1131 if (name.empty())
1132 continue;
1133
1134 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001135 auto vm_addr = pub_sym_up->getVirtualAddress();
1136
1137 // PDB public symbol has mangled name for its associated function.
1138 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1139 // Cache mangled name.
1140 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1141 }
1142 }
1143 }
1144 }
1145 // Sort them before value searching is working properly
1146 m_func_full_names.Sort();
1147 m_func_full_names.SizeToFit();
1148 m_func_method_names.Sort();
1149 m_func_method_names.SizeToFit();
1150 m_func_base_names.Sort();
1151 m_func_base_names.SizeToFit();
1152}
1153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154uint32_t SymbolFilePDB::FindFunctions(
1155 const lldb_private::ConstString &name,
1156 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1157 uint32_t name_type_mask, bool include_inlines, bool append,
1158 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001159 if (!append)
1160 sc_list.Clear();
1161 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1162
1163 if (name_type_mask == eFunctionNameTypeNone)
1164 return 0;
1165 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1166 return 0;
1167 if (name.IsEmpty())
1168 return 0;
1169
1170 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001171 if (name_type_mask & eFunctionNameTypeFull ||
1172 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001173 name_type_mask & eFunctionNameTypeMethod) {
1174 CacheFunctionNames();
1175
1176 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001177 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
1178 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001179 std::vector<uint32_t> ids;
1180 if (Names.GetValues(name, ids)) {
1181 for (auto id : ids) {
1182 if (resolved_ids.find(id) == resolved_ids.end()) {
1183 if (ResolveFunction(id, include_inlines, sc_list))
1184 resolved_ids.insert(id);
1185 }
1186 }
1187 }
1188 };
1189 if (name_type_mask & eFunctionNameTypeFull) {
1190 ResolveFn(m_func_full_names);
1191 }
1192 if (name_type_mask & eFunctionNameTypeBase) {
1193 ResolveFn(m_func_base_names);
1194 }
1195 if (name_type_mask & eFunctionNameTypeMethod) {
1196 ResolveFn(m_func_method_names);
1197 }
1198 }
1199 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200}
1201
1202uint32_t
1203SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1204 bool include_inlines, bool append,
1205 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001206 if (!append)
1207 sc_list.Clear();
1208 if (!regex.IsValid())
1209 return 0;
1210
1211 auto old_size = sc_list.GetSize();
1212 CacheFunctionNames();
1213
1214 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001215 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1216 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001217 std::vector<uint32_t> ids;
1218 if (Names.GetValues(regex, ids)) {
1219 for (auto id : ids) {
1220 if (resolved_ids.find(id) == resolved_ids.end())
1221 if (ResolveFunction(id, include_inlines, sc_list))
1222 resolved_ids.insert(id);
1223 }
1224 }
1225 };
1226 ResolveFn(m_func_full_names);
1227 ResolveFn(m_func_base_names);
1228
1229 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230}
1231
1232void SymbolFilePDB::GetMangledNamesForFunction(
1233 const std::string &scope_qualified_name,
1234 std::vector<lldb_private::ConstString> &mangled_names) {}
1235
1236uint32_t SymbolFilePDB::FindTypes(
1237 const lldb_private::SymbolContext &sc,
1238 const lldb_private::ConstString &name,
1239 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1240 uint32_t max_matches,
1241 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1242 lldb_private::TypeMap &types) {
1243 if (!append)
1244 types.Clear();
1245 if (!name)
1246 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001247 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1248 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249
1250 searched_symbol_files.clear();
1251 searched_symbol_files.insert(this);
1252
1253 std::string name_str = name.AsCString();
1254
Aaron Smith86e94342017-12-22 05:26:50 +00001255 // There is an assumption 'name' is not a regex
1256 FindTypesByName(name_str, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 return types.GetSize();
1259}
1260
Aaron Smithc8316ed2018-03-22 03:44:51 +00001261void SymbolFilePDB::FindTypesByRegex(
1262 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1263 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001265 // space as much as possible since this searches EVERYTHING in the PDB,
1266 // manually doing regex comparisons. PDB library isn't optimized for regex
1267 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001269 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1271 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 std::unique_ptr<IPDBEnumSymbols> results;
1273
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 uint32_t matches = 0;
1275
1276 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001277 results = m_global_scope_up->findAllChildren(tag);
1278 if (!results)
1279 continue;
1280
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 while (auto result = results->getNext()) {
1282 if (max_matches > 0 && matches >= max_matches)
1283 break;
1284
1285 std::string type_name;
1286 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1287 type_name = enum_type->getName();
1288 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001289 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 type_name = typedef_type->getName();
1291 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1292 type_name = class_type->getName();
1293 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001294 // We're looking only for types that have names. Skip symbols, as well
1295 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 continue;
1297 }
1298
Aaron Smith86e94342017-12-22 05:26:50 +00001299 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 continue;
1301
1302 // This should cause the type to get cached and stored in the `m_types`
1303 // lookup.
1304 if (!ResolveTypeUID(result->getSymIndexId()))
1305 continue;
1306
1307 auto iter = m_types.find(result->getSymIndexId());
1308 if (iter == m_types.end())
1309 continue;
1310 types.Insert(iter->second);
1311 ++matches;
1312 }
1313 }
1314}
1315
1316void SymbolFilePDB::FindTypesByName(const std::string &name,
1317 uint32_t max_matches,
1318 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001320 if (name.empty())
1321 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001322 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1323 PDB_NameSearchFlags::NS_Default);
1324 if (!results)
1325 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326
1327 uint32_t matches = 0;
1328
1329 while (auto result = results->getNext()) {
1330 if (max_matches > 0 && matches >= max_matches)
1331 break;
1332 switch (result->getSymTag()) {
1333 case PDB_SymType::Enum:
1334 case PDB_SymType::UDT:
1335 case PDB_SymType::Typedef:
1336 break;
1337 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001338 // We're looking only for types that have names. Skip symbols, as well
1339 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 continue;
1341 }
1342
1343 // This should cause the type to get cached and stored in the `m_types`
1344 // lookup.
1345 if (!ResolveTypeUID(result->getSymIndexId()))
1346 continue;
1347
1348 auto iter = m_types.find(result->getSymIndexId());
1349 if (iter == m_types.end())
1350 continue;
1351 types.Insert(iter->second);
1352 ++matches;
1353 }
1354}
1355
1356size_t SymbolFilePDB::FindTypes(
1357 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1358 lldb_private::TypeMap &types) {
1359 return 0;
1360}
1361
Aaron Smithec40f812018-01-23 20:35:19 +00001362lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1363 return m_obj_file->GetModule()->GetTypeList();
1364}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365
Aaron Smithc8316ed2018-03-22 03:44:51 +00001366void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1367 uint32_t type_mask,
1368 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001369 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001370 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001371 case PDB_SymType::ArrayType:
1372 can_parse = ((type_mask & eTypeClassArray) != 0);
1373 break;
1374 case PDB_SymType::BuiltinType:
1375 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1376 break;
1377 case PDB_SymType::Enum:
1378 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1379 break;
1380 case PDB_SymType::Function:
1381 case PDB_SymType::FunctionSig:
1382 can_parse = ((type_mask & eTypeClassFunction) != 0);
1383 break;
1384 case PDB_SymType::PointerType:
1385 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1386 eTypeClassMemberPointer)) != 0);
1387 break;
1388 case PDB_SymType::Typedef:
1389 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1390 break;
1391 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001392 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001393 assert(udt);
1394 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001395 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1396 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001397 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001398 default:
1399 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001400 }
1401
1402 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001403 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001404 auto result =
1405 std::find(type_collection.begin(), type_collection.end(), type);
1406 if (result == type_collection.end())
1407 type_collection.push_back(type);
1408 }
1409 }
1410
Aaron Smithe664b5d2018-03-19 21:14:19 +00001411 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001412 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001413 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001414}
1415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1417 uint32_t type_mask,
1418 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001419 TypeCollection type_collection;
1420 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001421 CompileUnit *cu =
1422 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001423 if (cu) {
1424 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001425 if (!compiland_up)
1426 return 0;
1427 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001428 } else {
1429 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1430 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001431 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001432 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1433 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001434 }
1435 }
1436 }
1437
1438 for (auto type : type_collection) {
1439 type->GetForwardCompilerType();
1440 type_list.Insert(type->shared_from_this());
1441 }
1442 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001443}
1444
1445lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1447 auto type_system =
1448 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1449 if (type_system)
1450 type_system->SetSymbolFile(this);
1451 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001452}
1453
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1455 const lldb_private::SymbolContext &sc,
1456 const lldb_private::ConstString &name,
1457 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1458 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001459}
1460
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1462 static ConstString g_name("pdb");
1463 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001464}
1465
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1467
1468IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1469
1470const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1471 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001472}
1473
Aaron Smithc8316ed2018-03-22 03:44:51 +00001474lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1475 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476 auto found_cu = m_comp_units.find(id);
1477 if (found_cu != m_comp_units.end())
1478 return found_cu->second;
1479
Aaron Smith10a02572018-01-13 06:58:18 +00001480 auto compiland_up = GetPDBCompilandByUID(id);
1481 if (!compiland_up)
1482 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483
1484 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001485 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 if (!details)
1487 lang = lldb::eLanguageTypeC_plus_plus;
1488 else
1489 lang = TranslateLanguage(details->getLanguage());
1490
Aaron Smithf76fe682018-03-07 03:16:50 +00001491 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1492 return CompUnitSP();
1493
Aaron Smith487b0c62018-03-20 00:18:22 +00001494 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001495 if (path.empty())
1496 return CompUnitSP();
1497
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 // Don't support optimized code for now, DebugInfoPDB does not return this
1499 // information.
1500 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001501 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1502 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001503
1504 if (!cu_sp)
1505 return CompUnitSP();
1506
1507 m_comp_units.insert(std::make_pair(id, cu_sp));
1508 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001509 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001510 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001511 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1512 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001513 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001514}
1515
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516bool SymbolFilePDB::ParseCompileUnitLineTable(
1517 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001518 lldbassert(sc.comp_unit);
1519
1520 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1521 if (!compiland_up)
1522 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001523
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001525 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001526 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1527 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001529 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001531
Aaron Smith10a02572018-01-13 06:58:18 +00001532 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001534 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1535 if (!files)
1536 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001537
Adrian Prantl05097242018-04-30 16:49:04 +00001538 // For each source and header file, create a LineSequence for contributions
1539 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 while (auto file = files->getNext()) {
1541 std::unique_ptr<LineSequence> sequence(
1542 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001543 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1544 if (!lines)
1545 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001547
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 uint64_t prev_addr;
1549 uint32_t prev_length;
1550 uint32_t prev_line;
1551 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001552
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 for (int i = 0; i < entry_count; ++i) {
1554 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001555
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556 uint64_t lno = line->getLineNumber();
1557 uint64_t addr = line->getVirtualAddress();
1558 uint32_t length = line->getLength();
1559 uint32_t source_id = line->getSourceFileId();
1560 uint32_t col = line->getColumnNumber();
1561 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001562
Adrian Prantl05097242018-04-30 16:49:04 +00001563 // There was a gap between the current entry and the previous entry if
1564 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001566
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001568 // of the previous entry's address range if the current entry resulted in
1569 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001570 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1571 line_table->AppendLineEntryToSequence(
1572 sequence.get(), prev_addr + prev_length, prev_line, 0,
1573 prev_source_idx, false, false, false, false, true);
1574 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001575
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 if (ShouldAddLine(match_line, lno, length)) {
1577 bool is_statement = line->isStatement();
1578 bool is_prologue = false;
1579 bool is_epilogue = false;
1580 auto func =
1581 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1582 if (func) {
1583 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001584 if (prologue)
1585 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001588 if (epilogue)
1589 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001590 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1593 source_idx, is_statement, false,
1594 is_prologue, is_epilogue, false);
1595 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001596
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 prev_addr = addr;
1598 prev_length = length;
1599 prev_line = lno;
1600 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001601 }
1602
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1604 // The end is always a terminal entry, so insert it regardless.
1605 line_table->AppendLineEntryToSequence(
1606 sequence.get(), prev_addr + prev_length, prev_line, 0,
1607 prev_source_idx, false, false, false, false, true);
1608 }
1609
1610 line_table->InsertSequence(sequence.release());
1611 }
1612
Aaron Smith10a02572018-01-13 06:58:18 +00001613 if (line_table->GetSize()) {
1614 sc.comp_unit->SetLineTable(line_table.release());
1615 return true;
1616 }
1617 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001618}
1619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001621 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001623 // This is a hack, but we need to convert the source id into an index into
1624 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001625 // basename / full path issues that may or may not even be a problem, so we
1626 // use the globally unique source file identifiers. Ideally we could use the
1627 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001628 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1629 if (!source_files)
1630 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001632
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 while (auto file = source_files->getNext()) {
1634 uint32_t source_id = file->getUniqueId();
1635 index_map[source_id] = index++;
1636 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001637}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001638
1639lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001640 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001641 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001642 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001643 return nullptr;
1644
Aaron Smith308e39c2018-03-22 19:26:33 +00001645 // If it is a PDB function's vm addr, this is the first sure bet.
1646 if (auto lines =
1647 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1648 if (auto first_line = lines->getNext())
1649 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001650 }
1651
Aaron Smith308e39c2018-03-22 19:26:33 +00001652 // Otherwise we resort to section contributions.
1653 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1654 while (auto section = sec_contribs->getNext()) {
1655 auto va = section->getVirtualAddress();
1656 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1657 return ParseCompileUnitForUID(section->getCompilandId());
1658 }
1659 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001660 return nullptr;
1661}
1662
1663Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001664SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001665 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001666 auto func_name = pdb_func.getName();
1667 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001668 std::string func_decorated_name;
1669
1670 // Seek from public symbols for non-static function's decorated name if any.
1671 // For static functions, they don't have undecorated names and aren't exposed
1672 // in Public Symbols either.
1673 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001674 auto result_up = m_global_scope_up->findChildren(
1675 PDB_SymType::PublicSymbol, func_undecorated_name,
1676 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001677 if (result_up) {
1678 while (auto symbol_up = result_up->getNext()) {
1679 // For a public symbol, it is unique.
1680 lldbassert(result_up->getChildCount() == 1);
1681 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001682 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1683 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001684 if (pdb_public_sym->isFunction()) {
1685 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001686 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001687 }
1688 }
1689 }
1690 }
1691 }
1692 if (!func_decorated_name.empty()) {
1693 mangled.SetMangledName(ConstString(func_decorated_name));
1694
1695 // For MSVC, format of C funciton's decorated name depends on calling
1696 // conventon. Unfortunately none of the format is recognized by current
1697 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001698 // `__purecall` is retrieved as both its decorated and undecorated name
1699 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1700 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1701 // Mangled::GetDemangledName method will fail internally and caches an
1702 // empty string as its undecorated name. So we will face a contradition
1703 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001704 // non-empty undecorated name from PDB
1705 // empty undecorated name from LLDB
1706 if (!func_undecorated_name.empty() &&
1707 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1708 mangled.SetDemangledName(ConstString(func_undecorated_name));
1709
1710 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001711 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1712 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001713 // PDB source unless we also apply same flags in getting undecorated
1714 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1715 if (!func_undecorated_name.empty() &&
1716 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1717 ConstString(func_undecorated_name))
1718 mangled.SetDemangledName(ConstString(func_undecorated_name));
1719 } else if (!func_undecorated_name.empty()) {
1720 mangled.SetDemangledName(ConstString(func_undecorated_name));
1721 } else if (!func_name.empty())
1722 mangled.SetValue(ConstString(func_name), false);
1723
1724 return mangled;
1725}
1726
1727bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1728 const lldb_private::CompilerDeclContext *decl_ctx) {
1729 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1730 return true;
1731
1732 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1733 if (!decl_ctx_type_system)
1734 return false;
1735 TypeSystem *type_system = GetTypeSystemForLanguage(
1736 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1737 if (decl_ctx_type_system == type_system)
1738 return true; // The type systems match, return true
1739
1740 return false;
1741}