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 #ifndef SIMPLE_PERF_COMMAND_H_
18 #define SIMPLE_PERF_COMMAND_H_
19 
20 #include <functional>
21 #include <limits>
22 #include <map>
23 #include <memory>
24 #include <optional>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include <android-base/logging.h>
30 #include <android-base/macros.h>
31 #include <android-base/parseint.h>
32 
33 namespace simpleperf {
34 
35 using OptionName = std::string;
36 
37 enum class OptionType {
38   SINGLE,  // this option has a single value (use the last one in the arg list)
39   MULTIPLE,  // this option can have multiple values (keep all values appeared in the arg list)
40   ORDERED,  // keep the order of this option in the arg list
41 };
42 
43 enum class OptionValueType {
44   NONE,  // No value is needed
45   STRING,
46   OPT_STRING,  // optional string
47   UINT,
48   DOUBLE,
49 };
50 
51 // Whether an option is allowed to pass through simpleperf_app_runner.
52 enum class AppRunnerType {
53   NOT_ALLOWED,
54   ALLOWED,
55   CHECK_FD,
56   CHECK_PATH,
57 };
58 
59 struct OptionFormat {
60   OptionValueType value_type;
61   OptionType type;
62   AppRunnerType app_runner_type;
63 };
64 
65 using OptionFormatMap = std::unordered_map<OptionName, OptionFormat>;
66 
67 union OptionValue {
68   const std::string* str_value;
69   uint64_t uint_value;
70   double double_value;
71 };
72 
73 struct OptionValueMap {
74   std::multimap<OptionName, OptionValue> values;
75 
PullBoolValueOptionValueMap76   bool PullBoolValue(const OptionName& name) {
77     return PullValue(name).has_value();
78   }
79 
80   template <typename T>
81   bool PullUintValue(const OptionName& name, T* value, uint64_t min = 0,
82                      uint64_t max = std::numeric_limits<T>::max()) {
83     if (auto option_value = PullValue(name); option_value) {
84       if (option_value->uint_value < min || option_value->uint_value > max) {
85         LOG(ERROR) << "invalid " << name << ": " << option_value->uint_value;
86         return false;
87       }
88       *value = option_value->uint_value;
89     }
90     return true;
91   }
92 
93   bool PullDoubleValue(const OptionName& name, double* value,
94                        double min = std::numeric_limits<double>::lowest(),
95                        double max = std::numeric_limits<double>::max()) {
96     if (auto option_value = PullValue(name); option_value) {
97       if (option_value->double_value < min || option_value->double_value > max) {
98         LOG(ERROR) << "invalid " << name << ": " << option_value->double_value;
99         return false;
100       }
101       *value = option_value->double_value;
102     }
103     return true;
104   }
105 
PullValueOptionValueMap106   std::optional<OptionValue> PullValue(const OptionName& name) {
107     std::optional<OptionValue> res;
108     if (auto it = values.find(name); it != values.end()) {
109       res.emplace(it->second);
110       values.erase(it);
111     }
112     return res;
113   }
114 
PullValuesOptionValueMap115   std::optional<std::vector<OptionValue>> PullValues(const OptionName& name) {
116     auto pair = values.equal_range(name);
117     if (pair.first != pair.second) {
118       std::vector<OptionValue> res;
119       for (auto it = pair.first; it != pair.second; ++it) {
120         res.emplace_back(it->second);
121       }
122       values.erase(name);
123       return res;
124     }
125     return {};
126   }
127 };
128 
GetCommonOptionFormatMap()129 inline const OptionFormatMap& GetCommonOptionFormatMap() {
130   static const OptionFormatMap option_formats = {
131       {"-h", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
132       {"--help", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
133       {"--log", {OptionValueType::STRING, OptionType::SINGLE, AppRunnerType::ALLOWED}},
134       {"--log-to-android-buffer",
135        {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
136       {"--version", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
137   };
138   return option_formats;
139 }
140 
141 class Command {
142  public:
Command(const std::string & name,const std::string & short_help_string,const std::string & long_help_string)143   Command(const std::string& name, const std::string& short_help_string,
144           const std::string& long_help_string)
145       : name_(name), short_help_string_(short_help_string), long_help_string_(long_help_string) {
146   }
147 
~Command()148   virtual ~Command() {
149   }
150 
Name()151   const std::string& Name() const {
152     return name_;
153   }
154 
ShortHelpString()155   const std::string& ShortHelpString() const {
156     return short_help_string_;
157   }
158 
LongHelpString()159   const std::string LongHelpString() const {
160     return long_help_string_;
161   }
162 
163   virtual bool Run(const std::vector<std::string>& args) = 0;
164 
165   bool PreprocessOptions(const std::vector<std::string>& args,
166                          const OptionFormatMap& option_formats, OptionValueMap* options,
167                          std::vector<std::pair<OptionName, OptionValue>>* ordered_options,
168                          std::vector<std::string>* non_option_args = nullptr);
169 
170   template <typename T>
171   bool GetUintOption(const std::vector<std::string>& args, size_t* pi, T* value, uint64_t min = 0,
172                      uint64_t max = std::numeric_limits<T>::max(), bool allow_suffixes = false) {
173     if (!NextArgumentOrError(args, pi)) {
174       return false;
175     }
176     uint64_t tmp_value;
177     if (!android::base::ParseUint(args[*pi], &tmp_value, max, allow_suffixes) || tmp_value < min) {
178       LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi];
179       return false;
180     }
181     *value = static_cast<T>(tmp_value);
182     return true;
183   }
184 
185   bool GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value,
186                        double min = 0, double max = std::numeric_limits<double>::max());
187 
188  protected:
189   bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi);
190   void ReportUnknownOption(const std::vector<std::string>& args, size_t i);
191 
192  private:
193   const std::string name_;
194   const std::string short_help_string_;
195   const std::string long_help_string_;
196 
197   DISALLOW_COPY_AND_ASSIGN(Command);
198 };
199 
200 void RegisterCommand(const std::string& cmd_name,
201                      const std::function<std::unique_ptr<Command>(void)>& callback);
202 void UnRegisterCommand(const std::string& cmd_name);
203 std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name);
204 const std::vector<std::string> GetAllCommandNames();
205 bool RunSimpleperfCmd(int argc, char** argv);
206 
207 extern bool log_to_android_buffer;
208 
209 }  // namespace simpleperf
210 
211 #endif  // SIMPLE_PERF_COMMAND_H_
212