xref: /linux/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_stacktrace_build_id.skel.h"
4 
5 void test_stacktrace_build_id(void)
6 {
7 
8 	int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
9 	struct test_stacktrace_build_id *skel;
10 	int err, stack_trace_len, build_id_size;
11 	__u32 key, prev_key, val, duration = 0;
12 	char buf[BPF_BUILD_ID_SIZE];
13 	struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
14 	int build_id_matches = 0;
15 	int i, retry = 1;
16 
17 retry:
18 	skel = test_stacktrace_build_id__open_and_load();
19 	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
20 		return;
21 
22 	err = test_stacktrace_build_id__attach(skel);
23 	if (CHECK(err, "attach_tp", "err %d\n", err))
24 		goto cleanup;
25 
26 	/* find map fds */
27 	control_map_fd = bpf_map__fd(skel->maps.control_map);
28 	stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
29 	stackmap_fd = bpf_map__fd(skel->maps.stackmap);
30 	stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
31 
32 	if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")))
33 		goto cleanup;
34 	if (CHECK_FAIL(system("./urandom_read")))
35 		goto cleanup;
36 	/* disable stack trace collection */
37 	key = 0;
38 	val = 1;
39 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
40 
41 	/* for every element in stackid_hmap, we can find a corresponding one
42 	 * in stackmap, and vise versa.
43 	 */
44 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
45 	if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
46 		  "err %d errno %d\n", err, errno))
47 		goto cleanup;
48 
49 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
50 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
51 		  "err %d errno %d\n", err, errno))
52 		goto cleanup;
53 
54 	build_id_size = read_build_id("urandom_read", buf, sizeof(buf));
55 	err = build_id_size < 0 ? build_id_size : 0;
56 
57 	if (CHECK(err, "read_build_id",
58 		  "err %d errno %d\n", err, errno))
59 		goto cleanup;
60 
61 	err = bpf_map__get_next_key(skel->maps.stackmap, NULL, &key, sizeof(key));
62 	if (CHECK(err, "get_next_key from stackmap",
63 		  "err %d, errno %d\n", err, errno))
64 		goto cleanup;
65 
66 	do {
67 		err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs);
68 		if (CHECK(err, "lookup_elem from stackmap",
69 			  "err %d, errno %d\n", err, errno))
70 			goto cleanup;
71 		for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i)
72 			if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID &&
73 			    id_offs[i].offset != 0) {
74 				if (memcmp(buf, id_offs[i].build_id, build_id_size) == 0)
75 					build_id_matches = 1;
76 			}
77 		prev_key = key;
78 	} while (bpf_map__get_next_key(skel->maps.stackmap, &prev_key, &key, sizeof(key)) == 0);
79 
80 	/* stack_map_get_build_id_offset() is racy and sometimes can return
81 	 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID;
82 	 * try it one more time.
83 	 */
84 	if (build_id_matches < 1 && retry--) {
85 		test_stacktrace_build_id__destroy(skel);
86 		printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
87 		       __func__);
88 		goto retry;
89 	}
90 
91 	if (CHECK(build_id_matches < 1, "build id match",
92 		  "Didn't find expected build ID from the map\n"))
93 		goto cleanup;
94 
95 	stack_trace_len = PERF_MAX_STACK_DEPTH *
96 			  sizeof(struct bpf_stack_build_id);
97 	err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
98 	CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
99 	      "err %d errno %d\n", err, errno);
100 
101 cleanup:
102 	test_stacktrace_build_id__destroy(skel);
103 }
104