1 //===-- CommandObjectLog.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "CommandObjectLog.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Host/OptionParser.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Interpreter/OptionArgParser.h"
22 #include "lldb/Interpreter/Options.h"
23 #include "lldb/Symbol/LineTable.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/SymbolVendor.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Utility/Args.h"
30 #include "lldb/Utility/FileSpec.h"
31 #include "lldb/Utility/Log.h"
32 #include "lldb/Utility/RegularExpression.h"
33 #include "lldb/Utility/Stream.h"
34 #include "lldb/Utility/Timer.h"
37 using namespace lldb_private;
39 static OptionDefinition g_log_options[] = {
41 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, "Set the destination file to log to." },
42 { LLDB_OPT_SET_1, false, "threadsafe", 't', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." },
43 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose logging." },
44 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
45 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
46 { LLDB_OPT_SET_1, false, "pid-tid", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with the process and thread ID that generates the log line." },
47 { LLDB_OPT_SET_1, false, "thread-name",'n', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with the thread name for the thread that generates the log line." },
48 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append a stack backtrace to each log line." },
49 { LLDB_OPT_SET_1, false, "append", 'a', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append to the log file instead of overwriting." },
50 { LLDB_OPT_SET_1, false, "file-function",'F',OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend the names of files and function that generate the logs." },
54 class CommandObjectLogEnable : public CommandObjectParsed {
56 //------------------------------------------------------------------
57 // Constructors and Destructors
58 //------------------------------------------------------------------
59 CommandObjectLogEnable(CommandInterpreter &interpreter)
60 : CommandObjectParsed(interpreter, "log enable",
61 "Enable logging for a single log channel.",
64 CommandArgumentEntry arg1;
65 CommandArgumentEntry arg2;
66 CommandArgumentData channel_arg;
67 CommandArgumentData category_arg;
69 // Define the first (and only) variant of this arg.
70 channel_arg.arg_type = eArgTypeLogChannel;
71 channel_arg.arg_repetition = eArgRepeatPlain;
73 // There is only one variant this argument could be; put it into the
75 arg1.push_back(channel_arg);
77 category_arg.arg_type = eArgTypeLogCategory;
78 category_arg.arg_repetition = eArgRepeatPlus;
80 arg2.push_back(category_arg);
82 // Push the data for the first argument into the m_arguments vector.
83 m_arguments.push_back(arg1);
84 m_arguments.push_back(arg2);
87 ~CommandObjectLogEnable() override = default;
89 Options *GetOptions() override { return &m_options; }
91 class CommandOptions : public Options {
93 CommandOptions() : Options(), log_file(), log_options(0) {}
95 ~CommandOptions() override = default;
97 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
98 ExecutionContext *execution_context) override {
100 const int short_option = m_getopt_table[option_idx].val;
102 switch (short_option) {
104 log_file.SetFile(option_arg, true, FileSpec::Style::native);
107 log_options |= LLDB_LOG_OPTION_THREADSAFE;
110 log_options |= LLDB_LOG_OPTION_VERBOSE;
113 log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE;
116 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP;
119 log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;
122 log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
125 log_options |= LLDB_LOG_OPTION_BACKTRACE;
128 log_options |= LLDB_LOG_OPTION_APPEND;
131 log_options |= LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION;
134 error.SetErrorStringWithFormat("unrecognized option '%c'",
142 void OptionParsingStarting(ExecutionContext *execution_context) override {
147 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
148 return llvm::makeArrayRef(g_log_options);
151 // Instance variables to hold the values for command options.
154 uint32_t log_options;
158 bool DoExecute(Args &args, CommandReturnObject &result) override {
159 if (args.GetArgumentCount() < 2) {
160 result.AppendErrorWithFormat(
161 "%s takes a log channel and one or more log types.\n",
166 // Store into a std::string since we're about to shift the channel off.
167 const std::string channel = args[0].ref;
168 args.Shift(); // Shift off the channel
169 char log_file[PATH_MAX];
170 if (m_options.log_file)
171 m_options.log_file.GetPath(log_file, sizeof(log_file));
176 llvm::raw_string_ostream error_stream(error);
177 bool success = m_interpreter.GetDebugger().EnableLog(
178 channel, args.GetArgumentArrayRef(), log_file, m_options.log_options,
180 result.GetErrorStream() << error_stream.str();
183 result.SetStatus(eReturnStatusSuccessFinishNoResult);
185 result.SetStatus(eReturnStatusFailed);
186 return result.Succeeded();
189 CommandOptions m_options;
192 class CommandObjectLogDisable : public CommandObjectParsed {
194 //------------------------------------------------------------------
195 // Constructors and Destructors
196 //------------------------------------------------------------------
197 CommandObjectLogDisable(CommandInterpreter &interpreter)
198 : CommandObjectParsed(interpreter, "log disable",
199 "Disable one or more log channel categories.",
201 CommandArgumentEntry arg1;
202 CommandArgumentEntry arg2;
203 CommandArgumentData channel_arg;
204 CommandArgumentData category_arg;
206 // Define the first (and only) variant of this arg.
207 channel_arg.arg_type = eArgTypeLogChannel;
208 channel_arg.arg_repetition = eArgRepeatPlain;
210 // There is only one variant this argument could be; put it into the
212 arg1.push_back(channel_arg);
214 category_arg.arg_type = eArgTypeLogCategory;
215 category_arg.arg_repetition = eArgRepeatPlus;
217 arg2.push_back(category_arg);
219 // Push the data for the first argument into the m_arguments vector.
220 m_arguments.push_back(arg1);
221 m_arguments.push_back(arg2);
224 ~CommandObjectLogDisable() override = default;
227 bool DoExecute(Args &args, CommandReturnObject &result) override {
229 result.AppendErrorWithFormat(
230 "%s takes a log channel and one or more log types.\n",
235 const std::string channel = args[0].ref;
236 args.Shift(); // Shift off the channel
237 if (channel == "all") {
238 Log::DisableAllLogChannels();
239 result.SetStatus(eReturnStatusSuccessFinishNoResult);
242 llvm::raw_string_ostream error_stream(error);
243 if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(),
245 result.SetStatus(eReturnStatusSuccessFinishNoResult);
246 result.GetErrorStream() << error_stream.str();
248 return result.Succeeded();
252 class CommandObjectLogList : public CommandObjectParsed {
254 //------------------------------------------------------------------
255 // Constructors and Destructors
256 //------------------------------------------------------------------
257 CommandObjectLogList(CommandInterpreter &interpreter)
258 : CommandObjectParsed(interpreter, "log list",
259 "List the log categories for one or more log "
260 "channels. If none specified, lists them all.",
262 CommandArgumentEntry arg;
263 CommandArgumentData channel_arg;
265 // Define the first (and only) variant of this arg.
266 channel_arg.arg_type = eArgTypeLogChannel;
267 channel_arg.arg_repetition = eArgRepeatStar;
269 // There is only one variant this argument could be; put it into the
271 arg.push_back(channel_arg);
273 // Push the data for the first argument into the m_arguments vector.
274 m_arguments.push_back(arg);
277 ~CommandObjectLogList() override = default;
280 bool DoExecute(Args &args, CommandReturnObject &result) override {
282 llvm::raw_string_ostream output_stream(output);
284 Log::ListAllLogChannels(output_stream);
285 result.SetStatus(eReturnStatusSuccessFinishResult);
288 for (const auto &entry : args.entries())
290 success && Log::ListChannelCategories(entry.ref, output_stream);
292 result.SetStatus(eReturnStatusSuccessFinishResult);
294 result.GetOutputStream() << output_stream.str();
295 return result.Succeeded();
299 class CommandObjectLogTimer : public CommandObjectParsed {
301 //------------------------------------------------------------------
302 // Constructors and Destructors
303 //------------------------------------------------------------------
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;