blob: 3c0aa61a02bef0418a56ca3aa30fd148c3107cc7 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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 Cuiacbdb242020-07-07 15:56:34 -070019#include <string.h>
20
Yabin Cui67d3abd2015-04-16 15:26:31 -070021#include <algorithm>
Yabin Cuif79f07e2015-06-01 11:21:37 -070022#include <map>
Yabin Cui67d3abd2015-04-16 15:26:31 -070023#include <string>
24#include <vector>
25
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/logging.h>
Yabin Cuiacf04b22018-04-18 13:10:40 -070027#include <android-base/parsedouble.h>
28#include <android-base/parseint.h>
Yabin Cuif79f07e2015-06-01 11:21:37 -070029
Yabin Cui616b3a02017-07-14 15:59:56 -070030#include "utils.h"
31
Yabin Cuiacbdb242020-07-07 15:56:34 -070032namespace simpleperf {
Yabin Cuia556c562020-02-14 16:50:10 -080033
Yabin Cuif79f07e2015-06-01 11:21:37 -070034bool 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 Cui690f00b2022-01-07 14:46:09 -080044bool 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 Cuiacbdb242020-07-07 15:56:34 -070049 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 Cui86e7a162024-07-16 16:28:33 -070053 std::string value_after_equal;
Yabin Cuiacbdb242020-07-07 15:56:34 -070054 auto it = option_formats.find(args[i]);
55 if (it == option_formats.end()) {
Yabin Cuif5d7a3b2024-07-11 17:11:55 -070056 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 Cuiacbdb242020-07-07 15:56:34 -070059 }
Yabin Cuif5d7a3b2024-07-11 17:11:55 -070060 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 Cuiacbdb242020-07-07 15:56:34 -070068 }
69 const OptionName& name = it->first;
70 const OptionFormat& format = it->second;
71 OptionValue value;
Yabin Cuiacbdb242020-07-07 15:56:34 -070072
Yabin Cui86e7a162024-07-16 16:28:33 -070073 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 Cuif5d7a3b2024-07-11 17:11:55 -070085 value.str_value = args[++i];
Yabin Cui86e7a162024-07-16 16:28:33 -070086 }
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 Cuiacbdb242020-07-07 15:56:34 -0700114 }
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 Cui690f00b2022-01-07 14:46:09 -0800134 LOG(ERROR) << "Invalid option " << args[i] << "." << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -0700135 return false;
136 }
137 non_option_args->assign(args.begin() + i, args.end());
138 }
139 return true;
140}
141
Yabin Cui690f00b2022-01-07 14:46:09 -0800142bool 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 Cuiacf04b22018-04-18 13:10:40 -0700151bool 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 Cuif79f07e2015-06-01 11:21:37 -0700163void 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
168typedef std::function<std::unique_ptr<Command>(void)> callback_t;
169
170static std::map<std::string, callback_t>& CommandMap() {
Yabin Cui67d3abd2015-04-16 15:26:31 -0700171 // 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 Cuif79f07e2015-06-01 11:21:37 -0700173 static std::map<std::string, callback_t> command_map;
174 return command_map;
Yabin Cui67d3abd2015-04-16 15:26:31 -0700175}
176
Yabin Cuif79f07e2015-06-01 11:21:37 -0700177void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -0700178 const std::function<std::unique_ptr<Command>(void)>& callback) {
Yabin Cuif79f07e2015-06-01 11:21:37 -0700179 CommandMap().insert(std::make_pair(cmd_name, callback));
180}
181
182void UnRegisterCommand(const std::string& cmd_name) {
183 CommandMap().erase(cmd_name);
184}
185
186std::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
191const std::vector<std::string> GetAllCommandNames() {
192 std::vector<std::string> names;
Chih-Hung Hsieh742d78a2018-12-11 10:45:02 -0800193 for (const auto& pair : CommandMap()) {
Yabin Cuif79f07e2015-06-01 11:21:37 -0700194 names.push_back(pair.first);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700195 }
Yabin Cuif79f07e2015-06-01 11:21:37 -0700196 return names;
Yabin Cui323e9452015-04-20 18:07:17 -0700197}
Yabin Cuiff7465c2016-02-25 11:02:30 -0800198
Yabin Cui34227b22023-07-20 19:40:28 +0000199void RegisterAllCommands() {
200 RegisterDumpRecordCommand();
201 RegisterHelpCommand();
202 RegisterInjectCommand();
203 RegisterKmemCommand();
204 RegisterMergeCommand();
205 RegisterReportCommand();
206 RegisterReportSampleCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800207#if defined(__linux__)
Yabin Cuif5d7a3b2024-07-11 17:11:55 -0700208 RegisterListCommand();
209 RegisterRecordCommand();
210 RegisterStatCommand();
211 RegisterDebugUnwindCommand();
212 RegisterTraceSchedCommand();
213 RegisterMonitorCommand();
Yabin Cui1befe4f2019-02-25 15:22:43 -0800214#if defined(__ANDROID__)
Yabin Cuif5d7a3b2024-07-11 17:11:55 -0700215 RegisterAPICommands();
216 RegisterBootRecordCommand();
Yabin Cui1befe4f2019-02-25 15:22:43 -0800217#endif
Yabin Cuiff7465c2016-02-25 11:02:30 -0800218#endif
Yabin Cui34227b22023-07-20 19:40:28 +0000219}
Yabin Cui616b3a02017-07-14 15:59:56 -0700220
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200221static void StderrLogger(android::base::LogId, android::base::LogSeverity severity, const char*,
222 const char* file, unsigned int line, const char* message) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700223 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 Cuia556c562020-02-14 16:50:10 -0800228bool log_to_android_buffer = false;
Yabin Cuia556c562020-02-14 16:50:10 -0800229
Yabin Cui616b3a02017-07-14 15:59:56 -0700230bool RunSimpleperfCmd(int argc, char** argv) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700231 android::base::InitLogging(argv, StderrLogger);
Yabin Cui616b3a02017-07-14 15:59:56 -0700232 std::vector<std::string> args;
233 android::base::LogSeverity log_severity = android::base::INFO;
Yabin Cuia556c562020-02-14 16:50:10 -0800234 log_to_android_buffer = false;
Yabin Cui6f094672020-07-22 14:50:35 -0700235 const OptionFormatMap& common_option_formats = GetCommonOptionFormatMap();
Yabin Cui616b3a02017-07-14 15:59:56 -0700236
Yabin Cui6f094672020-07-22 14:50:35 -0700237 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 Cui616b3a02017-07-14 15:59:56 -0700250 args.insert(args.begin(), "help");
Yabin Cui6f094672020-07-22 14:50:35 -0700251 } else if (option_name == "--log") {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200252 if (!GetLogSeverity(argv[i + 1], &log_severity)) {
253 LOG(ERROR) << "Unknown log severity: " << argv[i + 1];
Yabin Cui616b3a02017-07-14 15:59:56 -0700254 }
Yabin Cui6f094672020-07-22 14:50:35 -0700255 ++i;
Yabin Cuid9121ce2019-02-07 11:06:16 -0800256#if defined(__ANDROID__)
Yabin Cui6f094672020-07-22 14:50:35 -0700257 } else if (option_name == "--log-to-android-buffer") {
Yabin Cuid9121ce2019-02-07 11:06:16 -0800258 android::base::SetLogger(android::base::LogdLogger());
Yabin Cuia556c562020-02-14 16:50:10 -0800259 log_to_android_buffer = true;
Yabin Cuid9121ce2019-02-07 11:06:16 -0800260#endif
Yabin Cui6f094672020-07-22 14:50:35 -0700261 } else if (option_name == "--version") {
Yabin Cui616b3a02017-07-14 15:59:56 -0700262 LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion();
263 return true;
264 } else {
Yabin Cui6f094672020-07-22 14:50:35 -0700265 CHECK(false) << "Unreachable code";
Yabin Cui616b3a02017-07-14 15:59:56 -0700266 }
267 }
Yabin Cui6f094672020-07-22 14:50:35 -0700268 while (i < argc) {
269 args.emplace_back(argv[i++]);
270 }
271
Yabin Cui616b3a02017-07-14 15:59:56 -0700272 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 Cui248ef5e2021-12-16 14:09:39 -0800286 int exit_code;
287 command->Run(args, &exit_code);
Yabin Cui616b3a02017-07-14 15:59:56 -0700288 LOG(DEBUG) << "command '" << command_name << "' "
Yabin Cui248ef5e2021-12-16 14:09:39 -0800289 << (exit_code == 0 ? "finished successfully" : "failed");
Elliott Hughes697f2e62019-10-29 18:34:28 -0700290 // Quick exit to avoid the cost of freeing memory and closing files.
Yabin Cuifbd54f72018-02-26 12:12:44 -0800291 fflush(stdout);
292 fflush(stderr);
Yabin Cui248ef5e2021-12-16 14:09:39 -0800293 _Exit(exit_code);
294 return exit_code == 0;
Yabin Cui616b3a02017-07-14 15:59:56 -0700295}
Yabin Cuiacbdb242020-07-07 15:56:34 -0700296
297} // namespace simpleperf