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 "event_type.h"
18 
19 #include <unistd.h>
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 
27 #include "event_attr.h"
28 #include "utils.h"
29 
30 #define EVENT_TYPE_TABLE_ENTRY(name, type, config) {name, type, config},
31 
32 static const std::vector<EventType> static_event_type_array = {
33 #include "event_type_table.h"
34 };
35 
GetTracepointEventTypes()36 static const std::vector<EventType> GetTracepointEventTypes() {
37   std::vector<EventType> result;
38   if (!IsRoot()) {
39     // Not having permission to profile tracing events.
40     return result;
41   }
42   const std::string tracepoint_dirname = "/sys/kernel/debug/tracing/events";
43   for (const auto& system_name : GetSubDirs(tracepoint_dirname)) {
44     std::string system_path = tracepoint_dirname + "/" + system_name;
45     for (const auto& event_name : GetSubDirs(system_path)) {
46       std::string id_path = system_path + "/" + event_name + "/id";
47       std::string id_content;
48       if (!android::base::ReadFileToString(id_path, &id_content)) {
49         continue;
50       }
51       char* endptr;
52       uint64_t id = strtoull(id_content.c_str(), &endptr, 10);
53       if (endptr == id_content.c_str()) {
54         LOG(DEBUG) << "unexpected id '" << id_content << "' in " << id_path;
55         continue;
56       }
57       result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id));
58     }
59   }
60   std::sort(result.begin(), result.end(),
61             [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
62   return result;
63 }
64 
GetAllEventTypes()65 const std::vector<EventType>& GetAllEventTypes() {
66   static std::vector<EventType> event_type_array;
67   if (event_type_array.empty()) {
68     event_type_array.insert(event_type_array.end(), static_event_type_array.begin(),
69                             static_event_type_array.end());
70     const std::vector<EventType> tracepoint_array = GetTracepointEventTypes();
71     event_type_array.insert(event_type_array.end(), tracepoint_array.begin(),
72                             tracepoint_array.end());
73   }
74   return event_type_array;
75 }
76 
FindEventTypeByName(const std::string & name)77 const EventType* FindEventTypeByName(const std::string& name) {
78   const EventType* result = nullptr;
79   for (auto& event_type : GetAllEventTypes()) {
80     if (event_type.name == name) {
81       result = &event_type;
82       break;
83     }
84   }
85   if (result == nullptr) {
86     LOG(ERROR) << "Unknown event_type '" << name
87                << "', try `simpleperf list` to list all possible event type names";
88     return nullptr;
89   }
90   return result;
91 }
92 
ParseEventType(const std::string & event_type_str)93 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str) {
94   static std::string modifier_characters = "ukhGHp";
95   std::unique_ptr<EventTypeAndModifier> event_type_modifier(new EventTypeAndModifier);
96   event_type_modifier->name = event_type_str;
97   std::string event_type_name = event_type_str;
98   std::string modifier;
99   size_t comm_pos = event_type_str.rfind(':');
100   if (comm_pos != std::string::npos) {
101     bool match_modifier = true;
102     for (size_t i = comm_pos + 1; i < event_type_str.size(); ++i) {
103       char c = event_type_str[i];
104       if (c != ' ' && modifier_characters.find(c) == std::string::npos) {
105         match_modifier = false;
106         break;
107       }
108     }
109     if (match_modifier) {
110       event_type_name = event_type_str.substr(0, comm_pos);
111       modifier = event_type_str.substr(comm_pos + 1);
112     }
113   }
114   const EventType* event_type = FindEventTypeByName(event_type_name);
115   if (event_type == nullptr) {
116     // Try if the modifier belongs to the event type name, like some tracepoint events.
117     if (!modifier.empty()) {
118       event_type_name = event_type_str;
119       modifier.clear();
120       event_type = FindEventTypeByName(event_type_name);
121     }
122     if (event_type == nullptr) {
123       return nullptr;
124     }
125   }
126   event_type_modifier->event_type = *event_type;
127   if (modifier.find_first_of("ukh") != std::string::npos) {
128     event_type_modifier->exclude_user = true;
129     event_type_modifier->exclude_kernel = true;
130     event_type_modifier->exclude_hv = true;
131   }
132   if (modifier.find_first_of("GH") != std::string::npos) {
133     event_type_modifier->exclude_guest = true;
134     event_type_modifier->exclude_host = true;
135   }
136 
137   for (auto& c : modifier) {
138     switch (c) {
139       case 'u':
140         event_type_modifier->exclude_user = false;
141         break;
142       case 'k':
143         event_type_modifier->exclude_kernel = false;
144         break;
145       case 'h':
146         event_type_modifier->exclude_hv = false;
147         break;
148       case 'G':
149         event_type_modifier->exclude_guest = false;
150         break;
151       case 'H':
152         event_type_modifier->exclude_host = false;
153         break;
154       case 'p':
155         event_type_modifier->precise_ip++;
156         break;
157       case ' ':
158         break;
159       default:
160         LOG(ERROR) << "Unknown event type modifier '" << c << "'";
161     }
162   }
163   event_type_modifier->modifier = modifier;
164   return event_type_modifier;
165 }
166