xref: /linux/tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
5 #include <stdbool.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 int pid = 0;
10 
11 __u64 test_uprobe_1_result = 0;
12 __u64 test_uprobe_2_result = 0;
13 __u64 test_uprobe_3_result = 0;
14 
15 static int check_cookie(struct pt_regs *ctx, __u64 val, __u64 *result)
16 {
17 	__u64 *cookie;
18 
19 	if (bpf_get_current_pid_tgid() >> 32 != pid)
20 		return 1;
21 
22 	cookie = bpf_session_cookie(ctx);
23 
24 	if (bpf_session_is_return(ctx))
25 		*result = *cookie == val ? val : 0;
26 	else
27 		*cookie = val;
28 	return 0;
29 }
30 
31 SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1")
32 int uprobe_1(struct pt_regs *ctx)
33 {
34 	return check_cookie(ctx, 1, &test_uprobe_1_result);
35 }
36 
37 SEC("uprobe.session//proc/self/exe:uprobe_multi_func_2")
38 int uprobe_2(struct pt_regs *ctx)
39 {
40 	return check_cookie(ctx, 2, &test_uprobe_2_result);
41 }
42 
43 SEC("uprobe.session//proc/self/exe:uprobe_multi_func_3")
44 int uprobe_3(struct pt_regs *ctx)
45 {
46 	return check_cookie(ctx, 3, &test_uprobe_3_result);
47 }
48