1 /* 2 * event tracer 3 * 4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> 5 * 6 * - Added format output of fields of the trace point. 7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. 8 * 9 */ 10 11 #define pr_fmt(fmt) fmt 12 13 #include <linux/workqueue.h> 14 #include <linux/spinlock.h> 15 #include <linux/kthread.h> 16 #include <linux/tracefs.h> 17 #include <linux/uaccess.h> 18 #include <linux/bsearch.h> 19 #include <linux/module.h> 20 #include <linux/ctype.h> 21 #include <linux/sort.h> 22 #include <linux/slab.h> 23 #include <linux/delay.h> 24 25 #include <trace/events/sched.h> 26 27 #include <asm/setup.h> 28 29 #include "trace_output.h" 30 31 #undef TRACE_SYSTEM 32 #define TRACE_SYSTEM "TRACE_SYSTEM" 33 34 DEFINE_MUTEX(event_mutex); 35 36 LIST_HEAD(ftrace_events); 37 static LIST_HEAD(ftrace_generic_fields); 38 static LIST_HEAD(ftrace_common_fields); 39 40 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO) 41 42 static struct kmem_cache *field_cachep; 43 static struct kmem_cache *file_cachep; 44 45 static inline int system_refcount(struct event_subsystem *system) 46 { 47 return system->ref_count; 48 } 49 50 static int system_refcount_inc(struct event_subsystem *system) 51 { 52 return system->ref_count++; 53 } 54 55 static int system_refcount_dec(struct event_subsystem *system) 56 { 57 return --system->ref_count; 58 } 59 60 /* Double loops, do not use break, only goto's work */ 61 #define do_for_each_event_file(tr, file) \ 62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 63 list_for_each_entry(file, &tr->events, list) 64 65 #define do_for_each_event_file_safe(tr, file) \ 66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 67 struct trace_event_file *___n; \ 68 list_for_each_entry_safe(file, ___n, &tr->events, list) 69 70 #define while_for_each_event_file() \ 71 } 72 73 static struct list_head * 74 trace_get_fields(struct trace_event_call *event_call) 75 { 76 if (!event_call->class->get_fields) 77 return &event_call->class->fields; 78 return event_call->class->get_fields(event_call); 79 } 80 81 static struct ftrace_event_field * 82 __find_event_field(struct list_head *head, char *name) 83 { 84 struct ftrace_event_field *field; 85 86 list_for_each_entry(field, head, link) { 87 if (!strcmp(field->name, name)) 88 return field; 89 } 90 91 return NULL; 92 } 93 94 struct ftrace_event_field * 95 trace_find_event_field(struct trace_event_call *call, char *name) 96 { 97 struct ftrace_event_field *field; 98 struct list_head *head; 99 100 field = __find_event_field(&ftrace_generic_fields, name); 101 if (field) 102 return field; 103 104 field = __find_event_field(&ftrace_common_fields, name); 105 if (field) 106 return field; 107 108 head = trace_get_fields(call); 109 return __find_event_field(head, name); 110 } 111 112 static int __trace_define_field(struct list_head *head, const char *type, 113 const char *name, int offset, int size, 114 int is_signed, int filter_type) 115 { 116 struct ftrace_event_field *field; 117 118 field = kmem_cache_alloc(field_cachep, GFP_TRACE); 119 if (!field) 120 return -ENOMEM; 121 122 field->name = name; 123 field->type = type; 124 125 if (filter_type == FILTER_OTHER) 126 field->filter_type = filter_assign_type(type); 127 else 128 field->filter_type = filter_type; 129 130 field->offset = offset; 131 field->size = size; 132 field->is_signed = is_signed; 133 134 list_add(&field->link, head); 135 136 return 0; 137 } 138 139 int trace_define_field(struct trace_event_call *call, const char *type, 140 const char *name, int offset, int size, int is_signed, 141 int filter_type) 142 { 143 struct list_head *head; 144 145 if (WARN_ON(!call->class)) 146 return 0; 147 148 head = trace_get_fields(call); 149 return __trace_define_field(head, type, name, offset, size, 150 is_signed, filter_type); 151 } 152 EXPORT_SYMBOL_GPL(trace_define_field); 153 154 #define __generic_field(type, item, filter_type) \ 155 ret = __trace_define_field(&ftrace_generic_fields, #type, \ 156 #item, 0, 0, is_signed_type(type), \ 157 filter_type); \ 158 if (ret) \ 159 return ret; 160 161 #define __common_field(type, item) \ 162 ret = __trace_define_field(&ftrace_common_fields, #type, \ 163 "common_" #item, \ 164 offsetof(typeof(ent), item), \ 165 sizeof(ent.item), \ 166 is_signed_type(type), FILTER_OTHER); \ 167 if (ret) \ 168 return ret; 169 170 static int trace_define_generic_fields(void) 171 { 172 int ret; 173 174 __generic_field(int, cpu, FILTER_OTHER); 175 __generic_field(char *, comm, FILTER_PTR_STRING); 176 177 return ret; 178 } 179 180 static int trace_define_common_fields(void) 181 { 182 int ret; 183 struct trace_entry ent; 184 185 __common_field(unsigned short, type); 186 __common_field(unsigned char, flags); 187 __common_field(unsigned char, preempt_count); 188 __common_field(int, pid); 189 190 return ret; 191 } 192 193 static void trace_destroy_fields(struct trace_event_call *call) 194 { 195 struct ftrace_event_field *field, *next; 196 struct list_head *head; 197 198 head = trace_get_fields(call); 199 list_for_each_entry_safe(field, next, head, link) { 200 list_del(&field->link); 201 kmem_cache_free(field_cachep, field); 202 } 203 } 204 205 int trace_event_raw_init(struct trace_event_call *call) 206 { 207 int id; 208 209 id = register_trace_event(&call->event); 210 if (!id) 211 return -ENODEV; 212 213 return 0; 214 } 215 EXPORT_SYMBOL_GPL(trace_event_raw_init); 216 217 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file) 218 { 219 struct trace_array *tr = trace_file->tr; 220 struct trace_array_cpu *data; 221 struct trace_pid_list *pid_list; 222 223 pid_list = rcu_dereference_sched(tr->filtered_pids); 224 if (!pid_list) 225 return false; 226 227 data = this_cpu_ptr(tr->trace_buffer.data); 228 229 return data->ignore_pid; 230 } 231 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid); 232 233 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer, 234 struct trace_event_file *trace_file, 235 unsigned long len) 236 { 237 struct trace_event_call *event_call = trace_file->event_call; 238 239 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) && 240 trace_event_ignore_this_pid(trace_file)) 241 return NULL; 242 243 local_save_flags(fbuffer->flags); 244 fbuffer->pc = preempt_count(); 245 fbuffer->trace_file = trace_file; 246 247 fbuffer->event = 248 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file, 249 event_call->event.type, len, 250 fbuffer->flags, fbuffer->pc); 251 if (!fbuffer->event) 252 return NULL; 253 254 fbuffer->entry = ring_buffer_event_data(fbuffer->event); 255 return fbuffer->entry; 256 } 257 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve); 258 259 static DEFINE_SPINLOCK(tracepoint_iter_lock); 260 261 static void output_printk(struct trace_event_buffer *fbuffer) 262 { 263 struct trace_event_call *event_call; 264 struct trace_event *event; 265 unsigned long flags; 266 struct trace_iterator *iter = tracepoint_print_iter; 267 268 if (!iter) 269 return; 270 271 event_call = fbuffer->trace_file->event_call; 272 if (!event_call || !event_call->event.funcs || 273 !event_call->event.funcs->trace) 274 return; 275 276 event = &fbuffer->trace_file->event_call->event; 277 278 spin_lock_irqsave(&tracepoint_iter_lock, flags); 279 trace_seq_init(&iter->seq); 280 iter->ent = fbuffer->entry; 281 event_call->event.funcs->trace(iter, 0, event); 282 trace_seq_putc(&iter->seq, 0); 283 printk("%s", iter->seq.buffer); 284 285 spin_unlock_irqrestore(&tracepoint_iter_lock, flags); 286 } 287 288 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer) 289 { 290 if (tracepoint_printk) 291 output_printk(fbuffer); 292 293 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer, 294 fbuffer->event, fbuffer->entry, 295 fbuffer->flags, fbuffer->pc); 296 } 297 EXPORT_SYMBOL_GPL(trace_event_buffer_commit); 298 299 int trace_event_reg(struct trace_event_call *call, 300 enum trace_reg type, void *data) 301 { 302 struct trace_event_file *file = data; 303 304 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT)); 305 switch (type) { 306 case TRACE_REG_REGISTER: 307 return tracepoint_probe_register(call->tp, 308 call->class->probe, 309 file); 310 case TRACE_REG_UNREGISTER: 311 tracepoint_probe_unregister(call->tp, 312 call->class->probe, 313 file); 314 return 0; 315 316 #ifdef CONFIG_PERF_EVENTS 317 case TRACE_REG_PERF_REGISTER: 318 return tracepoint_probe_register(call->tp, 319 call->class->perf_probe, 320 call); 321 case TRACE_REG_PERF_UNREGISTER: 322 tracepoint_probe_unregister(call->tp, 323 call->class->perf_probe, 324 call); 325 return 0; 326 case TRACE_REG_PERF_OPEN: 327 case TRACE_REG_PERF_CLOSE: 328 case TRACE_REG_PERF_ADD: 329 case TRACE_REG_PERF_DEL: 330 return 0; 331 #endif 332 } 333 return 0; 334 } 335 EXPORT_SYMBOL_GPL(trace_event_reg); 336 337 void trace_event_enable_cmd_record(bool enable) 338 { 339 struct trace_event_file *file; 340 struct trace_array *tr; 341 342 mutex_lock(&event_mutex); 343 do_for_each_event_file(tr, file) { 344 345 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 346 continue; 347 348 if (enable) { 349 tracing_start_cmdline_record(); 350 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 351 } else { 352 tracing_stop_cmdline_record(); 353 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 354 } 355 } while_for_each_event_file(); 356 mutex_unlock(&event_mutex); 357 } 358 359 static int __ftrace_event_enable_disable(struct trace_event_file *file, 360 int enable, int soft_disable) 361 { 362 struct trace_event_call *call = file->event_call; 363 struct trace_array *tr = file->tr; 364 int ret = 0; 365 int disable; 366 367 switch (enable) { 368 case 0: 369 /* 370 * When soft_disable is set and enable is cleared, the sm_ref 371 * reference counter is decremented. If it reaches 0, we want 372 * to clear the SOFT_DISABLED flag but leave the event in the 373 * state that it was. That is, if the event was enabled and 374 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED 375 * is set we do not want the event to be enabled before we 376 * clear the bit. 377 * 378 * When soft_disable is not set but the SOFT_MODE flag is, 379 * we do nothing. Do not disable the tracepoint, otherwise 380 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work. 381 */ 382 if (soft_disable) { 383 if (atomic_dec_return(&file->sm_ref) > 0) 384 break; 385 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED; 386 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags); 387 } else 388 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE); 389 390 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) { 391 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 392 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) { 393 tracing_stop_cmdline_record(); 394 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 395 } 396 call->class->reg(call, TRACE_REG_UNREGISTER, file); 397 } 398 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */ 399 if (file->flags & EVENT_FILE_FL_SOFT_MODE) 400 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 401 else 402 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 403 break; 404 case 1: 405 /* 406 * When soft_disable is set and enable is set, we want to 407 * register the tracepoint for the event, but leave the event 408 * as is. That means, if the event was already enabled, we do 409 * nothing (but set SOFT_MODE). If the event is disabled, we 410 * set SOFT_DISABLED before enabling the event tracepoint, so 411 * it still seems to be disabled. 412 */ 413 if (!soft_disable) 414 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 415 else { 416 if (atomic_inc_return(&file->sm_ref) > 1) 417 break; 418 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags); 419 } 420 421 if (!(file->flags & EVENT_FILE_FL_ENABLED)) { 422 423 /* Keep the event disabled, when going to SOFT_MODE. */ 424 if (soft_disable) 425 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 426 427 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) { 428 tracing_start_cmdline_record(); 429 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 430 } 431 ret = call->class->reg(call, TRACE_REG_REGISTER, file); 432 if (ret) { 433 tracing_stop_cmdline_record(); 434 pr_info("event trace: Could not enable event " 435 "%s\n", trace_event_name(call)); 436 break; 437 } 438 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 439 440 /* WAS_ENABLED gets set but never cleared. */ 441 call->flags |= TRACE_EVENT_FL_WAS_ENABLED; 442 } 443 break; 444 } 445 446 return ret; 447 } 448 449 int trace_event_enable_disable(struct trace_event_file *file, 450 int enable, int soft_disable) 451 { 452 return __ftrace_event_enable_disable(file, enable, soft_disable); 453 } 454 455 static int ftrace_event_enable_disable(struct trace_event_file *file, 456 int enable) 457 { 458 return __ftrace_event_enable_disable(file, enable, 0); 459 } 460 461 static void ftrace_clear_events(struct trace_array *tr) 462 { 463 struct trace_event_file *file; 464 465 mutex_lock(&event_mutex); 466 list_for_each_entry(file, &tr->events, list) { 467 ftrace_event_enable_disable(file, 0); 468 } 469 mutex_unlock(&event_mutex); 470 } 471 472 static int cmp_pid(const void *key, const void *elt) 473 { 474 const pid_t *search_pid = key; 475 const pid_t *pid = elt; 476 477 if (*search_pid == *pid) 478 return 0; 479 if (*search_pid < *pid) 480 return -1; 481 return 1; 482 } 483 484 static bool 485 check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task) 486 { 487 pid_t search_pid; 488 pid_t *pid; 489 490 /* 491 * Return false, because if filtered_pids does not exist, 492 * all pids are good to trace. 493 */ 494 if (!filtered_pids) 495 return false; 496 497 search_pid = task->pid; 498 499 pid = bsearch(&search_pid, filtered_pids->pids, 500 filtered_pids->nr_pids, sizeof(pid_t), 501 cmp_pid); 502 if (!pid) 503 return true; 504 505 return false; 506 } 507 508 static void 509 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt, 510 struct task_struct *prev, struct task_struct *next) 511 { 512 struct trace_array *tr = data; 513 struct trace_pid_list *pid_list; 514 515 pid_list = rcu_dereference_sched(tr->filtered_pids); 516 517 this_cpu_write(tr->trace_buffer.data->ignore_pid, 518 check_ignore_pid(pid_list, prev) && 519 check_ignore_pid(pid_list, next)); 520 } 521 522 static void 523 event_filter_pid_sched_switch_probe_post(void *data, bool preempt, 524 struct task_struct *prev, struct task_struct *next) 525 { 526 struct trace_array *tr = data; 527 struct trace_pid_list *pid_list; 528 529 pid_list = rcu_dereference_sched(tr->filtered_pids); 530 531 this_cpu_write(tr->trace_buffer.data->ignore_pid, 532 check_ignore_pid(pid_list, next)); 533 } 534 535 static void 536 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task) 537 { 538 struct trace_array *tr = data; 539 struct trace_pid_list *pid_list; 540 541 /* Nothing to do if we are already tracing */ 542 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid)) 543 return; 544 545 pid_list = rcu_dereference_sched(tr->filtered_pids); 546 547 this_cpu_write(tr->trace_buffer.data->ignore_pid, 548 check_ignore_pid(pid_list, task)); 549 } 550 551 static void 552 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task) 553 { 554 struct trace_array *tr = data; 555 struct trace_pid_list *pid_list; 556 557 /* Nothing to do if we are not tracing */ 558 if (this_cpu_read(tr->trace_buffer.data->ignore_pid)) 559 return; 560 561 pid_list = rcu_dereference_sched(tr->filtered_pids); 562 563 /* Set tracing if current is enabled */ 564 this_cpu_write(tr->trace_buffer.data->ignore_pid, 565 check_ignore_pid(pid_list, current)); 566 } 567 568 static void __ftrace_clear_event_pids(struct trace_array *tr) 569 { 570 struct trace_pid_list *pid_list; 571 struct trace_event_file *file; 572 int cpu; 573 574 pid_list = rcu_dereference_protected(tr->filtered_pids, 575 lockdep_is_held(&event_mutex)); 576 if (!pid_list) 577 return; 578 579 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr); 580 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr); 581 582 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr); 583 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr); 584 585 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr); 586 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr); 587 588 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr); 589 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr); 590 591 list_for_each_entry(file, &tr->events, list) { 592 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 593 } 594 595 for_each_possible_cpu(cpu) 596 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false; 597 598 rcu_assign_pointer(tr->filtered_pids, NULL); 599 600 /* Wait till all users are no longer using pid filtering */ 601 synchronize_sched(); 602 603 free_pages((unsigned long)pid_list->pids, pid_list->order); 604 kfree(pid_list); 605 } 606 607 static void ftrace_clear_event_pids(struct trace_array *tr) 608 { 609 mutex_lock(&event_mutex); 610 __ftrace_clear_event_pids(tr); 611 mutex_unlock(&event_mutex); 612 } 613 614 static void __put_system(struct event_subsystem *system) 615 { 616 struct event_filter *filter = system->filter; 617 618 WARN_ON_ONCE(system_refcount(system) == 0); 619 if (system_refcount_dec(system)) 620 return; 621 622 list_del(&system->list); 623 624 if (filter) { 625 kfree(filter->filter_string); 626 kfree(filter); 627 } 628 kfree_const(system->name); 629 kfree(system); 630 } 631 632 static void __get_system(struct event_subsystem *system) 633 { 634 WARN_ON_ONCE(system_refcount(system) == 0); 635 system_refcount_inc(system); 636 } 637 638 static void __get_system_dir(struct trace_subsystem_dir *dir) 639 { 640 WARN_ON_ONCE(dir->ref_count == 0); 641 dir->ref_count++; 642 __get_system(dir->subsystem); 643 } 644 645 static void __put_system_dir(struct trace_subsystem_dir *dir) 646 { 647 WARN_ON_ONCE(dir->ref_count == 0); 648 /* If the subsystem is about to be freed, the dir must be too */ 649 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1); 650 651 __put_system(dir->subsystem); 652 if (!--dir->ref_count) 653 kfree(dir); 654 } 655 656 static void put_system(struct trace_subsystem_dir *dir) 657 { 658 mutex_lock(&event_mutex); 659 __put_system_dir(dir); 660 mutex_unlock(&event_mutex); 661 } 662 663 static void remove_subsystem(struct trace_subsystem_dir *dir) 664 { 665 if (!dir) 666 return; 667 668 if (!--dir->nr_events) { 669 tracefs_remove_recursive(dir->entry); 670 list_del(&dir->list); 671 __put_system_dir(dir); 672 } 673 } 674 675 static void remove_event_file_dir(struct trace_event_file *file) 676 { 677 struct dentry *dir = file->dir; 678 struct dentry *child; 679 680 if (dir) { 681 spin_lock(&dir->d_lock); /* probably unneeded */ 682 list_for_each_entry(child, &dir->d_subdirs, d_child) { 683 if (d_really_is_positive(child)) /* probably unneeded */ 684 d_inode(child)->i_private = NULL; 685 } 686 spin_unlock(&dir->d_lock); 687 688 tracefs_remove_recursive(dir); 689 } 690 691 list_del(&file->list); 692 remove_subsystem(file->system); 693 free_event_filter(file->filter); 694 kmem_cache_free(file_cachep, file); 695 } 696 697 /* 698 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. 699 */ 700 static int 701 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match, 702 const char *sub, const char *event, int set) 703 { 704 struct trace_event_file *file; 705 struct trace_event_call *call; 706 const char *name; 707 int ret = -EINVAL; 708 709 list_for_each_entry(file, &tr->events, list) { 710 711 call = file->event_call; 712 name = trace_event_name(call); 713 714 if (!name || !call->class || !call->class->reg) 715 continue; 716 717 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 718 continue; 719 720 if (match && 721 strcmp(match, name) != 0 && 722 strcmp(match, call->class->system) != 0) 723 continue; 724 725 if (sub && strcmp(sub, call->class->system) != 0) 726 continue; 727 728 if (event && strcmp(event, name) != 0) 729 continue; 730 731 ftrace_event_enable_disable(file, set); 732 733 ret = 0; 734 } 735 736 return ret; 737 } 738 739 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match, 740 const char *sub, const char *event, int set) 741 { 742 int ret; 743 744 mutex_lock(&event_mutex); 745 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set); 746 mutex_unlock(&event_mutex); 747 748 return ret; 749 } 750 751 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set) 752 { 753 char *event = NULL, *sub = NULL, *match; 754 int ret; 755 756 /* 757 * The buf format can be <subsystem>:<event-name> 758 * *:<event-name> means any event by that name. 759 * :<event-name> is the same. 760 * 761 * <subsystem>:* means all events in that subsystem 762 * <subsystem>: means the same. 763 * 764 * <name> (no ':') means all events in a subsystem with 765 * the name <name> or any event that matches <name> 766 */ 767 768 match = strsep(&buf, ":"); 769 if (buf) { 770 sub = match; 771 event = buf; 772 match = NULL; 773 774 if (!strlen(sub) || strcmp(sub, "*") == 0) 775 sub = NULL; 776 if (!strlen(event) || strcmp(event, "*") == 0) 777 event = NULL; 778 } 779 780 ret = __ftrace_set_clr_event(tr, match, sub, event, set); 781 782 /* Put back the colon to allow this to be called again */ 783 if (buf) 784 *(buf - 1) = ':'; 785 786 return ret; 787 } 788 789 /** 790 * trace_set_clr_event - enable or disable an event 791 * @system: system name to match (NULL for any system) 792 * @event: event name to match (NULL for all events, within system) 793 * @set: 1 to enable, 0 to disable 794 * 795 * This is a way for other parts of the kernel to enable or disable 796 * event recording. 797 * 798 * Returns 0 on success, -EINVAL if the parameters do not match any 799 * registered events. 800 */ 801 int trace_set_clr_event(const char *system, const char *event, int set) 802 { 803 struct trace_array *tr = top_trace_array(); 804 805 if (!tr) 806 return -ENODEV; 807 808 return __ftrace_set_clr_event(tr, NULL, system, event, set); 809 } 810 EXPORT_SYMBOL_GPL(trace_set_clr_event); 811 812 /* 128 should be much more than enough */ 813 #define EVENT_BUF_SIZE 127 814 815 static ssize_t 816 ftrace_event_write(struct file *file, const char __user *ubuf, 817 size_t cnt, loff_t *ppos) 818 { 819 struct trace_parser parser; 820 struct seq_file *m = file->private_data; 821 struct trace_array *tr = m->private; 822 ssize_t read, ret; 823 824 if (!cnt) 825 return 0; 826 827 ret = tracing_update_buffers(); 828 if (ret < 0) 829 return ret; 830 831 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) 832 return -ENOMEM; 833 834 read = trace_get_user(&parser, ubuf, cnt, ppos); 835 836 if (read >= 0 && trace_parser_loaded((&parser))) { 837 int set = 1; 838 839 if (*parser.buffer == '!') 840 set = 0; 841 842 parser.buffer[parser.idx] = 0; 843 844 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set); 845 if (ret) 846 goto out_put; 847 } 848 849 ret = read; 850 851 out_put: 852 trace_parser_put(&parser); 853 854 return ret; 855 } 856 857 static void * 858 t_next(struct seq_file *m, void *v, loff_t *pos) 859 { 860 struct trace_event_file *file = v; 861 struct trace_event_call *call; 862 struct trace_array *tr = m->private; 863 864 (*pos)++; 865 866 list_for_each_entry_continue(file, &tr->events, list) { 867 call = file->event_call; 868 /* 869 * The ftrace subsystem is for showing formats only. 870 * They can not be enabled or disabled via the event files. 871 */ 872 if (call->class && call->class->reg) 873 return file; 874 } 875 876 return NULL; 877 } 878 879 static void *t_start(struct seq_file *m, loff_t *pos) 880 { 881 struct trace_event_file *file; 882 struct trace_array *tr = m->private; 883 loff_t l; 884 885 mutex_lock(&event_mutex); 886 887 file = list_entry(&tr->events, struct trace_event_file, list); 888 for (l = 0; l <= *pos; ) { 889 file = t_next(m, file, &l); 890 if (!file) 891 break; 892 } 893 return file; 894 } 895 896 static void * 897 s_next(struct seq_file *m, void *v, loff_t *pos) 898 { 899 struct trace_event_file *file = v; 900 struct trace_array *tr = m->private; 901 902 (*pos)++; 903 904 list_for_each_entry_continue(file, &tr->events, list) { 905 if (file->flags & EVENT_FILE_FL_ENABLED) 906 return file; 907 } 908 909 return NULL; 910 } 911 912 static void *s_start(struct seq_file *m, loff_t *pos) 913 { 914 struct trace_event_file *file; 915 struct trace_array *tr = m->private; 916 loff_t l; 917 918 mutex_lock(&event_mutex); 919 920 file = list_entry(&tr->events, struct trace_event_file, list); 921 for (l = 0; l <= *pos; ) { 922 file = s_next(m, file, &l); 923 if (!file) 924 break; 925 } 926 return file; 927 } 928 929 static int t_show(struct seq_file *m, void *v) 930 { 931 struct trace_event_file *file = v; 932 struct trace_event_call *call = file->event_call; 933 934 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) 935 seq_printf(m, "%s:", call->class->system); 936 seq_printf(m, "%s\n", trace_event_name(call)); 937 938 return 0; 939 } 940 941 static void t_stop(struct seq_file *m, void *p) 942 { 943 mutex_unlock(&event_mutex); 944 } 945 946 static void *p_start(struct seq_file *m, loff_t *pos) 947 __acquires(RCU) 948 { 949 struct trace_pid_list *pid_list; 950 struct trace_array *tr = m->private; 951 952 /* 953 * Grab the mutex, to keep calls to p_next() having the same 954 * tr->filtered_pids as p_start() has. 955 * If we just passed the tr->filtered_pids around, then RCU would 956 * have been enough, but doing that makes things more complex. 957 */ 958 mutex_lock(&event_mutex); 959 rcu_read_lock_sched(); 960 961 pid_list = rcu_dereference_sched(tr->filtered_pids); 962 963 if (!pid_list || *pos >= pid_list->nr_pids) 964 return NULL; 965 966 return (void *)&pid_list->pids[*pos]; 967 } 968 969 static void p_stop(struct seq_file *m, void *p) 970 __releases(RCU) 971 { 972 rcu_read_unlock_sched(); 973 mutex_unlock(&event_mutex); 974 } 975 976 static void * 977 p_next(struct seq_file *m, void *v, loff_t *pos) 978 { 979 struct trace_array *tr = m->private; 980 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids); 981 982 (*pos)++; 983 984 if (*pos >= pid_list->nr_pids) 985 return NULL; 986 987 return (void *)&pid_list->pids[*pos]; 988 } 989 990 static int p_show(struct seq_file *m, void *v) 991 { 992 pid_t *pid = v; 993 994 seq_printf(m, "%d\n", *pid); 995 return 0; 996 } 997 998 static ssize_t 999 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 1000 loff_t *ppos) 1001 { 1002 struct trace_event_file *file; 1003 unsigned long flags; 1004 char buf[4] = "0"; 1005 1006 mutex_lock(&event_mutex); 1007 file = event_file_data(filp); 1008 if (likely(file)) 1009 flags = file->flags; 1010 mutex_unlock(&event_mutex); 1011 1012 if (!file) 1013 return -ENODEV; 1014 1015 if (flags & EVENT_FILE_FL_ENABLED && 1016 !(flags & EVENT_FILE_FL_SOFT_DISABLED)) 1017 strcpy(buf, "1"); 1018 1019 if (flags & EVENT_FILE_FL_SOFT_DISABLED || 1020 flags & EVENT_FILE_FL_SOFT_MODE) 1021 strcat(buf, "*"); 1022 1023 strcat(buf, "\n"); 1024 1025 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf)); 1026 } 1027 1028 static ssize_t 1029 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 1030 loff_t *ppos) 1031 { 1032 struct trace_event_file *file; 1033 unsigned long val; 1034 int ret; 1035 1036 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1037 if (ret) 1038 return ret; 1039 1040 ret = tracing_update_buffers(); 1041 if (ret < 0) 1042 return ret; 1043 1044 switch (val) { 1045 case 0: 1046 case 1: 1047 ret = -ENODEV; 1048 mutex_lock(&event_mutex); 1049 file = event_file_data(filp); 1050 if (likely(file)) 1051 ret = ftrace_event_enable_disable(file, val); 1052 mutex_unlock(&event_mutex); 1053 break; 1054 1055 default: 1056 return -EINVAL; 1057 } 1058 1059 *ppos += cnt; 1060 1061 return ret ? ret : cnt; 1062 } 1063 1064 static ssize_t 1065 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 1066 loff_t *ppos) 1067 { 1068 const char set_to_char[4] = { '?', '0', '1', 'X' }; 1069 struct trace_subsystem_dir *dir = filp->private_data; 1070 struct event_subsystem *system = dir->subsystem; 1071 struct trace_event_call *call; 1072 struct trace_event_file *file; 1073 struct trace_array *tr = dir->tr; 1074 char buf[2]; 1075 int set = 0; 1076 int ret; 1077 1078 mutex_lock(&event_mutex); 1079 list_for_each_entry(file, &tr->events, list) { 1080 call = file->event_call; 1081 if (!trace_event_name(call) || !call->class || !call->class->reg) 1082 continue; 1083 1084 if (system && strcmp(call->class->system, system->name) != 0) 1085 continue; 1086 1087 /* 1088 * We need to find out if all the events are set 1089 * or if all events or cleared, or if we have 1090 * a mixture. 1091 */ 1092 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED)); 1093 1094 /* 1095 * If we have a mixture, no need to look further. 1096 */ 1097 if (set == 3) 1098 break; 1099 } 1100 mutex_unlock(&event_mutex); 1101 1102 buf[0] = set_to_char[set]; 1103 buf[1] = '\n'; 1104 1105 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 1106 1107 return ret; 1108 } 1109 1110 static ssize_t 1111 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 1112 loff_t *ppos) 1113 { 1114 struct trace_subsystem_dir *dir = filp->private_data; 1115 struct event_subsystem *system = dir->subsystem; 1116 const char *name = NULL; 1117 unsigned long val; 1118 ssize_t ret; 1119 1120 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1121 if (ret) 1122 return ret; 1123 1124 ret = tracing_update_buffers(); 1125 if (ret < 0) 1126 return ret; 1127 1128 if (val != 0 && val != 1) 1129 return -EINVAL; 1130 1131 /* 1132 * Opening of "enable" adds a ref count to system, 1133 * so the name is safe to use. 1134 */ 1135 if (system) 1136 name = system->name; 1137 1138 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val); 1139 if (ret) 1140 goto out; 1141 1142 ret = cnt; 1143 1144 out: 1145 *ppos += cnt; 1146 1147 return ret; 1148 } 1149 1150 enum { 1151 FORMAT_HEADER = 1, 1152 FORMAT_FIELD_SEPERATOR = 2, 1153 FORMAT_PRINTFMT = 3, 1154 }; 1155 1156 static void *f_next(struct seq_file *m, void *v, loff_t *pos) 1157 { 1158 struct trace_event_call *call = event_file_data(m->private); 1159 struct list_head *common_head = &ftrace_common_fields; 1160 struct list_head *head = trace_get_fields(call); 1161 struct list_head *node = v; 1162 1163 (*pos)++; 1164 1165 switch ((unsigned long)v) { 1166 case FORMAT_HEADER: 1167 node = common_head; 1168 break; 1169 1170 case FORMAT_FIELD_SEPERATOR: 1171 node = head; 1172 break; 1173 1174 case FORMAT_PRINTFMT: 1175 /* all done */ 1176 return NULL; 1177 } 1178 1179 node = node->prev; 1180 if (node == common_head) 1181 return (void *)FORMAT_FIELD_SEPERATOR; 1182 else if (node == head) 1183 return (void *)FORMAT_PRINTFMT; 1184 else 1185 return node; 1186 } 1187 1188 static int f_show(struct seq_file *m, void *v) 1189 { 1190 struct trace_event_call *call = event_file_data(m->private); 1191 struct ftrace_event_field *field; 1192 const char *array_descriptor; 1193 1194 switch ((unsigned long)v) { 1195 case FORMAT_HEADER: 1196 seq_printf(m, "name: %s\n", trace_event_name(call)); 1197 seq_printf(m, "ID: %d\n", call->event.type); 1198 seq_puts(m, "format:\n"); 1199 return 0; 1200 1201 case FORMAT_FIELD_SEPERATOR: 1202 seq_putc(m, '\n'); 1203 return 0; 1204 1205 case FORMAT_PRINTFMT: 1206 seq_printf(m, "\nprint fmt: %s\n", 1207 call->print_fmt); 1208 return 0; 1209 } 1210 1211 field = list_entry(v, struct ftrace_event_field, link); 1212 /* 1213 * Smartly shows the array type(except dynamic array). 1214 * Normal: 1215 * field:TYPE VAR 1216 * If TYPE := TYPE[LEN], it is shown: 1217 * field:TYPE VAR[LEN] 1218 */ 1219 array_descriptor = strchr(field->type, '['); 1220 1221 if (!strncmp(field->type, "__data_loc", 10)) 1222 array_descriptor = NULL; 1223 1224 if (!array_descriptor) 1225 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", 1226 field->type, field->name, field->offset, 1227 field->size, !!field->is_signed); 1228 else 1229 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", 1230 (int)(array_descriptor - field->type), 1231 field->type, field->name, 1232 array_descriptor, field->offset, 1233 field->size, !!field->is_signed); 1234 1235 return 0; 1236 } 1237 1238 static void *f_start(struct seq_file *m, loff_t *pos) 1239 { 1240 void *p = (void *)FORMAT_HEADER; 1241 loff_t l = 0; 1242 1243 /* ->stop() is called even if ->start() fails */ 1244 mutex_lock(&event_mutex); 1245 if (!event_file_data(m->private)) 1246 return ERR_PTR(-ENODEV); 1247 1248 while (l < *pos && p) 1249 p = f_next(m, p, &l); 1250 1251 return p; 1252 } 1253 1254 static void f_stop(struct seq_file *m, void *p) 1255 { 1256 mutex_unlock(&event_mutex); 1257 } 1258 1259 static const struct seq_operations trace_format_seq_ops = { 1260 .start = f_start, 1261 .next = f_next, 1262 .stop = f_stop, 1263 .show = f_show, 1264 }; 1265 1266 static int trace_format_open(struct inode *inode, struct file *file) 1267 { 1268 struct seq_file *m; 1269 int ret; 1270 1271 ret = seq_open(file, &trace_format_seq_ops); 1272 if (ret < 0) 1273 return ret; 1274 1275 m = file->private_data; 1276 m->private = file; 1277 1278 return 0; 1279 } 1280 1281 static ssize_t 1282 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 1283 { 1284 int id = (long)event_file_data(filp); 1285 char buf[32]; 1286 int len; 1287 1288 if (*ppos) 1289 return 0; 1290 1291 if (unlikely(!id)) 1292 return -ENODEV; 1293 1294 len = sprintf(buf, "%d\n", id); 1295 1296 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 1297 } 1298 1299 static ssize_t 1300 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 1301 loff_t *ppos) 1302 { 1303 struct trace_event_file *file; 1304 struct trace_seq *s; 1305 int r = -ENODEV; 1306 1307 if (*ppos) 1308 return 0; 1309 1310 s = kmalloc(sizeof(*s), GFP_KERNEL); 1311 1312 if (!s) 1313 return -ENOMEM; 1314 1315 trace_seq_init(s); 1316 1317 mutex_lock(&event_mutex); 1318 file = event_file_data(filp); 1319 if (file) 1320 print_event_filter(file, s); 1321 mutex_unlock(&event_mutex); 1322 1323 if (file) 1324 r = simple_read_from_buffer(ubuf, cnt, ppos, 1325 s->buffer, trace_seq_used(s)); 1326 1327 kfree(s); 1328 1329 return r; 1330 } 1331 1332 static ssize_t 1333 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 1334 loff_t *ppos) 1335 { 1336 struct trace_event_file *file; 1337 char *buf; 1338 int err = -ENODEV; 1339 1340 if (cnt >= PAGE_SIZE) 1341 return -EINVAL; 1342 1343 buf = (char *)__get_free_page(GFP_TEMPORARY); 1344 if (!buf) 1345 return -ENOMEM; 1346 1347 if (copy_from_user(buf, ubuf, cnt)) { 1348 free_page((unsigned long) buf); 1349 return -EFAULT; 1350 } 1351 buf[cnt] = '\0'; 1352 1353 mutex_lock(&event_mutex); 1354 file = event_file_data(filp); 1355 if (file) 1356 err = apply_event_filter(file, buf); 1357 mutex_unlock(&event_mutex); 1358 1359 free_page((unsigned long) buf); 1360 if (err < 0) 1361 return err; 1362 1363 *ppos += cnt; 1364 1365 return cnt; 1366 } 1367 1368 static LIST_HEAD(event_subsystems); 1369 1370 static int subsystem_open(struct inode *inode, struct file *filp) 1371 { 1372 struct event_subsystem *system = NULL; 1373 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */ 1374 struct trace_array *tr; 1375 int ret; 1376 1377 if (tracing_is_disabled()) 1378 return -ENODEV; 1379 1380 /* Make sure the system still exists */ 1381 mutex_lock(&trace_types_lock); 1382 mutex_lock(&event_mutex); 1383 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 1384 list_for_each_entry(dir, &tr->systems, list) { 1385 if (dir == inode->i_private) { 1386 /* Don't open systems with no events */ 1387 if (dir->nr_events) { 1388 __get_system_dir(dir); 1389 system = dir->subsystem; 1390 } 1391 goto exit_loop; 1392 } 1393 } 1394 } 1395 exit_loop: 1396 mutex_unlock(&event_mutex); 1397 mutex_unlock(&trace_types_lock); 1398 1399 if (!system) 1400 return -ENODEV; 1401 1402 /* Some versions of gcc think dir can be uninitialized here */ 1403 WARN_ON(!dir); 1404 1405 /* Still need to increment the ref count of the system */ 1406 if (trace_array_get(tr) < 0) { 1407 put_system(dir); 1408 return -ENODEV; 1409 } 1410 1411 ret = tracing_open_generic(inode, filp); 1412 if (ret < 0) { 1413 trace_array_put(tr); 1414 put_system(dir); 1415 } 1416 1417 return ret; 1418 } 1419 1420 static int system_tr_open(struct inode *inode, struct file *filp) 1421 { 1422 struct trace_subsystem_dir *dir; 1423 struct trace_array *tr = inode->i_private; 1424 int ret; 1425 1426 if (tracing_is_disabled()) 1427 return -ENODEV; 1428 1429 if (trace_array_get(tr) < 0) 1430 return -ENODEV; 1431 1432 /* Make a temporary dir that has no system but points to tr */ 1433 dir = kzalloc(sizeof(*dir), GFP_KERNEL); 1434 if (!dir) { 1435 trace_array_put(tr); 1436 return -ENOMEM; 1437 } 1438 1439 dir->tr = tr; 1440 1441 ret = tracing_open_generic(inode, filp); 1442 if (ret < 0) { 1443 trace_array_put(tr); 1444 kfree(dir); 1445 return ret; 1446 } 1447 1448 filp->private_data = dir; 1449 1450 return 0; 1451 } 1452 1453 static int subsystem_release(struct inode *inode, struct file *file) 1454 { 1455 struct trace_subsystem_dir *dir = file->private_data; 1456 1457 trace_array_put(dir->tr); 1458 1459 /* 1460 * If dir->subsystem is NULL, then this is a temporary 1461 * descriptor that was made for a trace_array to enable 1462 * all subsystems. 1463 */ 1464 if (dir->subsystem) 1465 put_system(dir); 1466 else 1467 kfree(dir); 1468 1469 return 0; 1470 } 1471 1472 static ssize_t 1473 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 1474 loff_t *ppos) 1475 { 1476 struct trace_subsystem_dir *dir = filp->private_data; 1477 struct event_subsystem *system = dir->subsystem; 1478 struct trace_seq *s; 1479 int r; 1480 1481 if (*ppos) 1482 return 0; 1483 1484 s = kmalloc(sizeof(*s), GFP_KERNEL); 1485 if (!s) 1486 return -ENOMEM; 1487 1488 trace_seq_init(s); 1489 1490 print_subsystem_event_filter(system, s); 1491 r = simple_read_from_buffer(ubuf, cnt, ppos, 1492 s->buffer, trace_seq_used(s)); 1493 1494 kfree(s); 1495 1496 return r; 1497 } 1498 1499 static ssize_t 1500 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 1501 loff_t *ppos) 1502 { 1503 struct trace_subsystem_dir *dir = filp->private_data; 1504 char *buf; 1505 int err; 1506 1507 if (cnt >= PAGE_SIZE) 1508 return -EINVAL; 1509 1510 buf = (char *)__get_free_page(GFP_TEMPORARY); 1511 if (!buf) 1512 return -ENOMEM; 1513 1514 if (copy_from_user(buf, ubuf, cnt)) { 1515 free_page((unsigned long) buf); 1516 return -EFAULT; 1517 } 1518 buf[cnt] = '\0'; 1519 1520 err = apply_subsystem_event_filter(dir, buf); 1521 free_page((unsigned long) buf); 1522 if (err < 0) 1523 return err; 1524 1525 *ppos += cnt; 1526 1527 return cnt; 1528 } 1529 1530 static ssize_t 1531 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 1532 { 1533 int (*func)(struct trace_seq *s) = filp->private_data; 1534 struct trace_seq *s; 1535 int r; 1536 1537 if (*ppos) 1538 return 0; 1539 1540 s = kmalloc(sizeof(*s), GFP_KERNEL); 1541 if (!s) 1542 return -ENOMEM; 1543 1544 trace_seq_init(s); 1545 1546 func(s); 1547 r = simple_read_from_buffer(ubuf, cnt, ppos, 1548 s->buffer, trace_seq_used(s)); 1549 1550 kfree(s); 1551 1552 return r; 1553 } 1554 1555 static int max_pids(struct trace_pid_list *pid_list) 1556 { 1557 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t); 1558 } 1559 1560 static void ignore_task_cpu(void *data) 1561 { 1562 struct trace_array *tr = data; 1563 struct trace_pid_list *pid_list; 1564 1565 /* 1566 * This function is called by on_each_cpu() while the 1567 * event_mutex is held. 1568 */ 1569 pid_list = rcu_dereference_protected(tr->filtered_pids, 1570 mutex_is_locked(&event_mutex)); 1571 1572 this_cpu_write(tr->trace_buffer.data->ignore_pid, 1573 check_ignore_pid(pid_list, current)); 1574 } 1575 1576 static ssize_t 1577 ftrace_event_pid_write(struct file *filp, const char __user *ubuf, 1578 size_t cnt, loff_t *ppos) 1579 { 1580 struct seq_file *m = filp->private_data; 1581 struct trace_array *tr = m->private; 1582 struct trace_pid_list *filtered_pids = NULL; 1583 struct trace_pid_list *pid_list = NULL; 1584 struct trace_event_file *file; 1585 struct trace_parser parser; 1586 unsigned long val; 1587 loff_t this_pos; 1588 ssize_t read = 0; 1589 ssize_t ret = 0; 1590 pid_t pid; 1591 int i; 1592 1593 if (!cnt) 1594 return 0; 1595 1596 ret = tracing_update_buffers(); 1597 if (ret < 0) 1598 return ret; 1599 1600 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) 1601 return -ENOMEM; 1602 1603 mutex_lock(&event_mutex); 1604 /* 1605 * Load as many pids into the array before doing a 1606 * swap from the tr->filtered_pids to the new list. 1607 */ 1608 while (cnt > 0) { 1609 1610 this_pos = 0; 1611 1612 ret = trace_get_user(&parser, ubuf, cnt, &this_pos); 1613 if (ret < 0 || !trace_parser_loaded(&parser)) 1614 break; 1615 1616 read += ret; 1617 ubuf += ret; 1618 cnt -= ret; 1619 1620 parser.buffer[parser.idx] = 0; 1621 1622 ret = -EINVAL; 1623 if (kstrtoul(parser.buffer, 0, &val)) 1624 break; 1625 if (val > INT_MAX) 1626 break; 1627 1628 pid = (pid_t)val; 1629 1630 ret = -ENOMEM; 1631 if (!pid_list) { 1632 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); 1633 if (!pid_list) 1634 break; 1635 1636 filtered_pids = rcu_dereference_protected(tr->filtered_pids, 1637 lockdep_is_held(&event_mutex)); 1638 if (filtered_pids) 1639 pid_list->order = filtered_pids->order; 1640 else 1641 pid_list->order = 0; 1642 1643 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL, 1644 pid_list->order); 1645 if (!pid_list->pids) 1646 break; 1647 1648 if (filtered_pids) { 1649 pid_list->nr_pids = filtered_pids->nr_pids; 1650 memcpy(pid_list->pids, filtered_pids->pids, 1651 pid_list->nr_pids * sizeof(pid_t)); 1652 } else 1653 pid_list->nr_pids = 0; 1654 } 1655 1656 if (pid_list->nr_pids >= max_pids(pid_list)) { 1657 pid_t *pid_page; 1658 1659 pid_page = (void *)__get_free_pages(GFP_KERNEL, 1660 pid_list->order + 1); 1661 if (!pid_page) 1662 break; 1663 memcpy(pid_page, pid_list->pids, 1664 pid_list->nr_pids * sizeof(pid_t)); 1665 free_pages((unsigned long)pid_list->pids, pid_list->order); 1666 1667 pid_list->order++; 1668 pid_list->pids = pid_page; 1669 } 1670 1671 pid_list->pids[pid_list->nr_pids++] = pid; 1672 trace_parser_clear(&parser); 1673 ret = 0; 1674 } 1675 trace_parser_put(&parser); 1676 1677 if (ret < 0) { 1678 if (pid_list) 1679 free_pages((unsigned long)pid_list->pids, pid_list->order); 1680 kfree(pid_list); 1681 mutex_unlock(&event_mutex); 1682 return ret; 1683 } 1684 1685 if (!pid_list) { 1686 mutex_unlock(&event_mutex); 1687 return ret; 1688 } 1689 1690 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL); 1691 1692 /* Remove duplicates */ 1693 for (i = 1; i < pid_list->nr_pids; i++) { 1694 int start = i; 1695 1696 while (i < pid_list->nr_pids && 1697 pid_list->pids[i - 1] == pid_list->pids[i]) 1698 i++; 1699 1700 if (start != i) { 1701 if (i < pid_list->nr_pids) { 1702 memmove(&pid_list->pids[start], &pid_list->pids[i], 1703 (pid_list->nr_pids - i) * sizeof(pid_t)); 1704 pid_list->nr_pids -= i - start; 1705 i = start; 1706 } else 1707 pid_list->nr_pids = start; 1708 } 1709 } 1710 1711 rcu_assign_pointer(tr->filtered_pids, pid_list); 1712 1713 list_for_each_entry(file, &tr->events, list) { 1714 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 1715 } 1716 1717 if (filtered_pids) { 1718 synchronize_sched(); 1719 1720 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order); 1721 kfree(filtered_pids); 1722 } else { 1723 /* 1724 * Register a probe that is called before all other probes 1725 * to set ignore_pid if next or prev do not match. 1726 * Register a probe this is called after all other probes 1727 * to only keep ignore_pid set if next pid matches. 1728 */ 1729 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre, 1730 tr, INT_MAX); 1731 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post, 1732 tr, 0); 1733 1734 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, 1735 tr, INT_MAX); 1736 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, 1737 tr, 0); 1738 1739 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, 1740 tr, INT_MAX); 1741 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, 1742 tr, 0); 1743 1744 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre, 1745 tr, INT_MAX); 1746 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post, 1747 tr, 0); 1748 } 1749 1750 /* 1751 * Ignoring of pids is done at task switch. But we have to 1752 * check for those tasks that are currently running. 1753 * Always do this in case a pid was appended or removed. 1754 */ 1755 on_each_cpu(ignore_task_cpu, tr, 1); 1756 1757 mutex_unlock(&event_mutex); 1758 1759 ret = read; 1760 *ppos += read; 1761 1762 return ret; 1763 } 1764 1765 static int ftrace_event_avail_open(struct inode *inode, struct file *file); 1766 static int ftrace_event_set_open(struct inode *inode, struct file *file); 1767 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file); 1768 static int ftrace_event_release(struct inode *inode, struct file *file); 1769 1770 static const struct seq_operations show_event_seq_ops = { 1771 .start = t_start, 1772 .next = t_next, 1773 .show = t_show, 1774 .stop = t_stop, 1775 }; 1776 1777 static const struct seq_operations show_set_event_seq_ops = { 1778 .start = s_start, 1779 .next = s_next, 1780 .show = t_show, 1781 .stop = t_stop, 1782 }; 1783 1784 static const struct seq_operations show_set_pid_seq_ops = { 1785 .start = p_start, 1786 .next = p_next, 1787 .show = p_show, 1788 .stop = p_stop, 1789 }; 1790 1791 static const struct file_operations ftrace_avail_fops = { 1792 .open = ftrace_event_avail_open, 1793 .read = seq_read, 1794 .llseek = seq_lseek, 1795 .release = seq_release, 1796 }; 1797 1798 static const struct file_operations ftrace_set_event_fops = { 1799 .open = ftrace_event_set_open, 1800 .read = seq_read, 1801 .write = ftrace_event_write, 1802 .llseek = seq_lseek, 1803 .release = ftrace_event_release, 1804 }; 1805 1806 static const struct file_operations ftrace_set_event_pid_fops = { 1807 .open = ftrace_event_set_pid_open, 1808 .read = seq_read, 1809 .write = ftrace_event_pid_write, 1810 .llseek = seq_lseek, 1811 .release = ftrace_event_release, 1812 }; 1813 1814 static const struct file_operations ftrace_enable_fops = { 1815 .open = tracing_open_generic, 1816 .read = event_enable_read, 1817 .write = event_enable_write, 1818 .llseek = default_llseek, 1819 }; 1820 1821 static const struct file_operations ftrace_event_format_fops = { 1822 .open = trace_format_open, 1823 .read = seq_read, 1824 .llseek = seq_lseek, 1825 .release = seq_release, 1826 }; 1827 1828 static const struct file_operations ftrace_event_id_fops = { 1829 .read = event_id_read, 1830 .llseek = default_llseek, 1831 }; 1832 1833 static const struct file_operations ftrace_event_filter_fops = { 1834 .open = tracing_open_generic, 1835 .read = event_filter_read, 1836 .write = event_filter_write, 1837 .llseek = default_llseek, 1838 }; 1839 1840 static const struct file_operations ftrace_subsystem_filter_fops = { 1841 .open = subsystem_open, 1842 .read = subsystem_filter_read, 1843 .write = subsystem_filter_write, 1844 .llseek = default_llseek, 1845 .release = subsystem_release, 1846 }; 1847 1848 static const struct file_operations ftrace_system_enable_fops = { 1849 .open = subsystem_open, 1850 .read = system_enable_read, 1851 .write = system_enable_write, 1852 .llseek = default_llseek, 1853 .release = subsystem_release, 1854 }; 1855 1856 static const struct file_operations ftrace_tr_enable_fops = { 1857 .open = system_tr_open, 1858 .read = system_enable_read, 1859 .write = system_enable_write, 1860 .llseek = default_llseek, 1861 .release = subsystem_release, 1862 }; 1863 1864 static const struct file_operations ftrace_show_header_fops = { 1865 .open = tracing_open_generic, 1866 .read = show_header, 1867 .llseek = default_llseek, 1868 }; 1869 1870 static int 1871 ftrace_event_open(struct inode *inode, struct file *file, 1872 const struct seq_operations *seq_ops) 1873 { 1874 struct seq_file *m; 1875 int ret; 1876 1877 ret = seq_open(file, seq_ops); 1878 if (ret < 0) 1879 return ret; 1880 m = file->private_data; 1881 /* copy tr over to seq ops */ 1882 m->private = inode->i_private; 1883 1884 return ret; 1885 } 1886 1887 static int ftrace_event_release(struct inode *inode, struct file *file) 1888 { 1889 struct trace_array *tr = inode->i_private; 1890 1891 trace_array_put(tr); 1892 1893 return seq_release(inode, file); 1894 } 1895 1896 static int 1897 ftrace_event_avail_open(struct inode *inode, struct file *file) 1898 { 1899 const struct seq_operations *seq_ops = &show_event_seq_ops; 1900 1901 return ftrace_event_open(inode, file, seq_ops); 1902 } 1903 1904 static int 1905 ftrace_event_set_open(struct inode *inode, struct file *file) 1906 { 1907 const struct seq_operations *seq_ops = &show_set_event_seq_ops; 1908 struct trace_array *tr = inode->i_private; 1909 int ret; 1910 1911 if (trace_array_get(tr) < 0) 1912 return -ENODEV; 1913 1914 if ((file->f_mode & FMODE_WRITE) && 1915 (file->f_flags & O_TRUNC)) 1916 ftrace_clear_events(tr); 1917 1918 ret = ftrace_event_open(inode, file, seq_ops); 1919 if (ret < 0) 1920 trace_array_put(tr); 1921 return ret; 1922 } 1923 1924 static int 1925 ftrace_event_set_pid_open(struct inode *inode, struct file *file) 1926 { 1927 const struct seq_operations *seq_ops = &show_set_pid_seq_ops; 1928 struct trace_array *tr = inode->i_private; 1929 int ret; 1930 1931 if (trace_array_get(tr) < 0) 1932 return -ENODEV; 1933 1934 if ((file->f_mode & FMODE_WRITE) && 1935 (file->f_flags & O_TRUNC)) 1936 ftrace_clear_event_pids(tr); 1937 1938 ret = ftrace_event_open(inode, file, seq_ops); 1939 if (ret < 0) 1940 trace_array_put(tr); 1941 return ret; 1942 } 1943 1944 static struct event_subsystem * 1945 create_new_subsystem(const char *name) 1946 { 1947 struct event_subsystem *system; 1948 1949 /* need to create new entry */ 1950 system = kmalloc(sizeof(*system), GFP_KERNEL); 1951 if (!system) 1952 return NULL; 1953 1954 system->ref_count = 1; 1955 1956 /* Only allocate if dynamic (kprobes and modules) */ 1957 system->name = kstrdup_const(name, GFP_KERNEL); 1958 if (!system->name) 1959 goto out_free; 1960 1961 system->filter = NULL; 1962 1963 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); 1964 if (!system->filter) 1965 goto out_free; 1966 1967 list_add(&system->list, &event_subsystems); 1968 1969 return system; 1970 1971 out_free: 1972 kfree_const(system->name); 1973 kfree(system); 1974 return NULL; 1975 } 1976 1977 static struct dentry * 1978 event_subsystem_dir(struct trace_array *tr, const char *name, 1979 struct trace_event_file *file, struct dentry *parent) 1980 { 1981 struct trace_subsystem_dir *dir; 1982 struct event_subsystem *system; 1983 struct dentry *entry; 1984 1985 /* First see if we did not already create this dir */ 1986 list_for_each_entry(dir, &tr->systems, list) { 1987 system = dir->subsystem; 1988 if (strcmp(system->name, name) == 0) { 1989 dir->nr_events++; 1990 file->system = dir; 1991 return dir->entry; 1992 } 1993 } 1994 1995 /* Now see if the system itself exists. */ 1996 list_for_each_entry(system, &event_subsystems, list) { 1997 if (strcmp(system->name, name) == 0) 1998 break; 1999 } 2000 /* Reset system variable when not found */ 2001 if (&system->list == &event_subsystems) 2002 system = NULL; 2003 2004 dir = kmalloc(sizeof(*dir), GFP_KERNEL); 2005 if (!dir) 2006 goto out_fail; 2007 2008 if (!system) { 2009 system = create_new_subsystem(name); 2010 if (!system) 2011 goto out_free; 2012 } else 2013 __get_system(system); 2014 2015 dir->entry = tracefs_create_dir(name, parent); 2016 if (!dir->entry) { 2017 pr_warn("Failed to create system directory %s\n", name); 2018 __put_system(system); 2019 goto out_free; 2020 } 2021 2022 dir->tr = tr; 2023 dir->ref_count = 1; 2024 dir->nr_events = 1; 2025 dir->subsystem = system; 2026 file->system = dir; 2027 2028 entry = tracefs_create_file("filter", 0644, dir->entry, dir, 2029 &ftrace_subsystem_filter_fops); 2030 if (!entry) { 2031 kfree(system->filter); 2032 system->filter = NULL; 2033 pr_warn("Could not create tracefs '%s/filter' entry\n", name); 2034 } 2035 2036 trace_create_file("enable", 0644, dir->entry, dir, 2037 &ftrace_system_enable_fops); 2038 2039 list_add(&dir->list, &tr->systems); 2040 2041 return dir->entry; 2042 2043 out_free: 2044 kfree(dir); 2045 out_fail: 2046 /* Only print this message if failed on memory allocation */ 2047 if (!dir || !system) 2048 pr_warn("No memory to create event subsystem %s\n", name); 2049 return NULL; 2050 } 2051 2052 static int 2053 event_create_dir(struct dentry *parent, struct trace_event_file *file) 2054 { 2055 struct trace_event_call *call = file->event_call; 2056 struct trace_array *tr = file->tr; 2057 struct list_head *head; 2058 struct dentry *d_events; 2059 const char *name; 2060 int ret; 2061 2062 /* 2063 * If the trace point header did not define TRACE_SYSTEM 2064 * then the system would be called "TRACE_SYSTEM". 2065 */ 2066 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) { 2067 d_events = event_subsystem_dir(tr, call->class->system, file, parent); 2068 if (!d_events) 2069 return -ENOMEM; 2070 } else 2071 d_events = parent; 2072 2073 name = trace_event_name(call); 2074 file->dir = tracefs_create_dir(name, d_events); 2075 if (!file->dir) { 2076 pr_warn("Could not create tracefs '%s' directory\n", name); 2077 return -1; 2078 } 2079 2080 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) 2081 trace_create_file("enable", 0644, file->dir, file, 2082 &ftrace_enable_fops); 2083 2084 #ifdef CONFIG_PERF_EVENTS 2085 if (call->event.type && call->class->reg) 2086 trace_create_file("id", 0444, file->dir, 2087 (void *)(long)call->event.type, 2088 &ftrace_event_id_fops); 2089 #endif 2090 2091 /* 2092 * Other events may have the same class. Only update 2093 * the fields if they are not already defined. 2094 */ 2095 head = trace_get_fields(call); 2096 if (list_empty(head)) { 2097 ret = call->class->define_fields(call); 2098 if (ret < 0) { 2099 pr_warn("Could not initialize trace point events/%s\n", 2100 name); 2101 return -1; 2102 } 2103 } 2104 trace_create_file("filter", 0644, file->dir, file, 2105 &ftrace_event_filter_fops); 2106 2107 trace_create_file("trigger", 0644, file->dir, file, 2108 &event_trigger_fops); 2109 2110 trace_create_file("format", 0444, file->dir, call, 2111 &ftrace_event_format_fops); 2112 2113 return 0; 2114 } 2115 2116 static void remove_event_from_tracers(struct trace_event_call *call) 2117 { 2118 struct trace_event_file *file; 2119 struct trace_array *tr; 2120 2121 do_for_each_event_file_safe(tr, file) { 2122 if (file->event_call != call) 2123 continue; 2124 2125 remove_event_file_dir(file); 2126 /* 2127 * The do_for_each_event_file_safe() is 2128 * a double loop. After finding the call for this 2129 * trace_array, we use break to jump to the next 2130 * trace_array. 2131 */ 2132 break; 2133 } while_for_each_event_file(); 2134 } 2135 2136 static void event_remove(struct trace_event_call *call) 2137 { 2138 struct trace_array *tr; 2139 struct trace_event_file *file; 2140 2141 do_for_each_event_file(tr, file) { 2142 if (file->event_call != call) 2143 continue; 2144 ftrace_event_enable_disable(file, 0); 2145 /* 2146 * The do_for_each_event_file() is 2147 * a double loop. After finding the call for this 2148 * trace_array, we use break to jump to the next 2149 * trace_array. 2150 */ 2151 break; 2152 } while_for_each_event_file(); 2153 2154 if (call->event.funcs) 2155 __unregister_trace_event(&call->event); 2156 remove_event_from_tracers(call); 2157 list_del(&call->list); 2158 } 2159 2160 static int event_init(struct trace_event_call *call) 2161 { 2162 int ret = 0; 2163 const char *name; 2164 2165 name = trace_event_name(call); 2166 if (WARN_ON(!name)) 2167 return -EINVAL; 2168 2169 if (call->class->raw_init) { 2170 ret = call->class->raw_init(call); 2171 if (ret < 0 && ret != -ENOSYS) 2172 pr_warn("Could not initialize trace events/%s\n", name); 2173 } 2174 2175 return ret; 2176 } 2177 2178 static int 2179 __register_event(struct trace_event_call *call, struct module *mod) 2180 { 2181 int ret; 2182 2183 ret = event_init(call); 2184 if (ret < 0) 2185 return ret; 2186 2187 list_add(&call->list, &ftrace_events); 2188 call->mod = mod; 2189 2190 return 0; 2191 } 2192 2193 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len) 2194 { 2195 int rlen; 2196 int elen; 2197 2198 /* Find the length of the enum value as a string */ 2199 elen = snprintf(ptr, 0, "%ld", map->enum_value); 2200 /* Make sure there's enough room to replace the string with the value */ 2201 if (len < elen) 2202 return NULL; 2203 2204 snprintf(ptr, elen + 1, "%ld", map->enum_value); 2205 2206 /* Get the rest of the string of ptr */ 2207 rlen = strlen(ptr + len); 2208 memmove(ptr + elen, ptr + len, rlen); 2209 /* Make sure we end the new string */ 2210 ptr[elen + rlen] = 0; 2211 2212 return ptr + elen; 2213 } 2214 2215 static void update_event_printk(struct trace_event_call *call, 2216 struct trace_enum_map *map) 2217 { 2218 char *ptr; 2219 int quote = 0; 2220 int len = strlen(map->enum_string); 2221 2222 for (ptr = call->print_fmt; *ptr; ptr++) { 2223 if (*ptr == '\\') { 2224 ptr++; 2225 /* paranoid */ 2226 if (!*ptr) 2227 break; 2228 continue; 2229 } 2230 if (*ptr == '"') { 2231 quote ^= 1; 2232 continue; 2233 } 2234 if (quote) 2235 continue; 2236 if (isdigit(*ptr)) { 2237 /* skip numbers */ 2238 do { 2239 ptr++; 2240 /* Check for alpha chars like ULL */ 2241 } while (isalnum(*ptr)); 2242 if (!*ptr) 2243 break; 2244 /* 2245 * A number must have some kind of delimiter after 2246 * it, and we can ignore that too. 2247 */ 2248 continue; 2249 } 2250 if (isalpha(*ptr) || *ptr == '_') { 2251 if (strncmp(map->enum_string, ptr, len) == 0 && 2252 !isalnum(ptr[len]) && ptr[len] != '_') { 2253 ptr = enum_replace(ptr, map, len); 2254 /* Hmm, enum string smaller than value */ 2255 if (WARN_ON_ONCE(!ptr)) 2256 return; 2257 /* 2258 * No need to decrement here, as enum_replace() 2259 * returns the pointer to the character passed 2260 * the enum, and two enums can not be placed 2261 * back to back without something in between. 2262 * We can skip that something in between. 2263 */ 2264 continue; 2265 } 2266 skip_more: 2267 do { 2268 ptr++; 2269 } while (isalnum(*ptr) || *ptr == '_'); 2270 if (!*ptr) 2271 break; 2272 /* 2273 * If what comes after this variable is a '.' or 2274 * '->' then we can continue to ignore that string. 2275 */ 2276 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) { 2277 ptr += *ptr == '.' ? 1 : 2; 2278 if (!*ptr) 2279 break; 2280 goto skip_more; 2281 } 2282 /* 2283 * Once again, we can skip the delimiter that came 2284 * after the string. 2285 */ 2286 continue; 2287 } 2288 } 2289 } 2290 2291 void trace_event_enum_update(struct trace_enum_map **map, int len) 2292 { 2293 struct trace_event_call *call, *p; 2294 const char *last_system = NULL; 2295 int last_i; 2296 int i; 2297 2298 down_write(&trace_event_sem); 2299 list_for_each_entry_safe(call, p, &ftrace_events, list) { 2300 /* events are usually grouped together with systems */ 2301 if (!last_system || call->class->system != last_system) { 2302 last_i = 0; 2303 last_system = call->class->system; 2304 } 2305 2306 for (i = last_i; i < len; i++) { 2307 if (call->class->system == map[i]->system) { 2308 /* Save the first system if need be */ 2309 if (!last_i) 2310 last_i = i; 2311 update_event_printk(call, map[i]); 2312 } 2313 } 2314 } 2315 up_write(&trace_event_sem); 2316 } 2317 2318 static struct trace_event_file * 2319 trace_create_new_event(struct trace_event_call *call, 2320 struct trace_array *tr) 2321 { 2322 struct trace_event_file *file; 2323 2324 file = kmem_cache_alloc(file_cachep, GFP_TRACE); 2325 if (!file) 2326 return NULL; 2327 2328 file->event_call = call; 2329 file->tr = tr; 2330 atomic_set(&file->sm_ref, 0); 2331 atomic_set(&file->tm_ref, 0); 2332 INIT_LIST_HEAD(&file->triggers); 2333 list_add(&file->list, &tr->events); 2334 2335 return file; 2336 } 2337 2338 /* Add an event to a trace directory */ 2339 static int 2340 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr) 2341 { 2342 struct trace_event_file *file; 2343 2344 file = trace_create_new_event(call, tr); 2345 if (!file) 2346 return -ENOMEM; 2347 2348 return event_create_dir(tr->event_dir, file); 2349 } 2350 2351 /* 2352 * Just create a decriptor for early init. A descriptor is required 2353 * for enabling events at boot. We want to enable events before 2354 * the filesystem is initialized. 2355 */ 2356 static __init int 2357 __trace_early_add_new_event(struct trace_event_call *call, 2358 struct trace_array *tr) 2359 { 2360 struct trace_event_file *file; 2361 2362 file = trace_create_new_event(call, tr); 2363 if (!file) 2364 return -ENOMEM; 2365 2366 return 0; 2367 } 2368 2369 struct ftrace_module_file_ops; 2370 static void __add_event_to_tracers(struct trace_event_call *call); 2371 2372 /* Add an additional event_call dynamically */ 2373 int trace_add_event_call(struct trace_event_call *call) 2374 { 2375 int ret; 2376 mutex_lock(&trace_types_lock); 2377 mutex_lock(&event_mutex); 2378 2379 ret = __register_event(call, NULL); 2380 if (ret >= 0) 2381 __add_event_to_tracers(call); 2382 2383 mutex_unlock(&event_mutex); 2384 mutex_unlock(&trace_types_lock); 2385 return ret; 2386 } 2387 2388 /* 2389 * Must be called under locking of trace_types_lock, event_mutex and 2390 * trace_event_sem. 2391 */ 2392 static void __trace_remove_event_call(struct trace_event_call *call) 2393 { 2394 event_remove(call); 2395 trace_destroy_fields(call); 2396 free_event_filter(call->filter); 2397 call->filter = NULL; 2398 } 2399 2400 static int probe_remove_event_call(struct trace_event_call *call) 2401 { 2402 struct trace_array *tr; 2403 struct trace_event_file *file; 2404 2405 #ifdef CONFIG_PERF_EVENTS 2406 if (call->perf_refcount) 2407 return -EBUSY; 2408 #endif 2409 do_for_each_event_file(tr, file) { 2410 if (file->event_call != call) 2411 continue; 2412 /* 2413 * We can't rely on ftrace_event_enable_disable(enable => 0) 2414 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress 2415 * TRACE_REG_UNREGISTER. 2416 */ 2417 if (file->flags & EVENT_FILE_FL_ENABLED) 2418 return -EBUSY; 2419 /* 2420 * The do_for_each_event_file_safe() is 2421 * a double loop. After finding the call for this 2422 * trace_array, we use break to jump to the next 2423 * trace_array. 2424 */ 2425 break; 2426 } while_for_each_event_file(); 2427 2428 __trace_remove_event_call(call); 2429 2430 return 0; 2431 } 2432 2433 /* Remove an event_call */ 2434 int trace_remove_event_call(struct trace_event_call *call) 2435 { 2436 int ret; 2437 2438 mutex_lock(&trace_types_lock); 2439 mutex_lock(&event_mutex); 2440 down_write(&trace_event_sem); 2441 ret = probe_remove_event_call(call); 2442 up_write(&trace_event_sem); 2443 mutex_unlock(&event_mutex); 2444 mutex_unlock(&trace_types_lock); 2445 2446 return ret; 2447 } 2448 2449 #define for_each_event(event, start, end) \ 2450 for (event = start; \ 2451 (unsigned long)event < (unsigned long)end; \ 2452 event++) 2453 2454 #ifdef CONFIG_MODULES 2455 2456 static void trace_module_add_events(struct module *mod) 2457 { 2458 struct trace_event_call **call, **start, **end; 2459 2460 if (!mod->num_trace_events) 2461 return; 2462 2463 /* Don't add infrastructure for mods without tracepoints */ 2464 if (trace_module_has_bad_taint(mod)) { 2465 pr_err("%s: module has bad taint, not creating trace events\n", 2466 mod->name); 2467 return; 2468 } 2469 2470 start = mod->trace_events; 2471 end = mod->trace_events + mod->num_trace_events; 2472 2473 for_each_event(call, start, end) { 2474 __register_event(*call, mod); 2475 __add_event_to_tracers(*call); 2476 } 2477 } 2478 2479 static void trace_module_remove_events(struct module *mod) 2480 { 2481 struct trace_event_call *call, *p; 2482 bool clear_trace = false; 2483 2484 down_write(&trace_event_sem); 2485 list_for_each_entry_safe(call, p, &ftrace_events, list) { 2486 if (call->mod == mod) { 2487 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED) 2488 clear_trace = true; 2489 __trace_remove_event_call(call); 2490 } 2491 } 2492 up_write(&trace_event_sem); 2493 2494 /* 2495 * It is safest to reset the ring buffer if the module being unloaded 2496 * registered any events that were used. The only worry is if 2497 * a new module gets loaded, and takes on the same id as the events 2498 * of this module. When printing out the buffer, traced events left 2499 * over from this module may be passed to the new module events and 2500 * unexpected results may occur. 2501 */ 2502 if (clear_trace) 2503 tracing_reset_all_online_cpus(); 2504 } 2505 2506 static int trace_module_notify(struct notifier_block *self, 2507 unsigned long val, void *data) 2508 { 2509 struct module *mod = data; 2510 2511 mutex_lock(&trace_types_lock); 2512 mutex_lock(&event_mutex); 2513 switch (val) { 2514 case MODULE_STATE_COMING: 2515 trace_module_add_events(mod); 2516 break; 2517 case MODULE_STATE_GOING: 2518 trace_module_remove_events(mod); 2519 break; 2520 } 2521 mutex_unlock(&event_mutex); 2522 mutex_unlock(&trace_types_lock); 2523 2524 return 0; 2525 } 2526 2527 static struct notifier_block trace_module_nb = { 2528 .notifier_call = trace_module_notify, 2529 .priority = 1, /* higher than trace.c module notify */ 2530 }; 2531 #endif /* CONFIG_MODULES */ 2532 2533 /* Create a new event directory structure for a trace directory. */ 2534 static void 2535 __trace_add_event_dirs(struct trace_array *tr) 2536 { 2537 struct trace_event_call *call; 2538 int ret; 2539 2540 list_for_each_entry(call, &ftrace_events, list) { 2541 ret = __trace_add_new_event(call, tr); 2542 if (ret < 0) 2543 pr_warn("Could not create directory for event %s\n", 2544 trace_event_name(call)); 2545 } 2546 } 2547 2548 struct trace_event_file * 2549 find_event_file(struct trace_array *tr, const char *system, const char *event) 2550 { 2551 struct trace_event_file *file; 2552 struct trace_event_call *call; 2553 const char *name; 2554 2555 list_for_each_entry(file, &tr->events, list) { 2556 2557 call = file->event_call; 2558 name = trace_event_name(call); 2559 2560 if (!name || !call->class || !call->class->reg) 2561 continue; 2562 2563 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 2564 continue; 2565 2566 if (strcmp(event, name) == 0 && 2567 strcmp(system, call->class->system) == 0) 2568 return file; 2569 } 2570 return NULL; 2571 } 2572 2573 #ifdef CONFIG_DYNAMIC_FTRACE 2574 2575 /* Avoid typos */ 2576 #define ENABLE_EVENT_STR "enable_event" 2577 #define DISABLE_EVENT_STR "disable_event" 2578 2579 struct event_probe_data { 2580 struct trace_event_file *file; 2581 unsigned long count; 2582 int ref; 2583 bool enable; 2584 }; 2585 2586 static void 2587 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data) 2588 { 2589 struct event_probe_data **pdata = (struct event_probe_data **)_data; 2590 struct event_probe_data *data = *pdata; 2591 2592 if (!data) 2593 return; 2594 2595 if (data->enable) 2596 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 2597 else 2598 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 2599 } 2600 2601 static void 2602 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data) 2603 { 2604 struct event_probe_data **pdata = (struct event_probe_data **)_data; 2605 struct event_probe_data *data = *pdata; 2606 2607 if (!data) 2608 return; 2609 2610 if (!data->count) 2611 return; 2612 2613 /* Skip if the event is in a state we want to switch to */ 2614 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 2615 return; 2616 2617 if (data->count != -1) 2618 (data->count)--; 2619 2620 event_enable_probe(ip, parent_ip, _data); 2621 } 2622 2623 static int 2624 event_enable_print(struct seq_file *m, unsigned long ip, 2625 struct ftrace_probe_ops *ops, void *_data) 2626 { 2627 struct event_probe_data *data = _data; 2628 2629 seq_printf(m, "%ps:", (void *)ip); 2630 2631 seq_printf(m, "%s:%s:%s", 2632 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, 2633 data->file->event_call->class->system, 2634 trace_event_name(data->file->event_call)); 2635 2636 if (data->count == -1) 2637 seq_puts(m, ":unlimited\n"); 2638 else 2639 seq_printf(m, ":count=%ld\n", data->count); 2640 2641 return 0; 2642 } 2643 2644 static int 2645 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip, 2646 void **_data) 2647 { 2648 struct event_probe_data **pdata = (struct event_probe_data **)_data; 2649 struct event_probe_data *data = *pdata; 2650 2651 data->ref++; 2652 return 0; 2653 } 2654 2655 static void 2656 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip, 2657 void **_data) 2658 { 2659 struct event_probe_data **pdata = (struct event_probe_data **)_data; 2660 struct event_probe_data *data = *pdata; 2661 2662 if (WARN_ON_ONCE(data->ref <= 0)) 2663 return; 2664 2665 data->ref--; 2666 if (!data->ref) { 2667 /* Remove the SOFT_MODE flag */ 2668 __ftrace_event_enable_disable(data->file, 0, 1); 2669 module_put(data->file->event_call->mod); 2670 kfree(data); 2671 } 2672 *pdata = NULL; 2673 } 2674 2675 static struct ftrace_probe_ops event_enable_probe_ops = { 2676 .func = event_enable_probe, 2677 .print = event_enable_print, 2678 .init = event_enable_init, 2679 .free = event_enable_free, 2680 }; 2681 2682 static struct ftrace_probe_ops event_enable_count_probe_ops = { 2683 .func = event_enable_count_probe, 2684 .print = event_enable_print, 2685 .init = event_enable_init, 2686 .free = event_enable_free, 2687 }; 2688 2689 static struct ftrace_probe_ops event_disable_probe_ops = { 2690 .func = event_enable_probe, 2691 .print = event_enable_print, 2692 .init = event_enable_init, 2693 .free = event_enable_free, 2694 }; 2695 2696 static struct ftrace_probe_ops event_disable_count_probe_ops = { 2697 .func = event_enable_count_probe, 2698 .print = event_enable_print, 2699 .init = event_enable_init, 2700 .free = event_enable_free, 2701 }; 2702 2703 static int 2704 event_enable_func(struct ftrace_hash *hash, 2705 char *glob, char *cmd, char *param, int enabled) 2706 { 2707 struct trace_array *tr = top_trace_array(); 2708 struct trace_event_file *file; 2709 struct ftrace_probe_ops *ops; 2710 struct event_probe_data *data; 2711 const char *system; 2712 const char *event; 2713 char *number; 2714 bool enable; 2715 int ret; 2716 2717 if (!tr) 2718 return -ENODEV; 2719 2720 /* hash funcs only work with set_ftrace_filter */ 2721 if (!enabled || !param) 2722 return -EINVAL; 2723 2724 system = strsep(¶m, ":"); 2725 if (!param) 2726 return -EINVAL; 2727 2728 event = strsep(¶m, ":"); 2729 2730 mutex_lock(&event_mutex); 2731 2732 ret = -EINVAL; 2733 file = find_event_file(tr, system, event); 2734 if (!file) 2735 goto out; 2736 2737 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 2738 2739 if (enable) 2740 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops; 2741 else 2742 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops; 2743 2744 if (glob[0] == '!') { 2745 unregister_ftrace_function_probe_func(glob+1, ops); 2746 ret = 0; 2747 goto out; 2748 } 2749 2750 ret = -ENOMEM; 2751 data = kzalloc(sizeof(*data), GFP_KERNEL); 2752 if (!data) 2753 goto out; 2754 2755 data->enable = enable; 2756 data->count = -1; 2757 data->file = file; 2758 2759 if (!param) 2760 goto out_reg; 2761 2762 number = strsep(¶m, ":"); 2763 2764 ret = -EINVAL; 2765 if (!strlen(number)) 2766 goto out_free; 2767 2768 /* 2769 * We use the callback data field (which is a pointer) 2770 * as our counter. 2771 */ 2772 ret = kstrtoul(number, 0, &data->count); 2773 if (ret) 2774 goto out_free; 2775 2776 out_reg: 2777 /* Don't let event modules unload while probe registered */ 2778 ret = try_module_get(file->event_call->mod); 2779 if (!ret) { 2780 ret = -EBUSY; 2781 goto out_free; 2782 } 2783 2784 ret = __ftrace_event_enable_disable(file, 1, 1); 2785 if (ret < 0) 2786 goto out_put; 2787 ret = register_ftrace_function_probe(glob, ops, data); 2788 /* 2789 * The above returns on success the # of functions enabled, 2790 * but if it didn't find any functions it returns zero. 2791 * Consider no functions a failure too. 2792 */ 2793 if (!ret) { 2794 ret = -ENOENT; 2795 goto out_disable; 2796 } else if (ret < 0) 2797 goto out_disable; 2798 /* Just return zero, not the number of enabled functions */ 2799 ret = 0; 2800 out: 2801 mutex_unlock(&event_mutex); 2802 return ret; 2803 2804 out_disable: 2805 __ftrace_event_enable_disable(file, 0, 1); 2806 out_put: 2807 module_put(file->event_call->mod); 2808 out_free: 2809 kfree(data); 2810 goto out; 2811 } 2812 2813 static struct ftrace_func_command event_enable_cmd = { 2814 .name = ENABLE_EVENT_STR, 2815 .func = event_enable_func, 2816 }; 2817 2818 static struct ftrace_func_command event_disable_cmd = { 2819 .name = DISABLE_EVENT_STR, 2820 .func = event_enable_func, 2821 }; 2822 2823 static __init int register_event_cmds(void) 2824 { 2825 int ret; 2826 2827 ret = register_ftrace_command(&event_enable_cmd); 2828 if (WARN_ON(ret < 0)) 2829 return ret; 2830 ret = register_ftrace_command(&event_disable_cmd); 2831 if (WARN_ON(ret < 0)) 2832 unregister_ftrace_command(&event_enable_cmd); 2833 return ret; 2834 } 2835 #else 2836 static inline int register_event_cmds(void) { return 0; } 2837 #endif /* CONFIG_DYNAMIC_FTRACE */ 2838 2839 /* 2840 * The top level array has already had its trace_event_file 2841 * descriptors created in order to allow for early events to 2842 * be recorded. This function is called after the tracefs has been 2843 * initialized, and we now have to create the files associated 2844 * to the events. 2845 */ 2846 static __init void 2847 __trace_early_add_event_dirs(struct trace_array *tr) 2848 { 2849 struct trace_event_file *file; 2850 int ret; 2851 2852 2853 list_for_each_entry(file, &tr->events, list) { 2854 ret = event_create_dir(tr->event_dir, file); 2855 if (ret < 0) 2856 pr_warn("Could not create directory for event %s\n", 2857 trace_event_name(file->event_call)); 2858 } 2859 } 2860 2861 /* 2862 * For early boot up, the top trace array requires to have 2863 * a list of events that can be enabled. This must be done before 2864 * the filesystem is set up in order to allow events to be traced 2865 * early. 2866 */ 2867 static __init void 2868 __trace_early_add_events(struct trace_array *tr) 2869 { 2870 struct trace_event_call *call; 2871 int ret; 2872 2873 list_for_each_entry(call, &ftrace_events, list) { 2874 /* Early boot up should not have any modules loaded */ 2875 if (WARN_ON_ONCE(call->mod)) 2876 continue; 2877 2878 ret = __trace_early_add_new_event(call, tr); 2879 if (ret < 0) 2880 pr_warn("Could not create early event %s\n", 2881 trace_event_name(call)); 2882 } 2883 } 2884 2885 /* Remove the event directory structure for a trace directory. */ 2886 static void 2887 __trace_remove_event_dirs(struct trace_array *tr) 2888 { 2889 struct trace_event_file *file, *next; 2890 2891 list_for_each_entry_safe(file, next, &tr->events, list) 2892 remove_event_file_dir(file); 2893 } 2894 2895 static void __add_event_to_tracers(struct trace_event_call *call) 2896 { 2897 struct trace_array *tr; 2898 2899 list_for_each_entry(tr, &ftrace_trace_arrays, list) 2900 __trace_add_new_event(call, tr); 2901 } 2902 2903 extern struct trace_event_call *__start_ftrace_events[]; 2904 extern struct trace_event_call *__stop_ftrace_events[]; 2905 2906 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; 2907 2908 static __init int setup_trace_event(char *str) 2909 { 2910 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE); 2911 ring_buffer_expanded = true; 2912 tracing_selftest_disabled = true; 2913 2914 return 1; 2915 } 2916 __setup("trace_event=", setup_trace_event); 2917 2918 /* Expects to have event_mutex held when called */ 2919 static int 2920 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) 2921 { 2922 struct dentry *d_events; 2923 struct dentry *entry; 2924 2925 entry = tracefs_create_file("set_event", 0644, parent, 2926 tr, &ftrace_set_event_fops); 2927 if (!entry) { 2928 pr_warn("Could not create tracefs 'set_event' entry\n"); 2929 return -ENOMEM; 2930 } 2931 2932 d_events = tracefs_create_dir("events", parent); 2933 if (!d_events) { 2934 pr_warn("Could not create tracefs 'events' directory\n"); 2935 return -ENOMEM; 2936 } 2937 2938 entry = tracefs_create_file("set_event_pid", 0644, parent, 2939 tr, &ftrace_set_event_pid_fops); 2940 2941 /* ring buffer internal formats */ 2942 trace_create_file("header_page", 0444, d_events, 2943 ring_buffer_print_page_header, 2944 &ftrace_show_header_fops); 2945 2946 trace_create_file("header_event", 0444, d_events, 2947 ring_buffer_print_entry_header, 2948 &ftrace_show_header_fops); 2949 2950 trace_create_file("enable", 0644, d_events, 2951 tr, &ftrace_tr_enable_fops); 2952 2953 tr->event_dir = d_events; 2954 2955 return 0; 2956 } 2957 2958 /** 2959 * event_trace_add_tracer - add a instance of a trace_array to events 2960 * @parent: The parent dentry to place the files/directories for events in 2961 * @tr: The trace array associated with these events 2962 * 2963 * When a new instance is created, it needs to set up its events 2964 * directory, as well as other files associated with events. It also 2965 * creates the event hierachry in the @parent/events directory. 2966 * 2967 * Returns 0 on success. 2968 */ 2969 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr) 2970 { 2971 int ret; 2972 2973 mutex_lock(&event_mutex); 2974 2975 ret = create_event_toplevel_files(parent, tr); 2976 if (ret) 2977 goto out_unlock; 2978 2979 down_write(&trace_event_sem); 2980 __trace_add_event_dirs(tr); 2981 up_write(&trace_event_sem); 2982 2983 out_unlock: 2984 mutex_unlock(&event_mutex); 2985 2986 return ret; 2987 } 2988 2989 /* 2990 * The top trace array already had its file descriptors created. 2991 * Now the files themselves need to be created. 2992 */ 2993 static __init int 2994 early_event_add_tracer(struct dentry *parent, struct trace_array *tr) 2995 { 2996 int ret; 2997 2998 mutex_lock(&event_mutex); 2999 3000 ret = create_event_toplevel_files(parent, tr); 3001 if (ret) 3002 goto out_unlock; 3003 3004 down_write(&trace_event_sem); 3005 __trace_early_add_event_dirs(tr); 3006 up_write(&trace_event_sem); 3007 3008 out_unlock: 3009 mutex_unlock(&event_mutex); 3010 3011 return ret; 3012 } 3013 3014 int event_trace_del_tracer(struct trace_array *tr) 3015 { 3016 mutex_lock(&event_mutex); 3017 3018 /* Disable any event triggers and associated soft-disabled events */ 3019 clear_event_triggers(tr); 3020 3021 /* Clear the pid list */ 3022 __ftrace_clear_event_pids(tr); 3023 3024 /* Disable any running events */ 3025 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0); 3026 3027 /* Access to events are within rcu_read_lock_sched() */ 3028 synchronize_sched(); 3029 3030 down_write(&trace_event_sem); 3031 __trace_remove_event_dirs(tr); 3032 tracefs_remove_recursive(tr->event_dir); 3033 up_write(&trace_event_sem); 3034 3035 tr->event_dir = NULL; 3036 3037 mutex_unlock(&event_mutex); 3038 3039 return 0; 3040 } 3041 3042 static __init int event_trace_memsetup(void) 3043 { 3044 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC); 3045 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC); 3046 return 0; 3047 } 3048 3049 static __init void 3050 early_enable_events(struct trace_array *tr, bool disable_first) 3051 { 3052 char *buf = bootup_event_buf; 3053 char *token; 3054 int ret; 3055 3056 while (true) { 3057 token = strsep(&buf, ","); 3058 3059 if (!token) 3060 break; 3061 3062 if (*token) { 3063 /* Restarting syscalls requires that we stop them first */ 3064 if (disable_first) 3065 ftrace_set_clr_event(tr, token, 0); 3066 3067 ret = ftrace_set_clr_event(tr, token, 1); 3068 if (ret) 3069 pr_warn("Failed to enable trace event: %s\n", token); 3070 } 3071 3072 /* Put back the comma to allow this to be called again */ 3073 if (buf) 3074 *(buf - 1) = ','; 3075 } 3076 } 3077 3078 static __init int event_trace_enable(void) 3079 { 3080 struct trace_array *tr = top_trace_array(); 3081 struct trace_event_call **iter, *call; 3082 int ret; 3083 3084 if (!tr) 3085 return -ENODEV; 3086 3087 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) { 3088 3089 call = *iter; 3090 ret = event_init(call); 3091 if (!ret) 3092 list_add(&call->list, &ftrace_events); 3093 } 3094 3095 /* 3096 * We need the top trace array to have a working set of trace 3097 * points at early init, before the debug files and directories 3098 * are created. Create the file entries now, and attach them 3099 * to the actual file dentries later. 3100 */ 3101 __trace_early_add_events(tr); 3102 3103 early_enable_events(tr, false); 3104 3105 trace_printk_start_comm(); 3106 3107 register_event_cmds(); 3108 3109 register_trigger_cmds(); 3110 3111 return 0; 3112 } 3113 3114 /* 3115 * event_trace_enable() is called from trace_event_init() first to 3116 * initialize events and perhaps start any events that are on the 3117 * command line. Unfortunately, there are some events that will not 3118 * start this early, like the system call tracepoints that need 3119 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable() 3120 * is called before pid 1 starts, and this flag is never set, making 3121 * the syscall tracepoint never get reached, but the event is enabled 3122 * regardless (and not doing anything). 3123 */ 3124 static __init int event_trace_enable_again(void) 3125 { 3126 struct trace_array *tr; 3127 3128 tr = top_trace_array(); 3129 if (!tr) 3130 return -ENODEV; 3131 3132 early_enable_events(tr, true); 3133 3134 return 0; 3135 } 3136 3137 early_initcall(event_trace_enable_again); 3138 3139 static __init int event_trace_init(void) 3140 { 3141 struct trace_array *tr; 3142 struct dentry *d_tracer; 3143 struct dentry *entry; 3144 int ret; 3145 3146 tr = top_trace_array(); 3147 if (!tr) 3148 return -ENODEV; 3149 3150 d_tracer = tracing_init_dentry(); 3151 if (IS_ERR(d_tracer)) 3152 return 0; 3153 3154 entry = tracefs_create_file("available_events", 0444, d_tracer, 3155 tr, &ftrace_avail_fops); 3156 if (!entry) 3157 pr_warn("Could not create tracefs 'available_events' entry\n"); 3158 3159 if (trace_define_generic_fields()) 3160 pr_warn("tracing: Failed to allocated generic fields"); 3161 3162 if (trace_define_common_fields()) 3163 pr_warn("tracing: Failed to allocate common fields"); 3164 3165 ret = early_event_add_tracer(d_tracer, tr); 3166 if (ret) 3167 return ret; 3168 3169 #ifdef CONFIG_MODULES 3170 ret = register_module_notifier(&trace_module_nb); 3171 if (ret) 3172 pr_warn("Failed to register trace events module notifier\n"); 3173 #endif 3174 return 0; 3175 } 3176 3177 void __init trace_event_init(void) 3178 { 3179 event_trace_memsetup(); 3180 init_ftrace_syscalls(); 3181 event_trace_enable(); 3182 } 3183 3184 fs_initcall(event_trace_init); 3185 3186 #ifdef CONFIG_FTRACE_STARTUP_TEST 3187 3188 static DEFINE_SPINLOCK(test_spinlock); 3189 static DEFINE_SPINLOCK(test_spinlock_irq); 3190 static DEFINE_MUTEX(test_mutex); 3191 3192 static __init void test_work(struct work_struct *dummy) 3193 { 3194 spin_lock(&test_spinlock); 3195 spin_lock_irq(&test_spinlock_irq); 3196 udelay(1); 3197 spin_unlock_irq(&test_spinlock_irq); 3198 spin_unlock(&test_spinlock); 3199 3200 mutex_lock(&test_mutex); 3201 msleep(1); 3202 mutex_unlock(&test_mutex); 3203 } 3204 3205 static __init int event_test_thread(void *unused) 3206 { 3207 void *test_malloc; 3208 3209 test_malloc = kmalloc(1234, GFP_KERNEL); 3210 if (!test_malloc) 3211 pr_info("failed to kmalloc\n"); 3212 3213 schedule_on_each_cpu(test_work); 3214 3215 kfree(test_malloc); 3216 3217 set_current_state(TASK_INTERRUPTIBLE); 3218 while (!kthread_should_stop()) { 3219 schedule(); 3220 set_current_state(TASK_INTERRUPTIBLE); 3221 } 3222 __set_current_state(TASK_RUNNING); 3223 3224 return 0; 3225 } 3226 3227 /* 3228 * Do various things that may trigger events. 3229 */ 3230 static __init void event_test_stuff(void) 3231 { 3232 struct task_struct *test_thread; 3233 3234 test_thread = kthread_run(event_test_thread, NULL, "test-events"); 3235 msleep(1); 3236 kthread_stop(test_thread); 3237 } 3238 3239 /* 3240 * For every trace event defined, we will test each trace point separately, 3241 * and then by groups, and finally all trace points. 3242 */ 3243 static __init void event_trace_self_tests(void) 3244 { 3245 struct trace_subsystem_dir *dir; 3246 struct trace_event_file *file; 3247 struct trace_event_call *call; 3248 struct event_subsystem *system; 3249 struct trace_array *tr; 3250 int ret; 3251 3252 tr = top_trace_array(); 3253 if (!tr) 3254 return; 3255 3256 pr_info("Running tests on trace events:\n"); 3257 3258 list_for_each_entry(file, &tr->events, list) { 3259 3260 call = file->event_call; 3261 3262 /* Only test those that have a probe */ 3263 if (!call->class || !call->class->probe) 3264 continue; 3265 3266 /* 3267 * Testing syscall events here is pretty useless, but 3268 * we still do it if configured. But this is time consuming. 3269 * What we really need is a user thread to perform the 3270 * syscalls as we test. 3271 */ 3272 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS 3273 if (call->class->system && 3274 strcmp(call->class->system, "syscalls") == 0) 3275 continue; 3276 #endif 3277 3278 pr_info("Testing event %s: ", trace_event_name(call)); 3279 3280 /* 3281 * If an event is already enabled, someone is using 3282 * it and the self test should not be on. 3283 */ 3284 if (file->flags & EVENT_FILE_FL_ENABLED) { 3285 pr_warn("Enabled event during self test!\n"); 3286 WARN_ON_ONCE(1); 3287 continue; 3288 } 3289 3290 ftrace_event_enable_disable(file, 1); 3291 event_test_stuff(); 3292 ftrace_event_enable_disable(file, 0); 3293 3294 pr_cont("OK\n"); 3295 } 3296 3297 /* Now test at the sub system level */ 3298 3299 pr_info("Running tests on trace event systems:\n"); 3300 3301 list_for_each_entry(dir, &tr->systems, list) { 3302 3303 system = dir->subsystem; 3304 3305 /* the ftrace system is special, skip it */ 3306 if (strcmp(system->name, "ftrace") == 0) 3307 continue; 3308 3309 pr_info("Testing event system %s: ", system->name); 3310 3311 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1); 3312 if (WARN_ON_ONCE(ret)) { 3313 pr_warn("error enabling system %s\n", 3314 system->name); 3315 continue; 3316 } 3317 3318 event_test_stuff(); 3319 3320 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0); 3321 if (WARN_ON_ONCE(ret)) { 3322 pr_warn("error disabling system %s\n", 3323 system->name); 3324 continue; 3325 } 3326 3327 pr_cont("OK\n"); 3328 } 3329 3330 /* Test with all events enabled */ 3331 3332 pr_info("Running tests on all trace events:\n"); 3333 pr_info("Testing all events: "); 3334 3335 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1); 3336 if (WARN_ON_ONCE(ret)) { 3337 pr_warn("error enabling all events\n"); 3338 return; 3339 } 3340 3341 event_test_stuff(); 3342 3343 /* reset sysname */ 3344 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0); 3345 if (WARN_ON_ONCE(ret)) { 3346 pr_warn("error disabling all events\n"); 3347 return; 3348 } 3349 3350 pr_cont("OK\n"); 3351 } 3352 3353 #ifdef CONFIG_FUNCTION_TRACER 3354 3355 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); 3356 3357 static struct trace_array *event_tr; 3358 3359 static void __init 3360 function_test_events_call(unsigned long ip, unsigned long parent_ip, 3361 struct ftrace_ops *op, struct pt_regs *pt_regs) 3362 { 3363 struct ring_buffer_event *event; 3364 struct ring_buffer *buffer; 3365 struct ftrace_entry *entry; 3366 unsigned long flags; 3367 long disabled; 3368 int cpu; 3369 int pc; 3370 3371 pc = preempt_count(); 3372 preempt_disable_notrace(); 3373 cpu = raw_smp_processor_id(); 3374 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); 3375 3376 if (disabled != 1) 3377 goto out; 3378 3379 local_save_flags(flags); 3380 3381 event = trace_current_buffer_lock_reserve(&buffer, 3382 TRACE_FN, sizeof(*entry), 3383 flags, pc); 3384 if (!event) 3385 goto out; 3386 entry = ring_buffer_event_data(event); 3387 entry->ip = ip; 3388 entry->parent_ip = parent_ip; 3389 3390 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc); 3391 3392 out: 3393 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); 3394 preempt_enable_notrace(); 3395 } 3396 3397 static struct ftrace_ops trace_ops __initdata = 3398 { 3399 .func = function_test_events_call, 3400 .flags = FTRACE_OPS_FL_RECURSION_SAFE, 3401 }; 3402 3403 static __init void event_trace_self_test_with_function(void) 3404 { 3405 int ret; 3406 event_tr = top_trace_array(); 3407 if (WARN_ON(!event_tr)) 3408 return; 3409 ret = register_ftrace_function(&trace_ops); 3410 if (WARN_ON(ret < 0)) { 3411 pr_info("Failed to enable function tracer for event tests\n"); 3412 return; 3413 } 3414 pr_info("Running tests again, along with the function tracer\n"); 3415 event_trace_self_tests(); 3416 unregister_ftrace_function(&trace_ops); 3417 } 3418 #else 3419 static __init void event_trace_self_test_with_function(void) 3420 { 3421 } 3422 #endif 3423 3424 static __init int event_trace_self_tests_init(void) 3425 { 3426 if (!tracing_selftest_disabled) { 3427 event_trace_self_tests(); 3428 event_trace_self_test_with_function(); 3429 } 3430 3431 return 0; 3432 } 3433 3434 late_initcall(event_trace_self_tests_init); 3435 3436 #endif 3437