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 //===----------------------------------------------------------------------===//
10 #include "CommandObjectLog.h"
14 // Other libraries and framework includes
16 #include "lldb/Interpreter/Args.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Host/FileSpec.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Interpreter/Options.h"
22 #include "lldb/Core/RegularExpression.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Core/StreamFile.h"
25 #include "lldb/Core/Timer.h"
27 #include "lldb/Core/Debugger.h"
28 #include "lldb/Host/StringConvert.h"
29 #include "lldb/Interpreter/CommandInterpreter.h"
30 #include "lldb/Interpreter/CommandReturnObject.h"
32 #include "lldb/Symbol/LineTable.h"
33 #include "lldb/Symbol/ObjectFile.h"
34 #include "lldb/Symbol/SymbolFile.h"
35 #include "lldb/Symbol/SymbolVendor.h"
37 #include "lldb/Target/Process.h"
38 #include "lldb/Target/Target.h"
41 using namespace lldb_private;
44 class CommandObjectLogEnable : public CommandObjectParsed
47 //------------------------------------------------------------------
48 // Constructors and Destructors
49 //------------------------------------------------------------------
50 CommandObjectLogEnable(CommandInterpreter &interpreter) :
51 CommandObjectParsed (interpreter,
53 "Enable logging for a single log channel.",
55 m_options (interpreter)
58 CommandArgumentEntry arg1;
59 CommandArgumentEntry arg2;
60 CommandArgumentData channel_arg;
61 CommandArgumentData category_arg;
63 // Define the first (and only) variant of this arg.
64 channel_arg.arg_type = eArgTypeLogChannel;
65 channel_arg.arg_repetition = eArgRepeatPlain;
67 // There is only one variant this argument could be; put it into the argument entry.
68 arg1.push_back (channel_arg);
70 category_arg.arg_type = eArgTypeLogCategory;
71 category_arg.arg_repetition = eArgRepeatPlus;
73 arg2.push_back (category_arg);
75 // Push the data for the first argument into the m_arguments vector.
76 m_arguments.push_back (arg1);
77 m_arguments.push_back (arg2);
81 ~CommandObjectLogEnable()
92 // HandleArgumentCompletion (Args &input,
94 // int &cursor_char_position,
95 // OptionElementVector &opt_element_vector,
96 // int match_start_point,
97 // int max_return_elements,
98 // bool &word_complete,
99 // StringList &matches)
101 // std::string completion_str (input.GetArgumentAtIndex(cursor_index));
102 // completion_str.erase (cursor_char_position);
104 // if (cursor_index == 1)
107 // Log::AutoCompleteChannelName (completion_str.c_str(), matches);
109 // return matches.GetSize();
113 class CommandOptions : public Options
117 CommandOptions (CommandInterpreter &interpreter) :
118 Options (interpreter),
131 SetOptionValue (uint32_t option_idx, const char *option_arg)
134 const int short_option = m_getopt_table[option_idx].val;
136 switch (short_option)
138 case 'f': log_file.SetFile(option_arg, true); break;
139 case 't': log_options |= LLDB_LOG_OPTION_THREADSAFE; break;
140 case 'v': log_options |= LLDB_LOG_OPTION_VERBOSE; break;
141 case 'g': log_options |= LLDB_LOG_OPTION_DEBUG; break;
142 case 's': log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE; break;
143 case 'T': log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP; break;
144 case 'p': log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;break;
145 case 'n': log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME; break;
146 case 'S': log_options |= LLDB_LOG_OPTION_BACKTRACE; break;
147 case 'a': log_options |= LLDB_LOG_OPTION_APPEND; break;
149 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
157 OptionParsingStarting ()
163 const OptionDefinition*
166 return g_option_table;
169 // Options table: Required for subclasses of Options.
171 static OptionDefinition g_option_table[];
173 // Instance variables to hold the values for command options.
176 uint32_t log_options;
181 DoExecute (Args& args,
182 CommandReturnObject &result)
184 if (args.GetArgumentCount() < 2)
186 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str());
190 std::string channel(args.GetArgumentAtIndex(0));
191 args.Shift (); // Shift off the channel
192 char log_file[PATH_MAX];
193 if (m_options.log_file)
194 m_options.log_file.GetPath(log_file, sizeof(log_file));
197 bool success = m_interpreter.GetDebugger().EnableLog (channel.c_str(),
198 args.GetConstArgumentVector(),
200 m_options.log_options,
201 result.GetErrorStream());
203 result.SetStatus (eReturnStatusSuccessFinishNoResult);
205 result.SetStatus (eReturnStatusFailed);
207 return result.Succeeded();
210 CommandOptions m_options;
214 CommandObjectLogEnable::CommandOptions::g_option_table[] =
216 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Set the destination file to log to."},
217 { LLDB_OPT_SET_1, false, "threadsafe", 't', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." },
218 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose logging." },
219 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable debug logging." },
220 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
221 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
222 { LLDB_OPT_SET_1, false, "pid-tid", 'p', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with the process and thread ID that generates the log line." },
223 { LLDB_OPT_SET_1, false, "thread-name",'n', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with the thread name for the thread that generates the log line." },
224 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Append a stack backtrace to each log line." },
225 { LLDB_OPT_SET_1, false, "append", 'a', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Append to the log file instead of overwriting." },
226 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
229 class CommandObjectLogDisable : public CommandObjectParsed
232 //------------------------------------------------------------------
233 // Constructors and Destructors
234 //------------------------------------------------------------------
235 CommandObjectLogDisable(CommandInterpreter &interpreter) :
236 CommandObjectParsed (interpreter,
238 "Disable one or more log channel categories.",
241 CommandArgumentEntry arg1;
242 CommandArgumentEntry arg2;
243 CommandArgumentData channel_arg;
244 CommandArgumentData category_arg;
246 // Define the first (and only) variant of this arg.
247 channel_arg.arg_type = eArgTypeLogChannel;
248 channel_arg.arg_repetition = eArgRepeatPlain;
250 // There is only one variant this argument could be; put it into the argument entry.
251 arg1.push_back (channel_arg);
253 category_arg.arg_type = eArgTypeLogCategory;
254 category_arg.arg_repetition = eArgRepeatPlus;
256 arg2.push_back (category_arg);
258 // Push the data for the first argument into the m_arguments vector.
259 m_arguments.push_back (arg1);
260 m_arguments.push_back (arg2);
264 ~CommandObjectLogDisable()
270 DoExecute (Args& args,
271 CommandReturnObject &result)
273 const size_t argc = args.GetArgumentCount();
276 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str());
280 Log::Callbacks log_callbacks;
282 std::string channel(args.GetArgumentAtIndex(0));
283 args.Shift (); // Shift off the channel
284 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks))
286 log_callbacks.disable (args.GetConstArgumentVector(), &result.GetErrorStream());
287 result.SetStatus(eReturnStatusSuccessFinishNoResult);
289 else if (channel == "all")
291 Log::DisableAllLogChannels(&result.GetErrorStream());
295 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
298 log_channel_sp->Disable(args.GetConstArgumentVector(), &result.GetErrorStream());
299 result.SetStatus(eReturnStatusSuccessFinishNoResult);
302 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
305 return result.Succeeded();
309 class CommandObjectLogList : public CommandObjectParsed
312 //------------------------------------------------------------------
313 // Constructors and Destructors
314 //------------------------------------------------------------------
315 CommandObjectLogList(CommandInterpreter &interpreter) :
316 CommandObjectParsed (interpreter,
318 "List the log categories for one or more log channels. If none specified, lists them all.",
321 CommandArgumentEntry arg;
322 CommandArgumentData channel_arg;
324 // Define the first (and only) variant of this arg.
325 channel_arg.arg_type = eArgTypeLogChannel;
326 channel_arg.arg_repetition = eArgRepeatStar;
328 // There is only one variant this argument could be; put it into the argument entry.
329 arg.push_back (channel_arg);
331 // Push the data for the first argument into the m_arguments vector.
332 m_arguments.push_back (arg);
336 ~CommandObjectLogList()
342 DoExecute (Args& args,
343 CommandReturnObject &result)
345 const size_t argc = args.GetArgumentCount();
348 Log::ListAllLogChannels (&result.GetOutputStream());
349 result.SetStatus(eReturnStatusSuccessFinishResult);
353 for (size_t i=0; i<argc; ++i)
355 Log::Callbacks log_callbacks;
357 std::string channel(args.GetArgumentAtIndex(i));
358 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks))
360 log_callbacks.list_categories (&result.GetOutputStream());
361 result.SetStatus(eReturnStatusSuccessFinishResult);
363 else if (channel == "all")
365 Log::ListAllLogChannels (&result.GetOutputStream());
366 result.SetStatus(eReturnStatusSuccessFinishResult);
370 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
373 log_channel_sp->ListCategories(&result.GetOutputStream());
374 result.SetStatus(eReturnStatusSuccessFinishNoResult);
377 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
381 return result.Succeeded();
385 class CommandObjectLogTimer : public CommandObjectParsed
388 //------------------------------------------------------------------
389 // Constructors and Destructors
390 //------------------------------------------------------------------
391 CommandObjectLogTimer(CommandInterpreter &interpreter) :
392 CommandObjectParsed (interpreter,
394 "Enable, disable, dump, and reset LLDB internal performance timers.",
395 "log timers < enable <depth> | disable | dump | increment <bool> | reset >")
400 ~CommandObjectLogTimer()
406 DoExecute (Args& args,
407 CommandReturnObject &result)
409 const size_t argc = args.GetArgumentCount();
410 result.SetStatus(eReturnStatusFailed);
414 const char *sub_command = args.GetArgumentAtIndex(0);
416 if (strcasecmp(sub_command, "enable") == 0)
418 Timer::SetDisplayDepth (UINT32_MAX);
419 result.SetStatus(eReturnStatusSuccessFinishNoResult);
421 else if (strcasecmp(sub_command, "disable") == 0)
423 Timer::DumpCategoryTimes (&result.GetOutputStream());
424 Timer::SetDisplayDepth (0);
425 result.SetStatus(eReturnStatusSuccessFinishResult);
427 else if (strcasecmp(sub_command, "dump") == 0)
429 Timer::DumpCategoryTimes (&result.GetOutputStream());
430 result.SetStatus(eReturnStatusSuccessFinishResult);
432 else if (strcasecmp(sub_command, "reset") == 0)
434 Timer::ResetCategoryTimes ();
435 result.SetStatus(eReturnStatusSuccessFinishResult);
441 const char *sub_command = args.GetArgumentAtIndex(0);
443 if (strcasecmp(sub_command, "enable") == 0)
446 uint32_t depth = StringConvert::ToUInt32(args.GetArgumentAtIndex(1), 0, 0, &success);
449 Timer::SetDisplayDepth (depth);
450 result.SetStatus(eReturnStatusSuccessFinishNoResult);
453 result.AppendError("Could not convert enable depth to an unsigned integer.");
455 if (strcasecmp(sub_command, "increment") == 0)
458 bool increment = Args::StringToBoolean(args.GetArgumentAtIndex(1), false, &success);
461 Timer::SetQuiet (!increment);
462 result.SetStatus(eReturnStatusSuccessFinishNoResult);
465 result.AppendError("Could not convert increment value to boolean.");
469 if (!result.Succeeded())
471 result.AppendError("Missing subcommand");
472 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
474 return result.Succeeded();
478 //----------------------------------------------------------------------
479 // CommandObjectLog constructor
480 //----------------------------------------------------------------------
481 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) :
482 CommandObjectMultiword (interpreter,
484 "A set of commands for operating on logs.",
485 "log <command> [<command-options>]")
487 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectLogEnable (interpreter)));
488 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectLogDisable (interpreter)));
489 LoadSubCommand ("list", CommandObjectSP (new CommandObjectLogList (interpreter)));
490 LoadSubCommand ("timers", CommandObjectSP (new CommandObjectLogTimer (interpreter)));
493 //----------------------------------------------------------------------
495 //----------------------------------------------------------------------
496 CommandObjectLog::~CommandObjectLog()