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