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