|
| 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 | +} |
0 commit comments