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 componenents, 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 (file) { 1351 if (tracer_tracing_is_on(file->tr)) 1352 return; 1353 1354 tracer_tracing_on(file->tr); 1355 return; 1356 } 1357 1358 if (tracing_is_on()) 1359 return; 1360 1361 tracing_on(); 1362 } 1363 1364 static bool 1365 traceon_count_func(struct event_trigger_data *data, 1366 struct trace_buffer *buffer, void *rec, 1367 struct ring_buffer_event *event) 1368 { 1369 struct trace_event_file *file = data->private_data; 1370 1371 if (file) { 1372 if (tracer_tracing_is_on(file->tr)) 1373 return false; 1374 } else { 1375 if (tracing_is_on()) 1376 return false; 1377 } 1378 1379 if (!data->count) 1380 return false; 1381 1382 if (data->count != -1) 1383 (data->count)--; 1384 1385 return true; 1386 } 1387 1388 static void 1389 traceoff_trigger(struct event_trigger_data *data, 1390 struct trace_buffer *buffer, void *rec, 1391 struct ring_buffer_event *event) 1392 { 1393 struct trace_event_file *file = data->private_data; 1394 1395 if (file) { 1396 if (!tracer_tracing_is_on(file->tr)) 1397 return; 1398 1399 tracer_tracing_off(file->tr); 1400 return; 1401 } 1402 1403 if (!tracing_is_on()) 1404 return; 1405 1406 tracing_off(); 1407 } 1408 1409 static bool 1410 traceoff_count_func(struct event_trigger_data *data, 1411 struct trace_buffer *buffer, void *rec, 1412 struct ring_buffer_event *event) 1413 { 1414 struct trace_event_file *file = data->private_data; 1415 1416 if (file) { 1417 if (!tracer_tracing_is_on(file->tr)) 1418 return false; 1419 } else { 1420 if (!tracing_is_on()) 1421 return false; 1422 } 1423 1424 if (!data->count) 1425 return false; 1426 1427 if (data->count != -1) 1428 (data->count)--; 1429 1430 return true; 1431 } 1432 1433 static int 1434 traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1435 { 1436 return event_trigger_print("traceon", m, (void *)data->count, 1437 data->filter_str); 1438 } 1439 1440 static int 1441 traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1442 { 1443 return event_trigger_print("traceoff", m, (void *)data->count, 1444 data->filter_str); 1445 } 1446 1447 static struct event_command trigger_traceon_cmd = { 1448 .name = "traceon", 1449 .trigger_type = ETT_TRACE_ONOFF, 1450 .parse = event_trigger_parse, 1451 .reg = register_trigger, 1452 .unreg = unregister_trigger, 1453 .set_filter = set_trigger_filter, 1454 .trigger = traceon_trigger, 1455 .count_func = traceon_count_func, 1456 .print = traceon_trigger_print, 1457 .init = event_trigger_init, 1458 .free = event_trigger_free, 1459 }; 1460 1461 static struct event_command trigger_traceoff_cmd = { 1462 .name = "traceoff", 1463 .trigger_type = ETT_TRACE_ONOFF, 1464 .flags = EVENT_CMD_FL_POST_TRIGGER, 1465 .parse = event_trigger_parse, 1466 .reg = register_trigger, 1467 .unreg = unregister_trigger, 1468 .set_filter = set_trigger_filter, 1469 .trigger = traceoff_trigger, 1470 .count_func = traceoff_count_func, 1471 .print = traceoff_trigger_print, 1472 .init = event_trigger_init, 1473 .free = event_trigger_free, 1474 }; 1475 1476 #ifdef CONFIG_TRACER_SNAPSHOT 1477 static void 1478 snapshot_trigger(struct event_trigger_data *data, 1479 struct trace_buffer *buffer, void *rec, 1480 struct ring_buffer_event *event) 1481 { 1482 struct trace_event_file *file = data->private_data; 1483 1484 if (file) 1485 tracing_snapshot_instance(file->tr); 1486 else 1487 tracing_snapshot(); 1488 } 1489 1490 static int 1491 register_snapshot_trigger(char *glob, 1492 struct event_trigger_data *data, 1493 struct trace_event_file *file) 1494 { 1495 int ret = tracing_arm_snapshot(file->tr); 1496 1497 if (ret < 0) 1498 return ret; 1499 1500 ret = register_trigger(glob, data, file); 1501 if (ret < 0) 1502 tracing_disarm_snapshot(file->tr); 1503 return ret; 1504 } 1505 1506 static void unregister_snapshot_trigger(char *glob, 1507 struct event_trigger_data *data, 1508 struct trace_event_file *file) 1509 { 1510 if (try_unregister_trigger(glob, data, file)) 1511 tracing_disarm_snapshot(file->tr); 1512 } 1513 1514 static int 1515 snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1516 { 1517 return event_trigger_print("snapshot", m, (void *)data->count, 1518 data->filter_str); 1519 } 1520 1521 static struct event_command trigger_snapshot_cmd = { 1522 .name = "snapshot", 1523 .trigger_type = ETT_SNAPSHOT, 1524 .parse = event_trigger_parse, 1525 .reg = register_snapshot_trigger, 1526 .unreg = unregister_snapshot_trigger, 1527 .set_filter = set_trigger_filter, 1528 .trigger = snapshot_trigger, 1529 .count_func = event_trigger_count, 1530 .print = snapshot_trigger_print, 1531 .init = event_trigger_init, 1532 .free = event_trigger_free, 1533 }; 1534 1535 static __init int register_trigger_snapshot_cmd(void) 1536 { 1537 int ret; 1538 1539 ret = register_event_command(&trigger_snapshot_cmd); 1540 WARN_ON(ret < 0); 1541 1542 return ret; 1543 } 1544 #else 1545 static __init int register_trigger_snapshot_cmd(void) { return 0; } 1546 #endif /* CONFIG_TRACER_SNAPSHOT */ 1547 1548 #ifdef CONFIG_STACKTRACE 1549 #ifdef CONFIG_UNWINDER_ORC 1550 /* Skip 2: 1551 * event_triggers_post_call() 1552 * trace_event_raw_event_xxx() 1553 */ 1554 # define STACK_SKIP 2 1555 #else 1556 /* 1557 * Skip 4: 1558 * stacktrace_trigger() 1559 * event_triggers_post_call() 1560 * trace_event_buffer_commit() 1561 * trace_event_raw_event_xxx() 1562 */ 1563 #define STACK_SKIP 4 1564 #endif 1565 1566 static void 1567 stacktrace_trigger(struct event_trigger_data *data, 1568 struct trace_buffer *buffer, void *rec, 1569 struct ring_buffer_event *event) 1570 { 1571 struct trace_event_file *file = data->private_data; 1572 1573 if (file) 1574 __trace_stack(file->tr, tracing_gen_ctx_dec(), STACK_SKIP); 1575 else 1576 trace_dump_stack(STACK_SKIP); 1577 } 1578 1579 static int 1580 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data) 1581 { 1582 return event_trigger_print("stacktrace", m, (void *)data->count, 1583 data->filter_str); 1584 } 1585 1586 static struct event_command trigger_stacktrace_cmd = { 1587 .name = "stacktrace", 1588 .trigger_type = ETT_STACKTRACE, 1589 .flags = EVENT_CMD_FL_POST_TRIGGER, 1590 .parse = event_trigger_parse, 1591 .reg = register_trigger, 1592 .unreg = unregister_trigger, 1593 .set_filter = set_trigger_filter, 1594 .trigger = stacktrace_trigger, 1595 .count_func = event_trigger_count, 1596 .print = stacktrace_trigger_print, 1597 .init = event_trigger_init, 1598 .free = event_trigger_free, 1599 }; 1600 1601 static __init int register_trigger_stacktrace_cmd(void) 1602 { 1603 int ret; 1604 1605 ret = register_event_command(&trigger_stacktrace_cmd); 1606 WARN_ON(ret < 0); 1607 1608 return ret; 1609 } 1610 #else 1611 static __init int register_trigger_stacktrace_cmd(void) { return 0; } 1612 #endif /* CONFIG_STACKTRACE */ 1613 1614 static __init void unregister_trigger_traceon_traceoff_cmds(void) 1615 { 1616 unregister_event_command(&trigger_traceon_cmd); 1617 unregister_event_command(&trigger_traceoff_cmd); 1618 } 1619 1620 static void 1621 event_enable_trigger(struct event_trigger_data *data, 1622 struct trace_buffer *buffer, void *rec, 1623 struct ring_buffer_event *event) 1624 { 1625 struct enable_trigger_data *enable_data = data->private_data; 1626 1627 if (enable_data->enable) 1628 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1629 else 1630 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1631 } 1632 1633 static bool 1634 event_enable_count_func(struct event_trigger_data *data, 1635 struct trace_buffer *buffer, void *rec, 1636 struct ring_buffer_event *event) 1637 { 1638 struct enable_trigger_data *enable_data = data->private_data; 1639 1640 if (!data->count) 1641 return false; 1642 1643 /* Skip if the event is in a state we want to switch to */ 1644 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 1645 return false; 1646 1647 if (data->count != -1) 1648 (data->count)--; 1649 1650 return true; 1651 } 1652 1653 int event_enable_trigger_print(struct seq_file *m, 1654 struct event_trigger_data *data) 1655 { 1656 struct enable_trigger_data *enable_data = data->private_data; 1657 1658 seq_printf(m, "%s:%s:%s", 1659 enable_data->hist ? 1660 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) : 1661 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR), 1662 enable_data->file->event_call->class->system, 1663 trace_event_name(enable_data->file->event_call)); 1664 1665 if (data->count == -1) 1666 seq_puts(m, ":unlimited"); 1667 else 1668 seq_printf(m, ":count=%ld", data->count); 1669 1670 if (data->filter_str) 1671 seq_printf(m, " if %s\n", data->filter_str); 1672 else 1673 seq_putc(m, '\n'); 1674 1675 return 0; 1676 } 1677 1678 void event_enable_trigger_free(struct event_trigger_data *data) 1679 { 1680 struct enable_trigger_data *enable_data = data->private_data; 1681 1682 if (WARN_ON_ONCE(data->ref <= 0)) 1683 return; 1684 1685 data->ref--; 1686 if (!data->ref) { 1687 /* Remove the SOFT_MODE flag */ 1688 trace_event_enable_disable(enable_data->file, 0, 1); 1689 trace_event_put_ref(enable_data->file->event_call); 1690 trigger_data_free(data); 1691 kfree(enable_data); 1692 } 1693 } 1694 1695 int event_enable_trigger_parse(struct event_command *cmd_ops, 1696 struct trace_event_file *file, 1697 char *glob, char *cmd, char *param_and_filter) 1698 { 1699 struct trace_event_file *event_enable_file; 1700 struct enable_trigger_data *enable_data; 1701 struct event_trigger_data *trigger_data; 1702 struct trace_array *tr = file->tr; 1703 char *param, *filter; 1704 bool enable, remove; 1705 const char *system; 1706 const char *event; 1707 bool hist = false; 1708 int ret; 1709 1710 remove = event_trigger_check_remove(glob); 1711 1712 if (event_trigger_empty_param(param_and_filter)) 1713 return -EINVAL; 1714 1715 ret = event_trigger_separate_filter(param_and_filter, ¶m, &filter, true); 1716 if (ret) 1717 return ret; 1718 1719 system = strsep(¶m, ":"); 1720 if (!param) 1721 return -EINVAL; 1722 1723 event = strsep(¶m, ":"); 1724 1725 ret = -EINVAL; 1726 event_enable_file = find_event_file(tr, system, event); 1727 if (!event_enable_file) 1728 return ret; 1729 1730 #ifdef CONFIG_HIST_TRIGGERS 1731 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) || 1732 (strcmp(cmd, DISABLE_HIST_STR) == 0)); 1733 1734 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) || 1735 (strcmp(cmd, ENABLE_HIST_STR) == 0)); 1736 #else 1737 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 1738 #endif 1739 ret = -ENOMEM; 1740 1741 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL); 1742 if (!enable_data) 1743 return ret; 1744 1745 enable_data->hist = hist; 1746 enable_data->enable = enable; 1747 enable_data->file = event_enable_file; 1748 1749 trigger_data = trigger_data_alloc(cmd_ops, cmd, param, enable_data); 1750 if (!trigger_data) { 1751 kfree(enable_data); 1752 return ret; 1753 } 1754 1755 if (remove) { 1756 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data); 1757 kfree(trigger_data); 1758 kfree(enable_data); 1759 ret = 0; 1760 return ret; 1761 } 1762 1763 /* Up the trigger_data count to make sure nothing frees it on failure */ 1764 event_trigger_init(trigger_data); 1765 1766 ret = event_trigger_parse_num(param, trigger_data); 1767 if (ret) 1768 goto out_free; 1769 1770 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data); 1771 if (ret < 0) 1772 goto out_free; 1773 1774 /* Don't let event modules unload while probe registered */ 1775 ret = trace_event_try_get_ref(event_enable_file->event_call); 1776 if (!ret) { 1777 ret = -EBUSY; 1778 goto out_free; 1779 } 1780 1781 ret = trace_event_enable_disable(event_enable_file, 1, 1); 1782 if (ret < 0) 1783 goto out_put; 1784 1785 ret = event_trigger_register(cmd_ops, file, glob, trigger_data); 1786 if (ret) 1787 goto out_disable; 1788 1789 event_trigger_free(trigger_data); 1790 return ret; 1791 out_disable: 1792 trace_event_enable_disable(event_enable_file, 0, 1); 1793 out_put: 1794 trace_event_put_ref(event_enable_file->event_call); 1795 out_free: 1796 event_trigger_reset_filter(cmd_ops, trigger_data); 1797 event_trigger_free(trigger_data); 1798 kfree(enable_data); 1799 1800 return ret; 1801 } 1802 1803 int event_enable_register_trigger(char *glob, 1804 struct event_trigger_data *data, 1805 struct trace_event_file *file) 1806 { 1807 struct enable_trigger_data *enable_data = data->private_data; 1808 struct enable_trigger_data *test_enable_data; 1809 struct event_trigger_data *test; 1810 int ret = 0; 1811 1812 lockdep_assert_held(&event_mutex); 1813 1814 list_for_each_entry(test, &file->triggers, list) { 1815 test_enable_data = test->private_data; 1816 if (test_enable_data && 1817 (test->cmd_ops->trigger_type == 1818 data->cmd_ops->trigger_type) && 1819 (test_enable_data->file == enable_data->file)) { 1820 return -EEXIST; 1821 } 1822 } 1823 1824 if (data->cmd_ops->init) { 1825 ret = data->cmd_ops->init(data); 1826 if (ret < 0) 1827 return ret; 1828 } 1829 1830 list_add_rcu(&data->list, &file->triggers); 1831 1832 update_cond_flag(file); 1833 ret = trace_event_trigger_enable_disable(file, 1); 1834 if (ret < 0) { 1835 list_del_rcu(&data->list); 1836 update_cond_flag(file); 1837 } 1838 return ret; 1839 } 1840 1841 void event_enable_unregister_trigger(char *glob, 1842 struct event_trigger_data *test, 1843 struct trace_event_file *file) 1844 { 1845 struct enable_trigger_data *test_enable_data = test->private_data; 1846 struct event_trigger_data *data = NULL, *iter; 1847 struct enable_trigger_data *enable_data; 1848 1849 lockdep_assert_held(&event_mutex); 1850 1851 list_for_each_entry(iter, &file->triggers, list) { 1852 enable_data = iter->private_data; 1853 if (enable_data && 1854 (iter->cmd_ops->trigger_type == 1855 test->cmd_ops->trigger_type) && 1856 (enable_data->file == test_enable_data->file)) { 1857 data = iter; 1858 list_del_rcu(&data->list); 1859 trace_event_trigger_enable_disable(file, 0); 1860 update_cond_flag(file); 1861 break; 1862 } 1863 } 1864 1865 if (data && data->cmd_ops->free) 1866 data->cmd_ops->free(data); 1867 } 1868 1869 static struct event_command trigger_enable_cmd = { 1870 .name = ENABLE_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 struct event_command trigger_disable_cmd = { 1884 .name = DISABLE_EVENT_STR, 1885 .trigger_type = ETT_EVENT_ENABLE, 1886 .parse = event_enable_trigger_parse, 1887 .reg = event_enable_register_trigger, 1888 .unreg = event_enable_unregister_trigger, 1889 .set_filter = set_trigger_filter, 1890 .trigger = event_enable_trigger, 1891 .count_func = event_enable_count_func, 1892 .print = event_enable_trigger_print, 1893 .init = event_trigger_init, 1894 .free = event_enable_trigger_free, 1895 }; 1896 1897 static __init void unregister_trigger_enable_disable_cmds(void) 1898 { 1899 unregister_event_command(&trigger_enable_cmd); 1900 unregister_event_command(&trigger_disable_cmd); 1901 } 1902 1903 static __init int register_trigger_enable_disable_cmds(void) 1904 { 1905 int ret; 1906 1907 ret = register_event_command(&trigger_enable_cmd); 1908 if (WARN_ON(ret < 0)) 1909 return ret; 1910 ret = register_event_command(&trigger_disable_cmd); 1911 if (WARN_ON(ret < 0)) 1912 unregister_trigger_enable_disable_cmds(); 1913 1914 return ret; 1915 } 1916 1917 static __init int register_trigger_traceon_traceoff_cmds(void) 1918 { 1919 int ret; 1920 1921 ret = register_event_command(&trigger_traceon_cmd); 1922 if (WARN_ON(ret < 0)) 1923 return ret; 1924 ret = register_event_command(&trigger_traceoff_cmd); 1925 if (WARN_ON(ret < 0)) 1926 unregister_trigger_traceon_traceoff_cmds(); 1927 1928 return ret; 1929 } 1930 1931 __init int register_trigger_cmds(void) 1932 { 1933 register_trigger_traceon_traceoff_cmds(); 1934 register_trigger_snapshot_cmd(); 1935 register_trigger_stacktrace_cmd(); 1936 register_trigger_enable_disable_cmds(); 1937 register_trigger_hist_enable_disable_cmds(); 1938 register_trigger_hist_cmd(); 1939 1940 return 0; 1941 } 1942