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>
16 #include <linux/perf_event.h>
18 #include <bpf/libbpf.h>
22 #include "trace_helpers.h"
24 #define CHECK_PERROR_RET(condition) ({ \
25 int __ret = !!(condition); \
27 printf("FAIL: %s:\n", __func__); \
33 #define CHECK_AND_RET(condition) ({ \
34 int __ret = !!(condition); \
39 static __u64 ptr_to_u64(void *ptr)
41 return (__u64) (unsigned long) ptr;
44 #define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type"
45 static int bpf_find_probe_type(const char *event_type)
50 ret = snprintf(buf, sizeof(buf), PMU_TYPE_FILE, event_type);
51 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
53 fd = open(buf, O_RDONLY);
54 CHECK_PERROR_RET(fd < 0);
56 ret = read(fd, buf, sizeof(buf));
58 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
61 ret = (int)strtol(buf, NULL, 10);
62 CHECK_PERROR_RET(errno);
66 #define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe"
67 static int bpf_get_retprobe_bit(const char *event_type)
72 ret = snprintf(buf, sizeof(buf), PMU_RETPROBE_FILE, event_type);
73 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
75 fd = open(buf, O_RDONLY);
76 CHECK_PERROR_RET(fd < 0);
78 ret = read(fd, buf, sizeof(buf));
80 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
81 CHECK_PERROR_RET(strlen(buf) < strlen("config:"));
84 ret = (int)strtol(buf + strlen("config:"), NULL, 10);
85 CHECK_PERROR_RET(errno);
89 static int test_debug_fs_kprobe(int prog_fd_idx, const char *fn_name,
90 __u32 expected_fd_type)
92 __u64 probe_offset, probe_addr;
93 __u32 len, prog_id, fd_type;
98 err = bpf_task_fd_query(getpid(), event_fd[prog_fd_idx], 0, buf, &len,
99 &prog_id, &fd_type, &probe_offset,
102 printf("FAIL: %s, for event_fd idx %d, fn_name %s\n",
103 __func__, prog_fd_idx, fn_name);
107 if (strcmp(buf, fn_name) != 0 ||
108 fd_type != expected_fd_type ||
109 probe_offset != 0x0 || probe_addr != 0x0) {
110 printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n",
112 printf("buf: %s, fd_type: %u, probe_offset: 0x%llx,"
113 " probe_addr: 0x%llx\n",
114 buf, fd_type, probe_offset, probe_addr);
120 static int test_nondebug_fs_kuprobe_common(const char *event_type,
121 const char *name, __u64 offset, __u64 addr, bool is_return,
122 char *buf, __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
123 __u64 *probe_offset, __u64 *probe_addr)
125 int is_return_bit = bpf_get_retprobe_bit(event_type);
126 int type = bpf_find_probe_type(event_type);
127 struct perf_event_attr attr = {};
130 if (type < 0 || is_return_bit < 0) {
131 printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n",
132 __func__, type, is_return_bit);
136 attr.sample_period = 1;
137 attr.wakeup_events = 1;
139 attr.config |= 1 << is_return_bit;
142 attr.config1 = ptr_to_u64((void *)name);
143 attr.config2 = offset;
148 attr.size = sizeof(attr);
151 fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
152 CHECK_PERROR_RET(fd < 0);
154 CHECK_PERROR_RET(ioctl(fd, PERF_EVENT_IOC_ENABLE, 0) < 0);
155 CHECK_PERROR_RET(ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) < 0);
156 CHECK_PERROR_RET(bpf_task_fd_query(getpid(), fd, 0, buf, buf_len,
157 prog_id, fd_type, probe_offset, probe_addr) < 0);
162 static int test_nondebug_fs_probe(const char *event_type, const char *name,
163 __u64 offset, __u64 addr, bool is_return,
164 __u32 expected_fd_type,
165 __u32 expected_ret_fd_type,
166 char *buf, __u32 buf_len)
168 __u64 probe_offset, probe_addr;
169 __u32 prog_id, fd_type;
172 err = test_nondebug_fs_kuprobe_common(event_type, name,
173 offset, addr, is_return,
174 buf, &buf_len, &prog_id,
175 &fd_type, &probe_offset,
179 "for name %s, offset 0x%llx, addr 0x%llx, is_return %d\n",
180 __func__, name ? name : "", offset, addr, is_return);
184 if ((is_return && fd_type != expected_ret_fd_type) ||
185 (!is_return && fd_type != expected_fd_type)) {
186 printf("FAIL: %s, incorrect fd_type %u\n",
191 if (strcmp(name, buf) != 0) {
192 printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
195 if (probe_offset != offset) {
196 printf("FAIL: %s, incorrect probe_offset 0x%llx\n",
197 __func__, probe_offset);
202 printf("FAIL: %s, incorrect buf %p\n",
207 if (probe_addr != addr) {
208 printf("FAIL: %s, incorrect probe_addr 0x%llx\n",
209 __func__, probe_addr);
216 static int test_debug_fs_uprobe(char *binary_path, long offset, bool is_return)
218 const char *event_type = "uprobe";
219 struct perf_event_attr attr = {};
220 char buf[256], event_alias[sizeof("test_1234567890")];
221 __u64 probe_offset, probe_addr;
222 __u32 len, prog_id, fd_type;
223 int err, res, kfd, efd;
226 snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events",
228 kfd = open(buf, O_WRONLY | O_APPEND, 0);
229 CHECK_PERROR_RET(kfd < 0);
231 res = snprintf(event_alias, sizeof(event_alias), "test_%d", getpid());
232 CHECK_PERROR_RET(res < 0 || res >= sizeof(event_alias));
234 res = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx",
235 is_return ? 'r' : 'p', event_type, event_alias,
236 binary_path, offset);
237 CHECK_PERROR_RET(res < 0 || res >= sizeof(buf));
238 CHECK_PERROR_RET(write(kfd, buf, strlen(buf)) < 0);
243 snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s/id",
244 event_type, event_alias);
245 efd = open(buf, O_RDONLY, 0);
246 CHECK_PERROR_RET(efd < 0);
248 bytes = read(efd, buf, sizeof(buf));
249 CHECK_PERROR_RET(bytes <= 0 || bytes >= sizeof(buf));
253 attr.config = strtol(buf, NULL, 0);
254 attr.type = PERF_TYPE_TRACEPOINT;
255 attr.sample_period = 1;
256 attr.wakeup_events = 1;
257 kfd = sys_perf_event_open(&attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
258 CHECK_PERROR_RET(kfd < 0);
259 CHECK_PERROR_RET(ioctl(kfd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) < 0);
260 CHECK_PERROR_RET(ioctl(kfd, PERF_EVENT_IOC_ENABLE, 0) < 0);
263 err = bpf_task_fd_query(getpid(), kfd, 0, buf, &len,
264 &prog_id, &fd_type, &probe_offset,
267 printf("FAIL: %s, binary_path %s\n", __func__, binary_path);
271 if ((is_return && fd_type != BPF_FD_TYPE_URETPROBE) ||
272 (!is_return && fd_type != BPF_FD_TYPE_UPROBE)) {
273 printf("FAIL: %s, incorrect fd_type %u\n", __func__,
277 if (strcmp(binary_path, buf) != 0) {
278 printf("FAIL: %s, incorrect buf %s\n", __func__, buf);
281 if (probe_offset != offset) {
282 printf("FAIL: %s, incorrect probe_offset 0x%llx\n", __func__,
291 int main(int argc, char **argv)
293 struct rlimit r = {1024*1024, RLIM_INFINITY};
294 extern char __executable_start;
295 char filename[256], buf[256];
296 __u64 uprobe_file_offset;
298 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
299 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
300 perror("setrlimit(RLIMIT_MEMLOCK)");
304 if (load_kallsyms()) {
305 printf("failed to process /proc/kallsyms\n");
309 if (load_bpf_file(filename)) {
310 printf("%s", bpf_log_buf);
314 /* test two functions in the corresponding *_kern.c file */
315 CHECK_AND_RET(test_debug_fs_kprobe(0, "blk_mq_start_request",
316 BPF_FD_TYPE_KPROBE));
317 CHECK_AND_RET(test_debug_fs_kprobe(1, "blk_account_io_completion",
318 BPF_FD_TYPE_KRETPROBE));
320 /* test nondebug fs kprobe */
321 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
322 false, BPF_FD_TYPE_KPROBE,
323 BPF_FD_TYPE_KRETPROBE,
326 /* set a kprobe on "bpf_check + 0x5", which is x64 specific */
327 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x5, 0x0,
328 false, BPF_FD_TYPE_KPROBE,
329 BPF_FD_TYPE_KRETPROBE,
332 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", "bpf_check", 0x0, 0x0,
333 true, BPF_FD_TYPE_KPROBE,
334 BPF_FD_TYPE_KRETPROBE,
336 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
337 ksym_get_addr("bpf_check"), false,
339 BPF_FD_TYPE_KRETPROBE,
341 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
342 ksym_get_addr("bpf_check"), false,
344 BPF_FD_TYPE_KRETPROBE,
346 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
347 ksym_get_addr("bpf_check"), true,
349 BPF_FD_TYPE_KRETPROBE,
351 CHECK_AND_RET(test_nondebug_fs_probe("kprobe", NULL, 0x0,
352 ksym_get_addr("bpf_check"), true,
354 BPF_FD_TYPE_KRETPROBE,
357 /* test nondebug fs uprobe */
358 /* the calculation of uprobe file offset is based on gcc 7.3.1 on x64
359 * and the default linker script, which defines __executable_start as
360 * the start of the .text section. The calculation could be different
361 * on different systems with different compilers. The right way is
362 * to parse the ELF file. We took a shortcut here.
364 uprobe_file_offset = (__u64)main - (__u64)&__executable_start;
365 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
366 uprobe_file_offset, 0x0, false,
368 BPF_FD_TYPE_URETPROBE,
370 CHECK_AND_RET(test_nondebug_fs_probe("uprobe", (char *)argv[0],
371 uprobe_file_offset, 0x0, true,
373 BPF_FD_TYPE_URETPROBE,
376 /* test debug fs uprobe */
377 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,
379 CHECK_AND_RET(test_debug_fs_uprobe((char *)argv[0], uprobe_file_offset,