hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/samples/bpf/syscall_tp_user.c
....@@ -1,23 +1,16 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* 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.
63 */
74 #include <stdio.h>
85 #include <unistd.h>
96 #include <fcntl.h>
107 #include <stdlib.h>
11
-#include <signal.h>
12
-#include <linux/bpf.h>
138 #include <string.h>
149 #include <linux/perf_event.h>
1510 #include <errno.h>
16
-#include <assert.h>
17
-#include <stdbool.h>
1811 #include <sys/resource.h>
12
+#include <bpf/libbpf.h>
1913 #include <bpf/bpf.h>
20
-#include "bpf_load.h"
2114
2215 /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
2316 * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
....@@ -52,16 +45,44 @@
5245
5346 static int test(char *filename, int num_progs)
5447 {
55
- int i, fd, map0_fds[num_progs], map1_fds[num_progs];
48
+ int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0;
49
+ struct bpf_link *links[num_progs * 4];
50
+ struct bpf_object *objs[num_progs];
51
+ struct bpf_program *prog;
5652
5753 for (i = 0; i < num_progs; i++) {
58
- if (load_bpf_file(filename)) {
59
- fprintf(stderr, "%s", bpf_log_buf);
60
- return 1;
54
+ objs[i] = bpf_object__open_file(filename, NULL);
55
+ if (libbpf_get_error(objs[i])) {
56
+ fprintf(stderr, "opening BPF object file failed\n");
57
+ objs[i] = NULL;
58
+ goto cleanup;
6159 }
62
- printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]);
63
- map0_fds[i] = map_fd[0];
64
- map1_fds[i] = map_fd[1];
60
+
61
+ /* load BPF program */
62
+ if (bpf_object__load(objs[i])) {
63
+ fprintf(stderr, "loading BPF object file failed\n");
64
+ goto cleanup;
65
+ }
66
+
67
+ map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
68
+ "enter_open_map");
69
+ map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
70
+ "exit_open_map");
71
+ if (map0_fds[i] < 0 || map1_fds[i] < 0) {
72
+ fprintf(stderr, "finding a map in obj file failed\n");
73
+ goto cleanup;
74
+ }
75
+
76
+ bpf_object__for_each_program(prog, objs[i]) {
77
+ links[j] = bpf_program__attach(prog);
78
+ if (libbpf_get_error(links[j])) {
79
+ fprintf(stderr, "bpf_program__attach failed\n");
80
+ links[j] = NULL;
81
+ goto cleanup;
82
+ }
83
+ j++;
84
+ }
85
+ printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]);
6586 }
6687
6788 /* current load_bpf_file has perf_event_open default pid = -1
....@@ -83,6 +104,12 @@
83104 verify_map(map1_fds[i]);
84105 }
85106
107
+cleanup:
108
+ for (j--; j >= 0; j--)
109
+ bpf_link__destroy(links[j]);
110
+
111
+ for (i--; i >= 0; i--)
112
+ bpf_object__close(objs[i]);
86113 return 0;
87114 }
88115