blob: e490433fac00c794403671c4840ee00dc5a8977c [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,
952 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
953 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000954 if (!append)
955 variables.Clear();
956 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
957 return 0;
958 if (name.IsEmpty())
959 return 0;
960
961 auto results =
962 m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(),
963 PDB_NameSearchFlags::NS_CaseSensitive);
964 if (!results)
965 return 0;
966
967 uint32_t matches = 0;
968 size_t old_size = variables.GetSize();
969 while (auto result = results->getNext()) {
970 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
971 if (max_matches > 0 && matches >= max_matches)
972 break;
973
974 SymbolContext sc;
975 sc.module_sp = m_obj_file->GetModule();
976 lldbassert(sc.module_sp.get());
977
978 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
979 // FIXME: We are not able to determine the compile unit.
980 if (sc.comp_unit == nullptr)
981 continue;
982
983 ParseVariables(sc, *pdb_data, &variables);
984 matches = variables.GetSize() - old_size;
985 }
986
987 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988}
989
990uint32_t
991SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
992 bool append, uint32_t max_matches,
993 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000994 if (!regex.IsValid())
995 return 0;
996 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
997 if (!results)
998 return 0;
999
1000 uint32_t matches = 0;
1001 size_t old_size = variables.GetSize();
1002 while (auto pdb_data = results->getNext()) {
1003 if (max_matches > 0 && matches >= max_matches)
1004 break;
1005
1006 auto var_name = pdb_data->getName();
1007 if (var_name.empty())
1008 continue;
1009 if (!regex.Execute(var_name))
1010 continue;
1011 SymbolContext sc;
1012 sc.module_sp = m_obj_file->GetModule();
1013 lldbassert(sc.module_sp.get());
1014
1015 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
1016 // FIXME: We are not able to determine the compile unit.
1017 if (sc.comp_unit == nullptr)
1018 continue;
1019
1020 ParseVariables(sc, *pdb_data, &variables);
1021 matches = variables.GetSize() - old_size;
1022 }
1023
1024 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025}
1026
Aaron Smithe664b5d2018-03-19 21:14:19 +00001027bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001028 bool include_inlines,
1029 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001030 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001031 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001032 if (!sc.comp_unit)
1033 return false;
1034 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001035 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001036 if (!sc.function)
1037 return false;
1038
1039 sc_list.Append(sc);
1040 return true;
1041}
1042
1043bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1044 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001045 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001046 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1047 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001048 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001049}
1050
1051void SymbolFilePDB::CacheFunctionNames() {
1052 if (!m_func_full_names.IsEmpty())
1053 return;
1054
1055 std::map<uint64_t, uint32_t> addr_ids;
1056
1057 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1058 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001059 if (pdb_func_up->isCompilerGenerated())
1060 continue;
1061
Aaron Smith7ac1c782018-02-09 05:31:28 +00001062 auto name = pdb_func_up->getName();
1063 auto demangled_name = pdb_func_up->getUndecoratedName();
1064 if (name.empty() && demangled_name.empty())
1065 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001066
Aaron Smithf76fe682018-03-07 03:16:50 +00001067 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001068 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1069 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1070
1071 if (auto parent = pdb_func_up->getClassParent()) {
1072
1073 // PDB have symbols for class/struct methods or static methods in Enum
1074 // Class. We won't bother to check if the parent is UDT or Enum here.
1075 m_func_method_names.Append(ConstString(name), uid);
1076
1077 ConstString cstr_name(name);
1078
Adrian Prantl05097242018-04-30 16:49:04 +00001079 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1080 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1081 // not have inforamtion of this, we extract base names and cache them
1082 // by our own effort.
Aaron Smith7ac1c782018-02-09 05:31:28 +00001083 llvm::StringRef basename;
1084 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
1085 if (cpp_method.IsValid()) {
1086 llvm::StringRef context;
1087 basename = cpp_method.GetBasename();
1088 if (basename.empty())
1089 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
1090 context, basename);
1091 }
1092
1093 if (!basename.empty())
1094 m_func_base_names.Append(ConstString(basename), uid);
1095 else {
1096 m_func_base_names.Append(ConstString(name), uid);
1097 }
1098
1099 if (!demangled_name.empty())
1100 m_func_full_names.Append(ConstString(demangled_name), uid);
1101
1102 } else {
1103 // Handle not-method symbols.
1104
1105 // The function name might contain namespace, or its lexical scope. It
1106 // is not safe to get its base name by applying same scheme as we deal
1107 // with the method names.
1108 // FIXME: Remove namespace if function is static in a scope.
1109 m_func_base_names.Append(ConstString(name), uid);
1110
1111 if (name == "main") {
1112 m_func_full_names.Append(ConstString(name), uid);
1113
1114 if (!demangled_name.empty() && name != demangled_name) {
1115 m_func_full_names.Append(ConstString(demangled_name), uid);
1116 m_func_base_names.Append(ConstString(demangled_name), uid);
1117 }
1118 } else if (!demangled_name.empty()) {
1119 m_func_full_names.Append(ConstString(demangled_name), uid);
1120 } else {
1121 m_func_full_names.Append(ConstString(name), uid);
1122 }
1123 }
1124 }
1125 }
1126
1127 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001128 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001129 while (auto pub_sym_up = results_up->getNext()) {
1130 if (!pub_sym_up->isFunction())
1131 continue;
1132 auto name = pub_sym_up->getName();
1133 if (name.empty())
1134 continue;
1135
1136 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001137 auto vm_addr = pub_sym_up->getVirtualAddress();
1138
1139 // PDB public symbol has mangled name for its associated function.
1140 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1141 // Cache mangled name.
1142 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1143 }
1144 }
1145 }
1146 }
1147 // Sort them before value searching is working properly
1148 m_func_full_names.Sort();
1149 m_func_full_names.SizeToFit();
1150 m_func_method_names.Sort();
1151 m_func_method_names.SizeToFit();
1152 m_func_base_names.Sort();
1153 m_func_base_names.SizeToFit();
1154}
1155
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156uint32_t SymbolFilePDB::FindFunctions(
1157 const lldb_private::ConstString &name,
1158 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1159 uint32_t name_type_mask, bool include_inlines, bool append,
1160 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001161 if (!append)
1162 sc_list.Clear();
1163 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1164
1165 if (name_type_mask == eFunctionNameTypeNone)
1166 return 0;
1167 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1168 return 0;
1169 if (name.IsEmpty())
1170 return 0;
1171
1172 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001173 if (name_type_mask & eFunctionNameTypeFull ||
1174 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001175 name_type_mask & eFunctionNameTypeMethod) {
1176 CacheFunctionNames();
1177
1178 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001179 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
1180 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001181 std::vector<uint32_t> ids;
1182 if (Names.GetValues(name, ids)) {
1183 for (auto id : ids) {
1184 if (resolved_ids.find(id) == resolved_ids.end()) {
1185 if (ResolveFunction(id, include_inlines, sc_list))
1186 resolved_ids.insert(id);
1187 }
1188 }
1189 }
1190 };
1191 if (name_type_mask & eFunctionNameTypeFull) {
1192 ResolveFn(m_func_full_names);
1193 }
1194 if (name_type_mask & eFunctionNameTypeBase) {
1195 ResolveFn(m_func_base_names);
1196 }
1197 if (name_type_mask & eFunctionNameTypeMethod) {
1198 ResolveFn(m_func_method_names);
1199 }
1200 }
1201 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202}
1203
1204uint32_t
1205SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1206 bool include_inlines, bool append,
1207 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001208 if (!append)
1209 sc_list.Clear();
1210 if (!regex.IsValid())
1211 return 0;
1212
1213 auto old_size = sc_list.GetSize();
1214 CacheFunctionNames();
1215
1216 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001217 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1218 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001219 std::vector<uint32_t> ids;
1220 if (Names.GetValues(regex, ids)) {
1221 for (auto id : ids) {
1222 if (resolved_ids.find(id) == resolved_ids.end())
1223 if (ResolveFunction(id, include_inlines, sc_list))
1224 resolved_ids.insert(id);
1225 }
1226 }
1227 };
1228 ResolveFn(m_func_full_names);
1229 ResolveFn(m_func_base_names);
1230
1231 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232}
1233
1234void SymbolFilePDB::GetMangledNamesForFunction(
1235 const std::string &scope_qualified_name,
1236 std::vector<lldb_private::ConstString> &mangled_names) {}
1237
1238uint32_t SymbolFilePDB::FindTypes(
1239 const lldb_private::SymbolContext &sc,
1240 const lldb_private::ConstString &name,
1241 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1242 uint32_t max_matches,
1243 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1244 lldb_private::TypeMap &types) {
1245 if (!append)
1246 types.Clear();
1247 if (!name)
1248 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001249 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1250 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251
1252 searched_symbol_files.clear();
1253 searched_symbol_files.insert(this);
1254
1255 std::string name_str = name.AsCString();
1256
Aaron Smith86e94342017-12-22 05:26:50 +00001257 // There is an assumption 'name' is not a regex
1258 FindTypesByName(name_str, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001259
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 return types.GetSize();
1261}
1262
Aaron Smithc8316ed2018-03-22 03:44:51 +00001263void SymbolFilePDB::FindTypesByRegex(
1264 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1265 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001267 // space as much as possible since this searches EVERYTHING in the PDB,
1268 // manually doing regex comparisons. PDB library isn't optimized for regex
1269 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001271 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1273 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 std::unique_ptr<IPDBEnumSymbols> results;
1275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 uint32_t matches = 0;
1277
1278 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001279 results = m_global_scope_up->findAllChildren(tag);
1280 if (!results)
1281 continue;
1282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 while (auto result = results->getNext()) {
1284 if (max_matches > 0 && matches >= max_matches)
1285 break;
1286
1287 std::string type_name;
1288 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1289 type_name = enum_type->getName();
1290 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001291 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 type_name = typedef_type->getName();
1293 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1294 type_name = class_type->getName();
1295 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001296 // We're looking only for types that have names. Skip symbols, as well
1297 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 continue;
1299 }
1300
Aaron Smith86e94342017-12-22 05:26:50 +00001301 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 continue;
1303
1304 // This should cause the type to get cached and stored in the `m_types`
1305 // lookup.
1306 if (!ResolveTypeUID(result->getSymIndexId()))
1307 continue;
1308
1309 auto iter = m_types.find(result->getSymIndexId());
1310 if (iter == m_types.end())
1311 continue;
1312 types.Insert(iter->second);
1313 ++matches;
1314 }
1315 }
1316}
1317
1318void SymbolFilePDB::FindTypesByName(const std::string &name,
1319 uint32_t max_matches,
1320 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001322 if (name.empty())
1323 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001324 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1325 PDB_NameSearchFlags::NS_Default);
1326 if (!results)
1327 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328
1329 uint32_t matches = 0;
1330
1331 while (auto result = results->getNext()) {
1332 if (max_matches > 0 && matches >= max_matches)
1333 break;
1334 switch (result->getSymTag()) {
1335 case PDB_SymType::Enum:
1336 case PDB_SymType::UDT:
1337 case PDB_SymType::Typedef:
1338 break;
1339 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001340 // We're looking only for types that have names. Skip symbols, as well
1341 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 continue;
1343 }
1344
1345 // This should cause the type to get cached and stored in the `m_types`
1346 // lookup.
1347 if (!ResolveTypeUID(result->getSymIndexId()))
1348 continue;
1349
1350 auto iter = m_types.find(result->getSymIndexId());
1351 if (iter == m_types.end())
1352 continue;
1353 types.Insert(iter->second);
1354 ++matches;
1355 }
1356}
1357
1358size_t SymbolFilePDB::FindTypes(
1359 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1360 lldb_private::TypeMap &types) {
1361 return 0;
1362}
1363
Aaron Smithec40f812018-01-23 20:35:19 +00001364lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1365 return m_obj_file->GetModule()->GetTypeList();
1366}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367
Aaron Smithc8316ed2018-03-22 03:44:51 +00001368void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1369 uint32_t type_mask,
1370 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001371 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001372 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001373 case PDB_SymType::ArrayType:
1374 can_parse = ((type_mask & eTypeClassArray) != 0);
1375 break;
1376 case PDB_SymType::BuiltinType:
1377 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1378 break;
1379 case PDB_SymType::Enum:
1380 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1381 break;
1382 case PDB_SymType::Function:
1383 case PDB_SymType::FunctionSig:
1384 can_parse = ((type_mask & eTypeClassFunction) != 0);
1385 break;
1386 case PDB_SymType::PointerType:
1387 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1388 eTypeClassMemberPointer)) != 0);
1389 break;
1390 case PDB_SymType::Typedef:
1391 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1392 break;
1393 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001394 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001395 assert(udt);
1396 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001397 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1398 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001399 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001400 default:
1401 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001402 }
1403
1404 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001405 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001406 auto result =
1407 std::find(type_collection.begin(), type_collection.end(), type);
1408 if (result == type_collection.end())
1409 type_collection.push_back(type);
1410 }
1411 }
1412
Aaron Smithe664b5d2018-03-19 21:14:19 +00001413 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001414 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001415 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001416}
1417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1419 uint32_t type_mask,
1420 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001421 TypeCollection type_collection;
1422 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001423 CompileUnit *cu =
1424 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001425 if (cu) {
1426 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001427 if (!compiland_up)
1428 return 0;
1429 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001430 } else {
1431 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1432 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001433 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001434 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1435 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001436 }
1437 }
1438 }
1439
1440 for (auto type : type_collection) {
1441 type->GetForwardCompilerType();
1442 type_list.Insert(type->shared_from_this());
1443 }
1444 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001445}
1446
1447lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1449 auto type_system =
1450 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1451 if (type_system)
1452 type_system->SetSymbolFile(this);
1453 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001454}
1455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1457 const lldb_private::SymbolContext &sc,
1458 const lldb_private::ConstString &name,
1459 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1460 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001461}
1462
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1464 static ConstString g_name("pdb");
1465 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001466}
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1469
1470IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1471
1472const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1473 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001474}
1475
Aaron Smithc8316ed2018-03-22 03:44:51 +00001476lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1477 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001478 auto found_cu = m_comp_units.find(id);
1479 if (found_cu != m_comp_units.end())
1480 return found_cu->second;
1481
Aaron Smith10a02572018-01-13 06:58:18 +00001482 auto compiland_up = GetPDBCompilandByUID(id);
1483 if (!compiland_up)
1484 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485
1486 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001487 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 if (!details)
1489 lang = lldb::eLanguageTypeC_plus_plus;
1490 else
1491 lang = TranslateLanguage(details->getLanguage());
1492
Aaron Smithf76fe682018-03-07 03:16:50 +00001493 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1494 return CompUnitSP();
1495
Aaron Smith487b0c62018-03-20 00:18:22 +00001496 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001497 if (path.empty())
1498 return CompUnitSP();
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 // Don't support optimized code for now, DebugInfoPDB does not return this
1501 // information.
1502 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001503 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1504 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001505
1506 if (!cu_sp)
1507 return CompUnitSP();
1508
1509 m_comp_units.insert(std::make_pair(id, cu_sp));
1510 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001511 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001512 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001513 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1514 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001515 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001516}
1517
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518bool SymbolFilePDB::ParseCompileUnitLineTable(
1519 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001520 lldbassert(sc.comp_unit);
1521
1522 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1523 if (!compiland_up)
1524 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001527 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001528 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1529 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001531 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001533
Aaron Smith10a02572018-01-13 06:58:18 +00001534 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001536 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1537 if (!files)
1538 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001539
Adrian Prantl05097242018-04-30 16:49:04 +00001540 // For each source and header file, create a LineSequence for contributions
1541 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 while (auto file = files->getNext()) {
1543 std::unique_ptr<LineSequence> sequence(
1544 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001545 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1546 if (!lines)
1547 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001549
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 uint64_t prev_addr;
1551 uint32_t prev_length;
1552 uint32_t prev_line;
1553 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001554
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 for (int i = 0; i < entry_count; ++i) {
1556 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 uint64_t lno = line->getLineNumber();
1559 uint64_t addr = line->getVirtualAddress();
1560 uint32_t length = line->getLength();
1561 uint32_t source_id = line->getSourceFileId();
1562 uint32_t col = line->getColumnNumber();
1563 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001564
Adrian Prantl05097242018-04-30 16:49:04 +00001565 // There was a gap between the current entry and the previous entry if
1566 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001570 // of the previous entry's address range if the current entry resulted in
1571 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1573 line_table->AppendLineEntryToSequence(
1574 sequence.get(), prev_addr + prev_length, prev_line, 0,
1575 prev_source_idx, false, false, false, false, true);
1576 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001577
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 if (ShouldAddLine(match_line, lno, length)) {
1579 bool is_statement = line->isStatement();
1580 bool is_prologue = false;
1581 bool is_epilogue = false;
1582 auto func =
1583 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1584 if (func) {
1585 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001586 if (prologue)
1587 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001588
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001590 if (epilogue)
1591 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001592 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001593
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1595 source_idx, is_statement, false,
1596 is_prologue, is_epilogue, false);
1597 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001598
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 prev_addr = addr;
1600 prev_length = length;
1601 prev_line = lno;
1602 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001603 }
1604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1606 // The end is always a terminal entry, so insert it regardless.
1607 line_table->AppendLineEntryToSequence(
1608 sequence.get(), prev_addr + prev_length, prev_line, 0,
1609 prev_source_idx, false, false, false, false, true);
1610 }
1611
1612 line_table->InsertSequence(sequence.release());
1613 }
1614
Aaron Smith10a02572018-01-13 06:58:18 +00001615 if (line_table->GetSize()) {
1616 sc.comp_unit->SetLineTable(line_table.release());
1617 return true;
1618 }
1619 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001620}
1621
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001623 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001625 // This is a hack, but we need to convert the source id into an index into
1626 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001627 // basename / full path issues that may or may not even be a problem, so we
1628 // use the globally unique source file identifiers. Ideally we could use the
1629 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001630 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1631 if (!source_files)
1632 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001634
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 while (auto file = source_files->getNext()) {
1636 uint32_t source_id = file->getUniqueId();
1637 index_map[source_id] = index++;
1638 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001639}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001640
1641lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001642 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001643 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001644 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001645 return nullptr;
1646
Aaron Smith308e39c2018-03-22 19:26:33 +00001647 // If it is a PDB function's vm addr, this is the first sure bet.
1648 if (auto lines =
1649 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1650 if (auto first_line = lines->getNext())
1651 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001652 }
1653
Aaron Smith308e39c2018-03-22 19:26:33 +00001654 // Otherwise we resort to section contributions.
1655 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1656 while (auto section = sec_contribs->getNext()) {
1657 auto va = section->getVirtualAddress();
1658 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1659 return ParseCompileUnitForUID(section->getCompilandId());
1660 }
1661 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001662 return nullptr;
1663}
1664
1665Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001666SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001667 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001668 auto func_name = pdb_func.getName();
1669 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001670 std::string func_decorated_name;
1671
1672 // Seek from public symbols for non-static function's decorated name if any.
1673 // For static functions, they don't have undecorated names and aren't exposed
1674 // in Public Symbols either.
1675 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001676 auto result_up = m_global_scope_up->findChildren(
1677 PDB_SymType::PublicSymbol, func_undecorated_name,
1678 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001679 if (result_up) {
1680 while (auto symbol_up = result_up->getNext()) {
1681 // For a public symbol, it is unique.
1682 lldbassert(result_up->getChildCount() == 1);
1683 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001684 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1685 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001686 if (pdb_public_sym->isFunction()) {
1687 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001688 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001689 }
1690 }
1691 }
1692 }
1693 }
1694 if (!func_decorated_name.empty()) {
1695 mangled.SetMangledName(ConstString(func_decorated_name));
1696
1697 // For MSVC, format of C funciton's decorated name depends on calling
1698 // conventon. Unfortunately none of the format is recognized by current
1699 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001700 // `__purecall` is retrieved as both its decorated and undecorated name
1701 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1702 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1703 // Mangled::GetDemangledName method will fail internally and caches an
1704 // empty string as its undecorated name. So we will face a contradition
1705 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001706 // non-empty undecorated name from PDB
1707 // empty undecorated name from LLDB
1708 if (!func_undecorated_name.empty() &&
1709 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1710 mangled.SetDemangledName(ConstString(func_undecorated_name));
1711
1712 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001713 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1714 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001715 // PDB source unless we also apply same flags in getting undecorated
1716 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1717 if (!func_undecorated_name.empty() &&
1718 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1719 ConstString(func_undecorated_name))
1720 mangled.SetDemangledName(ConstString(func_undecorated_name));
1721 } else if (!func_undecorated_name.empty()) {
1722 mangled.SetDemangledName(ConstString(func_undecorated_name));
1723 } else if (!func_name.empty())
1724 mangled.SetValue(ConstString(func_name), false);
1725
1726 return mangled;
1727}
1728
1729bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1730 const lldb_private::CompilerDeclContext *decl_ctx) {
1731 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1732 return true;
1733
1734 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1735 if (!decl_ctx_type_system)
1736 return false;
1737 TypeSystem *type_system = GetTypeSystemForLanguage(
1738 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1739 if (decl_ctx_type_system == type_system)
1740 return true; // The type systems match, return true
1741
1742 return false;
1743}