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