2 * trace_events_trigger - trace event triggers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/rculist.h>
29 static LIST_HEAD(trigger_commands);
30 static DEFINE_MUTEX(trigger_cmd_mutex);
32 void trigger_data_free(struct event_trigger_data *data)
34 if (data->cmd_ops->set_filter)
35 data->cmd_ops->set_filter(NULL, data, NULL);
37 synchronize_sched(); /* make sure current triggers exit before free */
42 * event_triggers_call - Call triggers associated with a trace event
43 * @file: The trace_event_file associated with the event
44 * @rec: The trace entry for the event, NULL for unconditional invocation
46 * For each trigger associated with an event, invoke the trigger
47 * function registered with the associated trigger command. If rec is
48 * non-NULL, it means that the trigger requires further processing and
49 * shouldn't be unconditionally invoked. If rec is non-NULL and the
50 * trigger has a filter associated with it, rec will checked against
51 * the filter and if the record matches the trigger will be invoked.
52 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
53 * in any case until the current event is written, the trigger
54 * function isn't invoked but the bit associated with the deferred
55 * trigger is set in the return value.
57 * Returns an enum event_trigger_type value containing a set bit for
58 * any trigger that should be deferred, ETT_NONE if nothing to defer.
60 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
62 * Return: an enum event_trigger_type value containing a set bit for
63 * any trigger that should be deferred, ETT_NONE if nothing to defer.
65 enum event_trigger_type
66 event_triggers_call(struct trace_event_file *file, void *rec)
68 struct event_trigger_data *data;
69 enum event_trigger_type tt = ETT_NONE;
70 struct event_filter *filter;
72 if (list_empty(&file->triggers))
75 list_for_each_entry_rcu(data, &file->triggers, list) {
79 data->ops->func(data, rec);
82 filter = rcu_dereference_sched(data->filter);
83 if (filter && !filter_match_preds(filter, rec))
85 if (event_command_post_trigger(data->cmd_ops)) {
86 tt |= data->cmd_ops->trigger_type;
89 data->ops->func(data, rec);
93 EXPORT_SYMBOL_GPL(event_triggers_call);
96 * event_triggers_post_call - Call 'post_triggers' for a trace event
97 * @file: The trace_event_file associated with the event
98 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
99 * @rec: The trace entry for the event
101 * For each trigger associated with an event, invoke the trigger
102 * function registered with the associated trigger command, if the
103 * corresponding bit is set in the tt enum passed into this function.
104 * See @event_triggers_call for details on how those bits are set.
106 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
109 event_triggers_post_call(struct trace_event_file *file,
110 enum event_trigger_type tt,
113 struct event_trigger_data *data;
115 list_for_each_entry_rcu(data, &file->triggers, list) {
118 if (data->cmd_ops->trigger_type & tt)
119 data->ops->func(data, rec);
122 EXPORT_SYMBOL_GPL(event_triggers_post_call);
124 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
126 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
128 struct trace_event_file *event_file = event_file_data(m->private);
130 if (t == SHOW_AVAILABLE_TRIGGERS)
133 return seq_list_next(t, &event_file->triggers, pos);
136 static void *trigger_start(struct seq_file *m, loff_t *pos)
138 struct trace_event_file *event_file;
140 /* ->stop() is called even if ->start() fails */
141 mutex_lock(&event_mutex);
142 event_file = event_file_data(m->private);
143 if (unlikely(!event_file))
144 return ERR_PTR(-ENODEV);
146 if (list_empty(&event_file->triggers))
147 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
149 return seq_list_start(&event_file->triggers, *pos);
152 static void trigger_stop(struct seq_file *m, void *t)
154 mutex_unlock(&event_mutex);
157 static int trigger_show(struct seq_file *m, void *v)
159 struct event_trigger_data *data;
160 struct event_command *p;
162 if (v == SHOW_AVAILABLE_TRIGGERS) {
163 seq_puts(m, "# Available triggers:\n");
165 mutex_lock(&trigger_cmd_mutex);
166 list_for_each_entry_reverse(p, &trigger_commands, list)
167 seq_printf(m, " %s", p->name);
169 mutex_unlock(&trigger_cmd_mutex);
173 data = list_entry(v, struct event_trigger_data, list);
174 data->ops->print(m, data->ops, data);
179 static const struct seq_operations event_triggers_seq_ops = {
180 .start = trigger_start,
181 .next = trigger_next,
182 .stop = trigger_stop,
183 .show = trigger_show,
186 static int event_trigger_regex_open(struct inode *inode, struct file *file)
190 mutex_lock(&event_mutex);
192 if (unlikely(!event_file_data(file))) {
193 mutex_unlock(&event_mutex);
197 if ((file->f_mode & FMODE_WRITE) &&
198 (file->f_flags & O_TRUNC)) {
199 struct trace_event_file *event_file;
200 struct event_command *p;
202 event_file = event_file_data(file);
204 list_for_each_entry(p, &trigger_commands, list) {
206 p->unreg_all(event_file);
210 if (file->f_mode & FMODE_READ) {
211 ret = seq_open(file, &event_triggers_seq_ops);
213 struct seq_file *m = file->private_data;
218 mutex_unlock(&event_mutex);
223 static int trigger_process_regex(struct trace_event_file *file, char *buff)
225 char *command, *next = buff;
226 struct event_command *p;
229 command = strsep(&next, ": \t");
230 command = (command[0] != '!') ? command : command + 1;
232 mutex_lock(&trigger_cmd_mutex);
233 list_for_each_entry(p, &trigger_commands, list) {
234 if (strcmp(p->name, command) == 0) {
235 ret = p->func(p, file, buff, command, next);
240 mutex_unlock(&trigger_cmd_mutex);
245 static ssize_t event_trigger_regex_write(struct file *file,
246 const char __user *ubuf,
247 size_t cnt, loff_t *ppos)
249 struct trace_event_file *event_file;
256 if (cnt >= PAGE_SIZE)
259 buf = memdup_user_nul(ubuf, cnt);
265 mutex_lock(&event_mutex);
266 event_file = event_file_data(file);
267 if (unlikely(!event_file)) {
268 mutex_unlock(&event_mutex);
272 ret = trigger_process_regex(event_file, buf);
273 mutex_unlock(&event_mutex);
285 static int event_trigger_regex_release(struct inode *inode, struct file *file)
287 mutex_lock(&event_mutex);
289 if (file->f_mode & FMODE_READ)
290 seq_release(inode, file);
292 mutex_unlock(&event_mutex);
298 event_trigger_write(struct file *filp, const char __user *ubuf,
299 size_t cnt, loff_t *ppos)
301 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
305 event_trigger_open(struct inode *inode, struct file *filp)
307 return event_trigger_regex_open(inode, filp);
311 event_trigger_release(struct inode *inode, struct file *file)
313 return event_trigger_regex_release(inode, file);
316 const struct file_operations event_trigger_fops = {
317 .open = event_trigger_open,
319 .write = event_trigger_write,
320 .llseek = tracing_lseek,
321 .release = event_trigger_release,
325 * Currently we only register event commands from __init, so mark this
328 __init int register_event_command(struct event_command *cmd)
330 struct event_command *p;
333 mutex_lock(&trigger_cmd_mutex);
334 list_for_each_entry(p, &trigger_commands, list) {
335 if (strcmp(cmd->name, p->name) == 0) {
340 list_add(&cmd->list, &trigger_commands);
342 mutex_unlock(&trigger_cmd_mutex);
348 * Currently we only unregister event commands from __init, so mark
351 __init int unregister_event_command(struct event_command *cmd)
353 struct event_command *p, *n;
356 mutex_lock(&trigger_cmd_mutex);
357 list_for_each_entry_safe(p, n, &trigger_commands, list) {
358 if (strcmp(cmd->name, p->name) == 0) {
360 list_del_init(&p->list);
365 mutex_unlock(&trigger_cmd_mutex);
371 * event_trigger_print - Generic event_trigger_ops @print implementation
372 * @name: The name of the event trigger
373 * @m: The seq_file being printed to
374 * @data: Trigger-specific data
375 * @filter_str: filter_str to print, if present
377 * Common implementation for event triggers to print themselves.
379 * Usually wrapped by a function that simply sets the @name of the
380 * trigger command and then invokes this.
382 * Return: 0 on success, errno otherwise
385 event_trigger_print(const char *name, struct seq_file *m,
386 void *data, char *filter_str)
388 long count = (long)data;
393 seq_puts(m, ":unlimited");
395 seq_printf(m, ":count=%ld", count);
398 seq_printf(m, " if %s\n", filter_str);
406 * event_trigger_init - Generic event_trigger_ops @init implementation
407 * @ops: The trigger ops associated with the trigger
408 * @data: Trigger-specific data
410 * Common implementation of event trigger initialization.
412 * Usually used directly as the @init method in event trigger
415 * Return: 0 on success, errno otherwise
417 int event_trigger_init(struct event_trigger_ops *ops,
418 struct event_trigger_data *data)
425 * event_trigger_free - Generic event_trigger_ops @free implementation
426 * @ops: The trigger ops associated with the trigger
427 * @data: Trigger-specific data
429 * Common implementation of event trigger de-initialization.
431 * Usually used directly as the @free method in event trigger
435 event_trigger_free(struct event_trigger_ops *ops,
436 struct event_trigger_data *data)
438 if (WARN_ON_ONCE(data->ref <= 0))
443 trigger_data_free(data);
446 int trace_event_trigger_enable_disable(struct trace_event_file *file,
451 if (trigger_enable) {
452 if (atomic_inc_return(&file->tm_ref) > 1)
454 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
455 ret = trace_event_enable_disable(file, 1, 1);
457 if (atomic_dec_return(&file->tm_ref) > 0)
459 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
460 ret = trace_event_enable_disable(file, 0, 1);
467 * clear_event_triggers - Clear all triggers associated with a trace array
468 * @tr: The trace array to clear
470 * For each trigger, the triggering event has its tm_ref decremented
471 * via trace_event_trigger_enable_disable(), and any associated event
472 * (in the case of enable/disable_event triggers) will have its sm_ref
473 * decremented via free()->trace_event_enable_disable(). That
474 * combination effectively reverses the soft-mode/trigger state added
475 * by trigger registration.
477 * Must be called with event_mutex held.
480 clear_event_triggers(struct trace_array *tr)
482 struct trace_event_file *file;
484 list_for_each_entry(file, &tr->events, list) {
485 struct event_trigger_data *data, *n;
486 list_for_each_entry_safe(data, n, &file->triggers, list) {
487 trace_event_trigger_enable_disable(file, 0);
488 list_del_rcu(&data->list);
490 data->ops->free(data->ops, data);
496 * update_cond_flag - Set or reset the TRIGGER_COND bit
497 * @file: The trace_event_file associated with the event
499 * If an event has triggers and any of those triggers has a filter or
500 * a post_trigger, trigger invocation needs to be deferred until after
501 * the current event has logged its data, and the event should have
502 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
505 void update_cond_flag(struct trace_event_file *file)
507 struct event_trigger_data *data;
508 bool set_cond = false;
510 list_for_each_entry_rcu(data, &file->triggers, list) {
511 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
512 event_command_needs_rec(data->cmd_ops)) {
519 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
521 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
525 * register_trigger - Generic event_command @reg implementation
526 * @glob: The raw string used to register the trigger
527 * @ops: The trigger ops associated with the trigger
528 * @data: Trigger-specific data to associate with the trigger
529 * @file: The trace_event_file associated with the event
531 * Common implementation for event trigger registration.
533 * Usually used directly as the @reg method in event command
536 * Return: 0 on success, errno otherwise
538 static int register_trigger(char *glob, struct event_trigger_ops *ops,
539 struct event_trigger_data *data,
540 struct trace_event_file *file)
542 struct event_trigger_data *test;
545 list_for_each_entry_rcu(test, &file->triggers, list) {
546 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
552 if (data->ops->init) {
553 ret = data->ops->init(data->ops, data);
558 list_add_rcu(&data->list, &file->triggers);
561 update_cond_flag(file);
562 if (trace_event_trigger_enable_disable(file, 1) < 0) {
563 list_del_rcu(&data->list);
564 update_cond_flag(file);
572 * unregister_trigger - Generic event_command @unreg implementation
573 * @glob: The raw string used to register the trigger
574 * @ops: The trigger ops associated with the trigger
575 * @test: Trigger-specific data used to find the trigger to remove
576 * @file: The trace_event_file associated with the event
578 * Common implementation for event trigger unregistration.
580 * Usually used directly as the @unreg method in event command
583 void unregister_trigger(char *glob, struct event_trigger_ops *ops,
584 struct event_trigger_data *test,
585 struct trace_event_file *file)
587 struct event_trigger_data *data;
588 bool unregistered = false;
590 list_for_each_entry_rcu(data, &file->triggers, list) {
591 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
593 list_del_rcu(&data->list);
594 trace_event_trigger_enable_disable(file, 0);
595 update_cond_flag(file);
600 if (unregistered && data->ops->free)
601 data->ops->free(data->ops, data);
605 * event_trigger_callback - Generic event_command @func implementation
606 * @cmd_ops: The command ops, used for trigger registration
607 * @file: The trace_event_file associated with the event
608 * @glob: The raw string used to register the trigger
609 * @cmd: The cmd portion of the string used to register the trigger
610 * @param: The params portion of the string used to register the trigger
612 * Common implementation for event command parsing and trigger
615 * Usually used directly as the @func method in event command
618 * Return: 0 on success, errno otherwise
621 event_trigger_callback(struct event_command *cmd_ops,
622 struct trace_event_file *file,
623 char *glob, char *cmd, char *param)
625 struct event_trigger_data *trigger_data;
626 struct event_trigger_ops *trigger_ops;
627 char *trigger = NULL;
631 /* separate the trigger from the filter (t:n [if filter]) */
632 if (param && isdigit(param[0]))
633 trigger = strsep(¶m, " \t");
635 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
638 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
642 trigger_data->count = -1;
643 trigger_data->ops = trigger_ops;
644 trigger_data->cmd_ops = cmd_ops;
645 trigger_data->private_data = file;
646 INIT_LIST_HEAD(&trigger_data->list);
647 INIT_LIST_HEAD(&trigger_data->named_list);
649 if (glob[0] == '!') {
650 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
657 number = strsep(&trigger, ":");
664 * We use the callback data field (which is a pointer)
667 ret = kstrtoul(number, 0, &trigger_data->count);
672 if (!param) /* if param is non-empty, it's supposed to be a filter */
675 if (!cmd_ops->set_filter)
678 ret = cmd_ops->set_filter(param, trigger_data, file);
683 /* Up the trigger_data count to make sure reg doesn't free it on failure */
684 event_trigger_init(trigger_ops, trigger_data);
685 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
687 * The above returns on success the # of functions enabled,
688 * but if it didn't find any functions it returns zero.
689 * Consider no functions a failure too.
692 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
697 /* Down the counter of trigger_data or free it if not used anymore */
698 event_trigger_free(trigger_ops, trigger_data);
703 if (cmd_ops->set_filter)
704 cmd_ops->set_filter(NULL, trigger_data, NULL);
710 * set_trigger_filter - Generic event_command @set_filter implementation
711 * @filter_str: The filter string for the trigger, NULL to remove filter
712 * @trigger_data: Trigger-specific data
713 * @file: The trace_event_file associated with the event
715 * Common implementation for event command filter parsing and filter
718 * Usually used directly as the @set_filter method in event command
721 * Also used to remove a filter (if filter_str = NULL).
723 * Return: 0 on success, errno otherwise
725 int set_trigger_filter(char *filter_str,
726 struct event_trigger_data *trigger_data,
727 struct trace_event_file *file)
729 struct event_trigger_data *data = trigger_data;
730 struct event_filter *filter = NULL, *tmp;
734 if (!filter_str) /* clear the current filter */
737 s = strsep(&filter_str, " \t");
739 if (!strlen(s) || strcmp(s, "if") != 0)
745 /* The filter is for the 'trigger' event, not the triggered event */
746 ret = create_event_filter(file->event_call, filter_str, false, &filter);
750 tmp = rcu_access_pointer(data->filter);
752 rcu_assign_pointer(data->filter, filter);
755 /* Make sure the call is done with the filter */
757 free_event_filter(tmp);
760 kfree(data->filter_str);
761 data->filter_str = NULL;
764 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
765 if (!data->filter_str) {
766 free_event_filter(rcu_access_pointer(data->filter));
775 static LIST_HEAD(named_triggers);
778 * find_named_trigger - Find the common named trigger associated with @name
779 * @name: The name of the set of named triggers to find the common data for
781 * Named triggers are sets of triggers that share a common set of
782 * trigger data. The first named trigger registered with a given name
783 * owns the common trigger data that the others subsequently
784 * registered with the same name will reference. This function
785 * returns the common trigger data associated with that first
786 * registered instance.
788 * Return: the common trigger data for the given named trigger on
789 * success, NULL otherwise.
791 struct event_trigger_data *find_named_trigger(const char *name)
793 struct event_trigger_data *data;
798 list_for_each_entry(data, &named_triggers, named_list) {
799 if (data->named_data)
801 if (strcmp(data->name, name) == 0)
809 * is_named_trigger - determine if a given trigger is a named trigger
810 * @test: The trigger data to test
812 * Return: true if 'test' is a named trigger, false otherwise.
814 bool is_named_trigger(struct event_trigger_data *test)
816 struct event_trigger_data *data;
818 list_for_each_entry(data, &named_triggers, named_list) {
827 * save_named_trigger - save the trigger in the named trigger list
828 * @name: The name of the named trigger set
829 * @data: The trigger data to save
831 * Return: 0 if successful, negative error otherwise.
833 int save_named_trigger(const char *name, struct event_trigger_data *data)
835 data->name = kstrdup(name, GFP_KERNEL);
839 list_add(&data->named_list, &named_triggers);
845 * del_named_trigger - delete a trigger from the named trigger list
846 * @data: The trigger data to delete
848 void del_named_trigger(struct event_trigger_data *data)
853 list_del(&data->named_list);
856 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
858 struct event_trigger_data *test;
860 list_for_each_entry(test, &named_triggers, named_list) {
861 if (strcmp(test->name, data->name) == 0) {
863 test->paused_tmp = test->paused;
866 test->paused = test->paused_tmp;
873 * pause_named_trigger - Pause all named triggers with the same name
874 * @data: The trigger data of a named trigger to pause
876 * Pauses a named trigger along with all other triggers having the
877 * same name. Because named triggers share a common set of data,
878 * pausing only one is meaningless, so pausing one named trigger needs
879 * to pause all triggers with the same name.
881 void pause_named_trigger(struct event_trigger_data *data)
883 __pause_named_trigger(data, true);
887 * unpause_named_trigger - Un-pause all named triggers with the same name
888 * @data: The trigger data of a named trigger to unpause
890 * Un-pauses a named trigger along with all other triggers having the
891 * same name. Because named triggers share a common set of data,
892 * unpausing only one is meaningless, so unpausing one named trigger
893 * needs to unpause all triggers with the same name.
895 void unpause_named_trigger(struct event_trigger_data *data)
897 __pause_named_trigger(data, false);
901 * set_named_trigger_data - Associate common named trigger data
902 * @data: The trigger data of a named trigger to unpause
904 * Named triggers are sets of triggers that share a common set of
905 * trigger data. The first named trigger registered with a given name
906 * owns the common trigger data that the others subsequently
907 * registered with the same name will reference. This function
908 * associates the common trigger data from the first trigger with the
911 void set_named_trigger_data(struct event_trigger_data *data,
912 struct event_trigger_data *named_data)
914 data->named_data = named_data;
918 traceon_trigger(struct event_trigger_data *data, void *rec)
927 traceon_count_trigger(struct event_trigger_data *data, void *rec)
935 if (data->count != -1)
942 traceoff_trigger(struct event_trigger_data *data, void *rec)
944 if (!tracing_is_on())
951 traceoff_count_trigger(struct event_trigger_data *data, void *rec)
953 if (!tracing_is_on())
959 if (data->count != -1)
966 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
967 struct event_trigger_data *data)
969 return event_trigger_print("traceon", m, (void *)data->count,
974 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
975 struct event_trigger_data *data)
977 return event_trigger_print("traceoff", m, (void *)data->count,
981 static struct event_trigger_ops traceon_trigger_ops = {
982 .func = traceon_trigger,
983 .print = traceon_trigger_print,
984 .init = event_trigger_init,
985 .free = event_trigger_free,
988 static struct event_trigger_ops traceon_count_trigger_ops = {
989 .func = traceon_count_trigger,
990 .print = traceon_trigger_print,
991 .init = event_trigger_init,
992 .free = event_trigger_free,
995 static struct event_trigger_ops traceoff_trigger_ops = {
996 .func = traceoff_trigger,
997 .print = traceoff_trigger_print,
998 .init = event_trigger_init,
999 .free = event_trigger_free,
1002 static struct event_trigger_ops traceoff_count_trigger_ops = {
1003 .func = traceoff_count_trigger,
1004 .print = traceoff_trigger_print,
1005 .init = event_trigger_init,
1006 .free = event_trigger_free,
1009 static struct event_trigger_ops *
1010 onoff_get_trigger_ops(char *cmd, char *param)
1012 struct event_trigger_ops *ops;
1014 /* we register both traceon and traceoff to this callback */
1015 if (strcmp(cmd, "traceon") == 0)
1016 ops = param ? &traceon_count_trigger_ops :
1017 &traceon_trigger_ops;
1019 ops = param ? &traceoff_count_trigger_ops :
1020 &traceoff_trigger_ops;
1025 static struct event_command trigger_traceon_cmd = {
1027 .trigger_type = ETT_TRACE_ONOFF,
1028 .func = event_trigger_callback,
1029 .reg = register_trigger,
1030 .unreg = unregister_trigger,
1031 .get_trigger_ops = onoff_get_trigger_ops,
1032 .set_filter = set_trigger_filter,
1035 static struct event_command trigger_traceoff_cmd = {
1037 .trigger_type = ETT_TRACE_ONOFF,
1038 .flags = EVENT_CMD_FL_POST_TRIGGER,
1039 .func = event_trigger_callback,
1040 .reg = register_trigger,
1041 .unreg = unregister_trigger,
1042 .get_trigger_ops = onoff_get_trigger_ops,
1043 .set_filter = set_trigger_filter,
1046 #ifdef CONFIG_TRACER_SNAPSHOT
1048 snapshot_trigger(struct event_trigger_data *data, void *rec)
1050 struct trace_event_file *file = data->private_data;
1053 tracing_snapshot_instance(file->tr);
1059 snapshot_count_trigger(struct event_trigger_data *data, void *rec)
1064 if (data->count != -1)
1067 snapshot_trigger(data, rec);
1071 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1072 struct event_trigger_data *data,
1073 struct trace_event_file *file)
1075 int ret = register_trigger(glob, ops, data, file);
1077 if (ret > 0 && tracing_alloc_snapshot_instance(file->tr) != 0) {
1078 unregister_trigger(glob, ops, data, file);
1086 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1087 struct event_trigger_data *data)
1089 return event_trigger_print("snapshot", m, (void *)data->count,
1093 static struct event_trigger_ops snapshot_trigger_ops = {
1094 .func = snapshot_trigger,
1095 .print = snapshot_trigger_print,
1096 .init = event_trigger_init,
1097 .free = event_trigger_free,
1100 static struct event_trigger_ops snapshot_count_trigger_ops = {
1101 .func = snapshot_count_trigger,
1102 .print = snapshot_trigger_print,
1103 .init = event_trigger_init,
1104 .free = event_trigger_free,
1107 static struct event_trigger_ops *
1108 snapshot_get_trigger_ops(char *cmd, char *param)
1110 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1113 static struct event_command trigger_snapshot_cmd = {
1115 .trigger_type = ETT_SNAPSHOT,
1116 .func = event_trigger_callback,
1117 .reg = register_snapshot_trigger,
1118 .unreg = unregister_trigger,
1119 .get_trigger_ops = snapshot_get_trigger_ops,
1120 .set_filter = set_trigger_filter,
1123 static __init int register_trigger_snapshot_cmd(void)
1127 ret = register_event_command(&trigger_snapshot_cmd);
1133 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1134 #endif /* CONFIG_TRACER_SNAPSHOT */
1136 #ifdef CONFIG_STACKTRACE
1139 * stacktrace_trigger()
1140 * event_triggers_post_call()
1141 * trace_event_raw_event_xxx()
1143 #define STACK_SKIP 3
1146 stacktrace_trigger(struct event_trigger_data *data, void *rec)
1148 trace_dump_stack(STACK_SKIP);
1152 stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
1157 if (data->count != -1)
1160 stacktrace_trigger(data, rec);
1164 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1165 struct event_trigger_data *data)
1167 return event_trigger_print("stacktrace", m, (void *)data->count,
1171 static struct event_trigger_ops stacktrace_trigger_ops = {
1172 .func = stacktrace_trigger,
1173 .print = stacktrace_trigger_print,
1174 .init = event_trigger_init,
1175 .free = event_trigger_free,
1178 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1179 .func = stacktrace_count_trigger,
1180 .print = stacktrace_trigger_print,
1181 .init = event_trigger_init,
1182 .free = event_trigger_free,
1185 static struct event_trigger_ops *
1186 stacktrace_get_trigger_ops(char *cmd, char *param)
1188 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1191 static struct event_command trigger_stacktrace_cmd = {
1192 .name = "stacktrace",
1193 .trigger_type = ETT_STACKTRACE,
1194 .flags = EVENT_CMD_FL_POST_TRIGGER,
1195 .func = event_trigger_callback,
1196 .reg = register_trigger,
1197 .unreg = unregister_trigger,
1198 .get_trigger_ops = stacktrace_get_trigger_ops,
1199 .set_filter = set_trigger_filter,
1202 static __init int register_trigger_stacktrace_cmd(void)
1206 ret = register_event_command(&trigger_stacktrace_cmd);
1212 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1213 #endif /* CONFIG_STACKTRACE */
1215 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1217 unregister_event_command(&trigger_traceon_cmd);
1218 unregister_event_command(&trigger_traceoff_cmd);
1222 event_enable_trigger(struct event_trigger_data *data, void *rec)
1224 struct enable_trigger_data *enable_data = data->private_data;
1226 if (enable_data->enable)
1227 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1229 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1233 event_enable_count_trigger(struct event_trigger_data *data, void *rec)
1235 struct enable_trigger_data *enable_data = data->private_data;
1240 /* Skip if the event is in a state we want to switch to */
1241 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1244 if (data->count != -1)
1247 event_enable_trigger(data, rec);
1250 int event_enable_trigger_print(struct seq_file *m,
1251 struct event_trigger_ops *ops,
1252 struct event_trigger_data *data)
1254 struct enable_trigger_data *enable_data = data->private_data;
1256 seq_printf(m, "%s:%s:%s",
1258 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1259 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1260 enable_data->file->event_call->class->system,
1261 trace_event_name(enable_data->file->event_call));
1263 if (data->count == -1)
1264 seq_puts(m, ":unlimited");
1266 seq_printf(m, ":count=%ld", data->count);
1268 if (data->filter_str)
1269 seq_printf(m, " if %s\n", data->filter_str);
1276 void event_enable_trigger_free(struct event_trigger_ops *ops,
1277 struct event_trigger_data *data)
1279 struct enable_trigger_data *enable_data = data->private_data;
1281 if (WARN_ON_ONCE(data->ref <= 0))
1286 /* Remove the SOFT_MODE flag */
1287 trace_event_enable_disable(enable_data->file, 0, 1);
1288 module_put(enable_data->file->event_call->mod);
1289 trigger_data_free(data);
1294 static struct event_trigger_ops event_enable_trigger_ops = {
1295 .func = event_enable_trigger,
1296 .print = event_enable_trigger_print,
1297 .init = event_trigger_init,
1298 .free = event_enable_trigger_free,
1301 static struct event_trigger_ops event_enable_count_trigger_ops = {
1302 .func = event_enable_count_trigger,
1303 .print = event_enable_trigger_print,
1304 .init = event_trigger_init,
1305 .free = event_enable_trigger_free,
1308 static struct event_trigger_ops event_disable_trigger_ops = {
1309 .func = event_enable_trigger,
1310 .print = event_enable_trigger_print,
1311 .init = event_trigger_init,
1312 .free = event_enable_trigger_free,
1315 static struct event_trigger_ops event_disable_count_trigger_ops = {
1316 .func = event_enable_count_trigger,
1317 .print = event_enable_trigger_print,
1318 .init = event_trigger_init,
1319 .free = event_enable_trigger_free,
1322 int event_enable_trigger_func(struct event_command *cmd_ops,
1323 struct trace_event_file *file,
1324 char *glob, char *cmd, char *param)
1326 struct trace_event_file *event_enable_file;
1327 struct enable_trigger_data *enable_data;
1328 struct event_trigger_data *trigger_data;
1329 struct event_trigger_ops *trigger_ops;
1330 struct trace_array *tr = file->tr;
1342 /* separate the trigger from the filter (s:e:n [if filter]) */
1343 trigger = strsep(¶m, " \t");
1347 system = strsep(&trigger, ":");
1351 event = strsep(&trigger, ":");
1354 event_enable_file = find_event_file(tr, system, event);
1355 if (!event_enable_file)
1358 #ifdef CONFIG_HIST_TRIGGERS
1359 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1360 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1362 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1363 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1365 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1367 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1370 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1374 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1376 kfree(trigger_data);
1380 trigger_data->count = -1;
1381 trigger_data->ops = trigger_ops;
1382 trigger_data->cmd_ops = cmd_ops;
1383 INIT_LIST_HEAD(&trigger_data->list);
1384 RCU_INIT_POINTER(trigger_data->filter, NULL);
1386 enable_data->hist = hist;
1387 enable_data->enable = enable;
1388 enable_data->file = event_enable_file;
1389 trigger_data->private_data = enable_data;
1391 if (glob[0] == '!') {
1392 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1393 kfree(trigger_data);
1400 number = strsep(&trigger, ":");
1403 if (!strlen(number))
1407 * We use the callback data field (which is a pointer)
1410 ret = kstrtoul(number, 0, &trigger_data->count);
1415 if (!param) /* if param is non-empty, it's supposed to be a filter */
1418 if (!cmd_ops->set_filter)
1421 ret = cmd_ops->set_filter(param, trigger_data, file);
1426 /* Don't let event modules unload while probe registered */
1427 ret = try_module_get(event_enable_file->event_call->mod);
1433 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1436 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1438 * The above returns on success the # of functions enabled,
1439 * but if it didn't find any functions it returns zero.
1440 * Consider no functions a failure too.
1447 /* Just return zero, not the number of enabled functions */
1453 trace_event_enable_disable(event_enable_file, 0, 1);
1455 module_put(event_enable_file->event_call->mod);
1457 if (cmd_ops->set_filter)
1458 cmd_ops->set_filter(NULL, trigger_data, NULL);
1459 kfree(trigger_data);
1464 int event_enable_register_trigger(char *glob,
1465 struct event_trigger_ops *ops,
1466 struct event_trigger_data *data,
1467 struct trace_event_file *file)
1469 struct enable_trigger_data *enable_data = data->private_data;
1470 struct enable_trigger_data *test_enable_data;
1471 struct event_trigger_data *test;
1474 list_for_each_entry_rcu(test, &file->triggers, list) {
1475 test_enable_data = test->private_data;
1476 if (test_enable_data &&
1477 (test->cmd_ops->trigger_type ==
1478 data->cmd_ops->trigger_type) &&
1479 (test_enable_data->file == enable_data->file)) {
1485 if (data->ops->init) {
1486 ret = data->ops->init(data->ops, data);
1491 list_add_rcu(&data->list, &file->triggers);
1494 update_cond_flag(file);
1495 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1496 list_del_rcu(&data->list);
1497 update_cond_flag(file);
1504 void event_enable_unregister_trigger(char *glob,
1505 struct event_trigger_ops *ops,
1506 struct event_trigger_data *test,
1507 struct trace_event_file *file)
1509 struct enable_trigger_data *test_enable_data = test->private_data;
1510 struct enable_trigger_data *enable_data;
1511 struct event_trigger_data *data;
1512 bool unregistered = false;
1514 list_for_each_entry_rcu(data, &file->triggers, list) {
1515 enable_data = data->private_data;
1517 (data->cmd_ops->trigger_type ==
1518 test->cmd_ops->trigger_type) &&
1519 (enable_data->file == test_enable_data->file)) {
1520 unregistered = true;
1521 list_del_rcu(&data->list);
1522 trace_event_trigger_enable_disable(file, 0);
1523 update_cond_flag(file);
1528 if (unregistered && data->ops->free)
1529 data->ops->free(data->ops, data);
1532 static struct event_trigger_ops *
1533 event_enable_get_trigger_ops(char *cmd, char *param)
1535 struct event_trigger_ops *ops;
1538 #ifdef CONFIG_HIST_TRIGGERS
1539 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1540 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1542 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1545 ops = param ? &event_enable_count_trigger_ops :
1546 &event_enable_trigger_ops;
1548 ops = param ? &event_disable_count_trigger_ops :
1549 &event_disable_trigger_ops;
1554 static struct event_command trigger_enable_cmd = {
1555 .name = ENABLE_EVENT_STR,
1556 .trigger_type = ETT_EVENT_ENABLE,
1557 .func = event_enable_trigger_func,
1558 .reg = event_enable_register_trigger,
1559 .unreg = event_enable_unregister_trigger,
1560 .get_trigger_ops = event_enable_get_trigger_ops,
1561 .set_filter = set_trigger_filter,
1564 static struct event_command trigger_disable_cmd = {
1565 .name = DISABLE_EVENT_STR,
1566 .trigger_type = ETT_EVENT_ENABLE,
1567 .func = event_enable_trigger_func,
1568 .reg = event_enable_register_trigger,
1569 .unreg = event_enable_unregister_trigger,
1570 .get_trigger_ops = event_enable_get_trigger_ops,
1571 .set_filter = set_trigger_filter,
1574 static __init void unregister_trigger_enable_disable_cmds(void)
1576 unregister_event_command(&trigger_enable_cmd);
1577 unregister_event_command(&trigger_disable_cmd);
1580 static __init int register_trigger_enable_disable_cmds(void)
1584 ret = register_event_command(&trigger_enable_cmd);
1585 if (WARN_ON(ret < 0))
1587 ret = register_event_command(&trigger_disable_cmd);
1588 if (WARN_ON(ret < 0))
1589 unregister_trigger_enable_disable_cmds();
1594 static __init int register_trigger_traceon_traceoff_cmds(void)
1598 ret = register_event_command(&trigger_traceon_cmd);
1599 if (WARN_ON(ret < 0))
1601 ret = register_event_command(&trigger_traceoff_cmd);
1602 if (WARN_ON(ret < 0))
1603 unregister_trigger_traceon_traceoff_cmds();
1608 __init int register_trigger_cmds(void)
1610 register_trigger_traceon_traceoff_cmds();
1611 register_trigger_snapshot_cmd();
1612 register_trigger_stacktrace_cmd();
1613 register_trigger_enable_disable_cmds();
1614 register_trigger_hist_enable_disable_cmds();
1615 register_trigger_hist_cmd();