1 //===-- CommandObjectLog.cpp ------------------------------------*- C++ -*-===//
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 "CommandObjectLog.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Host/OptionParser.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 #include "lldb/Interpreter/OptionArgParser.h"
14 #include "lldb/Interpreter/Options.h"
15 #include "lldb/Utility/Args.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/Timer.h"
22 using namespace lldb_private
;
24 #define LLDB_OPTIONS_log
25 #include "CommandOptions.inc"
27 /// Common completion logic for log enable/disable.
28 static void CompleteEnableDisable(CompletionRequest
&request
) {
29 size_t arg_index
= request
.GetCursorIndex();
30 if (arg_index
== 0) { // We got: log enable/disable x[tab]
31 for (llvm::StringRef channel
: Log::ListChannels())
32 request
.TryCompleteCurrentArg(channel
);
33 } else if (arg_index
>= 1) { // We got: log enable/disable channel x[tab]
34 llvm::StringRef channel
= request
.GetParsedLine().GetArgumentAtIndex(0);
35 Log::ForEachChannelCategory(
36 channel
, [&request
](llvm::StringRef name
, llvm::StringRef desc
) {
37 request
.TryCompleteCurrentArg(name
, desc
);
42 class CommandObjectLogEnable
: public CommandObjectParsed
{
44 // Constructors and Destructors
45 CommandObjectLogEnable(CommandInterpreter
&interpreter
)
46 : CommandObjectParsed(interpreter
, "log enable",
47 "Enable logging for a single log channel.",
50 CommandArgumentEntry arg1
;
51 CommandArgumentEntry arg2
;
52 CommandArgumentData channel_arg
;
53 CommandArgumentData category_arg
;
55 // Define the first (and only) variant of this arg.
56 channel_arg
.arg_type
= eArgTypeLogChannel
;
57 channel_arg
.arg_repetition
= eArgRepeatPlain
;
59 // There is only one variant this argument could be; put it into the
61 arg1
.push_back(channel_arg
);
63 category_arg
.arg_type
= eArgTypeLogCategory
;
64 category_arg
.arg_repetition
= eArgRepeatPlus
;
66 arg2
.push_back(category_arg
);
68 // Push the data for the first argument into the m_arguments vector.
69 m_arguments
.push_back(arg1
);
70 m_arguments
.push_back(arg2
);
73 ~CommandObjectLogEnable() override
= default;
75 Options
*GetOptions() override
{ return &m_options
; }
77 class CommandOptions
: public Options
{
79 CommandOptions() : Options(), log_file(), log_options(0) {}
81 ~CommandOptions() override
= default;
83 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
84 ExecutionContext
*execution_context
) override
{
86 const int short_option
= m_getopt_table
[option_idx
].val
;
88 switch (short_option
) {
90 log_file
.SetFile(option_arg
, FileSpec::Style::native
);
91 FileSystem::Instance().Resolve(log_file
);
94 log_options
|= LLDB_LOG_OPTION_THREADSAFE
;
97 log_options
|= LLDB_LOG_OPTION_VERBOSE
;
100 log_options
|= LLDB_LOG_OPTION_PREPEND_SEQUENCE
;
103 log_options
|= LLDB_LOG_OPTION_PREPEND_TIMESTAMP
;
106 log_options
|= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD
;
109 log_options
|= LLDB_LOG_OPTION_PREPEND_THREAD_NAME
;
112 log_options
|= LLDB_LOG_OPTION_BACKTRACE
;
115 log_options
|= LLDB_LOG_OPTION_APPEND
;
118 log_options
|= LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION
;
121 llvm_unreachable("Unimplemented option");
127 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
132 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
133 return llvm::makeArrayRef(g_log_options
);
136 // Instance variables to hold the values for command options.
139 uint32_t log_options
;
143 HandleArgumentCompletion(CompletionRequest
&request
,
144 OptionElementVector
&opt_element_vector
) override
{
145 CompleteEnableDisable(request
);
149 bool DoExecute(Args
&args
, CommandReturnObject
&result
) override
{
150 if (args
.GetArgumentCount() < 2) {
151 result
.AppendErrorWithFormat(
152 "%s takes a log channel and one or more log types.\n",
154 result
.SetStatus(eReturnStatusFailed
);
158 // Store into a std::string since we're about to shift the channel off.
159 const std::string channel
= args
[0].ref();
160 args
.Shift(); // Shift off the channel
161 char log_file
[PATH_MAX
];
162 if (m_options
.log_file
)
163 m_options
.log_file
.GetPath(log_file
, sizeof(log_file
));
168 llvm::raw_string_ostream
error_stream(error
);
170 GetDebugger().EnableLog(channel
, args
.GetArgumentArrayRef(), log_file
,
171 m_options
.log_options
, error_stream
);
172 result
.GetErrorStream() << error_stream
.str();
175 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
177 result
.SetStatus(eReturnStatusFailed
);
178 return result
.Succeeded();
181 CommandOptions m_options
;
184 class CommandObjectLogDisable
: public CommandObjectParsed
{
186 // Constructors and Destructors
187 CommandObjectLogDisable(CommandInterpreter
&interpreter
)
188 : CommandObjectParsed(interpreter
, "log disable",
189 "Disable one or more log channel categories.",
191 CommandArgumentEntry arg1
;
192 CommandArgumentEntry arg2
;
193 CommandArgumentData channel_arg
;
194 CommandArgumentData category_arg
;
196 // Define the first (and only) variant of this arg.
197 channel_arg
.arg_type
= eArgTypeLogChannel
;
198 channel_arg
.arg_repetition
= eArgRepeatPlain
;
200 // There is only one variant this argument could be; put it into the
202 arg1
.push_back(channel_arg
);
204 category_arg
.arg_type
= eArgTypeLogCategory
;
205 category_arg
.arg_repetition
= eArgRepeatPlus
;
207 arg2
.push_back(category_arg
);
209 // Push the data for the first argument into the m_arguments vector.
210 m_arguments
.push_back(arg1
);
211 m_arguments
.push_back(arg2
);
214 ~CommandObjectLogDisable() override
= default;
217 HandleArgumentCompletion(CompletionRequest
&request
,
218 OptionElementVector
&opt_element_vector
) override
{
219 CompleteEnableDisable(request
);
223 bool DoExecute(Args
&args
, CommandReturnObject
&result
) override
{
225 result
.AppendErrorWithFormat(
226 "%s takes a log channel and one or more log types.\n",
228 result
.SetStatus(eReturnStatusFailed
);
232 const std::string channel
= args
[0].ref();
233 args
.Shift(); // Shift off the channel
234 if (channel
== "all") {
235 Log::DisableAllLogChannels();
236 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
239 llvm::raw_string_ostream
error_stream(error
);
240 if (Log::DisableLogChannel(channel
, args
.GetArgumentArrayRef(),
242 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
243 result
.GetErrorStream() << error_stream
.str();
245 return result
.Succeeded();
249 class CommandObjectLogList
: public CommandObjectParsed
{
251 // Constructors and Destructors
252 CommandObjectLogList(CommandInterpreter
&interpreter
)
253 : CommandObjectParsed(interpreter
, "log list",
254 "List the log categories for one or more log "
255 "channels. If none specified, lists them all.",
257 CommandArgumentEntry arg
;
258 CommandArgumentData channel_arg
;
260 // Define the first (and only) variant of this arg.
261 channel_arg
.arg_type
= eArgTypeLogChannel
;
262 channel_arg
.arg_repetition
= eArgRepeatStar
;
264 // There is only one variant this argument could be; put it into the
266 arg
.push_back(channel_arg
);
268 // Push the data for the first argument into the m_arguments vector.
269 m_arguments
.push_back(arg
);
272 ~CommandObjectLogList() override
= default;
275 HandleArgumentCompletion(CompletionRequest
&request
,
276 OptionElementVector
&opt_element_vector
) override
{
277 for (llvm::StringRef channel
: Log::ListChannels())
278 request
.TryCompleteCurrentArg(channel
);
282 bool DoExecute(Args
&args
, CommandReturnObject
&result
) override
{
284 llvm::raw_string_ostream
output_stream(output
);
286 Log::ListAllLogChannels(output_stream
);
287 result
.SetStatus(eReturnStatusSuccessFinishResult
);
290 for (const auto &entry
: args
.entries())
292 success
&& Log::ListChannelCategories(entry
.ref(), output_stream
);
294 result
.SetStatus(eReturnStatusSuccessFinishResult
);
296 result
.GetOutputStream() << output_stream
.str();
297 return result
.Succeeded();
301 class CommandObjectLogTimer
: public CommandObjectParsed
{
303 // Constructors and Destructors
304 CommandObjectLogTimer(CommandInterpreter
&interpreter
)
305 : CommandObjectParsed(interpreter
, "log timers",
306 "Enable, disable, dump, and reset LLDB internal "
307 "performance timers.",
308 "log timers < enable <depth> | disable | dump | "
309 "increment <bool> | reset >") {}
311 ~CommandObjectLogTimer() override
= default;
314 bool DoExecute(Args
&args
, CommandReturnObject
&result
) override
{
315 result
.SetStatus(eReturnStatusFailed
);
317 if (args
.GetArgumentCount() == 1) {
318 auto sub_command
= args
[0].ref();
320 if (sub_command
.equals_lower("enable")) {
321 Timer::SetDisplayDepth(UINT32_MAX
);
322 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
323 } else if (sub_command
.equals_lower("disable")) {
324 Timer::DumpCategoryTimes(&result
.GetOutputStream());
325 Timer::SetDisplayDepth(0);
326 result
.SetStatus(eReturnStatusSuccessFinishResult
);
327 } else if (sub_command
.equals_lower("dump")) {
328 Timer::DumpCategoryTimes(&result
.GetOutputStream());
329 result
.SetStatus(eReturnStatusSuccessFinishResult
);
330 } else if (sub_command
.equals_lower("reset")) {
331 Timer::ResetCategoryTimes();
332 result
.SetStatus(eReturnStatusSuccessFinishResult
);
334 } else if (args
.GetArgumentCount() == 2) {
335 auto sub_command
= args
[0].ref();
336 auto param
= args
[1].ref();
338 if (sub_command
.equals_lower("enable")) {
340 if (param
.consumeInteger(0, depth
)) {
342 "Could not convert enable depth to an unsigned integer.");
344 Timer::SetDisplayDepth(depth
);
345 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
347 } else if (sub_command
.equals_lower("increment")) {
349 bool increment
= OptionArgParser::ToBoolean(param
, false, &success
);
351 Timer::SetQuiet(!increment
);
352 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
354 result
.AppendError("Could not convert increment value to boolean.");
358 if (!result
.Succeeded()) {
359 result
.AppendError("Missing subcommand");
360 result
.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax
.c_str());
362 return result
.Succeeded();
366 CommandObjectLog::CommandObjectLog(CommandInterpreter
&interpreter
)
367 : CommandObjectMultiword(interpreter
, "log",
368 "Commands controlling LLDB internal logging.",
369 "log <subcommand> [<command-options>]") {
370 LoadSubCommand("enable",
371 CommandObjectSP(new CommandObjectLogEnable(interpreter
)));
372 LoadSubCommand("disable",
373 CommandObjectSP(new CommandObjectLogDisable(interpreter
)));
374 LoadSubCommand("list",
375 CommandObjectSP(new CommandObjectLogList(interpreter
)));
376 LoadSubCommand("timers",
377 CommandObjectSP(new CommandObjectLogTimer(interpreter
)));
380 CommandObjectLog::~CommandObjectLog() = default;