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