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