task_fd_query_tp.c (2294B)
1// SPDX-License-Identifier: GPL-2.0 2#include <test_progs.h> 3 4static void test_task_fd_query_tp_core(const char *probe_name, 5 const char *tp_name) 6{ 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; 13 __u32 duration = 0; 14 char buf[256]; 15 16 err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); 17 if (CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno)) 18 goto close_prog; 19 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)) 24 goto close_prog; 25 bytes = read(efd, buf, sizeof(buf)); 26 close(efd); 27 if (CHECK(bytes <= 0 || bytes >= sizeof(buf), "read", 28 "bytes %d errno %d\n", bytes, errno)) 29 goto close_prog; 30 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 */, 38 0 /* flags */); 39 if (CHECK(err, "perf_event_open", "err %d errno %d\n", err, errno)) 40 goto close_pmu; 41 42 err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); 43 if (CHECK(err, "perf_event_ioc_enable", "err %d errno %d\n", err, 44 errno)) 45 goto close_pmu; 46 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, 49 errno)) 50 goto close_pmu; 51 52 /* query (getpid(), pmu_fd) */ 53 len = sizeof(buf); 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, 57 errno)) 58 goto close_pmu; 59 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", 62 fd_type, buf)) 63 goto close_pmu; 64 65close_pmu: 66 close(pmu_fd); 67close_prog: 68 bpf_object__close(obj); 69} 70 71void test_task_fd_query_tp(void) 72{ 73 test_task_fd_query_tp_core("sched/sched_switch", 74 "sched_switch"); 75 test_task_fd_query_tp_core("syscalls/sys_enter_read", 76 "sys_enter_read"); 77}