Skip to content

Commit 0a210af

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: selftests: Test fentry tracing a struct_ops program
This patch tests attaching an fentry prog to a struct_ops prog. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20220330011502.2985292-1-kafai@fb.com
1 parent 4a9c7bb commit 0a210af

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
33
#include <test_progs.h>
44
#include "dummy_st_ops.skel.h"
5+
#include "trace_dummy_st_ops.skel.h"
56

67
/* Need to keep consistent with definition in include/linux/bpf.h */
78
struct bpf_dummy_ops_state {
@@ -56,6 +57,7 @@ static void test_dummy_init_ptr_arg(void)
5657
.ctx_in = args,
5758
.ctx_size_in = sizeof(args),
5859
);
60+
struct trace_dummy_st_ops *trace_skel;
5961
struct dummy_st_ops *skel;
6062
int fd, err;
6163

@@ -64,12 +66,33 @@ static void test_dummy_init_ptr_arg(void)
6466
return;
6567

6668
fd = bpf_program__fd(skel->progs.test_1);
69+
70+
trace_skel = trace_dummy_st_ops__open();
71+
if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
72+
goto done;
73+
74+
err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
75+
fd, "test_1");
76+
if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
77+
goto done;
78+
79+
err = trace_dummy_st_ops__load(trace_skel);
80+
if (!ASSERT_OK(err, "load(trace_skel)"))
81+
goto done;
82+
83+
err = trace_dummy_st_ops__attach(trace_skel);
84+
if (!ASSERT_OK(err, "attach(trace_skel)"))
85+
goto done;
86+
6787
err = bpf_prog_test_run_opts(fd, &attr);
6888
ASSERT_OK(err, "test_run");
6989
ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret");
7090
ASSERT_EQ(attr.retval, exp_retval, "test_ret");
91+
ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val");
7192

93+
done:
7294
dummy_st_ops__destroy(skel);
95+
trace_dummy_st_ops__destroy(trace_skel);
7396
}
7497

7598
static void test_dummy_multiple_args(void)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
6+
int val = 0;
7+
8+
SEC("fentry/test_1")
9+
int BPF_PROG(fentry_test_1, __u64 *st_ops_ctx)
10+
{
11+
__u64 state;
12+
13+
/* Read the traced st_ops arg1 which is a pointer */
14+
bpf_probe_read_kernel(&state, sizeof(__u64), (void *)st_ops_ctx);
15+
/* Read state->val */
16+
bpf_probe_read_kernel(&val, sizeof(__u32), (void *)state);
17+
18+
return 0;
19+
}
20+
21+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)