Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020 Facebook
0003 #include <test_progs.h>
0004 #include "test_stacktrace_build_id.skel.h"
0005 
0006 void test_get_stackid_cannot_attach(void)
0007 {
0008     struct perf_event_attr attr = {
0009         /* .type = PERF_TYPE_SOFTWARE, */
0010         .type = PERF_TYPE_HARDWARE,
0011         .config = PERF_COUNT_HW_CPU_CYCLES,
0012         .precise_ip = 1,
0013         .sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_BRANCH_STACK,
0014         .branch_sample_type = PERF_SAMPLE_BRANCH_USER |
0015             PERF_SAMPLE_BRANCH_NO_FLAGS |
0016             PERF_SAMPLE_BRANCH_NO_CYCLES |
0017             PERF_SAMPLE_BRANCH_CALL_STACK,
0018         .sample_period = 5000,
0019         .size = sizeof(struct perf_event_attr),
0020     };
0021     struct test_stacktrace_build_id *skel;
0022     __u32 duration = 0;
0023     int pmu_fd, err;
0024 
0025     skel = test_stacktrace_build_id__open();
0026     if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
0027         return;
0028 
0029     /* override program type */
0030     bpf_program__set_type(skel->progs.oncpu, BPF_PROG_TYPE_PERF_EVENT);
0031 
0032     err = test_stacktrace_build_id__load(skel);
0033     if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err))
0034         goto cleanup;
0035 
0036     pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
0037              0 /* cpu 0 */, -1 /* group id */,
0038              0 /* flags */);
0039     if (pmu_fd < 0 && (errno == ENOENT || errno == EOPNOTSUPP)) {
0040         printf("%s:SKIP:cannot open PERF_COUNT_HW_CPU_CYCLES with precise_ip > 0\n",
0041                __func__);
0042         test__skip();
0043         goto cleanup;
0044     }
0045     if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
0046           pmu_fd, errno))
0047         goto cleanup;
0048 
0049     skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
0050                                pmu_fd);
0051     ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_no_callchain");
0052     close(pmu_fd);
0053 
0054     /* add PERF_SAMPLE_CALLCHAIN, attach should succeed */
0055     attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
0056 
0057     pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
0058              0 /* cpu 0 */, -1 /* group id */,
0059              0 /* flags */);
0060 
0061     if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
0062           pmu_fd, errno))
0063         goto cleanup;
0064 
0065     skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
0066                                pmu_fd);
0067     ASSERT_OK_PTR(skel->links.oncpu, "attach_perf_event_callchain");
0068     close(pmu_fd);
0069 
0070     /* add exclude_callchain_kernel, attach should fail */
0071     attr.exclude_callchain_kernel = 1;
0072 
0073     pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
0074              0 /* cpu 0 */, -1 /* group id */,
0075              0 /* flags */);
0076 
0077     if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
0078           pmu_fd, errno))
0079         goto cleanup;
0080 
0081     skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
0082                                pmu_fd);
0083     ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_exclude_callchain_kernel");
0084     close(pmu_fd);
0085 
0086 cleanup:
0087     test_stacktrace_build_id__destroy(skel);
0088 }