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