xref: /freebsd-src/contrib/llvm-project/lldb/source/Commands/CommandObjectStats.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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(static_cast<lldb_private::StatisticKind>(i))
85               .c_str(),
86           stat);
87       i += 1;
88     }
89     result.SetStatus(eReturnStatusSuccessFinishResult);
90     return true;
91   }
92 };
93 
94 CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter)
95     : CommandObjectMultiword(interpreter, "statistics",
96                              "Print statistics about a debugging session",
97                              "statistics <subcommand> [<subcommand-options>]") {
98   LoadSubCommand("enable",
99                  CommandObjectSP(new CommandObjectStatsEnable(interpreter)));
100   LoadSubCommand("disable",
101                  CommandObjectSP(new CommandObjectStatsDisable(interpreter)));
102   LoadSubCommand("dump",
103                  CommandObjectSP(new CommandObjectStatsDump(interpreter)));
104 }
105 
106 CommandObjectStats::~CommandObjectStats() = default;
107