1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBPERF_EVENT_H
3 #define __LIBPERF_EVENT_H
4 
5 #include <linux/perf_event.h>
6 #include <linux/types.h>
7 #include <linux/limits.h>
8 #include <linux/bpf.h>
9 #include <sys/types.h> /* pid_t */
10 
11 #define event_contains(obj, mem) ((obj).header.size > offsetof(typeof(obj), mem))
12 
13 struct perf_record_mmap {
14 	struct perf_event_header header;
15 	__u32			 pid, tid;
16 	__u64			 start;
17 	__u64			 len;
18 	__u64			 pgoff;
19 	char			 filename[PATH_MAX];
20 };
21 
22 struct perf_record_mmap2 {
23 	struct perf_event_header header;
24 	__u32			 pid, tid;
25 	__u64			 start;
26 	__u64			 len;
27 	__u64			 pgoff;
28 	union {
29 		struct {
30 			__u32	 maj;
31 			__u32	 min;
32 			__u64	 ino;
33 			__u64	 ino_generation;
34 		};
35 		struct {
36 			__u8	 build_id_size;
37 			__u8	 __reserved_1;
38 			__u16	 __reserved_2;
39 			__u8	 build_id[20];
40 		};
41 	};
42 	__u32			 prot;
43 	__u32			 flags;
44 	char			 filename[PATH_MAX];
45 };
46 
47 struct perf_record_comm {
48 	struct perf_event_header header;
49 	__u32			 pid, tid;
50 	char			 comm[16];
51 };
52 
53 struct perf_record_namespaces {
54 	struct perf_event_header header;
55 	__u32			 pid, tid;
56 	__u64			 nr_namespaces;
57 	struct perf_ns_link_info link_info[];
58 };
59 
60 struct perf_record_fork {
61 	struct perf_event_header header;
62 	__u32			 pid, ppid;
63 	__u32			 tid, ptid;
64 	__u64			 time;
65 };
66 
67 struct perf_record_lost {
68 	struct perf_event_header header;
69 	__u64			 id;
70 	__u64			 lost;
71 };
72 
73 struct perf_record_lost_samples {
74 	struct perf_event_header header;
75 	__u64			 lost;
76 };
77 
78 /*
79  * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID
80  */
81 struct perf_record_read {
82 	struct perf_event_header header;
83 	__u32			 pid, tid;
84 	__u64			 value;
85 	__u64			 time_enabled;
86 	__u64			 time_running;
87 	__u64			 id;
88 };
89 
90 struct perf_record_throttle {
91 	struct perf_event_header header;
92 	__u64			 time;
93 	__u64			 id;
94 	__u64			 stream_id;
95 };
96 
97 #ifndef KSYM_NAME_LEN
98 #define KSYM_NAME_LEN 256
99 #endif
100 
101 struct perf_record_ksymbol {
102 	struct perf_event_header header;
103 	__u64			 addr;
104 	__u32			 len;
105 	__u16			 ksym_type;
106 	__u16			 flags;
107 	char			 name[KSYM_NAME_LEN];
108 };
109 
110 struct perf_record_bpf_event {
111 	struct perf_event_header header;
112 	__u16			 type;
113 	__u16			 flags;
114 	__u32			 id;
115 
116 	/* for bpf_prog types */
117 	__u8			 tag[BPF_TAG_SIZE];  // prog tag
118 };
119 
120 struct perf_record_cgroup {
121 	struct perf_event_header header;
122 	__u64			 id;
123 	char			 path[PATH_MAX];
124 };
125 
126 struct perf_record_text_poke_event {
127 	struct perf_event_header header;
128 	__u64			addr;
129 	__u16			old_len;
130 	__u16			new_len;
131 	__u8			bytes[];
132 };
133 
134 struct perf_record_sample {
135 	struct perf_event_header header;
136 	__u64			 array[];
137 };
138 
139 struct perf_record_switch {
140 	struct perf_event_header header;
141 	__u32			 next_prev_pid;
142 	__u32			 next_prev_tid;
143 };
144 
145 struct perf_record_header_attr {
146 	struct perf_event_header header;
147 	struct perf_event_attr	 attr;
148 	__u64			 id[];
149 };
150 
151 enum {
152 	PERF_CPU_MAP__CPUS = 0,
153 	PERF_CPU_MAP__MASK = 1,
154 };
155 
156 struct cpu_map_entries {
157 	__u16			 nr;
158 	__u16			 cpu[];
159 };
160 
161 struct perf_record_record_cpu_map {
162 	__u16			 nr;
163 	__u16			 long_size;
164 	unsigned long		 mask[];
165 };
166 
167 struct perf_record_cpu_map_data {
168 	__u16			 type;
169 	char			 data[];
170 };
171 
172 struct perf_record_cpu_map {
173 	struct perf_event_header	 header;
174 	struct perf_record_cpu_map_data	 data;
175 };
176 
177 enum {
178 	PERF_EVENT_UPDATE__UNIT  = 0,
179 	PERF_EVENT_UPDATE__SCALE = 1,
180 	PERF_EVENT_UPDATE__NAME  = 2,
181 	PERF_EVENT_UPDATE__CPUS  = 3,
182 };
183 
184 struct perf_record_event_update_cpus {
185 	struct perf_record_cpu_map_data	 cpus;
186 };
187 
188 struct perf_record_event_update_scale {
189 	double			 scale;
190 };
191 
192 struct perf_record_event_update {
193 	struct perf_event_header header;
194 	__u64			 type;
195 	__u64			 id;
196 	char			 data[];
197 };
198 
199 #define MAX_EVENT_NAME 64
200 
201 struct perf_trace_event_type {
202 	__u64			 event_id;
203 	char			 name[MAX_EVENT_NAME];
204 };
205 
206 struct perf_record_header_event_type {
207 	struct perf_event_header	 header;
208 	struct perf_trace_event_type	 event_type;
209 };
210 
211 struct perf_record_header_tracing_data {
212 	struct perf_event_header header;
213 	__u32			 size;
214 };
215 
216 #define PERF_RECORD_MISC_BUILD_ID_SIZE (1 << 15)
217 
218 struct perf_record_header_build_id {
219 	struct perf_event_header header;
220 	pid_t			 pid;
221 	union {
222 		__u8		 build_id[24];
223 		struct {
224 			__u8	 data[20];
225 			__u8	 size;
226 			__u8	 reserved1__;
227 			__u16	 reserved2__;
228 		};
229 	};
230 	char			 filename[];
231 };
232 
233 struct id_index_entry {
234 	__u64			 id;
235 	__u64			 idx;
236 	__u64			 cpu;
237 	__u64			 tid;
238 };
239 
240 struct perf_record_id_index {
241 	struct perf_event_header header;
242 	__u64			 nr;
243 	struct id_index_entry	 entries[0];
244 };
245 
246 struct perf_record_auxtrace_info {
247 	struct perf_event_header header;
248 	__u32			 type;
249 	__u32			 reserved__; /* For alignment */
250 	__u64			 priv[];
251 };
252 
253 struct perf_record_auxtrace {
254 	struct perf_event_header header;
255 	__u64			 size;
256 	__u64			 offset;
257 	__u64			 reference;
258 	__u32			 idx;
259 	__u32			 tid;
260 	__u32			 cpu;
261 	__u32			 reserved__; /* For alignment */
262 };
263 
264 #define MAX_AUXTRACE_ERROR_MSG 64
265 
266 struct perf_record_auxtrace_error {
267 	struct perf_event_header header;
268 	__u32			 type;
269 	__u32			 code;
270 	__u32			 cpu;
271 	__u32			 pid;
272 	__u32			 tid;
273 	__u32			 fmt;
274 	__u64			 ip;
275 	__u64			 time;
276 	char			 msg[MAX_AUXTRACE_ERROR_MSG];
277 };
278 
279 struct perf_record_aux {
280 	struct perf_event_header header;
281 	__u64			 aux_offset;
282 	__u64			 aux_size;
283 	__u64			 flags;
284 };
285 
286 struct perf_record_itrace_start {
287 	struct perf_event_header header;
288 	__u32			 pid;
289 	__u32			 tid;
290 };
291 
292 struct perf_record_thread_map_entry {
293 	__u64			 pid;
294 	char			 comm[16];
295 };
296 
297 struct perf_record_thread_map {
298 	struct perf_event_header		 header;
299 	__u64					 nr;
300 	struct perf_record_thread_map_entry	 entries[];
301 };
302 
303 enum {
304 	PERF_STAT_CONFIG_TERM__AGGR_MODE	= 0,
305 	PERF_STAT_CONFIG_TERM__INTERVAL		= 1,
306 	PERF_STAT_CONFIG_TERM__SCALE		= 2,
307 	PERF_STAT_CONFIG_TERM__MAX		= 3,
308 };
309 
310 struct perf_record_stat_config_entry {
311 	__u64			 tag;
312 	__u64			 val;
313 };
314 
315 struct perf_record_stat_config {
316 	struct perf_event_header		 header;
317 	__u64					 nr;
318 	struct perf_record_stat_config_entry	 data[];
319 };
320 
321 struct perf_record_stat {
322 	struct perf_event_header header;
323 
324 	__u64			 id;
325 	__u32			 cpu;
326 	__u32			 thread;
327 
328 	union {
329 		struct {
330 			__u64	 val;
331 			__u64	 ena;
332 			__u64	 run;
333 		};
334 		__u64		 values[3];
335 	};
336 };
337 
338 struct perf_record_stat_round {
339 	struct perf_event_header header;
340 	__u64			 type;
341 	__u64			 time;
342 };
343 
344 struct perf_record_time_conv {
345 	struct perf_event_header header;
346 	__u64			 time_shift;
347 	__u64			 time_mult;
348 	__u64			 time_zero;
349 	__u64			 time_cycles;
350 	__u64			 time_mask;
351 	__u8			 cap_user_time_zero;
352 	__u8			 cap_user_time_short;
353 	__u8			 reserved[6];	/* For alignment */
354 };
355 
356 struct perf_record_header_feature {
357 	struct perf_event_header header;
358 	__u64			 feat_id;
359 	char			 data[];
360 };
361 
362 struct perf_record_compressed {
363 	struct perf_event_header header;
364 	char			 data[];
365 };
366 
367 enum perf_user_event_type { /* above any possible kernel type */
368 	PERF_RECORD_USER_TYPE_START		= 64,
369 	PERF_RECORD_HEADER_ATTR			= 64,
370 	PERF_RECORD_HEADER_EVENT_TYPE		= 65, /* deprecated */
371 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
372 	PERF_RECORD_HEADER_BUILD_ID		= 67,
373 	PERF_RECORD_FINISHED_ROUND		= 68,
374 	PERF_RECORD_ID_INDEX			= 69,
375 	PERF_RECORD_AUXTRACE_INFO		= 70,
376 	PERF_RECORD_AUXTRACE			= 71,
377 	PERF_RECORD_AUXTRACE_ERROR		= 72,
378 	PERF_RECORD_THREAD_MAP			= 73,
379 	PERF_RECORD_CPU_MAP			= 74,
380 	PERF_RECORD_STAT_CONFIG			= 75,
381 	PERF_RECORD_STAT			= 76,
382 	PERF_RECORD_STAT_ROUND			= 77,
383 	PERF_RECORD_EVENT_UPDATE		= 78,
384 	PERF_RECORD_TIME_CONV			= 79,
385 	PERF_RECORD_HEADER_FEATURE		= 80,
386 	PERF_RECORD_COMPRESSED			= 81,
387 	PERF_RECORD_HEADER_MAX
388 };
389 
390 union perf_event {
391 	struct perf_event_header		header;
392 	struct perf_record_mmap			mmap;
393 	struct perf_record_mmap2		mmap2;
394 	struct perf_record_comm			comm;
395 	struct perf_record_namespaces		namespaces;
396 	struct perf_record_cgroup		cgroup;
397 	struct perf_record_fork			fork;
398 	struct perf_record_lost			lost;
399 	struct perf_record_lost_samples		lost_samples;
400 	struct perf_record_read			read;
401 	struct perf_record_throttle		throttle;
402 	struct perf_record_sample		sample;
403 	struct perf_record_bpf_event		bpf;
404 	struct perf_record_ksymbol		ksymbol;
405 	struct perf_record_text_poke_event	text_poke;
406 	struct perf_record_header_attr		attr;
407 	struct perf_record_event_update		event_update;
408 	struct perf_record_header_event_type	event_type;
409 	struct perf_record_header_tracing_data	tracing_data;
410 	struct perf_record_header_build_id	build_id;
411 	struct perf_record_id_index		id_index;
412 	struct perf_record_auxtrace_info	auxtrace_info;
413 	struct perf_record_auxtrace		auxtrace;
414 	struct perf_record_auxtrace_error	auxtrace_error;
415 	struct perf_record_aux			aux;
416 	struct perf_record_itrace_start		itrace_start;
417 	struct perf_record_switch		context_switch;
418 	struct perf_record_thread_map		thread_map;
419 	struct perf_record_cpu_map		cpu_map;
420 	struct perf_record_stat_config		stat_config;
421 	struct perf_record_stat			stat;
422 	struct perf_record_stat_round		stat_round;
423 	struct perf_record_time_conv		time_conv;
424 	struct perf_record_header_feature	feat;
425 	struct perf_record_compressed		pack;
426 };
427 
428 #endif /* __LIBPERF_EVENT_H */
429