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;
11 	__u32 key, prev_key, val, duration = 0;
12 	char buf[256];
13 	int i, j;
14 	struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
15 	int build_id_matches = 0;
16 	int retry = 1;
17 
18 retry:
19 	skel = test_stacktrace_build_id__open_and_load();
20 	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
21 		return;
22 
23 	err = test_stacktrace_build_id__attach(skel);
24 	if (CHECK(err, "attach_tp", "err %d\n", err))
25 		goto cleanup;
26 
27 	/* find map fds */
28 	control_map_fd = bpf_map__fd(skel->maps.control_map);
29 	stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
30 	stackmap_fd = bpf_map__fd(skel->maps.stackmap);
31 	stack_amap_fd = bpf_map__fd(skel->maps.stack_amap);
32 
33 	if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")))
34 		goto cleanup;
35 	if (CHECK_FAIL(system("./urandom_read")))
36 		goto cleanup;
37 	/* disable stack trace collection */
38 	key = 0;
39 	val = 1;
40 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
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 (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
47 		  "err %d errno %d\n", err, errno))
48 		goto cleanup;
49 
50 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
51 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
52 		  "err %d errno %d\n", err, errno))
53 		goto cleanup;
54 
55 	err = extract_build_id(buf, 256);
56 
57 	if (CHECK(err, "get build_id with readelf",
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 		char build_id[64];
68 
69 		err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs);
70 		if (CHECK(err, "lookup_elem from stackmap",
71 			  "err %d, errno %d\n", err, errno))
72 			goto cleanup;
73 		for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i)
74 			if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID &&
75 			    id_offs[i].offset != 0) {
76 				for (j = 0; j < 20; ++j)
77 					sprintf(build_id + 2 * j, "%02x",
78 						id_offs[i].build_id[j] & 0xff);
79 				if (strstr(buf, build_id) != NULL)
80 					build_id_matches = 1;
81 			}
82 		prev_key = key;
83 	} while (bpf_map__get_next_key(skel->maps.stackmap, &prev_key, &key, sizeof(key)) == 0);
84 
85 	/* stack_map_get_build_id_offset() is racy and sometimes can return
86 	 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID;
87 	 * try it one more time.
88 	 */
89 	if (build_id_matches < 1 && retry--) {
90 		test_stacktrace_build_id__destroy(skel);
91 		printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
92 		       __func__);
93 		goto retry;
94 	}
95 
96 	if (CHECK(build_id_matches < 1, "build id match",
97 		  "Didn't find expected build ID from the map\n"))
98 		goto cleanup;
99 
100 	stack_trace_len = PERF_MAX_STACK_DEPTH *
101 			  sizeof(struct bpf_stack_build_id);
102 	err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
103 	CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
104 	      "err %d errno %d\n", err, errno);
105 
106 cleanup:
107 	test_stacktrace_build_id__destroy(skel);
108 }
109