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