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