1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/bpf.h>
12 #include <sys/ioctl.h>
13 #include <sys/resource.h>
14 #include <sys/types.h>
21 #include "trace_helpers.h"
23 #define CHECK_PERROR_RET(condition) ({ \
24 int __ret = !!(condition); \
26 printf("FAIL: %s:\n", __func__); \
32 #define CHECK_AND_RET(condition) ({ \
33 int __ret = !!(condition); \
38 static __u64
ptr_to_u64(void *ptr
)
40 return (__u64
) (unsigned long) ptr
;
43 #define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type"
44 static int bpf_find_probe_type(const char *event_type
)
49 ret
= snprintf(buf
, sizeof(buf
), PMU_TYPE_FILE
, event_type
);
50 CHECK_PERROR_RET(ret
< 0 || ret
>= sizeof(buf
));
52 fd
= open(buf
, O_RDONLY
);
53 CHECK_PERROR_RET(fd
< 0);
55 ret
= read(fd
, buf
, sizeof(buf
));
57 CHECK_PERROR_RET(ret
< 0 || ret
>= sizeof(buf
));
60 ret
= (int)strtol(buf
, NULL
, 10);
61 CHECK_PERROR_RET(errno
);
65 #define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe"
66 static int bpf_get_retprobe_bit(const char *event_type
)
71 ret
= snprintf(buf
, sizeof(buf
), PMU_RETPROBE_FILE
, event_type
);
72 CHECK_PERROR_RET(ret
< 0 || ret
>= sizeof(buf
));
74 fd
= open(buf
, O_RDONLY
);
75 CHECK_PERROR_RET(fd
< 0);
77 ret
= read(fd
, buf
, sizeof(buf
));
79 CHECK_PERROR_RET(ret
< 0 || ret
>= sizeof(buf
));
80 CHECK_PERROR_RET(strlen(buf
) < strlen("config:"));
83 ret
= (int)strtol(buf
+ strlen("config:"), NULL
, 10);
84 CHECK_PERROR_RET(errno
);
88 static int test_debug_fs_kprobe(int prog_fd_idx
, const char *fn_name
,
89 __u32 expected_fd_type
)
91 __u64 probe_offset
, probe_addr
;
92 __u32 len
, prog_id
, fd_type
;
97 err
= bpf_task_fd_query(getpid(), event_fd
[prog_fd_idx
], 0, buf
, &len
,
98 &prog_id
, &fd_type
, &probe_offset
,
101 printf("FAIL: %s, for event_fd idx %d, fn_name %s\n",
102 __func__
, prog_fd_idx
, fn_name
);
106 if (strcmp(buf
, fn_name
) != 0 ||
107 fd_type
!= expected_fd_type
||
108 probe_offset
!= 0x0 || probe_addr
!= 0x0) {
109 printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n",
111 printf("buf: %s, fd_type: %u, probe_offset: 0x%llx,"
112 " probe_addr: 0x%llx\n",
113 buf
, fd_type
, probe_offset
, probe_addr
);
119 static int test_nondebug_fs_kuprobe_common(const char *event_type
,
120 const char *name
, __u64 offset
, __u64 addr
, bool is_return
,
121 char *buf
, __u32
*buf_len
, __u32
*prog_id
, __u32
*fd_type
,
122 __u64
*probe_offset
, __u64
*probe_addr
)
124 int is_return_bit
= bpf_get_retprobe_bit(event_type
);
125 int type
= bpf_find_probe_type(event_type
);
126 struct perf_event_attr attr
= {};
129 if (type
< 0 || is_return_bit
< 0) {
130 printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n",
131 __func__
, type
, is_return_bit
);
135 attr
.sample_period
= 1;
136 attr
.wakeup_events
= 1;
138 attr
.config
|= 1 << is_return_bit
;
141 attr
.config1
= ptr_to_u64((void *)name
);
142 attr
.config2
= offset
;
147 attr
.size
= sizeof(attr
);
150 fd
= sys_perf_event_open(&attr
, -1, 0, -1, 0);
151 CHECK_PERROR_RET(fd
< 0);
153 CHECK_PERROR_RET(ioctl(fd
, PERF_EVENT_IOC_ENABLE
, 0) < 0);
154 CHECK_PERROR_RET(ioctl(fd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) < 0);
155 CHECK_PERROR_RET(bpf_task_fd_query(getpid(), fd
, 0, buf
, buf_len
,
156 prog_id
, fd_type
, probe_offset
, probe_addr
) < 0);
161 static int test_nondebug_fs_probe(const char *event_type
, const char *name
,
162 __u64 offset
, __u64 addr
, bool is_return
,
163 __u32 expected_fd_type
,
164 __u32 expected_ret_fd_type
,
165 char *buf
, __u32 buf_len
)
167 __u64 probe_offset
, probe_addr
;
168 __u32 prog_id
, fd_type
;
171 err
= test_nondebug_fs_kuprobe_common(event_type
, name
,
172 offset
, addr
, is_return
,
173 buf
, &buf_len
, &prog_id
,
174 &fd_type
, &probe_offset
,
178 "for name %s, offset 0x%llx, addr 0x%llx, is_return %d\n",
179 __func__
, name
? name
: "", offset
, addr
, is_return
);
183 if ((is_return
&& fd_type
!= expected_ret_fd_type
) ||
184 (!is_return
&& fd_type
!= expected_fd_type
)) {
185 printf("FAIL: %s, incorrect fd_type %u\n",
190 if (strcmp(name
, buf
) != 0) {
191 printf("FAIL: %s, incorrect buf %s\n", __func__
, buf
);
194 if (probe_offset
!= offset
) {
195 printf("FAIL: %s, incorrect probe_offset 0x%llx\n",
196 __func__
, probe_offset
);
201 printf("FAIL: %s, incorrect buf %p\n",
206 if (probe_addr
!= addr
) {
207 printf("FAIL: %s, incorrect probe_addr 0x%llx\n",
208 __func__
, probe_addr
);
215 static int test_debug_fs_uprobe(char *binary_path
, long offset
, bool is_return
)
217 const char *event_type
= "uprobe";
218 struct perf_event_attr attr
= {};
219 char buf
[256], event_alias
[sizeof("test_1234567890")];
220 __u64 probe_offset
, probe_addr
;
221 __u32 len
, prog_id
, fd_type
;
222 int err
, res
, kfd
, efd
;
225 snprintf(buf
, sizeof(buf
), "/sys/kernel/debug/tracing/%s_events",
227 kfd
= open(buf
, O_WRONLY
| O_APPEND
, 0);
228 CHECK_PERROR_RET(kfd
< 0);
230 res
= snprintf(event_alias
, sizeof(event_alias
), "test_%d", getpid());
231 CHECK_PERROR_RET(res
< 0 || res
>= sizeof(event_alias
));
233 res
= snprintf(buf
, sizeof(buf
), "%c:%ss/%s %s:0x%lx",
234 is_return
? 'r' : 'p', event_type
, event_alias
,
235 binary_path
, offset
);
236 CHECK_PERROR_RET(res
< 0 || res
>= sizeof(buf
));
237 CHECK_PERROR_RET(write(kfd
, buf
, strlen(buf
)) < 0);
242 snprintf(buf
, sizeof(buf
), "/sys/kernel/debug/tracing/events/%ss/%s/id",
243 event_type
, event_alias
);
244 efd
= open(buf
, O_RDONLY
, 0);
245 CHECK_PERROR_RET(efd
< 0);
247 bytes
= read(efd
, buf
, sizeof(buf
));
248 CHECK_PERROR_RET(bytes
<= 0 || bytes
>= sizeof(buf
));
252 attr
.config
= strtol(buf
, NULL
, 0);
253 attr
.type
= PERF_TYPE_TRACEPOINT
;
254 attr
.sample_period
= 1;
255 attr
.wakeup_events
= 1;
256 kfd
= sys_perf_event_open(&attr
, -1, 0, -1, PERF_FLAG_FD_CLOEXEC
);
257 CHECK_PERROR_RET(kfd
< 0);
258 CHECK_PERROR_RET(ioctl(kfd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) < 0);
259 CHECK_PERROR_RET(ioctl(kfd
, PERF_EVENT_IOC_ENABLE
, 0) < 0);
262 err
= bpf_task_fd_query(getpid(), kfd
, 0, buf
, &len
,
263 &prog_id
, &fd_type
, &probe_offset
,
266 printf("FAIL: %s, binary_path %s\n", __func__
, binary_path
);
270 if ((is_return
&& fd_type
!= BPF_FD_TYPE_URETPROBE
) ||
271 (!is_return
&& fd_type
!= BPF_FD_TYPE_UPROBE
)) {
272 printf("FAIL: %s, incorrect fd_type %u\n", __func__
,
276 if (strcmp(binary_path
, buf
) != 0) {
277 printf("FAIL: %s, incorrect buf %s\n", __func__
, buf
);
280 if (probe_offset
!= offset
) {
281 printf("FAIL: %s, incorrect probe_offset 0x%llx\n", __func__
,
290 int main(int argc
, char **argv
)
292 struct rlimit r
= {1024*1024, RLIM_INFINITY
};
293 extern char __executable_start
;
294 char filename
[256], buf
[256];
295 __u64 uprobe_file_offset
;
297 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
298 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
299 perror("setrlimit(RLIMIT_MEMLOCK)");
303 if (load_kallsyms()) {
304 printf("failed to process /proc/kallsyms\n");
308 if (load_bpf_file(filename
)) {
309 printf("%s", bpf_log_buf
);
313 /* test two functions in the corresponding *_kern.c file */
314 CHECK_AND_RET(test_debug_fs_kprobe(0, "blk_mq_start_request",
315 BPF_FD_TYPE_KPROBE
));
316 CHECK_AND_RET(test_debug_fs_kprobe(1, "blk_account_io_completion",
317 BPF_FD_TYPE_KRETPROBE
));
319 /* test nondebug fs kprobe */
320 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
321 false, BPF_FD_TYPE_KPROBE
,
322 BPF_FD_TYPE_KRETPROBE
,
325 /* set a kprobe on "bpf_check + 0x5", which is x64 specific */
326 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x5, 0x0,
327 false, BPF_FD_TYPE_KPROBE
,
328 BPF_FD_TYPE_KRETPROBE
,
331 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
332 true, BPF_FD_TYPE_KPROBE
,
333 BPF_FD_TYPE_KRETPROBE
,
335 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL
, 0x0,
336 ksym_get_addr("bpf_check"), false,
338 BPF_FD_TYPE_KRETPROBE
,
340 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL
, 0x0,
341 ksym_get_addr("bpf_check"), false,
343 BPF_FD_TYPE_KRETPROBE
,
345 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL
, 0x0,
346 ksym_get_addr("bpf_check"), true,
348 BPF_FD_TYPE_KRETPROBE
,
350 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL
, 0x0,
351 ksym_get_addr("bpf_check"), true,
353 BPF_FD_TYPE_KRETPROBE
,
356 /* test nondebug fs uprobe */
357 /* the calculation of uprobe file offset is based on gcc 7.3.1 on x64
358 * and the default linker script, which defines __executable_start as
359 * the start of the .text section. The calculation could be different
360 * on different systems with different compilers. The right way is
361 * to parse the ELF file. We took a shortcut here.
363 uprobe_file_offset
= (__u64
)main
- (__u64
)&__executable_start
;
364 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv
[0],
365 uprobe_file_offset
, 0x0, false,
367 BPF_FD_TYPE_URETPROBE
,
369 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv
[0],
370 uprobe_file_offset
, 0x0, true,
372 BPF_FD_TYPE_URETPROBE
,
375 /* test debug fs uprobe */
376 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv
[0], uprobe_file_offset
,
378 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv
[0], uprobe_file_offset
,