blob: f5d4e8bd02cc2f36bce97ced407a707199d275f5 [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#include "command.h"
18
19#include <algorithm>
Yabin Cuif79f07e2015-06-01 11:21:37 -070020#include <map>
Yabin Cui67d3abd2015-04-16 15:26:31 -070021#include <string>
22#include <vector>
23
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cuif79f07e2015-06-01 11:21:37 -070025
26bool Command::NextArgumentOrError(const std::vector<std::string>& args, size_t* pi) {
27 if (*pi + 1 == args.size()) {
28 LOG(ERROR) << "No argument following " << args[*pi] << " option. Try `simpleperf help " << name_
29 << "`";
30 return false;
31 }
32 ++*pi;
33 return true;
34}
35
36void Command::ReportUnknownOption(const std::vector<std::string>& args, size_t i) {
37 LOG(ERROR) << "Unknown option for " << name_ << " command: '" << args[i]
38 << "'. Try `simpleperf help " << name_ << "`";
39}
40
41typedef std::function<std::unique_ptr<Command>(void)> callback_t;
42
43static std::map<std::string, callback_t>& CommandMap() {
Yabin Cui67d3abd2015-04-16 15:26:31 -070044 // commands is used in the constructor of Command. Defining it as a static
45 // variable in a function makes sure it is initialized before use.
Yabin Cuif79f07e2015-06-01 11:21:37 -070046 static std::map<std::string, callback_t> command_map;
47 return command_map;
Yabin Cui67d3abd2015-04-16 15:26:31 -070048}
49
Yabin Cuif79f07e2015-06-01 11:21:37 -070050void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -070051 const std::function<std::unique_ptr<Command>(void)>& callback) {
Yabin Cuif79f07e2015-06-01 11:21:37 -070052 CommandMap().insert(std::make_pair(cmd_name, callback));
53}
54
55void UnRegisterCommand(const std::string& cmd_name) {
56 CommandMap().erase(cmd_name);
57}
58
59std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name) {
60 auto it = CommandMap().find(cmd_name);
61 return (it == CommandMap().end()) ? nullptr : (it->second)();
62}
63
64const std::vector<std::string> GetAllCommandNames() {
65 std::vector<std::string> names;
66 for (auto pair : CommandMap()) {
67 names.push_back(pair.first);
Yabin Cui67d3abd2015-04-16 15:26:31 -070068 }
Yabin Cuif79f07e2015-06-01 11:21:37 -070069 return names;
Yabin Cui323e9452015-04-20 18:07:17 -070070}
Yabin Cuiff7465c2016-02-25 11:02:30 -080071
72extern void RegisterDumpRecordCommand();
73extern void RegisterHelpCommand();
74extern void RegisterListCommand();
Yabin Cui6965d422016-06-15 11:41:42 -070075extern void RegisterKmemCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -080076extern void RegisterRecordCommand();
77extern void RegisterReportCommand();
Yabin Cui767dd172016-06-02 21:02:43 -070078extern void RegisterReportSampleCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -080079extern void RegisterStatCommand();
80
81class CommandRegister {
82 public:
83 CommandRegister() {
84 RegisterDumpRecordCommand();
85 RegisterHelpCommand();
Yabin Cui6965d422016-06-15 11:41:42 -070086 RegisterKmemCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -080087 RegisterReportCommand();
Yabin Cui767dd172016-06-02 21:02:43 -070088 RegisterReportSampleCommand();
Yabin Cuiff7465c2016-02-25 11:02:30 -080089#if defined(__linux__)
90 RegisterListCommand();
91 RegisterRecordCommand();
92 RegisterStatCommand();
93#endif
94 }
95};
96
97CommandRegister command_register;