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