xref: /linux/samples/user_events/example.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c57eb478SBeau Belgrave // SPDX-License-Identifier: GPL-2.0-only
2c57eb478SBeau Belgrave /*
3c57eb478SBeau Belgrave  * Copyright (c) 2021, Microsoft Corporation.
4c57eb478SBeau Belgrave  *
5c57eb478SBeau Belgrave  * Authors:
6c57eb478SBeau Belgrave  *   Beau Belgrave <beaub@linux.microsoft.com>
7c57eb478SBeau Belgrave  */
8c57eb478SBeau Belgrave 
9c57eb478SBeau Belgrave #include <errno.h>
10c57eb478SBeau Belgrave #include <sys/ioctl.h>
11c57eb478SBeau Belgrave #include <sys/mman.h>
12*9211ddaaSBeau Belgrave #include <sys/uio.h>
13c57eb478SBeau Belgrave #include <fcntl.h>
14c57eb478SBeau Belgrave #include <stdio.h>
15c57eb478SBeau Belgrave #include <unistd.h>
16c57eb478SBeau Belgrave #include <linux/user_events.h>
17c57eb478SBeau Belgrave 
182455f0e1SRoss Zwisler const char *data_file = "/sys/kernel/tracing/user_events_data";
19*9211ddaaSBeau Belgrave int enabled = 0;
20c57eb478SBeau Belgrave 
event_reg(int fd,const char * command,int * write,int * enabled)21*9211ddaaSBeau Belgrave static int event_reg(int fd, const char *command, int *write, int *enabled)
22c57eb478SBeau Belgrave {
23c57eb478SBeau Belgrave 	struct user_reg reg = {0};
24c57eb478SBeau Belgrave 
25c57eb478SBeau Belgrave 	reg.size = sizeof(reg);
26*9211ddaaSBeau Belgrave 	reg.enable_bit = 31;
27*9211ddaaSBeau Belgrave 	reg.enable_size = sizeof(*enabled);
28*9211ddaaSBeau Belgrave 	reg.enable_addr = (__u64)enabled;
29c57eb478SBeau Belgrave 	reg.name_args = (__u64)command;
30c57eb478SBeau Belgrave 
31c57eb478SBeau Belgrave 	if (ioctl(fd, DIAG_IOCSREG, &reg) == -1)
32c57eb478SBeau Belgrave 		return -1;
33c57eb478SBeau Belgrave 
34c57eb478SBeau Belgrave 	*write = reg.write_index;
35c57eb478SBeau Belgrave 
36c57eb478SBeau Belgrave 	return 0;
37c57eb478SBeau Belgrave }
38c57eb478SBeau Belgrave 
main(int argc,char ** argv)39c57eb478SBeau Belgrave int main(int argc, char **argv)
40c57eb478SBeau Belgrave {
4139d6d08bSBeau Belgrave 	int data_fd, write;
42c57eb478SBeau Belgrave 	struct iovec io[2];
43c57eb478SBeau Belgrave 	__u32 count = 0;
44c57eb478SBeau Belgrave 
45c57eb478SBeau Belgrave 	data_fd = open(data_file, O_RDWR);
46c57eb478SBeau Belgrave 
47*9211ddaaSBeau Belgrave 	if (event_reg(data_fd, "test u32 count", &write, &enabled) == -1)
48c57eb478SBeau Belgrave 		return errno;
49c57eb478SBeau Belgrave 
50c57eb478SBeau Belgrave 	/* Setup iovec */
51c57eb478SBeau Belgrave 	io[0].iov_base = &write;
52c57eb478SBeau Belgrave 	io[0].iov_len = sizeof(write);
53c57eb478SBeau Belgrave 	io[1].iov_base = &count;
54c57eb478SBeau Belgrave 	io[1].iov_len = sizeof(count);
55c57eb478SBeau Belgrave ask:
56c57eb478SBeau Belgrave 	printf("Press enter to check status...\n");
57c57eb478SBeau Belgrave 	getchar();
58c57eb478SBeau Belgrave 
59c57eb478SBeau Belgrave 	/* Check if anyone is listening */
60*9211ddaaSBeau Belgrave 	if (enabled) {
61c57eb478SBeau Belgrave 		/* Yep, trace out our data */
62c57eb478SBeau Belgrave 		writev(data_fd, (const struct iovec *)io, 2);
63c57eb478SBeau Belgrave 
64c57eb478SBeau Belgrave 		/* Increase the count */
65c57eb478SBeau Belgrave 		count++;
66c57eb478SBeau Belgrave 
67c57eb478SBeau Belgrave 		printf("Something was attached, wrote data\n");
68c57eb478SBeau Belgrave 	}
69c57eb478SBeau Belgrave 
70c57eb478SBeau Belgrave 	goto ask;
71c57eb478SBeau Belgrave 
72c57eb478SBeau Belgrave 	return 0;
73c57eb478SBeau Belgrave }
74