1 // SPDX-License-Identifier: GPL-2.0
3 * User Events Dyn Events Test Program
5 * Copyright (c) 2021 Beau Belgrave <beaub@linux.microsoft.com>
9 #include <linux/user_events.h>
13 #include <sys/ioctl.h>
17 #include "../kselftest_harness.h"
18 #include "user_events_selftests.h"
20 const char *abi_file = "/sys/kernel/tracing/user_events_data";
21 const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/enable";
23 static bool wait_for_delete(void)
27 for (i = 0; i < 1000; ++i) {
28 int fd = open(enable_file, O_RDONLY);
40 static int reg_event(int fd, int *check, int bit, const char *value)
42 struct user_reg reg = {0};
44 reg.size = sizeof(reg);
45 reg.name_args = (__u64)value;
47 reg.enable_addr = (__u64)check;
48 reg.enable_size = sizeof(*check);
50 if (ioctl(fd, DIAG_IOCSREG, ®) == -1)
56 static int unreg_event(int fd, int *check, int bit)
58 struct user_unreg unreg = {0};
60 unreg.size = sizeof(unreg);
61 unreg.disable_bit = bit;
62 unreg.disable_addr = (__u64)check;
64 return ioctl(fd, DIAG_IOCSUNREG, &unreg);
67 static int parse(int *check, const char *value)
69 int fd = open(abi_file, O_RDWR);
75 /* Until we have persist flags via dynamic events, use the base name */
76 if (value[0] != 'u' || value[1] != ':') {
81 ret = reg_event(fd, check, 31, value + 2);
84 if (unreg_event(fd, check, 31) == -1)
85 printf("WARN: Couldn't unreg event\n");
93 static int check_match(int *check, const char *first, const char *second, bool *match)
95 int fd = open(abi_file, O_RDWR);
101 if (reg_event(fd, check, 31, first) == -1)
104 if (reg_event(fd, check, 30, second) == -1) {
105 if (errno == EADDRINUSE) {
106 /* Name is in use, with different fields */
117 unreg_event(fd, check, 31);
118 unreg_event(fd, check, 30);
127 #define TEST_MATCH(x, y) \
130 ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \
131 ASSERT_EQ(true, match); \
134 #define TEST_NMATCH(x, y) \
137 ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \
138 ASSERT_EQ(false, match); \
141 #define TEST_PARSE(x) ASSERT_NE(-1, parse(&self->check, x))
143 #define TEST_NPARSE(x) ASSERT_EQ(-1, parse(&self->check, x))
150 FIXTURE_SETUP(user) {
151 USER_EVENT_FIXTURE_SETUP(return, self->umount);
154 FIXTURE_TEARDOWN(user) {
155 USER_EVENT_FIXTURE_TEARDOWN(self->umount);
160 TEST_F(user, basic_types) {
161 /* All should work */
162 TEST_PARSE("u:__test_event u64 a");
163 TEST_PARSE("u:__test_event u32 a");
164 TEST_PARSE("u:__test_event u16 a");
165 TEST_PARSE("u:__test_event u8 a");
166 TEST_PARSE("u:__test_event char a");
167 TEST_PARSE("u:__test_event unsigned char a");
168 TEST_PARSE("u:__test_event int a");
169 TEST_PARSE("u:__test_event unsigned int a");
170 TEST_PARSE("u:__test_event short a");
171 TEST_PARSE("u:__test_event unsigned short a");
172 TEST_PARSE("u:__test_event char[20] a");
173 TEST_PARSE("u:__test_event unsigned char[20] a");
174 TEST_PARSE("u:__test_event char[0x14] a");
175 TEST_PARSE("u:__test_event unsigned char[0x14] a");
176 /* Bad size format should fail */
177 TEST_NPARSE("u:__test_event char[aa] a");
178 /* Large size should fail */
179 TEST_NPARSE("u:__test_event char[9999] a");
180 /* Long size string should fail */
181 TEST_NPARSE("u:__test_event char[0x0000000000001] a");
184 TEST_F(user, loc_types) {
185 /* All should work */
186 TEST_PARSE("u:__test_event __data_loc char[] a");
187 TEST_PARSE("u:__test_event __data_loc unsigned char[] a");
188 TEST_PARSE("u:__test_event __rel_loc char[] a");
189 TEST_PARSE("u:__test_event __rel_loc unsigned char[] a");
192 TEST_F(user, size_types) {
194 TEST_PARSE("u:__test_event struct custom a 20");
195 /* Size not specified on struct should fail */
196 TEST_NPARSE("u:__test_event struct custom a");
197 /* Size specified on non-struct should fail */
198 TEST_NPARSE("u:__test_event char a 20");
201 TEST_F(user, matching) {
202 /* Single name matches */
203 TEST_MATCH("__test_event u32 a",
204 "__test_event u32 a");
206 /* Multiple names match */
207 TEST_MATCH("__test_event u32 a; u32 b",
208 "__test_event u32 a; u32 b");
210 /* Multiple names match with dangling ; */
211 TEST_MATCH("__test_event u32 a; u32 b",
212 "__test_event u32 a; u32 b;");
214 /* Single name doesn't match */
215 TEST_NMATCH("__test_event u32 a",
216 "__test_event u32 b");
218 /* Multiple names don't match */
219 TEST_NMATCH("__test_event u32 a; u32 b",
220 "__test_event u32 b; u32 a");
222 /* Types don't match */
223 TEST_NMATCH("__test_event u64 a; u64 b",
224 "__test_event u32 a; u32 b");
226 /* Struct name and size matches */
227 TEST_MATCH("__test_event struct my_struct a 20",
228 "__test_event struct my_struct a 20");
230 /* Struct name don't match */
231 TEST_NMATCH("__test_event struct my_struct a 20",
232 "__test_event struct my_struct b 20");
234 /* Struct size don't match */
235 TEST_NMATCH("__test_event struct my_struct a 20",
236 "__test_event struct my_struct a 21");
239 int main(int argc, char **argv)
241 return test_harness_run(argc, argv);