blob: 35db84696e90f7cd37b7929415dc14ebac1473d6 [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#ifndef SIMPLE_PERF_COMMAND_H_
18#define SIMPLE_PERF_COMMAND_H_
19
Yabin Cuif79f07e2015-06-01 11:21:37 -070020#include <functional>
Yabin Cuiacf04b22018-04-18 13:10:40 -070021#include <limits>
Yabin Cuiacbdb242020-07-07 15:56:34 -070022#include <map>
23#include <memory>
24#include <optional>
Yabin Cui67d3abd2015-04-16 15:26:31 -070025#include <string>
Yabin Cuiacbdb242020-07-07 15:56:34 -070026#include <unordered_map>
Yabin Cui67d3abd2015-04-16 15:26:31 -070027#include <vector>
28
Yabin Cuiacf04b22018-04-18 13:10:40 -070029#include <android-base/logging.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080030#include <android-base/macros.h>
Yabin Cuiacf04b22018-04-18 13:10:40 -070031#include <android-base/parseint.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070032
Yabin Cuiacbdb242020-07-07 15:56:34 -070033namespace simpleperf {
34
35using OptionName = std::string;
36
37enum class OptionType {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020038 SINGLE, // this option has a single value (use the last one in the arg list)
Yabin Cuiacbdb242020-07-07 15:56:34 -070039 MULTIPLE, // this option can have multiple values (keep all values appeared in the arg list)
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020040 ORDERED, // keep the order of this option in the arg list
Yabin Cuiacbdb242020-07-07 15:56:34 -070041};
42
43enum class OptionValueType {
44 NONE, // No value is needed
45 STRING,
46 OPT_STRING, // optional string
47 UINT,
48 DOUBLE,
49};
50
Yabin Cui6f094672020-07-22 14:50:35 -070051// Whether an option is allowed to pass through simpleperf_app_runner.
52enum class AppRunnerType {
53 NOT_ALLOWED,
54 ALLOWED,
55 CHECK_FD,
56 CHECK_PATH,
57};
58
Yabin Cuiacbdb242020-07-07 15:56:34 -070059struct OptionFormat {
60 OptionValueType value_type;
61 OptionType type;
Yabin Cuie3ca9982020-10-16 13:16:26 -070062 AppRunnerType app_runner_type = AppRunnerType::NOT_ALLOWED;
Yabin Cuiacbdb242020-07-07 15:56:34 -070063};
64
Yabin Cui6f094672020-07-22 14:50:35 -070065using OptionFormatMap = std::unordered_map<OptionName, OptionFormat>;
66
Yabin Cuiacbdb242020-07-07 15:56:34 -070067union OptionValue {
68 const std::string* str_value;
69 uint64_t uint_value;
70 double double_value;
71};
72
73struct OptionValueMap {
74 std::multimap<OptionName, OptionValue> values;
75
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020076 bool PullBoolValue(const OptionName& name) { return PullValue(name).has_value(); }
Yabin Cuiacbdb242020-07-07 15:56:34 -070077
78 template <typename T>
79 bool PullUintValue(const OptionName& name, T* value, uint64_t min = 0,
80 uint64_t max = std::numeric_limits<T>::max()) {
81 if (auto option_value = PullValue(name); option_value) {
82 if (option_value->uint_value < min || option_value->uint_value > max) {
83 LOG(ERROR) << "invalid " << name << ": " << option_value->uint_value;
84 return false;
85 }
86 *value = option_value->uint_value;
87 }
88 return true;
89 }
90
91 bool PullDoubleValue(const OptionName& name, double* value,
92 double min = std::numeric_limits<double>::lowest(),
93 double max = std::numeric_limits<double>::max()) {
94 if (auto option_value = PullValue(name); option_value) {
95 if (option_value->double_value < min || option_value->double_value > max) {
96 LOG(ERROR) << "invalid " << name << ": " << option_value->double_value;
97 return false;
98 }
99 *value = option_value->double_value;
100 }
101 return true;
102 }
103
Yabin Cuie3ca9982020-10-16 13:16:26 -0700104 void PullStringValue(const OptionName& name, std::string* value) {
105 if (auto option_value = PullValue(name); option_value) {
106 CHECK(option_value->str_value != nullptr);
107 *value = *option_value->str_value;
108 }
109 }
110
Yabin Cuiacbdb242020-07-07 15:56:34 -0700111 std::optional<OptionValue> PullValue(const OptionName& name) {
112 std::optional<OptionValue> res;
113 if (auto it = values.find(name); it != values.end()) {
114 res.emplace(it->second);
115 values.erase(it);
116 }
117 return res;
118 }
119
Yabin Cui1c6be752023-02-28 11:46:37 -0800120 std::vector<std::string> PullStringValues(const OptionName& name) {
121 std::vector<std::string> res;
122 for (const auto& value : PullValues(name)) {
123 res.emplace_back(*value.str_value);
124 }
125 return res;
126 }
127
Yabin Cuie3ca9982020-10-16 13:16:26 -0700128 std::vector<OptionValue> PullValues(const OptionName& name) {
Yabin Cuiacbdb242020-07-07 15:56:34 -0700129 auto pair = values.equal_range(name);
130 if (pair.first != pair.second) {
131 std::vector<OptionValue> res;
132 for (auto it = pair.first; it != pair.second; ++it) {
133 res.emplace_back(it->second);
134 }
135 values.erase(name);
136 return res;
137 }
138 return {};
139 }
140};
141
Yabin Cui690f00b2022-01-07 14:46:09 -0800142bool ConvertArgsToOptions(const std::vector<std::string>& args,
143 const OptionFormatMap& option_formats, const std::string& help_msg,
144 OptionValueMap* options,
145 std::vector<std::pair<OptionName, OptionValue>>* ordered_options,
146 std::vector<std::string>* non_option_args);
147
Yabin Cui6f094672020-07-22 14:50:35 -0700148inline const OptionFormatMap& GetCommonOptionFormatMap() {
149 static const OptionFormatMap option_formats = {
150 {"-h", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
151 {"--help", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
152 {"--log", {OptionValueType::STRING, OptionType::SINGLE, AppRunnerType::ALLOWED}},
153 {"--log-to-android-buffer",
154 {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
155 {"--version", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
156 };
157 return option_formats;
158}
159
Yabin Cui67d3abd2015-04-16 15:26:31 -0700160class Command {
161 public:
162 Command(const std::string& name, const std::string& short_help_string,
163 const std::string& long_help_string)
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200164 : name_(name), short_help_string_(short_help_string), long_help_string_(long_help_string) {}
Yabin Cui67d3abd2015-04-16 15:26:31 -0700165
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200166 virtual ~Command() {}
Yabin Cui67d3abd2015-04-16 15:26:31 -0700167
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200168 const std::string& Name() const { return name_; }
Yabin Cui67d3abd2015-04-16 15:26:31 -0700169
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200170 const std::string& ShortHelpString() const { return short_help_string_; }
Yabin Cui67d3abd2015-04-16 15:26:31 -0700171
Yabin Cuicfdc7832023-03-02 15:35:01 -0800172 virtual std::string LongHelpString() const { return long_help_string_; }
Yabin Cui67d3abd2015-04-16 15:26:31 -0700173
Yabin Cui248ef5e2021-12-16 14:09:39 -0800174 virtual bool Run(const std::vector<std::string>&) { return false; }
175 virtual void Run(const std::vector<std::string>& args, int* exit_code) {
176 *exit_code = Run(args) ? 0 : 1;
177 }
Yabin Cui67d3abd2015-04-16 15:26:31 -0700178
Yabin Cuiacbdb242020-07-07 15:56:34 -0700179 bool PreprocessOptions(const std::vector<std::string>& args,
Yabin Cui6f094672020-07-22 14:50:35 -0700180 const OptionFormatMap& option_formats, OptionValueMap* options,
Yabin Cuiacbdb242020-07-07 15:56:34 -0700181 std::vector<std::pair<OptionName, OptionValue>>* ordered_options,
182 std::vector<std::string>* non_option_args = nullptr);
183
Yabin Cuiacf04b22018-04-18 13:10:40 -0700184 template <typename T>
185 bool GetUintOption(const std::vector<std::string>& args, size_t* pi, T* value, uint64_t min = 0,
186 uint64_t max = std::numeric_limits<T>::max(), bool allow_suffixes = false) {
187 if (!NextArgumentOrError(args, pi)) {
188 return false;
189 }
190 uint64_t tmp_value;
191 if (!android::base::ParseUint(args[*pi], &tmp_value, max, allow_suffixes) || tmp_value < min) {
192 LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi];
193 return false;
194 }
195 *value = static_cast<T>(tmp_value);
196 return true;
197 }
198
199 bool GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value,
200 double min = 0, double max = std::numeric_limits<double>::max());
201
Yabin Cuif79f07e2015-06-01 11:21:37 -0700202 protected:
203 bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi);
204 void ReportUnknownOption(const std::vector<std::string>& args, size_t i);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700205
Yabin Cui67d3abd2015-04-16 15:26:31 -0700206 const std::string name_;
207 const std::string short_help_string_;
208 const std::string long_help_string_;
209
Yabin Cui67d3abd2015-04-16 15:26:31 -0700210 DISALLOW_COPY_AND_ASSIGN(Command);
211};
212
Yabin Cuif79f07e2015-06-01 11:21:37 -0700213void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -0700214 const std::function<std::unique_ptr<Command>(void)>& callback);
Yabin Cuif79f07e2015-06-01 11:21:37 -0700215void UnRegisterCommand(const std::string& cmd_name);
216std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name);
217const std::vector<std::string> GetAllCommandNames();
Yabin Cui616b3a02017-07-14 15:59:56 -0700218bool RunSimpleperfCmd(int argc, char** argv);
Yabin Cuif79f07e2015-06-01 11:21:37 -0700219
Yabin Cuia556c562020-02-14 16:50:10 -0800220extern bool log_to_android_buffer;
Yabin Cuiacbdb242020-07-07 15:56:34 -0700221
222} // namespace simpleperf
Yabin Cuia556c562020-02-14 16:50:10 -0800223
Yabin Cui67d3abd2015-04-16 15:26:31 -0700224#endif // SIMPLE_PERF_COMMAND_H_