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