xref: /openbmc/linux/tools/perf/util/bpf-utils.h (revision aac28965)
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 
12 /*
13  * Get bpf_prog_info in continuous memory
14  *
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
19  * file.
20  *
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.
25  *
26  * Examples:
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);
32  *
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);
36  *
37  *   # To read data from file
38  *   read(f, info_linear, <proper_size>);
39  *   bpil_offs_to_addr(info_linear);
40  */
41 enum perf_bpil_array_types {
42 	PERF_BPIL_FIRST_ARRAY = 0,
43 	PERF_BPIL_JITED_INSNS = 0,
44 	PERF_BPIL_XLATED_INSNS,
45 	PERF_BPIL_MAP_IDS,
46 	PERF_BPIL_JITED_KSYMS,
47 	PERF_BPIL_JITED_FUNC_LENS,
48 	PERF_BPIL_FUNC_INFO,
49 	PERF_BPIL_LINE_INFO,
50 	PERF_BPIL_JITED_LINE_INFO,
51 	PERF_BPIL_PROG_TAGS,
52 	PERF_BPIL_LAST_ARRAY,
53 };
54 
55 struct perf_bpil {
56 	/* size of struct bpf_prog_info, when the tool is compiled */
57 	__u32			info_len;
58 	/* total bytes allocated for data, round up to 8 bytes */
59 	__u32			data_len;
60 	/* which arrays are included in data */
61 	__u64			arrays;
62 	struct bpf_prog_info	info;
63 	__u8			data[];
64 };
65 
66 struct perf_bpil *
67 get_bpf_prog_info_linear(int fd, __u64 arrays);
68 
69 void
70 bpil_addr_to_offs(struct perf_bpil *info_linear);
71 
72 void
73 bpil_offs_to_addr(struct perf_bpil *info_linear);
74 
75 #endif /* HAVE_LIBBPF_SUPPORT */
76 #endif /* __PERF_BPF_UTILS_H */
77