1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_attach_probe.skel.h"
5 ssize_t
get_base_addr() {
10 f
= fopen("/proc/self/maps", "r");
14 while (fscanf(f
, "%zx-%*x %s %zx %*[^\n]\n",
15 &start
, buf
, &offset
) == 3) {
16 if (strcmp(buf
, "r-xp") == 0) {
18 return start
- offset
;
26 void test_attach_probe(void)
29 struct bpf_link
*kprobe_link
, *kretprobe_link
;
30 struct bpf_link
*uprobe_link
, *uretprobe_link
;
31 struct test_attach_probe
* skel
;
35 base_addr
= get_base_addr();
36 if (CHECK(base_addr
< 0, "get_base_addr",
37 "failed to find base addr: %zd", base_addr
))
39 uprobe_offset
= (size_t)&get_base_addr
- base_addr
;
41 skel
= test_attach_probe__open_and_load();
42 if (CHECK(!skel
, "skel_open", "failed to open skeleton\n"))
44 if (CHECK(!skel
->bss
, "check_bss", ".bss wasn't mmap()-ed\n"))
47 kprobe_link
= bpf_program__attach_kprobe(skel
->progs
.handle_kprobe
,
49 SYS_NANOSLEEP_KPROBE_NAME
);
50 if (CHECK(IS_ERR(kprobe_link
), "attach_kprobe",
51 "err %ld\n", PTR_ERR(kprobe_link
)))
53 skel
->links
.handle_kprobe
= kprobe_link
;
55 kretprobe_link
= bpf_program__attach_kprobe(skel
->progs
.handle_kretprobe
,
57 SYS_NANOSLEEP_KPROBE_NAME
);
58 if (CHECK(IS_ERR(kretprobe_link
), "attach_kretprobe",
59 "err %ld\n", PTR_ERR(kretprobe_link
)))
61 skel
->links
.handle_kretprobe
= kretprobe_link
;
63 uprobe_link
= bpf_program__attach_uprobe(skel
->progs
.handle_uprobe
,
68 if (CHECK(IS_ERR(uprobe_link
), "attach_uprobe",
69 "err %ld\n", PTR_ERR(uprobe_link
)))
71 skel
->links
.handle_uprobe
= uprobe_link
;
73 uretprobe_link
= bpf_program__attach_uprobe(skel
->progs
.handle_uretprobe
,
78 if (CHECK(IS_ERR(uretprobe_link
), "attach_uretprobe",
79 "err %ld\n", PTR_ERR(uretprobe_link
)))
81 skel
->links
.handle_uretprobe
= uretprobe_link
;
83 /* trigger & validate kprobe && kretprobe */
86 if (CHECK(skel
->bss
->kprobe_res
!= 1, "check_kprobe_res",
87 "wrong kprobe res: %d\n", skel
->bss
->kprobe_res
))
89 if (CHECK(skel
->bss
->kretprobe_res
!= 2, "check_kretprobe_res",
90 "wrong kretprobe res: %d\n", skel
->bss
->kretprobe_res
))
93 /* trigger & validate uprobe & uretprobe */
96 if (CHECK(skel
->bss
->uprobe_res
!= 3, "check_uprobe_res",
97 "wrong uprobe res: %d\n", skel
->bss
->uprobe_res
))
99 if (CHECK(skel
->bss
->uretprobe_res
!= 4, "check_uretprobe_res",
100 "wrong uretprobe res: %d\n", skel
->bss
->uretprobe_res
))
104 test_attach_probe__destroy(skel
);