1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_stacktrace_build_id.skel.h"
5 void test_stacktrace_build_id(void)
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;
14 struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
15 int build_id_matches = 0;
19 skel = test_stacktrace_build_id__open_and_load();
20 if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
23 err = test_stacktrace_build_id__attach(skel);
24 if (CHECK(err, "attach_tp", "err %d\n", err))
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);
33 if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")))
35 if (CHECK_FAIL(system("./urandom_read")))
37 /* disable stack trace collection */
40 bpf_map_update_elem(control_map_fd, &key, &val, 0);
42 /* for every element in stackid_hmap, we can find a corresponding one
43 * in stackmap, and vise versa.
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))
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))
55 err = extract_build_id(buf, 256);
57 if (CHECK(err, "get build_id with readelf",
58 "err %d errno %d\n", err, errno))
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))
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))
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)
83 } while (bpf_map__get_next_key(skel->maps.stackmap, &prev_key, &key, sizeof(key)) == 0);
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.
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",
96 if (CHECK(build_id_matches < 1, "build id match",
97 "Didn't find expected build ID from the map\n"))
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);
107 test_stacktrace_build_id__destroy(skel);