blob: aa831f0bfff0062236abd4601cd3d440f000d414 [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turner74e08ca2016-03-02 22:05:52 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolFilePDB.h"
10
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000011#include "PDBASTParser.h"
12#include "PDBLocationToDWARFExpression.h"
13
Zachary Turner42dff792016-04-15 00:21:26 +000014#include "clang/Lex/Lexer.h"
15
Zachary Turner74e08ca2016-03-02 22:05:52 +000016#include "lldb/Core/Module.h"
17#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000018#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000019#include "lldb/Symbol/CompileUnit.h"
20#include "lldb/Symbol/LineTable.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000023#include "lldb/Symbol/SymbolVendor.h"
Aaron Smithec40f812018-01-23 20:35:19 +000024#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000025#include "lldb/Symbol/TypeMap.h"
Aaron Smithcab0d232018-05-23 01:52:42 +000026#include "lldb/Symbol/Variable.h"
Aaron Smith86e94342017-12-22 05:26:50 +000027#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000028
Pavel Labathb8d8c622016-05-09 11:07:43 +000029#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000030#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000031#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
32#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000033#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000035#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000036#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000037#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000038#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000040#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000041#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
42#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000045#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000046#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
47#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
48#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
49
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000050#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000051#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000052#include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
Zachary Turner42dff792016-04-15 00:21:26 +000053
54#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000055
Aaron Smith10a02572018-01-13 06:58:18 +000056using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000057using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000058using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060namespace {
61lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
62 switch (lang) {
63 case PDB_Lang::Cpp:
64 return lldb::LanguageType::eLanguageTypeC_plus_plus;
65 case PDB_Lang::C:
66 return lldb::LanguageType::eLanguageTypeC;
Nathan Lanza0561be62019-03-11 23:30:58 +000067 case PDB_Lang::Swift:
68 return lldb::LanguageType::eLanguageTypeSwift;
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 default:
70 return lldb::LanguageType::eLanguageTypeUnknown;
71 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
75 uint32_t addr_length) {
76 return ((requested_line == 0 || actual_line == requested_line) &&
77 addr_length > 0);
78}
Aaron Smithc8316ed2018-03-22 03:44:51 +000079} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000080
Zachary Turner307f5ae2018-10-12 19:47:13 +000081static bool ShouldUseNativeReader() {
Greg Clayton71970b72018-12-12 18:14:27 +000082#if defined(_WIN32)
Zachary Turner307f5ae2018-10-12 19:47:13 +000083 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
84 return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
85 use_native.equals_lower("1") || use_native.equals_lower("true");
Greg Clayton71970b72018-12-12 18:14:27 +000086#else
87 return true;
88#endif
Zachary Turner307f5ae2018-10-12 19:47:13 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000092 if (ShouldUseNativeReader()) {
93 npdb::SymbolFileNativePDB::Initialize();
94 } else {
95 PluginManager::RegisterPlugin(GetPluginNameStatic(),
96 GetPluginDescriptionStatic(), CreateInstance,
97 DebuggerInitialize);
98 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000102 if (ShouldUseNativeReader()) {
103 npdb::SymbolFileNativePDB::Terminate();
104 } else {
105 PluginManager::UnregisterPlugin(CreateInstance);
106 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
110
111lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
112 static ConstString g_name("pdb");
113 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000114}
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116const char *SymbolFilePDB::GetPluginDescriptionStatic() {
117 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000118}
119
120lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
122 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000123}
124
125SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000126 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
127 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000132 uint32_t abilities = 0;
133 if (!m_obj_file)
134 return 0;
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 if (!m_session_up) {
137 // Lazily load and match the PDB file, but only do this once.
138 std::string exePath = m_obj_file->GetFileSpec().GetPath();
139 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
140 m_session_up);
141 if (error) {
142 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000143 auto module_sp = m_obj_file->GetModule();
144 if (!module_sp)
145 return 0;
146 // See if any symbol file is specified through `--symfile` option.
147 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
148 if (!symfile)
149 return 0;
150 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000151 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000152 if (error) {
153 llvm::consumeError(std::move(error));
154 return 0;
155 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000158 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000159 return 0;
160
161 auto enum_tables_up = m_session_up->getEnumTables();
162 if (!enum_tables_up)
163 return 0;
164 while (auto table_up = enum_tables_up->getNext()) {
165 if (table_up->getItemCount() == 0)
166 continue;
167 auto type = table_up->getTableType();
168 switch (type) {
169 case PDB_TableType::Symbols:
170 // This table represents a store of symbols with types listed in
171 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000172 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
173 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000174 break;
175 case PDB_TableType::LineNumbers:
176 abilities |= LineTables;
177 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000178 default:
179 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000180 }
181 }
182 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185void SymbolFilePDB::InitializeObject() {
Pavel Labathd1304bb2019-02-18 11:06:57 +0000186 lldb::addr_t obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000187 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000189 if (!m_global_scope_up)
190 m_global_scope_up = m_session_up->getGlobalScope();
191 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 TypeSystem *type_system =
194 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
195 ClangASTContext *clang_type_system =
196 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000197 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
199 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202uint32_t SymbolFilePDB::GetNumCompileUnits() {
203 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000204 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
205 if (!compilands)
206 return 0;
207
208 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000209 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
210 // found as a child of the global scope (PDB executable). Usually, such
211 // compilands contain `thunk` symbols in which we are not interested for
212 // now. However we still count them in the compiland list. If we perform
213 // any compiland related activity, like finding symbols through
214 // llvm::pdb::IPDBSession methods, such compilands will all be searched
215 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000219 // PDB. Ignore this special compile unit for our purposes, if it is there.
220 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000221 auto last_compiland_up =
222 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
223 lldbassert(last_compiland_up.get());
224 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (name == "* Linker *")
226 --m_cached_compile_unit_count;
227 }
228 return m_cached_compile_unit_count;
229}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000230
Aaron Smith10a02572018-01-13 06:58:18 +0000231void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000232 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000233 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
234 if (!results_up)
235 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000236 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000237 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000238 auto compiland_up = results_up->getChildAtIndex(cu_idx);
239 if (!compiland_up)
240 continue;
241 if (compiland_up->getSymIndexId() == uid) {
242 index = cu_idx;
243 return;
244 }
245 }
246 index = UINT32_MAX;
247 return;
248}
249
250std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
251SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
252 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
253}
254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000256 if (index >= GetNumCompileUnits())
257 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258
Aaron Smith10a02572018-01-13 06:58:18 +0000259 // Assuming we always retrieve same compilands listed in same order through
260 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
261 // compile unit makes no sense.
262 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
263 if (!results)
264 return CompUnitSP();
265 auto compiland_up = results->getChildAtIndex(index);
266 if (!compiland_up)
267 return CompUnitSP();
268 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000269}
270
Zachary Turner863f8c12019-01-11 18:03:20 +0000271lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
272 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000273 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000275 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 if (!details)
277 return lldb::eLanguageTypeUnknown;
278 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000279}
280
Zachary Turner863f8c12019-01-11 18:03:20 +0000281lldb_private::Function *
282SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
283 CompileUnit &comp_unit) {
284 if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId()))
Aleksandr Urakova5235af2018-12-03 13:31:13 +0000285 return result.get();
286
Aaron Smithe664b5d2018-03-19 21:14:19 +0000287 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000288 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000289 return nullptr;
290
Aaron Smithe664b5d2018-03-19 21:14:19 +0000291 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000292 AddressRange func_range =
Zachary Turner863f8c12019-01-11 18:03:20 +0000293 AddressRange(file_vm_addr, func_length,
294 GetObjectFile()->GetModule()->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000295 if (!func_range.GetBaseAddress().IsValid())
296 return nullptr;
297
Aaron Smithc8316ed2018-03-22 03:44:51 +0000298 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000299 if (!func_type)
300 return nullptr;
301
Aaron Smithe664b5d2018-03-19 21:14:19 +0000302 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000303
Aaron Smith7ac1c782018-02-09 05:31:28 +0000304 Mangled mangled = GetMangledForPDBFunc(pdb_func);
305
Aaron Smithc8316ed2018-03-22 03:44:51 +0000306 FunctionSP func_sp =
Zachary Turner863f8c12019-01-11 18:03:20 +0000307 std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(),
Aaron Smithc8316ed2018-03-22 03:44:51 +0000308 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000309
Zachary Turner863f8c12019-01-11 18:03:20 +0000310 comp_unit.AddFunction(func_sp);
Zachary Turnerc68925a2019-01-09 21:20:44 +0000311
Nathan Lanzad0050d12019-03-27 01:24:03 +0000312 LanguageType lang = ParseLanguage(comp_unit);
313 TypeSystem *type_system = GetTypeSystemForLanguage(lang);
Zachary Turnerc68925a2019-01-09 21:20:44 +0000314 if (!type_system)
315 return nullptr;
316 ClangASTContext *clang_type_system =
317 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
318 if (!clang_type_system)
319 return nullptr;
320 clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func);
321
Aaron Smith7ac1c782018-02-09 05:31:28 +0000322 return func_sp.get();
323}
324
Zachary Turner863f8c12019-01-11 18:03:20 +0000325size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000326 size_t func_added = 0;
Zachary Turner863f8c12019-01-11 18:03:20 +0000327 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000328 if (!compiland_up)
329 return 0;
330 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
331 if (!results_up)
332 return 0;
333 while (auto pdb_func_up = results_up->getNext()) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000334 auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000335 if (!func_sp) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000336 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000337 ++func_added;
338 }
339 }
340 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000341}
342
Zachary Turner863f8c12019-01-11 18:03:20 +0000343bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
344 if (comp_unit.GetLineTable())
Aaron Smith10a02572018-01-13 06:58:18 +0000345 return true;
Zachary Turner863f8c12019-01-11 18:03:20 +0000346 return ParseCompileUnitLineTable(comp_unit, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000347}
348
Zachary Turner863f8c12019-01-11 18:03:20 +0000349bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 // PDB doesn't contain information about macros
351 return false;
352}
353
Zachary Turner863f8c12019-01-11 18:03:20 +0000354bool SymbolFilePDB::ParseSupportFiles(
355 CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) {
Zachary Turner74e08ca2016-03-02 22:05:52 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000358 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
359 // second time seems like a waste. Unfortunately, there's no good way around
360 // this short of a moderate refactor since SymbolVendor depends on being able
361 // to cache this list.
Zachary Turner863f8c12019-01-11 18:03:20 +0000362 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000363 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000364 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000365 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000367 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368
369 while (auto file = files->getNext()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000370 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000371 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000373
374 // LLDB uses the DWARF-like file numeration (one based),
375 // the zeroth file is the compile unit itself
Zachary Turner863f8c12019-01-11 18:03:20 +0000376 support_files.Insert(0, comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000379}
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381bool SymbolFilePDB::ParseImportedModules(
382 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000383 std::vector<SourceModule> &imported_modules) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 // PDB does not yet support module debug info
385 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000386}
387
Aaron Smithc8316ed2018-03-22 03:44:51 +0000388static size_t ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000389 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
390 lldb_private::Block *parent_block, bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000391 assert(pdb_symbol && parent_block);
392
393 size_t num_added = 0;
394 switch (pdb_symbol->getSymTag()) {
395 case PDB_SymType::Block:
396 case PDB_SymType::Function: {
397 Block *block = nullptr;
398 auto &raw_sym = pdb_symbol->getRawSymbol();
399 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
400 if (pdb_func->hasNoInlineAttribute())
401 break;
402 if (is_top_parent)
403 block = parent_block;
404 else
405 break;
406 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
407 auto uid = pdb_symbol->getSymIndexId();
408 if (parent_block->FindBlockByID(uid))
409 break;
410 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
411 break;
412
413 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
414 parent_block->AddChild(block_sp);
415 block = block_sp.get();
416 } else
417 llvm_unreachable("Unexpected PDB symbol!");
418
Aaron Smithc8316ed2018-03-22 03:44:51 +0000419 block->AddRange(Block::Range(
420 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000421 block->FinalizeRanges();
422 ++num_added;
423
424 auto results_up = pdb_symbol->findAllChildren();
425 if (!results_up)
426 break;
427 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000428 num_added += ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000429 func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000430 }
431 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000432 default:
433 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000434 }
435 return num_added;
436}
437
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000438size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000439 size_t num_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000440 auto uid = func.GetID();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000441 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
442 if (!pdb_func_up)
443 return 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000444 Block &parent_block = func.GetBlock(false);
445 num_added = ParseFunctionBlocksForPDBSymbol(
446 pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000447 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000448}
449
Zachary Turner863f8c12019-01-11 18:03:20 +0000450size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) {
Aaron Smith66b84072018-03-14 04:05:27 +0000451
452 size_t num_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000453 auto compiland = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000454 if (!compiland)
455 return 0;
456
457 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
458 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000459 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
460 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000461 for (auto tag : tags_to_search) {
462 results = raw_sym.findAllChildren(tag);
463 if (!results || results->getChildCount() == 0)
464 continue;
465 while (auto symbol = results->getNext()) {
466 switch (symbol->getSymTag()) {
467 case PDB_SymType::Enum:
468 case PDB_SymType::UDT:
469 case PDB_SymType::Typedef:
470 break;
471 default:
472 continue;
473 }
474
475 // This should cause the type to get cached and stored in the `m_types`
476 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000477 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
478 // Resolve the type completely to avoid a completion
479 // (and so a list change, which causes an iterators invalidation)
480 // during a TypeList dumping
481 type->GetFullCompilerType();
482 ++num_added;
483 }
Aaron Smith66b84072018-03-14 04:05:27 +0000484 }
Aaron Smithec40f812018-01-23 20:35:19 +0000485 }
Aaron Smith66b84072018-03-14 04:05:27 +0000486 };
Aaron Smithec40f812018-01-23 20:35:19 +0000487
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000488 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000489
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000490 // Also parse global types particularly coming from this compiland.
491 // Unfortunately, PDB has no compiland information for each global type. We
492 // have to parse them all. But ensure we only do this once.
493 static bool parse_all_global_types = false;
494 if (!parse_all_global_types) {
495 ParseTypesByTagFn(*m_global_scope_up);
496 parse_all_global_types = true;
Aaron Smithec40f812018-01-23 20:35:19 +0000497 }
498 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499}
500
501size_t
502SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000503 if (!sc.comp_unit)
504 return 0;
505
506 size_t num_added = 0;
507 if (sc.function) {
508 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
509 sc.function->GetID());
510 if (!pdb_func)
511 return 0;
512
513 num_added += ParseVariables(sc, *pdb_func);
514 sc.function->GetBlock(false).SetDidParseVariables(true, true);
515 } else if (sc.comp_unit) {
516 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
517 if (!compiland)
518 return 0;
519
520 if (sc.comp_unit->GetVariableList(false))
521 return 0;
522
523 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
524 if (results && results->getChildCount()) {
525 while (auto result = results->getNext()) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000526 auto cu_id = GetCompilandId(*result);
Aaron Smithcab0d232018-05-23 01:52:42 +0000527 // FIXME: We are not able to determine variable's compile unit.
528 if (cu_id == 0)
529 continue;
530
531 if (cu_id == sc.comp_unit->GetID())
532 num_added += ParseVariables(sc, *result);
533 }
534 }
535
536 // FIXME: A `file static` or `global constant` variable appears both in
537 // compiland's children and global scope's children with unexpectedly
538 // different symbol's Id making it ambiguous.
539
540 // FIXME: 'local constant', for example, const char var[] = "abc", declared
541 // in a function scope, can't be found in PDB.
542
543 // Parse variables in this compiland.
544 num_added += ParseVariables(sc, *compiland);
545 }
546
547 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548}
549
550lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
551 auto find_result = m_types.find(type_uid);
552 if (find_result != m_types.end())
553 return find_result->second.get();
554
555 TypeSystem *type_system =
556 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
557 ClangASTContext *clang_type_system =
558 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
559 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000560 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000561 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 if (!pdb)
563 return nullptr;
564
565 auto pdb_type = m_session_up->getSymbolById(type_uid);
566 if (pdb_type == nullptr)
567 return nullptr;
568
569 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000570 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000571 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000572 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000573 if (type_list)
574 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000575 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000577}
578
Adrian Prantleca07c52018-11-05 20:49:07 +0000579llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
580 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
581 return llvm::None;
582}
583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000585 std::lock_guard<std::recursive_mutex> guard(
586 GetObjectFile()->GetModule()->GetMutex());
587
588 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
589 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
590 if (!clang_ast_ctx)
591 return false;
592
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000593 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000594 if (!pdb)
595 return false;
596
597 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598}
599
600lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000601 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
602 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
603 if (!clang_ast_ctx)
604 return CompilerDecl();
605
606 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
607 if (!pdb)
608 return CompilerDecl();
609
610 auto symbol = m_session_up->getSymbolById(uid);
611 if (!symbol)
612 return CompilerDecl();
613
614 auto decl = pdb->GetDeclForSymbol(*symbol);
615 if (!decl)
616 return CompilerDecl();
617
618 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619}
620
621lldb_private::CompilerDeclContext
622SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000623 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
624 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
625 if (!clang_ast_ctx)
626 return CompilerDeclContext();
627
628 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
629 if (!pdb)
630 return CompilerDeclContext();
631
632 auto symbol = m_session_up->getSymbolById(uid);
633 if (!symbol)
634 return CompilerDeclContext();
635
636 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
637 if (!decl_context)
638 return GetDeclContextContainingUID(uid);
639
640 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641}
642
643lldb_private::CompilerDeclContext
644SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000645 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
646 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
647 if (!clang_ast_ctx)
648 return CompilerDeclContext();
649
650 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
651 if (!pdb)
652 return CompilerDeclContext();
653
654 auto symbol = m_session_up->getSymbolById(uid);
655 if (!symbol)
656 return CompilerDeclContext();
657
658 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
659 assert(decl_context);
660
661 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662}
663
664void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000665 lldb_private::CompilerDeclContext decl_ctx) {
666 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
667 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
668 if (!clang_ast_ctx)
669 return;
670
671 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
672 if (!pdb)
673 return;
674
675 pdb->ParseDeclsForDeclContext(
676 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
677}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678
679uint32_t
680SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000681 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000683 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000684 if (resolve_scope & eSymbolContextCompUnit ||
685 resolve_scope & eSymbolContextVariable ||
686 resolve_scope & eSymbolContextFunction ||
687 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000688 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000689 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
690 if (!cu_sp) {
691 if (resolved_flags | eSymbolContextVariable) {
692 // TODO: Resolve variables
693 }
694 return 0;
695 }
696 sc.comp_unit = cu_sp.get();
697 resolved_flags |= eSymbolContextCompUnit;
698 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000699 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000700
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000701 if (resolve_scope & eSymbolContextFunction ||
702 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000703 addr_t file_vm_addr = so_addr.GetFileAddress();
704 auto symbol_up =
705 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
706 if (symbol_up) {
707 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
708 assert(pdb_func);
709 auto func_uid = pdb_func->getSymIndexId();
710 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
711 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +0000712 sc.function =
713 ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000714 if (sc.function) {
715 resolved_flags |= eSymbolContextFunction;
716 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000717 auto block_symbol = m_session_up->findSymbolByAddress(
718 file_vm_addr, PDB_SymType::Block);
719 auto block_id = block_symbol ? block_symbol->getSymIndexId()
720 : sc.function->GetID();
721 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000722 if (sc.block)
723 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000724 }
725 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000726 }
727 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000728
729 if (resolve_scope & eSymbolContextLineEntry) {
730 if (auto *line_table = sc.comp_unit->GetLineTable()) {
731 Address addr(so_addr);
732 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
733 resolved_flags |= eSymbolContextLineEntry;
734 }
735 }
736
Aaron Smith7ac1c782018-02-09 05:31:28 +0000737 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738}
739
740uint32_t SymbolFilePDB::ResolveSymbolContext(
741 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000742 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000743 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 if (resolve_scope & lldb::eSymbolContextCompUnit) {
745 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000746 // file. For example, if `file_spec` is <vector>, then this should return
747 // all source files and header files that reference <vector>, either
748 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 auto compilands = m_session_up->findCompilandsForSourceFile(
750 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
751
Aaron Smith10a02572018-01-13 06:58:18 +0000752 if (!compilands)
753 return 0;
754
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000755 // For each one, either find its previously parsed data or parse it afresh
756 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000758 // If we're not checking inlines, then don't add line information for
759 // this file unless the FileSpec matches. For inline functions, we don't
760 // have to match the FileSpec since they could be defined in headers
761 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000763 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000764 if (source_file.empty())
765 continue;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000766 FileSpec this_spec(source_file, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000767 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
768 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 continue;
770 }
771
772 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000773 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000774 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000775 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 sc.comp_unit = cu.get();
777 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778
779 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000780 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000781 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
782 eSymbolContextLineEntry)) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000783 bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000784
785 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
786 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000787 // compile unit. This is not normal for `line` = 0. So just assert
788 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000789 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000790
791 // Current compiland does not have the requested line. Search next.
792 continue;
793 }
794
795 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
796 if (!has_line_table)
797 continue;
798
799 auto *line_table = sc.comp_unit->GetLineTable();
800 lldbassert(line_table);
801
802 uint32_t num_line_entries = line_table->GetSize();
803 // Skip the terminal line entry.
804 --num_line_entries;
805
Adrian Prantl05097242018-04-30 16:49:04 +0000806 // If `line `!= 0, see if we can resolve function for each line entry
807 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000808 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
809 ++line_idx) {
810 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
811 continue;
812
813 auto file_vm_addr =
814 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000815 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000816 continue;
817
Aaron Smithc8316ed2018-03-22 03:44:51 +0000818 auto symbol_up = m_session_up->findSymbolByAddress(
819 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000820 if (symbol_up) {
821 auto func_uid = symbol_up->getSymIndexId();
822 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
823 if (sc.function == nullptr) {
824 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
825 assert(pdb_func);
Zachary Turner863f8c12019-01-11 18:03:20 +0000826 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func,
827 *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000828 }
829 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
830 Block &block = sc.function->GetBlock(true);
831 sc.block = block.FindBlockByID(sc.function->GetID());
832 }
833 }
834 sc_list.Append(sc);
835 }
836 } else if (has_line_table) {
837 // We can parse line table for the compile unit. But no query to
838 // resolve function or block. We append `sc` to the list anyway.
839 sc_list.Append(sc);
840 }
841 } else {
842 // No query for line entry, function or block. But we have a valid
843 // compile unit, append `sc` to the list.
844 sc_list.Append(sc);
845 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 }
847 }
Aaron Smith10a02572018-01-13 06:58:18 +0000848 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849}
850
Aaron Smithcab0d232018-05-23 01:52:42 +0000851std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000852 // Cache public names at first
853 if (m_public_names.empty())
854 if (auto result_up =
855 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
856 while (auto symbol_up = result_up->getNext())
857 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
858 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000859
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000860 // Look up the name in the cache
861 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000862}
863
864VariableSP SymbolFilePDB::ParseVariableForPDBData(
865 const lldb_private::SymbolContext &sc,
866 const llvm::pdb::PDBSymbolData &pdb_data) {
867 VariableSP var_sp;
868 uint32_t var_uid = pdb_data.getSymIndexId();
869 auto result = m_variables.find(var_uid);
870 if (result != m_variables.end())
871 return result->second;
872
873 ValueType scope = eValueTypeInvalid;
874 bool is_static_member = false;
875 bool is_external = false;
876 bool is_artificial = false;
877
878 switch (pdb_data.getDataKind()) {
879 case PDB_DataKind::Global:
880 scope = eValueTypeVariableGlobal;
881 is_external = true;
882 break;
883 case PDB_DataKind::Local:
884 scope = eValueTypeVariableLocal;
885 break;
886 case PDB_DataKind::FileStatic:
887 scope = eValueTypeVariableStatic;
888 break;
889 case PDB_DataKind::StaticMember:
890 is_static_member = true;
891 scope = eValueTypeVariableStatic;
892 break;
893 case PDB_DataKind::Member:
894 scope = eValueTypeVariableStatic;
895 break;
896 case PDB_DataKind::Param:
897 scope = eValueTypeVariableArgument;
898 break;
899 case PDB_DataKind::Constant:
900 scope = eValueTypeConstResult;
901 break;
902 default:
903 break;
904 }
905
906 switch (pdb_data.getLocationType()) {
907 case PDB_LocType::TLS:
908 scope = eValueTypeVariableThreadLocal;
909 break;
910 case PDB_LocType::RegRel: {
911 // It is a `this` pointer.
912 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
913 scope = eValueTypeVariableArgument;
914 is_artificial = true;
915 }
916 } break;
917 default:
918 break;
919 }
920
921 Declaration decl;
922 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
923 if (auto lines = pdb_data.getLineNumbers()) {
924 if (auto first_line = lines->getNext()) {
925 uint32_t src_file_id = first_line->getSourceFileId();
926 auto src_file = m_session_up->getSourceFileById(src_file_id);
927 if (src_file) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000928 FileSpec spec(src_file->getFileName());
Aaron Smithcab0d232018-05-23 01:52:42 +0000929 decl.SetFile(spec);
930 decl.SetColumn(first_line->getColumnNumber());
931 decl.SetLine(first_line->getLineNumber());
932 }
933 }
934 }
935 }
936
937 Variable::RangeList ranges;
938 SymbolContextScope *context_scope = sc.comp_unit;
Aleksandr Urakov758657e2019-02-01 10:01:18 +0000939 if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000940 if (sc.function) {
Aleksandr Urakov758657e2019-02-01 10:01:18 +0000941 Block &function_block = sc.function->GetBlock(true);
942 Block *block =
943 function_block.FindBlockByID(pdb_data.getLexicalParentId());
944 if (!block)
945 block = &function_block;
946
947 context_scope = block;
948
949 for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges;
950 ++i) {
951 AddressRange range;
952 if (!block->GetRangeAtIndex(i, range))
953 continue;
954
955 ranges.Append(range.GetBaseAddress().GetFileAddress(),
956 range.GetByteSize());
957 }
Aaron Smithcab0d232018-05-23 01:52:42 +0000958 }
959 }
960
961 SymbolFileTypeSP type_sp =
962 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
963
964 auto var_name = pdb_data.getName();
965 auto mangled = GetMangledForPDBData(pdb_data);
966 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
967
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000968 bool is_constant;
969 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
Aleksandr Urakov758657e2019-02-01 10:01:18 +0000970 GetObjectFile()->GetModule(), pdb_data, ranges, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000971
972 var_sp = std::make_shared<Variable>(
973 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
974 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000975 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000976
977 m_variables.insert(std::make_pair(var_uid, var_sp));
978 return var_sp;
979}
980
981size_t
982SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
983 const llvm::pdb::PDBSymbol &pdb_symbol,
984 lldb_private::VariableList *variable_list) {
985 size_t num_added = 0;
986
987 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
988 VariableListSP local_variable_list_sp;
989
990 auto result = m_variables.find(pdb_data->getSymIndexId());
991 if (result != m_variables.end()) {
992 if (variable_list)
993 variable_list->AddVariableIfUnique(result->second);
994 } else {
995 // Prepare right VariableList for this variable.
996 if (auto lexical_parent = pdb_data->getLexicalParent()) {
997 switch (lexical_parent->getSymTag()) {
998 case PDB_SymType::Exe:
999 assert(sc.comp_unit);
1000 LLVM_FALLTHROUGH;
1001 case PDB_SymType::Compiland: {
1002 if (sc.comp_unit) {
1003 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1004 if (!local_variable_list_sp) {
1005 local_variable_list_sp = std::make_shared<VariableList>();
1006 sc.comp_unit->SetVariableList(local_variable_list_sp);
1007 }
1008 }
1009 } break;
1010 case PDB_SymType::Block:
1011 case PDB_SymType::Function: {
1012 if (sc.function) {
1013 Block *block = sc.function->GetBlock(true).FindBlockByID(
1014 lexical_parent->getSymIndexId());
1015 if (block) {
1016 local_variable_list_sp = block->GetBlockVariableList(false);
1017 if (!local_variable_list_sp) {
1018 local_variable_list_sp = std::make_shared<VariableList>();
1019 block->SetVariableList(local_variable_list_sp);
1020 }
1021 }
1022 }
1023 } break;
1024 default:
1025 break;
1026 }
1027 }
1028
1029 if (local_variable_list_sp) {
1030 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1031 local_variable_list_sp->AddVariableIfUnique(var_sp);
1032 if (variable_list)
1033 variable_list->AddVariableIfUnique(var_sp);
1034 ++num_added;
Zachary Turnerc68925a2019-01-09 21:20:44 +00001035 PDBASTParser *ast = GetPDBAstParser();
1036 if (ast)
1037 ast->GetDeclForSymbol(*pdb_data);
Aaron Smithcab0d232018-05-23 01:52:42 +00001038 }
1039 }
1040 }
1041 }
1042
1043 if (auto results = pdb_symbol.findAllChildren()) {
1044 while (auto result = results->getNext())
1045 num_added += ParseVariables(sc, *result, variable_list);
1046 }
1047
1048 return num_added;
1049}
1050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051uint32_t SymbolFilePDB::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001052 lldb_private::ConstString name,
Pavel Labath34cda142018-05-31 09:46:26 +00001053 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001055 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1056 return 0;
1057 if (name.IsEmpty())
1058 return 0;
1059
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001060 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001061 if (!results)
1062 return 0;
1063
1064 uint32_t matches = 0;
1065 size_t old_size = variables.GetSize();
1066 while (auto result = results->getNext()) {
1067 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1068 if (max_matches > 0 && matches >= max_matches)
1069 break;
1070
1071 SymbolContext sc;
1072 sc.module_sp = m_obj_file->GetModule();
1073 lldbassert(sc.module_sp.get());
1074
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001075 if (!name.GetStringRef().equals(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001076 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001077 continue;
1078
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001079 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1080 // FIXME: We are not able to determine the compile unit.
1081 if (sc.comp_unit == nullptr)
1082 continue;
1083
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001084 if (parent_decl_ctx && GetDeclContextContainingUID(
1085 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001086 continue;
1087
Aaron Smithcab0d232018-05-23 01:52:42 +00001088 ParseVariables(sc, *pdb_data, &variables);
1089 matches = variables.GetSize() - old_size;
1090 }
1091
1092 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093}
1094
1095uint32_t
1096SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001097 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001099 if (!regex.IsValid())
1100 return 0;
1101 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1102 if (!results)
1103 return 0;
1104
1105 uint32_t matches = 0;
1106 size_t old_size = variables.GetSize();
1107 while (auto pdb_data = results->getNext()) {
1108 if (max_matches > 0 && matches >= max_matches)
1109 break;
1110
1111 auto var_name = pdb_data->getName();
1112 if (var_name.empty())
1113 continue;
1114 if (!regex.Execute(var_name))
1115 continue;
1116 SymbolContext sc;
1117 sc.module_sp = m_obj_file->GetModule();
1118 lldbassert(sc.module_sp.get());
1119
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001120 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001121 // FIXME: We are not able to determine the compile unit.
1122 if (sc.comp_unit == nullptr)
1123 continue;
1124
1125 ParseVariables(sc, *pdb_data, &variables);
1126 matches = variables.GetSize() - old_size;
1127 }
1128
1129 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130}
1131
Aaron Smithe664b5d2018-03-19 21:14:19 +00001132bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001133 bool include_inlines,
1134 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001135 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001136 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001137 if (!sc.comp_unit)
1138 return false;
1139 sc.module_sp = sc.comp_unit->GetModule();
Zachary Turner863f8c12019-01-11 18:03:20 +00001140 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001141 if (!sc.function)
1142 return false;
1143
1144 sc_list.Append(sc);
1145 return true;
1146}
1147
1148bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1149 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001150 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001151 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1152 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001153 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001154}
1155
1156void SymbolFilePDB::CacheFunctionNames() {
1157 if (!m_func_full_names.IsEmpty())
1158 return;
1159
1160 std::map<uint64_t, uint32_t> addr_ids;
1161
1162 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1163 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001164 if (pdb_func_up->isCompilerGenerated())
1165 continue;
1166
Aaron Smith7ac1c782018-02-09 05:31:28 +00001167 auto name = pdb_func_up->getName();
1168 auto demangled_name = pdb_func_up->getUndecoratedName();
1169 if (name.empty() && demangled_name.empty())
1170 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001171
Aaron Smithf76fe682018-03-07 03:16:50 +00001172 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001173 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1174 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1175
1176 if (auto parent = pdb_func_up->getClassParent()) {
1177
1178 // PDB have symbols for class/struct methods or static methods in Enum
1179 // Class. We won't bother to check if the parent is UDT or Enum here.
1180 m_func_method_names.Append(ConstString(name), uid);
1181
Adrian Prantl05097242018-04-30 16:49:04 +00001182 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1183 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1184 // not have inforamtion of this, we extract base names and cache them
1185 // by our own effort.
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001186 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001187 if (!basename.empty())
1188 m_func_base_names.Append(ConstString(basename), uid);
1189 else {
1190 m_func_base_names.Append(ConstString(name), uid);
1191 }
1192
1193 if (!demangled_name.empty())
1194 m_func_full_names.Append(ConstString(demangled_name), uid);
1195
1196 } else {
1197 // Handle not-method symbols.
1198
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001199 // The function name might contain namespace, or its lexical scope.
1200 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1201 if (!basename.empty())
1202 m_func_base_names.Append(ConstString(basename), uid);
1203 else
1204 m_func_base_names.Append(ConstString(name), uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001205
1206 if (name == "main") {
1207 m_func_full_names.Append(ConstString(name), uid);
1208
1209 if (!demangled_name.empty() && name != demangled_name) {
1210 m_func_full_names.Append(ConstString(demangled_name), uid);
1211 m_func_base_names.Append(ConstString(demangled_name), uid);
1212 }
1213 } else if (!demangled_name.empty()) {
1214 m_func_full_names.Append(ConstString(demangled_name), uid);
1215 } else {
1216 m_func_full_names.Append(ConstString(name), uid);
1217 }
1218 }
1219 }
1220 }
1221
1222 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001223 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001224 while (auto pub_sym_up = results_up->getNext()) {
1225 if (!pub_sym_up->isFunction())
1226 continue;
1227 auto name = pub_sym_up->getName();
1228 if (name.empty())
1229 continue;
1230
1231 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001232 auto vm_addr = pub_sym_up->getVirtualAddress();
1233
1234 // PDB public symbol has mangled name for its associated function.
1235 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1236 // Cache mangled name.
1237 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1238 }
1239 }
1240 }
1241 }
1242 // Sort them before value searching is working properly
1243 m_func_full_names.Sort();
1244 m_func_full_names.SizeToFit();
1245 m_func_method_names.Sort();
1246 m_func_method_names.SizeToFit();
1247 m_func_base_names.Sort();
1248 m_func_base_names.SizeToFit();
1249}
1250
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251uint32_t SymbolFilePDB::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001252 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001254 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001256 if (!append)
1257 sc_list.Clear();
1258 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1259
1260 if (name_type_mask == eFunctionNameTypeNone)
1261 return 0;
1262 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1263 return 0;
1264 if (name.IsEmpty())
1265 return 0;
1266
1267 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001268 if (name_type_mask & eFunctionNameTypeFull ||
1269 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001270 name_type_mask & eFunctionNameTypeMethod) {
1271 CacheFunctionNames();
1272
1273 std::set<uint32_t> resolved_ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001274 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1275 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001276 std::vector<uint32_t> ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001277 if (!Names.GetValues(name, ids))
1278 return;
1279
1280 for (uint32_t id : ids) {
1281 if (resolved_ids.find(id) != resolved_ids.end())
1282 continue;
1283
1284 if (parent_decl_ctx &&
1285 GetDeclContextContainingUID(id) != *parent_decl_ctx)
1286 continue;
1287
1288 if (ResolveFunction(id, include_inlines, sc_list))
1289 resolved_ids.insert(id);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001290 }
1291 };
1292 if (name_type_mask & eFunctionNameTypeFull) {
1293 ResolveFn(m_func_full_names);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001294 ResolveFn(m_func_base_names);
1295 ResolveFn(m_func_method_names);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001296 }
1297 if (name_type_mask & eFunctionNameTypeBase) {
1298 ResolveFn(m_func_base_names);
1299 }
1300 if (name_type_mask & eFunctionNameTypeMethod) {
1301 ResolveFn(m_func_method_names);
1302 }
1303 }
1304 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305}
1306
1307uint32_t
1308SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1309 bool include_inlines, bool append,
1310 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001311 if (!append)
1312 sc_list.Clear();
1313 if (!regex.IsValid())
1314 return 0;
1315
1316 auto old_size = sc_list.GetSize();
1317 CacheFunctionNames();
1318
1319 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001320 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1321 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001322 std::vector<uint32_t> ids;
1323 if (Names.GetValues(regex, ids)) {
1324 for (auto id : ids) {
1325 if (resolved_ids.find(id) == resolved_ids.end())
1326 if (ResolveFunction(id, include_inlines, sc_list))
1327 resolved_ids.insert(id);
1328 }
1329 }
1330 };
1331 ResolveFn(m_func_full_names);
1332 ResolveFn(m_func_base_names);
1333
1334 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335}
1336
1337void SymbolFilePDB::GetMangledNamesForFunction(
1338 const std::string &scope_qualified_name,
1339 std::vector<lldb_private::ConstString> &mangled_names) {}
1340
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001341void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1342 std::set<lldb::addr_t> sym_addresses;
1343 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1344 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
1345
1346 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1347 if (!results)
1348 return;
1349
1350 auto section_list = m_obj_file->GetSectionList();
1351 if (!section_list)
1352 return;
1353
1354 while (auto pub_symbol = results->getNext()) {
Pavel Labath7db8b5c2019-02-13 07:17:24 +00001355 auto section_id = pub_symbol->getAddressSection();
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001356
Pavel Labath7db8b5c2019-02-13 07:17:24 +00001357 auto section = section_list->FindSectionByID(section_id);
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001358 if (!section)
1359 continue;
1360
1361 auto offset = pub_symbol->getAddressOffset();
1362
1363 auto file_addr = section->GetFileAddress() + offset;
1364 if (sym_addresses.find(file_addr) != sym_addresses.end())
1365 continue;
1366 sym_addresses.insert(file_addr);
1367
1368 auto size = pub_symbol->getLength();
1369 symtab.AddSymbol(
1370 Symbol(pub_symbol->getSymIndexId(), // symID
1371 pub_symbol->getName().c_str(), // name
1372 true, // name_is_mangled
1373 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1374 true, // external
1375 false, // is_debug
1376 false, // is_trampoline
1377 false, // is_artificial
1378 section, // section_sp
1379 offset, // value
1380 size, // size
1381 size != 0, // size_is_valid
1382 false, // contains_linker_annotations
1383 0 // flags
1384 ));
1385 }
1386
1387 symtab.CalculateSymbolSizes();
1388 symtab.Finalize();
1389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391uint32_t SymbolFilePDB::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001392 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1394 uint32_t max_matches,
1395 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1396 lldb_private::TypeMap &types) {
1397 if (!append)
1398 types.Clear();
1399 if (!name)
1400 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001401 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1402 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403
1404 searched_symbol_files.clear();
1405 searched_symbol_files.insert(this);
1406
Aaron Smith86e94342017-12-22 05:26:50 +00001407 // There is an assumption 'name' is not a regex
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001408 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001409
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410 return types.GetSize();
1411}
1412
Zachary Turner49110232018-11-05 17:40:28 +00001413void SymbolFilePDB::DumpClangAST(Stream &s) {
1414 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1415 auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1416 if (!clang)
1417 return;
1418 clang->Dump(s);
1419}
1420
Aaron Smithc8316ed2018-03-22 03:44:51 +00001421void SymbolFilePDB::FindTypesByRegex(
1422 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1423 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001425 // space as much as possible since this searches EVERYTHING in the PDB,
1426 // manually doing regex comparisons. PDB library isn't optimized for regex
1427 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001429 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1431 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 std::unique_ptr<IPDBEnumSymbols> results;
1433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 uint32_t matches = 0;
1435
1436 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001437 results = m_global_scope_up->findAllChildren(tag);
1438 if (!results)
1439 continue;
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 while (auto result = results->getNext()) {
1442 if (max_matches > 0 && matches >= max_matches)
1443 break;
1444
1445 std::string type_name;
1446 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1447 type_name = enum_type->getName();
1448 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001449 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 type_name = typedef_type->getName();
1451 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1452 type_name = class_type->getName();
1453 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001454 // We're looking only for types that have names. Skip symbols, as well
1455 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 continue;
1457 }
1458
Aaron Smith86e94342017-12-22 05:26:50 +00001459 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 continue;
1461
1462 // This should cause the type to get cached and stored in the `m_types`
1463 // lookup.
1464 if (!ResolveTypeUID(result->getSymIndexId()))
1465 continue;
1466
1467 auto iter = m_types.find(result->getSymIndexId());
1468 if (iter == m_types.end())
1469 continue;
1470 types.Insert(iter->second);
1471 ++matches;
1472 }
1473 }
1474}
1475
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001476void SymbolFilePDB::FindTypesByName(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001477 llvm::StringRef name,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001478 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1479 uint32_t max_matches, lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001481 if (name.empty())
1482 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001483 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001484 if (!results)
1485 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486
1487 uint32_t matches = 0;
1488
1489 while (auto result = results->getNext()) {
1490 if (max_matches > 0 && matches >= max_matches)
1491 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001492
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001493 if (MSVCUndecoratedNameParser::DropScope(
1494 result->getRawSymbol().getName()) != name)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001495 continue;
1496
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497 switch (result->getSymTag()) {
1498 case PDB_SymType::Enum:
1499 case PDB_SymType::UDT:
1500 case PDB_SymType::Typedef:
1501 break;
1502 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001503 // We're looking only for types that have names. Skip symbols, as well
1504 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 continue;
1506 }
1507
1508 // This should cause the type to get cached and stored in the `m_types`
1509 // lookup.
1510 if (!ResolveTypeUID(result->getSymIndexId()))
1511 continue;
1512
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001513 if (parent_decl_ctx && GetDeclContextContainingUID(
1514 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001515 continue;
1516
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 auto iter = m_types.find(result->getSymIndexId());
1518 if (iter == m_types.end())
1519 continue;
1520 types.Insert(iter->second);
1521 ++matches;
1522 }
1523}
1524
1525size_t SymbolFilePDB::FindTypes(
1526 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1527 lldb_private::TypeMap &types) {
1528 return 0;
1529}
1530
Aaron Smithec40f812018-01-23 20:35:19 +00001531lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1532 return m_obj_file->GetModule()->GetTypeList();
1533}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534
Aaron Smithc8316ed2018-03-22 03:44:51 +00001535void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1536 uint32_t type_mask,
1537 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001538 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001539 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001540 case PDB_SymType::ArrayType:
1541 can_parse = ((type_mask & eTypeClassArray) != 0);
1542 break;
1543 case PDB_SymType::BuiltinType:
1544 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1545 break;
1546 case PDB_SymType::Enum:
1547 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1548 break;
1549 case PDB_SymType::Function:
1550 case PDB_SymType::FunctionSig:
1551 can_parse = ((type_mask & eTypeClassFunction) != 0);
1552 break;
1553 case PDB_SymType::PointerType:
1554 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1555 eTypeClassMemberPointer)) != 0);
1556 break;
1557 case PDB_SymType::Typedef:
1558 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1559 break;
1560 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001561 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001562 assert(udt);
1563 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001564 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1565 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001566 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001567 default:
1568 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001569 }
1570
1571 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001572 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001573 auto result =
1574 std::find(type_collection.begin(), type_collection.end(), type);
1575 if (result == type_collection.end())
1576 type_collection.push_back(type);
1577 }
1578 }
1579
Aaron Smithe664b5d2018-03-19 21:14:19 +00001580 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001581 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001582 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001583}
1584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001586 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001588 TypeCollection type_collection;
1589 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001590 CompileUnit *cu =
1591 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001592 if (cu) {
1593 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001594 if (!compiland_up)
1595 return 0;
1596 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001597 } else {
1598 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1599 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001600 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001601 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1602 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001603 }
1604 }
1605 }
1606
1607 for (auto type : type_collection) {
1608 type->GetForwardCompilerType();
1609 type_list.Insert(type->shared_from_this());
1610 }
1611 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001612}
1613
1614lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1616 auto type_system =
1617 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1618 if (type_system)
1619 type_system->SetSymbolFile(this);
1620 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001621}
1622
Zachary Turnerc68925a2019-01-09 21:20:44 +00001623PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
1624 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1625 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1626 if (!clang_type_system)
1627 return nullptr;
1628
1629 return clang_type_system->GetPDBParser();
1630}
1631
1632
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001634 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001636 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1637 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1638 if (!clang_type_system)
1639 return CompilerDeclContext();
1640
1641 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1642 if (!pdb)
1643 return CompilerDeclContext();
1644
1645 clang::DeclContext *decl_context = nullptr;
1646 if (parent_decl_ctx)
1647 decl_context = static_cast<clang::DeclContext *>(
1648 parent_decl_ctx->GetOpaqueDeclContext());
1649
1650 auto namespace_decl =
1651 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1652 if (!namespace_decl)
1653 return CompilerDeclContext();
1654
1655 return CompilerDeclContext(type_system,
1656 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001657}
1658
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1660 static ConstString g_name("pdb");
1661 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001662}
1663
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1665
1666IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1667
1668const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1669 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001670}
1671
Aaron Smithc8316ed2018-03-22 03:44:51 +00001672lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1673 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674 auto found_cu = m_comp_units.find(id);
1675 if (found_cu != m_comp_units.end())
1676 return found_cu->second;
1677
Aaron Smith10a02572018-01-13 06:58:18 +00001678 auto compiland_up = GetPDBCompilandByUID(id);
1679 if (!compiland_up)
1680 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681
1682 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001683 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 if (!details)
1685 lang = lldb::eLanguageTypeC_plus_plus;
1686 else
1687 lang = TranslateLanguage(details->getLanguage());
1688
Aaron Smithf76fe682018-03-07 03:16:50 +00001689 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1690 return CompUnitSP();
1691
Aaron Smith487b0c62018-03-20 00:18:22 +00001692 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001693 if (path.empty())
1694 return CompUnitSP();
1695
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 // Don't support optimized code for now, DebugInfoPDB does not return this
1697 // information.
1698 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001699 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1700 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001701
1702 if (!cu_sp)
1703 return CompUnitSP();
1704
1705 m_comp_units.insert(std::make_pair(id, cu_sp));
1706 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001707 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001708 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001709 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1710 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001711 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001712}
1713
Zachary Turner863f8c12019-01-11 18:03:20 +00001714bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1715 uint32_t match_line) {
1716 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +00001717 if (!compiland_up)
1718 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001719
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001721 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001722 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1723 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001725 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Zachary Turner863f8c12019-01-11 18:03:20 +00001726 auto line_table = llvm::make_unique<LineTable>(&comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001727
Aaron Smith10a02572018-01-13 06:58:18 +00001728 // Find contributions to `compiland` from all source and header files.
Zachary Turner863f8c12019-01-11 18:03:20 +00001729 std::string path = comp_unit.GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001730 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1731 if (!files)
1732 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001733
Adrian Prantl05097242018-04-30 16:49:04 +00001734 // For each source and header file, create a LineSequence for contributions
1735 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 while (auto file = files->getNext()) {
1737 std::unique_ptr<LineSequence> sequence(
1738 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001739 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1740 if (!lines)
1741 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001743
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 uint64_t prev_addr;
1745 uint32_t prev_length;
1746 uint32_t prev_line;
1747 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001748
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 for (int i = 0; i < entry_count; ++i) {
1750 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001751
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 uint64_t lno = line->getLineNumber();
1753 uint64_t addr = line->getVirtualAddress();
1754 uint32_t length = line->getLength();
1755 uint32_t source_id = line->getSourceFileId();
1756 uint32_t col = line->getColumnNumber();
1757 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001758
Adrian Prantl05097242018-04-30 16:49:04 +00001759 // There was a gap between the current entry and the previous entry if
1760 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001764 // of the previous entry's address range if the current entry resulted in
1765 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001766 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1767 line_table->AppendLineEntryToSequence(
1768 sequence.get(), prev_addr + prev_length, prev_line, 0,
1769 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001770
1771 line_table->InsertSequence(sequence.release());
1772 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001774
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 if (ShouldAddLine(match_line, lno, length)) {
1776 bool is_statement = line->isStatement();
1777 bool is_prologue = false;
1778 bool is_epilogue = false;
1779 auto func =
1780 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1781 if (func) {
1782 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001783 if (prologue)
1784 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001785
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001787 if (epilogue)
1788 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001789 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1792 source_idx, is_statement, false,
1793 is_prologue, is_epilogue, false);
1794 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001795
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 prev_addr = addr;
1797 prev_length = length;
1798 prev_line = lno;
1799 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001800 }
1801
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1803 // The end is always a terminal entry, so insert it regardless.
1804 line_table->AppendLineEntryToSequence(
1805 sequence.get(), prev_addr + prev_length, prev_line, 0,
1806 prev_source_idx, false, false, false, false, true);
1807 }
1808
1809 line_table->InsertSequence(sequence.release());
1810 }
1811
Aaron Smith10a02572018-01-13 06:58:18 +00001812 if (line_table->GetSize()) {
Zachary Turner863f8c12019-01-11 18:03:20 +00001813 comp_unit.SetLineTable(line_table.release());
Aaron Smith10a02572018-01-13 06:58:18 +00001814 return true;
1815 }
1816 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001817}
1818
Kate Stoneb9c1b512016-09-06 20:57:50 +00001819void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001820 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001822 // This is a hack, but we need to convert the source id into an index into
1823 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001824 // basename / full path issues that may or may not even be a problem, so we
1825 // use the globally unique source file identifiers. Ideally we could use the
1826 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001827 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1828 if (!source_files)
1829 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001830
1831 // LLDB uses the DWARF-like file numeration (one based)
1832 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001833
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834 while (auto file = source_files->getNext()) {
1835 uint32_t source_id = file->getUniqueId();
1836 index_map[source_id] = index++;
1837 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001838}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001839
1840lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001841 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001842 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001843 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001844 return nullptr;
1845
Aaron Smith308e39c2018-03-22 19:26:33 +00001846 // If it is a PDB function's vm addr, this is the first sure bet.
1847 if (auto lines =
1848 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1849 if (auto first_line = lines->getNext())
1850 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001851 }
1852
Aaron Smith308e39c2018-03-22 19:26:33 +00001853 // Otherwise we resort to section contributions.
1854 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1855 while (auto section = sec_contribs->getNext()) {
1856 auto va = section->getVirtualAddress();
1857 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1858 return ParseCompileUnitForUID(section->getCompilandId());
1859 }
1860 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001861 return nullptr;
1862}
1863
1864Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001865SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001866 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001867 auto func_name = pdb_func.getName();
1868 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001869 std::string func_decorated_name;
1870
1871 // Seek from public symbols for non-static function's decorated name if any.
1872 // For static functions, they don't have undecorated names and aren't exposed
1873 // in Public Symbols either.
1874 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001875 auto result_up = m_global_scope_up->findChildren(
1876 PDB_SymType::PublicSymbol, func_undecorated_name,
1877 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001878 if (result_up) {
1879 while (auto symbol_up = result_up->getNext()) {
1880 // For a public symbol, it is unique.
1881 lldbassert(result_up->getChildCount() == 1);
1882 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001883 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1884 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001885 if (pdb_public_sym->isFunction()) {
1886 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001887 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001888 }
1889 }
1890 }
1891 }
1892 }
1893 if (!func_decorated_name.empty()) {
1894 mangled.SetMangledName(ConstString(func_decorated_name));
1895
1896 // For MSVC, format of C funciton's decorated name depends on calling
1897 // conventon. Unfortunately none of the format is recognized by current
1898 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001899 // `__purecall` is retrieved as both its decorated and undecorated name
1900 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1901 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1902 // Mangled::GetDemangledName method will fail internally and caches an
1903 // empty string as its undecorated name. So we will face a contradition
1904 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001905 // non-empty undecorated name from PDB
1906 // empty undecorated name from LLDB
1907 if (!func_undecorated_name.empty() &&
1908 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1909 mangled.SetDemangledName(ConstString(func_undecorated_name));
1910
1911 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001912 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1913 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001914 // PDB source unless we also apply same flags in getting undecorated
1915 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1916 if (!func_undecorated_name.empty() &&
1917 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1918 ConstString(func_undecorated_name))
1919 mangled.SetDemangledName(ConstString(func_undecorated_name));
1920 } else if (!func_undecorated_name.empty()) {
1921 mangled.SetDemangledName(ConstString(func_undecorated_name));
1922 } else if (!func_name.empty())
1923 mangled.SetValue(ConstString(func_name), false);
1924
1925 return mangled;
1926}
1927
1928bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1929 const lldb_private::CompilerDeclContext *decl_ctx) {
1930 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1931 return true;
1932
1933 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1934 if (!decl_ctx_type_system)
1935 return false;
1936 TypeSystem *type_system = GetTypeSystemForLanguage(
1937 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1938 if (decl_ctx_type_system == type_system)
1939 return true; // The type systems match, return true
1940
1941 return false;
1942}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001943
1944uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1945 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1946 return lhs < rhs.Offset;
1947 };
1948
1949 // Cache section contributions
1950 if (m_sec_contribs.empty()) {
1951 if (auto SecContribs = m_session_up->getSectionContribs()) {
1952 while (auto SectionContrib = SecContribs->getNext()) {
1953 auto comp_id = SectionContrib->getCompilandId();
1954 if (!comp_id)
1955 continue;
1956
1957 auto sec = SectionContrib->getAddressSection();
1958 auto &sec_cs = m_sec_contribs[sec];
1959
1960 auto offset = SectionContrib->getAddressOffset();
1961 auto it =
1962 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
1963
1964 auto size = SectionContrib->getLength();
1965 sec_cs.insert(it, {offset, size, comp_id});
1966 }
1967 }
1968 }
1969
1970 // Check by line number
1971 if (auto Lines = data.getLineNumbers()) {
1972 if (auto FirstLine = Lines->getNext())
1973 return FirstLine->getCompilandId();
1974 }
1975
1976 // Retrieve section + offset
1977 uint32_t DataSection = data.getAddressSection();
1978 uint32_t DataOffset = data.getAddressOffset();
1979 if (DataSection == 0) {
1980 if (auto RVA = data.getRelativeVirtualAddress())
1981 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
1982 }
1983
1984 if (DataSection) {
1985 // Search by section contributions
1986 auto &sec_cs = m_sec_contribs[DataSection];
1987 auto it =
1988 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
1989 if (it != sec_cs.begin()) {
1990 --it;
1991 if (DataOffset < it->Offset + it->Size)
1992 return it->CompilandId;
1993 }
1994 } else {
1995 // Search in lexical tree
1996 auto LexParentId = data.getLexicalParentId();
1997 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
1998 if (LexParent->getSymTag() == PDB_SymType::Exe)
1999 break;
2000 if (LexParent->getSymTag() == PDB_SymType::Compiland)
2001 return LexParentId;
2002 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
2003 }
2004 }
2005
2006 return 0;
2007}