• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_stacktrace_build_id.skel.h"
4 
test_stacktrace_build_id_nmi(void)5 void test_stacktrace_build_id_nmi(void)
6 {
7 	int control_map_fd, stackid_hmap_fd, stackmap_fd;
8 	struct test_stacktrace_build_id *skel;
9 	int err, pmu_fd;
10 	struct perf_event_attr attr = {
11 		.freq = 1,
12 		.type = PERF_TYPE_HARDWARE,
13 		.config = PERF_COUNT_HW_CPU_CYCLES,
14 	};
15 	__u32 key, prev_key, val, duration = 0;
16 	char buf[256];
17 	int i, j;
18 	struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
19 	int build_id_matches = 0;
20 	int retry = 1;
21 
22 	attr.sample_freq = read_perf_max_sample_freq();
23 
24 retry:
25 	skel = test_stacktrace_build_id__open();
26 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
27 		return;
28 
29 	/* override program type */
30 	bpf_program__set_type(skel->progs.oncpu, BPF_PROG_TYPE_PERF_EVENT);
31 
32 	err = test_stacktrace_build_id__load(skel);
33 	if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err))
34 		goto cleanup;
35 
36 	pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
37 			 0 /* cpu 0 */, -1 /* group id */,
38 			 0 /* flags */);
39 	if (pmu_fd < 0 && errno == ENOENT) {
40 		printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
41 		test__skip();
42 		goto cleanup;
43 	}
44 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
45 		  pmu_fd, errno))
46 		goto cleanup;
47 
48 	skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
49 							   pmu_fd);
50 	if (!ASSERT_OK_PTR(skel->links.oncpu, "attach_perf_event")) {
51 		close(pmu_fd);
52 		goto cleanup;
53 	}
54 
55 	/* find map fds */
56 	control_map_fd = bpf_map__fd(skel->maps.control_map);
57 	stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
58 	stackmap_fd = bpf_map__fd(skel->maps.stackmap);
59 
60 	if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")))
61 		goto cleanup;
62 	if (CHECK_FAIL(system("taskset 0x1 ./urandom_read 100000")))
63 		goto cleanup;
64 	/* disable stack trace collection */
65 	key = 0;
66 	val = 1;
67 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
68 
69 	/* for every element in stackid_hmap, we can find a corresponding one
70 	 * in stackmap, and vise versa.
71 	 */
72 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
73 	if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
74 		  "err %d errno %d\n", err, errno))
75 		goto cleanup;
76 
77 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
78 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
79 		  "err %d errno %d\n", err, errno))
80 		goto cleanup;
81 
82 	err = extract_build_id(buf, 256);
83 
84 	if (CHECK(err, "get build_id with readelf",
85 		  "err %d errno %d\n", err, errno))
86 		goto cleanup;
87 
88 	err = bpf_map__get_next_key(skel->maps.stackmap, NULL, &key, sizeof(key));
89 	if (CHECK(err, "get_next_key from stackmap",
90 		  "err %d, errno %d\n", err, errno))
91 		goto cleanup;
92 
93 	do {
94 		char build_id[64];
95 
96 		err = bpf_map__lookup_elem(skel->maps.stackmap, &key, sizeof(key),
97 					   id_offs, sizeof(id_offs), 0);
98 		if (CHECK(err, "lookup_elem from stackmap",
99 			  "err %d, errno %d\n", err, errno))
100 			goto cleanup;
101 		for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i)
102 			if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID &&
103 			    id_offs[i].offset != 0) {
104 				for (j = 0; j < 20; ++j)
105 					sprintf(build_id + 2 * j, "%02x",
106 						id_offs[i].build_id[j] & 0xff);
107 				if (strstr(buf, build_id) != NULL)
108 					build_id_matches = 1;
109 			}
110 		prev_key = key;
111 	} while (bpf_map__get_next_key(skel->maps.stackmap, &prev_key, &key, sizeof(key)) == 0);
112 
113 	/* stack_map_get_build_id_offset() is racy and sometimes can return
114 	 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID;
115 	 * try it one more time.
116 	 */
117 	if (build_id_matches < 1 && retry--) {
118 		test_stacktrace_build_id__destroy(skel);
119 		printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
120 		       __func__);
121 		goto retry;
122 	}
123 
124 	if (CHECK(build_id_matches < 1, "build id match",
125 		  "Didn't find expected build ID from the map\n"))
126 		goto cleanup;
127 
128 	/*
129 	 * We intentionally skip compare_stack_ips(). This is because we
130 	 * only support one in_nmi() ips-to-build_id translation per cpu
131 	 * at any time, thus stack_amap here will always fallback to
132 	 * BPF_STACK_BUILD_ID_IP;
133 	 */
134 
135 cleanup:
136 	test_stacktrace_build_id__destroy(skel);
137 }
138