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_descriptor_gen.h"
18
19 #include <google/protobuf/descriptor.h>
20 #include <google/protobuf/descriptor.pb.h>
21
22 namespace perfetto {
23
GenerateFtraceDescriptors(const google::protobuf::DescriptorPool & descriptor_pool,std::ostream * fout)24 void GenerateFtraceDescriptors(
25 const google::protobuf::DescriptorPool& descriptor_pool,
26 std::ostream* fout) {
27 const google::protobuf::Descriptor* ftrace_event =
28 descriptor_pool.FindMessageTypeByName("perfetto.protos.FtraceEvent");
29 const google::protobuf::OneofDescriptor* one_of_event =
30 ftrace_event->FindOneofByName("event");
31
32 // Find max id for any ftrace event.
33 int max_id = 0;
34 for (int i = 0; i < one_of_event->field_count(); i++)
35 max_id = std::max(max_id, one_of_event->field(i)->number());
36
37 *fout << R"(/*
38 * Copyright (C) 2017 The Android Open Source Project
39 *
40 * Licensed under the Apache License, Version 2.0 (the "License");
41 * you may not use this file except in compliance with the License.
42 * You may obtain a copy of the License at
43 *
44 * http://www.apache.org/licenses/LICENSE-2.0
45 *
46 * Unless required by applicable law or agreed to in writing, software
47 * distributed under the License is distributed on an "AS IS" BASIS,
48 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49 * See the License for the specific language governing permissions and
50 * limitations under the License.
51 */
52
53 )";
54 *fout << "// Autogenerated by:\n";
55 *fout << std::string("// ") + __FILE__ + "\n";
56 *fout << "// Do not edit.\n";
57 *fout << R"(
58 #include "src/trace_processor/importers/ftrace/ftrace_descriptors.h"
59
60 namespace perfetto {
61 namespace trace_processor {
62 namespace {
63
64 std::array<MessageDescriptor,
65 )";
66 *fout << std::to_string(max_id + 1) + "> descriptors{{";
67
68 for (int i = 0; i <= max_id; i++) {
69 const google::protobuf::FieldDescriptor* event =
70 ftrace_event->FindFieldByNumber(i);
71 // Skip events that don't exist or are not messages. (Proxy for events)
72 if (!event ||
73 event->type() != google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
74 *fout << "{nullptr, 0, {}},";
75 continue;
76 }
77
78 const auto* event_descriptor = event->message_type();
79
80 // Find the max field id in the event.
81 int max_field_id = 0;
82 for (int j = 0; j < event_descriptor->field_count(); j++)
83 max_field_id =
84 std::max(max_field_id, event_descriptor->field(j)->number());
85
86 *fout << "{\"" + event->name() + "\", " << max_field_id << ", "
87 << "{";
88
89 for (int j = 0; j <= max_field_id; j++) {
90 const auto* field = event_descriptor->FindFieldByNumber(j);
91 // Skip fields that don't exist or are nested messages.
92 if (!field ||
93 field->type() == google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
94 *fout << "{},";
95 continue;
96 }
97 ProtoType type = ProtoType::FromDescriptor(field->type());
98 *fout << "{\"" + field->name() + "\", ProtoSchemaType::k" +
99 ToCamelCase(type.ToString()) + "},";
100 }
101 *fout << "},\n},";
102 }
103 *fout << "}};\n";
104 *fout << R"(
105 } // namespace
106
107 MessageDescriptor* GetMessageDescriptorForId(size_t id) {
108 PERFETTO_CHECK(id < descriptors.size());
109 return &descriptors[id];
110 }
111
112 MessageDescriptor* GetMessageDescriptorForName(base::StringView name) {
113 for (MessageDescriptor& descriptor : descriptors) {
114 if (descriptor.name != nullptr && descriptor.name == name)
115 return &descriptor;
116 }
117 return nullptr;
118 }
119
120 size_t GetDescriptorsSize() {
121 return descriptors.size();
122 }
123 )";
124 *fout << "} // namespace trace_processor\n} // namespace perfetto\n";
125 }
126
127 } // namespace perfetto
128