blob: f48b464998554f3bd21a1a234107c6261004e735 [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++) {
53 auto it = option_formats.find(args[i]);
54 if (it == option_formats.end()) {
55 if (args[i] == "--") {
56 i++;
57 break;
58 }
Yabin Cui690f00b2022-01-07 14:46:09 -080059 LOG(ERROR) << "Unknown option " << args[i] << "." << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -070060 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 Cui690f00b2022-01-07 14:46:09 -080070 LOG(ERROR) << "No argument following " << name << " option." << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -070071 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 Cui690f00b2022-01-07 14:46:09 -080088 LOG(ERROR) << "Invalid argument for option " << name << ": " << args[i] << "."
89 << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -070090 return false;
91 }
92 break;
93 case OptionValueType::DOUBLE:
94 if (!android::base::ParseDouble(args[++i], &value.double_value)) {
Yabin Cui690f00b2022-01-07 14:46:09 -080095 LOG(ERROR) << "Invalid argument for option " << name << ": " << args[i] << "."
96 << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -070097 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 Cui690f00b2022-01-07 14:46:09 -0800121 LOG(ERROR) << "Invalid option " << args[i] << "." << help_msg;
Yabin Cuiacbdb242020-07-07 15:56:34 -0700122 return false;
123 }
124 non_option_args->assign(args.begin() + i, args.end());
125 }
126 return true;
127}
128
Yabin Cui690f00b2022-01-07 14:46:09 -0800129bool 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 Cuiacf04b22018-04-18 13:10:40 -0700138bool 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 Cuif79f07e2015-06-01 11:21:37 -0700150void 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
155typedef std::function<std::unique_ptr<Command>(void)> callback_t;
156
157static std::map<std::string, callback_t>& CommandMap() {
Yabin Cui67d3abd2015-04-16 15:26:31 -0700158 // 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 Cuif79f07e2015-06-01 11:21:37 -0700160 static std::map<std::string, callback_t> command_map;
161 return command_map;
Yabin Cui67d3abd2015-04-16 15:26:31 -0700162}
163
Yabin Cuif79f07e2015-06-01 11:21:37 -0700164void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -0700165 const std::function<std::unique_ptr<Command>(void)>& callback) {
Yabin Cuif79f07e2015-06-01 11:21:37 -0700166 CommandMap().insert(std::make_pair(cmd_name, callback));
167}
168
169void UnRegisterCommand(const std::string& cmd_name) {
170 CommandMap().erase(cmd_name);
171}
172
173std::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
178const std::vector<std::string> GetAllCommandNames() {
179 std::vector<std::string> names;
Chih-Hung Hsieh742d78a2018-12-11 10:45:02 -0800180 for (const auto& pair : CommandMap()) {
Yabin Cuif79f07e2015-06-01 11:21:37 -0700181 names.push_back(pair.first);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700182 }
Yabin Cuif79f07e2015-06-01 11:21:37 -0700183 return names;
Yabin Cui323e9452015-04-20 18:07:17 -0700184}
Yabin Cuiff7465c2016-02-25 11:02:30 -0800185
Yabin Cui321bfe62022-01-15 14:09:46 -0800186extern void RegisterBootRecordCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800187extern void RegisterDumpRecordCommand();
188extern void RegisterHelpCommand();
Yabin Cuic573eaa2019-08-21 16:05:07 -0700189extern void RegisterInjectCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800190extern void RegisterListCommand();
Yabin Cui6965d422016-06-15 11:41:42 -0700191extern void RegisterKmemCommand();
Yabin Cui8d005de2020-10-21 13:48:53 -0700192extern void RegisterMergeCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800193extern void RegisterRecordCommand();
194extern void RegisterReportCommand();
Yabin Cui767dd172016-06-02 21:02:43 -0700195extern void RegisterReportSampleCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800196extern void RegisterStatCommand();
Yabin Cuic3bf9d02018-02-02 14:18:40 -0800197extern void RegisterDebugUnwindCommand();
Yabin Cuibbaa5122018-03-19 13:58:51 -0700198extern void RegisterTraceSchedCommand();
Yabin Cui1befe4f2019-02-25 15:22:43 -0800199extern void RegisterAPICommands();
Thiébaud Weksteenf5c93be2020-10-19 13:07:20 +0200200extern void RegisterMonitorCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800201
202class CommandRegister {
203 public:
204 CommandRegister() {
205 RegisterDumpRecordCommand();
206 RegisterHelpCommand();
Yabin Cuic573eaa2019-08-21 16:05:07 -0700207 RegisterInjectCommand();
Yabin Cui6965d422016-06-15 11:41:42 -0700208 RegisterKmemCommand();
Yabin Cui8d005de2020-10-21 13:48:53 -0700209 RegisterMergeCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800210 RegisterReportCommand();
Yabin Cui767dd172016-06-02 21:02:43 -0700211 RegisterReportSampleCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -0800212#if defined(__linux__)
213 RegisterListCommand();
214 RegisterRecordCommand();
215 RegisterStatCommand();
Yabin Cuic3bf9d02018-02-02 14:18:40 -0800216 RegisterDebugUnwindCommand();
Yabin Cuibbaa5122018-03-19 13:58:51 -0700217 RegisterTraceSchedCommand();
Thiébaud Weksteenf5c93be2020-10-19 13:07:20 +0200218 RegisterMonitorCommand();
Yabin Cui1befe4f2019-02-25 15:22:43 -0800219#if defined(__ANDROID__)
220 RegisterAPICommands();
Yabin Cui321bfe62022-01-15 14:09:46 -0800221 RegisterBootRecordCommand();
Yabin Cui1befe4f2019-02-25 15:22:43 -0800222#endif
Yabin Cuiff7465c2016-02-25 11:02:30 -0800223#endif
224 }
225};
226
227CommandRegister command_register;
Yabin Cui616b3a02017-07-14 15:59:56 -0700228
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200229static void StderrLogger(android::base::LogId, android::base::LogSeverity severity, const char*,
230 const char* file, unsigned int line, const char* message) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700231 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 Cuia556c562020-02-14 16:50:10 -0800236bool log_to_android_buffer = false;
Yabin Cuia556c562020-02-14 16:50:10 -0800237
Yabin Cui616b3a02017-07-14 15:59:56 -0700238bool RunSimpleperfCmd(int argc, char** argv) {
Yabin Cuie466d4d2017-08-11 17:03:07 -0700239 android::base::InitLogging(argv, StderrLogger);
Yabin Cui616b3a02017-07-14 15:59:56 -0700240 std::vector<std::string> args;
241 android::base::LogSeverity log_severity = android::base::INFO;
Yabin Cuia556c562020-02-14 16:50:10 -0800242 log_to_android_buffer = false;
Yabin Cui6f094672020-07-22 14:50:35 -0700243 const OptionFormatMap& common_option_formats = GetCommonOptionFormatMap();
Yabin Cui616b3a02017-07-14 15:59:56 -0700244
Yabin Cui6f094672020-07-22 14:50:35 -0700245 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 Cui616b3a02017-07-14 15:59:56 -0700258 args.insert(args.begin(), "help");
Yabin Cui6f094672020-07-22 14:50:35 -0700259 } else if (option_name == "--log") {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200260 if (!GetLogSeverity(argv[i + 1], &log_severity)) {
261 LOG(ERROR) << "Unknown log severity: " << argv[i + 1];
Yabin Cui616b3a02017-07-14 15:59:56 -0700262 }
Yabin Cui6f094672020-07-22 14:50:35 -0700263 ++i;
Yabin Cuid9121ce2019-02-07 11:06:16 -0800264#if defined(__ANDROID__)
Yabin Cui6f094672020-07-22 14:50:35 -0700265 } else if (option_name == "--log-to-android-buffer") {
Yabin Cuid9121ce2019-02-07 11:06:16 -0800266 android::base::SetLogger(android::base::LogdLogger());
Yabin Cuia556c562020-02-14 16:50:10 -0800267 log_to_android_buffer = true;
Yabin Cuid9121ce2019-02-07 11:06:16 -0800268#endif
Yabin Cui6f094672020-07-22 14:50:35 -0700269 } else if (option_name == "--version") {
Yabin Cui616b3a02017-07-14 15:59:56 -0700270 LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion();
271 return true;
272 } else {
Yabin Cui6f094672020-07-22 14:50:35 -0700273 CHECK(false) << "Unreachable code";
Yabin Cui616b3a02017-07-14 15:59:56 -0700274 }
275 }
Yabin Cui6f094672020-07-22 14:50:35 -0700276 while (i < argc) {
277 args.emplace_back(argv[i++]);
278 }
279
Yabin Cui616b3a02017-07-14 15:59:56 -0700280 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 Cui248ef5e2021-12-16 14:09:39 -0800294 int exit_code;
295 command->Run(args, &exit_code);
Yabin Cui616b3a02017-07-14 15:59:56 -0700296 LOG(DEBUG) << "command '" << command_name << "' "
Yabin Cui248ef5e2021-12-16 14:09:39 -0800297 << (exit_code == 0 ? "finished successfully" : "failed");
Elliott Hughes697f2e62019-10-29 18:34:28 -0700298 // Quick exit to avoid the cost of freeing memory and closing files.
Yabin Cuifbd54f72018-02-26 12:12:44 -0800299 fflush(stdout);
300 fflush(stderr);
Yabin Cui248ef5e2021-12-16 14:09:39 -0800301 _Exit(exit_code);
302 return exit_code == 0;
Yabin Cui616b3a02017-07-14 15:59:56 -0700303}
Yabin Cuiacbdb242020-07-07 15:56:34 -0700304
305} // namespace simpleperf