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_TRACED_PROBES_FTRACE_EVENT_INFO_CONSTANTS_H_
18 #define SRC_TRACED_PROBES_FTRACE_EVENT_INFO_CONSTANTS_H_
19
20 #include <stdint.h>
21
22 #include <string>
23 #include <vector>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/protozero/proto_utils.h"
27
28 namespace perfetto {
29
30 enum FtraceFieldType {
31 kFtraceUint8 = 1,
32 kFtraceUint16,
33 kFtraceUint32,
34 kFtraceUint64,
35 kFtraceInt8,
36 kFtraceInt16,
37 kFtraceInt32,
38 kFtraceInt64,
39 kFtraceFixedCString,
40 kFtraceCString,
41 kFtraceStringPtr,
42 kFtraceBool,
43 kFtraceInode32,
44 kFtraceInode64,
45 kFtracePid32,
46 kFtraceCommonPid32,
47 kFtraceDevId32,
48 kFtraceDevId64,
49 kFtraceDataLoc,
50 };
51
52 // Joint enum of FtraceFieldType (left) and ProtoFieldType (right).
53 // where there exists a way to convert from the FtraceFieldType
54 // into the ProtoFieldType.
55 enum TranslationStrategy {
56 kUint8ToUint32 = 1,
57 kUint8ToUint64,
58 kUint16ToUint32,
59 kUint16ToUint64,
60 kUint32ToUint32,
61 kUint32ToUint64,
62 kUint64ToUint64,
63 kInt8ToInt32,
64 kInt8ToInt64,
65 kInt16ToInt32,
66 kInt16ToInt64,
67 kInt32ToInt32,
68 kInt32ToInt64,
69 kInt64ToInt64,
70 kFixedCStringToString,
71 kCStringToString,
72 kStringPtrToString,
73 kBoolToUint32,
74 kBoolToUint64,
75 kInode32ToUint64,
76 kInode64ToUint64,
77 kPid32ToInt32,
78 kPid32ToInt64,
79 kCommonPid32ToInt32,
80 kCommonPid32ToInt64,
81 kDevId32ToUint64,
82 kDevId64ToUint64,
83 kDataLocToString,
84 };
85
ToString(FtraceFieldType v)86 inline const char* ToString(FtraceFieldType v) {
87 switch (v) {
88 case kFtraceUint8:
89 return "uint8";
90 case kFtraceUint16:
91 return "uint16";
92 case kFtraceUint32:
93 return "uint32";
94 case kFtraceUint64:
95 return "uint64";
96 case kFtraceInt8:
97 return "int8";
98 case kFtraceInt16:
99 return "int16";
100 case kFtraceInt32:
101 return "int32";
102 case kFtraceInt64:
103 return "int64";
104 case kFtraceFixedCString:
105 return "fixed length null terminated string";
106 case kFtraceCString:
107 return "null terminated string";
108 case kFtraceStringPtr:
109 return "string ptr";
110 case kFtraceBool:
111 return "bool";
112 case kFtraceInode32:
113 return "inode32";
114 case kFtraceInode64:
115 return "inode64";
116 case kFtracePid32:
117 return "pid32";
118 case kFtraceCommonPid32:
119 return "common_pid32";
120 case kFtraceDevId32:
121 return "devid32";
122 case kFtraceDevId64:
123 return "devid64";
124 case kFtraceDataLoc:
125 return "__data_loc";
126 }
127 // For gcc:
128 PERFETTO_FATAL("Not reached");
129 return "";
130 }
131
132 struct Field {
133 Field() = default;
FieldField134 Field(uint16_t offset, uint16_t size)
135 : ftrace_offset(offset), ftrace_size(size) {}
136
137 uint16_t ftrace_offset;
138 uint16_t ftrace_size;
139 FtraceFieldType ftrace_type;
140 const char* ftrace_name;
141
142 uint32_t proto_field_id;
143 protozero::proto_utils::ProtoSchemaType proto_field_type;
144
145 TranslationStrategy strategy;
146 };
147
148 struct Event {
149 Event() = default;
EventEvent150 Event(const char* event_name, const char* event_group)
151 : name(event_name), group(event_group) {}
152
153 const char* name;
154 const char* group;
155 std::vector<Field> fields;
156 uint32_t ftrace_event_id;
157
158 // Field id of the subevent proto (e.g. PrintFtraceEvent) in the FtraceEvent
159 // parent proto.
160 uint32_t proto_field_id;
161
162 // 'Size' of the event. Some caveats: some events (e.g. print) end with a null
163 // terminated string of unknown size. This size doesn't include the length of
164 // that string.
165 uint16_t size;
166 };
167
168 // The compile time information needed to read the common fields from
169 // the raw ftrace buffer.
170 std::vector<Field> GetStaticCommonFieldsInfo();
171
172 bool SetTranslationStrategy(FtraceFieldType ftrace,
173 protozero::proto_utils::ProtoSchemaType proto,
174 TranslationStrategy* out);
175
176 Field MakeField(const char* name,
177 uint32_t id,
178 protozero::proto_utils::ProtoSchemaType type);
179
180 } // namespace perfetto
181
182 #endif // SRC_TRACED_PROBES_FTRACE_EVENT_INFO_CONSTANTS_H_
183