Add a tp subtest that checks the stack captured by bpf_get_stack() from a tracepoint program, recorded only for the test's own process. Depending on static calls and inlining, the stack may or may not have __traceiter_bpf_testmod_test_read in front of bpf_testmod_test_read, so accept both. Also check that the entry after it differs: kernels without commit 6d08340d1e35 ("Revert "perf/x86: Always store regs->ip in perf_callchain_kernel()"") report the tracepoint's caller twice. Signed-off-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/stacktrace_ips.c | 54 +++++++++++++++++++ .../selftests/bpf/progs/stacktrace_ips.c | 13 +++++ 2 files changed, 67 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c index da42b00e3d1f..a03d4c10c469 100644 --- a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c +++ b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c @@ -137,6 +137,58 @@ static void test_stacktrace_ips_raw_tp(void) stacktrace_ips__destroy(skel); } +static void test_stacktrace_ips_tp(void) +{ + __u64 traceiter_addr, read_addr, *ips; + struct stacktrace_ips *skel; + struct ksyms *ksyms = NULL; + struct ksym *ksym; + int i; + + skel = stacktrace_ips__open_and_load(); + if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load")) + return; + + if (!skel->kconfig->CONFIG_UNWINDER_ORC) { + test__skip(); + goto cleanup; + } + + skel->bss->my_pid = getpid(); + + skel->links.tp_test = bpf_program__attach(skel->progs.tp_test); + if (!ASSERT_OK_PTR(skel->links.tp_test, "bpf_program__attach")) + goto cleanup; + + trigger_module_test_read(1); + + ksyms = load_kallsyms_local(); + if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local")) + goto cleanup; + traceiter_addr = ksym_get_addr_local(ksyms, "__traceiter_bpf_testmod_test_read"); + read_addr = ksym_get_addr_local(ksyms, "bpf_testmod_test_read"); + + /* + * Depending on static calls and inlining, __traceiter_bpf_testmod_test_read + * may or may not be in front of bpf_testmod_test_read. + */ + ips = skel->bss->tp_ips; + ksym = ksym_search_local(ksyms, ips[0]); + if (!ASSERT_OK_PTR(ksym, "ksym_search_local")) + goto cleanup; + i = ksym->addr == traceiter_addr ? 1 : 0; + + ksym = ksym_search_local(ksyms, ips[i]); + if (!ASSERT_OK_PTR(ksym, "ksym_search_local")) + goto cleanup; + ASSERT_EQ(ksym->addr, read_addr, "stack_cmp"); + ASSERT_NEQ(ips[i], ips[i + 1], "stack_dup"); + +cleanup: + free_kallsyms_local(ksyms); + stacktrace_ips__destroy(skel); +} + static void test_stacktrace_ips_kprobe(bool retprobe) { LIBBPF_OPTS(bpf_kprobe_opts, opts, @@ -238,6 +290,8 @@ static void __test_stacktrace_ips(void) test_stacktrace_ips_kprobe_multi(true); if (test__start_subtest("raw_tp")) test_stacktrace_ips_raw_tp(); + if (test__start_subtest("tp")) + test_stacktrace_ips_tp(); if (test__start_subtest("kprobe")) test_stacktrace_ips_kprobe(false); if (test__start_subtest("kretprobe")) diff --git a/tools/testing/selftests/bpf/progs/stacktrace_ips.c b/tools/testing/selftests/bpf/progs/stacktrace_ips.c index 6830f2978613..df10ef3cb14d 100644 --- a/tools/testing/selftests/bpf/progs/stacktrace_ips.c +++ b/tools/testing/selftests/bpf/progs/stacktrace_ips.c @@ -53,6 +53,19 @@ int rawtp_test(void *ctx) return 0; } +__u64 tp_ips[128]; +int my_pid; + +SEC("tp/bpf_testmod/bpf_testmod_test_read") +int tp_test(void *ctx) +{ + if (bpf_get_current_pid_tgid() >> 32 != my_pid) + return 0; + + bpf_get_stack(ctx, tp_ips, sizeof(tp_ips), 0); + return 0; +} + SEC("fentry/bpf_testmod_stacktrace_test") int fentry_test(struct pt_regs *ctx) { -- 2.53.1-Meta