1 //===-- CommandObjectStats.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
17 using namespace lldb_private
;
19 class CommandObjectStatsEnable
: public CommandObjectParsed
{
21 CommandObjectStatsEnable(CommandInterpreter
&interpreter
)
22 : CommandObjectParsed(interpreter
, "enable",
23 "Enable statistics collection", nullptr,
24 eCommandProcessMustBePaused
) {}
26 ~CommandObjectStatsEnable() override
= default;
29 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
30 if (DebuggerStats::GetCollectingStats()) {
31 result
.AppendError("statistics already enabled");
35 DebuggerStats::SetCollectingStats(true);
36 result
.SetStatus(eReturnStatusSuccessFinishResult
);
40 class CommandObjectStatsDisable
: public CommandObjectParsed
{
42 CommandObjectStatsDisable(CommandInterpreter
&interpreter
)
43 : CommandObjectParsed(interpreter
, "disable",
44 "Disable statistics collection", nullptr,
45 eCommandProcessMustBePaused
) {}
47 ~CommandObjectStatsDisable() override
= default;
50 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
51 if (!DebuggerStats::GetCollectingStats()) {
52 result
.AppendError("need to enable statistics before disabling them");
56 DebuggerStats::SetCollectingStats(false);
57 result
.SetStatus(eReturnStatusSuccessFinishResult
);
61 #define LLDB_OPTIONS_statistics_dump
62 #include "CommandOptions.inc"
64 class CommandObjectStatsDump
: public CommandObjectParsed
{
65 class CommandOptions
: public Options
{
67 CommandOptions() { OptionParsingStarting(nullptr); }
69 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
70 ExecutionContext
*execution_context
) override
{
72 const int short_option
= m_getopt_table
[option_idx
].val
;
74 switch (short_option
) {
79 llvm_unreachable("Unimplemented option");
84 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
85 m_all_targets
= false;
88 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
89 return llvm::ArrayRef(g_statistics_dump_options
);
92 bool m_all_targets
= false;
96 CommandObjectStatsDump(CommandInterpreter
&interpreter
)
97 : CommandObjectParsed(
98 interpreter
, "statistics dump", "Dump metrics in JSON format",
99 "statistics dump [<options>]", eCommandRequiresTarget
) {}
101 ~CommandObjectStatsDump() override
= default;
103 Options
*GetOptions() override
{ return &m_options
; }
106 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
107 Target
*target
= nullptr;
108 if (!m_options
.m_all_targets
)
109 target
= m_exe_ctx
.GetTargetPtr();
111 result
.AppendMessageWithFormatv(
112 "{0:2}", DebuggerStats::ReportStatistics(GetDebugger(), target
));
113 result
.SetStatus(eReturnStatusSuccessFinishResult
);
116 CommandOptions m_options
;
119 CommandObjectStats::CommandObjectStats(CommandInterpreter
&interpreter
)
120 : CommandObjectMultiword(interpreter
, "statistics",
121 "Print statistics about a debugging session",
122 "statistics <subcommand> [<subcommand-options>]") {
123 LoadSubCommand("enable",
124 CommandObjectSP(new CommandObjectStatsEnable(interpreter
)));
125 LoadSubCommand("disable",
126 CommandObjectSP(new CommandObjectStatsDisable(interpreter
)));
127 LoadSubCommand("dump",
128 CommandObjectSP(new CommandObjectStatsDump(interpreter
)));
131 CommandObjectStats::~CommandObjectStats() = default;