hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/samples/bpf/tracex6_user.c
....@@ -4,7 +4,6 @@
44 #include <assert.h>
55 #include <fcntl.h>
66 #include <linux/perf_event.h>
7
-#include <linux/bpf.h>
87 #include <sched.h>
98 #include <stdio.h>
109 #include <stdlib.h>
....@@ -15,11 +14,14 @@
1514 #include <sys/wait.h>
1615 #include <unistd.h>
1716
18
-#include "bpf_load.h"
1917 #include <bpf/bpf.h>
18
+#include <bpf/libbpf.h>
2019 #include "perf-sys.h"
2120
2221 #define SAMPLE_PERIOD 0x7fffffffffffffffULL
22
+
23
+/* counters, values, values2 */
24
+static int map_fd[3];
2325
2426 static void check_on_cpu(int cpu, struct perf_event_attr *attr)
2527 {
....@@ -174,16 +176,51 @@
174176 int main(int argc, char **argv)
175177 {
176178 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
179
+ struct bpf_link *links[2];
180
+ struct bpf_program *prog;
181
+ struct bpf_object *obj;
177182 char filename[256];
178
-
179
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
183
+ int i = 0;
180184
181185 setrlimit(RLIMIT_MEMLOCK, &r);
182
- if (load_bpf_file(filename)) {
183
- printf("%s", bpf_log_buf);
184
- return 1;
186
+
187
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
188
+ obj = bpf_object__open_file(filename, NULL);
189
+ if (libbpf_get_error(obj)) {
190
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
191
+ return 0;
192
+ }
193
+
194
+ /* load BPF program */
195
+ if (bpf_object__load(obj)) {
196
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
197
+ goto cleanup;
198
+ }
199
+
200
+ map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counters");
201
+ map_fd[1] = bpf_object__find_map_fd_by_name(obj, "values");
202
+ map_fd[2] = bpf_object__find_map_fd_by_name(obj, "values2");
203
+ if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) {
204
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
205
+ goto cleanup;
206
+ }
207
+
208
+ bpf_object__for_each_program(prog, obj) {
209
+ links[i] = bpf_program__attach(prog);
210
+ if (libbpf_get_error(links[i])) {
211
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
212
+ links[i] = NULL;
213
+ goto cleanup;
214
+ }
215
+ i++;
185216 }
186217
187218 test_bpf_perf_event();
219
+
220
+cleanup:
221
+ for (i--; i >= 0; i--)
222
+ bpf_link__destroy(links[i]);
223
+
224
+ bpf_object__close(obj);
188225 return 0;
189226 }