1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include "test_attach_probe.skel.h" 4 5 /* this is how USDT semaphore is actually defined, except volatile modifier */ 6 volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute((section(".probes"))); 7 8 /* uprobe attach point */ 9 static void trigger_func(void) 10 { 11 asm volatile (""); 12 } 13 14 void test_attach_probe(void) 15 { 16 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); 17 int duration = 0; 18 struct bpf_link *kprobe_link, *kretprobe_link; 19 struct bpf_link *uprobe_link, *uretprobe_link; 20 struct test_attach_probe* skel; 21 ssize_t uprobe_offset, ref_ctr_offset; 22 bool legacy; 23 24 /* Check if new-style kprobe/uprobe API is supported. 25 * Kernels that support new FD-based kprobe and uprobe BPF attachment 26 * through perf_event_open() syscall expose 27 * /sys/bus/event_source/devices/kprobe/type and 28 * /sys/bus/event_source/devices/uprobe/type files, respectively. They 29 * contain magic numbers that are passed as "type" field of 30 * perf_event_attr. Lack of such file in the system indicates legacy 31 * kernel with old-style kprobe/uprobe attach interface through 32 * creating per-probe event through tracefs. For such cases 33 * ref_ctr_offset feature is not supported, so we don't test it. 34 */ 35 legacy = access("/sys/bus/event_source/devices/kprobe/type", F_OK) != 0; 36 37 uprobe_offset = get_uprobe_offset(&trigger_func); 38 if (!ASSERT_GE(uprobe_offset, 0, "uprobe_offset")) 39 return; 40 41 ref_ctr_offset = get_rel_offset((uintptr_t)&uprobe_ref_ctr); 42 if (!ASSERT_GE(ref_ctr_offset, 0, "ref_ctr_offset")) 43 return; 44 45 skel = test_attach_probe__open_and_load(); 46 if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) 47 return; 48 if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n")) 49 goto cleanup; 50 51 kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe, 52 false /* retprobe */, 53 SYS_NANOSLEEP_KPROBE_NAME); 54 if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe")) 55 goto cleanup; 56 skel->links.handle_kprobe = kprobe_link; 57 58 kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe, 59 true /* retprobe */, 60 SYS_NANOSLEEP_KPROBE_NAME); 61 if (!ASSERT_OK_PTR(kretprobe_link, "attach_kretprobe")) 62 goto cleanup; 63 skel->links.handle_kretprobe = kretprobe_link; 64 65 if (!legacy) 66 ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before"); 67 68 uprobe_opts.retprobe = false; 69 uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset; 70 uprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe, 71 0 /* self pid */, 72 "/proc/self/exe", 73 uprobe_offset, 74 &uprobe_opts); 75 if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe")) 76 goto cleanup; 77 skel->links.handle_uprobe = uprobe_link; 78 79 if (!legacy) 80 ASSERT_GT(uprobe_ref_ctr, 0, "uprobe_ref_ctr_after"); 81 82 /* if uprobe uses ref_ctr, uretprobe has to use ref_ctr as well */ 83 uprobe_opts.retprobe = true; 84 uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset; 85 uretprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe, 86 -1 /* any pid */, 87 "/proc/self/exe", 88 uprobe_offset, &uprobe_opts); 89 if (!ASSERT_OK_PTR(uretprobe_link, "attach_uretprobe")) 90 goto cleanup; 91 skel->links.handle_uretprobe = uretprobe_link; 92 93 /* trigger & validate kprobe && kretprobe */ 94 usleep(1); 95 96 if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", 97 "wrong kprobe res: %d\n", skel->bss->kprobe_res)) 98 goto cleanup; 99 if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res", 100 "wrong kretprobe res: %d\n", skel->bss->kretprobe_res)) 101 goto cleanup; 102 103 /* trigger & validate uprobe & uretprobe */ 104 trigger_func(); 105 106 if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", 107 "wrong uprobe res: %d\n", skel->bss->uprobe_res)) 108 goto cleanup; 109 if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res", 110 "wrong uretprobe res: %d\n", skel->bss->uretprobe_res)) 111 goto cleanup; 112 113 cleanup: 114 test_attach_probe__destroy(skel); 115 ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_cleanup"); 116 } 117