OSDN Git Service

Merge "Simpleperf: adjust default mmap_pages used when recording."
[android-x86/system-extras.git] / simpleperf / main.cpp
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 <string.h>
18 #include <map>
19 #include <string>
20 #include <vector>
21
22 #include <android-base/logging.h>
23
24 #include "command.h"
25
26 static std::map<std::string, android::base::LogSeverity> log_severity_map = {
27     {"debug", android::base::DEBUG},
28     {"warning", android::base::WARNING},
29     {"error", android::base::ERROR},
30     {"fatal", android::base::FATAL},
31 };
32
33 int main(int argc, char** argv) {
34   InitLogging(argv, android::base::StderrLogger);
35   std::vector<std::string> args;
36   android::base::LogSeverity log_severity = android::base::WARNING;
37
38   if (argc == 1) {
39     args.push_back("help");
40   } else {
41     for (int i = 1; i < argc; ++i) {
42       if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
43         args.insert(args.begin(), "help");
44       } else if (strcmp(argv[i], "--log") == 0) {
45         if (i + 1 < argc) {
46           ++i;
47           auto it = log_severity_map.find(argv[i]);
48           if (it != log_severity_map.end()) {
49             log_severity = it->second;
50           } else {
51             LOG(ERROR) << "Unknown log severity: " << argv[i];
52             return 1;
53           }
54         } else {
55           LOG(ERROR) << "Missing argument for --log option.\n";
56           return 1;
57         }
58       } else {
59         args.push_back(argv[i]);
60       }
61     }
62   }
63   android::base::ScopedLogSeverity severity(log_severity);
64
65   std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
66   if (command == nullptr) {
67     LOG(ERROR) << "malformed command line: unknown command " << args[0];
68     return 1;
69   }
70   std::string command_name = args[0];
71   args.erase(args.begin());
72
73   LOG(DEBUG) << "command '" << command_name << "' starts running";
74   bool result = command->Run(args);
75   LOG(DEBUG) << "command '" << command_name << "' "
76              << (result ? "finished successfully" : "failed");
77   return result ? 0 : 1;
78 }