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 return Invalid();
174 }
175
GetCommon(ProtoType one,ProtoType other)176 ProtoType GetCommon(ProtoType one, ProtoType other) {
177 // Always need to prefer the LHS as it is the one already present
178 // in the proto.
179 if (one.type == ProtoType::STRING)
180 return ProtoType::String(one.is_repeated);
181
182 if (one.is_signed || other.is_signed) {
183 one = one.GetSigned();
184 other = other.GetSigned();
185 }
186
187 return ProtoType::Numeric(std::max(one.size, other.size), one.is_signed,
188 one.is_repeated || other.is_repeated);
189 }
190
InferProtoType(const FtraceEvent::Field & field)191 ProtoType InferProtoType(const FtraceEvent::Field& field) {
192 // Fixed length strings: "char foo[16]"
193 if (std::regex_match(field.type_and_name, std::regex(R"(char \w+\[\d+\])")))
194 return ProtoType::String();
195
196 // String pointers: "__data_loc char[] foo" (as in
197 // 'cpufreq_interactive_boost').
198 if (Contains(field.type_and_name, "char[] "))
199 return ProtoType::String();
200 if (Contains(field.type_and_name, "char * "))
201 return ProtoType::String();
202
203 // Variable length strings: "char* foo"
204 if (StartsWith(field.type_and_name, "char *"))
205 return ProtoType::String();
206
207 // Variable length strings: "char foo" + size: 0 (as in 'print').
208 if (StartsWith(field.type_and_name, "char ") && field.size == 0)
209 return ProtoType::String();
210
211 // ino_t, i_ino and dev_t are 32bit on some devices 64bit on others. For the
212 // protos we need to choose the largest possible size.
213 if (StartsWith(field.type_and_name, "ino_t ") ||
214 StartsWith(field.type_and_name, "i_ino ") ||
215 StartsWith(field.type_and_name, "dev_t ")) {
216 return ProtoType::Numeric(64, /* is_signed= */ false);
217 }
218
219 // Bools should always uint32 even if they are signed.
220 if (StartsWith(field.type_and_name, "bool ")) {
221 return ProtoType::Numeric(32, /* is_signed= */ false);
222 }
223
224 // Fixed size array for syscall args. Similar to ino_t choose the largest
225 // possible size to cover 32bit and 64bit.
226 if (StartsWith(field.type_and_name, "unsigned long args[6]")) {
227 return ProtoType::Numeric(64, /* is_signed= */ false,
228 /* is_repeated= */ true);
229 }
230
231 // Ints of various sizes:
232 if (field.size <= 4)
233 return ProtoType::Numeric(32, field.is_signed);
234 if (field.size <= 8)
235 return ProtoType::Numeric(64, field.is_signed);
236 return ProtoType::Invalid();
237 }
238
Proto(std::string evt_name,const google::protobuf::Descriptor & desc)239 Proto::Proto(std::string evt_name, const google::protobuf::Descriptor& desc)
240 : name(desc.name()), event_name(evt_name) {
241 for (int i = 0; i < desc.field_count(); ++i) {
242 const google::protobuf::FieldDescriptor* field = desc.field(i);
243 PERFETTO_CHECK(field);
244 AddField(Field{ProtoType::FromDescriptor(field->type()), field->name(),
245 uint32_t(field->number())});
246 }
247 }
248
SortedFields()249 std::vector<const Proto::Field*> Proto::SortedFields() {
250 std::vector<const Proto::Field*> sorted_fields;
251
252 for (const auto& p : fields) {
253 sorted_fields.emplace_back(&p.second);
254 }
255 std::sort(sorted_fields.begin(), sorted_fields.end(),
256 [](const Proto::Field* a, const Proto::Field* b) {
257 return a->number < b->number;
258 });
259 return sorted_fields;
260 }
261
ToString()262 std::string Proto::ToString() {
263 std::string s;
264 s += "message " + name + " {\n";
265 for (const auto field : SortedFields()) {
266 if (field->type.is_repeated)
267 s += " repeated ";
268 else
269 s += " optional ";
270 s += field->type.ToString() + " " + field->name + " = " +
271 std::to_string(field->number) + ";\n";
272 }
273 s += "}\n";
274 return s;
275 }
276
UnionFields(const std::vector<Proto::Field> & candidate_fields)277 void Proto::UnionFields(const std::vector<Proto::Field>& candidate_fields) {
278 for (const auto& candidate : candidate_fields) {
279 auto it = fields.find(candidate.name);
280 if (it != fields.end()) {
281 // potentially expand proto type to cover both cases
282 it->second.type = GetCommon(it->second.type, candidate.type);
283 continue;
284 }
285 Proto::Field new_field = candidate;
286 new_field.number = ++max_id;
287 AddField(std::move(new_field));
288 }
289 }
290
AddField(Proto::Field other)291 void Proto::AddField(Proto::Field other) {
292 max_id = std::max(max_id, other.number);
293 fields.emplace(other.name, std::move(other));
294 }
295
296 } // namespace perfetto
297