1 //===-- 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 "lldb/lldb-python.h" 11 12 #include "CommandObjectLog.h" 13 14 // C Includes 15 // C++ Includes 16 // Other libraries and framework includes 17 // Project includes 18 #include "lldb/lldb-private-log.h" 19 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" 30 31 #include "lldb/Core/Debugger.h" 32 #include "lldb/Host/StringConvert.h" 33 #include "lldb/Interpreter/CommandInterpreter.h" 34 #include "lldb/Interpreter/CommandReturnObject.h" 35 36 #include "lldb/Symbol/LineTable.h" 37 #include "lldb/Symbol/ObjectFile.h" 38 #include "lldb/Symbol/SymbolFile.h" 39 #include "lldb/Symbol/SymbolVendor.h" 40 41 #include "lldb/Target/Process.h" 42 #include "lldb/Target/Target.h" 43 44 using namespace lldb; 45 using namespace lldb_private; 46 47 48 class CommandObjectLogEnable : public CommandObjectParsed 49 { 50 public: 51 //------------------------------------------------------------------ 52 // Constructors and Destructors 53 //------------------------------------------------------------------ 54 CommandObjectLogEnable(CommandInterpreter &interpreter) : 55 CommandObjectParsed (interpreter, 56 "log enable", 57 "Enable logging for a single log channel.", 58 NULL), 59 m_options (interpreter) 60 { 61 62 CommandArgumentEntry arg1; 63 CommandArgumentEntry arg2; 64 CommandArgumentData channel_arg; 65 CommandArgumentData category_arg; 66 67 // Define the first (and only) variant of this arg. 68 channel_arg.arg_type = eArgTypeLogChannel; 69 channel_arg.arg_repetition = eArgRepeatPlain; 70 71 // There is only one variant this argument could be; put it into the argument entry. 72 arg1.push_back (channel_arg); 73 74 category_arg.arg_type = eArgTypeLogCategory; 75 category_arg.arg_repetition = eArgRepeatPlus; 76 77 arg2.push_back (category_arg); 78 79 // Push the data for the first argument into the m_arguments vector. 80 m_arguments.push_back (arg1); 81 m_arguments.push_back (arg2); 82 } 83 84 virtual 85 ~CommandObjectLogEnable() 86 { 87 } 88 89 Options * 90 GetOptions () 91 { 92 return &m_options; 93 } 94 95 // int 96 // HandleArgumentCompletion (Args &input, 97 // int &cursor_index, 98 // int &cursor_char_position, 99 // OptionElementVector &opt_element_vector, 100 // int match_start_point, 101 // int max_return_elements, 102 // bool &word_complete, 103 // StringList &matches) 104 // { 105 // std::string completion_str (input.GetArgumentAtIndex(cursor_index)); 106 // completion_str.erase (cursor_char_position); 107 // 108 // if (cursor_index == 1) 109 // { 110 // // 111 // Log::AutoCompleteChannelName (completion_str.c_str(), matches); 112 // } 113 // return matches.GetSize(); 114 // } 115 // 116 117 class CommandOptions : public Options 118 { 119 public: 120 121 CommandOptions (CommandInterpreter &interpreter) : 122 Options (interpreter), 123 log_file (), 124 log_options (0) 125 { 126 } 127 128 129 virtual 130 ~CommandOptions () 131 { 132 } 133 134 virtual Error 135 SetOptionValue (uint32_t option_idx, const char *option_arg) 136 { 137 Error error; 138 const int short_option = m_getopt_table[option_idx].val; 139 140 switch (short_option) 141 { 142 case 'f': log_file.SetFile(option_arg, true); break; 143 case 't': log_options |= LLDB_LOG_OPTION_THREADSAFE; break; 144 case 'v': log_options |= LLDB_LOG_OPTION_VERBOSE; break; 145 case 'g': log_options |= LLDB_LOG_OPTION_DEBUG; break; 146 case 's': log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE; break; 147 case 'T': log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP; break; 148 case 'p': log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;break; 149 case 'n': log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME; break; 150 case 'S': log_options |= LLDB_LOG_OPTION_BACKTRACE; break; 151 default: 152 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 153 break; 154 } 155 156 return error; 157 } 158 159 void 160 OptionParsingStarting () 161 { 162 log_file.Clear(); 163 log_options = 0; 164 } 165 166 const OptionDefinition* 167 GetDefinitions () 168 { 169 return g_option_table; 170 } 171 172 // Options table: Required for subclasses of Options. 173 174 static OptionDefinition g_option_table[]; 175 176 // Instance variables to hold the values for command options. 177 178 FileSpec log_file; 179 uint32_t log_options; 180 }; 181 182 protected: 183 virtual bool 184 DoExecute (Args& args, 185 CommandReturnObject &result) 186 { 187 if (args.GetArgumentCount() < 2) 188 { 189 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str()); 190 } 191 else 192 { 193 std::string channel(args.GetArgumentAtIndex(0)); 194 args.Shift (); // Shift off the channel 195 char log_file[PATH_MAX]; 196 if (m_options.log_file) 197 m_options.log_file.GetPath(log_file, sizeof(log_file)); 198 else 199 log_file[0] = '\0'; 200 bool success = m_interpreter.GetDebugger().EnableLog (channel.c_str(), 201 args.GetConstArgumentVector(), 202 log_file, 203 m_options.log_options, 204 result.GetErrorStream()); 205 if (success) 206 result.SetStatus (eReturnStatusSuccessFinishNoResult); 207 else 208 result.SetStatus (eReturnStatusFailed); 209 } 210 return result.Succeeded(); 211 } 212 213 CommandOptions m_options; 214 }; 215 216 OptionDefinition 217 CommandObjectLogEnable::CommandOptions::g_option_table[] = 218 { 219 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Set the destination file to log to."}, 220 { LLDB_OPT_SET_1, false, "threadsafe", 't', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable thread safe logging to avoid interweaved log lines." }, 221 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose logging." }, 222 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable debug logging." }, 223 { LLDB_OPT_SET_1, false, "sequence", 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with an increasing integer sequence id." }, 224 { LLDB_OPT_SET_1, false, "timestamp", 'T', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Prepend all log lines with a timestamp." }, 225 { 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." }, 226 { 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." }, 227 { LLDB_OPT_SET_1, false, "stack", 'S', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Append a stack backtrace to each log line." }, 228 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } 229 }; 230 231 class CommandObjectLogDisable : public CommandObjectParsed 232 { 233 public: 234 //------------------------------------------------------------------ 235 // Constructors and Destructors 236 //------------------------------------------------------------------ 237 CommandObjectLogDisable(CommandInterpreter &interpreter) : 238 CommandObjectParsed (interpreter, 239 "log disable", 240 "Disable one or more log channel categories.", 241 NULL) 242 { 243 CommandArgumentEntry arg1; 244 CommandArgumentEntry arg2; 245 CommandArgumentData channel_arg; 246 CommandArgumentData category_arg; 247 248 // Define the first (and only) variant of this arg. 249 channel_arg.arg_type = eArgTypeLogChannel; 250 channel_arg.arg_repetition = eArgRepeatPlain; 251 252 // There is only one variant this argument could be; put it into the argument entry. 253 arg1.push_back (channel_arg); 254 255 category_arg.arg_type = eArgTypeLogCategory; 256 category_arg.arg_repetition = eArgRepeatPlus; 257 258 arg2.push_back (category_arg); 259 260 // Push the data for the first argument into the m_arguments vector. 261 m_arguments.push_back (arg1); 262 m_arguments.push_back (arg2); 263 } 264 265 virtual 266 ~CommandObjectLogDisable() 267 { 268 } 269 270 protected: 271 virtual bool 272 DoExecute (Args& args, 273 CommandReturnObject &result) 274 { 275 const size_t argc = args.GetArgumentCount(); 276 if (argc == 0) 277 { 278 result.AppendErrorWithFormat("%s takes a log channel and one or more log types.\n", m_cmd_name.c_str()); 279 } 280 else 281 { 282 Log::Callbacks log_callbacks; 283 284 std::string channel(args.GetArgumentAtIndex(0)); 285 args.Shift (); // Shift off the channel 286 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks)) 287 { 288 log_callbacks.disable (args.GetConstArgumentVector(), &result.GetErrorStream()); 289 result.SetStatus(eReturnStatusSuccessFinishNoResult); 290 } 291 else if (channel == "all") 292 { 293 Log::DisableAllLogChannels(&result.GetErrorStream()); 294 } 295 else 296 { 297 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str())); 298 if (log_channel_sp) 299 { 300 log_channel_sp->Disable(args.GetConstArgumentVector(), &result.GetErrorStream()); 301 result.SetStatus(eReturnStatusSuccessFinishNoResult); 302 } 303 else 304 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0)); 305 } 306 } 307 return result.Succeeded(); 308 } 309 }; 310 311 class CommandObjectLogList : public CommandObjectParsed 312 { 313 public: 314 //------------------------------------------------------------------ 315 // Constructors and Destructors 316 //------------------------------------------------------------------ 317 CommandObjectLogList(CommandInterpreter &interpreter) : 318 CommandObjectParsed (interpreter, 319 "log list", 320 "List the log categories for one or more log channels. If none specified, lists them all.", 321 NULL) 322 { 323 CommandArgumentEntry arg; 324 CommandArgumentData channel_arg; 325 326 // Define the first (and only) variant of this arg. 327 channel_arg.arg_type = eArgTypeLogChannel; 328 channel_arg.arg_repetition = eArgRepeatStar; 329 330 // There is only one variant this argument could be; put it into the argument entry. 331 arg.push_back (channel_arg); 332 333 // Push the data for the first argument into the m_arguments vector. 334 m_arguments.push_back (arg); 335 } 336 337 virtual 338 ~CommandObjectLogList() 339 { 340 } 341 342 protected: 343 virtual bool 344 DoExecute (Args& args, 345 CommandReturnObject &result) 346 { 347 const size_t argc = args.GetArgumentCount(); 348 if (argc == 0) 349 { 350 Log::ListAllLogChannels (&result.GetOutputStream()); 351 result.SetStatus(eReturnStatusSuccessFinishResult); 352 } 353 else 354 { 355 for (size_t i=0; i<argc; ++i) 356 { 357 Log::Callbacks log_callbacks; 358 359 std::string channel(args.GetArgumentAtIndex(i)); 360 if (Log::GetLogChannelCallbacks (ConstString(channel.c_str()), log_callbacks)) 361 { 362 log_callbacks.list_categories (&result.GetOutputStream()); 363 result.SetStatus(eReturnStatusSuccessFinishResult); 364 } 365 else if (channel == "all") 366 { 367 Log::ListAllLogChannels (&result.GetOutputStream()); 368 result.SetStatus(eReturnStatusSuccessFinishResult); 369 } 370 else 371 { 372 LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str())); 373 if (log_channel_sp) 374 { 375 log_channel_sp->ListCategories(&result.GetOutputStream()); 376 result.SetStatus(eReturnStatusSuccessFinishNoResult); 377 } 378 else 379 result.AppendErrorWithFormat("Invalid log channel '%s'.\n", args.GetArgumentAtIndex(0)); 380 } 381 } 382 } 383 return result.Succeeded(); 384 } 385 }; 386 387 class CommandObjectLogTimer : public CommandObjectParsed 388 { 389 public: 390 //------------------------------------------------------------------ 391 // Constructors and Destructors 392 //------------------------------------------------------------------ 393 CommandObjectLogTimer(CommandInterpreter &interpreter) : 394 CommandObjectParsed (interpreter, 395 "log timers", 396 "Enable, disable, dump, and reset LLDB internal performance timers.", 397 "log timers < enable <depth> | disable | dump | increment <bool> | reset >") 398 { 399 } 400 401 virtual 402 ~CommandObjectLogTimer() 403 { 404 } 405 406 protected: 407 virtual bool 408 DoExecute (Args& args, 409 CommandReturnObject &result) 410 { 411 const size_t argc = args.GetArgumentCount(); 412 result.SetStatus(eReturnStatusFailed); 413 414 if (argc == 1) 415 { 416 const char *sub_command = args.GetArgumentAtIndex(0); 417 418 if (strcasecmp(sub_command, "enable") == 0) 419 { 420 Timer::SetDisplayDepth (UINT32_MAX); 421 result.SetStatus(eReturnStatusSuccessFinishNoResult); 422 } 423 else if (strcasecmp(sub_command, "disable") == 0) 424 { 425 Timer::DumpCategoryTimes (&result.GetOutputStream()); 426 Timer::SetDisplayDepth (0); 427 result.SetStatus(eReturnStatusSuccessFinishResult); 428 } 429 else if (strcasecmp(sub_command, "dump") == 0) 430 { 431 Timer::DumpCategoryTimes (&result.GetOutputStream()); 432 result.SetStatus(eReturnStatusSuccessFinishResult); 433 } 434 else if (strcasecmp(sub_command, "reset") == 0) 435 { 436 Timer::ResetCategoryTimes (); 437 result.SetStatus(eReturnStatusSuccessFinishResult); 438 } 439 440 } 441 else if (argc == 2) 442 { 443 const char *sub_command = args.GetArgumentAtIndex(0); 444 445 if (strcasecmp(sub_command, "enable") == 0) 446 { 447 bool success; 448 uint32_t depth = StringConvert::ToUInt32(args.GetArgumentAtIndex(1), 0, 0, &success); 449 if (success) 450 { 451 Timer::SetDisplayDepth (depth); 452 result.SetStatus(eReturnStatusSuccessFinishNoResult); 453 } 454 else 455 result.AppendError("Could not convert enable depth to an unsigned integer."); 456 } 457 if (strcasecmp(sub_command, "increment") == 0) 458 { 459 bool success; 460 bool increment = Args::StringToBoolean(args.GetArgumentAtIndex(1), false, &success); 461 if (success) 462 { 463 Timer::SetQuiet (!increment); 464 result.SetStatus(eReturnStatusSuccessFinishNoResult); 465 } 466 else 467 result.AppendError("Could not convert increment value to boolean."); 468 } 469 } 470 471 if (!result.Succeeded()) 472 { 473 result.AppendError("Missing subcommand"); 474 result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str()); 475 } 476 return result.Succeeded(); 477 } 478 }; 479 480 //---------------------------------------------------------------------- 481 // CommandObjectLog constructor 482 //---------------------------------------------------------------------- 483 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) : 484 CommandObjectMultiword (interpreter, 485 "log", 486 "A set of commands for operating on logs.", 487 "log <command> [<command-options>]") 488 { 489 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectLogEnable (interpreter))); 490 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectLogDisable (interpreter))); 491 LoadSubCommand ("list", CommandObjectSP (new CommandObjectLogList (interpreter))); 492 LoadSubCommand ("timers", CommandObjectSP (new CommandObjectLogTimer (interpreter))); 493 } 494 495 //---------------------------------------------------------------------- 496 // Destructor 497 //---------------------------------------------------------------------- 498 CommandObjectLog::~CommandObjectLog() 499 { 500 } 501 502 503 504 505