Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 1 | //===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "CommandObjectReproducer.h" |
| 10 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 11 | #include "lldb/Host/OptionParser.h" |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/GDBRemote.h" |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame] | 13 | #include "lldb/Utility/Reproducer.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 14 | |
Jonas Devlieghere | df14b94 | 2018-11-15 01:05:40 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 17 | #include "lldb/Interpreter/OptionArgParser.h" |
| 18 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 19 | |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 20 | #include <csignal> |
| 21 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 22 | using namespace lldb; |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 24 | using namespace lldb_private; |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 25 | using namespace lldb_private::repro; |
| 26 | |
| 27 | enum ReproducerProvider { |
| 28 | eReproducerProviderCommands, |
| 29 | eReproducerProviderFiles, |
| 30 | eReproducerProviderGDB, |
| 31 | eReproducerProviderVersion, |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame] | 32 | eReproducerProviderWorkingDirectory, |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 33 | eReproducerProviderNone |
| 34 | }; |
| 35 | |
| 36 | static constexpr OptionEnumValueElement g_reproducer_provider_type[] = { |
| 37 | { |
| 38 | eReproducerProviderCommands, |
| 39 | "commands", |
| 40 | "Command Interpreter Commands", |
| 41 | }, |
| 42 | { |
| 43 | eReproducerProviderFiles, |
| 44 | "files", |
| 45 | "Files", |
| 46 | }, |
| 47 | { |
| 48 | eReproducerProviderGDB, |
| 49 | "gdb", |
| 50 | "GDB Remote Packets", |
| 51 | }, |
| 52 | { |
| 53 | eReproducerProviderVersion, |
| 54 | "version", |
| 55 | "Version", |
| 56 | }, |
| 57 | { |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame] | 58 | eReproducerProviderWorkingDirectory, |
| 59 | "cwd", |
| 60 | "Working Directory", |
| 61 | }, |
| 62 | { |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 63 | eReproducerProviderNone, |
| 64 | "none", |
| 65 | "None", |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | static constexpr OptionEnumValues ReproducerProviderType() { |
| 70 | return OptionEnumValues(g_reproducer_provider_type); |
| 71 | } |
| 72 | |
Jonas Devlieghere | 36eea5c | 2019-11-19 16:18:19 -0800 | [diff] [blame] | 73 | #define LLDB_OPTIONS_reproducer_dump |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 74 | #include "CommandOptions.inc" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 75 | |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 76 | enum ReproducerCrashSignal { |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 77 | eReproducerCrashSigill, |
| 78 | eReproducerCrashSigsegv, |
| 79 | }; |
| 80 | |
| 81 | static constexpr OptionEnumValueElement g_reproducer_signaltype[] = { |
| 82 | { |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 83 | eReproducerCrashSigill, |
| 84 | "SIGILL", |
| 85 | "Illegal instruction", |
| 86 | }, |
| 87 | { |
| 88 | eReproducerCrashSigsegv, |
| 89 | "SIGSEGV", |
| 90 | "Segmentation fault", |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | static constexpr OptionEnumValues ReproducerSignalType() { |
| 95 | return OptionEnumValues(g_reproducer_signaltype); |
| 96 | } |
| 97 | |
| 98 | #define LLDB_OPTIONS_reproducer_xcrash |
| 99 | #include "CommandOptions.inc" |
| 100 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 101 | class CommandObjectReproducerGenerate : public CommandObjectParsed { |
| 102 | public: |
| 103 | CommandObjectReproducerGenerate(CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 104 | : CommandObjectParsed( |
| 105 | interpreter, "reproducer generate", |
| 106 | "Generate reproducer on disk. When the debugger is in capture " |
| 107 | "mode, this command will output the reproducer to a directory on " |
Jonas Devlieghere | 0cf86da | 2019-11-11 14:16:52 -0800 | [diff] [blame] | 108 | "disk and quit. In replay mode this command in a no-op.", |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 109 | nullptr) {} |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 110 | |
| 111 | ~CommandObjectReproducerGenerate() override = default; |
| 112 | |
| 113 | protected: |
| 114 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 115 | if (!command.empty()) { |
| 116 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 117 | m_cmd_name.c_str()); |
| 118 | return false; |
| 119 | } |
| 120 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 121 | auto &r = Reproducer::Instance(); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 122 | if (auto generator = r.GetGenerator()) { |
| 123 | generator->Keep(); |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 124 | } else if (r.IsReplaying()) { |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 125 | // Make this operation a NOP in replay mode. |
| 126 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 127 | return result.Succeeded(); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 128 | } else { |
| 129 | result.AppendErrorWithFormat("Unable to get the reproducer generator"); |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 130 | result.SetStatus(eReturnStatusFailed); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
| 134 | result.GetOutputStream() |
| 135 | << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; |
Jonas Devlieghere | 1c5250a | 2019-04-02 18:23:16 +0000 | [diff] [blame] | 136 | result.GetOutputStream() |
| 137 | << "Please have a look at the directory to assess if you're willing to " |
| 138 | "share the contained information.\n"; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 139 | |
Jonas Devlieghere | 0cf86da | 2019-11-11 14:16:52 -0800 | [diff] [blame] | 140 | m_interpreter.BroadcastEvent( |
| 141 | CommandInterpreter::eBroadcastBitQuitCommandReceived); |
| 142 | result.SetStatus(eReturnStatusQuit); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 143 | return result.Succeeded(); |
| 144 | } |
| 145 | }; |
| 146 | |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 147 | class CommandObjectReproducerXCrash : public CommandObjectParsed { |
| 148 | public: |
| 149 | CommandObjectReproducerXCrash(CommandInterpreter &interpreter) |
| 150 | : CommandObjectParsed(interpreter, "reproducer xcrash", |
| 151 | "Intentionally force the debugger to crash in " |
| 152 | "order to trigger and test reproducer generation.", |
| 153 | nullptr) {} |
| 154 | |
| 155 | ~CommandObjectReproducerXCrash() override = default; |
| 156 | |
| 157 | Options *GetOptions() override { return &m_options; } |
| 158 | |
| 159 | class CommandOptions : public Options { |
| 160 | public: |
| 161 | CommandOptions() : Options() {} |
| 162 | |
| 163 | ~CommandOptions() override = default; |
| 164 | |
| 165 | Status SetOptionValue(uint32_t option_idx, StringRef option_arg, |
| 166 | ExecutionContext *execution_context) override { |
| 167 | Status error; |
| 168 | const int short_option = m_getopt_table[option_idx].val; |
| 169 | |
| 170 | switch (short_option) { |
| 171 | case 's': |
| 172 | signal = (ReproducerCrashSignal)OptionArgParser::ToOptionEnum( |
| 173 | option_arg, GetDefinitions()[option_idx].enum_values, 0, error); |
| 174 | if (!error.Success()) |
| 175 | error.SetErrorStringWithFormat("unrecognized value for signal '%s'", |
| 176 | option_arg.str().c_str()); |
| 177 | break; |
| 178 | default: |
| 179 | llvm_unreachable("Unimplemented option"); |
| 180 | } |
| 181 | |
| 182 | return error; |
| 183 | } |
| 184 | |
| 185 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 186 | signal = eReproducerCrashSigsegv; |
| 187 | } |
| 188 | |
| 189 | ArrayRef<OptionDefinition> GetDefinitions() override { |
| 190 | return makeArrayRef(g_reproducer_xcrash_options); |
| 191 | } |
| 192 | |
| 193 | ReproducerCrashSignal signal = eReproducerCrashSigsegv; |
| 194 | }; |
| 195 | |
| 196 | protected: |
| 197 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 198 | if (!command.empty()) { |
| 199 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 200 | m_cmd_name.c_str()); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | auto &r = Reproducer::Instance(); |
| 205 | if (!r.IsCapturing()) { |
| 206 | result.SetError( |
| 207 | "forcing a crash is only supported when capturing a reproducer."); |
| 208 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | switch (m_options.signal) { |
| 213 | case eReproducerCrashSigill: |
| 214 | std::raise(SIGILL); |
| 215 | break; |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 216 | case eReproducerCrashSigsegv: |
| 217 | std::raise(SIGSEGV); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | result.SetStatus(eReturnStatusQuit); |
| 222 | return result.Succeeded(); |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | CommandOptions m_options; |
| 227 | }; |
| 228 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 229 | class CommandObjectReproducerStatus : public CommandObjectParsed { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 230 | public: |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 231 | CommandObjectReproducerStatus(CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 232 | : CommandObjectParsed( |
| 233 | interpreter, "reproducer status", |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 234 | "Show the current reproducer status. In capture mode the " |
| 235 | "debugger " |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 236 | "is collecting all the information it needs to create a " |
| 237 | "reproducer. In replay mode the reproducer is replaying a " |
| 238 | "reproducer. When the reproducers are off, no data is collected " |
| 239 | "and no reproducer can be generated.", |
| 240 | nullptr) {} |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 241 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 242 | ~CommandObjectReproducerStatus() override = default; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 243 | |
| 244 | protected: |
| 245 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 246 | if (!command.empty()) { |
| 247 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 248 | m_cmd_name.c_str()); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 252 | auto &r = Reproducer::Instance(); |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 253 | if (r.IsCapturing()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 254 | result.GetOutputStream() << "Reproducer is in capture mode.\n"; |
Jonas Devlieghere | 865cd09 | 2019-10-09 21:47:49 +0000 | [diff] [blame] | 255 | } else if (r.IsReplaying()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 256 | result.GetOutputStream() << "Reproducer is in replay mode.\n"; |
| 257 | } else { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 258 | result.GetOutputStream() << "Reproducer is off.\n"; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 261 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 262 | return result.Succeeded(); |
| 263 | } |
| 264 | }; |
| 265 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 266 | static void SetError(CommandReturnObject &result, Error err) { |
| 267 | result.GetErrorStream().Printf("error: %s\n", |
| 268 | toString(std::move(err)).c_str()); |
| 269 | result.SetStatus(eReturnStatusFailed); |
| 270 | } |
| 271 | |
| 272 | class CommandObjectReproducerDump : public CommandObjectParsed { |
| 273 | public: |
| 274 | CommandObjectReproducerDump(CommandInterpreter &interpreter) |
| 275 | : CommandObjectParsed(interpreter, "reproducer dump", |
Jonas Devlieghere | 64b7d95 | 2019-10-18 21:47:31 +0000 | [diff] [blame] | 276 | "Dump the information contained in a reproducer. " |
| 277 | "If no reproducer is specified during replay, it " |
| 278 | "dumps the content of the current reproducer.", |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 279 | nullptr) {} |
| 280 | |
| 281 | ~CommandObjectReproducerDump() override = default; |
| 282 | |
| 283 | Options *GetOptions() override { return &m_options; } |
| 284 | |
| 285 | class CommandOptions : public Options { |
| 286 | public: |
| 287 | CommandOptions() : Options(), file() {} |
| 288 | |
| 289 | ~CommandOptions() override = default; |
| 290 | |
| 291 | Status SetOptionValue(uint32_t option_idx, StringRef option_arg, |
| 292 | ExecutionContext *execution_context) override { |
| 293 | Status error; |
| 294 | const int short_option = m_getopt_table[option_idx].val; |
| 295 | |
| 296 | switch (short_option) { |
| 297 | case 'f': |
| 298 | file.SetFile(option_arg, FileSpec::Style::native); |
| 299 | FileSystem::Instance().Resolve(file); |
| 300 | break; |
| 301 | case 'p': |
| 302 | provider = (ReproducerProvider)OptionArgParser::ToOptionEnum( |
| 303 | option_arg, GetDefinitions()[option_idx].enum_values, 0, error); |
| 304 | if (!error.Success()) |
| 305 | error.SetErrorStringWithFormat("unrecognized value for provider '%s'", |
| 306 | option_arg.str().c_str()); |
| 307 | break; |
| 308 | default: |
| 309 | llvm_unreachable("Unimplemented option"); |
| 310 | } |
| 311 | |
| 312 | return error; |
| 313 | } |
| 314 | |
| 315 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 316 | file.Clear(); |
| 317 | provider = eReproducerProviderNone; |
| 318 | } |
| 319 | |
| 320 | ArrayRef<OptionDefinition> GetDefinitions() override { |
Jonas Devlieghere | 36eea5c | 2019-11-19 16:18:19 -0800 | [diff] [blame] | 321 | return makeArrayRef(g_reproducer_dump_options); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | FileSpec file; |
| 325 | ReproducerProvider provider = eReproducerProviderNone; |
| 326 | }; |
| 327 | |
| 328 | protected: |
| 329 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 330 | if (!command.empty()) { |
| 331 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 332 | m_cmd_name.c_str()); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | // If no reproducer path is specified, use the loader currently used for |
| 337 | // replay. Otherwise create a new loader just for dumping. |
| 338 | llvm::Optional<Loader> loader_storage; |
| 339 | Loader *loader = nullptr; |
| 340 | if (!m_options.file) { |
| 341 | loader = Reproducer::Instance().GetLoader(); |
| 342 | if (loader == nullptr) { |
| 343 | result.SetError( |
| 344 | "Not specifying a reproducer is only support during replay."); |
| 345 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 346 | return false; |
| 347 | } |
| 348 | } else { |
| 349 | loader_storage.emplace(m_options.file); |
| 350 | loader = &(*loader_storage); |
| 351 | if (Error err = loader->LoadIndex()) { |
| 352 | SetError(result, std::move(err)); |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // If we get here we should have a valid loader. |
| 358 | assert(loader); |
| 359 | |
| 360 | switch (m_options.provider) { |
| 361 | case eReproducerProviderFiles: { |
| 362 | FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>(); |
| 363 | |
| 364 | // Read the VFS mapping. |
| 365 | ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = |
| 366 | vfs::getRealFileSystem()->getBufferForFile(vfs_mapping.GetPath()); |
| 367 | if (!buffer) { |
| 368 | SetError(result, errorCodeToError(buffer.getError())); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | // Initialize a VFS from the given mapping. |
| 373 | IntrusiveRefCntPtr<vfs::FileSystem> vfs = vfs::getVFSFromYAML( |
| 374 | std::move(buffer.get()), nullptr, vfs_mapping.GetPath()); |
| 375 | |
| 376 | // Dump the VFS to a buffer. |
| 377 | std::string str; |
| 378 | raw_string_ostream os(str); |
| 379 | static_cast<vfs::RedirectingFileSystem &>(*vfs).dump(os); |
| 380 | os.flush(); |
| 381 | |
| 382 | // Return the string. |
| 383 | result.AppendMessage(str); |
| 384 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 385 | return true; |
| 386 | } |
| 387 | case eReproducerProviderVersion: { |
Jonas Devlieghere | b2575da | 2019-10-17 00:01:57 +0000 | [diff] [blame] | 388 | Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); |
| 389 | if (!version) { |
| 390 | SetError(result, version.takeError()); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 391 | return false; |
| 392 | } |
Jonas Devlieghere | b2575da | 2019-10-17 00:01:57 +0000 | [diff] [blame] | 393 | result.AppendMessage(*version); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 394 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 395 | return true; |
| 396 | } |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame] | 397 | case eReproducerProviderWorkingDirectory: { |
| 398 | Expected<std::string> cwd = |
| 399 | loader->LoadBuffer<WorkingDirectoryProvider>(); |
| 400 | if (!cwd) { |
| 401 | SetError(result, cwd.takeError()); |
| 402 | return false; |
| 403 | } |
| 404 | result.AppendMessage(*cwd); |
| 405 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 406 | return true; |
| 407 | } |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 408 | case eReproducerProviderCommands: { |
| 409 | // Create a new command loader. |
| 410 | std::unique_ptr<repro::CommandLoader> command_loader = |
| 411 | repro::CommandLoader::Create(loader); |
| 412 | if (!command_loader) { |
| 413 | SetError(result, |
| 414 | make_error<StringError>(llvm::inconvertibleErrorCode(), |
| 415 | "Unable to create command loader.")); |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | // Iterate over the command files and dump them. |
| 420 | while (true) { |
| 421 | llvm::Optional<std::string> command_file = |
| 422 | command_loader->GetNextFile(); |
| 423 | if (!command_file) |
| 424 | break; |
| 425 | |
| 426 | auto command_buffer = llvm::MemoryBuffer::getFile(*command_file); |
| 427 | if (auto err = command_buffer.getError()) { |
| 428 | SetError(result, errorCodeToError(err)); |
| 429 | return false; |
| 430 | } |
| 431 | result.AppendMessage((*command_buffer)->getBuffer()); |
| 432 | } |
| 433 | |
| 434 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 435 | return true; |
| 436 | } |
| 437 | case eReproducerProviderGDB: { |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 438 | FileSpec gdb_file = loader->GetFile<ProcessGDBRemoteProvider::Info>(); |
| 439 | auto error_or_file = MemoryBuffer::getFile(gdb_file.GetPath()); |
| 440 | if (auto err = error_or_file.getError()) { |
| 441 | SetError(result, errorCodeToError(err)); |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | std::vector<GDBRemotePacket> packets; |
| 446 | yaml::Input yin((*error_or_file)->getBuffer()); |
| 447 | yin >> packets; |
| 448 | |
| 449 | if (auto err = yin.error()) { |
| 450 | SetError(result, errorCodeToError(err)); |
| 451 | return false; |
| 452 | } |
| 453 | |
Jonas Devlieghere | f4f1201 | 2019-10-17 00:02:00 +0000 | [diff] [blame] | 454 | for (GDBRemotePacket &packet : packets) { |
Jonas Devlieghere | 8fc8d3f | 2019-09-16 23:31:06 +0000 | [diff] [blame] | 455 | packet.Dump(result.GetOutputStream()); |
| 456 | } |
| 457 | |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 458 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 459 | return true; |
| 460 | } |
| 461 | case eReproducerProviderNone: |
| 462 | result.SetError("No valid provider specified."); |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 467 | return result.Succeeded(); |
| 468 | } |
| 469 | |
| 470 | private: |
| 471 | CommandOptions m_options; |
| 472 | }; |
| 473 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 474 | CommandObjectReproducer::CommandObjectReproducer( |
| 475 | CommandInterpreter &interpreter) |
Jonas Devlieghere | 973d66e | 2019-05-03 00:10:31 +0000 | [diff] [blame] | 476 | : CommandObjectMultiword( |
| 477 | interpreter, "reproducer", |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 478 | "Commands for manipulating reproducers. Reproducers make it " |
| 479 | "possible " |
Jonas Devlieghere | 64b7d95 | 2019-10-18 21:47:31 +0000 | [diff] [blame] | 480 | "to capture full debug sessions with all its dependencies. The " |
| 481 | "resulting reproducer is used to replay the debug session while " |
| 482 | "debugging the debugger.\n" |
| 483 | "Because reproducers need the whole the debug session from " |
| 484 | "beginning to end, you need to launch the debugger in capture or " |
| 485 | "replay mode, commonly though the command line driver.\n" |
| 486 | "Reproducers are unrelated record-replay debugging, as you cannot " |
| 487 | "interact with the debugger during replay.\n", |
Jonas Devlieghere | 130ec06 | 2019-07-30 18:06:38 +0000 | [diff] [blame] | 488 | "reproducer <subcommand> [<subcommand-options>]") { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 489 | LoadSubCommand( |
| 490 | "generate", |
| 491 | CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 492 | LoadSubCommand("status", CommandObjectSP( |
| 493 | new CommandObjectReproducerStatus(interpreter))); |
Jonas Devlieghere | 97fc8eb | 2019-09-13 23:27:31 +0000 | [diff] [blame] | 494 | LoadSubCommand("dump", |
| 495 | CommandObjectSP(new CommandObjectReproducerDump(interpreter))); |
Jonas Devlieghere | c8dfe90 | 2019-11-19 17:28:46 -0800 | [diff] [blame] | 496 | LoadSubCommand("xcrash", CommandObjectSP( |
| 497 | new CommandObjectReproducerXCrash(interpreter))); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | CommandObjectReproducer::~CommandObjectReproducer() = default; |