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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamFile.h" |
| 18 | #include "lldb/Core/Timer.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 19 | #include "lldb/Host/OptionParser.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/Args.h" |
Sean Callanan | 4be3990 | 2010-06-23 21:28:25 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/LineTable.h" |
| 25 | #include "lldb/Symbol/ObjectFile.h" |
| 26 | #include "lldb/Symbol/SymbolFile.h" |
| 27 | #include "lldb/Symbol/SymbolVendor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Process.h" |
| 29 | #include "lldb/Target/Target.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/RegularExpression.h" |
| 33 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 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." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 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." }, |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 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." }, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 50 | // clang-format on |
| 51 | }; |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | class CommandObjectLogEnable : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | //------------------------------------------------------------------ |
| 56 | // Constructors and Destructors |
| 57 | //------------------------------------------------------------------ |
| 58 | CommandObjectLogEnable(CommandInterpreter &interpreter) |
| 59 | : CommandObjectParsed(interpreter, "log enable", |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 60 | "Enable logging for a single log channel.", |
| 61 | nullptr), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | m_options() { |
| 63 | CommandArgumentEntry arg1; |
| 64 | CommandArgumentEntry arg2; |
| 65 | CommandArgumentData channel_arg; |
| 66 | CommandArgumentData category_arg; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | // Define the first (and only) variant of this arg. |
| 69 | channel_arg.arg_type = eArgTypeLogChannel; |
| 70 | channel_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | class CommandOptions : public Options { |
| 91 | public: |
| 92 | CommandOptions() : Options(), log_file(), log_options(0) {} |
| 93 | |
| 94 | ~CommandOptions() override = default; |
| 95 | |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 96 | Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 129 | case 'F': |
| 130 | log_options |= LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION; |
Pavel Labath | 90505a0 | 2017-02-03 20:26:57 +0000 | [diff] [blame] | 131 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | default: |
| 133 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 134 | short_option); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 142 | log_file.Clear(); |
| 143 | log_options = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 146 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 147 | return llvm::makeArrayRef(g_log_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | // Instance variables to hold the values for command options. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | FileSpec log_file; |
| 153 | uint32_t log_options; |
| 154 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | |
| 156 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 162 | return false; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 163 | } |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 164 | |
| 165 | // Store into a std::string since we're about to shift the channel off. |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 166 | const std::string channel = args[0].ref; |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 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'; |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 173 | |
| 174 | std::string error; |
| 175 | llvm::raw_string_ostream error_stream(error); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 176 | bool success = m_interpreter.GetDebugger().EnableLog( |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 177 | channel, args.GetArgumentArrayRef(), log_file, m_options.log_options, |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 178 | error_stream); |
| 179 | result.GetErrorStream() << error_stream.str(); |
| 180 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 181 | if (success) |
| 182 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 183 | else |
| 184 | result.SetStatus(eReturnStatusFailed); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | return result.Succeeded(); |
| 186 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 187 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | class CommandObjectLogDisable : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | //------------------------------------------------------------------ |
| 194 | // Constructors and Destructors |
| 195 | //------------------------------------------------------------------ |
| 196 | CommandObjectLogDisable(CommandInterpreter &interpreter) |
| 197 | : CommandObjectParsed(interpreter, "log disable", |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 198 | "Disable one or more log channel categories.", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | nullptr) { |
| 200 | CommandArgumentEntry arg1; |
| 201 | CommandArgumentEntry arg2; |
| 202 | CommandArgumentData channel_arg; |
| 203 | CommandArgumentData category_arg; |
Caroline Tice | 7149fab | 2010-10-29 21:56:41 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | // Define the first (and only) variant of this arg. |
| 206 | channel_arg.arg_type = eArgTypeLogChannel; |
| 207 | channel_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 7149fab | 2010-10-29 21:56:41 +0000 | [diff] [blame] | 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | // There is only one variant this argument could be; put it into the |
| 210 | // argument entry. |
| 211 | arg1.push_back(channel_arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | category_arg.arg_type = eArgTypeLogCategory; |
| 214 | category_arg.arg_repetition = eArgRepeatPlus; |
| 215 | |
| 216 | arg2.push_back(category_arg); |
| 217 | |
| 218 | // Push the data for the first argument into the m_arguments vector. |
| 219 | m_arguments.push_back(arg1); |
| 220 | m_arguments.push_back(arg2); |
| 221 | } |
| 222 | |
| 223 | ~CommandObjectLogDisable() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 225 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 227 | if (args.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | result.AppendErrorWithFormat( |
| 229 | "%s takes a log channel and one or more log types.\n", |
| 230 | m_cmd_name.c_str()); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 231 | return false; |
| 232 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 234 | const std::string channel = args[0].ref; |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 235 | args.Shift(); // Shift off the channel |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 236 | if (channel == "all") { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 237 | Log::DisableAllLogChannels(); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 238 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 239 | } else { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 240 | std::string error; |
| 241 | llvm::raw_string_ostream error_stream(error); |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 242 | if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(), |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 243 | error_stream)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 245 | result.GetErrorStream() << error_stream.str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | return result.Succeeded(); |
| 248 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | class CommandObjectLogList : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | //------------------------------------------------------------------ |
| 254 | // Constructors and Destructors |
| 255 | //------------------------------------------------------------------ |
| 256 | CommandObjectLogList(CommandInterpreter &interpreter) |
| 257 | : CommandObjectParsed(interpreter, "log list", |
| 258 | "List the log categories for one or more log " |
| 259 | "channels. If none specified, lists them all.", |
| 260 | nullptr) { |
| 261 | CommandArgumentEntry arg; |
| 262 | CommandArgumentData channel_arg; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | // Define the first (and only) variant of this arg. |
| 265 | channel_arg.arg_type = eArgTypeLogChannel; |
| 266 | channel_arg.arg_repetition = eArgRepeatStar; |
| 267 | |
| 268 | // There is only one variant this argument could be; put it into the |
| 269 | // argument entry. |
| 270 | arg.push_back(channel_arg); |
| 271 | |
| 272 | // Push the data for the first argument into the m_arguments vector. |
| 273 | m_arguments.push_back(arg); |
| 274 | } |
| 275 | |
| 276 | ~CommandObjectLogList() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 278 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 280 | std::string output; |
| 281 | llvm::raw_string_ostream output_stream(output); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 282 | if (args.empty()) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 283 | Log::ListAllLogChannels(output_stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 285 | } else { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 286 | bool success = true; |
| 287 | for (const auto &entry : args.entries()) |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 288 | success = |
| 289 | success && Log::ListChannelCategories(entry.ref, output_stream); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 290 | if (success) |
| 291 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | } |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 293 | result.GetOutputStream() << output_stream.str(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | return result.Succeeded(); |
| 295 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | class CommandObjectLogTimer : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | //------------------------------------------------------------------ |
| 301 | // Constructors and Destructors |
| 302 | //------------------------------------------------------------------ |
| 303 | CommandObjectLogTimer(CommandInterpreter &interpreter) |
| 304 | : CommandObjectParsed(interpreter, "log timers", |
| 305 | "Enable, disable, dump, and reset LLDB internal " |
| 306 | "performance timers.", |
| 307 | "log timers < enable <depth> | disable | dump | " |
| 308 | "increment <bool> | reset >") {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | ~CommandObjectLogTimer() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 312 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | result.SetStatus(eReturnStatusFailed); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 316 | if (args.GetArgumentCount() == 1) { |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 317 | auto sub_command = args[0].ref; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 319 | if (sub_command.equals_lower("enable")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | Timer::SetDisplayDepth(UINT32_MAX); |
| 321 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 322 | } else if (sub_command.equals_lower("disable")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | Timer::DumpCategoryTimes(&result.GetOutputStream()); |
| 324 | Timer::SetDisplayDepth(0); |
| 325 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 326 | } else if (sub_command.equals_lower("dump")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | Timer::DumpCategoryTimes(&result.GetOutputStream()); |
| 328 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 329 | } else if (sub_command.equals_lower("reset")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | Timer::ResetCategoryTimes(); |
| 331 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 332 | } |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 333 | } else if (args.GetArgumentCount() == 2) { |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 334 | auto sub_command = args[0].ref; |
| 335 | auto param = args[1].ref; |
Jim Ingham | 932725f | 2010-11-04 23:08:26 +0000 | [diff] [blame] | 336 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 337 | if (sub_command.equals_lower("enable")) { |
| 338 | uint32_t depth; |
| 339 | if (param.consumeInteger(0, depth)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | result.AppendError( |
| 341 | "Could not convert enable depth to an unsigned integer."); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 342 | } else { |
| 343 | Timer::SetDisplayDepth(depth); |
| 344 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 345 | } |
| 346 | } else if (sub_command.equals_lower("increment")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | bool success; |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 348 | bool increment = Args::StringToBoolean(param, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | if (success) { |
| 350 | Timer::SetQuiet(!increment); |
| 351 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 352 | } else |
| 353 | result.AppendError("Could not convert increment value to boolean."); |
| 354 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | |
| 357 | if (!result.Succeeded()) { |
| 358 | result.AppendError("Missing subcommand"); |
| 359 | result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str()); |
| 360 | } |
| 361 | return result.Succeeded(); |
| 362 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 365 | CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | : CommandObjectMultiword(interpreter, "log", |
| 367 | "Commands controlling LLDB internal logging.", |
| 368 | "log <subcommand> [<command-options>]") { |
| 369 | LoadSubCommand("enable", |
| 370 | CommandObjectSP(new CommandObjectLogEnable(interpreter))); |
| 371 | LoadSubCommand("disable", |
| 372 | CommandObjectSP(new CommandObjectLogDisable(interpreter))); |
| 373 | LoadSubCommand("list", |
| 374 | CommandObjectSP(new CommandObjectLogList(interpreter))); |
| 375 | LoadSubCommand("timers", |
| 376 | CommandObjectSP(new CommandObjectLogTimer(interpreter))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 379 | CommandObjectLog::~CommandObjectLog() = default; |