Index: lldb/include/lldb/Core/Debugger.h =================================================================== --- lldb/include/lldb/Core/Debugger.h +++ lldb/include/lldb/Core/Debugger.h @@ -63,6 +63,11 @@ namespace lldb_private { class Target; } +namespace lldb_private { +namespace repro { +class DataRecorder; +} +} // namespace lldb_private namespace llvm { class raw_ostream; } @@ -129,6 +134,8 @@ lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; } + repro::DataRecorder *GetInputRecorder(); + void SetInputFileHandle(FILE *fh, bool tranfer_ownership); void SetOutputFileHandle(FILE *fh, bool tranfer_ownership); Index: lldb/include/lldb/Core/IOHandler.h =================================================================== --- lldb/include/lldb/Core/IOHandler.h +++ lldb/include/lldb/Core/IOHandler.h @@ -13,6 +13,7 @@ #include "lldb/Utility/ConstString.h" #include "lldb/Utility/Flags.h" #include "lldb/Utility/Predicate.h" +#include "lldb/Utility/Reproducer.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StringList.h" #include "lldb/lldb-defines.h" @@ -58,7 +59,8 @@ IOHandler(Debugger &debugger, IOHandler::Type type, const lldb::StreamFileSP &input_sp, const lldb::StreamFileSP &output_sp, - const lldb::StreamFileSP &error_sp, uint32_t flags); + const lldb::StreamFileSP &error_sp, uint32_t flags, + repro::DataRecorder *data_recorder); virtual ~IOHandler(); @@ -169,6 +171,7 @@ lldb::StreamFileSP m_input_sp; lldb::StreamFileSP m_output_sp; lldb::StreamFileSP m_error_sp; + repro::DataRecorder *m_data_recorder; Predicate m_popped; Flags m_flags; Type m_type; @@ -343,7 +346,8 @@ uint32_t line_number_start, // If non-zero show line numbers // starting at // 'line_number_start' - IOHandlerDelegate &delegate); + IOHandlerDelegate &delegate, + repro::DataRecorder *data_recorder); IOHandlerEditline(Debugger &debugger, IOHandler::Type type, const lldb::StreamFileSP &input_sp, @@ -355,7 +359,8 @@ uint32_t line_number_start, // If non-zero show line numbers // starting at // 'line_number_start' - IOHandlerDelegate &delegate); + IOHandlerDelegate &delegate, + repro::DataRecorder *data_recorder); IOHandlerEditline(Debugger &, IOHandler::Type, const char *, const char *, const char *, bool, bool, uint32_t, Index: lldb/include/lldb/Utility/Reproducer.h =================================================================== --- lldb/include/lldb/Utility/Reproducer.h +++ lldb/include/lldb/Utility/Reproducer.h @@ -111,6 +111,52 @@ FileCollector m_collector; }; +class DataRecorder { +public: + DataRecorder(FileSpec filename) + : m_filename(std::move(filename)), + m_os(m_filename.GetPath(), m_ec, llvm::sys::fs::F_Text) {} + + operator bool() { return !m_ec.operator bool(); } + + template void Record(const T &t, bool newline = false) { + assert(!m_ec); + m_os << t; + if (newline) + m_os << '\n'; + } + + const FileSpec &GetFilename() { return m_filename; } + +private: + FileSpec m_filename; + std::error_code m_ec; + llvm::raw_fd_ostream m_os; +}; + +struct CommandInfo { + static const char *name; + static const char *file; +}; + +class CommandProvider : public Provider { +public: + typedef CommandInfo info; + + CommandProvider(const FileSpec &directory) : Provider(directory) {} + + DataRecorder *GetNewDataRecorder(); + DataRecorder *GetCurrentDataRecorder(); + + void Keep() override; + void Discard() override; + + static char ID; + +private: + std::vector> m_data_recorders; +}; + /// The generator is responsible for the logic needed to generate a /// reproducer. For doing so it relies on providers, who serialize data that /// is necessary for reproducing a failure. Index: lldb/source/Commands/CommandObjectCommands.cpp =================================================================== --- lldb/source/Commands/CommandObjectCommands.cpp +++ lldb/source/Commands/CommandObjectCommands.cpp @@ -1040,7 +1040,7 @@ llvm::StringRef(), // Continuation prompt multiple_lines, color_prompt, 0, // Don't show line numbers - *this)); + *this, nullptr)); if (io_handler_sp) { debugger.PushIOHandler(io_handler_sp); Index: lldb/source/Commands/CommandObjectExpression.cpp =================================================================== --- lldb/source/Commands/CommandObjectExpression.cpp +++ lldb/source/Commands/CommandObjectExpression.cpp @@ -234,7 +234,7 @@ with no newlines. To evaluate a multi-line expression, \ hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ Hit return on an empty line to end the multi-line expression." - + R"( Timeouts: @@ -560,7 +560,7 @@ llvm::StringRef(), // Continuation prompt multiple_lines, color_prompt, 1, // Show line numbers starting at 1 - *this)); + *this, nullptr)); StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); if (output_sp) { Index: lldb/source/Core/Debugger.cpp =================================================================== --- lldb/source/Core/Debugger.cpp +++ lldb/source/Core/Debugger.cpp @@ -313,6 +313,45 @@ LoadPluginCallbackType Debugger::g_load_plugin_callback = nullptr; +/// Helper class for replaying commands. +class CommandLoader { +public: + CommandLoader() { + repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); + if (!loader) + return; + + FileSpec file = loader->GetFile(); + if (!file) + return; + + auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath()); + if (auto err = error_or_file.getError()) + return; + + llvm::yaml::Input yin((*error_or_file)->getBuffer()); + yin >> m_files; + + if (auto err = yin.error()) + return; + + m_initialized = true; + }; + + operator bool() { return m_initialized; } + + FILE *GetNextFile() { + if (m_index >= m_files.size()) + return nullptr; + return FileSystem::Instance().Fopen(m_files[m_index++].c_str(), "r"); + } + +private: + std::vector m_files; + unsigned m_index = 0; + bool m_initialized = false; +}; + Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef property_path, @@ -877,7 +916,25 @@ m_command_interpreter_up->SetSynchronous(!async_execution); } +repro::DataRecorder *Debugger::GetInputRecorder() { + if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) { + if (repro::DataRecorder *recorder = + g->GetOrCreate().GetCurrentDataRecorder()) { + assert(*recorder); + return recorder; + } + } + return nullptr; +} + void Debugger::SetInputFileHandle(FILE *fh, bool tranfer_ownership) { + static CommandLoader loader; + if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) + g->GetOrCreate().GetNewDataRecorder(); + + if (loader) + fh = loader.GetNextFile(); + if (m_input_file_sp) m_input_file_sp->GetFile().SetStream(fh, tranfer_ownership); else Index: lldb/source/Core/IOHandler.cpp =================================================================== --- lldb/source/Core/IOHandler.cpp +++ lldb/source/Core/IOHandler.cpp @@ -76,16 +76,19 @@ StreamFileSP(), // Adopt STDIN from top input reader StreamFileSP(), // Adopt STDOUT from top input reader StreamFileSP(), // Adopt STDERR from top input reader - 0) // Flags -{} + 0, // Flags + nullptr // Shadow file recorder + ) {} IOHandler::IOHandler(Debugger &debugger, IOHandler::Type type, const lldb::StreamFileSP &input_sp, const lldb::StreamFileSP &output_sp, - const lldb::StreamFileSP &error_sp, uint32_t flags) + const lldb::StreamFileSP &error_sp, uint32_t flags, + repro::DataRecorder *data_recorder) : m_debugger(debugger), m_input_sp(input_sp), m_output_sp(output_sp), - m_error_sp(error_sp), m_popped(false), m_flags(flags), m_type(type), - m_user_data(nullptr), m_done(false), m_active(false) { + m_error_sp(error_sp), m_data_recorder(data_recorder), m_popped(false), + m_flags(flags), m_type(type), m_user_data(nullptr), m_done(false), + m_active(false) { // If any files are not specified, then adopt them from the top input reader. if (!m_input_sp || !m_output_sp || !m_error_sp) debugger.AdoptTopIOHandlerFilesIfInvalid(m_input_sp, m_output_sp, @@ -153,7 +156,7 @@ llvm::StringRef(), // No continuation prompt false, // Multi-line false, // Don't colorize the prompt (i.e. the confirm message.) - 0, *this), + 0, *this, nullptr), m_default_response(default_response), m_user_response(default_response) { StreamString prompt_stream; prompt_stream.PutCString(prompt); @@ -264,7 +267,7 @@ const char *editline_name, // Used for saving history files llvm::StringRef prompt, llvm::StringRef continuation_prompt, bool multi_line, bool color_prompts, uint32_t line_number_start, - IOHandlerDelegate &delegate) + IOHandlerDelegate &delegate, repro::DataRecorder *data_recorder) : IOHandlerEditline(debugger, type, StreamFileSP(), // Inherit input from top input reader StreamFileSP(), // Inherit output from top input reader @@ -272,7 +275,7 @@ 0, // Flags editline_name, // Used for saving history files prompt, continuation_prompt, multi_line, color_prompts, - line_number_start, delegate) {} + line_number_start, delegate, data_recorder) {} IOHandlerEditline::IOHandlerEditline( Debugger &debugger, IOHandler::Type type, @@ -281,8 +284,9 @@ const char *editline_name, // Used for saving history files llvm::StringRef prompt, llvm::StringRef continuation_prompt, bool multi_line, bool color_prompts, uint32_t line_number_start, - IOHandlerDelegate &delegate) - : IOHandler(debugger, type, input_sp, output_sp, error_sp, flags), + IOHandlerDelegate &delegate, repro::DataRecorder *data_recorder) + : IOHandler(debugger, type, input_sp, output_sp, error_sp, flags, + data_recorder), #ifndef LLDB_DISABLE_LIBEDIT m_editline_up(), #endif @@ -338,7 +342,10 @@ bool IOHandlerEditline::GetLine(std::string &line, bool &interrupted) { #ifndef LLDB_DISABLE_LIBEDIT if (m_editline_up) { - return m_editline_up->GetLine(line, interrupted); + bool b = m_editline_up->GetLine(line, interrupted); + if (m_data_recorder) + m_data_recorder->Record(line, true); + return b; } else { #endif line.clear(); @@ -394,6 +401,8 @@ } } m_editing = false; + if (m_data_recorder && got_line) + m_data_recorder->Record(line, true); // We might have gotten a newline on a line by itself make sure to return // true in this case. return got_line; Index: lldb/source/Expression/REPL.cpp =================================================================== --- lldb/source/Expression/REPL.cpp +++ lldb/source/Expression/REPL.cpp @@ -75,13 +75,13 @@ Debugger &debugger = m_target.GetDebugger(); m_io_handler_sp = std::make_shared( debugger, IOHandler::Type::REPL, - "lldb-repl", // Name of input reader for history - llvm::StringRef("> "), // prompt - llvm::StringRef(". "), // Continuation prompt - true, // Multi-line - true, // The REPL prompt is always colored - 1, // Line number - *this); + "lldb-repl", // Name of input reader for history + llvm::StringRef("> "), // prompt + llvm::StringRef(". "), // Continuation prompt + true, // Multi-line + true, // The REPL prompt is always colored + 1, // Line number + *this, nullptr); // Don't exit if CTRL+C is pressed static_cast(m_io_handler_sp.get()) Index: lldb/source/Interpreter/CommandInterpreter.cpp =================================================================== --- lldb/source/Interpreter/CommandInterpreter.cpp +++ lldb/source/Interpreter/CommandInterpreter.cpp @@ -2483,7 +2483,7 @@ // or written debugger.GetPrompt(), llvm::StringRef(), false, // Not multi-line - debugger.GetUseColor(), 0, *this)); + debugger.GetUseColor(), 0, *this, nullptr)); const bool old_async_execution = debugger.GetAsyncExecution(); // Set synchronous execution if we are not stopping on continue @@ -2905,8 +2905,9 @@ llvm::StringRef(), // Continuation prompt true, // Get multiple lines debugger.GetUseColor(), - 0, // Don't show line numbers - delegate)); // IOHandlerDelegate + 0, // Don't show line numbers + delegate, // IOHandlerDelegate + nullptr)); // FileShadowCollector if (io_handler_sp) { io_handler_sp->SetUserData(baton); @@ -2928,8 +2929,9 @@ llvm::StringRef(), // Continuation prompt true, // Get multiple lines debugger.GetUseColor(), - 0, // Don't show line numbers - delegate)); // IOHandlerDelegate + 0, // Don't show line numbers + delegate, // IOHandlerDelegate + nullptr)); // FileShadowCollector if (io_handler_sp) { io_handler_sp->SetUserData(baton); @@ -2980,8 +2982,9 @@ llvm::StringRef(), // Continuation prompt false, // Don't enable multiple line input, just single line commands m_debugger.GetUseColor(), - 0, // Don't show line numbers - *this); + 0, // Don't show line numbers + *this, // IOHandlerDelegate + GetDebugger().GetInputRecorder()); } return m_command_io_handler_sp; } Index: lldb/source/Utility/Reproducer.cpp =================================================================== --- lldb/source/Utility/Reproducer.cpp +++ lldb/source/Utility/Reproducer.cpp @@ -220,8 +220,45 @@ return (it != m_files.end()) && (*it == file); } +DataRecorder *CommandProvider::GetNewDataRecorder() { + std::size_t i = m_data_recorders.size() + 1; + std::string filename = (llvm::Twine(info::name) + llvm::Twine("-") + + llvm::Twine(i) + llvm::Twine(".txt")) + .str(); + m_data_recorders.push_back(llvm::make_unique( + GetRoot().CopyByAppendingPathComponent(filename))); + return m_data_recorders.back().get(); +} + +DataRecorder *CommandProvider::GetCurrentDataRecorder() { + if (m_data_recorders.empty()) + return nullptr; + return m_data_recorders.back().get(); +} + +void CommandProvider::Keep() { + std::vector files; + for (auto &recorder : m_data_recorders) + files.push_back(recorder->GetFilename().GetPath()); + + FileSpec file = GetRoot().CopyByAppendingPathComponent(info::file); + std::error_code ec; + llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::F_Text); + if (ec) + return; + yaml::Output yout(os); + yout << files; + + m_data_recorders.clear(); +} + +void CommandProvider::Discard() { m_data_recorders.clear(); } + void ProviderBase::anchor() {} char ProviderBase::ID = 0; char FileProvider::ID = 0; +char CommandProvider::ID = 0; const char *FileInfo::name = "files"; const char *FileInfo::file = "files.yaml"; +const char *CommandInfo::name = "command-interpreter"; +const char *CommandInfo::file = "command-interpreter.yaml";