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 | |
| 11 | #include "lldb/Utility/Reproducer.h" |
| 12 | |
Jonas Devlieghere | df14b94 | 2018-11-15 01:05:40 +0000 | [diff] [blame] | 13 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 14 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 15 | #include "lldb/Interpreter/OptionArgParser.h" |
| 16 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 21 | class CommandObjectReproducerGenerate : public CommandObjectParsed { |
| 22 | public: |
| 23 | CommandObjectReproducerGenerate(CommandInterpreter &interpreter) |
| 24 | : CommandObjectParsed(interpreter, "reproducer generate", |
| 25 | "Generate reproducer on disk.", nullptr) {} |
| 26 | |
| 27 | ~CommandObjectReproducerGenerate() override = default; |
| 28 | |
| 29 | protected: |
| 30 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 31 | if (!command.empty()) { |
| 32 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 33 | m_cmd_name.c_str()); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | auto &r = repro::Reproducer::Instance(); |
| 38 | if (auto generator = r.GetGenerator()) { |
| 39 | generator->Keep(); |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 40 | } else if (r.GetLoader()) { |
| 41 | // Make this operation a NOP in replay mode. |
| 42 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 43 | return result.Succeeded(); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 44 | } else { |
| 45 | result.AppendErrorWithFormat("Unable to get the reproducer generator"); |
Jonas Devlieghere | 2dca653 | 2019-02-27 17:47:06 +0000 | [diff] [blame] | 46 | result.SetStatus(eReturnStatusFailed); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
| 50 | result.GetOutputStream() |
| 51 | << "Reproducer written to '" << r.GetReproducerPath() << "'\n"; |
| 52 | |
| 53 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 54 | return result.Succeeded(); |
| 55 | } |
| 56 | }; |
| 57 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 58 | class CommandObjectReproducerStatus : public CommandObjectParsed { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 59 | public: |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 60 | CommandObjectReproducerStatus(CommandInterpreter &interpreter) |
| 61 | : CommandObjectParsed(interpreter, "reproducer status", |
| 62 | "Show the current reproducer status.", nullptr) {} |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 63 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 64 | ~CommandObjectReproducerStatus() override = default; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 65 | |
| 66 | protected: |
| 67 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 68 | if (!command.empty()) { |
| 69 | result.AppendErrorWithFormat("'%s' takes no arguments", |
| 70 | m_cmd_name.c_str()); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | auto &r = repro::Reproducer::Instance(); |
Zachary Turner | 52f8f34 | 2019-01-29 22:55:21 +0000 | [diff] [blame] | 75 | if (r.GetGenerator()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 76 | result.GetOutputStream() << "Reproducer is in capture mode.\n"; |
Zachary Turner | 52f8f34 | 2019-01-29 22:55:21 +0000 | [diff] [blame] | 77 | } else if (r.GetLoader()) { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 78 | result.GetOutputStream() << "Reproducer is in replay mode.\n"; |
| 79 | } else { |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 80 | result.GetOutputStream() << "Reproducer is off.\n"; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 83 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 84 | return result.Succeeded(); |
| 85 | } |
| 86 | }; |
| 87 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 88 | CommandObjectReproducer::CommandObjectReproducer( |
| 89 | CommandInterpreter &interpreter) |
| 90 | : CommandObjectMultiword(interpreter, "reproducer", |
| 91 | "Commands controlling LLDB reproducers.", |
| 92 | "log <subcommand> [<command-options>]") { |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 93 | LoadSubCommand( |
| 94 | "generate", |
| 95 | CommandObjectSP(new CommandObjectReproducerGenerate(interpreter))); |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 96 | LoadSubCommand("status", CommandObjectSP( |
| 97 | new CommandObjectReproducerStatus(interpreter))); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | CommandObjectReproducer::~CommandObjectReproducer() = default; |