1 //===-- CommandObjectStats.cpp ----------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectStats.h"
10 #include "lldb/Interpreter/CommandReturnObject.h"
11 #include "lldb/Target/Target.h"
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
16 class CommandObjectStatsEnable : public CommandObjectParsed {
17 public:
18   CommandObjectStatsEnable(CommandInterpreter &interpreter)
19       : CommandObjectParsed(interpreter, "enable",
20                             "Enable statistics collection", nullptr,
21                             eCommandProcessMustBePaused) {}
22 
23   ~CommandObjectStatsEnable() override = default;
24 
25 protected:
26   bool DoExecute(Args &command, CommandReturnObject &result) override {
27     Target &target = GetSelectedOrDummyTarget();
28 
29     if (target.GetCollectingStats()) {
30       result.AppendError("statistics already enabled");
31       result.SetStatus(eReturnStatusFailed);
32       return false;
33     }
34 
35     target.SetCollectingStats(true);
36     result.SetStatus(eReturnStatusSuccessFinishResult);
37     return true;
38   }
39 };
40 
41 class CommandObjectStatsDisable : public CommandObjectParsed {
42 public:
43   CommandObjectStatsDisable(CommandInterpreter &interpreter)
44       : CommandObjectParsed(interpreter, "disable",
45                             "Disable statistics collection", nullptr,
46                             eCommandProcessMustBePaused) {}
47 
48   ~CommandObjectStatsDisable() override = default;
49 
50 protected:
51   bool DoExecute(Args &command, CommandReturnObject &result) override {
52     Target &target = GetSelectedOrDummyTarget();
53 
54     if (!target.GetCollectingStats()) {
55       result.AppendError("need to enable statistics before disabling them");
56       result.SetStatus(eReturnStatusFailed);
57       return false;
58     }
59 
60     target.SetCollectingStats(false);
61     result.SetStatus(eReturnStatusSuccessFinishResult);
62     return true;
63   }
64 };
65 
66 class CommandObjectStatsDump : public CommandObjectParsed {
67 public:
68   CommandObjectStatsDump(CommandInterpreter &interpreter)
69       : CommandObjectParsed(interpreter, "dump", "Dump statistics results",
70                             nullptr, eCommandProcessMustBePaused) {}
71 
72   ~CommandObjectStatsDump() override = default;
73 
74 protected:
75   bool DoExecute(Args &command, CommandReturnObject &result) override {
76     Target &target = GetSelectedOrDummyTarget();
77 
78     uint32_t i = 0;
79     for (auto &stat : target.GetStatistics()) {
80       result.AppendMessageWithFormat(
81           "%s : %u\n",
82           lldb_private::GetStatDescription(
83               static_cast<lldb_private::StatisticKind>(i))
84               .c_str(),
85           stat);
86       i += 1;
87     }
88     result.SetStatus(eReturnStatusSuccessFinishResult);
89     return true;
90   }
91 };
92 
93 CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter)
94     : CommandObjectMultiword(interpreter, "statistics",
95                              "Print statistics about a debugging session",
96                              "statistics <subcommand> [<subcommand-options>]") {
97   LoadSubCommand("enable",
98                  CommandObjectSP(new CommandObjectStatsEnable(interpreter)));
99   LoadSubCommand("disable",
100                  CommandObjectSP(new CommandObjectStatsDisable(interpreter)));
101   LoadSubCommand("dump",
102                  CommandObjectSP(new CommandObjectStatsDump(interpreter)));
103 }
104 
105 CommandObjectStats::~CommandObjectStats() = default;
106