1 // SPDX-License-Identifier: GPL-2.0
6 #include <sys/resource.h>
7 #include <bpf/libbpf.h>
9 #include "trace_helpers.h"
11 int main(int ac, char **argv)
13 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
14 char filename[256], symbol[256];
15 struct bpf_object *obj = NULL;
16 struct bpf_link *links[20];
17 long key, next_key, value;
18 struct bpf_program *prog;
23 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
24 perror("setrlimit(RLIMIT_MEMLOCK)");
28 if (load_kallsyms()) {
29 printf("failed to process /proc/kallsyms\n");
33 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
34 obj = bpf_object__open_file(filename, NULL);
35 if (libbpf_get_error(obj)) {
36 fprintf(stderr, "ERROR: opening BPF object file failed\n");
41 /* load BPF program */
42 if (bpf_object__load(obj)) {
43 fprintf(stderr, "ERROR: loading BPF object file failed\n");
47 map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
49 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
53 bpf_object__for_each_program(prog, obj) {
54 section = bpf_program__section_name(prog);
55 if (sscanf(section, "kprobe/%s", symbol) != 1)
58 /* Attach prog only when symbol exists */
59 if (ksym_get_addr(symbol)) {
60 links[j] = bpf_program__attach(prog);
61 if (libbpf_get_error(links[j])) {
62 fprintf(stderr, "bpf_program__attach failed\n");
70 for (i = 0; i < 5; i++) {
72 printf("kprobing funcs:");
73 while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
74 bpf_map_lookup_elem(map_fd, &next_key, &value);
75 assert(next_key == value);
76 sym = ksym_search(value);
79 printf("ksym not found. Is kallsyms loaded?\n");
83 printf(" %s", sym->name);
88 while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0)
89 bpf_map_delete_elem(map_fd, &next_key);
94 for (j--; j >= 0; j--)
95 bpf_link__destroy(links[j]);
97 bpf_object__close(obj);