//===- DIASession.cpp - DIA implementation of IPDBSession -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "llvm/DebugInfo/PDB/DIA/DIASession.h" #include "llvm/ADT/STLExtras.h" #include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h" #include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h" #include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h" #include "llvm/DebugInfo/PDB/DIA/DIAEnumTables.h" #include "llvm/DebugInfo/PDB/DIA/DIAError.h" #include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h" #include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h" #include "llvm/DebugInfo/PDB/DIA/DIASupport.h" #include "llvm/DebugInfo/PDB/GenericError.h" #include "llvm/DebugInfo/PDB/PDB.h" #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Format.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace llvm::pdb; template static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) { SmallString<64> MessageStorage; StringRef Context; if (sizeof...(Args) > 0) { MessageStorage = formatv(Str, std::forward(Args)...).str(); Context = MessageStorage; } else Context = Str; switch (Result) { case E_PDB_NOT_FOUND: return make_error(generic_error_code::invalid_path, Context); case E_PDB_FORMAT: return make_error(dia_error_code::invalid_file_format, Context); case E_INVALIDARG: return make_error(dia_error_code::invalid_parameter, Context); case E_UNEXPECTED: return make_error(dia_error_code::already_loaded, Context); case E_PDB_INVALID_SIG: case E_PDB_INVALID_AGE: return make_error(dia_error_code::debug_info_mismatch, Context); default: { std::string S; raw_string_ostream OS(S); OS << "HRESULT: " << format_hex(static_cast(Result), 10, true) << ": " << Context; return make_error(dia_error_code::unspecified, OS.str()); } } } static Error LoadDIA(CComPtr &DiaDataSource) { if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER, IID_IDiaDataSource, reinterpret_cast(&DiaDataSource)))) return Error::success(); // If the CoCreateInstance call above failed, msdia*.dll is not registered. // Try loading the DLL corresponding to the #included DIA SDK. #if !defined(_MSC_VER) return llvm::make_error( "DIA is only supported when using MSVC."); #else const wchar_t *msdia_dll = nullptr; #if _MSC_VER >= 1900 && _MSC_VER < 2000 msdia_dll = L"msdia140.dll"; // VS2015 #elif _MSC_VER >= 1800 msdia_dll = L"msdia120.dll"; // VS2013 #else #error "Unknown Visual Studio version." #endif HRESULT HR; if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource, reinterpret_cast(&DiaDataSource)))) return ErrorFromHResult(HR, "Calling NoRegCoCreate"); return Error::success(); #endif } DIASession::DIASession(CComPtr DiaSession) : Session(DiaSession) {} Error DIASession::createFromPdb(StringRef Path, std::unique_ptr &Session) { CComPtr DiaDataSource; CComPtr DiaSession; // We assume that CoInitializeEx has already been called by the executable. if (auto E = LoadDIA(DiaDataSource)) return E; llvm::SmallVector Path16; if (!llvm::convertUTF8ToUTF16String(Path, Path16)) return make_error(generic_error_code::invalid_path); const wchar_t *Path16Str = reinterpret_cast(Path16.data()); HRESULT HR; if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) { return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path); } if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) return ErrorFromHResult(HR, "Calling openSession"); Session.reset(new DIASession(DiaSession)); return Error::success(); } Error DIASession::createFromExe(StringRef Path, std::unique_ptr &Session) { CComPtr DiaDataSource; CComPtr DiaSession; // We assume that CoInitializeEx has already been called by the executable. if (auto EC = LoadDIA(DiaDataSource)) return EC; llvm::SmallVector Path16; if (!llvm::convertUTF8ToUTF16String(Path, Path16)) return make_error(generic_error_code::invalid_path, Path); const wchar_t *Path16Str = reinterpret_cast(Path16.data()); HRESULT HR; if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr))) return ErrorFromHResult(HR, "Calling loadDataForExe"); if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) return ErrorFromHResult(HR, "Calling openSession"); Session.reset(new DIASession(DiaSession)); return Error::success(); } uint64_t DIASession::getLoadAddress() const { uint64_t LoadAddress; bool success = (S_OK == Session->get_loadAddress(&LoadAddress)); return (success) ? LoadAddress : 0; } void DIASession::setLoadAddress(uint64_t Address) { Session->put_loadAddress(Address); } std::unique_ptr DIASession::getGlobalScope() { CComPtr GlobalScope; if (S_OK != Session->get_globalScope(&GlobalScope)) return nullptr; auto RawSymbol = llvm::make_unique(*this, GlobalScope); auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol))); std::unique_ptr ExeSymbol( static_cast(PdbSymbol.release())); return ExeSymbol; } std::unique_ptr DIASession::getSymbolById(uint32_t SymbolId) const { CComPtr LocatedSymbol; if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) return nullptr; auto RawSymbol = llvm::make_unique(*this, LocatedSymbol); return PDBSymbol::create(*this, std::move(RawSymbol)); } std::unique_ptr DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { enum SymTagEnum EnumVal = static_cast(Type); CComPtr Symbol; if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) { ULONGLONG LoadAddr = 0; if (S_OK != Session->get_loadAddress(&LoadAddr)) return nullptr; DWORD RVA = static_cast(Address - LoadAddr); if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol)) return nullptr; } auto RawSymbol = llvm::make_unique(*this, Symbol); return PDBSymbol::create(*this, std::move(RawSymbol)); } std::unique_ptr DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland, const IPDBSourceFile &File) const { const DIARawSymbol &RawCompiland = static_cast(Compiland.getRawSymbol()); const DIASourceFile &RawFile = static_cast(File); CComPtr LineNumbers; if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(), &LineNumbers)) return nullptr; return llvm::make_unique(LineNumbers); } std::unique_ptr DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const { CComPtr LineNumbers; if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) return nullptr; return llvm::make_unique(LineNumbers); } std::unique_ptr DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const { IDiaSymbol *DiaCompiland = nullptr; CComBSTR Utf16Pattern; if (!Pattern.empty()) Utf16Pattern = CComBSTR(Pattern.data()); if (Compiland) DiaCompiland = static_cast(Compiland->getRawSymbol()) .getDiaSymbol(); Flags = static_cast( Flags | PDB_NameSearchFlags::NS_FileNameExtMatch); CComPtr SourceFiles; if (S_OK != Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles)) return nullptr; return llvm::make_unique(*this, SourceFiles); } std::unique_ptr DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const { auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags); if (!SourceFiles || SourceFiles->getChildCount() == 0) return nullptr; return SourceFiles->getNext(); } std::unique_ptr> DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const { auto File = findOneSourceFile(nullptr, Pattern, Flags); if (!File) return nullptr; return File->getCompilands(); } std::unique_ptr DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const { auto Compilands = findCompilandsForSourceFile(Pattern, Flags); if (!Compilands || Compilands->getChildCount() == 0) return nullptr; return Compilands->getNext(); } std::unique_ptr DIASession::getAllSourceFiles() const { CComPtr Files; if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files)) return nullptr; return llvm::make_unique(*this, Files); } std::unique_ptr DIASession::getSourceFilesForCompiland( const PDBSymbolCompiland &Compiland) const { CComPtr Files; const DIARawSymbol &RawSymbol = static_cast(Compiland.getRawSymbol()); if (S_OK != Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files)) return nullptr; return llvm::make_unique(*this, Files); } std::unique_ptr DIASession::getSourceFileById(uint32_t FileId) const { CComPtr LocatedFile; if (S_OK != Session->findFileById(FileId, &LocatedFile)) return nullptr; return llvm::make_unique(*this, LocatedFile); } std::unique_ptr DIASession::getDebugStreams() const { CComPtr DiaEnumerator; if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) return nullptr; return llvm::make_unique(DiaEnumerator); } std::unique_ptr DIASession::getEnumTables() const { CComPtr DiaEnumerator; if (S_OK != Session->getEnumTables(&DiaEnumerator)) return nullptr; return llvm::make_unique(DiaEnumerator); }