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
19 #include <algorithm>
20 #include <map>
21 #include <string>
22 #include <vector>
23
24 #include <android-base/logging.h>
25 #include <android-base/parsedouble.h>
26 #include <android-base/parseint.h>
27 #include <android-base/quick_exit.h>
28
29 #include "utils.h"
30
NextArgumentOrError(const std::vector<std::string> & args,size_t * pi)31 bool Command::NextArgumentOrError(const std::vector<std::string>& args, size_t* pi) {
32 if (*pi + 1 == args.size()) {
33 LOG(ERROR) << "No argument following " << args[*pi] << " option. Try `simpleperf help " << name_
34 << "`";
35 return false;
36 }
37 ++*pi;
38 return true;
39 }
40
GetDoubleOption(const std::vector<std::string> & args,size_t * pi,double * value,double min,double max)41 bool Command::GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value,
42 double min, double max) {
43 if (!NextArgumentOrError(args, pi)) {
44 return false;
45 }
46 if (!android::base::ParseDouble(args[*pi].c_str(), value, min, max)) {
47 LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi];
48 return false;
49 }
50 return true;
51 }
52
ReportUnknownOption(const std::vector<std::string> & args,size_t i)53 void Command::ReportUnknownOption(const std::vector<std::string>& args, size_t i) {
54 LOG(ERROR) << "Unknown option for " << name_ << " command: '" << args[i]
55 << "'. Try `simpleperf help " << name_ << "`";
56 }
57
58 typedef std::function<std::unique_ptr<Command>(void)> callback_t;
59
CommandMap()60 static std::map<std::string, callback_t>& CommandMap() {
61 // commands is used in the constructor of Command. Defining it as a static
62 // variable in a function makes sure it is initialized before use.
63 static std::map<std::string, callback_t> command_map;
64 return command_map;
65 }
66
RegisterCommand(const std::string & cmd_name,const std::function<std::unique_ptr<Command> (void)> & callback)67 void RegisterCommand(const std::string& cmd_name,
68 const std::function<std::unique_ptr<Command>(void)>& callback) {
69 CommandMap().insert(std::make_pair(cmd_name, callback));
70 }
71
UnRegisterCommand(const std::string & cmd_name)72 void UnRegisterCommand(const std::string& cmd_name) {
73 CommandMap().erase(cmd_name);
74 }
75
CreateCommandInstance(const std::string & cmd_name)76 std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name) {
77 auto it = CommandMap().find(cmd_name);
78 return (it == CommandMap().end()) ? nullptr : (it->second)();
79 }
80
GetAllCommandNames()81 const std::vector<std::string> GetAllCommandNames() {
82 std::vector<std::string> names;
83 for (const auto& pair : CommandMap()) {
84 names.push_back(pair.first);
85 }
86 return names;
87 }
88
89 extern void RegisterDumpRecordCommand();
90 extern void RegisterHelpCommand();
91 extern void RegisterListCommand();
92 extern void RegisterKmemCommand();
93 extern void RegisterRecordCommand();
94 extern void RegisterReportCommand();
95 extern void RegisterReportSampleCommand();
96 extern void RegisterStatCommand();
97 extern void RegisterDebugUnwindCommand();
98 extern void RegisterTraceSchedCommand();
99 extern void RegisterAPICommands();
100
101 class CommandRegister {
102 public:
CommandRegister()103 CommandRegister() {
104 RegisterDumpRecordCommand();
105 RegisterHelpCommand();
106 RegisterKmemCommand();
107 RegisterReportCommand();
108 RegisterReportSampleCommand();
109 #if defined(__linux__)
110 RegisterListCommand();
111 RegisterRecordCommand();
112 RegisterStatCommand();
113 RegisterDebugUnwindCommand();
114 RegisterTraceSchedCommand();
115 #if defined(__ANDROID__)
116 RegisterAPICommands();
117 #endif
118 #endif
119 }
120 };
121
122 CommandRegister command_register;
123
StderrLogger(android::base::LogId,android::base::LogSeverity severity,const char *,const char * file,unsigned int line,const char * message)124 static void StderrLogger(android::base::LogId, android::base::LogSeverity severity,
125 const char*, const char* file, unsigned int line, const char* message) {
126 static const char log_characters[] = "VDIWEFF";
127 char severity_char = log_characters[severity];
128 fprintf(stderr, "simpleperf %c %s:%u] %s\n", severity_char, file, line, message);
129 }
130
RunSimpleperfCmd(int argc,char ** argv)131 bool RunSimpleperfCmd(int argc, char** argv) {
132 android::base::InitLogging(argv, StderrLogger);
133 std::vector<std::string> args;
134 android::base::LogSeverity log_severity = android::base::INFO;
135
136 for (int i = 1; i < argc; ++i) {
137 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
138 args.insert(args.begin(), "help");
139 } else if (strcmp(argv[i], "--log") == 0) {
140 if (i + 1 < argc) {
141 ++i;
142 if (!GetLogSeverity(argv[i], &log_severity)) {
143 LOG(ERROR) << "Unknown log severity: " << argv[i];
144 return false;
145 }
146 } else {
147 LOG(ERROR) << "Missing argument for --log option.\n";
148 return false;
149 }
150 #if defined(__ANDROID__)
151 } else if (strcmp(argv[i], "--log-to-android-buffer") == 0) {
152 android::base::SetLogger(android::base::LogdLogger());
153 #endif
154 } else if (strcmp(argv[i], "--version") == 0) {
155 LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion();
156 return true;
157 } else {
158 args.push_back(argv[i]);
159 }
160 }
161 android::base::ScopedLogSeverity severity(log_severity);
162
163 if (args.empty()) {
164 args.push_back("help");
165 }
166 std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
167 if (command == nullptr) {
168 LOG(ERROR) << "malformed command line: unknown command " << args[0];
169 return false;
170 }
171 std::string command_name = args[0];
172 args.erase(args.begin());
173
174 LOG(DEBUG) << "command '" << command_name << "' starts running";
175 bool result = command->Run(args);
176 LOG(DEBUG) << "command '" << command_name << "' "
177 << (result ? "finished successfully" : "failed");
178 // Quick exit to avoid cost freeing memory and closing files.
179 fflush(stdout);
180 fflush(stderr);
181 android::base::quick_exit(result ? 0 : 1);
182 return result;
183 }
184