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 | PERF_FORMAT_LOST
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 	__u64			 lost;
89 };
90 
91 struct perf_record_throttle {
92 	struct perf_event_header header;
93 	__u64			 time;
94 	__u64			 id;
95 	__u64			 stream_id;
96 };
97 
98 #ifndef KSYM_NAME_LEN
99 #define KSYM_NAME_LEN 512
100 #endif
101 
102 struct perf_record_ksymbol {
103 	struct perf_event_header header;
104 	__u64			 addr;
105 	__u32			 len;
106 	__u16			 ksym_type;
107 	__u16			 flags;
108 	char			 name[KSYM_NAME_LEN];
109 };
110 
111 struct perf_record_bpf_event {
112 	struct perf_event_header header;
113 	__u16			 type;
114 	__u16			 flags;
115 	__u32			 id;
116 
117 	/* for bpf_prog types */
118 	__u8			 tag[BPF_TAG_SIZE];  // prog tag
119 };
120 
121 struct perf_record_cgroup {
122 	struct perf_event_header header;
123 	__u64			 id;
124 	char			 path[PATH_MAX];
125 };
126 
127 struct perf_record_text_poke_event {
128 	struct perf_event_header header;
129 	__u64			addr;
130 	__u16			old_len;
131 	__u16			new_len;
132 	__u8			bytes[];
133 };
134 
135 struct perf_record_sample {
136 	struct perf_event_header header;
137 	__u64			 array[];
138 };
139 
140 struct perf_record_switch {
141 	struct perf_event_header header;
142 	__u32			 next_prev_pid;
143 	__u32			 next_prev_tid;
144 };
145 
146 struct perf_record_header_attr {
147 	struct perf_event_header header;
148 	struct perf_event_attr	 attr;
149 	__u64			 id[];
150 };
151 
152 enum {
153 	PERF_CPU_MAP__CPUS = 0,
154 	PERF_CPU_MAP__MASK = 1,
155 	PERF_CPU_MAP__RANGE_CPUS = 2,
156 };
157 
158 /*
159  * Array encoding of a perf_cpu_map where nr is the number of entries in cpu[]
160  * and each entry is a value for a CPU in the map.
161  */
162 struct cpu_map_entries {
163 	__u16			 nr;
164 	__u16			 cpu[];
165 };
166 
167 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 32-bit. */
168 struct perf_record_mask_cpu_map32 {
169 	/* Number of mask values. */
170 	__u16			 nr;
171 	/* Constant 4. */
172 	__u16			 long_size;
173 	/* Bitmap data. */
174 	__u32			 mask[];
175 };
176 
177 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 64-bit. */
178 struct perf_record_mask_cpu_map64 {
179 	/* Number of mask values. */
180 	__u16			 nr;
181 	/* Constant 8. */
182 	__u16			 long_size;
183 	/* Legacy padding. */
184 	char                     __pad[4];
185 	/* Bitmap data. */
186 	__u64			 mask[];
187 };
188 
189 /*
190  * 'struct perf_record_cpu_map_data' is packed as unfortunately an earlier
191  * version had unaligned data and we wish to retain file format compatibility.
192  * -irogers
193  */
194 #pragma GCC diagnostic push
195 #pragma GCC diagnostic ignored "-Wpacked"
196 #pragma GCC diagnostic ignored "-Wattributes"
197 
198 /*
199  * An encoding of a CPU map for a range starting at start_cpu through to
200  * end_cpu. If any_cpu is 1, an any CPU (-1) value (aka dummy value) is present.
201  */
202 struct perf_record_range_cpu_map {
203 	__u8 any_cpu;
204 	__u8 __pad;
205 	__u16 start_cpu;
206 	__u16 end_cpu;
207 };
208 
209 struct perf_record_cpu_map_data {
210 	__u16			 type;
211 	union {
212 		/* Used when type == PERF_CPU_MAP__CPUS. */
213 		struct cpu_map_entries cpus_data;
214 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 4. */
215 		struct perf_record_mask_cpu_map32 mask32_data;
216 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 8. */
217 		struct perf_record_mask_cpu_map64 mask64_data;
218 		/* Used when type == PERF_CPU_MAP__RANGE_CPUS. */
219 		struct perf_record_range_cpu_map range_cpu_data;
220 	};
221 } __attribute__((packed));
222 
223 #pragma GCC diagnostic pop
224 
225 struct perf_record_cpu_map {
226 	struct perf_event_header	 header;
227 	struct perf_record_cpu_map_data	 data;
228 };
229 
230 enum {
231 	PERF_EVENT_UPDATE__UNIT  = 0,
232 	PERF_EVENT_UPDATE__SCALE = 1,
233 	PERF_EVENT_UPDATE__NAME  = 2,
234 	PERF_EVENT_UPDATE__CPUS  = 3,
235 };
236 
237 struct perf_record_event_update_cpus {
238 	struct perf_record_cpu_map_data	 cpus;
239 };
240 
241 struct perf_record_event_update_scale {
242 	double			 scale;
243 };
244 
245 struct perf_record_event_update {
246 	struct perf_event_header header;
247 	__u64			 type;
248 	__u64			 id;
249 	union {
250 		/* Used when type == PERF_EVENT_UPDATE__SCALE. */
251 		struct perf_record_event_update_scale scale;
252 		/* Used when type == PERF_EVENT_UPDATE__UNIT. */
253 		char unit[0];
254 		/* Used when type == PERF_EVENT_UPDATE__NAME. */
255 		char name[0];
256 		/* Used when type == PERF_EVENT_UPDATE__CPUS. */
257 		struct perf_record_event_update_cpus cpus;
258 	};
259 };
260 
261 #define MAX_EVENT_NAME 64
262 
263 struct perf_trace_event_type {
264 	__u64			 event_id;
265 	char			 name[MAX_EVENT_NAME];
266 };
267 
268 struct perf_record_header_event_type {
269 	struct perf_event_header	 header;
270 	struct perf_trace_event_type	 event_type;
271 };
272 
273 struct perf_record_header_tracing_data {
274 	struct perf_event_header header;
275 	__u32			 size;
276 };
277 
278 #define PERF_RECORD_MISC_BUILD_ID_SIZE (1 << 15)
279 
280 struct perf_record_header_build_id {
281 	struct perf_event_header header;
282 	pid_t			 pid;
283 	union {
284 		__u8		 build_id[24];
285 		struct {
286 			__u8	 data[20];
287 			__u8	 size;
288 			__u8	 reserved1__;
289 			__u16	 reserved2__;
290 		};
291 	};
292 	char			 filename[];
293 };
294 
295 struct id_index_entry {
296 	__u64			 id;
297 	__u64			 idx;
298 	__u64			 cpu;
299 	__u64			 tid;
300 };
301 
302 struct id_index_entry_2 {
303 	__u64			 machine_pid;
304 	__u64			 vcpu;
305 };
306 
307 struct perf_record_id_index {
308 	struct perf_event_header header;
309 	__u64			 nr;
310 	struct id_index_entry	 entries[];
311 };
312 
313 struct perf_record_auxtrace_info {
314 	struct perf_event_header header;
315 	__u32			 type;
316 	__u32			 reserved__; /* For alignment */
317 	__u64			 priv[];
318 };
319 
320 struct perf_record_auxtrace {
321 	struct perf_event_header header;
322 	__u64			 size;
323 	__u64			 offset;
324 	__u64			 reference;
325 	__u32			 idx;
326 	__u32			 tid;
327 	__u32			 cpu;
328 	__u32			 reserved__; /* For alignment */
329 };
330 
331 #define MAX_AUXTRACE_ERROR_MSG 64
332 
333 struct perf_record_auxtrace_error {
334 	struct perf_event_header header;
335 	__u32			 type;
336 	__u32			 code;
337 	__u32			 cpu;
338 	__u32			 pid;
339 	__u32			 tid;
340 	__u32			 fmt;
341 	__u64			 ip;
342 	__u64			 time;
343 	char			 msg[MAX_AUXTRACE_ERROR_MSG];
344 	__u32			 machine_pid;
345 	__u32			 vcpu;
346 };
347 
348 struct perf_record_aux {
349 	struct perf_event_header header;
350 	__u64			 aux_offset;
351 	__u64			 aux_size;
352 	__u64			 flags;
353 };
354 
355 struct perf_record_itrace_start {
356 	struct perf_event_header header;
357 	__u32			 pid;
358 	__u32			 tid;
359 };
360 
361 struct perf_record_aux_output_hw_id {
362 	struct perf_event_header header;
363 	__u64			hw_id;
364 };
365 
366 struct perf_record_thread_map_entry {
367 	__u64			 pid;
368 	char			 comm[16];
369 };
370 
371 struct perf_record_thread_map {
372 	struct perf_event_header		 header;
373 	__u64					 nr;
374 	struct perf_record_thread_map_entry	 entries[];
375 };
376 
377 enum {
378 	PERF_STAT_CONFIG_TERM__AGGR_MODE	= 0,
379 	PERF_STAT_CONFIG_TERM__INTERVAL		= 1,
380 	PERF_STAT_CONFIG_TERM__SCALE		= 2,
381 	PERF_STAT_CONFIG_TERM__MAX		= 3,
382 };
383 
384 struct perf_record_stat_config_entry {
385 	__u64			 tag;
386 	__u64			 val;
387 };
388 
389 struct perf_record_stat_config {
390 	struct perf_event_header		 header;
391 	__u64					 nr;
392 	struct perf_record_stat_config_entry	 data[];
393 };
394 
395 struct perf_record_stat {
396 	struct perf_event_header header;
397 
398 	__u64			 id;
399 	__u32			 cpu;
400 	__u32			 thread;
401 
402 	union {
403 		struct {
404 			__u64	 val;
405 			__u64	 ena;
406 			__u64	 run;
407 		};
408 		__u64		 values[3];
409 	};
410 };
411 
412 struct perf_record_stat_round {
413 	struct perf_event_header header;
414 	__u64			 type;
415 	__u64			 time;
416 };
417 
418 struct perf_record_time_conv {
419 	struct perf_event_header header;
420 	__u64			 time_shift;
421 	__u64			 time_mult;
422 	__u64			 time_zero;
423 	__u64			 time_cycles;
424 	__u64			 time_mask;
425 	__u8			 cap_user_time_zero;
426 	__u8			 cap_user_time_short;
427 	__u8			 reserved[6];	/* For alignment */
428 };
429 
430 struct perf_record_header_feature {
431 	struct perf_event_header header;
432 	__u64			 feat_id;
433 	char			 data[];
434 };
435 
436 struct perf_record_compressed {
437 	struct perf_event_header header;
438 	char			 data[];
439 };
440 
441 enum perf_user_event_type { /* above any possible kernel type */
442 	PERF_RECORD_USER_TYPE_START		= 64,
443 	PERF_RECORD_HEADER_ATTR			= 64,
444 	PERF_RECORD_HEADER_EVENT_TYPE		= 65, /* deprecated */
445 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
446 	PERF_RECORD_HEADER_BUILD_ID		= 67,
447 	PERF_RECORD_FINISHED_ROUND		= 68,
448 	PERF_RECORD_ID_INDEX			= 69,
449 	PERF_RECORD_AUXTRACE_INFO		= 70,
450 	PERF_RECORD_AUXTRACE			= 71,
451 	PERF_RECORD_AUXTRACE_ERROR		= 72,
452 	PERF_RECORD_THREAD_MAP			= 73,
453 	PERF_RECORD_CPU_MAP			= 74,
454 	PERF_RECORD_STAT_CONFIG			= 75,
455 	PERF_RECORD_STAT			= 76,
456 	PERF_RECORD_STAT_ROUND			= 77,
457 	PERF_RECORD_EVENT_UPDATE		= 78,
458 	PERF_RECORD_TIME_CONV			= 79,
459 	PERF_RECORD_HEADER_FEATURE		= 80,
460 	PERF_RECORD_COMPRESSED			= 81,
461 	PERF_RECORD_FINISHED_INIT		= 82,
462 	PERF_RECORD_HEADER_MAX
463 };
464 
465 union perf_event {
466 	struct perf_event_header		header;
467 	struct perf_record_mmap			mmap;
468 	struct perf_record_mmap2		mmap2;
469 	struct perf_record_comm			comm;
470 	struct perf_record_namespaces		namespaces;
471 	struct perf_record_cgroup		cgroup;
472 	struct perf_record_fork			fork;
473 	struct perf_record_lost			lost;
474 	struct perf_record_lost_samples		lost_samples;
475 	struct perf_record_read			read;
476 	struct perf_record_throttle		throttle;
477 	struct perf_record_sample		sample;
478 	struct perf_record_bpf_event		bpf;
479 	struct perf_record_ksymbol		ksymbol;
480 	struct perf_record_text_poke_event	text_poke;
481 	struct perf_record_header_attr		attr;
482 	struct perf_record_event_update		event_update;
483 	struct perf_record_header_event_type	event_type;
484 	struct perf_record_header_tracing_data	tracing_data;
485 	struct perf_record_header_build_id	build_id;
486 	struct perf_record_id_index		id_index;
487 	struct perf_record_auxtrace_info	auxtrace_info;
488 	struct perf_record_auxtrace		auxtrace;
489 	struct perf_record_auxtrace_error	auxtrace_error;
490 	struct perf_record_aux			aux;
491 	struct perf_record_itrace_start		itrace_start;
492 	struct perf_record_aux_output_hw_id	aux_output_hw_id;
493 	struct perf_record_switch		context_switch;
494 	struct perf_record_thread_map		thread_map;
495 	struct perf_record_cpu_map		cpu_map;
496 	struct perf_record_stat_config		stat_config;
497 	struct perf_record_stat			stat;
498 	struct perf_record_stat_round		stat_round;
499 	struct perf_record_time_conv		time_conv;
500 	struct perf_record_header_feature	feat;
501 	struct perf_record_compressed		pack;
502 };
503 
504 #endif /* __LIBPERF_EVENT_H */
505