Skip to content

Commit c7aec81

Browse files
menglongdonganakryiko
authored andcommitted
selftests/bpf: Add test for legacy/perf kprobe/uprobe attach mode
Add the testing for kprobe/uprobe attaching in default, legacy, perf and link mode. And the testing passed: ./test_progs -t attach_probe $5/1 attach_probe/manual-default:OK $5/2 attach_probe/manual-legacy:OK $5/3 attach_probe/manual-perf:OK $5/4 attach_probe/manual-link:OK $5/5 attach_probe/auto:OK $5/6 attach_probe/kprobe-sleepable:OK $5/7 attach_probe/uprobe-lib:OK $5/8 attach_probe/uprobe-sleepable:OK $5/9 attach_probe/uprobe-ref_ctr:OK $5 attach_probe:OK Summary: 1/9 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Menglong Dong <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Biao Jiang <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 7391ec6 commit c7aec81

File tree

3 files changed

+81
-41
lines changed

3 files changed

+81
-41
lines changed

tools/testing/selftests/bpf/prog_tests/attach_probe.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
33
#include "test_attach_kprobe_sleepable.skel.h"
4+
#include "test_attach_probe_manual.skel.h"
45
#include "test_attach_probe.skel.h"
56

67
/* this is how USDT semaphore is actually defined, except volatile modifier */
@@ -33,33 +34,43 @@ static noinline void trigger_func4(void)
3334
static char test_data[] = "test_data";
3435

3536
/* manual attach kprobe/kretprobe/uprobe/uretprobe testings */
36-
static void test_attach_probe_manual(struct test_attach_probe *skel)
37+
static void test_attach_probe_manual(enum probe_attach_mode attach_mode)
3738
{
3839
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
40+
DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
3941
struct bpf_link *kprobe_link, *kretprobe_link;
4042
struct bpf_link *uprobe_link, *uretprobe_link;
43+
struct test_attach_probe_manual *skel;
4144
ssize_t uprobe_offset;
4245

46+
skel = test_attach_probe_manual__open_and_load();
47+
if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
48+
return;
49+
4350
uprobe_offset = get_uprobe_offset(&trigger_func);
4451
if (!ASSERT_GE(uprobe_offset, 0, "uprobe_offset"))
4552
goto cleanup;
4653

4754
/* manual-attach kprobe/kretprobe */
48-
kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
49-
false /* retprobe */,
50-
SYS_NANOSLEEP_KPROBE_NAME);
55+
kprobe_opts.attach_mode = attach_mode;
56+
kprobe_opts.retprobe = false;
57+
kprobe_link = bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
58+
SYS_NANOSLEEP_KPROBE_NAME,
59+
&kprobe_opts);
5160
if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe"))
5261
goto cleanup;
5362
skel->links.handle_kprobe = kprobe_link;
5463

55-
kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe,
56-
true /* retprobe */,
57-
SYS_NANOSLEEP_KPROBE_NAME);
64+
kprobe_opts.retprobe = true;
65+
kretprobe_link = bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
66+
SYS_NANOSLEEP_KPROBE_NAME,
67+
&kprobe_opts);
5868
if (!ASSERT_OK_PTR(kretprobe_link, "attach_kretprobe"))
5969
goto cleanup;
6070
skel->links.handle_kretprobe = kretprobe_link;
6171

6272
/* manual-attach uprobe/uretprobe */
73+
uprobe_opts.attach_mode = attach_mode;
6374
uprobe_opts.ref_ctr_offset = 0;
6475
uprobe_opts.retprobe = false;
6576
uprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe,
@@ -108,6 +119,7 @@ static void test_attach_probe_manual(struct test_attach_probe *skel)
108119
ASSERT_EQ(skel->bss->uprobe_byname_res, 5, "check_uprobe_byname_res");
109120

110121
cleanup:
122+
test_attach_probe_manual__destroy(skel);
111123
}
112124

113125
static void test_attach_probe_auto(struct test_attach_probe *skel)
@@ -289,8 +301,15 @@ void test_attach_probe(void)
289301
if (!ASSERT_OK_PTR(skel->bss, "check_bss"))
290302
goto cleanup;
291303

292-
if (test__start_subtest("manual"))
293-
test_attach_probe_manual(skel);
304+
if (test__start_subtest("manual-default"))
305+
test_attach_probe_manual(PROBE_ATTACH_MODE_DEFAULT);
306+
if (test__start_subtest("manual-legacy"))
307+
test_attach_probe_manual(PROBE_ATTACH_MODE_LEGACY);
308+
if (test__start_subtest("manual-perf"))
309+
test_attach_probe_manual(PROBE_ATTACH_MODE_PERF);
310+
if (test__start_subtest("manual-link"))
311+
test_attach_probe_manual(PROBE_ATTACH_MODE_LINK);
312+
294313
if (test__start_subtest("auto"))
295314
test_attach_probe_auto(skel);
296315
if (test__start_subtest("kprobe-sleepable"))

tools/testing/selftests/bpf/progs/test_attach_probe.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
#include <bpf/bpf_core_read.h>
88
#include "bpf_misc.h"
99

10-
int kprobe_res = 0;
1110
int kprobe2_res = 0;
12-
int kretprobe_res = 0;
1311
int kretprobe2_res = 0;
14-
int uprobe_res = 0;
15-
int uretprobe_res = 0;
1612
int uprobe_byname_res = 0;
1713
int uretprobe_byname_res = 0;
1814
int uprobe_byname2_res = 0;
@@ -23,48 +19,20 @@ int uretprobe_byname3_sleepable_res = 0;
2319
int uretprobe_byname3_res = 0;
2420
void *user_ptr = 0;
2521

26-
SEC("kprobe")
27-
int handle_kprobe(struct pt_regs *ctx)
28-
{
29-
kprobe_res = 1;
30-
return 0;
31-
}
32-
3322
SEC("ksyscall/nanosleep")
3423
int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
3524
{
3625
kprobe2_res = 11;
3726
return 0;
3827
}
3928

40-
SEC("kretprobe")
41-
int handle_kretprobe(struct pt_regs *ctx)
42-
{
43-
kretprobe_res = 2;
44-
return 0;
45-
}
46-
4729
SEC("kretsyscall/nanosleep")
4830
int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
4931
{
5032
kretprobe2_res = 22;
5133
return ret;
5234
}
5335

54-
SEC("uprobe")
55-
int handle_uprobe(struct pt_regs *ctx)
56-
{
57-
uprobe_res = 3;
58-
return 0;
59-
}
60-
61-
SEC("uretprobe")
62-
int handle_uretprobe(struct pt_regs *ctx)
63-
{
64-
uretprobe_res = 4;
65-
return 0;
66-
}
67-
6836
SEC("uprobe")
6937
int handle_uprobe_ref_ctr(struct pt_regs *ctx)
7038
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2017 Facebook
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <bpf/bpf_core_read.h>
8+
#include "bpf_misc.h"
9+
10+
int kprobe_res = 0;
11+
int kretprobe_res = 0;
12+
int uprobe_res = 0;
13+
int uretprobe_res = 0;
14+
int uprobe_byname_res = 0;
15+
void *user_ptr = 0;
16+
17+
SEC("kprobe")
18+
int handle_kprobe(struct pt_regs *ctx)
19+
{
20+
kprobe_res = 1;
21+
return 0;
22+
}
23+
24+
SEC("kretprobe")
25+
int handle_kretprobe(struct pt_regs *ctx)
26+
{
27+
kretprobe_res = 2;
28+
return 0;
29+
}
30+
31+
SEC("uprobe")
32+
int handle_uprobe(struct pt_regs *ctx)
33+
{
34+
uprobe_res = 3;
35+
return 0;
36+
}
37+
38+
SEC("uretprobe")
39+
int handle_uretprobe(struct pt_regs *ctx)
40+
{
41+
uretprobe_res = 4;
42+
return 0;
43+
}
44+
45+
SEC("uprobe")
46+
int handle_uprobe_byname(struct pt_regs *ctx)
47+
{
48+
uprobe_byname_res = 5;
49+
return 0;
50+
}
51+
52+
53+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)