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/Debugger.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/RegularExpression.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Core/Timer.h"
23 #include "lldb/Host/FileSpec.h"
24 #include "lldb/Host/StringConvert.h"
25 #include "lldb/Interpreter/Args.h"
26 #include "lldb/Interpreter/CommandInterpreter.h"
27 #include "lldb/Interpreter/CommandReturnObject.h"
28 #include "lldb/Interpreter/Options.h"
29 #include "lldb/Symbol/LineTable.h"
30 #include "lldb/Symbol/ObjectFile.h"
31 #include "lldb/Symbol/SymbolFile.h"
32 #include "lldb/Symbol/SymbolVendor.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/Target.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, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable debug logging." },
45 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
46 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
47 { 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." },
48 { 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." },
49 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append a stack backtrace to each log line." },
50 { LLDB_OPT_SET_1, false, "append", 'a', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Append to the log file instead of overwriting." },
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 Error 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);
107 log_options |= LLDB_LOG_OPTION_THREADSAFE;
110 log_options |= LLDB_LOG_OPTION_VERBOSE;
113 log_options |= LLDB_LOG_OPTION_DEBUG;
116 log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE;
119 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP;
122 log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;
125 log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
128 log_options |= LLDB_LOG_OPTION_BACKTRACE;
131 log_options |= LLDB_LOG_OPTION_APPEND;
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));
174 bool success = m_interpreter.GetDebugger().EnableLog(
175 channel.c_str(), args.GetConstArgumentVector(), log_file,
176 m_options.log_options, result.GetErrorStream());
178 result.SetStatus(eReturnStatusSuccessFinishNoResult);
180 result.SetStatus(eReturnStatusFailed);
181 return result.Succeeded();
184 CommandOptions m_options;
187 class CommandObjectLogDisable : public CommandObjectParsed {
189 //------------------------------------------------------------------
190 // Constructors and Destructors
191 //------------------------------------------------------------------
192 CommandObjectLogDisable(CommandInterpreter &interpreter)
193 : CommandObjectParsed(interpreter, "log disable",
194 "Disable one or more log channel categories.",
196 CommandArgumentEntry arg1;
197 CommandArgumentEntry arg2;
198 CommandArgumentData channel_arg;
199 CommandArgumentData category_arg;
201 // Define the first (and only) variant of this arg.
202 channel_arg.arg_type = eArgTypeLogChannel;
203 channel_arg.arg_repetition = eArgRepeatPlain;
205 // There is only one variant this argument could be; put it into the
207 arg1.push_back(channel_arg);
209 category_arg.arg_type = eArgTypeLogCategory;
210 category_arg.arg_repetition = eArgRepeatPlus;
212 arg2.push_back(category_arg);
214 // Push the data for the first argument into the m_arguments vector.
215 m_arguments.push_back(arg1);
216 m_arguments.push_back(arg2);
219 ~CommandObjectLogDisable() override = default;
222 bool DoExecute(Args &args, CommandReturnObject &result) override {
224 result.AppendErrorWithFormat(
225 "%s takes a log channel and one or more log types.\n",
230 Log::Callbacks log_callbacks;
232 const std::string channel = args[0].ref;
233 args.Shift(); // Shift off the channel
234 if (Log::GetLogChannelCallbacks(ConstString(channel), log_callbacks)) {
235 log_callbacks.disable(args.GetConstArgumentVector(),
236 &result.GetErrorStream());
237 result.SetStatus(eReturnStatusSuccessFinishNoResult);
238 } else if (channel == "all") {
239 Log::DisableAllLogChannels(&result.GetErrorStream());
241 LogChannelSP log_channel_sp(LogChannel::FindPlugin(channel.data()));
242 if (log_channel_sp) {
243 log_channel_sp->Disable(args.GetConstArgumentVector(),
244 &result.GetErrorStream());
245 result.SetStatus(eReturnStatusSuccessFinishNoResult);
247 result.AppendErrorWithFormat("Invalid log channel '%s'.\n",
250 return result.Succeeded();
254 class CommandObjectLogList : public CommandObjectParsed {
256 //------------------------------------------------------------------
257 // Constructors and Destructors
258 //------------------------------------------------------------------
259 CommandObjectLogList(CommandInterpreter &interpreter)
260 : CommandObjectParsed(interpreter, "log list",
261 "List the log categories for one or more log "
262 "channels. If none specified, lists them all.",
264 CommandArgumentEntry arg;
265 CommandArgumentData channel_arg;
267 // Define the first (and only) variant of this arg.
268 channel_arg.arg_type = eArgTypeLogChannel;
269 channel_arg.arg_repetition = eArgRepeatStar;
271 // There is only one variant this argument could be; put it into the
273 arg.push_back(channel_arg);
275 // Push the data for the first argument into the m_arguments vector.
276 m_arguments.push_back(arg);
279 ~CommandObjectLogList() override = default;
282 bool DoExecute(Args &args, CommandReturnObject &result) override {
284 Log::ListAllLogChannels(&result.GetOutputStream());
285 result.SetStatus(eReturnStatusSuccessFinishResult);
287 for (auto &entry : args.entries()) {
288 Log::Callbacks log_callbacks;
290 if (Log::GetLogChannelCallbacks(ConstString(entry.ref),
292 log_callbacks.list_categories(&result.GetOutputStream());
293 result.SetStatus(eReturnStatusSuccessFinishResult);
294 } else if (entry.ref == "all") {
295 Log::ListAllLogChannels(&result.GetOutputStream());
296 result.SetStatus(eReturnStatusSuccessFinishResult);
298 LogChannelSP log_channel_sp(LogChannel::FindPlugin(entry.c_str()));
299 if (log_channel_sp) {
300 log_channel_sp->ListCategories(&result.GetOutputStream());
301 result.SetStatus(eReturnStatusSuccessFinishNoResult);
303 result.AppendErrorWithFormat("Invalid log channel '%s'.\n",
308 return result.Succeeded();
312 class CommandObjectLogTimer : public CommandObjectParsed {
314 //------------------------------------------------------------------
315 // Constructors and Destructors
316 //------------------------------------------------------------------
317 CommandObjectLogTimer(CommandInterpreter &interpreter)
318 : CommandObjectParsed(interpreter, "log timers",
319 "Enable, disable, dump, and reset LLDB internal "
320 "performance timers.",
321 "log timers < enable <depth> | disable | dump | "
322 "increment <bool> | reset >") {}
324 ~CommandObjectLogTimer() override = default;
327 bool DoExecute(Args &args, CommandReturnObject &result) override {
328 result.SetStatus(eReturnStatusFailed);
330 if (args.GetArgumentCount() == 1) {
331 auto sub_command = args[0].ref;
333 if (sub_command.equals_lower("enable")) {
334 Timer::SetDisplayDepth(UINT32_MAX);
335 result.SetStatus(eReturnStatusSuccessFinishNoResult);
336 } else if (sub_command.equals_lower("disable")) {
337 Timer::DumpCategoryTimes(&result.GetOutputStream());
338 Timer::SetDisplayDepth(0);
339 result.SetStatus(eReturnStatusSuccessFinishResult);
340 } else if (sub_command.equals_lower("dump")) {
341 Timer::DumpCategoryTimes(&result.GetOutputStream());
342 result.SetStatus(eReturnStatusSuccessFinishResult);
343 } else if (sub_command.equals_lower("reset")) {
344 Timer::ResetCategoryTimes();
345 result.SetStatus(eReturnStatusSuccessFinishResult);
347 } else if (args.GetArgumentCount() == 2) {
348 auto sub_command = args[0].ref;
349 auto param = args[1].ref;
351 if (sub_command.equals_lower("enable")) {
353 if (param.consumeInteger(0, depth)) {
355 "Could not convert enable depth to an unsigned integer.");
357 Timer::SetDisplayDepth(depth);
358 result.SetStatus(eReturnStatusSuccessFinishNoResult);
360 } else if (sub_command.equals_lower("increment")) {
362 bool increment = Args::StringToBoolean(param, false, &success);
364 Timer::SetQuiet(!increment);
365 result.SetStatus(eReturnStatusSuccessFinishNoResult);
367 result.AppendError("Could not convert increment value to boolean.");
371 if (!result.Succeeded()) {
372 result.AppendError("Missing subcommand");
373 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
375 return result.Succeeded();
379 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter)
380 : CommandObjectMultiword(interpreter, "log",
381 "Commands controlling LLDB internal logging.",
382 "log <subcommand> [<command-options>]") {
383 LoadSubCommand("enable",
384 CommandObjectSP(new CommandObjectLogEnable(interpreter)));
385 LoadSubCommand("disable",
386 CommandObjectSP(new CommandObjectLogDisable(interpreter)));
387 LoadSubCommand("list",
388 CommandObjectSP(new CommandObjectLogList(interpreter)));
389 LoadSubCommand("timers",
390 CommandObjectSP(new CommandObjectLogTimer(interpreter)));
393 CommandObjectLog::~CommandObjectLog() = default;