1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 static void test_task_fd_query_tp_core(const char *probe_name
,
7 const char *file
= "./test_tracepoint.o";
8 int err
, bytes
, efd
, prog_fd
, pmu_fd
;
9 struct perf_event_attr attr
= {};
10 __u64 probe_offset
, probe_addr
;
11 __u32 len
, prog_id
, fd_type
;
12 struct bpf_object
*obj
= NULL
;
16 err
= bpf_prog_load(file
, BPF_PROG_TYPE_TRACEPOINT
, &obj
, &prog_fd
);
17 if (CHECK(err
, "bpf_prog_load", "err %d errno %d\n", err
, errno
))
20 snprintf(buf
, sizeof(buf
),
21 "/sys/kernel/debug/tracing/events/%s/id", probe_name
);
22 efd
= open(buf
, O_RDONLY
, 0);
23 if (CHECK(efd
< 0, "open", "err %d errno %d\n", efd
, errno
))
25 bytes
= read(efd
, buf
, sizeof(buf
));
27 if (CHECK(bytes
<= 0 || bytes
>= sizeof(buf
), "read",
28 "bytes %d errno %d\n", bytes
, errno
))
31 attr
.config
= strtol(buf
, NULL
, 0);
32 attr
.type
= PERF_TYPE_TRACEPOINT
;
33 attr
.sample_type
= PERF_SAMPLE_RAW
;
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 */,
39 if (CHECK(err
, "perf_event_open", "err %d errno %d\n", err
, errno
))
42 err
= ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
, 0);
43 if (CHECK(err
, "perf_event_ioc_enable", "err %d errno %d\n", err
,
47 err
= ioctl(pmu_fd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
);
48 if (CHECK(err
, "perf_event_ioc_set_bpf", "err %d errno %d\n", err
,
52 /* query (getpid(), pmu_fd) */
54 err
= bpf_task_fd_query(getpid(), pmu_fd
, 0, buf
, &len
, &prog_id
,
55 &fd_type
, &probe_offset
, &probe_addr
);
56 if (CHECK(err
< 0, "bpf_task_fd_query", "err %d errno %d\n", err
,
60 err
= (fd_type
== BPF_FD_TYPE_TRACEPOINT
) && !strcmp(buf
, tp_name
);
61 if (CHECK(!err
, "check_results", "fd_type %d tp_name %s\n",
66 goto close_prog_noerr
;
73 bpf_object__close(obj
);
76 void test_task_fd_query_tp(void)
78 test_task_fd_query_tp_core("sched/sched_switch",
80 test_task_fd_query_tp_core("syscalls/sys_enter_read",