180814287SRaphael Isemann //===-- CommandObjectReproducer.cpp ---------------------------------------===// 29e046f02SJonas Devlieghere // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 69e046f02SJonas Devlieghere // 79e046f02SJonas Devlieghere //===----------------------------------------------------------------------===// 89e046f02SJonas Devlieghere 99e046f02SJonas Devlieghere #include "CommandObjectReproducer.h" 109e046f02SJonas Devlieghere 112451cbf0SJonas Devlieghere #include "lldb/Host/HostInfo.h" 1297fc8eb4SJonas Devlieghere #include "lldb/Host/OptionParser.h" 13df14b942SJonas Devlieghere #include "lldb/Interpreter/CommandInterpreter.h" 149e046f02SJonas Devlieghere #include "lldb/Interpreter/CommandReturnObject.h" 159e046f02SJonas Devlieghere #include "lldb/Interpreter/OptionArgParser.h" 162451cbf0SJonas Devlieghere #include "lldb/Utility/GDBRemote.h" 172451cbf0SJonas Devlieghere #include "lldb/Utility/ProcessInfo.h" 182451cbf0SJonas Devlieghere #include "lldb/Utility/Reproducer.h" 199e046f02SJonas Devlieghere 20c8dfe907SJonas Devlieghere #include <csignal> 21c8dfe907SJonas Devlieghere 229e046f02SJonas Devlieghere using namespace lldb; 2397fc8eb4SJonas Devlieghere using namespace llvm; 249e046f02SJonas Devlieghere using namespace lldb_private; 2597fc8eb4SJonas Devlieghere using namespace lldb_private::repro; 2697fc8eb4SJonas Devlieghere 2797fc8eb4SJonas Devlieghere enum ReproducerProvider { 2897fc8eb4SJonas Devlieghere eReproducerProviderCommands, 2997fc8eb4SJonas Devlieghere eReproducerProviderFiles, 30*a842950bSJonas Devlieghere eReproducerProviderSymbolFiles, 3197fc8eb4SJonas Devlieghere eReproducerProviderGDB, 322451cbf0SJonas Devlieghere eReproducerProviderProcessInfo, 3397fc8eb4SJonas Devlieghere eReproducerProviderVersion, 34f4f12012SJonas Devlieghere eReproducerProviderWorkingDirectory, 3573af341bSJonas Devlieghere eReproducerProviderHomeDirectory, 3697fc8eb4SJonas Devlieghere eReproducerProviderNone 3797fc8eb4SJonas Devlieghere }; 3897fc8eb4SJonas Devlieghere 3997fc8eb4SJonas Devlieghere static constexpr OptionEnumValueElement g_reproducer_provider_type[] = { 4097fc8eb4SJonas Devlieghere { 4197fc8eb4SJonas Devlieghere eReproducerProviderCommands, 4297fc8eb4SJonas Devlieghere "commands", 4397fc8eb4SJonas Devlieghere "Command Interpreter Commands", 4497fc8eb4SJonas Devlieghere }, 4597fc8eb4SJonas Devlieghere { 4697fc8eb4SJonas Devlieghere eReproducerProviderFiles, 4797fc8eb4SJonas Devlieghere "files", 4897fc8eb4SJonas Devlieghere "Files", 4997fc8eb4SJonas Devlieghere }, 5097fc8eb4SJonas Devlieghere { 51*a842950bSJonas Devlieghere eReproducerProviderSymbolFiles, 52*a842950bSJonas Devlieghere "symbol-files", 53*a842950bSJonas Devlieghere "Symbol Files", 54*a842950bSJonas Devlieghere }, 55*a842950bSJonas Devlieghere { 5697fc8eb4SJonas Devlieghere eReproducerProviderGDB, 5797fc8eb4SJonas Devlieghere "gdb", 5897fc8eb4SJonas Devlieghere "GDB Remote Packets", 5997fc8eb4SJonas Devlieghere }, 6097fc8eb4SJonas Devlieghere { 612451cbf0SJonas Devlieghere eReproducerProviderProcessInfo, 622451cbf0SJonas Devlieghere "processes", 632451cbf0SJonas Devlieghere "Process Info", 642451cbf0SJonas Devlieghere }, 652451cbf0SJonas Devlieghere { 6697fc8eb4SJonas Devlieghere eReproducerProviderVersion, 6797fc8eb4SJonas Devlieghere "version", 6897fc8eb4SJonas Devlieghere "Version", 6997fc8eb4SJonas Devlieghere }, 7097fc8eb4SJonas Devlieghere { 71f4f12012SJonas Devlieghere eReproducerProviderWorkingDirectory, 72f4f12012SJonas Devlieghere "cwd", 73f4f12012SJonas Devlieghere "Working Directory", 74f4f12012SJonas Devlieghere }, 75f4f12012SJonas Devlieghere { 7673af341bSJonas Devlieghere eReproducerProviderHomeDirectory, 7773af341bSJonas Devlieghere "home", 7873af341bSJonas Devlieghere "Home Directory", 7973af341bSJonas Devlieghere }, 8073af341bSJonas Devlieghere { 8197fc8eb4SJonas Devlieghere eReproducerProviderNone, 8297fc8eb4SJonas Devlieghere "none", 8397fc8eb4SJonas Devlieghere "None", 8497fc8eb4SJonas Devlieghere }, 8597fc8eb4SJonas Devlieghere }; 8697fc8eb4SJonas Devlieghere 8797fc8eb4SJonas Devlieghere static constexpr OptionEnumValues ReproducerProviderType() { 8897fc8eb4SJonas Devlieghere return OptionEnumValues(g_reproducer_provider_type); 8997fc8eb4SJonas Devlieghere } 9097fc8eb4SJonas Devlieghere 9136eea5c3SJonas Devlieghere #define LLDB_OPTIONS_reproducer_dump 9297fc8eb4SJonas Devlieghere #include "CommandOptions.inc" 939e046f02SJonas Devlieghere 94c8dfe907SJonas Devlieghere enum ReproducerCrashSignal { 95c8dfe907SJonas Devlieghere eReproducerCrashSigill, 96c8dfe907SJonas Devlieghere eReproducerCrashSigsegv, 97c8dfe907SJonas Devlieghere }; 98c8dfe907SJonas Devlieghere 99c8dfe907SJonas Devlieghere static constexpr OptionEnumValueElement g_reproducer_signaltype[] = { 100c8dfe907SJonas Devlieghere { 101c8dfe907SJonas Devlieghere eReproducerCrashSigill, 102c8dfe907SJonas Devlieghere "SIGILL", 103c8dfe907SJonas Devlieghere "Illegal instruction", 104c8dfe907SJonas Devlieghere }, 105c8dfe907SJonas Devlieghere { 106c8dfe907SJonas Devlieghere eReproducerCrashSigsegv, 107c8dfe907SJonas Devlieghere "SIGSEGV", 108c8dfe907SJonas Devlieghere "Segmentation fault", 109c8dfe907SJonas Devlieghere }, 110c8dfe907SJonas Devlieghere }; 111c8dfe907SJonas Devlieghere 112c8dfe907SJonas Devlieghere static constexpr OptionEnumValues ReproducerSignalType() { 113c8dfe907SJonas Devlieghere return OptionEnumValues(g_reproducer_signaltype); 114c8dfe907SJonas Devlieghere } 115c8dfe907SJonas Devlieghere 116c8dfe907SJonas Devlieghere #define LLDB_OPTIONS_reproducer_xcrash 117c8dfe907SJonas Devlieghere #include "CommandOptions.inc" 118c8dfe907SJonas Devlieghere 1192451cbf0SJonas Devlieghere template <typename T> 1202451cbf0SJonas Devlieghere llvm::Expected<T> static ReadFromYAML(StringRef filename) { 1212451cbf0SJonas Devlieghere auto error_or_file = MemoryBuffer::getFile(filename); 1222451cbf0SJonas Devlieghere if (auto err = error_or_file.getError()) { 1232451cbf0SJonas Devlieghere return errorCodeToError(err); 1242451cbf0SJonas Devlieghere } 1252451cbf0SJonas Devlieghere 1262451cbf0SJonas Devlieghere T t; 1272451cbf0SJonas Devlieghere yaml::Input yin((*error_or_file)->getBuffer()); 1282451cbf0SJonas Devlieghere yin >> t; 1292451cbf0SJonas Devlieghere 1302451cbf0SJonas Devlieghere if (auto err = yin.error()) { 1312451cbf0SJonas Devlieghere return errorCodeToError(err); 1322451cbf0SJonas Devlieghere } 1332451cbf0SJonas Devlieghere 1342451cbf0SJonas Devlieghere return t; 1352451cbf0SJonas Devlieghere } 1362451cbf0SJonas Devlieghere 1379e046f02SJonas Devlieghere class CommandObjectReproducerGenerate : public CommandObjectParsed { 1389e046f02SJonas Devlieghere public: 1399e046f02SJonas Devlieghere CommandObjectReproducerGenerate(CommandInterpreter &interpreter) 140973d66eeSJonas Devlieghere : CommandObjectParsed( 141973d66eeSJonas Devlieghere interpreter, "reproducer generate", 142973d66eeSJonas Devlieghere "Generate reproducer on disk. When the debugger is in capture " 143973d66eeSJonas Devlieghere "mode, this command will output the reproducer to a directory on " 1440cf86da1SJonas Devlieghere "disk and quit. In replay mode this command in a no-op.", 145973d66eeSJonas Devlieghere nullptr) {} 1469e046f02SJonas Devlieghere 1479e046f02SJonas Devlieghere ~CommandObjectReproducerGenerate() override = default; 1489e046f02SJonas Devlieghere 1499e046f02SJonas Devlieghere protected: 1509e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 1519e046f02SJonas Devlieghere if (!command.empty()) { 1529e046f02SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 1539e046f02SJonas Devlieghere m_cmd_name.c_str()); 1549e046f02SJonas Devlieghere return false; 1559e046f02SJonas Devlieghere } 1569e046f02SJonas Devlieghere 15797fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 1589e046f02SJonas Devlieghere if (auto generator = r.GetGenerator()) { 1599e046f02SJonas Devlieghere generator->Keep(); 160865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 161bb090bb1SJonas Devlieghere // Make this operation a NO-OP in replay mode. 1622dca6538SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 1632dca6538SJonas Devlieghere return result.Succeeded(); 1649e046f02SJonas Devlieghere } else { 1659e046f02SJonas Devlieghere result.AppendErrorWithFormat("Unable to get the reproducer generator"); 1662dca6538SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 1679e046f02SJonas Devlieghere return false; 1689e046f02SJonas Devlieghere } 1699e046f02SJonas Devlieghere 1709e046f02SJonas Devlieghere result.GetOutputStream() 1719e046f02SJonas Devlieghere << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; 1721c5250abSJonas Devlieghere result.GetOutputStream() 1731c5250abSJonas Devlieghere << "Please have a look at the directory to assess if you're willing to " 1741c5250abSJonas Devlieghere "share the contained information.\n"; 1759e046f02SJonas Devlieghere 1760cf86da1SJonas Devlieghere m_interpreter.BroadcastEvent( 1770cf86da1SJonas Devlieghere CommandInterpreter::eBroadcastBitQuitCommandReceived); 1780cf86da1SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 1799e046f02SJonas Devlieghere return result.Succeeded(); 1809e046f02SJonas Devlieghere } 1819e046f02SJonas Devlieghere }; 1829e046f02SJonas Devlieghere 183c8dfe907SJonas Devlieghere class CommandObjectReproducerXCrash : public CommandObjectParsed { 184c8dfe907SJonas Devlieghere public: 185c8dfe907SJonas Devlieghere CommandObjectReproducerXCrash(CommandInterpreter &interpreter) 186c8dfe907SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer xcrash", 187c8dfe907SJonas Devlieghere "Intentionally force the debugger to crash in " 188c8dfe907SJonas Devlieghere "order to trigger and test reproducer generation.", 189c8dfe907SJonas Devlieghere nullptr) {} 190c8dfe907SJonas Devlieghere 191c8dfe907SJonas Devlieghere ~CommandObjectReproducerXCrash() override = default; 192c8dfe907SJonas Devlieghere 193c8dfe907SJonas Devlieghere Options *GetOptions() override { return &m_options; } 194c8dfe907SJonas Devlieghere 195c8dfe907SJonas Devlieghere class CommandOptions : public Options { 196c8dfe907SJonas Devlieghere public: 197c8dfe907SJonas Devlieghere CommandOptions() : Options() {} 198c8dfe907SJonas Devlieghere 199c8dfe907SJonas Devlieghere ~CommandOptions() override = default; 200c8dfe907SJonas Devlieghere 201c8dfe907SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 202c8dfe907SJonas Devlieghere ExecutionContext *execution_context) override { 203c8dfe907SJonas Devlieghere Status error; 204c8dfe907SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 205c8dfe907SJonas Devlieghere 206c8dfe907SJonas Devlieghere switch (short_option) { 207c8dfe907SJonas Devlieghere case 's': 208c8dfe907SJonas Devlieghere signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum( 209c8dfe907SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 210c8dfe907SJonas Devlieghere if (!error.Success()) 211c8dfe907SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for signal '%s'", 212c8dfe907SJonas Devlieghere option_arg.str().c_str()); 213c8dfe907SJonas Devlieghere break; 214c8dfe907SJonas Devlieghere default: 215c8dfe907SJonas Devlieghere llvm_unreachable("Unimplemented option"); 216c8dfe907SJonas Devlieghere } 217c8dfe907SJonas Devlieghere 218c8dfe907SJonas Devlieghere return error; 219c8dfe907SJonas Devlieghere } 220c8dfe907SJonas Devlieghere 221c8dfe907SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 222c8dfe907SJonas Devlieghere signal = eReproducerCrashSigsegv; 223c8dfe907SJonas Devlieghere } 224c8dfe907SJonas Devlieghere 225c8dfe907SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 226c8dfe907SJonas Devlieghere return makeArrayRef(g_reproducer_xcrash_options); 227c8dfe907SJonas Devlieghere } 228c8dfe907SJonas Devlieghere 229c8dfe907SJonas Devlieghere ReproducerCrashSignal signal = eReproducerCrashSigsegv; 230c8dfe907SJonas Devlieghere }; 231c8dfe907SJonas Devlieghere 232c8dfe907SJonas Devlieghere protected: 233c8dfe907SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 234c8dfe907SJonas Devlieghere if (!command.empty()) { 235c8dfe907SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 236c8dfe907SJonas Devlieghere m_cmd_name.c_str()); 237c8dfe907SJonas Devlieghere return false; 238c8dfe907SJonas Devlieghere } 239c8dfe907SJonas Devlieghere 240c8dfe907SJonas Devlieghere auto &r = Reproducer::Instance(); 241bb090bb1SJonas Devlieghere 242bb090bb1SJonas Devlieghere if (!r.IsCapturing() && !r.IsReplaying()) { 243c8dfe907SJonas Devlieghere result.SetError( 244c8dfe907SJonas Devlieghere "forcing a crash is only supported when capturing a reproducer."); 245c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 246c8dfe907SJonas Devlieghere return false; 247c8dfe907SJonas Devlieghere } 248c8dfe907SJonas Devlieghere 249c8dfe907SJonas Devlieghere switch (m_options.signal) { 250c8dfe907SJonas Devlieghere case eReproducerCrashSigill: 251c8dfe907SJonas Devlieghere std::raise(SIGILL); 252c8dfe907SJonas Devlieghere break; 253c8dfe907SJonas Devlieghere case eReproducerCrashSigsegv: 254c8dfe907SJonas Devlieghere std::raise(SIGSEGV); 255c8dfe907SJonas Devlieghere break; 256c8dfe907SJonas Devlieghere } 257c8dfe907SJonas Devlieghere 258c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 259c8dfe907SJonas Devlieghere return result.Succeeded(); 260c8dfe907SJonas Devlieghere } 261c8dfe907SJonas Devlieghere 262c8dfe907SJonas Devlieghere private: 263c8dfe907SJonas Devlieghere CommandOptions m_options; 264c8dfe907SJonas Devlieghere }; 265c8dfe907SJonas Devlieghere 26615eacd74SJonas Devlieghere class CommandObjectReproducerStatus : public CommandObjectParsed { 2679e046f02SJonas Devlieghere public: 26815eacd74SJonas Devlieghere CommandObjectReproducerStatus(CommandInterpreter &interpreter) 269973d66eeSJonas Devlieghere : CommandObjectParsed( 270973d66eeSJonas Devlieghere interpreter, "reproducer status", 271c8dfe907SJonas Devlieghere "Show the current reproducer status. In capture mode the " 272c8dfe907SJonas Devlieghere "debugger " 273973d66eeSJonas Devlieghere "is collecting all the information it needs to create a " 274973d66eeSJonas Devlieghere "reproducer. In replay mode the reproducer is replaying a " 275973d66eeSJonas Devlieghere "reproducer. When the reproducers are off, no data is collected " 276973d66eeSJonas Devlieghere "and no reproducer can be generated.", 277973d66eeSJonas Devlieghere nullptr) {} 2789e046f02SJonas Devlieghere 27915eacd74SJonas Devlieghere ~CommandObjectReproducerStatus() override = default; 2809e046f02SJonas Devlieghere 2819e046f02SJonas Devlieghere protected: 2829e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 28315eacd74SJonas Devlieghere if (!command.empty()) { 28415eacd74SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 2859e046f02SJonas Devlieghere m_cmd_name.c_str()); 2869e046f02SJonas Devlieghere return false; 2879e046f02SJonas Devlieghere } 2889e046f02SJonas Devlieghere 28997fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 290865cd093SJonas Devlieghere if (r.IsCapturing()) { 29115eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in capture mode.\n"; 292865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 29315eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in replay mode.\n"; 29415eacd74SJonas Devlieghere } else { 29515eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is off.\n"; 2969e046f02SJonas Devlieghere } 2979e046f02SJonas Devlieghere 298982a77b6SJonas Devlieghere if (r.IsCapturing() || r.IsReplaying()) { 299982a77b6SJonas Devlieghere result.GetOutputStream() 300982a77b6SJonas Devlieghere << "Path: " << r.GetReproducerPath().GetPath() << '\n'; 301982a77b6SJonas Devlieghere } 302982a77b6SJonas Devlieghere 303982a77b6SJonas Devlieghere // Auto generate is hidden unless enabled because this is mostly for 304982a77b6SJonas Devlieghere // development and testing. 305982a77b6SJonas Devlieghere if (Generator *g = r.GetGenerator()) { 306982a77b6SJonas Devlieghere if (g->IsAutoGenerate()) 307982a77b6SJonas Devlieghere result.GetOutputStream() << "Auto generate: on\n"; 308982a77b6SJonas Devlieghere } 309982a77b6SJonas Devlieghere 31015eacd74SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 3119e046f02SJonas Devlieghere return result.Succeeded(); 3129e046f02SJonas Devlieghere } 3139e046f02SJonas Devlieghere }; 3149e046f02SJonas Devlieghere 31597fc8eb4SJonas Devlieghere static void SetError(CommandReturnObject &result, Error err) { 31697fc8eb4SJonas Devlieghere result.GetErrorStream().Printf("error: %s\n", 31797fc8eb4SJonas Devlieghere toString(std::move(err)).c_str()); 31897fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 31997fc8eb4SJonas Devlieghere } 32097fc8eb4SJonas Devlieghere 32197fc8eb4SJonas Devlieghere class CommandObjectReproducerDump : public CommandObjectParsed { 32297fc8eb4SJonas Devlieghere public: 32397fc8eb4SJonas Devlieghere CommandObjectReproducerDump(CommandInterpreter &interpreter) 32497fc8eb4SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer dump", 32564b7d955SJonas Devlieghere "Dump the information contained in a reproducer. " 32664b7d955SJonas Devlieghere "If no reproducer is specified during replay, it " 32764b7d955SJonas Devlieghere "dumps the content of the current reproducer.", 32897fc8eb4SJonas Devlieghere nullptr) {} 32997fc8eb4SJonas Devlieghere 33097fc8eb4SJonas Devlieghere ~CommandObjectReproducerDump() override = default; 33197fc8eb4SJonas Devlieghere 33297fc8eb4SJonas Devlieghere Options *GetOptions() override { return &m_options; } 33397fc8eb4SJonas Devlieghere 33497fc8eb4SJonas Devlieghere class CommandOptions : public Options { 33597fc8eb4SJonas Devlieghere public: 33697fc8eb4SJonas Devlieghere CommandOptions() : Options(), file() {} 33797fc8eb4SJonas Devlieghere 33897fc8eb4SJonas Devlieghere ~CommandOptions() override = default; 33997fc8eb4SJonas Devlieghere 34097fc8eb4SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 34197fc8eb4SJonas Devlieghere ExecutionContext *execution_context) override { 34297fc8eb4SJonas Devlieghere Status error; 34397fc8eb4SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 34497fc8eb4SJonas Devlieghere 34597fc8eb4SJonas Devlieghere switch (short_option) { 34697fc8eb4SJonas Devlieghere case 'f': 34797fc8eb4SJonas Devlieghere file.SetFile(option_arg, FileSpec::Style::native); 34897fc8eb4SJonas Devlieghere FileSystem::Instance().Resolve(file); 34997fc8eb4SJonas Devlieghere break; 35097fc8eb4SJonas Devlieghere case 'p': 35197fc8eb4SJonas Devlieghere provider = (ReproducerProvider)OptionArgParser::ToOptionEnum( 35297fc8eb4SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 35397fc8eb4SJonas Devlieghere if (!error.Success()) 35497fc8eb4SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for provider '%s'", 35597fc8eb4SJonas Devlieghere option_arg.str().c_str()); 35697fc8eb4SJonas Devlieghere break; 35797fc8eb4SJonas Devlieghere default: 35897fc8eb4SJonas Devlieghere llvm_unreachable("Unimplemented option"); 35997fc8eb4SJonas Devlieghere } 36097fc8eb4SJonas Devlieghere 36197fc8eb4SJonas Devlieghere return error; 36297fc8eb4SJonas Devlieghere } 36397fc8eb4SJonas Devlieghere 36497fc8eb4SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 36597fc8eb4SJonas Devlieghere file.Clear(); 36697fc8eb4SJonas Devlieghere provider = eReproducerProviderNone; 36797fc8eb4SJonas Devlieghere } 36897fc8eb4SJonas Devlieghere 36997fc8eb4SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 37036eea5c3SJonas Devlieghere return makeArrayRef(g_reproducer_dump_options); 37197fc8eb4SJonas Devlieghere } 37297fc8eb4SJonas Devlieghere 37397fc8eb4SJonas Devlieghere FileSpec file; 37497fc8eb4SJonas Devlieghere ReproducerProvider provider = eReproducerProviderNone; 37597fc8eb4SJonas Devlieghere }; 37697fc8eb4SJonas Devlieghere 37797fc8eb4SJonas Devlieghere protected: 37897fc8eb4SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 37997fc8eb4SJonas Devlieghere if (!command.empty()) { 38097fc8eb4SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 38197fc8eb4SJonas Devlieghere m_cmd_name.c_str()); 38297fc8eb4SJonas Devlieghere return false; 38397fc8eb4SJonas Devlieghere } 38497fc8eb4SJonas Devlieghere 38597fc8eb4SJonas Devlieghere // If no reproducer path is specified, use the loader currently used for 38697fc8eb4SJonas Devlieghere // replay. Otherwise create a new loader just for dumping. 38797fc8eb4SJonas Devlieghere llvm::Optional<Loader> loader_storage; 38897fc8eb4SJonas Devlieghere Loader *loader = nullptr; 38997fc8eb4SJonas Devlieghere if (!m_options.file) { 39097fc8eb4SJonas Devlieghere loader = Reproducer::Instance().GetLoader(); 39197fc8eb4SJonas Devlieghere if (loader == nullptr) { 39297fc8eb4SJonas Devlieghere result.SetError( 39397fc8eb4SJonas Devlieghere "Not specifying a reproducer is only support during replay."); 39497fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 39597fc8eb4SJonas Devlieghere return false; 39697fc8eb4SJonas Devlieghere } 39797fc8eb4SJonas Devlieghere } else { 39897fc8eb4SJonas Devlieghere loader_storage.emplace(m_options.file); 39997fc8eb4SJonas Devlieghere loader = &(*loader_storage); 40097fc8eb4SJonas Devlieghere if (Error err = loader->LoadIndex()) { 40197fc8eb4SJonas Devlieghere SetError(result, std::move(err)); 40297fc8eb4SJonas Devlieghere return false; 40397fc8eb4SJonas Devlieghere } 40497fc8eb4SJonas Devlieghere } 40597fc8eb4SJonas Devlieghere 40697fc8eb4SJonas Devlieghere // If we get here we should have a valid loader. 40797fc8eb4SJonas Devlieghere assert(loader); 40897fc8eb4SJonas Devlieghere 40997fc8eb4SJonas Devlieghere switch (m_options.provider) { 41097fc8eb4SJonas Devlieghere case eReproducerProviderFiles: { 41197fc8eb4SJonas Devlieghere FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); 41297fc8eb4SJonas Devlieghere 41397fc8eb4SJonas Devlieghere // Read the VFS mapping. 41497fc8eb4SJonas Devlieghere ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = 41597fc8eb4SJonas Devlieghere vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath()); 41697fc8eb4SJonas Devlieghere if (!buffer) { 41797fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(buffer.getError())); 41897fc8eb4SJonas Devlieghere return false; 41997fc8eb4SJonas Devlieghere } 42097fc8eb4SJonas Devlieghere 42197fc8eb4SJonas Devlieghere // Initialize a VFS from the given mapping. 42297fc8eb4SJonas Devlieghere IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML( 42397fc8eb4SJonas Devlieghere std::move(buffer.get()), nullptr, vfs_mapping.GetPath()); 42497fc8eb4SJonas Devlieghere 42597fc8eb4SJonas Devlieghere // Dump the VFS to a buffer. 42697fc8eb4SJonas Devlieghere std::string str; 42797fc8eb4SJonas Devlieghere raw_string_ostream os(str); 42897fc8eb4SJonas Devlieghere static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os); 42997fc8eb4SJonas Devlieghere os.flush(); 43097fc8eb4SJonas Devlieghere 43197fc8eb4SJonas Devlieghere // Return the string. 43297fc8eb4SJonas Devlieghere result.AppendMessage(str); 43397fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 43497fc8eb4SJonas Devlieghere return true; 43597fc8eb4SJonas Devlieghere } 436*a842950bSJonas Devlieghere case eReproducerProviderSymbolFiles: { 437*a842950bSJonas Devlieghere Expected<std::string> symbol_files = 438*a842950bSJonas Devlieghere loader->LoadBuffer<SymbolFileProvider>(); 439*a842950bSJonas Devlieghere if (!symbol_files) { 440*a842950bSJonas Devlieghere SetError(result, symbol_files.takeError()); 441*a842950bSJonas Devlieghere return false; 442*a842950bSJonas Devlieghere } 443*a842950bSJonas Devlieghere 444*a842950bSJonas Devlieghere std::vector<SymbolFileProvider::Entry> entries; 445*a842950bSJonas Devlieghere llvm::yaml::Input yin(*symbol_files); 446*a842950bSJonas Devlieghere yin >> entries; 447*a842950bSJonas Devlieghere 448*a842950bSJonas Devlieghere for (const auto &entry : entries) { 449*a842950bSJonas Devlieghere result.AppendMessageWithFormat("- uuid: %s\n", 450*a842950bSJonas Devlieghere entry.uuid.c_str()); 451*a842950bSJonas Devlieghere result.AppendMessageWithFormat(" module path: %s\n", 452*a842950bSJonas Devlieghere entry.module_path.c_str()); 453*a842950bSJonas Devlieghere result.AppendMessageWithFormat(" symbol path: %s\n", 454*a842950bSJonas Devlieghere entry.symbol_path.c_str()); 455*a842950bSJonas Devlieghere } 456*a842950bSJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 457*a842950bSJonas Devlieghere return true; 458*a842950bSJonas Devlieghere } 45997fc8eb4SJonas Devlieghere case eReproducerProviderVersion: { 460b2575da9SJonas Devlieghere Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); 461b2575da9SJonas Devlieghere if (!version) { 462b2575da9SJonas Devlieghere SetError(result, version.takeError()); 46397fc8eb4SJonas Devlieghere return false; 46497fc8eb4SJonas Devlieghere } 465b2575da9SJonas Devlieghere result.AppendMessage(*version); 46697fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 46797fc8eb4SJonas Devlieghere return true; 46897fc8eb4SJonas Devlieghere } 469f4f12012SJonas Devlieghere case eReproducerProviderWorkingDirectory: { 470f4f12012SJonas Devlieghere Expected<std::string> cwd = 47173af341bSJonas Devlieghere repro::GetDirectoryFrom<WorkingDirectoryProvider>(loader); 472f4f12012SJonas Devlieghere if (!cwd) { 473f4f12012SJonas Devlieghere SetError(result, cwd.takeError()); 474f4f12012SJonas Devlieghere return false; 475f4f12012SJonas Devlieghere } 476f4f12012SJonas Devlieghere result.AppendMessage(*cwd); 477f4f12012SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 478f4f12012SJonas Devlieghere return true; 479f4f12012SJonas Devlieghere } 48073af341bSJonas Devlieghere case eReproducerProviderHomeDirectory: { 48173af341bSJonas Devlieghere Expected<std::string> home = 48273af341bSJonas Devlieghere repro::GetDirectoryFrom<HomeDirectoryProvider>(loader); 48373af341bSJonas Devlieghere if (!home) { 48473af341bSJonas Devlieghere SetError(result, home.takeError()); 48573af341bSJonas Devlieghere return false; 48673af341bSJonas Devlieghere } 48773af341bSJonas Devlieghere result.AppendMessage(*home); 48873af341bSJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 48973af341bSJonas Devlieghere return true; 49073af341bSJonas Devlieghere } 49197fc8eb4SJonas Devlieghere case eReproducerProviderCommands: { 4921d41d1bcSEric Christopher std::unique_ptr<repro::MultiLoader<repro::CommandProvider>> multi_loader = 4931d41d1bcSEric Christopher repro::MultiLoader<repro::CommandProvider>::Create(loader); 4941d41d1bcSEric Christopher if (!multi_loader) { 49597fc8eb4SJonas Devlieghere SetError(result, 4964016c6b0SJonas Devlieghere make_error<StringError>("Unable to create command loader.", 4974016c6b0SJonas Devlieghere llvm::inconvertibleErrorCode())); 49897fc8eb4SJonas Devlieghere return false; 49997fc8eb4SJonas Devlieghere } 50097fc8eb4SJonas Devlieghere 50197fc8eb4SJonas Devlieghere // Iterate over the command files and dump them. 5021d41d1bcSEric Christopher llvm::Optional<std::string> command_file; 5031d41d1bcSEric Christopher while ((command_file = multi_loader->GetNextFile())) { 50497fc8eb4SJonas Devlieghere if (!command_file) 50597fc8eb4SJonas Devlieghere break; 50697fc8eb4SJonas Devlieghere 50797fc8eb4SJonas Devlieghere auto command_buffer = llvm::MemoryBuffer::getFile(*command_file); 50897fc8eb4SJonas Devlieghere if (auto err = command_buffer.getError()) { 50997fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(err)); 51097fc8eb4SJonas Devlieghere return false; 51197fc8eb4SJonas Devlieghere } 51297fc8eb4SJonas Devlieghere result.AppendMessage((*command_buffer)->getBuffer()); 51397fc8eb4SJonas Devlieghere } 51497fc8eb4SJonas Devlieghere 51597fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 51697fc8eb4SJonas Devlieghere return true; 51797fc8eb4SJonas Devlieghere } 51897fc8eb4SJonas Devlieghere case eReproducerProviderGDB: { 5191d41d1bcSEric Christopher std::unique_ptr<repro::MultiLoader<repro::GDBRemoteProvider>> 5201d41d1bcSEric Christopher multi_loader = 5211d41d1bcSEric Christopher repro::MultiLoader<repro::GDBRemoteProvider>::Create(loader); 5224016c6b0SJonas Devlieghere 5234016c6b0SJonas Devlieghere if (!multi_loader) { 5244016c6b0SJonas Devlieghere SetError(result, 5254016c6b0SJonas Devlieghere make_error<StringError>("Unable to create GDB loader.", 5264016c6b0SJonas Devlieghere llvm::inconvertibleErrorCode())); 5274016c6b0SJonas Devlieghere return false; 5284016c6b0SJonas Devlieghere } 5294016c6b0SJonas Devlieghere 5301d41d1bcSEric Christopher llvm::Optional<std::string> gdb_file; 5311d41d1bcSEric Christopher while ((gdb_file = multi_loader->GetNextFile())) { 5322451cbf0SJonas Devlieghere if (llvm::Expected<std::vector<GDBRemotePacket>> packets = 5332451cbf0SJonas Devlieghere ReadFromYAML<std::vector<GDBRemotePacket>>(*gdb_file)) { 5342451cbf0SJonas Devlieghere for (GDBRemotePacket &packet : *packets) { 5358fc8d3feSJonas Devlieghere packet.Dump(result.GetOutputStream()); 5368fc8d3feSJonas Devlieghere } 5372451cbf0SJonas Devlieghere } else { 5382451cbf0SJonas Devlieghere SetError(result, packets.takeError()); 5392451cbf0SJonas Devlieghere return false; 5402451cbf0SJonas Devlieghere } 5412451cbf0SJonas Devlieghere } 5422451cbf0SJonas Devlieghere 5432451cbf0SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 5442451cbf0SJonas Devlieghere return true; 5452451cbf0SJonas Devlieghere } 5462451cbf0SJonas Devlieghere case eReproducerProviderProcessInfo: { 5472451cbf0SJonas Devlieghere std::unique_ptr<repro::MultiLoader<repro::ProcessInfoProvider>> 5482451cbf0SJonas Devlieghere multi_loader = 5492451cbf0SJonas Devlieghere repro::MultiLoader<repro::ProcessInfoProvider>::Create(loader); 5502451cbf0SJonas Devlieghere 5512451cbf0SJonas Devlieghere if (!multi_loader) { 5522451cbf0SJonas Devlieghere SetError(result, make_error<StringError>( 5532451cbf0SJonas Devlieghere llvm::inconvertibleErrorCode(), 5542451cbf0SJonas Devlieghere "Unable to create process info loader.")); 5552451cbf0SJonas Devlieghere return false; 5562451cbf0SJonas Devlieghere } 5572451cbf0SJonas Devlieghere 5582451cbf0SJonas Devlieghere llvm::Optional<std::string> process_file; 5592451cbf0SJonas Devlieghere while ((process_file = multi_loader->GetNextFile())) { 5602451cbf0SJonas Devlieghere if (llvm::Expected<ProcessInstanceInfoList> infos = 5612451cbf0SJonas Devlieghere ReadFromYAML<ProcessInstanceInfoList>(*process_file)) { 5622451cbf0SJonas Devlieghere for (ProcessInstanceInfo info : *infos) 5632451cbf0SJonas Devlieghere info.Dump(result.GetOutputStream(), HostInfo::GetUserIDResolver()); 5642451cbf0SJonas Devlieghere } else { 5652451cbf0SJonas Devlieghere SetError(result, infos.takeError()); 5662451cbf0SJonas Devlieghere return false; 5672451cbf0SJonas Devlieghere } 5681d41d1bcSEric Christopher } 5698fc8d3feSJonas Devlieghere 57097fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 57197fc8eb4SJonas Devlieghere return true; 57297fc8eb4SJonas Devlieghere } 57397fc8eb4SJonas Devlieghere case eReproducerProviderNone: 57497fc8eb4SJonas Devlieghere result.SetError("No valid provider specified."); 57597fc8eb4SJonas Devlieghere return false; 57697fc8eb4SJonas Devlieghere } 57797fc8eb4SJonas Devlieghere 57897fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 57997fc8eb4SJonas Devlieghere return result.Succeeded(); 58097fc8eb4SJonas Devlieghere } 58197fc8eb4SJonas Devlieghere 58297fc8eb4SJonas Devlieghere private: 58397fc8eb4SJonas Devlieghere CommandOptions m_options; 58497fc8eb4SJonas Devlieghere }; 58597fc8eb4SJonas Devlieghere 5869e046f02SJonas Devlieghere CommandObjectReproducer::CommandObjectReproducer( 5879e046f02SJonas Devlieghere CommandInterpreter &interpreter) 588973d66eeSJonas Devlieghere : CommandObjectMultiword( 589973d66eeSJonas Devlieghere interpreter, "reproducer", 590c8dfe907SJonas Devlieghere "Commands for manipulating reproducers. Reproducers make it " 591c8dfe907SJonas Devlieghere "possible " 59264b7d955SJonas Devlieghere "to capture full debug sessions with all its dependencies. The " 59364b7d955SJonas Devlieghere "resulting reproducer is used to replay the debug session while " 59464b7d955SJonas Devlieghere "debugging the debugger.\n" 59564b7d955SJonas Devlieghere "Because reproducers need the whole the debug session from " 59664b7d955SJonas Devlieghere "beginning to end, you need to launch the debugger in capture or " 59764b7d955SJonas Devlieghere "replay mode, commonly though the command line driver.\n" 59864b7d955SJonas Devlieghere "Reproducers are unrelated record-replay debugging, as you cannot " 59964b7d955SJonas Devlieghere "interact with the debugger during replay.\n", 600130ec068SJonas Devlieghere "reproducer <subcommand> [<subcommand-options>]") { 6019e046f02SJonas Devlieghere LoadSubCommand( 6029e046f02SJonas Devlieghere "generate", 6039e046f02SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); 60415eacd74SJonas Devlieghere LoadSubCommand("status", CommandObjectSP( 60515eacd74SJonas Devlieghere new CommandObjectReproducerStatus(interpreter))); 60697fc8eb4SJonas Devlieghere LoadSubCommand("dump", 60797fc8eb4SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerDump(interpreter))); 608c8dfe907SJonas Devlieghere LoadSubCommand("xcrash", CommandObjectSP( 609c8dfe907SJonas Devlieghere new CommandObjectReproducerXCrash(interpreter))); 6109e046f02SJonas Devlieghere } 6119e046f02SJonas Devlieghere 6129e046f02SJonas Devlieghere CommandObjectReproducer::~CommandObjectReproducer() = default; 613