Skip to content

Commit 78c9225

Browse files
committed
selftests/bpf: Add stacktrace ips test for kprobe_multi/kretprobe_multi
Adding test that attaches kprobe/kretprobe multi and verifies the stacktrace matches expected functions. Signed-off-by: Jiri Olsa <[email protected]>
1 parent bf62454 commit 78c9225

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "stacktrace_ips.skel.h"
4+
5+
#ifdef __x86_64__
6+
static int check_stacktrace_ips(int fd, __u32 key, int cnt, ...)
7+
{
8+
__u64 ips[PERF_MAX_STACK_DEPTH];
9+
struct ksyms *ksyms = NULL;
10+
int i, err = 0;
11+
va_list args;
12+
13+
/* sorted by addr */
14+
ksyms = load_kallsyms_local();
15+
if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local"))
16+
return -1;
17+
18+
/* unlikely, but... */
19+
if (!ASSERT_LT(cnt, PERF_MAX_STACK_DEPTH, "check_max"))
20+
return -1;
21+
22+
err = bpf_map_lookup_elem(fd, &key, ips);
23+
if (err)
24+
goto out;
25+
26+
va_start(args, cnt);
27+
28+
for (i = 0; i < cnt; i++) {
29+
unsigned long val;
30+
struct ksym *ksym;
31+
32+
if (!ASSERT_NEQ(ips[i], 0, "ip_not_zero"))
33+
break;
34+
val = va_arg(args, unsigned long);
35+
ksym = ksym_search_local(ksyms, ips[i]);
36+
if (!ASSERT_OK_PTR(ksym, "ksym_search_local"))
37+
break;
38+
ASSERT_EQ(ksym->addr, val, "stack_cmp");
39+
}
40+
41+
va_end(args);
42+
43+
out:
44+
free_kallsyms_local(ksyms);
45+
return err;
46+
}
47+
48+
static void test_stacktrace_ips_kprobe_multi(bool retprobe)
49+
{
50+
LIBBPF_OPTS(bpf_kprobe_multi_opts, opts,
51+
.retprobe = retprobe
52+
);
53+
LIBBPF_OPTS(bpf_test_run_opts, topts);
54+
struct stacktrace_ips *skel;
55+
int prog_fd, err;
56+
57+
skel = stacktrace_ips__open_and_load();
58+
if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
59+
return;
60+
61+
skel->links.kprobe_multi_stack_test = bpf_program__attach_kprobe_multi_opts(
62+
skel->progs.kprobe_multi_stack_test,
63+
"bpf_testmod_stacktrace_test", &opts);
64+
if (!ASSERT_OK_PTR(skel->links.kprobe_multi_stack_test, "bpf_program__attach_kprobe_multi_opts"))
65+
goto cleanup;
66+
67+
prog_fd = bpf_program__fd(skel->progs.trigger);
68+
err = bpf_prog_test_run_opts(prog_fd, &topts);
69+
ASSERT_OK(err, "test_run");
70+
ASSERT_EQ(topts.retval, 0, "test_run");
71+
72+
trigger_module_test_read(1);
73+
74+
load_kallsyms();
75+
76+
check_stacktrace_ips(bpf_map__fd(skel->maps.stackmap), skel->bss->stack_key, 3,
77+
ksym_get_addr("bpf_testmod_stacktrace_test_3"),
78+
ksym_get_addr("bpf_testmod_stacktrace_test_2"),
79+
ksym_get_addr("bpf_testmod_stacktrace_test_1"));
80+
81+
cleanup:
82+
stacktrace_ips__destroy(skel);
83+
}
84+
85+
static void __test_stacktrace_ips(void)
86+
{
87+
if (test__start_subtest("kprobe_multi"))
88+
test_stacktrace_ips_kprobe_multi(false);
89+
if (test__start_subtest("kretprobe_multi"))
90+
test_stacktrace_ips_kprobe_multi(true);
91+
}
92+
#else
93+
static void __test_stacktrace_ips(void)
94+
{
95+
test__skip();
96+
}
97+
#endif
98+
99+
void test_stacktrace_ips(void)
100+
{
101+
__test_stacktrace_ips();
102+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
#ifndef PERF_MAX_STACK_DEPTH
8+
#define PERF_MAX_STACK_DEPTH 127
9+
#endif
10+
11+
typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH];
12+
13+
struct {
14+
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
15+
__uint(max_entries, 16384);
16+
__type(key, __u32);
17+
__type(value, stack_trace_t);
18+
} stackmap SEC(".maps");
19+
20+
__u32 stack_key;
21+
22+
/*
23+
* No tests in here, just to trigger 'bpf_fentry_test*'
24+
* through tracing test_run.
25+
*/
26+
SEC("fentry/bpf_modify_return_test")
27+
int BPF_PROG(trigger)
28+
{
29+
return 0;
30+
}
31+
32+
SEC("kprobe.multi")
33+
int kprobe_multi_stack_test(struct pt_regs *ctx)
34+
{
35+
stack_key = bpf_get_stackid(ctx, &stackmap, 0);
36+
return 0;
37+
}
38+
39+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,30 @@ noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
417417
return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
418418
}
419419

420+
noinline void bpf_testmod_stacktrace_test(void)
421+
{
422+
/* used for stacktrace test as attach function */
423+
asm volatile ("");
424+
}
425+
426+
noinline void bpf_testmod_stacktrace_test_3(void)
427+
{
428+
bpf_testmod_stacktrace_test();
429+
asm volatile ("");
430+
}
431+
432+
noinline void bpf_testmod_stacktrace_test_2(void)
433+
{
434+
bpf_testmod_stacktrace_test_3();
435+
asm volatile ("");
436+
}
437+
438+
noinline void bpf_testmod_stacktrace_test_1(void)
439+
{
440+
bpf_testmod_stacktrace_test_2();
441+
asm volatile ("");
442+
}
443+
420444
int bpf_testmod_fentry_ok;
421445

422446
noinline ssize_t
@@ -497,6 +521,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
497521
21, 22, 23, 24, 25, 26) != 231)
498522
goto out;
499523

524+
bpf_testmod_stacktrace_test_1();
525+
500526
bpf_testmod_fentry_ok = 1;
501527
out:
502528
return -EIO; /* always fail */

0 commit comments

Comments
 (0)