1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 #include <test_progs.h>
4 #include "test_stacktrace_build_id.skel.h"
5 
test_get_stackid_cannot_attach(void)6 void test_get_stackid_cannot_attach(void)
7 {
8 	struct perf_event_attr attr = {
9 		/* .type = PERF_TYPE_SOFTWARE, */
10 		.type = PERF_TYPE_HARDWARE,
11 		.config = PERF_COUNT_HW_CPU_CYCLES,
12 		.precise_ip = 1,
13 		.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_BRANCH_STACK,
14 		.branch_sample_type = PERF_SAMPLE_BRANCH_USER |
15 			PERF_SAMPLE_BRANCH_NO_FLAGS |
16 			PERF_SAMPLE_BRANCH_NO_CYCLES |
17 			PERF_SAMPLE_BRANCH_CALL_STACK,
18 		.sample_period = 5000,
19 		.size = sizeof(struct perf_event_attr),
20 	};
21 	struct test_stacktrace_build_id *skel;
22 	__u32 duration = 0;
23 	int pmu_fd, err;
24 
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_perf_event(skel->progs.oncpu);
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 || errno == EOPNOTSUPP)) {
40 		printf("%s:SKIP:cannot open PERF_COUNT_HW_CPU_CYCLES with precise_ip > 0\n",
41 		       __func__);
42 		test__skip();
43 		goto cleanup;
44 	}
45 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
46 		  pmu_fd, errno))
47 		goto cleanup;
48 
49 	skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
50 							   pmu_fd);
51 	CHECK(!IS_ERR(skel->links.oncpu), "attach_perf_event_no_callchain",
52 	      "should have failed\n");
53 	close(pmu_fd);
54 
55 	/* add PERF_SAMPLE_CALLCHAIN, attach should succeed */
56 	attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
57 
58 	pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
59 			 0 /* cpu 0 */, -1 /* group id */,
60 			 0 /* flags */);
61 
62 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
63 		  pmu_fd, errno))
64 		goto cleanup;
65 
66 	skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
67 							   pmu_fd);
68 	CHECK(IS_ERR(skel->links.oncpu), "attach_perf_event_callchain",
69 	      "err: %ld\n", PTR_ERR(skel->links.oncpu));
70 	close(pmu_fd);
71 
72 	/* add exclude_callchain_kernel, attach should fail */
73 	attr.exclude_callchain_kernel = 1;
74 
75 	pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
76 			 0 /* cpu 0 */, -1 /* group id */,
77 			 0 /* flags */);
78 
79 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
80 		  pmu_fd, errno))
81 		goto cleanup;
82 
83 	skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
84 							   pmu_fd);
85 	CHECK(!IS_ERR(skel->links.oncpu), "attach_perf_event_exclude_callchain_kernel",
86 	      "should have failed\n");
87 	close(pmu_fd);
88 
89 cleanup:
90 	test_stacktrace_build_id__destroy(skel);
91 }
92