1346938e9SSong Liu // SPDX-License-Identifier: GPL-2.0
2346938e9SSong Liu // Copyright (c) 2020 Facebook
3346938e9SSong Liu #include <test_progs.h>
4346938e9SSong Liu #include "test_stacktrace_build_id.skel.h"
5346938e9SSong Liu
test_get_stackid_cannot_attach(void)6346938e9SSong Liu void test_get_stackid_cannot_attach(void)
7346938e9SSong Liu {
8346938e9SSong Liu struct perf_event_attr attr = {
9346938e9SSong Liu /* .type = PERF_TYPE_SOFTWARE, */
10346938e9SSong Liu .type = PERF_TYPE_HARDWARE,
11346938e9SSong Liu .config = PERF_COUNT_HW_CPU_CYCLES,
12346938e9SSong Liu .precise_ip = 1,
13346938e9SSong Liu .sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_BRANCH_STACK,
14346938e9SSong Liu .branch_sample_type = PERF_SAMPLE_BRANCH_USER |
15346938e9SSong Liu PERF_SAMPLE_BRANCH_NO_FLAGS |
16346938e9SSong Liu PERF_SAMPLE_BRANCH_NO_CYCLES |
17346938e9SSong Liu PERF_SAMPLE_BRANCH_CALL_STACK,
18346938e9SSong Liu .sample_period = 5000,
19346938e9SSong Liu .size = sizeof(struct perf_event_attr),
20346938e9SSong Liu };
21346938e9SSong Liu struct test_stacktrace_build_id *skel;
22346938e9SSong Liu __u32 duration = 0;
23346938e9SSong Liu int pmu_fd, err;
24346938e9SSong Liu
25346938e9SSong Liu skel = test_stacktrace_build_id__open();
26346938e9SSong Liu if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
27346938e9SSong Liu return;
28346938e9SSong Liu
29346938e9SSong Liu /* override program type */
30379d19ecSAndrii Nakryiko bpf_program__set_type(skel->progs.oncpu, BPF_PROG_TYPE_PERF_EVENT);
31346938e9SSong Liu
32346938e9SSong Liu err = test_stacktrace_build_id__load(skel);
33346938e9SSong Liu if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err))
34346938e9SSong Liu goto cleanup;
35346938e9SSong Liu
36346938e9SSong Liu pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
37346938e9SSong Liu 0 /* cpu 0 */, -1 /* group id */,
38346938e9SSong Liu 0 /* flags */);
39346938e9SSong Liu if (pmu_fd < 0 && (errno == ENOENT || errno == EOPNOTSUPP)) {
40346938e9SSong Liu printf("%s:SKIP:cannot open PERF_COUNT_HW_CPU_CYCLES with precise_ip > 0\n",
41346938e9SSong Liu __func__);
42346938e9SSong Liu test__skip();
43346938e9SSong Liu goto cleanup;
44346938e9SSong Liu }
45346938e9SSong Liu if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
46346938e9SSong Liu pmu_fd, errno))
47346938e9SSong Liu goto cleanup;
48346938e9SSong Liu
49346938e9SSong Liu skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
50346938e9SSong Liu pmu_fd);
51bad2e478SAndrii Nakryiko ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_no_callchain");
52346938e9SSong Liu close(pmu_fd);
53346938e9SSong Liu
54346938e9SSong Liu /* add PERF_SAMPLE_CALLCHAIN, attach should succeed */
55346938e9SSong Liu attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
56346938e9SSong Liu
57346938e9SSong Liu pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
58346938e9SSong Liu 0 /* cpu 0 */, -1 /* group id */,
59346938e9SSong Liu 0 /* flags */);
60346938e9SSong Liu
61346938e9SSong Liu if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
62346938e9SSong Liu pmu_fd, errno))
63346938e9SSong Liu goto cleanup;
64346938e9SSong Liu
65346938e9SSong Liu skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
66346938e9SSong Liu pmu_fd);
67bad2e478SAndrii Nakryiko ASSERT_OK_PTR(skel->links.oncpu, "attach_perf_event_callchain");
68*c1e07a80SSong Liu bpf_link__destroy(skel->links.oncpu);
69346938e9SSong Liu close(pmu_fd);
70346938e9SSong Liu
71346938e9SSong Liu /* add exclude_callchain_kernel, attach should fail */
72346938e9SSong Liu attr.exclude_callchain_kernel = 1;
73346938e9SSong Liu
74346938e9SSong Liu pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
75346938e9SSong Liu 0 /* cpu 0 */, -1 /* group id */,
76346938e9SSong Liu 0 /* flags */);
77346938e9SSong Liu
78346938e9SSong Liu if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
79346938e9SSong Liu pmu_fd, errno))
80346938e9SSong Liu goto cleanup;
81346938e9SSong Liu
82346938e9SSong Liu skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
83346938e9SSong Liu pmu_fd);
84bad2e478SAndrii Nakryiko ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_exclude_callchain_kernel");
85346938e9SSong Liu close(pmu_fd);
86346938e9SSong Liu
87346938e9SSong Liu cleanup:
88346938e9SSong Liu test_stacktrace_build_id__destroy(skel);
89346938e9SSong Liu }
90