1 // SPDX-License-Identifier: GPL-2.0
3 * User Events FTrace Test Program
5 * Copyright (c) 2021 Beau Belgrave <beaub@linux.microsoft.com>
9 #include <linux/user_events.h>
13 #include <sys/ioctl.h>
18 #include "../kselftest_harness.h"
19 #include "user_events_selftests.h"
21 const char *data_file = "/sys/kernel/tracing/user_events_data";
22 const char *status_file = "/sys/kernel/tracing/user_events_status";
23 const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/enable";
24 const char *trace_file = "/sys/kernel/tracing/trace";
25 const char *fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format";
27 static int trace_bytes(void)
29 int fd = open(trace_file, O_RDONLY);
37 got = read(fd, buf, sizeof(buf));
53 static int skip_until_empty_line(FILE *fp)
63 if (last == '\n' && c == '\n')
72 static int get_print_fmt(char *buffer, int len)
74 FILE *fp = fopen(fmt_file, "r");
80 /* Read until empty line (Skip Common) */
81 if (skip_until_empty_line(fp) < 0)
84 /* Read until empty line (Skip Properties) */
85 if (skip_until_empty_line(fp) < 0)
88 /* Read in print_fmt: */
89 if (fgets(buffer, len, fp) == NULL)
92 newline = strchr(buffer, '\n');
106 static bool wait_for_delete(void)
110 for (i = 0; i < 1000; ++i) {
111 int fd = open(enable_file, O_RDONLY);
123 static int clear(int *check)
125 struct user_unreg unreg = {0};
128 unreg.size = sizeof(unreg);
129 unreg.disable_bit = 31;
130 unreg.disable_addr = (__u64)check;
132 fd = open(data_file, O_RDWR);
137 if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1)
141 if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1) {
142 if (errno == EBUSY) {
143 if (!wait_for_delete())
145 } else if (errno != ENOENT)
158 static int check_print_fmt(const char *event, const char *expected, int *check)
160 struct user_reg reg = {0};
171 fd = open(data_file, O_RDWR);
176 reg.size = sizeof(reg);
177 reg.name_args = (__u64)event;
179 reg.enable_addr = (__u64)check;
180 reg.enable_size = sizeof(*check);
182 /* Register should work */
183 ret = ioctl(fd, DIAG_IOCSREG, ®);
187 printf("Reg failed in fmt\n");
191 /* Ensure correct print_fmt */
192 ret = get_print_fmt(print_fmt, sizeof(print_fmt));
199 return strcmp(print_fmt, expected);
209 FIXTURE_SETUP(user) {
210 USER_EVENT_FIXTURE_SETUP(return);
212 self->status_fd = open(status_file, O_RDONLY);
213 ASSERT_NE(-1, self->status_fd);
215 self->data_fd = open(data_file, O_RDWR);
216 ASSERT_NE(-1, self->data_fd);
218 self->enable_fd = -1;
221 FIXTURE_TEARDOWN(user) {
222 close(self->status_fd);
223 close(self->data_fd);
225 if (self->enable_fd != -1) {
226 write(self->enable_fd, "0", sizeof("0"));
227 close(self->enable_fd);
230 if (clear(&self->check) != 0)
231 printf("WARNING: Clear didn't work!\n");
234 TEST_F(user, register_events) {
235 struct user_reg reg = {0};
236 struct user_unreg unreg = {0};
238 reg.size = sizeof(reg);
239 reg.name_args = (__u64)"__test_event u32 field1; u32 field2";
241 reg.enable_addr = (__u64)&self->check;
242 reg.enable_size = sizeof(self->check);
244 unreg.size = sizeof(unreg);
245 unreg.disable_bit = 31;
246 unreg.disable_addr = (__u64)&self->check;
248 /* Register should work */
249 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
250 ASSERT_EQ(0, reg.write_index);
252 /* Multiple registers to the same addr + bit should fail */
253 ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSREG, ®));
254 ASSERT_EQ(EADDRINUSE, errno);
256 /* Multiple registers to same name should result in same index */
258 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
259 ASSERT_EQ(0, reg.write_index);
261 /* Multiple registers to same name but different args should fail */
263 reg.name_args = (__u64)"__test_event u32 field1;";
264 ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSREG, ®));
265 ASSERT_EQ(EADDRINUSE, errno);
267 /* Ensure disabled */
268 self->enable_fd = open(enable_file, O_RDWR);
269 ASSERT_NE(-1, self->enable_fd);
270 ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0")))
272 /* Enable event and ensure bits updated in status */
273 ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
274 ASSERT_EQ(1 << reg.enable_bit, self->check);
276 /* Disable event and ensure bits updated in status */
277 ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0")))
278 ASSERT_EQ(0, self->check);
280 /* File still open should return -EBUSY for delete */
281 ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSDEL, "__test_event"));
282 ASSERT_EQ(EBUSY, errno);
285 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSUNREG, &unreg));
286 unreg.disable_bit = 30;
287 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSUNREG, &unreg));
289 /* Delete should have been auto-done after close and unregister */
290 close(self->data_fd);
292 ASSERT_EQ(true, wait_for_delete());
295 TEST_F(user, write_events) {
296 struct user_reg reg = {0};
298 __u32 field1, field2;
299 int before = 0, after = 0;
301 reg.size = sizeof(reg);
302 reg.name_args = (__u64)"__test_event u32 field1; u32 field2";
304 reg.enable_addr = (__u64)&self->check;
305 reg.enable_size = sizeof(self->check);
310 io[0].iov_base = ®.write_index;
311 io[0].iov_len = sizeof(reg.write_index);
312 io[1].iov_base = &field1;
313 io[1].iov_len = sizeof(field1);
314 io[2].iov_base = &field2;
315 io[2].iov_len = sizeof(field2);
317 /* Register should work */
318 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
319 ASSERT_EQ(0, reg.write_index);
320 ASSERT_EQ(0, self->check);
322 /* Write should fail on invalid slot with ENOENT */
323 io[0].iov_base = &field2;
324 io[0].iov_len = sizeof(field2);
325 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
326 ASSERT_EQ(ENOENT, errno);
327 io[0].iov_base = ®.write_index;
328 io[0].iov_len = sizeof(reg.write_index);
330 /* Write should return -EBADF when event is not enabled */
331 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
332 ASSERT_EQ(EBADF, errno);
335 self->enable_fd = open(enable_file, O_RDWR);
336 ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
338 /* Event should now be enabled */
339 ASSERT_NE(1 << reg.enable_bit, self->check);
341 /* Write should make it out to ftrace buffers */
342 before = trace_bytes();
343 ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 3));
344 after = trace_bytes();
345 ASSERT_GT(after, before);
347 /* Negative index should fail with EINVAL */
348 reg.write_index = -1;
349 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
350 ASSERT_EQ(EINVAL, errno);
353 TEST_F(user, write_empty_events) {
354 struct user_reg reg = {0};
356 int before = 0, after = 0;
358 reg.size = sizeof(reg);
359 reg.name_args = (__u64)"__test_event";
361 reg.enable_addr = (__u64)&self->check;
362 reg.enable_size = sizeof(self->check);
364 io[0].iov_base = ®.write_index;
365 io[0].iov_len = sizeof(reg.write_index);
367 /* Register should work */
368 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
369 ASSERT_EQ(0, reg.write_index);
370 ASSERT_EQ(0, self->check);
373 self->enable_fd = open(enable_file, O_RDWR);
374 ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
376 /* Event should now be enabled */
377 ASSERT_EQ(1 << reg.enable_bit, self->check);
379 /* Write should make it out to ftrace buffers */
380 before = trace_bytes();
381 ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 1));
382 after = trace_bytes();
383 ASSERT_GT(after, before);
386 TEST_F(user, write_fault) {
387 struct user_reg reg = {0};
389 int l = sizeof(__u64);
392 reg.size = sizeof(reg);
393 reg.name_args = (__u64)"__test_event u64 anon";
395 reg.enable_addr = (__u64)&self->check;
396 reg.enable_size = sizeof(self->check);
398 anon = mmap(NULL, l, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
399 ASSERT_NE(MAP_FAILED, anon);
401 io[0].iov_base = ®.write_index;
402 io[0].iov_len = sizeof(reg.write_index);
403 io[1].iov_base = anon;
406 /* Register should work */
407 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
408 ASSERT_EQ(0, reg.write_index);
411 self->enable_fd = open(enable_file, O_RDWR);
412 ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
414 /* Write should work normally */
415 ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 2));
417 /* Faulted data should zero fill and work */
418 ASSERT_EQ(0, madvise(anon, l, MADV_DONTNEED));
419 ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 2));
420 ASSERT_EQ(0, munmap(anon, l));
423 TEST_F(user, write_validator) {
424 struct user_reg reg = {0};
428 int before = 0, after = 0;
430 reg.size = sizeof(reg);
431 reg.name_args = (__u64)"__test_event __rel_loc char[] data";
433 reg.enable_addr = (__u64)&self->check;
434 reg.enable_size = sizeof(self->check);
436 /* Register should work */
437 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®));
438 ASSERT_EQ(0, reg.write_index);
439 ASSERT_EQ(0, self->check);
441 io[0].iov_base = ®.write_index;
442 io[0].iov_len = sizeof(reg.write_index);
443 io[1].iov_base = &loc;
444 io[1].iov_len = sizeof(loc);
445 io[2].iov_base = data;
446 bytes = snprintf(data, sizeof(data), "Test") + 1;
447 io[2].iov_len = bytes;
449 /* Undersized write should fail */
450 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 1));
451 ASSERT_EQ(EINVAL, errno);
454 self->enable_fd = open(enable_file, O_RDWR);
455 ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
457 /* Event should now be enabled */
458 ASSERT_EQ(1 << reg.enable_bit, self->check);
460 /* Full in-bounds write should work */
461 before = trace_bytes();
462 loc = DYN_LOC(0, bytes);
463 ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 3));
464 after = trace_bytes();
465 ASSERT_GT(after, before);
467 /* Out of bounds write should fault (offset way out) */
468 loc = DYN_LOC(1024, bytes);
469 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
470 ASSERT_EQ(EFAULT, errno);
472 /* Out of bounds write should fault (offset 1 byte out) */
473 loc = DYN_LOC(1, bytes);
474 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
475 ASSERT_EQ(EFAULT, errno);
477 /* Out of bounds write should fault (size way out) */
478 loc = DYN_LOC(0, bytes + 1024);
479 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
480 ASSERT_EQ(EFAULT, errno);
482 /* Out of bounds write should fault (size 1 byte out) */
483 loc = DYN_LOC(0, bytes + 1);
484 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
485 ASSERT_EQ(EFAULT, errno);
487 /* Non-Null should fault */
488 memset(data, 'A', sizeof(data));
489 loc = DYN_LOC(0, bytes);
490 ASSERT_EQ(-1, writev(self->data_fd, (const struct iovec *)io, 3));
491 ASSERT_EQ(EFAULT, errno);
494 TEST_F(user, print_fmt) {
497 ret = check_print_fmt("__test_event __rel_loc char[] data",
498 "print fmt: \"data=%s\", __get_rel_str(data)",
502 ret = check_print_fmt("__test_event __data_loc char[] data",
503 "print fmt: \"data=%s\", __get_str(data)",
507 ret = check_print_fmt("__test_event s64 data",
508 "print fmt: \"data=%lld\", REC->data",
512 ret = check_print_fmt("__test_event u64 data",
513 "print fmt: \"data=%llu\", REC->data",
517 ret = check_print_fmt("__test_event s32 data",
518 "print fmt: \"data=%d\", REC->data",
522 ret = check_print_fmt("__test_event u32 data",
523 "print fmt: \"data=%u\", REC->data",
527 ret = check_print_fmt("__test_event int data",
528 "print fmt: \"data=%d\", REC->data",
532 ret = check_print_fmt("__test_event unsigned int data",
533 "print fmt: \"data=%u\", REC->data",
537 ret = check_print_fmt("__test_event s16 data",
538 "print fmt: \"data=%d\", REC->data",
542 ret = check_print_fmt("__test_event u16 data",
543 "print fmt: \"data=%u\", REC->data",
547 ret = check_print_fmt("__test_event short data",
548 "print fmt: \"data=%d\", REC->data",
552 ret = check_print_fmt("__test_event unsigned short data",
553 "print fmt: \"data=%u\", REC->data",
557 ret = check_print_fmt("__test_event s8 data",
558 "print fmt: \"data=%d\", REC->data",
562 ret = check_print_fmt("__test_event u8 data",
563 "print fmt: \"data=%u\", REC->data",
567 ret = check_print_fmt("__test_event char data",
568 "print fmt: \"data=%d\", REC->data",
572 ret = check_print_fmt("__test_event unsigned char data",
573 "print fmt: \"data=%u\", REC->data",
577 ret = check_print_fmt("__test_event char[4] data",
578 "print fmt: \"data=%s\", REC->data",
583 int main(int argc, char **argv)
585 return test_harness_run(argc, argv);