xref: /linux/tools/testing/selftests/bpf/prog_tests/stacktrace_map_skip.c (revision 69bfec7548f4c1595bac0e3ddfc0458a5af31f4c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "stacktrace_map_skip.skel.h"
4 
5 #define TEST_STACK_DEPTH  2
6 
7 void test_stacktrace_map_skip(void)
8 {
9 	struct stacktrace_map_skip *skel;
10 	int stackid_hmap_fd, stackmap_fd, stack_amap_fd;
11 	int err, stack_trace_len;
12 
13 	skel = stacktrace_map_skip__open_and_load();
14 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
15 		return;
16 
17 	/* find map fds */
18 	stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
19 	if (!ASSERT_GE(stackid_hmap_fd, 0, "stackid_hmap fd"))
20 		goto out;
21 
22 	stackmap_fd = bpf_map__fd(skel->maps.stackmap);
23 	if (!ASSERT_GE(stackmap_fd, 0, "stackmap fd"))
24 		goto out;
25 
26 	stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
27 	if (!ASSERT_GE(stack_amap_fd, 0, "stack_amap fd"))
28 		goto out;
29 
30 	skel->bss->pid = getpid();
31 
32 	err = stacktrace_map_skip__attach(skel);
33 	if (!ASSERT_OK(err, "skel_attach"))
34 		goto out;
35 
36 	/* give some time for bpf program run */
37 	sleep(1);
38 
39 	/* disable stack trace collection */
40 	skel->bss->control = 1;
41 
42 	/* for every element in stackid_hmap, we can find a corresponding one
43 	 * in stackmap, and vise versa.
44 	 */
45 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
46 	if (!ASSERT_OK(err, "compare_map_keys stackid_hmap vs. stackmap"))
47 		goto out;
48 
49 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
50 	if (!ASSERT_OK(err, "compare_map_keys stackmap vs. stackid_hmap"))
51 		goto out;
52 
53 	stack_trace_len = TEST_STACK_DEPTH * sizeof(__u64);
54 	err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
55 	if (!ASSERT_OK(err, "compare_stack_ips stackmap vs. stack_amap"))
56 		goto out;
57 
58 	if (!ASSERT_EQ(skel->bss->failed, 0, "skip_failed"))
59 		goto out;
60 
61 out:
62 	stacktrace_map_skip__destroy(skel);
63 }
64