blob: f698798ab24013f07de2d9175d741f73db6c73b2 [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFilePDB.h"
11
Zachary Turner42dff792016-04-15 00:21:26 +000012#include "clang/Lex/Lexer.h"
13
Zachary Turner74e08ca2016-03-02 22:05:52 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/LineTable.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000021#include "lldb/Symbol/SymbolVendor.h"
Zachary Turner42dff792016-04-15 00:21:26 +000022#include "lldb/Symbol/TypeMap.h"
Aaron Smithec40f812018-01-23 20:35:19 +000023#include "lldb/Symbol/TypeList.h"
Aaron Smith86e94342017-12-22 05:26:50 +000024#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000025
Pavel Labathb8d8c622016-05-09 11:07:43 +000026#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000027#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000028#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
29#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
30#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000031#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000033#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000036#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000037#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
38#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000041#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000042#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
45
Aaron Smith7ac1c782018-02-09 05:31:28 +000046#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner42dff792016-04-15 00:21:26 +000047#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
48
49#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000050
Aaron Smith10a02572018-01-13 06:58:18 +000051using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000052using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000053using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055namespace {
56lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
57 switch (lang) {
58 case PDB_Lang::Cpp:
59 return lldb::LanguageType::eLanguageTypeC_plus_plus;
60 case PDB_Lang::C:
61 return lldb::LanguageType::eLanguageTypeC;
62 default:
63 return lldb::LanguageType::eLanguageTypeUnknown;
64 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
68 uint32_t addr_length) {
69 return ((requested_line == 0 || actual_line == requested_line) &&
70 addr_length > 0);
71}
Zachary Turner74e08ca2016-03-02 22:05:52 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074void SymbolFilePDB::Initialize() {
75 PluginManager::RegisterPlugin(GetPluginNameStatic(),
76 GetPluginDescriptionStatic(), CreateInstance,
77 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void SymbolFilePDB::Terminate() {
81 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
85
86lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
87 static ConstString g_name("pdb");
88 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091const char *SymbolFilePDB::GetPluginDescriptionStatic() {
92 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000093}
94
95lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000096SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
97 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +000098}
99
100SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000101 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
102 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000107 uint32_t abilities = 0;
108 if (!m_obj_file)
109 return 0;
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (!m_session_up) {
112 // Lazily load and match the PDB file, but only do this once.
113 std::string exePath = m_obj_file->GetFileSpec().GetPath();
114 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
115 m_session_up);
116 if (error) {
117 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000118 auto module_sp = m_obj_file->GetModule();
119 if (!module_sp)
120 return 0;
121 // See if any symbol file is specified through `--symfile` option.
122 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
123 if (!symfile)
124 return 0;
125 error = loadDataForPDB(PDB_ReaderType::DIA,
126 llvm::StringRef(symfile.GetPath()),
127 m_session_up);
128 if (error) {
129 llvm::consumeError(std::move(error));
130 return 0;
131 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 }
Aaron Smith1f8552a2017-12-22 00:04:36 +0000134 if (!m_session_up.get())
135 return 0;
136
137 auto enum_tables_up = m_session_up->getEnumTables();
138 if (!enum_tables_up)
139 return 0;
140 while (auto table_up = enum_tables_up->getNext()) {
141 if (table_up->getItemCount() == 0)
142 continue;
143 auto type = table_up->getTableType();
144 switch (type) {
145 case PDB_TableType::Symbols:
146 // This table represents a store of symbols with types listed in
147 // PDBSym_Type
148 abilities |= (CompileUnits | Functions | Blocks |
149 GlobalVariables | LocalVariables | VariableTypes);
150 break;
151 case PDB_TableType::LineNumbers:
152 abilities |= LineTables;
153 break;
154 default: break;
155 }
156 }
157 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void SymbolFilePDB::InitializeObject() {
161 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smith10a02572018-01-13 06:58:18 +0000162 lldbassert(obj_load_address &&
163 obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000165 if (!m_global_scope_up)
166 m_global_scope_up = m_session_up->getGlobalScope();
167 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 TypeSystem *type_system =
170 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
171 ClangASTContext *clang_type_system =
172 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000173 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
175 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178uint32_t SymbolFilePDB::GetNumCompileUnits() {
179 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000180 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
181 if (!compilands)
182 return 0;
183
184 // The linker could link *.dll (compiland language = LINK), or import
185 // *.dll. For example, a compiland with name `Import:KERNEL32.dll`
186 // could be found as a child of the global scope (PDB executable).
187 // Usually, such compilands contain `thunk` symbols in which we are not
188 // interested for now. However we still count them in the compiland list.
189 // If we perform any compiland related activity, like finding symbols
190 // through llvm::pdb::IPDBSession methods, such compilands will all be
191 // searched automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000195 // PDB. Ignore this special compile unit for our purposes, if it is there.
196 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000197 auto last_compiland_up =
198 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
199 lldbassert(last_compiland_up.get());
200 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (name == "* Linker *")
202 --m_cached_compile_unit_count;
203 }
204 return m_cached_compile_unit_count;
205}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000206
Aaron Smith10a02572018-01-13 06:58:18 +0000207void SymbolFilePDB::GetCompileUnitIndex(
208 const llvm::pdb::PDBSymbolCompiland *pdb_compiland,
209 uint32_t &index) {
210 if (!pdb_compiland)
211 return;
212
213 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
214 if (!results_up)
215 return;
216 auto uid = pdb_compiland->getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000217 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000218 auto compiland_up = results_up->getChildAtIndex(cu_idx);
219 if (!compiland_up)
220 continue;
221 if (compiland_up->getSymIndexId() == uid) {
222 index = cu_idx;
223 return;
224 }
225 }
226 index = UINT32_MAX;
227 return;
228}
229
230std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
231SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
232 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000236 if (index >= GetNumCompileUnits())
237 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238
Aaron Smith10a02572018-01-13 06:58:18 +0000239 // Assuming we always retrieve same compilands listed in same order through
240 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
241 // compile unit makes no sense.
242 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
243 if (!results)
244 return CompUnitSP();
245 auto compiland_up = results->getChildAtIndex(index);
246 if (!compiland_up)
247 return CompUnitSP();
248 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000249}
250
251lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
253 // What fields should I expect to be filled out on the SymbolContext? Is it
254 // safe to assume that `sc.comp_unit` is valid?
255 if (!sc.comp_unit)
256 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000257
Aaron Smith10a02572018-01-13 06:58:18 +0000258 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
259 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000261 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 if (!details)
263 return lldb::eLanguageTypeUnknown;
264 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000265}
266
Aaron Smith7ac1c782018-02-09 05:31:28 +0000267lldb_private::Function *
268SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
269 const PDBSymbolFunc *pdb_func,
270 const lldb_private::SymbolContext &sc) {
271 assert(pdb_func != nullptr);
272 lldbassert(sc.comp_unit && sc.module_sp.get());
273
274 auto file_vm_addr = pdb_func->getVirtualAddress();
275 if (file_vm_addr == LLDB_INVALID_ADDRESS)
276 return nullptr;
277
278 auto func_length = pdb_func->getLength();
279 AddressRange func_range = AddressRange(file_vm_addr,
280 func_length,
281 sc.module_sp->GetSectionList());
282 if (!func_range.GetBaseAddress().IsValid())
283 return nullptr;
284
285 user_id_t func_type_uid = pdb_func->getSignatureId();
286 // TODO: Function symbol with invalid signature won't be handled. We'll set up
287 // a white list to trace them.
288 if (!pdb_func->getSignature())
289 return nullptr;
290
291 lldb_private::Type* func_type = ResolveTypeUID(pdb_func->getSymIndexId());
292 if (!func_type)
293 return nullptr;
294
295 Mangled mangled = GetMangledForPDBFunc(pdb_func);
296
297 FunctionSP func_sp = std::make_shared<Function>(sc.comp_unit,
298 pdb_func->getSymIndexId(),
299 func_type_uid,
300 mangled,
301 func_type,
302 func_range);
303
304 sc.comp_unit->AddFunction(func_sp);
305 return func_sp.get();
306}
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308size_t SymbolFilePDB::ParseCompileUnitFunctions(
309 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000310 lldbassert(sc.comp_unit);
311 size_t func_added = 0;
312 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
313 if (!compiland_up)
314 return 0;
315 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
316 if (!results_up)
317 return 0;
318 while (auto pdb_func_up = results_up->getNext()) {
319 auto func_sp =
320 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
321 if (!func_sp) {
322 if (ParseCompileUnitFunctionForPDBFunc(pdb_func_up.get(), sc))
323 ++func_added;
324 }
325 }
326 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000327}
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329bool SymbolFilePDB::ParseCompileUnitLineTable(
330 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000331 lldbassert(sc.comp_unit);
332 if (sc.comp_unit->GetLineTable())
333 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000335}
336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337bool SymbolFilePDB::ParseCompileUnitDebugMacros(
338 const lldb_private::SymbolContext &sc) {
339 // PDB doesn't contain information about macros
340 return false;
341}
342
343bool SymbolFilePDB::ParseCompileUnitSupportFiles(
344 const lldb_private::SymbolContext &sc,
345 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000346 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000349 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
350 // second time seems like a waste. Unfortunately, there's no good way around
351 // this short of a moderate refactor since SymbolVendor depends on being able
352 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000353 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
354 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000355 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000356 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000358 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359
360 while (auto file = files->getNext()) {
Aaron Smith10a02572018-01-13 06:58:18 +0000361 FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
362 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 }
364 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000365}
366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367bool SymbolFilePDB::ParseImportedModules(
368 const lldb_private::SymbolContext &sc,
369 std::vector<lldb_private::ConstString> &imported_modules) {
370 // PDB does not yet support module debug info
371 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000372}
373
Aaron Smith7ac1c782018-02-09 05:31:28 +0000374static size_t
375ParseFunctionBlocksForPDBSymbol(const lldb_private::SymbolContext &sc,
376 uint64_t func_file_vm_addr,
377 const llvm::pdb::PDBSymbol *pdb_symbol,
378 lldb_private::Block *parent_block,
379 bool is_top_parent) {
380 assert(pdb_symbol && parent_block);
381
382 size_t num_added = 0;
383 switch (pdb_symbol->getSymTag()) {
384 case PDB_SymType::Block:
385 case PDB_SymType::Function: {
386 Block *block = nullptr;
387 auto &raw_sym = pdb_symbol->getRawSymbol();
388 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
389 if (pdb_func->hasNoInlineAttribute())
390 break;
391 if (is_top_parent)
392 block = parent_block;
393 else
394 break;
395 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
396 auto uid = pdb_symbol->getSymIndexId();
397 if (parent_block->FindBlockByID(uid))
398 break;
399 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
400 break;
401
402 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
403 parent_block->AddChild(block_sp);
404 block = block_sp.get();
405 } else
406 llvm_unreachable("Unexpected PDB symbol!");
407
408 block->AddRange(
409 Block::Range(raw_sym.getVirtualAddress() - func_file_vm_addr,
410 raw_sym.getLength()));
411 block->FinalizeRanges();
412 ++num_added;
413
414 auto results_up = pdb_symbol->findAllChildren();
415 if (!results_up)
416 break;
417 while (auto symbol_up = results_up->getNext()) {
418 num_added += ParseFunctionBlocksForPDBSymbol(sc, func_file_vm_addr,
419 symbol_up.get(),
420 block, false);
421 }
422 } break;
423 default: break;
424 }
425 return num_added;
426}
427
Zachary Turner74e08ca2016-03-02 22:05:52 +0000428size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000430 lldbassert(sc.comp_unit && sc.function);
431 size_t num_added = 0;
432 auto uid = sc.function->GetID();
433 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
434 if (!pdb_func_up)
435 return 0;
436 Block &parent_block = sc.function->GetBlock(false);
437 num_added =
438 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
439 pdb_func_up.get(), &parent_block, true);
440 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000441}
442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000444 lldbassert(sc.module_sp.get());
445 size_t num_added = 0;
446 auto results_up = m_session_up->getGlobalScope()->findAllChildren();
447 if (!results_up)
448 return 0;
449 while (auto symbol_up = results_up->getNext()) {
450 switch (symbol_up->getSymTag()) {
451 case PDB_SymType::Enum:
452 case PDB_SymType::UDT:
453 case PDB_SymType::Typedef:
454 break;
455 default:
456 continue;
457 }
458
459 auto type_uid = symbol_up->getSymIndexId();
460 if (m_types.find(type_uid) != m_types.end())
461 continue;
462
463 // This should cause the type to get cached and stored in the `m_types`
464 // lookup.
465 if (!ResolveTypeUID(symbol_up->getSymIndexId()))
466 continue;
467
468 ++num_added;
469 }
470 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471}
472
473size_t
474SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
475 // TODO: Implement this
476 return size_t();
477}
478
479lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
480 auto find_result = m_types.find(type_uid);
481 if (find_result != m_types.end())
482 return find_result->second.get();
483
484 TypeSystem *type_system =
485 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
486 ClangASTContext *clang_type_system =
487 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
488 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000489 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 PDBASTParser *pdb =
491 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
492 if (!pdb)
493 return nullptr;
494
495 auto pdb_type = m_session_up->getSymbolById(type_uid);
496 if (pdb_type == nullptr)
497 return nullptr;
498
499 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000500 if (result.get()) {
Aaron Smith86e94342017-12-22 05:26:50 +0000501 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000502 auto type_list = GetTypeList();
503 type_list->Insert(result);
504 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000506}
507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
509 // TODO: Implement this
510 return false;
511}
512
513lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
514 return lldb_private::CompilerDecl();
515}
516
517lldb_private::CompilerDeclContext
518SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
519 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000520 // improve this later but it's not easy because PDB doesn't provide a high
521 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 return *m_tu_decl_ctx_up;
523}
524
525lldb_private::CompilerDeclContext
526SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
527 return *m_tu_decl_ctx_up;
528}
529
530void SymbolFilePDB::ParseDeclsForContext(
531 lldb_private::CompilerDeclContext decl_ctx) {}
532
533uint32_t
534SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
535 uint32_t resolve_scope,
536 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000537 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000538 if (resolve_scope & eSymbolContextCompUnit ||
539 resolve_scope & eSymbolContextVariable ||
540 resolve_scope & eSymbolContextFunction ||
541 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000542 resolve_scope & eSymbolContextLineEntry) {
543 addr_t file_vm_addr = so_addr.GetFileAddress();
544 auto symbol_up =
545 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
546 if (!symbol_up)
547 return 0;
548
549 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
550 if (!cu_sp) {
551 if (resolved_flags | eSymbolContextVariable) {
552 // TODO: Resolve variables
553 }
554 return 0;
555 }
556 sc.comp_unit = cu_sp.get();
557 resolved_flags |= eSymbolContextCompUnit;
558 lldbassert(sc.module_sp == cu_sp->GetModule());
559
560 switch (symbol_up->getSymTag()) {
561 case PDB_SymType::Function:
562 if (resolve_scope & eSymbolContextFunction) {
563 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
564 assert(pdb_func);
565 auto func_uid = pdb_func->getSymIndexId();
566 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
567 if (sc.function == nullptr)
568 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
569 if (sc.function) {
570 resolved_flags |= eSymbolContextFunction;
571 if (resolve_scope & eSymbolContextBlock) {
572 Block &block = sc.function->GetBlock(true);
573 sc.block = block.FindBlockByID(sc.function->GetID());
574 if (sc.block)
575 resolved_flags |= eSymbolContextBlock;
576 }
577 }
578 }
579 break;
580 default:
581 break;
582 }
583
584 if (resolve_scope & eSymbolContextLineEntry) {
585 if (auto *line_table = sc.comp_unit->GetLineTable()) {
586 Address addr(so_addr);
587 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
588 resolved_flags |= eSymbolContextLineEntry;
589 }
590 }
591 }
592 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593}
594
Aaron Smith10a02572018-01-13 06:58:18 +0000595std::string SymbolFilePDB::GetSourceFileNameForPDBCompiland(
596 const PDBSymbolCompiland *pdb_compiland) {
597 if (!pdb_compiland)
598 return std::string();
599
600 std::string source_file_name;
601 // `getSourceFileName` returns the basename of the original source file
602 // used to generate this compiland. It does not return the full path.
603 // Currently the only way to get that is to do a basename lookup to get the
604 // IPDBSourceFile, but this is ambiguous in the case of two source files
605 // with the same name contributing to the same compiland. This is an edge
606 // case that we ignore for now, although we need to a long-term solution.
607 std::string file_name = pdb_compiland->getSourceFileName();
608 if (!file_name.empty()) {
609 auto one_src_file_up =
610 m_session_up->findOneSourceFile(pdb_compiland, file_name,
611 PDB_NameSearchFlags::NS_CaseInsensitive);
612 if (one_src_file_up)
613 source_file_name = one_src_file_up->getFileName();
614 }
615 // For some reason, source file name could be empty, so we will walk through
616 // all source files of this compiland, and determine the right source file
617 // if any that is used to generate this compiland based on language
618 // indicated in compilanddetails language field.
619 if (!source_file_name.empty())
620 return source_file_name;
621
622 auto details_up = pdb_compiland->findOneChild<PDBSymbolCompilandDetails>();
623 PDB_Lang pdb_lang = details_up ? details_up->getLanguage() : PDB_Lang::Cpp;
624 auto src_files_up =
625 m_session_up->getSourceFilesForCompiland(*pdb_compiland);
626 if (src_files_up) {
627 while (auto file_up = src_files_up->getNext()) {
628 FileSpec file_spec(file_up->getFileName(), false,
629 FileSpec::ePathSyntaxWindows);
630 auto file_extension = file_spec.GetFileNameExtension();
631 if (pdb_lang == PDB_Lang::Cpp || pdb_lang == PDB_Lang::C) {
632 static const char* exts[] = { "cpp", "c", "cc", "cxx" };
633 if (llvm::is_contained(exts, file_extension.GetStringRef().lower()))
634 source_file_name = file_up->getFileName();
635 break;
636 } else if (pdb_lang == PDB_Lang::Masm &&
637 ConstString::Compare(file_extension, ConstString("ASM"),
638 false) == 0) {
639 source_file_name = file_up->getFileName();
640 break;
641 }
642 }
643 }
644 return source_file_name;
645}
646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647uint32_t SymbolFilePDB::ResolveSymbolContext(
648 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
649 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000650 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (resolve_scope & lldb::eSymbolContextCompUnit) {
652 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000653 // file. For example, if `file_spec` is <vector>, then this should return
654 // all source files and header files that reference <vector>, either
655 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 auto compilands = m_session_up->findCompilandsForSourceFile(
657 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
658
Aaron Smith10a02572018-01-13 06:58:18 +0000659 if (!compilands)
660 return 0;
661
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000662 // For each one, either find its previously parsed data or parse it afresh
663 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 while (auto compiland = compilands->getNext()) {
665 // If we're not checking inlines, then don't add line information for this
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000666 // file unless the FileSpec matches.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000667 // For inline functions, we don't have to match the FileSpec since they
668 // could be defined in headers other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 if (!check_inlines) {
670 // `getSourceFileName` returns the basename of the original source file
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000671 // used to generate this compiland. It does not return the full path.
672 // Currently the only way to get that is to do a basename lookup to get
673 // the IPDBSourceFile, but this is ambiguous in the case of two source
674 // files with the same name contributing to the same compiland. This is
675 // a moderately extreme edge case, so we consider this OK for now,
676 // although we need to find a long-term solution.
Aaron Smith10a02572018-01-13 06:58:18 +0000677 std::string source_file =
678 GetSourceFileNameForPDBCompiland(compiland.get());
679 if (source_file.empty())
680 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
Aaron Smith10a02572018-01-13 06:58:18 +0000682 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
683 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 continue;
685 }
686
687 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000688 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
689 if (!cu.get())
690 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 sc.comp_unit = cu.get();
692 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693
694 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000695 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000696 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
697 eSymbolContextLineEntry)) {
698 bool has_line_table = ParseCompileUnitLineTable(sc, line);
699
700 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
701 // The query asks for line entries, but we can't get them for the
702 // compile unit. This is not normal for `line` = 0. So just assert it.
703 if (line == 0) {
704 assert(0 && "Couldn't get all line entries!\n");
705 }
706
707 // Current compiland does not have the requested line. Search next.
708 continue;
709 }
710
711 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
712 if (!has_line_table)
713 continue;
714
715 auto *line_table = sc.comp_unit->GetLineTable();
716 lldbassert(line_table);
717
718 uint32_t num_line_entries = line_table->GetSize();
719 // Skip the terminal line entry.
720 --num_line_entries;
721
722 // If `line `!= 0, see if we can resolve function for each line
723 // entry in the line table.
724 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
725 ++line_idx) {
726 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
727 continue;
728
729 auto file_vm_addr =
730 sc.line_entry.range.GetBaseAddress().GetFileAddress();
731 if (file_vm_addr == LLDB_INVALID_ADDRESS)
732 continue;
733
734 auto symbol_up =
735 m_session_up->findSymbolByAddress(file_vm_addr,
736 PDB_SymType::Function);
737 if (symbol_up) {
738 auto func_uid = symbol_up->getSymIndexId();
739 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
740 if (sc.function == nullptr) {
741 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
742 assert(pdb_func);
743 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
744 }
745 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
746 Block &block = sc.function->GetBlock(true);
747 sc.block = block.FindBlockByID(sc.function->GetID());
748 }
749 }
750 sc_list.Append(sc);
751 }
752 } else if (has_line_table) {
753 // We can parse line table for the compile unit. But no query to
754 // resolve function or block. We append `sc` to the list anyway.
755 sc_list.Append(sc);
756 }
757 } else {
758 // No query for line entry, function or block. But we have a valid
759 // compile unit, append `sc` to the list.
760 sc_list.Append(sc);
761 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
763 }
Aaron Smith10a02572018-01-13 06:58:18 +0000764 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765}
766
767uint32_t SymbolFilePDB::FindGlobalVariables(
768 const lldb_private::ConstString &name,
769 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
770 uint32_t max_matches, lldb_private::VariableList &variables) {
771 return uint32_t();
772}
773
774uint32_t
775SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
776 bool append, uint32_t max_matches,
777 lldb_private::VariableList &variables) {
778 return uint32_t();
779}
780
Aaron Smith7ac1c782018-02-09 05:31:28 +0000781bool SymbolFilePDB::ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func,
782 bool include_inlines,
783 lldb_private::SymbolContextList &sc_list) {
784 if (!pdb_func)
785 return false;
786 lldb_private::SymbolContext sc;
787 auto file_vm_addr = pdb_func->getVirtualAddress();
788 if (file_vm_addr == LLDB_INVALID_ADDRESS)
789 return false;
790
791 Address so_addr(file_vm_addr);
792 sc.comp_unit = GetCompileUnitContainsAddress(so_addr).get();
793 if (!sc.comp_unit)
794 return false;
795 sc.module_sp = sc.comp_unit->GetModule();
796 auto symbol_up =
797 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
798 if (!symbol_up)
799 return false;
800
801 auto *func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
802 assert(func);
803 sc.function = ParseCompileUnitFunctionForPDBFunc(func, sc);
804 if (!sc.function)
805 return false;
806
807 sc_list.Append(sc);
808 return true;
809}
810
811bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
812 lldb_private::SymbolContextList &sc_list) {
813 auto pdb_func_up =
814 m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
815 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
816 return false;
817 return ResolveFunction(pdb_func_up.get(), include_inlines, sc_list);
818}
819
820void SymbolFilePDB::CacheFunctionNames() {
821 if (!m_func_full_names.IsEmpty())
822 return;
823
824 std::map<uint64_t, uint32_t> addr_ids;
825
826 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
827 while (auto pdb_func_up = results_up->getNext()) {
828 auto uid = pdb_func_up->getSymIndexId();
829 auto name = pdb_func_up->getName();
830 auto demangled_name = pdb_func_up->getUndecoratedName();
831 if (name.empty() && demangled_name.empty())
832 continue;
833 if (pdb_func_up->isCompilerGenerated())
834 continue;
835
836 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
837 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
838
839 if (auto parent = pdb_func_up->getClassParent()) {
840
841 // PDB have symbols for class/struct methods or static methods in Enum
842 // Class. We won't bother to check if the parent is UDT or Enum here.
843 m_func_method_names.Append(ConstString(name), uid);
844
845 ConstString cstr_name(name);
846
847 // To search a method name, like NS::Class:MemberFunc, LLDB searches its
848 // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not
849 // have inforamtion of this, we extract base names and cache them by our
850 // own effort.
851 llvm::StringRef basename;
852 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
853 if (cpp_method.IsValid()) {
854 llvm::StringRef context;
855 basename = cpp_method.GetBasename();
856 if (basename.empty())
857 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
858 context, basename);
859 }
860
861 if (!basename.empty())
862 m_func_base_names.Append(ConstString(basename), uid);
863 else {
864 m_func_base_names.Append(ConstString(name), uid);
865 }
866
867 if (!demangled_name.empty())
868 m_func_full_names.Append(ConstString(demangled_name), uid);
869
870 } else {
871 // Handle not-method symbols.
872
873 // The function name might contain namespace, or its lexical scope. It
874 // is not safe to get its base name by applying same scheme as we deal
875 // with the method names.
876 // FIXME: Remove namespace if function is static in a scope.
877 m_func_base_names.Append(ConstString(name), uid);
878
879 if (name == "main") {
880 m_func_full_names.Append(ConstString(name), uid);
881
882 if (!demangled_name.empty() && name != demangled_name) {
883 m_func_full_names.Append(ConstString(demangled_name), uid);
884 m_func_base_names.Append(ConstString(demangled_name), uid);
885 }
886 } else if (!demangled_name.empty()) {
887 m_func_full_names.Append(ConstString(demangled_name), uid);
888 } else {
889 m_func_full_names.Append(ConstString(name), uid);
890 }
891 }
892 }
893 }
894
895 if (auto results_up =
896 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
897 while (auto pub_sym_up = results_up->getNext()) {
898 if (!pub_sym_up->isFunction())
899 continue;
900 auto name = pub_sym_up->getName();
901 if (name.empty())
902 continue;
903
904 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
905 auto demangled_name = pub_sym_up->getUndecoratedName();
906 std::vector<uint32_t> ids;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000907 auto vm_addr = pub_sym_up->getVirtualAddress();
908
909 // PDB public symbol has mangled name for its associated function.
910 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
911 // Cache mangled name.
912 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
913 }
914 }
915 }
916 }
917 // Sort them before value searching is working properly
918 m_func_full_names.Sort();
919 m_func_full_names.SizeToFit();
920 m_func_method_names.Sort();
921 m_func_method_names.SizeToFit();
922 m_func_base_names.Sort();
923 m_func_base_names.SizeToFit();
924}
925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926uint32_t SymbolFilePDB::FindFunctions(
927 const lldb_private::ConstString &name,
928 const lldb_private::CompilerDeclContext *parent_decl_ctx,
929 uint32_t name_type_mask, bool include_inlines, bool append,
930 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000931 if (!append)
932 sc_list.Clear();
933 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
934
935 if (name_type_mask == eFunctionNameTypeNone)
936 return 0;
937 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
938 return 0;
939 if (name.IsEmpty())
940 return 0;
941
942 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000943 if (name_type_mask & eFunctionNameTypeFull ||
944 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000945 name_type_mask & eFunctionNameTypeMethod) {
946 CacheFunctionNames();
947
948 std::set<uint32_t> resolved_ids;
949 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, this] (
950 UniqueCStringMap<uint32_t> &Names)
951 {
952 std::vector<uint32_t> ids;
953 if (Names.GetValues(name, ids)) {
954 for (auto id : ids) {
955 if (resolved_ids.find(id) == resolved_ids.end()) {
956 if (ResolveFunction(id, include_inlines, sc_list))
957 resolved_ids.insert(id);
958 }
959 }
960 }
961 };
962 if (name_type_mask & eFunctionNameTypeFull) {
963 ResolveFn(m_func_full_names);
964 }
965 if (name_type_mask & eFunctionNameTypeBase) {
966 ResolveFn(m_func_base_names);
967 }
968 if (name_type_mask & eFunctionNameTypeMethod) {
969 ResolveFn(m_func_method_names);
970 }
971 }
972 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973}
974
975uint32_t
976SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
977 bool include_inlines, bool append,
978 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000979 if (!append)
980 sc_list.Clear();
981 if (!regex.IsValid())
982 return 0;
983
984 auto old_size = sc_list.GetSize();
985 CacheFunctionNames();
986
987 std::set<uint32_t> resolved_ids;
988 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids, this] (
989 UniqueCStringMap<uint32_t> &Names)
990 {
991 std::vector<uint32_t> ids;
992 if (Names.GetValues(regex, ids)) {
993 for (auto id : ids) {
994 if (resolved_ids.find(id) == resolved_ids.end())
995 if (ResolveFunction(id, include_inlines, sc_list))
996 resolved_ids.insert(id);
997 }
998 }
999 };
1000 ResolveFn(m_func_full_names);
1001 ResolveFn(m_func_base_names);
1002
1003 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004}
1005
1006void SymbolFilePDB::GetMangledNamesForFunction(
1007 const std::string &scope_qualified_name,
1008 std::vector<lldb_private::ConstString> &mangled_names) {}
1009
1010uint32_t SymbolFilePDB::FindTypes(
1011 const lldb_private::SymbolContext &sc,
1012 const lldb_private::ConstString &name,
1013 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1014 uint32_t max_matches,
1015 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1016 lldb_private::TypeMap &types) {
1017 if (!append)
1018 types.Clear();
1019 if (!name)
1020 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001021 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1022 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023
1024 searched_symbol_files.clear();
1025 searched_symbol_files.insert(this);
1026
1027 std::string name_str = name.AsCString();
1028
Aaron Smith86e94342017-12-22 05:26:50 +00001029 // There is an assumption 'name' is not a regex
1030 FindTypesByName(name_str, max_matches, types);
1031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 return types.GetSize();
1033}
1034
Aaron Smith86e94342017-12-22 05:26:50 +00001035void
1036SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression &regex,
1037 uint32_t max_matches,
1038 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001040 // space as much as possible since this searches EVERYTHING in the PDB,
1041 // manually doing regex comparisons. PDB library isn't optimized for regex
1042 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001044 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1046 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 std::unique_ptr<IPDBEnumSymbols> results;
1048
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 uint32_t matches = 0;
1050
1051 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001052 results = m_global_scope_up->findAllChildren(tag);
1053 if (!results)
1054 continue;
1055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 while (auto result = results->getNext()) {
1057 if (max_matches > 0 && matches >= max_matches)
1058 break;
1059
1060 std::string type_name;
1061 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1062 type_name = enum_type->getName();
1063 else if (auto typedef_type =
Aaron Smith7ac1c782018-02-09 05:31:28 +00001064 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 type_name = typedef_type->getName();
1066 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1067 type_name = class_type->getName();
1068 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001069 // We're looking only for types that have names. Skip symbols, as well
1070 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 continue;
1072 }
1073
Aaron Smith86e94342017-12-22 05:26:50 +00001074 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 continue;
1076
1077 // This should cause the type to get cached and stored in the `m_types`
1078 // lookup.
1079 if (!ResolveTypeUID(result->getSymIndexId()))
1080 continue;
1081
1082 auto iter = m_types.find(result->getSymIndexId());
1083 if (iter == m_types.end())
1084 continue;
1085 types.Insert(iter->second);
1086 ++matches;
1087 }
1088 }
1089}
1090
1091void SymbolFilePDB::FindTypesByName(const std::string &name,
1092 uint32_t max_matches,
1093 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smith10a02572018-01-13 06:58:18 +00001095 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1096 PDB_NameSearchFlags::NS_Default);
1097 if (!results)
1098 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099
1100 uint32_t matches = 0;
1101
1102 while (auto result = results->getNext()) {
1103 if (max_matches > 0 && matches >= max_matches)
1104 break;
1105 switch (result->getSymTag()) {
1106 case PDB_SymType::Enum:
1107 case PDB_SymType::UDT:
1108 case PDB_SymType::Typedef:
1109 break;
1110 default:
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001111 // We're looking only for types that have names. Skip symbols, as well as
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 // unnamed types such as arrays, pointers, etc.
1113 continue;
1114 }
1115
1116 // This should cause the type to get cached and stored in the `m_types`
1117 // lookup.
1118 if (!ResolveTypeUID(result->getSymIndexId()))
1119 continue;
1120
1121 auto iter = m_types.find(result->getSymIndexId());
1122 if (iter == m_types.end())
1123 continue;
1124 types.Insert(iter->second);
1125 ++matches;
1126 }
1127}
1128
1129size_t SymbolFilePDB::FindTypes(
1130 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1131 lldb_private::TypeMap &types) {
1132 return 0;
1133}
1134
Aaron Smithec40f812018-01-23 20:35:19 +00001135lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1136 return m_obj_file->GetModule()->GetTypeList();
1137}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138
Aaron Smith7ac1c782018-02-09 05:31:28 +00001139void
1140SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol,
1141 uint32_t type_mask,
1142 TypeCollection &type_collection) {
1143 if (!pdb_symbol)
1144 return;
1145
1146 bool can_parse = false;
1147 switch (pdb_symbol->getSymTag()) {
1148 case PDB_SymType::ArrayType:
1149 can_parse = ((type_mask & eTypeClassArray) != 0);
1150 break;
1151 case PDB_SymType::BuiltinType:
1152 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1153 break;
1154 case PDB_SymType::Enum:
1155 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1156 break;
1157 case PDB_SymType::Function:
1158 case PDB_SymType::FunctionSig:
1159 can_parse = ((type_mask & eTypeClassFunction) != 0);
1160 break;
1161 case PDB_SymType::PointerType:
1162 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1163 eTypeClassMemberPointer)) != 0);
1164 break;
1165 case PDB_SymType::Typedef:
1166 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1167 break;
1168 case PDB_SymType::UDT: {
1169 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(pdb_symbol);
1170 assert(udt);
1171 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1172 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1173 eTypeClassUnion)) != 0));
1174 } break;
1175 default:break;
1176 }
1177
1178 if (can_parse) {
1179 if (auto *type = ResolveTypeUID(pdb_symbol->getSymIndexId())) {
1180 auto result =
1181 std::find(type_collection.begin(), type_collection.end(), type);
1182 if (result == type_collection.end())
1183 type_collection.push_back(type);
1184 }
1185 }
1186
1187 auto results_up = pdb_symbol->findAllChildren();
1188 while (auto symbol_up = results_up->getNext())
1189 GetTypesForPDBSymbol(symbol_up.get(), type_mask, type_collection);
1190}
1191
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1193 uint32_t type_mask,
1194 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001195 TypeCollection type_collection;
1196 uint32_t old_size = type_list.GetSize();
1197 CompileUnit *cu = sc_scope ?
1198 sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1199 if (cu) {
1200 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1201 GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1202 } else {
1203 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1204 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1205 if (cu_sp.get()) {
1206 auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID());
1207 GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1208 }
1209 }
1210 }
1211
1212 for (auto type : type_collection) {
1213 type->GetForwardCompilerType();
1214 type_list.Insert(type->shared_from_this());
1215 }
1216 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001217}
1218
1219lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1221 auto type_system =
1222 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1223 if (type_system)
1224 type_system->SetSymbolFile(this);
1225 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001226}
1227
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1229 const lldb_private::SymbolContext &sc,
1230 const lldb_private::ConstString &name,
1231 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1232 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001233}
1234
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1236 static ConstString g_name("pdb");
1237 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001238}
1239
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1241
1242IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1243
1244const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1245 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001246}
1247
Aaron Smith10a02572018-01-13 06:58:18 +00001248lldb::CompUnitSP
1249SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 auto found_cu = m_comp_units.find(id);
1251 if (found_cu != m_comp_units.end())
1252 return found_cu->second;
1253
Aaron Smith10a02572018-01-13 06:58:18 +00001254 auto compiland_up = GetPDBCompilandByUID(id);
1255 if (!compiland_up)
1256 return CompUnitSP();
1257 std::string path = GetSourceFileNameForPDBCompiland(compiland_up.get());
1258 if (path.empty())
1259 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260
1261 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001262 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 if (!details)
1264 lang = lldb::eLanguageTypeC_plus_plus;
1265 else
1266 lang = TranslateLanguage(details->getLanguage());
1267
1268 // Don't support optimized code for now, DebugInfoPDB does not return this
1269 // information.
1270 LazyBool optimized = eLazyBoolNo;
Aaron Smith10a02572018-01-13 06:58:18 +00001271 auto cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001273
1274 if (!cu_sp)
1275 return CompUnitSP();
1276
1277 m_comp_units.insert(std::make_pair(id, cu_sp));
1278 if (index == UINT32_MAX)
1279 GetCompileUnitIndex(compiland_up.get(), index);
1280 lldbassert(index != UINT32_MAX);
1281 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
1282 index, cu_sp);
1283 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001284}
1285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286bool SymbolFilePDB::ParseCompileUnitLineTable(
1287 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001288 lldbassert(sc.comp_unit);
1289
1290 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1291 if (!compiland_up)
1292 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001293
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001295 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
1296 // a globally unique idenfitifier in the namespace of the PDB. So, we have to
1297 // do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001299 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001301
Aaron Smith10a02572018-01-13 06:58:18 +00001302 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001304 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1305 if (!files)
1306 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 // For each source and header file, create a LineSequence for contributions to
Aaron Smith10a02572018-01-13 06:58:18 +00001309 // the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310 while (auto file = files->getNext()) {
1311 std::unique_ptr<LineSequence> sequence(
1312 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001313 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1314 if (!lines)
1315 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001317
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 uint64_t prev_addr;
1319 uint32_t prev_length;
1320 uint32_t prev_line;
1321 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001322
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 for (int i = 0; i < entry_count; ++i) {
1324 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 uint64_t lno = line->getLineNumber();
1327 uint64_t addr = line->getVirtualAddress();
1328 uint32_t length = line->getLength();
1329 uint32_t source_id = line->getSourceFileId();
1330 uint32_t col = line->getColumnNumber();
1331 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001332
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 // There was a gap between the current entry and the previous entry if the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001334 // addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001338 // of the previous entry's address range if the current entry resulted in
1339 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1341 line_table->AppendLineEntryToSequence(
1342 sequence.get(), prev_addr + prev_length, prev_line, 0,
1343 prev_source_idx, false, false, false, false, true);
1344 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001345
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 if (ShouldAddLine(match_line, lno, length)) {
1347 bool is_statement = line->isStatement();
1348 bool is_prologue = false;
1349 bool is_epilogue = false;
1350 auto func =
1351 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1352 if (func) {
1353 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001354 if (prologue)
1355 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001358 if (epilogue)
1359 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001360 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1363 source_idx, is_statement, false,
1364 is_prologue, is_epilogue, false);
1365 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 prev_addr = addr;
1368 prev_length = length;
1369 prev_line = lno;
1370 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001371 }
1372
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1374 // The end is always a terminal entry, so insert it regardless.
1375 line_table->AppendLineEntryToSequence(
1376 sequence.get(), prev_addr + prev_length, prev_line, 0,
1377 prev_source_idx, false, false, false, false, true);
1378 }
1379
1380 line_table->InsertSequence(sequence.release());
1381 }
1382
Aaron Smith10a02572018-01-13 06:58:18 +00001383 if (line_table->GetSize()) {
1384 sc.comp_unit->SetLineTable(line_table.release());
1385 return true;
1386 }
1387 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001388}
1389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001391 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1393 // This is a hack, but we need to convert the source id into an index into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001394 // support files array. We don't want to do path comparisons to avoid
1395 // basename / full path issues that may or may not even be a problem, so we
1396 // use the globally unique source file identifiers. Ideally we could use the
1397 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001398 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1399 if (!source_files)
1400 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001402
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 while (auto file = source_files->getNext()) {
1404 uint32_t source_id = file->getUniqueId();
1405 index_map[source_id] = index++;
1406 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001407}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001408
1409lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1410 const lldb_private::Address &so_addr) {
1411 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1412 if (file_vm_addr == LLDB_INVALID_ADDRESS)
1413 return nullptr;
1414
1415 auto lines_up =
1416 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200);
1417 if (!lines_up)
1418 return nullptr;
1419
1420 auto first_line_up = lines_up->getNext();
1421 if (!first_line_up)
1422 return nullptr;
1423 auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId());
1424 if (compiland_up) {
1425 return ParseCompileUnitForUID(compiland_up->getSymIndexId());
1426 }
1427
1428 return nullptr;
1429}
1430
1431Mangled
1432SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc *pdb_func) {
1433 Mangled mangled;
1434 if (!pdb_func)
1435 return mangled;
1436
1437 auto func_name = pdb_func->getName();
1438 auto func_undecorated_name = pdb_func->getUndecoratedName();
1439 std::string func_decorated_name;
1440
1441 // Seek from public symbols for non-static function's decorated name if any.
1442 // For static functions, they don't have undecorated names and aren't exposed
1443 // in Public Symbols either.
1444 if (!func_undecorated_name.empty()) {
1445 auto result_up =
1446 m_global_scope_up->findChildren(PDB_SymType::PublicSymbol,
1447 func_undecorated_name,
1448 PDB_NameSearchFlags::NS_UndecoratedName);
1449 if (result_up) {
1450 while (auto symbol_up = result_up->getNext()) {
1451 // For a public symbol, it is unique.
1452 lldbassert(result_up->getChildCount() == 1);
1453 if (auto *pdb_public_sym =
1454 llvm::dyn_cast<PDBSymbolPublicSymbol>(symbol_up.get())) {
1455 if (pdb_public_sym->isFunction()) {
1456 func_decorated_name = pdb_public_sym->getName();
1457 }
1458 }
1459 }
1460 }
1461 }
1462 if (!func_decorated_name.empty()) {
1463 mangled.SetMangledName(ConstString(func_decorated_name));
1464
1465 // For MSVC, format of C funciton's decorated name depends on calling
1466 // conventon. Unfortunately none of the format is recognized by current
1467 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1468 // `__purecall` is retrieved as both its decorated and
1469 // undecorated name (using PDBSymbolFunc::getUndecoratedName method).
1470 // However `__purecall` string is not treated as mangled in LLDB
1471 // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method
1472 // will fail internally and caches an empty string as its undecorated
1473 // name. So we will face a contradition here for the same symbol:
1474 // non-empty undecorated name from PDB
1475 // empty undecorated name from LLDB
1476 if (!func_undecorated_name.empty() &&
1477 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1478 mangled.SetDemangledName(ConstString(func_undecorated_name));
1479
1480 // LLDB uses several flags to control how a C++ decorated name is
1481 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled.
1482 // So the yielded name could be different from what we retrieve from
1483 // PDB source unless we also apply same flags in getting undecorated
1484 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1485 if (!func_undecorated_name.empty() &&
1486 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1487 ConstString(func_undecorated_name))
1488 mangled.SetDemangledName(ConstString(func_undecorated_name));
1489 } else if (!func_undecorated_name.empty()) {
1490 mangled.SetDemangledName(ConstString(func_undecorated_name));
1491 } else if (!func_name.empty())
1492 mangled.SetValue(ConstString(func_name), false);
1493
1494 return mangled;
1495}
1496
1497bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1498 const lldb_private::CompilerDeclContext *decl_ctx) {
1499 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1500 return true;
1501
1502 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1503 if (!decl_ctx_type_system)
1504 return false;
1505 TypeSystem *type_system = GetTypeSystemForLanguage(
1506 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1507 if (decl_ctx_type_system == type_system)
1508 return true; // The type systems match, return true
1509
1510 return false;
1511}