xref: /aosp_15_r20/external/perfetto/src/tools/ftrace_proto_gen/proto_gen_utils.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_
18 #define SRC_TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_
19 
20 #include <map>
21 #include <set>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 
26 #include <google/protobuf/descriptor.h>
27 
28 #include "src/traced/probes/ftrace/format_parser/format_parser.h"
29 
30 namespace perfetto {
31 
32 class VerifyStream : public std::ostringstream {
33  public:
34   VerifyStream(std::string filename);
35   ~VerifyStream() override;
36 
37  private:
38   std::string filename_;
39   std::string expected_;
40 };
41 
42 class FtraceEventName {
43  public:
44   explicit FtraceEventName(const std::string& full_name);
45 
46   bool valid() const;
47   const std::string& name() const;
48   const std::string& group() const;
49 
50  private:
51   bool valid_;
52   std::string name_;
53   std::string group_;
54 };
55 
56 struct ProtoType {
57   enum Type { INVALID, NUMERIC, STRING };
58   Type type;
59   uint16_t size;
60   bool is_signed;
61   bool is_repeated;
62 
63   ProtoType GetSigned() const;
64   std::string ToString() const;
65 
66   static ProtoType Invalid();
67   static ProtoType String(bool is_repeated = false);
68   static ProtoType Numeric(uint16_t size,
69                            bool is_signed,
70                            bool is_repeated = false);
71   static ProtoType FromDescriptor(google::protobuf::FieldDescriptor::Type type,
72                                   bool is_repeated = false);
73 };
74 
75 struct Proto {
76   Proto() = default;
77   Proto(std::string evt_name, const google::protobuf::Descriptor& desc);
78   struct Field {
79     ProtoType type;
80     std::string name;
81     uint32_t number;
82   };
83   std::string name;
84   std::string event_name;
85   std::map<std::string, Field> fields;
86 
87   std::string ToString();
88   void UnionFields(const std::vector<Proto::Field>& candidate_fields);
89   void AddField(Proto::Field field);
90   std::vector<const Field*> SortedFields();
91   uint32_t max_id = 0;
92 };
93 
94 std::string ToCamelCase(const std::string& s);
95 ProtoType GetCommon(ProtoType one, ProtoType other);
96 std::string ProtoHeader();
97 ProtoType InferProtoType(const FtraceEvent::Field& field);
98 
99 }  // namespace perfetto
100 
101 #endif  // SRC_TOOLS_FTRACE_PROTO_GEN_PROTO_GEN_UTILS_H_
102