|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <linux/filter.h> |
| 5 | +#include <linux/seccomp.h> |
| 6 | +#include <sys/prctl.h> |
| 7 | +#include <bpf/bpf.h> |
| 8 | +#include <bpf/libbpf.h> |
| 9 | +#include <sys/resource.h> |
| 10 | +#include "trace_helpers.h" |
| 11 | + |
| 12 | +int main(int argc, char *argv[]) |
| 13 | +{ |
| 14 | + struct bpf_link *link = NULL; |
| 15 | + struct bpf_program *prog; |
| 16 | + struct bpf_object *obj; |
| 17 | + int key, fd, progs_fd; |
| 18 | + const char *section; |
| 19 | + char filename[256]; |
| 20 | + |
| 21 | + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 22 | + obj = bpf_object__open_file(filename, NULL); |
| 23 | + if (libbpf_get_error(obj)) { |
| 24 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
| 25 | + return 0; |
| 26 | + } |
| 27 | + |
| 28 | + prog = bpf_object__find_program_by_name(obj, "mybpfprog"); |
| 29 | + if (!prog) { |
| 30 | + printf("finding a prog in obj file failed\n"); |
| 31 | + goto cleanup; |
| 32 | + } |
| 33 | + |
| 34 | + /* load BPF program */ |
| 35 | + if (bpf_object__load(obj)) { |
| 36 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
| 37 | + goto cleanup; |
| 38 | + } |
| 39 | + |
| 40 | + link = bpf_program__attach(prog); |
| 41 | + if (libbpf_get_error(link)) { |
| 42 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
| 43 | + link = NULL; |
| 44 | + goto cleanup; |
| 45 | + } |
| 46 | + |
| 47 | + progs_fd = bpf_object__find_map_fd_by_name(obj, "progs"); |
| 48 | + if (progs_fd < 0) { |
| 49 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
| 50 | + goto cleanup; |
| 51 | + } |
| 52 | + |
| 53 | + bpf_object__for_each_program(prog, obj) { |
| 54 | + section = bpf_program__section_name(prog); |
| 55 | + /* register only syscalls to PROG_ARRAY */ |
| 56 | + if (sscanf(section, "kprobe/%d", &key) != 1) |
| 57 | + continue; |
| 58 | + |
| 59 | + fd = bpf_program__fd(prog); |
| 60 | + bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY); |
| 61 | + } |
| 62 | + |
| 63 | + read_trace_pipe(); |
| 64 | + |
| 65 | +cleanup: |
| 66 | + bpf_link__destroy(link); |
| 67 | + bpf_object__close(obj); |
| 68 | + return 0; |
| 69 | +} |
0 commit comments