1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
11 #include <linux/perf_event.h>
15 #include <sys/resource.h>
19 /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
20 * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
23 static void usage(const char *cmd
)
25 printf("USAGE: %s [-i num_progs] [-h]\n", cmd
);
26 printf(" -i num_progs # number of progs of the test\n");
27 printf(" -h # help\n");
30 static void verify_map(int map_id
)
35 if (bpf_map_lookup_elem(map_id
, &key
, &val
) != 0) {
36 fprintf(stderr
, "map_lookup failed: %s\n", strerror(errno
));
40 fprintf(stderr
, "failed: map #%d returns value 0\n", map_id
);
44 if (bpf_map_update_elem(map_id
, &key
, &val
, BPF_ANY
) != 0) {
45 fprintf(stderr
, "map_update failed: %s\n", strerror(errno
));
50 static int test(char *filename
, int num_progs
)
52 int i
, fd
, map0_fds
[num_progs
], map1_fds
[num_progs
];
54 for (i
= 0; i
< num_progs
; i
++) {
55 if (load_bpf_file(filename
)) {
56 fprintf(stderr
, "%s", bpf_log_buf
);
59 printf("prog #%d: map ids %d %d\n", i
, map_fd
[0], map_fd
[1]);
60 map0_fds
[i
] = map_fd
[0];
61 map1_fds
[i
] = map_fd
[1];
64 /* current load_bpf_file has perf_event_open default pid = -1
65 * and cpu = 0, which permits attached bpf execution on
66 * all cpus for all pid's. bpf program execution ignores
69 /* trigger some "open" operations */
70 fd
= open(filename
, O_RDONLY
);
72 fprintf(stderr
, "open failed: %s\n", strerror(errno
));
78 for (i
= 0; i
< num_progs
; i
++) {
79 verify_map(map0_fds
[i
]);
80 verify_map(map1_fds
[i
]);
86 int main(int argc
, char **argv
)
88 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
89 int opt
, num_progs
= 1;
92 while ((opt
= getopt(argc
, argv
, "i:h")) != -1) {
95 num_progs
= atoi(optarg
);
104 setrlimit(RLIMIT_MEMLOCK
, &r
);
105 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
107 return test(filename
, num_progs
);