Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 14 | #include "CommandObjectLog.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Debugger.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Log.h" |
| 18 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/RegularExpression.h" |
| 20 | #include "lldb/Core/Stream.h" |
| 21 | #include "lldb/Core/StreamFile.h" |
| 22 | #include "lldb/Core/Timer.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "lldb/Host/FileSpec.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 24 | #include "lldb/Host/StringConvert.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/Args.h" |
Sean Callanan | 4be3990 | 2010-06-23 21:28:25 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Symbol/LineTable.h" |
| 30 | #include "lldb/Symbol/ObjectFile.h" |
| 31 | #include "lldb/Symbol/SymbolFile.h" |
| 32 | #include "lldb/Symbol/SymbolVendor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Process.h" |
| 34 | #include "lldb/Target/Target.h" |
| 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | class CommandObjectLogEnable : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | //------------------------------------------------------------------ |
| 57 | // Constructors and Destructors |
| 58 | //------------------------------------------------------------------ |
| 59 | CommandObjectLogEnable(CommandInterpreter &interpreter) |
| 60 | : CommandObjectParsed(interpreter, "log enable", |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 61 | "Enable logging for a single log channel.", |
| 62 | nullptr), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | m_options() { |
| 64 | CommandArgumentEntry arg1; |
| 65 | CommandArgumentEntry arg2; |
| 66 | CommandArgumentData channel_arg; |
| 67 | CommandArgumentData category_arg; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | // Define the first (and only) variant of this arg. |
| 70 | channel_arg.arg_type = eArgTypeLogChannel; |
| 71 | channel_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | // int |
| 92 | // HandleArgumentCompletion (Args &input, |
| 93 | // int &cursor_index, |
| 94 | // int &cursor_char_position, |
| 95 | // OptionElementVector &opt_element_vector, |
| 96 | // int match_start_point, |
| 97 | // int max_return_elements, |
| 98 | // bool &word_complete, |
| 99 | // StringList &matches) |
| 100 | // { |
| 101 | // std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 102 | // completion_str.erase (cursor_char_position); |
| 103 | // |
| 104 | // if (cursor_index == 1) |
| 105 | // { |
| 106 | // // |
| 107 | // Log::AutoCompleteChannelName (completion_str.c_str(), matches); |
| 108 | // } |
| 109 | // return matches.GetSize(); |
| 110 | // } |
| 111 | // |
| 112 | |
| 113 | class CommandOptions : public Options { |
| 114 | public: |
| 115 | CommandOptions() : Options(), log_file(), log_options(0) {} |
| 116 | |
| 117 | ~CommandOptions() override = default; |
| 118 | |
| 119 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 120 | ExecutionContext *execution_context) override { |
| 121 | Error error; |
| 122 | const int short_option = m_getopt_table[option_idx].val; |
| 123 | |
| 124 | switch (short_option) { |
| 125 | case 'f': |
| 126 | log_file.SetFile(option_arg, true); |
| 127 | break; |
| 128 | case 't': |
| 129 | log_options |= LLDB_LOG_OPTION_THREADSAFE; |
| 130 | break; |
| 131 | case 'v': |
| 132 | log_options |= LLDB_LOG_OPTION_VERBOSE; |
| 133 | break; |
| 134 | case 'g': |
| 135 | log_options |= LLDB_LOG_OPTION_DEBUG; |
| 136 | break; |
| 137 | case 's': |
| 138 | log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE; |
| 139 | break; |
| 140 | case 'T': |
| 141 | log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP; |
| 142 | break; |
| 143 | case 'p': |
| 144 | log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD; |
| 145 | break; |
| 146 | case 'n': |
| 147 | log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME; |
| 148 | break; |
| 149 | case 'S': |
| 150 | log_options |= LLDB_LOG_OPTION_BACKTRACE; |
| 151 | break; |
| 152 | case 'a': |
| 153 | log_options |= LLDB_LOG_OPTION_APPEND; |
| 154 | break; |
| 155 | default: |
| 156 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 157 | short_option); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 165 | log_file.Clear(); |
| 166 | log_options = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 169 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 170 | return llvm::makeArrayRef(g_log_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | // Instance variables to hold the values for command options. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | FileSpec log_file; |
| 176 | uint32_t log_options; |
| 177 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | |
| 179 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 181 | if (args.GetArgumentCount() < 2) { |
| 182 | result.AppendErrorWithFormat( |
| 183 | "%s takes a log channel and one or more log types.\n", |
| 184 | m_cmd_name.c_str()); |
| 185 | } else { |
| 186 | std::string channel(args.GetArgumentAtIndex(0)); |
| 187 | args.Shift(); // Shift off the channel |
| 188 | char log_file[PATH_MAX]; |
| 189 | if (m_options.log_file) |
| 190 | m_options.log_file.GetPath(log_file, sizeof(log_file)); |
| 191 | else |
| 192 | log_file[0] = '\0'; |
| 193 | bool success = m_interpreter.GetDebugger().EnableLog( |
| 194 | channel.c_str(), args.GetConstArgumentVector(), log_file, |
| 195 | m_options.log_options, result.GetErrorStream()); |
| 196 | if (success) |
| 197 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 198 | else |
| 199 | result.SetStatus(eReturnStatusFailed); |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 200 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | return result.Succeeded(); |
| 202 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | class CommandObjectLogDisable : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | //------------------------------------------------------------------ |
| 210 | // Constructors and Destructors |
| 211 | //------------------------------------------------------------------ |
| 212 | CommandObjectLogDisable(CommandInterpreter &interpreter) |
| 213 | : CommandObjectParsed(interpreter, "log disable", |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 214 | "Disable one or more log channel categories.", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | nullptr) { |
| 216 | CommandArgumentEntry arg1; |
| 217 | CommandArgumentEntry arg2; |
| 218 | CommandArgumentData channel_arg; |
| 219 | CommandArgumentData category_arg; |
Caroline Tice | 7149fab | 2010-10-29 21:56:41 +0000 | [diff] [blame] | 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | // Define the first (and only) variant of this arg. |
| 222 | channel_arg.arg_type = eArgTypeLogChannel; |
| 223 | channel_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 7149fab | 2010-10-29 21:56:41 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | // There is only one variant this argument could be; put it into the |
| 226 | // argument entry. |
| 227 | arg1.push_back(channel_arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | category_arg.arg_type = eArgTypeLogCategory; |
| 230 | category_arg.arg_repetition = eArgRepeatPlus; |
| 231 | |
| 232 | arg2.push_back(category_arg); |
| 233 | |
| 234 | // Push the data for the first argument into the m_arguments vector. |
| 235 | m_arguments.push_back(arg1); |
| 236 | m_arguments.push_back(arg2); |
| 237 | } |
| 238 | |
| 239 | ~CommandObjectLogDisable() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 241 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 243 | const size_t argc = args.GetArgumentCount(); |
| 244 | if (argc == 0) { |
| 245 | result.AppendErrorWithFormat( |
| 246 | "%s takes a log channel and one or more log types.\n", |
| 247 | m_cmd_name.c_str()); |
| 248 | } else { |
| 249 | Log::Callbacks log_callbacks; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | std::string channel(args.GetArgumentAtIndex(0)); |
| 252 | args.Shift(); // Shift off the channel |
| 253 | if (Log::GetLogChannelCallbacks(ConstString(channel.c_str()), |
| 254 | log_callbacks)) { |
| 255 | log_callbacks.disable(args.GetConstArgumentVector(), |
| 256 | &result.GetErrorStream()); |
| 257 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 258 | } else if (channel == "all") { |
| 259 | Log::DisableAllLogChannels(&result.GetErrorStream()); |
| 260 | } else { |
| 261 | LogChannelSP log_channel_sp(LogChannel::FindPlugin(channel.c_str())); |
| 262 | if (log_channel_sp) { |
| 263 | log_channel_sp->Disable(args.GetConstArgumentVector(), |
| 264 | &result.GetErrorStream()); |
| 265 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 266 | } else |
| 267 | result.AppendErrorWithFormat("Invalid log channel '%s'.\n", |
| 268 | args.GetArgumentAtIndex(0)); |
| 269 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | return result.Succeeded(); |
| 272 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | class CommandObjectLogList : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | //------------------------------------------------------------------ |
| 278 | // Constructors and Destructors |
| 279 | //------------------------------------------------------------------ |
| 280 | CommandObjectLogList(CommandInterpreter &interpreter) |
| 281 | : CommandObjectParsed(interpreter, "log list", |
| 282 | "List the log categories for one or more log " |
| 283 | "channels. If none specified, lists them all.", |
| 284 | nullptr) { |
| 285 | CommandArgumentEntry arg; |
| 286 | CommandArgumentData channel_arg; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | // Define the first (and only) variant of this arg. |
| 289 | channel_arg.arg_type = eArgTypeLogChannel; |
| 290 | channel_arg.arg_repetition = eArgRepeatStar; |
| 291 | |
| 292 | // There is only one variant this argument could be; put it into the |
| 293 | // argument entry. |
| 294 | arg.push_back(channel_arg); |
| 295 | |
| 296 | // Push the data for the first argument into the m_arguments vector. |
| 297 | m_arguments.push_back(arg); |
| 298 | } |
| 299 | |
| 300 | ~CommandObjectLogList() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 302 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 304 | const size_t argc = args.GetArgumentCount(); |
| 305 | if (argc == 0) { |
| 306 | Log::ListAllLogChannels(&result.GetOutputStream()); |
| 307 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 308 | } else { |
| 309 | for (size_t i = 0; i < argc; ++i) { |
| 310 | Log::Callbacks log_callbacks; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | std::string channel(args.GetArgumentAtIndex(i)); |
| 313 | if (Log::GetLogChannelCallbacks(ConstString(channel.c_str()), |
| 314 | log_callbacks)) { |
| 315 | log_callbacks.list_categories(&result.GetOutputStream()); |
| 316 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 317 | } else if (channel == "all") { |
| 318 | Log::ListAllLogChannels(&result.GetOutputStream()); |
| 319 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 320 | } else { |
| 321 | LogChannelSP log_channel_sp(LogChannel::FindPlugin(channel.c_str())); |
| 322 | if (log_channel_sp) { |
| 323 | log_channel_sp->ListCategories(&result.GetOutputStream()); |
| 324 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 325 | } else |
| 326 | result.AppendErrorWithFormat("Invalid log channel '%s'.\n", |
| 327 | args.GetArgumentAtIndex(0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | return result.Succeeded(); |
| 332 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | class CommandObjectLogTimer : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 336 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 337 | //------------------------------------------------------------------ |
| 338 | // Constructors and Destructors |
| 339 | //------------------------------------------------------------------ |
| 340 | CommandObjectLogTimer(CommandInterpreter &interpreter) |
| 341 | : CommandObjectParsed(interpreter, "log timers", |
| 342 | "Enable, disable, dump, and reset LLDB internal " |
| 343 | "performance timers.", |
| 344 | "log timers < enable <depth> | disable | dump | " |
| 345 | "increment <bool> | reset >") {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | ~CommandObjectLogTimer() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 349 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 351 | const size_t argc = args.GetArgumentCount(); |
| 352 | result.SetStatus(eReturnStatusFailed); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | if (argc == 1) { |
| 355 | const char *sub_command = args.GetArgumentAtIndex(0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | if (strcasecmp(sub_command, "enable") == 0) { |
| 358 | Timer::SetDisplayDepth(UINT32_MAX); |
| 359 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 360 | } else if (strcasecmp(sub_command, "disable") == 0) { |
| 361 | Timer::DumpCategoryTimes(&result.GetOutputStream()); |
| 362 | Timer::SetDisplayDepth(0); |
| 363 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 364 | } else if (strcasecmp(sub_command, "dump") == 0) { |
| 365 | Timer::DumpCategoryTimes(&result.GetOutputStream()); |
| 366 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 367 | } else if (strcasecmp(sub_command, "reset") == 0) { |
| 368 | Timer::ResetCategoryTimes(); |
| 369 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 370 | } |
| 371 | } else if (argc == 2) { |
| 372 | const char *sub_command = args.GetArgumentAtIndex(0); |
Jim Ingham | 932725f | 2010-11-04 23:08:26 +0000 | [diff] [blame] | 373 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | if (strcasecmp(sub_command, "enable") == 0) { |
| 375 | bool success; |
| 376 | uint32_t depth = |
| 377 | StringConvert::ToUInt32(args.GetArgumentAtIndex(1), 0, 0, &success); |
| 378 | if (success) { |
| 379 | Timer::SetDisplayDepth(depth); |
| 380 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 381 | } else |
| 382 | result.AppendError( |
| 383 | "Could not convert enable depth to an unsigned integer."); |
| 384 | } |
| 385 | if (strcasecmp(sub_command, "increment") == 0) { |
| 386 | bool success; |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 387 | bool increment = Args::StringToBoolean( |
| 388 | llvm::StringRef::withNullAsEmpty(args.GetArgumentAtIndex(1)), false, |
| 389 | &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | if (success) { |
| 391 | Timer::SetQuiet(!increment); |
| 392 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 393 | } else |
| 394 | result.AppendError("Could not convert increment value to boolean."); |
| 395 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | |
| 398 | if (!result.Succeeded()) { |
| 399 | result.AppendError("Missing subcommand"); |
| 400 | result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str()); |
| 401 | } |
| 402 | return result.Succeeded(); |
| 403 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 406 | CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | : CommandObjectMultiword(interpreter, "log", |
| 408 | "Commands controlling LLDB internal logging.", |
| 409 | "log <subcommand> [<command-options>]") { |
| 410 | LoadSubCommand("enable", |
| 411 | CommandObjectSP(new CommandObjectLogEnable(interpreter))); |
| 412 | LoadSubCommand("disable", |
| 413 | CommandObjectSP(new CommandObjectLogDisable(interpreter))); |
| 414 | LoadSubCommand("list", |
| 415 | CommandObjectSP(new CommandObjectLogList(interpreter))); |
| 416 | LoadSubCommand("timers", |
| 417 | CommandObjectSP(new CommandObjectLogTimer(interpreter))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 420 | CommandObjectLog::~CommandObjectLog() = default; |