GNU Linux-libre 4.19.263-gnu1
[releases.git] / tools / testing / selftests / bpf / trace_helpers.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <errno.h>
7 #include <poll.h>
8 #include <unistd.h>
9 #include <linux/perf_event.h>
10 #include <sys/mman.h>
11 #include "trace_helpers.h"
12
13 #define MAX_SYMS 300000
14 static struct ksym syms[MAX_SYMS];
15 static int sym_cnt;
16
17 static int ksym_cmp(const void *p1, const void *p2)
18 {
19         return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
20 }
21
22 int load_kallsyms(void)
23 {
24         FILE *f = fopen("/proc/kallsyms", "r");
25         char func[256], buf[256];
26         char symbol;
27         void *addr;
28         int i = 0;
29
30         if (!f)
31                 return -ENOENT;
32
33         while (!feof(f)) {
34                 if (!fgets(buf, sizeof(buf), f))
35                         break;
36                 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
37                         break;
38                 if (!addr)
39                         continue;
40                 syms[i].addr = (long) addr;
41                 syms[i].name = strdup(func);
42                 i++;
43         }
44         fclose(f);
45         sym_cnt = i;
46         qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
47         return 0;
48 }
49
50 struct ksym *ksym_search(long key)
51 {
52         int start = 0, end = sym_cnt;
53         int result;
54
55         /* kallsyms not loaded. return NULL */
56         if (sym_cnt <= 0)
57                 return NULL;
58
59         while (start < end) {
60                 size_t mid = start + (end - start) / 2;
61
62                 result = key - syms[mid].addr;
63                 if (result < 0)
64                         end = mid;
65                 else if (result > 0)
66                         start = mid + 1;
67                 else
68                         return &syms[mid];
69         }
70
71         if (start >= 1 && syms[start - 1].addr < key &&
72             key < syms[start].addr)
73                 /* valid ksym */
74                 return &syms[start - 1];
75
76         /* out of range. return _stext */
77         return &syms[0];
78 }
79
80 long ksym_get_addr(const char *name)
81 {
82         int i;
83
84         for (i = 0; i < sym_cnt; i++) {
85                 if (strcmp(syms[i].name, name) == 0)
86                         return syms[i].addr;
87         }
88
89         return 0;
90 }
91
92 static int page_size;
93 static int page_cnt = 8;
94 static struct perf_event_mmap_page *header;
95
96 int perf_event_mmap_header(int fd, struct perf_event_mmap_page **header)
97 {
98         void *base;
99         int mmap_size;
100
101         page_size = getpagesize();
102         mmap_size = page_size * (page_cnt + 1);
103
104         base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
105         if (base == MAP_FAILED) {
106                 printf("mmap err\n");
107                 return -1;
108         }
109
110         *header = base;
111         return 0;
112 }
113
114 int perf_event_mmap(int fd)
115 {
116         return perf_event_mmap_header(fd, &header);
117 }
118
119 static int perf_event_poll(int fd)
120 {
121         struct pollfd pfd = { .fd = fd, .events = POLLIN };
122
123         return poll(&pfd, 1, 1000);
124 }
125
126 struct perf_event_sample {
127         struct perf_event_header header;
128         __u32 size;
129         char data[];
130 };
131
132 static enum bpf_perf_event_ret bpf_perf_event_print(void *event, void *priv)
133 {
134         struct perf_event_sample *e = event;
135         perf_event_print_fn fn = priv;
136         int ret;
137
138         if (e->header.type == PERF_RECORD_SAMPLE) {
139                 ret = fn(e->data, e->size);
140                 if (ret != LIBBPF_PERF_EVENT_CONT)
141                         return ret;
142         } else if (e->header.type == PERF_RECORD_LOST) {
143                 struct {
144                         struct perf_event_header header;
145                         __u64 id;
146                         __u64 lost;
147                 } *lost = (void *) e;
148                 printf("lost %lld events\n", lost->lost);
149         } else {
150                 printf("unknown event type=%d size=%d\n",
151                        e->header.type, e->header.size);
152         }
153
154         return LIBBPF_PERF_EVENT_CONT;
155 }
156
157 int perf_event_poller(int fd, perf_event_print_fn output_fn)
158 {
159         enum bpf_perf_event_ret ret;
160         void *buf = NULL;
161         size_t len = 0;
162
163         for (;;) {
164                 perf_event_poll(fd);
165                 ret = bpf_perf_event_read_simple(header, page_cnt * page_size,
166                                                  page_size, &buf, &len,
167                                                  bpf_perf_event_print,
168                                                  output_fn);
169                 if (ret != LIBBPF_PERF_EVENT_CONT)
170                         break;
171         }
172         free(buf);
173
174         return ret;
175 }
176
177 int perf_event_poller_multi(int *fds, struct perf_event_mmap_page **headers,
178                             int num_fds, perf_event_print_fn output_fn)
179 {
180         enum bpf_perf_event_ret ret;
181         struct pollfd *pfds;
182         void *buf = NULL;
183         size_t len = 0;
184         int i;
185
186         pfds = calloc(num_fds, sizeof(*pfds));
187         if (!pfds)
188                 return LIBBPF_PERF_EVENT_ERROR;
189
190         for (i = 0; i < num_fds; i++) {
191                 pfds[i].fd = fds[i];
192                 pfds[i].events = POLLIN;
193         }
194
195         for (;;) {
196                 poll(pfds, num_fds, 1000);
197                 for (i = 0; i < num_fds; i++) {
198                         if (!pfds[i].revents)
199                                 continue;
200
201                         ret = bpf_perf_event_read_simple(headers[i],
202                                                          page_cnt * page_size,
203                                                          page_size, &buf, &len,
204                                                          bpf_perf_event_print,
205                                                          output_fn);
206                         if (ret != LIBBPF_PERF_EVENT_CONT)
207                                 break;
208                 }
209         }
210         free(buf);
211         free(pfds);
212
213         return ret;
214 }