1//===-- CommandObjectStats.cpp --------------------------------------------===//
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/Core/Debugger.h"
11#include "lldb/Host/OptionParser.h"
12#include "lldb/Interpreter/CommandOptionArgumentTable.h"
13#include "lldb/Interpreter/CommandReturnObject.h"
14#include "lldb/Target/Target.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
19class CommandObjectStatsEnable : public CommandObjectParsed {
20public:
21 CommandObjectStatsEnable(CommandInterpreter &interpreter)
22 : CommandObjectParsed(interpreter, "enable",
23 "Enable statistics collection", nullptr,
24 eCommandProcessMustBePaused) {}
25
26 ~CommandObjectStatsEnable() override = default;
27
28protected:
29 void DoExecute(Args &command, CommandReturnObject &result) override {
30 if (DebuggerStats::GetCollectingStats()) {
31 result.AppendError(in_string: "statistics already enabled");
32 return;
33 }
34
35 DebuggerStats::SetCollectingStats(true);
36 result.SetStatus(eReturnStatusSuccessFinishResult);
37 }
38};
39
40class CommandObjectStatsDisable : public CommandObjectParsed {
41public:
42 CommandObjectStatsDisable(CommandInterpreter &interpreter)
43 : CommandObjectParsed(interpreter, "disable",
44 "Disable statistics collection", nullptr,
45 eCommandProcessMustBePaused) {}
46
47 ~CommandObjectStatsDisable() override = default;
48
49protected:
50 void DoExecute(Args &command, CommandReturnObject &result) override {
51 if (!DebuggerStats::GetCollectingStats()) {
52 result.AppendError(in_string: "need to enable statistics before disabling them");
53 return;
54 }
55
56 DebuggerStats::SetCollectingStats(false);
57 result.SetStatus(eReturnStatusSuccessFinishResult);
58 }
59};
60
61#define LLDB_OPTIONS_statistics_dump
62#include "CommandOptions.inc"
63
64class CommandObjectStatsDump : public CommandObjectParsed {
65 class CommandOptions : public Options {
66 public:
67 CommandOptions() { OptionParsingStarting(execution_context: nullptr); }
68
69 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
70 ExecutionContext *execution_context) override {
71 Status error;
72 const int short_option = m_getopt_table[option_idx].val;
73
74 switch (short_option) {
75 case 'a':
76 m_all_targets = true;
77 break;
78 case 's':
79 m_stats_options.summary_only = true;
80 break;
81 default:
82 llvm_unreachable("Unimplemented option");
83 }
84 return error;
85 }
86
87 void OptionParsingStarting(ExecutionContext *execution_context) override {
88 m_all_targets = false;
89 m_stats_options = StatisticsOptions();
90 }
91
92 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
93 return llvm::ArrayRef(g_statistics_dump_options);
94 }
95
96 const StatisticsOptions &GetStatisticsOptions() { return m_stats_options; }
97
98 bool m_all_targets = false;
99 StatisticsOptions m_stats_options = StatisticsOptions();
100 };
101
102public:
103 CommandObjectStatsDump(CommandInterpreter &interpreter)
104 : CommandObjectParsed(
105 interpreter, "statistics dump", "Dump metrics in JSON format",
106 "statistics dump [<options>]", eCommandRequiresTarget) {}
107
108 ~CommandObjectStatsDump() override = default;
109
110 Options *GetOptions() override { return &m_options; }
111
112protected:
113 void DoExecute(Args &command, CommandReturnObject &result) override {
114 Target *target = nullptr;
115 if (!m_options.m_all_targets)
116 target = m_exe_ctx.GetTargetPtr();
117
118 result.AppendMessageWithFormatv(
119 "{0:2}", DebuggerStats::ReportStatistics(
120 debugger&: GetDebugger(), target, options: m_options.GetStatisticsOptions()));
121 result.SetStatus(eReturnStatusSuccessFinishResult);
122 }
123
124 CommandOptions m_options;
125};
126
127CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter)
128 : CommandObjectMultiword(interpreter, "statistics",
129 "Print statistics about a debugging session",
130 "statistics <subcommand> [<subcommand-options>]") {
131 LoadSubCommand(cmd_name: "enable",
132 command_obj: CommandObjectSP(new CommandObjectStatsEnable(interpreter)));
133 LoadSubCommand(cmd_name: "disable",
134 command_obj: CommandObjectSP(new CommandObjectStatsDisable(interpreter)));
135 LoadSubCommand(cmd_name: "dump",
136 command_obj: CommandObjectSP(new CommandObjectStatsDump(interpreter)));
137}
138
139CommandObjectStats::~CommandObjectStats() = default;
140

source code of lldb/source/Commands/CommandObjectStats.cpp