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