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