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 <stdio.h>
18 #include <string>
19 #include <vector>
20 
21 #include <base/logging.h>
22 
23 #include "command.h"
24 #include "event_type.h"
25 #include "perf_event.h"
26 
PrintEventTypesOfType(uint32_t type,const char * type_name,const std::vector<const EventType> & event_types)27 static void PrintEventTypesOfType(uint32_t type, const char* type_name,
28                                   const std::vector<const EventType>& event_types) {
29   printf("List of %s:\n", type_name);
30   for (auto& event_type : event_types) {
31     if (event_type.type == type && event_type.IsSupportedByKernel()) {
32       printf("  %s\n", event_type.name);
33     }
34   }
35   printf("\n");
36 }
37 
38 class ListCommand : public Command {
39  public:
ListCommand()40   ListCommand()
41       : Command("list", "list all available perf events",
42                 "Usage: simpleperf list\n"
43                 "    List all available perf events on this machine.\n") {
44   }
45 
46   bool Run(const std::vector<std::string>& args) override;
47 };
48 
Run(const std::vector<std::string> & args)49 bool ListCommand::Run(const std::vector<std::string>& args) {
50   if (args.size() != 1) {
51     LOG(ERROR) << "malformed command line: list subcommand needs no argument";
52     LOG(ERROR) << "try using \"help list\"";
53     return false;
54   }
55   auto& event_types = EventTypeFactory::GetAllEventTypes();
56 
57   PrintEventTypesOfType(PERF_TYPE_HARDWARE, "hardware events", event_types);
58   PrintEventTypesOfType(PERF_TYPE_SOFTWARE, "software events", event_types);
59   PrintEventTypesOfType(PERF_TYPE_HW_CACHE, "hw-cache events", event_types);
60   return true;
61 }
62 
63 ListCommand list_command;
64