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