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