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/Args.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 #include "lldb/Interpreter/CommandReturnObject.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/FileSpec.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/RegularExpression.h"
32 #include "lldb/Utility/Stream.h"
33 #include "lldb/Utility/Timer.h"
36 using namespace lldb_private;
38 static OptionDefinition g_log_options[] = {
40 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, "Set the destination file to log to." },
41 { LLDB_OPT_SET_1, false, "threadsafe", 't', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." },
42 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose logging." },
43 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
44 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
45 { 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." },
46 { 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." },
47 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append a stack backtrace to each log line." },
48 { LLDB_OPT_SET_1, false, "append", 'a', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append to the log file instead of overwriting." },
49 { 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." },
53 class CommandObjectLogEnable : public CommandObjectParsed {
55 //------------------------------------------------------------------
56 // Constructors and Destructors
57 //------------------------------------------------------------------
58 CommandObjectLogEnable(CommandInterpreter &interpreter)
59 : CommandObjectParsed(interpreter, "log enable",
60 "Enable logging for a single log channel.",
63 CommandArgumentEntry arg1;
64 CommandArgumentEntry arg2;
65 CommandArgumentData channel_arg;
66 CommandArgumentData category_arg;
68 // Define the first (and only) variant of this arg.
69 channel_arg.arg_type = eArgTypeLogChannel;
70 channel_arg.arg_repetition = eArgRepeatPlain;
72 // There is only one variant this argument could be; put it into the
74 arg1.push_back(channel_arg);
76 category_arg.arg_type = eArgTypeLogCategory;
77 category_arg.arg_repetition = eArgRepeatPlus;
79 arg2.push_back(category_arg);
81 // Push the data for the first argument into the m_arguments vector.
82 m_arguments.push_back(arg1);
83 m_arguments.push_back(arg2);
86 ~CommandObjectLogEnable() override = default;
88 Options *GetOptions() override { return &m_options; }
90 class CommandOptions : public Options {
92 CommandOptions() : Options(), log_file(), log_options(0) {}
94 ~CommandOptions() override = default;
96 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
97 ExecutionContext *execution_context) override {
99 const int short_option = m_getopt_table[option_idx].val;
101 switch (short_option) {
103 log_file.SetFile(option_arg, true);
106 log_options |= LLDB_LOG_OPTION_THREADSAFE;
109 log_options |= LLDB_LOG_OPTION_VERBOSE;
112 log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE;
115 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP;
118 log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;
121 log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
124 log_options |= LLDB_LOG_OPTION_BACKTRACE;
127 log_options |= LLDB_LOG_OPTION_APPEND;
130 log_options |= LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION;
133 error.SetErrorStringWithFormat("unrecognized option '%c'",
141 void OptionParsingStarting(ExecutionContext *execution_context) override {
146 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
147 return llvm::makeArrayRef(g_log_options);
150 // Instance variables to hold the values for command options.
153 uint32_t log_options;
157 bool DoExecute(Args &args, CommandReturnObject &result) override {
158 if (args.GetArgumentCount() < 2) {
159 result.AppendErrorWithFormat(
160 "%s takes a log channel and one or more log types.\n",
165 // Store into a std::string since we're about to shift the channel off.
166 const std::string channel = args[0].ref;
167 args.Shift(); // Shift off the channel
168 char log_file[PATH_MAX];
169 if (m_options.log_file)
170 m_options.log_file.GetPath(log_file, sizeof(log_file));
175 llvm::raw_string_ostream error_stream(error);
176 bool success = m_interpreter.GetDebugger().EnableLog(
177 channel, args.GetArgumentArrayRef(), log_file, m_options.log_options,
179 result.GetErrorStream() << error_stream.str();
182 result.SetStatus(eReturnStatusSuccessFinishNoResult);
184 result.SetStatus(eReturnStatusFailed);
185 return result.Succeeded();
188 CommandOptions m_options;
191 class CommandObjectLogDisable : public CommandObjectParsed {
193 //------------------------------------------------------------------
194 // Constructors and Destructors
195 //------------------------------------------------------------------
196 CommandObjectLogDisable(CommandInterpreter &interpreter)
197 : CommandObjectParsed(interpreter, "log disable",
198 "Disable one or more log channel categories.",
200 CommandArgumentEntry arg1;
201 CommandArgumentEntry arg2;
202 CommandArgumentData channel_arg;
203 CommandArgumentData category_arg;
205 // Define the first (and only) variant of this arg.
206 channel_arg.arg_type = eArgTypeLogChannel;
207 channel_arg.arg_repetition = eArgRepeatPlain;
209 // There is only one variant this argument could be; put it into the
211 arg1.push_back(channel_arg);
213 category_arg.arg_type = eArgTypeLogCategory;
214 category_arg.arg_repetition = eArgRepeatPlus;
216 arg2.push_back(category_arg);
218 // Push the data for the first argument into the m_arguments vector.
219 m_arguments.push_back(arg1);
220 m_arguments.push_back(arg2);
223 ~CommandObjectLogDisable() override = default;
226 bool DoExecute(Args &args, CommandReturnObject &result) override {
228 result.AppendErrorWithFormat(
229 "%s takes a log channel and one or more log types.\n",
234 const std::string channel = args[0].ref;
235 args.Shift(); // Shift off the channel
236 if (channel == "all") {
237 Log::DisableAllLogChannels();
238 result.SetStatus(eReturnStatusSuccessFinishNoResult);
241 llvm::raw_string_ostream error_stream(error);
242 if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(),
244 result.SetStatus(eReturnStatusSuccessFinishNoResult);
245 result.GetErrorStream() << error_stream.str();
247 return result.Succeeded();
251 class CommandObjectLogList : public CommandObjectParsed {
253 //------------------------------------------------------------------
254 // Constructors and Destructors
255 //------------------------------------------------------------------
256 CommandObjectLogList(CommandInterpreter &interpreter)
257 : CommandObjectParsed(interpreter, "log list",
258 "List the log categories for one or more log "
259 "channels. If none specified, lists them all.",
261 CommandArgumentEntry arg;
262 CommandArgumentData channel_arg;
264 // Define the first (and only) variant of this arg.
265 channel_arg.arg_type = eArgTypeLogChannel;
266 channel_arg.arg_repetition = eArgRepeatStar;
268 // There is only one variant this argument could be; put it into the
270 arg.push_back(channel_arg);
272 // Push the data for the first argument into the m_arguments vector.
273 m_arguments.push_back(arg);
276 ~CommandObjectLogList() override = default;
279 bool DoExecute(Args &args, CommandReturnObject &result) override {
281 llvm::raw_string_ostream output_stream(output);
283 Log::ListAllLogChannels(output_stream);
284 result.SetStatus(eReturnStatusSuccessFinishResult);
287 for (const auto &entry : args.entries())
289 success && Log::ListChannelCategories(entry.ref, output_stream);
291 result.SetStatus(eReturnStatusSuccessFinishResult);
293 result.GetOutputStream() << output_stream.str();
294 return result.Succeeded();
298 class CommandObjectLogTimer : public CommandObjectParsed {
300 //------------------------------------------------------------------
301 // Constructors and Destructors
302 //------------------------------------------------------------------
303 CommandObjectLogTimer(CommandInterpreter &interpreter)
304 : CommandObjectParsed(interpreter, "log timers",
305 "Enable, disable, dump, and reset LLDB internal "
306 "performance timers.",
307 "log timers < enable <depth> | disable | dump | "
308 "increment <bool> | reset >") {}
310 ~CommandObjectLogTimer() override = default;
313 bool DoExecute(Args &args, CommandReturnObject &result) override {
314 result.SetStatus(eReturnStatusFailed);
316 if (args.GetArgumentCount() == 1) {
317 auto sub_command = args[0].ref;
319 if (sub_command.equals_lower("enable")) {
320 Timer::SetDisplayDepth(UINT32_MAX);
321 result.SetStatus(eReturnStatusSuccessFinishNoResult);
322 } else if (sub_command.equals_lower("disable")) {
323 Timer::DumpCategoryTimes(&result.GetOutputStream());
324 Timer::SetDisplayDepth(0);
325 result.SetStatus(eReturnStatusSuccessFinishResult);
326 } else if (sub_command.equals_lower("dump")) {
327 Timer::DumpCategoryTimes(&result.GetOutputStream());
328 result.SetStatus(eReturnStatusSuccessFinishResult);
329 } else if (sub_command.equals_lower("reset")) {
330 Timer::ResetCategoryTimes();
331 result.SetStatus(eReturnStatusSuccessFinishResult);
333 } else if (args.GetArgumentCount() == 2) {
334 auto sub_command = args[0].ref;
335 auto param = args[1].ref;
337 if (sub_command.equals_lower("enable")) {
339 if (param.consumeInteger(0, depth)) {
341 "Could not convert enable depth to an unsigned integer.");
343 Timer::SetDisplayDepth(depth);
344 result.SetStatus(eReturnStatusSuccessFinishNoResult);
346 } else if (sub_command.equals_lower("increment")) {
348 bool increment = Args::StringToBoolean(param, false, &success);
350 Timer::SetQuiet(!increment);
351 result.SetStatus(eReturnStatusSuccessFinishNoResult);
353 result.AppendError("Could not convert increment value to boolean.");
357 if (!result.Succeeded()) {
358 result.AppendError("Missing subcommand");
359 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
361 return result.Succeeded();
365 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter)
366 : CommandObjectMultiword(interpreter, "log",
367 "Commands controlling LLDB internal logging.",
368 "log <subcommand> [<command-options>]") {
369 LoadSubCommand("enable",
370 CommandObjectSP(new CommandObjectLogEnable(interpreter)));
371 LoadSubCommand("disable",
372 CommandObjectSP(new CommandObjectLogDisable(interpreter)));
373 LoadSubCommand("list",
374 CommandObjectSP(new CommandObjectLogList(interpreter)));
375 LoadSubCommand("timers",
376 CommandObjectSP(new CommandObjectLogTimer(interpreter)));
379 CommandObjectLog::~CommandObjectLog() = default;