1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/bpf.h>
12 #include <sys/ioctl.h>
13 #include <sys/types.h>
15 #include <linux/perf_event.h>
18 #include <bpf/libbpf.h>
21 #include "trace_helpers.h"
23 static struct bpf_program *progs[2];
24 static struct bpf_link *links[2];
26 #define CHECK_PERROR_RET(condition) ({ \
27 int __ret = !!(condition); \
29 printf("FAIL: %s:\n", __func__); \
35 #define CHECK_AND_RET(condition) ({ \
36 int __ret = !!(condition); \
41 static __u64 ptr_to_u64(void *ptr)
43 return (__u64) (unsigned long) ptr;
46 #define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type"
47 static int bpf_find_probe_type(const char *event_type)
52 ret = snprintf(buf, sizeof(buf), PMU_TYPE_FILE, event_type);
53 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
55 fd = open(buf, O_RDONLY);
56 CHECK_PERROR_RET(fd < 0);
58 ret = read(fd, buf, sizeof(buf));
60 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
63 ret = (int)strtol(buf, NULL, 10);
64 CHECK_PERROR_RET(errno);
68 #define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe"
69 static int bpf_get_retprobe_bit(const char *event_type)
74 ret = snprintf(buf, sizeof(buf), PMU_RETPROBE_FILE, event_type);
75 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
77 fd = open(buf, O_RDONLY);
78 CHECK_PERROR_RET(fd < 0);
80 ret = read(fd, buf, sizeof(buf));
82 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
83 CHECK_PERROR_RET(strlen(buf) < strlen("config:"));
86 ret = (int)strtol(buf + strlen("config:"), NULL, 10);
87 CHECK_PERROR_RET(errno);
91 static int test_debug_fs_kprobe(int link_idx, const char *fn_name,
92 __u32 expected_fd_type)
94 __u64 probe_offset, probe_addr;
95 __u32 len, prog_id, fd_type;
100 event_fd = bpf_link__fd(links[link_idx]);
101 err = bpf_task_fd_query(getpid(), event_fd, 0, buf, &len,
102 &prog_id, &fd_type, &probe_offset,
105 printf("FAIL: %s, for event_fd idx %d, fn_name %s\n",
106 __func__, link_idx, fn_name);
110 if (strcmp(buf, fn_name) != 0 ||
111 fd_type != expected_fd_type ||
112 probe_offset != 0x0 || probe_addr != 0x0) {
113 printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n",
115 printf("buf: %s, fd_type: %u, probe_offset: 0x%llx,"
116 " probe_addr: 0x%llx\n",
117 buf, fd_type, probe_offset, probe_addr);
123 static int test_nondebug_fs_kuprobe_common(const char *event_type,
124 const char *name, __u64 offset, __u64 addr, bool is_return,
125 char *buf, __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
126 __u64 *probe_offset, __u64 *probe_addr)
128 int is_return_bit = bpf_get_retprobe_bit(event_type);
129 int type = bpf_find_probe_type(event_type);
130 struct perf_event_attr attr = {};
131 struct bpf_link *link;
134 if (type < 0 || is_return_bit < 0) {
135 printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n",
136 __func__, type, is_return_bit);
140 attr.sample_period = 1;
141 attr.wakeup_events = 1;
143 attr.config |= 1 << is_return_bit;
146 attr.config1 = ptr_to_u64((void *)name);
147 attr.config2 = offset;
152 attr.size = sizeof(attr);
155 fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
156 link = bpf_program__attach_perf_event(progs[0], fd);
157 if (libbpf_get_error(link)) {
158 printf("ERROR: bpf_program__attach_perf_event failed\n");
164 CHECK_PERROR_RET(bpf_task_fd_query(getpid(), fd, 0, buf, buf_len,
165 prog_id, fd_type, probe_offset, probe_addr) < 0);
169 bpf_link__destroy(link);
173 static int test_nondebug_fs_probe(const char *event_type, const char *name,
174 __u64 offset, __u64 addr, bool is_return,
175 __u32 expected_fd_type,
176 __u32 expected_ret_fd_type,
177 char *buf, __u32 buf_len)
179 __u64 probe_offset, probe_addr;
180 __u32 prog_id, fd_type;
183 err = test_nondebug_fs_kuprobe_common(event_type, name,
184 offset, addr, is_return,
185 buf, &buf_len, &prog_id,
186 &fd_type, &probe_offset,
190 "for name %s, offset 0x%llx, addr 0x%llx, is_return %d\n",
191 __func__, name ? name : "", offset, addr, is_return);
195 if ((is_return && fd_type != expected_ret_fd_type) ||
196 (!is_return && fd_type != expected_fd_type)) {
197 printf("FAIL: %s, incorrect fd_type %u\n",
202 if (strcmp(name, buf) != 0) {
203 printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
206 if (probe_offset != offset) {
207 printf("FAIL: %s, incorrect probe_offset 0x%llx\n",
208 __func__, probe_offset);
213 printf("FAIL: %s, incorrect buf %p\n",
218 if (probe_addr != addr) {
219 printf("FAIL: %s, incorrect probe_addr 0x%llx\n",
220 __func__, probe_addr);
227 static int test_debug_fs_uprobe(char *binary_path, long offset, bool is_return)
229 char buf[256], event_alias[sizeof("test_1234567890")];
230 const char *event_type = "uprobe";
231 struct perf_event_attr attr = {};
232 __u64 probe_offset, probe_addr;
233 __u32 len, prog_id, fd_type;
234 int err = -1, res, kfd, efd;
235 struct bpf_link *link;
238 snprintf(buf, sizeof(buf), "/sys/kernel/tracing/%s_events",
240 kfd = open(buf, O_WRONLY | O_TRUNC, 0);
241 CHECK_PERROR_RET(kfd < 0);
243 res = snprintf(event_alias, sizeof(event_alias), "test_%d", getpid());
244 CHECK_PERROR_RET(res < 0 || res >= sizeof(event_alias));
246 res = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx",
247 is_return ? 'r' : 'p', event_type, event_alias,
248 binary_path, offset);
249 CHECK_PERROR_RET(res < 0 || res >= sizeof(buf));
250 CHECK_PERROR_RET(write(kfd, buf, strlen(buf)) < 0);
255 snprintf(buf, sizeof(buf), "/sys/kernel/tracing/events/%ss/%s/id",
256 event_type, event_alias);
257 efd = open(buf, O_RDONLY, 0);
258 CHECK_PERROR_RET(efd < 0);
260 bytes = read(efd, buf, sizeof(buf));
261 CHECK_PERROR_RET(bytes <= 0 || bytes >= sizeof(buf));
265 attr.config = strtol(buf, NULL, 0);
266 attr.type = PERF_TYPE_TRACEPOINT;
267 attr.sample_period = 1;
268 attr.wakeup_events = 1;
270 kfd = sys_perf_event_open(&attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
271 link = bpf_program__attach_perf_event(progs[0], kfd);
272 if (libbpf_get_error(link)) {
273 printf("ERROR: bpf_program__attach_perf_event failed\n");
280 err = bpf_task_fd_query(getpid(), kfd, 0, buf, &len,
281 &prog_id, &fd_type, &probe_offset,
284 printf("FAIL: %s, binary_path %s\n", __func__, binary_path);
288 if ((is_return && fd_type != BPF_FD_TYPE_URETPROBE) ||
289 (!is_return && fd_type != BPF_FD_TYPE_UPROBE)) {
290 printf("FAIL: %s, incorrect fd_type %u\n", __func__,
294 if (strcmp(binary_path, buf) != 0) {
295 printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
298 if (probe_offset != offset) {
299 printf("FAIL: %s, incorrect probe_offset 0x%llx\n", __func__,
306 bpf_link__destroy(link);
310 int main(int argc, char **argv)
312 extern char __executable_start;
313 char filename[256], buf[256];
314 __u64 uprobe_file_offset;
315 struct bpf_program *prog;
316 struct bpf_object *obj;
319 if (load_kallsyms()) {
320 printf("failed to process /proc/kallsyms\n");
324 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
325 obj = bpf_object__open_file(filename, NULL);
326 if (libbpf_get_error(obj)) {
327 fprintf(stderr, "ERROR: opening BPF object file failed\n");
331 /* load BPF program */
332 if (bpf_object__load(obj)) {
333 fprintf(stderr, "ERROR: loading BPF object file failed\n");
337 bpf_object__for_each_program(prog, obj) {
339 links[i] = bpf_program__attach(progs[i]);
340 if (libbpf_get_error(links[i])) {
341 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
348 /* test two functions in the corresponding *_kern.c file */
349 CHECK_AND_RET(test_debug_fs_kprobe(0, "blk_mq_start_request",
350 BPF_FD_TYPE_KPROBE));
351 CHECK_AND_RET(test_debug_fs_kprobe(1, "__blk_account_io_done",
352 BPF_FD_TYPE_KRETPROBE));
354 /* test nondebug fs kprobe */
355 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
356 false, BPF_FD_TYPE_KPROBE,
357 BPF_FD_TYPE_KRETPROBE,
360 /* set a kprobe on "bpf_check + 0x5", which is x64 specific */
361 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x5, 0x0,
362 false, BPF_FD_TYPE_KPROBE,
363 BPF_FD_TYPE_KRETPROBE,
366 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
367 true, BPF_FD_TYPE_KPROBE,
368 BPF_FD_TYPE_KRETPROBE,
370 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
371 ksym_get_addr("bpf_check"), false,
373 BPF_FD_TYPE_KRETPROBE,
375 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
376 ksym_get_addr("bpf_check"), false,
378 BPF_FD_TYPE_KRETPROBE,
380 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
381 ksym_get_addr("bpf_check"), true,
383 BPF_FD_TYPE_KRETPROBE,
385 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
386 ksym_get_addr("bpf_check"), true,
388 BPF_FD_TYPE_KRETPROBE,
391 /* test nondebug fs uprobe */
392 /* the calculation of uprobe file offset is based on gcc 7.3.1 on x64
393 * and the default linker script, which defines __executable_start as
394 * the start of the .text section. The calculation could be different
395 * on different systems with different compilers. The right way is
396 * to parse the ELF file. We took a shortcut here.
398 uprobe_file_offset = (unsigned long)main - (unsigned long)&__executable_start;
399 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
400 uprobe_file_offset, 0x0, false,
402 BPF_FD_TYPE_URETPROBE,
404 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
405 uprobe_file_offset, 0x0, true,
407 BPF_FD_TYPE_URETPROBE,
410 /* test debug fs uprobe */
411 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,
413 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,
418 for (i--; i >= 0; i--)
419 bpf_link__destroy(links[i]);
421 bpf_object__close(obj);