hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/samples/bpf/tracex4_user.c
....@@ -1,8 +1,5 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
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 <stdlib.h>
....@@ -11,11 +8,10 @@
118 #include <stdbool.h>
129 #include <string.h>
1310 #include <time.h>
14
-#include <linux/bpf.h>
1511 #include <sys/resource.h>
1612
1713 #include <bpf/bpf.h>
18
-#include "bpf_load.h"
14
+#include <bpf/libbpf.h>
1915
2016 struct pair {
2117 long long val;
....@@ -39,8 +35,8 @@
3935 key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
4036
4137 key = -1;
42
- while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
43
- bpf_map_lookup_elem(map_fd[0], &next_key, &v);
38
+ while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
39
+ bpf_map_lookup_elem(fd, &next_key, &v);
4440 key = next_key;
4541 if (val - v.val < 1000000000ll)
4642 /* object was allocated more then 1 sec ago */
....@@ -53,25 +49,55 @@
5349 int main(int ac, char **argv)
5450 {
5551 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
52
+ struct bpf_link *links[2];
53
+ struct bpf_program *prog;
54
+ struct bpf_object *obj;
5655 char filename[256];
57
- int i;
58
-
59
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
56
+ int map_fd, i, j = 0;
6057
6158 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
6259 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
6360 return 1;
6461 }
6562
66
- if (load_bpf_file(filename)) {
67
- printf("%s", bpf_log_buf);
68
- return 1;
63
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
64
+ obj = bpf_object__open_file(filename, NULL);
65
+ if (libbpf_get_error(obj)) {
66
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
67
+ return 0;
68
+ }
69
+
70
+ /* load BPF program */
71
+ if (bpf_object__load(obj)) {
72
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
73
+ goto cleanup;
74
+ }
75
+
76
+ map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
77
+ if (map_fd < 0) {
78
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
79
+ goto cleanup;
80
+ }
81
+
82
+ bpf_object__for_each_program(prog, obj) {
83
+ links[j] = bpf_program__attach(prog);
84
+ if (libbpf_get_error(links[j])) {
85
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
86
+ links[j] = NULL;
87
+ goto cleanup;
88
+ }
89
+ j++;
6990 }
7091
7192 for (i = 0; ; i++) {
72
- print_old_objects(map_fd[1]);
93
+ print_old_objects(map_fd);
7394 sleep(1);
7495 }
7596
97
+cleanup:
98
+ for (j--; j >= 0; j--)
99
+ bpf_link__destroy(links[j]);
100
+
101
+ bpf_object__close(obj);
76102 return 0;
77103 }