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