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