19e046f02SJonas Devlieghere //===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===// 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 1197fc8eb4SJonas Devlieghere #include "lldb/Host/OptionParser.h" 128fc8d3feSJonas Devlieghere #include "lldb/Utility/GDBRemote.h" 13f4f12012SJonas Devlieghere #include "lldb/Utility/Reproducer.h" 149e046f02SJonas Devlieghere 15df14b942SJonas Devlieghere #include "lldb/Interpreter/CommandInterpreter.h" 169e046f02SJonas Devlieghere #include "lldb/Interpreter/CommandReturnObject.h" 179e046f02SJonas Devlieghere #include "lldb/Interpreter/OptionArgParser.h" 189e046f02SJonas Devlieghere #include "lldb/Interpreter/OptionGroupBoolean.h" 199e046f02SJonas Devlieghere 20*c8dfe907SJonas Devlieghere #include <csignal> 21*c8dfe907SJonas 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, 3097fc8eb4SJonas Devlieghere eReproducerProviderGDB, 3197fc8eb4SJonas Devlieghere eReproducerProviderVersion, 32f4f12012SJonas Devlieghere eReproducerProviderWorkingDirectory, 3397fc8eb4SJonas Devlieghere eReproducerProviderNone 3497fc8eb4SJonas Devlieghere }; 3597fc8eb4SJonas Devlieghere 3697fc8eb4SJonas Devlieghere static constexpr OptionEnumValueElement g_reproducer_provider_type[] = { 3797fc8eb4SJonas Devlieghere { 3897fc8eb4SJonas Devlieghere eReproducerProviderCommands, 3997fc8eb4SJonas Devlieghere "commands", 4097fc8eb4SJonas Devlieghere "Command Interpreter Commands", 4197fc8eb4SJonas Devlieghere }, 4297fc8eb4SJonas Devlieghere { 4397fc8eb4SJonas Devlieghere eReproducerProviderFiles, 4497fc8eb4SJonas Devlieghere "files", 4597fc8eb4SJonas Devlieghere "Files", 4697fc8eb4SJonas Devlieghere }, 4797fc8eb4SJonas Devlieghere { 4897fc8eb4SJonas Devlieghere eReproducerProviderGDB, 4997fc8eb4SJonas Devlieghere "gdb", 5097fc8eb4SJonas Devlieghere "GDB Remote Packets", 5197fc8eb4SJonas Devlieghere }, 5297fc8eb4SJonas Devlieghere { 5397fc8eb4SJonas Devlieghere eReproducerProviderVersion, 5497fc8eb4SJonas Devlieghere "version", 5597fc8eb4SJonas Devlieghere "Version", 5697fc8eb4SJonas Devlieghere }, 5797fc8eb4SJonas Devlieghere { 58f4f12012SJonas Devlieghere eReproducerProviderWorkingDirectory, 59f4f12012SJonas Devlieghere "cwd", 60f4f12012SJonas Devlieghere "Working Directory", 61f4f12012SJonas Devlieghere }, 62f4f12012SJonas Devlieghere { 6397fc8eb4SJonas Devlieghere eReproducerProviderNone, 6497fc8eb4SJonas Devlieghere "none", 6597fc8eb4SJonas Devlieghere "None", 6697fc8eb4SJonas Devlieghere }, 6797fc8eb4SJonas Devlieghere }; 6897fc8eb4SJonas Devlieghere 6997fc8eb4SJonas Devlieghere static constexpr OptionEnumValues ReproducerProviderType() { 7097fc8eb4SJonas Devlieghere return OptionEnumValues(g_reproducer_provider_type); 7197fc8eb4SJonas Devlieghere } 7297fc8eb4SJonas Devlieghere 7336eea5c3SJonas Devlieghere #define LLDB_OPTIONS_reproducer_dump 7497fc8eb4SJonas Devlieghere #include "CommandOptions.inc" 759e046f02SJonas Devlieghere 76*c8dfe907SJonas Devlieghere enum ReproducerCrashSignal { 77*c8dfe907SJonas Devlieghere eReproducerCrashSigbus, 78*c8dfe907SJonas Devlieghere eReproducerCrashSigill, 79*c8dfe907SJonas Devlieghere eReproducerCrashSigsegv, 80*c8dfe907SJonas Devlieghere }; 81*c8dfe907SJonas Devlieghere 82*c8dfe907SJonas Devlieghere static constexpr OptionEnumValueElement g_reproducer_signaltype[] = { 83*c8dfe907SJonas Devlieghere { 84*c8dfe907SJonas Devlieghere eReproducerCrashSigbus, 85*c8dfe907SJonas Devlieghere "SIGBUS", 86*c8dfe907SJonas Devlieghere "Bus error", 87*c8dfe907SJonas Devlieghere }, 88*c8dfe907SJonas Devlieghere { 89*c8dfe907SJonas Devlieghere eReproducerCrashSigill, 90*c8dfe907SJonas Devlieghere "SIGILL", 91*c8dfe907SJonas Devlieghere "Illegal instruction", 92*c8dfe907SJonas Devlieghere }, 93*c8dfe907SJonas Devlieghere { 94*c8dfe907SJonas Devlieghere eReproducerCrashSigsegv, 95*c8dfe907SJonas Devlieghere "SIGSEGV", 96*c8dfe907SJonas Devlieghere "Segmentation fault", 97*c8dfe907SJonas Devlieghere }, 98*c8dfe907SJonas Devlieghere }; 99*c8dfe907SJonas Devlieghere 100*c8dfe907SJonas Devlieghere static constexpr OptionEnumValues ReproducerSignalType() { 101*c8dfe907SJonas Devlieghere return OptionEnumValues(g_reproducer_signaltype); 102*c8dfe907SJonas Devlieghere } 103*c8dfe907SJonas Devlieghere 104*c8dfe907SJonas Devlieghere #define LLDB_OPTIONS_reproducer_xcrash 105*c8dfe907SJonas Devlieghere #include "CommandOptions.inc" 106*c8dfe907SJonas Devlieghere 1079e046f02SJonas Devlieghere class CommandObjectReproducerGenerate : public CommandObjectParsed { 1089e046f02SJonas Devlieghere public: 1099e046f02SJonas Devlieghere CommandObjectReproducerGenerate(CommandInterpreter &interpreter) 110973d66eeSJonas Devlieghere : CommandObjectParsed( 111973d66eeSJonas Devlieghere interpreter, "reproducer generate", 112973d66eeSJonas Devlieghere "Generate reproducer on disk. When the debugger is in capture " 113973d66eeSJonas Devlieghere "mode, this command will output the reproducer to a directory on " 1140cf86da1SJonas Devlieghere "disk and quit. In replay mode this command in a no-op.", 115973d66eeSJonas Devlieghere nullptr) {} 1169e046f02SJonas Devlieghere 1179e046f02SJonas Devlieghere ~CommandObjectReproducerGenerate() override = default; 1189e046f02SJonas Devlieghere 1199e046f02SJonas Devlieghere protected: 1209e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 1219e046f02SJonas Devlieghere if (!command.empty()) { 1229e046f02SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 1239e046f02SJonas Devlieghere m_cmd_name.c_str()); 1249e046f02SJonas Devlieghere return false; 1259e046f02SJonas Devlieghere } 1269e046f02SJonas Devlieghere 12797fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 1289e046f02SJonas Devlieghere if (auto generator = r.GetGenerator()) { 1299e046f02SJonas Devlieghere generator->Keep(); 130865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 1312dca6538SJonas Devlieghere // Make this operation a NOP in replay mode. 1322dca6538SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 1332dca6538SJonas Devlieghere return result.Succeeded(); 1349e046f02SJonas Devlieghere } else { 1359e046f02SJonas Devlieghere result.AppendErrorWithFormat("Unable to get the reproducer generator"); 1362dca6538SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 1379e046f02SJonas Devlieghere return false; 1389e046f02SJonas Devlieghere } 1399e046f02SJonas Devlieghere 1409e046f02SJonas Devlieghere result.GetOutputStream() 1419e046f02SJonas Devlieghere << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; 1421c5250abSJonas Devlieghere result.GetOutputStream() 1431c5250abSJonas Devlieghere << "Please have a look at the directory to assess if you're willing to " 1441c5250abSJonas Devlieghere "share the contained information.\n"; 1459e046f02SJonas Devlieghere 1460cf86da1SJonas Devlieghere m_interpreter.BroadcastEvent( 1470cf86da1SJonas Devlieghere CommandInterpreter::eBroadcastBitQuitCommandReceived); 1480cf86da1SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 1499e046f02SJonas Devlieghere return result.Succeeded(); 1509e046f02SJonas Devlieghere } 1519e046f02SJonas Devlieghere }; 1529e046f02SJonas Devlieghere 153*c8dfe907SJonas Devlieghere class CommandObjectReproducerXCrash : public CommandObjectParsed { 154*c8dfe907SJonas Devlieghere public: 155*c8dfe907SJonas Devlieghere CommandObjectReproducerXCrash(CommandInterpreter &interpreter) 156*c8dfe907SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer xcrash", 157*c8dfe907SJonas Devlieghere "Intentionally force the debugger to crash in " 158*c8dfe907SJonas Devlieghere "order to trigger and test reproducer generation.", 159*c8dfe907SJonas Devlieghere nullptr) {} 160*c8dfe907SJonas Devlieghere 161*c8dfe907SJonas Devlieghere ~CommandObjectReproducerXCrash() override = default; 162*c8dfe907SJonas Devlieghere 163*c8dfe907SJonas Devlieghere Options *GetOptions() override { return &m_options; } 164*c8dfe907SJonas Devlieghere 165*c8dfe907SJonas Devlieghere class CommandOptions : public Options { 166*c8dfe907SJonas Devlieghere public: 167*c8dfe907SJonas Devlieghere CommandOptions() : Options() {} 168*c8dfe907SJonas Devlieghere 169*c8dfe907SJonas Devlieghere ~CommandOptions() override = default; 170*c8dfe907SJonas Devlieghere 171*c8dfe907SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 172*c8dfe907SJonas Devlieghere ExecutionContext *execution_context) override { 173*c8dfe907SJonas Devlieghere Status error; 174*c8dfe907SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 175*c8dfe907SJonas Devlieghere 176*c8dfe907SJonas Devlieghere switch (short_option) { 177*c8dfe907SJonas Devlieghere case 's': 178*c8dfe907SJonas Devlieghere signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum( 179*c8dfe907SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 180*c8dfe907SJonas Devlieghere if (!error.Success()) 181*c8dfe907SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for signal '%s'", 182*c8dfe907SJonas Devlieghere option_arg.str().c_str()); 183*c8dfe907SJonas Devlieghere break; 184*c8dfe907SJonas Devlieghere default: 185*c8dfe907SJonas Devlieghere llvm_unreachable("Unimplemented option"); 186*c8dfe907SJonas Devlieghere } 187*c8dfe907SJonas Devlieghere 188*c8dfe907SJonas Devlieghere return error; 189*c8dfe907SJonas Devlieghere } 190*c8dfe907SJonas Devlieghere 191*c8dfe907SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 192*c8dfe907SJonas Devlieghere signal = eReproducerCrashSigsegv; 193*c8dfe907SJonas Devlieghere } 194*c8dfe907SJonas Devlieghere 195*c8dfe907SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 196*c8dfe907SJonas Devlieghere return makeArrayRef(g_reproducer_xcrash_options); 197*c8dfe907SJonas Devlieghere } 198*c8dfe907SJonas Devlieghere 199*c8dfe907SJonas Devlieghere ReproducerCrashSignal signal = eReproducerCrashSigsegv; 200*c8dfe907SJonas Devlieghere }; 201*c8dfe907SJonas Devlieghere 202*c8dfe907SJonas Devlieghere protected: 203*c8dfe907SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 204*c8dfe907SJonas Devlieghere if (!command.empty()) { 205*c8dfe907SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 206*c8dfe907SJonas Devlieghere m_cmd_name.c_str()); 207*c8dfe907SJonas Devlieghere return false; 208*c8dfe907SJonas Devlieghere } 209*c8dfe907SJonas Devlieghere 210*c8dfe907SJonas Devlieghere auto &r = Reproducer::Instance(); 211*c8dfe907SJonas Devlieghere if (!r.IsCapturing()) { 212*c8dfe907SJonas Devlieghere result.SetError( 213*c8dfe907SJonas Devlieghere "forcing a crash is only supported when capturing a reproducer."); 214*c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 215*c8dfe907SJonas Devlieghere return false; 216*c8dfe907SJonas Devlieghere } 217*c8dfe907SJonas Devlieghere 218*c8dfe907SJonas Devlieghere switch (m_options.signal) { 219*c8dfe907SJonas Devlieghere case eReproducerCrashSigill: 220*c8dfe907SJonas Devlieghere std::raise(SIGILL); 221*c8dfe907SJonas Devlieghere break; 222*c8dfe907SJonas Devlieghere case eReproducerCrashSigbus: 223*c8dfe907SJonas Devlieghere std::raise(SIGBUS); 224*c8dfe907SJonas Devlieghere break; 225*c8dfe907SJonas Devlieghere case eReproducerCrashSigsegv: 226*c8dfe907SJonas Devlieghere std::raise(SIGSEGV); 227*c8dfe907SJonas Devlieghere break; 228*c8dfe907SJonas Devlieghere } 229*c8dfe907SJonas Devlieghere 230*c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 231*c8dfe907SJonas Devlieghere return result.Succeeded(); 232*c8dfe907SJonas Devlieghere } 233*c8dfe907SJonas Devlieghere 234*c8dfe907SJonas Devlieghere private: 235*c8dfe907SJonas Devlieghere CommandOptions m_options; 236*c8dfe907SJonas Devlieghere }; 237*c8dfe907SJonas Devlieghere 23815eacd74SJonas Devlieghere class CommandObjectReproducerStatus : public CommandObjectParsed { 2399e046f02SJonas Devlieghere public: 24015eacd74SJonas Devlieghere CommandObjectReproducerStatus(CommandInterpreter &interpreter) 241973d66eeSJonas Devlieghere : CommandObjectParsed( 242973d66eeSJonas Devlieghere interpreter, "reproducer status", 243*c8dfe907SJonas Devlieghere "Show the current reproducer status. In capture mode the " 244*c8dfe907SJonas Devlieghere "debugger " 245973d66eeSJonas Devlieghere "is collecting all the information it needs to create a " 246973d66eeSJonas Devlieghere "reproducer. In replay mode the reproducer is replaying a " 247973d66eeSJonas Devlieghere "reproducer. When the reproducers are off, no data is collected " 248973d66eeSJonas Devlieghere "and no reproducer can be generated.", 249973d66eeSJonas Devlieghere nullptr) {} 2509e046f02SJonas Devlieghere 25115eacd74SJonas Devlieghere ~CommandObjectReproducerStatus() override = default; 2529e046f02SJonas Devlieghere 2539e046f02SJonas Devlieghere protected: 2549e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 25515eacd74SJonas Devlieghere if (!command.empty()) { 25615eacd74SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 2579e046f02SJonas Devlieghere m_cmd_name.c_str()); 2589e046f02SJonas Devlieghere return false; 2599e046f02SJonas Devlieghere } 2609e046f02SJonas Devlieghere 26197fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 262865cd093SJonas Devlieghere if (r.IsCapturing()) { 26315eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in capture mode.\n"; 264865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 26515eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in replay mode.\n"; 26615eacd74SJonas Devlieghere } else { 26715eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is off.\n"; 2689e046f02SJonas Devlieghere } 2699e046f02SJonas Devlieghere 27015eacd74SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 2719e046f02SJonas Devlieghere return result.Succeeded(); 2729e046f02SJonas Devlieghere } 2739e046f02SJonas Devlieghere }; 2749e046f02SJonas Devlieghere 27597fc8eb4SJonas Devlieghere static void SetError(CommandReturnObject &result, Error err) { 27697fc8eb4SJonas Devlieghere result.GetErrorStream().Printf("error: %s\n", 27797fc8eb4SJonas Devlieghere toString(std::move(err)).c_str()); 27897fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 27997fc8eb4SJonas Devlieghere } 28097fc8eb4SJonas Devlieghere 28197fc8eb4SJonas Devlieghere class CommandObjectReproducerDump : public CommandObjectParsed { 28297fc8eb4SJonas Devlieghere public: 28397fc8eb4SJonas Devlieghere CommandObjectReproducerDump(CommandInterpreter &interpreter) 28497fc8eb4SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer dump", 28564b7d955SJonas Devlieghere "Dump the information contained in a reproducer. " 28664b7d955SJonas Devlieghere "If no reproducer is specified during replay, it " 28764b7d955SJonas Devlieghere "dumps the content of the current reproducer.", 28897fc8eb4SJonas Devlieghere nullptr) {} 28997fc8eb4SJonas Devlieghere 29097fc8eb4SJonas Devlieghere ~CommandObjectReproducerDump() override = default; 29197fc8eb4SJonas Devlieghere 29297fc8eb4SJonas Devlieghere Options *GetOptions() override { return &m_options; } 29397fc8eb4SJonas Devlieghere 29497fc8eb4SJonas Devlieghere class CommandOptions : public Options { 29597fc8eb4SJonas Devlieghere public: 29697fc8eb4SJonas Devlieghere CommandOptions() : Options(), file() {} 29797fc8eb4SJonas Devlieghere 29897fc8eb4SJonas Devlieghere ~CommandOptions() override = default; 29997fc8eb4SJonas Devlieghere 30097fc8eb4SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 30197fc8eb4SJonas Devlieghere ExecutionContext *execution_context) override { 30297fc8eb4SJonas Devlieghere Status error; 30397fc8eb4SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 30497fc8eb4SJonas Devlieghere 30597fc8eb4SJonas Devlieghere switch (short_option) { 30697fc8eb4SJonas Devlieghere case 'f': 30797fc8eb4SJonas Devlieghere file.SetFile(option_arg, FileSpec::Style::native); 30897fc8eb4SJonas Devlieghere FileSystem::Instance().Resolve(file); 30997fc8eb4SJonas Devlieghere break; 31097fc8eb4SJonas Devlieghere case 'p': 31197fc8eb4SJonas Devlieghere provider = (ReproducerProvider)OptionArgParser::ToOptionEnum( 31297fc8eb4SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 31397fc8eb4SJonas Devlieghere if (!error.Success()) 31497fc8eb4SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for provider '%s'", 31597fc8eb4SJonas Devlieghere option_arg.str().c_str()); 31697fc8eb4SJonas Devlieghere break; 31797fc8eb4SJonas Devlieghere default: 31897fc8eb4SJonas Devlieghere llvm_unreachable("Unimplemented option"); 31997fc8eb4SJonas Devlieghere } 32097fc8eb4SJonas Devlieghere 32197fc8eb4SJonas Devlieghere return error; 32297fc8eb4SJonas Devlieghere } 32397fc8eb4SJonas Devlieghere 32497fc8eb4SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 32597fc8eb4SJonas Devlieghere file.Clear(); 32697fc8eb4SJonas Devlieghere provider = eReproducerProviderNone; 32797fc8eb4SJonas Devlieghere } 32897fc8eb4SJonas Devlieghere 32997fc8eb4SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 33036eea5c3SJonas Devlieghere return makeArrayRef(g_reproducer_dump_options); 33197fc8eb4SJonas Devlieghere } 33297fc8eb4SJonas Devlieghere 33397fc8eb4SJonas Devlieghere FileSpec file; 33497fc8eb4SJonas Devlieghere ReproducerProvider provider = eReproducerProviderNone; 33597fc8eb4SJonas Devlieghere }; 33697fc8eb4SJonas Devlieghere 33797fc8eb4SJonas Devlieghere protected: 33897fc8eb4SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 33997fc8eb4SJonas Devlieghere if (!command.empty()) { 34097fc8eb4SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 34197fc8eb4SJonas Devlieghere m_cmd_name.c_str()); 34297fc8eb4SJonas Devlieghere return false; 34397fc8eb4SJonas Devlieghere } 34497fc8eb4SJonas Devlieghere 34597fc8eb4SJonas Devlieghere // If no reproducer path is specified, use the loader currently used for 34697fc8eb4SJonas Devlieghere // replay. Otherwise create a new loader just for dumping. 34797fc8eb4SJonas Devlieghere llvm::Optional<Loader> loader_storage; 34897fc8eb4SJonas Devlieghere Loader *loader = nullptr; 34997fc8eb4SJonas Devlieghere if (!m_options.file) { 35097fc8eb4SJonas Devlieghere loader = Reproducer::Instance().GetLoader(); 35197fc8eb4SJonas Devlieghere if (loader == nullptr) { 35297fc8eb4SJonas Devlieghere result.SetError( 35397fc8eb4SJonas Devlieghere "Not specifying a reproducer is only support during replay."); 35497fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 35597fc8eb4SJonas Devlieghere return false; 35697fc8eb4SJonas Devlieghere } 35797fc8eb4SJonas Devlieghere } else { 35897fc8eb4SJonas Devlieghere loader_storage.emplace(m_options.file); 35997fc8eb4SJonas Devlieghere loader = &(*loader_storage); 36097fc8eb4SJonas Devlieghere if (Error err = loader->LoadIndex()) { 36197fc8eb4SJonas Devlieghere SetError(result, std::move(err)); 36297fc8eb4SJonas Devlieghere return false; 36397fc8eb4SJonas Devlieghere } 36497fc8eb4SJonas Devlieghere } 36597fc8eb4SJonas Devlieghere 36697fc8eb4SJonas Devlieghere // If we get here we should have a valid loader. 36797fc8eb4SJonas Devlieghere assert(loader); 36897fc8eb4SJonas Devlieghere 36997fc8eb4SJonas Devlieghere switch (m_options.provider) { 37097fc8eb4SJonas Devlieghere case eReproducerProviderFiles: { 37197fc8eb4SJonas Devlieghere FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); 37297fc8eb4SJonas Devlieghere 37397fc8eb4SJonas Devlieghere // Read the VFS mapping. 37497fc8eb4SJonas Devlieghere ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = 37597fc8eb4SJonas Devlieghere vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath()); 37697fc8eb4SJonas Devlieghere if (!buffer) { 37797fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(buffer.getError())); 37897fc8eb4SJonas Devlieghere return false; 37997fc8eb4SJonas Devlieghere } 38097fc8eb4SJonas Devlieghere 38197fc8eb4SJonas Devlieghere // Initialize a VFS from the given mapping. 38297fc8eb4SJonas Devlieghere IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML( 38397fc8eb4SJonas Devlieghere std::move(buffer.get()), nullptr, vfs_mapping.GetPath()); 38497fc8eb4SJonas Devlieghere 38597fc8eb4SJonas Devlieghere // Dump the VFS to a buffer. 38697fc8eb4SJonas Devlieghere std::string str; 38797fc8eb4SJonas Devlieghere raw_string_ostream os(str); 38897fc8eb4SJonas Devlieghere static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os); 38997fc8eb4SJonas Devlieghere os.flush(); 39097fc8eb4SJonas Devlieghere 39197fc8eb4SJonas Devlieghere // Return the string. 39297fc8eb4SJonas Devlieghere result.AppendMessage(str); 39397fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 39497fc8eb4SJonas Devlieghere return true; 39597fc8eb4SJonas Devlieghere } 39697fc8eb4SJonas Devlieghere case eReproducerProviderVersion: { 397b2575da9SJonas Devlieghere Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); 398b2575da9SJonas Devlieghere if (!version) { 399b2575da9SJonas Devlieghere SetError(result, version.takeError()); 40097fc8eb4SJonas Devlieghere return false; 40197fc8eb4SJonas Devlieghere } 402b2575da9SJonas Devlieghere result.AppendMessage(*version); 40397fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 40497fc8eb4SJonas Devlieghere return true; 40597fc8eb4SJonas Devlieghere } 406f4f12012SJonas Devlieghere case eReproducerProviderWorkingDirectory: { 407f4f12012SJonas Devlieghere Expected<std::string> cwd = 408f4f12012SJonas Devlieghere loader->LoadBuffer<WorkingDirectoryProvider>(); 409f4f12012SJonas Devlieghere if (!cwd) { 410f4f12012SJonas Devlieghere SetError(result, cwd.takeError()); 411f4f12012SJonas Devlieghere return false; 412f4f12012SJonas Devlieghere } 413f4f12012SJonas Devlieghere result.AppendMessage(*cwd); 414f4f12012SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 415f4f12012SJonas Devlieghere return true; 416f4f12012SJonas Devlieghere } 41797fc8eb4SJonas Devlieghere case eReproducerProviderCommands: { 41897fc8eb4SJonas Devlieghere // Create a new command loader. 41997fc8eb4SJonas Devlieghere std::unique_ptr<repro::CommandLoader> command_loader = 42097fc8eb4SJonas Devlieghere repro::CommandLoader::Create(loader); 42197fc8eb4SJonas Devlieghere if (!command_loader) { 42297fc8eb4SJonas Devlieghere SetError(result, 42397fc8eb4SJonas Devlieghere make_error<StringError>(llvm::inconvertibleErrorCode(), 42497fc8eb4SJonas Devlieghere "Unable to create command loader.")); 42597fc8eb4SJonas Devlieghere return false; 42697fc8eb4SJonas Devlieghere } 42797fc8eb4SJonas Devlieghere 42897fc8eb4SJonas Devlieghere // Iterate over the command files and dump them. 42997fc8eb4SJonas Devlieghere while (true) { 43097fc8eb4SJonas Devlieghere llvm::Optional<std::string> command_file = 43197fc8eb4SJonas Devlieghere command_loader->GetNextFile(); 43297fc8eb4SJonas Devlieghere if (!command_file) 43397fc8eb4SJonas Devlieghere break; 43497fc8eb4SJonas Devlieghere 43597fc8eb4SJonas Devlieghere auto command_buffer = llvm::MemoryBuffer::getFile(*command_file); 43697fc8eb4SJonas Devlieghere if (auto err = command_buffer.getError()) { 43797fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(err)); 43897fc8eb4SJonas Devlieghere return false; 43997fc8eb4SJonas Devlieghere } 44097fc8eb4SJonas Devlieghere result.AppendMessage((*command_buffer)->getBuffer()); 44197fc8eb4SJonas Devlieghere } 44297fc8eb4SJonas Devlieghere 44397fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 44497fc8eb4SJonas Devlieghere return true; 44597fc8eb4SJonas Devlieghere } 44697fc8eb4SJonas Devlieghere case eReproducerProviderGDB: { 4478fc8d3feSJonas Devlieghere FileSpec gdb_file = loader->GetFile<ProcessGDBRemoteProvider::Info>(); 4488fc8d3feSJonas Devlieghere auto error_or_file = MemoryBuffer::getFile(gdb_file.GetPath()); 4498fc8d3feSJonas Devlieghere if (auto err = error_or_file.getError()) { 4508fc8d3feSJonas Devlieghere SetError(result, errorCodeToError(err)); 4518fc8d3feSJonas Devlieghere return false; 4528fc8d3feSJonas Devlieghere } 4538fc8d3feSJonas Devlieghere 4548fc8d3feSJonas Devlieghere std::vector<GDBRemotePacket> packets; 4558fc8d3feSJonas Devlieghere yaml::Input yin((*error_or_file)->getBuffer()); 4568fc8d3feSJonas Devlieghere yin >> packets; 4578fc8d3feSJonas Devlieghere 4588fc8d3feSJonas Devlieghere if (auto err = yin.error()) { 4598fc8d3feSJonas Devlieghere SetError(result, errorCodeToError(err)); 4608fc8d3feSJonas Devlieghere return false; 4618fc8d3feSJonas Devlieghere } 4628fc8d3feSJonas Devlieghere 4638fc8d3feSJonas Devlieghere for (GDBRemotePacket &packet : packets) { 4648fc8d3feSJonas Devlieghere packet.Dump(result.GetOutputStream()); 4658fc8d3feSJonas Devlieghere } 4668fc8d3feSJonas Devlieghere 46797fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 46897fc8eb4SJonas Devlieghere return true; 46997fc8eb4SJonas Devlieghere } 47097fc8eb4SJonas Devlieghere case eReproducerProviderNone: 47197fc8eb4SJonas Devlieghere result.SetError("No valid provider specified."); 47297fc8eb4SJonas Devlieghere return false; 47397fc8eb4SJonas Devlieghere } 47497fc8eb4SJonas Devlieghere 47597fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 47697fc8eb4SJonas Devlieghere return result.Succeeded(); 47797fc8eb4SJonas Devlieghere } 47897fc8eb4SJonas Devlieghere 47997fc8eb4SJonas Devlieghere private: 48097fc8eb4SJonas Devlieghere CommandOptions m_options; 48197fc8eb4SJonas Devlieghere }; 48297fc8eb4SJonas Devlieghere 4839e046f02SJonas Devlieghere CommandObjectReproducer::CommandObjectReproducer( 4849e046f02SJonas Devlieghere CommandInterpreter &interpreter) 485973d66eeSJonas Devlieghere : CommandObjectMultiword( 486973d66eeSJonas Devlieghere interpreter, "reproducer", 487*c8dfe907SJonas Devlieghere "Commands for manipulating reproducers. Reproducers make it " 488*c8dfe907SJonas Devlieghere "possible " 48964b7d955SJonas Devlieghere "to capture full debug sessions with all its dependencies. The " 49064b7d955SJonas Devlieghere "resulting reproducer is used to replay the debug session while " 49164b7d955SJonas Devlieghere "debugging the debugger.\n" 49264b7d955SJonas Devlieghere "Because reproducers need the whole the debug session from " 49364b7d955SJonas Devlieghere "beginning to end, you need to launch the debugger in capture or " 49464b7d955SJonas Devlieghere "replay mode, commonly though the command line driver.\n" 49564b7d955SJonas Devlieghere "Reproducers are unrelated record-replay debugging, as you cannot " 49664b7d955SJonas Devlieghere "interact with the debugger during replay.\n", 497130ec068SJonas Devlieghere "reproducer <subcommand> [<subcommand-options>]") { 4989e046f02SJonas Devlieghere LoadSubCommand( 4999e046f02SJonas Devlieghere "generate", 5009e046f02SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); 50115eacd74SJonas Devlieghere LoadSubCommand("status", CommandObjectSP( 50215eacd74SJonas Devlieghere new CommandObjectReproducerStatus(interpreter))); 50397fc8eb4SJonas Devlieghere LoadSubCommand("dump", 50497fc8eb4SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerDump(interpreter))); 505*c8dfe907SJonas Devlieghere LoadSubCommand("xcrash", CommandObjectSP( 506*c8dfe907SJonas Devlieghere new CommandObjectReproducerXCrash(interpreter))); 5079e046f02SJonas Devlieghere } 5089e046f02SJonas Devlieghere 5099e046f02SJonas Devlieghere CommandObjectReproducer::~CommandObjectReproducer() = default; 510