blob: 99e5a2f63e7629e701cf7663d33a096bab4522c5 [file] [log] [blame]
Alexei Starovoitov249b8122014-12-01 15:06:37 -08001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <libelf.h>
6#include <gelf.h>
7#include <errno.h>
8#include <unistd.h>
9#include <string.h>
10#include <stdbool.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070011#include <stdlib.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080012#include <linux/bpf.h>
13#include <linux/filter.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070014#include <linux/perf_event.h>
15#include <sys/syscall.h>
16#include <sys/ioctl.h>
17#include <sys/mman.h>
18#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070019#include <ctype.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080020#include "libbpf.h"
21#include "bpf_helpers.h"
22#include "bpf_load.h"
23
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070024#define DEBUGFS "/sys/kernel/debug/tracing/"
25
Alexei Starovoitov249b8122014-12-01 15:06:37 -080026static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070027static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080028static bool processed_sec[128];
29int map_fd[MAX_MAPS];
30int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070031int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080032int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070033int prog_array_fd = -1;
34
35static int populate_prog_array(const char *event, int prog_fd)
36{
37 int ind = atoi(event), err;
38
39 err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
40 if (err < 0) {
41 printf("failed to store prog_fd in prog_array\n");
42 return -1;
43 }
44 return 0;
45}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080046
47static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
48{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080049 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070050 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
51 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070052 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070053 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070054 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070055 enum bpf_prog_type prog_type;
56 char buf[256];
57 int fd, efd, err, id;
58 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080059
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070060 attr.type = PERF_TYPE_TRACEPOINT;
61 attr.sample_type = PERF_SAMPLE_RAW;
62 attr.sample_period = 1;
63 attr.wakeup_events = 1;
64
65 if (is_socket) {
66 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
67 } else if (is_kprobe || is_kretprobe) {
68 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070069 } else if (is_tracepoint) {
70 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -070071 } else if (is_xdp) {
72 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070073 } else if (is_perf_event) {
74 prog_type = BPF_PROG_TYPE_PERF_EVENT;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070075 } else {
76 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -080077 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070078 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -080079
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070080 fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
81 if (fd < 0) {
82 printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
83 return -1;
84 }
85
86 prog_fd[prog_cnt++] = fd;
87
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070088 if (is_xdp || is_perf_event)
Brenden Blanco86af8b42016-07-19 12:16:51 -070089 return 0;
90
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070091 if (is_socket) {
92 event += 6;
93 if (*event != '/')
94 return 0;
95 event++;
96 if (!isdigit(*event)) {
97 printf("invalid prog number\n");
98 return -1;
99 }
100 return populate_prog_array(event, fd);
101 }
102
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700103 if (is_kprobe || is_kretprobe) {
104 if (is_kprobe)
105 event += 7;
106 else
107 event += 10;
108
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700109 if (*event == 0) {
110 printf("event name cannot be empty\n");
111 return -1;
112 }
113
114 if (isdigit(*event))
115 return populate_prog_array(event, fd);
116
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700117 snprintf(buf, sizeof(buf),
118 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
119 is_kprobe ? 'p' : 'r', event, event);
120 err = system(buf);
121 if (err < 0) {
122 printf("failed to create kprobe '%s' error '%s'\n",
123 event, strerror(errno));
124 return -1;
125 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700126
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700127 strcpy(buf, DEBUGFS);
128 strcat(buf, "events/kprobes/");
129 strcat(buf, event);
130 strcat(buf, "/id");
131 } else if (is_tracepoint) {
132 event += 11;
133
134 if (*event == 0) {
135 printf("event name cannot be empty\n");
136 return -1;
137 }
138 strcpy(buf, DEBUGFS);
139 strcat(buf, "events/");
140 strcat(buf, event);
141 strcat(buf, "/id");
142 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700143
144 efd = open(buf, O_RDONLY, 0);
145 if (efd < 0) {
146 printf("failed to open event %s\n", event);
147 return -1;
148 }
149
150 err = read(efd, buf, sizeof(buf));
151 if (err < 0 || err >= sizeof(buf)) {
152 printf("read from '%s' failed '%s'\n", event, strerror(errno));
153 return -1;
154 }
155
156 close(efd);
157
158 buf[err] = 0;
159 id = atoi(buf);
160 attr.config = id;
161
162 efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
163 if (efd < 0) {
164 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
165 return -1;
166 }
167 event_fd[prog_cnt - 1] = efd;
168 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
169 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
170
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800171 return 0;
172}
173
174static int load_maps(struct bpf_map_def *maps, int len)
175{
176 int i;
177
178 for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
179
180 map_fd[i] = bpf_create_map(maps[i].type,
181 maps[i].key_size,
182 maps[i].value_size,
Alexei Starovoitov89b97602016-03-07 21:57:20 -0800183 maps[i].max_entries,
184 maps[i].map_flags);
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800185 if (map_fd[i] < 0) {
186 printf("failed to create a map: %d %s\n",
187 errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800188 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800189 }
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700190
191 if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
192 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800193 }
194 return 0;
195}
196
197static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
198 GElf_Shdr *shdr, Elf_Data **data)
199{
200 Elf_Scn *scn;
201
202 scn = elf_getscn(elf, i);
203 if (!scn)
204 return 1;
205
206 if (gelf_getshdr(scn, shdr) != shdr)
207 return 2;
208
209 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
210 if (!*shname || !shdr->sh_size)
211 return 3;
212
213 *data = elf_getdata(scn, 0);
214 if (!*data || elf_getdata(scn, *data) != NULL)
215 return 4;
216
217 return 0;
218}
219
220static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
221 GElf_Shdr *shdr, struct bpf_insn *insn)
222{
223 int i, nrels;
224
225 nrels = shdr->sh_size / shdr->sh_entsize;
226
227 for (i = 0; i < nrels; i++) {
228 GElf_Sym sym;
229 GElf_Rel rel;
230 unsigned int insn_idx;
231
232 gelf_getrel(data, i, &rel);
233
234 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
235
236 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
237
238 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
239 printf("invalid relo for insn[%d].code 0x%x\n",
240 insn_idx, insn[insn_idx].code);
241 return 1;
242 }
243 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
244 insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
245 }
246
247 return 0;
248}
249
250int load_bpf_file(char *path)
251{
252 int fd, i;
253 Elf *elf;
254 GElf_Ehdr ehdr;
255 GElf_Shdr shdr, shdr_prog;
256 Elf_Data *data, *data_prog, *symbols = NULL;
257 char *shname, *shname_prog;
258
259 if (elf_version(EV_CURRENT) == EV_NONE)
260 return 1;
261
262 fd = open(path, O_RDONLY, 0);
263 if (fd < 0)
264 return 1;
265
266 elf = elf_begin(fd, ELF_C_READ, NULL);
267
268 if (!elf)
269 return 1;
270
271 if (gelf_getehdr(elf, &ehdr) != &ehdr)
272 return 1;
273
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700274 /* clear all kprobes */
275 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
276
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800277 /* scan over all elf sections to get license and map info */
278 for (i = 1; i < ehdr.e_shnum; i++) {
279
280 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
281 continue;
282
283 if (0) /* helpful for llvm debugging */
284 printf("section %d:%s data %p size %zd link %d flags %d\n",
285 i, shname, data->d_buf, data->d_size,
286 shdr.sh_link, (int) shdr.sh_flags);
287
288 if (strcmp(shname, "license") == 0) {
289 processed_sec[i] = true;
290 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700291 } else if (strcmp(shname, "version") == 0) {
292 processed_sec[i] = true;
293 if (data->d_size != sizeof(int)) {
294 printf("invalid size of version section %zd\n",
295 data->d_size);
296 return 1;
297 }
298 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800299 } else if (strcmp(shname, "maps") == 0) {
300 processed_sec[i] = true;
301 if (load_maps(data->d_buf, data->d_size))
302 return 1;
303 } else if (shdr.sh_type == SHT_SYMTAB) {
304 symbols = data;
305 }
306 }
307
308 /* load programs that need map fixup (relocations) */
309 for (i = 1; i < ehdr.e_shnum; i++) {
310
311 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
312 continue;
313 if (shdr.sh_type == SHT_REL) {
314 struct bpf_insn *insns;
315
316 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
317 &shdr_prog, &data_prog))
318 continue;
319
320 insns = (struct bpf_insn *) data_prog->d_buf;
321
322 processed_sec[shdr.sh_info] = true;
323 processed_sec[i] = true;
324
325 if (parse_relo_and_apply(data, symbols, &shdr, insns))
326 continue;
327
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700328 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
329 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700330 memcmp(shname_prog, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700331 memcmp(shname_prog, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700332 memcmp(shname_prog, "perf_event", 10) == 0 ||
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800333 memcmp(shname_prog, "socket", 6) == 0)
334 load_and_attach(shname_prog, insns, data_prog->d_size);
335 }
336 }
337
338 /* load programs that don't use maps */
339 for (i = 1; i < ehdr.e_shnum; i++) {
340
341 if (processed_sec[i])
342 continue;
343
344 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
345 continue;
346
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700347 if (memcmp(shname, "kprobe/", 7) == 0 ||
348 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700349 memcmp(shname, "tracepoint/", 11) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700350 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700351 memcmp(shname, "perf_event", 10) == 0 ||
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800352 memcmp(shname, "socket", 6) == 0)
353 load_and_attach(shname, data->d_buf, data->d_size);
354 }
355
356 close(fd);
357 return 0;
358}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700359
360void read_trace_pipe(void)
361{
362 int trace_fd;
363
364 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
365 if (trace_fd < 0)
366 return;
367
368 while (1) {
369 static char buf[4096];
370 ssize_t sz;
371
Chang-Hsien Tsai95a21e72019-05-19 09:05:44 +0000372 sz = read(trace_fd, buf, sizeof(buf) - 1);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700373 if (sz > 0) {
374 buf[sz] = 0;
375 puts(buf);
376 }
377 }
378}
Alexei Starovoitov3622e7e2016-03-07 21:57:19 -0800379
380#define MAX_SYMS 300000
381static struct ksym syms[MAX_SYMS];
382static int sym_cnt;
383
384static int ksym_cmp(const void *p1, const void *p2)
385{
386 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
387}
388
389int load_kallsyms(void)
390{
391 FILE *f = fopen("/proc/kallsyms", "r");
392 char func[256], buf[256];
393 char symbol;
394 void *addr;
395 int i = 0;
396
397 if (!f)
398 return -ENOENT;
399
400 while (!feof(f)) {
401 if (!fgets(buf, sizeof(buf), f))
402 break;
403 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
404 break;
405 if (!addr)
406 continue;
407 syms[i].addr = (long) addr;
408 syms[i].name = strdup(func);
409 i++;
410 }
411 sym_cnt = i;
412 qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
413 return 0;
414}
415
416struct ksym *ksym_search(long key)
417{
418 int start = 0, end = sym_cnt;
419 int result;
420
421 while (start < end) {
422 size_t mid = start + (end - start) / 2;
423
424 result = key - syms[mid].addr;
425 if (result < 0)
426 end = mid;
427 else if (result > 0)
428 start = mid + 1;
429 else
430 return &syms[mid];
431 }
432
433 if (start >= 1 && syms[start - 1].addr < key &&
434 key < syms[start].addr)
435 /* valid ksym */
436 return &syms[start - 1];
437
438 /* out of range. return _stext */
439 return &syms[0];
440}