1 /* Copyright (c) 2017 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <linux/bpf.h>
13 #include <string.h>
14 #include <linux/perf_event.h>
15 #include <errno.h>
16 #include <assert.h>
17 #include <stdbool.h>
18 #include <sys/resource.h>
19 #include "libbpf.h"
20 #include "bpf_load.h"
21
22 /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
23 * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
24 */
25
verify_map(int map_id)26 static void verify_map(int map_id)
27 {
28 __u32 key = 0;
29 __u32 val;
30
31 if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
32 fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
33 return;
34 }
35 if (val == 0)
36 fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
37 }
38
main(int argc,char ** argv)39 int main(int argc, char **argv)
40 {
41 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
42 char filename[256];
43 int fd;
44
45 setrlimit(RLIMIT_MEMLOCK, &r);
46 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
47
48 if (load_bpf_file(filename)) {
49 fprintf(stderr, "%s", bpf_log_buf);
50 return 1;
51 }
52
53 /* current load_bpf_file has perf_event_open default pid = -1
54 * and cpu = 0, which permits attached bpf execution on
55 * all cpus for all pid's. bpf program execution ignores
56 * cpu affinity.
57 */
58 /* trigger some "open" operations */
59 fd = open(filename, O_RDONLY);
60 if (fd < 0) {
61 fprintf(stderr, "open failed: %s\n", strerror(errno));
62 return 1;
63 }
64 close(fd);
65
66 /* verify the map */
67 verify_map(map_fd[0]);
68 verify_map(map_fd[1]);
69
70 return 0;
71 }
72