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