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 "lldb/lldb-python.h"
12 #include "CommandObjectLog.h"
16 // Other libraries and framework includes
18 #include "lldb/lldb-private-log.h"
20 #include "lldb/Interpreter/Args.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Host/FileSpec.h"
23 #include "lldb/Core/Log.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Interpreter/Options.h"
26 #include "lldb/Core/RegularExpression.h"
27 #include "lldb/Core/Stream.h"
28 #include "lldb/Core/StreamFile.h"
29 #include "lldb/Core/Timer.h"
31 #include "lldb/Core/Debugger.h"
32 #include "lldb/Interpreter/CommandInterpreter.h"
33 #include "lldb/Interpreter/CommandReturnObject.h"
35 #include "lldb/Symbol/LineTable.h"
36 #include "lldb/Symbol/ObjectFile.h"
37 #include "lldb/Symbol/SymbolFile.h"
38 #include "lldb/Symbol/SymbolVendor.h"
40 #include "lldb/Target/Process.h"
41 #include "lldb/Target/Target.h"
44 using namespace lldb_private;
47 class CommandObjectLogEnable : public CommandObjectParsed
50 //------------------------------------------------------------------
51 // Constructors and Destructors
52 //------------------------------------------------------------------
53 CommandObjectLogEnable(CommandInterpreter &interpreter) :
54 CommandObjectParsed (interpreter,
56 "Enable logging for a single log channel.",
58 m_options (interpreter)
61 CommandArgumentEntry arg1;
62 CommandArgumentEntry arg2;
63 CommandArgumentData channel_arg;
64 CommandArgumentData category_arg;
66 // Define the first (and only) variant of this arg.
67 channel_arg.arg_type = eArgTypeLogChannel;
68 channel_arg.arg_repetition = eArgRepeatPlain;
70 // There is only one variant this argument could be; put it into the argument entry.
71 arg1.push_back (channel_arg);
73 category_arg.arg_type = eArgTypeLogCategory;
74 category_arg.arg_repetition = eArgRepeatPlus;
76 arg2.push_back (category_arg);
78 // Push the data for the first argument into the m_arguments vector.
79 m_arguments.push_back (arg1);
80 m_arguments.push_back (arg2);
84 ~CommandObjectLogEnable()
95 // HandleArgumentCompletion (Args &input,
97 // int &cursor_char_position,
98 // OptionElementVector &opt_element_vector,
99 // int match_start_point,
100 // int max_return_elements,
101 // bool &word_complete,
102 // StringList &matches)
104 // std::string completion_str (input.GetArgumentAtIndex(cursor_index));
105 // completion_str.erase (cursor_char_position);
107 // if (cursor_index == 1)
110 // Log::AutoCompleteChannelName (completion_str.c_str(), matches);
112 // return matches.GetSize();
116 class CommandOptions : public Options
120 CommandOptions (CommandInterpreter &interpreter) :
121 Options (interpreter),
134 SetOptionValue (uint32_t option_idx, const char *option_arg)
137 const int short_option = m_getopt_table[option_idx].val;
139 switch (short_option)
141 case 'f': log_file.SetFile(option_arg, true); break;
142 case 't': log_options |= LLDB_LOG_OPTION_THREADSAFE; break;
143 case 'v': log_options |= LLDB_LOG_OPTION_VERBOSE; break;
144 case 'g': log_options |= LLDB_LOG_OPTION_DEBUG; break;
145 case 's': log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE; break;
146 case 'T': log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP; break;
147 case 'p': log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;break;
148 case 'n': log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME; break;
149 case 'S': log_options |= LLDB_LOG_OPTION_BACKTRACE; break;
151 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
159 OptionParsingStarting ()
165 const OptionDefinition*
168 return g_option_table;
171 // Options table: Required for subclasses of Options.
173 static OptionDefinition g_option_table[];
175 // Instance variables to hold the values for command options.
178 uint32_t log_options;
183 DoExecute (Args& args,
184 CommandReturnObject &result)
186 if (args.GetArgumentCount() < 2)
188 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str());
192 std::string channel(args.GetArgumentAtIndex(0));
193 args.Shift (); // Shift off the channel
194 char log_file[PATH_MAX];
195 if (m_options.log_file)
196 m_options.log_file.GetPath(log_file, sizeof(log_file));
199 bool success = m_interpreter.GetDebugger().EnableLog (channel.c_str(),
200 args.GetConstArgumentVector(),
202 m_options.log_options,
203 result.GetErrorStream());
205 result.SetStatus (eReturnStatusSuccessFinishNoResult);
207 result.SetStatus (eReturnStatusFailed);
209 return result.Succeeded();
212 CommandOptions m_options;
216 CommandObjectLogEnable::CommandOptions::g_option_table[] =
218 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Set the destination file to log to."},
219 { LLDB_OPT_SET_1, false, "threadsafe", 't', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." },
220 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Enable verbose logging." },
221 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Enable debug logging." },
222 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
223 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
224 { LLDB_OPT_SET_1, false, "pid-tid", 'p', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Prepend all log lines with the process and thread ID that generates the log line." },
225 { LLDB_OPT_SET_1, false, "thread-name",'n', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Prepend all log lines with the thread name for the thread that generates the log line." },
226 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Append a stack backtrace to each log line." },
227 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
230 class CommandObjectLogDisable : public CommandObjectParsed
233 //------------------------------------------------------------------
234 // Constructors and Destructors
235 //------------------------------------------------------------------
236 CommandObjectLogDisable(CommandInterpreter &interpreter) :
237 CommandObjectParsed (interpreter,
239 "Disable one or more log channel categories.",
242 CommandArgumentEntry arg1;
243 CommandArgumentEntry arg2;
244 CommandArgumentData channel_arg;
245 CommandArgumentData category_arg;
247 // Define the first (and only) variant of this arg.
248 channel_arg.arg_type = eArgTypeLogChannel;
249 channel_arg.arg_repetition = eArgRepeatPlain;
251 // There is only one variant this argument could be; put it into the argument entry.
252 arg1.push_back (channel_arg);
254 category_arg.arg_type = eArgTypeLogCategory;
255 category_arg.arg_repetition = eArgRepeatPlus;
257 arg2.push_back (category_arg);
259 // Push the data for the first argument into the m_arguments vector.
260 m_arguments.push_back (arg1);
261 m_arguments.push_back (arg2);
265 ~CommandObjectLogDisable()
271 DoExecute (Args& args,
272 CommandReturnObject &result)
274 const size_t argc = args.GetArgumentCount();
277 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str());
281 Log::Callbacks log_callbacks;
283 std::string channel(args.GetArgumentAtIndex(0));
284 args.Shift (); // Shift off the channel
285 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks))
287 log_callbacks.disable (args.GetConstArgumentVector(), &result.GetErrorStream());
288 result.SetStatus(eReturnStatusSuccessFinishNoResult);
290 else if (channel == "all")
292 Log::DisableAllLogChannels(&result.GetErrorStream());
296 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
299 log_channel_sp->Disable(args.GetConstArgumentVector(), &result.GetErrorStream());
300 result.SetStatus(eReturnStatusSuccessFinishNoResult);
303 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
306 return result.Succeeded();
310 class CommandObjectLogList : public CommandObjectParsed
313 //------------------------------------------------------------------
314 // Constructors and Destructors
315 //------------------------------------------------------------------
316 CommandObjectLogList(CommandInterpreter &interpreter) :
317 CommandObjectParsed (interpreter,
319 "List the log categories for one or more log channels. If none specified, lists them all.",
322 CommandArgumentEntry arg;
323 CommandArgumentData channel_arg;
325 // Define the first (and only) variant of this arg.
326 channel_arg.arg_type = eArgTypeLogChannel;
327 channel_arg.arg_repetition = eArgRepeatStar;
329 // There is only one variant this argument could be; put it into the argument entry.
330 arg.push_back (channel_arg);
332 // Push the data for the first argument into the m_arguments vector.
333 m_arguments.push_back (arg);
337 ~CommandObjectLogList()
343 DoExecute (Args& args,
344 CommandReturnObject &result)
346 const size_t argc = args.GetArgumentCount();
349 Log::ListAllLogChannels (&result.GetOutputStream());
350 result.SetStatus(eReturnStatusSuccessFinishResult);
354 for (size_t i=0; i<argc; ++i)
356 Log::Callbacks log_callbacks;
358 std::string channel(args.GetArgumentAtIndex(i));
359 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks))
361 log_callbacks.list_categories (&result.GetOutputStream());
362 result.SetStatus(eReturnStatusSuccessFinishResult);
364 else if (channel == "all")
366 Log::ListAllLogChannels (&result.GetOutputStream());
367 result.SetStatus(eReturnStatusSuccessFinishResult);
371 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
374 log_channel_sp->ListCategories(&result.GetOutputStream());
375 result.SetStatus(eReturnStatusSuccessFinishNoResult);
378 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
382 return result.Succeeded();
386 class CommandObjectLogTimer : public CommandObjectParsed
389 //------------------------------------------------------------------
390 // Constructors and Destructors
391 //------------------------------------------------------------------
392 CommandObjectLogTimer(CommandInterpreter &interpreter) :
393 CommandObjectParsed (interpreter,
395 "Enable, disable, dump, and reset LLDB internal performance timers.",
396 "log timers < enable <depth> | disable | dump | increment <bool> | reset >")
401 ~CommandObjectLogTimer()
407 DoExecute (Args& args,
408 CommandReturnObject &result)
410 const size_t argc = args.GetArgumentCount();
411 result.SetStatus(eReturnStatusFailed);
415 const char *sub_command = args.GetArgumentAtIndex(0);
417 if (strcasecmp(sub_command, "enable") == 0)
419 Timer::SetDisplayDepth (UINT32_MAX);
420 result.SetStatus(eReturnStatusSuccessFinishNoResult);
422 else if (strcasecmp(sub_command, "disable") == 0)
424 Timer::DumpCategoryTimes (&result.GetOutputStream());
425 Timer::SetDisplayDepth (0);
426 result.SetStatus(eReturnStatusSuccessFinishResult);
428 else if (strcasecmp(sub_command, "dump") == 0)
430 Timer::DumpCategoryTimes (&result.GetOutputStream());
431 result.SetStatus(eReturnStatusSuccessFinishResult);
433 else if (strcasecmp(sub_command, "reset") == 0)
435 Timer::ResetCategoryTimes ();
436 result.SetStatus(eReturnStatusSuccessFinishResult);
442 const char *sub_command = args.GetArgumentAtIndex(0);
444 if (strcasecmp(sub_command, "enable") == 0)
447 uint32_t depth = Args::StringToUInt32(args.GetArgumentAtIndex(1), 0, 0, &success);
450 Timer::SetDisplayDepth (depth);
451 result.SetStatus(eReturnStatusSuccessFinishNoResult);
454 result.AppendError("Could not convert enable depth to an unsigned integer.");
456 if (strcasecmp(sub_command, "increment") == 0)
459 bool increment = Args::StringToBoolean(args.GetArgumentAtIndex(1), false, &success);
462 Timer::SetQuiet (!increment);
463 result.SetStatus(eReturnStatusSuccessFinishNoResult);
466 result.AppendError("Could not convert increment value to boolean.");
470 if (!result.Succeeded())
472 result.AppendError("Missing subcommand");
473 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
475 return result.Succeeded();
479 //----------------------------------------------------------------------
480 // CommandObjectLog constructor
481 //----------------------------------------------------------------------
482 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) :
483 CommandObjectMultiword (interpreter,
485 "A set of commands for operating on logs.",
486 "log <command> [<command-options>]")
488 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectLogEnable (interpreter)));
489 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectLogDisable (interpreter)));
490 LoadSubCommand ("list", CommandObjectSP (new CommandObjectLogList (interpreter)));
491 LoadSubCommand ("timers", CommandObjectSP (new CommandObjectLogTimer (interpreter)));
494 //----------------------------------------------------------------------
496 //----------------------------------------------------------------------
497 CommandObjectLog::~CommandObjectLog()