1 //===-- SymbolFilePDB.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SymbolFilePDB.h" 10 11 #include "PDBASTParser.h" 12 #include "PDBLocationToDWARFExpression.h" 13 14 #include "clang/Lex/Lexer.h" 15 16 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Symbol/CompileUnit.h" 20 #include "lldb/Symbol/LineTable.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Symbol/SymbolVendor.h" 24 #include "lldb/Symbol/TypeList.h" 25 #include "lldb/Symbol/TypeMap.h" 26 #include "lldb/Symbol/Variable.h" 27 #include "lldb/Utility/Log.h" 28 #include "lldb/Utility/RegularExpression.h" 29 30 #include "llvm/DebugInfo/PDB/GenericError.h" 31 #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 32 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 33 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 34 #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h" 35 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 36 #include "llvm/DebugInfo/PDB/IPDBTable.h" 37 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 38 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 39 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 40 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 41 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 42 #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" 46 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 47 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 48 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 49 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 50 51 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 52 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" 53 #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h" 54 55 #include <regex> 56 57 using namespace lldb; 58 using namespace lldb_private; 59 using namespace llvm::pdb; 60 61 LLDB_PLUGIN_DEFINE(SymbolFilePDB) 62 63 char SymbolFilePDB::ID; 64 65 namespace { 66 lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 67 switch (lang) { 68 case PDB_Lang::Cpp: 69 return lldb::LanguageType::eLanguageTypeC_plus_plus; 70 case PDB_Lang::C: 71 return lldb::LanguageType::eLanguageTypeC; 72 case PDB_Lang::Swift: 73 return lldb::LanguageType::eLanguageTypeSwift; 74 default: 75 return lldb::LanguageType::eLanguageTypeUnknown; 76 } 77 } 78 79 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 80 uint32_t addr_length) { 81 return ((requested_line == 0 || actual_line == requested_line) && 82 addr_length > 0); 83 } 84 } // namespace 85 86 static bool ShouldUseNativeReader() { 87 #if defined(_WIN32) 88 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER"); 89 return use_native.equals_lower("on") || use_native.equals_lower("yes") || 90 use_native.equals_lower("1") || use_native.equals_lower("true"); 91 #else 92 return true; 93 #endif 94 } 95 96 void SymbolFilePDB::Initialize() { 97 if (ShouldUseNativeReader()) { 98 npdb::SymbolFileNativePDB::Initialize(); 99 } else { 100 PluginManager::RegisterPlugin(GetPluginNameStatic(), 101 GetPluginDescriptionStatic(), CreateInstance, 102 DebuggerInitialize); 103 } 104 } 105 106 void SymbolFilePDB::Terminate() { 107 if (ShouldUseNativeReader()) { 108 npdb::SymbolFileNativePDB::Terminate(); 109 } else { 110 PluginManager::UnregisterPlugin(CreateInstance); 111 } 112 } 113 114 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 115 116 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 117 static ConstString g_name("pdb"); 118 return g_name; 119 } 120 121 const char *SymbolFilePDB::GetPluginDescriptionStatic() { 122 return "Microsoft PDB debug symbol file reader."; 123 } 124 125 lldb_private::SymbolFile * 126 SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) { 127 return new SymbolFilePDB(std::move(objfile_sp)); 128 } 129 130 SymbolFilePDB::SymbolFilePDB(lldb::ObjectFileSP objfile_sp) 131 : SymbolFile(std::move(objfile_sp)), m_session_up(), m_global_scope_up() {} 132 133 SymbolFilePDB::~SymbolFilePDB() {} 134 135 uint32_t SymbolFilePDB::CalculateAbilities() { 136 uint32_t abilities = 0; 137 if (!m_objfile_sp) 138 return 0; 139 140 if (!m_session_up) { 141 // Lazily load and match the PDB file, but only do this once. 142 std::string exePath = m_objfile_sp->GetFileSpec().GetPath(); 143 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 144 m_session_up); 145 if (error) { 146 llvm::consumeError(std::move(error)); 147 auto module_sp = m_objfile_sp->GetModule(); 148 if (!module_sp) 149 return 0; 150 // See if any symbol file is specified through `--symfile` option. 151 FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 152 if (!symfile) 153 return 0; 154 error = loadDataForPDB(PDB_ReaderType::DIA, 155 llvm::StringRef(symfile.GetPath()), m_session_up); 156 if (error) { 157 llvm::consumeError(std::move(error)); 158 return 0; 159 } 160 } 161 } 162 if (!m_session_up) 163 return 0; 164 165 auto enum_tables_up = m_session_up->getEnumTables(); 166 if (!enum_tables_up) 167 return 0; 168 while (auto table_up = enum_tables_up->getNext()) { 169 if (table_up->getItemCount() == 0) 170 continue; 171 auto type = table_up->getTableType(); 172 switch (type) { 173 case PDB_TableType::Symbols: 174 // This table represents a store of symbols with types listed in 175 // PDBSym_Type 176 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | 177 LocalVariables | VariableTypes); 178 break; 179 case PDB_TableType::LineNumbers: 180 abilities |= LineTables; 181 break; 182 default: 183 break; 184 } 185 } 186 return abilities; 187 } 188 189 void SymbolFilePDB::InitializeObject() { 190 lldb::addr_t obj_load_address = 191 m_objfile_sp->GetBaseAddress().GetFileAddress(); 192 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); 193 m_session_up->setLoadAddress(obj_load_address); 194 if (!m_global_scope_up) 195 m_global_scope_up = m_session_up->getGlobalScope(); 196 lldbassert(m_global_scope_up.get()); 197 } 198 199 uint32_t SymbolFilePDB::CalculateNumCompileUnits() { 200 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 201 if (!compilands) 202 return 0; 203 204 // The linker could link *.dll (compiland language = LINK), or import 205 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be 206 // found as a child of the global scope (PDB executable). Usually, such 207 // compilands contain `thunk` symbols in which we are not interested for 208 // now. However we still count them in the compiland list. If we perform 209 // any compiland related activity, like finding symbols through 210 // llvm::pdb::IPDBSession methods, such compilands will all be searched 211 // automatically no matter whether we include them or not. 212 uint32_t compile_unit_count = compilands->getChildCount(); 213 214 // The linker can inject an additional "dummy" compilation unit into the 215 // PDB. Ignore this special compile unit for our purposes, if it is there. 216 // It is always the last one. 217 auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1); 218 lldbassert(last_compiland_up.get()); 219 std::string name = last_compiland_up->getName(); 220 if (name == "* Linker *") 221 --compile_unit_count; 222 return compile_unit_count; 223 } 224 225 void SymbolFilePDB::GetCompileUnitIndex( 226 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { 227 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 228 if (!results_up) 229 return; 230 auto uid = pdb_compiland.getSymIndexId(); 231 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 232 auto compiland_up = results_up->getChildAtIndex(cu_idx); 233 if (!compiland_up) 234 continue; 235 if (compiland_up->getSymIndexId() == uid) { 236 index = cu_idx; 237 return; 238 } 239 } 240 index = UINT32_MAX; 241 return; 242 } 243 244 std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 245 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 246 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 247 } 248 249 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 250 if (index >= GetNumCompileUnits()) 251 return CompUnitSP(); 252 253 // Assuming we always retrieve same compilands listed in same order through 254 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 255 // compile unit makes no sense. 256 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 257 if (!results) 258 return CompUnitSP(); 259 auto compiland_up = results->getChildAtIndex(index); 260 if (!compiland_up) 261 return CompUnitSP(); 262 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 263 } 264 265 lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) { 266 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 267 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 268 if (!compiland_up) 269 return lldb::eLanguageTypeUnknown; 270 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 271 if (!details) 272 return lldb::eLanguageTypeUnknown; 273 return TranslateLanguage(details->getLanguage()); 274 } 275 276 lldb_private::Function * 277 SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func, 278 CompileUnit &comp_unit) { 279 if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId())) 280 return result.get(); 281 282 auto file_vm_addr = pdb_func.getVirtualAddress(); 283 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 284 return nullptr; 285 286 auto func_length = pdb_func.getLength(); 287 AddressRange func_range = 288 AddressRange(file_vm_addr, func_length, 289 GetObjectFile()->GetModule()->GetSectionList()); 290 if (!func_range.GetBaseAddress().IsValid()) 291 return nullptr; 292 293 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 294 if (!func_type) 295 return nullptr; 296 297 user_id_t func_type_uid = pdb_func.getSignatureId(); 298 299 Mangled mangled = GetMangledForPDBFunc(pdb_func); 300 301 FunctionSP func_sp = 302 std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(), 303 func_type_uid, mangled, func_type, func_range); 304 305 comp_unit.AddFunction(func_sp); 306 307 LanguageType lang = ParseLanguage(comp_unit); 308 auto type_system_or_err = GetTypeSystemForLanguage(lang); 309 if (auto err = type_system_or_err.takeError()) { 310 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 311 std::move(err), "Unable to parse PDBFunc"); 312 return nullptr; 313 } 314 315 TypeSystemClang *clang_type_system = 316 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 317 if (!clang_type_system) 318 return nullptr; 319 clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func); 320 321 return func_sp.get(); 322 } 323 324 size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) { 325 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 326 size_t func_added = 0; 327 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 328 if (!compiland_up) 329 return 0; 330 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 331 if (!results_up) 332 return 0; 333 while (auto pdb_func_up = results_up->getNext()) { 334 auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId()); 335 if (!func_sp) { 336 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit)) 337 ++func_added; 338 } 339 } 340 return func_added; 341 } 342 343 bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) { 344 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 345 if (comp_unit.GetLineTable()) 346 return true; 347 return ParseCompileUnitLineTable(comp_unit, 0); 348 } 349 350 bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) { 351 // PDB doesn't contain information about macros 352 return false; 353 } 354 355 bool SymbolFilePDB::ParseSupportFiles( 356 CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) { 357 358 // In theory this is unnecessary work for us, because all of this information 359 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 360 // second time seems like a waste. Unfortunately, there's no good way around 361 // this short of a moderate refactor since SymbolVendor depends on being able 362 // to cache this list. 363 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 364 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 365 if (!compiland_up) 366 return false; 367 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 368 if (!files || files->getChildCount() == 0) 369 return false; 370 371 while (auto file = files->getNext()) { 372 FileSpec spec(file->getFileName(), FileSpec::Style::windows); 373 support_files.AppendIfUnique(spec); 374 } 375 376 return true; 377 } 378 379 bool SymbolFilePDB::ParseImportedModules( 380 const lldb_private::SymbolContext &sc, 381 std::vector<SourceModule> &imported_modules) { 382 // PDB does not yet support module debug info 383 return false; 384 } 385 386 static size_t ParseFunctionBlocksForPDBSymbol( 387 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol, 388 lldb_private::Block *parent_block, bool is_top_parent) { 389 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 417 block->AddRange(Block::Range( 418 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); 419 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()) { 426 num_added += ParseFunctionBlocksForPDBSymbol( 427 func_file_vm_addr, symbol_up.get(), block, false); 428 } 429 } break; 430 default: 431 break; 432 } 433 return num_added; 434 } 435 436 size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) { 437 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 438 size_t num_added = 0; 439 auto uid = func.GetID(); 440 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 441 if (!pdb_func_up) 442 return 0; 443 Block &parent_block = func.GetBlock(false); 444 num_added = ParseFunctionBlocksForPDBSymbol( 445 pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true); 446 return num_added; 447 } 448 449 size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) { 450 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 451 452 size_t num_added = 0; 453 auto compiland = GetPDBCompilandByUID(comp_unit.GetID()); 454 if (!compiland) 455 return 0; 456 457 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 458 std::unique_ptr<IPDBEnumSymbols> results; 459 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 460 PDB_SymType::UDT}; 461 for (auto tag : tags_to_search) { 462 results = raw_sym.findAllChildren(tag); 463 if (!results || results->getChildCount() == 0) 464 continue; 465 while (auto symbol = results->getNext()) { 466 switch (symbol->getSymTag()) { 467 case PDB_SymType::Enum: 468 case PDB_SymType::UDT: 469 case PDB_SymType::Typedef: 470 break; 471 default: 472 continue; 473 } 474 475 // This should cause the type to get cached and stored in the `m_types` 476 // lookup. 477 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { 478 // Resolve the type completely to avoid a completion 479 // (and so a list change, which causes an iterators invalidation) 480 // during a TypeList dumping 481 type->GetFullCompilerType(); 482 ++num_added; 483 } 484 } 485 } 486 }; 487 488 ParseTypesByTagFn(*compiland); 489 490 // Also parse global types particularly coming from this compiland. 491 // Unfortunately, PDB has no compiland information for each global type. We 492 // have to parse them all. But ensure we only do this once. 493 static bool parse_all_global_types = false; 494 if (!parse_all_global_types) { 495 ParseTypesByTagFn(*m_global_scope_up); 496 parse_all_global_types = true; 497 } 498 return num_added; 499 } 500 501 size_t 502 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 503 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 504 if (!sc.comp_unit) 505 return 0; 506 507 size_t num_added = 0; 508 if (sc.function) { 509 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 510 sc.function->GetID()); 511 if (!pdb_func) 512 return 0; 513 514 num_added += ParseVariables(sc, *pdb_func); 515 sc.function->GetBlock(false).SetDidParseVariables(true, true); 516 } else if (sc.comp_unit) { 517 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 518 if (!compiland) 519 return 0; 520 521 if (sc.comp_unit->GetVariableList(false)) 522 return 0; 523 524 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 525 if (results && results->getChildCount()) { 526 while (auto result = results->getNext()) { 527 auto cu_id = GetCompilandId(*result); 528 // FIXME: We are not able to determine variable's compile unit. 529 if (cu_id == 0) 530 continue; 531 532 if (cu_id == sc.comp_unit->GetID()) 533 num_added += ParseVariables(sc, *result); 534 } 535 } 536 537 // FIXME: A `file static` or `global constant` variable appears both in 538 // compiland's children and global scope's children with unexpectedly 539 // different symbol's Id making it ambiguous. 540 541 // FIXME: 'local constant', for example, const char var[] = "abc", declared 542 // in a function scope, can't be found in PDB. 543 544 // Parse variables in this compiland. 545 num_added += ParseVariables(sc, *compiland); 546 } 547 548 return num_added; 549 } 550 551 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 552 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 553 auto find_result = m_types.find(type_uid); 554 if (find_result != m_types.end()) 555 return find_result->second.get(); 556 557 auto type_system_or_err = 558 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 559 if (auto err = type_system_or_err.takeError()) { 560 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 561 std::move(err), "Unable to ResolveTypeUID"); 562 return nullptr; 563 } 564 565 TypeSystemClang *clang_type_system = 566 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 567 if (!clang_type_system) 568 return nullptr; 569 PDBASTParser *pdb = clang_type_system->GetPDBParser(); 570 if (!pdb) 571 return nullptr; 572 573 auto pdb_type = m_session_up->getSymbolById(type_uid); 574 if (pdb_type == nullptr) 575 return nullptr; 576 577 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 578 if (result) { 579 m_types.insert(std::make_pair(type_uid, result)); 580 GetTypeList().Insert(result); 581 } 582 return result.get(); 583 } 584 585 llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID( 586 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 587 return llvm::None; 588 } 589 590 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 591 std::lock_guard<std::recursive_mutex> guard( 592 GetObjectFile()->GetModule()->GetMutex()); 593 594 auto type_system_or_err = 595 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 596 if (auto err = type_system_or_err.takeError()) { 597 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 598 std::move(err), "Unable to get dynamic array info for UID"); 599 return false; 600 } 601 602 TypeSystemClang *clang_ast_ctx = 603 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 604 605 if (!clang_ast_ctx) 606 return false; 607 608 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 609 if (!pdb) 610 return false; 611 612 return pdb->CompleteTypeFromPDB(compiler_type); 613 } 614 615 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 616 auto type_system_or_err = 617 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 618 if (auto err = type_system_or_err.takeError()) { 619 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 620 std::move(err), "Unable to get decl for UID"); 621 return CompilerDecl(); 622 } 623 624 TypeSystemClang *clang_ast_ctx = 625 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 626 if (!clang_ast_ctx) 627 return CompilerDecl(); 628 629 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 630 if (!pdb) 631 return CompilerDecl(); 632 633 auto symbol = m_session_up->getSymbolById(uid); 634 if (!symbol) 635 return CompilerDecl(); 636 637 auto decl = pdb->GetDeclForSymbol(*symbol); 638 if (!decl) 639 return CompilerDecl(); 640 641 return clang_ast_ctx->GetCompilerDecl(decl); 642 } 643 644 lldb_private::CompilerDeclContext 645 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 646 auto type_system_or_err = 647 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 648 if (auto err = type_system_or_err.takeError()) { 649 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 650 std::move(err), "Unable to get DeclContext for UID"); 651 return CompilerDeclContext(); 652 } 653 654 TypeSystemClang *clang_ast_ctx = 655 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 656 if (!clang_ast_ctx) 657 return CompilerDeclContext(); 658 659 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 660 if (!pdb) 661 return CompilerDeclContext(); 662 663 auto symbol = m_session_up->getSymbolById(uid); 664 if (!symbol) 665 return CompilerDeclContext(); 666 667 auto decl_context = pdb->GetDeclContextForSymbol(*symbol); 668 if (!decl_context) 669 return GetDeclContextContainingUID(uid); 670 671 return clang_ast_ctx->CreateDeclContext(decl_context); 672 } 673 674 lldb_private::CompilerDeclContext 675 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 676 auto type_system_or_err = 677 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 678 if (auto err = type_system_or_err.takeError()) { 679 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 680 std::move(err), "Unable to get DeclContext containing UID"); 681 return CompilerDeclContext(); 682 } 683 684 TypeSystemClang *clang_ast_ctx = 685 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 686 if (!clang_ast_ctx) 687 return CompilerDeclContext(); 688 689 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 690 if (!pdb) 691 return CompilerDeclContext(); 692 693 auto symbol = m_session_up->getSymbolById(uid); 694 if (!symbol) 695 return CompilerDeclContext(); 696 697 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol); 698 assert(decl_context); 699 700 return clang_ast_ctx->CreateDeclContext(decl_context); 701 } 702 703 void SymbolFilePDB::ParseDeclsForContext( 704 lldb_private::CompilerDeclContext decl_ctx) { 705 auto type_system_or_err = 706 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 707 if (auto err = type_system_or_err.takeError()) { 708 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 709 std::move(err), "Unable to parse decls for context"); 710 return; 711 } 712 713 TypeSystemClang *clang_ast_ctx = 714 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 715 if (!clang_ast_ctx) 716 return; 717 718 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 719 if (!pdb) 720 return; 721 722 pdb->ParseDeclsForDeclContext( 723 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext())); 724 } 725 726 uint32_t 727 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 728 SymbolContextItem resolve_scope, 729 lldb_private::SymbolContext &sc) { 730 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 731 uint32_t resolved_flags = 0; 732 if (resolve_scope & eSymbolContextCompUnit || 733 resolve_scope & eSymbolContextVariable || 734 resolve_scope & eSymbolContextFunction || 735 resolve_scope & eSymbolContextBlock || 736 resolve_scope & eSymbolContextLineEntry) { 737 auto cu_sp = GetCompileUnitContainsAddress(so_addr); 738 if (!cu_sp) { 739 if (resolved_flags & eSymbolContextVariable) { 740 // TODO: Resolve variables 741 } 742 return 0; 743 } 744 sc.comp_unit = cu_sp.get(); 745 resolved_flags |= eSymbolContextCompUnit; 746 lldbassert(sc.module_sp == cu_sp->GetModule()); 747 } 748 749 if (resolve_scope & eSymbolContextFunction || 750 resolve_scope & eSymbolContextBlock) { 751 addr_t file_vm_addr = so_addr.GetFileAddress(); 752 auto symbol_up = 753 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); 754 if (symbol_up) { 755 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 756 assert(pdb_func); 757 auto func_uid = pdb_func->getSymIndexId(); 758 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 759 if (sc.function == nullptr) 760 sc.function = 761 ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit); 762 if (sc.function) { 763 resolved_flags |= eSymbolContextFunction; 764 if (resolve_scope & eSymbolContextBlock) { 765 auto block_symbol = m_session_up->findSymbolByAddress( 766 file_vm_addr, PDB_SymType::Block); 767 auto block_id = block_symbol ? block_symbol->getSymIndexId() 768 : sc.function->GetID(); 769 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id); 770 if (sc.block) 771 resolved_flags |= eSymbolContextBlock; 772 } 773 } 774 } 775 } 776 777 if (resolve_scope & eSymbolContextLineEntry) { 778 if (auto *line_table = sc.comp_unit->GetLineTable()) { 779 Address addr(so_addr); 780 if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 781 resolved_flags |= eSymbolContextLineEntry; 782 } 783 } 784 785 return resolved_flags; 786 } 787 788 uint32_t SymbolFilePDB::ResolveSymbolContext( 789 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 790 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) { 791 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 792 const size_t old_size = sc_list.GetSize(); 793 if (resolve_scope & lldb::eSymbolContextCompUnit) { 794 // Locate all compilation units with line numbers referencing the specified 795 // file. For example, if `file_spec` is <vector>, then this should return 796 // all source files and header files that reference <vector>, either 797 // directly or indirectly. 798 auto compilands = m_session_up->findCompilandsForSourceFile( 799 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 800 801 if (!compilands) 802 return 0; 803 804 // For each one, either find its previously parsed data or parse it afresh 805 // and add it to the symbol context list. 806 while (auto compiland = compilands->getNext()) { 807 // If we're not checking inlines, then don't add line information for 808 // this file unless the FileSpec matches. For inline functions, we don't 809 // have to match the FileSpec since they could be defined in headers 810 // other than file specified in FileSpec. 811 if (!check_inlines) { 812 std::string source_file = compiland->getSourceFileFullPath(); 813 if (source_file.empty()) 814 continue; 815 FileSpec this_spec(source_file, FileSpec::Style::windows); 816 bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 817 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 818 continue; 819 } 820 821 SymbolContext sc; 822 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 823 if (!cu) 824 continue; 825 sc.comp_unit = cu.get(); 826 sc.module_sp = cu->GetModule(); 827 828 // If we were asked to resolve line entries, add all entries to the line 829 // table that match the requested line (or all lines if `line` == 0). 830 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 831 eSymbolContextLineEntry)) { 832 bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line); 833 834 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 835 // The query asks for line entries, but we can't get them for the 836 // compile unit. This is not normal for `line` = 0. So just assert 837 // it. 838 assert(line && "Couldn't get all line entries!\n"); 839 840 // Current compiland does not have the requested line. Search next. 841 continue; 842 } 843 844 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 845 if (!has_line_table) 846 continue; 847 848 auto *line_table = sc.comp_unit->GetLineTable(); 849 lldbassert(line_table); 850 851 uint32_t num_line_entries = line_table->GetSize(); 852 // Skip the terminal line entry. 853 --num_line_entries; 854 855 // If `line `!= 0, see if we can resolve function for each line entry 856 // in the line table. 857 for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 858 ++line_idx) { 859 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 860 continue; 861 862 auto file_vm_addr = 863 sc.line_entry.range.GetBaseAddress().GetFileAddress(); 864 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 865 continue; 866 867 auto symbol_up = m_session_up->findSymbolByAddress( 868 file_vm_addr, PDB_SymType::Function); 869 if (symbol_up) { 870 auto func_uid = symbol_up->getSymIndexId(); 871 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 872 if (sc.function == nullptr) { 873 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 874 assert(pdb_func); 875 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, 876 *sc.comp_unit); 877 } 878 if (sc.function && (resolve_scope & eSymbolContextBlock)) { 879 Block &block = sc.function->GetBlock(true); 880 sc.block = block.FindBlockByID(sc.function->GetID()); 881 } 882 } 883 sc_list.Append(sc); 884 } 885 } else if (has_line_table) { 886 // We can parse line table for the compile unit. But no query to 887 // resolve function or block. We append `sc` to the list anyway. 888 sc_list.Append(sc); 889 } 890 } else { 891 // No query for line entry, function or block. But we have a valid 892 // compile unit, append `sc` to the list. 893 sc_list.Append(sc); 894 } 895 } 896 } 897 return sc_list.GetSize() - old_size; 898 } 899 900 std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { 901 // Cache public names at first 902 if (m_public_names.empty()) 903 if (auto result_up = 904 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol)) 905 while (auto symbol_up = result_up->getNext()) 906 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress()) 907 m_public_names[addr] = symbol_up->getRawSymbol().getName(); 908 909 // Look up the name in the cache 910 return m_public_names.lookup(pdb_data.getVirtualAddress()); 911 } 912 913 VariableSP SymbolFilePDB::ParseVariableForPDBData( 914 const lldb_private::SymbolContext &sc, 915 const llvm::pdb::PDBSymbolData &pdb_data) { 916 VariableSP var_sp; 917 uint32_t var_uid = pdb_data.getSymIndexId(); 918 auto result = m_variables.find(var_uid); 919 if (result != m_variables.end()) 920 return result->second; 921 922 ValueType scope = eValueTypeInvalid; 923 bool is_static_member = false; 924 bool is_external = false; 925 bool is_artificial = false; 926 927 switch (pdb_data.getDataKind()) { 928 case PDB_DataKind::Global: 929 scope = eValueTypeVariableGlobal; 930 is_external = true; 931 break; 932 case PDB_DataKind::Local: 933 scope = eValueTypeVariableLocal; 934 break; 935 case PDB_DataKind::FileStatic: 936 scope = eValueTypeVariableStatic; 937 break; 938 case PDB_DataKind::StaticMember: 939 is_static_member = true; 940 scope = eValueTypeVariableStatic; 941 break; 942 case PDB_DataKind::Member: 943 scope = eValueTypeVariableStatic; 944 break; 945 case PDB_DataKind::Param: 946 scope = eValueTypeVariableArgument; 947 break; 948 case PDB_DataKind::Constant: 949 scope = eValueTypeConstResult; 950 break; 951 default: 952 break; 953 } 954 955 switch (pdb_data.getLocationType()) { 956 case PDB_LocType::TLS: 957 scope = eValueTypeVariableThreadLocal; 958 break; 959 case PDB_LocType::RegRel: { 960 // It is a `this` pointer. 961 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { 962 scope = eValueTypeVariableArgument; 963 is_artificial = true; 964 } 965 } break; 966 default: 967 break; 968 } 969 970 Declaration decl; 971 if (!is_artificial && !pdb_data.isCompilerGenerated()) { 972 if (auto lines = pdb_data.getLineNumbers()) { 973 if (auto first_line = lines->getNext()) { 974 uint32_t src_file_id = first_line->getSourceFileId(); 975 auto src_file = m_session_up->getSourceFileById(src_file_id); 976 if (src_file) { 977 FileSpec spec(src_file->getFileName()); 978 decl.SetFile(spec); 979 decl.SetColumn(first_line->getColumnNumber()); 980 decl.SetLine(first_line->getLineNumber()); 981 } 982 } 983 } 984 } 985 986 Variable::RangeList ranges; 987 SymbolContextScope *context_scope = sc.comp_unit; 988 if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) { 989 if (sc.function) { 990 Block &function_block = sc.function->GetBlock(true); 991 Block *block = 992 function_block.FindBlockByID(pdb_data.getLexicalParentId()); 993 if (!block) 994 block = &function_block; 995 996 context_scope = block; 997 998 for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges; 999 ++i) { 1000 AddressRange range; 1001 if (!block->GetRangeAtIndex(i, range)) 1002 continue; 1003 1004 ranges.Append(range.GetBaseAddress().GetFileAddress(), 1005 range.GetByteSize()); 1006 } 1007 } 1008 } 1009 1010 SymbolFileTypeSP type_sp = 1011 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); 1012 1013 auto var_name = pdb_data.getName(); 1014 auto mangled = GetMangledForPDBData(pdb_data); 1015 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); 1016 1017 bool is_constant; 1018 DWARFExpression location = ConvertPDBLocationToDWARFExpression( 1019 GetObjectFile()->GetModule(), pdb_data, ranges, is_constant); 1020 1021 var_sp = std::make_shared<Variable>( 1022 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, 1023 ranges, &decl, location, is_external, is_artificial, is_static_member); 1024 var_sp->SetLocationIsConstantValueData(is_constant); 1025 1026 m_variables.insert(std::make_pair(var_uid, var_sp)); 1027 return var_sp; 1028 } 1029 1030 size_t 1031 SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, 1032 const llvm::pdb::PDBSymbol &pdb_symbol, 1033 lldb_private::VariableList *variable_list) { 1034 size_t num_added = 0; 1035 1036 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { 1037 VariableListSP local_variable_list_sp; 1038 1039 auto result = m_variables.find(pdb_data->getSymIndexId()); 1040 if (result != m_variables.end()) { 1041 if (variable_list) 1042 variable_list->AddVariableIfUnique(result->second); 1043 } else { 1044 // Prepare right VariableList for this variable. 1045 if (auto lexical_parent = pdb_data->getLexicalParent()) { 1046 switch (lexical_parent->getSymTag()) { 1047 case PDB_SymType::Exe: 1048 assert(sc.comp_unit); 1049 LLVM_FALLTHROUGH; 1050 case PDB_SymType::Compiland: { 1051 if (sc.comp_unit) { 1052 local_variable_list_sp = sc.comp_unit->GetVariableList(false); 1053 if (!local_variable_list_sp) { 1054 local_variable_list_sp = std::make_shared<VariableList>(); 1055 sc.comp_unit->SetVariableList(local_variable_list_sp); 1056 } 1057 } 1058 } break; 1059 case PDB_SymType::Block: 1060 case PDB_SymType::Function: { 1061 if (sc.function) { 1062 Block *block = sc.function->GetBlock(true).FindBlockByID( 1063 lexical_parent->getSymIndexId()); 1064 if (block) { 1065 local_variable_list_sp = block->GetBlockVariableList(false); 1066 if (!local_variable_list_sp) { 1067 local_variable_list_sp = std::make_shared<VariableList>(); 1068 block->SetVariableList(local_variable_list_sp); 1069 } 1070 } 1071 } 1072 } break; 1073 default: 1074 break; 1075 } 1076 } 1077 1078 if (local_variable_list_sp) { 1079 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { 1080 local_variable_list_sp->AddVariableIfUnique(var_sp); 1081 if (variable_list) 1082 variable_list->AddVariableIfUnique(var_sp); 1083 ++num_added; 1084 PDBASTParser *ast = GetPDBAstParser(); 1085 if (ast) 1086 ast->GetDeclForSymbol(*pdb_data); 1087 } 1088 } 1089 } 1090 } 1091 1092 if (auto results = pdb_symbol.findAllChildren()) { 1093 while (auto result = results->getNext()) 1094 num_added += ParseVariables(sc, *result, variable_list); 1095 } 1096 1097 return num_added; 1098 } 1099 1100 void SymbolFilePDB::FindGlobalVariables( 1101 lldb_private::ConstString name, 1102 const lldb_private::CompilerDeclContext *parent_decl_ctx, 1103 uint32_t max_matches, lldb_private::VariableList &variables) { 1104 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1105 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1106 return; 1107 if (name.IsEmpty()) 1108 return; 1109 1110 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1111 if (!results) 1112 return; 1113 1114 uint32_t matches = 0; 1115 size_t old_size = variables.GetSize(); 1116 while (auto result = results->getNext()) { 1117 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get()); 1118 if (max_matches > 0 && matches >= max_matches) 1119 break; 1120 1121 SymbolContext sc; 1122 sc.module_sp = m_objfile_sp->GetModule(); 1123 lldbassert(sc.module_sp.get()); 1124 1125 if (!name.GetStringRef().equals( 1126 MSVCUndecoratedNameParser::DropScope(pdb_data->getName()))) 1127 continue; 1128 1129 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1130 // FIXME: We are not able to determine the compile unit. 1131 if (sc.comp_unit == nullptr) 1132 continue; 1133 1134 if (parent_decl_ctx && GetDeclContextContainingUID( 1135 result->getSymIndexId()) != *parent_decl_ctx) 1136 continue; 1137 1138 ParseVariables(sc, *pdb_data, &variables); 1139 matches = variables.GetSize() - old_size; 1140 } 1141 } 1142 1143 void SymbolFilePDB::FindGlobalVariables( 1144 const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1145 lldb_private::VariableList &variables) { 1146 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1147 if (!regex.IsValid()) 1148 return; 1149 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1150 if (!results) 1151 return; 1152 1153 uint32_t matches = 0; 1154 size_t old_size = variables.GetSize(); 1155 while (auto pdb_data = results->getNext()) { 1156 if (max_matches > 0 && matches >= max_matches) 1157 break; 1158 1159 auto var_name = pdb_data->getName(); 1160 if (var_name.empty()) 1161 continue; 1162 if (!regex.Execute(var_name)) 1163 continue; 1164 SymbolContext sc; 1165 sc.module_sp = m_objfile_sp->GetModule(); 1166 lldbassert(sc.module_sp.get()); 1167 1168 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1169 // FIXME: We are not able to determine the compile unit. 1170 if (sc.comp_unit == nullptr) 1171 continue; 1172 1173 ParseVariables(sc, *pdb_data, &variables); 1174 matches = variables.GetSize() - old_size; 1175 } 1176 } 1177 1178 bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 1179 bool include_inlines, 1180 lldb_private::SymbolContextList &sc_list) { 1181 lldb_private::SymbolContext sc; 1182 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 1183 if (!sc.comp_unit) 1184 return false; 1185 sc.module_sp = sc.comp_unit->GetModule(); 1186 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit); 1187 if (!sc.function) 1188 return false; 1189 1190 sc_list.Append(sc); 1191 return true; 1192 } 1193 1194 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 1195 lldb_private::SymbolContextList &sc_list) { 1196 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 1197 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 1198 return false; 1199 return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 1200 } 1201 1202 void SymbolFilePDB::CacheFunctionNames() { 1203 if (!m_func_full_names.IsEmpty()) 1204 return; 1205 1206 std::map<uint64_t, uint32_t> addr_ids; 1207 1208 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 1209 while (auto pdb_func_up = results_up->getNext()) { 1210 if (pdb_func_up->isCompilerGenerated()) 1211 continue; 1212 1213 auto name = pdb_func_up->getName(); 1214 auto demangled_name = pdb_func_up->getUndecoratedName(); 1215 if (name.empty() && demangled_name.empty()) 1216 continue; 1217 1218 auto uid = pdb_func_up->getSymIndexId(); 1219 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 1220 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 1221 1222 if (auto parent = pdb_func_up->getClassParent()) { 1223 1224 // PDB have symbols for class/struct methods or static methods in Enum 1225 // Class. We won't bother to check if the parent is UDT or Enum here. 1226 m_func_method_names.Append(ConstString(name), uid); 1227 1228 // To search a method name, like NS::Class:MemberFunc, LLDB searches 1229 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does 1230 // not have information of this, we extract base names and cache them 1231 // by our own effort. 1232 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1233 if (!basename.empty()) 1234 m_func_base_names.Append(ConstString(basename), uid); 1235 else { 1236 m_func_base_names.Append(ConstString(name), uid); 1237 } 1238 1239 if (!demangled_name.empty()) 1240 m_func_full_names.Append(ConstString(demangled_name), uid); 1241 1242 } else { 1243 // Handle not-method symbols. 1244 1245 // The function name might contain namespace, or its lexical scope. 1246 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1247 if (!basename.empty()) 1248 m_func_base_names.Append(ConstString(basename), uid); 1249 else 1250 m_func_base_names.Append(ConstString(name), uid); 1251 1252 if (name == "main") { 1253 m_func_full_names.Append(ConstString(name), uid); 1254 1255 if (!demangled_name.empty() && name != demangled_name) { 1256 m_func_full_names.Append(ConstString(demangled_name), uid); 1257 m_func_base_names.Append(ConstString(demangled_name), uid); 1258 } 1259 } else if (!demangled_name.empty()) { 1260 m_func_full_names.Append(ConstString(demangled_name), uid); 1261 } else { 1262 m_func_full_names.Append(ConstString(name), uid); 1263 } 1264 } 1265 } 1266 } 1267 1268 if (auto results_up = 1269 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 1270 while (auto pub_sym_up = results_up->getNext()) { 1271 if (!pub_sym_up->isFunction()) 1272 continue; 1273 auto name = pub_sym_up->getName(); 1274 if (name.empty()) 1275 continue; 1276 1277 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 1278 auto vm_addr = pub_sym_up->getVirtualAddress(); 1279 1280 // PDB public symbol has mangled name for its associated function. 1281 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { 1282 // Cache mangled name. 1283 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); 1284 } 1285 } 1286 } 1287 } 1288 // Sort them before value searching is working properly 1289 m_func_full_names.Sort(); 1290 m_func_full_names.SizeToFit(); 1291 m_func_method_names.Sort(); 1292 m_func_method_names.SizeToFit(); 1293 m_func_base_names.Sort(); 1294 m_func_base_names.SizeToFit(); 1295 } 1296 1297 void SymbolFilePDB::FindFunctions( 1298 lldb_private::ConstString name, 1299 const lldb_private::CompilerDeclContext *parent_decl_ctx, 1300 FunctionNameType name_type_mask, bool include_inlines, 1301 lldb_private::SymbolContextList &sc_list) { 1302 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1303 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 1304 1305 if (name_type_mask == eFunctionNameTypeNone) 1306 return; 1307 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1308 return; 1309 if (name.IsEmpty()) 1310 return; 1311 1312 if (name_type_mask & eFunctionNameTypeFull || 1313 name_type_mask & eFunctionNameTypeBase || 1314 name_type_mask & eFunctionNameTypeMethod) { 1315 CacheFunctionNames(); 1316 1317 std::set<uint32_t> resolved_ids; 1318 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list, 1319 &resolved_ids](UniqueCStringMap<uint32_t> &Names) { 1320 std::vector<uint32_t> ids; 1321 if (!Names.GetValues(name, ids)) 1322 return; 1323 1324 for (uint32_t id : ids) { 1325 if (resolved_ids.find(id) != resolved_ids.end()) 1326 continue; 1327 1328 if (parent_decl_ctx && 1329 GetDeclContextContainingUID(id) != *parent_decl_ctx) 1330 continue; 1331 1332 if (ResolveFunction(id, include_inlines, sc_list)) 1333 resolved_ids.insert(id); 1334 } 1335 }; 1336 if (name_type_mask & eFunctionNameTypeFull) { 1337 ResolveFn(m_func_full_names); 1338 ResolveFn(m_func_base_names); 1339 ResolveFn(m_func_method_names); 1340 } 1341 if (name_type_mask & eFunctionNameTypeBase) 1342 ResolveFn(m_func_base_names); 1343 if (name_type_mask & eFunctionNameTypeMethod) 1344 ResolveFn(m_func_method_names); 1345 } 1346 } 1347 1348 void SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 1349 bool include_inlines, 1350 lldb_private::SymbolContextList &sc_list) { 1351 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1352 if (!regex.IsValid()) 1353 return; 1354 1355 CacheFunctionNames(); 1356 1357 std::set<uint32_t> resolved_ids; 1358 auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, 1359 this](UniqueCStringMap<uint32_t> &Names) { 1360 std::vector<uint32_t> ids; 1361 if (Names.GetValues(regex, ids)) { 1362 for (auto id : ids) { 1363 if (resolved_ids.find(id) == resolved_ids.end()) 1364 if (ResolveFunction(id, include_inlines, sc_list)) 1365 resolved_ids.insert(id); 1366 } 1367 } 1368 }; 1369 ResolveFn(m_func_full_names); 1370 ResolveFn(m_func_base_names); 1371 } 1372 1373 void SymbolFilePDB::GetMangledNamesForFunction( 1374 const std::string &scope_qualified_name, 1375 std::vector<lldb_private::ConstString> &mangled_names) {} 1376 1377 void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) { 1378 std::set<lldb::addr_t> sym_addresses; 1379 for (size_t i = 0; i < symtab.GetNumSymbols(); i++) 1380 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress()); 1381 1382 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>(); 1383 if (!results) 1384 return; 1385 1386 auto section_list = m_objfile_sp->GetSectionList(); 1387 if (!section_list) 1388 return; 1389 1390 while (auto pub_symbol = results->getNext()) { 1391 auto section_id = pub_symbol->getAddressSection(); 1392 1393 auto section = section_list->FindSectionByID(section_id); 1394 if (!section) 1395 continue; 1396 1397 auto offset = pub_symbol->getAddressOffset(); 1398 1399 auto file_addr = section->GetFileAddress() + offset; 1400 if (sym_addresses.find(file_addr) != sym_addresses.end()) 1401 continue; 1402 sym_addresses.insert(file_addr); 1403 1404 auto size = pub_symbol->getLength(); 1405 symtab.AddSymbol( 1406 Symbol(pub_symbol->getSymIndexId(), // symID 1407 pub_symbol->getName().c_str(), // name 1408 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type 1409 true, // external 1410 false, // is_debug 1411 false, // is_trampoline 1412 false, // is_artificial 1413 section, // section_sp 1414 offset, // value 1415 size, // size 1416 size != 0, // size_is_valid 1417 false, // contains_linker_annotations 1418 0 // flags 1419 )); 1420 } 1421 1422 symtab.CalculateSymbolSizes(); 1423 symtab.Finalize(); 1424 } 1425 1426 void SymbolFilePDB::FindTypes( 1427 lldb_private::ConstString name, 1428 const lldb_private::CompilerDeclContext *parent_decl_ctx, 1429 uint32_t max_matches, 1430 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1431 lldb_private::TypeMap &types) { 1432 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1433 if (!name) 1434 return; 1435 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1436 return; 1437 1438 searched_symbol_files.clear(); 1439 searched_symbol_files.insert(this); 1440 1441 // There is an assumption 'name' is not a regex 1442 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types); 1443 } 1444 1445 void SymbolFilePDB::DumpClangAST(Stream &s) { 1446 auto type_system_or_err = 1447 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1448 if (auto err = type_system_or_err.takeError()) { 1449 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 1450 std::move(err), "Unable to dump ClangAST"); 1451 return; 1452 } 1453 1454 auto *clang_type_system = 1455 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 1456 if (!clang_type_system) 1457 return; 1458 clang_type_system->Dump(s); 1459 } 1460 1461 void SymbolFilePDB::FindTypesByRegex( 1462 const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1463 lldb_private::TypeMap &types) { 1464 // When searching by regex, we need to go out of our way to limit the search 1465 // space as much as possible since this searches EVERYTHING in the PDB, 1466 // manually doing regex comparisons. PDB library isn't optimized for regex 1467 // searches or searches across multiple symbol types at the same time, so the 1468 // best we can do is to search enums, then typedefs, then classes one by one, 1469 // and do a regex comparison against each of them. 1470 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 1471 PDB_SymType::UDT}; 1472 std::unique_ptr<IPDBEnumSymbols> results; 1473 1474 uint32_t matches = 0; 1475 1476 for (auto tag : tags_to_search) { 1477 results = m_global_scope_up->findAllChildren(tag); 1478 if (!results) 1479 continue; 1480 1481 while (auto result = results->getNext()) { 1482 if (max_matches > 0 && matches >= max_matches) 1483 break; 1484 1485 std::string type_name; 1486 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 1487 type_name = enum_type->getName(); 1488 else if (auto typedef_type = 1489 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 1490 type_name = typedef_type->getName(); 1491 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 1492 type_name = class_type->getName(); 1493 else { 1494 // We're looking only for types that have names. Skip symbols, as well 1495 // as unnamed types such as arrays, pointers, etc. 1496 continue; 1497 } 1498 1499 if (!regex.Execute(type_name)) 1500 continue; 1501 1502 // This should cause the type to get cached and stored in the `m_types` 1503 // lookup. 1504 if (!ResolveTypeUID(result->getSymIndexId())) 1505 continue; 1506 1507 auto iter = m_types.find(result->getSymIndexId()); 1508 if (iter == m_types.end()) 1509 continue; 1510 types.Insert(iter->second); 1511 ++matches; 1512 } 1513 } 1514 } 1515 1516 void SymbolFilePDB::FindTypesByName( 1517 llvm::StringRef name, 1518 const lldb_private::CompilerDeclContext *parent_decl_ctx, 1519 uint32_t max_matches, lldb_private::TypeMap &types) { 1520 std::unique_ptr<IPDBEnumSymbols> results; 1521 if (name.empty()) 1522 return; 1523 results = m_global_scope_up->findAllChildren(PDB_SymType::None); 1524 if (!results) 1525 return; 1526 1527 uint32_t matches = 0; 1528 1529 while (auto result = results->getNext()) { 1530 if (max_matches > 0 && matches >= max_matches) 1531 break; 1532 1533 if (MSVCUndecoratedNameParser::DropScope( 1534 result->getRawSymbol().getName()) != name) 1535 continue; 1536 1537 switch (result->getSymTag()) { 1538 case PDB_SymType::Enum: 1539 case PDB_SymType::UDT: 1540 case PDB_SymType::Typedef: 1541 break; 1542 default: 1543 // We're looking only for types that have names. Skip symbols, as well 1544 // as unnamed types such as arrays, pointers, etc. 1545 continue; 1546 } 1547 1548 // This should cause the type to get cached and stored in the `m_types` 1549 // lookup. 1550 if (!ResolveTypeUID(result->getSymIndexId())) 1551 continue; 1552 1553 if (parent_decl_ctx && GetDeclContextContainingUID( 1554 result->getSymIndexId()) != *parent_decl_ctx) 1555 continue; 1556 1557 auto iter = m_types.find(result->getSymIndexId()); 1558 if (iter == m_types.end()) 1559 continue; 1560 types.Insert(iter->second); 1561 ++matches; 1562 } 1563 } 1564 1565 void SymbolFilePDB::FindTypes( 1566 llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 1567 llvm::DenseSet<SymbolFile *> &searched_symbol_files, 1568 lldb_private::TypeMap &types) {} 1569 1570 void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, 1571 uint32_t type_mask, 1572 TypeCollection &type_collection) { 1573 bool can_parse = false; 1574 switch (pdb_symbol.getSymTag()) { 1575 case PDB_SymType::ArrayType: 1576 can_parse = ((type_mask & eTypeClassArray) != 0); 1577 break; 1578 case PDB_SymType::BuiltinType: 1579 can_parse = ((type_mask & eTypeClassBuiltin) != 0); 1580 break; 1581 case PDB_SymType::Enum: 1582 can_parse = ((type_mask & eTypeClassEnumeration) != 0); 1583 break; 1584 case PDB_SymType::Function: 1585 case PDB_SymType::FunctionSig: 1586 can_parse = ((type_mask & eTypeClassFunction) != 0); 1587 break; 1588 case PDB_SymType::PointerType: 1589 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | 1590 eTypeClassMemberPointer)) != 0); 1591 break; 1592 case PDB_SymType::Typedef: 1593 can_parse = ((type_mask & eTypeClassTypedef) != 0); 1594 break; 1595 case PDB_SymType::UDT: { 1596 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); 1597 assert(udt); 1598 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && 1599 ((type_mask & (eTypeClassClass | eTypeClassStruct | 1600 eTypeClassUnion)) != 0)); 1601 } break; 1602 default: 1603 break; 1604 } 1605 1606 if (can_parse) { 1607 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { 1608 auto result = 1609 std::find(type_collection.begin(), type_collection.end(), type); 1610 if (result == type_collection.end()) 1611 type_collection.push_back(type); 1612 } 1613 } 1614 1615 auto results_up = pdb_symbol.findAllChildren(); 1616 while (auto symbol_up = results_up->getNext()) 1617 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); 1618 } 1619 1620 void SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1621 TypeClass type_mask, 1622 lldb_private::TypeList &type_list) { 1623 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1624 TypeCollection type_collection; 1625 CompileUnit *cu = 1626 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 1627 if (cu) { 1628 auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1629 if (!compiland_up) 1630 return; 1631 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1632 } else { 1633 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 1634 auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1635 if (cu_sp) { 1636 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1637 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1638 } 1639 } 1640 } 1641 1642 for (auto type : type_collection) { 1643 type->GetForwardCompilerType(); 1644 type_list.Insert(type->shared_from_this()); 1645 } 1646 } 1647 1648 llvm::Expected<lldb_private::TypeSystem &> 1649 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1650 auto type_system_or_err = 1651 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language); 1652 if (type_system_or_err) { 1653 type_system_or_err->SetSymbolFile(this); 1654 } 1655 return type_system_or_err; 1656 } 1657 1658 PDBASTParser *SymbolFilePDB::GetPDBAstParser() { 1659 auto type_system_or_err = 1660 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1661 if (auto err = type_system_or_err.takeError()) { 1662 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 1663 std::move(err), "Unable to get PDB AST parser"); 1664 return nullptr; 1665 } 1666 1667 auto *clang_type_system = 1668 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 1669 if (!clang_type_system) 1670 return nullptr; 1671 1672 return clang_type_system->GetPDBParser(); 1673 } 1674 1675 1676 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 1677 lldb_private::ConstString name, 1678 const lldb_private::CompilerDeclContext *parent_decl_ctx) { 1679 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1680 auto type_system_or_err = 1681 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1682 if (auto err = type_system_or_err.takeError()) { 1683 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 1684 std::move(err), "Unable to find namespace {}", 1685 name.AsCString()); 1686 return CompilerDeclContext(); 1687 } 1688 1689 auto *clang_type_system = 1690 llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get()); 1691 if (!clang_type_system) 1692 return CompilerDeclContext(); 1693 1694 PDBASTParser *pdb = clang_type_system->GetPDBParser(); 1695 if (!pdb) 1696 return CompilerDeclContext(); 1697 1698 clang::DeclContext *decl_context = nullptr; 1699 if (parent_decl_ctx) 1700 decl_context = static_cast<clang::DeclContext *>( 1701 parent_decl_ctx->GetOpaqueDeclContext()); 1702 1703 auto namespace_decl = 1704 pdb->FindNamespaceDecl(decl_context, name.GetStringRef()); 1705 if (!namespace_decl) 1706 return CompilerDeclContext(); 1707 1708 return clang_type_system->CreateDeclContext(namespace_decl); 1709 } 1710 1711 lldb_private::ConstString SymbolFilePDB::GetPluginName() { 1712 static ConstString g_name("pdb"); 1713 return g_name; 1714 } 1715 1716 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 1717 1718 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1719 1720 const IPDBSession &SymbolFilePDB::GetPDBSession() const { 1721 return *m_session_up; 1722 } 1723 1724 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, 1725 uint32_t index) { 1726 auto found_cu = m_comp_units.find(id); 1727 if (found_cu != m_comp_units.end()) 1728 return found_cu->second; 1729 1730 auto compiland_up = GetPDBCompilandByUID(id); 1731 if (!compiland_up) 1732 return CompUnitSP(); 1733 1734 lldb::LanguageType lang; 1735 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 1736 if (!details) 1737 lang = lldb::eLanguageTypeC_plus_plus; 1738 else 1739 lang = TranslateLanguage(details->getLanguage()); 1740 1741 if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1742 return CompUnitSP(); 1743 1744 std::string path = compiland_up->getSourceFileFullPath(); 1745 if (path.empty()) 1746 return CompUnitSP(); 1747 1748 // Don't support optimized code for now, DebugInfoPDB does not return this 1749 // information. 1750 LazyBool optimized = eLazyBoolNo; 1751 auto cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr, 1752 path.c_str(), id, lang, optimized); 1753 1754 if (!cu_sp) 1755 return CompUnitSP(); 1756 1757 m_comp_units.insert(std::make_pair(id, cu_sp)); 1758 if (index == UINT32_MAX) 1759 GetCompileUnitIndex(*compiland_up, index); 1760 lldbassert(index != UINT32_MAX); 1761 SetCompileUnitAtIndex(index, cu_sp); 1762 return cu_sp; 1763 } 1764 1765 bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit, 1766 uint32_t match_line) { 1767 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 1768 if (!compiland_up) 1769 return false; 1770 1771 // LineEntry needs the *index* of the file into the list of support files 1772 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 1773 // a globally unique idenfitifier in the namespace of the PDB. So, we have 1774 // to do a mapping so that we can hand out indices. 1775 llvm::DenseMap<uint32_t, uint32_t> index_map; 1776 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 1777 auto line_table = std::make_unique<LineTable>(&comp_unit); 1778 1779 // Find contributions to `compiland` from all source and header files. 1780 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 1781 if (!files) 1782 return false; 1783 1784 // For each source and header file, create a LineSequence for contributions 1785 // to the compiland from that file, and add the sequence. 1786 while (auto file = files->getNext()) { 1787 std::unique_ptr<LineSequence> sequence( 1788 line_table->CreateLineSequenceContainer()); 1789 auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 1790 if (!lines) 1791 continue; 1792 int entry_count = lines->getChildCount(); 1793 1794 uint64_t prev_addr; 1795 uint32_t prev_length; 1796 uint32_t prev_line; 1797 uint32_t prev_source_idx; 1798 1799 for (int i = 0; i < entry_count; ++i) { 1800 auto line = lines->getChildAtIndex(i); 1801 1802 uint64_t lno = line->getLineNumber(); 1803 uint64_t addr = line->getVirtualAddress(); 1804 uint32_t length = line->getLength(); 1805 uint32_t source_id = line->getSourceFileId(); 1806 uint32_t col = line->getColumnNumber(); 1807 uint32_t source_idx = index_map[source_id]; 1808 1809 // There was a gap between the current entry and the previous entry if 1810 // the addresses don't perfectly line up. 1811 bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 1812 1813 // Before inserting the current entry, insert a terminal entry at the end 1814 // of the previous entry's address range if the current entry resulted in 1815 // a gap from the previous entry. 1816 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1817 line_table->AppendLineEntryToSequence( 1818 sequence.get(), prev_addr + prev_length, prev_line, 0, 1819 prev_source_idx, false, false, false, false, true); 1820 1821 line_table->InsertSequence(sequence.release()); 1822 sequence = line_table->CreateLineSequenceContainer(); 1823 } 1824 1825 if (ShouldAddLine(match_line, lno, length)) { 1826 bool is_statement = line->isStatement(); 1827 bool is_prologue = false; 1828 bool is_epilogue = false; 1829 auto func = 1830 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1831 if (func) { 1832 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 1833 if (prologue) 1834 is_prologue = (addr == prologue->getVirtualAddress()); 1835 1836 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 1837 if (epilogue) 1838 is_epilogue = (addr == epilogue->getVirtualAddress()); 1839 } 1840 1841 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1842 source_idx, is_statement, false, 1843 is_prologue, is_epilogue, false); 1844 } 1845 1846 prev_addr = addr; 1847 prev_length = length; 1848 prev_line = lno; 1849 prev_source_idx = source_idx; 1850 } 1851 1852 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 1853 // The end is always a terminal entry, so insert it regardless. 1854 line_table->AppendLineEntryToSequence( 1855 sequence.get(), prev_addr + prev_length, prev_line, 0, 1856 prev_source_idx, false, false, false, false, true); 1857 } 1858 1859 line_table->InsertSequence(sequence.get()); 1860 } 1861 1862 if (line_table->GetSize()) { 1863 comp_unit.SetLineTable(line_table.release()); 1864 return true; 1865 } 1866 return false; 1867 } 1868 1869 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 1870 const PDBSymbolCompiland &compiland, 1871 llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 1872 // This is a hack, but we need to convert the source id into an index into 1873 // the support files array. We don't want to do path comparisons to avoid 1874 // basename / full path issues that may or may not even be a problem, so we 1875 // use the globally unique source file identifiers. Ideally we could use the 1876 // global identifiers everywhere, but LineEntry currently assumes indices. 1877 auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 1878 if (!source_files) 1879 return; 1880 1881 int index = 0; 1882 while (auto file = source_files->getNext()) { 1883 uint32_t source_id = file->getUniqueId(); 1884 index_map[source_id] = index++; 1885 } 1886 } 1887 1888 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 1889 const lldb_private::Address &so_addr) { 1890 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1891 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 1892 return nullptr; 1893 1894 // If it is a PDB function's vm addr, this is the first sure bet. 1895 if (auto lines = 1896 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { 1897 if (auto first_line = lines->getNext()) 1898 return ParseCompileUnitForUID(first_line->getCompilandId()); 1899 } 1900 1901 // Otherwise we resort to section contributions. 1902 if (auto sec_contribs = m_session_up->getSectionContribs()) { 1903 while (auto section = sec_contribs->getNext()) { 1904 auto va = section->getVirtualAddress(); 1905 if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) 1906 return ParseCompileUnitForUID(section->getCompilandId()); 1907 } 1908 } 1909 return nullptr; 1910 } 1911 1912 Mangled 1913 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 1914 Mangled mangled; 1915 auto func_name = pdb_func.getName(); 1916 auto func_undecorated_name = pdb_func.getUndecoratedName(); 1917 std::string func_decorated_name; 1918 1919 // Seek from public symbols for non-static function's decorated name if any. 1920 // For static functions, they don't have undecorated names and aren't exposed 1921 // in Public Symbols either. 1922 if (!func_undecorated_name.empty()) { 1923 auto result_up = m_global_scope_up->findChildren( 1924 PDB_SymType::PublicSymbol, func_undecorated_name, 1925 PDB_NameSearchFlags::NS_UndecoratedName); 1926 if (result_up) { 1927 while (auto symbol_up = result_up->getNext()) { 1928 // For a public symbol, it is unique. 1929 lldbassert(result_up->getChildCount() == 1); 1930 if (auto *pdb_public_sym = 1931 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( 1932 symbol_up.get())) { 1933 if (pdb_public_sym->isFunction()) { 1934 func_decorated_name = pdb_public_sym->getName(); 1935 break; 1936 } 1937 } 1938 } 1939 } 1940 } 1941 if (!func_decorated_name.empty()) { 1942 mangled.SetMangledName(ConstString(func_decorated_name)); 1943 1944 // For MSVC, format of C funciton's decorated name depends on calling 1945 // conventon. Unfortunately none of the format is recognized by current 1946 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 1947 // `__purecall` is retrieved as both its decorated and undecorated name 1948 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` 1949 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). 1950 // Mangled::GetDemangledName method will fail internally and caches an 1951 // empty string as its undecorated name. So we will face a contradition 1952 // here for the same symbol: 1953 // non-empty undecorated name from PDB 1954 // empty undecorated name from LLDB 1955 if (!func_undecorated_name.empty() && mangled.GetDemangledName().IsEmpty()) 1956 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1957 1958 // LLDB uses several flags to control how a C++ decorated name is 1959 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the 1960 // yielded name could be different from what we retrieve from 1961 // PDB source unless we also apply same flags in getting undecorated 1962 // name through PDBSymbolFunc::getUndecoratedNameEx method. 1963 if (!func_undecorated_name.empty() && 1964 mangled.GetDemangledName() != ConstString(func_undecorated_name)) 1965 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1966 } else if (!func_undecorated_name.empty()) { 1967 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1968 } else if (!func_name.empty()) 1969 mangled.SetValue(ConstString(func_name), false); 1970 1971 return mangled; 1972 } 1973 1974 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 1975 const lldb_private::CompilerDeclContext *decl_ctx) { 1976 if (decl_ctx == nullptr || !decl_ctx->IsValid()) 1977 return true; 1978 1979 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 1980 if (!decl_ctx_type_system) 1981 return false; 1982 auto type_system_or_err = GetTypeSystemForLanguage( 1983 decl_ctx_type_system->GetMinimumLanguage(nullptr)); 1984 if (auto err = type_system_or_err.takeError()) { 1985 LLDB_LOG_ERROR( 1986 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 1987 std::move(err), 1988 "Unable to determine if DeclContext matches this symbol file"); 1989 return false; 1990 } 1991 1992 if (decl_ctx_type_system == &type_system_or_err.get()) 1993 return true; // The type systems match, return true 1994 1995 return false; 1996 } 1997 1998 uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) { 1999 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) { 2000 return lhs < rhs.Offset; 2001 }; 2002 2003 // Cache section contributions 2004 if (m_sec_contribs.empty()) { 2005 if (auto SecContribs = m_session_up->getSectionContribs()) { 2006 while (auto SectionContrib = SecContribs->getNext()) { 2007 auto comp_id = SectionContrib->getCompilandId(); 2008 if (!comp_id) 2009 continue; 2010 2011 auto sec = SectionContrib->getAddressSection(); 2012 auto &sec_cs = m_sec_contribs[sec]; 2013 2014 auto offset = SectionContrib->getAddressOffset(); 2015 auto it = 2016 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper); 2017 2018 auto size = SectionContrib->getLength(); 2019 sec_cs.insert(it, {offset, size, comp_id}); 2020 } 2021 } 2022 } 2023 2024 // Check by line number 2025 if (auto Lines = data.getLineNumbers()) { 2026 if (auto FirstLine = Lines->getNext()) 2027 return FirstLine->getCompilandId(); 2028 } 2029 2030 // Retrieve section + offset 2031 uint32_t DataSection = data.getAddressSection(); 2032 uint32_t DataOffset = data.getAddressOffset(); 2033 if (DataSection == 0) { 2034 if (auto RVA = data.getRelativeVirtualAddress()) 2035 m_session_up->addressForRVA(RVA, DataSection, DataOffset); 2036 } 2037 2038 if (DataSection) { 2039 // Search by section contributions 2040 auto &sec_cs = m_sec_contribs[DataSection]; 2041 auto it = 2042 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper); 2043 if (it != sec_cs.begin()) { 2044 --it; 2045 if (DataOffset < it->Offset + it->Size) 2046 return it->CompilandId; 2047 } 2048 } else { 2049 // Search in lexical tree 2050 auto LexParentId = data.getLexicalParentId(); 2051 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) { 2052 if (LexParent->getSymTag() == PDB_SymType::Exe) 2053 break; 2054 if (LexParent->getSymTag() == PDB_SymType::Compiland) 2055 return LexParentId; 2056 LexParentId = LexParent->getRawSymbol().getLexicalParentId(); 2057 } 2058 } 2059 2060 return 0; 2061 } 2062