1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace_events_synth - synthetic trace events 4 * 5 * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/kallsyms.h> 10 #include <linux/security.h> 11 #include <linux/mutex.h> 12 #include <linux/slab.h> 13 #include <linux/stacktrace.h> 14 #include <linux/rculist.h> 15 #include <linux/tracefs.h> 16 17 /* for gfp flag names */ 18 #include <linux/trace_events.h> 19 #include <trace/events/mmflags.h> 20 21 #include "trace_synth.h" 22 23 #undef ERRORS 24 #define ERRORS \ 25 C(BAD_NAME, "Illegal name"), \ 26 C(CMD_INCOMPLETE, "Incomplete command"), \ 27 C(EVENT_EXISTS, "Event already exists"), \ 28 C(TOO_MANY_FIELDS, "Too many fields"), \ 29 C(INCOMPLETE_TYPE, "Incomplete type"), \ 30 C(INVALID_TYPE, "Invalid type"), \ 31 C(INVALID_FIELD, "Invalid field"), \ 32 C(CMD_TOO_LONG, "Command too long"), 33 34 #undef C 35 #define C(a, b) SYNTH_ERR_##a 36 37 enum { ERRORS }; 38 39 #undef C 40 #define C(a, b) b 41 42 static const char *err_text[] = { ERRORS }; 43 44 static char last_cmd[MAX_FILTER_STR_VAL]; 45 46 static int errpos(const char *str) 47 { 48 return err_pos(last_cmd, str); 49 } 50 51 static void last_cmd_set(char *str) 52 { 53 if (!str) 54 return; 55 56 strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1); 57 } 58 59 static void synth_err(u8 err_type, u8 err_pos) 60 { 61 tracing_log_err(NULL, "synthetic_events", last_cmd, err_text, 62 err_type, err_pos); 63 } 64 65 static int create_synth_event(int argc, const char **argv); 66 static int synth_event_show(struct seq_file *m, struct dyn_event *ev); 67 static int synth_event_release(struct dyn_event *ev); 68 static bool synth_event_is_busy(struct dyn_event *ev); 69 static bool synth_event_match(const char *system, const char *event, 70 int argc, const char **argv, struct dyn_event *ev); 71 72 static struct dyn_event_operations synth_event_ops = { 73 .create = create_synth_event, 74 .show = synth_event_show, 75 .is_busy = synth_event_is_busy, 76 .free = synth_event_release, 77 .match = synth_event_match, 78 }; 79 80 static bool is_synth_event(struct dyn_event *ev) 81 { 82 return ev->ops == &synth_event_ops; 83 } 84 85 static struct synth_event *to_synth_event(struct dyn_event *ev) 86 { 87 return container_of(ev, struct synth_event, devent); 88 } 89 90 static bool synth_event_is_busy(struct dyn_event *ev) 91 { 92 struct synth_event *event = to_synth_event(ev); 93 94 return event->ref != 0; 95 } 96 97 static bool synth_event_match(const char *system, const char *event, 98 int argc, const char **argv, struct dyn_event *ev) 99 { 100 struct synth_event *sev = to_synth_event(ev); 101 102 return strcmp(sev->name, event) == 0 && 103 (!system || strcmp(system, SYNTH_SYSTEM) == 0); 104 } 105 106 struct synth_trace_event { 107 struct trace_entry ent; 108 u64 fields[]; 109 }; 110 111 static int synth_event_define_fields(struct trace_event_call *call) 112 { 113 struct synth_trace_event trace; 114 int offset = offsetof(typeof(trace), fields); 115 struct synth_event *event = call->data; 116 unsigned int i, size, n_u64; 117 char *name, *type; 118 bool is_signed; 119 int ret = 0; 120 121 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 122 size = event->fields[i]->size; 123 is_signed = event->fields[i]->is_signed; 124 type = event->fields[i]->type; 125 name = event->fields[i]->name; 126 ret = trace_define_field(call, type, name, offset, size, 127 is_signed, FILTER_OTHER); 128 if (ret) 129 break; 130 131 event->fields[i]->offset = n_u64; 132 133 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) { 134 offset += STR_VAR_LEN_MAX; 135 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 136 } else { 137 offset += sizeof(u64); 138 n_u64++; 139 } 140 } 141 142 event->n_u64 = n_u64; 143 144 return ret; 145 } 146 147 static bool synth_field_signed(char *type) 148 { 149 if (str_has_prefix(type, "u")) 150 return false; 151 if (strcmp(type, "gfp_t") == 0) 152 return false; 153 154 return true; 155 } 156 157 static int synth_field_is_string(char *type) 158 { 159 if (strstr(type, "char[") != NULL) 160 return true; 161 162 return false; 163 } 164 165 static int synth_field_string_size(char *type) 166 { 167 char buf[4], *end, *start; 168 unsigned int len; 169 int size, err; 170 171 start = strstr(type, "char["); 172 if (start == NULL) 173 return -EINVAL; 174 start += sizeof("char[") - 1; 175 176 end = strchr(type, ']'); 177 if (!end || end < start || type + strlen(type) > end + 1) 178 return -EINVAL; 179 180 len = end - start; 181 if (len > 3) 182 return -EINVAL; 183 184 if (len == 0) 185 return 0; /* variable-length string */ 186 187 strncpy(buf, start, len); 188 buf[len] = '\0'; 189 190 err = kstrtouint(buf, 0, &size); 191 if (err) 192 return err; 193 194 if (size > STR_VAR_LEN_MAX) 195 return -EINVAL; 196 197 return size; 198 } 199 200 static int synth_field_size(char *type) 201 { 202 int size = 0; 203 204 if (strcmp(type, "s64") == 0) 205 size = sizeof(s64); 206 else if (strcmp(type, "u64") == 0) 207 size = sizeof(u64); 208 else if (strcmp(type, "s32") == 0) 209 size = sizeof(s32); 210 else if (strcmp(type, "u32") == 0) 211 size = sizeof(u32); 212 else if (strcmp(type, "s16") == 0) 213 size = sizeof(s16); 214 else if (strcmp(type, "u16") == 0) 215 size = sizeof(u16); 216 else if (strcmp(type, "s8") == 0) 217 size = sizeof(s8); 218 else if (strcmp(type, "u8") == 0) 219 size = sizeof(u8); 220 else if (strcmp(type, "char") == 0) 221 size = sizeof(char); 222 else if (strcmp(type, "unsigned char") == 0) 223 size = sizeof(unsigned char); 224 else if (strcmp(type, "int") == 0) 225 size = sizeof(int); 226 else if (strcmp(type, "unsigned int") == 0) 227 size = sizeof(unsigned int); 228 else if (strcmp(type, "long") == 0) 229 size = sizeof(long); 230 else if (strcmp(type, "unsigned long") == 0) 231 size = sizeof(unsigned long); 232 else if (strcmp(type, "pid_t") == 0) 233 size = sizeof(pid_t); 234 else if (strcmp(type, "gfp_t") == 0) 235 size = sizeof(gfp_t); 236 else if (synth_field_is_string(type)) 237 size = synth_field_string_size(type); 238 239 return size; 240 } 241 242 static const char *synth_field_fmt(char *type) 243 { 244 const char *fmt = "%llu"; 245 246 if (strcmp(type, "s64") == 0) 247 fmt = "%lld"; 248 else if (strcmp(type, "u64") == 0) 249 fmt = "%llu"; 250 else if (strcmp(type, "s32") == 0) 251 fmt = "%d"; 252 else if (strcmp(type, "u32") == 0) 253 fmt = "%u"; 254 else if (strcmp(type, "s16") == 0) 255 fmt = "%d"; 256 else if (strcmp(type, "u16") == 0) 257 fmt = "%u"; 258 else if (strcmp(type, "s8") == 0) 259 fmt = "%d"; 260 else if (strcmp(type, "u8") == 0) 261 fmt = "%u"; 262 else if (strcmp(type, "char") == 0) 263 fmt = "%d"; 264 else if (strcmp(type, "unsigned char") == 0) 265 fmt = "%u"; 266 else if (strcmp(type, "int") == 0) 267 fmt = "%d"; 268 else if (strcmp(type, "unsigned int") == 0) 269 fmt = "%u"; 270 else if (strcmp(type, "long") == 0) 271 fmt = "%ld"; 272 else if (strcmp(type, "unsigned long") == 0) 273 fmt = "%lu"; 274 else if (strcmp(type, "pid_t") == 0) 275 fmt = "%d"; 276 else if (strcmp(type, "gfp_t") == 0) 277 fmt = "%x"; 278 else if (synth_field_is_string(type)) 279 fmt = "%.*s"; 280 281 return fmt; 282 } 283 284 static void print_synth_event_num_val(struct trace_seq *s, 285 char *print_fmt, char *name, 286 int size, u64 val, char *space) 287 { 288 switch (size) { 289 case 1: 290 trace_seq_printf(s, print_fmt, name, (u8)val, space); 291 break; 292 293 case 2: 294 trace_seq_printf(s, print_fmt, name, (u16)val, space); 295 break; 296 297 case 4: 298 trace_seq_printf(s, print_fmt, name, (u32)val, space); 299 break; 300 301 default: 302 trace_seq_printf(s, print_fmt, name, val, space); 303 break; 304 } 305 } 306 307 static enum print_line_t print_synth_event(struct trace_iterator *iter, 308 int flags, 309 struct trace_event *event) 310 { 311 struct trace_array *tr = iter->tr; 312 struct trace_seq *s = &iter->seq; 313 struct synth_trace_event *entry; 314 struct synth_event *se; 315 unsigned int i, n_u64; 316 char print_fmt[32]; 317 const char *fmt; 318 319 entry = (struct synth_trace_event *)iter->ent; 320 se = container_of(event, struct synth_event, call.event); 321 322 trace_seq_printf(s, "%s: ", se->name); 323 324 for (i = 0, n_u64 = 0; i < se->n_fields; i++) { 325 if (trace_seq_has_overflowed(s)) 326 goto end; 327 328 fmt = synth_field_fmt(se->fields[i]->type); 329 330 /* parameter types */ 331 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE) 332 trace_seq_printf(s, "%s ", fmt); 333 334 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt); 335 336 /* parameter values */ 337 if (se->fields[i]->is_string) { 338 if (se->fields[i]->is_dynamic) { 339 u32 offset, data_offset; 340 char *str_field; 341 342 offset = (u32)entry->fields[n_u64]; 343 data_offset = offset & 0xffff; 344 345 str_field = (char *)entry + data_offset; 346 347 trace_seq_printf(s, print_fmt, se->fields[i]->name, 348 STR_VAR_LEN_MAX, 349 str_field, 350 i == se->n_fields - 1 ? "" : " "); 351 n_u64++; 352 } else { 353 trace_seq_printf(s, print_fmt, se->fields[i]->name, 354 STR_VAR_LEN_MAX, 355 (char *)&entry->fields[n_u64], 356 i == se->n_fields - 1 ? "" : " "); 357 n_u64 += STR_VAR_LEN_MAX / sizeof(u64); 358 } 359 } else { 360 struct trace_print_flags __flags[] = { 361 __def_gfpflag_names, {-1, NULL} }; 362 char *space = (i == se->n_fields - 1 ? "" : " "); 363 364 print_synth_event_num_val(s, print_fmt, 365 se->fields[i]->name, 366 se->fields[i]->size, 367 entry->fields[n_u64], 368 space); 369 370 if (strcmp(se->fields[i]->type, "gfp_t") == 0) { 371 trace_seq_puts(s, " ("); 372 trace_print_flags_seq(s, "|", 373 entry->fields[n_u64], 374 __flags); 375 trace_seq_putc(s, ')'); 376 } 377 n_u64++; 378 } 379 } 380 end: 381 trace_seq_putc(s, '\n'); 382 383 return trace_handle_return(s); 384 } 385 386 static struct trace_event_functions synth_event_funcs = { 387 .trace = print_synth_event 388 }; 389 390 static unsigned int trace_string(struct synth_trace_event *entry, 391 struct synth_event *event, 392 char *str_val, 393 bool is_dynamic, 394 unsigned int data_size, 395 unsigned int *n_u64) 396 { 397 unsigned int len = 0; 398 char *str_field; 399 400 if (is_dynamic) { 401 u32 data_offset; 402 403 data_offset = offsetof(typeof(*entry), fields); 404 data_offset += event->n_u64 * sizeof(u64); 405 data_offset += data_size; 406 407 str_field = (char *)entry + data_offset; 408 409 len = strlen(str_val) + 1; 410 strscpy(str_field, str_val, len); 411 412 data_offset |= len << 16; 413 *(u32 *)&entry->fields[*n_u64] = data_offset; 414 415 (*n_u64)++; 416 } else { 417 str_field = (char *)&entry->fields[*n_u64]; 418 419 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 420 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64); 421 } 422 423 return len; 424 } 425 426 static notrace void trace_event_raw_event_synth(void *__data, 427 u64 *var_ref_vals, 428 unsigned int *var_ref_idx) 429 { 430 unsigned int i, n_u64, val_idx, len, data_size = 0; 431 struct trace_event_file *trace_file = __data; 432 struct synth_trace_event *entry; 433 struct trace_event_buffer fbuffer; 434 struct trace_buffer *buffer; 435 struct synth_event *event; 436 int fields_size = 0; 437 438 event = trace_file->event_call->data; 439 440 if (trace_trigger_soft_disabled(trace_file)) 441 return; 442 443 fields_size = event->n_u64 * sizeof(u64); 444 445 for (i = 0; i < event->n_dynamic_fields; i++) { 446 unsigned int field_pos = event->dynamic_fields[i]->field_pos; 447 char *str_val; 448 449 val_idx = var_ref_idx[field_pos]; 450 str_val = (char *)(long)var_ref_vals[val_idx]; 451 452 len = strlen(str_val) + 1; 453 454 fields_size += len; 455 } 456 457 /* 458 * Avoid ring buffer recursion detection, as this event 459 * is being performed within another event. 460 */ 461 buffer = trace_file->tr->array_buffer.buffer; 462 ring_buffer_nest_start(buffer); 463 464 entry = trace_event_buffer_reserve(&fbuffer, trace_file, 465 sizeof(*entry) + fields_size); 466 if (!entry) 467 goto out; 468 469 for (i = 0, n_u64 = 0; i < event->n_fields; i++) { 470 val_idx = var_ref_idx[i]; 471 if (event->fields[i]->is_string) { 472 char *str_val = (char *)(long)var_ref_vals[val_idx]; 473 474 len = trace_string(entry, event, str_val, 475 event->fields[i]->is_dynamic, 476 data_size, &n_u64); 477 data_size += len; /* only dynamic string increments */ 478 } else { 479 struct synth_field *field = event->fields[i]; 480 u64 val = var_ref_vals[val_idx]; 481 482 switch (field->size) { 483 case 1: 484 *(u8 *)&entry->fields[n_u64] = (u8)val; 485 break; 486 487 case 2: 488 *(u16 *)&entry->fields[n_u64] = (u16)val; 489 break; 490 491 case 4: 492 *(u32 *)&entry->fields[n_u64] = (u32)val; 493 break; 494 495 default: 496 entry->fields[n_u64] = val; 497 break; 498 } 499 n_u64++; 500 } 501 } 502 503 trace_event_buffer_commit(&fbuffer); 504 out: 505 ring_buffer_nest_end(buffer); 506 } 507 508 static void free_synth_event_print_fmt(struct trace_event_call *call) 509 { 510 if (call) { 511 kfree(call->print_fmt); 512 call->print_fmt = NULL; 513 } 514 } 515 516 static int __set_synth_event_print_fmt(struct synth_event *event, 517 char *buf, int len) 518 { 519 const char *fmt; 520 int pos = 0; 521 int i; 522 523 /* When len=0, we just calculate the needed length */ 524 #define LEN_OR_ZERO (len ? len - pos : 0) 525 526 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 527 for (i = 0; i < event->n_fields; i++) { 528 fmt = synth_field_fmt(event->fields[i]->type); 529 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s", 530 event->fields[i]->name, fmt, 531 i == event->n_fields - 1 ? "" : ", "); 532 } 533 pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); 534 535 for (i = 0; i < event->n_fields; i++) { 536 if (event->fields[i]->is_string && 537 event->fields[i]->is_dynamic) 538 pos += snprintf(buf + pos, LEN_OR_ZERO, 539 ", __get_str(%s)", event->fields[i]->name); 540 else 541 pos += snprintf(buf + pos, LEN_OR_ZERO, 542 ", REC->%s", event->fields[i]->name); 543 } 544 545 #undef LEN_OR_ZERO 546 547 /* return the length of print_fmt */ 548 return pos; 549 } 550 551 static int set_synth_event_print_fmt(struct trace_event_call *call) 552 { 553 struct synth_event *event = call->data; 554 char *print_fmt; 555 int len; 556 557 /* First: called with 0 length to calculate the needed length */ 558 len = __set_synth_event_print_fmt(event, NULL, 0); 559 560 print_fmt = kmalloc(len + 1, GFP_KERNEL); 561 if (!print_fmt) 562 return -ENOMEM; 563 564 /* Second: actually write the @print_fmt */ 565 __set_synth_event_print_fmt(event, print_fmt, len + 1); 566 call->print_fmt = print_fmt; 567 568 return 0; 569 } 570 571 static void free_synth_field(struct synth_field *field) 572 { 573 kfree(field->type); 574 kfree(field->name); 575 kfree(field); 576 } 577 578 static struct synth_field *parse_synth_field(int argc, const char **argv, 579 int *consumed) 580 { 581 struct synth_field *field; 582 const char *prefix = NULL, *field_type = argv[0], *field_name, *array; 583 int len, ret = 0; 584 ssize_t size; 585 586 if (field_type[0] == ';') 587 field_type++; 588 589 if (!strcmp(field_type, "unsigned")) { 590 if (argc < 3) { 591 synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type)); 592 return ERR_PTR(-EINVAL); 593 } 594 prefix = "unsigned "; 595 field_type = argv[1]; 596 field_name = argv[2]; 597 *consumed = 3; 598 } else { 599 field_name = argv[1]; 600 *consumed = 2; 601 } 602 603 field = kzalloc(sizeof(*field), GFP_KERNEL); 604 if (!field) 605 return ERR_PTR(-ENOMEM); 606 607 len = strlen(field_name); 608 array = strchr(field_name, '['); 609 if (array) 610 len -= strlen(array); 611 else if (field_name[len - 1] == ';') 612 len--; 613 614 field->name = kmemdup_nul(field_name, len, GFP_KERNEL); 615 if (!field->name) { 616 ret = -ENOMEM; 617 goto free; 618 } 619 if (!is_good_name(field->name)) { 620 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name)); 621 ret = -EINVAL; 622 goto free; 623 } 624 625 if (field_type[0] == ';') 626 field_type++; 627 len = strlen(field_type) + 1; 628 629 if (array) { 630 int l = strlen(array); 631 632 if (l && array[l - 1] == ';') 633 l--; 634 len += l; 635 } 636 if (prefix) 637 len += strlen(prefix); 638 639 field->type = kzalloc(len, GFP_KERNEL); 640 if (!field->type) { 641 ret = -ENOMEM; 642 goto free; 643 } 644 if (prefix) 645 strcat(field->type, prefix); 646 strcat(field->type, field_type); 647 if (array) { 648 strcat(field->type, array); 649 if (field->type[len - 1] == ';') 650 field->type[len - 1] = '\0'; 651 } 652 653 size = synth_field_size(field->type); 654 if (size < 0) { 655 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type)); 656 ret = -EINVAL; 657 goto free; 658 } else if (size == 0) { 659 if (synth_field_is_string(field->type)) { 660 char *type; 661 662 type = kzalloc(sizeof("__data_loc ") + strlen(field->type) + 1, GFP_KERNEL); 663 if (!type) { 664 ret = -ENOMEM; 665 goto free; 666 } 667 668 strcat(type, "__data_loc "); 669 strcat(type, field->type); 670 kfree(field->type); 671 field->type = type; 672 673 field->is_dynamic = true; 674 size = sizeof(u64); 675 } else { 676 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type)); 677 ret = -EINVAL; 678 goto free; 679 } 680 } 681 field->size = size; 682 683 if (synth_field_is_string(field->type)) 684 field->is_string = true; 685 686 field->is_signed = synth_field_signed(field->type); 687 out: 688 return field; 689 free: 690 free_synth_field(field); 691 field = ERR_PTR(ret); 692 goto out; 693 } 694 695 static void free_synth_tracepoint(struct tracepoint *tp) 696 { 697 if (!tp) 698 return; 699 700 kfree(tp->name); 701 kfree(tp); 702 } 703 704 static struct tracepoint *alloc_synth_tracepoint(char *name) 705 { 706 struct tracepoint *tp; 707 708 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 709 if (!tp) 710 return ERR_PTR(-ENOMEM); 711 712 tp->name = kstrdup(name, GFP_KERNEL); 713 if (!tp->name) { 714 kfree(tp); 715 return ERR_PTR(-ENOMEM); 716 } 717 718 return tp; 719 } 720 721 struct synth_event *find_synth_event(const char *name) 722 { 723 struct dyn_event *pos; 724 struct synth_event *event; 725 726 for_each_dyn_event(pos) { 727 if (!is_synth_event(pos)) 728 continue; 729 event = to_synth_event(pos); 730 if (strcmp(event->name, name) == 0) 731 return event; 732 } 733 734 return NULL; 735 } 736 737 static struct trace_event_fields synth_event_fields_array[] = { 738 { .type = TRACE_FUNCTION_TYPE, 739 .define_fields = synth_event_define_fields }, 740 {} 741 }; 742 743 static int register_synth_event(struct synth_event *event) 744 { 745 struct trace_event_call *call = &event->call; 746 int ret = 0; 747 748 event->call.class = &event->class; 749 event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL); 750 if (!event->class.system) { 751 ret = -ENOMEM; 752 goto out; 753 } 754 755 event->tp = alloc_synth_tracepoint(event->name); 756 if (IS_ERR(event->tp)) { 757 ret = PTR_ERR(event->tp); 758 event->tp = NULL; 759 goto out; 760 } 761 762 INIT_LIST_HEAD(&call->class->fields); 763 call->event.funcs = &synth_event_funcs; 764 call->class->fields_array = synth_event_fields_array; 765 766 ret = register_trace_event(&call->event); 767 if (!ret) { 768 ret = -ENODEV; 769 goto out; 770 } 771 call->flags = TRACE_EVENT_FL_TRACEPOINT; 772 call->class->reg = trace_event_reg; 773 call->class->probe = trace_event_raw_event_synth; 774 call->data = event; 775 call->tp = event->tp; 776 777 ret = trace_add_event_call(call); 778 if (ret) { 779 pr_warn("Failed to register synthetic event: %s\n", 780 trace_event_name(call)); 781 goto err; 782 } 783 784 ret = set_synth_event_print_fmt(call); 785 if (ret < 0) { 786 trace_remove_event_call(call); 787 goto err; 788 } 789 out: 790 return ret; 791 err: 792 unregister_trace_event(&call->event); 793 goto out; 794 } 795 796 static int unregister_synth_event(struct synth_event *event) 797 { 798 struct trace_event_call *call = &event->call; 799 int ret; 800 801 ret = trace_remove_event_call(call); 802 803 return ret; 804 } 805 806 static void free_synth_event(struct synth_event *event) 807 { 808 unsigned int i; 809 810 if (!event) 811 return; 812 813 for (i = 0; i < event->n_fields; i++) 814 free_synth_field(event->fields[i]); 815 816 kfree(event->fields); 817 kfree(event->dynamic_fields); 818 kfree(event->name); 819 kfree(event->class.system); 820 free_synth_tracepoint(event->tp); 821 free_synth_event_print_fmt(&event->call); 822 kfree(event); 823 } 824 825 static struct synth_event *alloc_synth_event(const char *name, int n_fields, 826 struct synth_field **fields) 827 { 828 unsigned int i, j, n_dynamic_fields = 0; 829 struct synth_event *event; 830 831 event = kzalloc(sizeof(*event), GFP_KERNEL); 832 if (!event) { 833 event = ERR_PTR(-ENOMEM); 834 goto out; 835 } 836 837 event->name = kstrdup(name, GFP_KERNEL); 838 if (!event->name) { 839 kfree(event); 840 event = ERR_PTR(-ENOMEM); 841 goto out; 842 } 843 844 event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL); 845 if (!event->fields) { 846 free_synth_event(event); 847 event = ERR_PTR(-ENOMEM); 848 goto out; 849 } 850 851 for (i = 0; i < n_fields; i++) 852 if (fields[i]->is_dynamic) 853 n_dynamic_fields++; 854 855 if (n_dynamic_fields) { 856 event->dynamic_fields = kcalloc(n_dynamic_fields, 857 sizeof(*event->dynamic_fields), 858 GFP_KERNEL); 859 if (!event->dynamic_fields) { 860 free_synth_event(event); 861 event = ERR_PTR(-ENOMEM); 862 goto out; 863 } 864 } 865 866 dyn_event_init(&event->devent, &synth_event_ops); 867 868 for (i = 0, j = 0; i < n_fields; i++) { 869 event->fields[i] = fields[i]; 870 871 if (fields[i]->is_dynamic) { 872 event->dynamic_fields[j] = fields[i]; 873 event->dynamic_fields[j]->field_pos = i; 874 event->dynamic_fields[j++] = fields[i]; 875 event->n_dynamic_fields++; 876 } 877 } 878 event->n_fields = n_fields; 879 out: 880 return event; 881 } 882 883 static int synth_event_check_arg_fn(void *data) 884 { 885 struct dynevent_arg_pair *arg_pair = data; 886 int size; 887 888 size = synth_field_size((char *)arg_pair->lhs); 889 if (size == 0) { 890 if (strstr((char *)arg_pair->lhs, "[")) 891 return 0; 892 } 893 894 return size ? 0 : -EINVAL; 895 } 896 897 /** 898 * synth_event_add_field - Add a new field to a synthetic event cmd 899 * @cmd: A pointer to the dynevent_cmd struct representing the new event 900 * @type: The type of the new field to add 901 * @name: The name of the new field to add 902 * 903 * Add a new field to a synthetic event cmd object. Field ordering is in 904 * the same order the fields are added. 905 * 906 * See synth_field_size() for available types. If field_name contains 907 * [n] the field is considered to be an array. 908 * 909 * Return: 0 if successful, error otherwise. 910 */ 911 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type, 912 const char *name) 913 { 914 struct dynevent_arg_pair arg_pair; 915 int ret; 916 917 if (cmd->type != DYNEVENT_TYPE_SYNTH) 918 return -EINVAL; 919 920 if (!type || !name) 921 return -EINVAL; 922 923 dynevent_arg_pair_init(&arg_pair, 0, ';'); 924 925 arg_pair.lhs = type; 926 arg_pair.rhs = name; 927 928 ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn); 929 if (ret) 930 return ret; 931 932 if (++cmd->n_fields > SYNTH_FIELDS_MAX) 933 ret = -EINVAL; 934 935 return ret; 936 } 937 EXPORT_SYMBOL_GPL(synth_event_add_field); 938 939 /** 940 * synth_event_add_field_str - Add a new field to a synthetic event cmd 941 * @cmd: A pointer to the dynevent_cmd struct representing the new event 942 * @type_name: The type and name of the new field to add, as a single string 943 * 944 * Add a new field to a synthetic event cmd object, as a single 945 * string. The @type_name string is expected to be of the form 'type 946 * name', which will be appended by ';'. No sanity checking is done - 947 * what's passed in is assumed to already be well-formed. Field 948 * ordering is in the same order the fields are added. 949 * 950 * See synth_field_size() for available types. If field_name contains 951 * [n] the field is considered to be an array. 952 * 953 * Return: 0 if successful, error otherwise. 954 */ 955 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name) 956 { 957 struct dynevent_arg arg; 958 int ret; 959 960 if (cmd->type != DYNEVENT_TYPE_SYNTH) 961 return -EINVAL; 962 963 if (!type_name) 964 return -EINVAL; 965 966 dynevent_arg_init(&arg, ';'); 967 968 arg.str = type_name; 969 970 ret = dynevent_arg_add(cmd, &arg, NULL); 971 if (ret) 972 return ret; 973 974 if (++cmd->n_fields > SYNTH_FIELDS_MAX) 975 ret = -EINVAL; 976 977 return ret; 978 } 979 EXPORT_SYMBOL_GPL(synth_event_add_field_str); 980 981 /** 982 * synth_event_add_fields - Add multiple fields to a synthetic event cmd 983 * @cmd: A pointer to the dynevent_cmd struct representing the new event 984 * @fields: An array of type/name field descriptions 985 * @n_fields: The number of field descriptions contained in the fields array 986 * 987 * Add a new set of fields to a synthetic event cmd object. The event 988 * fields that will be defined for the event should be passed in as an 989 * array of struct synth_field_desc, and the number of elements in the 990 * array passed in as n_fields. Field ordering will retain the 991 * ordering given in the fields array. 992 * 993 * See synth_field_size() for available types. If field_name contains 994 * [n] the field is considered to be an array. 995 * 996 * Return: 0 if successful, error otherwise. 997 */ 998 int synth_event_add_fields(struct dynevent_cmd *cmd, 999 struct synth_field_desc *fields, 1000 unsigned int n_fields) 1001 { 1002 unsigned int i; 1003 int ret = 0; 1004 1005 for (i = 0; i < n_fields; i++) { 1006 if (fields[i].type == NULL || fields[i].name == NULL) { 1007 ret = -EINVAL; 1008 break; 1009 } 1010 1011 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name); 1012 if (ret) 1013 break; 1014 } 1015 1016 return ret; 1017 } 1018 EXPORT_SYMBOL_GPL(synth_event_add_fields); 1019 1020 /** 1021 * __synth_event_gen_cmd_start - Start a synthetic event command from arg list 1022 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1023 * @name: The name of the synthetic event 1024 * @mod: The module creating the event, NULL if not created from a module 1025 * @args: Variable number of arg (pairs), one pair for each field 1026 * 1027 * NOTE: Users normally won't want to call this function directly, but 1028 * rather use the synth_event_gen_cmd_start() wrapper, which 1029 * automatically adds a NULL to the end of the arg list. If this 1030 * function is used directly, make sure the last arg in the variable 1031 * arg list is NULL. 1032 * 1033 * Generate a synthetic event command to be executed by 1034 * synth_event_gen_cmd_end(). This function can be used to generate 1035 * the complete command or only the first part of it; in the latter 1036 * case, synth_event_add_field(), synth_event_add_field_str(), or 1037 * synth_event_add_fields() can be used to add more fields following 1038 * this. 1039 * 1040 * There should be an even number variable args, each pair consisting 1041 * of a type followed by a field name. 1042 * 1043 * See synth_field_size() for available types. If field_name contains 1044 * [n] the field is considered to be an array. 1045 * 1046 * Return: 0 if successful, error otherwise. 1047 */ 1048 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name, 1049 struct module *mod, ...) 1050 { 1051 struct dynevent_arg arg; 1052 va_list args; 1053 int ret; 1054 1055 cmd->event_name = name; 1056 cmd->private_data = mod; 1057 1058 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1059 return -EINVAL; 1060 1061 dynevent_arg_init(&arg, 0); 1062 arg.str = name; 1063 ret = dynevent_arg_add(cmd, &arg, NULL); 1064 if (ret) 1065 return ret; 1066 1067 va_start(args, mod); 1068 for (;;) { 1069 const char *type, *name; 1070 1071 type = va_arg(args, const char *); 1072 if (!type) 1073 break; 1074 name = va_arg(args, const char *); 1075 if (!name) 1076 break; 1077 1078 if (++cmd->n_fields > SYNTH_FIELDS_MAX) { 1079 ret = -EINVAL; 1080 break; 1081 } 1082 1083 ret = synth_event_add_field(cmd, type, name); 1084 if (ret) 1085 break; 1086 } 1087 va_end(args); 1088 1089 return ret; 1090 } 1091 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start); 1092 1093 /** 1094 * synth_event_gen_cmd_array_start - Start synthetic event command from an array 1095 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1096 * @name: The name of the synthetic event 1097 * @fields: An array of type/name field descriptions 1098 * @n_fields: The number of field descriptions contained in the fields array 1099 * 1100 * Generate a synthetic event command to be executed by 1101 * synth_event_gen_cmd_end(). This function can be used to generate 1102 * the complete command or only the first part of it; in the latter 1103 * case, synth_event_add_field(), synth_event_add_field_str(), or 1104 * synth_event_add_fields() can be used to add more fields following 1105 * this. 1106 * 1107 * The event fields that will be defined for the event should be 1108 * passed in as an array of struct synth_field_desc, and the number of 1109 * elements in the array passed in as n_fields. Field ordering will 1110 * retain the ordering given in the fields array. 1111 * 1112 * See synth_field_size() for available types. If field_name contains 1113 * [n] the field is considered to be an array. 1114 * 1115 * Return: 0 if successful, error otherwise. 1116 */ 1117 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name, 1118 struct module *mod, 1119 struct synth_field_desc *fields, 1120 unsigned int n_fields) 1121 { 1122 struct dynevent_arg arg; 1123 unsigned int i; 1124 int ret = 0; 1125 1126 cmd->event_name = name; 1127 cmd->private_data = mod; 1128 1129 if (cmd->type != DYNEVENT_TYPE_SYNTH) 1130 return -EINVAL; 1131 1132 if (n_fields > SYNTH_FIELDS_MAX) 1133 return -EINVAL; 1134 1135 dynevent_arg_init(&arg, 0); 1136 arg.str = name; 1137 ret = dynevent_arg_add(cmd, &arg, NULL); 1138 if (ret) 1139 return ret; 1140 1141 for (i = 0; i < n_fields; i++) { 1142 if (fields[i].type == NULL || fields[i].name == NULL) 1143 return -EINVAL; 1144 1145 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name); 1146 if (ret) 1147 break; 1148 } 1149 1150 return ret; 1151 } 1152 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start); 1153 1154 static int save_cmdstr(int argc, const char *name, const char **argv) 1155 { 1156 struct seq_buf s; 1157 char *buf; 1158 int i; 1159 1160 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL); 1161 if (!buf) 1162 return -ENOMEM; 1163 1164 seq_buf_init(&s, buf, MAX_DYNEVENT_CMD_LEN); 1165 1166 seq_buf_puts(&s, name); 1167 1168 for (i = 0; i < argc; i++) { 1169 seq_buf_putc(&s, ' '); 1170 seq_buf_puts(&s, argv[i]); 1171 } 1172 1173 if (!seq_buf_buffer_left(&s)) { 1174 synth_err(SYNTH_ERR_CMD_TOO_LONG, 0); 1175 kfree(buf); 1176 return -EINVAL; 1177 } 1178 buf[s.len] = 0; 1179 last_cmd_set(buf); 1180 1181 kfree(buf); 1182 return 0; 1183 } 1184 1185 static int __create_synth_event(int argc, const char *name, const char **argv) 1186 { 1187 struct synth_field *field, *fields[SYNTH_FIELDS_MAX]; 1188 struct synth_event *event = NULL; 1189 int i, consumed = 0, n_fields = 0, ret = 0; 1190 1191 ret = save_cmdstr(argc, name, argv); 1192 if (ret) 1193 return ret; 1194 1195 /* 1196 * Argument syntax: 1197 * - Add synthetic event: <event_name> field[;field] ... 1198 * - Remove synthetic event: !<event_name> field[;field] ... 1199 * where 'field' = type field_name 1200 */ 1201 1202 if (name[0] == '\0' || argc < 1) { 1203 synth_err(SYNTH_ERR_CMD_INCOMPLETE, 0); 1204 return -EINVAL; 1205 } 1206 1207 mutex_lock(&event_mutex); 1208 1209 if (!is_good_name(name)) { 1210 synth_err(SYNTH_ERR_BAD_NAME, errpos(name)); 1211 ret = -EINVAL; 1212 goto out; 1213 } 1214 1215 event = find_synth_event(name); 1216 if (event) { 1217 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name)); 1218 ret = -EEXIST; 1219 goto out; 1220 } 1221 1222 for (i = 0; i < argc - 1; i++) { 1223 if (strcmp(argv[i], ";") == 0) 1224 continue; 1225 if (n_fields == SYNTH_FIELDS_MAX) { 1226 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0); 1227 ret = -EINVAL; 1228 goto err; 1229 } 1230 1231 field = parse_synth_field(argc - i, &argv[i], &consumed); 1232 if (IS_ERR(field)) { 1233 ret = PTR_ERR(field); 1234 goto err; 1235 } 1236 fields[n_fields++] = field; 1237 i += consumed - 1; 1238 } 1239 1240 if (i < argc && strcmp(argv[i], ";") != 0) { 1241 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(argv[i])); 1242 ret = -EINVAL; 1243 goto err; 1244 } 1245 1246 event = alloc_synth_event(name, n_fields, fields); 1247 if (IS_ERR(event)) { 1248 ret = PTR_ERR(event); 1249 event = NULL; 1250 goto err; 1251 } 1252 ret = register_synth_event(event); 1253 if (!ret) 1254 dyn_event_add(&event->devent); 1255 else 1256 free_synth_event(event); 1257 out: 1258 mutex_unlock(&event_mutex); 1259 1260 return ret; 1261 err: 1262 for (i = 0; i < n_fields; i++) 1263 free_synth_field(fields[i]); 1264 1265 goto out; 1266 } 1267 1268 /** 1269 * synth_event_create - Create a new synthetic event 1270 * @name: The name of the new sythetic event 1271 * @fields: An array of type/name field descriptions 1272 * @n_fields: The number of field descriptions contained in the fields array 1273 * @mod: The module creating the event, NULL if not created from a module 1274 * 1275 * Create a new synthetic event with the given name under the 1276 * trace/events/synthetic/ directory. The event fields that will be 1277 * defined for the event should be passed in as an array of struct 1278 * synth_field_desc, and the number elements in the array passed in as 1279 * n_fields. Field ordering will retain the ordering given in the 1280 * fields array. 1281 * 1282 * If the new synthetic event is being created from a module, the mod 1283 * param must be non-NULL. This will ensure that the trace buffer 1284 * won't contain unreadable events. 1285 * 1286 * The new synth event should be deleted using synth_event_delete() 1287 * function. The new synthetic event can be generated from modules or 1288 * other kernel code using trace_synth_event() and related functions. 1289 * 1290 * Return: 0 if successful, error otherwise. 1291 */ 1292 int synth_event_create(const char *name, struct synth_field_desc *fields, 1293 unsigned int n_fields, struct module *mod) 1294 { 1295 struct dynevent_cmd cmd; 1296 char *buf; 1297 int ret; 1298 1299 buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL); 1300 if (!buf) 1301 return -ENOMEM; 1302 1303 synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN); 1304 1305 ret = synth_event_gen_cmd_array_start(&cmd, name, mod, 1306 fields, n_fields); 1307 if (ret) 1308 goto out; 1309 1310 ret = synth_event_gen_cmd_end(&cmd); 1311 out: 1312 kfree(buf); 1313 1314 return ret; 1315 } 1316 EXPORT_SYMBOL_GPL(synth_event_create); 1317 1318 static int destroy_synth_event(struct synth_event *se) 1319 { 1320 int ret; 1321 1322 if (se->ref) 1323 ret = -EBUSY; 1324 else { 1325 ret = unregister_synth_event(se); 1326 if (!ret) { 1327 dyn_event_remove(&se->devent); 1328 free_synth_event(se); 1329 } 1330 } 1331 1332 return ret; 1333 } 1334 1335 /** 1336 * synth_event_delete - Delete a synthetic event 1337 * @event_name: The name of the new sythetic event 1338 * 1339 * Delete a synthetic event that was created with synth_event_create(). 1340 * 1341 * Return: 0 if successful, error otherwise. 1342 */ 1343 int synth_event_delete(const char *event_name) 1344 { 1345 struct synth_event *se = NULL; 1346 struct module *mod = NULL; 1347 int ret = -ENOENT; 1348 1349 mutex_lock(&event_mutex); 1350 se = find_synth_event(event_name); 1351 if (se) { 1352 mod = se->mod; 1353 ret = destroy_synth_event(se); 1354 } 1355 mutex_unlock(&event_mutex); 1356 1357 if (mod) { 1358 mutex_lock(&trace_types_lock); 1359 /* 1360 * It is safest to reset the ring buffer if the module 1361 * being unloaded registered any events that were 1362 * used. The only worry is if a new module gets 1363 * loaded, and takes on the same id as the events of 1364 * this module. When printing out the buffer, traced 1365 * events left over from this module may be passed to 1366 * the new module events and unexpected results may 1367 * occur. 1368 */ 1369 tracing_reset_all_online_cpus(); 1370 mutex_unlock(&trace_types_lock); 1371 } 1372 1373 return ret; 1374 } 1375 EXPORT_SYMBOL_GPL(synth_event_delete); 1376 1377 static int create_or_delete_synth_event(int argc, char **argv) 1378 { 1379 const char *name = argv[0]; 1380 int ret; 1381 1382 /* trace_run_command() ensures argc != 0 */ 1383 if (name[0] == '!') { 1384 ret = synth_event_delete(name + 1); 1385 return ret; 1386 } 1387 1388 ret = __create_synth_event(argc - 1, name, (const char **)argv + 1); 1389 return ret == -ECANCELED ? -EINVAL : ret; 1390 } 1391 1392 static int synth_event_run_command(struct dynevent_cmd *cmd) 1393 { 1394 struct synth_event *se; 1395 int ret; 1396 1397 ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event); 1398 if (ret) 1399 return ret; 1400 1401 se = find_synth_event(cmd->event_name); 1402 if (WARN_ON(!se)) 1403 return -ENOENT; 1404 1405 se->mod = cmd->private_data; 1406 1407 return ret; 1408 } 1409 1410 /** 1411 * synth_event_cmd_init - Initialize a synthetic event command object 1412 * @cmd: A pointer to the dynevent_cmd struct representing the new event 1413 * @buf: A pointer to the buffer used to build the command 1414 * @maxlen: The length of the buffer passed in @buf 1415 * 1416 * Initialize a synthetic event command object. Use this before 1417 * calling any of the other dyenvent_cmd functions. 1418 */ 1419 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen) 1420 { 1421 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH, 1422 synth_event_run_command); 1423 } 1424 EXPORT_SYMBOL_GPL(synth_event_cmd_init); 1425 1426 static inline int 1427 __synth_event_trace_init(struct trace_event_file *file, 1428 struct synth_event_trace_state *trace_state) 1429 { 1430 int ret = 0; 1431 1432 memset(trace_state, '\0', sizeof(*trace_state)); 1433 1434 /* 1435 * Normal event tracing doesn't get called at all unless the 1436 * ENABLED bit is set (which attaches the probe thus allowing 1437 * this code to be called, etc). Because this is called 1438 * directly by the user, we don't have that but we still need 1439 * to honor not logging when disabled. For the iterated 1440 * trace case, we save the enabed state upon start and just 1441 * ignore the following data calls. 1442 */ 1443 if (!(file->flags & EVENT_FILE_FL_ENABLED) || 1444 trace_trigger_soft_disabled(file)) { 1445 trace_state->disabled = true; 1446 ret = -ENOENT; 1447 goto out; 1448 } 1449 1450 trace_state->event = file->event_call->data; 1451 out: 1452 return ret; 1453 } 1454 1455 static inline int 1456 __synth_event_trace_start(struct trace_event_file *file, 1457 struct synth_event_trace_state *trace_state, 1458 int dynamic_fields_size) 1459 { 1460 int entry_size, fields_size = 0; 1461 int ret = 0; 1462 1463 fields_size = trace_state->event->n_u64 * sizeof(u64); 1464 fields_size += dynamic_fields_size; 1465 1466 /* 1467 * Avoid ring buffer recursion detection, as this event 1468 * is being performed within another event. 1469 */ 1470 trace_state->buffer = file->tr->array_buffer.buffer; 1471 ring_buffer_nest_start(trace_state->buffer); 1472 1473 entry_size = sizeof(*trace_state->entry) + fields_size; 1474 trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer, 1475 file, 1476 entry_size); 1477 if (!trace_state->entry) { 1478 ring_buffer_nest_end(trace_state->buffer); 1479 ret = -EINVAL; 1480 } 1481 1482 return ret; 1483 } 1484 1485 static inline void 1486 __synth_event_trace_end(struct synth_event_trace_state *trace_state) 1487 { 1488 trace_event_buffer_commit(&trace_state->fbuffer); 1489 1490 ring_buffer_nest_end(trace_state->buffer); 1491 } 1492 1493 /** 1494 * synth_event_trace - Trace a synthetic event 1495 * @file: The trace_event_file representing the synthetic event 1496 * @n_vals: The number of values in vals 1497 * @args: Variable number of args containing the event values 1498 * 1499 * Trace a synthetic event using the values passed in the variable 1500 * argument list. 1501 * 1502 * The argument list should be a list 'n_vals' u64 values. The number 1503 * of vals must match the number of field in the synthetic event, and 1504 * must be in the same order as the synthetic event fields. 1505 * 1506 * All vals should be cast to u64, and string vals are just pointers 1507 * to strings, cast to u64. Strings will be copied into space 1508 * reserved in the event for the string, using these pointers. 1509 * 1510 * Return: 0 on success, err otherwise. 1511 */ 1512 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...) 1513 { 1514 unsigned int i, n_u64, len, data_size = 0; 1515 struct synth_event_trace_state state; 1516 va_list args; 1517 int ret; 1518 1519 ret = __synth_event_trace_init(file, &state); 1520 if (ret) { 1521 if (ret == -ENOENT) 1522 ret = 0; /* just disabled, not really an error */ 1523 return ret; 1524 } 1525 1526 if (state.event->n_dynamic_fields) { 1527 va_start(args, n_vals); 1528 1529 for (i = 0; i < state.event->n_fields; i++) { 1530 u64 val = va_arg(args, u64); 1531 1532 if (state.event->fields[i]->is_string && 1533 state.event->fields[i]->is_dynamic) { 1534 char *str_val = (char *)(long)val; 1535 1536 data_size += strlen(str_val) + 1; 1537 } 1538 } 1539 1540 va_end(args); 1541 } 1542 1543 ret = __synth_event_trace_start(file, &state, data_size); 1544 if (ret) 1545 return ret; 1546 1547 if (n_vals != state.event->n_fields) { 1548 ret = -EINVAL; 1549 goto out; 1550 } 1551 1552 data_size = 0; 1553 1554 va_start(args, n_vals); 1555 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) { 1556 u64 val; 1557 1558 val = va_arg(args, u64); 1559 1560 if (state.event->fields[i]->is_string) { 1561 char *str_val = (char *)(long)val; 1562 1563 len = trace_string(state.entry, state.event, str_val, 1564 state.event->fields[i]->is_dynamic, 1565 data_size, &n_u64); 1566 data_size += len; /* only dynamic string increments */ 1567 } else { 1568 struct synth_field *field = state.event->fields[i]; 1569 1570 switch (field->size) { 1571 case 1: 1572 *(u8 *)&state.entry->fields[n_u64] = (u8)val; 1573 break; 1574 1575 case 2: 1576 *(u16 *)&state.entry->fields[n_u64] = (u16)val; 1577 break; 1578 1579 case 4: 1580 *(u32 *)&state.entry->fields[n_u64] = (u32)val; 1581 break; 1582 1583 default: 1584 state.entry->fields[n_u64] = val; 1585 break; 1586 } 1587 n_u64++; 1588 } 1589 } 1590 va_end(args); 1591 out: 1592 __synth_event_trace_end(&state); 1593 1594 return ret; 1595 } 1596 EXPORT_SYMBOL_GPL(synth_event_trace); 1597 1598 /** 1599 * synth_event_trace_array - Trace a synthetic event from an array 1600 * @file: The trace_event_file representing the synthetic event 1601 * @vals: Array of values 1602 * @n_vals: The number of values in vals 1603 * 1604 * Trace a synthetic event using the values passed in as 'vals'. 1605 * 1606 * The 'vals' array is just an array of 'n_vals' u64. The number of 1607 * vals must match the number of field in the synthetic event, and 1608 * must be in the same order as the synthetic event fields. 1609 * 1610 * All vals should be cast to u64, and string vals are just pointers 1611 * to strings, cast to u64. Strings will be copied into space 1612 * reserved in the event for the string, using these pointers. 1613 * 1614 * Return: 0 on success, err otherwise. 1615 */ 1616 int synth_event_trace_array(struct trace_event_file *file, u64 *vals, 1617 unsigned int n_vals) 1618 { 1619 unsigned int i, n_u64, field_pos, len, data_size = 0; 1620 struct synth_event_trace_state state; 1621 char *str_val; 1622 int ret; 1623 1624 ret = __synth_event_trace_init(file, &state); 1625 if (ret) { 1626 if (ret == -ENOENT) 1627 ret = 0; /* just disabled, not really an error */ 1628 return ret; 1629 } 1630 1631 if (state.event->n_dynamic_fields) { 1632 for (i = 0; i < state.event->n_dynamic_fields; i++) { 1633 field_pos = state.event->dynamic_fields[i]->field_pos; 1634 str_val = (char *)(long)vals[field_pos]; 1635 len = strlen(str_val) + 1; 1636 data_size += len; 1637 } 1638 } 1639 1640 ret = __synth_event_trace_start(file, &state, data_size); 1641 if (ret) 1642 return ret; 1643 1644 if (n_vals != state.event->n_fields) { 1645 ret = -EINVAL; 1646 goto out; 1647 } 1648 1649 data_size = 0; 1650 1651 for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) { 1652 if (state.event->fields[i]->is_string) { 1653 char *str_val = (char *)(long)vals[i]; 1654 1655 len = trace_string(state.entry, state.event, str_val, 1656 state.event->fields[i]->is_dynamic, 1657 data_size, &n_u64); 1658 data_size += len; /* only dynamic string increments */ 1659 } else { 1660 struct synth_field *field = state.event->fields[i]; 1661 u64 val = vals[i]; 1662 1663 switch (field->size) { 1664 case 1: 1665 *(u8 *)&state.entry->fields[n_u64] = (u8)val; 1666 break; 1667 1668 case 2: 1669 *(u16 *)&state.entry->fields[n_u64] = (u16)val; 1670 break; 1671 1672 case 4: 1673 *(u32 *)&state.entry->fields[n_u64] = (u32)val; 1674 break; 1675 1676 default: 1677 state.entry->fields[n_u64] = val; 1678 break; 1679 } 1680 n_u64++; 1681 } 1682 } 1683 out: 1684 __synth_event_trace_end(&state); 1685 1686 return ret; 1687 } 1688 EXPORT_SYMBOL_GPL(synth_event_trace_array); 1689 1690 /** 1691 * synth_event_trace_start - Start piecewise synthetic event trace 1692 * @file: The trace_event_file representing the synthetic event 1693 * @trace_state: A pointer to object tracking the piecewise trace state 1694 * 1695 * Start the trace of a synthetic event field-by-field rather than all 1696 * at once. 1697 * 1698 * This function 'opens' an event trace, which means space is reserved 1699 * for the event in the trace buffer, after which the event's 1700 * individual field values can be set through either 1701 * synth_event_add_next_val() or synth_event_add_val(). 1702 * 1703 * A pointer to a trace_state object is passed in, which will keep 1704 * track of the current event trace state until the event trace is 1705 * closed (and the event finally traced) using 1706 * synth_event_trace_end(). 1707 * 1708 * Note that synth_event_trace_end() must be called after all values 1709 * have been added for each event trace, regardless of whether adding 1710 * all field values succeeded or not. 1711 * 1712 * Note also that for a given event trace, all fields must be added 1713 * using either synth_event_add_next_val() or synth_event_add_val() 1714 * but not both together or interleaved. 1715 * 1716 * Return: 0 on success, err otherwise. 1717 */ 1718 int synth_event_trace_start(struct trace_event_file *file, 1719 struct synth_event_trace_state *trace_state) 1720 { 1721 int ret; 1722 1723 if (!trace_state) 1724 return -EINVAL; 1725 1726 ret = __synth_event_trace_init(file, trace_state); 1727 if (ret) { 1728 if (ret == -ENOENT) 1729 ret = 0; /* just disabled, not really an error */ 1730 return ret; 1731 } 1732 1733 if (trace_state->event->n_dynamic_fields) 1734 return -ENOTSUPP; 1735 1736 ret = __synth_event_trace_start(file, trace_state, 0); 1737 1738 return ret; 1739 } 1740 EXPORT_SYMBOL_GPL(synth_event_trace_start); 1741 1742 static int __synth_event_add_val(const char *field_name, u64 val, 1743 struct synth_event_trace_state *trace_state) 1744 { 1745 struct synth_field *field = NULL; 1746 struct synth_trace_event *entry; 1747 struct synth_event *event; 1748 int i, ret = 0; 1749 1750 if (!trace_state) { 1751 ret = -EINVAL; 1752 goto out; 1753 } 1754 1755 /* can't mix add_next_synth_val() with add_synth_val() */ 1756 if (field_name) { 1757 if (trace_state->add_next) { 1758 ret = -EINVAL; 1759 goto out; 1760 } 1761 trace_state->add_name = true; 1762 } else { 1763 if (trace_state->add_name) { 1764 ret = -EINVAL; 1765 goto out; 1766 } 1767 trace_state->add_next = true; 1768 } 1769 1770 if (trace_state->disabled) 1771 goto out; 1772 1773 event = trace_state->event; 1774 if (trace_state->add_name) { 1775 for (i = 0; i < event->n_fields; i++) { 1776 field = event->fields[i]; 1777 if (strcmp(field->name, field_name) == 0) 1778 break; 1779 } 1780 if (!field) { 1781 ret = -EINVAL; 1782 goto out; 1783 } 1784 } else { 1785 if (trace_state->cur_field >= event->n_fields) { 1786 ret = -EINVAL; 1787 goto out; 1788 } 1789 field = event->fields[trace_state->cur_field++]; 1790 } 1791 1792 entry = trace_state->entry; 1793 if (field->is_string) { 1794 char *str_val = (char *)(long)val; 1795 char *str_field; 1796 1797 if (field->is_dynamic) { /* add_val can't do dynamic strings */ 1798 ret = -EINVAL; 1799 goto out; 1800 } 1801 1802 if (!str_val) { 1803 ret = -EINVAL; 1804 goto out; 1805 } 1806 1807 str_field = (char *)&entry->fields[field->offset]; 1808 strscpy(str_field, str_val, STR_VAR_LEN_MAX); 1809 } else { 1810 switch (field->size) { 1811 case 1: 1812 *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val; 1813 break; 1814 1815 case 2: 1816 *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val; 1817 break; 1818 1819 case 4: 1820 *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val; 1821 break; 1822 1823 default: 1824 trace_state->entry->fields[field->offset] = val; 1825 break; 1826 } 1827 } 1828 out: 1829 return ret; 1830 } 1831 1832 /** 1833 * synth_event_add_next_val - Add the next field's value to an open synth trace 1834 * @val: The value to set the next field to 1835 * @trace_state: A pointer to object tracking the piecewise trace state 1836 * 1837 * Set the value of the next field in an event that's been opened by 1838 * synth_event_trace_start(). 1839 * 1840 * The val param should be the value cast to u64. If the value points 1841 * to a string, the val param should be a char * cast to u64. 1842 * 1843 * This function assumes all the fields in an event are to be set one 1844 * after another - successive calls to this function are made, one for 1845 * each field, in the order of the fields in the event, until all 1846 * fields have been set. If you'd rather set each field individually 1847 * without regard to ordering, synth_event_add_val() can be used 1848 * instead. 1849 * 1850 * Note however that synth_event_add_next_val() and 1851 * synth_event_add_val() can't be intermixed for a given event trace - 1852 * one or the other but not both can be used at the same time. 1853 * 1854 * Note also that synth_event_trace_end() must be called after all 1855 * values have been added for each event trace, regardless of whether 1856 * adding all field values succeeded or not. 1857 * 1858 * Return: 0 on success, err otherwise. 1859 */ 1860 int synth_event_add_next_val(u64 val, 1861 struct synth_event_trace_state *trace_state) 1862 { 1863 return __synth_event_add_val(NULL, val, trace_state); 1864 } 1865 EXPORT_SYMBOL_GPL(synth_event_add_next_val); 1866 1867 /** 1868 * synth_event_add_val - Add a named field's value to an open synth trace 1869 * @field_name: The name of the synthetic event field value to set 1870 * @val: The value to set the next field to 1871 * @trace_state: A pointer to object tracking the piecewise trace state 1872 * 1873 * Set the value of the named field in an event that's been opened by 1874 * synth_event_trace_start(). 1875 * 1876 * The val param should be the value cast to u64. If the value points 1877 * to a string, the val param should be a char * cast to u64. 1878 * 1879 * This function looks up the field name, and if found, sets the field 1880 * to the specified value. This lookup makes this function more 1881 * expensive than synth_event_add_next_val(), so use that or the 1882 * none-piecewise synth_event_trace() instead if efficiency is more 1883 * important. 1884 * 1885 * Note however that synth_event_add_next_val() and 1886 * synth_event_add_val() can't be intermixed for a given event trace - 1887 * one or the other but not both can be used at the same time. 1888 * 1889 * Note also that synth_event_trace_end() must be called after all 1890 * values have been added for each event trace, regardless of whether 1891 * adding all field values succeeded or not. 1892 * 1893 * Return: 0 on success, err otherwise. 1894 */ 1895 int synth_event_add_val(const char *field_name, u64 val, 1896 struct synth_event_trace_state *trace_state) 1897 { 1898 return __synth_event_add_val(field_name, val, trace_state); 1899 } 1900 EXPORT_SYMBOL_GPL(synth_event_add_val); 1901 1902 /** 1903 * synth_event_trace_end - End piecewise synthetic event trace 1904 * @trace_state: A pointer to object tracking the piecewise trace state 1905 * 1906 * End the trace of a synthetic event opened by 1907 * synth_event_trace__start(). 1908 * 1909 * This function 'closes' an event trace, which basically means that 1910 * it commits the reserved event and cleans up other loose ends. 1911 * 1912 * A pointer to a trace_state object is passed in, which will keep 1913 * track of the current event trace state opened with 1914 * synth_event_trace_start(). 1915 * 1916 * Note that this function must be called after all values have been 1917 * added for each event trace, regardless of whether adding all field 1918 * values succeeded or not. 1919 * 1920 * Return: 0 on success, err otherwise. 1921 */ 1922 int synth_event_trace_end(struct synth_event_trace_state *trace_state) 1923 { 1924 if (!trace_state) 1925 return -EINVAL; 1926 1927 __synth_event_trace_end(trace_state); 1928 1929 return 0; 1930 } 1931 EXPORT_SYMBOL_GPL(synth_event_trace_end); 1932 1933 static int create_synth_event(int argc, const char **argv) 1934 { 1935 const char *name = argv[0]; 1936 int len; 1937 1938 if (name[0] != 's' || name[1] != ':') 1939 return -ECANCELED; 1940 name += 2; 1941 1942 /* This interface accepts group name prefix */ 1943 if (strchr(name, '/')) { 1944 len = str_has_prefix(name, SYNTH_SYSTEM "/"); 1945 if (len == 0) 1946 return -EINVAL; 1947 name += len; 1948 } 1949 return __create_synth_event(argc - 1, name, argv + 1); 1950 } 1951 1952 static int synth_event_release(struct dyn_event *ev) 1953 { 1954 struct synth_event *event = to_synth_event(ev); 1955 int ret; 1956 1957 if (event->ref) 1958 return -EBUSY; 1959 1960 ret = unregister_synth_event(event); 1961 if (ret) 1962 return ret; 1963 1964 dyn_event_remove(ev); 1965 free_synth_event(event); 1966 return 0; 1967 } 1968 1969 static int __synth_event_show(struct seq_file *m, struct synth_event *event) 1970 { 1971 struct synth_field *field; 1972 unsigned int i; 1973 char *type, *t; 1974 1975 seq_printf(m, "%s\t", event->name); 1976 1977 for (i = 0; i < event->n_fields; i++) { 1978 field = event->fields[i]; 1979 1980 type = field->type; 1981 t = strstr(type, "__data_loc"); 1982 if (t) { /* __data_loc belongs in format but not event desc */ 1983 t += sizeof("__data_loc"); 1984 type = t; 1985 } 1986 1987 /* parameter values */ 1988 seq_printf(m, "%s %s%s", type, field->name, 1989 i == event->n_fields - 1 ? "" : "; "); 1990 } 1991 1992 seq_putc(m, '\n'); 1993 1994 return 0; 1995 } 1996 1997 static int synth_event_show(struct seq_file *m, struct dyn_event *ev) 1998 { 1999 struct synth_event *event = to_synth_event(ev); 2000 2001 seq_printf(m, "s:%s/", event->class.system); 2002 2003 return __synth_event_show(m, event); 2004 } 2005 2006 static int synth_events_seq_show(struct seq_file *m, void *v) 2007 { 2008 struct dyn_event *ev = v; 2009 2010 if (!is_synth_event(ev)) 2011 return 0; 2012 2013 return __synth_event_show(m, to_synth_event(ev)); 2014 } 2015 2016 static const struct seq_operations synth_events_seq_op = { 2017 .start = dyn_event_seq_start, 2018 .next = dyn_event_seq_next, 2019 .stop = dyn_event_seq_stop, 2020 .show = synth_events_seq_show, 2021 }; 2022 2023 static int synth_events_open(struct inode *inode, struct file *file) 2024 { 2025 int ret; 2026 2027 ret = security_locked_down(LOCKDOWN_TRACEFS); 2028 if (ret) 2029 return ret; 2030 2031 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 2032 ret = dyn_events_release_all(&synth_event_ops); 2033 if (ret < 0) 2034 return ret; 2035 } 2036 2037 return seq_open(file, &synth_events_seq_op); 2038 } 2039 2040 static ssize_t synth_events_write(struct file *file, 2041 const char __user *buffer, 2042 size_t count, loff_t *ppos) 2043 { 2044 return trace_parse_run_command(file, buffer, count, ppos, 2045 create_or_delete_synth_event); 2046 } 2047 2048 static const struct file_operations synth_events_fops = { 2049 .open = synth_events_open, 2050 .write = synth_events_write, 2051 .read = seq_read, 2052 .llseek = seq_lseek, 2053 .release = seq_release, 2054 }; 2055 2056 /* 2057 * Register dynevent at core_initcall. This allows kernel to setup kprobe 2058 * events in postcore_initcall without tracefs. 2059 */ 2060 static __init int trace_events_synth_init_early(void) 2061 { 2062 int err = 0; 2063 2064 err = dyn_event_register(&synth_event_ops); 2065 if (err) 2066 pr_warn("Could not register synth_event_ops\n"); 2067 2068 return err; 2069 } 2070 core_initcall(trace_events_synth_init_early); 2071 2072 static __init int trace_events_synth_init(void) 2073 { 2074 struct dentry *entry = NULL; 2075 int err = 0; 2076 err = tracing_init_dentry(); 2077 if (err) 2078 goto err; 2079 2080 entry = tracefs_create_file("synthetic_events", 0644, NULL, 2081 NULL, &synth_events_fops); 2082 if (!entry) { 2083 err = -ENODEV; 2084 goto err; 2085 } 2086 2087 return err; 2088 err: 2089 pr_warn("Could not create tracefs 'synthetic_events' entry\n"); 2090 2091 return err; 2092 } 2093 2094 fs_initcall(trace_events_synth_init); 2095