blob: 664c142d7cea0df7ee36f791484c779e2b3fd262 [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"
Alex Langford0e252e32019-07-30 22:12:34 +000027#include "lldb/Utility/Log.h"
Aaron Smith86e94342017-12-22 05:26:50 +000028#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000029
Pavel Labathb8d8c622016-05-09 11:07:43 +000030#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000031#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
33#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000034#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000035#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000036#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000037#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000038#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000039#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000041#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000042#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000046#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000047#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
48#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
49#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
50
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000051#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000052#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000053#include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
Zachary Turner42dff792016-04-15 00:21:26 +000054
55#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000056
Aaron Smith10a02572018-01-13 06:58:18 +000057using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000058using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000059using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061namespace {
62lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
63 switch (lang) {
64 case PDB_Lang::Cpp:
65 return lldb::LanguageType::eLanguageTypeC_plus_plus;
66 case PDB_Lang::C:
67 return lldb::LanguageType::eLanguageTypeC;
Nathan Lanza0561be62019-03-11 23:30:58 +000068 case PDB_Lang::Swift:
69 return lldb::LanguageType::eLanguageTypeSwift;
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 default:
71 return lldb::LanguageType::eLanguageTypeUnknown;
72 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
76 uint32_t addr_length) {
77 return ((requested_line == 0 || actual_line == requested_line) &&
78 addr_length > 0);
79}
Aaron Smithc8316ed2018-03-22 03:44:51 +000080} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000081
Zachary Turner307f5ae2018-10-12 19:47:13 +000082static bool ShouldUseNativeReader() {
Greg Clayton71970b72018-12-12 18:14:27 +000083#if defined(_WIN32)
Zachary Turner307f5ae2018-10-12 19:47:13 +000084 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
85 return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
86 use_native.equals_lower("1") || use_native.equals_lower("true");
Greg Clayton71970b72018-12-12 18:14:27 +000087#else
88 return true;
89#endif
Zachary Turner307f5ae2018-10-12 19:47:13 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000093 if (ShouldUseNativeReader()) {
94 npdb::SymbolFileNativePDB::Initialize();
95 } else {
96 PluginManager::RegisterPlugin(GetPluginNameStatic(),
97 GetPluginDescriptionStatic(), CreateInstance,
98 DebuggerInitialize);
99 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000103 if (ShouldUseNativeReader()) {
104 npdb::SymbolFileNativePDB::Terminate();
105 } else {
106 PluginManager::UnregisterPlugin(CreateInstance);
107 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
111
112lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
113 static ConstString g_name("pdb");
114 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117const char *SymbolFilePDB::GetPluginDescriptionStatic() {
118 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000119}
120
121lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
123 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000124}
125
126SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Pavel Labathe0119902019-07-23 09:24:02 +0000127 : SymbolFile(object_file), m_session_up(), m_global_scope_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 Turner74e08ca2016-03-02 22:05:52 +0000192}
193
Pavel Labathe0119902019-07-23 09:24:02 +0000194uint32_t SymbolFilePDB::CalculateNumCompileUnits() {
195 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
196 if (!compilands)
197 return 0;
Aaron Smith10a02572018-01-13 06:58:18 +0000198
Pavel Labathe0119902019-07-23 09:24:02 +0000199 // The linker could link *.dll (compiland language = LINK), or import
200 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
201 // found as a child of the global scope (PDB executable). Usually, such
202 // compilands contain `thunk` symbols in which we are not interested for
203 // now. However we still count them in the compiland list. If we perform
204 // any compiland related activity, like finding symbols through
205 // llvm::pdb::IPDBSession methods, such compilands will all be searched
206 // automatically no matter whether we include them or not.
207 uint32_t compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000208
Pavel Labathe0119902019-07-23 09:24:02 +0000209 // The linker can inject an additional "dummy" compilation unit into the
210 // PDB. Ignore this special compile unit for our purposes, if it is there.
211 // It is always the last one.
212 auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1);
213 lldbassert(last_compiland_up.get());
214 std::string name = last_compiland_up->getName();
215 if (name == "* Linker *")
216 --compile_unit_count;
217 return compile_unit_count;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000219
Aaron Smith10a02572018-01-13 06:58:18 +0000220void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000221 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000222 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
223 if (!results_up)
224 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000225 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000226 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000227 auto compiland_up = results_up->getChildAtIndex(cu_idx);
228 if (!compiland_up)
229 continue;
230 if (compiland_up->getSymIndexId() == uid) {
231 index = cu_idx;
232 return;
233 }
234 }
235 index = UINT32_MAX;
236 return;
237}
238
239std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
240SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
241 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000245 if (index >= GetNumCompileUnits())
246 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247
Aaron Smith10a02572018-01-13 06:58:18 +0000248 // Assuming we always retrieve same compilands listed in same order through
249 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
250 // compile unit makes no sense.
251 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
252 if (!results)
253 return CompUnitSP();
254 auto compiland_up = results->getChildAtIndex(index);
255 if (!compiland_up)
256 return CompUnitSP();
257 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000258}
259
Zachary Turner863f8c12019-01-11 18:03:20 +0000260lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000261 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000262 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000263 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000265 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 if (!details)
267 return lldb::eLanguageTypeUnknown;
268 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000269}
270
Zachary Turner863f8c12019-01-11 18:03:20 +0000271lldb_private::Function *
272SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
273 CompileUnit &comp_unit) {
274 if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId()))
Aleksandr Urakova5235af2018-12-03 13:31:13 +0000275 return result.get();
276
Aaron Smithe664b5d2018-03-19 21:14:19 +0000277 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000278 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000279 return nullptr;
280
Aaron Smithe664b5d2018-03-19 21:14:19 +0000281 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000282 AddressRange func_range =
Zachary Turner863f8c12019-01-11 18:03:20 +0000283 AddressRange(file_vm_addr, func_length,
284 GetObjectFile()->GetModule()->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000285 if (!func_range.GetBaseAddress().IsValid())
286 return nullptr;
287
Aaron Smithc8316ed2018-03-22 03:44:51 +0000288 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000289 if (!func_type)
290 return nullptr;
291
Aaron Smithe664b5d2018-03-19 21:14:19 +0000292 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000293
Aaron Smith7ac1c782018-02-09 05:31:28 +0000294 Mangled mangled = GetMangledForPDBFunc(pdb_func);
295
Aaron Smithc8316ed2018-03-22 03:44:51 +0000296 FunctionSP func_sp =
Zachary Turner863f8c12019-01-11 18:03:20 +0000297 std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(),
Aaron Smithc8316ed2018-03-22 03:44:51 +0000298 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000299
Zachary Turner863f8c12019-01-11 18:03:20 +0000300 comp_unit.AddFunction(func_sp);
Zachary Turnerc68925a2019-01-09 21:20:44 +0000301
Nathan Lanzad0050d12019-03-27 01:24:03 +0000302 LanguageType lang = ParseLanguage(comp_unit);
Alex Langford0e252e32019-07-30 22:12:34 +0000303 auto type_system_or_err = GetTypeSystemForLanguage(lang);
304 if (auto err = type_system_or_err.takeError()) {
305 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
306 std::move(err), "Unable to parse PDBFunc");
Zachary Turnerc68925a2019-01-09 21:20:44 +0000307 return nullptr;
Alex Langford0e252e32019-07-30 22:12:34 +0000308 }
309
Zachary Turnerc68925a2019-01-09 21:20:44 +0000310 ClangASTContext *clang_type_system =
Alex Langford0e252e32019-07-30 22:12:34 +0000311 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Zachary Turnerc68925a2019-01-09 21:20:44 +0000312 if (!clang_type_system)
313 return nullptr;
314 clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func);
315
Aaron Smith7ac1c782018-02-09 05:31:28 +0000316 return func_sp.get();
317}
318
Zachary Turner863f8c12019-01-11 18:03:20 +0000319size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000320 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000321 size_t func_added = 0;
Zachary Turner863f8c12019-01-11 18:03:20 +0000322 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000323 if (!compiland_up)
324 return 0;
325 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
326 if (!results_up)
327 return 0;
328 while (auto pdb_func_up = results_up->getNext()) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000329 auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000330 if (!func_sp) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000331 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000332 ++func_added;
333 }
334 }
335 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000336}
337
Zachary Turner863f8c12019-01-11 18:03:20 +0000338bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000339 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000340 if (comp_unit.GetLineTable())
Aaron Smith10a02572018-01-13 06:58:18 +0000341 return true;
Zachary Turner863f8c12019-01-11 18:03:20 +0000342 return ParseCompileUnitLineTable(comp_unit, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000343}
344
Zachary Turner863f8c12019-01-11 18:03:20 +0000345bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 // PDB doesn't contain information about macros
347 return false;
348}
349
Zachary Turner863f8c12019-01-11 18:03:20 +0000350bool SymbolFilePDB::ParseSupportFiles(
351 CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) {
Zachary Turner74e08ca2016-03-02 22:05:52 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000354 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
355 // second time seems like a waste. Unfortunately, there's no good way around
356 // this short of a moderate refactor since SymbolVendor depends on being able
357 // to cache this list.
Pavel Labath656ddeb2019-07-30 08:20:05 +0000358 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Zachary Turner863f8c12019-01-11 18:03:20 +0000359 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000360 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000361 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000362 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000364 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365
366 while (auto file = files->getNext()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000367 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000368 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000370
371 // LLDB uses the DWARF-like file numeration (one based),
372 // the zeroth file is the compile unit itself
Zachary Turner863f8c12019-01-11 18:03:20 +0000373 support_files.Insert(0, comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000376}
377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378bool SymbolFilePDB::ParseImportedModules(
379 const lldb_private::SymbolContext &sc,
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000380 std::vector<SourceModule> &imported_modules) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 // PDB does not yet support module debug info
382 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000383}
384
Aaron Smithc8316ed2018-03-22 03:44:51 +0000385static size_t ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000386 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
387 lldb_private::Block *parent_block, bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000388 assert(pdb_symbol && parent_block);
389
390 size_t num_added = 0;
391 switch (pdb_symbol->getSymTag()) {
392 case PDB_SymType::Block:
393 case PDB_SymType::Function: {
394 Block *block = nullptr;
395 auto &raw_sym = pdb_symbol->getRawSymbol();
396 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
397 if (pdb_func->hasNoInlineAttribute())
398 break;
399 if (is_top_parent)
400 block = parent_block;
401 else
402 break;
403 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
404 auto uid = pdb_symbol->getSymIndexId();
405 if (parent_block->FindBlockByID(uid))
406 break;
407 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
408 break;
409
410 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
411 parent_block->AddChild(block_sp);
412 block = block_sp.get();
413 } else
414 llvm_unreachable("Unexpected PDB symbol!");
415
Aaron Smithc8316ed2018-03-22 03:44:51 +0000416 block->AddRange(Block::Range(
417 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000418 block->FinalizeRanges();
419 ++num_added;
420
421 auto results_up = pdb_symbol->findAllChildren();
422 if (!results_up)
423 break;
424 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000425 num_added += ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000426 func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000427 }
428 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000429 default:
430 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000431 }
432 return num_added;
433}
434
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000435size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000436 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000437 size_t num_added = 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000438 auto uid = func.GetID();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000439 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
440 if (!pdb_func_up)
441 return 0;
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000442 Block &parent_block = func.GetBlock(false);
443 num_added = ParseFunctionBlocksForPDBSymbol(
444 pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000445 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000446}
447
Zachary Turner863f8c12019-01-11 18:03:20 +0000448size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000449 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith66b84072018-03-14 04:05:27 +0000450
451 size_t num_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000452 auto compiland = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000453 if (!compiland)
454 return 0;
455
456 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
457 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000458 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
459 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000460 for (auto tag : tags_to_search) {
461 results = raw_sym.findAllChildren(tag);
462 if (!results || results->getChildCount() == 0)
463 continue;
464 while (auto symbol = results->getNext()) {
465 switch (symbol->getSymTag()) {
466 case PDB_SymType::Enum:
467 case PDB_SymType::UDT:
468 case PDB_SymType::Typedef:
469 break;
470 default:
471 continue;
472 }
473
474 // This should cause the type to get cached and stored in the `m_types`
475 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000476 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
477 // Resolve the type completely to avoid a completion
478 // (and so a list change, which causes an iterators invalidation)
479 // during a TypeList dumping
480 type->GetFullCompilerType();
481 ++num_added;
482 }
Aaron Smith66b84072018-03-14 04:05:27 +0000483 }
Aaron Smithec40f812018-01-23 20:35:19 +0000484 }
Aaron Smith66b84072018-03-14 04:05:27 +0000485 };
Aaron Smithec40f812018-01-23 20:35:19 +0000486
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000487 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000488
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000489 // Also parse global types particularly coming from this compiland.
490 // Unfortunately, PDB has no compiland information for each global type. We
491 // have to parse them all. But ensure we only do this once.
492 static bool parse_all_global_types = false;
493 if (!parse_all_global_types) {
494 ParseTypesByTagFn(*m_global_scope_up);
495 parse_all_global_types = true;
Aaron Smithec40f812018-01-23 20:35:19 +0000496 }
497 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498}
499
500size_t
501SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000502 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
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) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000551 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 auto find_result = m_types.find(type_uid);
553 if (find_result != m_types.end())
554 return find_result->second.get();
555
Alex Langford0e252e32019-07-30 22:12:34 +0000556 auto type_system_or_err =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
Alex Langford0e252e32019-07-30 22:12:34 +0000558 if (auto err = type_system_or_err.takeError()) {
559 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
560 std::move(err), "Unable to ResolveTypeUID");
561 return nullptr;
562 }
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 ClangASTContext *clang_type_system =
Alex Langford0e252e32019-07-30 22:12:34 +0000565 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000567 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000568 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 if (!pdb)
570 return nullptr;
571
572 auto pdb_type = m_session_up->getSymbolById(type_uid);
573 if (pdb_type == nullptr)
574 return nullptr;
575
576 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000577 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000578 m_types.insert(std::make_pair(type_uid, result));
Pavel Labathf46e8972019-07-25 08:22:05 +0000579 GetTypeList().Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000582}
583
Adrian Prantleca07c52018-11-05 20:49:07 +0000584llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
585 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
586 return llvm::None;
587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000590 std::lock_guard<std::recursive_mutex> guard(
591 GetObjectFile()->GetModule()->GetMutex());
592
Alex Langford0e252e32019-07-30 22:12:34 +0000593 auto type_system_or_err =
594 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
595 if (auto err = type_system_or_err.takeError()) {
596 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
597 std::move(err), "Unable to get dynamic array info for UID");
598 return false;
599 }
600
601 ClangASTContext *clang_ast_ctx =
602 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
603
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000604 if (!clang_ast_ctx)
605 return false;
606
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000607 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000608 if (!pdb)
609 return false;
610
611 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612}
613
614lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Alex Langford0e252e32019-07-30 22:12:34 +0000615 auto type_system_or_err =
616 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
617 if (auto err = type_system_or_err.takeError()) {
618 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
619 std::move(err), "Unable to get decl for UID");
620 return CompilerDecl();
621 }
622
623 ClangASTContext *clang_ast_ctx =
624 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000625 if (!clang_ast_ctx)
626 return CompilerDecl();
627
628 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
629 if (!pdb)
630 return CompilerDecl();
631
632 auto symbol = m_session_up->getSymbolById(uid);
633 if (!symbol)
634 return CompilerDecl();
635
636 auto decl = pdb->GetDeclForSymbol(*symbol);
637 if (!decl)
638 return CompilerDecl();
639
640 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641}
642
643lldb_private::CompilerDeclContext
644SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Alex Langford0e252e32019-07-30 22:12:34 +0000645 auto type_system_or_err =
646 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
647 if (auto err = type_system_or_err.takeError()) {
648 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
649 std::move(err), "Unable to get DeclContext for UID");
650 return CompilerDeclContext();
651 }
652
653 ClangASTContext *clang_ast_ctx =
654 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000655 if (!clang_ast_ctx)
656 return CompilerDeclContext();
657
658 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
659 if (!pdb)
660 return CompilerDeclContext();
661
662 auto symbol = m_session_up->getSymbolById(uid);
663 if (!symbol)
664 return CompilerDeclContext();
665
666 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
667 if (!decl_context)
668 return GetDeclContextContainingUID(uid);
669
670 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671}
672
673lldb_private::CompilerDeclContext
674SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Alex Langford0e252e32019-07-30 22:12:34 +0000675 auto type_system_or_err =
676 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
677 if (auto err = type_system_or_err.takeError()) {
678 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
679 std::move(err), "Unable to get DeclContext containing UID");
680 return CompilerDeclContext();
681 }
682
683 ClangASTContext *clang_ast_ctx =
684 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000685 if (!clang_ast_ctx)
686 return CompilerDeclContext();
687
688 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
689 if (!pdb)
690 return CompilerDeclContext();
691
692 auto symbol = m_session_up->getSymbolById(uid);
693 if (!symbol)
694 return CompilerDeclContext();
695
696 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
697 assert(decl_context);
698
699 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700}
701
702void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000703 lldb_private::CompilerDeclContext decl_ctx) {
Alex Langford0e252e32019-07-30 22:12:34 +0000704 auto type_system_or_err =
705 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
706 if (auto err = type_system_or_err.takeError()) {
707 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
708 std::move(err), "Unable to parse decls for context");
709 return;
710 }
711
712 ClangASTContext *clang_ast_ctx =
713 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000714 if (!clang_ast_ctx)
715 return;
716
717 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
718 if (!pdb)
719 return;
720
721 pdb->ParseDeclsForDeclContext(
722 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
723}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724
725uint32_t
726SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000727 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 lldb_private::SymbolContext &sc) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000729 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000730 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000731 if (resolve_scope & eSymbolContextCompUnit ||
732 resolve_scope & eSymbolContextVariable ||
733 resolve_scope & eSymbolContextFunction ||
734 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000735 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000736 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
737 if (!cu_sp) {
738 if (resolved_flags | eSymbolContextVariable) {
739 // TODO: Resolve variables
740 }
741 return 0;
742 }
743 sc.comp_unit = cu_sp.get();
744 resolved_flags |= eSymbolContextCompUnit;
745 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000746 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000747
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000748 if (resolve_scope & eSymbolContextFunction ||
749 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000750 addr_t file_vm_addr = so_addr.GetFileAddress();
751 auto symbol_up =
752 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
753 if (symbol_up) {
754 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
755 assert(pdb_func);
756 auto func_uid = pdb_func->getSymIndexId();
757 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
758 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +0000759 sc.function =
760 ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000761 if (sc.function) {
762 resolved_flags |= eSymbolContextFunction;
763 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000764 auto block_symbol = m_session_up->findSymbolByAddress(
765 file_vm_addr, PDB_SymType::Block);
766 auto block_id = block_symbol ? block_symbol->getSymIndexId()
767 : sc.function->GetID();
768 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000769 if (sc.block)
770 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000771 }
772 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000773 }
774 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000775
776 if (resolve_scope & eSymbolContextLineEntry) {
777 if (auto *line_table = sc.comp_unit->GetLineTable()) {
778 Address addr(so_addr);
779 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
780 resolved_flags |= eSymbolContextLineEntry;
781 }
782 }
783
Aaron Smith7ac1c782018-02-09 05:31:28 +0000784 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785}
786
787uint32_t SymbolFilePDB::ResolveSymbolContext(
788 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000789 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000790 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith10a02572018-01-13 06:58:18 +0000791 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 if (resolve_scope & lldb::eSymbolContextCompUnit) {
793 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000794 // file. For example, if `file_spec` is <vector>, then this should return
795 // all source files and header files that reference <vector>, either
796 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 auto compilands = m_session_up->findCompilandsForSourceFile(
798 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
799
Aaron Smith10a02572018-01-13 06:58:18 +0000800 if (!compilands)
801 return 0;
802
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000803 // For each one, either find its previously parsed data or parse it afresh
804 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000806 // If we're not checking inlines, then don't add line information for
807 // this file unless the FileSpec matches. For inline functions, we don't
808 // have to match the FileSpec since they could be defined in headers
809 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000811 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000812 if (source_file.empty())
813 continue;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000814 FileSpec this_spec(source_file, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000815 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
816 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 continue;
818 }
819
820 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000821 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000822 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000823 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 sc.comp_unit = cu.get();
825 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826
827 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000828 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000829 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
830 eSymbolContextLineEntry)) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000831 bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000832
833 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
834 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000835 // compile unit. This is not normal for `line` = 0. So just assert
836 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000837 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000838
839 // Current compiland does not have the requested line. Search next.
840 continue;
841 }
842
843 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
844 if (!has_line_table)
845 continue;
846
847 auto *line_table = sc.comp_unit->GetLineTable();
848 lldbassert(line_table);
849
850 uint32_t num_line_entries = line_table->GetSize();
851 // Skip the terminal line entry.
852 --num_line_entries;
853
Adrian Prantl05097242018-04-30 16:49:04 +0000854 // If `line `!= 0, see if we can resolve function for each line entry
855 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000856 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
857 ++line_idx) {
858 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
859 continue;
860
861 auto file_vm_addr =
862 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000863 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000864 continue;
865
Aaron Smithc8316ed2018-03-22 03:44:51 +0000866 auto symbol_up = m_session_up->findSymbolByAddress(
867 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000868 if (symbol_up) {
869 auto func_uid = symbol_up->getSymIndexId();
870 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
871 if (sc.function == nullptr) {
872 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
873 assert(pdb_func);
Zachary Turner863f8c12019-01-11 18:03:20 +0000874 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func,
875 *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000876 }
877 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
878 Block &block = sc.function->GetBlock(true);
879 sc.block = block.FindBlockByID(sc.function->GetID());
880 }
881 }
882 sc_list.Append(sc);
883 }
884 } else if (has_line_table) {
885 // We can parse line table for the compile unit. But no query to
886 // resolve function or block. We append `sc` to the list anyway.
887 sc_list.Append(sc);
888 }
889 } else {
890 // No query for line entry, function or block. But we have a valid
891 // compile unit, append `sc` to the list.
892 sc_list.Append(sc);
893 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 }
895 }
Aaron Smith10a02572018-01-13 06:58:18 +0000896 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897}
898
Aaron Smithcab0d232018-05-23 01:52:42 +0000899std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000900 // Cache public names at first
901 if (m_public_names.empty())
902 if (auto result_up =
903 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
904 while (auto symbol_up = result_up->getNext())
905 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
906 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000907
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000908 // Look up the name in the cache
909 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000910}
911
912VariableSP SymbolFilePDB::ParseVariableForPDBData(
913 const lldb_private::SymbolContext &sc,
914 const llvm::pdb::PDBSymbolData &pdb_data) {
915 VariableSP var_sp;
916 uint32_t var_uid = pdb_data.getSymIndexId();
917 auto result = m_variables.find(var_uid);
918 if (result != m_variables.end())
919 return result->second;
920
921 ValueType scope = eValueTypeInvalid;
922 bool is_static_member = false;
923 bool is_external = false;
924 bool is_artificial = false;
925
926 switch (pdb_data.getDataKind()) {
927 case PDB_DataKind::Global:
928 scope = eValueTypeVariableGlobal;
929 is_external = true;
930 break;
931 case PDB_DataKind::Local:
932 scope = eValueTypeVariableLocal;
933 break;
934 case PDB_DataKind::FileStatic:
935 scope = eValueTypeVariableStatic;
936 break;
937 case PDB_DataKind::StaticMember:
938 is_static_member = true;
939 scope = eValueTypeVariableStatic;
940 break;
941 case PDB_DataKind::Member:
942 scope = eValueTypeVariableStatic;
943 break;
944 case PDB_DataKind::Param:
945 scope = eValueTypeVariableArgument;
946 break;
947 case PDB_DataKind::Constant:
948 scope = eValueTypeConstResult;
949 break;
950 default:
951 break;
952 }
953
954 switch (pdb_data.getLocationType()) {
955 case PDB_LocType::TLS:
956 scope = eValueTypeVariableThreadLocal;
957 break;
958 case PDB_LocType::RegRel: {
959 // It is a `this` pointer.
960 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
961 scope = eValueTypeVariableArgument;
962 is_artificial = true;
963 }
964 } break;
965 default:
966 break;
967 }
968
969 Declaration decl;
970 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
971 if (auto lines = pdb_data.getLineNumbers()) {
972 if (auto first_line = lines->getNext()) {
973 uint32_t src_file_id = first_line->getSourceFileId();
974 auto src_file = m_session_up->getSourceFileById(src_file_id);
975 if (src_file) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000976 FileSpec spec(src_file->getFileName());
Aaron Smithcab0d232018-05-23 01:52:42 +0000977 decl.SetFile(spec);
978 decl.SetColumn(first_line->getColumnNumber());
979 decl.SetLine(first_line->getLineNumber());
980 }
981 }
982 }
983 }
984
985 Variable::RangeList ranges;
986 SymbolContextScope *context_scope = sc.comp_unit;
Aleksandr Urakov758657e2019-02-01 10:01:18 +0000987 if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000988 if (sc.function) {
Aleksandr Urakov758657e2019-02-01 10:01:18 +0000989 Block &function_block = sc.function->GetBlock(true);
990 Block *block =
991 function_block.FindBlockByID(pdb_data.getLexicalParentId());
992 if (!block)
993 block = &function_block;
994
995 context_scope = block;
996
997 for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges;
998 ++i) {
999 AddressRange range;
1000 if (!block->GetRangeAtIndex(i, range))
1001 continue;
1002
1003 ranges.Append(range.GetBaseAddress().GetFileAddress(),
1004 range.GetByteSize());
1005 }
Aaron Smithcab0d232018-05-23 01:52:42 +00001006 }
1007 }
1008
1009 SymbolFileTypeSP type_sp =
1010 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
1011
1012 auto var_name = pdb_data.getName();
1013 auto mangled = GetMangledForPDBData(pdb_data);
1014 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
1015
Jonas Devlieghere924d5602018-07-13 10:29:27 +00001016 bool is_constant;
1017 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
Aleksandr Urakov758657e2019-02-01 10:01:18 +00001018 GetObjectFile()->GetModule(), pdb_data, ranges, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +00001019
1020 var_sp = std::make_shared<Variable>(
1021 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
1022 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +00001023 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +00001024
1025 m_variables.insert(std::make_pair(var_uid, var_sp));
1026 return var_sp;
1027}
1028
1029size_t
1030SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
1031 const llvm::pdb::PDBSymbol &pdb_symbol,
1032 lldb_private::VariableList *variable_list) {
1033 size_t num_added = 0;
1034
1035 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
1036 VariableListSP local_variable_list_sp;
1037
1038 auto result = m_variables.find(pdb_data->getSymIndexId());
1039 if (result != m_variables.end()) {
1040 if (variable_list)
1041 variable_list->AddVariableIfUnique(result->second);
1042 } else {
1043 // Prepare right VariableList for this variable.
1044 if (auto lexical_parent = pdb_data->getLexicalParent()) {
1045 switch (lexical_parent->getSymTag()) {
1046 case PDB_SymType::Exe:
1047 assert(sc.comp_unit);
1048 LLVM_FALLTHROUGH;
1049 case PDB_SymType::Compiland: {
1050 if (sc.comp_unit) {
1051 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1052 if (!local_variable_list_sp) {
1053 local_variable_list_sp = std::make_shared<VariableList>();
1054 sc.comp_unit->SetVariableList(local_variable_list_sp);
1055 }
1056 }
1057 } break;
1058 case PDB_SymType::Block:
1059 case PDB_SymType::Function: {
1060 if (sc.function) {
1061 Block *block = sc.function->GetBlock(true).FindBlockByID(
1062 lexical_parent->getSymIndexId());
1063 if (block) {
1064 local_variable_list_sp = block->GetBlockVariableList(false);
1065 if (!local_variable_list_sp) {
1066 local_variable_list_sp = std::make_shared<VariableList>();
1067 block->SetVariableList(local_variable_list_sp);
1068 }
1069 }
1070 }
1071 } break;
1072 default:
1073 break;
1074 }
1075 }
1076
1077 if (local_variable_list_sp) {
1078 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1079 local_variable_list_sp->AddVariableIfUnique(var_sp);
1080 if (variable_list)
1081 variable_list->AddVariableIfUnique(var_sp);
1082 ++num_added;
Zachary Turnerc68925a2019-01-09 21:20:44 +00001083 PDBASTParser *ast = GetPDBAstParser();
1084 if (ast)
1085 ast->GetDeclForSymbol(*pdb_data);
Aaron Smithcab0d232018-05-23 01:52:42 +00001086 }
1087 }
1088 }
1089 }
1090
1091 if (auto results = pdb_symbol.findAllChildren()) {
1092 while (auto result = results->getNext())
1093 num_added += ParseVariables(sc, *result, variable_list);
1094 }
1095
1096 return num_added;
1097}
1098
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099uint32_t SymbolFilePDB::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001100 lldb_private::ConstString name,
Pavel Labath34cda142018-05-31 09:46:26 +00001101 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 uint32_t max_matches, lldb_private::VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001103 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smithcab0d232018-05-23 01:52:42 +00001104 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1105 return 0;
1106 if (name.IsEmpty())
1107 return 0;
1108
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001109 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001110 if (!results)
1111 return 0;
1112
1113 uint32_t matches = 0;
1114 size_t old_size = variables.GetSize();
1115 while (auto result = results->getNext()) {
1116 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1117 if (max_matches > 0 && matches >= max_matches)
1118 break;
1119
1120 SymbolContext sc;
1121 sc.module_sp = m_obj_file->GetModule();
1122 lldbassert(sc.module_sp.get());
1123
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001124 if (!name.GetStringRef().equals(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001125 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001126 continue;
1127
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001128 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1129 // FIXME: We are not able to determine the compile unit.
1130 if (sc.comp_unit == nullptr)
1131 continue;
1132
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001133 if (parent_decl_ctx && GetDeclContextContainingUID(
1134 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001135 continue;
1136
Aaron Smithcab0d232018-05-23 01:52:42 +00001137 ParseVariables(sc, *pdb_data, &variables);
1138 matches = variables.GetSize() - old_size;
1139 }
1140
1141 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142}
1143
1144uint32_t
1145SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001146 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 lldb_private::VariableList &variables) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001148 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smithcab0d232018-05-23 01:52:42 +00001149 if (!regex.IsValid())
1150 return 0;
1151 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1152 if (!results)
1153 return 0;
1154
1155 uint32_t matches = 0;
1156 size_t old_size = variables.GetSize();
1157 while (auto pdb_data = results->getNext()) {
1158 if (max_matches > 0 && matches >= max_matches)
1159 break;
1160
1161 auto var_name = pdb_data->getName();
1162 if (var_name.empty())
1163 continue;
1164 if (!regex.Execute(var_name))
1165 continue;
1166 SymbolContext sc;
1167 sc.module_sp = m_obj_file->GetModule();
1168 lldbassert(sc.module_sp.get());
1169
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001170 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001171 // FIXME: We are not able to determine the compile unit.
1172 if (sc.comp_unit == nullptr)
1173 continue;
1174
1175 ParseVariables(sc, *pdb_data, &variables);
1176 matches = variables.GetSize() - old_size;
1177 }
1178
1179 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180}
1181
Aaron Smithe664b5d2018-03-19 21:14:19 +00001182bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001183 bool include_inlines,
1184 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001185 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001186 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001187 if (!sc.comp_unit)
1188 return false;
1189 sc.module_sp = sc.comp_unit->GetModule();
Zachary Turner863f8c12019-01-11 18:03:20 +00001190 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001191 if (!sc.function)
1192 return false;
1193
1194 sc_list.Append(sc);
1195 return true;
1196}
1197
1198bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1199 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001200 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001201 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1202 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001203 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001204}
1205
1206void SymbolFilePDB::CacheFunctionNames() {
1207 if (!m_func_full_names.IsEmpty())
1208 return;
1209
1210 std::map<uint64_t, uint32_t> addr_ids;
1211
1212 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1213 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001214 if (pdb_func_up->isCompilerGenerated())
1215 continue;
1216
Aaron Smith7ac1c782018-02-09 05:31:28 +00001217 auto name = pdb_func_up->getName();
1218 auto demangled_name = pdb_func_up->getUndecoratedName();
1219 if (name.empty() && demangled_name.empty())
1220 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001221
Aaron Smithf76fe682018-03-07 03:16:50 +00001222 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001223 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1224 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1225
1226 if (auto parent = pdb_func_up->getClassParent()) {
1227
1228 // PDB have symbols for class/struct methods or static methods in Enum
1229 // Class. We won't bother to check if the parent is UDT or Enum here.
1230 m_func_method_names.Append(ConstString(name), uid);
1231
Adrian Prantl05097242018-04-30 16:49:04 +00001232 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1233 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1234 // not have inforamtion of this, we extract base names and cache them
1235 // by our own effort.
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001236 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001237 if (!basename.empty())
1238 m_func_base_names.Append(ConstString(basename), uid);
1239 else {
1240 m_func_base_names.Append(ConstString(name), uid);
1241 }
1242
1243 if (!demangled_name.empty())
1244 m_func_full_names.Append(ConstString(demangled_name), uid);
1245
1246 } else {
1247 // Handle not-method symbols.
1248
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001249 // The function name might contain namespace, or its lexical scope.
1250 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1251 if (!basename.empty())
1252 m_func_base_names.Append(ConstString(basename), uid);
1253 else
1254 m_func_base_names.Append(ConstString(name), uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001255
1256 if (name == "main") {
1257 m_func_full_names.Append(ConstString(name), uid);
1258
1259 if (!demangled_name.empty() && name != demangled_name) {
1260 m_func_full_names.Append(ConstString(demangled_name), uid);
1261 m_func_base_names.Append(ConstString(demangled_name), uid);
1262 }
1263 } else if (!demangled_name.empty()) {
1264 m_func_full_names.Append(ConstString(demangled_name), uid);
1265 } else {
1266 m_func_full_names.Append(ConstString(name), uid);
1267 }
1268 }
1269 }
1270 }
1271
1272 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001273 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001274 while (auto pub_sym_up = results_up->getNext()) {
1275 if (!pub_sym_up->isFunction())
1276 continue;
1277 auto name = pub_sym_up->getName();
1278 if (name.empty())
1279 continue;
1280
1281 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001282 auto vm_addr = pub_sym_up->getVirtualAddress();
1283
1284 // PDB public symbol has mangled name for its associated function.
1285 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1286 // Cache mangled name.
1287 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1288 }
1289 }
1290 }
1291 }
1292 // Sort them before value searching is working properly
1293 m_func_full_names.Sort();
1294 m_func_full_names.SizeToFit();
1295 m_func_method_names.Sort();
1296 m_func_method_names.SizeToFit();
1297 m_func_base_names.Sort();
1298 m_func_base_names.SizeToFit();
1299}
1300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301uint32_t SymbolFilePDB::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001302 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001304 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305 lldb_private::SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001306 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001307 if (!append)
1308 sc_list.Clear();
1309 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1310
1311 if (name_type_mask == eFunctionNameTypeNone)
1312 return 0;
1313 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1314 return 0;
1315 if (name.IsEmpty())
1316 return 0;
1317
1318 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001319 if (name_type_mask & eFunctionNameTypeFull ||
1320 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001321 name_type_mask & eFunctionNameTypeMethod) {
1322 CacheFunctionNames();
1323
1324 std::set<uint32_t> resolved_ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001325 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1326 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001327 std::vector<uint32_t> ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001328 if (!Names.GetValues(name, ids))
1329 return;
1330
1331 for (uint32_t id : ids) {
1332 if (resolved_ids.find(id) != resolved_ids.end())
1333 continue;
1334
1335 if (parent_decl_ctx &&
1336 GetDeclContextContainingUID(id) != *parent_decl_ctx)
1337 continue;
1338
1339 if (ResolveFunction(id, include_inlines, sc_list))
1340 resolved_ids.insert(id);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001341 }
1342 };
1343 if (name_type_mask & eFunctionNameTypeFull) {
1344 ResolveFn(m_func_full_names);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001345 ResolveFn(m_func_base_names);
1346 ResolveFn(m_func_method_names);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001347 }
1348 if (name_type_mask & eFunctionNameTypeBase) {
1349 ResolveFn(m_func_base_names);
1350 }
1351 if (name_type_mask & eFunctionNameTypeMethod) {
1352 ResolveFn(m_func_method_names);
1353 }
1354 }
1355 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356}
1357
1358uint32_t
1359SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1360 bool include_inlines, bool append,
1361 lldb_private::SymbolContextList &sc_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001362 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001363 if (!append)
1364 sc_list.Clear();
1365 if (!regex.IsValid())
1366 return 0;
1367
1368 auto old_size = sc_list.GetSize();
1369 CacheFunctionNames();
1370
1371 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001372 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1373 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001374 std::vector<uint32_t> ids;
1375 if (Names.GetValues(regex, ids)) {
1376 for (auto id : ids) {
1377 if (resolved_ids.find(id) == resolved_ids.end())
1378 if (ResolveFunction(id, include_inlines, sc_list))
1379 resolved_ids.insert(id);
1380 }
1381 }
1382 };
1383 ResolveFn(m_func_full_names);
1384 ResolveFn(m_func_base_names);
1385
1386 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387}
1388
1389void SymbolFilePDB::GetMangledNamesForFunction(
1390 const std::string &scope_qualified_name,
1391 std::vector<lldb_private::ConstString> &mangled_names) {}
1392
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001393void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1394 std::set<lldb::addr_t> sym_addresses;
1395 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1396 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
1397
1398 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1399 if (!results)
1400 return;
1401
1402 auto section_list = m_obj_file->GetSectionList();
1403 if (!section_list)
1404 return;
1405
1406 while (auto pub_symbol = results->getNext()) {
Pavel Labath7db8b5c2019-02-13 07:17:24 +00001407 auto section_id = pub_symbol->getAddressSection();
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001408
Pavel Labath7db8b5c2019-02-13 07:17:24 +00001409 auto section = section_list->FindSectionByID(section_id);
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001410 if (!section)
1411 continue;
1412
1413 auto offset = pub_symbol->getAddressOffset();
1414
1415 auto file_addr = section->GetFileAddress() + offset;
1416 if (sym_addresses.find(file_addr) != sym_addresses.end())
1417 continue;
1418 sym_addresses.insert(file_addr);
1419
1420 auto size = pub_symbol->getLength();
1421 symtab.AddSymbol(
1422 Symbol(pub_symbol->getSymIndexId(), // symID
1423 pub_symbol->getName().c_str(), // name
1424 true, // name_is_mangled
1425 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1426 true, // external
1427 false, // is_debug
1428 false, // is_trampoline
1429 false, // is_artificial
1430 section, // section_sp
1431 offset, // value
1432 size, // size
1433 size != 0, // size_is_valid
1434 false, // contains_linker_annotations
1435 0 // flags
1436 ));
1437 }
1438
1439 symtab.CalculateSymbolSizes();
1440 symtab.Finalize();
1441}
1442
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443uint32_t SymbolFilePDB::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001444 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1446 uint32_t max_matches,
1447 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1448 lldb_private::TypeMap &types) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001449 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 if (!append)
1451 types.Clear();
1452 if (!name)
1453 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001454 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1455 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456
1457 searched_symbol_files.clear();
1458 searched_symbol_files.insert(this);
1459
Aaron Smith86e94342017-12-22 05:26:50 +00001460 // There is an assumption 'name' is not a regex
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001461 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001462
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 return types.GetSize();
1464}
1465
Zachary Turner49110232018-11-05 17:40:28 +00001466void SymbolFilePDB::DumpClangAST(Stream &s) {
Alex Langford0e252e32019-07-30 22:12:34 +00001467 auto type_system_or_err =
1468 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1469 if (auto err = type_system_or_err.takeError()) {
1470 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
1471 std::move(err), "Unable to dump ClangAST");
Zachary Turner49110232018-11-05 17:40:28 +00001472 return;
Alex Langford0e252e32019-07-30 22:12:34 +00001473 }
1474
1475 auto *clang_type_system =
1476 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
1477 if (!clang_type_system)
1478 return;
1479 clang_type_system->Dump(s);
Zachary Turner49110232018-11-05 17:40:28 +00001480}
1481
Aaron Smithc8316ed2018-03-22 03:44:51 +00001482void SymbolFilePDB::FindTypesByRegex(
1483 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1484 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001486 // space as much as possible since this searches EVERYTHING in the PDB,
1487 // manually doing regex comparisons. PDB library isn't optimized for regex
1488 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001490 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1492 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 std::unique_ptr<IPDBEnumSymbols> results;
1494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 uint32_t matches = 0;
1496
1497 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001498 results = m_global_scope_up->findAllChildren(tag);
1499 if (!results)
1500 continue;
1501
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 while (auto result = results->getNext()) {
1503 if (max_matches > 0 && matches >= max_matches)
1504 break;
1505
1506 std::string type_name;
1507 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1508 type_name = enum_type->getName();
1509 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001510 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 type_name = typedef_type->getName();
1512 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1513 type_name = class_type->getName();
1514 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001515 // We're looking only for types that have names. Skip symbols, as well
1516 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 continue;
1518 }
1519
Aaron Smith86e94342017-12-22 05:26:50 +00001520 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521 continue;
1522
1523 // This should cause the type to get cached and stored in the `m_types`
1524 // lookup.
1525 if (!ResolveTypeUID(result->getSymIndexId()))
1526 continue;
1527
1528 auto iter = m_types.find(result->getSymIndexId());
1529 if (iter == m_types.end())
1530 continue;
1531 types.Insert(iter->second);
1532 ++matches;
1533 }
1534 }
1535}
1536
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001537void SymbolFilePDB::FindTypesByName(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001538 llvm::StringRef name,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001539 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1540 uint32_t max_matches, lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001542 if (name.empty())
1543 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001544 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001545 if (!results)
1546 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547
1548 uint32_t matches = 0;
1549
1550 while (auto result = results->getNext()) {
1551 if (max_matches > 0 && matches >= max_matches)
1552 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001553
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001554 if (MSVCUndecoratedNameParser::DropScope(
1555 result->getRawSymbol().getName()) != name)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001556 continue;
1557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 switch (result->getSymTag()) {
1559 case PDB_SymType::Enum:
1560 case PDB_SymType::UDT:
1561 case PDB_SymType::Typedef:
1562 break;
1563 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001564 // We're looking only for types that have names. Skip symbols, as well
1565 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 continue;
1567 }
1568
1569 // This should cause the type to get cached and stored in the `m_types`
1570 // lookup.
1571 if (!ResolveTypeUID(result->getSymIndexId()))
1572 continue;
1573
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001574 if (parent_decl_ctx && GetDeclContextContainingUID(
1575 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001576 continue;
1577
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 auto iter = m_types.find(result->getSymIndexId());
1579 if (iter == m_types.end())
1580 continue;
1581 types.Insert(iter->second);
1582 ++matches;
1583 }
1584}
1585
1586size_t SymbolFilePDB::FindTypes(
1587 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1588 lldb_private::TypeMap &types) {
1589 return 0;
1590}
1591
Aaron Smithc8316ed2018-03-22 03:44:51 +00001592void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1593 uint32_t type_mask,
1594 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001595 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001596 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001597 case PDB_SymType::ArrayType:
1598 can_parse = ((type_mask & eTypeClassArray) != 0);
1599 break;
1600 case PDB_SymType::BuiltinType:
1601 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1602 break;
1603 case PDB_SymType::Enum:
1604 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1605 break;
1606 case PDB_SymType::Function:
1607 case PDB_SymType::FunctionSig:
1608 can_parse = ((type_mask & eTypeClassFunction) != 0);
1609 break;
1610 case PDB_SymType::PointerType:
1611 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1612 eTypeClassMemberPointer)) != 0);
1613 break;
1614 case PDB_SymType::Typedef:
1615 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1616 break;
1617 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001618 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001619 assert(udt);
1620 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001621 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1622 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001623 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001624 default:
1625 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001626 }
1627
1628 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001629 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001630 auto result =
1631 std::find(type_collection.begin(), type_collection.end(), type);
1632 if (result == type_collection.end())
1633 type_collection.push_back(type);
1634 }
1635 }
1636
Aaron Smithe664b5d2018-03-19 21:14:19 +00001637 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001638 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001639 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001640}
1641
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001643 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 lldb_private::TypeList &type_list) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001645 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001646 TypeCollection type_collection;
1647 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001648 CompileUnit *cu =
1649 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001650 if (cu) {
1651 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001652 if (!compiland_up)
1653 return 0;
1654 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001655 } else {
1656 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1657 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001658 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001659 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1660 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001661 }
1662 }
1663 }
1664
1665 for (auto type : type_collection) {
1666 type->GetForwardCompilerType();
1667 type_list.Insert(type->shared_from_this());
1668 }
1669 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001670}
1671
Alex Langford0e252e32019-07-30 22:12:34 +00001672llvm::Expected<lldb_private::TypeSystem &>
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
Alex Langford0e252e32019-07-30 22:12:34 +00001674 auto type_system_or_err =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
Alex Langford0e252e32019-07-30 22:12:34 +00001676 if (type_system_or_err) {
1677 type_system_or_err->SetSymbolFile(this);
1678 }
1679 return type_system_or_err;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001680}
1681
Zachary Turnerc68925a2019-01-09 21:20:44 +00001682PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
Alex Langford0e252e32019-07-30 22:12:34 +00001683 auto type_system_or_err =
1684 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1685 if (auto err = type_system_or_err.takeError()) {
1686 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
1687 std::move(err), "Unable to get PDB AST parser");
1688 return nullptr;
1689 }
1690
1691 auto *clang_type_system =
1692 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Zachary Turnerc68925a2019-01-09 21:20:44 +00001693 if (!clang_type_system)
1694 return nullptr;
1695
1696 return clang_type_system->GetPDBParser();
1697}
1698
1699
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001701 lldb_private::ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Pavel Labath656ddeb2019-07-30 08:20:05 +00001703 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Alex Langford0e252e32019-07-30 22:12:34 +00001704 auto type_system_or_err =
1705 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1706 if (auto err = type_system_or_err.takeError()) {
1707 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
1708 std::move(err), "Unable to find namespace {}",
1709 name.AsCString());
1710 return CompilerDeclContext();
1711 }
1712
1713 auto *clang_type_system =
1714 llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001715 if (!clang_type_system)
1716 return CompilerDeclContext();
1717
1718 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1719 if (!pdb)
1720 return CompilerDeclContext();
1721
1722 clang::DeclContext *decl_context = nullptr;
1723 if (parent_decl_ctx)
1724 decl_context = static_cast<clang::DeclContext *>(
1725 parent_decl_ctx->GetOpaqueDeclContext());
1726
1727 auto namespace_decl =
1728 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1729 if (!namespace_decl)
1730 return CompilerDeclContext();
1731
Alex Langford0e252e32019-07-30 22:12:34 +00001732 return CompilerDeclContext(clang_type_system,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001733 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001734}
1735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1737 static ConstString g_name("pdb");
1738 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001739}
1740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1742
1743IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1744
1745const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1746 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001747}
1748
Aaron Smithc8316ed2018-03-22 03:44:51 +00001749lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1750 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 auto found_cu = m_comp_units.find(id);
1752 if (found_cu != m_comp_units.end())
1753 return found_cu->second;
1754
Aaron Smith10a02572018-01-13 06:58:18 +00001755 auto compiland_up = GetPDBCompilandByUID(id);
1756 if (!compiland_up)
1757 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758
1759 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001760 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 if (!details)
1762 lang = lldb::eLanguageTypeC_plus_plus;
1763 else
1764 lang = TranslateLanguage(details->getLanguage());
1765
Aaron Smithf76fe682018-03-07 03:16:50 +00001766 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1767 return CompUnitSP();
1768
Aaron Smith487b0c62018-03-20 00:18:22 +00001769 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001770 if (path.empty())
1771 return CompUnitSP();
1772
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 // Don't support optimized code for now, DebugInfoPDB does not return this
1774 // information.
1775 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001776 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1777 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001778
1779 if (!cu_sp)
1780 return CompUnitSP();
1781
1782 m_comp_units.insert(std::make_pair(id, cu_sp));
1783 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001784 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001785 lldbassert(index != UINT32_MAX);
Pavel Labathe0119902019-07-23 09:24:02 +00001786 SetCompileUnitAtIndex(index, cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001787 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001788}
1789
Zachary Turner863f8c12019-01-11 18:03:20 +00001790bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1791 uint32_t match_line) {
1792 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +00001793 if (!compiland_up)
1794 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001795
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001797 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001798 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1799 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001801 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Zachary Turner863f8c12019-01-11 18:03:20 +00001802 auto line_table = llvm::make_unique<LineTable>(&comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001803
Aaron Smith10a02572018-01-13 06:58:18 +00001804 // Find contributions to `compiland` from all source and header files.
Zachary Turner863f8c12019-01-11 18:03:20 +00001805 std::string path = comp_unit.GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001806 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1807 if (!files)
1808 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001809
Adrian Prantl05097242018-04-30 16:49:04 +00001810 // For each source and header file, create a LineSequence for contributions
1811 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 while (auto file = files->getNext()) {
1813 std::unique_ptr<LineSequence> sequence(
1814 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001815 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1816 if (!lines)
1817 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001819
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 uint64_t prev_addr;
1821 uint32_t prev_length;
1822 uint32_t prev_line;
1823 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001824
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 for (int i = 0; i < entry_count; ++i) {
1826 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001827
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 uint64_t lno = line->getLineNumber();
1829 uint64_t addr = line->getVirtualAddress();
1830 uint32_t length = line->getLength();
1831 uint32_t source_id = line->getSourceFileId();
1832 uint32_t col = line->getColumnNumber();
1833 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001834
Adrian Prantl05097242018-04-30 16:49:04 +00001835 // There was a gap between the current entry and the previous entry if
1836 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001840 // of the previous entry's address range if the current entry resulted in
1841 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1843 line_table->AppendLineEntryToSequence(
1844 sequence.get(), prev_addr + prev_length, prev_line, 0,
1845 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001846
1847 line_table->InsertSequence(sequence.release());
1848 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001849 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001850
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851 if (ShouldAddLine(match_line, lno, length)) {
1852 bool is_statement = line->isStatement();
1853 bool is_prologue = false;
1854 bool is_epilogue = false;
1855 auto func =
1856 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1857 if (func) {
1858 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001859 if (prologue)
1860 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001861
Kate Stoneb9c1b512016-09-06 20:57:50 +00001862 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001863 if (epilogue)
1864 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001865 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001866
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1868 source_idx, is_statement, false,
1869 is_prologue, is_epilogue, false);
1870 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001871
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872 prev_addr = addr;
1873 prev_length = length;
1874 prev_line = lno;
1875 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001876 }
1877
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1879 // The end is always a terminal entry, so insert it regardless.
1880 line_table->AppendLineEntryToSequence(
1881 sequence.get(), prev_addr + prev_length, prev_line, 0,
1882 prev_source_idx, false, false, false, false, true);
1883 }
1884
1885 line_table->InsertSequence(sequence.release());
1886 }
1887
Aaron Smith10a02572018-01-13 06:58:18 +00001888 if (line_table->GetSize()) {
Zachary Turner863f8c12019-01-11 18:03:20 +00001889 comp_unit.SetLineTable(line_table.release());
Aaron Smith10a02572018-01-13 06:58:18 +00001890 return true;
1891 }
1892 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001893}
1894
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001896 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001898 // This is a hack, but we need to convert the source id into an index into
1899 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001900 // basename / full path issues that may or may not even be a problem, so we
1901 // use the globally unique source file identifiers. Ideally we could use the
1902 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001903 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1904 if (!source_files)
1905 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001906
1907 // LLDB uses the DWARF-like file numeration (one based)
1908 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001909
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 while (auto file = source_files->getNext()) {
1911 uint32_t source_id = file->getUniqueId();
1912 index_map[source_id] = index++;
1913 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001914}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001915
1916lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001917 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001918 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001919 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001920 return nullptr;
1921
Aaron Smith308e39c2018-03-22 19:26:33 +00001922 // If it is a PDB function's vm addr, this is the first sure bet.
1923 if (auto lines =
1924 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1925 if (auto first_line = lines->getNext())
1926 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001927 }
1928
Aaron Smith308e39c2018-03-22 19:26:33 +00001929 // Otherwise we resort to section contributions.
1930 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1931 while (auto section = sec_contribs->getNext()) {
1932 auto va = section->getVirtualAddress();
1933 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1934 return ParseCompileUnitForUID(section->getCompilandId());
1935 }
1936 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001937 return nullptr;
1938}
1939
1940Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001941SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001942 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001943 auto func_name = pdb_func.getName();
1944 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001945 std::string func_decorated_name;
1946
1947 // Seek from public symbols for non-static function's decorated name if any.
1948 // For static functions, they don't have undecorated names and aren't exposed
1949 // in Public Symbols either.
1950 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001951 auto result_up = m_global_scope_up->findChildren(
1952 PDB_SymType::PublicSymbol, func_undecorated_name,
1953 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001954 if (result_up) {
1955 while (auto symbol_up = result_up->getNext()) {
1956 // For a public symbol, it is unique.
1957 lldbassert(result_up->getChildCount() == 1);
1958 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001959 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1960 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001961 if (pdb_public_sym->isFunction()) {
1962 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001963 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001964 }
1965 }
1966 }
1967 }
1968 }
1969 if (!func_decorated_name.empty()) {
1970 mangled.SetMangledName(ConstString(func_decorated_name));
1971
1972 // For MSVC, format of C funciton's decorated name depends on calling
1973 // conventon. Unfortunately none of the format is recognized by current
1974 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001975 // `__purecall` is retrieved as both its decorated and undecorated name
1976 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1977 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1978 // Mangled::GetDemangledName method will fail internally and caches an
1979 // empty string as its undecorated name. So we will face a contradition
1980 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001981 // non-empty undecorated name from PDB
1982 // empty undecorated name from LLDB
1983 if (!func_undecorated_name.empty() &&
1984 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1985 mangled.SetDemangledName(ConstString(func_undecorated_name));
1986
1987 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001988 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1989 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001990 // PDB source unless we also apply same flags in getting undecorated
1991 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1992 if (!func_undecorated_name.empty() &&
1993 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1994 ConstString(func_undecorated_name))
1995 mangled.SetDemangledName(ConstString(func_undecorated_name));
1996 } else if (!func_undecorated_name.empty()) {
1997 mangled.SetDemangledName(ConstString(func_undecorated_name));
1998 } else if (!func_name.empty())
1999 mangled.SetValue(ConstString(func_name), false);
2000
2001 return mangled;
2002}
2003
2004bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
2005 const lldb_private::CompilerDeclContext *decl_ctx) {
2006 if (decl_ctx == nullptr || !decl_ctx->IsValid())
2007 return true;
2008
2009 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
2010 if (!decl_ctx_type_system)
2011 return false;
Alex Langford0e252e32019-07-30 22:12:34 +00002012 auto type_system_or_err = GetTypeSystemForLanguage(
Aaron Smith7ac1c782018-02-09 05:31:28 +00002013 decl_ctx_type_system->GetMinimumLanguage(nullptr));
Alex Langford0e252e32019-07-30 22:12:34 +00002014 if (auto err = type_system_or_err.takeError()) {
2015 LLDB_LOG_ERROR(
2016 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
2017 std::move(err),
2018 "Unable to determine if DeclContext matches this symbol file");
2019 return false;
2020 }
2021
2022 if (decl_ctx_type_system == &type_system_or_err.get())
Aaron Smith7ac1c782018-02-09 05:31:28 +00002023 return true; // The type systems match, return true
2024
2025 return false;
2026}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00002027
2028uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
2029 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
2030 return lhs < rhs.Offset;
2031 };
2032
2033 // Cache section contributions
2034 if (m_sec_contribs.empty()) {
2035 if (auto SecContribs = m_session_up->getSectionContribs()) {
2036 while (auto SectionContrib = SecContribs->getNext()) {
2037 auto comp_id = SectionContrib->getCompilandId();
2038 if (!comp_id)
2039 continue;
2040
2041 auto sec = SectionContrib->getAddressSection();
2042 auto &sec_cs = m_sec_contribs[sec];
2043
2044 auto offset = SectionContrib->getAddressOffset();
2045 auto it =
2046 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
2047
2048 auto size = SectionContrib->getLength();
2049 sec_cs.insert(it, {offset, size, comp_id});
2050 }
2051 }
2052 }
2053
2054 // Check by line number
2055 if (auto Lines = data.getLineNumbers()) {
2056 if (auto FirstLine = Lines->getNext())
2057 return FirstLine->getCompilandId();
2058 }
2059
2060 // Retrieve section + offset
2061 uint32_t DataSection = data.getAddressSection();
2062 uint32_t DataOffset = data.getAddressOffset();
2063 if (DataSection == 0) {
2064 if (auto RVA = data.getRelativeVirtualAddress())
2065 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
2066 }
2067
2068 if (DataSection) {
2069 // Search by section contributions
2070 auto &sec_cs = m_sec_contribs[DataSection];
2071 auto it =
2072 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
2073 if (it != sec_cs.begin()) {
2074 --it;
2075 if (DataOffset < it->Offset + it->Size)
2076 return it->CompilandId;
2077 }
2078 } else {
2079 // Search in lexical tree
2080 auto LexParentId = data.getLexicalParentId();
2081 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
2082 if (LexParent->getSymTag() == PDB_SymType::Exe)
2083 break;
2084 if (LexParent->getSymTag() == PDB_SymType::Compiland)
2085 return LexParentId;
2086 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
2087 }
2088 }
2089
2090 return 0;
2091}