1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace_events_trigger - trace event triggers 4 * 5 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com> 6 */ 7 8 #include <linux/security.h> 9 #include <linux/kthread.h> 10 #include <linux/module.h> 11 #include <linux/ctype.h> 12 #include <linux/mutex.h> 13 #include <linux/slab.h> 14 #include <linux/rculist.h> 15 16 #include "trace.h" 17 18 static LIST_HEAD(trigger_commands); 19 static DEFINE_MUTEX(trigger_cmd_mutex); 20 21 static struct task_struct *trigger_kthread; 22 static struct llist_head trigger_data_free_list; 23 static DEFINE_MUTEX(trigger_data_kthread_mutex); 24 25 /* Bulk garbage collection of event_trigger_data elements */ 26 static int trigger_kthread_fn(void *ignore) 27 { 28 struct event_trigger_data *data, *tmp; 29 struct llist_node *llnodes; 30 31 /* Once this task starts, it lives forever */ 32 for (;;) { 33 set_current_state(TASK_INTERRUPTIBLE); 34 if (llist_empty(&trigger_data_free_list)) 35 schedule(); 36 37 __set_current_state(TASK_RUNNING); 38 39 llnodes = llist_del_all(&trigger_data_free_list); 40 41 /* make sure current triggers exit before free */ 42 tracepoint_synchronize_unregister(); 43 44 llist_for_each_entry_safe(data, tmp, llnodes, llist) 45 kfree(data); 46 } 47 48 return 0; 49 } 50 51 void trigger_data_free(struct event_trigger_data *data) 52 { 53 if (!data) 54 return; 55 56 if (data->cmd_ops->set_filter) 57 data->cmd_ops->set_filter(NULL, data, NULL); 58 59 if (unlikely(!trigger_kthread)) { 60 guard(mutex)(&trigger_data_kthread_mutex); 61 /* Check again after taking mutex */ 62 if (!trigger_kthread) { 63 struct task_struct *kthread; 64 65 kthread = kthread_create(trigger_kthread_fn, NULL, 66 "trigger_data_free"); 67 if (!IS_ERR(kthread)) 68 WRITE_ONCE(trigger_kthread, kthread); 69 } 70 } 71 72 if (!trigger_kthread) { 73 /* Do it the slow way */ 74 tracepoint_synchronize_unregister(); 75 kfree(data); 76 return; 77 } 78 79 llist_add(&data->llist, &trigger_data_free_list); 80 wake_up_process(trigger_kthread); 81 } 82 83 static inline void data_ops_trigger(struct event_trigger_data *data, 84 struct trace_buffer *buffer, void *rec, 85 struct ring_buffer_event *event) 86 { 87 const struct event_command *cmd_ops = data->cmd_ops; 88 89 if (data->flags & EVENT_TRIGGER_FL_COUNT) { 90 if (!cmd_ops->count_func(data, buffer, rec, event)) 91 return; 92 } 93 94 cmd_ops->trigger(data, buffer, rec, event); 95 } 96 97 /** 98 * event_triggers_call - Call triggers associated with a trace event 99 * @file: The trace_event_file associated with the event 100 * @buffer: The ring buffer that the event is being written to 101 * @rec: The trace entry for the event, NULL for unconditional invocation 102 * @event: The event meta data in the ring buffer 103 * 104 * For each trigger associated with an event, invoke the trigger 105 * function registered with the associated trigger command. If rec is 106 * non-NULL, it means that the trigger requires further processing and 107 * shouldn't be unconditionally invoked. If rec is non-NULL and the 108 * trigger has a filter associated with it, rec will checked against 109 * the filter and if the record matches the trigger will be invoked. 110 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked 111 * in any case until the current event is written, the trigger 112 * function isn't invoked but the bit associated with the deferred 113 * trigger is set in the return value. 114 * 115 * Returns an enum event_trigger_type value containing a set bit for 116 * any trigger that should be deferred, ETT_NONE if nothing to defer. 117 * 118 * Called from tracepoint handlers (with rcu_read_lock_sched() held). 119 * 120 * Return: an enum event_trigger_type value containing a set bit for 121 * any trigger that should be deferred, ETT_NONE if nothing to defer. 122 */ 123 enum event_trigger_type 124 event_triggers_call(struct trace_event_file *file, 125 struct trace_buffer *buffer, void *rec, 126 struct ring_buffer_event *event) 127 { 128 struct event_trigger_data *data; 129 enum event_trigger_type tt = ETT_NONE; 130 struct event_filter *filter; 131 132 if (list_empty(&file->triggers)) 133 return tt; 134 135 list_for_each_entry_rcu(data, &file->triggers, list) { 136 if (data->paused) 137 continue; 138 if (!rec) { 139 data_ops_trigger(data, buffer, rec, event); 140 continue; 141 } 142 filter = rcu_dereference_sched(data->filter); 143 if (filter && !filter_match_preds(filter, rec)) 144 continue; 145 if (event_command_post_trigger(data->cmd_ops)) { 146 tt |= data->cmd_ops->trigger_type; 147 continue; 148 } 149 data_ops_trigger(data, buffer, rec, event); 150 } 151 return tt; 152 } 153 EXPORT_SYMBOL_GPL(event_triggers_call); 154 155 bool __trace_trigger_soft_disabled(struct trace_event_file *file) 156 { 157 unsigned long eflags = file->flags; 158 159 if (eflags & EVENT_FILE_FL_TRIGGER_MODE) 160 event_triggers_call(file, NULL, NULL, NULL); 161 if (eflags & EVENT_FILE_FL_SOFT_DISABLED) 162 return true; 163 if (eflags & EVENT_FILE_FL_PID_FILTER) 164 return trace_event_ignore_this_pid(file); 165 return false; 166 } 167 EXPORT_SYMBOL_GPL(__trace_trigger_soft_disabled); 168 169 /** 170 * event_triggers_post_call - Call 'post_triggers' for a trace event 171 * @file: The trace_event_file associated with the event 172 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke 173 * 174 * For each trigger associated with an event, invoke the trigger 175 * function registered with the associated trigger command, if the 176 * corresponding bit is set in the tt enum passed into this function. 177 * See @event_triggers_call for details on how those bits are set. 178 * 179 * Called from tracepoint handlers (with rcu_read_lock_sched() held). 180 */ 181 void 182 event_triggers_post_call(struct trace_event_file *file, 183 enum event_trigger_type tt) 184 { 185 struct event_trigger_data *data; 186 187 list_for_each_entry_rcu(data, &file->triggers, list) { 188 if (data->paused) 189 continue; 190 if (data->cmd_ops->trigger_type & tt) 191 data_ops_trigger(data, NULL, NULL, NULL); 192 } 193 } 194 EXPORT_SYMBOL_GPL(event_triggers_post_call); 195 196 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL) 197 198 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos) 199 { 200 struct trace_event_file *event_file = event_file_data(m->private); 201 202 if (t == SHOW_AVAILABLE_TRIGGERS) { 203 (*pos)++; 204 return NULL; 205 } 206 return seq_list_next(t, &event_file->triggers, pos); 207 } 208 209 static bool check_user_trigger(struct trace_event_file *file) 210 { 211 struct event_trigger_data *data; 212 213 list_for_each_entry_rcu(data, &file->triggers, list, 214 lockdep_is_held(&event_mutex)) { 215 if (data->flags & EVENT_TRIGGER_FL_PROBE) 216 continue; 217 return true; 218 } 219 return false; 220 } 221 222 static void *trigger_start(struct seq_file *m, loff_t *pos) 223 { 224 struct trace_event_file *event_file; 225 226 /* ->stop() is called even if ->start() fails */ 227 mutex_lock(&event_mutex); 228 event_file = event_file_file(m->private); 229 if (unlikely(!event_file)) 230 return ERR_PTR(-ENODEV); 231 232 if (list_empty(&event_file->triggers) || !check_user_trigger(event_file)) 233 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL; 234 235 return seq_list_start(&event_file->triggers, *pos); 236 } 237 238 static void trigger_stop(struct seq_file *m, void *t) 239 { 240 mutex_unlock(&event_mutex); 241 } 242 243 static int trigger_show(struct seq_file *m, void *v) 244 { 245 struct event_trigger_data *data; 246 struct event_command *p; 247 248 if (v == SHOW_AVAILABLE_TRIGGERS) { 249 seq_puts(m, "# Available triggers:\n"); 250 seq_putc(m, '#'); 251 mutex_lock(&trigger_cmd_mutex); 252 list_for_each_entry_reverse(p, &trigger_commands, list) 253 seq_printf(m, " %s", p->name); 254 seq_putc(m, '\n'); 255 mutex_unlock(&trigger_cmd_mutex); 256 return 0; 257 } 258 259 data = list_entry(v, struct event_trigger_data, list); 260 data->cmd_ops->print(m, data); 261 262 return 0; 263 } 264 265 static const struct seq_operations event_triggers_seq_ops = { 266 .start = trigger_start, 267 .next = trigger_next, 268 .stop = trigger_stop, 269 .show = trigger_show, 270 }; 271 272 static int event_trigger_regex_open(struct inode *inode, struct file *file) 273 { 274 int ret; 275 276 ret = security_locked_down(LOCKDOWN_TRACEFS); 277 if (ret) 278 return ret; 279 280 guard(mutex)(&event_mutex); 281 282 if (unlikely(!event_file_file(file))) 283 return -ENODEV; 284 285 if ((file->f_mode & FMODE_WRITE) && 286 (file->f_flags & O_TRUNC)) { 287 struct trace_event_file *event_file; 288 struct event_command *p; 289 290 event_file = event_file_data(file); 291 292 list_for_each_entry(p, &trigger_commands, list) { 293 if (p->unreg_all) 294 p->unreg_all(event_file); 295 } 296 } 297 298 if (file->f_mode & FMODE_READ) { 299 ret = seq_open(file, &event_triggers_seq_ops); 300 if (!ret) { 301 struct seq_file *m = file->private_data; 302 m->private = file; 303 } 304 } 305 306 return ret; 307 } 308 309 int trigger_process_regex(struct trace_event_file *file, char *buff) 310 { 311 char *command, *next; 312 struct event_command *p; 313 314 next = buff = strim(buff); 315 316 command = strsep(&next, ": \t"); 317 if (next) { 318 next = skip_spaces(next); 319 if (!*next) 320 next = NULL; 321 } 322 command = (command[0] != '!') ? command : command + 1; 323 324 guard(mutex)(&trigger_cmd_mutex); 325 326 list_for_each_entry(p, &trigger_commands, list) { 327 if (strcmp(p->name, command) == 0) 328 return p->parse(p, file, buff, command, next); 329 } 330 331 return -EINVAL; 332 } 333 334 static ssize_t event_trigger_regex_write(struct file *file, 335 const char __user *ubuf, 336 size_t cnt, loff_t *ppos) 337 { 338 struct trace_event_file *event_file; 339 ssize_t ret; 340 char *buf __free(kfree) = NULL; 341 342 if (!cnt) 343 return 0; 344 345 if (cnt >= PAGE_SIZE) 346 return -EINVAL; 347 348 buf = memdup_user_nul(ubuf, cnt); 349 if (IS_ERR(buf)) 350 return PTR_ERR(buf); 351 352 guard(mutex)(&event_mutex); 353 354 event_file = event_file_file(file); 355 if (unlikely(!event_file)) 356 return -ENODEV; 357 358 ret = trigger_process_regex(event_file, buf); 359 if (ret < 0) 360 return ret; 361 362 *ppos += cnt; 363 return cnt; 364 } 365 366 static int event_trigger_regex_release(struct inode *inode, struct file *file) 367 { 368 if (file->f_mode & FMODE_READ) 369 seq_release(inode, file); 370 371 return 0; 372 } 373 374 static ssize_t 375 event_trigger_write(struct file *filp, const char __user *ubuf, 376 size_t cnt, loff_t *ppos) 377 { 378 return event_trigger_regex_write(filp, ubuf, cnt, ppos); 379 } 380 381 static int 382 event_trigger_open(struct inode *inode, struct file *filp) 383 { 384 /* Checks for tracefs lockdown */ 385 return event_trigger_regex_open(inode, filp); 386 } 387 388 static int 389 event_trigger_release(struct inode *inode, struct file *file) 390 { 391 return event_trigger_regex_release(inode, file); 392 } 393 394 const struct file_operations event_trigger_fops = { 395 .open = event_trigger_open, 396 .read = seq_read, 397 .write = event_trigger_write, 398 .llseek = tracing_lseek, 399 .release = event_trigger_release, 400 }; 401 402 /* 403 * Currently we only register event commands from __init, so mark this 404 * __init too. 405 */ 406 __init int register_event_command(struct event_command *cmd) 407 { 408 struct event_command *p; 409 410 guard(mutex)(&trigger_cmd_mutex); 411 412 list_for_each_entry(p, &trigger_commands, list) { 413 if (strcmp(cmd->name, p->name) == 0) 414 return -EBUSY; 415 } 416 list_add(&cmd->list, &trigger_commands); 417 418 return 0; 419 } 420 421 /* 422 * Currently we only unregister event commands from __init, so mark 423 * this __init too. 424 */ 425 __init int unregister_event_command(struct event_command *cmd) 426 { 427 struct event_command *p, *n; 428 429 guard(mutex)(&trigger_cmd_mutex); 430 431 list_for_each_entry_safe(p, n, &trigger_commands, list) { 432 if (strcmp(cmd->name, p->name) == 0) { 433 list_del_init(&p->list); 434 return 0; 435 } 436 } 437 438 return -ENODEV; 439 } 440 441 /** 442 * event_trigger_count - Optional count function for event triggers 443 * @data: Trigger-specific data 444 * @buffer: The ring buffer that the event is being written to 445 * @rec: The trace entry for the event, NULL for unconditional invocation 446 * @event: The event meta data in the ring buffer 447 * 448 * For triggers that can take a count parameter that doesn't do anything 449 * special, they can use this function to assign to their .count_func 450 * field. 451 * 452 * This simply does a count down of the @data->count field. 453 * 454 * If the @data->count is greater than zero, it will decrement it. 455 * 456 * Returns false if @data->count is zero, otherwise true. 457 */ 458 bool event_trigger_count(struct event_trigger_data *data, 459 struct trace_buffer *buffer, void *rec, 460 struct ring_buffer_event *event) 461 { 462 if (!data->count) 463 return false; 464 465 if (data->count != -1) 466 (data->count)--; 467 468 return true; 469 } 470 471 /** 472 * event_trigger_print - Generic event_command @print implementation 473 * @name: The name of the event trigger 474 * @m: The seq_file being printed to 475 * @data: Trigger-specific data 476 * @filter_str: filter_str to print, if present 477 * 478 * Common implementation for event triggers to print themselves. 479 * 480 * Usually wrapped by a function that simply sets the @name of the 481 * trigger command and then invokes this. 482 * 483 * Return: 0 on success, errno otherwise 484 */ 485 static int 486 event_trigger_print(const char *name, struct seq_file *m, 487 void *data, char *filter_str) 488 { 489 long count = (long)data; 490 491 seq_puts(m, name); 492 493 if (count == -1) 494 seq_puts(m, ":unlimited"); 495 else 496 seq_printf(m, ":count=%ld", count); 497 498 if (filter_str) 499 seq_printf(m, " if %s\n", filter_str); 500 else 501 seq_putc(m, '\n'); 502 503 return 0; 504 } 505 506 /** 507 * event_trigger_init - Generic event_command @init implementation 508 * @data: Trigger-specific data 509 * 510 * Common implementation of event trigger initialization. 511 * 512 * Usually used directly as the @init method in event trigger 513 * implementations. 514 * 515 * Return: 0 on success, errno otherwise 516 */ 517 int event_trigger_init(struct event_trigger_data *data) 518 { 519 data->ref++; 520 return 0; 521 } 522 523 /** 524 * event_trigger_free - Generic event_command @free implementation 525 * @data: Trigger-specific data 526 * 527 * Common implementation of event trigger de-initialization. 528 * 529 * Usually used directly as the @free method in event trigger 530 * implementations. 531 */ 532 static void 533 event_trigger_free(struct event_trigger_data *data) 534 { 535 if (WARN_ON_ONCE(data->ref <= 0)) 536 return; 537 538 data->ref--; 539 if (!data->ref) 540 trigger_data_free(data); 541 } 542 543 int trace_event_trigger_enable_disable(struct trace_event_file *file, 544 int trigger_enable) 545 { 546 int ret = 0; 547 548 if (trigger_enable) { 549 if (atomic_inc_return(&file->tm_ref) > 1) 550 return ret; 551 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags); 552 ret = trace_event_enable_disable(file, 1, 1); 553 } else { 554 if (atomic_dec_return(&file->tm_ref) > 0) 555 return ret; 556 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags); 557 ret = trace_event_enable_disable(file, 0, 1); 558 } 559 560 return ret; 561 } 562 563 /** 564 * clear_event_triggers - Clear all triggers associated with a trace array 565 * @tr: The trace array to clear 566 * 567 * For each trigger, the triggering event has its tm_ref decremented 568 * via trace_event_trigger_enable_disable(), and any associated event 569 * (in the case of enable/disable_event triggers) will have its sm_ref 570 * decremented via free()->trace_event_enable_disable(). That 571 * combination effectively reverses the soft-mode/trigger state added 572 * by trigger registration. 573 * 574 * Must be called with event_mutex held. 575 */ 576 void 577 clear_event_triggers(struct trace_array *tr) 578 { 579 struct trace_event_file *file; 580 581 list_for_each_entry(file, &tr->events, list) { 582 struct event_trigger_data *data, *n; 583 list_for_each_entry_safe(data, n, &file->triggers, list) { 584 trace_event_trigger_enable_disable(file, 0); 585 list_del_rcu(&data->list); 586 if (data->cmd_ops->free) 587 data->cmd_ops->free(data); 588 } 589 } 590 } 591 592 /** 593 * update_cond_flag - Set or reset the TRIGGER_COND bit 594 * @file: The trace_event_file associated with the event 595 * 596 * If an event has triggers and any of those triggers has a filter or 597 * a post_trigger, trigger invocation needs to be deferred until after 598 * the current event has logged its data, and the event should have 599 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be 600 * cleared. 601 */ 602 void update_cond_flag(struct trace_event_file *file) 603 { 604 struct event_trigger_data *data; 605 bool set_cond = false; 606 607 lockdep_assert_held(&event_mutex); 608 609 list_for_each_entry(data, &file->triggers, list) { 610 if (data->filter || event_command_post_trigger(data->cmd_ops) || 611 event_command_needs_rec(data->cmd_ops)) { 612 set_cond = true; 613 break; 614 } 615 } 616 617 if (set_cond) 618 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags); 619 else 620 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags); 621 } 622 623 /** 624 * register_trigger - Generic event_command @reg implementation 625 * @glob: The raw string used to register the trigger 626 * @data: Trigger-specific data to associate with the trigger 627 * @file: The trace_event_file associated with the event 628 * 629 * Common implementation for event trigger registration. 630 * 631 * Usually used directly as the @reg method in event command 632 * implementations. 633 * 634 * Return: 0 on success, errno otherwise 635 */ 636 static int register_trigger(char *glob, 637 struct event_trigger_data *data, 638 struct trace_event_file *file) 639 { 640 struct event_trigger_data *test; 641 int ret = 0; 642 643 lockdep_assert_held(&event_mutex); 644 645 list_for_each_entry(test, &file->triggers, list) { 646 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) 647 return -EEXIST; 648 } 649 650 if (data->cmd_ops->init) { 651 ret = data->cmd_ops->init(data); 652 if (ret < 0) 653 return ret; 654 } 655 656 list_add_rcu(&data->list, &file->triggers); 657 658 update_cond_flag(file); 659 ret = trace_event_trigger_enable_disable(file, 1); 660 if (ret < 0) { 661 list_del_rcu(&data->list); 662 update_cond_flag(file); 663 } 664 return ret; 665 } 666 667 /* 668 * True if the trigger was found and unregistered, else false. 669 */ 670 static bool try_unregister_trigger(char *glob, 671 struct event_trigger_data *test, 672 struct trace_event_file *file) 673 { 674 struct event_trigger_data *data = NULL, *iter; 675 676 lockdep_assert_held(&event_mutex); 677 678 list_for_each_entry(iter, &file->triggers, list) { 679 if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) { 680 data = iter; 681 list_del_rcu(&data->list); 682 trace_event_trigger_enable_disable(file, 0); 683 update_cond_flag(file); 684 break; 685 } 686 } 687 688 if (data) { 689 if (data->cmd_ops->free) 690 data->cmd_ops->free(data); 691 692 return true; 693 } 694 695 return false; 696 } 697 698 /** 699 * unregister_trigger - Generic event_command @unreg implementation 700 * @glob: The raw string used to register the trigger 701 * @test: Trigger-specific data used to find the trigger to remove 702 * @file: The trace_event_file associated with the event 703 * 704 * Common implementation for event trigger unregistration. 705 * 706 * Usually used directly as the @unreg method in event command 707 * implementations. 708 */ 709 static void unregister_trigger(char *glob, 710 struct event_trigger_data *test, 711 struct trace_event_file *file) 712 { 713 try_unregister_trigger(glob, test, file); 714 } 715 716 /* 717 * Event trigger parsing helper functions. 718 * 719 * These functions help make it easier to write an event trigger 720 * parsing function i.e. the struct event_command.parse() callback 721 * function responsible for parsing and registering a trigger command 722 * written to the 'trigger' file. 723 * 724 * A trigger command (or just 'trigger' for short) takes the form: 725 * [trigger] [if filter] 726 * 727 * The struct event_command.parse() callback (and other struct 728 * event_command functions) refer to several components of a trigger 729 * command. Those same components are referenced by the event trigger 730 * parsing helper functions defined below. These components are: 731 * 732 * cmd - the trigger command name 733 * glob - the trigger command name optionally prefaced with '!' 734 * param_and_filter - text following cmd and ':' 735 * param - text following cmd and ':' and stripped of filter 736 * filter - the optional filter text following (and including) 'if' 737 * 738 * To illustrate the use of these components, here are some concrete 739 * examples. For the following triggers: 740 * 741 * echo 'traceon:5 if pid == 0' > trigger 742 * - 'traceon' is both cmd and glob 743 * - '5 if pid == 0' is the param_and_filter 744 * - '5' is the param 745 * - 'if pid == 0' is the filter 746 * 747 * echo 'enable_event:sys:event:n' > trigger 748 * - 'enable_event' is both cmd and glob 749 * - 'sys:event:n' is the param_and_filter 750 * - 'sys:event:n' is the param 751 * - there is no filter 752 * 753 * echo 'hist:keys=pid if prio > 50' > trigger 754 * - 'hist' is both cmd and glob 755 * - 'keys=pid if prio > 50' is the param_and_filter 756 * - 'keys=pid' is the param 757 * - 'if prio > 50' is the filter 758 * 759 * echo '!enable_event:sys:event:n' > trigger 760 * - 'enable_event' the cmd 761 * - '!enable_event' is the glob 762 * - 'sys:event:n' is the param_and_filter 763 * - 'sys:event:n' is the param 764 * - there is no filter 765 * 766 * echo 'traceoff' > trigger 767 * - 'traceoff' is both cmd and glob 768 * - there is no param_and_filter 769 * - there is no param 770 * - there is no filter 771 * 772 * There are a few different categories of event trigger covered by 773 * these helpers: 774 * 775 * - triggers that don't require a parameter e.g. traceon 776 * - triggers that do require a parameter e.g. enable_event and hist 777 * - triggers that though they may not require a param may support an 778 * optional 'n' param (n = number of times the trigger should fire) 779 * e.g.: traceon:5 or enable_event:sys:event:n 780 * - triggers that do not support an 'n' param e.g. hist 781 * 782 * These functions can be used or ignored as necessary - it all 783 * depends on the complexity of the trigger, and the granularity of 784 * the functions supported reflects the fact that some implementations 785 * may need to customize certain aspects of their implementations and 786 * won't need certain functions. For instance, the hist trigger 787 * implementation doesn't use event_trigger_separate_filter() because 788 * it has special requirements for handling the filter. 789 */ 790 791 /** 792 * event_trigger_check_remove - check whether an event trigger specifies remove 793 * @glob: The trigger command string, with optional remove(!) operator 794 * 795 * The event trigger callback implementations pass in 'glob' as a 796 * parameter. This is the command name either with or without a 797 * remove(!) operator. This function simply parses the glob and 798 * determines whether the command corresponds to a trigger removal or 799 * a trigger addition. 800 * 801 * Return: true if this is a remove command, false otherwise 802 */ 803 bool event_trigger_check_remove(const char *glob) 804 { 805 return (glob && glob[0] == '!') ? true : false; 806 } 807 808 /** 809 * event_trigger_empty_param - check whether the param is empty 810 * @param: The trigger param string 811 * 812 * The event trigger callback implementations pass in 'param' as a 813 * parameter. This corresponds to the string following the command 814 * name minus the command name. This function can be called by a 815 * callback implementation for any command that requires a param; a 816 * callback that doesn't require a param can ignore it. 817 * 818 * Return: true if this is an empty param, false otherwise 819 */ 820 bool event_trigger_empty_param(const char *param) 821 { 822 return !param; 823 } 824 825 /** 826 * event_trigger_separate_filter - separate an event trigger from a filter 827 * @param_and_filter: String containing trigger and possibly filter 828 * @param: outparam, will be filled with a pointer to the trigger 829 * @filter: outparam, will be filled with a pointer to the filter 830 * @param_required: Specifies whether or not the param string is required 831 * 832 * Given a param string of the form '[trigger] [if filter]', this 833 * function separates the filter from the trigger and returns the 834 * trigger in @param and the filter in @filter. Either the @param 835 * or the @filter may be set to NULL by this function - if not set to 836 * NULL, they will contain strings corresponding to the trigger and 837 * filter. 838 * 839 * There are two cases that need to be handled with respect to the 840 * passed-in param: either the param is required, or it is not 841 * required. If @param_required is set, and there's no param, it will 842 * return -EINVAL. If @param_required is not set and there's a param 843 * that starts with a number, that corresponds to the case of a 844 * trigger with :n (n = number of times the trigger should fire) and 845 * the parsing continues normally; otherwise the function just returns 846 * and assumes param just contains a filter and there's nothing else 847 * to do. 848 * 849 * Return: 0 on success, errno otherwise 850 */ 851 int event_trigger_separate_filter(char *param_and_filter, char **param, 852 char **filter, bool param_required) 853 { 854 int ret = 0; 855 856 *param = *filter = NULL; 857 858 if (!param_and_filter) { 859 if (param_required) 860 ret = -EINVAL; 861 return ret; 862 } 863 864 /* 865 * Here we check for an optional param. The only legal 866 * optional param is :n, and if that's the case, continue 867 * below. Otherwise we assume what's left is a filter and 868 * return it as the filter string for the caller to deal with. 869 */ 870 if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) { 871 *filter = param_and_filter; 872 return ret; 873 } 874 875 /* 876 * Separate the param from the filter (param [if filter]). 877 * Here we have either an optional :n param or a required 878 * param and an optional filter. 879 */ 880 *param = strsep(¶m_and_filter, " \t"); 881 882 /* 883 * Here we have a filter, though it may be empty. 884 */ 885 if (param_and_filter) { 886 *filter = skip_spaces(param_and_filter); 887 if (!**filter) 888 *filter = NULL; 889 } 890 return ret; 891 } 892 893 /** 894 * trigger_data_alloc - allocate and init event_trigger_data for a trigger 895 * @cmd_ops: The event_command operations for the trigger 896 * @cmd: The cmd string 897 * @param: The param string 898 * @private_data: User data to associate with the event trigger 899 * 900 * Allocate an event_trigger_data instance and initialize it. The 901 * @cmd_ops defines how the trigger will operate. If @param is set, 902 * and @cmd_ops->trigger_ops->count_func is non NULL, then the 903 * data->count is set to @param and before the trigger is executed, the 904 * @cmd_ops->trigger_ops->count_func() is called. If that function returns 905 * false, the @cmd_ops->trigger_ops->trigger() function will not be called. 906 * @private_data can also be passed in and associated with the 907 * event_trigger_data. 908 * 909 * Use trigger_data_free() to free an event_trigger_data object. 910 * 911 * Return: The trigger_data object success, NULL otherwise 912 */ 913 struct event_trigger_data *trigger_data_alloc(struct event_command *cmd_ops, 914 char *cmd, 915 char *param, 916 void *private_data) 917 { 918 struct event_trigger_data *trigger_data; 919 920 trigger_data = kzalloc_obj(*trigger_data); 921 if (!trigger_data) 922 return NULL; 923 924 trigger_data->count = -1; 925 trigger_data->cmd_ops = cmd_ops; 926 trigger_data->private_data = private_data; 927 if (param && cmd_ops->count_func) 928 trigger_data->flags |= EVENT_TRIGGER_FL_COUNT; 929 930 INIT_LIST_HEAD(&trigger_data->list); 931 INIT_LIST_HEAD(&trigger_data->named_list); 932 RCU_INIT_POINTER(trigger_data->filter, NULL); 933 934 return trigger_data; 935 } 936 937 /** 938 * event_trigger_parse_num - parse and return the number param for a trigger 939 * @param: The param string 940 * @trigger_data: The trigger_data for the trigger 941 * 942 * Parse the :n (n = number of times the trigger should fire) param 943 * and set the count variable in the trigger_data to the parsed count. 944 * 945 * Return: 0 on success, errno otherwise 946 */ 947 int event_trigger_parse_num(char *param, 948 struct event_trigger_data *trigger_data) 949 { 950 char *number; 951 int ret = 0; 952 953 if (param) { 954 number = strsep(¶m, ":"); 955 956 if (!strlen(number)) 957 return -EINVAL; 958 959 /* 960 * We use the callback data field (which is a pointer) 961 * as our counter. 962 */ 963 ret = kstrtoul(number, 0, &trigger_data->count); 964 } 965 966 return ret; 967 } 968 969 /** 970 * event_trigger_set_filter - set an event trigger's filter 971 * @cmd_ops: The event_command operations for the trigger 972 * @file: The event file for the trigger's event 973 * @param: The string containing the filter 974 * @trigger_data: The trigger_data for the trigger 975 * 976 * Set the filter for the trigger. If the filter is NULL, just return 977 * without error. 978 * 979 * Return: 0 on success, errno otherwise 980 */ 981 int event_trigger_set_filter(struct event_command *cmd_ops, 982 struct trace_event_file *file, 983 char *param, 984 struct event_trigger_data *trigger_data) 985 { 986 if (param && cmd_ops->set_filter) 987 return cmd_ops->set_filter(param, trigger_data, file); 988 989 return 0; 990 } 991 992 /** 993 * event_trigger_reset_filter - reset an event trigger's filter 994 * @cmd_ops: The event_command operations for the trigger 995 * @trigger_data: The trigger_data for the trigger 996 * 997 * Reset the filter for the trigger to no filter. 998 */ 999 void event_trigger_reset_filter(struct event_command *cmd_ops, 1000 struct event_trigger_data *trigger_data) 1001 { 1002 if (cmd_ops->set_filter) 1003 cmd_ops->set_filter(NULL, trigger_data, NULL); 1004 } 1005 1006 /** 1007 * event_trigger_register - register an event trigger 1008 * @cmd_ops: The event_command operations for the trigger 1009 * @file: The event file for the trigger's event 1010 * @glob: The trigger command string, with optional remove(!) operator 1011 * @trigger_data: The trigger_data for the trigger 1012 * 1013 * Register an event trigger. The @cmd_ops are used to call the 1014 * cmd_ops->reg() function which actually does the registration. 1015 * 1016 * Return: 0 on success, errno otherwise 1017 */ 1018 int event_trigger_register(struct event_command *cmd_ops, 1019 struct trace_event_file *file, 1020 char *glob, 1021 struct event_trigger_data *trigger_data) 1022 { 1023 return cmd_ops->reg(glob, trigger_data, file); 1024 } 1025 1026 /** 1027 * event_trigger_unregister - unregister an event trigger 1028 * @cmd_ops: The event_command operations for the trigger 1029 * @file: The event file for the trigger's event 1030 * @glob: The trigger command string, with optional remove(!) operator 1031 * @trigger_data: The trigger_data for the trigger 1032 * 1033 * Unregister an event trigger. The @cmd_ops are used to call the 1034 * cmd_ops->unreg() function which actually does the unregistration. 1035 */ 1036 void event_trigger_unregister(struct event_command *cmd_ops, 1037 struct trace_event_file *file, 1038 char *glob, 1039 struct event_trigger_data *trigger_data) 1040 { 1041 cmd_ops->unreg(glob, trigger_data, file); 1042 } 1043 1044 /* 1045 * End event trigger parsing helper functions. 1046 */ 1047 1048 /** 1049 * event_trigger_parse - Generic event_command @parse implementation 1050 * @cmd_ops: The command ops, used for trigger registration 1051 * @file: The trace_event_file associated with the event 1052 * @glob: The raw string used to register the trigger 1053 * @cmd: The cmd portion of the string used to register the trigger 1054 * @param_and_filter: The param and filter portion of the string used to register the trigger 1055 * 1056 * Common implementation for event command parsing and trigger 1057 * instantiation. 1058 * 1059 * Usually used directly as the @parse method in event command 1060 * implementations. 1061 * 1062 * Return: 0 on success, errno otherwise 1063 */ 1064 static int 1065 event_trigger_parse(struct event_command *cmd_ops, 1066 struct trace_event_file *file, 1067 char *glob, char *cmd, char *param_and_filter) 1068 { 1069 struct event_trigger_data *trigger_data; 1070 char *param, *filter; 1071 bool remove; 1072 int ret; 1073 1074 remove = event_trigger_check_remove(glob); 1075 1076 ret = event_trigger_separate_filter(param_and_filter, ¶m, &filter, false); 1077 if (ret) 1078 return ret; 1079 1080 ret = -ENOMEM; 1081 trigger_data = trigger_data_alloc(cmd_ops, cmd, param, file); 1082 if (!trigger_data) 1083 return ret; 1084 1085 if (remove) { 1086 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data); 1087 trigger_data_free(trigger_data); 1088 return 0; 1089 } 1090 1091 ret = event_trigger_parse_num(param, trigger_data); 1092 if (ret) 1093 goto out_free; 1094 1095 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data); 1096 if (ret < 0) 1097 goto out_free; 1098 1099 /* Up the trigger_data count to make sure reg doesn't free it on failure */ 1100 event_trigger_init(trigger_data); 1101 1102 ret = event_trigger_register(cmd_ops, file, glob, trigger_data); 1103 if (ret) 1104 goto out_free; 1105 1106 /* Down the counter of trigger_data or free it if not used anymore */ 1107 event_trigger_free(trigger_data); 1108 return ret; 1109 1110 out_free: 1111 event_trigger_reset_filter(cmd_ops, trigger_data); 1112 trigger_data_free(trigger_data); 1113 return ret; 1114 } 1115 1116 /** 1117 * set_trigger_filter - Generic event_command @set_filter implementation 1118 * @filter_str: The filter string for the trigger, NULL to remove filter 1119 * @trigger_data: Trigger-specific data 1120 * @file: The trace_event_file associated with the event 1121 * 1122 * Common implementation for event command filter parsing and filter 1123 * instantiation. 1124 * 1125 * Usually used directly as the @set_filter method in event command 1126 * implementations. 1127 * 1128 * Also used to remove a filter (if filter_str = NULL). 1129 * 1130 * Return: 0 on success, errno otherwise 1131 */ 1132 int set_trigger_filter(char *filter_str, 1133 struct event_trigger_data *trigger_data, 1134 struct trace_event_file *file) 1135 { 1136 struct event_trigger_data *data = trigger_data; 1137 struct event_filter *filter = NULL, *tmp; 1138 int ret = -EINVAL; 1139 char *s; 1140 1141 if (!filter_str) /* clear the current filter */ 1142 goto assign; 1143 1144 s = strsep(&filter_str, " \t"); 1145 1146 if (!strlen(s) || strcmp(s, "if") != 0) 1147 return ret; 1148 1149 if (!filter_str) 1150 return ret; 1151 1152 /* The filter is for the 'trigger' event, not the triggered event */ 1153 ret = create_event_filter(file->tr, file->event_call, 1154 filter_str, true, &filter); 1155 1156 /* Only enabled set_str for error handling */ 1157 if (filter) { 1158 kfree(filter->filter_string); 1159 filter->filter_string = NULL; 1160 } 1161 1162 /* 1163 * If create_event_filter() fails, filter still needs to be freed. 1164 * Which the calling code will do with data->filter. 1165 */ 1166 assign: 1167 tmp = rcu_access_pointer(data->filter); 1168 1169 rcu_assign_pointer(data->filter, filter); 1170 1171 if (tmp) { 1172 /* 1173 * Make sure the call is done with the filter. 1174 * It is possible that a filter could fail at boot up, 1175 * and then this path will be called. Avoid the synchronization 1176 * in that case. 1177 */ 1178 if (system_state != SYSTEM_BOOTING) 1179 tracepoint_synchronize_unregister(); 1180 free_event_filter(tmp); 1181 } 1182 1183 kfree(data->filter_str); 1184 data->filter_str = NULL; 1185 1186 if (filter_str) { 1187 data->filter_str = kstrdup(filter_str, GFP_KERNEL); 1188 if (!data->filter_str) { 1189 free_event_filter(rcu_access_pointer(data->filter)); 1190 data->filter = NULL; 1191 ret = -ENOMEM; 1192 } 1193 } 1194 return ret; 1195 } 1196 1197 static LIST_HEAD(named_triggers); 1198 1199 /** 1200 * find_named_trigger - Find the common named trigger associated with @name 1201 * @name: The name of the set of named triggers to find the common data for 1202 * 1203 * Named triggers are sets of triggers that share a common set of 1204 * trigger data. The first named trigger registered with a given name 1205 * owns the common trigger data that the others subsequently 1206 * registered with the same name will reference. This function 1207 * returns the common trigger data associated with that first 1208 * registered instance. 1209 * 1210 * Return: the common trigger data for the given named trigger on 1211 * success, NULL otherwise. 1212 */ 1213 struct event_trigger_data *find_named_trigger(const char *name) 1214 { 1215 struct event_trigger_data *data; 1216 1217 if (!name) 1218 return NULL; 1219 1220 list_for_each_entry(data, &named_triggers, named_list) { 1221 if (data->named_data) 1222 continue; 1223 if (strcmp(data->name, name) == 0) 1224 return data; 1225 } 1226 1227 return NULL; 1228 } 1229 1230 /** 1231 * is_named_trigger - determine if a given trigger is a named trigger 1232 * @test: The trigger data to test 1233 * 1234 * Return: true if 'test' is a named trigger, false otherwise. 1235 */ 1236 bool is_named_trigger(struct event_trigger_data *test) 1237 { 1238 struct event_trigger_data *data; 1239 1240 list_for_each_entry(data, &named_triggers, named_list) { 1241 if (test == data) 1242 return true; 1243 } 1244 1245 return false; 1246 } 1247 1248 /** 1249 * save_named_trigger - save the trigger in the named trigger list 1250 * @name: The name of the named trigger set 1251 * @data: The trigger data to save 1252 * 1253 * Return: 0 if successful, negative error otherwise. 1254 */ 1255 int save_named_trigger(const char *name, struct event_trigger_data *data) 1256 { 1257 data->name = kstrdup(name, GFP_KERNEL); 1258 if (!data->name) 1259 return -ENOMEM; 1260 1261 list_add(&data->named_list, &named_triggers); 1262 1263 return 0; 1264 } 1265 1266 /** 1267 * del_named_trigger - delete a trigger from the named trigger list 1268 * @data: The trigger data to delete 1269 */ 1270 void del_named_trigger(struct event_trigger_data *data) 1271 { 1272 kfree(data->name); 1273 data->name = NULL; 1274 1275 list_del(&data->named_list); 1276 } 1277 1278 static void __pause_named_trigger(struct event_trigger_data *data, bool pause) 1279 { 1280 struct event_trigger_data *test; 1281 1282 list_for_each_entry(test, &named_triggers, named_list) { 1283 if (strcmp(test->name, data->name) == 0) { 1284 if (pause) { 1285 test->paused_tmp = test->paused; 1286 test->paused = true; 1287 } else { 1288 test->paused = test->paused_tmp; 1289 } 1290 } 1291 } 1292 } 1293 1294 /** 1295 * pause_named_trigger - Pause all named triggers with the same name 1296 * @data: The trigger data of a named trigger to pause 1297 * 1298 * Pauses a named trigger along with all other triggers having the 1299 * same name. Because named triggers share a common set of data, 1300 * pausing only one is meaningless, so pausing one named trigger needs 1301 * to pause all triggers with the same name. 1302 */ 1303 void pause_named_trigger(struct event_trigger_data *data) 1304 { 1305 __pause_named_trigger(data, true); 1306 } 1307 1308 /** 1309 * unpause_named_trigger - Un-pause all named triggers with the same name 1310 * @data: The trigger data of a named trigger to unpause 1311 * 1312 * Un-pauses a named trigger along with all other triggers having the 1313 * same name. Because named triggers share a common set of data, 1314 * unpausing only one is meaningless, so unpausing one named trigger 1315 * needs to unpause all triggers with the same name. 1316 */ 1317 void unpause_named_trigger(struct event_trigger_data *data) 1318 { 1319 __pause_named_trigger(data, false); 1320 } 1321 1322 /** 1323 * set_named_trigger_data - Associate common named trigger data 1324 * @data: The trigger data to associate 1325 * @named_data: The common named trigger to be associated 1326 * 1327 * Named triggers are sets of triggers that share a common set of 1328 * trigger data. The first named trigger registered with a given name 1329 * owns the common trigger data that the others subsequently 1330 * registered with the same name will reference. This function 1331 * associates the common trigger data from the first trigger with the 1332 * given trigger. 1333 */ 1334 void set_named_trigger_data(struct event_trigger_data *data, 1335 struct event_trigger_data *named_data) 1336 { 1337 data->named_data = named_data; 1338 } 1339 1340 struct event_trigger_data * 1341 get_named_trigger_data(struct event_trigger_data *data) 1342 { 1343 return data->named_data; 1344 } 1345 1346 static void 1347 traceon_trigger(struct event_trigger_data *data, 1348 struct trace_buffer *buffer, void *rec, 1349 struct ring_buffer_event *event) 1350 { 1351 struct trace_event_file *file = data->private_data; 1352 1353 if (WARN_ON_ONCE(!file)) 1354 return; 1355 1356 if (tracer_tracing_is_on(file->tr)) 1357 return; 1358 1359 tracer_tracing_on(file->tr); 1360 } 1361 1362 static bool 1363 traceon_count_func(struct event_trigger_data *data, 1364 struct trace_buffer *buffer, void *rec, 1365 struct ring_buffer_event *event) 1366 { 1367 struct trace_event_file *file = data->private_data; 1368 1369 if (WARN_ON_ONCE(!file)) 1370 return false; 1371 1372 if (tracer_tracing_is_on(file->tr)) 1373 return false; 1374 1375 if (!data->count) 1376 return false; 1377 1378 if (data->count != -1) 1379 (data->count)--; 1380 1381 return true; 1382 } 1383 1384 static void 1385 traceoff_trigger(struct event_trigger_data *data, 1386 struct trace_buffer *buffer, void *rec, 1387 struct ring_buffer_event *event) 1388 { 1389 struct trace_event_file *file = data->private_data; 1390 1391 if (WARN_ON_ONCE(!file)) 1392 return; 1393 1394 if (!tracer_tracing_is_on(file->tr)) 1395 return; 1396 1397 tracer_tracing_off(file->tr); 1398 } 1399 1400 static bool 1401 traceoff_count_func(struct event_trigger_data *data, 1402 struct trace_buffer *buffer, void *rec, 1403 struct ring_buffer_event *event) 1404 { 1405 struct trace_event_file *file = data->private_data; 1406 1407 if (WARN_ON_ONCE(!file)) 1408 return false; 1409 1410 if (!tracer_tracing_is_on(file->tr)) 1411 return false; 1412 1413 if (!data->count) 1414 return false; 1415 1416 if (data->count != -1) 1417 (data->count)--; 1418 1419 return true; 1420 } 1421 1422 static int 1423 traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1424 { 1425 return event_trigger_print("traceon", m, (void *)data->count, 1426 data->filter_str); 1427 } 1428 1429 static int 1430 traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1431 { 1432 return event_trigger_print("traceoff", m, (void *)data->count, 1433 data->filter_str); 1434 } 1435 1436 static struct event_command trigger_traceon_cmd = { 1437 .name = "traceon", 1438 .trigger_type = ETT_TRACE_ONOFF, 1439 .parse = event_trigger_parse, 1440 .reg = register_trigger, 1441 .unreg = unregister_trigger, 1442 .set_filter = set_trigger_filter, 1443 .trigger = traceon_trigger, 1444 .count_func = traceon_count_func, 1445 .print = traceon_trigger_print, 1446 .init = event_trigger_init, 1447 .free = event_trigger_free, 1448 }; 1449 1450 static struct event_command trigger_traceoff_cmd = { 1451 .name = "traceoff", 1452 .trigger_type = ETT_TRACE_ONOFF, 1453 .flags = EVENT_CMD_FL_POST_TRIGGER, 1454 .parse = event_trigger_parse, 1455 .reg = register_trigger, 1456 .unreg = unregister_trigger, 1457 .set_filter = set_trigger_filter, 1458 .trigger = traceoff_trigger, 1459 .count_func = traceoff_count_func, 1460 .print = traceoff_trigger_print, 1461 .init = event_trigger_init, 1462 .free = event_trigger_free, 1463 }; 1464 1465 #ifdef CONFIG_TRACER_SNAPSHOT 1466 static void 1467 snapshot_trigger(struct event_trigger_data *data, 1468 struct trace_buffer *buffer, void *rec, 1469 struct ring_buffer_event *event) 1470 { 1471 struct trace_event_file *file = data->private_data; 1472 1473 if (WARN_ON_ONCE(!file)) 1474 return; 1475 1476 tracing_snapshot_instance(file->tr); 1477 } 1478 1479 static int 1480 register_snapshot_trigger(char *glob, 1481 struct event_trigger_data *data, 1482 struct trace_event_file *file) 1483 { 1484 int ret = tracing_arm_snapshot(file->tr); 1485 1486 if (ret < 0) 1487 return ret; 1488 1489 ret = register_trigger(glob, data, file); 1490 if (ret < 0) 1491 tracing_disarm_snapshot(file->tr); 1492 return ret; 1493 } 1494 1495 static void unregister_snapshot_trigger(char *glob, 1496 struct event_trigger_data *data, 1497 struct trace_event_file *file) 1498 { 1499 if (try_unregister_trigger(glob, data, file)) 1500 tracing_disarm_snapshot(file->tr); 1501 } 1502 1503 static int 1504 snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1505 { 1506 return event_trigger_print("snapshot", m, (void *)data->count, 1507 data->filter_str); 1508 } 1509 1510 static struct event_command trigger_snapshot_cmd = { 1511 .name = "snapshot", 1512 .trigger_type = ETT_SNAPSHOT, 1513 .parse = event_trigger_parse, 1514 .reg = register_snapshot_trigger, 1515 .unreg = unregister_snapshot_trigger, 1516 .set_filter = set_trigger_filter, 1517 .trigger = snapshot_trigger, 1518 .count_func = event_trigger_count, 1519 .print = snapshot_trigger_print, 1520 .init = event_trigger_init, 1521 .free = event_trigger_free, 1522 }; 1523 1524 static __init int register_trigger_snapshot_cmd(void) 1525 { 1526 int ret; 1527 1528 ret = register_event_command(&trigger_snapshot_cmd); 1529 WARN_ON(ret < 0); 1530 1531 return ret; 1532 } 1533 #else 1534 static __init int register_trigger_snapshot_cmd(void) { return 0; } 1535 #endif /* CONFIG_TRACER_SNAPSHOT */ 1536 1537 #ifdef CONFIG_STACKTRACE 1538 #ifdef CONFIG_UNWINDER_ORC 1539 /* Skip 2: 1540 * event_triggers_post_call() 1541 * trace_event_raw_event_xxx() 1542 */ 1543 # define STACK_SKIP 2 1544 #else 1545 /* 1546 * Skip 4: 1547 * stacktrace_trigger() 1548 * event_triggers_post_call() 1549 * trace_event_buffer_commit() 1550 * trace_event_raw_event_xxx() 1551 */ 1552 #define STACK_SKIP 4 1553 #endif 1554 1555 static void 1556 stacktrace_trigger(struct event_trigger_data *data, 1557 struct trace_buffer *buffer, void *rec, 1558 struct ring_buffer_event *event) 1559 { 1560 struct trace_event_file *file = data->private_data; 1561 1562 if (WARN_ON_ONCE(!file)) 1563 return; 1564 1565 __trace_stack(file->tr, tracing_gen_ctx_dec(), STACK_SKIP); 1566 } 1567 1568 static int 1569 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1570 { 1571 return event_trigger_print("stacktrace", m, (void *)data->count, 1572 data->filter_str); 1573 } 1574 1575 static struct event_command trigger_stacktrace_cmd = { 1576 .name = "stacktrace", 1577 .trigger_type = ETT_STACKTRACE, 1578 .flags = EVENT_CMD_FL_POST_TRIGGER, 1579 .parse = event_trigger_parse, 1580 .reg = register_trigger, 1581 .unreg = unregister_trigger, 1582 .set_filter = set_trigger_filter, 1583 .trigger = stacktrace_trigger, 1584 .count_func = event_trigger_count, 1585 .print = stacktrace_trigger_print, 1586 .init = event_trigger_init, 1587 .free = event_trigger_free, 1588 }; 1589 1590 static __init int register_trigger_stacktrace_cmd(void) 1591 { 1592 int ret; 1593 1594 ret = register_event_command(&trigger_stacktrace_cmd); 1595 WARN_ON(ret < 0); 1596 1597 return ret; 1598 } 1599 #else 1600 static __init int register_trigger_stacktrace_cmd(void) { return 0; } 1601 #endif /* CONFIG_STACKTRACE */ 1602 1603 static __init void unregister_trigger_traceon_traceoff_cmds(void) 1604 { 1605 unregister_event_command(&trigger_traceon_cmd); 1606 unregister_event_command(&trigger_traceoff_cmd); 1607 } 1608 1609 static void 1610 event_enable_trigger(struct event_trigger_data *data, 1611 struct trace_buffer *buffer, void *rec, 1612 struct ring_buffer_event *event) 1613 { 1614 struct enable_trigger_data *enable_data = data->private_data; 1615 1616 if (enable_data->enable) 1617 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1618 else 1619 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1620 } 1621 1622 static bool 1623 event_enable_count_func(struct event_trigger_data *data, 1624 struct trace_buffer *buffer, void *rec, 1625 struct ring_buffer_event *event) 1626 { 1627 struct enable_trigger_data *enable_data = data->private_data; 1628 1629 if (!data->count) 1630 return false; 1631 1632 /* Skip if the event is in a state we want to switch to */ 1633 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 1634 return false; 1635 1636 if (data->count != -1) 1637 (data->count)--; 1638 1639 return true; 1640 } 1641 1642 int event_enable_trigger_print(struct seq_file *m, 1643 struct event_trigger_data *data) 1644 { 1645 struct enable_trigger_data *enable_data = data->private_data; 1646 1647 seq_printf(m, "%s:%s:%s", 1648 enable_data->hist ? 1649 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) : 1650 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR), 1651 enable_data->file->event_call->class->system, 1652 trace_event_name(enable_data->file->event_call)); 1653 1654 if (data->count == -1) 1655 seq_puts(m, ":unlimited"); 1656 else 1657 seq_printf(m, ":count=%ld", data->count); 1658 1659 if (data->filter_str) 1660 seq_printf(m, " if %s\n", data->filter_str); 1661 else 1662 seq_putc(m, '\n'); 1663 1664 return 0; 1665 } 1666 1667 void event_enable_trigger_free(struct event_trigger_data *data) 1668 { 1669 struct enable_trigger_data *enable_data = data->private_data; 1670 1671 if (WARN_ON_ONCE(data->ref <= 0)) 1672 return; 1673 1674 data->ref--; 1675 if (!data->ref) { 1676 /* Remove the SOFT_MODE flag */ 1677 trace_event_enable_disable(enable_data->file, 0, 1); 1678 trace_event_put_ref(enable_data->file->event_call); 1679 trigger_data_free(data); 1680 kfree(enable_data); 1681 } 1682 } 1683 1684 int event_enable_trigger_parse(struct event_command *cmd_ops, 1685 struct trace_event_file *file, 1686 char *glob, char *cmd, char *param_and_filter) 1687 { 1688 struct trace_event_file *event_enable_file; 1689 struct enable_trigger_data *enable_data; 1690 struct event_trigger_data *trigger_data; 1691 struct trace_array *tr = file->tr; 1692 char *param, *filter; 1693 bool enable, remove; 1694 const char *system; 1695 const char *event; 1696 bool hist = false; 1697 int ret; 1698 1699 remove = event_trigger_check_remove(glob); 1700 1701 if (event_trigger_empty_param(param_and_filter)) 1702 return -EINVAL; 1703 1704 ret = event_trigger_separate_filter(param_and_filter, ¶m, &filter, true); 1705 if (ret) 1706 return ret; 1707 1708 system = strsep(¶m, ":"); 1709 if (!param) 1710 return -EINVAL; 1711 1712 event = strsep(¶m, ":"); 1713 1714 ret = -EINVAL; 1715 event_enable_file = find_event_file(tr, system, event); 1716 if (!event_enable_file) 1717 return ret; 1718 1719 #ifdef CONFIG_HIST_TRIGGERS 1720 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) || 1721 (strcmp(cmd, DISABLE_HIST_STR) == 0)); 1722 1723 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) || 1724 (strcmp(cmd, ENABLE_HIST_STR) == 0)); 1725 #else 1726 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 1727 #endif 1728 ret = -ENOMEM; 1729 1730 enable_data = kzalloc_obj(*enable_data); 1731 if (!enable_data) 1732 return ret; 1733 1734 enable_data->hist = hist; 1735 enable_data->enable = enable; 1736 enable_data->file = event_enable_file; 1737 1738 trigger_data = trigger_data_alloc(cmd_ops, cmd, param, enable_data); 1739 if (!trigger_data) { 1740 kfree(enable_data); 1741 return ret; 1742 } 1743 1744 if (remove) { 1745 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data); 1746 kfree(trigger_data); 1747 kfree(enable_data); 1748 ret = 0; 1749 return ret; 1750 } 1751 1752 /* Up the trigger_data count to make sure nothing frees it on failure */ 1753 event_trigger_init(trigger_data); 1754 1755 ret = event_trigger_parse_num(param, trigger_data); 1756 if (ret) 1757 goto out_free; 1758 1759 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data); 1760 if (ret < 0) 1761 goto out_free; 1762 1763 /* Don't let event modules unload while probe registered */ 1764 ret = trace_event_try_get_ref(event_enable_file->event_call); 1765 if (!ret) { 1766 ret = -EBUSY; 1767 goto out_free; 1768 } 1769 1770 ret = trace_event_enable_disable(event_enable_file, 1, 1); 1771 if (ret < 0) 1772 goto out_put; 1773 1774 ret = event_trigger_register(cmd_ops, file, glob, trigger_data); 1775 if (ret) 1776 goto out_disable; 1777 1778 event_trigger_free(trigger_data); 1779 return ret; 1780 out_disable: 1781 trace_event_enable_disable(event_enable_file, 0, 1); 1782 out_put: 1783 trace_event_put_ref(event_enable_file->event_call); 1784 out_free: 1785 event_trigger_reset_filter(cmd_ops, trigger_data); 1786 event_trigger_free(trigger_data); 1787 kfree(enable_data); 1788 1789 return ret; 1790 } 1791 1792 int event_enable_register_trigger(char *glob, 1793 struct event_trigger_data *data, 1794 struct trace_event_file *file) 1795 { 1796 struct enable_trigger_data *enable_data = data->private_data; 1797 struct enable_trigger_data *test_enable_data; 1798 struct event_trigger_data *test; 1799 int ret = 0; 1800 1801 lockdep_assert_held(&event_mutex); 1802 1803 list_for_each_entry(test, &file->triggers, list) { 1804 test_enable_data = test->private_data; 1805 if (test_enable_data && 1806 (test->cmd_ops->trigger_type == 1807 data->cmd_ops->trigger_type) && 1808 (test_enable_data->file == enable_data->file)) { 1809 return -EEXIST; 1810 } 1811 } 1812 1813 if (data->cmd_ops->init) { 1814 ret = data->cmd_ops->init(data); 1815 if (ret < 0) 1816 return ret; 1817 } 1818 1819 list_add_rcu(&data->list, &file->triggers); 1820 1821 update_cond_flag(file); 1822 ret = trace_event_trigger_enable_disable(file, 1); 1823 if (ret < 0) { 1824 list_del_rcu(&data->list); 1825 update_cond_flag(file); 1826 } 1827 return ret; 1828 } 1829 1830 void event_enable_unregister_trigger(char *glob, 1831 struct event_trigger_data *test, 1832 struct trace_event_file *file) 1833 { 1834 struct enable_trigger_data *test_enable_data = test->private_data; 1835 struct event_trigger_data *data = NULL, *iter; 1836 struct enable_trigger_data *enable_data; 1837 1838 lockdep_assert_held(&event_mutex); 1839 1840 list_for_each_entry(iter, &file->triggers, list) { 1841 enable_data = iter->private_data; 1842 if (enable_data && 1843 (iter->cmd_ops->trigger_type == 1844 test->cmd_ops->trigger_type) && 1845 (enable_data->file == test_enable_data->file)) { 1846 data = iter; 1847 list_del_rcu(&data->list); 1848 trace_event_trigger_enable_disable(file, 0); 1849 update_cond_flag(file); 1850 break; 1851 } 1852 } 1853 1854 if (data && data->cmd_ops->free) 1855 data->cmd_ops->free(data); 1856 } 1857 1858 static struct event_command trigger_enable_cmd = { 1859 .name = ENABLE_EVENT_STR, 1860 .trigger_type = ETT_EVENT_ENABLE, 1861 .parse = event_enable_trigger_parse, 1862 .reg = event_enable_register_trigger, 1863 .unreg = event_enable_unregister_trigger, 1864 .set_filter = set_trigger_filter, 1865 .trigger = event_enable_trigger, 1866 .count_func = event_enable_count_func, 1867 .print = event_enable_trigger_print, 1868 .init = event_trigger_init, 1869 .free = event_enable_trigger_free, 1870 }; 1871 1872 static struct event_command trigger_disable_cmd = { 1873 .name = DISABLE_EVENT_STR, 1874 .trigger_type = ETT_EVENT_ENABLE, 1875 .parse = event_enable_trigger_parse, 1876 .reg = event_enable_register_trigger, 1877 .unreg = event_enable_unregister_trigger, 1878 .set_filter = set_trigger_filter, 1879 .trigger = event_enable_trigger, 1880 .count_func = event_enable_count_func, 1881 .print = event_enable_trigger_print, 1882 .init = event_trigger_init, 1883 .free = event_enable_trigger_free, 1884 }; 1885 1886 static __init void unregister_trigger_enable_disable_cmds(void) 1887 { 1888 unregister_event_command(&trigger_enable_cmd); 1889 unregister_event_command(&trigger_disable_cmd); 1890 } 1891 1892 static __init int register_trigger_enable_disable_cmds(void) 1893 { 1894 int ret; 1895 1896 ret = register_event_command(&trigger_enable_cmd); 1897 if (WARN_ON(ret < 0)) 1898 return ret; 1899 ret = register_event_command(&trigger_disable_cmd); 1900 if (WARN_ON(ret < 0)) 1901 unregister_trigger_enable_disable_cmds(); 1902 1903 return ret; 1904 } 1905 1906 static __init int register_trigger_traceon_traceoff_cmds(void) 1907 { 1908 int ret; 1909 1910 ret = register_event_command(&trigger_traceon_cmd); 1911 if (WARN_ON(ret < 0)) 1912 return ret; 1913 ret = register_event_command(&trigger_traceoff_cmd); 1914 if (WARN_ON(ret < 0)) 1915 unregister_trigger_traceon_traceoff_cmds(); 1916 1917 return ret; 1918 } 1919 1920 __init int register_trigger_cmds(void) 1921 { 1922 register_trigger_traceon_traceoff_cmds(); 1923 register_trigger_snapshot_cmd(); 1924 register_trigger_stacktrace_cmd(); 1925 register_trigger_enable_disable_cmds(); 1926 register_trigger_hist_enable_disable_cmds(); 1927 register_trigger_hist_cmd(); 1928 1929 return 0; 1930 } 1931