xref: /linux/tools/testing/selftests/bpf/progs/test_uprobe.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Hengqi Chen */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 pid_t my_pid = 0;
9 
10 int test1_result = 0;
11 int test2_result = 0;
12 int test3_result = 0;
13 int test4_result = 0;
14 
15 SEC("uprobe/./liburandom_read.so:urandlib_api_sameoffset")
16 int BPF_UPROBE(test1)
17 {
18 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
19 
20 	if (pid != my_pid)
21 		return 0;
22 
23 	test1_result = 1;
24 	return 0;
25 }
26 
27 SEC("uprobe/./liburandom_read.so:urandlib_api_sameoffset@LIBURANDOM_READ_1.0.0")
28 int BPF_UPROBE(test2)
29 {
30 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
31 
32 	if (pid != my_pid)
33 		return 0;
34 
35 	test2_result = 1;
36 	return 0;
37 }
38 
39 SEC("uretprobe/./liburandom_read.so:urandlib_api_sameoffset@@LIBURANDOM_READ_2.0.0")
40 int BPF_URETPROBE(test3, int ret)
41 {
42 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
43 
44 	if (pid != my_pid)
45 		return 0;
46 
47 	test3_result = ret;
48 	return 0;
49 }
50 
51 SEC("uprobe")
52 int BPF_UPROBE(test4)
53 {
54 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
55 
56 	if (pid != my_pid)
57 		return 0;
58 
59 	test4_result = 1;
60 	return 0;
61 }
62 
63 #if defined(__TARGET_ARCH_x86)
64 struct pt_regs regs;
65 
66 SEC("uprobe")
67 int BPF_UPROBE(test_regs_change)
68 {
69 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
70 
71 	if (pid != my_pid)
72 		return 0;
73 
74 	ctx->ax  = regs.ax;
75 	ctx->cx  = regs.cx;
76 	ctx->dx  = regs.dx;
77 	ctx->r8  = regs.r8;
78 	ctx->r9  = regs.r9;
79 	ctx->r10 = regs.r10;
80 	ctx->r11 = regs.r11;
81 	ctx->di  = regs.di;
82 	ctx->si  = regs.si;
83 	return 0;
84 }
85 
86 unsigned long ip;
87 
88 SEC("uprobe")
89 int BPF_UPROBE(test_regs_change_ip)
90 {
91 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
92 
93 	if (pid != my_pid)
94 		return 0;
95 
96 	ctx->ip = ip;
97 	return 0;
98 }
99 #endif
100