blob: c7cdd6491a42377b7251613a53845502b011c88f [file] [log] [blame]
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001//===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Devlieghere9e046f02018-11-13 19:18:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "CommandObjectReproducer.h"
10
11#include "lldb/Utility/Reproducer.h"
12
Jonas Devliegheredf14b942018-11-15 01:05:40 +000013#include "lldb/Interpreter/CommandInterpreter.h"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000014#include "lldb/Interpreter/CommandReturnObject.h"
15#include "lldb/Interpreter/OptionArgParser.h"
16#include "lldb/Interpreter/OptionGroupBoolean.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000021class CommandObjectReproducerGenerate : public CommandObjectParsed {
22public:
23 CommandObjectReproducerGenerate(CommandInterpreter &interpreter)
24 : CommandObjectParsed(interpreter, "reproducer generate",
25 "Generate reproducer on disk.", nullptr) {}
26
27 ~CommandObjectReproducerGenerate() override = default;
28
29protected:
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 Devlieghere2dca6532019-02-27 17:47:06 +000040 } else if (r.GetLoader()) {
41 // Make this operation a NOP in replay mode.
42 result.SetStatus(eReturnStatusSuccessFinishNoResult);
43 return result.Succeeded();
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000044 } else {
45 result.AppendErrorWithFormat("Unable to get the reproducer generator");
Jonas Devlieghere2dca6532019-02-27 17:47:06 +000046 result.SetStatus(eReturnStatusFailed);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000047 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 Devlieghere15eacd72018-12-03 17:28:29 +000058class CommandObjectReproducerStatus : public CommandObjectParsed {
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000059public:
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000060 CommandObjectReproducerStatus(CommandInterpreter &interpreter)
61 : CommandObjectParsed(interpreter, "reproducer status",
62 "Show the current reproducer status.", nullptr) {}
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000063
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000064 ~CommandObjectReproducerStatus() override = default;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000065
66protected:
67 bool DoExecute(Args &command, CommandReturnObject &result) override {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000068 if (!command.empty()) {
69 result.AppendErrorWithFormat("'%s' takes no arguments",
70 m_cmd_name.c_str());
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000071 return false;
72 }
73
74 auto &r = repro::Reproducer::Instance();
Zachary Turner52f8f342019-01-29 22:55:21 +000075 if (r.GetGenerator()) {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000076 result.GetOutputStream() << "Reproducer is in capture mode.\n";
Zachary Turner52f8f342019-01-29 22:55:21 +000077 } else if (r.GetLoader()) {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000078 result.GetOutputStream() << "Reproducer is in replay mode.\n";
79 } else {
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000080 result.GetOutputStream() << "Reproducer is off.\n";
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000081 }
82
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000083 result.SetStatus(eReturnStatusSuccessFinishResult);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000084 return result.Succeeded();
85 }
86};
87
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000088CommandObjectReproducer::CommandObjectReproducer(
89 CommandInterpreter &interpreter)
90 : CommandObjectMultiword(interpreter, "reproducer",
91 "Commands controlling LLDB reproducers.",
92 "log <subcommand> [<command-options>]") {
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000093 LoadSubCommand(
94 "generate",
95 CommandObjectSP(new CommandObjectReproducerGenerate(interpreter)));
Jonas Devlieghere15eacd72018-12-03 17:28:29 +000096 LoadSubCommand("status", CommandObjectSP(
97 new CommandObjectReproducerStatus(interpreter)));
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000098}
99
100CommandObjectReproducer::~CommandObjectReproducer() = default;