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