xref: /llvm-project/lldb/source/Commands/CommandObjectStats.cpp (revision 24fff2429cb612dfe4293ccb93e664f02671ef8a)
1 //===-- CommandObjectStats.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "CommandObjectStats.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Interpreter/CommandReturnObject.h"
14 #include "lldb/Target/Target.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 class CommandObjectStatsEnable : public CommandObjectParsed {
20 public:
21   CommandObjectStatsEnable(CommandInterpreter &interpreter)
22       : CommandObjectParsed(interpreter, "enable",
23                             "Enable statistics collection", nullptr,
24                             eCommandProcessMustBePaused) {}
25 
26   ~CommandObjectStatsEnable() override = default;
27 
28 protected:
29   bool DoExecute(Args &command, CommandReturnObject &result) override {
30     Target *target = GetSelectedOrDummyTarget();
31 
32     if (target->GetCollectingStats()) {
33       result.AppendError("statistics already enabled");
34       result.SetStatus(eReturnStatusFailed);
35       return false;
36     }
37 
38     target->SetCollectingStats(true);
39     result.SetStatus(eReturnStatusSuccessFinishResult);
40     return true;
41   }
42 };
43 
44 class CommandObjectStatsDisable : public CommandObjectParsed {
45 public:
46   CommandObjectStatsDisable(CommandInterpreter &interpreter)
47       : CommandObjectParsed(interpreter, "disable",
48                             "Disable statistics collection", nullptr,
49                             eCommandProcessMustBePaused) {}
50 
51   ~CommandObjectStatsDisable() override = default;
52 
53 protected:
54   bool DoExecute(Args &command, CommandReturnObject &result) override {
55     Target *target = GetSelectedOrDummyTarget();
56 
57     if (!target->GetCollectingStats()) {
58       result.AppendError("need to enable statistics before disabling them");
59       result.SetStatus(eReturnStatusFailed);
60       return false;
61     }
62 
63     target->SetCollectingStats(false);
64     result.SetStatus(eReturnStatusSuccessFinishResult);
65     return true;
66   }
67 };
68 
69 class CommandObjectStatsDump : public CommandObjectParsed {
70 private:
71   std::string GetStatDescription(lldb_private::StatisticKind K) {
72     switch (K) {
73     case StatisticKind::ExpressionSuccessful:
74       return "Number of expr evaluation successes";
75     case StatisticKind::ExpressionFailure:
76       return "Number of expr evaluation failures";
77     case StatisticKind::FrameVarSuccess:
78       return "Number of frame var successes";
79     case StatisticKind::FrameVarFailure:
80       return "Number of frame var failures";
81     case StatisticKind::StatisticMax:
82       return "";
83     }
84     llvm_unreachable("Statistic not registered!");
85   }
86 
87 public:
88   CommandObjectStatsDump(CommandInterpreter &interpreter)
89       : CommandObjectParsed(interpreter, "dump", "Dump statistics results",
90                             nullptr, eCommandProcessMustBePaused) {}
91 
92   ~CommandObjectStatsDump() override = default;
93 
94 protected:
95   bool DoExecute(Args &command, CommandReturnObject &result) override {
96     Target *target = GetSelectedOrDummyTarget();
97 
98     uint32_t i = 0;
99     for (auto &stat : target->GetStatistics()) {
100       result.AppendMessageWithFormat(
101           "%s : %u\n",
102           GetStatDescription(static_cast<lldb_private::StatisticKind>(i))
103               .c_str(),
104           stat);
105       i += 1;
106     }
107     result.SetStatus(eReturnStatusSuccessFinishResult);
108     return true;
109   }
110 };
111 
112 CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter)
113     : CommandObjectMultiword(interpreter, "statistics",
114                              "Print statistics about a debugging session",
115                              "statistics <subcommand> [<subcommand-options>]") {
116   LoadSubCommand("enable",
117                  CommandObjectSP(new CommandObjectStatsEnable(interpreter)));
118   LoadSubCommand("disable",
119                  CommandObjectSP(new CommandObjectStatsDisable(interpreter)));
120   LoadSubCommand("dump",
121                  CommandObjectSP(new CommandObjectStatsDump(interpreter)));
122 }
123 
124 CommandObjectStats::~CommandObjectStats() = default;
125