1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, Microsoft Corporation.
6 * Beau Belgrave <beaub@linux.microsoft.com>
10 #include <sys/ioctl.h>
15 #include <linux/user_events.h>
17 /* Assumes debugfs is mounted */
18 const char *data_file = "/sys/kernel/debug/tracing/user_events_data";
19 const char *status_file = "/sys/kernel/debug/tracing/user_events_status";
21 static int event_status(char **status)
23 int fd = open(status_file, O_RDONLY);
25 *status = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ,
30 if (*status == MAP_FAILED)
36 static int event_reg(int fd, const char *command, int *status, int *write)
38 struct user_reg reg = {0};
40 reg.size = sizeof(reg);
41 reg.name_args = (__u64)command;
43 if (ioctl(fd, DIAG_IOCSREG, ®) == -1)
46 *status = reg.status_index;
47 *write = reg.write_index;
52 int main(int argc, char **argv)
54 int data_fd, status, write;
59 if (event_status(&status_page) == -1)
62 data_fd = open(data_file, O_RDWR);
64 if (event_reg(data_fd, "test u32 count", &status, &write) == -1)
68 io[0].iov_base = &write;
69 io[0].iov_len = sizeof(write);
70 io[1].iov_base = &count;
71 io[1].iov_len = sizeof(count);
74 printf("Press enter to check status...\n");
77 /* Check if anyone is listening */
78 if (status_page[status]) {
79 /* Yep, trace out our data */
80 writev(data_fd, (const struct iovec *)io, 2);
82 /* Increase the count */
85 printf("Something was attached, wrote data\n");