Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "command.h" |
| 18 | |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 22 | #include <map> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 27 | #include <android-base/parsedouble.h> |
| 28 | #include <android-base/parseint.h> |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 29 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 30 | #include "utils.h" |
| 31 | |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 32 | namespace simpleperf { |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 33 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 34 | bool Command::NextArgumentOrError(const std::vector<std::string>& args, size_t* pi) { |
| 35 | if (*pi + 1 == args.size()) { |
| 36 | LOG(ERROR) << "No argument following " << args[*pi] << " option. Try `simpleperf help " << name_ |
| 37 | << "`"; |
| 38 | return false; |
| 39 | } |
| 40 | ++*pi; |
| 41 | return true; |
| 42 | } |
| 43 | |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 44 | bool ConvertArgsToOptions(const std::vector<std::string>& args, |
| 45 | const OptionFormatMap& option_formats, const std::string& help_msg, |
| 46 | OptionValueMap* options, |
| 47 | std::vector<std::pair<OptionName, OptionValue>>* ordered_options, |
| 48 | std::vector<std::string>* non_option_args) { |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 49 | options->values.clear(); |
| 50 | ordered_options->clear(); |
| 51 | size_t i; |
| 52 | for (i = 0; i < args.size() && !args[i].empty() && args[i][0] == '-'; i++) { |
| 53 | auto it = option_formats.find(args[i]); |
| 54 | if (it == option_formats.end()) { |
| 55 | if (args[i] == "--") { |
| 56 | i++; |
| 57 | break; |
| 58 | } |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 59 | LOG(ERROR) << "Unknown option " << args[i] << "." << help_msg; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | const OptionName& name = it->first; |
| 63 | const OptionFormat& format = it->second; |
| 64 | OptionValue value; |
| 65 | memset(&value, 0, sizeof(value)); |
| 66 | |
| 67 | if (i + 1 == args.size()) { |
| 68 | if (format.value_type != OptionValueType::NONE && |
| 69 | format.value_type != OptionValueType::OPT_STRING) { |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 70 | LOG(ERROR) << "No argument following " << name << " option." << help_msg; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | } else { |
| 74 | switch (format.value_type) { |
| 75 | case OptionValueType::NONE: |
| 76 | break; |
| 77 | case OptionValueType::STRING: |
| 78 | value.str_value = &args[++i]; |
| 79 | break; |
| 80 | case OptionValueType::OPT_STRING: |
| 81 | if (!args[i + 1].empty() && args[i + 1][0] != '-') { |
| 82 | value.str_value = &args[++i]; |
| 83 | } |
| 84 | break; |
| 85 | case OptionValueType::UINT: |
| 86 | if (!android::base::ParseUint(args[++i], &value.uint_value, |
| 87 | std::numeric_limits<uint64_t>::max(), true)) { |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 88 | LOG(ERROR) << "Invalid argument for option " << name << ": " << args[i] << "." |
| 89 | << help_msg; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | break; |
| 93 | case OptionValueType::DOUBLE: |
| 94 | if (!android::base::ParseDouble(args[++i], &value.double_value)) { |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 95 | LOG(ERROR) << "Invalid argument for option " << name << ": " << args[i] << "." |
| 96 | << help_msg; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | switch (format.type) { |
| 104 | case OptionType::SINGLE: |
| 105 | if (auto it = options->values.find(name); it != options->values.end()) { |
| 106 | it->second = value; |
| 107 | } else { |
| 108 | options->values.emplace(name, value); |
| 109 | } |
| 110 | break; |
| 111 | case OptionType::MULTIPLE: |
| 112 | options->values.emplace(name, value); |
| 113 | break; |
| 114 | case OptionType::ORDERED: |
| 115 | ordered_options->emplace_back(name, value); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | if (i < args.size()) { |
| 120 | if (non_option_args == nullptr) { |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 121 | LOG(ERROR) << "Invalid option " << args[i] << "." << help_msg; |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | non_option_args->assign(args.begin() + i, args.end()); |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
Yabin Cui | 690f00b | 2022-01-07 14:46:09 -0800 | [diff] [blame] | 129 | bool Command::PreprocessOptions(const std::vector<std::string>& args, |
| 130 | const OptionFormatMap& option_formats, OptionValueMap* options, |
| 131 | std::vector<std::pair<OptionName, OptionValue>>* ordered_options, |
| 132 | std::vector<std::string>* non_option_args) { |
| 133 | const std::string help_msg = " Try `simpleperf help " + name_ + "`."; |
| 134 | return ConvertArgsToOptions(args, option_formats, help_msg, options, ordered_options, |
| 135 | non_option_args); |
| 136 | } |
| 137 | |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 138 | bool Command::GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value, |
| 139 | double min, double max) { |
| 140 | if (!NextArgumentOrError(args, pi)) { |
| 141 | return false; |
| 142 | } |
| 143 | if (!android::base::ParseDouble(args[*pi].c_str(), value, min, max)) { |
| 144 | LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi]; |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 150 | void Command::ReportUnknownOption(const std::vector<std::string>& args, size_t i) { |
| 151 | LOG(ERROR) << "Unknown option for " << name_ << " command: '" << args[i] |
| 152 | << "'. Try `simpleperf help " << name_ << "`"; |
| 153 | } |
| 154 | |
| 155 | typedef std::function<std::unique_ptr<Command>(void)> callback_t; |
| 156 | |
| 157 | static std::map<std::string, callback_t>& CommandMap() { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 158 | // commands is used in the constructor of Command. Defining it as a static |
| 159 | // variable in a function makes sure it is initialized before use. |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 160 | static std::map<std::string, callback_t> command_map; |
| 161 | return command_map; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 164 | void RegisterCommand(const std::string& cmd_name, |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 165 | const std::function<std::unique_ptr<Command>(void)>& callback) { |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 166 | CommandMap().insert(std::make_pair(cmd_name, callback)); |
| 167 | } |
| 168 | |
| 169 | void UnRegisterCommand(const std::string& cmd_name) { |
| 170 | CommandMap().erase(cmd_name); |
| 171 | } |
| 172 | |
| 173 | std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name) { |
| 174 | auto it = CommandMap().find(cmd_name); |
| 175 | return (it == CommandMap().end()) ? nullptr : (it->second)(); |
| 176 | } |
| 177 | |
| 178 | const std::vector<std::string> GetAllCommandNames() { |
| 179 | std::vector<std::string> names; |
Chih-Hung Hsieh | 742d78a | 2018-12-11 10:45:02 -0800 | [diff] [blame] | 180 | for (const auto& pair : CommandMap()) { |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 181 | names.push_back(pair.first); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 182 | } |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 183 | return names; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 184 | } |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 185 | |
Yabin Cui | 321bfe6 | 2022-01-15 14:09:46 -0800 | [diff] [blame] | 186 | extern void RegisterBootRecordCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 187 | extern void RegisterDumpRecordCommand(); |
| 188 | extern void RegisterHelpCommand(); |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 189 | extern void RegisterInjectCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 190 | extern void RegisterListCommand(); |
Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 191 | extern void RegisterKmemCommand(); |
Yabin Cui | 8d005de | 2020-10-21 13:48:53 -0700 | [diff] [blame] | 192 | extern void RegisterMergeCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 193 | extern void RegisterRecordCommand(); |
| 194 | extern void RegisterReportCommand(); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 195 | extern void RegisterReportSampleCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 196 | extern void RegisterStatCommand(); |
Yabin Cui | c3bf9d0 | 2018-02-02 14:18:40 -0800 | [diff] [blame] | 197 | extern void RegisterDebugUnwindCommand(); |
Yabin Cui | bbaa512 | 2018-03-19 13:58:51 -0700 | [diff] [blame] | 198 | extern void RegisterTraceSchedCommand(); |
Yabin Cui | 1befe4f | 2019-02-25 15:22:43 -0800 | [diff] [blame] | 199 | extern void RegisterAPICommands(); |
Thiébaud Weksteen | f5c93be | 2020-10-19 13:07:20 +0200 | [diff] [blame] | 200 | extern void RegisterMonitorCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 201 | |
| 202 | class CommandRegister { |
| 203 | public: |
| 204 | CommandRegister() { |
| 205 | RegisterDumpRecordCommand(); |
| 206 | RegisterHelpCommand(); |
Yabin Cui | c573eaa | 2019-08-21 16:05:07 -0700 | [diff] [blame] | 207 | RegisterInjectCommand(); |
Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 208 | RegisterKmemCommand(); |
Yabin Cui | 8d005de | 2020-10-21 13:48:53 -0700 | [diff] [blame] | 209 | RegisterMergeCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 210 | RegisterReportCommand(); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 211 | RegisterReportSampleCommand(); |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 212 | #if defined(__linux__) |
| 213 | RegisterListCommand(); |
| 214 | RegisterRecordCommand(); |
| 215 | RegisterStatCommand(); |
Yabin Cui | c3bf9d0 | 2018-02-02 14:18:40 -0800 | [diff] [blame] | 216 | RegisterDebugUnwindCommand(); |
Yabin Cui | bbaa512 | 2018-03-19 13:58:51 -0700 | [diff] [blame] | 217 | RegisterTraceSchedCommand(); |
Thiébaud Weksteen | f5c93be | 2020-10-19 13:07:20 +0200 | [diff] [blame] | 218 | RegisterMonitorCommand(); |
Yabin Cui | 1befe4f | 2019-02-25 15:22:43 -0800 | [diff] [blame] | 219 | #if defined(__ANDROID__) |
| 220 | RegisterAPICommands(); |
Yabin Cui | 321bfe6 | 2022-01-15 14:09:46 -0800 | [diff] [blame] | 221 | RegisterBootRecordCommand(); |
Yabin Cui | 1befe4f | 2019-02-25 15:22:43 -0800 | [diff] [blame] | 222 | #endif |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 223 | #endif |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | CommandRegister command_register; |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 228 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 229 | static void StderrLogger(android::base::LogId, android::base::LogSeverity severity, const char*, |
| 230 | const char* file, unsigned int line, const char* message) { |
Yabin Cui | e466d4d | 2017-08-11 17:03:07 -0700 | [diff] [blame] | 231 | static const char log_characters[] = "VDIWEFF"; |
| 232 | char severity_char = log_characters[severity]; |
| 233 | fprintf(stderr, "simpleperf %c %s:%u] %s\n", severity_char, file, line, message); |
| 234 | } |
| 235 | |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 236 | bool log_to_android_buffer = false; |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 237 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 238 | bool RunSimpleperfCmd(int argc, char** argv) { |
Yabin Cui | e466d4d | 2017-08-11 17:03:07 -0700 | [diff] [blame] | 239 | android::base::InitLogging(argv, StderrLogger); |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 240 | std::vector<std::string> args; |
| 241 | android::base::LogSeverity log_severity = android::base::INFO; |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 242 | log_to_android_buffer = false; |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 243 | const OptionFormatMap& common_option_formats = GetCommonOptionFormatMap(); |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 244 | |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 245 | int i; |
| 246 | for (i = 1; i < argc && strcmp(argv[i], "--") != 0; ++i) { |
| 247 | std::string option_name = argv[i]; |
| 248 | auto it = common_option_formats.find(option_name); |
| 249 | if (it == common_option_formats.end()) { |
| 250 | args.emplace_back(std::move(option_name)); |
| 251 | continue; |
| 252 | } |
| 253 | if (it->second.value_type != OptionValueType::NONE && i + 1 == argc) { |
| 254 | LOG(ERROR) << "Missing argument for " << option_name; |
| 255 | return false; |
| 256 | } |
| 257 | if (option_name == "-h" || option_name == "--help") { |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 258 | args.insert(args.begin(), "help"); |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 259 | } else if (option_name == "--log") { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 260 | if (!GetLogSeverity(argv[i + 1], &log_severity)) { |
| 261 | LOG(ERROR) << "Unknown log severity: " << argv[i + 1]; |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 262 | } |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 263 | ++i; |
Yabin Cui | d9121ce | 2019-02-07 11:06:16 -0800 | [diff] [blame] | 264 | #if defined(__ANDROID__) |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 265 | } else if (option_name == "--log-to-android-buffer") { |
Yabin Cui | d9121ce | 2019-02-07 11:06:16 -0800 | [diff] [blame] | 266 | android::base::SetLogger(android::base::LogdLogger()); |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 267 | log_to_android_buffer = true; |
Yabin Cui | d9121ce | 2019-02-07 11:06:16 -0800 | [diff] [blame] | 268 | #endif |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 269 | } else if (option_name == "--version") { |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 270 | LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion(); |
| 271 | return true; |
| 272 | } else { |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 273 | CHECK(false) << "Unreachable code"; |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 276 | while (i < argc) { |
| 277 | args.emplace_back(argv[i++]); |
| 278 | } |
| 279 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 280 | android::base::ScopedLogSeverity severity(log_severity); |
| 281 | |
| 282 | if (args.empty()) { |
| 283 | args.push_back("help"); |
| 284 | } |
| 285 | std::unique_ptr<Command> command = CreateCommandInstance(args[0]); |
| 286 | if (command == nullptr) { |
| 287 | LOG(ERROR) << "malformed command line: unknown command " << args[0]; |
| 288 | return false; |
| 289 | } |
| 290 | std::string command_name = args[0]; |
| 291 | args.erase(args.begin()); |
| 292 | |
| 293 | LOG(DEBUG) << "command '" << command_name << "' starts running"; |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 294 | int exit_code; |
| 295 | command->Run(args, &exit_code); |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 296 | LOG(DEBUG) << "command '" << command_name << "' " |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 297 | << (exit_code == 0 ? "finished successfully" : "failed"); |
Elliott Hughes | 697f2e6 | 2019-10-29 18:34:28 -0700 | [diff] [blame] | 298 | // Quick exit to avoid the cost of freeing memory and closing files. |
Yabin Cui | fbd54f7 | 2018-02-26 12:12:44 -0800 | [diff] [blame] | 299 | fflush(stdout); |
| 300 | fflush(stderr); |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 301 | _Exit(exit_code); |
| 302 | return exit_code == 0; |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 303 | } |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 304 | |
| 305 | } // namespace simpleperf |