blob: 53a5488702dbce4c3504a236b21674819c216c78 [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 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000350
351 // LLDB uses the DWARF-like file numeration (one based),
352 // the zeroth file is the compile unit itself
353 support_files.Insert(0, *sc.comp_unit);
354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000356}
357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358bool SymbolFilePDB::ParseImportedModules(
359 const lldb_private::SymbolContext &sc,
360 std::vector<lldb_private::ConstString> &imported_modules) {
361 // PDB does not yet support module debug info
362 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000363}
364
Aaron Smithc8316ed2018-03-22 03:44:51 +0000365static size_t ParseFunctionBlocksForPDBSymbol(
366 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
367 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
368 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000369 assert(pdb_symbol && parent_block);
370
371 size_t num_added = 0;
372 switch (pdb_symbol->getSymTag()) {
373 case PDB_SymType::Block:
374 case PDB_SymType::Function: {
375 Block *block = nullptr;
376 auto &raw_sym = pdb_symbol->getRawSymbol();
377 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
378 if (pdb_func->hasNoInlineAttribute())
379 break;
380 if (is_top_parent)
381 block = parent_block;
382 else
383 break;
384 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
385 auto uid = pdb_symbol->getSymIndexId();
386 if (parent_block->FindBlockByID(uid))
387 break;
388 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
389 break;
390
391 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
392 parent_block->AddChild(block_sp);
393 block = block_sp.get();
394 } else
395 llvm_unreachable("Unexpected PDB symbol!");
396
Aaron Smithc8316ed2018-03-22 03:44:51 +0000397 block->AddRange(Block::Range(
398 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000399 block->FinalizeRanges();
400 ++num_added;
401
402 auto results_up = pdb_symbol->findAllChildren();
403 if (!results_up)
404 break;
405 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000406 num_added += ParseFunctionBlocksForPDBSymbol(
407 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000408 }
409 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000410 default:
411 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000412 }
413 return num_added;
414}
415
Zachary Turner74e08ca2016-03-02 22:05:52 +0000416size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000418 lldbassert(sc.comp_unit && sc.function);
419 size_t num_added = 0;
420 auto uid = sc.function->GetID();
421 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
422 if (!pdb_func_up)
423 return 0;
424 Block &parent_block = sc.function->GetBlock(false);
425 num_added =
426 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
427 pdb_func_up.get(), &parent_block, true);
428 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000429}
430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000432 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000433 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000434 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000435
436 size_t num_added = 0;
437 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
438 if (!compiland)
439 return 0;
440
441 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
442 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000443 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
444 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000445 for (auto tag : tags_to_search) {
446 results = raw_sym.findAllChildren(tag);
447 if (!results || results->getChildCount() == 0)
448 continue;
449 while (auto symbol = results->getNext()) {
450 switch (symbol->getSymTag()) {
451 case PDB_SymType::Enum:
452 case PDB_SymType::UDT:
453 case PDB_SymType::Typedef:
454 break;
455 default:
456 continue;
457 }
458
459 // This should cause the type to get cached and stored in the `m_types`
460 // lookup.
461 if (!ResolveTypeUID(symbol->getSymIndexId()))
462 continue;
463
464 ++num_added;
465 }
Aaron Smithec40f812018-01-23 20:35:19 +0000466 }
Aaron Smith66b84072018-03-14 04:05:27 +0000467 };
Aaron Smithec40f812018-01-23 20:35:19 +0000468
Aaron Smith66b84072018-03-14 04:05:27 +0000469 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000470 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
471 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000472 if (!pdb_func)
473 return 0;
474 ParseTypesByTagFn(*pdb_func);
475 } else {
476 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000477
Aaron Smith66b84072018-03-14 04:05:27 +0000478 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000479 // Unfortunately, PDB has no compiland information for each global type. We
480 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000481 static bool parse_all_global_types = false;
482 if (!parse_all_global_types) {
483 ParseTypesByTagFn(*m_global_scope_up);
484 parse_all_global_types = true;
485 }
Aaron Smithec40f812018-01-23 20:35:19 +0000486 }
487 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488}
489
490size_t
491SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000492 if (!sc.comp_unit)
493 return 0;
494
495 size_t num_added = 0;
496 if (sc.function) {
497 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
498 sc.function->GetID());
499 if (!pdb_func)
500 return 0;
501
502 num_added += ParseVariables(sc, *pdb_func);
503 sc.function->GetBlock(false).SetDidParseVariables(true, true);
504 } else if (sc.comp_unit) {
505 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
506 if (!compiland)
507 return 0;
508
509 if (sc.comp_unit->GetVariableList(false))
510 return 0;
511
512 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
513 if (results && results->getChildCount()) {
514 while (auto result = results->getNext()) {
515 auto cu_id = result->getCompilandId();
516 // FIXME: We are not able to determine variable's compile unit.
517 if (cu_id == 0)
518 continue;
519
520 if (cu_id == sc.comp_unit->GetID())
521 num_added += ParseVariables(sc, *result);
522 }
523 }
524
525 // FIXME: A `file static` or `global constant` variable appears both in
526 // compiland's children and global scope's children with unexpectedly
527 // different symbol's Id making it ambiguous.
528
529 // FIXME: 'local constant', for example, const char var[] = "abc", declared
530 // in a function scope, can't be found in PDB.
531
532 // Parse variables in this compiland.
533 num_added += ParseVariables(sc, *compiland);
534 }
535
536 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537}
538
539lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
540 auto find_result = m_types.find(type_uid);
541 if (find_result != m_types.end())
542 return find_result->second.get();
543
544 TypeSystem *type_system =
545 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
546 ClangASTContext *clang_type_system =
547 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
548 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000549 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 PDBASTParser *pdb =
551 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
552 if (!pdb)
553 return nullptr;
554
555 auto pdb_type = m_session_up->getSymbolById(type_uid);
556 if (pdb_type == nullptr)
557 return nullptr;
558
559 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000560 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000561 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000562 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000563 if (type_list)
564 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000567}
568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
570 // TODO: Implement this
571 return false;
572}
573
574lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
575 return lldb_private::CompilerDecl();
576}
577
578lldb_private::CompilerDeclContext
579SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
580 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000581 // improve this later but it's not easy because PDB doesn't provide a high
582 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 return *m_tu_decl_ctx_up;
584}
585
586lldb_private::CompilerDeclContext
587SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
588 return *m_tu_decl_ctx_up;
589}
590
591void SymbolFilePDB::ParseDeclsForContext(
592 lldb_private::CompilerDeclContext decl_ctx) {}
593
594uint32_t
595SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
596 uint32_t resolve_scope,
597 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000598 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000599 if (resolve_scope & eSymbolContextCompUnit ||
600 resolve_scope & eSymbolContextVariable ||
601 resolve_scope & eSymbolContextFunction ||
602 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000603 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000604 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
605 if (!cu_sp) {
606 if (resolved_flags | eSymbolContextVariable) {
607 // TODO: Resolve variables
608 }
609 return 0;
610 }
611 sc.comp_unit = cu_sp.get();
612 resolved_flags |= eSymbolContextCompUnit;
613 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000614 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000615
Pavel Labath9ea80d22018-06-28 10:03:42 +0000616 if (resolve_scope & eSymbolContextFunction) {
617 addr_t file_vm_addr = so_addr.GetFileAddress();
618 auto symbol_up =
619 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
620 if (symbol_up) {
621 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
622 assert(pdb_func);
623 auto func_uid = pdb_func->getSymIndexId();
624 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
625 if (sc.function == nullptr)
626 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
627 if (sc.function) {
628 resolved_flags |= eSymbolContextFunction;
629 if (resolve_scope & eSymbolContextBlock) {
630 Block &block = sc.function->GetBlock(true);
631 sc.block = block.FindBlockByID(sc.function->GetID());
632 if (sc.block)
633 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000634 }
635 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000636 }
637 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000638
639 if (resolve_scope & eSymbolContextLineEntry) {
640 if (auto *line_table = sc.comp_unit->GetLineTable()) {
641 Address addr(so_addr);
642 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
643 resolved_flags |= eSymbolContextLineEntry;
644 }
645 }
646
Aaron Smith7ac1c782018-02-09 05:31:28 +0000647 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648}
649
650uint32_t SymbolFilePDB::ResolveSymbolContext(
651 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
652 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000653 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 if (resolve_scope & lldb::eSymbolContextCompUnit) {
655 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000656 // file. For example, if `file_spec` is <vector>, then this should return
657 // all source files and header files that reference <vector>, either
658 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 auto compilands = m_session_up->findCompilandsForSourceFile(
660 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
661
Aaron Smith10a02572018-01-13 06:58:18 +0000662 if (!compilands)
663 return 0;
664
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000665 // For each one, either find its previously parsed data or parse it afresh
666 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000668 // If we're not checking inlines, then don't add line information for
669 // this file unless the FileSpec matches. For inline functions, we don't
670 // have to match the FileSpec since they could be defined in headers
671 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000673 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000674 if (source_file.empty())
675 continue;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000676 FileSpec this_spec(source_file, false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000677 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
678 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 continue;
680 }
681
682 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000683 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000684 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000685 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 sc.comp_unit = cu.get();
687 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688
689 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000690 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000691 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
692 eSymbolContextLineEntry)) {
693 bool has_line_table = ParseCompileUnitLineTable(sc, line);
694
695 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
696 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000697 // compile unit. This is not normal for `line` = 0. So just assert
698 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000699 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000700
701 // Current compiland does not have the requested line. Search next.
702 continue;
703 }
704
705 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
706 if (!has_line_table)
707 continue;
708
709 auto *line_table = sc.comp_unit->GetLineTable();
710 lldbassert(line_table);
711
712 uint32_t num_line_entries = line_table->GetSize();
713 // Skip the terminal line entry.
714 --num_line_entries;
715
Adrian Prantl05097242018-04-30 16:49:04 +0000716 // If `line `!= 0, see if we can resolve function for each line entry
717 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000718 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
719 ++line_idx) {
720 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
721 continue;
722
723 auto file_vm_addr =
724 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000725 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000726 continue;
727
Aaron Smithc8316ed2018-03-22 03:44:51 +0000728 auto symbol_up = m_session_up->findSymbolByAddress(
729 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000730 if (symbol_up) {
731 auto func_uid = symbol_up->getSymIndexId();
732 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
733 if (sc.function == nullptr) {
734 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
735 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000736 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000737 }
738 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
739 Block &block = sc.function->GetBlock(true);
740 sc.block = block.FindBlockByID(sc.function->GetID());
741 }
742 }
743 sc_list.Append(sc);
744 }
745 } else if (has_line_table) {
746 // We can parse line table for the compile unit. But no query to
747 // resolve function or block. We append `sc` to the list anyway.
748 sc_list.Append(sc);
749 }
750 } else {
751 // No query for line entry, function or block. But we have a valid
752 // compile unit, append `sc` to the list.
753 sc_list.Append(sc);
754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
756 }
Aaron Smith10a02572018-01-13 06:58:18 +0000757 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758}
759
Aaron Smithcab0d232018-05-23 01:52:42 +0000760std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
761 std::string decorated_name;
762 auto vm_addr = pdb_data.getVirtualAddress();
763 if (vm_addr != LLDB_INVALID_ADDRESS && vm_addr) {
764 auto result_up =
765 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol);
766 if (result_up) {
767 while (auto symbol_up = result_up->getNext()) {
768 if (symbol_up->getRawSymbol().getVirtualAddress() == vm_addr) {
769 decorated_name = symbol_up->getRawSymbol().getName();
770 break;
771 }
772 }
773 }
774 }
775 if (!decorated_name.empty())
776 return decorated_name;
777
778 return std::string();
779}
780
781VariableSP SymbolFilePDB::ParseVariableForPDBData(
782 const lldb_private::SymbolContext &sc,
783 const llvm::pdb::PDBSymbolData &pdb_data) {
784 VariableSP var_sp;
785 uint32_t var_uid = pdb_data.getSymIndexId();
786 auto result = m_variables.find(var_uid);
787 if (result != m_variables.end())
788 return result->second;
789
790 ValueType scope = eValueTypeInvalid;
791 bool is_static_member = false;
792 bool is_external = false;
793 bool is_artificial = false;
794
795 switch (pdb_data.getDataKind()) {
796 case PDB_DataKind::Global:
797 scope = eValueTypeVariableGlobal;
798 is_external = true;
799 break;
800 case PDB_DataKind::Local:
801 scope = eValueTypeVariableLocal;
802 break;
803 case PDB_DataKind::FileStatic:
804 scope = eValueTypeVariableStatic;
805 break;
806 case PDB_DataKind::StaticMember:
807 is_static_member = true;
808 scope = eValueTypeVariableStatic;
809 break;
810 case PDB_DataKind::Member:
811 scope = eValueTypeVariableStatic;
812 break;
813 case PDB_DataKind::Param:
814 scope = eValueTypeVariableArgument;
815 break;
816 case PDB_DataKind::Constant:
817 scope = eValueTypeConstResult;
818 break;
819 default:
820 break;
821 }
822
823 switch (pdb_data.getLocationType()) {
824 case PDB_LocType::TLS:
825 scope = eValueTypeVariableThreadLocal;
826 break;
827 case PDB_LocType::RegRel: {
828 // It is a `this` pointer.
829 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
830 scope = eValueTypeVariableArgument;
831 is_artificial = true;
832 }
833 } break;
834 default:
835 break;
836 }
837
838 Declaration decl;
839 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
840 if (auto lines = pdb_data.getLineNumbers()) {
841 if (auto first_line = lines->getNext()) {
842 uint32_t src_file_id = first_line->getSourceFileId();
843 auto src_file = m_session_up->getSourceFileById(src_file_id);
844 if (src_file) {
845 FileSpec spec(src_file->getFileName(), /*resolve_path*/ false);
846 decl.SetFile(spec);
847 decl.SetColumn(first_line->getColumnNumber());
848 decl.SetLine(first_line->getLineNumber());
849 }
850 }
851 }
852 }
853
854 Variable::RangeList ranges;
855 SymbolContextScope *context_scope = sc.comp_unit;
856 if (scope == eValueTypeVariableLocal) {
857 if (sc.function) {
858 context_scope = sc.function->GetBlock(true).FindBlockByID(
859 pdb_data.getClassParentId());
860 if (context_scope == nullptr)
861 context_scope = sc.function;
862 }
863 }
864
865 SymbolFileTypeSP type_sp =
866 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
867
868 auto var_name = pdb_data.getName();
869 auto mangled = GetMangledForPDBData(pdb_data);
870 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
871
872 DWARFExpression location(nullptr);
873
874 var_sp = std::make_shared<Variable>(
875 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
876 ranges, &decl, location, is_external, is_artificial, is_static_member);
877
878 m_variables.insert(std::make_pair(var_uid, var_sp));
879 return var_sp;
880}
881
882size_t
883SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
884 const llvm::pdb::PDBSymbol &pdb_symbol,
885 lldb_private::VariableList *variable_list) {
886 size_t num_added = 0;
887
888 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
889 VariableListSP local_variable_list_sp;
890
891 auto result = m_variables.find(pdb_data->getSymIndexId());
892 if (result != m_variables.end()) {
893 if (variable_list)
894 variable_list->AddVariableIfUnique(result->second);
895 } else {
896 // Prepare right VariableList for this variable.
897 if (auto lexical_parent = pdb_data->getLexicalParent()) {
898 switch (lexical_parent->getSymTag()) {
899 case PDB_SymType::Exe:
900 assert(sc.comp_unit);
901 LLVM_FALLTHROUGH;
902 case PDB_SymType::Compiland: {
903 if (sc.comp_unit) {
904 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
905 if (!local_variable_list_sp) {
906 local_variable_list_sp = std::make_shared<VariableList>();
907 sc.comp_unit->SetVariableList(local_variable_list_sp);
908 }
909 }
910 } break;
911 case PDB_SymType::Block:
912 case PDB_SymType::Function: {
913 if (sc.function) {
914 Block *block = sc.function->GetBlock(true).FindBlockByID(
915 lexical_parent->getSymIndexId());
916 if (block) {
917 local_variable_list_sp = block->GetBlockVariableList(false);
918 if (!local_variable_list_sp) {
919 local_variable_list_sp = std::make_shared<VariableList>();
920 block->SetVariableList(local_variable_list_sp);
921 }
922 }
923 }
924 } break;
925 default:
926 break;
927 }
928 }
929
930 if (local_variable_list_sp) {
931 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
932 local_variable_list_sp->AddVariableIfUnique(var_sp);
933 if (variable_list)
934 variable_list->AddVariableIfUnique(var_sp);
935 ++num_added;
936 }
937 }
938 }
939 }
940
941 if (auto results = pdb_symbol.findAllChildren()) {
942 while (auto result = results->getNext())
943 num_added += ParseVariables(sc, *result, variable_list);
944 }
945
946 return num_added;
947}
948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949uint32_t SymbolFilePDB::FindGlobalVariables(
950 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +0000951 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000953 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
954 return 0;
955 if (name.IsEmpty())
956 return 0;
957
958 auto results =
959 m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(),
960 PDB_NameSearchFlags::NS_CaseSensitive);
961 if (!results)
962 return 0;
963
964 uint32_t matches = 0;
965 size_t old_size = variables.GetSize();
966 while (auto result = results->getNext()) {
967 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
968 if (max_matches > 0 && matches >= max_matches)
969 break;
970
971 SymbolContext sc;
972 sc.module_sp = m_obj_file->GetModule();
973 lldbassert(sc.module_sp.get());
974
975 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
976 // FIXME: We are not able to determine the compile unit.
977 if (sc.comp_unit == nullptr)
978 continue;
979
980 ParseVariables(sc, *pdb_data, &variables);
981 matches = variables.GetSize() - old_size;
982 }
983
984 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985}
986
987uint32_t
988SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +0000989 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000991 if (!regex.IsValid())
992 return 0;
993 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
994 if (!results)
995 return 0;
996
997 uint32_t matches = 0;
998 size_t old_size = variables.GetSize();
999 while (auto pdb_data = results->getNext()) {
1000 if (max_matches > 0 && matches >= max_matches)
1001 break;
1002
1003 auto var_name = pdb_data->getName();
1004 if (var_name.empty())
1005 continue;
1006 if (!regex.Execute(var_name))
1007 continue;
1008 SymbolContext sc;
1009 sc.module_sp = m_obj_file->GetModule();
1010 lldbassert(sc.module_sp.get());
1011
1012 sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
1013 // FIXME: We are not able to determine the compile unit.
1014 if (sc.comp_unit == nullptr)
1015 continue;
1016
1017 ParseVariables(sc, *pdb_data, &variables);
1018 matches = variables.GetSize() - old_size;
1019 }
1020
1021 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022}
1023
Aaron Smithe664b5d2018-03-19 21:14:19 +00001024bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001025 bool include_inlines,
1026 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001027 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001028 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001029 if (!sc.comp_unit)
1030 return false;
1031 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001032 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001033 if (!sc.function)
1034 return false;
1035
1036 sc_list.Append(sc);
1037 return true;
1038}
1039
1040bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1041 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001042 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001043 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1044 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001045 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001046}
1047
1048void SymbolFilePDB::CacheFunctionNames() {
1049 if (!m_func_full_names.IsEmpty())
1050 return;
1051
1052 std::map<uint64_t, uint32_t> addr_ids;
1053
1054 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1055 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001056 if (pdb_func_up->isCompilerGenerated())
1057 continue;
1058
Aaron Smith7ac1c782018-02-09 05:31:28 +00001059 auto name = pdb_func_up->getName();
1060 auto demangled_name = pdb_func_up->getUndecoratedName();
1061 if (name.empty() && demangled_name.empty())
1062 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001063
Aaron Smithf76fe682018-03-07 03:16:50 +00001064 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001065 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1066 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1067
1068 if (auto parent = pdb_func_up->getClassParent()) {
1069
1070 // PDB have symbols for class/struct methods or static methods in Enum
1071 // Class. We won't bother to check if the parent is UDT or Enum here.
1072 m_func_method_names.Append(ConstString(name), uid);
1073
1074 ConstString cstr_name(name);
1075
Adrian Prantl05097242018-04-30 16:49:04 +00001076 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1077 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1078 // not have inforamtion of this, we extract base names and cache them
1079 // by our own effort.
Aaron Smith7ac1c782018-02-09 05:31:28 +00001080 llvm::StringRef basename;
1081 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
1082 if (cpp_method.IsValid()) {
1083 llvm::StringRef context;
1084 basename = cpp_method.GetBasename();
1085 if (basename.empty())
1086 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
1087 context, basename);
1088 }
1089
1090 if (!basename.empty())
1091 m_func_base_names.Append(ConstString(basename), uid);
1092 else {
1093 m_func_base_names.Append(ConstString(name), uid);
1094 }
1095
1096 if (!demangled_name.empty())
1097 m_func_full_names.Append(ConstString(demangled_name), uid);
1098
1099 } else {
1100 // Handle not-method symbols.
1101
1102 // The function name might contain namespace, or its lexical scope. It
1103 // is not safe to get its base name by applying same scheme as we deal
1104 // with the method names.
1105 // FIXME: Remove namespace if function is static in a scope.
1106 m_func_base_names.Append(ConstString(name), uid);
1107
1108 if (name == "main") {
1109 m_func_full_names.Append(ConstString(name), uid);
1110
1111 if (!demangled_name.empty() && name != demangled_name) {
1112 m_func_full_names.Append(ConstString(demangled_name), uid);
1113 m_func_base_names.Append(ConstString(demangled_name), uid);
1114 }
1115 } else if (!demangled_name.empty()) {
1116 m_func_full_names.Append(ConstString(demangled_name), uid);
1117 } else {
1118 m_func_full_names.Append(ConstString(name), uid);
1119 }
1120 }
1121 }
1122 }
1123
1124 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001125 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001126 while (auto pub_sym_up = results_up->getNext()) {
1127 if (!pub_sym_up->isFunction())
1128 continue;
1129 auto name = pub_sym_up->getName();
1130 if (name.empty())
1131 continue;
1132
1133 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001134 auto vm_addr = pub_sym_up->getVirtualAddress();
1135
1136 // PDB public symbol has mangled name for its associated function.
1137 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1138 // Cache mangled name.
1139 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1140 }
1141 }
1142 }
1143 }
1144 // Sort them before value searching is working properly
1145 m_func_full_names.Sort();
1146 m_func_full_names.SizeToFit();
1147 m_func_method_names.Sort();
1148 m_func_method_names.SizeToFit();
1149 m_func_base_names.Sort();
1150 m_func_base_names.SizeToFit();
1151}
1152
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153uint32_t SymbolFilePDB::FindFunctions(
1154 const lldb_private::ConstString &name,
1155 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1156 uint32_t name_type_mask, bool include_inlines, bool append,
1157 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001158 if (!append)
1159 sc_list.Clear();
1160 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1161
1162 if (name_type_mask == eFunctionNameTypeNone)
1163 return 0;
1164 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1165 return 0;
1166 if (name.IsEmpty())
1167 return 0;
1168
1169 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001170 if (name_type_mask & eFunctionNameTypeFull ||
1171 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001172 name_type_mask & eFunctionNameTypeMethod) {
1173 CacheFunctionNames();
1174
1175 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001176 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
1177 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001178 std::vector<uint32_t> ids;
1179 if (Names.GetValues(name, ids)) {
1180 for (auto id : ids) {
1181 if (resolved_ids.find(id) == resolved_ids.end()) {
1182 if (ResolveFunction(id, include_inlines, sc_list))
1183 resolved_ids.insert(id);
1184 }
1185 }
1186 }
1187 };
1188 if (name_type_mask & eFunctionNameTypeFull) {
1189 ResolveFn(m_func_full_names);
1190 }
1191 if (name_type_mask & eFunctionNameTypeBase) {
1192 ResolveFn(m_func_base_names);
1193 }
1194 if (name_type_mask & eFunctionNameTypeMethod) {
1195 ResolveFn(m_func_method_names);
1196 }
1197 }
1198 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199}
1200
1201uint32_t
1202SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1203 bool include_inlines, bool append,
1204 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001205 if (!append)
1206 sc_list.Clear();
1207 if (!regex.IsValid())
1208 return 0;
1209
1210 auto old_size = sc_list.GetSize();
1211 CacheFunctionNames();
1212
1213 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001214 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1215 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001216 std::vector<uint32_t> ids;
1217 if (Names.GetValues(regex, ids)) {
1218 for (auto id : ids) {
1219 if (resolved_ids.find(id) == resolved_ids.end())
1220 if (ResolveFunction(id, include_inlines, sc_list))
1221 resolved_ids.insert(id);
1222 }
1223 }
1224 };
1225 ResolveFn(m_func_full_names);
1226 ResolveFn(m_func_base_names);
1227
1228 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229}
1230
1231void SymbolFilePDB::GetMangledNamesForFunction(
1232 const std::string &scope_qualified_name,
1233 std::vector<lldb_private::ConstString> &mangled_names) {}
1234
1235uint32_t SymbolFilePDB::FindTypes(
1236 const lldb_private::SymbolContext &sc,
1237 const lldb_private::ConstString &name,
1238 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1239 uint32_t max_matches,
1240 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1241 lldb_private::TypeMap &types) {
1242 if (!append)
1243 types.Clear();
1244 if (!name)
1245 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001246 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1247 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248
1249 searched_symbol_files.clear();
1250 searched_symbol_files.insert(this);
1251
1252 std::string name_str = name.AsCString();
1253
Aaron Smith86e94342017-12-22 05:26:50 +00001254 // There is an assumption 'name' is not a regex
1255 FindTypesByName(name_str, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001256
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 return types.GetSize();
1258}
1259
Aaron Smithc8316ed2018-03-22 03:44:51 +00001260void SymbolFilePDB::FindTypesByRegex(
1261 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1262 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001264 // space as much as possible since this searches EVERYTHING in the PDB,
1265 // manually doing regex comparisons. PDB library isn't optimized for regex
1266 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001268 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1270 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 std::unique_ptr<IPDBEnumSymbols> results;
1272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273 uint32_t matches = 0;
1274
1275 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001276 results = m_global_scope_up->findAllChildren(tag);
1277 if (!results)
1278 continue;
1279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 while (auto result = results->getNext()) {
1281 if (max_matches > 0 && matches >= max_matches)
1282 break;
1283
1284 std::string type_name;
1285 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1286 type_name = enum_type->getName();
1287 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001288 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 type_name = typedef_type->getName();
1290 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1291 type_name = class_type->getName();
1292 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001293 // We're looking only for types that have names. Skip symbols, as well
1294 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 continue;
1296 }
1297
Aaron Smith86e94342017-12-22 05:26:50 +00001298 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 continue;
1300
1301 // This should cause the type to get cached and stored in the `m_types`
1302 // lookup.
1303 if (!ResolveTypeUID(result->getSymIndexId()))
1304 continue;
1305
1306 auto iter = m_types.find(result->getSymIndexId());
1307 if (iter == m_types.end())
1308 continue;
1309 types.Insert(iter->second);
1310 ++matches;
1311 }
1312 }
1313}
1314
1315void SymbolFilePDB::FindTypesByName(const std::string &name,
1316 uint32_t max_matches,
1317 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001319 if (name.empty())
1320 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001321 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1322 PDB_NameSearchFlags::NS_Default);
1323 if (!results)
1324 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325
1326 uint32_t matches = 0;
1327
1328 while (auto result = results->getNext()) {
1329 if (max_matches > 0 && matches >= max_matches)
1330 break;
1331 switch (result->getSymTag()) {
1332 case PDB_SymType::Enum:
1333 case PDB_SymType::UDT:
1334 case PDB_SymType::Typedef:
1335 break;
1336 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001337 // We're looking only for types that have names. Skip symbols, as well
1338 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 continue;
1340 }
1341
1342 // This should cause the type to get cached and stored in the `m_types`
1343 // lookup.
1344 if (!ResolveTypeUID(result->getSymIndexId()))
1345 continue;
1346
1347 auto iter = m_types.find(result->getSymIndexId());
1348 if (iter == m_types.end())
1349 continue;
1350 types.Insert(iter->second);
1351 ++matches;
1352 }
1353}
1354
1355size_t SymbolFilePDB::FindTypes(
1356 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1357 lldb_private::TypeMap &types) {
1358 return 0;
1359}
1360
Aaron Smithec40f812018-01-23 20:35:19 +00001361lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1362 return m_obj_file->GetModule()->GetTypeList();
1363}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364
Aaron Smithc8316ed2018-03-22 03:44:51 +00001365void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1366 uint32_t type_mask,
1367 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001368 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001369 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001370 case PDB_SymType::ArrayType:
1371 can_parse = ((type_mask & eTypeClassArray) != 0);
1372 break;
1373 case PDB_SymType::BuiltinType:
1374 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1375 break;
1376 case PDB_SymType::Enum:
1377 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1378 break;
1379 case PDB_SymType::Function:
1380 case PDB_SymType::FunctionSig:
1381 can_parse = ((type_mask & eTypeClassFunction) != 0);
1382 break;
1383 case PDB_SymType::PointerType:
1384 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1385 eTypeClassMemberPointer)) != 0);
1386 break;
1387 case PDB_SymType::Typedef:
1388 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1389 break;
1390 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001391 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001392 assert(udt);
1393 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001394 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1395 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001396 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001397 default:
1398 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001399 }
1400
1401 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001402 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001403 auto result =
1404 std::find(type_collection.begin(), type_collection.end(), type);
1405 if (result == type_collection.end())
1406 type_collection.push_back(type);
1407 }
1408 }
1409
Aaron Smithe664b5d2018-03-19 21:14:19 +00001410 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001411 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001412 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1416 uint32_t type_mask,
1417 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001418 TypeCollection type_collection;
1419 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001420 CompileUnit *cu =
1421 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001422 if (cu) {
1423 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001424 if (!compiland_up)
1425 return 0;
1426 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001427 } else {
1428 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1429 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001430 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001431 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1432 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001433 }
1434 }
1435 }
1436
1437 for (auto type : type_collection) {
1438 type->GetForwardCompilerType();
1439 type_list.Insert(type->shared_from_this());
1440 }
1441 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001442}
1443
1444lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1446 auto type_system =
1447 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1448 if (type_system)
1449 type_system->SetSymbolFile(this);
1450 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001451}
1452
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1454 const lldb_private::SymbolContext &sc,
1455 const lldb_private::ConstString &name,
1456 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1457 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001458}
1459
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1461 static ConstString g_name("pdb");
1462 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001463}
1464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1466
1467IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1468
1469const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1470 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001471}
1472
Aaron Smithc8316ed2018-03-22 03:44:51 +00001473lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1474 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 auto found_cu = m_comp_units.find(id);
1476 if (found_cu != m_comp_units.end())
1477 return found_cu->second;
1478
Aaron Smith10a02572018-01-13 06:58:18 +00001479 auto compiland_up = GetPDBCompilandByUID(id);
1480 if (!compiland_up)
1481 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482
1483 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001484 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 if (!details)
1486 lang = lldb::eLanguageTypeC_plus_plus;
1487 else
1488 lang = TranslateLanguage(details->getLanguage());
1489
Aaron Smithf76fe682018-03-07 03:16:50 +00001490 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1491 return CompUnitSP();
1492
Aaron Smith487b0c62018-03-20 00:18:22 +00001493 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001494 if (path.empty())
1495 return CompUnitSP();
1496
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497 // Don't support optimized code for now, DebugInfoPDB does not return this
1498 // information.
1499 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001500 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1501 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001502
1503 if (!cu_sp)
1504 return CompUnitSP();
1505
1506 m_comp_units.insert(std::make_pair(id, cu_sp));
1507 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001508 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001509 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001510 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1511 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001512 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001513}
1514
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515bool SymbolFilePDB::ParseCompileUnitLineTable(
1516 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001517 lldbassert(sc.comp_unit);
1518
1519 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1520 if (!compiland_up)
1521 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001524 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001525 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1526 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001528 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001530
Aaron Smith10a02572018-01-13 06:58:18 +00001531 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001533 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1534 if (!files)
1535 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001536
Adrian Prantl05097242018-04-30 16:49:04 +00001537 // For each source and header file, create a LineSequence for contributions
1538 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 while (auto file = files->getNext()) {
1540 std::unique_ptr<LineSequence> sequence(
1541 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001542 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1543 if (!lines)
1544 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 uint64_t prev_addr;
1548 uint32_t prev_length;
1549 uint32_t prev_line;
1550 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001551
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 for (int i = 0; i < entry_count; ++i) {
1553 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001554
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 uint64_t lno = line->getLineNumber();
1556 uint64_t addr = line->getVirtualAddress();
1557 uint32_t length = line->getLength();
1558 uint32_t source_id = line->getSourceFileId();
1559 uint32_t col = line->getColumnNumber();
1560 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001561
Adrian Prantl05097242018-04-30 16:49:04 +00001562 // There was a gap between the current entry and the previous entry if
1563 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001567 // of the previous entry's address range if the current entry resulted in
1568 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1570 line_table->AppendLineEntryToSequence(
1571 sequence.get(), prev_addr + prev_length, prev_line, 0,
1572 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001573
1574 line_table->InsertSequence(sequence.release());
1575 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 }
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;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001633
1634 // LLDB uses the DWARF-like file numeration (one based)
1635 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 while (auto file = source_files->getNext()) {
1638 uint32_t source_id = file->getUniqueId();
1639 index_map[source_id] = index++;
1640 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001641}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001642
1643lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001644 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001645 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001646 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001647 return nullptr;
1648
Aaron Smith308e39c2018-03-22 19:26:33 +00001649 // If it is a PDB function's vm addr, this is the first sure bet.
1650 if (auto lines =
1651 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1652 if (auto first_line = lines->getNext())
1653 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001654 }
1655
Aaron Smith308e39c2018-03-22 19:26:33 +00001656 // Otherwise we resort to section contributions.
1657 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1658 while (auto section = sec_contribs->getNext()) {
1659 auto va = section->getVirtualAddress();
1660 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1661 return ParseCompileUnitForUID(section->getCompilandId());
1662 }
1663 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001664 return nullptr;
1665}
1666
1667Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001668SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001669 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001670 auto func_name = pdb_func.getName();
1671 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001672 std::string func_decorated_name;
1673
1674 // Seek from public symbols for non-static function's decorated name if any.
1675 // For static functions, they don't have undecorated names and aren't exposed
1676 // in Public Symbols either.
1677 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001678 auto result_up = m_global_scope_up->findChildren(
1679 PDB_SymType::PublicSymbol, func_undecorated_name,
1680 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001681 if (result_up) {
1682 while (auto symbol_up = result_up->getNext()) {
1683 // For a public symbol, it is unique.
1684 lldbassert(result_up->getChildCount() == 1);
1685 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001686 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1687 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001688 if (pdb_public_sym->isFunction()) {
1689 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001690 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001691 }
1692 }
1693 }
1694 }
1695 }
1696 if (!func_decorated_name.empty()) {
1697 mangled.SetMangledName(ConstString(func_decorated_name));
1698
1699 // For MSVC, format of C funciton's decorated name depends on calling
1700 // conventon. Unfortunately none of the format is recognized by current
1701 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001702 // `__purecall` is retrieved as both its decorated and undecorated name
1703 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1704 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1705 // Mangled::GetDemangledName method will fail internally and caches an
1706 // empty string as its undecorated name. So we will face a contradition
1707 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001708 // non-empty undecorated name from PDB
1709 // empty undecorated name from LLDB
1710 if (!func_undecorated_name.empty() &&
1711 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1712 mangled.SetDemangledName(ConstString(func_undecorated_name));
1713
1714 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001715 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1716 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001717 // PDB source unless we also apply same flags in getting undecorated
1718 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1719 if (!func_undecorated_name.empty() &&
1720 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1721 ConstString(func_undecorated_name))
1722 mangled.SetDemangledName(ConstString(func_undecorated_name));
1723 } else if (!func_undecorated_name.empty()) {
1724 mangled.SetDemangledName(ConstString(func_undecorated_name));
1725 } else if (!func_name.empty())
1726 mangled.SetValue(ConstString(func_name), false);
1727
1728 return mangled;
1729}
1730
1731bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1732 const lldb_private::CompilerDeclContext *decl_ctx) {
1733 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1734 return true;
1735
1736 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1737 if (!decl_ctx_type_system)
1738 return false;
1739 TypeSystem *type_system = GetTypeSystemForLanguage(
1740 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1741 if (decl_ctx_type_system == type_system)
1742 return true; // The type systems match, return true
1743
1744 return false;
1745}