1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdbool.h>
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/perf_event.h>
9 #include "util/evsel_fprintf.h"
10 #include "trace-event.h"
11 
12 struct bit_names {
13 	int bit;
14 	const char *name;
15 };
16 
17 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
18 {
19 	bool first_bit = true;
20 	int i = 0;
21 
22 	do {
23 		if (value & bits[i].bit) {
24 			buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
25 			first_bit = false;
26 		}
27 	} while (bits[++i].name != NULL);
28 }
29 
30 static void __p_sample_type(char *buf, size_t size, u64 value)
31 {
32 #define bit_name(n) { PERF_SAMPLE_##n, #n }
33 	struct bit_names bits[] = {
34 		bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
35 		bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
36 		bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
37 		bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
38 		bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
39 		bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
40 		bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE),
41 		bit_name(WEIGHT_STRUCT),
42 		{ .name = NULL, }
43 	};
44 #undef bit_name
45 	__p_bits(buf, size, value, bits);
46 }
47 
48 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
49 {
50 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
51 	struct bit_names bits[] = {
52 		bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
53 		bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
54 		bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
55 		bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
56 		bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
57 		bit_name(TYPE_SAVE), bit_name(HW_INDEX), bit_name(PRIV_SAVE),
58 		{ .name = NULL, }
59 	};
60 #undef bit_name
61 	__p_bits(buf, size, value, bits);
62 }
63 
64 static void __p_read_format(char *buf, size_t size, u64 value)
65 {
66 #define bit_name(n) { PERF_FORMAT_##n, #n }
67 	struct bit_names bits[] = {
68 		bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
69 		bit_name(ID), bit_name(GROUP), bit_name(LOST),
70 		{ .name = NULL, }
71 	};
72 #undef bit_name
73 	__p_bits(buf, size, value, bits);
74 }
75 
76 #define ENUM_ID_TO_STR_CASE(x) case x: return (#x);
77 static const char *stringify_perf_type_id(u64 value)
78 {
79 	switch (value) {
80 	ENUM_ID_TO_STR_CASE(PERF_TYPE_HARDWARE)
81 	ENUM_ID_TO_STR_CASE(PERF_TYPE_SOFTWARE)
82 	ENUM_ID_TO_STR_CASE(PERF_TYPE_TRACEPOINT)
83 	ENUM_ID_TO_STR_CASE(PERF_TYPE_HW_CACHE)
84 	ENUM_ID_TO_STR_CASE(PERF_TYPE_RAW)
85 	ENUM_ID_TO_STR_CASE(PERF_TYPE_BREAKPOINT)
86 	default:
87 		return NULL;
88 	}
89 }
90 
91 static const char *stringify_perf_hw_id(u64 value)
92 {
93 	switch (value) {
94 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CPU_CYCLES)
95 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_INSTRUCTIONS)
96 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_REFERENCES)
97 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_MISSES)
98 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)
99 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BRANCH_MISSES)
100 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_BUS_CYCLES)
101 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_STALLED_CYCLES_FRONTEND)
102 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_STALLED_CYCLES_BACKEND)
103 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_REF_CPU_CYCLES)
104 	default:
105 		return NULL;
106 	}
107 }
108 
109 static const char *stringify_perf_hw_cache_id(u64 value)
110 {
111 	switch (value) {
112 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_L1D)
113 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_L1I)
114 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_LL)
115 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_DTLB)
116 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_ITLB)
117 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_BPU)
118 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_NODE)
119 	default:
120 		return NULL;
121 	}
122 }
123 
124 static const char *stringify_perf_hw_cache_op_id(u64 value)
125 {
126 	switch (value) {
127 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_READ)
128 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_WRITE)
129 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_OP_PREFETCH)
130 	default:
131 		return NULL;
132 	}
133 }
134 
135 static const char *stringify_perf_hw_cache_op_result_id(u64 value)
136 {
137 	switch (value) {
138 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_RESULT_ACCESS)
139 	ENUM_ID_TO_STR_CASE(PERF_COUNT_HW_CACHE_RESULT_MISS)
140 	default:
141 		return NULL;
142 	}
143 }
144 
145 static const char *stringify_perf_sw_id(u64 value)
146 {
147 	switch (value) {
148 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CPU_CLOCK)
149 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_TASK_CLOCK)
150 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS)
151 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CONTEXT_SWITCHES)
152 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CPU_MIGRATIONS)
153 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS_MIN)
154 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_PAGE_FAULTS_MAJ)
155 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_ALIGNMENT_FAULTS)
156 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_EMULATION_FAULTS)
157 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_DUMMY)
158 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_BPF_OUTPUT)
159 	ENUM_ID_TO_STR_CASE(PERF_COUNT_SW_CGROUP_SWITCHES)
160 	default:
161 		return NULL;
162 	}
163 }
164 #undef ENUM_ID_TO_STR_CASE
165 
166 #define PRINT_ID(_s, _f)					\
167 do {								\
168 	const char *__s = _s;					\
169 	if (__s == NULL)					\
170 		snprintf(buf, size, _f, value);			\
171 	else							\
172 		snprintf(buf, size, _f" (%s)", value, __s);	\
173 } while (0)
174 #define print_id_unsigned(_s)	PRINT_ID(_s, "%"PRIu64)
175 #define print_id_hex(_s)	PRINT_ID(_s, "%#"PRIx64)
176 
177 static void __p_type_id(char *buf, size_t size, u64 value)
178 {
179 	print_id_unsigned(stringify_perf_type_id(value));
180 }
181 
182 static void __p_config_hw_id(char *buf, size_t size, u64 value)
183 {
184 	print_id_hex(stringify_perf_hw_id(value));
185 }
186 
187 static void __p_config_sw_id(char *buf, size_t size, u64 value)
188 {
189 	print_id_hex(stringify_perf_sw_id(value));
190 }
191 
192 static void __p_config_hw_cache_id(char *buf, size_t size, u64 value)
193 {
194 	const char *hw_cache_str = stringify_perf_hw_cache_id(value & 0xff);
195 	const char *hw_cache_op_str =
196 		stringify_perf_hw_cache_op_id((value & 0xff00) >> 8);
197 	const char *hw_cache_op_result_str =
198 		stringify_perf_hw_cache_op_result_id((value & 0xff0000) >> 16);
199 
200 	if (hw_cache_str == NULL || hw_cache_op_str == NULL ||
201 	    hw_cache_op_result_str == NULL) {
202 		snprintf(buf, size, "%#"PRIx64, value);
203 	} else {
204 		snprintf(buf, size, "%#"PRIx64" (%s | %s | %s)", value,
205 			 hw_cache_op_result_str, hw_cache_op_str, hw_cache_str);
206 	}
207 }
208 
209 #ifdef HAVE_LIBTRACEEVENT
210 static void __p_config_tracepoint_id(char *buf, size_t size, u64 value)
211 {
212 	char *str = tracepoint_id_to_name(value);
213 
214 	print_id_hex(str);
215 	free(str);
216 }
217 #endif
218 
219 static void __p_config_id(char *buf, size_t size, u32 type, u64 value)
220 {
221 	switch (type) {
222 	case PERF_TYPE_HARDWARE:
223 		return __p_config_hw_id(buf, size, value);
224 	case PERF_TYPE_SOFTWARE:
225 		return __p_config_sw_id(buf, size, value);
226 	case PERF_TYPE_HW_CACHE:
227 		return __p_config_hw_cache_id(buf, size, value);
228 	case PERF_TYPE_TRACEPOINT:
229 #ifdef HAVE_LIBTRACEEVENT
230 		return __p_config_tracepoint_id(buf, size, value);
231 #endif
232 	case PERF_TYPE_RAW:
233 	case PERF_TYPE_BREAKPOINT:
234 	default:
235 		snprintf(buf, size, "%#"PRIx64, value);
236 		return;
237 	}
238 }
239 
240 #define BUF_SIZE		1024
241 
242 #define p_hex(val)		snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
243 #define p_unsigned(val)		snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
244 #define p_signed(val)		snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
245 #define p_sample_type(val)	__p_sample_type(buf, BUF_SIZE, val)
246 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
247 #define p_read_format(val)	__p_read_format(buf, BUF_SIZE, val)
248 #define p_type_id(val)		__p_type_id(buf, BUF_SIZE, val)
249 #define p_config_id(val)	__p_config_id(buf, BUF_SIZE, attr->type, val)
250 
251 #define PRINT_ATTRn(_n, _f, _p, _a)			\
252 do {							\
253 	if (_a || attr->_f) {				\
254 		_p(attr->_f);				\
255 		ret += attr__fprintf(fp, _n, buf, priv);\
256 	}						\
257 } while (0)
258 
259 #define PRINT_ATTRf(_f, _p)	PRINT_ATTRn(#_f, _f, _p, false)
260 
261 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
262 			     attr__fprintf_f attr__fprintf, void *priv)
263 {
264 	char buf[BUF_SIZE];
265 	int ret = 0;
266 
267 	PRINT_ATTRn("type", type, p_type_id, true);
268 	PRINT_ATTRf(size, p_unsigned);
269 	PRINT_ATTRn("config", config, p_config_id, true);
270 	PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned, false);
271 	PRINT_ATTRf(sample_type, p_sample_type);
272 	PRINT_ATTRf(read_format, p_read_format);
273 
274 	PRINT_ATTRf(disabled, p_unsigned);
275 	PRINT_ATTRf(inherit, p_unsigned);
276 	PRINT_ATTRf(pinned, p_unsigned);
277 	PRINT_ATTRf(exclusive, p_unsigned);
278 	PRINT_ATTRf(exclude_user, p_unsigned);
279 	PRINT_ATTRf(exclude_kernel, p_unsigned);
280 	PRINT_ATTRf(exclude_hv, p_unsigned);
281 	PRINT_ATTRf(exclude_idle, p_unsigned);
282 	PRINT_ATTRf(mmap, p_unsigned);
283 	PRINT_ATTRf(comm, p_unsigned);
284 	PRINT_ATTRf(freq, p_unsigned);
285 	PRINT_ATTRf(inherit_stat, p_unsigned);
286 	PRINT_ATTRf(enable_on_exec, p_unsigned);
287 	PRINT_ATTRf(task, p_unsigned);
288 	PRINT_ATTRf(watermark, p_unsigned);
289 	PRINT_ATTRf(precise_ip, p_unsigned);
290 	PRINT_ATTRf(mmap_data, p_unsigned);
291 	PRINT_ATTRf(sample_id_all, p_unsigned);
292 	PRINT_ATTRf(exclude_host, p_unsigned);
293 	PRINT_ATTRf(exclude_guest, p_unsigned);
294 	PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
295 	PRINT_ATTRf(exclude_callchain_user, p_unsigned);
296 	PRINT_ATTRf(mmap2, p_unsigned);
297 	PRINT_ATTRf(comm_exec, p_unsigned);
298 	PRINT_ATTRf(use_clockid, p_unsigned);
299 	PRINT_ATTRf(context_switch, p_unsigned);
300 	PRINT_ATTRf(write_backward, p_unsigned);
301 	PRINT_ATTRf(namespaces, p_unsigned);
302 	PRINT_ATTRf(ksymbol, p_unsigned);
303 	PRINT_ATTRf(bpf_event, p_unsigned);
304 	PRINT_ATTRf(aux_output, p_unsigned);
305 	PRINT_ATTRf(cgroup, p_unsigned);
306 	PRINT_ATTRf(text_poke, p_unsigned);
307 	PRINT_ATTRf(build_id, p_unsigned);
308 	PRINT_ATTRf(inherit_thread, p_unsigned);
309 	PRINT_ATTRf(remove_on_exec, p_unsigned);
310 	PRINT_ATTRf(sigtrap, p_unsigned);
311 
312 	PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned, false);
313 	PRINT_ATTRf(bp_type, p_unsigned);
314 	PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex, false);
315 	PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex, false);
316 	PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
317 	PRINT_ATTRf(sample_regs_user, p_hex);
318 	PRINT_ATTRf(sample_stack_user, p_unsigned);
319 	PRINT_ATTRf(clockid, p_signed);
320 	PRINT_ATTRf(sample_regs_intr, p_hex);
321 	PRINT_ATTRf(aux_watermark, p_unsigned);
322 	PRINT_ATTRf(sample_max_stack, p_unsigned);
323 	PRINT_ATTRf(aux_sample_size, p_unsigned);
324 	PRINT_ATTRf(sig_data, p_unsigned);
325 
326 	return ret;
327 }
328