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"
6 void test_get_stackid_cannot_attach(void)
8 struct perf_event_attr attr
= {
9 /* .type = PERF_TYPE_SOFTWARE, */
10 .type
= PERF_TYPE_HARDWARE
,
11 .config
= PERF_COUNT_HW_CPU_CYCLES
,
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
),
21 struct test_stacktrace_build_id
*skel
;
25 skel
= test_stacktrace_build_id__open();
26 if (CHECK(!skel
, "skel_open", "skeleton open failed\n"))
29 /* override program type */
30 bpf_program__set_perf_event(skel
->progs
.oncpu
);
32 err
= test_stacktrace_build_id__load(skel
);
33 if (CHECK(err
, "skel_load", "skeleton load failed: %d\n", err
))
36 pmu_fd
= syscall(__NR_perf_event_open
, &attr
, -1 /* pid */,
37 0 /* cpu 0 */, -1 /* group id */,
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",
45 if (CHECK(pmu_fd
< 0, "perf_event_open", "err %d errno %d\n",
49 skel
->links
.oncpu
= bpf_program__attach_perf_event(skel
->progs
.oncpu
,
51 CHECK(!IS_ERR(skel
->links
.oncpu
), "attach_perf_event_no_callchain",
52 "should have failed\n");
55 /* add PERF_SAMPLE_CALLCHAIN, attach should succeed */
56 attr
.sample_type
|= PERF_SAMPLE_CALLCHAIN
;
58 pmu_fd
= syscall(__NR_perf_event_open
, &attr
, -1 /* pid */,
59 0 /* cpu 0 */, -1 /* group id */,
62 if (CHECK(pmu_fd
< 0, "perf_event_open", "err %d errno %d\n",
66 skel
->links
.oncpu
= bpf_program__attach_perf_event(skel
->progs
.oncpu
,
68 CHECK(IS_ERR(skel
->links
.oncpu
), "attach_perf_event_callchain",
69 "err: %ld\n", PTR_ERR(skel
->links
.oncpu
));
72 /* add exclude_callchain_kernel, attach should fail */
73 attr
.exclude_callchain_kernel
= 1;
75 pmu_fd
= syscall(__NR_perf_event_open
, &attr
, -1 /* pid */,
76 0 /* cpu 0 */, -1 /* group id */,
79 if (CHECK(pmu_fd
< 0, "perf_event_open", "err %d errno %d\n",
83 skel
->links
.oncpu
= bpf_program__attach_perf_event(skel
->progs
.oncpu
,
85 CHECK(!IS_ERR(skel
->links
.oncpu
), "attach_perf_event_exclude_callchain_kernel",
86 "should have failed\n");
90 test_stacktrace_build_id__destroy(skel
);