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 <asm/bitsperlong.h>
17 #include <linux/user_events.h>
19 #if __BITS_PER_LONG == 64
20 #define endian_swap(x) htole64(x)
22 #define endian_swap(x) htole32(x)
25 /* Assumes debugfs is mounted */
26 const char *data_file = "/sys/kernel/debug/tracing/user_events_data";
27 const char *status_file = "/sys/kernel/debug/tracing/user_events_status";
29 static int event_status(long **status)
31 int fd = open(status_file, O_RDONLY);
33 *status = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ,
38 if (*status == MAP_FAILED)
44 static int event_reg(int fd, const char *command, long *index, long *mask,
47 struct user_reg reg = {0};
49 reg.size = sizeof(reg);
50 reg.name_args = (__u64)command;
52 if (ioctl(fd, DIAG_IOCSREG, ®) == -1)
55 *index = reg.status_bit / __BITS_PER_LONG;
56 *mask = endian_swap(1L << (reg.status_bit % __BITS_PER_LONG));
57 *write = reg.write_index;
62 int main(int argc, char **argv)
70 if (event_status(&status_page) == -1)
73 data_fd = open(data_file, O_RDWR);
75 if (event_reg(data_fd, "test u32 count", &index, &mask, &write) == -1)
79 io[0].iov_base = &write;
80 io[0].iov_len = sizeof(write);
81 io[1].iov_base = &count;
82 io[1].iov_len = sizeof(count);
85 printf("Press enter to check status...\n");
88 /* Check if anyone is listening */
89 if (status_page[index] & mask) {
90 /* Yep, trace out our data */
91 writev(data_fd, (const struct iovec *)io, 2);
93 /* Increase the count */
96 printf("Something was attached, wrote data\n");