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