1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * perf_dlfilter.h: API for perf --dlfilter shared object
4  * Copyright (c) 2021, Intel Corporation.
5  */
6 #ifndef _LINUX_PERF_DLFILTER_H
7 #define _LINUX_PERF_DLFILTER_H
8 
9 #include <linux/perf_event.h>
10 #include <linux/types.h>
11 
12 /*
13  * The following macro can be used to determine if this header defines
14  * perf_dlfilter_sample machine_pid and vcpu.
15  */
16 #define PERF_DLFILTER_HAS_MACHINE_PID
17 
18 /* Definitions for perf_dlfilter_sample flags */
19 enum {
20 	PERF_DLFILTER_FLAG_BRANCH	= 1ULL << 0,
21 	PERF_DLFILTER_FLAG_CALL		= 1ULL << 1,
22 	PERF_DLFILTER_FLAG_RETURN	= 1ULL << 2,
23 	PERF_DLFILTER_FLAG_CONDITIONAL	= 1ULL << 3,
24 	PERF_DLFILTER_FLAG_SYSCALLRET	= 1ULL << 4,
25 	PERF_DLFILTER_FLAG_ASYNC	= 1ULL << 5,
26 	PERF_DLFILTER_FLAG_INTERRUPT	= 1ULL << 6,
27 	PERF_DLFILTER_FLAG_TX_ABORT	= 1ULL << 7,
28 	PERF_DLFILTER_FLAG_TRACE_BEGIN	= 1ULL << 8,
29 	PERF_DLFILTER_FLAG_TRACE_END	= 1ULL << 9,
30 	PERF_DLFILTER_FLAG_IN_TX	= 1ULL << 10,
31 	PERF_DLFILTER_FLAG_VMENTRY	= 1ULL << 11,
32 	PERF_DLFILTER_FLAG_VMEXIT	= 1ULL << 12,
33 };
34 
35 /*
36  * perf sample event information (as per perf script and <linux/perf_event.h>)
37  */
38 struct perf_dlfilter_sample {
39 	__u32 size; /* Size of this structure (for compatibility checking) */
40 	__u16 ins_lat;		/* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
41 	__u16 p_stage_cyc;	/* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
42 	__u64 ip;
43 	__s32 pid;
44 	__s32 tid;
45 	__u64 time;
46 	__u64 addr;
47 	__u64 id;
48 	__u64 stream_id;
49 	__u64 period;
50 	__u64 weight;		/* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
51 	__u64 transaction;	/* Refer PERF_SAMPLE_TRANSACTION in <linux/perf_event.h> */
52 	__u64 insn_cnt;	/* For instructions-per-cycle (IPC) */
53 	__u64 cyc_cnt;		/* For instructions-per-cycle (IPC) */
54 	__s32 cpu;
55 	__u32 flags;		/* Refer PERF_DLFILTER_FLAG_* above */
56 	__u64 data_src;		/* Refer PERF_SAMPLE_DATA_SRC in <linux/perf_event.h> */
57 	__u64 phys_addr;	/* Refer PERF_SAMPLE_PHYS_ADDR in <linux/perf_event.h> */
58 	__u64 data_page_size;	/* Refer PERF_SAMPLE_DATA_PAGE_SIZE in <linux/perf_event.h> */
59 	__u64 code_page_size;	/* Refer PERF_SAMPLE_CODE_PAGE_SIZE in <linux/perf_event.h> */
60 	__u64 cgroup;		/* Refer PERF_SAMPLE_CGROUP in <linux/perf_event.h> */
61 	__u8  cpumode;		/* Refer CPUMODE_MASK etc in <linux/perf_event.h> */
62 	__u8  addr_correlates_sym; /* True => resolve_addr() can be called */
63 	__u16 misc;		/* Refer perf_event_header in <linux/perf_event.h> */
64 	__u32 raw_size;		/* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */
65 	const void *raw_data;	/* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */
66 	__u64 brstack_nr;	/* Number of brstack entries */
67 	const struct perf_branch_entry *brstack; /* Refer <linux/perf_event.h> */
68 	__u64 raw_callchain_nr;	/* Number of raw_callchain entries */
69 	const __u64 *raw_callchain; /* Refer <linux/perf_event.h> */
70 	const char *event;
71 	__s32 machine_pid;
72 	__s32 vcpu;
73 };
74 
75 /*
76  * Address location (as per perf script)
77  */
78 struct perf_dlfilter_al {
79 	__u32 size; /* Size of this structure (for compatibility checking) */
80 	__u32 symoff;
81 	const char *sym;
82 	__u64 addr; /* Mapped address (from dso) */
83 	__u64 sym_start;
84 	__u64 sym_end;
85 	const char *dso;
86 	__u8  sym_binding; /* STB_LOCAL, STB_GLOBAL or STB_WEAK, refer <elf.h> */
87 	__u8  is_64_bit; /* Only valid if dso is not NULL */
88 	__u8  is_kernel_ip; /* True if in kernel space */
89 	__u32 buildid_size;
90 	__u8 *buildid;
91 	/* Below members are only populated by resolve_ip() */
92 	__u8 filtered; /* True if this sample event will be filtered out */
93 	const char *comm;
94 	void *priv; /* Private data. Do not change */
95 };
96 
97 struct perf_dlfilter_fns {
98 	/* Return information about ip */
99 	const struct perf_dlfilter_al *(*resolve_ip)(void *ctx);
100 	/* Return information about addr (if addr_correlates_sym) */
101 	const struct perf_dlfilter_al *(*resolve_addr)(void *ctx);
102 	/* Return arguments from --dlarg option */
103 	char **(*args)(void *ctx, int *dlargc);
104 	/*
105 	 * Return information about address (al->size must be set before
106 	 * calling). Returns 0 on success, -1 otherwise. Call al_cleanup()
107 	 * when 'al' data is no longer needed.
108 	 */
109 	__s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al);
110 	/* Return instruction bytes and length */
111 	const __u8 *(*insn)(void *ctx, __u32 *length);
112 	/* Return source file name and line number */
113 	const char *(*srcline)(void *ctx, __u32 *line_number);
114 	/* Return perf_event_attr, refer <linux/perf_event.h> */
115 	struct perf_event_attr *(*attr)(void *ctx);
116 	/* Read object code, return numbers of bytes read */
117 	__s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len);
118 	/*
119 	 * If present (i.e. must check al_cleanup != NULL), call after
120 	 * resolve_address() to free any associated resources.
121 	 */
122 	void (*al_cleanup)(void *ctx, struct perf_dlfilter_al *al);
123 	/* Reserved */
124 	void *(*reserved[119])(void *);
125 };
126 
127 /*
128  * If implemented, 'start' will be called at the beginning,
129  * before any calls to 'filter_event'. Return 0 to indicate success,
130  * or return a negative error code. '*data' can be assigned for use
131  * by other functions. 'ctx' is needed for calls to perf_dlfilter_fns,
132  * but most perf_dlfilter_fns are not valid when called from 'start'.
133  */
134 int start(void **data, void *ctx);
135 
136 /*
137  * If implemented, 'stop' will be called at the end,
138  * after any calls to 'filter_event'. Return 0 to indicate success, or
139  * return a negative error code. 'data' is set by start(). 'ctx' is
140  * needed for calls to perf_dlfilter_fns, but most perf_dlfilter_fns
141  * are not valid when called from 'stop'.
142  */
143 int stop(void *data, void *ctx);
144 
145 /*
146  * If implemented, 'filter_event' will be called for each sample
147  * event. Return 0 to keep the sample event, 1 to filter it out, or
148  * return a negative error code. 'data' is set by start(). 'ctx' is
149  * needed for calls to perf_dlfilter_fns.
150  */
151 int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx);
152 
153 /*
154  * The same as 'filter_event' except it is called before internal
155  * filtering.
156  */
157 int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx);
158 
159 /*
160  * If implemented, return a one-line description of the filter, and optionally
161  * a longer description.
162  */
163 const char *filter_description(const char **long_description);
164 
165 #endif
166