2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <android-base/logging.h>
25 #include <android-base/quick_exit.h>
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_
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_ << "`";
44 typedef std::function<std::unique_ptr<Command>(void)> callback_t;
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;
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));
58 void UnRegisterCommand(const std::string& cmd_name) {
59 CommandMap().erase(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)();
67 const std::vector<std::string> GetAllCommandNames() {
68 std::vector<std::string> names;
69 for (auto pair : CommandMap()) {
70 names.push_back(pair.first);
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();
85 class CommandRegister {
88 RegisterDumpRecordCommand();
89 RegisterHelpCommand();
90 RegisterKmemCommand();
91 RegisterReportCommand();
92 RegisterReportSampleCommand();
93 #if defined(__linux__)
94 RegisterListCommand();
95 RegisterRecordCommand();
96 RegisterStatCommand();
97 RegisterDebugUnwindCommand();
102 CommandRegister command_register;
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);
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;
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) {
122 if (!GetLogSeverity(argv[i], &log_severity)) {
123 LOG(ERROR) << "Unknown log severity: " << argv[i];
127 LOG(ERROR) << "Missing argument for --log option.\n";
130 } else if (strcmp(argv[i], "--version") == 0) {
131 LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion();
134 args.push_back(argv[i]);
137 android::base::ScopedLogSeverity severity(log_severity);
140 args.push_back("help");
142 std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
143 if (command == nullptr) {
144 LOG(ERROR) << "malformed command line: unknown command " << args[0];
147 std::string command_name = args[0];
148 args.erase(args.begin());
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.
157 android::base::quick_exit(result ? 0 : 1);