1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 ssize_t
get_base_addr() {
9 f
= fopen("/proc/self/maps", "r");
13 while (fscanf(f
, "%zx-%*x %s %*s\n", &start
, buf
) == 2) {
14 if (strcmp(buf
, "r-xp") == 0) {
24 void test_attach_probe(void)
26 const char *kprobe_name
= "kprobe/sys_nanosleep";
27 const char *kretprobe_name
= "kretprobe/sys_nanosleep";
28 const char *uprobe_name
= "uprobe/trigger_func";
29 const char *uretprobe_name
= "uretprobe/trigger_func";
30 const int kprobe_idx
= 0, kretprobe_idx
= 1;
31 const int uprobe_idx
= 2, uretprobe_idx
= 3;
32 const char *file
= "./test_attach_probe.o";
33 struct bpf_program
*kprobe_prog
, *kretprobe_prog
;
34 struct bpf_program
*uprobe_prog
, *uretprobe_prog
;
35 struct bpf_object
*obj
;
36 int err
, prog_fd
, duration
= 0, res
;
37 struct bpf_link
*kprobe_link
= NULL
;
38 struct bpf_link
*kretprobe_link
= NULL
;
39 struct bpf_link
*uprobe_link
= NULL
;
40 struct bpf_link
*uretprobe_link
= NULL
;
45 base_addr
= get_base_addr();
46 if (CHECK(base_addr
< 0, "get_base_addr",
47 "failed to find base addr: %zd", base_addr
))
49 uprobe_offset
= (size_t)&get_base_addr
- base_addr
;
52 err
= bpf_prog_load(file
, BPF_PROG_TYPE_KPROBE
, &obj
, &prog_fd
);
53 if (CHECK(err
, "obj_load", "err %d errno %d\n", err
, errno
))
56 kprobe_prog
= bpf_object__find_program_by_title(obj
, kprobe_name
);
57 if (CHECK(!kprobe_prog
, "find_probe",
58 "prog '%s' not found\n", kprobe_name
))
60 kretprobe_prog
= bpf_object__find_program_by_title(obj
, kretprobe_name
);
61 if (CHECK(!kretprobe_prog
, "find_probe",
62 "prog '%s' not found\n", kretprobe_name
))
64 uprobe_prog
= bpf_object__find_program_by_title(obj
, uprobe_name
);
65 if (CHECK(!uprobe_prog
, "find_probe",
66 "prog '%s' not found\n", uprobe_name
))
68 uretprobe_prog
= bpf_object__find_program_by_title(obj
, uretprobe_name
);
69 if (CHECK(!uretprobe_prog
, "find_probe",
70 "prog '%s' not found\n", uretprobe_name
))
74 results_map_fd
= bpf_find_map(__func__
, obj
, "results_map");
75 if (CHECK(results_map_fd
< 0, "find_results_map",
76 "err %d\n", results_map_fd
))
79 kprobe_link
= bpf_program__attach_kprobe(kprobe_prog
,
81 SYS_NANOSLEEP_KPROBE_NAME
);
82 if (CHECK(IS_ERR(kprobe_link
), "attach_kprobe",
83 "err %ld\n", PTR_ERR(kprobe_link
))) {
87 kretprobe_link
= bpf_program__attach_kprobe(kretprobe_prog
,
89 SYS_NANOSLEEP_KPROBE_NAME
);
90 if (CHECK(IS_ERR(kretprobe_link
), "attach_kretprobe",
91 "err %ld\n", PTR_ERR(kretprobe_link
))) {
92 kretprobe_link
= NULL
;
95 uprobe_link
= bpf_program__attach_uprobe(uprobe_prog
,
100 if (CHECK(IS_ERR(uprobe_link
), "attach_uprobe",
101 "err %ld\n", PTR_ERR(uprobe_link
))) {
105 uretprobe_link
= bpf_program__attach_uprobe(uretprobe_prog
,
110 if (CHECK(IS_ERR(uretprobe_link
), "attach_uretprobe",
111 "err %ld\n", PTR_ERR(uretprobe_link
))) {
112 uretprobe_link
= NULL
;
116 /* trigger & validate kprobe && kretprobe */
119 err
= bpf_map_lookup_elem(results_map_fd
, &kprobe_idx
, &res
);
120 if (CHECK(err
, "get_kprobe_res",
121 "failed to get kprobe res: %d\n", err
))
123 if (CHECK(res
!= kprobe_idx
+ 1, "check_kprobe_res",
124 "wrong kprobe res: %d\n", res
))
127 err
= bpf_map_lookup_elem(results_map_fd
, &kretprobe_idx
, &res
);
128 if (CHECK(err
, "get_kretprobe_res",
129 "failed to get kretprobe res: %d\n", err
))
131 if (CHECK(res
!= kretprobe_idx
+ 1, "check_kretprobe_res",
132 "wrong kretprobe res: %d\n", res
))
135 /* trigger & validate uprobe & uretprobe */
138 err
= bpf_map_lookup_elem(results_map_fd
, &uprobe_idx
, &res
);
139 if (CHECK(err
, "get_uprobe_res",
140 "failed to get uprobe res: %d\n", err
))
142 if (CHECK(res
!= uprobe_idx
+ 1, "check_uprobe_res",
143 "wrong uprobe res: %d\n", res
))
146 err
= bpf_map_lookup_elem(results_map_fd
, &uretprobe_idx
, &res
);
147 if (CHECK(err
, "get_uretprobe_res",
148 "failed to get uretprobe res: %d\n", err
))
150 if (CHECK(res
!= uretprobe_idx
+ 1, "check_uretprobe_res",
151 "wrong uretprobe res: %d\n", res
))
155 bpf_link__destroy(kprobe_link
);
156 bpf_link__destroy(kretprobe_link
);
157 bpf_link__destroy(uprobe_link
);
158 bpf_link__destroy(uretprobe_link
);
159 bpf_object__close(obj
);