1 //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "SymbolFilePDB.h" 11 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Symbol/CompileUnit.h" 15 #include "lldb/Symbol/LineTable.h" 16 #include "lldb/Symbol/ObjectFile.h" 17 #include "lldb/Symbol/SymbolContext.h" 18 19 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 20 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 21 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 22 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 23 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 24 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 25 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 26 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 27 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 28 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 29 30 using namespace lldb_private; 31 32 namespace 33 { 34 lldb::LanguageType TranslateLanguage(llvm::PDB_Lang lang) 35 { 36 switch (lang) 37 { 38 case llvm::PDB_Lang::Cpp: 39 return lldb::LanguageType::eLanguageTypeC_plus_plus; 40 case llvm::PDB_Lang::C: 41 return lldb::LanguageType::eLanguageTypeC; 42 default: 43 return lldb::LanguageType::eLanguageTypeUnknown; 44 } 45 } 46 } 47 48 void 49 SymbolFilePDB::Initialize() 50 { 51 PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 52 DebuggerInitialize); 53 } 54 55 void 56 SymbolFilePDB::Terminate() 57 { 58 PluginManager::UnregisterPlugin(CreateInstance); 59 } 60 61 void 62 SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) 63 { 64 } 65 66 lldb_private::ConstString 67 SymbolFilePDB::GetPluginNameStatic() 68 { 69 static ConstString g_name("pdb"); 70 return g_name; 71 } 72 73 const char * 74 SymbolFilePDB::GetPluginDescriptionStatic() 75 { 76 return "Microsoft PDB debug symbol file reader."; 77 } 78 79 lldb_private::SymbolFile * 80 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) 81 { 82 return new SymbolFilePDB(obj_file); 83 } 84 85 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 86 : SymbolFile(object_file), m_cached_compile_unit_count(0) 87 { 88 } 89 90 SymbolFilePDB::~SymbolFilePDB() 91 { 92 } 93 94 uint32_t 95 SymbolFilePDB::CalculateAbilities() 96 { 97 if (!m_session_up) 98 { 99 // Lazily load and match the PDB file, but only do this once. 100 std::string exePath = m_obj_file->GetFileSpec().GetPath(); 101 auto error = llvm::loadDataForEXE(llvm::PDB_ReaderType::DIA, llvm::StringRef(exePath), m_session_up); 102 if (error != llvm::PDB_ErrorCode::Success) 103 return 0; 104 } 105 return CompileUnits | LineTables; 106 } 107 108 void 109 SymbolFilePDB::InitializeObject() 110 { 111 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); 112 m_session_up->setLoadAddress(obj_load_address); 113 } 114 115 uint32_t 116 SymbolFilePDB::GetNumCompileUnits() 117 { 118 if (m_cached_compile_unit_count == 0) 119 { 120 auto global = m_session_up->getGlobalScope(); 121 auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>(); 122 m_cached_compile_unit_count = compilands->getChildCount(); 123 124 // The linker can inject an additional "dummy" compilation unit into the PDB. 125 // Ignore this special compile unit for our purposes, if it is there. It is 126 // always the last one. 127 auto last_cu = compilands->getChildAtIndex(m_cached_compile_unit_count - 1); 128 std::string name = last_cu->getName(); 129 if (name == "* Linker *") 130 --m_cached_compile_unit_count; 131 } 132 return m_cached_compile_unit_count; 133 } 134 135 lldb::CompUnitSP 136 SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) 137 { 138 auto global = m_session_up->getGlobalScope(); 139 auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>(); 140 auto cu = compilands->getChildAtIndex(index); 141 142 uint32_t id = cu->getSymIndexId(); 143 144 return ParseCompileUnitForSymIndex(id); 145 } 146 147 lldb::LanguageType 148 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) 149 { 150 // What fields should I expect to be filled out on the SymbolContext? Is it 151 // safe to assume that `sc.comp_unit` is valid? 152 if (!sc.comp_unit) 153 return lldb::eLanguageTypeUnknown; 154 155 auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); 156 if (!cu) 157 return lldb::eLanguageTypeUnknown; 158 auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>(); 159 if (!details) 160 return lldb::eLanguageTypeUnknown; 161 return TranslateLanguage(details->getLanguage()); 162 } 163 164 size_t 165 SymbolFilePDB::ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc) 166 { 167 // TODO: Implement this 168 return size_t(); 169 } 170 171 bool 172 SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc) 173 { 174 return ParseCompileUnitLineTable(sc, 0); 175 } 176 177 bool 178 SymbolFilePDB::ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc) 179 { 180 // PDB doesn't contain information about macros 181 return false; 182 } 183 184 bool 185 SymbolFilePDB::ParseCompileUnitSupportFiles(const lldb_private::SymbolContext &sc, 186 lldb_private::FileSpecList &support_files) 187 { 188 if (!sc.comp_unit) 189 return false; 190 191 // In theory this is unnecessary work for us, because all of this information is easily 192 // (and quickly) accessible from DebugInfoPDB, so caching it a second time seems like a waste. 193 // Unfortunately, there's no good way around this short of a moderate refactor, since SymbolVendor 194 // depends on being able to cache this list. 195 auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); 196 if (!cu) 197 return false; 198 auto files = m_session_up->getSourceFilesForCompiland(*cu); 199 if (!files || files->getChildCount() == 0) 200 return false; 201 202 while (auto file = files->getNext()) 203 { 204 FileSpec spec(file->getFileName(), false); 205 support_files.Append(spec); 206 } 207 return true; 208 } 209 210 bool 211 SymbolFilePDB::ParseImportedModules(const lldb_private::SymbolContext &sc, 212 std::vector<lldb_private::ConstString> &imported_modules) 213 { 214 // PDB does not yet support module debug info 215 return false; 216 } 217 218 size_t 219 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) 220 { 221 // TODO: Implement this 222 return size_t(); 223 } 224 225 size_t 226 SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) 227 { 228 // TODO: Implement this 229 return size_t(); 230 } 231 232 size_t 233 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) 234 { 235 // TODO: Implement this 236 return size_t(); 237 } 238 239 lldb_private::Type * 240 SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) 241 { 242 return nullptr; 243 } 244 245 bool 246 SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) 247 { 248 // TODO: Implement this 249 return false; 250 } 251 252 lldb_private::CompilerDecl 253 SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) 254 { 255 return lldb_private::CompilerDecl(); 256 } 257 258 lldb_private::CompilerDeclContext 259 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) 260 { 261 return lldb_private::CompilerDeclContext(); 262 } 263 264 lldb_private::CompilerDeclContext 265 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) 266 { 267 return lldb_private::CompilerDeclContext(); 268 } 269 270 void 271 SymbolFilePDB::ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) 272 { 273 } 274 275 uint32_t 276 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, uint32_t resolve_scope, 277 lldb_private::SymbolContext &sc) 278 { 279 return uint32_t(); 280 } 281 282 uint32_t 283 SymbolFilePDB::ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 284 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) 285 { 286 if (resolve_scope & lldb::eSymbolContextCompUnit) 287 { 288 // Locate all compilation units with line numbers referencing the specified file. For example, if 289 // `file_spec` is <vector>, then this should return all source files and header files that reference 290 // <vector>, either directly or indirectly. 291 auto compilands = 292 m_session_up->findCompilandsForSourceFile(file_spec.GetPath(), llvm::PDB_NameSearchFlags::NS_CaseInsensitive); 293 294 // For each one, either find get its previously parsed data, or parse it afresh and add it to 295 // the symbol context list. 296 while (auto compiland = compilands->getNext()) 297 { 298 // If we're not checking inlines, then don't add line information for this file unless the FileSpec 299 // matches. 300 if (!check_inlines) 301 { 302 // `getSourceFileName` returns the basename of the original source file used to generate this compiland. 303 // It does not return the full path. Currently the only way to get that is to do a basename lookup to 304 // get the IPDBSourceFile, but this is ambiguous in the case of two source files with the same name 305 // contributing to the same compiland. This is a moderately extreme edge case, so we consider this ok 306 // for now, although we need to find a long term solution. 307 std::string source_file = compiland->getSourceFileName(); 308 auto pdb_file = m_session_up->findOneSourceFile(compiland.get(), source_file, 309 llvm::PDB_NameSearchFlags::NS_CaseInsensitive); 310 source_file = pdb_file->getFileName(); 311 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); 312 if (!file_spec.FileEquals(this_spec)) 313 continue; 314 } 315 316 SymbolContext sc; 317 auto cu = ParseCompileUnitForSymIndex(compiland->getSymIndexId()); 318 sc.comp_unit = cu.get(); 319 sc.module_sp = cu->GetModule(); 320 sc_list.Append(sc); 321 322 // If we were asked to resolve line entries, add all entries to the line table that match the requested 323 // line (or all lines if `line` == 0) 324 if (resolve_scope & lldb::eSymbolContextLineEntry) 325 ParseCompileUnitLineTable(sc, line); 326 } 327 } 328 return sc_list.GetSize(); 329 } 330 331 uint32_t 332 SymbolFilePDB::FindGlobalVariables(const lldb_private::ConstString &name, 333 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 334 uint32_t max_matches, lldb_private::VariableList &variables) 335 { 336 return uint32_t(); 337 } 338 339 uint32_t 340 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, bool append, uint32_t max_matches, 341 lldb_private::VariableList &variables) 342 { 343 return uint32_t(); 344 } 345 346 uint32_t 347 SymbolFilePDB::FindFunctions(const lldb_private::ConstString &name, 348 const lldb_private::CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask, 349 bool include_inlines, bool append, lldb_private::SymbolContextList &sc_list) 350 { 351 return uint32_t(); 352 } 353 354 uint32_t 355 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, bool include_inlines, bool append, 356 lldb_private::SymbolContextList &sc_list) 357 { 358 return uint32_t(); 359 } 360 361 void 362 SymbolFilePDB::GetMangledNamesForFunction(const std::string &scope_qualified_name, 363 std::vector<lldb_private::ConstString> &mangled_names) 364 { 365 } 366 367 uint32_t 368 SymbolFilePDB::FindTypes(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name, 369 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, 370 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 371 lldb_private::TypeMap &types) 372 { 373 return uint32_t(); 374 } 375 376 size_t 377 SymbolFilePDB::FindTypes(const std::vector<lldb_private::CompilerContext> &context, bool append, 378 lldb_private::TypeMap &types) 379 { 380 return size_t(); 381 } 382 383 lldb_private::TypeList * 384 SymbolFilePDB::GetTypeList() 385 { 386 return nullptr; 387 } 388 389 size_t 390 SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, uint32_t type_mask, 391 lldb_private::TypeList &type_list) 392 { 393 return size_t(); 394 } 395 396 lldb_private::TypeSystem * 397 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) 398 { 399 auto type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 400 if (type_system) 401 type_system->SetSymbolFile(this); 402 return type_system; 403 } 404 405 lldb_private::CompilerDeclContext 406 SymbolFilePDB::FindNamespace(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name, 407 const lldb_private::CompilerDeclContext *parent_decl_ctx) 408 { 409 return lldb_private::CompilerDeclContext(); 410 } 411 412 lldb_private::ConstString 413 SymbolFilePDB::GetPluginName() 414 { 415 static ConstString g_name("pdb"); 416 return g_name; 417 } 418 419 uint32_t 420 SymbolFilePDB::GetPluginVersion() 421 { 422 return 1; 423 } 424 425 lldb::CompUnitSP 426 SymbolFilePDB::ParseCompileUnitForSymIndex(uint32_t id) 427 { 428 auto found_cu = m_comp_units.find(id); 429 if (found_cu != m_comp_units.end()) 430 return found_cu->second; 431 432 auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(id); 433 434 // `getSourceFileName` returns the basename of the original source file used to generate this compiland. It does 435 // not return the full path. Currently the only way to get that is to do a basename lookup to get the 436 // IPDBSourceFile, but this is ambiguous in the case of two source files with the same name contributing to the 437 // same compiland. This is a moderately extreme edge case, so we consider this ok for now, although we need to find 438 // a long term solution. 439 auto file = m_session_up->findOneSourceFile(cu.get(), cu->getSourceFileName(), 440 llvm::PDB_NameSearchFlags::NS_CaseInsensitive); 441 std::string path = file->getFileName(); 442 443 lldb::LanguageType lang; 444 auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>(); 445 if (!details) 446 lang = lldb::eLanguageTypeC_plus_plus; 447 else 448 lang = TranslateLanguage(details->getLanguage()); 449 450 // Don't support optimized code for now, DebugInfoPDB does not return this information. 451 bool optimized = false; 452 auto result = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized); 453 m_comp_units.insert(std::make_pair(id, result)); 454 return result; 455 } 456 457 bool 458 SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc, uint32_t match_line) 459 { 460 auto global = m_session_up->getGlobalScope(); 461 auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); 462 463 // LineEntry needs the *index* of the file into the list of support files returned by 464 // ParseCompileUnitSupportFiles. But the underlying SDK gives us a globally unique 465 // idenfitifier in the namespace of the PDB. So, we have to do a mapping so that we 466 // can hand out indices. 467 std::unordered_map<uint32_t, uint32_t> index_map; 468 BuildSupportFileIdToSupportFileIndexMap(*cu, index_map); 469 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); 470 471 // Find contributions to `cu` from all source and header files. 472 std::string path = sc.comp_unit->GetPath(); 473 auto files = m_session_up->getSourceFilesForCompiland(*cu); 474 475 // For each source and header file, create a LineSequence for contributions to the cu 476 // from that file, and add the sequence. 477 while (auto file = files->getNext()) 478 { 479 std::unique_ptr<LineSequence> sequence(line_table->CreateLineSequenceContainer()); 480 auto lines = m_session_up->findLineNumbers(*cu, *file); 481 int entry_count = lines->getChildCount(); 482 483 for (int i = 0; i < entry_count; ++i) 484 { 485 auto line = lines->getChildAtIndex(i); 486 uint32_t lno = line->getLineNumber(); 487 488 // If `match_line` == 0 we add any line no matter what. Otherwise, we only add 489 // lines that match the requested line number. 490 if (match_line != 0 && lno != match_line) 491 continue; 492 493 uint64_t va = line->getVirtualAddress(); 494 uint32_t cno = line->getColumnNumber(); 495 uint32_t source_id = line->getSourceFileId(); 496 uint32_t source_idx = index_map[source_id]; 497 498 bool is_basic_block = false; // PDB doesn't even have this concept, but LLDB doesn't use it anyway. 499 bool is_prologue = false; 500 bool is_epilogue = false; 501 bool is_statement = line->isStatement(); 502 auto func = m_session_up->findSymbolByAddress(va, llvm::PDB_SymType::Function); 503 if (func) 504 { 505 auto prologue = func->findOneChild<llvm::PDBSymbolFuncDebugStart>(); 506 is_prologue = (va == prologue->getVirtualAddress()); 507 508 auto epilogue = func->findOneChild<llvm::PDBSymbolFuncDebugEnd>(); 509 is_epilogue = (va == epilogue->getVirtualAddress()); 510 511 if (is_epilogue) 512 { 513 // Once per function, add a termination entry after the last byte of the function. 514 // TODO: This makes the assumption that all functions are laid out contiguously in 515 // memory and have no gaps. This is a wrong assumption in the general case, but is 516 // good enough to allow simple scenarios to work. This needs to be revisited. 517 auto concrete_func = llvm::dyn_cast<llvm::PDBSymbolFunc>(func.get()); 518 lldb::addr_t end_addr = concrete_func->getVirtualAddress() + concrete_func->getLength(); 519 line_table->InsertLineEntry(end_addr, lno, 0, source_idx, false, false, false, false, true); 520 } 521 } 522 523 line_table->InsertLineEntry(va, lno, cno, source_idx, is_statement, is_basic_block, is_prologue, 524 is_epilogue, false); 525 } 526 } 527 528 sc.comp_unit->SetLineTable(line_table.release()); 529 return true; 530 } 531 532 void 533 SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(const llvm::PDBSymbolCompiland &cu, 534 std::unordered_map<uint32_t, uint32_t> &index_map) const 535 { 536 // This is a hack, but we need to convert the source id into an index into the support 537 // files array. We don't want to do path comparisons to avoid basename / full path 538 // issues that may or may not even be a problem, so we use the globally unique source 539 // file identifiers. Ideally we could use the global identifiers everywhere, but LineEntry 540 // currently assumes indices. 541 auto source_files = m_session_up->getSourceFilesForCompiland(cu); 542 int index = 0; 543 544 while (auto file = source_files->getNext()) 545 { 546 uint32_t source_id = file->getUniqueId(); 547 index_map[source_id] = index++; 548 } 549 } 550