1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_trigger - trace event triggers
5 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
8 #include <linux/module.h>
9 #include <linux/ctype.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/rculist.h>
16 static LIST_HEAD(trigger_commands);
17 static DEFINE_MUTEX(trigger_cmd_mutex);
19 void trigger_data_free(struct event_trigger_data *data)
21 if (data->cmd_ops->set_filter)
22 data->cmd_ops->set_filter(NULL, data, NULL);
24 /* make sure current triggers exit before free */
25 tracepoint_synchronize_unregister();
31 * event_triggers_call - Call triggers associated with a trace event
32 * @file: The trace_event_file associated with the event
33 * @rec: The trace entry for the event, NULL for unconditional invocation
35 * For each trigger associated with an event, invoke the trigger
36 * function registered with the associated trigger command. If rec is
37 * non-NULL, it means that the trigger requires further processing and
38 * shouldn't be unconditionally invoked. If rec is non-NULL and the
39 * trigger has a filter associated with it, rec will checked against
40 * the filter and if the record matches the trigger will be invoked.
41 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
42 * in any case until the current event is written, the trigger
43 * function isn't invoked but the bit associated with the deferred
44 * trigger is set in the return value.
46 * Returns an enum event_trigger_type value containing a set bit for
47 * any trigger that should be deferred, ETT_NONE if nothing to defer.
49 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
51 * Return: an enum event_trigger_type value containing a set bit for
52 * any trigger that should be deferred, ETT_NONE if nothing to defer.
54 enum event_trigger_type
55 event_triggers_call(struct trace_event_file *file, void *rec,
56 struct ring_buffer_event *event)
58 struct event_trigger_data *data;
59 enum event_trigger_type tt = ETT_NONE;
60 struct event_filter *filter;
62 if (list_empty(&file->triggers))
65 list_for_each_entry_rcu(data, &file->triggers, list) {
69 data->ops->func(data, rec, event);
72 filter = rcu_dereference_sched(data->filter);
73 if (filter && !filter_match_preds(filter, rec))
75 if (event_command_post_trigger(data->cmd_ops)) {
76 tt |= data->cmd_ops->trigger_type;
79 data->ops->func(data, rec, event);
83 EXPORT_SYMBOL_GPL(event_triggers_call);
86 * event_triggers_post_call - Call 'post_triggers' for a trace event
87 * @file: The trace_event_file associated with the event
88 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
90 * For each trigger associated with an event, invoke the trigger
91 * function registered with the associated trigger command, if the
92 * corresponding bit is set in the tt enum passed into this function.
93 * See @event_triggers_call for details on how those bits are set.
95 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
98 event_triggers_post_call(struct trace_event_file *file,
99 enum event_trigger_type tt)
101 struct event_trigger_data *data;
103 list_for_each_entry_rcu(data, &file->triggers, list) {
106 if (data->cmd_ops->trigger_type & tt)
107 data->ops->func(data, NULL, NULL);
110 EXPORT_SYMBOL_GPL(event_triggers_post_call);
112 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
114 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
116 struct trace_event_file *event_file = event_file_data(m->private);
118 if (t == SHOW_AVAILABLE_TRIGGERS) {
122 return seq_list_next(t, &event_file->triggers, pos);
125 static void *trigger_start(struct seq_file *m, loff_t *pos)
127 struct trace_event_file *event_file;
129 /* ->stop() is called even if ->start() fails */
130 mutex_lock(&event_mutex);
131 event_file = event_file_data(m->private);
132 if (unlikely(!event_file))
133 return ERR_PTR(-ENODEV);
135 if (list_empty(&event_file->triggers))
136 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
138 return seq_list_start(&event_file->triggers, *pos);
141 static void trigger_stop(struct seq_file *m, void *t)
143 mutex_unlock(&event_mutex);
146 static int trigger_show(struct seq_file *m, void *v)
148 struct event_trigger_data *data;
149 struct event_command *p;
151 if (v == SHOW_AVAILABLE_TRIGGERS) {
152 seq_puts(m, "# Available triggers:\n");
154 mutex_lock(&trigger_cmd_mutex);
155 list_for_each_entry_reverse(p, &trigger_commands, list)
156 seq_printf(m, " %s", p->name);
158 mutex_unlock(&trigger_cmd_mutex);
162 data = list_entry(v, struct event_trigger_data, list);
163 data->ops->print(m, data->ops, data);
168 static const struct seq_operations event_triggers_seq_ops = {
169 .start = trigger_start,
170 .next = trigger_next,
171 .stop = trigger_stop,
172 .show = trigger_show,
175 static int event_trigger_regex_open(struct inode *inode, struct file *file)
179 mutex_lock(&event_mutex);
181 if (unlikely(!event_file_data(file))) {
182 mutex_unlock(&event_mutex);
186 if ((file->f_mode & FMODE_WRITE) &&
187 (file->f_flags & O_TRUNC)) {
188 struct trace_event_file *event_file;
189 struct event_command *p;
191 event_file = event_file_data(file);
193 list_for_each_entry(p, &trigger_commands, list) {
195 p->unreg_all(event_file);
199 if (file->f_mode & FMODE_READ) {
200 ret = seq_open(file, &event_triggers_seq_ops);
202 struct seq_file *m = file->private_data;
207 mutex_unlock(&event_mutex);
212 static int trigger_process_regex(struct trace_event_file *file, char *buff)
214 char *command, *next;
215 struct event_command *p;
218 next = buff = skip_spaces(buff);
219 command = strsep(&next, ": \t");
221 next = skip_spaces(next);
225 command = (command[0] != '!') ? command : command + 1;
227 mutex_lock(&trigger_cmd_mutex);
228 list_for_each_entry(p, &trigger_commands, list) {
229 if (strcmp(p->name, command) == 0) {
230 ret = p->func(p, file, buff, command, next);
235 mutex_unlock(&trigger_cmd_mutex);
240 static ssize_t event_trigger_regex_write(struct file *file,
241 const char __user *ubuf,
242 size_t cnt, loff_t *ppos)
244 struct trace_event_file *event_file;
251 if (cnt >= PAGE_SIZE)
254 buf = memdup_user_nul(ubuf, cnt);
260 mutex_lock(&event_mutex);
261 event_file = event_file_data(file);
262 if (unlikely(!event_file)) {
263 mutex_unlock(&event_mutex);
267 ret = trigger_process_regex(event_file, buf);
268 mutex_unlock(&event_mutex);
280 static int event_trigger_regex_release(struct inode *inode, struct file *file)
282 mutex_lock(&event_mutex);
284 if (file->f_mode & FMODE_READ)
285 seq_release(inode, file);
287 mutex_unlock(&event_mutex);
293 event_trigger_write(struct file *filp, const char __user *ubuf,
294 size_t cnt, loff_t *ppos)
296 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
300 event_trigger_open(struct inode *inode, struct file *filp)
302 return event_trigger_regex_open(inode, filp);
306 event_trigger_release(struct inode *inode, struct file *file)
308 return event_trigger_regex_release(inode, file);
311 const struct file_operations event_trigger_fops = {
312 .open = event_trigger_open,
314 .write = event_trigger_write,
315 .llseek = tracing_lseek,
316 .release = event_trigger_release,
320 * Currently we only register event commands from __init, so mark this
323 __init int register_event_command(struct event_command *cmd)
325 struct event_command *p;
328 mutex_lock(&trigger_cmd_mutex);
329 list_for_each_entry(p, &trigger_commands, list) {
330 if (strcmp(cmd->name, p->name) == 0) {
335 list_add(&cmd->list, &trigger_commands);
337 mutex_unlock(&trigger_cmd_mutex);
343 * Currently we only unregister event commands from __init, so mark
346 __init int unregister_event_command(struct event_command *cmd)
348 struct event_command *p, *n;
351 mutex_lock(&trigger_cmd_mutex);
352 list_for_each_entry_safe(p, n, &trigger_commands, list) {
353 if (strcmp(cmd->name, p->name) == 0) {
355 list_del_init(&p->list);
360 mutex_unlock(&trigger_cmd_mutex);
366 * event_trigger_print - Generic event_trigger_ops @print implementation
367 * @name: The name of the event trigger
368 * @m: The seq_file being printed to
369 * @data: Trigger-specific data
370 * @filter_str: filter_str to print, if present
372 * Common implementation for event triggers to print themselves.
374 * Usually wrapped by a function that simply sets the @name of the
375 * trigger command and then invokes this.
377 * Return: 0 on success, errno otherwise
380 event_trigger_print(const char *name, struct seq_file *m,
381 void *data, char *filter_str)
383 long count = (long)data;
388 seq_puts(m, ":unlimited");
390 seq_printf(m, ":count=%ld", count);
393 seq_printf(m, " if %s\n", filter_str);
401 * event_trigger_init - Generic event_trigger_ops @init implementation
402 * @ops: The trigger ops associated with the trigger
403 * @data: Trigger-specific data
405 * Common implementation of event trigger initialization.
407 * Usually used directly as the @init method in event trigger
410 * Return: 0 on success, errno otherwise
412 int event_trigger_init(struct event_trigger_ops *ops,
413 struct event_trigger_data *data)
420 * event_trigger_free - Generic event_trigger_ops @free implementation
421 * @ops: The trigger ops associated with the trigger
422 * @data: Trigger-specific data
424 * Common implementation of event trigger de-initialization.
426 * Usually used directly as the @free method in event trigger
430 event_trigger_free(struct event_trigger_ops *ops,
431 struct event_trigger_data *data)
433 if (WARN_ON_ONCE(data->ref <= 0))
438 trigger_data_free(data);
441 int trace_event_trigger_enable_disable(struct trace_event_file *file,
446 if (trigger_enable) {
447 if (atomic_inc_return(&file->tm_ref) > 1)
449 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
450 ret = trace_event_enable_disable(file, 1, 1);
452 if (atomic_dec_return(&file->tm_ref) > 0)
454 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
455 ret = trace_event_enable_disable(file, 0, 1);
462 * clear_event_triggers - Clear all triggers associated with a trace array
463 * @tr: The trace array to clear
465 * For each trigger, the triggering event has its tm_ref decremented
466 * via trace_event_trigger_enable_disable(), and any associated event
467 * (in the case of enable/disable_event triggers) will have its sm_ref
468 * decremented via free()->trace_event_enable_disable(). That
469 * combination effectively reverses the soft-mode/trigger state added
470 * by trigger registration.
472 * Must be called with event_mutex held.
475 clear_event_triggers(struct trace_array *tr)
477 struct trace_event_file *file;
479 list_for_each_entry(file, &tr->events, list) {
480 struct event_trigger_data *data, *n;
481 list_for_each_entry_safe(data, n, &file->triggers, list) {
482 trace_event_trigger_enable_disable(file, 0);
483 list_del_rcu(&data->list);
485 data->ops->free(data->ops, data);
491 * update_cond_flag - Set or reset the TRIGGER_COND bit
492 * @file: The trace_event_file associated with the event
494 * If an event has triggers and any of those triggers has a filter or
495 * a post_trigger, trigger invocation needs to be deferred until after
496 * the current event has logged its data, and the event should have
497 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
500 void update_cond_flag(struct trace_event_file *file)
502 struct event_trigger_data *data;
503 bool set_cond = false;
505 lockdep_assert_held(&event_mutex);
507 list_for_each_entry(data, &file->triggers, list) {
508 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
509 event_command_needs_rec(data->cmd_ops)) {
516 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
518 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
522 * register_trigger - Generic event_command @reg implementation
523 * @glob: The raw string used to register the trigger
524 * @ops: The trigger ops associated with the trigger
525 * @data: Trigger-specific data to associate with the trigger
526 * @file: The trace_event_file associated with the event
528 * Common implementation for event trigger registration.
530 * Usually used directly as the @reg method in event command
533 * Return: 0 on success, errno otherwise
535 static int register_trigger(char *glob, struct event_trigger_ops *ops,
536 struct event_trigger_data *data,
537 struct trace_event_file *file)
539 struct event_trigger_data *test;
542 lockdep_assert_held(&event_mutex);
544 list_for_each_entry(test, &file->triggers, list) {
545 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
551 if (data->ops->init) {
552 ret = data->ops->init(data->ops, data);
557 list_add_rcu(&data->list, &file->triggers);
560 update_cond_flag(file);
561 if (trace_event_trigger_enable_disable(file, 1) < 0) {
562 list_del_rcu(&data->list);
563 update_cond_flag(file);
571 * unregister_trigger - Generic event_command @unreg implementation
572 * @glob: The raw string used to register the trigger
573 * @ops: The trigger ops associated with the trigger
574 * @test: Trigger-specific data used to find the trigger to remove
575 * @file: The trace_event_file associated with the event
577 * Common implementation for event trigger unregistration.
579 * Usually used directly as the @unreg method in event command
582 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
583 struct event_trigger_data *test,
584 struct trace_event_file *file)
586 struct event_trigger_data *data;
587 bool unregistered = false;
589 lockdep_assert_held(&event_mutex);
591 list_for_each_entry(data, &file->triggers, list) {
592 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
594 list_del_rcu(&data->list);
595 trace_event_trigger_enable_disable(file, 0);
596 update_cond_flag(file);
601 if (unregistered && data->ops->free)
602 data->ops->free(data->ops, data);
606 * event_trigger_callback - Generic event_command @func implementation
607 * @cmd_ops: The command ops, used for trigger registration
608 * @file: The trace_event_file associated with the event
609 * @glob: The raw string used to register the trigger
610 * @cmd: The cmd portion of the string used to register the trigger
611 * @param: The params portion of the string used to register the trigger
613 * Common implementation for event command parsing and trigger
616 * Usually used directly as the @func method in event command
619 * Return: 0 on success, errno otherwise
622 event_trigger_callback(struct event_command *cmd_ops,
623 struct trace_event_file *file,
624 char *glob, char *cmd, char *param)
626 struct event_trigger_data *trigger_data;
627 struct event_trigger_ops *trigger_ops;
628 char *trigger = NULL;
632 /* separate the trigger from the filter (t:n [if filter]) */
633 if (param && isdigit(param[0])) {
634 trigger = strsep(¶m, " \t");
636 param = skip_spaces(param);
642 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
645 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
649 trigger_data->count = -1;
650 trigger_data->ops = trigger_ops;
651 trigger_data->cmd_ops = cmd_ops;
652 trigger_data->private_data = file;
653 INIT_LIST_HEAD(&trigger_data->list);
654 INIT_LIST_HEAD(&trigger_data->named_list);
656 if (glob[0] == '!') {
657 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
664 number = strsep(&trigger, ":");
671 * We use the callback data field (which is a pointer)
674 ret = kstrtoul(number, 0, &trigger_data->count);
679 if (!param) /* if param is non-empty, it's supposed to be a filter */
682 if (!cmd_ops->set_filter)
685 ret = cmd_ops->set_filter(param, trigger_data, file);
690 /* Up the trigger_data count to make sure reg doesn't free it on failure */
691 event_trigger_init(trigger_ops, trigger_data);
692 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
694 * The above returns on success the # of functions enabled,
695 * but if it didn't find any functions it returns zero.
696 * Consider no functions a failure too.
699 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
704 /* Down the counter of trigger_data or free it if not used anymore */
705 event_trigger_free(trigger_ops, trigger_data);
710 if (cmd_ops->set_filter)
711 cmd_ops->set_filter(NULL, trigger_data, NULL);
717 * set_trigger_filter - Generic event_command @set_filter implementation
718 * @filter_str: The filter string for the trigger, NULL to remove filter
719 * @trigger_data: Trigger-specific data
720 * @file: The trace_event_file associated with the event
722 * Common implementation for event command filter parsing and filter
725 * Usually used directly as the @set_filter method in event command
728 * Also used to remove a filter (if filter_str = NULL).
730 * Return: 0 on success, errno otherwise
732 int set_trigger_filter(char *filter_str,
733 struct event_trigger_data *trigger_data,
734 struct trace_event_file *file)
736 struct event_trigger_data *data = trigger_data;
737 struct event_filter *filter = NULL, *tmp;
741 if (!filter_str) /* clear the current filter */
744 s = strsep(&filter_str, " \t");
746 if (!strlen(s) || strcmp(s, "if") != 0)
752 /* The filter is for the 'trigger' event, not the triggered event */
753 ret = create_event_filter(file->event_call, filter_str, false, &filter);
755 * If create_event_filter() fails, filter still needs to be freed.
756 * Which the calling code will do with data->filter.
759 tmp = rcu_access_pointer(data->filter);
761 rcu_assign_pointer(data->filter, filter);
764 /* Make sure the call is done with the filter */
765 tracepoint_synchronize_unregister();
766 free_event_filter(tmp);
769 kfree(data->filter_str);
770 data->filter_str = NULL;
773 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
774 if (!data->filter_str) {
775 free_event_filter(rcu_access_pointer(data->filter));
784 static LIST_HEAD(named_triggers);
787 * find_named_trigger - Find the common named trigger associated with @name
788 * @name: The name of the set of named triggers to find the common data for
790 * Named triggers are sets of triggers that share a common set of
791 * trigger data. The first named trigger registered with a given name
792 * owns the common trigger data that the others subsequently
793 * registered with the same name will reference. This function
794 * returns the common trigger data associated with that first
795 * registered instance.
797 * Return: the common trigger data for the given named trigger on
798 * success, NULL otherwise.
800 struct event_trigger_data *find_named_trigger(const char *name)
802 struct event_trigger_data *data;
807 list_for_each_entry(data, &named_triggers, named_list) {
808 if (data->named_data)
810 if (strcmp(data->name, name) == 0)
818 * is_named_trigger - determine if a given trigger is a named trigger
819 * @test: The trigger data to test
821 * Return: true if 'test' is a named trigger, false otherwise.
823 bool is_named_trigger(struct event_trigger_data *test)
825 struct event_trigger_data *data;
827 list_for_each_entry(data, &named_triggers, named_list) {
836 * save_named_trigger - save the trigger in the named trigger list
837 * @name: The name of the named trigger set
838 * @data: The trigger data to save
840 * Return: 0 if successful, negative error otherwise.
842 int save_named_trigger(const char *name, struct event_trigger_data *data)
844 data->name = kstrdup(name, GFP_KERNEL);
848 list_add(&data->named_list, &named_triggers);
854 * del_named_trigger - delete a trigger from the named trigger list
855 * @data: The trigger data to delete
857 void del_named_trigger(struct event_trigger_data *data)
862 list_del(&data->named_list);
865 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
867 struct event_trigger_data *test;
869 list_for_each_entry(test, &named_triggers, named_list) {
870 if (strcmp(test->name, data->name) == 0) {
872 test->paused_tmp = test->paused;
875 test->paused = test->paused_tmp;
882 * pause_named_trigger - Pause all named triggers with the same name
883 * @data: The trigger data of a named trigger to pause
885 * Pauses a named trigger along with all other triggers having the
886 * same name. Because named triggers share a common set of data,
887 * pausing only one is meaningless, so pausing one named trigger needs
888 * to pause all triggers with the same name.
890 void pause_named_trigger(struct event_trigger_data *data)
892 __pause_named_trigger(data, true);
896 * unpause_named_trigger - Un-pause all named triggers with the same name
897 * @data: The trigger data of a named trigger to unpause
899 * Un-pauses a named trigger along with all other triggers having the
900 * same name. Because named triggers share a common set of data,
901 * unpausing only one is meaningless, so unpausing one named trigger
902 * needs to unpause all triggers with the same name.
904 void unpause_named_trigger(struct event_trigger_data *data)
906 __pause_named_trigger(data, false);
910 * set_named_trigger_data - Associate common named trigger data
911 * @data: The trigger data of a named trigger to unpause
913 * Named triggers are sets of triggers that share a common set of
914 * trigger data. The first named trigger registered with a given name
915 * owns the common trigger data that the others subsequently
916 * registered with the same name will reference. This function
917 * associates the common trigger data from the first trigger with the
920 void set_named_trigger_data(struct event_trigger_data *data,
921 struct event_trigger_data *named_data)
923 data->named_data = named_data;
926 struct event_trigger_data *
927 get_named_trigger_data(struct event_trigger_data *data)
929 return data->named_data;
933 traceon_trigger(struct event_trigger_data *data, void *rec,
934 struct ring_buffer_event *event)
936 struct trace_event_file *file = data->private_data;
939 if (tracer_tracing_is_on(file->tr))
942 tracer_tracing_on(file->tr);
953 traceon_count_trigger(struct event_trigger_data *data, void *rec,
954 struct ring_buffer_event *event)
956 struct trace_event_file *file = data->private_data;
959 if (tracer_tracing_is_on(file->tr))
969 if (data->count != -1)
973 tracer_tracing_on(file->tr);
979 traceoff_trigger(struct event_trigger_data *data, void *rec,
980 struct ring_buffer_event *event)
982 struct trace_event_file *file = data->private_data;
985 if (!tracer_tracing_is_on(file->tr))
988 tracer_tracing_off(file->tr);
992 if (!tracing_is_on())
999 traceoff_count_trigger(struct event_trigger_data *data, void *rec,
1000 struct ring_buffer_event *event)
1002 struct trace_event_file *file = data->private_data;
1005 if (!tracer_tracing_is_on(file->tr))
1008 if (!tracing_is_on())
1015 if (data->count != -1)
1019 tracer_tracing_off(file->tr);
1025 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1026 struct event_trigger_data *data)
1028 return event_trigger_print("traceon", m, (void *)data->count,
1033 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1034 struct event_trigger_data *data)
1036 return event_trigger_print("traceoff", m, (void *)data->count,
1040 static struct event_trigger_ops traceon_trigger_ops = {
1041 .func = traceon_trigger,
1042 .print = traceon_trigger_print,
1043 .init = event_trigger_init,
1044 .free = event_trigger_free,
1047 static struct event_trigger_ops traceon_count_trigger_ops = {
1048 .func = traceon_count_trigger,
1049 .print = traceon_trigger_print,
1050 .init = event_trigger_init,
1051 .free = event_trigger_free,
1054 static struct event_trigger_ops traceoff_trigger_ops = {
1055 .func = traceoff_trigger,
1056 .print = traceoff_trigger_print,
1057 .init = event_trigger_init,
1058 .free = event_trigger_free,
1061 static struct event_trigger_ops traceoff_count_trigger_ops = {
1062 .func = traceoff_count_trigger,
1063 .print = traceoff_trigger_print,
1064 .init = event_trigger_init,
1065 .free = event_trigger_free,
1068 static struct event_trigger_ops *
1069 onoff_get_trigger_ops(char *cmd, char *param)
1071 struct event_trigger_ops *ops;
1073 /* we register both traceon and traceoff to this callback */
1074 if (strcmp(cmd, "traceon") == 0)
1075 ops = param ? &traceon_count_trigger_ops :
1076 &traceon_trigger_ops;
1078 ops = param ? &traceoff_count_trigger_ops :
1079 &traceoff_trigger_ops;
1084 static struct event_command trigger_traceon_cmd = {
1086 .trigger_type = ETT_TRACE_ONOFF,
1087 .func = event_trigger_callback,
1088 .reg = register_trigger,
1089 .unreg = unregister_trigger,
1090 .get_trigger_ops = onoff_get_trigger_ops,
1091 .set_filter = set_trigger_filter,
1094 static struct event_command trigger_traceoff_cmd = {
1096 .trigger_type = ETT_TRACE_ONOFF,
1097 .flags = EVENT_CMD_FL_POST_TRIGGER,
1098 .func = event_trigger_callback,
1099 .reg = register_trigger,
1100 .unreg = unregister_trigger,
1101 .get_trigger_ops = onoff_get_trigger_ops,
1102 .set_filter = set_trigger_filter,
1105 #ifdef CONFIG_TRACER_SNAPSHOT
1107 snapshot_trigger(struct event_trigger_data *data, void *rec,
1108 struct ring_buffer_event *event)
1110 struct trace_event_file *file = data->private_data;
1113 tracing_snapshot_instance(file->tr);
1119 snapshot_count_trigger(struct event_trigger_data *data, void *rec,
1120 struct ring_buffer_event *event)
1125 if (data->count != -1)
1128 snapshot_trigger(data, rec, event);
1132 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1133 struct event_trigger_data *data,
1134 struct trace_event_file *file)
1136 if (tracing_alloc_snapshot_instance(file->tr) != 0)
1139 return register_trigger(glob, ops, data, file);
1143 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1144 struct event_trigger_data *data)
1146 return event_trigger_print("snapshot", m, (void *)data->count,
1150 static struct event_trigger_ops snapshot_trigger_ops = {
1151 .func = snapshot_trigger,
1152 .print = snapshot_trigger_print,
1153 .init = event_trigger_init,
1154 .free = event_trigger_free,
1157 static struct event_trigger_ops snapshot_count_trigger_ops = {
1158 .func = snapshot_count_trigger,
1159 .print = snapshot_trigger_print,
1160 .init = event_trigger_init,
1161 .free = event_trigger_free,
1164 static struct event_trigger_ops *
1165 snapshot_get_trigger_ops(char *cmd, char *param)
1167 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1170 static struct event_command trigger_snapshot_cmd = {
1172 .trigger_type = ETT_SNAPSHOT,
1173 .func = event_trigger_callback,
1174 .reg = register_snapshot_trigger,
1175 .unreg = unregister_trigger,
1176 .get_trigger_ops = snapshot_get_trigger_ops,
1177 .set_filter = set_trigger_filter,
1180 static __init int register_trigger_snapshot_cmd(void)
1184 ret = register_event_command(&trigger_snapshot_cmd);
1190 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1191 #endif /* CONFIG_TRACER_SNAPSHOT */
1193 #ifdef CONFIG_STACKTRACE
1194 #ifdef CONFIG_UNWINDER_ORC
1196 * event_triggers_post_call()
1197 * trace_event_raw_event_xxx()
1199 # define STACK_SKIP 2
1203 * stacktrace_trigger()
1204 * event_triggers_post_call()
1205 * trace_event_buffer_commit()
1206 * trace_event_raw_event_xxx()
1208 #define STACK_SKIP 4
1212 stacktrace_trigger(struct event_trigger_data *data, void *rec,
1213 struct ring_buffer_event *event)
1215 struct trace_event_file *file = data->private_data;
1216 unsigned long flags;
1219 local_save_flags(flags);
1220 __trace_stack(file->tr, flags, STACK_SKIP, preempt_count());
1222 trace_dump_stack(STACK_SKIP);
1226 stacktrace_count_trigger(struct event_trigger_data *data, void *rec,
1227 struct ring_buffer_event *event)
1232 if (data->count != -1)
1235 stacktrace_trigger(data, rec, event);
1239 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1240 struct event_trigger_data *data)
1242 return event_trigger_print("stacktrace", m, (void *)data->count,
1246 static struct event_trigger_ops stacktrace_trigger_ops = {
1247 .func = stacktrace_trigger,
1248 .print = stacktrace_trigger_print,
1249 .init = event_trigger_init,
1250 .free = event_trigger_free,
1253 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1254 .func = stacktrace_count_trigger,
1255 .print = stacktrace_trigger_print,
1256 .init = event_trigger_init,
1257 .free = event_trigger_free,
1260 static struct event_trigger_ops *
1261 stacktrace_get_trigger_ops(char *cmd, char *param)
1263 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1266 static struct event_command trigger_stacktrace_cmd = {
1267 .name = "stacktrace",
1268 .trigger_type = ETT_STACKTRACE,
1269 .flags = EVENT_CMD_FL_POST_TRIGGER,
1270 .func = event_trigger_callback,
1271 .reg = register_trigger,
1272 .unreg = unregister_trigger,
1273 .get_trigger_ops = stacktrace_get_trigger_ops,
1274 .set_filter = set_trigger_filter,
1277 static __init int register_trigger_stacktrace_cmd(void)
1281 ret = register_event_command(&trigger_stacktrace_cmd);
1287 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1288 #endif /* CONFIG_STACKTRACE */
1290 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1292 unregister_event_command(&trigger_traceon_cmd);
1293 unregister_event_command(&trigger_traceoff_cmd);
1297 event_enable_trigger(struct event_trigger_data *data, void *rec,
1298 struct ring_buffer_event *event)
1300 struct enable_trigger_data *enable_data = data->private_data;
1302 if (enable_data->enable)
1303 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1305 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1309 event_enable_count_trigger(struct event_trigger_data *data, void *rec,
1310 struct ring_buffer_event *event)
1312 struct enable_trigger_data *enable_data = data->private_data;
1317 /* Skip if the event is in a state we want to switch to */
1318 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1321 if (data->count != -1)
1324 event_enable_trigger(data, rec, event);
1327 int event_enable_trigger_print(struct seq_file *m,
1328 struct event_trigger_ops *ops,
1329 struct event_trigger_data *data)
1331 struct enable_trigger_data *enable_data = data->private_data;
1333 seq_printf(m, "%s:%s:%s",
1335 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1336 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1337 enable_data->file->event_call->class->system,
1338 trace_event_name(enable_data->file->event_call));
1340 if (data->count == -1)
1341 seq_puts(m, ":unlimited");
1343 seq_printf(m, ":count=%ld", data->count);
1345 if (data->filter_str)
1346 seq_printf(m, " if %s\n", data->filter_str);
1353 void event_enable_trigger_free(struct event_trigger_ops *ops,
1354 struct event_trigger_data *data)
1356 struct enable_trigger_data *enable_data = data->private_data;
1358 if (WARN_ON_ONCE(data->ref <= 0))
1363 /* Remove the SOFT_MODE flag */
1364 trace_event_enable_disable(enable_data->file, 0, 1);
1365 module_put(enable_data->file->event_call->mod);
1366 trigger_data_free(data);
1371 static struct event_trigger_ops event_enable_trigger_ops = {
1372 .func = event_enable_trigger,
1373 .print = event_enable_trigger_print,
1374 .init = event_trigger_init,
1375 .free = event_enable_trigger_free,
1378 static struct event_trigger_ops event_enable_count_trigger_ops = {
1379 .func = event_enable_count_trigger,
1380 .print = event_enable_trigger_print,
1381 .init = event_trigger_init,
1382 .free = event_enable_trigger_free,
1385 static struct event_trigger_ops event_disable_trigger_ops = {
1386 .func = event_enable_trigger,
1387 .print = event_enable_trigger_print,
1388 .init = event_trigger_init,
1389 .free = event_enable_trigger_free,
1392 static struct event_trigger_ops event_disable_count_trigger_ops = {
1393 .func = event_enable_count_trigger,
1394 .print = event_enable_trigger_print,
1395 .init = event_trigger_init,
1396 .free = event_enable_trigger_free,
1399 int event_enable_trigger_func(struct event_command *cmd_ops,
1400 struct trace_event_file *file,
1401 char *glob, char *cmd, char *param)
1403 struct trace_event_file *event_enable_file;
1404 struct enable_trigger_data *enable_data;
1405 struct event_trigger_data *trigger_data;
1406 struct event_trigger_ops *trigger_ops;
1407 struct trace_array *tr = file->tr;
1419 /* separate the trigger from the filter (s:e:n [if filter]) */
1420 trigger = strsep(¶m, " \t");
1424 param = skip_spaces(param);
1429 system = strsep(&trigger, ":");
1433 event = strsep(&trigger, ":");
1436 event_enable_file = find_event_file(tr, system, event);
1437 if (!event_enable_file)
1440 #ifdef CONFIG_HIST_TRIGGERS
1441 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1442 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1444 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1445 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1447 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1449 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1452 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1456 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1458 kfree(trigger_data);
1462 trigger_data->count = -1;
1463 trigger_data->ops = trigger_ops;
1464 trigger_data->cmd_ops = cmd_ops;
1465 INIT_LIST_HEAD(&trigger_data->list);
1466 RCU_INIT_POINTER(trigger_data->filter, NULL);
1468 enable_data->hist = hist;
1469 enable_data->enable = enable;
1470 enable_data->file = event_enable_file;
1471 trigger_data->private_data = enable_data;
1473 if (glob[0] == '!') {
1474 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1475 kfree(trigger_data);
1481 /* Up the trigger_data count to make sure nothing frees it on failure */
1482 event_trigger_init(trigger_ops, trigger_data);
1485 number = strsep(&trigger, ":");
1488 if (!strlen(number))
1492 * We use the callback data field (which is a pointer)
1495 ret = kstrtoul(number, 0, &trigger_data->count);
1500 if (!param) /* if param is non-empty, it's supposed to be a filter */
1503 if (!cmd_ops->set_filter)
1506 ret = cmd_ops->set_filter(param, trigger_data, file);
1511 /* Don't let event modules unload while probe registered */
1512 ret = try_module_get(event_enable_file->event_call->mod);
1518 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1521 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1523 * The above returns on success the # of functions enabled,
1524 * but if it didn't find any functions it returns zero.
1525 * Consider no functions a failure too.
1532 /* Just return zero, not the number of enabled functions */
1534 event_trigger_free(trigger_ops, trigger_data);
1539 trace_event_enable_disable(event_enable_file, 0, 1);
1541 module_put(event_enable_file->event_call->mod);
1543 if (cmd_ops->set_filter)
1544 cmd_ops->set_filter(NULL, trigger_data, NULL);
1545 event_trigger_free(trigger_ops, trigger_data);
1550 int event_enable_register_trigger(char *glob,
1551 struct event_trigger_ops *ops,
1552 struct event_trigger_data *data,
1553 struct trace_event_file *file)
1555 struct enable_trigger_data *enable_data = data->private_data;
1556 struct enable_trigger_data *test_enable_data;
1557 struct event_trigger_data *test;
1560 lockdep_assert_held(&event_mutex);
1562 list_for_each_entry(test, &file->triggers, list) {
1563 test_enable_data = test->private_data;
1564 if (test_enable_data &&
1565 (test->cmd_ops->trigger_type ==
1566 data->cmd_ops->trigger_type) &&
1567 (test_enable_data->file == enable_data->file)) {
1573 if (data->ops->init) {
1574 ret = data->ops->init(data->ops, data);
1579 list_add_rcu(&data->list, &file->triggers);
1582 update_cond_flag(file);
1583 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1584 list_del_rcu(&data->list);
1585 update_cond_flag(file);
1592 void event_enable_unregister_trigger(char *glob,
1593 struct event_trigger_ops *ops,
1594 struct event_trigger_data *test,
1595 struct trace_event_file *file)
1597 struct enable_trigger_data *test_enable_data = test->private_data;
1598 struct enable_trigger_data *enable_data;
1599 struct event_trigger_data *data;
1600 bool unregistered = false;
1602 lockdep_assert_held(&event_mutex);
1604 list_for_each_entry(data, &file->triggers, list) {
1605 enable_data = data->private_data;
1607 (data->cmd_ops->trigger_type ==
1608 test->cmd_ops->trigger_type) &&
1609 (enable_data->file == test_enable_data->file)) {
1610 unregistered = true;
1611 list_del_rcu(&data->list);
1612 trace_event_trigger_enable_disable(file, 0);
1613 update_cond_flag(file);
1618 if (unregistered && data->ops->free)
1619 data->ops->free(data->ops, data);
1622 static struct event_trigger_ops *
1623 event_enable_get_trigger_ops(char *cmd, char *param)
1625 struct event_trigger_ops *ops;
1628 #ifdef CONFIG_HIST_TRIGGERS
1629 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1630 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1632 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1635 ops = param ? &event_enable_count_trigger_ops :
1636 &event_enable_trigger_ops;
1638 ops = param ? &event_disable_count_trigger_ops :
1639 &event_disable_trigger_ops;
1644 static struct event_command trigger_enable_cmd = {
1645 .name = ENABLE_EVENT_STR,
1646 .trigger_type = ETT_EVENT_ENABLE,
1647 .func = event_enable_trigger_func,
1648 .reg = event_enable_register_trigger,
1649 .unreg = event_enable_unregister_trigger,
1650 .get_trigger_ops = event_enable_get_trigger_ops,
1651 .set_filter = set_trigger_filter,
1654 static struct event_command trigger_disable_cmd = {
1655 .name = DISABLE_EVENT_STR,
1656 .trigger_type = ETT_EVENT_ENABLE,
1657 .func = event_enable_trigger_func,
1658 .reg = event_enable_register_trigger,
1659 .unreg = event_enable_unregister_trigger,
1660 .get_trigger_ops = event_enable_get_trigger_ops,
1661 .set_filter = set_trigger_filter,
1664 static __init void unregister_trigger_enable_disable_cmds(void)
1666 unregister_event_command(&trigger_enable_cmd);
1667 unregister_event_command(&trigger_disable_cmd);
1670 static __init int register_trigger_enable_disable_cmds(void)
1674 ret = register_event_command(&trigger_enable_cmd);
1675 if (WARN_ON(ret < 0))
1677 ret = register_event_command(&trigger_disable_cmd);
1678 if (WARN_ON(ret < 0))
1679 unregister_trigger_enable_disable_cmds();
1684 static __init int register_trigger_traceon_traceoff_cmds(void)
1688 ret = register_event_command(&trigger_traceon_cmd);
1689 if (WARN_ON(ret < 0))
1691 ret = register_event_command(&trigger_traceoff_cmd);
1692 if (WARN_ON(ret < 0))
1693 unregister_trigger_traceon_traceoff_cmds();
1698 __init int register_trigger_cmds(void)
1700 register_trigger_traceon_traceoff_cmds();
1701 register_trigger_snapshot_cmd();
1702 register_trigger_stacktrace_cmd();
1703 register_trigger_enable_disable_cmds();
1704 register_trigger_hist_enable_disable_cmds();
1705 register_trigger_hist_cmd();