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 
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,
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 
76c8dfe907SJonas Devlieghere enum ReproducerCrashSignal {
77c8dfe907SJonas Devlieghere   eReproducerCrashSigill,
78c8dfe907SJonas Devlieghere   eReproducerCrashSigsegv,
79c8dfe907SJonas Devlieghere };
80c8dfe907SJonas Devlieghere 
81c8dfe907SJonas Devlieghere static constexpr OptionEnumValueElement g_reproducer_signaltype[] = {
82c8dfe907SJonas Devlieghere     {
83c8dfe907SJonas Devlieghere         eReproducerCrashSigill,
84c8dfe907SJonas Devlieghere         "SIGILL",
85c8dfe907SJonas Devlieghere         "Illegal instruction",
86c8dfe907SJonas Devlieghere     },
87c8dfe907SJonas Devlieghere     {
88c8dfe907SJonas Devlieghere         eReproducerCrashSigsegv,
89c8dfe907SJonas Devlieghere         "SIGSEGV",
90c8dfe907SJonas Devlieghere         "Segmentation fault",
91c8dfe907SJonas Devlieghere     },
92c8dfe907SJonas Devlieghere };
93c8dfe907SJonas Devlieghere 
94c8dfe907SJonas Devlieghere static constexpr OptionEnumValues ReproducerSignalType() {
95c8dfe907SJonas Devlieghere   return OptionEnumValues(g_reproducer_signaltype);
96c8dfe907SJonas Devlieghere }
97c8dfe907SJonas Devlieghere 
98c8dfe907SJonas Devlieghere #define LLDB_OPTIONS_reproducer_xcrash
99c8dfe907SJonas Devlieghere #include "CommandOptions.inc"
100c8dfe907SJonas Devlieghere 
1019e046f02SJonas Devlieghere class CommandObjectReproducerGenerate : public CommandObjectParsed {
1029e046f02SJonas Devlieghere public:
1039e046f02SJonas Devlieghere   CommandObjectReproducerGenerate(CommandInterpreter &interpreter)
104973d66eeSJonas Devlieghere       : CommandObjectParsed(
105973d66eeSJonas Devlieghere             interpreter, "reproducer generate",
106973d66eeSJonas Devlieghere             "Generate reproducer on disk. When the debugger is in capture "
107973d66eeSJonas Devlieghere             "mode, this command will output the reproducer to a directory on "
1080cf86da1SJonas Devlieghere             "disk and quit. In replay mode this command in a no-op.",
109973d66eeSJonas Devlieghere             nullptr) {}
1109e046f02SJonas Devlieghere 
1119e046f02SJonas Devlieghere   ~CommandObjectReproducerGenerate() override = default;
1129e046f02SJonas Devlieghere 
1139e046f02SJonas Devlieghere protected:
1149e046f02SJonas Devlieghere   bool DoExecute(Args &command, CommandReturnObject &result) override {
1159e046f02SJonas Devlieghere     if (!command.empty()) {
1169e046f02SJonas Devlieghere       result.AppendErrorWithFormat("'%s' takes no arguments",
1179e046f02SJonas Devlieghere                                    m_cmd_name.c_str());
1189e046f02SJonas Devlieghere       return false;
1199e046f02SJonas Devlieghere     }
1209e046f02SJonas Devlieghere 
12197fc8eb4SJonas Devlieghere     auto &r = Reproducer::Instance();
1229e046f02SJonas Devlieghere     if (auto generator = r.GetGenerator()) {
1239e046f02SJonas Devlieghere       generator->Keep();
124865cd093SJonas Devlieghere     } else if (r.IsReplaying()) {
125*bb090bb1SJonas Devlieghere       // Make this operation a NO-OP in replay mode.
1262dca6538SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1272dca6538SJonas Devlieghere       return result.Succeeded();
1289e046f02SJonas Devlieghere     } else {
1299e046f02SJonas Devlieghere       result.AppendErrorWithFormat("Unable to get the reproducer generator");
1302dca6538SJonas Devlieghere       result.SetStatus(eReturnStatusFailed);
1319e046f02SJonas Devlieghere       return false;
1329e046f02SJonas Devlieghere     }
1339e046f02SJonas Devlieghere 
1349e046f02SJonas Devlieghere     result.GetOutputStream()
1359e046f02SJonas Devlieghere         << "Reproducer written to '" << r.GetReproducerPath() << "'\n";
1361c5250abSJonas Devlieghere     result.GetOutputStream()
1371c5250abSJonas Devlieghere         << "Please have a look at the directory to assess if you're willing to "
1381c5250abSJonas Devlieghere            "share the contained information.\n";
1399e046f02SJonas Devlieghere 
1400cf86da1SJonas Devlieghere     m_interpreter.BroadcastEvent(
1410cf86da1SJonas Devlieghere         CommandInterpreter::eBroadcastBitQuitCommandReceived);
1420cf86da1SJonas Devlieghere     result.SetStatus(eReturnStatusQuit);
1439e046f02SJonas Devlieghere     return result.Succeeded();
1449e046f02SJonas Devlieghere   }
1459e046f02SJonas Devlieghere };
1469e046f02SJonas Devlieghere 
147c8dfe907SJonas Devlieghere class CommandObjectReproducerXCrash : public CommandObjectParsed {
148c8dfe907SJonas Devlieghere public:
149c8dfe907SJonas Devlieghere   CommandObjectReproducerXCrash(CommandInterpreter &interpreter)
150c8dfe907SJonas Devlieghere       : CommandObjectParsed(interpreter, "reproducer xcrash",
151c8dfe907SJonas Devlieghere                             "Intentionally force  the debugger to crash in "
152c8dfe907SJonas Devlieghere                             "order to trigger and test reproducer generation.",
153c8dfe907SJonas Devlieghere                             nullptr) {}
154c8dfe907SJonas Devlieghere 
155c8dfe907SJonas Devlieghere   ~CommandObjectReproducerXCrash() override = default;
156c8dfe907SJonas Devlieghere 
157c8dfe907SJonas Devlieghere   Options *GetOptions() override { return &m_options; }
158c8dfe907SJonas Devlieghere 
159c8dfe907SJonas Devlieghere   class CommandOptions : public Options {
160c8dfe907SJonas Devlieghere   public:
161c8dfe907SJonas Devlieghere     CommandOptions() : Options() {}
162c8dfe907SJonas Devlieghere 
163c8dfe907SJonas Devlieghere     ~CommandOptions() override = default;
164c8dfe907SJonas Devlieghere 
165c8dfe907SJonas Devlieghere     Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
166c8dfe907SJonas Devlieghere                           ExecutionContext *execution_context) override {
167c8dfe907SJonas Devlieghere       Status error;
168c8dfe907SJonas Devlieghere       const int short_option = m_getopt_table[option_idx].val;
169c8dfe907SJonas Devlieghere 
170c8dfe907SJonas Devlieghere       switch (short_option) {
171c8dfe907SJonas Devlieghere       case 's':
172c8dfe907SJonas Devlieghere         signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum(
173c8dfe907SJonas Devlieghere             option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
174c8dfe907SJonas Devlieghere         if (!error.Success())
175c8dfe907SJonas Devlieghere           error.SetErrorStringWithFormat("unrecognized value for signal '%s'",
176c8dfe907SJonas Devlieghere                                          option_arg.str().c_str());
177c8dfe907SJonas Devlieghere         break;
178c8dfe907SJonas Devlieghere       default:
179c8dfe907SJonas Devlieghere         llvm_unreachable("Unimplemented option");
180c8dfe907SJonas Devlieghere       }
181c8dfe907SJonas Devlieghere 
182c8dfe907SJonas Devlieghere       return error;
183c8dfe907SJonas Devlieghere     }
184c8dfe907SJonas Devlieghere 
185c8dfe907SJonas Devlieghere     void OptionParsingStarting(ExecutionContext *execution_context) override {
186c8dfe907SJonas Devlieghere       signal = eReproducerCrashSigsegv;
187c8dfe907SJonas Devlieghere     }
188c8dfe907SJonas Devlieghere 
189c8dfe907SJonas Devlieghere     ArrayRef<OptionDefinition> GetDefinitions() override {
190c8dfe907SJonas Devlieghere       return makeArrayRef(g_reproducer_xcrash_options);
191c8dfe907SJonas Devlieghere     }
192c8dfe907SJonas Devlieghere 
193c8dfe907SJonas Devlieghere     ReproducerCrashSignal signal = eReproducerCrashSigsegv;
194c8dfe907SJonas Devlieghere   };
195c8dfe907SJonas Devlieghere 
196c8dfe907SJonas Devlieghere protected:
197c8dfe907SJonas Devlieghere   bool DoExecute(Args &command, CommandReturnObject &result) override {
198c8dfe907SJonas Devlieghere     if (!command.empty()) {
199c8dfe907SJonas Devlieghere       result.AppendErrorWithFormat("'%s' takes no arguments",
200c8dfe907SJonas Devlieghere                                    m_cmd_name.c_str());
201c8dfe907SJonas Devlieghere       return false;
202c8dfe907SJonas Devlieghere     }
203c8dfe907SJonas Devlieghere 
204c8dfe907SJonas Devlieghere     auto &r = Reproducer::Instance();
205*bb090bb1SJonas Devlieghere 
206*bb090bb1SJonas Devlieghere     if (!r.IsCapturing() && !r.IsReplaying()) {
207c8dfe907SJonas Devlieghere       result.SetError(
208c8dfe907SJonas Devlieghere           "forcing a crash is only supported when capturing a reproducer.");
209c8dfe907SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishNoResult);
210c8dfe907SJonas Devlieghere       return false;
211c8dfe907SJonas Devlieghere     }
212c8dfe907SJonas Devlieghere 
213c8dfe907SJonas Devlieghere     switch (m_options.signal) {
214c8dfe907SJonas Devlieghere     case eReproducerCrashSigill:
215c8dfe907SJonas Devlieghere       std::raise(SIGILL);
216c8dfe907SJonas Devlieghere       break;
217c8dfe907SJonas Devlieghere     case eReproducerCrashSigsegv:
218c8dfe907SJonas Devlieghere       std::raise(SIGSEGV);
219c8dfe907SJonas Devlieghere       break;
220c8dfe907SJonas Devlieghere     }
221c8dfe907SJonas Devlieghere 
222c8dfe907SJonas Devlieghere     result.SetStatus(eReturnStatusQuit);
223c8dfe907SJonas Devlieghere     return result.Succeeded();
224c8dfe907SJonas Devlieghere   }
225c8dfe907SJonas Devlieghere 
226c8dfe907SJonas Devlieghere private:
227c8dfe907SJonas Devlieghere   CommandOptions m_options;
228c8dfe907SJonas Devlieghere };
229c8dfe907SJonas Devlieghere 
23015eacd74SJonas Devlieghere class CommandObjectReproducerStatus : public CommandObjectParsed {
2319e046f02SJonas Devlieghere public:
23215eacd74SJonas Devlieghere   CommandObjectReproducerStatus(CommandInterpreter &interpreter)
233973d66eeSJonas Devlieghere       : CommandObjectParsed(
234973d66eeSJonas Devlieghere             interpreter, "reproducer status",
235c8dfe907SJonas Devlieghere             "Show the current reproducer status. In capture mode the "
236c8dfe907SJonas Devlieghere             "debugger "
237973d66eeSJonas Devlieghere             "is collecting all the information it needs to create a "
238973d66eeSJonas Devlieghere             "reproducer.  In replay mode the reproducer is replaying a "
239973d66eeSJonas Devlieghere             "reproducer. When the reproducers are off, no data is collected "
240973d66eeSJonas Devlieghere             "and no reproducer can be generated.",
241973d66eeSJonas Devlieghere             nullptr) {}
2429e046f02SJonas Devlieghere 
24315eacd74SJonas Devlieghere   ~CommandObjectReproducerStatus() override = default;
2449e046f02SJonas Devlieghere 
2459e046f02SJonas Devlieghere protected:
2469e046f02SJonas Devlieghere   bool DoExecute(Args &command, CommandReturnObject &result) override {
24715eacd74SJonas Devlieghere     if (!command.empty()) {
24815eacd74SJonas Devlieghere       result.AppendErrorWithFormat("'%s' takes no arguments",
2499e046f02SJonas Devlieghere                                    m_cmd_name.c_str());
2509e046f02SJonas Devlieghere       return false;
2519e046f02SJonas Devlieghere     }
2529e046f02SJonas Devlieghere 
25397fc8eb4SJonas Devlieghere     auto &r = Reproducer::Instance();
254865cd093SJonas Devlieghere     if (r.IsCapturing()) {
25515eacd74SJonas Devlieghere       result.GetOutputStream() << "Reproducer is in capture mode.\n";
256865cd093SJonas Devlieghere     } else if (r.IsReplaying()) {
25715eacd74SJonas Devlieghere       result.GetOutputStream() << "Reproducer is in replay mode.\n";
25815eacd74SJonas Devlieghere     } else {
25915eacd74SJonas Devlieghere       result.GetOutputStream() << "Reproducer is off.\n";
2609e046f02SJonas Devlieghere     }
2619e046f02SJonas Devlieghere 
26215eacd74SJonas Devlieghere     result.SetStatus(eReturnStatusSuccessFinishResult);
2639e046f02SJonas Devlieghere     return result.Succeeded();
2649e046f02SJonas Devlieghere   }
2659e046f02SJonas Devlieghere };
2669e046f02SJonas Devlieghere 
26797fc8eb4SJonas Devlieghere static void SetError(CommandReturnObject &result, Error err) {
26897fc8eb4SJonas Devlieghere   result.GetErrorStream().Printf("error: %s\n",
26997fc8eb4SJonas Devlieghere                                  toString(std::move(err)).c_str());
27097fc8eb4SJonas Devlieghere   result.SetStatus(eReturnStatusFailed);
27197fc8eb4SJonas Devlieghere }
27297fc8eb4SJonas Devlieghere 
27397fc8eb4SJonas Devlieghere class CommandObjectReproducerDump : public CommandObjectParsed {
27497fc8eb4SJonas Devlieghere public:
27597fc8eb4SJonas Devlieghere   CommandObjectReproducerDump(CommandInterpreter &interpreter)
27697fc8eb4SJonas Devlieghere       : CommandObjectParsed(interpreter, "reproducer dump",
27764b7d955SJonas Devlieghere                             "Dump the information contained in a reproducer. "
27864b7d955SJonas Devlieghere                             "If no reproducer is specified during replay, it "
27964b7d955SJonas Devlieghere                             "dumps the content of the current reproducer.",
28097fc8eb4SJonas Devlieghere                             nullptr) {}
28197fc8eb4SJonas Devlieghere 
28297fc8eb4SJonas Devlieghere   ~CommandObjectReproducerDump() override = default;
28397fc8eb4SJonas Devlieghere 
28497fc8eb4SJonas Devlieghere   Options *GetOptions() override { return &m_options; }
28597fc8eb4SJonas Devlieghere 
28697fc8eb4SJonas Devlieghere   class CommandOptions : public Options {
28797fc8eb4SJonas Devlieghere   public:
28897fc8eb4SJonas Devlieghere     CommandOptions() : Options(), file() {}
28997fc8eb4SJonas Devlieghere 
29097fc8eb4SJonas Devlieghere     ~CommandOptions() override = default;
29197fc8eb4SJonas Devlieghere 
29297fc8eb4SJonas Devlieghere     Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
29397fc8eb4SJonas Devlieghere                           ExecutionContext *execution_context) override {
29497fc8eb4SJonas Devlieghere       Status error;
29597fc8eb4SJonas Devlieghere       const int short_option = m_getopt_table[option_idx].val;
29697fc8eb4SJonas Devlieghere 
29797fc8eb4SJonas Devlieghere       switch (short_option) {
29897fc8eb4SJonas Devlieghere       case 'f':
29997fc8eb4SJonas Devlieghere         file.SetFile(option_arg, FileSpec::Style::native);
30097fc8eb4SJonas Devlieghere         FileSystem::Instance().Resolve(file);
30197fc8eb4SJonas Devlieghere         break;
30297fc8eb4SJonas Devlieghere       case 'p':
30397fc8eb4SJonas Devlieghere         provider = (ReproducerProvider)OptionArgParser::ToOptionEnum(
30497fc8eb4SJonas Devlieghere             option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
30597fc8eb4SJonas Devlieghere         if (!error.Success())
30697fc8eb4SJonas Devlieghere           error.SetErrorStringWithFormat("unrecognized value for provider '%s'",
30797fc8eb4SJonas Devlieghere                                          option_arg.str().c_str());
30897fc8eb4SJonas Devlieghere         break;
30997fc8eb4SJonas Devlieghere       default:
31097fc8eb4SJonas Devlieghere         llvm_unreachable("Unimplemented option");
31197fc8eb4SJonas Devlieghere       }
31297fc8eb4SJonas Devlieghere 
31397fc8eb4SJonas Devlieghere       return error;
31497fc8eb4SJonas Devlieghere     }
31597fc8eb4SJonas Devlieghere 
31697fc8eb4SJonas Devlieghere     void OptionParsingStarting(ExecutionContext *execution_context) override {
31797fc8eb4SJonas Devlieghere       file.Clear();
31897fc8eb4SJonas Devlieghere       provider = eReproducerProviderNone;
31997fc8eb4SJonas Devlieghere     }
32097fc8eb4SJonas Devlieghere 
32197fc8eb4SJonas Devlieghere     ArrayRef<OptionDefinition> GetDefinitions() override {
32236eea5c3SJonas Devlieghere       return makeArrayRef(g_reproducer_dump_options);
32397fc8eb4SJonas Devlieghere     }
32497fc8eb4SJonas Devlieghere 
32597fc8eb4SJonas Devlieghere     FileSpec file;
32697fc8eb4SJonas Devlieghere     ReproducerProvider provider = eReproducerProviderNone;
32797fc8eb4SJonas Devlieghere   };
32897fc8eb4SJonas Devlieghere 
32997fc8eb4SJonas Devlieghere protected:
33097fc8eb4SJonas Devlieghere   bool DoExecute(Args &command, CommandReturnObject &result) override {
33197fc8eb4SJonas Devlieghere     if (!command.empty()) {
33297fc8eb4SJonas Devlieghere       result.AppendErrorWithFormat("'%s' takes no arguments",
33397fc8eb4SJonas Devlieghere                                    m_cmd_name.c_str());
33497fc8eb4SJonas Devlieghere       return false;
33597fc8eb4SJonas Devlieghere     }
33697fc8eb4SJonas Devlieghere 
33797fc8eb4SJonas Devlieghere     // If no reproducer path is specified, use the loader currently used for
33897fc8eb4SJonas Devlieghere     // replay. Otherwise create a new loader just for dumping.
33997fc8eb4SJonas Devlieghere     llvm::Optional<Loader> loader_storage;
34097fc8eb4SJonas Devlieghere     Loader *loader = nullptr;
34197fc8eb4SJonas Devlieghere     if (!m_options.file) {
34297fc8eb4SJonas Devlieghere       loader = Reproducer::Instance().GetLoader();
34397fc8eb4SJonas Devlieghere       if (loader == nullptr) {
34497fc8eb4SJonas Devlieghere         result.SetError(
34597fc8eb4SJonas Devlieghere             "Not specifying a reproducer is only support during replay.");
34697fc8eb4SJonas Devlieghere         result.SetStatus(eReturnStatusSuccessFinishNoResult);
34797fc8eb4SJonas Devlieghere         return false;
34897fc8eb4SJonas Devlieghere       }
34997fc8eb4SJonas Devlieghere     } else {
35097fc8eb4SJonas Devlieghere       loader_storage.emplace(m_options.file);
35197fc8eb4SJonas Devlieghere       loader = &(*loader_storage);
35297fc8eb4SJonas Devlieghere       if (Error err = loader->LoadIndex()) {
35397fc8eb4SJonas Devlieghere         SetError(result, std::move(err));
35497fc8eb4SJonas Devlieghere         return false;
35597fc8eb4SJonas Devlieghere       }
35697fc8eb4SJonas Devlieghere     }
35797fc8eb4SJonas Devlieghere 
35897fc8eb4SJonas Devlieghere     // If we get here we should have a valid loader.
35997fc8eb4SJonas Devlieghere     assert(loader);
36097fc8eb4SJonas Devlieghere 
36197fc8eb4SJonas Devlieghere     switch (m_options.provider) {
36297fc8eb4SJonas Devlieghere     case eReproducerProviderFiles: {
36397fc8eb4SJonas Devlieghere       FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
36497fc8eb4SJonas Devlieghere 
36597fc8eb4SJonas Devlieghere       // Read the VFS mapping.
36697fc8eb4SJonas Devlieghere       ErrorOr<std::unique_ptr<MemoryBuffer>> buffer =
36797fc8eb4SJonas Devlieghere           vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath());
36897fc8eb4SJonas Devlieghere       if (!buffer) {
36997fc8eb4SJonas Devlieghere         SetError(result, errorCodeToError(buffer.getError()));
37097fc8eb4SJonas Devlieghere         return false;
37197fc8eb4SJonas Devlieghere       }
37297fc8eb4SJonas Devlieghere 
37397fc8eb4SJonas Devlieghere       // Initialize a VFS from the given mapping.
37497fc8eb4SJonas Devlieghere       IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML(
37597fc8eb4SJonas Devlieghere           std::move(buffer.get()), nullptr, vfs_mapping.GetPath());
37697fc8eb4SJonas Devlieghere 
37797fc8eb4SJonas Devlieghere       // Dump the VFS to a buffer.
37897fc8eb4SJonas Devlieghere       std::string str;
37997fc8eb4SJonas Devlieghere       raw_string_ostream os(str);
38097fc8eb4SJonas Devlieghere       static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os);
38197fc8eb4SJonas Devlieghere       os.flush();
38297fc8eb4SJonas Devlieghere 
38397fc8eb4SJonas Devlieghere       // Return the string.
38497fc8eb4SJonas Devlieghere       result.AppendMessage(str);
38597fc8eb4SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishResult);
38697fc8eb4SJonas Devlieghere       return true;
38797fc8eb4SJonas Devlieghere     }
38897fc8eb4SJonas Devlieghere     case eReproducerProviderVersion: {
389b2575da9SJonas Devlieghere       Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
390b2575da9SJonas Devlieghere       if (!version) {
391b2575da9SJonas Devlieghere         SetError(result, version.takeError());
39297fc8eb4SJonas Devlieghere         return false;
39397fc8eb4SJonas Devlieghere       }
394b2575da9SJonas Devlieghere       result.AppendMessage(*version);
39597fc8eb4SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishResult);
39697fc8eb4SJonas Devlieghere       return true;
39797fc8eb4SJonas Devlieghere     }
398f4f12012SJonas Devlieghere     case eReproducerProviderWorkingDirectory: {
399f4f12012SJonas Devlieghere       Expected<std::string> cwd =
400f4f12012SJonas Devlieghere           loader->LoadBuffer<WorkingDirectoryProvider>();
401f4f12012SJonas Devlieghere       if (!cwd) {
402f4f12012SJonas Devlieghere         SetError(result, cwd.takeError());
403f4f12012SJonas Devlieghere         return false;
404f4f12012SJonas Devlieghere       }
405f4f12012SJonas Devlieghere       result.AppendMessage(*cwd);
406f4f12012SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishResult);
407f4f12012SJonas Devlieghere       return true;
408f4f12012SJonas Devlieghere     }
40997fc8eb4SJonas Devlieghere     case eReproducerProviderCommands: {
41097fc8eb4SJonas Devlieghere       // Create a new command loader.
41197fc8eb4SJonas Devlieghere       std::unique_ptr<repro::CommandLoader> command_loader =
41297fc8eb4SJonas Devlieghere           repro::CommandLoader::Create(loader);
41397fc8eb4SJonas Devlieghere       if (!command_loader) {
41497fc8eb4SJonas Devlieghere         SetError(result,
41597fc8eb4SJonas Devlieghere                  make_error<StringError>(llvm::inconvertibleErrorCode(),
41697fc8eb4SJonas Devlieghere                                          "Unable to create command loader."));
41797fc8eb4SJonas Devlieghere         return false;
41897fc8eb4SJonas Devlieghere       }
41997fc8eb4SJonas Devlieghere 
42097fc8eb4SJonas Devlieghere       // Iterate over the command files and dump them.
42197fc8eb4SJonas Devlieghere       while (true) {
42297fc8eb4SJonas Devlieghere         llvm::Optional<std::string> command_file =
42397fc8eb4SJonas Devlieghere             command_loader->GetNextFile();
42497fc8eb4SJonas Devlieghere         if (!command_file)
42597fc8eb4SJonas Devlieghere           break;
42697fc8eb4SJonas Devlieghere 
42797fc8eb4SJonas Devlieghere         auto command_buffer = llvm::MemoryBuffer::getFile(*command_file);
42897fc8eb4SJonas Devlieghere         if (auto err = command_buffer.getError()) {
42997fc8eb4SJonas Devlieghere           SetError(result, errorCodeToError(err));
43097fc8eb4SJonas Devlieghere           return false;
43197fc8eb4SJonas Devlieghere         }
43297fc8eb4SJonas Devlieghere         result.AppendMessage((*command_buffer)->getBuffer());
43397fc8eb4SJonas Devlieghere       }
43497fc8eb4SJonas Devlieghere 
43597fc8eb4SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishResult);
43697fc8eb4SJonas Devlieghere       return true;
43797fc8eb4SJonas Devlieghere     }
43897fc8eb4SJonas Devlieghere     case eReproducerProviderGDB: {
4398fc8d3feSJonas Devlieghere       FileSpec gdb_file = loader->GetFile<ProcessGDBRemoteProvider::Info>();
4408fc8d3feSJonas Devlieghere       auto error_or_file = MemoryBuffer::getFile(gdb_file.GetPath());
4418fc8d3feSJonas Devlieghere       if (auto err = error_or_file.getError()) {
4428fc8d3feSJonas Devlieghere         SetError(result, errorCodeToError(err));
4438fc8d3feSJonas Devlieghere         return false;
4448fc8d3feSJonas Devlieghere       }
4458fc8d3feSJonas Devlieghere 
4468fc8d3feSJonas Devlieghere       std::vector<GDBRemotePacket> packets;
4478fc8d3feSJonas Devlieghere       yaml::Input yin((*error_or_file)->getBuffer());
4488fc8d3feSJonas Devlieghere       yin >> packets;
4498fc8d3feSJonas Devlieghere 
4508fc8d3feSJonas Devlieghere       if (auto err = yin.error()) {
4518fc8d3feSJonas Devlieghere         SetError(result, errorCodeToError(err));
4528fc8d3feSJonas Devlieghere         return false;
4538fc8d3feSJonas Devlieghere       }
4548fc8d3feSJonas Devlieghere 
4558fc8d3feSJonas Devlieghere       for (GDBRemotePacket &packet : packets) {
4568fc8d3feSJonas Devlieghere         packet.Dump(result.GetOutputStream());
4578fc8d3feSJonas Devlieghere       }
4588fc8d3feSJonas Devlieghere 
45997fc8eb4SJonas Devlieghere       result.SetStatus(eReturnStatusSuccessFinishResult);
46097fc8eb4SJonas Devlieghere       return true;
46197fc8eb4SJonas Devlieghere     }
46297fc8eb4SJonas Devlieghere     case eReproducerProviderNone:
46397fc8eb4SJonas Devlieghere       result.SetError("No valid provider specified.");
46497fc8eb4SJonas Devlieghere       return false;
46597fc8eb4SJonas Devlieghere     }
46697fc8eb4SJonas Devlieghere 
46797fc8eb4SJonas Devlieghere     result.SetStatus(eReturnStatusSuccessFinishNoResult);
46897fc8eb4SJonas Devlieghere     return result.Succeeded();
46997fc8eb4SJonas Devlieghere   }
47097fc8eb4SJonas Devlieghere 
47197fc8eb4SJonas Devlieghere private:
47297fc8eb4SJonas Devlieghere   CommandOptions m_options;
47397fc8eb4SJonas Devlieghere };
47497fc8eb4SJonas Devlieghere 
4759e046f02SJonas Devlieghere CommandObjectReproducer::CommandObjectReproducer(
4769e046f02SJonas Devlieghere     CommandInterpreter &interpreter)
477973d66eeSJonas Devlieghere     : CommandObjectMultiword(
478973d66eeSJonas Devlieghere           interpreter, "reproducer",
479c8dfe907SJonas Devlieghere           "Commands for manipulating reproducers. Reproducers make it "
480c8dfe907SJonas Devlieghere           "possible "
48164b7d955SJonas Devlieghere           "to capture full debug sessions with all its dependencies. The "
48264b7d955SJonas Devlieghere           "resulting reproducer is used to replay the debug session while "
48364b7d955SJonas Devlieghere           "debugging the debugger.\n"
48464b7d955SJonas Devlieghere           "Because reproducers need the whole the debug session from "
48564b7d955SJonas Devlieghere           "beginning to end, you need to launch the debugger in capture or "
48664b7d955SJonas Devlieghere           "replay mode, commonly though the command line driver.\n"
48764b7d955SJonas Devlieghere           "Reproducers are unrelated record-replay debugging, as you cannot "
48864b7d955SJonas Devlieghere           "interact with the debugger during replay.\n",
489130ec068SJonas Devlieghere           "reproducer <subcommand> [<subcommand-options>]") {
4909e046f02SJonas Devlieghere   LoadSubCommand(
4919e046f02SJonas Devlieghere       "generate",
4929e046f02SJonas Devlieghere       CommandObjectSP(new CommandObjectReproducerGenerate(interpreter)));
49315eacd74SJonas Devlieghere   LoadSubCommand("status", CommandObjectSP(
49415eacd74SJonas Devlieghere                                new CommandObjectReproducerStatus(interpreter)));
49597fc8eb4SJonas Devlieghere   LoadSubCommand("dump",
49697fc8eb4SJonas Devlieghere                  CommandObjectSP(new CommandObjectReproducerDump(interpreter)));
497c8dfe907SJonas Devlieghere   LoadSubCommand("xcrash", CommandObjectSP(
498c8dfe907SJonas Devlieghere                                new CommandObjectReproducerXCrash(interpreter)));
4999e046f02SJonas Devlieghere }
5009e046f02SJonas Devlieghere 
5019e046f02SJonas Devlieghere CommandObjectReproducer::~CommandObjectReproducer() = default;
502