1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 3 #ifndef __PERF_BPF_UTILS_H 4 #define __PERF_BPF_UTILS_H 5 6 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) 7 8 #ifdef HAVE_LIBBPF_SUPPORT 9 10 #include <bpf/libbpf.h> 11 #include <bpf/libbpf_version.h> 12 13 #define LIBBPF_CURRENT_VERSION_GEQ(major, minor) \ 14 (LIBBPF_MAJOR_VERSION > (major) || \ 15 (LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor))) 16 17 #if LIBBPF_CURRENT_VERSION_GEQ(1, 7) 18 // libbpf 1.7+ support the btf_dump_type_data_opts.emit_strings option. 19 #define HAVE_LIBBPF_STRINGS_SUPPORT 1 20 #endif 21 22 /* 23 * Get bpf_prog_info in continuous memory 24 * 25 * struct bpf_prog_info has multiple arrays. The user has option to choose 26 * arrays to fetch from kernel. The following APIs provide an uniform way to 27 * fetch these data. All arrays in bpf_prog_info are stored in a single 28 * continuous memory region. This makes it easy to store the info in a 29 * file. 30 * 31 * Before writing perf_bpil to files, it is necessary to 32 * translate pointers in bpf_prog_info to offsets. Helper functions 33 * bpil_addr_to_offs() and bpil_offs_to_addr() 34 * are introduced to switch between pointers and offsets. 35 * 36 * Examples: 37 * # To fetch map_ids and prog_tags: 38 * __u64 arrays = (1UL << PERF_BPIL_MAP_IDS) | 39 * (1UL << PERF_BPIL_PROG_TAGS); 40 * struct perf_bpil *info_linear = 41 * get_bpf_prog_info_linear(fd, arrays); 42 * 43 * # To save data in file 44 * bpil_addr_to_offs(info_linear); 45 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len); 46 * 47 * # To read data from file 48 * read(f, info_linear, <proper_size>); 49 * bpil_offs_to_addr(info_linear); 50 */ 51 enum perf_bpil_array_types { 52 PERF_BPIL_FIRST_ARRAY = 0, 53 PERF_BPIL_JITED_INSNS = 0, 54 PERF_BPIL_XLATED_INSNS, 55 PERF_BPIL_MAP_IDS, 56 PERF_BPIL_JITED_KSYMS, 57 PERF_BPIL_JITED_FUNC_LENS, 58 PERF_BPIL_FUNC_INFO, 59 PERF_BPIL_LINE_INFO, 60 PERF_BPIL_JITED_LINE_INFO, 61 PERF_BPIL_PROG_TAGS, 62 PERF_BPIL_LAST_ARRAY, 63 }; 64 65 struct perf_bpil { 66 /* size of struct bpf_prog_info, when the tool is compiled */ 67 __u32 info_len; 68 /* total bytes allocated for data, round up to 8 bytes */ 69 __u32 data_len; 70 /* which arrays are included in data */ 71 __u64 arrays; 72 struct bpf_prog_info info; 73 __u8 data[]; 74 }; 75 76 struct perf_bpil * 77 get_bpf_prog_info_linear(int fd, __u64 arrays); 78 79 void 80 bpil_addr_to_offs(struct perf_bpil *info_linear); 81 82 void 83 bpil_offs_to_addr(struct perf_bpil *info_linear); 84 85 #endif /* HAVE_LIBBPF_SUPPORT */ 86 #endif /* __PERF_BPF_UTILS_H */ 87