1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
3 #ifndef __PERF_BPF_UTILS_H
4 #define __PERF_BPF_UTILS_H
6 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
8 #ifdef HAVE_LIBBPF_SUPPORT
10 #include <bpf/libbpf.h>
13 * Get bpf_prog_info in continuous memory
15 * struct bpf_prog_info has multiple arrays. The user has option to choose
16 * arrays to fetch from kernel. The following APIs provide an uniform way to
17 * fetch these data. All arrays in bpf_prog_info are stored in a single
18 * continuous memory region. This makes it easy to store the info in a
21 * Before writing perf_bpil to files, it is necessary to
22 * translate pointers in bpf_prog_info to offsets. Helper functions
23 * bpil_addr_to_offs() and bpil_offs_to_addr()
24 * are introduced to switch between pointers and offsets.
27 * # To fetch map_ids and prog_tags:
28 * __u64 arrays = (1UL << PERF_BPIL_MAP_IDS) |
29 * (1UL << PERF_BPIL_PROG_TAGS);
30 * struct perf_bpil *info_linear =
31 * get_bpf_prog_info_linear(fd, arrays);
33 * # To save data in file
34 * bpil_addr_to_offs(info_linear);
35 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
37 * # To read data from file
38 * read(f, info_linear, <proper_size>);
39 * bpil_offs_to_addr(info_linear);
41 enum perf_bpil_array_types {
42 PERF_BPIL_FIRST_ARRAY = 0,
43 PERF_BPIL_JITED_INSNS = 0,
44 PERF_BPIL_XLATED_INSNS,
46 PERF_BPIL_JITED_KSYMS,
47 PERF_BPIL_JITED_FUNC_LENS,
50 PERF_BPIL_JITED_LINE_INFO,
56 /* size of struct bpf_prog_info, when the tool is compiled */
58 /* total bytes allocated for data, round up to 8 bytes */
60 /* which arrays are included in data */
62 struct bpf_prog_info info;
67 get_bpf_prog_info_linear(int fd, __u64 arrays);
70 bpil_addr_to_offs(struct perf_bpil *info_linear);
73 bpil_offs_to_addr(struct perf_bpil *info_linear);
75 #endif /* HAVE_LIBBPF_SUPPORT */
76 #endif /* __PERF_BPF_UTILS_H */