Skip to content

Commit effbb81

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
selftests/bpf: Add an usdt_o1 test case in selftests to cover SIB handling logic
When using GCC on x86-64 to compile an usdt prog with -O1 or higher optimization, the compiler will generate SIB addressing mode for global array and PC-relative addressing mode for global variable, e.g. "1@-96(%rbp,%rax,8)" and "-1@4+t1(%rip)". In this patch: - add usdt_o1 test case to cover SIB addressing usdt argument spec handling logic Signed-off-by: Jiawei Zhao <[email protected]>
1 parent c91fcac commit effbb81

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Jiawei Zhao <[email protected]>. */
3+
#include <test_progs.h>
4+
5+
#include "../sdt.h"
6+
#include "test_usdt_o1.skel.h"
7+
8+
#if (defined(__GNUC__) && !defined(__clang__))
9+
#pragma GCC optimize("O1")
10+
#else
11+
#pragma message("non-gcc compiler: the correct probes might not be installed")
12+
#endif
13+
14+
15+
#define test_value 0xFEDCBA9876543210ULL
16+
#define SEC(name) __attribute__((section(name), used))
17+
18+
int lets_test_this(int);
19+
static volatile __u64 array[1] = {test_value};
20+
21+
static __always_inline void trigger_func(void)
22+
{
23+
/* Base address + offset + (index * scale) */
24+
for (volatile int i = 0; i <= 0; i++)
25+
STAP_PROBE1(test, usdt1, array[i]);
26+
}
27+
28+
static void basic_sib_usdt(void)
29+
{
30+
LIBBPF_OPTS(bpf_usdt_opts, opts);
31+
struct test_usdt_o1 *skel;
32+
struct test_usdt_o1__bss *bss;
33+
int err;
34+
35+
skel = test_usdt_o1__open_and_load();
36+
if (!ASSERT_OK_PTR(skel, "skel_open"))
37+
return;
38+
39+
bss = skel->bss;
40+
bss->my_pid = getpid();
41+
42+
err = test_usdt_o1__attach(skel);
43+
if (!ASSERT_OK(err, "skel_attach"))
44+
goto cleanup;
45+
46+
/* usdt1 won't be auto-attached */
47+
opts.usdt_cookie = 0xcafedeadbeeffeed;
48+
skel->links.usdt1 = bpf_program__attach_usdt(skel->progs.usdt1,
49+
0 /*self*/, "/proc/self/exe",
50+
"test", "usdt1", &opts);
51+
if (!ASSERT_OK_PTR(skel->links.usdt1, "usdt1_link"))
52+
goto cleanup;
53+
54+
trigger_func();
55+
56+
ASSERT_EQ(bss->usdt1_called, 1, "usdt1_called");
57+
ASSERT_EQ(bss->usdt1_cookie, 0xcafedeadbeeffeed, "usdt1_cookie");
58+
ASSERT_EQ(bss->usdt1_arg_cnt, 1, "usdt1_arg_cnt");
59+
ASSERT_EQ(bss->usdt1_arg, test_value, "usdt1_arg");
60+
ASSERT_EQ(bss->usdt1_arg_ret, 0, "usdt1_arg_ret");
61+
ASSERT_EQ(bss->usdt1_arg_size, sizeof(array[0]), "usdt1_arg_size");
62+
63+
cleanup:
64+
test_usdt_o1__destroy(skel);
65+
}
66+
67+
void test_usdt_o1(void)
68+
{
69+
basic_sib_usdt();
70+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/usdt.bpf.h>
7+
8+
int my_pid;
9+
10+
int usdt1_called;
11+
u64 usdt1_cookie;
12+
int usdt1_arg_cnt;
13+
int usdt1_arg_ret;
14+
u64 usdt1_arg;
15+
int usdt1_arg_size;
16+
17+
SEC("usdt")
18+
int usdt1(struct pt_regs *ctx)
19+
{
20+
long tmp;
21+
22+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
23+
return 0;
24+
25+
__sync_fetch_and_add(&usdt1_called, 1);
26+
27+
usdt1_cookie = bpf_usdt_cookie(ctx);
28+
usdt1_arg_cnt = bpf_usdt_arg_cnt(ctx);
29+
30+
usdt1_arg_ret = bpf_usdt_arg(ctx, 0, &tmp);
31+
usdt1_arg = (u64)tmp;
32+
usdt1_arg_size = bpf_usdt_arg_size(ctx, 0);
33+
34+
return 0;
35+
}
36+
37+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)