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