1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_RECORD_H
3 #define __PERF_RECORD_H
4 /*
5  * The linux/stddef.h isn't need here, but is needed for __always_inline used
6  * in files included from uapi/linux/perf_event.h such as
7  * /usr/include/linux/swab.h and /usr/include/linux/byteorder/little_endian.h,
8  * detected in at least musl libc, used in Alpine Linux. -acme
9  */
10 #include <stdio.h>
11 #include <linux/stddef.h>
12 #include <perf/event.h>
13 #include <linux/types.h>
14 
15 #include "perf_regs.h"
16 
17 struct dso;
18 struct machine;
19 struct perf_event_attr;
20 
21 #ifdef __LP64__
22 /*
23  * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
24  * __u64 as long long unsigned int, and then -Werror=format= kicks in and
25  * complains of the mismatched types, so use these two special extra PRI
26  * macros to overcome that.
27  */
28 #define PRI_lu64 "l" PRIu64
29 #define PRI_lx64 "l" PRIx64
30 #define PRI_ld64 "l" PRId64
31 #else
32 #define PRI_lu64 PRIu64
33 #define PRI_lx64 PRIx64
34 #define PRI_ld64 PRId64
35 #endif
36 
37 #define PERF_SAMPLE_MASK				\
38 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
39 	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
40 	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
41 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
42 	 PERF_SAMPLE_IDENTIFIER)
43 
44 /* perf sample has 16 bits size limit */
45 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
46 
47 /* number of register is bound by the number of bits in regs_dump::mask (64) */
48 #define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
49 
50 struct regs_dump {
51 	u64 abi;
52 	u64 mask;
53 	u64 *regs;
54 
55 	/* Cached values/mask filled by first register access. */
56 	u64 cache_regs[PERF_SAMPLE_REGS_CACHE_SIZE];
57 	u64 cache_mask;
58 };
59 
60 struct stack_dump {
61 	u16 offset;
62 	u64 size;
63 	char *data;
64 };
65 
66 struct sample_read_value {
67 	u64 value;
68 	u64 id;
69 };
70 
71 struct sample_read {
72 	u64 time_enabled;
73 	u64 time_running;
74 	union {
75 		struct {
76 			u64 nr;
77 			struct sample_read_value *values;
78 		} group;
79 		struct sample_read_value one;
80 	};
81 };
82 
83 struct ip_callchain {
84 	u64 nr;
85 	u64 ips[];
86 };
87 
88 struct branch_stack;
89 
90 enum {
91 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
92 	PERF_IP_FLAG_CALL		= 1ULL << 1,
93 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
94 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
95 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
96 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
97 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
98 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
99 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
100 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
101 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
102 	PERF_IP_FLAG_VMENTRY		= 1ULL << 11,
103 	PERF_IP_FLAG_VMEXIT		= 1ULL << 12,
104 	PERF_IP_FLAG_INTR_DISABLE	= 1ULL << 13,
105 	PERF_IP_FLAG_INTR_TOGGLE	= 1ULL << 14,
106 };
107 
108 #define PERF_IP_FLAG_CHARS "bcrosyiABExghDt"
109 
110 #define PERF_BRANCH_MASK		(\
111 	PERF_IP_FLAG_BRANCH		|\
112 	PERF_IP_FLAG_CALL		|\
113 	PERF_IP_FLAG_RETURN		|\
114 	PERF_IP_FLAG_CONDITIONAL	|\
115 	PERF_IP_FLAG_SYSCALLRET		|\
116 	PERF_IP_FLAG_ASYNC		|\
117 	PERF_IP_FLAG_INTERRUPT		|\
118 	PERF_IP_FLAG_TX_ABORT		|\
119 	PERF_IP_FLAG_TRACE_BEGIN	|\
120 	PERF_IP_FLAG_TRACE_END		|\
121 	PERF_IP_FLAG_VMENTRY		|\
122 	PERF_IP_FLAG_VMEXIT)
123 
124 #define MAX_INSN 16
125 
126 struct aux_sample {
127 	u64 size;
128 	void *data;
129 };
130 
131 struct perf_sample {
132 	u64 ip;
133 	u32 pid, tid;
134 	u64 time;
135 	u64 addr;
136 	u64 id;
137 	u64 stream_id;
138 	u64 period;
139 	u64 weight;
140 	u64 transaction;
141 	u64 insn_cnt;
142 	u64 cyc_cnt;
143 	u32 cpu;
144 	u32 raw_size;
145 	u64 data_src;
146 	u64 phys_addr;
147 	u64 data_page_size;
148 	u64 code_page_size;
149 	u64 cgroup;
150 	u32 flags;
151 	u16 insn_len;
152 	u8  cpumode;
153 	u16 misc;
154 	u16 ins_lat;
155 	u16 p_stage_cyc;
156 	bool no_hw_idx;		/* No hw_idx collected in branch_stack */
157 	char insn[MAX_INSN];
158 	void *raw_data;
159 	struct ip_callchain *callchain;
160 	struct branch_stack *branch_stack;
161 	struct regs_dump  user_regs;
162 	struct regs_dump  intr_regs;
163 	struct stack_dump user_stack;
164 	struct sample_read read;
165 	struct aux_sample aux_sample;
166 };
167 
168 #define PERF_MEM_DATA_SRC_NONE \
169 	(PERF_MEM_S(OP, NA) |\
170 	 PERF_MEM_S(LVL, NA) |\
171 	 PERF_MEM_S(SNOOP, NA) |\
172 	 PERF_MEM_S(LOCK, NA) |\
173 	 PERF_MEM_S(TLB, NA))
174 
175 /* Attribute type for custom synthesized events */
176 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
177 
178 /* Attribute config for custom synthesized events */
179 enum perf_synth_id {
180 	PERF_SYNTH_INTEL_PTWRITE,
181 	PERF_SYNTH_INTEL_MWAIT,
182 	PERF_SYNTH_INTEL_PWRE,
183 	PERF_SYNTH_INTEL_EXSTOP,
184 	PERF_SYNTH_INTEL_PWRX,
185 	PERF_SYNTH_INTEL_CBR,
186 	PERF_SYNTH_INTEL_PSB,
187 	PERF_SYNTH_INTEL_EVT,
188 	PERF_SYNTH_INTEL_IFLAG_CHG,
189 };
190 
191 /*
192  * Raw data formats for synthesized events. Note that 4 bytes of padding are
193  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
194  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
195  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
196  * structure sizes are 4 bytes bigger than the raw_size, refer
197  * perf_synth__raw_size().
198  */
199 
200 struct perf_synth_intel_ptwrite {
201 	u32 padding;
202 	union {
203 		struct {
204 			u32	ip		:  1,
205 				reserved	: 31;
206 		};
207 		u32	flags;
208 	};
209 	u64	payload;
210 };
211 
212 struct perf_synth_intel_mwait {
213 	u32 padding;
214 	u32 reserved;
215 	union {
216 		struct {
217 			u64	hints		:  8,
218 				reserved1	: 24,
219 				extensions	:  2,
220 				reserved2	: 30;
221 		};
222 		u64	payload;
223 	};
224 };
225 
226 struct perf_synth_intel_pwre {
227 	u32 padding;
228 	u32 reserved;
229 	union {
230 		struct {
231 			u64	reserved1	:  7,
232 				hw		:  1,
233 				subcstate	:  4,
234 				cstate		:  4,
235 				reserved2	: 48;
236 		};
237 		u64	payload;
238 	};
239 };
240 
241 struct perf_synth_intel_exstop {
242 	u32 padding;
243 	union {
244 		struct {
245 			u32	ip		:  1,
246 				reserved	: 31;
247 		};
248 		u32	flags;
249 	};
250 };
251 
252 struct perf_synth_intel_pwrx {
253 	u32 padding;
254 	u32 reserved;
255 	union {
256 		struct {
257 			u64	deepest_cstate	:  4,
258 				last_cstate	:  4,
259 				wake_reason	:  4,
260 				reserved1	: 52;
261 		};
262 		u64	payload;
263 	};
264 };
265 
266 struct perf_synth_intel_cbr {
267 	u32 padding;
268 	union {
269 		struct {
270 			u32	cbr		:  8,
271 				reserved1	:  8,
272 				max_nonturbo	:  8,
273 				reserved2	:  8;
274 		};
275 		u32	flags;
276 	};
277 	u32 freq;
278 	u32 reserved3;
279 };
280 
281 struct perf_synth_intel_psb {
282 	u32 padding;
283 	u32 reserved;
284 	u64 offset;
285 };
286 
287 struct perf_synth_intel_evd {
288 	union {
289 		struct {
290 			u8	evd_type;
291 			u8	reserved[7];
292 		};
293 		u64	et;
294 	};
295 	u64	payload;
296 };
297 
298 /* Intel PT Event Trace */
299 struct perf_synth_intel_evt {
300 	u32 padding;
301 	union {
302 		struct {
303 			u32	type		:  5,
304 				reserved	:  2,
305 				ip		:  1,
306 				vector		:  8,
307 				evd_cnt		: 16;
308 		};
309 		u32	cfe;
310 	};
311 	struct perf_synth_intel_evd evd[0];
312 };
313 
314 struct perf_synth_intel_iflag_chg {
315 	u32 padding;
316 	union {
317 		struct {
318 			u32	iflag		:  1,
319 				via_branch	:  1;
320 		};
321 		u32	flags;
322 	};
323 	u64	branch_ip; /* If via_branch */
324 };
325 
326 /*
327  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
328  * 8-byte alignment.
329  */
perf_sample__synth_ptr(struct perf_sample * sample)330 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
331 {
332 	return sample->raw_data - 4;
333 }
334 
perf_synth__raw_data(void * p)335 static inline void *perf_synth__raw_data(void *p)
336 {
337 	return p + 4;
338 }
339 
340 #define perf_synth__raw_size(d) (sizeof(d) - 4)
341 
342 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
343 
344 enum {
345 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
346 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
347 };
348 
349 void perf_event__print_totals(void);
350 
351 struct perf_cpu_map;
352 struct perf_record_stat_config;
353 struct perf_stat_config;
354 struct perf_tool;
355 
356 void perf_event__read_stat_config(struct perf_stat_config *config,
357 				  struct perf_record_stat_config *event);
358 
359 int perf_event__process_comm(struct perf_tool *tool,
360 			     union perf_event *event,
361 			     struct perf_sample *sample,
362 			     struct machine *machine);
363 int perf_event__process_lost(struct perf_tool *tool,
364 			     union perf_event *event,
365 			     struct perf_sample *sample,
366 			     struct machine *machine);
367 int perf_event__process_lost_samples(struct perf_tool *tool,
368 				     union perf_event *event,
369 				     struct perf_sample *sample,
370 				     struct machine *machine);
371 int perf_event__process_aux(struct perf_tool *tool,
372 			    union perf_event *event,
373 			    struct perf_sample *sample,
374 			    struct machine *machine);
375 int perf_event__process_itrace_start(struct perf_tool *tool,
376 				     union perf_event *event,
377 				     struct perf_sample *sample,
378 				     struct machine *machine);
379 int perf_event__process_aux_output_hw_id(struct perf_tool *tool,
380 					 union perf_event *event,
381 					 struct perf_sample *sample,
382 					 struct machine *machine);
383 int perf_event__process_switch(struct perf_tool *tool,
384 			       union perf_event *event,
385 			       struct perf_sample *sample,
386 			       struct machine *machine);
387 int perf_event__process_namespaces(struct perf_tool *tool,
388 				   union perf_event *event,
389 				   struct perf_sample *sample,
390 				   struct machine *machine);
391 int perf_event__process_cgroup(struct perf_tool *tool,
392 			       union perf_event *event,
393 			       struct perf_sample *sample,
394 			       struct machine *machine);
395 int perf_event__process_mmap(struct perf_tool *tool,
396 			     union perf_event *event,
397 			     struct perf_sample *sample,
398 			     struct machine *machine);
399 int perf_event__process_mmap2(struct perf_tool *tool,
400 			     union perf_event *event,
401 			     struct perf_sample *sample,
402 			     struct machine *machine);
403 int perf_event__process_fork(struct perf_tool *tool,
404 			     union perf_event *event,
405 			     struct perf_sample *sample,
406 			     struct machine *machine);
407 int perf_event__process_exit(struct perf_tool *tool,
408 			     union perf_event *event,
409 			     struct perf_sample *sample,
410 			     struct machine *machine);
411 int perf_event__process_ksymbol(struct perf_tool *tool,
412 				union perf_event *event,
413 				struct perf_sample *sample,
414 				struct machine *machine);
415 int perf_event__process_bpf(struct perf_tool *tool,
416 			    union perf_event *event,
417 			    struct perf_sample *sample,
418 			    struct machine *machine);
419 int perf_event__process_text_poke(struct perf_tool *tool,
420 				  union perf_event *event,
421 				  struct perf_sample *sample,
422 				  struct machine *machine);
423 int perf_event__process(struct perf_tool *tool,
424 			union perf_event *event,
425 			struct perf_sample *sample,
426 			struct machine *machine);
427 
428 struct addr_location;
429 
430 int machine__resolve(struct machine *machine, struct addr_location *al,
431 		     struct perf_sample *sample);
432 
433 void addr_location__put(struct addr_location *al);
434 
435 struct thread;
436 
437 bool is_bts_event(struct perf_event_attr *attr);
438 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
439 void thread__resolve(struct thread *thread, struct addr_location *al,
440 		     struct perf_sample *sample);
441 
442 const char *perf_event__name(unsigned int id);
443 
444 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
445 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
446 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
447 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
448 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
449 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
450 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp);
451 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
452 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
453 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
454 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
455 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
456 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
457 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
458 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
459 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
460 
461 int kallsyms__get_function_start(const char *kallsyms_filename,
462 				 const char *symbol_name, u64 *addr);
463 
464 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
465 void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
466 			       u16 type, int max);
467 
468 void event_attr_init(struct perf_event_attr *attr);
469 
470 int perf_event_paranoid(void);
471 bool perf_event_paranoid_check(int max_level);
472 
473 extern int sysctl_perf_event_max_stack;
474 extern int sysctl_perf_event_max_contexts_per_stack;
475 extern unsigned int proc_map_timeout;
476 
477 #define PAGE_SIZE_NAME_LEN	32
478 char *get_page_size_name(u64 size, char *str);
479 
480 void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
481 void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);
482 const char *arch_perf_header_entry(const char *se_header);
483 int arch_support_sort_key(const char *sort_key);
484 
485 #endif /* __PERF_RECORD_H */
486