hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/samples/bpf/spintest_user.c
....@@ -1,52 +1,99 @@
11 // SPDX-License-Identifier: GPL-2.0
22 #include <stdio.h>
33 #include <unistd.h>
4
-#include <linux/bpf.h>
54 #include <string.h>
65 #include <assert.h>
76 #include <sys/resource.h>
8
-#include "libbpf.h"
9
-#include "bpf_load.h"
7
+#include <bpf/libbpf.h>
8
+#include <bpf/bpf.h>
109 #include "trace_helpers.h"
1110
1211 int main(int ac, char **argv)
1312 {
1413 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
14
+ char filename[256], symbol[256];
15
+ struct bpf_object *obj = NULL;
16
+ struct bpf_link *links[20];
1517 long key, next_key, value;
16
- char filename[256];
18
+ struct bpf_program *prog;
19
+ int map_fd, i, j = 0;
20
+ const char *section;
1721 struct ksym *sym;
18
- int i;
1922
20
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
21
- setrlimit(RLIMIT_MEMLOCK, &r);
23
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
24
+ perror("setrlimit(RLIMIT_MEMLOCK)");
25
+ return 1;
26
+ }
2227
2328 if (load_kallsyms()) {
2429 printf("failed to process /proc/kallsyms\n");
2530 return 2;
2631 }
2732
28
- if (load_bpf_file(filename)) {
29
- printf("%s", bpf_log_buf);
30
- return 1;
33
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
34
+ obj = bpf_object__open_file(filename, NULL);
35
+ if (libbpf_get_error(obj)) {
36
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
37
+ obj = NULL;
38
+ goto cleanup;
39
+ }
40
+
41
+ /* load BPF program */
42
+ if (bpf_object__load(obj)) {
43
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
44
+ goto cleanup;
45
+ }
46
+
47
+ map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
48
+ if (map_fd < 0) {
49
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
50
+ goto cleanup;
51
+ }
52
+
53
+ bpf_object__for_each_program(prog, obj) {
54
+ section = bpf_program__section_name(prog);
55
+ if (sscanf(section, "kprobe/%s", symbol) != 1)
56
+ continue;
57
+
58
+ /* Attach prog only when symbol exists */
59
+ if (ksym_get_addr(symbol)) {
60
+ links[j] = bpf_program__attach(prog);
61
+ if (libbpf_get_error(links[j])) {
62
+ fprintf(stderr, "bpf_program__attach failed\n");
63
+ links[j] = NULL;
64
+ goto cleanup;
65
+ }
66
+ j++;
67
+ }
3168 }
3269
3370 for (i = 0; i < 5; i++) {
3471 key = 0;
3572 printf("kprobing funcs:");
36
- while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
37
- bpf_map_lookup_elem(map_fd[0], &next_key, &value);
73
+ while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
74
+ bpf_map_lookup_elem(map_fd, &next_key, &value);
3875 assert(next_key == value);
3976 sym = ksym_search(value);
40
- printf(" %s", sym->name);
4177 key = next_key;
78
+ if (!sym) {
79
+ printf("ksym not found. Is kallsyms loaded?\n");
80
+ continue;
81
+ }
82
+
83
+ printf(" %s", sym->name);
4284 }
4385 if (key)
4486 printf("\n");
4587 key = 0;
46
- while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0)
47
- bpf_map_delete_elem(map_fd[0], &next_key);
88
+ while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0)
89
+ bpf_map_delete_elem(map_fd, &next_key);
4890 sleep(1);
4991 }
5092
93
+cleanup:
94
+ for (j--; j >= 0; j--)
95
+ bpf_link__destroy(links[j]);
96
+
97
+ bpf_object__close(obj);
5198 return 0;
5299 }