1*b5893f02SDimitry Andric //===-- CommandObjectReproducer.cpp -----------------------------*- C++ -*-===//
2*b5893f02SDimitry Andric //
3*b5893f02SDimitry Andric //                     The LLVM Compiler Infrastructure
4*b5893f02SDimitry Andric //
5*b5893f02SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*b5893f02SDimitry Andric // License. See LICENSE.TXT for details.
7*b5893f02SDimitry Andric //
8*b5893f02SDimitry Andric //===----------------------------------------------------------------------===//
9*b5893f02SDimitry Andric 
10*b5893f02SDimitry Andric #include "CommandObjectReproducer.h"
11*b5893f02SDimitry Andric 
12*b5893f02SDimitry Andric #include "lldb/Utility/Reproducer.h"
13*b5893f02SDimitry Andric 
14*b5893f02SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
15*b5893f02SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
16*b5893f02SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
17*b5893f02SDimitry Andric #include "lldb/Interpreter/OptionGroupBoolean.h"
18*b5893f02SDimitry Andric 
19*b5893f02SDimitry Andric using namespace lldb;
20*b5893f02SDimitry Andric using namespace lldb_private;
21*b5893f02SDimitry Andric 
22*b5893f02SDimitry Andric class CommandObjectReproducerGenerate : public CommandObjectParsed {
23*b5893f02SDimitry Andric public:
CommandObjectReproducerGenerate(CommandInterpreter & interpreter)24*b5893f02SDimitry Andric   CommandObjectReproducerGenerate(CommandInterpreter &interpreter)
25*b5893f02SDimitry Andric       : CommandObjectParsed(interpreter, "reproducer generate",
26*b5893f02SDimitry Andric                             "Generate reproducer on disk.", nullptr) {}
27*b5893f02SDimitry Andric 
28*b5893f02SDimitry Andric   ~CommandObjectReproducerGenerate() override = default;
29*b5893f02SDimitry Andric 
30*b5893f02SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)31*b5893f02SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
32*b5893f02SDimitry Andric     if (!command.empty()) {
33*b5893f02SDimitry Andric       result.AppendErrorWithFormat("'%s' takes no arguments",
34*b5893f02SDimitry Andric                                    m_cmd_name.c_str());
35*b5893f02SDimitry Andric       return false;
36*b5893f02SDimitry Andric     }
37*b5893f02SDimitry Andric 
38*b5893f02SDimitry Andric     auto &r = repro::Reproducer::Instance();
39*b5893f02SDimitry Andric     if (auto generator = r.GetGenerator()) {
40*b5893f02SDimitry Andric       generator->Keep();
41*b5893f02SDimitry Andric     } else {
42*b5893f02SDimitry Andric       result.AppendErrorWithFormat("Unable to get the reproducer generator");
43*b5893f02SDimitry Andric       return false;
44*b5893f02SDimitry Andric     }
45*b5893f02SDimitry Andric 
46*b5893f02SDimitry Andric     result.GetOutputStream()
47*b5893f02SDimitry Andric         << "Reproducer written to '" << r.GetReproducerPath() << "'\n";
48*b5893f02SDimitry Andric 
49*b5893f02SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
50*b5893f02SDimitry Andric     return result.Succeeded();
51*b5893f02SDimitry Andric   }
52*b5893f02SDimitry Andric };
53*b5893f02SDimitry Andric 
54*b5893f02SDimitry Andric class CommandObjectReproducerStatus : public CommandObjectParsed {
55*b5893f02SDimitry Andric public:
CommandObjectReproducerStatus(CommandInterpreter & interpreter)56*b5893f02SDimitry Andric   CommandObjectReproducerStatus(CommandInterpreter &interpreter)
57*b5893f02SDimitry Andric       : CommandObjectParsed(interpreter, "reproducer status",
58*b5893f02SDimitry Andric                             "Show the current reproducer status.", nullptr) {}
59*b5893f02SDimitry Andric 
60*b5893f02SDimitry Andric   ~CommandObjectReproducerStatus() override = default;
61*b5893f02SDimitry Andric 
62*b5893f02SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)63*b5893f02SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
64*b5893f02SDimitry Andric     if (!command.empty()) {
65*b5893f02SDimitry Andric       result.AppendErrorWithFormat("'%s' takes no arguments",
66*b5893f02SDimitry Andric                                    m_cmd_name.c_str());
67*b5893f02SDimitry Andric       return false;
68*b5893f02SDimitry Andric     }
69*b5893f02SDimitry Andric 
70*b5893f02SDimitry Andric     auto &r = repro::Reproducer::Instance();
71*b5893f02SDimitry Andric     if (auto generator = r.GetGenerator()) {
72*b5893f02SDimitry Andric       result.GetOutputStream() << "Reproducer is in capture mode.\n";
73*b5893f02SDimitry Andric     } else if (auto generator = r.GetLoader()) {
74*b5893f02SDimitry Andric       result.GetOutputStream() << "Reproducer is in replay mode.\n";
75*b5893f02SDimitry Andric     } else {
76*b5893f02SDimitry Andric 
77*b5893f02SDimitry Andric       result.GetOutputStream() << "Reproducer is off.\n";
78*b5893f02SDimitry Andric     }
79*b5893f02SDimitry Andric 
80*b5893f02SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
81*b5893f02SDimitry Andric     return result.Succeeded();
82*b5893f02SDimitry Andric   }
83*b5893f02SDimitry Andric };
84*b5893f02SDimitry Andric 
CommandObjectReproducer(CommandInterpreter & interpreter)85*b5893f02SDimitry Andric CommandObjectReproducer::CommandObjectReproducer(
86*b5893f02SDimitry Andric     CommandInterpreter &interpreter)
87*b5893f02SDimitry Andric     : CommandObjectMultiword(interpreter, "reproducer",
88*b5893f02SDimitry Andric                              "Commands controlling LLDB reproducers.",
89*b5893f02SDimitry Andric                              "log <subcommand> [<command-options>]") {
90*b5893f02SDimitry Andric   LoadSubCommand(
91*b5893f02SDimitry Andric       "generate",
92*b5893f02SDimitry Andric       CommandObjectSP(new CommandObjectReproducerGenerate(interpreter)));
93*b5893f02SDimitry Andric   LoadSubCommand("status", CommandObjectSP(
94*b5893f02SDimitry Andric                                new CommandObjectReproducerStatus(interpreter)));
95*b5893f02SDimitry Andric }
96*b5893f02SDimitry Andric 
97*b5893f02SDimitry Andric CommandObjectReproducer::~CommandObjectReproducer() = default;
98