Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / bpf / trace_helpers.c
blob82922f13dcd3a9c4ad3d2b412e17c3182e285128
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"
13 #define MAX_SYMS 300000
14 static struct ksym syms[MAX_SYMS];
15 static int sym_cnt;
17 static int ksym_cmp(const void *p1, const void *p2)
19 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
22 int load_kallsyms(void)
24 FILE *f = fopen("/proc/kallsyms", "r");
25 char func[256], buf[256];
26 char symbol;
27 void *addr;
28 int i = 0;
30 if (!f)
31 return -ENOENT;
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++;
44 fclose(f);
45 sym_cnt = i;
46 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
47 return 0;
50 struct ksym *ksym_search(long key)
52 int start = 0, end = sym_cnt;
53 int result;
55 /* kallsyms not loaded. return NULL */
56 if (sym_cnt <= 0)
57 return NULL;
59 while (start < end) {
60 size_t mid = start + (end - start) / 2;
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];
71 if (start >= 1 && syms[start - 1].addr < key &&
72 key < syms[start].addr)
73 /* valid ksym */
74 return &syms[start - 1];
76 /* out of range. return _stext */
77 return &syms[0];
80 long ksym_get_addr(const char *name)
82 int i;
84 for (i = 0; i < sym_cnt; i++) {
85 if (strcmp(syms[i].name, name) == 0)
86 return syms[i].addr;
89 return 0;
92 static int page_size;
93 static int page_cnt = 8;
94 static struct perf_event_mmap_page *header;
96 int perf_event_mmap_header(int fd, struct perf_event_mmap_page **header)
98 void *base;
99 int mmap_size;
101 page_size = getpagesize();
102 mmap_size = page_size * (page_cnt + 1);
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;
110 *header = base;
111 return 0;
114 int perf_event_mmap(int fd)
116 return perf_event_mmap_header(fd, &header);
119 static int perf_event_poll(int fd)
121 struct pollfd pfd = { .fd = fd, .events = POLLIN };
123 return poll(&pfd, 1, 1000);
126 struct perf_event_sample {
127 struct perf_event_header header;
128 __u32 size;
129 char data[];
132 static enum bpf_perf_event_ret bpf_perf_event_print(void *event, void *priv)
134 struct perf_event_sample *e = event;
135 perf_event_print_fn fn = priv;
136 int ret;
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);
154 return LIBBPF_PERF_EVENT_CONT;
157 int perf_event_poller(int fd, perf_event_print_fn output_fn)
159 enum bpf_perf_event_ret ret;
160 void *buf = NULL;
161 size_t len = 0;
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;
172 free(buf);
174 return ret;
177 int perf_event_poller_multi(int *fds, struct perf_event_mmap_page **headers,
178 int num_fds, perf_event_print_fn output_fn)
180 enum bpf_perf_event_ret ret;
181 struct pollfd *pfds;
182 void *buf = NULL;
183 size_t len = 0;
184 int i;
186 pfds = calloc(num_fds, sizeof(*pfds));
187 if (!pfds)
188 return LIBBPF_PERF_EVENT_ERROR;
190 for (i = 0; i < num_fds; i++) {
191 pfds[i].fd = fds[i];
192 pfds[i].events = POLLIN;
195 for (;;) {
196 poll(pfds, num_fds, 1000);
197 for (i = 0; i < num_fds; i++) {
198 if (!pfds[i].revents)
199 continue;
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;
210 free(buf);
211 free(pfds);
213 return ret;