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 "src/tools/ftrace_proto_gen/proto_gen_utils.h"
18
19 #include <algorithm>
20 #include <fstream>
21 #include <regex>
22
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/file_utils.h"
25 #include "perfetto/ext/base/pipe.h"
26 #include "perfetto/ext/base/string_splitter.h"
27 #include "perfetto/ext/base/string_utils.h"
28 #include "perfetto/ext/base/subprocess.h"
29
30 namespace perfetto {
31
32 namespace {
33
RunClangFmt(const std::string & input)34 std::string RunClangFmt(const std::string& input) {
35 #if PERFETTO_BUILDFLAG(PERFETTO_OS_MAC)
36 const std::string platform = "mac";
37 #else
38 const std::string platform = "linux64";
39 #endif
40 base::Subprocess clang_fmt({"buildtools/" + platform + "/clang-format"});
41 clang_fmt.args.stdout_mode = base::Subprocess::OutputMode::kBuffer;
42 clang_fmt.args.stderr_mode = base::Subprocess::OutputMode::kInherit;
43 clang_fmt.args.input = input;
44 PERFETTO_CHECK(clang_fmt.Call());
45 return std::move(clang_fmt.output());
46 }
47
48 } // namespace
49
50 using base::Contains;
51 using base::EndsWith;
52 using base::StartsWith;
53 using base::Uppercase;
54
VerifyStream(std::string filename)55 VerifyStream::VerifyStream(std::string filename)
56 : filename_(std::move(filename)) {
57 PERFETTO_CHECK(base::ReadFile(filename_, &expected_));
58 }
59
~VerifyStream()60 VerifyStream::~VerifyStream() {
61 std::string tidied = str();
62 if (EndsWith(filename_, "cc") || EndsWith(filename_, "proto"))
63 tidied = RunClangFmt(str());
64 if (expected_ != tidied) {
65 PERFETTO_FATAL("%s is out of date. Please run tools/run_ftrace_proto_gen.",
66 filename_.c_str());
67 }
68 }
69
FtraceEventName(const std::string & full_name)70 FtraceEventName::FtraceEventName(const std::string& full_name) {
71 if (full_name.rfind("removed", 0) != std::string::npos) {
72 valid_ = false;
73 return;
74 }
75 name_ = full_name.substr(full_name.find('/') + 1, std::string::npos);
76 group_ = full_name.substr(0, full_name.find('/'));
77 valid_ = true;
78 }
79
valid() const80 bool FtraceEventName::valid() const {
81 return valid_;
82 }
83
name() const84 const std::string& FtraceEventName::name() const {
85 PERFETTO_CHECK(valid_);
86 return name_;
87 }
88
group() const89 const std::string& FtraceEventName::group() const {
90 PERFETTO_CHECK(valid_);
91 return group_;
92 }
93
ToCamelCase(const std::string & s)94 std::string ToCamelCase(const std::string& s) {
95 std::string result;
96 result.reserve(s.size());
97 bool upperCaseNextChar = true;
98 for (size_t i = 0; i < s.size(); i++) {
99 char c = s[i];
100 if (c == '_') {
101 upperCaseNextChar = true;
102 continue;
103 }
104 if (upperCaseNextChar) {
105 upperCaseNextChar = false;
106 c = Uppercase(c);
107 }
108 result.push_back(c);
109 }
110 return result;
111 }
112
GetSigned() const113 ProtoType ProtoType::GetSigned() const {
114 PERFETTO_CHECK(type == NUMERIC);
115 if (is_signed)
116 return *this;
117
118 if (size == 64) {
119 return Numeric(64, true);
120 }
121
122 return Numeric(2 * size, true);
123 }
124
ToString() const125 std::string ProtoType::ToString() const {
126 switch (type) {
127 case INVALID:
128 PERFETTO_CHECK(false);
129 case STRING:
130 return "string";
131 case NUMERIC: {
132 std::string s;
133 if (!is_signed)
134 s += "u";
135 s += "int";
136 s += std::to_string(size);
137 return s;
138 }
139 }
140 PERFETTO_CHECK(false); // for GCC.
141 }
142
143 // static
String(bool is_repeated)144 ProtoType ProtoType::String(bool is_repeated) {
145 return {STRING, 0, false, is_repeated};
146 }
147
148 // static
Invalid()149 ProtoType ProtoType::Invalid() {
150 return {INVALID, 0, false, false};
151 }
152
153 // static
Numeric(uint16_t size,bool is_signed,bool is_repeated)154 ProtoType ProtoType::Numeric(uint16_t size, bool is_signed, bool is_repeated) {
155 PERFETTO_CHECK(size == 32 || size == 64);
156 return {NUMERIC, size, is_signed, is_repeated};
157 }
158
159 // static
FromDescriptor(google::protobuf::FieldDescriptor::Type type,bool is_repeated)160 ProtoType ProtoType::FromDescriptor(
161 google::protobuf::FieldDescriptor::Type type,
162 bool is_repeated) {
163 if (type == google::protobuf::FieldDescriptor::Type::TYPE_UINT64)
164 return Numeric(64, false, is_repeated);
165
166 if (type == google::protobuf::FieldDescriptor::Type::TYPE_INT64)
167 return Numeric(64, true, is_repeated);
168
169 if (type == google::protobuf::FieldDescriptor::Type::TYPE_UINT32)
170 return Numeric(32, false, is_repeated);
171
172 if (type == google::protobuf::FieldDescriptor::Type::TYPE_INT32)
173 return Numeric(32, true, is_repeated);
174
175 if (type == google::protobuf::FieldDescriptor::Type::TYPE_STRING)
176 return String(is_repeated);
177
178 return Invalid();
179 }
180
GetCommon(ProtoType one,ProtoType other)181 ProtoType GetCommon(ProtoType one, ProtoType other) {
182 // Always need to prefer the LHS as it is the one already present
183 // in the proto.
184 if (one.type == ProtoType::STRING)
185 return ProtoType::String(one.is_repeated);
186
187 if (one.is_signed || other.is_signed) {
188 one = one.GetSigned();
189 other = other.GetSigned();
190 }
191
192 return ProtoType::Numeric(std::max(one.size, other.size), one.is_signed,
193 one.is_repeated || other.is_repeated);
194 }
195
InferProtoType(const FtraceEvent::Field & field)196 ProtoType InferProtoType(const FtraceEvent::Field& field) {
197 // Fixed length strings: "char foo[16]"
198 if (std::regex_match(field.type_and_name, std::regex(R"(char \w+\[\d+\])")))
199 return ProtoType::String();
200
201 // String pointers: "__data_loc char[] foo" (as in
202 // 'cpufreq_interactive_boost').
203 if (Contains(field.type_and_name, "char[] "))
204 return ProtoType::String();
205 if (Contains(field.type_and_name, "char * "))
206 return ProtoType::String();
207
208 // Variable length strings: "char* foo"
209 if (StartsWith(field.type_and_name, "char *"))
210 return ProtoType::String();
211
212 // Variable length strings: "char foo" + size: 0 (as in 'print').
213 if (StartsWith(field.type_and_name, "char ") && field.size == 0)
214 return ProtoType::String();
215
216 // ino_t, i_ino and dev_t are 32bit on some devices 64bit on others. For the
217 // protos we need to choose the largest possible size.
218 if (StartsWith(field.type_and_name, "ino_t ") ||
219 StartsWith(field.type_and_name, "i_ino ") ||
220 StartsWith(field.type_and_name, "dev_t ")) {
221 return ProtoType::Numeric(64, /* is_signed= */ false);
222 }
223
224 // Bools should always uint32 even if they are signed.
225 if (StartsWith(field.type_and_name, "bool ")) {
226 return ProtoType::Numeric(32, /* is_signed= */ false);
227 }
228
229 // Fixed size array for syscall args. Similar to ino_t choose the largest
230 // possible size to cover 32bit and 64bit.
231 if (StartsWith(field.type_and_name, "unsigned long args[6]")) {
232 return ProtoType::Numeric(64, /* is_signed= */ false,
233 /* is_repeated= */ true);
234 }
235
236 // Ints of various sizes:
237 if (field.size <= 4)
238 return ProtoType::Numeric(32, field.is_signed);
239 if (field.size <= 8)
240 return ProtoType::Numeric(64, field.is_signed);
241 return ProtoType::Invalid();
242 }
243
Proto(std::string evt_name,const google::protobuf::Descriptor & desc)244 Proto::Proto(std::string evt_name, const google::protobuf::Descriptor& desc)
245 : name(desc.name()), event_name(evt_name) {
246 for (int i = 0; i < desc.field_count(); ++i) {
247 const google::protobuf::FieldDescriptor* field = desc.field(i);
248 PERFETTO_CHECK(field);
249 AddField(Field{ProtoType::FromDescriptor(field->type()), field->name(),
250 uint32_t(field->number())});
251 }
252 }
253
SortedFields()254 std::vector<const Proto::Field*> Proto::SortedFields() {
255 std::vector<const Proto::Field*> sorted_fields;
256
257 for (const auto& p : fields) {
258 sorted_fields.emplace_back(&p.second);
259 }
260 std::sort(sorted_fields.begin(), sorted_fields.end(),
261 [](const Proto::Field* a, const Proto::Field* b) {
262 return a->number < b->number;
263 });
264 return sorted_fields;
265 }
266
ToString()267 std::string Proto::ToString() {
268 std::string s;
269 s += "message " + name + " {\n";
270 for (const auto field : SortedFields()) {
271 if (field->type.is_repeated)
272 s += " repeated ";
273 else
274 s += " optional ";
275 s += field->type.ToString() + " " + field->name + " = " +
276 std::to_string(field->number) + ";\n";
277 }
278 s += "}\n";
279 return s;
280 }
281
UnionFields(const std::vector<Proto::Field> & candidate_fields)282 void Proto::UnionFields(const std::vector<Proto::Field>& candidate_fields) {
283 for (const auto& candidate : candidate_fields) {
284 auto it = fields.find(candidate.name);
285 if (it != fields.end()) {
286 // potentially expand proto type to cover both cases
287 it->second.type = GetCommon(it->second.type, candidate.type);
288 continue;
289 }
290 Proto::Field new_field = candidate;
291 new_field.number = ++max_id;
292 AddField(std::move(new_field));
293 }
294 }
295
AddField(Proto::Field other)296 void Proto::AddField(Proto::Field other) {
297 max_id = std::max(max_id, other.number);
298 fields.emplace(other.name, std::move(other));
299 }
300
301 } // namespace perfetto
302