Skip to content

Commit

Permalink
[Reproducer] Move the command loader into the reproducer (NFC)
Browse files Browse the repository at this point in the history
This just moves the CommandLoader utility into the reproducer namespace
and makes it accessible outside the API layer. This is setting things up
for a bigger change.

llvm-svn: 371689
  • Loading branch information
JDevlieghere committed Sep 11, 2019
1 parent d9aec34 commit 4a491ec
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
13 changes: 13 additions & 0 deletions lldb/include/lldb/Utility/Reproducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,19 @@ class Reproducer {
mutable std::mutex m_mutex;
};

/// Helper class for replaying commands through the reproducer.
class CommandLoader {
public:
CommandLoader(std::vector<std::string> files) : m_files(files) {}

static std::unique_ptr<CommandLoader> Create(Loader *loader);
llvm::Optional<std::string> GetNextFile();

private:
std::vector<std::string> m_files;
unsigned m_index = 0;
};

} // namespace repro
} // namespace lldb_private

Expand Down
54 changes: 6 additions & 48 deletions lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,51 +57,6 @@
using namespace lldb;
using namespace lldb_private;

/// Helper class for replaying commands through the reproducer.
class CommandLoader {
public:
CommandLoader(std::vector<std::string> files) : m_files(files) {}

static std::unique_ptr<CommandLoader> Create() {
repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
if (!loader)
return {};

FileSpec file = loader->GetFile<repro::CommandProvider::Info>();
if (!file)
return {};

auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
if (auto err = error_or_file.getError())
return {};

std::vector<std::string> files;
llvm::yaml::Input yin((*error_or_file)->getBuffer());
yin >> files;

if (auto err = yin.error())
return {};

for (auto &file : files) {
FileSpec absolute_path =
loader->GetRoot().CopyByAppendingPathComponent(file);
file = absolute_path.GetPath();
}

return std::make_unique<CommandLoader>(std::move(files));
}

FILE *GetNextFile() {
if (m_index >= m_files.size())
return nullptr;
return FileSystem::Instance().Fopen(m_files[m_index++].c_str(), "r");
}

private:
std::vector<std::string> m_files;
unsigned m_index = 0;
};

static llvm::sys::DynamicLibrary LoadPlugin(const lldb::DebuggerSP &debugger_sp,
const FileSpec &spec,
Status &error) {
Expand Down Expand Up @@ -344,9 +299,12 @@ void SBDebugger::SetInputFileHandle(FILE *fh, bool transfer_ownership) {
if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator())
recorder = g->GetOrCreate<repro::CommandProvider>().GetNewDataRecorder();

static std::unique_ptr<CommandLoader> loader = CommandLoader::Create();
if (loader)
fh = loader->GetNextFile();
static std::unique_ptr<repro::CommandLoader> loader =
repro::CommandLoader::Create(repro::Reproducer::Instance().GetLoader());
if (loader) {
llvm::Optional<std::string> file = loader->GetNextFile();
fh = file ? FileSystem::Instance().Fopen(file->c_str(), "r") : nullptr;
}

m_opaque_sp->SetInputFileHandle(fh, transfer_ownership, recorder);
}
Expand Down
34 changes: 34 additions & 0 deletions lldb/source/Utility/Reproducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,40 @@ llvm::raw_ostream *ProcessGDBRemoteProvider::GetHistoryStream() {
return m_stream_up.get();
}

std::unique_ptr<CommandLoader> CommandLoader::Create(Loader *loader) {
if (!loader)
return {};

FileSpec file = loader->GetFile<repro::CommandProvider::Info>();
if (!file)
return {};

auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
if (auto err = error_or_file.getError())
return {};

std::vector<std::string> files;
llvm::yaml::Input yin((*error_or_file)->getBuffer());
yin >> files;

if (auto err = yin.error())
return {};

for (auto &file : files) {
FileSpec absolute_path =
loader->GetRoot().CopyByAppendingPathComponent(file);
file = absolute_path.GetPath();
}

return std::make_unique<CommandLoader>(std::move(files));
}

llvm::Optional<std::string> CommandLoader::GetNextFile() {
if (m_index >= m_files.size())
return {};
return m_files[m_index++];
}

void ProviderBase::anchor() {}
char CommandProvider::ID = 0;
char FileProvider::ID = 0;
Expand Down

0 comments on commit 4a491ec

Please sign in to comment.