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>
28 static LIST_HEAD(trigger_commands);
29 static DEFINE_MUTEX(trigger_cmd_mutex);
31 void trigger_data_free(struct event_trigger_data *data)
33 if (data->cmd_ops->set_filter)
34 data->cmd_ops->set_filter(NULL, data, NULL);
36 synchronize_sched(); /* make sure current triggers exit before free */
41 * event_triggers_call - Call triggers associated with a trace event
42 * @file: The trace_event_file associated with the event
43 * @rec: The trace entry for the event, NULL for unconditional invocation
45 * For each trigger associated with an event, invoke the trigger
46 * function registered with the associated trigger command. If rec is
47 * non-NULL, it means that the trigger requires further processing and
48 * shouldn't be unconditionally invoked. If rec is non-NULL and the
49 * trigger has a filter associated with it, rec will checked against
50 * the filter and if the record matches the trigger will be invoked.
51 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
52 * in any case until the current event is written, the trigger
53 * function isn't invoked but the bit associated with the deferred
54 * trigger is set in the return value.
56 * Returns an enum event_trigger_type value containing a set bit for
57 * any trigger that should be deferred, ETT_NONE if nothing to defer.
59 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
61 * Return: an enum event_trigger_type value containing a set bit for
62 * any trigger that should be deferred, ETT_NONE if nothing to defer.
64 enum event_trigger_type
65 event_triggers_call(struct trace_event_file *file, void *rec)
67 struct event_trigger_data *data;
68 enum event_trigger_type tt = ETT_NONE;
69 struct event_filter *filter;
71 if (list_empty(&file->triggers))
74 list_for_each_entry_rcu(data, &file->triggers, list) {
78 data->ops->func(data, rec);
81 filter = rcu_dereference_sched(data->filter);
82 if (filter && !filter_match_preds(filter, rec))
84 if (event_command_post_trigger(data->cmd_ops)) {
85 tt |= data->cmd_ops->trigger_type;
88 data->ops->func(data, rec);
92 EXPORT_SYMBOL_GPL(event_triggers_call);
95 * event_triggers_post_call - Call 'post_triggers' for a trace event
96 * @file: The trace_event_file associated with the event
97 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
98 * @rec: The trace entry for the event
100 * For each trigger associated with an event, invoke the trigger
101 * function registered with the associated trigger command, if the
102 * corresponding bit is set in the tt enum passed into this function.
103 * See @event_triggers_call for details on how those bits are set.
105 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
108 event_triggers_post_call(struct trace_event_file *file,
109 enum event_trigger_type tt,
112 struct event_trigger_data *data;
114 list_for_each_entry_rcu(data, &file->triggers, list) {
117 if (data->cmd_ops->trigger_type & tt)
118 data->ops->func(data, rec);
121 EXPORT_SYMBOL_GPL(event_triggers_post_call);
123 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
125 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
127 struct trace_event_file *event_file = event_file_data(m->private);
129 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;
226 struct event_command *p;
229 next = buff = skip_spaces(buff);
230 command = strsep(&next, ": \t");
232 next = skip_spaces(next);
236 command = (command[0] != '!') ? command : command + 1;
238 mutex_lock(&trigger_cmd_mutex);
239 list_for_each_entry(p, &trigger_commands, list) {
240 if (strcmp(p->name, command) == 0) {
241 ret = p->func(p, file, buff, command, next);
246 mutex_unlock(&trigger_cmd_mutex);
251 static ssize_t event_trigger_regex_write(struct file *file,
252 const char __user *ubuf,
253 size_t cnt, loff_t *ppos)
255 struct trace_event_file *event_file;
262 if (cnt >= PAGE_SIZE)
265 buf = memdup_user_nul(ubuf, cnt);
271 mutex_lock(&event_mutex);
272 event_file = event_file_data(file);
273 if (unlikely(!event_file)) {
274 mutex_unlock(&event_mutex);
278 ret = trigger_process_regex(event_file, buf);
279 mutex_unlock(&event_mutex);
291 static int event_trigger_regex_release(struct inode *inode, struct file *file)
293 mutex_lock(&event_mutex);
295 if (file->f_mode & FMODE_READ)
296 seq_release(inode, file);
298 mutex_unlock(&event_mutex);
304 event_trigger_write(struct file *filp, const char __user *ubuf,
305 size_t cnt, loff_t *ppos)
307 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
311 event_trigger_open(struct inode *inode, struct file *filp)
313 return event_trigger_regex_open(inode, filp);
317 event_trigger_release(struct inode *inode, struct file *file)
319 return event_trigger_regex_release(inode, file);
322 const struct file_operations event_trigger_fops = {
323 .open = event_trigger_open,
325 .write = event_trigger_write,
326 .llseek = tracing_lseek,
327 .release = event_trigger_release,
331 * Currently we only register event commands from __init, so mark this
334 __init int register_event_command(struct event_command *cmd)
336 struct event_command *p;
339 mutex_lock(&trigger_cmd_mutex);
340 list_for_each_entry(p, &trigger_commands, list) {
341 if (strcmp(cmd->name, p->name) == 0) {
346 list_add(&cmd->list, &trigger_commands);
348 mutex_unlock(&trigger_cmd_mutex);
354 * Currently we only unregister event commands from __init, so mark
357 __init int unregister_event_command(struct event_command *cmd)
359 struct event_command *p, *n;
362 mutex_lock(&trigger_cmd_mutex);
363 list_for_each_entry_safe(p, n, &trigger_commands, list) {
364 if (strcmp(cmd->name, p->name) == 0) {
366 list_del_init(&p->list);
371 mutex_unlock(&trigger_cmd_mutex);
377 * event_trigger_print - Generic event_trigger_ops @print implementation
378 * @name: The name of the event trigger
379 * @m: The seq_file being printed to
380 * @data: Trigger-specific data
381 * @filter_str: filter_str to print, if present
383 * Common implementation for event triggers to print themselves.
385 * Usually wrapped by a function that simply sets the @name of the
386 * trigger command and then invokes this.
388 * Return: 0 on success, errno otherwise
391 event_trigger_print(const char *name, struct seq_file *m,
392 void *data, char *filter_str)
394 long count = (long)data;
399 seq_puts(m, ":unlimited");
401 seq_printf(m, ":count=%ld", count);
404 seq_printf(m, " if %s\n", filter_str);
412 * event_trigger_init - Generic event_trigger_ops @init implementation
413 * @ops: The trigger ops associated with the trigger
414 * @data: Trigger-specific data
416 * Common implementation of event trigger initialization.
418 * Usually used directly as the @init method in event trigger
421 * Return: 0 on success, errno otherwise
423 int event_trigger_init(struct event_trigger_ops *ops,
424 struct event_trigger_data *data)
431 * event_trigger_free - Generic event_trigger_ops @free implementation
432 * @ops: The trigger ops associated with the trigger
433 * @data: Trigger-specific data
435 * Common implementation of event trigger de-initialization.
437 * Usually used directly as the @free method in event trigger
441 event_trigger_free(struct event_trigger_ops *ops,
442 struct event_trigger_data *data)
444 if (WARN_ON_ONCE(data->ref <= 0))
449 trigger_data_free(data);
452 int trace_event_trigger_enable_disable(struct trace_event_file *file,
457 if (trigger_enable) {
458 if (atomic_inc_return(&file->tm_ref) > 1)
460 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
461 ret = trace_event_enable_disable(file, 1, 1);
463 if (atomic_dec_return(&file->tm_ref) > 0)
465 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
466 ret = trace_event_enable_disable(file, 0, 1);
473 * clear_event_triggers - Clear all triggers associated with a trace array
474 * @tr: The trace array to clear
476 * For each trigger, the triggering event has its tm_ref decremented
477 * via trace_event_trigger_enable_disable(), and any associated event
478 * (in the case of enable/disable_event triggers) will have its sm_ref
479 * decremented via free()->trace_event_enable_disable(). That
480 * combination effectively reverses the soft-mode/trigger state added
481 * by trigger registration.
483 * Must be called with event_mutex held.
486 clear_event_triggers(struct trace_array *tr)
488 struct trace_event_file *file;
490 list_for_each_entry(file, &tr->events, list) {
491 struct event_trigger_data *data, *n;
492 list_for_each_entry_safe(data, n, &file->triggers, list) {
493 trace_event_trigger_enable_disable(file, 0);
494 list_del_rcu(&data->list);
496 data->ops->free(data->ops, data);
502 * update_cond_flag - Set or reset the TRIGGER_COND bit
503 * @file: The trace_event_file associated with the event
505 * If an event has triggers and any of those triggers has a filter or
506 * a post_trigger, trigger invocation needs to be deferred until after
507 * the current event has logged its data, and the event should have
508 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
511 void update_cond_flag(struct trace_event_file *file)
513 struct event_trigger_data *data;
514 bool set_cond = false;
516 list_for_each_entry_rcu(data, &file->triggers, list) {
517 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
518 event_command_needs_rec(data->cmd_ops)) {
525 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
527 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
531 * register_trigger - Generic event_command @reg implementation
532 * @glob: The raw string used to register the trigger
533 * @ops: The trigger ops associated with the trigger
534 * @data: Trigger-specific data to associate with the trigger
535 * @file: The trace_event_file associated with the event
537 * Common implementation for event trigger registration.
539 * Usually used directly as the @reg method in event command
542 * Return: 0 on success, errno otherwise
544 static int register_trigger(char *glob, struct event_trigger_ops *ops,
545 struct event_trigger_data *data,
546 struct trace_event_file *file)
548 struct event_trigger_data *test;
551 list_for_each_entry_rcu(test, &file->triggers, list) {
552 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
558 if (data->ops->init) {
559 ret = data->ops->init(data->ops, data);
564 list_add_rcu(&data->list, &file->triggers);
567 update_cond_flag(file);
568 if (trace_event_trigger_enable_disable(file, 1) < 0) {
569 list_del_rcu(&data->list);
570 update_cond_flag(file);
578 * unregister_trigger - Generic event_command @unreg implementation
579 * @glob: The raw string used to register the trigger
580 * @ops: The trigger ops associated with the trigger
581 * @test: Trigger-specific data used to find the trigger to remove
582 * @file: The trace_event_file associated with the event
584 * Common implementation for event trigger unregistration.
586 * Usually used directly as the @unreg method in event command
589 void unregister_trigger(char *glob, struct event_trigger_ops *ops,
590 struct event_trigger_data *test,
591 struct trace_event_file *file)
593 struct event_trigger_data *data;
594 bool unregistered = false;
596 list_for_each_entry_rcu(data, &file->triggers, list) {
597 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
599 list_del_rcu(&data->list);
600 trace_event_trigger_enable_disable(file, 0);
601 update_cond_flag(file);
606 if (unregistered && data->ops->free)
607 data->ops->free(data->ops, data);
611 * event_trigger_callback - Generic event_command @func implementation
612 * @cmd_ops: The command ops, used for trigger registration
613 * @file: The trace_event_file associated with the event
614 * @glob: The raw string used to register the trigger
615 * @cmd: The cmd portion of the string used to register the trigger
616 * @param: The params portion of the string used to register the trigger
618 * Common implementation for event command parsing and trigger
621 * Usually used directly as the @func method in event command
624 * Return: 0 on success, errno otherwise
627 event_trigger_callback(struct event_command *cmd_ops,
628 struct trace_event_file *file,
629 char *glob, char *cmd, char *param)
631 struct event_trigger_data *trigger_data;
632 struct event_trigger_ops *trigger_ops;
633 char *trigger = NULL;
637 /* separate the trigger from the filter (t:n [if filter]) */
638 if (param && isdigit(param[0])) {
639 trigger = strsep(¶m, " \t");
641 param = skip_spaces(param);
647 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
650 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
654 trigger_data->count = -1;
655 trigger_data->ops = trigger_ops;
656 trigger_data->cmd_ops = cmd_ops;
657 INIT_LIST_HEAD(&trigger_data->list);
658 INIT_LIST_HEAD(&trigger_data->named_list);
660 if (glob[0] == '!') {
661 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
668 number = strsep(&trigger, ":");
675 * We use the callback data field (which is a pointer)
678 ret = kstrtoul(number, 0, &trigger_data->count);
683 if (!param) /* if param is non-empty, it's supposed to be a filter */
686 if (!cmd_ops->set_filter)
689 ret = cmd_ops->set_filter(param, trigger_data, file);
694 /* Up the trigger_data count to make sure reg doesn't free it on failure */
695 event_trigger_init(trigger_ops, trigger_data);
696 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
698 * The above returns on success the # of functions enabled,
699 * but if it didn't find any functions it returns zero.
700 * Consider no functions a failure too.
703 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
708 /* Down the counter of trigger_data or free it if not used anymore */
709 event_trigger_free(trigger_ops, trigger_data);
714 if (cmd_ops->set_filter)
715 cmd_ops->set_filter(NULL, trigger_data, NULL);
721 * set_trigger_filter - Generic event_command @set_filter implementation
722 * @filter_str: The filter string for the trigger, NULL to remove filter
723 * @trigger_data: Trigger-specific data
724 * @file: The trace_event_file associated with the event
726 * Common implementation for event command filter parsing and filter
729 * Usually used directly as the @set_filter method in event command
732 * Also used to remove a filter (if filter_str = NULL).
734 * Return: 0 on success, errno otherwise
736 int set_trigger_filter(char *filter_str,
737 struct event_trigger_data *trigger_data,
738 struct trace_event_file *file)
740 struct event_trigger_data *data = trigger_data;
741 struct event_filter *filter = NULL, *tmp;
745 if (!filter_str) /* clear the current filter */
748 s = strsep(&filter_str, " \t");
750 if (!strlen(s) || strcmp(s, "if") != 0)
756 /* The filter is for the 'trigger' event, not the triggered event */
757 ret = create_event_filter(file->event_call, filter_str, false, &filter);
759 * If create_event_filter() fails, filter still needs to be freed.
760 * Which the calling code will do with data->filter.
763 tmp = rcu_access_pointer(data->filter);
765 rcu_assign_pointer(data->filter, filter);
768 /* Make sure the call is done with the filter */
770 free_event_filter(tmp);
773 kfree(data->filter_str);
774 data->filter_str = NULL;
777 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
778 if (!data->filter_str) {
779 free_event_filter(rcu_access_pointer(data->filter));
788 static LIST_HEAD(named_triggers);
791 * find_named_trigger - Find the common named trigger associated with @name
792 * @name: The name of the set of named triggers to find the common data for
794 * Named triggers are sets of triggers that share a common set of
795 * trigger data. The first named trigger registered with a given name
796 * owns the common trigger data that the others subsequently
797 * registered with the same name will reference. This function
798 * returns the common trigger data associated with that first
799 * registered instance.
801 * Return: the common trigger data for the given named trigger on
802 * success, NULL otherwise.
804 struct event_trigger_data *find_named_trigger(const char *name)
806 struct event_trigger_data *data;
811 list_for_each_entry(data, &named_triggers, named_list) {
812 if (data->named_data)
814 if (strcmp(data->name, name) == 0)
822 * is_named_trigger - determine if a given trigger is a named trigger
823 * @test: The trigger data to test
825 * Return: true if 'test' is a named trigger, false otherwise.
827 bool is_named_trigger(struct event_trigger_data *test)
829 struct event_trigger_data *data;
831 list_for_each_entry(data, &named_triggers, named_list) {
840 * save_named_trigger - save the trigger in the named trigger list
841 * @name: The name of the named trigger set
842 * @data: The trigger data to save
844 * Return: 0 if successful, negative error otherwise.
846 int save_named_trigger(const char *name, struct event_trigger_data *data)
848 data->name = kstrdup(name, GFP_KERNEL);
852 list_add(&data->named_list, &named_triggers);
858 * del_named_trigger - delete a trigger from the named trigger list
859 * @data: The trigger data to delete
861 void del_named_trigger(struct event_trigger_data *data)
866 list_del(&data->named_list);
869 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
871 struct event_trigger_data *test;
873 list_for_each_entry(test, &named_triggers, named_list) {
874 if (strcmp(test->name, data->name) == 0) {
876 test->paused_tmp = test->paused;
879 test->paused = test->paused_tmp;
886 * pause_named_trigger - Pause all named triggers with the same name
887 * @data: The trigger data of a named trigger to pause
889 * Pauses a named trigger along with all other triggers having the
890 * same name. Because named triggers share a common set of data,
891 * pausing only one is meaningless, so pausing one named trigger needs
892 * to pause all triggers with the same name.
894 void pause_named_trigger(struct event_trigger_data *data)
896 __pause_named_trigger(data, true);
900 * unpause_named_trigger - Un-pause all named triggers with the same name
901 * @data: The trigger data of a named trigger to unpause
903 * Un-pauses a named trigger along with all other triggers having the
904 * same name. Because named triggers share a common set of data,
905 * unpausing only one is meaningless, so unpausing one named trigger
906 * needs to unpause all triggers with the same name.
908 void unpause_named_trigger(struct event_trigger_data *data)
910 __pause_named_trigger(data, false);
914 * set_named_trigger_data - Associate common named trigger data
915 * @data: The trigger data of a named trigger to unpause
917 * Named triggers are sets of triggers that share a common set of
918 * trigger data. The first named trigger registered with a given name
919 * owns the common trigger data that the others subsequently
920 * registered with the same name will reference. This function
921 * associates the common trigger data from the first trigger with the
924 void set_named_trigger_data(struct event_trigger_data *data,
925 struct event_trigger_data *named_data)
927 data->named_data = named_data;
931 traceon_trigger(struct event_trigger_data *data, void *rec)
940 traceon_count_trigger(struct event_trigger_data *data, void *rec)
948 if (data->count != -1)
955 traceoff_trigger(struct event_trigger_data *data, void *rec)
957 if (!tracing_is_on())
964 traceoff_count_trigger(struct event_trigger_data *data, void *rec)
966 if (!tracing_is_on())
972 if (data->count != -1)
979 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
980 struct event_trigger_data *data)
982 return event_trigger_print("traceon", m, (void *)data->count,
987 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
988 struct event_trigger_data *data)
990 return event_trigger_print("traceoff", m, (void *)data->count,
994 static struct event_trigger_ops traceon_trigger_ops = {
995 .func = traceon_trigger,
996 .print = traceon_trigger_print,
997 .init = event_trigger_init,
998 .free = event_trigger_free,
1001 static struct event_trigger_ops traceon_count_trigger_ops = {
1002 .func = traceon_count_trigger,
1003 .print = traceon_trigger_print,
1004 .init = event_trigger_init,
1005 .free = event_trigger_free,
1008 static struct event_trigger_ops traceoff_trigger_ops = {
1009 .func = traceoff_trigger,
1010 .print = traceoff_trigger_print,
1011 .init = event_trigger_init,
1012 .free = event_trigger_free,
1015 static struct event_trigger_ops traceoff_count_trigger_ops = {
1016 .func = traceoff_count_trigger,
1017 .print = traceoff_trigger_print,
1018 .init = event_trigger_init,
1019 .free = event_trigger_free,
1022 static struct event_trigger_ops *
1023 onoff_get_trigger_ops(char *cmd, char *param)
1025 struct event_trigger_ops *ops;
1027 /* we register both traceon and traceoff to this callback */
1028 if (strcmp(cmd, "traceon") == 0)
1029 ops = param ? &traceon_count_trigger_ops :
1030 &traceon_trigger_ops;
1032 ops = param ? &traceoff_count_trigger_ops :
1033 &traceoff_trigger_ops;
1038 static struct event_command trigger_traceon_cmd = {
1040 .trigger_type = ETT_TRACE_ONOFF,
1041 .func = event_trigger_callback,
1042 .reg = register_trigger,
1043 .unreg = unregister_trigger,
1044 .get_trigger_ops = onoff_get_trigger_ops,
1045 .set_filter = set_trigger_filter,
1048 static struct event_command trigger_traceoff_cmd = {
1050 .trigger_type = ETT_TRACE_ONOFF,
1051 .flags = EVENT_CMD_FL_POST_TRIGGER,
1052 .func = event_trigger_callback,
1053 .reg = register_trigger,
1054 .unreg = unregister_trigger,
1055 .get_trigger_ops = onoff_get_trigger_ops,
1056 .set_filter = set_trigger_filter,
1059 #ifdef CONFIG_TRACER_SNAPSHOT
1061 snapshot_trigger(struct event_trigger_data *data, void *rec)
1067 snapshot_count_trigger(struct event_trigger_data *data, void *rec)
1072 if (data->count != -1)
1075 snapshot_trigger(data, rec);
1079 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1080 struct event_trigger_data *data,
1081 struct trace_event_file *file)
1083 if (tracing_alloc_snapshot() != 0)
1086 return register_trigger(glob, ops, data, file);
1090 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1091 struct event_trigger_data *data)
1093 return event_trigger_print("snapshot", m, (void *)data->count,
1097 static struct event_trigger_ops snapshot_trigger_ops = {
1098 .func = snapshot_trigger,
1099 .print = snapshot_trigger_print,
1100 .init = event_trigger_init,
1101 .free = event_trigger_free,
1104 static struct event_trigger_ops snapshot_count_trigger_ops = {
1105 .func = snapshot_count_trigger,
1106 .print = snapshot_trigger_print,
1107 .init = event_trigger_init,
1108 .free = event_trigger_free,
1111 static struct event_trigger_ops *
1112 snapshot_get_trigger_ops(char *cmd, char *param)
1114 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1117 static struct event_command trigger_snapshot_cmd = {
1119 .trigger_type = ETT_SNAPSHOT,
1120 .func = event_trigger_callback,
1121 .reg = register_snapshot_trigger,
1122 .unreg = unregister_trigger,
1123 .get_trigger_ops = snapshot_get_trigger_ops,
1124 .set_filter = set_trigger_filter,
1127 static __init int register_trigger_snapshot_cmd(void)
1131 ret = register_event_command(&trigger_snapshot_cmd);
1137 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1138 #endif /* CONFIG_TRACER_SNAPSHOT */
1140 #ifdef CONFIG_STACKTRACE
1143 * stacktrace_trigger()
1144 * event_triggers_post_call()
1145 * trace_event_raw_event_xxx()
1147 #define STACK_SKIP 3
1150 stacktrace_trigger(struct event_trigger_data *data, void *rec)
1152 trace_dump_stack(STACK_SKIP);
1156 stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
1161 if (data->count != -1)
1164 stacktrace_trigger(data, rec);
1168 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1169 struct event_trigger_data *data)
1171 return event_trigger_print("stacktrace", m, (void *)data->count,
1175 static struct event_trigger_ops stacktrace_trigger_ops = {
1176 .func = stacktrace_trigger,
1177 .print = stacktrace_trigger_print,
1178 .init = event_trigger_init,
1179 .free = event_trigger_free,
1182 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1183 .func = stacktrace_count_trigger,
1184 .print = stacktrace_trigger_print,
1185 .init = event_trigger_init,
1186 .free = event_trigger_free,
1189 static struct event_trigger_ops *
1190 stacktrace_get_trigger_ops(char *cmd, char *param)
1192 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1195 static struct event_command trigger_stacktrace_cmd = {
1196 .name = "stacktrace",
1197 .trigger_type = ETT_STACKTRACE,
1198 .flags = EVENT_CMD_FL_POST_TRIGGER,
1199 .func = event_trigger_callback,
1200 .reg = register_trigger,
1201 .unreg = unregister_trigger,
1202 .get_trigger_ops = stacktrace_get_trigger_ops,
1203 .set_filter = set_trigger_filter,
1206 static __init int register_trigger_stacktrace_cmd(void)
1210 ret = register_event_command(&trigger_stacktrace_cmd);
1216 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1217 #endif /* CONFIG_STACKTRACE */
1219 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1221 unregister_event_command(&trigger_traceon_cmd);
1222 unregister_event_command(&trigger_traceoff_cmd);
1226 event_enable_trigger(struct event_trigger_data *data, void *rec)
1228 struct enable_trigger_data *enable_data = data->private_data;
1230 if (enable_data->enable)
1231 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1233 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1237 event_enable_count_trigger(struct event_trigger_data *data, void *rec)
1239 struct enable_trigger_data *enable_data = data->private_data;
1244 /* Skip if the event is in a state we want to switch to */
1245 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1248 if (data->count != -1)
1251 event_enable_trigger(data, rec);
1254 int event_enable_trigger_print(struct seq_file *m,
1255 struct event_trigger_ops *ops,
1256 struct event_trigger_data *data)
1258 struct enable_trigger_data *enable_data = data->private_data;
1260 seq_printf(m, "%s:%s:%s",
1262 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1263 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1264 enable_data->file->event_call->class->system,
1265 trace_event_name(enable_data->file->event_call));
1267 if (data->count == -1)
1268 seq_puts(m, ":unlimited");
1270 seq_printf(m, ":count=%ld", data->count);
1272 if (data->filter_str)
1273 seq_printf(m, " if %s\n", data->filter_str);
1280 void event_enable_trigger_free(struct event_trigger_ops *ops,
1281 struct event_trigger_data *data)
1283 struct enable_trigger_data *enable_data = data->private_data;
1285 if (WARN_ON_ONCE(data->ref <= 0))
1290 /* Remove the SOFT_MODE flag */
1291 trace_event_enable_disable(enable_data->file, 0, 1);
1292 module_put(enable_data->file->event_call->mod);
1293 trigger_data_free(data);
1298 static struct event_trigger_ops event_enable_trigger_ops = {
1299 .func = event_enable_trigger,
1300 .print = event_enable_trigger_print,
1301 .init = event_trigger_init,
1302 .free = event_enable_trigger_free,
1305 static struct event_trigger_ops event_enable_count_trigger_ops = {
1306 .func = event_enable_count_trigger,
1307 .print = event_enable_trigger_print,
1308 .init = event_trigger_init,
1309 .free = event_enable_trigger_free,
1312 static struct event_trigger_ops event_disable_trigger_ops = {
1313 .func = event_enable_trigger,
1314 .print = event_enable_trigger_print,
1315 .init = event_trigger_init,
1316 .free = event_enable_trigger_free,
1319 static struct event_trigger_ops event_disable_count_trigger_ops = {
1320 .func = event_enable_count_trigger,
1321 .print = event_enable_trigger_print,
1322 .init = event_trigger_init,
1323 .free = event_enable_trigger_free,
1326 int event_enable_trigger_func(struct event_command *cmd_ops,
1327 struct trace_event_file *file,
1328 char *glob, char *cmd, char *param)
1330 struct trace_event_file *event_enable_file;
1331 struct enable_trigger_data *enable_data;
1332 struct event_trigger_data *trigger_data;
1333 struct event_trigger_ops *trigger_ops;
1334 struct trace_array *tr = file->tr;
1346 /* separate the trigger from the filter (s:e:n [if filter]) */
1347 trigger = strsep(¶m, " \t");
1351 param = skip_spaces(param);
1356 system = strsep(&trigger, ":");
1360 event = strsep(&trigger, ":");
1363 event_enable_file = find_event_file(tr, system, event);
1364 if (!event_enable_file)
1367 #ifdef CONFIG_HIST_TRIGGERS
1368 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1369 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1371 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1372 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1374 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1376 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1379 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1383 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1385 kfree(trigger_data);
1389 trigger_data->count = -1;
1390 trigger_data->ops = trigger_ops;
1391 trigger_data->cmd_ops = cmd_ops;
1392 INIT_LIST_HEAD(&trigger_data->list);
1393 RCU_INIT_POINTER(trigger_data->filter, NULL);
1395 enable_data->hist = hist;
1396 enable_data->enable = enable;
1397 enable_data->file = event_enable_file;
1398 trigger_data->private_data = enable_data;
1400 if (glob[0] == '!') {
1401 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1402 kfree(trigger_data);
1408 /* Up the trigger_data count to make sure nothing frees it on failure */
1409 event_trigger_init(trigger_ops, trigger_data);
1412 number = strsep(&trigger, ":");
1415 if (!strlen(number))
1419 * We use the callback data field (which is a pointer)
1422 ret = kstrtoul(number, 0, &trigger_data->count);
1427 if (!param) /* if param is non-empty, it's supposed to be a filter */
1430 if (!cmd_ops->set_filter)
1433 ret = cmd_ops->set_filter(param, trigger_data, file);
1438 /* Don't let event modules unload while probe registered */
1439 ret = try_module_get(event_enable_file->event_call->mod);
1445 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1448 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1450 * The above returns on success the # of functions enabled,
1451 * but if it didn't find any functions it returns zero.
1452 * Consider no functions a failure too.
1459 /* Just return zero, not the number of enabled functions */
1461 event_trigger_free(trigger_ops, trigger_data);
1466 trace_event_enable_disable(event_enable_file, 0, 1);
1468 module_put(event_enable_file->event_call->mod);
1470 if (cmd_ops->set_filter)
1471 cmd_ops->set_filter(NULL, trigger_data, NULL);
1472 event_trigger_free(trigger_ops, trigger_data);
1477 int event_enable_register_trigger(char *glob,
1478 struct event_trigger_ops *ops,
1479 struct event_trigger_data *data,
1480 struct trace_event_file *file)
1482 struct enable_trigger_data *enable_data = data->private_data;
1483 struct enable_trigger_data *test_enable_data;
1484 struct event_trigger_data *test;
1487 list_for_each_entry_rcu(test, &file->triggers, list) {
1488 test_enable_data = test->private_data;
1489 if (test_enable_data &&
1490 (test->cmd_ops->trigger_type ==
1491 data->cmd_ops->trigger_type) &&
1492 (test_enable_data->file == enable_data->file)) {
1498 if (data->ops->init) {
1499 ret = data->ops->init(data->ops, data);
1504 list_add_rcu(&data->list, &file->triggers);
1507 update_cond_flag(file);
1508 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1509 list_del_rcu(&data->list);
1510 update_cond_flag(file);
1517 void event_enable_unregister_trigger(char *glob,
1518 struct event_trigger_ops *ops,
1519 struct event_trigger_data *test,
1520 struct trace_event_file *file)
1522 struct enable_trigger_data *test_enable_data = test->private_data;
1523 struct enable_trigger_data *enable_data;
1524 struct event_trigger_data *data;
1525 bool unregistered = false;
1527 list_for_each_entry_rcu(data, &file->triggers, list) {
1528 enable_data = data->private_data;
1530 (data->cmd_ops->trigger_type ==
1531 test->cmd_ops->trigger_type) &&
1532 (enable_data->file == test_enable_data->file)) {
1533 unregistered = true;
1534 list_del_rcu(&data->list);
1535 trace_event_trigger_enable_disable(file, 0);
1536 update_cond_flag(file);
1541 if (unregistered && data->ops->free)
1542 data->ops->free(data->ops, data);
1545 static struct event_trigger_ops *
1546 event_enable_get_trigger_ops(char *cmd, char *param)
1548 struct event_trigger_ops *ops;
1551 #ifdef CONFIG_HIST_TRIGGERS
1552 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1553 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1555 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1558 ops = param ? &event_enable_count_trigger_ops :
1559 &event_enable_trigger_ops;
1561 ops = param ? &event_disable_count_trigger_ops :
1562 &event_disable_trigger_ops;
1567 static struct event_command trigger_enable_cmd = {
1568 .name = ENABLE_EVENT_STR,
1569 .trigger_type = ETT_EVENT_ENABLE,
1570 .func = event_enable_trigger_func,
1571 .reg = event_enable_register_trigger,
1572 .unreg = event_enable_unregister_trigger,
1573 .get_trigger_ops = event_enable_get_trigger_ops,
1574 .set_filter = set_trigger_filter,
1577 static struct event_command trigger_disable_cmd = {
1578 .name = DISABLE_EVENT_STR,
1579 .trigger_type = ETT_EVENT_ENABLE,
1580 .func = event_enable_trigger_func,
1581 .reg = event_enable_register_trigger,
1582 .unreg = event_enable_unregister_trigger,
1583 .get_trigger_ops = event_enable_get_trigger_ops,
1584 .set_filter = set_trigger_filter,
1587 static __init void unregister_trigger_enable_disable_cmds(void)
1589 unregister_event_command(&trigger_enable_cmd);
1590 unregister_event_command(&trigger_disable_cmd);
1593 static __init int register_trigger_enable_disable_cmds(void)
1597 ret = register_event_command(&trigger_enable_cmd);
1598 if (WARN_ON(ret < 0))
1600 ret = register_event_command(&trigger_disable_cmd);
1601 if (WARN_ON(ret < 0))
1602 unregister_trigger_enable_disable_cmds();
1607 static __init int register_trigger_traceon_traceoff_cmds(void)
1611 ret = register_event_command(&trigger_traceon_cmd);
1612 if (WARN_ON(ret < 0))
1614 ret = register_event_command(&trigger_traceoff_cmd);
1615 if (WARN_ON(ret < 0))
1616 unregister_trigger_traceon_traceoff_cmds();
1621 __init int register_trigger_cmds(void)
1623 register_trigger_traceon_traceoff_cmds();
1624 register_trigger_snapshot_cmd();
1625 register_trigger_stacktrace_cmd();
1626 register_trigger_enable_disable_cmds();
1627 register_trigger_hist_enable_disable_cmds();
1628 register_trigger_hist_cmd();