1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/perf_event.h>
8 #include "util/evsel_fprintf.h"
9 
10 struct bit_names {
11 	int bit;
12 	const char *name;
13 };
14 
15 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
16 {
17 	bool first_bit = true;
18 	int i = 0;
19 
20 	do {
21 		if (value & bits[i].bit) {
22 			buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
23 			first_bit = false;
24 		}
25 	} while (bits[++i].name != NULL);
26 }
27 
28 static void __p_sample_type(char *buf, size_t size, u64 value)
29 {
30 #define bit_name(n) { PERF_SAMPLE_##n, #n }
31 	struct bit_names bits[] = {
32 		bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
33 		bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
34 		bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
35 		bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
36 		bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
37 		bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
38 		bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE),
39 		bit_name(WEIGHT_STRUCT),
40 		{ .name = NULL, }
41 	};
42 #undef bit_name
43 	__p_bits(buf, size, value, bits);
44 }
45 
46 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
47 {
48 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
49 	struct bit_names bits[] = {
50 		bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
51 		bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
52 		bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
53 		bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
54 		bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
55 		bit_name(TYPE_SAVE), bit_name(HW_INDEX),
56 		{ .name = NULL, }
57 	};
58 #undef bit_name
59 	__p_bits(buf, size, value, bits);
60 }
61 
62 static void __p_read_format(char *buf, size_t size, u64 value)
63 {
64 #define bit_name(n) { PERF_FORMAT_##n, #n }
65 	struct bit_names bits[] = {
66 		bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
67 		bit_name(ID), bit_name(GROUP),
68 		{ .name = NULL, }
69 	};
70 #undef bit_name
71 	__p_bits(buf, size, value, bits);
72 }
73 
74 #define BUF_SIZE		1024
75 
76 #define p_hex(val)		snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
77 #define p_unsigned(val)		snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
78 #define p_signed(val)		snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
79 #define p_sample_type(val)	__p_sample_type(buf, BUF_SIZE, val)
80 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
81 #define p_read_format(val)	__p_read_format(buf, BUF_SIZE, val)
82 
83 #define PRINT_ATTRn(_n, _f, _p)				\
84 do {							\
85 	if (attr->_f) {					\
86 		_p(attr->_f);				\
87 		ret += attr__fprintf(fp, _n, buf, priv);\
88 	}						\
89 } while (0)
90 
91 #define PRINT_ATTRf(_f, _p)	PRINT_ATTRn(#_f, _f, _p)
92 
93 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
94 			     attr__fprintf_f attr__fprintf, void *priv)
95 {
96 	char buf[BUF_SIZE];
97 	int ret = 0;
98 
99 	PRINT_ATTRf(type, p_unsigned);
100 	PRINT_ATTRf(size, p_unsigned);
101 	PRINT_ATTRf(config, p_hex);
102 	PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
103 	PRINT_ATTRf(sample_type, p_sample_type);
104 	PRINT_ATTRf(read_format, p_read_format);
105 
106 	PRINT_ATTRf(disabled, p_unsigned);
107 	PRINT_ATTRf(inherit, p_unsigned);
108 	PRINT_ATTRf(pinned, p_unsigned);
109 	PRINT_ATTRf(exclusive, p_unsigned);
110 	PRINT_ATTRf(exclude_user, p_unsigned);
111 	PRINT_ATTRf(exclude_kernel, p_unsigned);
112 	PRINT_ATTRf(exclude_hv, p_unsigned);
113 	PRINT_ATTRf(exclude_idle, p_unsigned);
114 	PRINT_ATTRf(mmap, p_unsigned);
115 	PRINT_ATTRf(comm, p_unsigned);
116 	PRINT_ATTRf(freq, p_unsigned);
117 	PRINT_ATTRf(inherit_stat, p_unsigned);
118 	PRINT_ATTRf(enable_on_exec, p_unsigned);
119 	PRINT_ATTRf(task, p_unsigned);
120 	PRINT_ATTRf(watermark, p_unsigned);
121 	PRINT_ATTRf(precise_ip, p_unsigned);
122 	PRINT_ATTRf(mmap_data, p_unsigned);
123 	PRINT_ATTRf(sample_id_all, p_unsigned);
124 	PRINT_ATTRf(exclude_host, p_unsigned);
125 	PRINT_ATTRf(exclude_guest, p_unsigned);
126 	PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
127 	PRINT_ATTRf(exclude_callchain_user, p_unsigned);
128 	PRINT_ATTRf(mmap2, p_unsigned);
129 	PRINT_ATTRf(comm_exec, p_unsigned);
130 	PRINT_ATTRf(use_clockid, p_unsigned);
131 	PRINT_ATTRf(context_switch, p_unsigned);
132 	PRINT_ATTRf(write_backward, p_unsigned);
133 	PRINT_ATTRf(namespaces, p_unsigned);
134 	PRINT_ATTRf(ksymbol, p_unsigned);
135 	PRINT_ATTRf(bpf_event, p_unsigned);
136 	PRINT_ATTRf(aux_output, p_unsigned);
137 	PRINT_ATTRf(cgroup, p_unsigned);
138 	PRINT_ATTRf(text_poke, p_unsigned);
139 	PRINT_ATTRf(build_id, p_unsigned);
140 	PRINT_ATTRf(inherit_thread, p_unsigned);
141 	PRINT_ATTRf(remove_on_exec, p_unsigned);
142 	PRINT_ATTRf(sigtrap, p_unsigned);
143 
144 	PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
145 	PRINT_ATTRf(bp_type, p_unsigned);
146 	PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
147 	PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
148 	PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
149 	PRINT_ATTRf(sample_regs_user, p_hex);
150 	PRINT_ATTRf(sample_stack_user, p_unsigned);
151 	PRINT_ATTRf(clockid, p_signed);
152 	PRINT_ATTRf(sample_regs_intr, p_hex);
153 	PRINT_ATTRf(aux_watermark, p_unsigned);
154 	PRINT_ATTRf(sample_max_stack, p_unsigned);
155 	PRINT_ATTRf(aux_sample_size, p_unsigned);
156 	PRINT_ATTRf(sig_data, p_unsigned);
157 
158 	return ret;
159 }
160