Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tracing/user_events: Use write ABI in example

The ABI has changed to use a remote write approach. Update the example
to show the expected use of this new ABI. Also remove debugfs
path and use tracefs to ensure example works in more environments.

Link: https://lkml.kernel.org/r/20230328235219.203-9-beaub@linux.microsoft.com

Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Beau Belgrave and committed by
Steven Rostedt (Google)
9211ddaa 60b1af8d

+8 -37
+8 -37
samples/user_events/example.c
··· 9 9 #include <errno.h> 10 10 #include <sys/ioctl.h> 11 11 #include <sys/mman.h> 12 + #include <sys/uio.h> 12 13 #include <fcntl.h> 13 14 #include <stdio.h> 14 15 #include <unistd.h> 15 - #include <asm/bitsperlong.h> 16 - #include <endian.h> 17 16 #include <linux/user_events.h> 18 17 19 - #if __BITS_PER_LONG == 64 20 - #define endian_swap(x) htole64(x) 21 - #else 22 - #define endian_swap(x) htole32(x) 23 - #endif 24 - 25 - /* Assumes debugfs is mounted */ 26 18 const char *data_file = "/sys/kernel/tracing/user_events_data"; 27 - const char *status_file = "/sys/kernel/tracing/user_events_status"; 19 + int enabled = 0; 28 20 29 - static int event_status(long **status) 30 - { 31 - int fd = open(status_file, O_RDONLY); 32 - 33 - *status = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ, 34 - MAP_SHARED, fd, 0); 35 - 36 - close(fd); 37 - 38 - if (*status == MAP_FAILED) 39 - return -1; 40 - 41 - return 0; 42 - } 43 - 44 - static int event_reg(int fd, const char *command, long *index, long *mask, 45 - int *write) 21 + static int event_reg(int fd, const char *command, int *write, int *enabled) 46 22 { 47 23 struct user_reg reg = {0}; 48 24 49 25 reg.size = sizeof(reg); 26 + reg.enable_bit = 31; 27 + reg.enable_size = sizeof(*enabled); 28 + reg.enable_addr = (__u64)enabled; 50 29 reg.name_args = (__u64)command; 51 30 52 31 if (ioctl(fd, DIAG_IOCSREG, &reg) == -1) 53 32 return -1; 54 33 55 - *index = reg.status_bit / __BITS_PER_LONG; 56 - *mask = endian_swap(1L << (reg.status_bit % __BITS_PER_LONG)); 57 34 *write = reg.write_index; 58 35 59 36 return 0; ··· 39 62 int main(int argc, char **argv) 40 63 { 41 64 int data_fd, write; 42 - long index, mask; 43 - long *status_page; 44 65 struct iovec io[2]; 45 66 __u32 count = 0; 46 67 47 - if (event_status(&status_page) == -1) 48 - return errno; 49 - 50 68 data_fd = open(data_file, O_RDWR); 51 69 52 - if (event_reg(data_fd, "test u32 count", &index, &mask, &write) == -1) 70 + if (event_reg(data_fd, "test u32 count", &write, &enabled) == -1) 53 71 return errno; 54 72 55 73 /* Setup iovec */ ··· 52 80 io[0].iov_len = sizeof(write); 53 81 io[1].iov_base = &count; 54 82 io[1].iov_len = sizeof(count); 55 - 56 83 ask: 57 84 printf("Press enter to check status...\n"); 58 85 getchar(); 59 86 60 87 /* Check if anyone is listening */ 61 - if (status_page[index] & mask) { 88 + if (enabled) { 62 89 /* Yep, trace out our data */ 63 90 writev(data_fd, (const struct iovec *)io, 2); 64 91