1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Test module for in-kernel synthetic event creation and generation. 4 * 5 * Copyright (C) 2019 Tom Zanussi <zanussi@kernel.org> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/trace_events.h> 10 11 /* 12 * This module is a simple test of basic functionality for in-kernel 13 * synthetic event creation and generation, the first and second tests 14 * using synth_event_gen_cmd_start() and synth_event_add_field(), the 15 * third uses synth_event_create() to do it all at once with a static 16 * field array. 17 * 18 * Following that are a few examples using the created events to test 19 * various ways of tracing a synthetic event. 20 * 21 * To test, select CONFIG_SYNTH_EVENT_GEN_TEST and build the module. 22 * Then: 23 * 24 * # insmod kernel/trace/synth_event_gen_test.ko 25 * # cat /sys/kernel/tracing/trace 26 * 27 * You should see several events in the trace buffer - 28 * "create_synth_test", "empty_synth_test", and several instances of 29 * "gen_synth_test". 30 * 31 * To remove the events, remove the module: 32 * 33 * # rmmod synth_event_gen_test 34 * 35 */ 36 37 static struct trace_event_file *create_synth_test; 38 static struct trace_event_file *empty_synth_test; 39 static struct trace_event_file *gen_synth_test; 40 41 /* 42 * Test to make sure we can create a synthetic event, then add more 43 * fields. 44 */ 45 static int __init test_gen_synth_cmd(void) 46 { 47 struct dynevent_cmd cmd; 48 u64 vals[7]; 49 char *buf; 50 int ret; 51 52 /* Create a buffer to hold the generated command */ 53 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL); 54 if (!buf) 55 return -ENOMEM; 56 57 /* Before generating the command, initialize the cmd object */ 58 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN); 59 60 /* 61 * Create the empty gen_synth_test synthetic event with the 62 * first 4 fields. 63 */ 64 ret = synth_event_gen_cmd_start(&cmd, "gen_synth_test", THIS_MODULE, 65 "pid_t", "next_pid_field", 66 "char[16]", "next_comm_field", 67 "u64", "ts_ns", 68 "u64", "ts_ms"); 69 if (ret) 70 goto free; 71 72 /* Use synth_event_add_field to add the rest of the fields */ 73 74 ret = synth_event_add_field(&cmd, "unsigned int", "cpu"); 75 if (ret) 76 goto free; 77 78 ret = synth_event_add_field(&cmd, "char[64]", "my_string_field"); 79 if (ret) 80 goto free; 81 82 ret = synth_event_add_field(&cmd, "int", "my_int_field"); 83 if (ret) 84 goto free; 85 86 ret = synth_event_gen_cmd_end(&cmd); 87 if (ret) 88 goto free; 89 90 /* 91 * Now get the gen_synth_test event file. We need to prevent 92 * the instance and event from disappearing from underneath 93 * us, which trace_get_event_file() does (though in this case 94 * we're using the top-level instance which never goes away). 95 */ 96 gen_synth_test = trace_get_event_file(NULL, "synthetic", 97 "gen_synth_test"); 98 if (IS_ERR(gen_synth_test)) { 99 ret = PTR_ERR(gen_synth_test); 100 goto delete; 101 } 102 103 /* Enable the event or you won't see anything */ 104 ret = trace_array_set_clr_event(gen_synth_test->tr, 105 "synthetic", "gen_synth_test", true); 106 if (ret) { 107 trace_put_event_file(gen_synth_test); 108 goto delete; 109 } 110 111 /* Create some bogus values just for testing */ 112 113 vals[0] = 777; /* next_pid_field */ 114 vals[1] = (u64)(long)"hula hoops"; /* next_comm_field */ 115 vals[2] = 1000000; /* ts_ns */ 116 vals[3] = 1000; /* ts_ms */ 117 vals[4] = raw_smp_processor_id(); /* cpu */ 118 vals[5] = (u64)(long)"thneed"; /* my_string_field */ 119 vals[6] = 598; /* my_int_field */ 120 121 /* Now generate a gen_synth_test event */ 122 ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals)); 123 free: 124 kfree(buf); 125 return ret; 126 delete: 127 /* We got an error after creating the event, delete it */ 128 synth_event_delete("gen_synth_test"); 129 goto free; 130 } 131 132 /* 133 * Test to make sure we can create an initially empty synthetic event, 134 * then add all the fields. 135 */ 136 static int __init test_empty_synth_event(void) 137 { 138 struct dynevent_cmd cmd; 139 u64 vals[7]; 140 char *buf; 141 int ret; 142 143 /* Create a buffer to hold the generated command */ 144 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL); 145 if (!buf) 146 return -ENOMEM; 147 148 /* Before generating the command, initialize the cmd object */ 149 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN); 150 151 /* 152 * Create the empty_synth_test synthetic event with no fields. 153 */ 154 ret = synth_event_gen_cmd_start(&cmd, "empty_synth_test", THIS_MODULE); 155 if (ret) 156 goto free; 157 158 /* Use synth_event_add_field to add all of the fields */ 159 160 ret = synth_event_add_field(&cmd, "pid_t", "next_pid_field"); 161 if (ret) 162 goto free; 163 164 ret = synth_event_add_field(&cmd, "char[16]", "next_comm_field"); 165 if (ret) 166 goto free; 167 168 ret = synth_event_add_field(&cmd, "u64", "ts_ns"); 169 if (ret) 170 goto free; 171 172 ret = synth_event_add_field(&cmd, "u64", "ts_ms"); 173 if (ret) 174 goto free; 175 176 ret = synth_event_add_field(&cmd, "unsigned int", "cpu"); 177 if (ret) 178 goto free; 179 180 ret = synth_event_add_field(&cmd, "char[64]", "my_string_field"); 181 if (ret) 182 goto free; 183 184 ret = synth_event_add_field(&cmd, "int", "my_int_field"); 185 if (ret) 186 goto free; 187 188 /* All fields have been added, close and register the synth event */ 189 190 ret = synth_event_gen_cmd_end(&cmd); 191 if (ret) 192 goto free; 193 194 /* 195 * Now get the empty_synth_test event file. We need to 196 * prevent the instance and event from disappearing from 197 * underneath us, which trace_get_event_file() does (though in 198 * this case we're using the top-level instance which never 199 * goes away). 200 */ 201 empty_synth_test = trace_get_event_file(NULL, "synthetic", 202 "empty_synth_test"); 203 if (IS_ERR(empty_synth_test)) { 204 ret = PTR_ERR(empty_synth_test); 205 goto delete; 206 } 207 208 /* Enable the event or you won't see anything */ 209 ret = trace_array_set_clr_event(empty_synth_test->tr, 210 "synthetic", "empty_synth_test", true); 211 if (ret) { 212 trace_put_event_file(empty_synth_test); 213 goto delete; 214 } 215 216 /* Create some bogus values just for testing */ 217 218 vals[0] = 777; /* next_pid_field */ 219 vals[1] = (u64)(long)"tiddlywinks"; /* next_comm_field */ 220 vals[2] = 1000000; /* ts_ns */ 221 vals[3] = 1000; /* ts_ms */ 222 vals[4] = raw_smp_processor_id(); /* cpu */ 223 vals[5] = (u64)(long)"thneed_2.0"; /* my_string_field */ 224 vals[6] = 399; /* my_int_field */ 225 226 /* Now trace an empty_synth_test event */ 227 ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals)); 228 free: 229 kfree(buf); 230 return ret; 231 delete: 232 /* We got an error after creating the event, delete it */ 233 synth_event_delete("empty_synth_test"); 234 goto free; 235 } 236 237 static struct synth_field_desc create_synth_test_fields[] = { 238 { .type = "pid_t", .name = "next_pid_field" }, 239 { .type = "char[16]", .name = "next_comm_field" }, 240 { .type = "u64", .name = "ts_ns" }, 241 { .type = "char[]", .name = "dynstring_field_1" }, 242 { .type = "u64", .name = "ts_ms" }, 243 { .type = "unsigned int", .name = "cpu" }, 244 { .type = "char[64]", .name = "my_string_field" }, 245 { .type = "char[]", .name = "dynstring_field_2" }, 246 { .type = "int", .name = "my_int_field" }, 247 }; 248 249 /* 250 * Test synthetic event creation all at once from array of field 251 * descriptors. 252 */ 253 static int __init test_create_synth_event(void) 254 { 255 u64 vals[9]; 256 int ret; 257 258 /* Create the create_synth_test event with the fields above */ 259 ret = synth_event_create("create_synth_test", 260 create_synth_test_fields, 261 ARRAY_SIZE(create_synth_test_fields), 262 THIS_MODULE); 263 if (ret) 264 goto out; 265 266 /* 267 * Now get the create_synth_test event file. We need to 268 * prevent the instance and event from disappearing from 269 * underneath us, which trace_get_event_file() does (though in 270 * this case we're using the top-level instance which never 271 * goes away). 272 */ 273 create_synth_test = trace_get_event_file(NULL, "synthetic", 274 "create_synth_test"); 275 if (IS_ERR(create_synth_test)) { 276 ret = PTR_ERR(create_synth_test); 277 goto delete; 278 } 279 280 /* Enable the event or you won't see anything */ 281 ret = trace_array_set_clr_event(create_synth_test->tr, 282 "synthetic", "create_synth_test", true); 283 if (ret) { 284 trace_put_event_file(create_synth_test); 285 goto delete; 286 } 287 288 /* Create some bogus values just for testing */ 289 290 vals[0] = 777; /* next_pid_field */ 291 vals[1] = (u64)(long)"tiddlywinks"; /* next_comm_field */ 292 vals[2] = 1000000; /* ts_ns */ 293 vals[3] = (u64)(long)"xrayspecs"; /* dynstring_field_1 */ 294 vals[4] = 1000; /* ts_ms */ 295 vals[5] = raw_smp_processor_id(); /* cpu */ 296 vals[6] = (u64)(long)"thneed"; /* my_string_field */ 297 vals[7] = (u64)(long)"kerplunk"; /* dynstring_field_2 */ 298 vals[8] = 398; /* my_int_field */ 299 300 /* Now generate a create_synth_test event */ 301 ret = synth_event_trace_array(create_synth_test, vals, ARRAY_SIZE(vals)); 302 out: 303 return ret; 304 delete: 305 /* We got an error after creating the event, delete it */ 306 synth_event_delete("create_synth_test"); 307 308 goto out; 309 } 310 311 /* 312 * Test tracing a synthetic event by reserving trace buffer space, 313 * then filling in fields one after another. 314 */ 315 static int __init test_add_next_synth_val(void) 316 { 317 struct synth_event_trace_state trace_state; 318 int ret; 319 320 /* Start by reserving space in the trace buffer */ 321 ret = synth_event_trace_start(gen_synth_test, &trace_state); 322 if (ret) 323 return ret; 324 325 /* Write some bogus values into the trace buffer, one after another */ 326 327 /* next_pid_field */ 328 ret = synth_event_add_next_val(777, &trace_state); 329 if (ret) 330 goto out; 331 332 /* next_comm_field */ 333 ret = synth_event_add_next_val((u64)(long)"slinky", &trace_state); 334 if (ret) 335 goto out; 336 337 /* ts_ns */ 338 ret = synth_event_add_next_val(1000000, &trace_state); 339 if (ret) 340 goto out; 341 342 /* ts_ms */ 343 ret = synth_event_add_next_val(1000, &trace_state); 344 if (ret) 345 goto out; 346 347 /* cpu */ 348 ret = synth_event_add_next_val(raw_smp_processor_id(), &trace_state); 349 if (ret) 350 goto out; 351 352 /* my_string_field */ 353 ret = synth_event_add_next_val((u64)(long)"thneed_2.01", &trace_state); 354 if (ret) 355 goto out; 356 357 /* my_int_field */ 358 ret = synth_event_add_next_val(395, &trace_state); 359 out: 360 /* Finally, commit the event */ 361 ret = synth_event_trace_end(&trace_state); 362 363 return ret; 364 } 365 366 /* 367 * Test tracing a synthetic event by reserving trace buffer space, 368 * then filling in fields using field names, which can be done in any 369 * order. 370 */ 371 static int __init test_add_synth_val(void) 372 { 373 struct synth_event_trace_state trace_state; 374 int ret; 375 376 /* Start by reserving space in the trace buffer */ 377 ret = synth_event_trace_start(gen_synth_test, &trace_state); 378 if (ret) 379 return ret; 380 381 /* Write some bogus values into the trace buffer, using field names */ 382 383 ret = synth_event_add_val("ts_ns", 1000000, &trace_state); 384 if (ret) 385 goto out; 386 387 ret = synth_event_add_val("ts_ms", 1000, &trace_state); 388 if (ret) 389 goto out; 390 391 ret = synth_event_add_val("cpu", raw_smp_processor_id(), &trace_state); 392 if (ret) 393 goto out; 394 395 ret = synth_event_add_val("next_pid_field", 777, &trace_state); 396 if (ret) 397 goto out; 398 399 ret = synth_event_add_val("next_comm_field", (u64)(long)"silly putty", 400 &trace_state); 401 if (ret) 402 goto out; 403 404 ret = synth_event_add_val("my_string_field", (u64)(long)"thneed_9", 405 &trace_state); 406 if (ret) 407 goto out; 408 409 ret = synth_event_add_val("my_int_field", 3999, &trace_state); 410 out: 411 /* Finally, commit the event */ 412 ret = synth_event_trace_end(&trace_state); 413 414 return ret; 415 } 416 417 /* 418 * Test tracing a synthetic event all at once from array of values. 419 */ 420 static int __init test_trace_synth_event(void) 421 { 422 int ret; 423 424 /* Trace some bogus values just for testing */ 425 ret = synth_event_trace(create_synth_test, 9, /* number of values */ 426 (u64)444, /* next_pid_field */ 427 (u64)(long)"clackers", /* next_comm_field */ 428 (u64)1000000, /* ts_ns */ 429 (u64)(long)"viewmaster",/* dynstring_field_1 */ 430 (u64)1000, /* ts_ms */ 431 (u64)raw_smp_processor_id(), /* cpu */ 432 (u64)(long)"Thneed", /* my_string_field */ 433 (u64)(long)"yoyos", /* dynstring_field_2 */ 434 (u64)999); /* my_int_field */ 435 return ret; 436 } 437 438 static int __init synth_event_gen_test_init(void) 439 { 440 int ret; 441 442 ret = test_gen_synth_cmd(); 443 if (ret) 444 return ret; 445 446 ret = test_empty_synth_event(); 447 if (ret) { 448 WARN_ON(trace_array_set_clr_event(gen_synth_test->tr, 449 "synthetic", 450 "gen_synth_test", false)); 451 trace_put_event_file(gen_synth_test); 452 WARN_ON(synth_event_delete("gen_synth_test")); 453 goto out; 454 } 455 456 ret = test_create_synth_event(); 457 if (ret) { 458 WARN_ON(trace_array_set_clr_event(gen_synth_test->tr, 459 "synthetic", 460 "gen_synth_test", false)); 461 trace_put_event_file(gen_synth_test); 462 WARN_ON(synth_event_delete("gen_synth_test")); 463 464 WARN_ON(trace_array_set_clr_event(empty_synth_test->tr, 465 "synthetic", 466 "empty_synth_test", false)); 467 trace_put_event_file(empty_synth_test); 468 WARN_ON(synth_event_delete("empty_synth_test")); 469 goto out; 470 } 471 472 ret = test_add_next_synth_val(); 473 WARN_ON(ret); 474 475 ret = test_add_synth_val(); 476 WARN_ON(ret); 477 478 ret = test_trace_synth_event(); 479 WARN_ON(ret); 480 481 /* Disable when done */ 482 trace_array_set_clr_event(gen_synth_test->tr, 483 "synthetic", 484 "gen_synth_test", false); 485 trace_array_set_clr_event(empty_synth_test->tr, 486 "synthetic", 487 "empty_synth_test", false); 488 trace_array_set_clr_event(create_synth_test->tr, 489 "synthetic", 490 "create_synth_test", false); 491 out: 492 return ret; 493 } 494 495 static void __exit synth_event_gen_test_exit(void) 496 { 497 /* Disable the event or you can't remove it */ 498 WARN_ON(trace_array_set_clr_event(gen_synth_test->tr, 499 "synthetic", 500 "gen_synth_test", false)); 501 502 /* Now give the file and instance back */ 503 trace_put_event_file(gen_synth_test); 504 505 /* Now unregister and free the synthetic event */ 506 WARN_ON(synth_event_delete("gen_synth_test")); 507 508 /* Disable the event or you can't remove it */ 509 WARN_ON(trace_array_set_clr_event(empty_synth_test->tr, 510 "synthetic", 511 "empty_synth_test", false)); 512 513 /* Now give the file and instance back */ 514 trace_put_event_file(empty_synth_test); 515 516 /* Now unregister and free the synthetic event */ 517 WARN_ON(synth_event_delete("empty_synth_test")); 518 519 /* Disable the event or you can't remove it */ 520 WARN_ON(trace_array_set_clr_event(create_synth_test->tr, 521 "synthetic", 522 "create_synth_test", false)); 523 524 /* Now give the file and instance back */ 525 trace_put_event_file(create_synth_test); 526 527 /* Now unregister and free the synthetic event */ 528 WARN_ON(synth_event_delete("create_synth_test")); 529 } 530 531 module_init(synth_event_gen_test_init) 532 module_exit(synth_event_gen_test_exit) 533 534 MODULE_AUTHOR("Tom Zanussi"); 535 MODULE_DESCRIPTION("synthetic event generation test"); 536 MODULE_LICENSE("GPL v2"); 537
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.