summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/uprobe_multi.c
blob: 44190efcdba217b507e0c5772f9dfc14af72de25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/usdt.bpf.h>

char _license[] SEC("license") = "GPL";

__u64 uprobe_multi_func_1_addr = 0;
__u64 uprobe_multi_func_2_addr = 0;
__u64 uprobe_multi_func_3_addr = 0;

__u64 uprobe_multi_func_1_result = 0;
__u64 uprobe_multi_func_2_result = 0;
__u64 uprobe_multi_func_3_result = 0;

__u64 uretprobe_multi_func_1_result = 0;
__u64 uretprobe_multi_func_2_result = 0;
__u64 uretprobe_multi_func_3_result = 0;

__u64 uprobe_multi_sleep_result = 0;

int pid = 0;
int child_pid = 0;
int child_tid = 0;
int child_pid_usdt = 0;
int child_tid_usdt = 0;

int expect_pid = 0;
bool bad_pid_seen = false;
bool bad_pid_seen_usdt = false;

bool test_cookie = false;
void *user_ptr = 0;

static __always_inline bool verify_sleepable_user_copy(void)
{
	char data[9];

	bpf_copy_from_user(data, sizeof(data), user_ptr);
	return bpf_strncmp(data, sizeof(data), "test_data") == 0;
}

static void uprobe_multi_check(void *ctx, bool is_return, bool is_sleep)
{
	__u64 cur_pid_tgid = bpf_get_current_pid_tgid();
	__u32 cur_pid;

	cur_pid = cur_pid_tgid >> 32;
	if (pid && cur_pid != pid)
		return;

	if (expect_pid && cur_pid != expect_pid)
		bad_pid_seen = true;

	child_pid = cur_pid_tgid >> 32;
	child_tid = (__u32)cur_pid_tgid;

	__u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
	__u64 addr = bpf_get_func_ip(ctx);

#define SET(__var, __addr, __cookie) ({			\
	if (addr == __addr &&				\
	   (!test_cookie || (cookie == __cookie)))	\
		__var += 1;				\
})

	if (is_return) {
		SET(uretprobe_multi_func_1_result, uprobe_multi_func_1_addr, 2);
		SET(uretprobe_multi_func_2_result, uprobe_multi_func_2_addr, 3);
		SET(uretprobe_multi_func_3_result, uprobe_multi_func_3_addr, 1);
	} else {
		SET(uprobe_multi_func_1_result, uprobe_multi_func_1_addr, 3);
		SET(uprobe_multi_func_2_result, uprobe_multi_func_2_addr, 1);
		SET(uprobe_multi_func_3_result, uprobe_multi_func_3_addr, 2);
	}

#undef SET

	if (is_sleep && verify_sleepable_user_copy())
		uprobe_multi_sleep_result += 1;
}

SEC("uprobe.multi//proc/self/exe:uprobe_multi_func_*")
int uprobe(struct pt_regs *ctx)
{
	uprobe_multi_check(ctx, false, false);
	return 0;
}

SEC("uretprobe.multi//proc/self/exe:uprobe_multi_func_*")
int uretprobe(struct pt_regs *ctx)
{
	uprobe_multi_check(ctx, true, false);
	return 0;
}

SEC("uprobe.multi.s//proc/self/exe:uprobe_multi_func_*")
int uprobe_sleep(struct pt_regs *ctx)
{
	uprobe_multi_check(ctx, false, true);
	return 0;
}

SEC("uretprobe.multi.s//proc/self/exe:uprobe_multi_func_*")
int uretprobe_sleep(struct pt_regs *ctx)
{
	uprobe_multi_check(ctx, true, true);
	return 0;
}

SEC("uprobe.multi//proc/self/exe:uprobe_multi_func_*")
int uprobe_extra(struct pt_regs *ctx)
{
	/* we need this one just to mix PID-filtered and global uprobes */
	return 0;
}

SEC("usdt")
int usdt_pid(struct pt_regs *ctx)
{
	__u64 cur_pid_tgid = bpf_get_current_pid_tgid();
	__u32 cur_pid;

	cur_pid = cur_pid_tgid >> 32;
	if (pid && cur_pid != pid)
		return 0;

	if (expect_pid && cur_pid != expect_pid)
		bad_pid_seen_usdt = true;

	child_pid_usdt = cur_pid_tgid >> 32;
	child_tid_usdt = (__u32)cur_pid_tgid;

	return 0;
}

SEC("usdt")
int usdt_extra(struct pt_regs *ctx)
{
	/* we need this one just to mix PID-filtered and global USDT probes */
	return 0;
}