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, 30a842950bSJonas 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 { 51a842950bSJonas Devlieghere eReproducerProviderSymbolFiles, 52a842950bSJonas Devlieghere "symbol-files", 53a842950bSJonas Devlieghere "Symbol Files", 54a842950bSJonas Devlieghere }, 55a842950bSJonas 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 11937469061SJonas Devlieghere #define LLDB_OPTIONS_reproducer_verify 12037469061SJonas Devlieghere #include "CommandOptions.inc" 12137469061SJonas Devlieghere 1222451cbf0SJonas Devlieghere template <typename T> 1232451cbf0SJonas Devlieghere llvm::Expected<T> static ReadFromYAML(StringRef filename) { 1242451cbf0SJonas Devlieghere auto error_or_file = MemoryBuffer::getFile(filename); 1252451cbf0SJonas Devlieghere if (auto err = error_or_file.getError()) { 1262451cbf0SJonas Devlieghere return errorCodeToError(err); 1272451cbf0SJonas Devlieghere } 1282451cbf0SJonas Devlieghere 1292451cbf0SJonas Devlieghere T t; 1302451cbf0SJonas Devlieghere yaml::Input yin((*error_or_file)->getBuffer()); 1312451cbf0SJonas Devlieghere yin >> t; 1322451cbf0SJonas Devlieghere 1332451cbf0SJonas Devlieghere if (auto err = yin.error()) { 1342451cbf0SJonas Devlieghere return errorCodeToError(err); 1352451cbf0SJonas Devlieghere } 1362451cbf0SJonas Devlieghere 1372451cbf0SJonas Devlieghere return t; 1382451cbf0SJonas Devlieghere } 1392451cbf0SJonas Devlieghere 14037469061SJonas Devlieghere static void SetError(CommandReturnObject &result, Error err) { 14137469061SJonas Devlieghere result.GetErrorStream().Printf("error: %s\n", 14237469061SJonas Devlieghere toString(std::move(err)).c_str()); 14337469061SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 14437469061SJonas Devlieghere } 14537469061SJonas Devlieghere 14637469061SJonas Devlieghere /// Create a loader from the given path if specified. Otherwise use the current 14737469061SJonas Devlieghere /// loader used for replay. 14837469061SJonas Devlieghere static Loader * 14937469061SJonas Devlieghere GetLoaderFromPathOrCurrent(llvm::Optional<Loader> &loader_storage, 15037469061SJonas Devlieghere CommandReturnObject &result, 15137469061SJonas Devlieghere FileSpec reproducer_path) { 15237469061SJonas Devlieghere if (reproducer_path) { 15337469061SJonas Devlieghere loader_storage.emplace(reproducer_path); 15437469061SJonas Devlieghere Loader *loader = &(*loader_storage); 15537469061SJonas Devlieghere if (Error err = loader->LoadIndex()) { 15637469061SJonas Devlieghere // This is a hard error and will set the result to eReturnStatusFailed. 15737469061SJonas Devlieghere SetError(result, std::move(err)); 15837469061SJonas Devlieghere return nullptr; 15937469061SJonas Devlieghere } 16037469061SJonas Devlieghere return loader; 16137469061SJonas Devlieghere } 16237469061SJonas Devlieghere 16337469061SJonas Devlieghere if (Loader *loader = Reproducer::Instance().GetLoader()) 16437469061SJonas Devlieghere return loader; 16537469061SJonas Devlieghere 16637469061SJonas Devlieghere // This is a soft error because this is expected to fail during capture. 16737469061SJonas Devlieghere result.SetError("Not specifying a reproducer is only support during replay."); 16837469061SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 16937469061SJonas Devlieghere return nullptr; 17037469061SJonas Devlieghere } 17137469061SJonas Devlieghere 1729e046f02SJonas Devlieghere class CommandObjectReproducerGenerate : public CommandObjectParsed { 1739e046f02SJonas Devlieghere public: 1749e046f02SJonas Devlieghere CommandObjectReproducerGenerate(CommandInterpreter &interpreter) 175973d66eeSJonas Devlieghere : CommandObjectParsed( 176973d66eeSJonas Devlieghere interpreter, "reproducer generate", 177973d66eeSJonas Devlieghere "Generate reproducer on disk. When the debugger is in capture " 178973d66eeSJonas Devlieghere "mode, this command will output the reproducer to a directory on " 1790cf86da1SJonas Devlieghere "disk and quit. In replay mode this command in a no-op.", 180973d66eeSJonas Devlieghere nullptr) {} 1819e046f02SJonas Devlieghere 1829e046f02SJonas Devlieghere ~CommandObjectReproducerGenerate() override = default; 1839e046f02SJonas Devlieghere 1849e046f02SJonas Devlieghere protected: 1859e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 1869e046f02SJonas Devlieghere if (!command.empty()) { 1879e046f02SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 1889e046f02SJonas Devlieghere m_cmd_name.c_str()); 1899e046f02SJonas Devlieghere return false; 1909e046f02SJonas Devlieghere } 1919e046f02SJonas Devlieghere 19297fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 1939e046f02SJonas Devlieghere if (auto generator = r.GetGenerator()) { 1949e046f02SJonas Devlieghere generator->Keep(); 195*73811d32SJonas Devlieghere if (llvm::Error e = repro::Finalize(r.GetReproducerPath())) { 196*73811d32SJonas Devlieghere SetError(result, std::move(e)); 197*73811d32SJonas Devlieghere return result.Succeeded(); 198*73811d32SJonas Devlieghere } 199865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 200bb090bb1SJonas Devlieghere // Make this operation a NO-OP in replay mode. 2012dca6538SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 2022dca6538SJonas Devlieghere return result.Succeeded(); 2039e046f02SJonas Devlieghere } else { 2049e046f02SJonas Devlieghere result.AppendErrorWithFormat("Unable to get the reproducer generator"); 2052dca6538SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 2069e046f02SJonas Devlieghere return false; 2079e046f02SJonas Devlieghere } 2089e046f02SJonas Devlieghere 2099e046f02SJonas Devlieghere result.GetOutputStream() 2109e046f02SJonas Devlieghere << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; 2111c5250abSJonas Devlieghere result.GetOutputStream() 2121c5250abSJonas Devlieghere << "Please have a look at the directory to assess if you're willing to " 2131c5250abSJonas Devlieghere "share the contained information.\n"; 2149e046f02SJonas Devlieghere 2150cf86da1SJonas Devlieghere m_interpreter.BroadcastEvent( 2160cf86da1SJonas Devlieghere CommandInterpreter::eBroadcastBitQuitCommandReceived); 2170cf86da1SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 2189e046f02SJonas Devlieghere return result.Succeeded(); 2199e046f02SJonas Devlieghere } 2209e046f02SJonas Devlieghere }; 2219e046f02SJonas Devlieghere 222c8dfe907SJonas Devlieghere class CommandObjectReproducerXCrash : public CommandObjectParsed { 223c8dfe907SJonas Devlieghere public: 224c8dfe907SJonas Devlieghere CommandObjectReproducerXCrash(CommandInterpreter &interpreter) 225c8dfe907SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer xcrash", 226c8dfe907SJonas Devlieghere "Intentionally force the debugger to crash in " 227c8dfe907SJonas Devlieghere "order to trigger and test reproducer generation.", 228c8dfe907SJonas Devlieghere nullptr) {} 229c8dfe907SJonas Devlieghere 230c8dfe907SJonas Devlieghere ~CommandObjectReproducerXCrash() override = default; 231c8dfe907SJonas Devlieghere 232c8dfe907SJonas Devlieghere Options *GetOptions() override { return &m_options; } 233c8dfe907SJonas Devlieghere 234c8dfe907SJonas Devlieghere class CommandOptions : public Options { 235c8dfe907SJonas Devlieghere public: 236c8dfe907SJonas Devlieghere CommandOptions() : Options() {} 237c8dfe907SJonas Devlieghere 238c8dfe907SJonas Devlieghere ~CommandOptions() override = default; 239c8dfe907SJonas Devlieghere 240c8dfe907SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 241c8dfe907SJonas Devlieghere ExecutionContext *execution_context) override { 242c8dfe907SJonas Devlieghere Status error; 243c8dfe907SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 244c8dfe907SJonas Devlieghere 245c8dfe907SJonas Devlieghere switch (short_option) { 246c8dfe907SJonas Devlieghere case 's': 247c8dfe907SJonas Devlieghere signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum( 248c8dfe907SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 249c8dfe907SJonas Devlieghere if (!error.Success()) 250c8dfe907SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for signal '%s'", 251c8dfe907SJonas Devlieghere option_arg.str().c_str()); 252c8dfe907SJonas Devlieghere break; 253c8dfe907SJonas Devlieghere default: 254c8dfe907SJonas Devlieghere llvm_unreachable("Unimplemented option"); 255c8dfe907SJonas Devlieghere } 256c8dfe907SJonas Devlieghere 257c8dfe907SJonas Devlieghere return error; 258c8dfe907SJonas Devlieghere } 259c8dfe907SJonas Devlieghere 260c8dfe907SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 261c8dfe907SJonas Devlieghere signal = eReproducerCrashSigsegv; 262c8dfe907SJonas Devlieghere } 263c8dfe907SJonas Devlieghere 264c8dfe907SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 265c8dfe907SJonas Devlieghere return makeArrayRef(g_reproducer_xcrash_options); 266c8dfe907SJonas Devlieghere } 267c8dfe907SJonas Devlieghere 268c8dfe907SJonas Devlieghere ReproducerCrashSignal signal = eReproducerCrashSigsegv; 269c8dfe907SJonas Devlieghere }; 270c8dfe907SJonas Devlieghere 271c8dfe907SJonas Devlieghere protected: 272c8dfe907SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 273c8dfe907SJonas Devlieghere if (!command.empty()) { 274c8dfe907SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 275c8dfe907SJonas Devlieghere m_cmd_name.c_str()); 276c8dfe907SJonas Devlieghere return false; 277c8dfe907SJonas Devlieghere } 278c8dfe907SJonas Devlieghere 279c8dfe907SJonas Devlieghere auto &r = Reproducer::Instance(); 280bb090bb1SJonas Devlieghere 281bb090bb1SJonas Devlieghere if (!r.IsCapturing() && !r.IsReplaying()) { 282c8dfe907SJonas Devlieghere result.SetError( 283c8dfe907SJonas Devlieghere "forcing a crash is only supported when capturing a reproducer."); 284c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 285c8dfe907SJonas Devlieghere return false; 286c8dfe907SJonas Devlieghere } 287c8dfe907SJonas Devlieghere 288c8dfe907SJonas Devlieghere switch (m_options.signal) { 289c8dfe907SJonas Devlieghere case eReproducerCrashSigill: 290c8dfe907SJonas Devlieghere std::raise(SIGILL); 291c8dfe907SJonas Devlieghere break; 292c8dfe907SJonas Devlieghere case eReproducerCrashSigsegv: 293c8dfe907SJonas Devlieghere std::raise(SIGSEGV); 294c8dfe907SJonas Devlieghere break; 295c8dfe907SJonas Devlieghere } 296c8dfe907SJonas Devlieghere 297c8dfe907SJonas Devlieghere result.SetStatus(eReturnStatusQuit); 298c8dfe907SJonas Devlieghere return result.Succeeded(); 299c8dfe907SJonas Devlieghere } 300c8dfe907SJonas Devlieghere 301c8dfe907SJonas Devlieghere private: 302c8dfe907SJonas Devlieghere CommandOptions m_options; 303c8dfe907SJonas Devlieghere }; 304c8dfe907SJonas Devlieghere 30515eacd74SJonas Devlieghere class CommandObjectReproducerStatus : public CommandObjectParsed { 3069e046f02SJonas Devlieghere public: 30715eacd74SJonas Devlieghere CommandObjectReproducerStatus(CommandInterpreter &interpreter) 308973d66eeSJonas Devlieghere : CommandObjectParsed( 309973d66eeSJonas Devlieghere interpreter, "reproducer status", 310c8dfe907SJonas Devlieghere "Show the current reproducer status. In capture mode the " 311c8dfe907SJonas Devlieghere "debugger " 312973d66eeSJonas Devlieghere "is collecting all the information it needs to create a " 313973d66eeSJonas Devlieghere "reproducer. In replay mode the reproducer is replaying a " 314973d66eeSJonas Devlieghere "reproducer. When the reproducers are off, no data is collected " 315973d66eeSJonas Devlieghere "and no reproducer can be generated.", 316973d66eeSJonas Devlieghere nullptr) {} 3179e046f02SJonas Devlieghere 31815eacd74SJonas Devlieghere ~CommandObjectReproducerStatus() override = default; 3199e046f02SJonas Devlieghere 3209e046f02SJonas Devlieghere protected: 3219e046f02SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 32215eacd74SJonas Devlieghere if (!command.empty()) { 32315eacd74SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 3249e046f02SJonas Devlieghere m_cmd_name.c_str()); 3259e046f02SJonas Devlieghere return false; 3269e046f02SJonas Devlieghere } 3279e046f02SJonas Devlieghere 32897fc8eb4SJonas Devlieghere auto &r = Reproducer::Instance(); 329865cd093SJonas Devlieghere if (r.IsCapturing()) { 33015eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in capture mode.\n"; 331865cd093SJonas Devlieghere } else if (r.IsReplaying()) { 33215eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is in replay mode.\n"; 33315eacd74SJonas Devlieghere } else { 33415eacd74SJonas Devlieghere result.GetOutputStream() << "Reproducer is off.\n"; 3359e046f02SJonas Devlieghere } 3369e046f02SJonas Devlieghere 337982a77b6SJonas Devlieghere if (r.IsCapturing() || r.IsReplaying()) { 338982a77b6SJonas Devlieghere result.GetOutputStream() 339982a77b6SJonas Devlieghere << "Path: " << r.GetReproducerPath().GetPath() << '\n'; 340982a77b6SJonas Devlieghere } 341982a77b6SJonas Devlieghere 342982a77b6SJonas Devlieghere // Auto generate is hidden unless enabled because this is mostly for 343982a77b6SJonas Devlieghere // development and testing. 344982a77b6SJonas Devlieghere if (Generator *g = r.GetGenerator()) { 345982a77b6SJonas Devlieghere if (g->IsAutoGenerate()) 346982a77b6SJonas Devlieghere result.GetOutputStream() << "Auto generate: on\n"; 347982a77b6SJonas Devlieghere } 348982a77b6SJonas Devlieghere 34915eacd74SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 3509e046f02SJonas Devlieghere return result.Succeeded(); 3519e046f02SJonas Devlieghere } 3529e046f02SJonas Devlieghere }; 3539e046f02SJonas Devlieghere 35497fc8eb4SJonas Devlieghere class CommandObjectReproducerDump : public CommandObjectParsed { 35597fc8eb4SJonas Devlieghere public: 35697fc8eb4SJonas Devlieghere CommandObjectReproducerDump(CommandInterpreter &interpreter) 35797fc8eb4SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer dump", 35864b7d955SJonas Devlieghere "Dump the information contained in a reproducer. " 35964b7d955SJonas Devlieghere "If no reproducer is specified during replay, it " 36064b7d955SJonas Devlieghere "dumps the content of the current reproducer.", 36197fc8eb4SJonas Devlieghere nullptr) {} 36297fc8eb4SJonas Devlieghere 36397fc8eb4SJonas Devlieghere ~CommandObjectReproducerDump() override = default; 36497fc8eb4SJonas Devlieghere 36597fc8eb4SJonas Devlieghere Options *GetOptions() override { return &m_options; } 36697fc8eb4SJonas Devlieghere 36797fc8eb4SJonas Devlieghere class CommandOptions : public Options { 36897fc8eb4SJonas Devlieghere public: 36997fc8eb4SJonas Devlieghere CommandOptions() : Options(), file() {} 37097fc8eb4SJonas Devlieghere 37197fc8eb4SJonas Devlieghere ~CommandOptions() override = default; 37297fc8eb4SJonas Devlieghere 37397fc8eb4SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 37497fc8eb4SJonas Devlieghere ExecutionContext *execution_context) override { 37597fc8eb4SJonas Devlieghere Status error; 37697fc8eb4SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 37797fc8eb4SJonas Devlieghere 37897fc8eb4SJonas Devlieghere switch (short_option) { 37997fc8eb4SJonas Devlieghere case 'f': 38097fc8eb4SJonas Devlieghere file.SetFile(option_arg, FileSpec::Style::native); 38197fc8eb4SJonas Devlieghere FileSystem::Instance().Resolve(file); 38297fc8eb4SJonas Devlieghere break; 38397fc8eb4SJonas Devlieghere case 'p': 38497fc8eb4SJonas Devlieghere provider = (ReproducerProvider)OptionArgParser::ToOptionEnum( 38597fc8eb4SJonas Devlieghere option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 38697fc8eb4SJonas Devlieghere if (!error.Success()) 38797fc8eb4SJonas Devlieghere error.SetErrorStringWithFormat("unrecognized value for provider '%s'", 38897fc8eb4SJonas Devlieghere option_arg.str().c_str()); 38997fc8eb4SJonas Devlieghere break; 39097fc8eb4SJonas Devlieghere default: 39197fc8eb4SJonas Devlieghere llvm_unreachable("Unimplemented option"); 39297fc8eb4SJonas Devlieghere } 39397fc8eb4SJonas Devlieghere 39497fc8eb4SJonas Devlieghere return error; 39597fc8eb4SJonas Devlieghere } 39697fc8eb4SJonas Devlieghere 39797fc8eb4SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 39897fc8eb4SJonas Devlieghere file.Clear(); 39997fc8eb4SJonas Devlieghere provider = eReproducerProviderNone; 40097fc8eb4SJonas Devlieghere } 40197fc8eb4SJonas Devlieghere 40297fc8eb4SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 40336eea5c3SJonas Devlieghere return makeArrayRef(g_reproducer_dump_options); 40497fc8eb4SJonas Devlieghere } 40597fc8eb4SJonas Devlieghere 40697fc8eb4SJonas Devlieghere FileSpec file; 40797fc8eb4SJonas Devlieghere ReproducerProvider provider = eReproducerProviderNone; 40897fc8eb4SJonas Devlieghere }; 40997fc8eb4SJonas Devlieghere 41097fc8eb4SJonas Devlieghere protected: 41197fc8eb4SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 41297fc8eb4SJonas Devlieghere if (!command.empty()) { 41397fc8eb4SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 41497fc8eb4SJonas Devlieghere m_cmd_name.c_str()); 41597fc8eb4SJonas Devlieghere return false; 41697fc8eb4SJonas Devlieghere } 41797fc8eb4SJonas Devlieghere 41897fc8eb4SJonas Devlieghere llvm::Optional<Loader> loader_storage; 41937469061SJonas Devlieghere Loader *loader = 42037469061SJonas Devlieghere GetLoaderFromPathOrCurrent(loader_storage, result, m_options.file); 42137469061SJonas Devlieghere if (!loader) 42297fc8eb4SJonas Devlieghere return false; 42397fc8eb4SJonas Devlieghere 42497fc8eb4SJonas Devlieghere switch (m_options.provider) { 42597fc8eb4SJonas Devlieghere case eReproducerProviderFiles: { 42697fc8eb4SJonas Devlieghere FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); 42797fc8eb4SJonas Devlieghere 42897fc8eb4SJonas Devlieghere // Read the VFS mapping. 42997fc8eb4SJonas Devlieghere ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = 43097fc8eb4SJonas Devlieghere vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath()); 43197fc8eb4SJonas Devlieghere if (!buffer) { 43297fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(buffer.getError())); 43397fc8eb4SJonas Devlieghere return false; 43497fc8eb4SJonas Devlieghere } 43597fc8eb4SJonas Devlieghere 43697fc8eb4SJonas Devlieghere // Initialize a VFS from the given mapping. 43797fc8eb4SJonas Devlieghere IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML( 43897fc8eb4SJonas Devlieghere std::move(buffer.get()), nullptr, vfs_mapping.GetPath()); 43997fc8eb4SJonas Devlieghere 44097fc8eb4SJonas Devlieghere // Dump the VFS to a buffer. 44197fc8eb4SJonas Devlieghere std::string str; 44297fc8eb4SJonas Devlieghere raw_string_ostream os(str); 44397fc8eb4SJonas Devlieghere static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os); 44497fc8eb4SJonas Devlieghere os.flush(); 44597fc8eb4SJonas Devlieghere 44697fc8eb4SJonas Devlieghere // Return the string. 44797fc8eb4SJonas Devlieghere result.AppendMessage(str); 44897fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 44997fc8eb4SJonas Devlieghere return true; 45097fc8eb4SJonas Devlieghere } 451a842950bSJonas Devlieghere case eReproducerProviderSymbolFiles: { 452a842950bSJonas Devlieghere Expected<std::string> symbol_files = 453a842950bSJonas Devlieghere loader->LoadBuffer<SymbolFileProvider>(); 454a842950bSJonas Devlieghere if (!symbol_files) { 455a842950bSJonas Devlieghere SetError(result, symbol_files.takeError()); 456a842950bSJonas Devlieghere return false; 457a842950bSJonas Devlieghere } 458a842950bSJonas Devlieghere 459a842950bSJonas Devlieghere std::vector<SymbolFileProvider::Entry> entries; 460a842950bSJonas Devlieghere llvm::yaml::Input yin(*symbol_files); 461a842950bSJonas Devlieghere yin >> entries; 462a842950bSJonas Devlieghere 463a842950bSJonas Devlieghere for (const auto &entry : entries) { 464a842950bSJonas Devlieghere result.AppendMessageWithFormat("- uuid: %s\n", 465a842950bSJonas Devlieghere entry.uuid.c_str()); 466a842950bSJonas Devlieghere result.AppendMessageWithFormat(" module path: %s\n", 467a842950bSJonas Devlieghere entry.module_path.c_str()); 468a842950bSJonas Devlieghere result.AppendMessageWithFormat(" symbol path: %s\n", 469a842950bSJonas Devlieghere entry.symbol_path.c_str()); 470a842950bSJonas Devlieghere } 471a842950bSJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 472a842950bSJonas Devlieghere return true; 473a842950bSJonas Devlieghere } 47497fc8eb4SJonas Devlieghere case eReproducerProviderVersion: { 475b2575da9SJonas Devlieghere Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); 476b2575da9SJonas Devlieghere if (!version) { 477b2575da9SJonas Devlieghere SetError(result, version.takeError()); 47897fc8eb4SJonas Devlieghere return false; 47997fc8eb4SJonas Devlieghere } 480b2575da9SJonas Devlieghere result.AppendMessage(*version); 48197fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 48297fc8eb4SJonas Devlieghere return true; 48397fc8eb4SJonas Devlieghere } 484f4f12012SJonas Devlieghere case eReproducerProviderWorkingDirectory: { 485f4f12012SJonas Devlieghere Expected<std::string> cwd = 48673af341bSJonas Devlieghere repro::GetDirectoryFrom<WorkingDirectoryProvider>(loader); 487f4f12012SJonas Devlieghere if (!cwd) { 488f4f12012SJonas Devlieghere SetError(result, cwd.takeError()); 489f4f12012SJonas Devlieghere return false; 490f4f12012SJonas Devlieghere } 491f4f12012SJonas Devlieghere result.AppendMessage(*cwd); 492f4f12012SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 493f4f12012SJonas Devlieghere return true; 494f4f12012SJonas Devlieghere } 49573af341bSJonas Devlieghere case eReproducerProviderHomeDirectory: { 49673af341bSJonas Devlieghere Expected<std::string> home = 49773af341bSJonas Devlieghere repro::GetDirectoryFrom<HomeDirectoryProvider>(loader); 49873af341bSJonas Devlieghere if (!home) { 49973af341bSJonas Devlieghere SetError(result, home.takeError()); 50073af341bSJonas Devlieghere return false; 50173af341bSJonas Devlieghere } 50273af341bSJonas Devlieghere result.AppendMessage(*home); 50373af341bSJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 50473af341bSJonas Devlieghere return true; 50573af341bSJonas Devlieghere } 50697fc8eb4SJonas Devlieghere case eReproducerProviderCommands: { 5071d41d1bcSEric Christopher std::unique_ptr<repro::MultiLoader<repro::CommandProvider>> multi_loader = 5081d41d1bcSEric Christopher repro::MultiLoader<repro::CommandProvider>::Create(loader); 5091d41d1bcSEric Christopher if (!multi_loader) { 51097fc8eb4SJonas Devlieghere SetError(result, 5114016c6b0SJonas Devlieghere make_error<StringError>("Unable to create command loader.", 5124016c6b0SJonas Devlieghere llvm::inconvertibleErrorCode())); 51397fc8eb4SJonas Devlieghere return false; 51497fc8eb4SJonas Devlieghere } 51597fc8eb4SJonas Devlieghere 51697fc8eb4SJonas Devlieghere // Iterate over the command files and dump them. 5171d41d1bcSEric Christopher llvm::Optional<std::string> command_file; 5181d41d1bcSEric Christopher while ((command_file = multi_loader->GetNextFile())) { 51997fc8eb4SJonas Devlieghere if (!command_file) 52097fc8eb4SJonas Devlieghere break; 52197fc8eb4SJonas Devlieghere 52297fc8eb4SJonas Devlieghere auto command_buffer = llvm::MemoryBuffer::getFile(*command_file); 52397fc8eb4SJonas Devlieghere if (auto err = command_buffer.getError()) { 52497fc8eb4SJonas Devlieghere SetError(result, errorCodeToError(err)); 52597fc8eb4SJonas Devlieghere return false; 52697fc8eb4SJonas Devlieghere } 52797fc8eb4SJonas Devlieghere result.AppendMessage((*command_buffer)->getBuffer()); 52897fc8eb4SJonas Devlieghere } 52997fc8eb4SJonas Devlieghere 53097fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 53197fc8eb4SJonas Devlieghere return true; 53297fc8eb4SJonas Devlieghere } 53397fc8eb4SJonas Devlieghere case eReproducerProviderGDB: { 5341d41d1bcSEric Christopher std::unique_ptr<repro::MultiLoader<repro::GDBRemoteProvider>> 5351d41d1bcSEric Christopher multi_loader = 5361d41d1bcSEric Christopher repro::MultiLoader<repro::GDBRemoteProvider>::Create(loader); 5374016c6b0SJonas Devlieghere 5384016c6b0SJonas Devlieghere if (!multi_loader) { 5394016c6b0SJonas Devlieghere SetError(result, 5404016c6b0SJonas Devlieghere make_error<StringError>("Unable to create GDB loader.", 5414016c6b0SJonas Devlieghere llvm::inconvertibleErrorCode())); 5424016c6b0SJonas Devlieghere return false; 5434016c6b0SJonas Devlieghere } 5444016c6b0SJonas Devlieghere 5451d41d1bcSEric Christopher llvm::Optional<std::string> gdb_file; 5461d41d1bcSEric Christopher while ((gdb_file = multi_loader->GetNextFile())) { 5472451cbf0SJonas Devlieghere if (llvm::Expected<std::vector<GDBRemotePacket>> packets = 5482451cbf0SJonas Devlieghere ReadFromYAML<std::vector<GDBRemotePacket>>(*gdb_file)) { 5492451cbf0SJonas Devlieghere for (GDBRemotePacket &packet : *packets) { 5508fc8d3feSJonas Devlieghere packet.Dump(result.GetOutputStream()); 5518fc8d3feSJonas Devlieghere } 5522451cbf0SJonas Devlieghere } else { 5532451cbf0SJonas Devlieghere SetError(result, packets.takeError()); 5542451cbf0SJonas Devlieghere return false; 5552451cbf0SJonas Devlieghere } 5562451cbf0SJonas Devlieghere } 5572451cbf0SJonas Devlieghere 5582451cbf0SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 5592451cbf0SJonas Devlieghere return true; 5602451cbf0SJonas Devlieghere } 5612451cbf0SJonas Devlieghere case eReproducerProviderProcessInfo: { 5622451cbf0SJonas Devlieghere std::unique_ptr<repro::MultiLoader<repro::ProcessInfoProvider>> 5632451cbf0SJonas Devlieghere multi_loader = 5642451cbf0SJonas Devlieghere repro::MultiLoader<repro::ProcessInfoProvider>::Create(loader); 5652451cbf0SJonas Devlieghere 5662451cbf0SJonas Devlieghere if (!multi_loader) { 5672451cbf0SJonas Devlieghere SetError(result, make_error<StringError>( 5682451cbf0SJonas Devlieghere llvm::inconvertibleErrorCode(), 5692451cbf0SJonas Devlieghere "Unable to create process info loader.")); 5702451cbf0SJonas Devlieghere return false; 5712451cbf0SJonas Devlieghere } 5722451cbf0SJonas Devlieghere 5732451cbf0SJonas Devlieghere llvm::Optional<std::string> process_file; 5742451cbf0SJonas Devlieghere while ((process_file = multi_loader->GetNextFile())) { 5752451cbf0SJonas Devlieghere if (llvm::Expected<ProcessInstanceInfoList> infos = 5762451cbf0SJonas Devlieghere ReadFromYAML<ProcessInstanceInfoList>(*process_file)) { 5772451cbf0SJonas Devlieghere for (ProcessInstanceInfo info : *infos) 5782451cbf0SJonas Devlieghere info.Dump(result.GetOutputStream(), HostInfo::GetUserIDResolver()); 5792451cbf0SJonas Devlieghere } else { 5802451cbf0SJonas Devlieghere SetError(result, infos.takeError()); 5812451cbf0SJonas Devlieghere return false; 5822451cbf0SJonas Devlieghere } 5831d41d1bcSEric Christopher } 5848fc8d3feSJonas Devlieghere 58597fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 58697fc8eb4SJonas Devlieghere return true; 58797fc8eb4SJonas Devlieghere } 58897fc8eb4SJonas Devlieghere case eReproducerProviderNone: 58997fc8eb4SJonas Devlieghere result.SetError("No valid provider specified."); 59097fc8eb4SJonas Devlieghere return false; 59197fc8eb4SJonas Devlieghere } 59297fc8eb4SJonas Devlieghere 59397fc8eb4SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishNoResult); 59497fc8eb4SJonas Devlieghere return result.Succeeded(); 59597fc8eb4SJonas Devlieghere } 59697fc8eb4SJonas Devlieghere 59797fc8eb4SJonas Devlieghere private: 59897fc8eb4SJonas Devlieghere CommandOptions m_options; 59997fc8eb4SJonas Devlieghere }; 60097fc8eb4SJonas Devlieghere 60137469061SJonas Devlieghere class CommandObjectReproducerVerify : public CommandObjectParsed { 60237469061SJonas Devlieghere public: 60337469061SJonas Devlieghere CommandObjectReproducerVerify(CommandInterpreter &interpreter) 60437469061SJonas Devlieghere : CommandObjectParsed(interpreter, "reproducer verify", 60537469061SJonas Devlieghere "Verify the contents of a reproducer. " 60637469061SJonas Devlieghere "If no reproducer is specified during replay, it " 60737469061SJonas Devlieghere "verifies the content of the current reproducer.", 60837469061SJonas Devlieghere nullptr) {} 60937469061SJonas Devlieghere 61037469061SJonas Devlieghere ~CommandObjectReproducerVerify() override = default; 61137469061SJonas Devlieghere 61237469061SJonas Devlieghere Options *GetOptions() override { return &m_options; } 61337469061SJonas Devlieghere 61437469061SJonas Devlieghere class CommandOptions : public Options { 61537469061SJonas Devlieghere public: 61637469061SJonas Devlieghere CommandOptions() : Options(), file() {} 61737469061SJonas Devlieghere 61837469061SJonas Devlieghere ~CommandOptions() override = default; 61937469061SJonas Devlieghere 62037469061SJonas Devlieghere Status SetOptionValue(uint32_t option_idx, StringRef option_arg, 62137469061SJonas Devlieghere ExecutionContext *execution_context) override { 62237469061SJonas Devlieghere Status error; 62337469061SJonas Devlieghere const int short_option = m_getopt_table[option_idx].val; 62437469061SJonas Devlieghere 62537469061SJonas Devlieghere switch (short_option) { 62637469061SJonas Devlieghere case 'f': 62737469061SJonas Devlieghere file.SetFile(option_arg, FileSpec::Style::native); 62837469061SJonas Devlieghere FileSystem::Instance().Resolve(file); 62937469061SJonas Devlieghere break; 63037469061SJonas Devlieghere default: 63137469061SJonas Devlieghere llvm_unreachable("Unimplemented option"); 63237469061SJonas Devlieghere } 63337469061SJonas Devlieghere 63437469061SJonas Devlieghere return error; 63537469061SJonas Devlieghere } 63637469061SJonas Devlieghere 63737469061SJonas Devlieghere void OptionParsingStarting(ExecutionContext *execution_context) override { 63837469061SJonas Devlieghere file.Clear(); 63937469061SJonas Devlieghere } 64037469061SJonas Devlieghere 64137469061SJonas Devlieghere ArrayRef<OptionDefinition> GetDefinitions() override { 64237469061SJonas Devlieghere return makeArrayRef(g_reproducer_verify_options); 64337469061SJonas Devlieghere } 64437469061SJonas Devlieghere 64537469061SJonas Devlieghere FileSpec file; 64637469061SJonas Devlieghere }; 64737469061SJonas Devlieghere 64837469061SJonas Devlieghere protected: 64937469061SJonas Devlieghere bool DoExecute(Args &command, CommandReturnObject &result) override { 65037469061SJonas Devlieghere if (!command.empty()) { 65137469061SJonas Devlieghere result.AppendErrorWithFormat("'%s' takes no arguments", 65237469061SJonas Devlieghere m_cmd_name.c_str()); 65337469061SJonas Devlieghere return false; 65437469061SJonas Devlieghere } 65537469061SJonas Devlieghere 65637469061SJonas Devlieghere llvm::Optional<Loader> loader_storage; 65737469061SJonas Devlieghere Loader *loader = 65837469061SJonas Devlieghere GetLoaderFromPathOrCurrent(loader_storage, result, m_options.file); 65937469061SJonas Devlieghere if (!loader) 66037469061SJonas Devlieghere return false; 66137469061SJonas Devlieghere 66237469061SJonas Devlieghere bool errors = false; 66337469061SJonas Devlieghere auto error_callback = [&](llvm::StringRef error) { 66437469061SJonas Devlieghere errors = true; 66537469061SJonas Devlieghere result.AppendError(error); 66637469061SJonas Devlieghere }; 66737469061SJonas Devlieghere 66837469061SJonas Devlieghere bool warnings = false; 66937469061SJonas Devlieghere auto warning_callback = [&](llvm::StringRef warning) { 67037469061SJonas Devlieghere warnings = true; 67137469061SJonas Devlieghere result.AppendWarning(warning); 67237469061SJonas Devlieghere }; 67337469061SJonas Devlieghere 67437469061SJonas Devlieghere auto note_callback = [&](llvm::StringRef warning) { 67537469061SJonas Devlieghere result.AppendMessage(warning); 67637469061SJonas Devlieghere }; 67737469061SJonas Devlieghere 67837469061SJonas Devlieghere Verifier verifier(loader); 67937469061SJonas Devlieghere verifier.Verify(error_callback, warning_callback, note_callback); 68037469061SJonas Devlieghere 68137469061SJonas Devlieghere if (warnings || errors) { 68237469061SJonas Devlieghere result.AppendMessage("reproducer verification failed"); 68337469061SJonas Devlieghere result.SetStatus(eReturnStatusFailed); 68437469061SJonas Devlieghere } else { 68537469061SJonas Devlieghere result.AppendMessage("reproducer verification succeeded"); 68637469061SJonas Devlieghere result.SetStatus(eReturnStatusSuccessFinishResult); 68737469061SJonas Devlieghere } 68837469061SJonas Devlieghere 68937469061SJonas Devlieghere return result.Succeeded(); 69037469061SJonas Devlieghere } 69137469061SJonas Devlieghere 69237469061SJonas Devlieghere private: 69337469061SJonas Devlieghere CommandOptions m_options; 69437469061SJonas Devlieghere }; 69537469061SJonas Devlieghere 6969e046f02SJonas Devlieghere CommandObjectReproducer::CommandObjectReproducer( 6979e046f02SJonas Devlieghere CommandInterpreter &interpreter) 698973d66eeSJonas Devlieghere : CommandObjectMultiword( 699973d66eeSJonas Devlieghere interpreter, "reproducer", 700c8dfe907SJonas Devlieghere "Commands for manipulating reproducers. Reproducers make it " 701c8dfe907SJonas Devlieghere "possible " 70264b7d955SJonas Devlieghere "to capture full debug sessions with all its dependencies. The " 70364b7d955SJonas Devlieghere "resulting reproducer is used to replay the debug session while " 70464b7d955SJonas Devlieghere "debugging the debugger.\n" 70564b7d955SJonas Devlieghere "Because reproducers need the whole the debug session from " 70664b7d955SJonas Devlieghere "beginning to end, you need to launch the debugger in capture or " 70764b7d955SJonas Devlieghere "replay mode, commonly though the command line driver.\n" 70864b7d955SJonas Devlieghere "Reproducers are unrelated record-replay debugging, as you cannot " 70964b7d955SJonas Devlieghere "interact with the debugger during replay.\n", 710130ec068SJonas Devlieghere "reproducer <subcommand> [<subcommand-options>]") { 7119e046f02SJonas Devlieghere LoadSubCommand( 7129e046f02SJonas Devlieghere "generate", 7139e046f02SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); 71415eacd74SJonas Devlieghere LoadSubCommand("status", CommandObjectSP( 71515eacd74SJonas Devlieghere new CommandObjectReproducerStatus(interpreter))); 71697fc8eb4SJonas Devlieghere LoadSubCommand("dump", 71797fc8eb4SJonas Devlieghere CommandObjectSP(new CommandObjectReproducerDump(interpreter))); 71837469061SJonas Devlieghere LoadSubCommand("verify", CommandObjectSP( 71937469061SJonas Devlieghere new CommandObjectReproducerVerify(interpreter))); 720c8dfe907SJonas Devlieghere LoadSubCommand("xcrash", CommandObjectSP( 721c8dfe907SJonas Devlieghere new CommandObjectReproducerXCrash(interpreter))); 7229e046f02SJonas Devlieghere } 7239e046f02SJonas Devlieghere 7249e046f02SJonas Devlieghere CommandObjectReproducer::~CommandObjectReproducer() = default; 725