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