xref: /linux/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022, Oracle and/or its affiliates. */
3 
4 #include "vmlinux.h"
5 
6 #include <bpf/bpf_core_read.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_tracing.h>
9 #include "bpf_misc.h"
10 
11 int uprobe_byname_parm1 = 0;
12 int uprobe_byname_ran = 0;
13 int uretprobe_byname_rc = 0;
14 int uretprobe_byname_ret = 0;
15 int uretprobe_byname_ran = 0;
16 u64 uprobe_byname2_parm1 = 0;
17 int uprobe_byname2_ran = 0;
18 u64 uretprobe_byname2_rc = 0;
19 int uretprobe_byname2_ran = 0;
20 
21 int test_pid;
22 
23 int a[8];
24 
25 /* This program cannot auto-attach, but that should not stop other
26  * programs from attaching.
27  */
28 SEC("uprobe")
29 int handle_uprobe_noautoattach(struct pt_regs *ctx)
30 {
31 	return 0;
32 }
33 
34 SEC("uprobe//proc/self/exe:autoattach_trigger_func")
35 int BPF_UPROBE(handle_uprobe_byname
36 	       , int arg1
37 	       , int arg2
38 	       , int arg3
39 #if FUNC_REG_ARG_CNT > 3
40 	       , int arg4
41 #endif
42 #if FUNC_REG_ARG_CNT > 4
43 	       , int arg5
44 #endif
45 #if FUNC_REG_ARG_CNT > 5
46 	       , int arg6
47 #endif
48 #if FUNC_REG_ARG_CNT > 6
49 	       , int arg7
50 #endif
51 #if FUNC_REG_ARG_CNT > 7
52 	       , int arg8
53 #endif
54 )
55 {
56 	uprobe_byname_parm1 = PT_REGS_PARM1_CORE(ctx);
57 	uprobe_byname_ran = 1;
58 
59 	a[0] = arg1;
60 	a[1] = arg2;
61 	a[2] = arg3;
62 #if FUNC_REG_ARG_CNT > 3
63 	a[3] = arg4;
64 #endif
65 #if FUNC_REG_ARG_CNT > 4
66 	a[4] = arg5;
67 #endif
68 #if FUNC_REG_ARG_CNT > 5
69 	a[5] = arg6;
70 #endif
71 #if FUNC_REG_ARG_CNT > 6
72 	a[6] = arg7;
73 #endif
74 #if FUNC_REG_ARG_CNT > 7
75 	a[7] = arg8;
76 #endif
77 	return 0;
78 }
79 
80 SEC("uretprobe//proc/self/exe:autoattach_trigger_func")
81 int BPF_URETPROBE(handle_uretprobe_byname, int ret)
82 {
83 	uretprobe_byname_rc = PT_REGS_RC_CORE(ctx);
84 	uretprobe_byname_ret = ret;
85 	uretprobe_byname_ran = 2;
86 
87 	return 0;
88 }
89 
90 
91 SEC("uprobe/libc.so.6:fopen")
92 int BPF_UPROBE(handle_uprobe_byname2, const char *pathname, const char *mode)
93 {
94 	int pid = bpf_get_current_pid_tgid() >> 32;
95 
96 	/* ignore irrelevant invocations */
97 	if (test_pid != pid)
98 		return 0;
99 	uprobe_byname2_parm1 = (u64)(long)pathname;
100 	uprobe_byname2_ran = 3;
101 	return 0;
102 }
103 
104 SEC("uretprobe/libc.so.6:fopen")
105 int BPF_URETPROBE(handle_uretprobe_byname2, void *ret)
106 {
107 	int pid = bpf_get_current_pid_tgid() >> 32;
108 
109 	/* ignore irrelevant invocations */
110 	if (test_pid != pid)
111 		return 0;
112 	uretprobe_byname2_rc = (u64)(long)ret;
113 	uretprobe_byname2_ran = 4;
114 	return 0;
115 }
116 
117 char _license[] SEC("license") = "GPL";
118