2015-02-11 05:17:52 +08:00
|
|
|
//===- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-05-07 04:51:57 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
|
2015-02-11 07:46:48 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-02-11 05:17:52 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
|
2015-04-18 06:40:36 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h"
|
2015-02-11 06:43:25 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
|
2016-05-07 04:51:57 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
|
2015-02-11 05:17:52 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
|
2016-05-07 04:51:57 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/DIA/DIASupport.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/GenericError.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/PDB.h"
|
2015-02-13 17:09:03 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
|
2015-02-11 05:17:52 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2016-10-20 00:42:20 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2017-04-25 01:47:24 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2016-10-20 00:42:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-02-11 05:17:52 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-05-05 04:32:13 +08:00
|
|
|
using namespace llvm::pdb;
|
2015-02-11 05:17:52 +08:00
|
|
|
|
2017-04-25 01:47:24 +08:00
|
|
|
template <typename... Ts>
|
|
|
|
static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) {
|
|
|
|
SmallString<64> MessageStorage;
|
|
|
|
StringRef Context;
|
|
|
|
if (sizeof...(Args) > 0) {
|
|
|
|
MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str();
|
|
|
|
Context = MessageStorage;
|
|
|
|
} else
|
|
|
|
Context = Str;
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
switch (Result) {
|
|
|
|
case E_PDB_NOT_FOUND:
|
2016-10-20 00:42:20 +08:00
|
|
|
return make_error<GenericError>(generic_error_code::invalid_path, Context);
|
2016-05-07 04:51:57 +08:00
|
|
|
case E_PDB_FORMAT:
|
2016-10-20 00:42:20 +08:00
|
|
|
return make_error<DIAError>(dia_error_code::invalid_file_format, Context);
|
2016-05-07 04:51:57 +08:00
|
|
|
case E_INVALIDARG:
|
2016-10-20 00:42:20 +08:00
|
|
|
return make_error<DIAError>(dia_error_code::invalid_parameter, Context);
|
2016-05-07 04:51:57 +08:00
|
|
|
case E_UNEXPECTED:
|
2016-10-20 00:42:20 +08:00
|
|
|
return make_error<DIAError>(dia_error_code::already_loaded, Context);
|
2016-05-07 04:51:57 +08:00
|
|
|
case E_PDB_INVALID_SIG:
|
|
|
|
case E_PDB_INVALID_AGE:
|
2016-10-20 00:42:20 +08:00
|
|
|
return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context);
|
|
|
|
default: {
|
|
|
|
std::string S;
|
|
|
|
raw_string_ostream OS(S);
|
|
|
|
OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true)
|
|
|
|
<< ": " << Context;
|
|
|
|
return make_error<DIAError>(dia_error_code::unspecified, OS.str());
|
|
|
|
}
|
2016-05-07 04:51:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 05:51:14 +08:00
|
|
|
static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
|
2016-04-02 06:21:51 +08:00
|
|
|
if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_IDiaDataSource,
|
|
|
|
reinterpret_cast<LPVOID *>(&DiaDataSource))))
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2016-04-02 06:21:51 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
// If the CoCreateInstance call above failed, msdia*.dll is not registered.
|
|
|
|
// Try loading the DLL corresponding to the #included DIA SDK.
|
2016-04-02 06:21:51 +08:00
|
|
|
#if !defined(_MSC_VER)
|
2016-05-07 04:51:57 +08:00
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"DIA is only supported when using MSVC.");
|
2016-10-13 05:51:14 +08:00
|
|
|
#else
|
2016-04-02 06:21:51 +08:00
|
|
|
const wchar_t *msdia_dll = nullptr;
|
2016-10-13 05:51:14 +08:00
|
|
|
#if _MSC_VER >= 1900 && _MSC_VER < 2000
|
2016-04-02 06:21:51 +08:00
|
|
|
msdia_dll = L"msdia140.dll"; // VS2015
|
2016-10-13 05:51:14 +08:00
|
|
|
#elif _MSC_VER >= 1800
|
2016-04-02 06:21:51 +08:00
|
|
|
msdia_dll = L"msdia120.dll"; // VS2013
|
|
|
|
#else
|
|
|
|
#error "Unknown Visual Studio version."
|
|
|
|
#endif
|
2016-04-20 01:36:58 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
HRESULT HR;
|
|
|
|
if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
|
|
|
|
reinterpret_cast<LPVOID *>(&DiaDataSource))))
|
2016-10-20 00:42:20 +08:00
|
|
|
return ErrorFromHResult(HR, "Calling NoRegCoCreate");
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2016-10-13 05:51:14 +08:00
|
|
|
#endif
|
2016-04-02 06:21:51 +08:00
|
|
|
}
|
2015-02-11 05:17:52 +08:00
|
|
|
|
|
|
|
DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Error DIASession::createFromPdb(StringRef Path,
|
|
|
|
std::unique_ptr<IPDBSession> &Session) {
|
2015-03-01 04:23:18 +08:00
|
|
|
CComPtr<IDiaDataSource> DiaDataSource;
|
|
|
|
CComPtr<IDiaSession> DiaSession;
|
2015-02-11 05:17:52 +08:00
|
|
|
|
|
|
|
// We assume that CoInitializeEx has already been called by the executable.
|
2016-05-07 04:51:57 +08:00
|
|
|
if (auto E = LoadDIA(DiaDataSource))
|
|
|
|
return E;
|
2015-02-11 05:17:52 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<UTF16, 128> Path16;
|
|
|
|
if (!llvm::convertUTF8ToUTF16String(Path, Path16))
|
2016-05-07 04:51:57 +08:00
|
|
|
return make_error<GenericError>(generic_error_code::invalid_path);
|
2015-02-11 05:17:52 +08:00
|
|
|
|
|
|
|
const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data());
|
2016-05-07 04:51:57 +08:00
|
|
|
HRESULT HR;
|
2017-04-25 01:47:24 +08:00
|
|
|
if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
|
|
|
|
return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path);
|
|
|
|
}
|
2015-02-11 05:17:52 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
|
2016-10-20 00:42:20 +08:00
|
|
|
return ErrorFromHResult(HR, "Calling openSession");
|
2015-03-01 04:23:18 +08:00
|
|
|
|
|
|
|
Session.reset(new DIASession(DiaSession));
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2015-02-11 05:17:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
Error DIASession::createFromExe(StringRef Path,
|
|
|
|
std::unique_ptr<IPDBSession> &Session) {
|
2015-04-18 06:40:36 +08:00
|
|
|
CComPtr<IDiaDataSource> DiaDataSource;
|
|
|
|
CComPtr<IDiaSession> DiaSession;
|
|
|
|
|
|
|
|
// We assume that CoInitializeEx has already been called by the executable.
|
2016-05-07 04:51:57 +08:00
|
|
|
if (auto EC = LoadDIA(DiaDataSource))
|
|
|
|
return EC;
|
2015-04-18 06:40:36 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<UTF16, 128> Path16;
|
|
|
|
if (!llvm::convertUTF8ToUTF16String(Path, Path16))
|
2016-05-07 04:51:57 +08:00
|
|
|
return make_error<GenericError>(generic_error_code::invalid_path, Path);
|
2015-04-18 06:40:36 +08:00
|
|
|
|
|
|
|
const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
|
2016-05-07 04:51:57 +08:00
|
|
|
HRESULT HR;
|
|
|
|
if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
|
2016-10-20 00:42:20 +08:00
|
|
|
return ErrorFromHResult(HR, "Calling loadDataForExe");
|
2015-04-18 06:40:36 +08:00
|
|
|
|
2016-05-07 04:51:57 +08:00
|
|
|
if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
|
2016-10-20 00:42:20 +08:00
|
|
|
return ErrorFromHResult(HR, "Calling openSession");
|
2015-04-18 06:40:36 +08:00
|
|
|
|
|
|
|
Session.reset(new DIASession(DiaSession));
|
2016-05-07 04:51:57 +08:00
|
|
|
return Error::success();
|
2015-04-18 06:40:36 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 05:17:52 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
[llvm-pdbdump] More advanced class definition dumping.
Previously the dumping of class definitions was very primitive,
and it made it hard to do more than the most trivial of output
formats when dumping. As such, we would only dump one line for
each field, and then dump non-layout items like nested types
and enums.
With this patch, we do a complete analysis of the object
hierarchy including aggregate types, bases, virtual bases,
vftable analysis, etc. The only immediately visible effects
of this are that a) we can now dump a line for the vfptr where
before we would treat that as padding, and b) we now don't
treat virtual bases that come at the end of a class as padding
since we have a more detailed analysis of the class's storage
usage.
In subsequent patches, we should be able to use this analysis
to display a complete graphical view of a class's layout including
recursing arbitrarily deep into an object's base class / aggregate
member hierarchy.
llvm-svn: 300133
2017-04-13 07:18:21 +08:00
|
|
|
std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const {
|
2015-02-11 05:17:52 +08:00
|
|
|
CComPtr<IDiaSymbol> GlobalScope;
|
|
|
|
if (S_OK != Session->get_globalScope(&GlobalScope))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
|
2015-02-11 05:17:52 +08:00
|
|
|
auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
|
|
|
|
std::unique_ptr<PDBSymbolExe> ExeSymbol(
|
|
|
|
static_cast<PDBSymbolExe *>(PdbSymbol.release()));
|
|
|
|
return ExeSymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const {
|
|
|
|
CComPtr<IDiaSymbol> LocatedSymbol;
|
|
|
|
if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
|
2015-02-11 05:17:52 +08:00
|
|
|
return PDBSymbol::create(*this, std::move(RawSymbol));
|
|
|
|
}
|
|
|
|
|
2015-04-18 06:40:36 +08:00
|
|
|
std::unique_ptr<PDBSymbol>
|
2015-05-02 04:24:26 +08:00
|
|
|
DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
|
|
|
|
enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
|
|
|
|
|
2015-04-18 06:40:36 +08:00
|
|
|
CComPtr<IDiaSymbol> Symbol;
|
2015-05-02 04:24:26 +08:00
|
|
|
if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
|
|
|
|
ULONGLONG LoadAddr = 0;
|
|
|
|
if (S_OK != Session->get_loadAddress(&LoadAddr))
|
|
|
|
return nullptr;
|
|
|
|
DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
|
|
|
|
if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-18 06:40:36 +08:00
|
|
|
auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
|
|
|
|
return PDBSymbol::create(*this, std::move(RawSymbol));
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:47:29 +08:00
|
|
|
std::unique_ptr<IPDBEnumLineNumbers>
|
|
|
|
DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland,
|
|
|
|
const IPDBSourceFile &File) const {
|
|
|
|
const DIARawSymbol &RawCompiland =
|
|
|
|
static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
|
|
|
|
const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
|
|
|
|
|
|
|
|
CComPtr<IDiaEnumLineNumbers> LineNumbers;
|
|
|
|
if (S_OK !=
|
|
|
|
Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(),
|
|
|
|
&LineNumbers))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
|
|
|
|
}
|
|
|
|
|
2015-04-18 06:40:36 +08:00
|
|
|
std::unique_ptr<IPDBEnumLineNumbers>
|
|
|
|
DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const {
|
|
|
|
CComPtr<IDiaEnumLineNumbers> LineNumbers;
|
|
|
|
if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:47:29 +08:00
|
|
|
std::unique_ptr<IPDBEnumSourceFiles>
|
|
|
|
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<const DIARawSymbol &>(Compiland->getRawSymbol())
|
|
|
|
.getDiaSymbol();
|
|
|
|
|
|
|
|
Flags = static_cast<PDB_NameSearchFlags>(
|
|
|
|
Flags | PDB_NameSearchFlags::NS_FileNameExtMatch);
|
|
|
|
CComPtr<IDiaEnumSourceFiles> SourceFiles;
|
|
|
|
if (S_OK !=
|
|
|
|
Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
|
|
|
|
return nullptr;
|
|
|
|
return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IPDBSourceFile>
|
|
|
|
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<IPDBEnumChildren<PDBSymbolCompiland>>
|
|
|
|
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<PDBSymbolCompiland>
|
|
|
|
DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern,
|
|
|
|
PDB_NameSearchFlags Flags) const {
|
|
|
|
auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
|
|
|
|
if (!Compilands || Compilands->getChildCount() == 0)
|
|
|
|
return nullptr;
|
|
|
|
return Compilands->getNext();
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:43:25 +08:00
|
|
|
std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
|
|
|
|
CComPtr<IDiaEnumSourceFiles> Files;
|
|
|
|
if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
|
2015-02-11 06:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
|
|
|
|
const PDBSymbolCompiland &Compiland) const {
|
|
|
|
CComPtr<IDiaEnumSourceFiles> Files;
|
|
|
|
|
|
|
|
const DIARawSymbol &RawSymbol =
|
|
|
|
static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
|
|
|
|
if (S_OK !=
|
|
|
|
Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
|
2015-02-11 06:43:25 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 05:17:52 +08:00
|
|
|
std::unique_ptr<IPDBSourceFile>
|
|
|
|
DIASession::getSourceFileById(uint32_t FileId) const {
|
|
|
|
CComPtr<IDiaSourceFile> LocatedFile;
|
|
|
|
if (S_OK != Session->findFileById(FileId, &LocatedFile))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
|
2015-02-11 05:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
|
|
|
|
CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
|
|
|
|
if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-02-11 07:46:48 +08:00
|
|
|
return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
|
2015-02-11 05:17:52 +08:00
|
|
|
}
|