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 case 'f':
82 m_stats_options.load_all_debug_info = true;
83 break;
84 default:
85 llvm_unreachable("Unimplemented option");
86 }
87 return error;
88 }
89
90 void OptionParsingStarting(ExecutionContext *execution_context) override {
91 m_all_targets = false;
92 m_stats_options = StatisticsOptions();
93 }
94
95 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
96 return llvm::ArrayRef(g_statistics_dump_options);
97 }
98
99 const StatisticsOptions &GetStatisticsOptions() { return m_stats_options; }
100
101 bool m_all_targets = false;
102 StatisticsOptions m_stats_options = StatisticsOptions();
103 };
104
105public:
106 CommandObjectStatsDump(CommandInterpreter &interpreter)
107 : CommandObjectParsed(
108 interpreter, "statistics dump", "Dump metrics in JSON format",
109 "statistics dump [<options>]", eCommandRequiresTarget) {}
110
111 ~CommandObjectStatsDump() override = default;
112
113 Options *GetOptions() override { return &m_options; }
114
115protected:
116 void DoExecute(Args &command, CommandReturnObject &result) override {
117 Target *target = nullptr;
118 if (!m_options.m_all_targets)
119 target = m_exe_ctx.GetTargetPtr();
120
121 result.AppendMessageWithFormatv(
122 "{0:2}", DebuggerStats::ReportStatistics(
123 debugger&: GetDebugger(), target, options: m_options.GetStatisticsOptions()));
124 result.SetStatus(eReturnStatusSuccessFinishResult);
125 }
126
127 CommandOptions m_options;
128};
129
130CommandObjectStats::CommandObjectStats(CommandInterpreter &interpreter)
131 : CommandObjectMultiword(interpreter, "statistics",
132 "Print statistics about a debugging session",
133 "statistics <subcommand> [<subcommand-options>]") {
134 LoadSubCommand(cmd_name: "enable",
135 command_obj: CommandObjectSP(new CommandObjectStatsEnable(interpreter)));
136 LoadSubCommand(cmd_name: "disable",
137 command_obj: CommandObjectSP(new CommandObjectStatsDisable(interpreter)));
138 LoadSubCommand(cmd_name: "dump",
139 command_obj: CommandObjectSP(new CommandObjectStatsDump(interpreter)));
140}
141
142CommandObjectStats::~CommandObjectStats() = default;
143

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