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), 39 { .name = NULL, } 40 }; 41 #undef bit_name 42 __p_bits(buf, size, value, bits); 43 } 44 45 static void __p_branch_sample_type(char *buf, size_t size, u64 value) 46 { 47 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n } 48 struct bit_names bits[] = { 49 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY), 50 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL), 51 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX), 52 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP), 53 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES), 54 bit_name(HW_INDEX), 55 { .name = NULL, } 56 }; 57 #undef bit_name 58 __p_bits(buf, size, value, bits); 59 } 60 61 static void __p_read_format(char *buf, size_t size, u64 value) 62 { 63 #define bit_name(n) { PERF_FORMAT_##n, #n } 64 struct bit_names bits[] = { 65 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING), 66 bit_name(ID), bit_name(GROUP), 67 { .name = NULL, } 68 }; 69 #undef bit_name 70 __p_bits(buf, size, value, bits); 71 } 72 73 #define BUF_SIZE 1024 74 75 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val)) 76 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val)) 77 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val)) 78 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val) 79 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val) 80 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val) 81 82 #define PRINT_ATTRn(_n, _f, _p) \ 83 do { \ 84 if (attr->_f) { \ 85 _p(attr->_f); \ 86 ret += attr__fprintf(fp, _n, buf, priv);\ 87 } \ 88 } while (0) 89 90 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p) 91 92 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, 93 attr__fprintf_f attr__fprintf, void *priv) 94 { 95 char buf[BUF_SIZE]; 96 int ret = 0; 97 98 PRINT_ATTRf(type, p_unsigned); 99 PRINT_ATTRf(size, p_unsigned); 100 PRINT_ATTRf(config, p_hex); 101 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned); 102 PRINT_ATTRf(sample_type, p_sample_type); 103 PRINT_ATTRf(read_format, p_read_format); 104 105 PRINT_ATTRf(disabled, p_unsigned); 106 PRINT_ATTRf(inherit, p_unsigned); 107 PRINT_ATTRf(pinned, p_unsigned); 108 PRINT_ATTRf(exclusive, p_unsigned); 109 PRINT_ATTRf(exclude_user, p_unsigned); 110 PRINT_ATTRf(exclude_kernel, p_unsigned); 111 PRINT_ATTRf(exclude_hv, p_unsigned); 112 PRINT_ATTRf(exclude_idle, p_unsigned); 113 PRINT_ATTRf(mmap, p_unsigned); 114 PRINT_ATTRf(comm, p_unsigned); 115 PRINT_ATTRf(freq, p_unsigned); 116 PRINT_ATTRf(inherit_stat, p_unsigned); 117 PRINT_ATTRf(enable_on_exec, p_unsigned); 118 PRINT_ATTRf(task, p_unsigned); 119 PRINT_ATTRf(watermark, p_unsigned); 120 PRINT_ATTRf(precise_ip, p_unsigned); 121 PRINT_ATTRf(mmap_data, p_unsigned); 122 PRINT_ATTRf(sample_id_all, p_unsigned); 123 PRINT_ATTRf(exclude_host, p_unsigned); 124 PRINT_ATTRf(exclude_guest, p_unsigned); 125 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned); 126 PRINT_ATTRf(exclude_callchain_user, p_unsigned); 127 PRINT_ATTRf(mmap2, p_unsigned); 128 PRINT_ATTRf(comm_exec, p_unsigned); 129 PRINT_ATTRf(use_clockid, p_unsigned); 130 PRINT_ATTRf(context_switch, p_unsigned); 131 PRINT_ATTRf(write_backward, p_unsigned); 132 PRINT_ATTRf(namespaces, p_unsigned); 133 PRINT_ATTRf(ksymbol, p_unsigned); 134 PRINT_ATTRf(bpf_event, p_unsigned); 135 PRINT_ATTRf(aux_output, p_unsigned); 136 PRINT_ATTRf(cgroup, p_unsigned); 137 138 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned); 139 PRINT_ATTRf(bp_type, p_unsigned); 140 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex); 141 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex); 142 PRINT_ATTRf(branch_sample_type, p_branch_sample_type); 143 PRINT_ATTRf(sample_regs_user, p_hex); 144 PRINT_ATTRf(sample_stack_user, p_unsigned); 145 PRINT_ATTRf(clockid, p_signed); 146 PRINT_ATTRf(sample_regs_intr, p_hex); 147 PRINT_ATTRf(aux_watermark, p_unsigned); 148 PRINT_ATTRf(sample_max_stack, p_unsigned); 149 PRINT_ATTRf(aux_sample_size, p_unsigned); 150 PRINT_ATTRf(text_poke, p_unsigned); 151 152 return ret; 153 } 154