Skip to content

Commit

Permalink
Revert "Implement some NativeSession functions" along with some
Browse files Browse the repository at this point in the history
followup fixes.

This reverts commits
a6d8a05
4927ae0
1e1f5eb
  • Loading branch information
amykhuang committed Apr 21, 2020
1 parent 5f6aa96 commit 507d80f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 250 deletions.
9 changes: 0 additions & 9 deletions llvm/include/llvm/DebugInfo/PDB/Native/NativeSession.h
Expand Up @@ -26,23 +26,15 @@ class PDBFile;
class NativeExeSymbol;

class NativeSession : public IPDBSession {
struct PdbSearchOptions {
StringRef ExePath;
// FIXME: Add other PDB search options (_NT_SYMBOL_PATH, symsrv)
};

public:
NativeSession(std::unique_ptr<PDBFile> PdbFile,
std::unique_ptr<BumpPtrAllocator> Allocator);
~NativeSession() override;

static Error createFromPdb(std::unique_ptr<MemoryBuffer> MB,
std::unique_ptr<IPDBSession> &Session);
static Error createFromPdbPath(StringRef PdbPath,
std::unique_ptr<IPDBSession> &Session);
static Error createFromExe(StringRef Path,
std::unique_ptr<IPDBSession> &Session);
static Expected<std::string> searchForPdb(const PdbSearchOptions &Opts);

uint64_t getLoadAddress() const override;
bool setLoadAddress(uint64_t Address) override;
Expand Down Expand Up @@ -117,7 +109,6 @@ class NativeSession : public IPDBSession {

SymbolCache Cache;
SymIndexId ExeSymbol = 0;
uint64_t LoadAddress = 0;
};
} // namespace pdb
} // namespace llvm
Expand Down
137 changes: 7 additions & 130 deletions llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
Expand Up @@ -12,7 +12,6 @@
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
#include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
#include "llvm/DebugInfo/PDB/Native/NativeEnumInjectedSources.h"
#include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
Expand All @@ -26,14 +25,11 @@
#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -79,115 +75,14 @@ Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
return Error::success();
}

static Expected<std::unique_ptr<PDBFile>>
loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1,
/*RequiresNullTerminator=*/false);
if (!ErrorOrBuffer)
return make_error<RawError>(ErrorOrBuffer.getError());
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);

PdbPath = Buffer->getBufferIdentifier();
file_magic Magic;
auto EC = identify_magic(PdbPath, Magic);
if (EC || Magic != file_magic::pdb)
return make_error<RawError>(EC);

auto Stream = std::make_unique<MemoryBufferByteStream>(std::move(Buffer),
llvm::support::little);

auto File = std::make_unique<PDBFile>(PdbPath, std::move(Stream), *Allocator);
if (auto EC = File->parseFileHeaders())
return std::move(EC);

if (auto EC = File->parseStreamData())
return std::move(EC);

return std::move(File);
}

Error NativeSession::createFromPdbPath(StringRef PdbPath,
std::unique_ptr<IPDBSession> &Session) {
auto Allocator = std::make_unique<BumpPtrAllocator>();
auto PdbFile = loadPdbFile(PdbPath, Allocator);
if (!PdbFile)
return PdbFile.takeError();

Session = std::make_unique<NativeSession>(std::move(PdbFile.get()),
std::move(Allocator));
return Error::success();
}

static Expected<std::string> getPdbPathFromExe(StringRef ExePath) {
Expected<object::OwningBinary<object::Binary>> BinaryFile =
object::createBinary(ExePath);
if (!BinaryFile)
return BinaryFile.takeError();

const object::COFFObjectFile *ObjFile =
dyn_cast<object::COFFObjectFile>(BinaryFile->getBinary());
if (!ObjFile)
return make_error<RawError>(raw_error_code::invalid_format);

StringRef PdbPath;
const llvm::codeview::DebugInfo *PdbInfo = nullptr;
if (auto EC = ObjFile->getDebugPDBInfo(PdbInfo, PdbPath))
return make_error<RawError>(EC);

return std::string(PdbPath);
}

Error NativeSession::createFromExe(StringRef ExePath,
Error NativeSession::createFromExe(StringRef Path,
std::unique_ptr<IPDBSession> &Session) {
Expected<std::string> PdbPath = getPdbPathFromExe(ExePath);
if (!PdbPath)
return PdbPath.takeError();

file_magic Magic;
auto EC = identify_magic(PdbPath.get(), Magic);
if (EC || Magic != file_magic::pdb)
return make_error<RawError>(EC);

auto Allocator = std::make_unique<BumpPtrAllocator>();
auto File = loadPdbFile(PdbPath.get(), Allocator);
if (!File)
return File.takeError();

Session = std::make_unique<NativeSession>(std::move(File.get()),
std::move(Allocator));

return Error::success();
return make_error<RawError>(raw_error_code::feature_unsupported);
}

Expected<std::string>
NativeSession::searchForPdb(const PdbSearchOptions &Opts) {
Expected<std::string> PathOrErr = getPdbPathFromExe(Opts.ExePath);
if (!PathOrErr)
return PathOrErr.takeError();
StringRef PdbName = sys::path::filename(PathOrErr.get());
uint64_t NativeSession::getLoadAddress() const { return 0; }

// Check if pdb exists in the executable directory.
SmallString<128> PdbPath = StringRef(Opts.ExePath);
sys::path::remove_filename(PdbPath);
sys::path::append(PdbPath, PdbName);

auto Allocator = std::make_unique<BumpPtrAllocator>();

if (auto File = loadPdbFile(PdbPath, Allocator))
return std::string(PdbPath);
else
return File.takeError();

return make_error<RawError>("PDB not found");
}

uint64_t NativeSession::getLoadAddress() const { return LoadAddress; }

bool NativeSession::setLoadAddress(uint64_t Address) {
LoadAddress = Address;
return true;
}
bool NativeSession::setLoadAddress(uint64_t Address) { return false; }

std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
Expand All @@ -200,30 +95,12 @@ NativeSession::getSymbolById(SymIndexId SymbolId) const {

bool NativeSession::addressForVA(uint64_t VA, uint32_t &Section,
uint32_t &Offset) const {
uint32_t RVA = VA - getLoadAddress();
return addressForRVA(RVA, Section, Offset);
return false;
}

bool NativeSession::addressForRVA(uint32_t RVA, uint32_t &Section,
bool NativeSession::addressForRVA(uint32_t VA, uint32_t &Section,
uint32_t &Offset) const {
auto Dbi = Pdb->getPDBDbiStream();
if (!Dbi)
return false;

Section = 0;
Offset = 0;

if ((int32_t)RVA < 0)
return true;

Offset = RVA;
for (; Section < Dbi->getSectionHeaders().size(); ++Section) {
auto &Sec = Dbi->getSectionHeaders()[Section];
if (RVA < Sec.VirtualAddress)
return true;
Offset = RVA - Sec.VirtualAddress;
}
return true;
return false;
}

std::unique_ptr<PDBSymbol>
Expand Down
24 changes: 11 additions & 13 deletions llvm/lib/DebugInfo/PDB/PDB.cpp
Expand Up @@ -23,8 +23,15 @@ using namespace llvm::pdb;
Error llvm::pdb::loadDataForPDB(PDB_ReaderType Type, StringRef Path,
std::unique_ptr<IPDBSession> &Session) {
// Create the correct concrete instance type based on the value of Type.
if (Type == PDB_ReaderType::Native)
return NativeSession::createFromPdbPath(Path, Session);
if (Type == PDB_ReaderType::Native) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
/*RequiresNullTerminator=*/false);
if (!ErrorOrBuffer)
return errorCodeToError(ErrorOrBuffer.getError());

return NativeSession::createFromPdb(std::move(*ErrorOrBuffer), Session);
}

#if LLVM_ENABLE_DIA_SDK
return DIASession::createFromPdb(Path, Session);
Expand All @@ -36,17 +43,8 @@ Error llvm::pdb::loadDataForPDB(PDB_ReaderType Type, StringRef Path,
Error llvm::pdb::loadDataForEXE(PDB_ReaderType Type, StringRef Path,
std::unique_ptr<IPDBSession> &Session) {
// Create the correct concrete instance type based on the value of Type.
if (Type == PDB_ReaderType::Native) {
if (auto Err = NativeSession::createFromExe(Path, Session)) {
consumeError(std::move(Err));

Expected<std::string> PdbPath = NativeSession::searchForPdb({Path});
if (!PdbPath)
return PdbPath.takeError();
return NativeSession::createFromPdbPath(PdbPath.get(), Session);
}
return Error::success();
}
if (Type == PDB_ReaderType::Native)
return NativeSession::createFromExe(Path, Session);

#if LLVM_ENABLE_DIA_SDK
return DIASession::createFromExe(Path, Session);
Expand Down
1 change: 0 additions & 1 deletion llvm/unittests/DebugInfo/PDB/CMakeLists.txt
Expand Up @@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS

add_llvm_unittest_with_input_files(DebugInfoPDBTests
HashTableTest.cpp
NativeSessionTest.cpp
NativeSymbolReuseTest.cpp
StringTableBuilderTest.cpp
PDBApiTest.cpp
Expand Down
4 changes: 0 additions & 4 deletions llvm/unittests/DebugInfo/PDB/Inputs/SimpleTest.cpp

This file was deleted.

Binary file not shown.
Binary file not shown.
93 changes: 0 additions & 93 deletions llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp

This file was deleted.

0 comments on commit 507d80f

Please sign in to comment.