1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 void test_stacktrace_map(void)
5 {
6 	int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
7 	const char *file = "./test_stacktrace_map.o";
8 	int bytes, efd, err, pmu_fd, prog_fd, stack_trace_len;
9 	struct perf_event_attr attr = {};
10 	__u32 key, val, duration = 0;
11 	struct bpf_object *obj;
12 	char buf[256];
13 
14 	err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
15 	if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
16 		return;
17 
18 	/* Get the ID for the sched/sched_switch tracepoint */
19 	snprintf(buf, sizeof(buf),
20 		 "/sys/kernel/debug/tracing/events/sched/sched_switch/id");
21 	efd = open(buf, O_RDONLY, 0);
22 	if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno))
23 		goto close_prog;
24 
25 	bytes = read(efd, buf, sizeof(buf));
26 	close(efd);
27 	if (bytes <= 0 || bytes >= sizeof(buf))
28 		goto close_prog;
29 
30 	/* Open the perf event and attach bpf progrram */
31 	attr.config = strtol(buf, NULL, 0);
32 	attr.type = PERF_TYPE_TRACEPOINT;
33 	attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
34 	attr.sample_period = 1;
35 	attr.wakeup_events = 1;
36 	pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
37 			 0 /* cpu 0 */, -1 /* group id */,
38 			 0 /* flags */);
39 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
40 		  pmu_fd, errno))
41 		goto close_prog;
42 
43 	err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
44 	if (err)
45 		goto disable_pmu;
46 
47 	err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
48 	if (err)
49 		goto disable_pmu;
50 
51 	/* find map fds */
52 	control_map_fd = bpf_find_map(__func__, obj, "control_map");
53 	if (control_map_fd < 0)
54 		goto disable_pmu;
55 
56 	stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
57 	if (stackid_hmap_fd < 0)
58 		goto disable_pmu;
59 
60 	stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
61 	if (stackmap_fd < 0)
62 		goto disable_pmu;
63 
64 	stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap");
65 	if (stack_amap_fd < 0)
66 		goto disable_pmu;
67 
68 	/* give some time for bpf program run */
69 	sleep(1);
70 
71 	/* disable stack trace collection */
72 	key = 0;
73 	val = 1;
74 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
75 
76 	/* for every element in stackid_hmap, we can find a corresponding one
77 	 * in stackmap, and vise versa.
78 	 */
79 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
80 	if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
81 		  "err %d errno %d\n", err, errno))
82 		goto disable_pmu_noerr;
83 
84 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
85 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
86 		  "err %d errno %d\n", err, errno))
87 		goto disable_pmu_noerr;
88 
89 	stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
90 	err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
91 	if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
92 		  "err %d errno %d\n", err, errno))
93 		goto disable_pmu_noerr;
94 
95 	goto disable_pmu_noerr;
96 disable_pmu:
97 	error_cnt++;
98 disable_pmu_noerr:
99 	ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
100 	close(pmu_fd);
101 close_prog:
102 	bpf_object__close(obj);
103 }
104