blob: 72766a1e553f0cb7e1f7977066705076aa8406bc [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFilePDB.h"
11
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000012#include "PDBASTParser.h"
13#include "PDBLocationToDWARFExpression.h"
14
Zachary Turner42dff792016-04-15 00:21:26 +000015#include "clang/Lex/Lexer.h"
16
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000019#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000020#include "lldb/Symbol/CompileUnit.h"
21#include "lldb/Symbol/LineTable.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000024#include "lldb/Symbol/SymbolVendor.h"
Aaron Smithec40f812018-01-23 20:35:19 +000025#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000026#include "lldb/Symbol/TypeMap.h"
Aaron Smithcab0d232018-05-23 01:52:42 +000027#include "lldb/Symbol/Variable.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;
68 default:
69 return lldb::LanguageType::eLanguageTypeUnknown;
70 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000071}
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
74 uint32_t addr_length) {
75 return ((requested_line == 0 || actual_line == requested_line) &&
76 addr_length > 0);
77}
Aaron Smithc8316ed2018-03-22 03:44:51 +000078} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000079
Zachary Turner307f5ae2018-10-12 19:47:13 +000080static bool ShouldUseNativeReader() {
Greg Clayton71970b72018-12-12 18:14:27 +000081#if defined(_WIN32)
Zachary Turner307f5ae2018-10-12 19:47:13 +000082 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
83 return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
84 use_native.equals_lower("1") || use_native.equals_lower("true");
Greg Clayton71970b72018-12-12 18:14:27 +000085#else
86 return true;
87#endif
Zachary Turner307f5ae2018-10-12 19:47:13 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000091 if (ShouldUseNativeReader()) {
92 npdb::SymbolFileNativePDB::Initialize();
93 } else {
94 PluginManager::RegisterPlugin(GetPluginNameStatic(),
95 GetPluginDescriptionStatic(), CreateInstance,
96 DebuggerInitialize);
97 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000098}
99
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000101 if (ShouldUseNativeReader()) {
102 npdb::SymbolFileNativePDB::Terminate();
103 } else {
104 PluginManager::UnregisterPlugin(CreateInstance);
105 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000106}
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
109
110lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
111 static ConstString g_name("pdb");
112 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115const char *SymbolFilePDB::GetPluginDescriptionStatic() {
116 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000117}
118
119lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
121 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000122}
123
124SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000125 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
126 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000131 uint32_t abilities = 0;
132 if (!m_obj_file)
133 return 0;
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 if (!m_session_up) {
136 // Lazily load and match the PDB file, but only do this once.
137 std::string exePath = m_obj_file->GetFileSpec().GetPath();
138 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
139 m_session_up);
140 if (error) {
141 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000142 auto module_sp = m_obj_file->GetModule();
143 if (!module_sp)
144 return 0;
145 // See if any symbol file is specified through `--symfile` option.
146 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
147 if (!symfile)
148 return 0;
149 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000150 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000151 if (error) {
152 llvm::consumeError(std::move(error));
153 return 0;
154 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000157 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000158 return 0;
159
160 auto enum_tables_up = m_session_up->getEnumTables();
161 if (!enum_tables_up)
162 return 0;
163 while (auto table_up = enum_tables_up->getNext()) {
164 if (table_up->getItemCount() == 0)
165 continue;
166 auto type = table_up->getTableType();
167 switch (type) {
168 case PDB_TableType::Symbols:
169 // This table represents a store of symbols with types listed in
170 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000171 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
172 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000173 break;
174 case PDB_TableType::LineNumbers:
175 abilities |= LineTables;
176 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000177 default:
178 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000179 }
180 }
181 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184void SymbolFilePDB::InitializeObject() {
185 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000186 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000188 if (!m_global_scope_up)
189 m_global_scope_up = m_session_up->getGlobalScope();
190 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 TypeSystem *type_system =
193 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
194 ClangASTContext *clang_type_system =
195 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000196 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
198 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201uint32_t SymbolFilePDB::GetNumCompileUnits() {
202 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000203 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
204 if (!compilands)
205 return 0;
206
207 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000208 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
209 // found as a child of the global scope (PDB executable). Usually, such
210 // compilands contain `thunk` symbols in which we are not interested for
211 // now. However we still count them in the compiland list. If we perform
212 // any compiland related activity, like finding symbols through
213 // llvm::pdb::IPDBSession methods, such compilands will all be searched
214 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000218 // PDB. Ignore this special compile unit for our purposes, if it is there.
219 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000220 auto last_compiland_up =
221 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
222 lldbassert(last_compiland_up.get());
223 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (name == "* Linker *")
225 --m_cached_compile_unit_count;
226 }
227 return m_cached_compile_unit_count;
228}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000229
Aaron Smith10a02572018-01-13 06:58:18 +0000230void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000231 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000232 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
233 if (!results_up)
234 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000235 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000236 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000237 auto compiland_up = results_up->getChildAtIndex(cu_idx);
238 if (!compiland_up)
239 continue;
240 if (compiland_up->getSymIndexId() == uid) {
241 index = cu_idx;
242 return;
243 }
244 }
245 index = UINT32_MAX;
246 return;
247}
248
249std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
250SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
251 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000255 if (index >= GetNumCompileUnits())
256 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257
Aaron Smith10a02572018-01-13 06:58:18 +0000258 // Assuming we always retrieve same compilands listed in same order through
259 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
260 // compile unit makes no sense.
261 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
262 if (!results)
263 return CompUnitSP();
264 auto compiland_up = results->getChildAtIndex(index);
265 if (!compiland_up)
266 return CompUnitSP();
267 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000268}
269
Zachary Turner863f8c12019-01-11 18:03:20 +0000270lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
271 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000272 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000274 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 if (!details)
276 return lldb::eLanguageTypeUnknown;
277 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000278}
279
Zachary Turner863f8c12019-01-11 18:03:20 +0000280lldb_private::Function *
281SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
282 CompileUnit &comp_unit) {
283 if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId()))
Aleksandr Urakova5235af2018-12-03 13:31:13 +0000284 return result.get();
285
Aaron Smithe664b5d2018-03-19 21:14:19 +0000286 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000287 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000288 return nullptr;
289
Aaron Smithe664b5d2018-03-19 21:14:19 +0000290 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000291 AddressRange func_range =
Zachary Turner863f8c12019-01-11 18:03:20 +0000292 AddressRange(file_vm_addr, func_length,
293 GetObjectFile()->GetModule()->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000294 if (!func_range.GetBaseAddress().IsValid())
295 return nullptr;
296
Aaron Smithc8316ed2018-03-22 03:44:51 +0000297 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000298 if (!func_type)
299 return nullptr;
300
Aaron Smithe664b5d2018-03-19 21:14:19 +0000301 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000302
Aaron Smith7ac1c782018-02-09 05:31:28 +0000303 Mangled mangled = GetMangledForPDBFunc(pdb_func);
304
Aaron Smithc8316ed2018-03-22 03:44:51 +0000305 FunctionSP func_sp =
Zachary Turner863f8c12019-01-11 18:03:20 +0000306 std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(),
Aaron Smithc8316ed2018-03-22 03:44:51 +0000307 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000308
Zachary Turner863f8c12019-01-11 18:03:20 +0000309 comp_unit.AddFunction(func_sp);
Zachary Turnerc68925a2019-01-09 21:20:44 +0000310
311 TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
312 if (!type_system)
313 return nullptr;
314 ClangASTContext *clang_type_system =
315 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
316 if (!clang_type_system)
317 return nullptr;
318 clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func);
319
Aaron Smith7ac1c782018-02-09 05:31:28 +0000320 return func_sp.get();
321}
322
Zachary Turner863f8c12019-01-11 18:03:20 +0000323size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000324 size_t func_added = 0;
Zachary Turner863f8c12019-01-11 18:03:20 +0000325 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000326 if (!compiland_up)
327 return 0;
328 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
329 if (!results_up)
330 return 0;
331 while (auto pdb_func_up = results_up->getNext()) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000332 auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000333 if (!func_sp) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000334 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000335 ++func_added;
336 }
337 }
338 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000339}
340
Zachary Turner863f8c12019-01-11 18:03:20 +0000341bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
342 if (comp_unit.GetLineTable())
Aaron Smith10a02572018-01-13 06:58:18 +0000343 return true;
Zachary Turner863f8c12019-01-11 18:03:20 +0000344 return ParseCompileUnitLineTable(comp_unit, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000345}
346
Zachary Turner863f8c12019-01-11 18:03:20 +0000347bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 // PDB doesn't contain information about macros
349 return false;
350}
351
Zachary Turner863f8c12019-01-11 18:03:20 +0000352bool SymbolFilePDB::ParseSupportFiles(
353 CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) {
Zachary Turner74e08ca2016-03-02 22:05:52 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000356 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
357 // second time seems like a waste. Unfortunately, there's no good way around
358 // this short of a moderate refactor since SymbolVendor depends on being able
359 // to cache this list.
Zachary Turner863f8c12019-01-11 18:03:20 +0000360 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +0000361 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000362 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000363 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000365 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366
367 while (auto file = files->getNext()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000368 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000369 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000371
372 // LLDB uses the DWARF-like file numeration (one based),
373 // the zeroth file is the compile unit itself
Zachary Turner863f8c12019-01-11 18:03:20 +0000374 support_files.Insert(0, comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000377}
378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379bool SymbolFilePDB::ParseImportedModules(
380 const lldb_private::SymbolContext &sc,
381 std::vector<lldb_private::ConstString> &imported_modules) {
382 // PDB does not yet support module debug info
383 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000384}
385
Aaron Smithc8316ed2018-03-22 03:44:51 +0000386static size_t ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000387 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
388 lldb_private::Block *parent_block, bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000389 assert(pdb_symbol && parent_block);
390
391 size_t num_added = 0;
392 switch (pdb_symbol->getSymTag()) {
393 case PDB_SymType::Block:
394 case PDB_SymType::Function: {
395 Block *block = nullptr;
396 auto &raw_sym = pdb_symbol->getRawSymbol();
397 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
398 if (pdb_func->hasNoInlineAttribute())
399 break;
400 if (is_top_parent)
401 block = parent_block;
402 else
403 break;
404 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
405 auto uid = pdb_symbol->getSymIndexId();
406 if (parent_block->FindBlockByID(uid))
407 break;
408 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
409 break;
410
411 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
412 parent_block->AddChild(block_sp);
413 block = block_sp.get();
414 } else
415 llvm_unreachable("Unexpected PDB symbol!");
416
Aaron Smithc8316ed2018-03-22 03:44:51 +0000417 block->AddRange(Block::Range(
418 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000419 block->FinalizeRanges();
420 ++num_added;
421
422 auto results_up = pdb_symbol->findAllChildren();
423 if (!results_up)
424 break;
425 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000426 num_added += ParseFunctionBlocksForPDBSymbol(
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000427 func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000428 }
429 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000430 default:
431 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000432 }
433 return num_added;
434}
435
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000436size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
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) {
Aaron Smith66b84072018-03-14 04:05:27 +0000449
450 size_t num_added = 0;
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000451 auto compiland = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000452 if (!compiland)
453 return 0;
454
455 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
456 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000457 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
458 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000459 for (auto tag : tags_to_search) {
460 results = raw_sym.findAllChildren(tag);
461 if (!results || results->getChildCount() == 0)
462 continue;
463 while (auto symbol = results->getNext()) {
464 switch (symbol->getSymTag()) {
465 case PDB_SymType::Enum:
466 case PDB_SymType::UDT:
467 case PDB_SymType::Typedef:
468 break;
469 default:
470 continue;
471 }
472
473 // This should cause the type to get cached and stored in the `m_types`
474 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000475 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
476 // Resolve the type completely to avoid a completion
477 // (and so a list change, which causes an iterators invalidation)
478 // during a TypeList dumping
479 type->GetFullCompilerType();
480 ++num_added;
481 }
Aaron Smith66b84072018-03-14 04:05:27 +0000482 }
Aaron Smithec40f812018-01-23 20:35:19 +0000483 }
Aaron Smith66b84072018-03-14 04:05:27 +0000484 };
Aaron Smithec40f812018-01-23 20:35:19 +0000485
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000486 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000487
Zachary Turnerac0d41c2019-01-10 20:57:50 +0000488 // Also parse global types particularly coming from this compiland.
489 // Unfortunately, PDB has no compiland information for each global type. We
490 // have to parse them all. But ensure we only do this once.
491 static bool parse_all_global_types = false;
492 if (!parse_all_global_types) {
493 ParseTypesByTagFn(*m_global_scope_up);
494 parse_all_global_types = true;
Aaron Smithec40f812018-01-23 20:35:19 +0000495 }
496 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497}
498
499size_t
500SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000501 if (!sc.comp_unit)
502 return 0;
503
504 size_t num_added = 0;
505 if (sc.function) {
506 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
507 sc.function->GetID());
508 if (!pdb_func)
509 return 0;
510
511 num_added += ParseVariables(sc, *pdb_func);
512 sc.function->GetBlock(false).SetDidParseVariables(true, true);
513 } else if (sc.comp_unit) {
514 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
515 if (!compiland)
516 return 0;
517
518 if (sc.comp_unit->GetVariableList(false))
519 return 0;
520
521 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
522 if (results && results->getChildCount()) {
523 while (auto result = results->getNext()) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000524 auto cu_id = GetCompilandId(*result);
Aaron Smithcab0d232018-05-23 01:52:42 +0000525 // FIXME: We are not able to determine variable's compile unit.
526 if (cu_id == 0)
527 continue;
528
529 if (cu_id == sc.comp_unit->GetID())
530 num_added += ParseVariables(sc, *result);
531 }
532 }
533
534 // FIXME: A `file static` or `global constant` variable appears both in
535 // compiland's children and global scope's children with unexpectedly
536 // different symbol's Id making it ambiguous.
537
538 // FIXME: 'local constant', for example, const char var[] = "abc", declared
539 // in a function scope, can't be found in PDB.
540
541 // Parse variables in this compiland.
542 num_added += ParseVariables(sc, *compiland);
543 }
544
545 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546}
547
548lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
549 auto find_result = m_types.find(type_uid);
550 if (find_result != m_types.end())
551 return find_result->second.get();
552
553 TypeSystem *type_system =
554 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
555 ClangASTContext *clang_type_system =
556 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
557 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000558 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000559 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 if (!pdb)
561 return nullptr;
562
563 auto pdb_type = m_session_up->getSymbolById(type_uid);
564 if (pdb_type == nullptr)
565 return nullptr;
566
567 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000568 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000569 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000570 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000571 if (type_list)
572 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000573 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000575}
576
Adrian Prantleca07c52018-11-05 20:49:07 +0000577llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
578 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
579 return llvm::None;
580}
581
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000583 std::lock_guard<std::recursive_mutex> guard(
584 GetObjectFile()->GetModule()->GetMutex());
585
586 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
587 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
588 if (!clang_ast_ctx)
589 return false;
590
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000591 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000592 if (!pdb)
593 return false;
594
595 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596}
597
598lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000599 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
600 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
601 if (!clang_ast_ctx)
602 return CompilerDecl();
603
604 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
605 if (!pdb)
606 return CompilerDecl();
607
608 auto symbol = m_session_up->getSymbolById(uid);
609 if (!symbol)
610 return CompilerDecl();
611
612 auto decl = pdb->GetDeclForSymbol(*symbol);
613 if (!decl)
614 return CompilerDecl();
615
616 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617}
618
619lldb_private::CompilerDeclContext
620SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000621 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
622 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
623 if (!clang_ast_ctx)
624 return CompilerDeclContext();
625
626 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
627 if (!pdb)
628 return CompilerDeclContext();
629
630 auto symbol = m_session_up->getSymbolById(uid);
631 if (!symbol)
632 return CompilerDeclContext();
633
634 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
635 if (!decl_context)
636 return GetDeclContextContainingUID(uid);
637
638 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639}
640
641lldb_private::CompilerDeclContext
642SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000643 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
644 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
645 if (!clang_ast_ctx)
646 return CompilerDeclContext();
647
648 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
649 if (!pdb)
650 return CompilerDeclContext();
651
652 auto symbol = m_session_up->getSymbolById(uid);
653 if (!symbol)
654 return CompilerDeclContext();
655
656 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
657 assert(decl_context);
658
659 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660}
661
662void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000663 lldb_private::CompilerDeclContext decl_ctx) {
664 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
665 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
666 if (!clang_ast_ctx)
667 return;
668
669 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
670 if (!pdb)
671 return;
672
673 pdb->ParseDeclsForDeclContext(
674 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
675}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676
677uint32_t
678SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000679 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000681 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000682 if (resolve_scope & eSymbolContextCompUnit ||
683 resolve_scope & eSymbolContextVariable ||
684 resolve_scope & eSymbolContextFunction ||
685 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000686 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000687 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
688 if (!cu_sp) {
689 if (resolved_flags | eSymbolContextVariable) {
690 // TODO: Resolve variables
691 }
692 return 0;
693 }
694 sc.comp_unit = cu_sp.get();
695 resolved_flags |= eSymbolContextCompUnit;
696 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000697 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000698
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000699 if (resolve_scope & eSymbolContextFunction ||
700 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000701 addr_t file_vm_addr = so_addr.GetFileAddress();
702 auto symbol_up =
703 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
704 if (symbol_up) {
705 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
706 assert(pdb_func);
707 auto func_uid = pdb_func->getSymIndexId();
708 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
709 if (sc.function == nullptr)
Zachary Turner863f8c12019-01-11 18:03:20 +0000710 sc.function =
711 ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000712 if (sc.function) {
713 resolved_flags |= eSymbolContextFunction;
714 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000715 auto block_symbol = m_session_up->findSymbolByAddress(
716 file_vm_addr, PDB_SymType::Block);
717 auto block_id = block_symbol ? block_symbol->getSymIndexId()
718 : sc.function->GetID();
719 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000720 if (sc.block)
721 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000722 }
723 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000724 }
725 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000726
727 if (resolve_scope & eSymbolContextLineEntry) {
728 if (auto *line_table = sc.comp_unit->GetLineTable()) {
729 Address addr(so_addr);
730 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
731 resolved_flags |= eSymbolContextLineEntry;
732 }
733 }
734
Aaron Smith7ac1c782018-02-09 05:31:28 +0000735 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736}
737
738uint32_t SymbolFilePDB::ResolveSymbolContext(
739 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000740 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000741 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 if (resolve_scope & lldb::eSymbolContextCompUnit) {
743 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000744 // file. For example, if `file_spec` is <vector>, then this should return
745 // all source files and header files that reference <vector>, either
746 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 auto compilands = m_session_up->findCompilandsForSourceFile(
748 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
749
Aaron Smith10a02572018-01-13 06:58:18 +0000750 if (!compilands)
751 return 0;
752
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000753 // For each one, either find its previously parsed data or parse it afresh
754 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000756 // If we're not checking inlines, then don't add line information for
757 // this file unless the FileSpec matches. For inline functions, we don't
758 // have to match the FileSpec since they could be defined in headers
759 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000761 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000762 if (source_file.empty())
763 continue;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000764 FileSpec this_spec(source_file, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000765 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
766 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 continue;
768 }
769
770 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000771 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000772 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000773 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 sc.comp_unit = cu.get();
775 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776
777 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000778 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000779 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
780 eSymbolContextLineEntry)) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000781 bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000782
783 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
784 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000785 // compile unit. This is not normal for `line` = 0. So just assert
786 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000787 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000788
789 // Current compiland does not have the requested line. Search next.
790 continue;
791 }
792
793 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
794 if (!has_line_table)
795 continue;
796
797 auto *line_table = sc.comp_unit->GetLineTable();
798 lldbassert(line_table);
799
800 uint32_t num_line_entries = line_table->GetSize();
801 // Skip the terminal line entry.
802 --num_line_entries;
803
Adrian Prantl05097242018-04-30 16:49:04 +0000804 // If `line `!= 0, see if we can resolve function for each line entry
805 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000806 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
807 ++line_idx) {
808 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
809 continue;
810
811 auto file_vm_addr =
812 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000813 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000814 continue;
815
Aaron Smithc8316ed2018-03-22 03:44:51 +0000816 auto symbol_up = m_session_up->findSymbolByAddress(
817 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000818 if (symbol_up) {
819 auto func_uid = symbol_up->getSymIndexId();
820 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
821 if (sc.function == nullptr) {
822 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
823 assert(pdb_func);
Zachary Turner863f8c12019-01-11 18:03:20 +0000824 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func,
825 *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000826 }
827 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
828 Block &block = sc.function->GetBlock(true);
829 sc.block = block.FindBlockByID(sc.function->GetID());
830 }
831 }
832 sc_list.Append(sc);
833 }
834 } else if (has_line_table) {
835 // We can parse line table for the compile unit. But no query to
836 // resolve function or block. We append `sc` to the list anyway.
837 sc_list.Append(sc);
838 }
839 } else {
840 // No query for line entry, function or block. But we have a valid
841 // compile unit, append `sc` to the list.
842 sc_list.Append(sc);
843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 }
Aaron Smith10a02572018-01-13 06:58:18 +0000846 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847}
848
Aaron Smithcab0d232018-05-23 01:52:42 +0000849std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000850 // Cache public names at first
851 if (m_public_names.empty())
852 if (auto result_up =
853 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
854 while (auto symbol_up = result_up->getNext())
855 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
856 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000857
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000858 // Look up the name in the cache
859 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000860}
861
862VariableSP SymbolFilePDB::ParseVariableForPDBData(
863 const lldb_private::SymbolContext &sc,
864 const llvm::pdb::PDBSymbolData &pdb_data) {
865 VariableSP var_sp;
866 uint32_t var_uid = pdb_data.getSymIndexId();
867 auto result = m_variables.find(var_uid);
868 if (result != m_variables.end())
869 return result->second;
870
871 ValueType scope = eValueTypeInvalid;
872 bool is_static_member = false;
873 bool is_external = false;
874 bool is_artificial = false;
875
876 switch (pdb_data.getDataKind()) {
877 case PDB_DataKind::Global:
878 scope = eValueTypeVariableGlobal;
879 is_external = true;
880 break;
881 case PDB_DataKind::Local:
882 scope = eValueTypeVariableLocal;
883 break;
884 case PDB_DataKind::FileStatic:
885 scope = eValueTypeVariableStatic;
886 break;
887 case PDB_DataKind::StaticMember:
888 is_static_member = true;
889 scope = eValueTypeVariableStatic;
890 break;
891 case PDB_DataKind::Member:
892 scope = eValueTypeVariableStatic;
893 break;
894 case PDB_DataKind::Param:
895 scope = eValueTypeVariableArgument;
896 break;
897 case PDB_DataKind::Constant:
898 scope = eValueTypeConstResult;
899 break;
900 default:
901 break;
902 }
903
904 switch (pdb_data.getLocationType()) {
905 case PDB_LocType::TLS:
906 scope = eValueTypeVariableThreadLocal;
907 break;
908 case PDB_LocType::RegRel: {
909 // It is a `this` pointer.
910 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
911 scope = eValueTypeVariableArgument;
912 is_artificial = true;
913 }
914 } break;
915 default:
916 break;
917 }
918
919 Declaration decl;
920 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
921 if (auto lines = pdb_data.getLineNumbers()) {
922 if (auto first_line = lines->getNext()) {
923 uint32_t src_file_id = first_line->getSourceFileId();
924 auto src_file = m_session_up->getSourceFileById(src_file_id);
925 if (src_file) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000926 FileSpec spec(src_file->getFileName());
Aaron Smithcab0d232018-05-23 01:52:42 +0000927 decl.SetFile(spec);
928 decl.SetColumn(first_line->getColumnNumber());
929 decl.SetLine(first_line->getLineNumber());
930 }
931 }
932 }
933 }
934
935 Variable::RangeList ranges;
936 SymbolContextScope *context_scope = sc.comp_unit;
937 if (scope == eValueTypeVariableLocal) {
938 if (sc.function) {
939 context_scope = sc.function->GetBlock(true).FindBlockByID(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000940 pdb_data.getLexicalParentId());
Aaron Smithcab0d232018-05-23 01:52:42 +0000941 if (context_scope == nullptr)
942 context_scope = sc.function;
943 }
944 }
945
946 SymbolFileTypeSP type_sp =
947 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
948
949 auto var_name = pdb_data.getName();
950 auto mangled = GetMangledForPDBData(pdb_data);
951 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
952
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000953 bool is_constant;
954 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
955 GetObjectFile()->GetModule(), pdb_data, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000956
957 var_sp = std::make_shared<Variable>(
958 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
959 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000960 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000961
962 m_variables.insert(std::make_pair(var_uid, var_sp));
963 return var_sp;
964}
965
966size_t
967SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
968 const llvm::pdb::PDBSymbol &pdb_symbol,
969 lldb_private::VariableList *variable_list) {
970 size_t num_added = 0;
971
972 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
973 VariableListSP local_variable_list_sp;
974
975 auto result = m_variables.find(pdb_data->getSymIndexId());
976 if (result != m_variables.end()) {
977 if (variable_list)
978 variable_list->AddVariableIfUnique(result->second);
979 } else {
980 // Prepare right VariableList for this variable.
981 if (auto lexical_parent = pdb_data->getLexicalParent()) {
982 switch (lexical_parent->getSymTag()) {
983 case PDB_SymType::Exe:
984 assert(sc.comp_unit);
985 LLVM_FALLTHROUGH;
986 case PDB_SymType::Compiland: {
987 if (sc.comp_unit) {
988 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
989 if (!local_variable_list_sp) {
990 local_variable_list_sp = std::make_shared<VariableList>();
991 sc.comp_unit->SetVariableList(local_variable_list_sp);
992 }
993 }
994 } break;
995 case PDB_SymType::Block:
996 case PDB_SymType::Function: {
997 if (sc.function) {
998 Block *block = sc.function->GetBlock(true).FindBlockByID(
999 lexical_parent->getSymIndexId());
1000 if (block) {
1001 local_variable_list_sp = block->GetBlockVariableList(false);
1002 if (!local_variable_list_sp) {
1003 local_variable_list_sp = std::make_shared<VariableList>();
1004 block->SetVariableList(local_variable_list_sp);
1005 }
1006 }
1007 }
1008 } break;
1009 default:
1010 break;
1011 }
1012 }
1013
1014 if (local_variable_list_sp) {
1015 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1016 local_variable_list_sp->AddVariableIfUnique(var_sp);
1017 if (variable_list)
1018 variable_list->AddVariableIfUnique(var_sp);
1019 ++num_added;
Zachary Turnerc68925a2019-01-09 21:20:44 +00001020 PDBASTParser *ast = GetPDBAstParser();
1021 if (ast)
1022 ast->GetDeclForSymbol(*pdb_data);
Aaron Smithcab0d232018-05-23 01:52:42 +00001023 }
1024 }
1025 }
1026 }
1027
1028 if (auto results = pdb_symbol.findAllChildren()) {
1029 while (auto result = results->getNext())
1030 num_added += ParseVariables(sc, *result, variable_list);
1031 }
1032
1033 return num_added;
1034}
1035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036uint32_t SymbolFilePDB::FindGlobalVariables(
1037 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +00001038 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001040 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1041 return 0;
1042 if (name.IsEmpty())
1043 return 0;
1044
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001045 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001046 if (!results)
1047 return 0;
1048
1049 uint32_t matches = 0;
1050 size_t old_size = variables.GetSize();
1051 while (auto result = results->getNext()) {
1052 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1053 if (max_matches > 0 && matches >= max_matches)
1054 break;
1055
1056 SymbolContext sc;
1057 sc.module_sp = m_obj_file->GetModule();
1058 lldbassert(sc.module_sp.get());
1059
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001060 if (!name.GetStringRef().equals(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001061 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001062 continue;
1063
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001064 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1065 // FIXME: We are not able to determine the compile unit.
1066 if (sc.comp_unit == nullptr)
1067 continue;
1068
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001069 if (parent_decl_ctx && GetDeclContextContainingUID(
1070 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001071 continue;
1072
Aaron Smithcab0d232018-05-23 01:52:42 +00001073 ParseVariables(sc, *pdb_data, &variables);
1074 matches = variables.GetSize() - old_size;
1075 }
1076
1077 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078}
1079
1080uint32_t
1081SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001082 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001084 if (!regex.IsValid())
1085 return 0;
1086 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1087 if (!results)
1088 return 0;
1089
1090 uint32_t matches = 0;
1091 size_t old_size = variables.GetSize();
1092 while (auto pdb_data = results->getNext()) {
1093 if (max_matches > 0 && matches >= max_matches)
1094 break;
1095
1096 auto var_name = pdb_data->getName();
1097 if (var_name.empty())
1098 continue;
1099 if (!regex.Execute(var_name))
1100 continue;
1101 SymbolContext sc;
1102 sc.module_sp = m_obj_file->GetModule();
1103 lldbassert(sc.module_sp.get());
1104
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001105 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001106 // FIXME: We are not able to determine the compile unit.
1107 if (sc.comp_unit == nullptr)
1108 continue;
1109
1110 ParseVariables(sc, *pdb_data, &variables);
1111 matches = variables.GetSize() - old_size;
1112 }
1113
1114 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115}
1116
Aaron Smithe664b5d2018-03-19 21:14:19 +00001117bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001118 bool include_inlines,
1119 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001120 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001121 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001122 if (!sc.comp_unit)
1123 return false;
1124 sc.module_sp = sc.comp_unit->GetModule();
Zachary Turner863f8c12019-01-11 18:03:20 +00001125 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001126 if (!sc.function)
1127 return false;
1128
1129 sc_list.Append(sc);
1130 return true;
1131}
1132
1133bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1134 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001135 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001136 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1137 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001138 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001139}
1140
1141void SymbolFilePDB::CacheFunctionNames() {
1142 if (!m_func_full_names.IsEmpty())
1143 return;
1144
1145 std::map<uint64_t, uint32_t> addr_ids;
1146
1147 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1148 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001149 if (pdb_func_up->isCompilerGenerated())
1150 continue;
1151
Aaron Smith7ac1c782018-02-09 05:31:28 +00001152 auto name = pdb_func_up->getName();
1153 auto demangled_name = pdb_func_up->getUndecoratedName();
1154 if (name.empty() && demangled_name.empty())
1155 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001156
Aaron Smithf76fe682018-03-07 03:16:50 +00001157 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001158 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1159 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1160
1161 if (auto parent = pdb_func_up->getClassParent()) {
1162
1163 // PDB have symbols for class/struct methods or static methods in Enum
1164 // Class. We won't bother to check if the parent is UDT or Enum here.
1165 m_func_method_names.Append(ConstString(name), uid);
1166
Adrian Prantl05097242018-04-30 16:49:04 +00001167 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1168 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1169 // not have inforamtion of this, we extract base names and cache them
1170 // by our own effort.
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001171 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001172 if (!basename.empty())
1173 m_func_base_names.Append(ConstString(basename), uid);
1174 else {
1175 m_func_base_names.Append(ConstString(name), uid);
1176 }
1177
1178 if (!demangled_name.empty())
1179 m_func_full_names.Append(ConstString(demangled_name), uid);
1180
1181 } else {
1182 // Handle not-method symbols.
1183
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001184 // The function name might contain namespace, or its lexical scope.
1185 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1186 if (!basename.empty())
1187 m_func_base_names.Append(ConstString(basename), uid);
1188 else
1189 m_func_base_names.Append(ConstString(name), uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001190
1191 if (name == "main") {
1192 m_func_full_names.Append(ConstString(name), uid);
1193
1194 if (!demangled_name.empty() && name != demangled_name) {
1195 m_func_full_names.Append(ConstString(demangled_name), uid);
1196 m_func_base_names.Append(ConstString(demangled_name), uid);
1197 }
1198 } else if (!demangled_name.empty()) {
1199 m_func_full_names.Append(ConstString(demangled_name), uid);
1200 } else {
1201 m_func_full_names.Append(ConstString(name), uid);
1202 }
1203 }
1204 }
1205 }
1206
1207 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001208 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001209 while (auto pub_sym_up = results_up->getNext()) {
1210 if (!pub_sym_up->isFunction())
1211 continue;
1212 auto name = pub_sym_up->getName();
1213 if (name.empty())
1214 continue;
1215
1216 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001217 auto vm_addr = pub_sym_up->getVirtualAddress();
1218
1219 // PDB public symbol has mangled name for its associated function.
1220 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1221 // Cache mangled name.
1222 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1223 }
1224 }
1225 }
1226 }
1227 // Sort them before value searching is working properly
1228 m_func_full_names.Sort();
1229 m_func_full_names.SizeToFit();
1230 m_func_method_names.Sort();
1231 m_func_method_names.SizeToFit();
1232 m_func_base_names.Sort();
1233 m_func_base_names.SizeToFit();
1234}
1235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236uint32_t SymbolFilePDB::FindFunctions(
1237 const lldb_private::ConstString &name,
1238 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001239 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001241 if (!append)
1242 sc_list.Clear();
1243 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1244
1245 if (name_type_mask == eFunctionNameTypeNone)
1246 return 0;
1247 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1248 return 0;
1249 if (name.IsEmpty())
1250 return 0;
1251
1252 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001253 if (name_type_mask & eFunctionNameTypeFull ||
1254 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001255 name_type_mask & eFunctionNameTypeMethod) {
1256 CacheFunctionNames();
1257
1258 std::set<uint32_t> resolved_ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001259 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1260 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001261 std::vector<uint32_t> ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001262 if (!Names.GetValues(name, ids))
1263 return;
1264
1265 for (uint32_t id : ids) {
1266 if (resolved_ids.find(id) != resolved_ids.end())
1267 continue;
1268
1269 if (parent_decl_ctx &&
1270 GetDeclContextContainingUID(id) != *parent_decl_ctx)
1271 continue;
1272
1273 if (ResolveFunction(id, include_inlines, sc_list))
1274 resolved_ids.insert(id);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001275 }
1276 };
1277 if (name_type_mask & eFunctionNameTypeFull) {
1278 ResolveFn(m_func_full_names);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001279 ResolveFn(m_func_base_names);
1280 ResolveFn(m_func_method_names);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001281 }
1282 if (name_type_mask & eFunctionNameTypeBase) {
1283 ResolveFn(m_func_base_names);
1284 }
1285 if (name_type_mask & eFunctionNameTypeMethod) {
1286 ResolveFn(m_func_method_names);
1287 }
1288 }
1289 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290}
1291
1292uint32_t
1293SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1294 bool include_inlines, bool append,
1295 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001296 if (!append)
1297 sc_list.Clear();
1298 if (!regex.IsValid())
1299 return 0;
1300
1301 auto old_size = sc_list.GetSize();
1302 CacheFunctionNames();
1303
1304 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001305 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1306 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001307 std::vector<uint32_t> ids;
1308 if (Names.GetValues(regex, ids)) {
1309 for (auto id : ids) {
1310 if (resolved_ids.find(id) == resolved_ids.end())
1311 if (ResolveFunction(id, include_inlines, sc_list))
1312 resolved_ids.insert(id);
1313 }
1314 }
1315 };
1316 ResolveFn(m_func_full_names);
1317 ResolveFn(m_func_base_names);
1318
1319 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320}
1321
1322void SymbolFilePDB::GetMangledNamesForFunction(
1323 const std::string &scope_qualified_name,
1324 std::vector<lldb_private::ConstString> &mangled_names) {}
1325
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001326void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1327 std::set<lldb::addr_t> sym_addresses;
1328 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1329 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
1330
1331 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1332 if (!results)
1333 return;
1334
1335 auto section_list = m_obj_file->GetSectionList();
1336 if (!section_list)
1337 return;
1338
1339 while (auto pub_symbol = results->getNext()) {
1340 auto section_idx = pub_symbol->getAddressSection() - 1;
1341 if (section_idx >= section_list->GetSize())
1342 continue;
1343
1344 auto section = section_list->GetSectionAtIndex(section_idx);
1345 if (!section)
1346 continue;
1347
1348 auto offset = pub_symbol->getAddressOffset();
1349
1350 auto file_addr = section->GetFileAddress() + offset;
1351 if (sym_addresses.find(file_addr) != sym_addresses.end())
1352 continue;
1353 sym_addresses.insert(file_addr);
1354
1355 auto size = pub_symbol->getLength();
1356 symtab.AddSymbol(
1357 Symbol(pub_symbol->getSymIndexId(), // symID
1358 pub_symbol->getName().c_str(), // name
1359 true, // name_is_mangled
1360 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1361 true, // external
1362 false, // is_debug
1363 false, // is_trampoline
1364 false, // is_artificial
1365 section, // section_sp
1366 offset, // value
1367 size, // size
1368 size != 0, // size_is_valid
1369 false, // contains_linker_annotations
1370 0 // flags
1371 ));
1372 }
1373
1374 symtab.CalculateSymbolSizes();
1375 symtab.Finalize();
1376}
1377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378uint32_t SymbolFilePDB::FindTypes(
1379 const lldb_private::SymbolContext &sc,
1380 const lldb_private::ConstString &name,
1381 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1382 uint32_t max_matches,
1383 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1384 lldb_private::TypeMap &types) {
1385 if (!append)
1386 types.Clear();
1387 if (!name)
1388 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001389 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1390 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391
1392 searched_symbol_files.clear();
1393 searched_symbol_files.insert(this);
1394
Aaron Smith86e94342017-12-22 05:26:50 +00001395 // There is an assumption 'name' is not a regex
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001396 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 return types.GetSize();
1399}
1400
Zachary Turner49110232018-11-05 17:40:28 +00001401void SymbolFilePDB::DumpClangAST(Stream &s) {
1402 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1403 auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1404 if (!clang)
1405 return;
1406 clang->Dump(s);
1407}
1408
Aaron Smithc8316ed2018-03-22 03:44:51 +00001409void SymbolFilePDB::FindTypesByRegex(
1410 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1411 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001413 // space as much as possible since this searches EVERYTHING in the PDB,
1414 // manually doing regex comparisons. PDB library isn't optimized for regex
1415 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001417 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1419 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 std::unique_ptr<IPDBEnumSymbols> results;
1421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 uint32_t matches = 0;
1423
1424 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001425 results = m_global_scope_up->findAllChildren(tag);
1426 if (!results)
1427 continue;
1428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 while (auto result = results->getNext()) {
1430 if (max_matches > 0 && matches >= max_matches)
1431 break;
1432
1433 std::string type_name;
1434 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1435 type_name = enum_type->getName();
1436 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001437 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 type_name = typedef_type->getName();
1439 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1440 type_name = class_type->getName();
1441 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001442 // We're looking only for types that have names. Skip symbols, as well
1443 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 continue;
1445 }
1446
Aaron Smith86e94342017-12-22 05:26:50 +00001447 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 continue;
1449
1450 // This should cause the type to get cached and stored in the `m_types`
1451 // lookup.
1452 if (!ResolveTypeUID(result->getSymIndexId()))
1453 continue;
1454
1455 auto iter = m_types.find(result->getSymIndexId());
1456 if (iter == m_types.end())
1457 continue;
1458 types.Insert(iter->second);
1459 ++matches;
1460 }
1461 }
1462}
1463
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001464void SymbolFilePDB::FindTypesByName(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001465 llvm::StringRef name,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001466 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1467 uint32_t max_matches, lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001469 if (name.empty())
1470 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001471 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001472 if (!results)
1473 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474
1475 uint32_t matches = 0;
1476
1477 while (auto result = results->getNext()) {
1478 if (max_matches > 0 && matches >= max_matches)
1479 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001480
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001481 if (MSVCUndecoratedNameParser::DropScope(
1482 result->getRawSymbol().getName()) != name)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001483 continue;
1484
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 switch (result->getSymTag()) {
1486 case PDB_SymType::Enum:
1487 case PDB_SymType::UDT:
1488 case PDB_SymType::Typedef:
1489 break;
1490 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001491 // We're looking only for types that have names. Skip symbols, as well
1492 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 continue;
1494 }
1495
1496 // This should cause the type to get cached and stored in the `m_types`
1497 // lookup.
1498 if (!ResolveTypeUID(result->getSymIndexId()))
1499 continue;
1500
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001501 if (parent_decl_ctx && GetDeclContextContainingUID(
1502 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001503 continue;
1504
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 auto iter = m_types.find(result->getSymIndexId());
1506 if (iter == m_types.end())
1507 continue;
1508 types.Insert(iter->second);
1509 ++matches;
1510 }
1511}
1512
1513size_t SymbolFilePDB::FindTypes(
1514 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1515 lldb_private::TypeMap &types) {
1516 return 0;
1517}
1518
Aaron Smithec40f812018-01-23 20:35:19 +00001519lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1520 return m_obj_file->GetModule()->GetTypeList();
1521}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522
Aaron Smithc8316ed2018-03-22 03:44:51 +00001523void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1524 uint32_t type_mask,
1525 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001526 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001527 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001528 case PDB_SymType::ArrayType:
1529 can_parse = ((type_mask & eTypeClassArray) != 0);
1530 break;
1531 case PDB_SymType::BuiltinType:
1532 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1533 break;
1534 case PDB_SymType::Enum:
1535 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1536 break;
1537 case PDB_SymType::Function:
1538 case PDB_SymType::FunctionSig:
1539 can_parse = ((type_mask & eTypeClassFunction) != 0);
1540 break;
1541 case PDB_SymType::PointerType:
1542 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1543 eTypeClassMemberPointer)) != 0);
1544 break;
1545 case PDB_SymType::Typedef:
1546 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1547 break;
1548 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001549 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001550 assert(udt);
1551 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001552 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1553 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001554 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001555 default:
1556 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001557 }
1558
1559 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001560 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001561 auto result =
1562 std::find(type_collection.begin(), type_collection.end(), type);
1563 if (result == type_collection.end())
1564 type_collection.push_back(type);
1565 }
1566 }
1567
Aaron Smithe664b5d2018-03-19 21:14:19 +00001568 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001569 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001570 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001571}
1572
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001574 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001576 TypeCollection type_collection;
1577 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001578 CompileUnit *cu =
1579 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001580 if (cu) {
1581 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001582 if (!compiland_up)
1583 return 0;
1584 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001585 } else {
1586 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1587 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001588 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001589 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1590 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001591 }
1592 }
1593 }
1594
1595 for (auto type : type_collection) {
1596 type->GetForwardCompilerType();
1597 type_list.Insert(type->shared_from_this());
1598 }
1599 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001600}
1601
1602lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1604 auto type_system =
1605 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1606 if (type_system)
1607 type_system->SetSymbolFile(this);
1608 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001609}
1610
Zachary Turnerc68925a2019-01-09 21:20:44 +00001611PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
1612 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1613 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1614 if (!clang_type_system)
1615 return nullptr;
1616
1617 return clang_type_system->GetPDBParser();
1618}
1619
1620
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1622 const lldb_private::SymbolContext &sc,
1623 const lldb_private::ConstString &name,
1624 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001625 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1626 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1627 if (!clang_type_system)
1628 return CompilerDeclContext();
1629
1630 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1631 if (!pdb)
1632 return CompilerDeclContext();
1633
1634 clang::DeclContext *decl_context = nullptr;
1635 if (parent_decl_ctx)
1636 decl_context = static_cast<clang::DeclContext *>(
1637 parent_decl_ctx->GetOpaqueDeclContext());
1638
1639 auto namespace_decl =
1640 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1641 if (!namespace_decl)
1642 return CompilerDeclContext();
1643
1644 return CompilerDeclContext(type_system,
1645 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001646}
1647
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1649 static ConstString g_name("pdb");
1650 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001651}
1652
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1654
1655IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1656
1657const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1658 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001659}
1660
Aaron Smithc8316ed2018-03-22 03:44:51 +00001661lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1662 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 auto found_cu = m_comp_units.find(id);
1664 if (found_cu != m_comp_units.end())
1665 return found_cu->second;
1666
Aaron Smith10a02572018-01-13 06:58:18 +00001667 auto compiland_up = GetPDBCompilandByUID(id);
1668 if (!compiland_up)
1669 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670
1671 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001672 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 if (!details)
1674 lang = lldb::eLanguageTypeC_plus_plus;
1675 else
1676 lang = TranslateLanguage(details->getLanguage());
1677
Aaron Smithf76fe682018-03-07 03:16:50 +00001678 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1679 return CompUnitSP();
1680
Aaron Smith487b0c62018-03-20 00:18:22 +00001681 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001682 if (path.empty())
1683 return CompUnitSP();
1684
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 // Don't support optimized code for now, DebugInfoPDB does not return this
1686 // information.
1687 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001688 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1689 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001690
1691 if (!cu_sp)
1692 return CompUnitSP();
1693
1694 m_comp_units.insert(std::make_pair(id, cu_sp));
1695 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001696 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001697 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001698 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1699 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001700 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001701}
1702
Zachary Turner863f8c12019-01-11 18:03:20 +00001703bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1704 uint32_t match_line) {
1705 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
Aaron Smith10a02572018-01-13 06:58:18 +00001706 if (!compiland_up)
1707 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001708
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001710 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001711 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1712 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001714 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Zachary Turner863f8c12019-01-11 18:03:20 +00001715 auto line_table = llvm::make_unique<LineTable>(&comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001716
Aaron Smith10a02572018-01-13 06:58:18 +00001717 // Find contributions to `compiland` from all source and header files.
Zachary Turner863f8c12019-01-11 18:03:20 +00001718 std::string path = comp_unit.GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001719 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1720 if (!files)
1721 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001722
Adrian Prantl05097242018-04-30 16:49:04 +00001723 // For each source and header file, create a LineSequence for contributions
1724 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 while (auto file = files->getNext()) {
1726 std::unique_ptr<LineSequence> sequence(
1727 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001728 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1729 if (!lines)
1730 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001732
Kate Stoneb9c1b512016-09-06 20:57:50 +00001733 uint64_t prev_addr;
1734 uint32_t prev_length;
1735 uint32_t prev_line;
1736 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001737
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 for (int i = 0; i < entry_count; ++i) {
1739 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 uint64_t lno = line->getLineNumber();
1742 uint64_t addr = line->getVirtualAddress();
1743 uint32_t length = line->getLength();
1744 uint32_t source_id = line->getSourceFileId();
1745 uint32_t col = line->getColumnNumber();
1746 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001747
Adrian Prantl05097242018-04-30 16:49:04 +00001748 // There was a gap between the current entry and the previous entry if
1749 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001751
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001753 // of the previous entry's address range if the current entry resulted in
1754 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1756 line_table->AppendLineEntryToSequence(
1757 sequence.get(), prev_addr + prev_length, prev_line, 0,
1758 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001759
1760 line_table->InsertSequence(sequence.release());
1761 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001763
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 if (ShouldAddLine(match_line, lno, length)) {
1765 bool is_statement = line->isStatement();
1766 bool is_prologue = false;
1767 bool is_epilogue = false;
1768 auto func =
1769 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1770 if (func) {
1771 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001772 if (prologue)
1773 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001774
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001776 if (epilogue)
1777 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001778 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001779
Kate Stoneb9c1b512016-09-06 20:57:50 +00001780 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1781 source_idx, is_statement, false,
1782 is_prologue, is_epilogue, false);
1783 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001784
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 prev_addr = addr;
1786 prev_length = length;
1787 prev_line = lno;
1788 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001789 }
1790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1792 // The end is always a terminal entry, so insert it regardless.
1793 line_table->AppendLineEntryToSequence(
1794 sequence.get(), prev_addr + prev_length, prev_line, 0,
1795 prev_source_idx, false, false, false, false, true);
1796 }
1797
1798 line_table->InsertSequence(sequence.release());
1799 }
1800
Aaron Smith10a02572018-01-13 06:58:18 +00001801 if (line_table->GetSize()) {
Zachary Turner863f8c12019-01-11 18:03:20 +00001802 comp_unit.SetLineTable(line_table.release());
Aaron Smith10a02572018-01-13 06:58:18 +00001803 return true;
1804 }
1805 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001806}
1807
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001809 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001811 // This is a hack, but we need to convert the source id into an index into
1812 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001813 // basename / full path issues that may or may not even be a problem, so we
1814 // use the globally unique source file identifiers. Ideally we could use the
1815 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001816 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1817 if (!source_files)
1818 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001819
1820 // LLDB uses the DWARF-like file numeration (one based)
1821 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001822
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823 while (auto file = source_files->getNext()) {
1824 uint32_t source_id = file->getUniqueId();
1825 index_map[source_id] = index++;
1826 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001827}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001828
1829lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001830 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001831 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001832 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001833 return nullptr;
1834
Aaron Smith308e39c2018-03-22 19:26:33 +00001835 // If it is a PDB function's vm addr, this is the first sure bet.
1836 if (auto lines =
1837 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1838 if (auto first_line = lines->getNext())
1839 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001840 }
1841
Aaron Smith308e39c2018-03-22 19:26:33 +00001842 // Otherwise we resort to section contributions.
1843 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1844 while (auto section = sec_contribs->getNext()) {
1845 auto va = section->getVirtualAddress();
1846 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1847 return ParseCompileUnitForUID(section->getCompilandId());
1848 }
1849 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001850 return nullptr;
1851}
1852
1853Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001854SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001855 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001856 auto func_name = pdb_func.getName();
1857 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001858 std::string func_decorated_name;
1859
1860 // Seek from public symbols for non-static function's decorated name if any.
1861 // For static functions, they don't have undecorated names and aren't exposed
1862 // in Public Symbols either.
1863 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001864 auto result_up = m_global_scope_up->findChildren(
1865 PDB_SymType::PublicSymbol, func_undecorated_name,
1866 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001867 if (result_up) {
1868 while (auto symbol_up = result_up->getNext()) {
1869 // For a public symbol, it is unique.
1870 lldbassert(result_up->getChildCount() == 1);
1871 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001872 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1873 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001874 if (pdb_public_sym->isFunction()) {
1875 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001876 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001877 }
1878 }
1879 }
1880 }
1881 }
1882 if (!func_decorated_name.empty()) {
1883 mangled.SetMangledName(ConstString(func_decorated_name));
1884
1885 // For MSVC, format of C funciton's decorated name depends on calling
1886 // conventon. Unfortunately none of the format is recognized by current
1887 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001888 // `__purecall` is retrieved as both its decorated and undecorated name
1889 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1890 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1891 // Mangled::GetDemangledName method will fail internally and caches an
1892 // empty string as its undecorated name. So we will face a contradition
1893 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001894 // non-empty undecorated name from PDB
1895 // empty undecorated name from LLDB
1896 if (!func_undecorated_name.empty() &&
1897 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1898 mangled.SetDemangledName(ConstString(func_undecorated_name));
1899
1900 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001901 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1902 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001903 // PDB source unless we also apply same flags in getting undecorated
1904 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1905 if (!func_undecorated_name.empty() &&
1906 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1907 ConstString(func_undecorated_name))
1908 mangled.SetDemangledName(ConstString(func_undecorated_name));
1909 } else if (!func_undecorated_name.empty()) {
1910 mangled.SetDemangledName(ConstString(func_undecorated_name));
1911 } else if (!func_name.empty())
1912 mangled.SetValue(ConstString(func_name), false);
1913
1914 return mangled;
1915}
1916
1917bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1918 const lldb_private::CompilerDeclContext *decl_ctx) {
1919 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1920 return true;
1921
1922 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1923 if (!decl_ctx_type_system)
1924 return false;
1925 TypeSystem *type_system = GetTypeSystemForLanguage(
1926 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1927 if (decl_ctx_type_system == type_system)
1928 return true; // The type systems match, return true
1929
1930 return false;
1931}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001932
1933uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1934 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1935 return lhs < rhs.Offset;
1936 };
1937
1938 // Cache section contributions
1939 if (m_sec_contribs.empty()) {
1940 if (auto SecContribs = m_session_up->getSectionContribs()) {
1941 while (auto SectionContrib = SecContribs->getNext()) {
1942 auto comp_id = SectionContrib->getCompilandId();
1943 if (!comp_id)
1944 continue;
1945
1946 auto sec = SectionContrib->getAddressSection();
1947 auto &sec_cs = m_sec_contribs[sec];
1948
1949 auto offset = SectionContrib->getAddressOffset();
1950 auto it =
1951 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
1952
1953 auto size = SectionContrib->getLength();
1954 sec_cs.insert(it, {offset, size, comp_id});
1955 }
1956 }
1957 }
1958
1959 // Check by line number
1960 if (auto Lines = data.getLineNumbers()) {
1961 if (auto FirstLine = Lines->getNext())
1962 return FirstLine->getCompilandId();
1963 }
1964
1965 // Retrieve section + offset
1966 uint32_t DataSection = data.getAddressSection();
1967 uint32_t DataOffset = data.getAddressOffset();
1968 if (DataSection == 0) {
1969 if (auto RVA = data.getRelativeVirtualAddress())
1970 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
1971 }
1972
1973 if (DataSection) {
1974 // Search by section contributions
1975 auto &sec_cs = m_sec_contribs[DataSection];
1976 auto it =
1977 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
1978 if (it != sec_cs.begin()) {
1979 --it;
1980 if (DataOffset < it->Offset + it->Size)
1981 return it->CompilandId;
1982 }
1983 } else {
1984 // Search in lexical tree
1985 auto LexParentId = data.getLexicalParentId();
1986 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
1987 if (LexParent->getSymTag() == PDB_SymType::Exe)
1988 break;
1989 if (LexParent->getSymTag() == PDB_SymType::Compiland)
1990 return LexParentId;
1991 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
1992 }
1993 }
1994
1995 return 0;
1996}