1 /*
2 * Copyright (C) 2018 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 "tools/ftrace_proto_gen/ftrace_proto_gen.h"
18
19 #include <fstream>
20 #include <regex>
21 #include <set>
22 #include <string>
23
24 namespace perfetto {
25
26 namespace {
27
ToCamelCase(const std::string & s)28 std::string ToCamelCase(const std::string& s) {
29 std::string result;
30 result.reserve(s.size());
31 bool upperCaseNextChar = true;
32 for (size_t i = 0; i < s.size(); i++) {
33 char c = s[i];
34 if (c == '_') {
35 upperCaseNextChar = true;
36 continue;
37 }
38 if (upperCaseNextChar) {
39 upperCaseNextChar = false;
40 c = static_cast<char>(toupper(c));
41 }
42 result.push_back(c);
43 }
44 return result;
45 }
46
StartsWith(const std::string & str,const std::string & prefix)47 bool StartsWith(const std::string& str, const std::string& prefix) {
48 return str.compare(0, prefix.length(), prefix) == 0;
49 }
50
Contains(const std::string & haystack,const std::string & needle)51 bool Contains(const std::string& haystack, const std::string& needle) {
52 return haystack.find(needle) != std::string::npos;
53 }
54
55 } // namespace
56
InferProtoType(const FtraceEvent::Field & field)57 std::string InferProtoType(const FtraceEvent::Field& field) {
58 // Fixed length strings: "char foo[16]"
59 if (std::regex_match(field.type_and_name, std::regex(R"(char \w+\[\d+\])")))
60 return "string";
61
62 // String pointers: "__data_loc char[] foo" (as in
63 // 'cpufreq_interactive_boost').
64 if (Contains(field.type_and_name, "char[] "))
65 return "string";
66 if (Contains(field.type_and_name, "char * "))
67 return "string";
68
69 // Variable length strings: "char* foo"
70 if (StartsWith(field.type_and_name, "char *"))
71 return "string";
72
73 // Variable length strings: "char foo" + size: 0 (as in 'print').
74 if (StartsWith(field.type_and_name, "char ") && field.size == 0)
75 return "string";
76
77 // ino_t, i_ino and dev_t are 32bit on some devices 64bit on others. For the
78 // protos we need to choose the largest possible size.
79 if (StartsWith(field.type_and_name, "ino_t ") ||
80 StartsWith(field.type_and_name, "i_ino ") ||
81 StartsWith(field.type_and_name, "dev_t ")) {
82 return "uint64";
83 }
84
85 // Ints of various sizes:
86 if (field.size <= 4 && field.is_signed)
87 return "int32";
88 if (field.size <= 4 && !field.is_signed)
89 return "uint32";
90 if (field.size <= 8 && field.is_signed)
91 return "int64";
92 if (field.size <= 8 && !field.is_signed)
93 return "uint64";
94 return "";
95 }
96
PrintFtraceEventProtoAdditions(const std::set<std::string> & events)97 void PrintFtraceEventProtoAdditions(const std::set<std::string>& events) {
98 printf(
99 "\nNumber appropriately and add output to "
100 "protos/perfetto/trace/ftrace/ftrace_event.proto\n");
101 for (auto event : events) {
102 printf("%sFtraceEvent %s = ;\n", ToCamelCase(event).c_str(), event.c_str());
103 }
104 }
105
PrintEventFormatterMain(const std::set<std::string> & events)106 void PrintEventFormatterMain(const std::set<std::string>& events) {
107 printf(
108 "\nAdd output to FormatEventText in "
109 "tools/ftrace_proto_gen/ftrace_event_formatter.cc\n");
110 for (auto event : events) {
111 printf(
112 "else if (event.has_%s()) {\nconst auto& inner = event.%s();\nline = "
113 "Format%s(inner);\n} ",
114 event.c_str(), event.c_str(), ToCamelCase(event).c_str());
115 }
116 }
117
118 // Add output to ParseInode in ftrace_inode_handler
PrintInodeHandlerMain(const std::string & event_name,const perfetto::Proto & proto)119 void PrintInodeHandlerMain(const std::string& event_name,
120 const perfetto::Proto& proto) {
121 for (const auto& field : proto.fields) {
122 if (Contains(field.name, "ino") && !Contains(field.name, "minor"))
123 printf(
124 "else if (event.has_%s() && event.%s().%s()) {\n*inode = "
125 "static_cast<uint64_t>(event.%s().%s());\n return true;\n} ",
126 event_name.c_str(), event_name.c_str(), field.name.c_str(),
127 event_name.c_str(), field.name.c_str());
128 }
129 }
130
PrintEventFormatterUsingStatements(const std::set<std::string> & events)131 void PrintEventFormatterUsingStatements(const std::set<std::string>& events) {
132 printf("\nAdd output to tools/ftrace_proto_gen/ftrace_event_formatter.cc\n");
133 for (auto event : events) {
134 printf("using protos::%sFtraceEvent;\n", ToCamelCase(event).c_str());
135 }
136 }
137
PrintEventFormatterFunctions(const std::set<std::string> & events)138 void PrintEventFormatterFunctions(const std::set<std::string>& events) {
139 printf(
140 "\nAdd output to tools/ftrace_proto_gen/ftrace_event_formatter.cc and "
141 "then manually go through format files to match fields\n");
142 for (auto event : events) {
143 printf(
144 "std::string Format%s(const %sFtraceEvent& event) {"
145 "\nchar line[2048];"
146 "\nsprintf(line,\"%s: );\nreturn std::string(line);\n}\n",
147 ToCamelCase(event).c_str(), ToCamelCase(event).c_str(), event.c_str());
148 }
149 }
150
GenerateProto(const FtraceEvent & format,Proto * proto_out)151 bool GenerateProto(const FtraceEvent& format, Proto* proto_out) {
152 proto_out->name = ToCamelCase(format.name) + "FtraceEvent";
153 proto_out->fields.reserve(format.fields.size());
154 std::set<std::string> seen;
155 // TODO(hjd): We should be cleverer about id assignment.
156 uint32_t i = 1;
157 for (const FtraceEvent::Field& field : format.fields) {
158 std::string name = GetNameFromTypeAndName(field.type_and_name);
159 // TODO(hjd): Handle dup names.
160 if (name == "" || seen.count(name))
161 continue;
162 seen.insert(name);
163 std::string type = InferProtoType(field);
164 // Check we managed to infer a type.
165 if (type == "")
166 continue;
167 proto_out->fields.emplace_back(Proto::Field{type, name, i});
168 i++;
169 }
170
171 return true;
172 }
173
GetWhitelistedEvents(const std::string & whitelist_path)174 std::set<std::string> GetWhitelistedEvents(const std::string& whitelist_path) {
175 std::string line;
176 std::set<std::string> whitelist;
177
178 std::ifstream fin(whitelist_path, std::ios::in);
179 if (!fin) {
180 fprintf(stderr, "Failed to open whitelist %s\n", whitelist_path.c_str());
181 return whitelist;
182 }
183 while (std::getline(fin, line)) {
184 if (!StartsWith(line, "#")) {
185 whitelist.insert(line);
186 }
187 }
188 return whitelist;
189 }
190
191 // Generates section of event_info.cc for a single event.
SingleEventInfo(perfetto::FtraceEvent format,perfetto::Proto proto,const std::string & group,const std::string & proto_field_id)192 std::string SingleEventInfo(perfetto::FtraceEvent format,
193 perfetto::Proto proto,
194 const std::string& group,
195 const std::string& proto_field_id) {
196 std::string s = "";
197 s += " event->name = \"" + format.name + "\";\n";
198 s += " event->group = \"" + group + "\";\n";
199 s += " event->proto_field_id = " + proto_field_id + ";\n";
200
201 for (const auto& field : proto.fields) {
202 s += " event->fields.push_back(MakeField(\"" + field.name + "\", " +
203 std::to_string(field.number) + ", kProto" + ToCamelCase(field.type) +
204 "));\n";
205 }
206 return s;
207 }
208
209 // This will generate the event_info.cc file for the whitelisted protos.
GenerateEventInfo(const std::vector<std::string> & events_info)210 void GenerateEventInfo(const std::vector<std::string>& events_info) {
211 std::string output_path = "src/ftrace_reader/event_info.cc";
212 std::ofstream fout(output_path.c_str(), std::ios::out);
213 if (!fout) {
214 fprintf(stderr, "Failed to open %s\n", output_path.c_str());
215 return;
216 }
217
218 std::string s = "// Autogenerated by:\n";
219 s += std::string("// ") + __FILE__ + "\n";
220 s += "// Do not edit.\n";
221 s += R"(
222 #include "src/ftrace_reader/event_info.h"
223
224 namespace perfetto {
225
226 std::vector<Event> GetStaticEventInfo() {
227 std::vector<Event> events;
228 )";
229
230 for (const auto& event : events_info) {
231 s += "\n";
232 s += " {\n";
233 s += " events.emplace_back(Event{});\n";
234 s += " Event* event = &events.back();\n";
235 s += event;
236 s += " }\n";
237 }
238
239 s += R"(
240 return events;
241 }
242
243 } // namespace perfetto
244 )";
245
246 fout << s;
247 fout.close();
248 }
249
ToString()250 std::string Proto::ToString() {
251 std::string s = "// Autogenerated by:\n";
252 s += std::string("// ") + __FILE__ + "\n";
253 s += "// Do not edit.\n";
254
255 s += R"(
256 syntax = "proto2";
257 option optimize_for = LITE_RUNTIME;
258 package perfetto.protos;
259
260 )";
261
262 s += "message " + name + " {\n";
263 for (const Proto::Field& field : fields) {
264 s += " optional " + field.type + " " + field.name + " = " +
265 std::to_string(field.number) + ";\n";
266 }
267 s += "}\n";
268 return s;
269 }
270
271 } // namespace perfetto
272