| 
 | 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 | +	if (!skel->kconfig->CONFIG_UNWINDER_ORC) {  | 
 | 62 | +		test__skip();  | 
 | 63 | +		goto cleanup;  | 
 | 64 | +	}  | 
 | 65 | + | 
 | 66 | +	skel->links.kprobe_multi_stack_test = bpf_program__attach_kprobe_multi_opts(  | 
 | 67 | +							skel->progs.kprobe_multi_stack_test,  | 
 | 68 | +							"bpf_testmod_stacktrace_test", &opts);  | 
 | 69 | +	if (!ASSERT_OK_PTR(skel->links.kprobe_multi_stack_test, "bpf_program__attach_kprobe_multi_opts"))  | 
 | 70 | +		goto cleanup;  | 
 | 71 | + | 
 | 72 | +	prog_fd = bpf_program__fd(skel->progs.trigger);  | 
 | 73 | +	err = bpf_prog_test_run_opts(prog_fd, &topts);  | 
 | 74 | +	ASSERT_OK(err, "test_run");  | 
 | 75 | +	ASSERT_EQ(topts.retval, 0, "test_run");  | 
 | 76 | + | 
 | 77 | +	trigger_module_test_read(1);  | 
 | 78 | + | 
 | 79 | +	load_kallsyms();  | 
 | 80 | + | 
 | 81 | +	check_stacktrace_ips(bpf_map__fd(skel->maps.stackmap), skel->bss->stack_key, 3,  | 
 | 82 | +			     ksym_get_addr("bpf_testmod_stacktrace_test_3"),  | 
 | 83 | +			     ksym_get_addr("bpf_testmod_stacktrace_test_2"),  | 
 | 84 | +			     ksym_get_addr("bpf_testmod_stacktrace_test_1"),  | 
 | 85 | +			     ksym_get_addr("bpf_testmod_test_read"));  | 
 | 86 | + | 
 | 87 | +cleanup:  | 
 | 88 | +	stacktrace_ips__destroy(skel);  | 
 | 89 | +}  | 
 | 90 | + | 
 | 91 | +static void __test_stacktrace_ips(void)  | 
 | 92 | +{  | 
 | 93 | +	if (test__start_subtest("kprobe_multi"))  | 
 | 94 | +		test_stacktrace_ips_kprobe_multi(false);  | 
 | 95 | +	if (test__start_subtest("kretprobe_multi"))  | 
 | 96 | +		test_stacktrace_ips_kprobe_multi(true);  | 
 | 97 | +}  | 
 | 98 | +#else  | 
 | 99 | +static void __test_stacktrace_ips(void)  | 
 | 100 | +{  | 
 | 101 | +	test__skip();  | 
 | 102 | +}  | 
 | 103 | +#endif  | 
 | 104 | + | 
 | 105 | +void test_stacktrace_ips(void)  | 
 | 106 | +{  | 
 | 107 | +	__test_stacktrace_ips();  | 
 | 108 | +}  | 
0 commit comments