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/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::kBuffer;
42 clang_fmt.args.stderr_mode = base::Subprocess::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()144 ProtoType ProtoType::String() {
145 return {STRING, 0, false};
146 }
147
148 // static
Invalid()149 ProtoType ProtoType::Invalid() {
150 return {INVALID, 0, false};
151 }
152
153 // static
Numeric(uint16_t size,bool is_signed)154 ProtoType ProtoType::Numeric(uint16_t size, bool is_signed) {
155 PERFETTO_CHECK(size == 32 || size == 64);
156 return {NUMERIC, size, is_signed};
157 }
158
159 // static
FromDescriptor(google::protobuf::FieldDescriptor::Type type)160 ProtoType ProtoType::FromDescriptor(
161 google::protobuf::FieldDescriptor::Type type) {
162 if (type == google::protobuf::FieldDescriptor::Type::TYPE_UINT64)
163 return Numeric(64, false);
164
165 if (type == google::protobuf::FieldDescriptor::Type::TYPE_INT64)
166 return Numeric(64, true);
167
168 if (type == google::protobuf::FieldDescriptor::Type::TYPE_UINT32)
169 return Numeric(32, false);
170
171 if (type == google::protobuf::FieldDescriptor::Type::TYPE_INT32)
172 return Numeric(32, true);
173
174 if (type == google::protobuf::FieldDescriptor::Type::TYPE_STRING)
175 return String();
176
177 return Invalid();
178 }
179
GetCommon(ProtoType one,ProtoType other)180 ProtoType GetCommon(ProtoType one, ProtoType other) {
181 // Always need to prefer the LHS as it is the one already present
182 // in the proto.
183 if (one.type == ProtoType::STRING)
184 return ProtoType::String();
185
186 if (one.is_signed || other.is_signed) {
187 one = one.GetSigned();
188 other = other.GetSigned();
189 }
190
191 return ProtoType::Numeric(std::max(one.size, other.size), one.is_signed);
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 // Ints of various sizes:
223 if (field.size <= 4)
224 return ProtoType::Numeric(32, field.is_signed);
225 if (field.size <= 8)
226 return ProtoType::Numeric(64, field.is_signed);
227 return ProtoType::Invalid();
228 }
229
Proto(std::string evt_name,const google::protobuf::Descriptor & desc)230 Proto::Proto(std::string evt_name, const google::protobuf::Descriptor& desc)
231 : name(desc.name()), event_name(evt_name) {
232 for (int i = 0; i < desc.field_count(); ++i) {
233 const google::protobuf::FieldDescriptor* field = desc.field(i);
234 PERFETTO_CHECK(field);
235 AddField(Field{ProtoType::FromDescriptor(field->type()), field->name(),
236 uint32_t(field->number())});
237 }
238 }
239
SortedFields()240 std::vector<const Proto::Field*> Proto::SortedFields() {
241 std::vector<const Proto::Field*> sorted_fields;
242
243 for (const auto& p : fields) {
244 sorted_fields.emplace_back(&p.second);
245 }
246 std::sort(sorted_fields.begin(), sorted_fields.end(),
247 [](const Proto::Field* a, const Proto::Field* b) {
248 return a->number < b->number;
249 });
250 return sorted_fields;
251 }
252
ToString()253 std::string Proto::ToString() {
254 std::string s;
255 s += "message " + name + " {\n";
256 for (const auto field : SortedFields()) {
257 s += " optional " + field->type.ToString() + " " + field->name + " = " +
258 std::to_string(field->number) + ";\n";
259 }
260 s += "}\n";
261 return s;
262 }
263
MergeFrom(const Proto & other)264 void Proto::MergeFrom(const Proto& other) {
265 // Always keep number from the left hand side.
266 PERFETTO_CHECK(name == other.name);
267 for (const auto& p : other.fields) {
268 auto it = fields.find(p.first);
269 if (it == fields.end()) {
270 Proto::Field field = p.second;
271 field.number = ++max_id;
272 AddField(std::move(field));
273 } else {
274 it->second.type = GetCommon(it->second.type, p.second.type);
275 }
276 }
277 }
278
AddField(Proto::Field other)279 void Proto::AddField(Proto::Field other) {
280 max_id = std::max(max_id, other.number);
281 fields.emplace(other.name, std::move(other));
282 }
283
284 } // namespace perfetto
285