hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/samples/bpf/offwaketime_user.c
....@@ -1,25 +1,22 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* Copyright (c) 2016 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 <stdlib.h>
107 #include <signal.h>
11
-#include <linux/bpf.h>
12
-#include <string.h>
138 #include <linux/perf_event.h>
149 #include <errno.h>
15
-#include <assert.h>
1610 #include <stdbool.h>
1711 #include <sys/resource.h>
18
-#include "libbpf.h"
19
-#include "bpf_load.h"
12
+#include <bpf/libbpf.h>
13
+#include <bpf/bpf.h>
2014 #include "trace_helpers.h"
2115
2216 #define PRINT_RAW_ADDR 0
17
+
18
+/* counts, stackmap */
19
+static int map_fd[2];
2320
2421 static void print_ksym(__u64 addr)
2522 {
....@@ -28,6 +25,11 @@
2825 if (!addr)
2926 return;
3027 sym = ksym_search(addr);
28
+ if (!sym) {
29
+ printf("ksym not found. Is kallsyms loaded?\n");
30
+ return;
31
+ }
32
+
3133 if (PRINT_RAW_ADDR)
3234 printf("%s/%llx;", sym->name, addr);
3335 else
....@@ -50,14 +52,14 @@
5052 int i;
5153
5254 printf("%s;", key->target);
53
- if (bpf_map_lookup_elem(map_fd[3], &key->tret, ip) != 0) {
55
+ if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
5456 printf("---;");
5557 } else {
5658 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
5759 print_ksym(ip[i]);
5860 }
5961 printf("-;");
60
- if (bpf_map_lookup_elem(map_fd[3], &key->wret, ip) != 0) {
62
+ if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
6163 printf("---;");
6264 } else {
6365 for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
....@@ -94,23 +96,54 @@
9496 int main(int argc, char **argv)
9597 {
9698 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
99
+ struct bpf_object *obj = NULL;
100
+ struct bpf_link *links[2];
101
+ struct bpf_program *prog;
102
+ int delay = 1, i = 0;
97103 char filename[256];
98
- int delay = 1;
99104
100
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
101
- setrlimit(RLIMIT_MEMLOCK, &r);
102
-
103
- signal(SIGINT, int_exit);
104
- signal(SIGTERM, int_exit);
105
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
106
+ perror("setrlimit(RLIMIT_MEMLOCK)");
107
+ return 1;
108
+ }
105109
106110 if (load_kallsyms()) {
107111 printf("failed to process /proc/kallsyms\n");
108112 return 2;
109113 }
110114
111
- if (load_bpf_file(filename)) {
112
- printf("%s", bpf_log_buf);
113
- return 1;
115
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
116
+ obj = bpf_object__open_file(filename, NULL);
117
+ if (libbpf_get_error(obj)) {
118
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
119
+ obj = NULL;
120
+ goto cleanup;
121
+ }
122
+
123
+ /* load BPF program */
124
+ if (bpf_object__load(obj)) {
125
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
126
+ goto cleanup;
127
+ }
128
+
129
+ map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
130
+ map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
131
+ if (map_fd[0] < 0 || map_fd[1] < 0) {
132
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
133
+ goto cleanup;
134
+ }
135
+
136
+ signal(SIGINT, int_exit);
137
+ signal(SIGTERM, int_exit);
138
+
139
+ bpf_object__for_each_program(prog, obj) {
140
+ links[i] = bpf_program__attach(prog);
141
+ if (libbpf_get_error(links[i])) {
142
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
143
+ links[i] = NULL;
144
+ goto cleanup;
145
+ }
146
+ i++;
114147 }
115148
116149 if (argc > 1)
....@@ -118,5 +151,10 @@
118151 sleep(delay);
119152 print_stacks(map_fd[0]);
120153
154
+cleanup:
155
+ for (i--; i >= 0; i--)
156
+ bpf_link__destroy(links[i]);
157
+
158
+ bpf_object__close(obj);
121159 return 0;
122160 }