blob: 56630c9068c8a0d73e517f6d836b7e8c0f94974d [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectLog.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectLog.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/lldb-private-log.h"
17
Jim Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Debugger.h"
20#include "lldb/Core/FileSpec.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Module.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000023#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/RegularExpression.h"
25#include "lldb/Core/Stream.h"
26#include "lldb/Core/StreamFile.h"
27#include "lldb/Core/Timer.h"
28
Greg Clayton63094e02010-06-23 01:19:29 +000029#include "lldb/Core/Debugger.h"
Sean Callanan705d6782010-06-23 21:28:25 +000030#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Interpreter/CommandReturnObject.h"
32
33#include "lldb/Symbol/LineTable.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Symbol/SymbolFile.h"
36#include "lldb/Symbol/SymbolVendor.h"
37
38#include "lldb/Target/Process.h"
39#include "lldb/Target/Target.h"
40
41using namespace lldb;
42using namespace lldb_private;
43
44
45static LogChannelSP
46GetLogChannelPluginForChannel (const char *channel)
47{
48 std::string log_channel_plugin_name(channel);
49 log_channel_plugin_name += LogChannel::GetPluginSuffix();
50 LogChannelSP log_channel_sp (LogChannel::FindPlugin (log_channel_plugin_name.c_str()));
51 return log_channel_sp;
52}
53
54
55class CommandObjectLogEnable : public CommandObject
56{
57public:
58 //------------------------------------------------------------------
59 // Constructors and Destructors
60 //------------------------------------------------------------------
Greg Clayton238c0a12010-09-18 01:14:36 +000061 CommandObjectLogEnable(CommandInterpreter &interpreter) :
62 CommandObject (interpreter,
63 "log enable",
Chris Lattner24943d22010-06-08 16:52:24 +000064 "Enable logging for a single log channel.",
Caroline Tice43b014a2010-10-04 22:28:36 +000065 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000066 {
Caroline Tice7826c882010-10-26 03:11:13 +000067
68 CommandArgumentEntry arg1;
69 CommandArgumentEntry arg2;
Caroline Tice43b014a2010-10-04 22:28:36 +000070 CommandArgumentData channel_arg;
Caroline Tice7826c882010-10-26 03:11:13 +000071 CommandArgumentData category_arg;
Caroline Tice43b014a2010-10-04 22:28:36 +000072
73 // Define the first (and only) variant of this arg.
74 channel_arg.arg_type = eArgTypeLogChannel;
75 channel_arg.arg_repetition = eArgRepeatPlain;
76
77 // There is only one variant this argument could be; put it into the argument entry.
Caroline Tice7826c882010-10-26 03:11:13 +000078 arg1.push_back (channel_arg);
Caroline Tice43b014a2010-10-04 22:28:36 +000079
Caroline Tice7826c882010-10-26 03:11:13 +000080 category_arg.arg_type = eArgTypeLogCategory;
81 category_arg.arg_repetition = eArgRepeatPlus;
82
83 arg2.push_back (category_arg);
84
Caroline Tice43b014a2010-10-04 22:28:36 +000085 // Push the data for the first argument into the m_arguments vector.
Caroline Tice7826c882010-10-26 03:11:13 +000086 m_arguments.push_back (arg1);
87 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +000088 }
89
90 virtual
91 ~CommandObjectLogEnable()
92 {
93 }
94
95 Options *
96 GetOptions ()
97 {
98 return &m_options;
99 }
100
101 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000102 Execute (Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000103 CommandReturnObject &result)
104 {
105 if (args.GetArgumentCount() < 1)
106 {
Caroline Ticeabb507a2010-09-08 21:06:11 +0000107 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000108 }
109 else
110 {
111 Log::Callbacks log_callbacks;
112
113 std::string channel(args.GetArgumentAtIndex(0));
114 args.Shift (); // Shift off the channel
115 StreamSP log_stream_sp;
116
117 if (m_options.log_file.empty())
118 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000119 log_stream_sp.reset(new StreamFile(m_interpreter.GetDebugger().GetOutputFileHandle()));
Chris Lattner24943d22010-06-08 16:52:24 +0000120 }
121 else
122 {
123 LogStreamMap::iterator pos = m_log_streams.find(m_options.log_file);
124 if (pos == m_log_streams.end())
125 {
126 log_stream_sp.reset (new StreamFile (m_options.log_file.c_str(), "w"));
127 m_log_streams[m_options.log_file] = log_stream_sp;
128 }
129 else
130 log_stream_sp = pos->second;
131 }
132 assert (log_stream_sp.get());
133 uint32_t log_options = m_options.log_options;
134 if (log_options == 0)
135 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
136 if (Log::GetLogChannelCallbacks (channel.c_str(), log_callbacks))
137 {
138 log_callbacks.enable (log_stream_sp, log_options, args, &result.GetErrorStream());
139 result.SetStatus(eReturnStatusSuccessFinishNoResult);
140 }
141 else
142 {
143 LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
144 if (log_channel_sp)
145 {
146 if (log_channel_sp->Enable (log_stream_sp, log_options, &result.GetErrorStream(), args))
147 {
148 result.SetStatus (eReturnStatusSuccessFinishNoResult);
149 }
150 else
151 {
152 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", channel.c_str());
153 result.SetStatus (eReturnStatusFailed);
154 }
155 }
156 else
157 {
158 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", channel.c_str());
159 result.SetStatus (eReturnStatusFailed);
160 }
161 }
162 }
163 return result.Succeeded();
164 }
165
166
167 class CommandOptions : public Options
168 {
169 public:
170
171 CommandOptions () :
172 Options (),
173 log_file (),
174 log_options (0)
175 {
176 }
177
178
179 virtual
180 ~CommandOptions ()
181 {
182 }
183
184 virtual Error
185 SetOptionValue (int option_idx, const char *option_arg)
186 {
187 Error error;
188 char short_option = (char) m_getopt_table[option_idx].val;
189
190 switch (short_option)
191 {
192 case 'f': log_file = option_arg; break;
193 case 't': log_options |= LLDB_LOG_OPTION_THREADSAFE; break;
194 case 'v': log_options |= LLDB_LOG_OPTION_VERBOSE; break;
195 case 'g': log_options |= LLDB_LOG_OPTION_DEBUG; break;
196 case 's': log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE; break;
197 case 'T': log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP; break;
198 case 'p': log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;break;
199 case 'n': log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME; break;
200 default:
201 error.SetErrorStringWithFormat ("Unrecognized option '%c'\n", short_option);
202 break;
203 }
204
205 return error;
206 }
207
208 void
209 ResetOptionValues ()
210 {
211 Options::ResetOptionValues();
212 log_file.clear();
213 log_options = 0;
214 }
215
216 const lldb::OptionDefinition*
217 GetDefinitions ()
218 {
219 return g_option_table;
220 }
221
222 // Options table: Required for subclasses of Options.
223
224 static lldb::OptionDefinition g_option_table[];
225
226 // Instance variables to hold the values for command options.
227
228 std::string log_file;
229 uint32_t log_options;
230 };
231
232protected:
233 typedef std::map<std::string, StreamSP> LogStreamMap;
234 CommandOptions m_options;
235 LogStreamMap m_log_streams;
236};
237
238lldb::OptionDefinition
239CommandObjectLogEnable::CommandOptions::g_option_table[] =
240{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000241{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, 0, eArgTypeFilename, "Set the destination file to log to."},
242{ LLDB_OPT_SET_1, false, "threadsafe", 't', no_argument, NULL, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." },
243{ LLDB_OPT_SET_1, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose logging." },
244{ LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable debug logging." },
245{ LLDB_OPT_SET_1, false, "sequence", 's', no_argument, NULL, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." },
246{ LLDB_OPT_SET_1, false, "timestamp", 'T', no_argument, NULL, 0, eArgTypeNone, "Prepend all log lines with a timestamp." },
247{ LLDB_OPT_SET_1, false, "pid-tid", 'p', no_argument, NULL, 0, eArgTypeNone, "Prepend all log lines with the process and thread ID that generates the log line." },
248{ LLDB_OPT_SET_1, false, "thread-name",'n', no_argument, NULL, 0, eArgTypeNone, "Prepend all log lines with the thread name for the thread that generates the log line." },
249{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000250};
251
252class CommandObjectLogDisable : public CommandObject
253{
254public:
255 //------------------------------------------------------------------
256 // Constructors and Destructors
257 //------------------------------------------------------------------
Greg Clayton238c0a12010-09-18 01:14:36 +0000258 CommandObjectLogDisable(CommandInterpreter &interpreter) :
259 CommandObject (interpreter,
260 "log disable",
261 "Disable one or more log channels.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000262 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000263 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000264 CommandArgumentEntry arg;
265 CommandArgumentData channel_arg;
266
267 // Define the first (and only) variant of this arg.
268 channel_arg.arg_type = eArgTypeLogChannel;
269 channel_arg.arg_repetition = eArgRepeatPlus;
270
271 // There is only one variant this argument could be; put it into the argument entry.
272 arg.push_back (channel_arg);
273
274 // Push the data for the first argument into the m_arguments vector.
275 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000276 }
277
278 virtual
279 ~CommandObjectLogDisable()
280 {
281 }
282
283 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000284 Execute (Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000285 CommandReturnObject &result)
286 {
287 const size_t argc = args.GetArgumentCount();
288 if (argc == 0)
289 {
Caroline Ticeabb507a2010-09-08 21:06:11 +0000290 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000291 }
292 else
293 {
294 for (size_t i=0; i<argc; ++i)
295 {
296 Log::Callbacks log_callbacks;
297
298 std::string channel(args.GetArgumentAtIndex(i));
299 if (Log::GetLogChannelCallbacks (channel.c_str(), log_callbacks))
300 {
301 log_callbacks.disable ();
302 result.SetStatus(eReturnStatusSuccessFinishNoResult);
303 }
304 else if (channel == "all")
305 {
306 Log::DisableAllLogChannels();
307 }
308 else
309 {
310 LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
311 if (log_channel_sp)
312 {
313 log_channel_sp->Disable();
314 result.SetStatus(eReturnStatusSuccessFinishNoResult);
315 }
316 else
317 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
318 }
319 }
320 }
321 return result.Succeeded();
322 }
323};
324
325class CommandObjectLogList : public CommandObject
326{
327public:
328 //------------------------------------------------------------------
329 // Constructors and Destructors
330 //------------------------------------------------------------------
Greg Clayton238c0a12010-09-18 01:14:36 +0000331 CommandObjectLogList(CommandInterpreter &interpreter) :
332 CommandObject (interpreter,
333 "log list",
Caroline Tice43b014a2010-10-04 22:28:36 +0000334 "List the log categories for one or more log channels. If none specified, lists them all.",
335 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000336 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000337 CommandArgumentEntry arg;
338 CommandArgumentData channel_arg;
339
340 // Define the first (and only) variant of this arg.
341 channel_arg.arg_type = eArgTypeLogChannel;
342 channel_arg.arg_repetition = eArgRepeatStar;
343
344 // There is only one variant this argument could be; put it into the argument entry.
345 arg.push_back (channel_arg);
346
347 // Push the data for the first argument into the m_arguments vector.
348 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000349 }
350
351 virtual
352 ~CommandObjectLogList()
353 {
354 }
355
356 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000357 Execute (Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000358 CommandReturnObject &result)
359 {
360 const size_t argc = args.GetArgumentCount();
361 if (argc == 0)
362 {
363 Log::ListAllLogChannels (&result.GetOutputStream());
364 result.SetStatus(eReturnStatusSuccessFinishResult);
365 }
366 else
367 {
368 for (size_t i=0; i<argc; ++i)
369 {
370 Log::Callbacks log_callbacks;
371
372 std::string channel(args.GetArgumentAtIndex(i));
373 if (Log::GetLogChannelCallbacks (channel.c_str(), log_callbacks))
374 {
375 log_callbacks.list_categories (&result.GetOutputStream());
376 result.SetStatus(eReturnStatusSuccessFinishResult);
377 }
378 else if (channel == "all")
379 {
380 Log::ListAllLogChannels (&result.GetOutputStream());
381 result.SetStatus(eReturnStatusSuccessFinishResult);
382 }
383 else
384 {
385 LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
386 if (log_channel_sp)
387 {
388 log_channel_sp->ListCategories(&result.GetOutputStream());
389 result.SetStatus(eReturnStatusSuccessFinishNoResult);
390 }
391 else
392 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0));
393 }
394 }
395 }
396 return result.Succeeded();
397 }
398};
399
400class CommandObjectLogTimer : public CommandObject
401{
402public:
403 //------------------------------------------------------------------
404 // Constructors and Destructors
405 //------------------------------------------------------------------
Greg Clayton238c0a12010-09-18 01:14:36 +0000406 CommandObjectLogTimer(CommandInterpreter &interpreter) :
407 CommandObject (interpreter,
408 "log timers",
Chris Lattner24943d22010-06-08 16:52:24 +0000409 "Enable, disable, dump, and reset LLDB internal performance timers.",
410 "log timers < enable | disable | dump | reset >")
411 {
412 }
413
414 virtual
415 ~CommandObjectLogTimer()
416 {
417 }
418
419 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000420 Execute (Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000421 CommandReturnObject &result)
422 {
423 const size_t argc = args.GetArgumentCount();
424 result.SetStatus(eReturnStatusFailed);
425
426 if (argc == 1)
427 {
428 const char *sub_command = args.GetArgumentAtIndex(0);
429
430 if (strcasecmp(sub_command, "enable") == 0)
431 {
432 Timer::SetDisplayDepth (UINT32_MAX);
433 result.SetStatus(eReturnStatusSuccessFinishNoResult);
434 }
435 else if (strcasecmp(sub_command, "disable") == 0)
436 {
437 Timer::DumpCategoryTimes (&result.GetOutputStream());
438 Timer::SetDisplayDepth (0);
439 result.SetStatus(eReturnStatusSuccessFinishResult);
440 }
441 else if (strcasecmp(sub_command, "dump") == 0)
442 {
443 Timer::DumpCategoryTimes (&result.GetOutputStream());
444 result.SetStatus(eReturnStatusSuccessFinishResult);
445 }
446 else if (strcasecmp(sub_command, "reset") == 0)
447 {
448 Timer::ResetCategoryTimes ();
449 result.SetStatus(eReturnStatusSuccessFinishResult);
450 }
451
452 }
453 if (!result.Succeeded())
454 {
455 result.AppendError("Missing subcommand");
456 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
457 }
458 return result.Succeeded();
459 }
460};
461
462//----------------------------------------------------------------------
463// CommandObjectLog constructor
464//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +0000465CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000466 CommandObjectMultiword (interpreter,
467 "log",
Chris Lattner24943d22010-06-08 16:52:24 +0000468 "A set of commands for operating on logs.",
469 "log <command> [<command-options>]")
470{
Greg Clayton238c0a12010-09-18 01:14:36 +0000471 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectLogEnable (interpreter)));
472 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectLogDisable (interpreter)));
473 LoadSubCommand ("list", CommandObjectSP (new CommandObjectLogList (interpreter)));
474 LoadSubCommand ("timers", CommandObjectSP (new CommandObjectLogTimer (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000475}
476
477//----------------------------------------------------------------------
478// Destructor
479//----------------------------------------------------------------------
480CommandObjectLog::~CommandObjectLog()
481{
482}
483
484
485
486