summaryrefslogtreecommitdiffstats
path: root/tools/perf/util/bpf-utils.h
blob: 86a5055cdfad6689f4db9b1b818c0eae31b77fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */

#ifndef __PERF_BPF_UTILS_H
#define __PERF_BPF_UTILS_H

#define ptr_to_u64(ptr)    ((__u64)(unsigned long)(ptr))

#ifdef HAVE_LIBBPF_SUPPORT

#include <bpf/libbpf.h>

/*
 * Get bpf_prog_info in continuous memory
 *
 * struct bpf_prog_info has multiple arrays. The user has option to choose
 * arrays to fetch from kernel. The following APIs provide an uniform way to
 * fetch these data. All arrays in bpf_prog_info are stored in a single
 * continuous memory region. This makes it easy to store the info in a
 * file.
 *
 * Before writing perf_bpil to files, it is necessary to
 * translate pointers in bpf_prog_info to offsets. Helper functions
 * bpil_addr_to_offs() and bpil_offs_to_addr()
 * are introduced to switch between pointers and offsets.
 *
 * Examples:
 *   # To fetch map_ids and prog_tags:
 *   __u64 arrays = (1UL << PERF_BPIL_MAP_IDS) |
 *           (1UL << PERF_BPIL_PROG_TAGS);
 *   struct perf_bpil *info_linear =
 *           get_bpf_prog_info_linear(fd, arrays);
 *
 *   # To save data in file
 *   bpil_addr_to_offs(info_linear);
 *   write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
 *
 *   # To read data from file
 *   read(f, info_linear, <proper_size>);
 *   bpil_offs_to_addr(info_linear);
 */
enum perf_bpil_array_types {
	PERF_BPIL_FIRST_ARRAY = 0,
	PERF_BPIL_JITED_INSNS = 0,
	PERF_BPIL_XLATED_INSNS,
	PERF_BPIL_MAP_IDS,
	PERF_BPIL_JITED_KSYMS,
	PERF_BPIL_JITED_FUNC_LENS,
	PERF_BPIL_FUNC_INFO,
	PERF_BPIL_LINE_INFO,
	PERF_BPIL_JITED_LINE_INFO,
	PERF_BPIL_PROG_TAGS,
	PERF_BPIL_LAST_ARRAY,
};

struct perf_bpil {
	/* size of struct bpf_prog_info, when the tool is compiled */
	__u32			info_len;
	/* total bytes allocated for data, round up to 8 bytes */
	__u32			data_len;
	/* which arrays are included in data */
	__u64			arrays;
	struct bpf_prog_info	info;
	__u8			data[];
};

struct perf_bpil *
get_bpf_prog_info_linear(int fd, __u64 arrays);

void
bpil_addr_to_offs(struct perf_bpil *info_linear);

void
bpil_offs_to_addr(struct perf_bpil *info_linear);

#endif /* HAVE_LIBBPF_SUPPORT */
#endif /* __PERF_BPF_UTILS_H */