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/security.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/rculist.h>
17 static LIST_HEAD(trigger_commands);
18 static DEFINE_MUTEX(trigger_cmd_mutex);
20 void trigger_data_free(struct event_trigger_data *data)
22 if (data->cmd_ops->set_filter)
23 data->cmd_ops->set_filter(NULL, data, NULL);
25 /* make sure current triggers exit before free */
26 tracepoint_synchronize_unregister();
32 * event_triggers_call - Call triggers associated with a trace event
33 * @file: The trace_event_file associated with the event
34 * @rec: The trace entry for the event, NULL for unconditional invocation
36 * For each trigger associated with an event, invoke the trigger
37 * function registered with the associated trigger command. If rec is
38 * non-NULL, it means that the trigger requires further processing and
39 * shouldn't be unconditionally invoked. If rec is non-NULL and the
40 * trigger has a filter associated with it, rec will checked against
41 * the filter and if the record matches the trigger will be invoked.
42 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
43 * in any case until the current event is written, the trigger
44 * function isn't invoked but the bit associated with the deferred
45 * trigger is set in the return value.
47 * Returns an enum event_trigger_type value containing a set bit for
48 * any trigger that should be deferred, ETT_NONE if nothing to defer.
50 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
52 * Return: an enum event_trigger_type value containing a set bit for
53 * any trigger that should be deferred, ETT_NONE if nothing to defer.
55 enum event_trigger_type
56 event_triggers_call(struct trace_event_file *file,
57 struct trace_buffer *buffer, void *rec,
58 struct ring_buffer_event *event)
60 struct event_trigger_data *data;
61 enum event_trigger_type tt = ETT_NONE;
62 struct event_filter *filter;
64 if (list_empty(&file->triggers))
67 list_for_each_entry_rcu(data, &file->triggers, list) {
71 data->ops->func(data, buffer, rec, event);
74 filter = rcu_dereference_sched(data->filter);
75 if (filter && !filter_match_preds(filter, rec))
77 if (event_command_post_trigger(data->cmd_ops)) {
78 tt |= data->cmd_ops->trigger_type;
81 data->ops->func(data, buffer, rec, event);
85 EXPORT_SYMBOL_GPL(event_triggers_call);
88 * event_triggers_post_call - Call 'post_triggers' for a trace event
89 * @file: The trace_event_file associated with the event
90 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
92 * For each trigger associated with an event, invoke the trigger
93 * function registered with the associated trigger command, if the
94 * corresponding bit is set in the tt enum passed into this function.
95 * See @event_triggers_call for details on how those bits are set.
97 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
100 event_triggers_post_call(struct trace_event_file *file,
101 enum event_trigger_type tt)
103 struct event_trigger_data *data;
105 list_for_each_entry_rcu(data, &file->triggers, list) {
108 if (data->cmd_ops->trigger_type & tt)
109 data->ops->func(data, NULL, NULL, NULL);
112 EXPORT_SYMBOL_GPL(event_triggers_post_call);
114 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
116 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
118 struct trace_event_file *event_file = event_file_data(m->private);
120 if (t == SHOW_AVAILABLE_TRIGGERS) {
124 return seq_list_next(t, &event_file->triggers, pos);
127 static bool check_user_trigger(struct trace_event_file *file)
129 struct event_trigger_data *data;
131 list_for_each_entry_rcu(data, &file->triggers, list,
132 lockdep_is_held(&event_mutex)) {
133 if (data->flags & EVENT_TRIGGER_FL_PROBE)
140 static void *trigger_start(struct seq_file *m, loff_t *pos)
142 struct trace_event_file *event_file;
144 /* ->stop() is called even if ->start() fails */
145 mutex_lock(&event_mutex);
146 event_file = event_file_data(m->private);
147 if (unlikely(!event_file))
148 return ERR_PTR(-ENODEV);
150 if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
151 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
153 return seq_list_start(&event_file->triggers, *pos);
156 static void trigger_stop(struct seq_file *m, void *t)
158 mutex_unlock(&event_mutex);
161 static int trigger_show(struct seq_file *m, void *v)
163 struct event_trigger_data *data;
164 struct event_command *p;
166 if (v == SHOW_AVAILABLE_TRIGGERS) {
167 seq_puts(m, "# Available triggers:\n");
169 mutex_lock(&trigger_cmd_mutex);
170 list_for_each_entry_reverse(p, &trigger_commands, list)
171 seq_printf(m, " %s", p->name);
173 mutex_unlock(&trigger_cmd_mutex);
177 data = list_entry(v, struct event_trigger_data, list);
178 data->ops->print(m, data->ops, data);
183 static const struct seq_operations event_triggers_seq_ops = {
184 .start = trigger_start,
185 .next = trigger_next,
186 .stop = trigger_stop,
187 .show = trigger_show,
190 static int event_trigger_regex_open(struct inode *inode, struct file *file)
194 ret = security_locked_down(LOCKDOWN_TRACEFS);
198 mutex_lock(&event_mutex);
200 if (unlikely(!event_file_data(file))) {
201 mutex_unlock(&event_mutex);
205 if ((file->f_mode & FMODE_WRITE) &&
206 (file->f_flags & O_TRUNC)) {
207 struct trace_event_file *event_file;
208 struct event_command *p;
210 event_file = event_file_data(file);
212 list_for_each_entry(p, &trigger_commands, list) {
214 p->unreg_all(event_file);
218 if (file->f_mode & FMODE_READ) {
219 ret = seq_open(file, &event_triggers_seq_ops);
221 struct seq_file *m = file->private_data;
226 mutex_unlock(&event_mutex);
231 int trigger_process_regex(struct trace_event_file *file, char *buff)
233 char *command, *next;
234 struct event_command *p;
237 next = buff = skip_spaces(buff);
238 command = strsep(&next, ": \t");
240 next = skip_spaces(next);
244 command = (command[0] != '!') ? command : command + 1;
246 mutex_lock(&trigger_cmd_mutex);
247 list_for_each_entry(p, &trigger_commands, list) {
248 if (strcmp(p->name, command) == 0) {
249 ret = p->func(p, file, buff, command, next);
254 mutex_unlock(&trigger_cmd_mutex);
259 static ssize_t event_trigger_regex_write(struct file *file,
260 const char __user *ubuf,
261 size_t cnt, loff_t *ppos)
263 struct trace_event_file *event_file;
270 if (cnt >= PAGE_SIZE)
273 buf = memdup_user_nul(ubuf, cnt);
279 mutex_lock(&event_mutex);
280 event_file = event_file_data(file);
281 if (unlikely(!event_file)) {
282 mutex_unlock(&event_mutex);
286 ret = trigger_process_regex(event_file, buf);
287 mutex_unlock(&event_mutex);
299 static int event_trigger_regex_release(struct inode *inode, struct file *file)
301 mutex_lock(&event_mutex);
303 if (file->f_mode & FMODE_READ)
304 seq_release(inode, file);
306 mutex_unlock(&event_mutex);
312 event_trigger_write(struct file *filp, const char __user *ubuf,
313 size_t cnt, loff_t *ppos)
315 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
319 event_trigger_open(struct inode *inode, struct file *filp)
321 /* Checks for tracefs lockdown */
322 return event_trigger_regex_open(inode, filp);
326 event_trigger_release(struct inode *inode, struct file *file)
328 return event_trigger_regex_release(inode, file);
331 const struct file_operations event_trigger_fops = {
332 .open = event_trigger_open,
334 .write = event_trigger_write,
335 .llseek = tracing_lseek,
336 .release = event_trigger_release,
340 * Currently we only register event commands from __init, so mark this
343 __init int register_event_command(struct event_command *cmd)
345 struct event_command *p;
348 mutex_lock(&trigger_cmd_mutex);
349 list_for_each_entry(p, &trigger_commands, list) {
350 if (strcmp(cmd->name, p->name) == 0) {
355 list_add(&cmd->list, &trigger_commands);
357 mutex_unlock(&trigger_cmd_mutex);
363 * Currently we only unregister event commands from __init, so mark
366 __init int unregister_event_command(struct event_command *cmd)
368 struct event_command *p, *n;
371 mutex_lock(&trigger_cmd_mutex);
372 list_for_each_entry_safe(p, n, &trigger_commands, list) {
373 if (strcmp(cmd->name, p->name) == 0) {
375 list_del_init(&p->list);
380 mutex_unlock(&trigger_cmd_mutex);
386 * event_trigger_print - Generic event_trigger_ops @print implementation
387 * @name: The name of the event trigger
388 * @m: The seq_file being printed to
389 * @data: Trigger-specific data
390 * @filter_str: filter_str to print, if present
392 * Common implementation for event triggers to print themselves.
394 * Usually wrapped by a function that simply sets the @name of the
395 * trigger command and then invokes this.
397 * Return: 0 on success, errno otherwise
400 event_trigger_print(const char *name, struct seq_file *m,
401 void *data, char *filter_str)
403 long count = (long)data;
408 seq_puts(m, ":unlimited");
410 seq_printf(m, ":count=%ld", count);
413 seq_printf(m, " if %s\n", filter_str);
421 * event_trigger_init - Generic event_trigger_ops @init implementation
422 * @ops: The trigger ops associated with the trigger
423 * @data: Trigger-specific data
425 * Common implementation of event trigger initialization.
427 * Usually used directly as the @init method in event trigger
430 * Return: 0 on success, errno otherwise
432 int event_trigger_init(struct event_trigger_ops *ops,
433 struct event_trigger_data *data)
440 * event_trigger_free - Generic event_trigger_ops @free implementation
441 * @ops: The trigger ops associated with the trigger
442 * @data: Trigger-specific data
444 * Common implementation of event trigger de-initialization.
446 * Usually used directly as the @free method in event trigger
450 event_trigger_free(struct event_trigger_ops *ops,
451 struct event_trigger_data *data)
453 if (WARN_ON_ONCE(data->ref <= 0))
458 trigger_data_free(data);
461 int trace_event_trigger_enable_disable(struct trace_event_file *file,
466 if (trigger_enable) {
467 if (atomic_inc_return(&file->tm_ref) > 1)
469 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
470 ret = trace_event_enable_disable(file, 1, 1);
472 if (atomic_dec_return(&file->tm_ref) > 0)
474 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
475 ret = trace_event_enable_disable(file, 0, 1);
482 * clear_event_triggers - Clear all triggers associated with a trace array
483 * @tr: The trace array to clear
485 * For each trigger, the triggering event has its tm_ref decremented
486 * via trace_event_trigger_enable_disable(), and any associated event
487 * (in the case of enable/disable_event triggers) will have its sm_ref
488 * decremented via free()->trace_event_enable_disable(). That
489 * combination effectively reverses the soft-mode/trigger state added
490 * by trigger registration.
492 * Must be called with event_mutex held.
495 clear_event_triggers(struct trace_array *tr)
497 struct trace_event_file *file;
499 list_for_each_entry(file, &tr->events, list) {
500 struct event_trigger_data *data, *n;
501 list_for_each_entry_safe(data, n, &file->triggers, list) {
502 trace_event_trigger_enable_disable(file, 0);
503 list_del_rcu(&data->list);
505 data->ops->free(data->ops, data);
511 * update_cond_flag - Set or reset the TRIGGER_COND bit
512 * @file: The trace_event_file associated with the event
514 * If an event has triggers and any of those triggers has a filter or
515 * a post_trigger, trigger invocation needs to be deferred until after
516 * the current event has logged its data, and the event should have
517 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
520 void update_cond_flag(struct trace_event_file *file)
522 struct event_trigger_data *data;
523 bool set_cond = false;
525 lockdep_assert_held(&event_mutex);
527 list_for_each_entry(data, &file->triggers, list) {
528 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
529 event_command_needs_rec(data->cmd_ops)) {
536 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
538 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
542 * register_trigger - Generic event_command @reg implementation
543 * @glob: The raw string used to register the trigger
544 * @ops: The trigger ops associated with the trigger
545 * @data: Trigger-specific data to associate with the trigger
546 * @file: The trace_event_file associated with the event
548 * Common implementation for event trigger registration.
550 * Usually used directly as the @reg method in event command
553 * Return: 0 on success, errno otherwise
555 static int register_trigger(char *glob, struct event_trigger_ops *ops,
556 struct event_trigger_data *data,
557 struct trace_event_file *file)
559 struct event_trigger_data *test;
562 lockdep_assert_held(&event_mutex);
564 list_for_each_entry(test, &file->triggers, list) {
565 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
571 if (data->ops->init) {
572 ret = data->ops->init(data->ops, data);
577 list_add_rcu(&data->list, &file->triggers);
580 update_cond_flag(file);
581 if (trace_event_trigger_enable_disable(file, 1) < 0) {
582 list_del_rcu(&data->list);
583 update_cond_flag(file);
591 * unregister_trigger - Generic event_command @unreg implementation
592 * @glob: The raw string used to register the trigger
593 * @ops: The trigger ops associated with the trigger
594 * @test: Trigger-specific data used to find the trigger to remove
595 * @file: The trace_event_file associated with the event
597 * Common implementation for event trigger unregistration.
599 * Usually used directly as the @unreg method in event command
602 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
603 struct event_trigger_data *test,
604 struct trace_event_file *file)
606 struct event_trigger_data *data;
607 bool unregistered = false;
609 lockdep_assert_held(&event_mutex);
611 list_for_each_entry(data, &file->triggers, list) {
612 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
614 list_del_rcu(&data->list);
615 trace_event_trigger_enable_disable(file, 0);
616 update_cond_flag(file);
621 if (unregistered && data->ops->free)
622 data->ops->free(data->ops, data);
626 * event_trigger_callback - Generic event_command @func implementation
627 * @cmd_ops: The command ops, used for trigger registration
628 * @file: The trace_event_file associated with the event
629 * @glob: The raw string used to register the trigger
630 * @cmd: The cmd portion of the string used to register the trigger
631 * @param: The params portion of the string used to register the trigger
633 * Common implementation for event command parsing and trigger
636 * Usually used directly as the @func method in event command
639 * Return: 0 on success, errno otherwise
642 event_trigger_callback(struct event_command *cmd_ops,
643 struct trace_event_file *file,
644 char *glob, char *cmd, char *param)
646 struct event_trigger_data *trigger_data;
647 struct event_trigger_ops *trigger_ops;
648 char *trigger = NULL;
652 /* separate the trigger from the filter (t:n [if filter]) */
653 if (param && isdigit(param[0])) {
654 trigger = strsep(¶m, " \t");
656 param = skip_spaces(param);
662 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
665 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
669 trigger_data->count = -1;
670 trigger_data->ops = trigger_ops;
671 trigger_data->cmd_ops = cmd_ops;
672 trigger_data->private_data = file;
673 INIT_LIST_HEAD(&trigger_data->list);
674 INIT_LIST_HEAD(&trigger_data->named_list);
676 if (glob[0] == '!') {
677 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
684 number = strsep(&trigger, ":");
691 * We use the callback data field (which is a pointer)
694 ret = kstrtoul(number, 0, &trigger_data->count);
699 if (!param) /* if param is non-empty, it's supposed to be a filter */
702 if (!cmd_ops->set_filter)
705 ret = cmd_ops->set_filter(param, trigger_data, file);
710 /* Up the trigger_data count to make sure reg doesn't free it on failure */
711 event_trigger_init(trigger_ops, trigger_data);
712 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
714 * The above returns on success the # of functions enabled,
715 * but if it didn't find any functions it returns zero.
716 * Consider no functions a failure too.
719 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
724 /* Down the counter of trigger_data or free it if not used anymore */
725 event_trigger_free(trigger_ops, trigger_data);
730 if (cmd_ops->set_filter)
731 cmd_ops->set_filter(NULL, trigger_data, NULL);
737 * set_trigger_filter - Generic event_command @set_filter implementation
738 * @filter_str: The filter string for the trigger, NULL to remove filter
739 * @trigger_data: Trigger-specific data
740 * @file: The trace_event_file associated with the event
742 * Common implementation for event command filter parsing and filter
745 * Usually used directly as the @set_filter method in event command
748 * Also used to remove a filter (if filter_str = NULL).
750 * Return: 0 on success, errno otherwise
752 int set_trigger_filter(char *filter_str,
753 struct event_trigger_data *trigger_data,
754 struct trace_event_file *file)
756 struct event_trigger_data *data = trigger_data;
757 struct event_filter *filter = NULL, *tmp;
761 if (!filter_str) /* clear the current filter */
764 s = strsep(&filter_str, " \t");
766 if (!strlen(s) || strcmp(s, "if") != 0)
772 /* The filter is for the 'trigger' event, not the triggered event */
773 ret = create_event_filter(file->tr, file->event_call,
774 filter_str, false, &filter);
776 * If create_event_filter() fails, filter still needs to be freed.
777 * Which the calling code will do with data->filter.
780 tmp = rcu_access_pointer(data->filter);
782 rcu_assign_pointer(data->filter, filter);
785 /* Make sure the call is done with the filter */
786 tracepoint_synchronize_unregister();
787 free_event_filter(tmp);
790 kfree(data->filter_str);
791 data->filter_str = NULL;
794 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
795 if (!data->filter_str) {
796 free_event_filter(rcu_access_pointer(data->filter));
805 static LIST_HEAD(named_triggers);
808 * find_named_trigger - Find the common named trigger associated with @name
809 * @name: The name of the set of named triggers to find the common data for
811 * Named triggers are sets of triggers that share a common set of
812 * trigger data. The first named trigger registered with a given name
813 * owns the common trigger data that the others subsequently
814 * registered with the same name will reference. This function
815 * returns the common trigger data associated with that first
816 * registered instance.
818 * Return: the common trigger data for the given named trigger on
819 * success, NULL otherwise.
821 struct event_trigger_data *find_named_trigger(const char *name)
823 struct event_trigger_data *data;
828 list_for_each_entry(data, &named_triggers, named_list) {
829 if (data->named_data)
831 if (strcmp(data->name, name) == 0)
839 * is_named_trigger - determine if a given trigger is a named trigger
840 * @test: The trigger data to test
842 * Return: true if 'test' is a named trigger, false otherwise.
844 bool is_named_trigger(struct event_trigger_data *test)
846 struct event_trigger_data *data;
848 list_for_each_entry(data, &named_triggers, named_list) {
857 * save_named_trigger - save the trigger in the named trigger list
858 * @name: The name of the named trigger set
859 * @data: The trigger data to save
861 * Return: 0 if successful, negative error otherwise.
863 int save_named_trigger(const char *name, struct event_trigger_data *data)
865 data->name = kstrdup(name, GFP_KERNEL);
869 list_add(&data->named_list, &named_triggers);
875 * del_named_trigger - delete a trigger from the named trigger list
876 * @data: The trigger data to delete
878 void del_named_trigger(struct event_trigger_data *data)
883 list_del(&data->named_list);
886 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
888 struct event_trigger_data *test;
890 list_for_each_entry(test, &named_triggers, named_list) {
891 if (strcmp(test->name, data->name) == 0) {
893 test->paused_tmp = test->paused;
896 test->paused = test->paused_tmp;
903 * pause_named_trigger - Pause all named triggers with the same name
904 * @data: The trigger data of a named trigger to pause
906 * Pauses a named trigger along with all other triggers having the
907 * same name. Because named triggers share a common set of data,
908 * pausing only one is meaningless, so pausing one named trigger needs
909 * to pause all triggers with the same name.
911 void pause_named_trigger(struct event_trigger_data *data)
913 __pause_named_trigger(data, true);
917 * unpause_named_trigger - Un-pause all named triggers with the same name
918 * @data: The trigger data of a named trigger to unpause
920 * Un-pauses a named trigger along with all other triggers having the
921 * same name. Because named triggers share a common set of data,
922 * unpausing only one is meaningless, so unpausing one named trigger
923 * needs to unpause all triggers with the same name.
925 void unpause_named_trigger(struct event_trigger_data *data)
927 __pause_named_trigger(data, false);
931 * set_named_trigger_data - Associate common named trigger data
932 * @data: The trigger data to associate
933 * @named_data: The common named trigger to be associated
935 * Named triggers are sets of triggers that share a common set of
936 * trigger data. The first named trigger registered with a given name
937 * owns the common trigger data that the others subsequently
938 * registered with the same name will reference. This function
939 * associates the common trigger data from the first trigger with the
942 void set_named_trigger_data(struct event_trigger_data *data,
943 struct event_trigger_data *named_data)
945 data->named_data = named_data;
948 struct event_trigger_data *
949 get_named_trigger_data(struct event_trigger_data *data)
951 return data->named_data;
955 traceon_trigger(struct event_trigger_data *data,
956 struct trace_buffer *buffer, void *rec,
957 struct ring_buffer_event *event)
959 struct trace_event_file *file = data->private_data;
962 if (tracer_tracing_is_on(file->tr))
965 tracer_tracing_on(file->tr);
976 traceon_count_trigger(struct event_trigger_data *data,
977 struct trace_buffer *buffer, void *rec,
978 struct ring_buffer_event *event)
980 struct trace_event_file *file = data->private_data;
983 if (tracer_tracing_is_on(file->tr))
993 if (data->count != -1)
997 tracer_tracing_on(file->tr);
1003 traceoff_trigger(struct event_trigger_data *data,
1004 struct trace_buffer *buffer, void *rec,
1005 struct ring_buffer_event *event)
1007 struct trace_event_file *file = data->private_data;
1010 if (!tracer_tracing_is_on(file->tr))
1013 tracer_tracing_off(file->tr);
1017 if (!tracing_is_on())
1024 traceoff_count_trigger(struct event_trigger_data *data,
1025 struct trace_buffer *buffer, void *rec,
1026 struct ring_buffer_event *event)
1028 struct trace_event_file *file = data->private_data;
1031 if (!tracer_tracing_is_on(file->tr))
1034 if (!tracing_is_on())
1041 if (data->count != -1)
1045 tracer_tracing_off(file->tr);
1051 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1052 struct event_trigger_data *data)
1054 return event_trigger_print("traceon", m, (void *)data->count,
1059 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1060 struct event_trigger_data *data)
1062 return event_trigger_print("traceoff", m, (void *)data->count,
1066 static struct event_trigger_ops traceon_trigger_ops = {
1067 .func = traceon_trigger,
1068 .print = traceon_trigger_print,
1069 .init = event_trigger_init,
1070 .free = event_trigger_free,
1073 static struct event_trigger_ops traceon_count_trigger_ops = {
1074 .func = traceon_count_trigger,
1075 .print = traceon_trigger_print,
1076 .init = event_trigger_init,
1077 .free = event_trigger_free,
1080 static struct event_trigger_ops traceoff_trigger_ops = {
1081 .func = traceoff_trigger,
1082 .print = traceoff_trigger_print,
1083 .init = event_trigger_init,
1084 .free = event_trigger_free,
1087 static struct event_trigger_ops traceoff_count_trigger_ops = {
1088 .func = traceoff_count_trigger,
1089 .print = traceoff_trigger_print,
1090 .init = event_trigger_init,
1091 .free = event_trigger_free,
1094 static struct event_trigger_ops *
1095 onoff_get_trigger_ops(char *cmd, char *param)
1097 struct event_trigger_ops *ops;
1099 /* we register both traceon and traceoff to this callback */
1100 if (strcmp(cmd, "traceon") == 0)
1101 ops = param ? &traceon_count_trigger_ops :
1102 &traceon_trigger_ops;
1104 ops = param ? &traceoff_count_trigger_ops :
1105 &traceoff_trigger_ops;
1110 static struct event_command trigger_traceon_cmd = {
1112 .trigger_type = ETT_TRACE_ONOFF,
1113 .func = event_trigger_callback,
1114 .reg = register_trigger,
1115 .unreg = unregister_trigger,
1116 .get_trigger_ops = onoff_get_trigger_ops,
1117 .set_filter = set_trigger_filter,
1120 static struct event_command trigger_traceoff_cmd = {
1122 .trigger_type = ETT_TRACE_ONOFF,
1123 .flags = EVENT_CMD_FL_POST_TRIGGER,
1124 .func = event_trigger_callback,
1125 .reg = register_trigger,
1126 .unreg = unregister_trigger,
1127 .get_trigger_ops = onoff_get_trigger_ops,
1128 .set_filter = set_trigger_filter,
1131 #ifdef CONFIG_TRACER_SNAPSHOT
1133 snapshot_trigger(struct event_trigger_data *data,
1134 struct trace_buffer *buffer, void *rec,
1135 struct ring_buffer_event *event)
1137 struct trace_event_file *file = data->private_data;
1140 tracing_snapshot_instance(file->tr);
1146 snapshot_count_trigger(struct event_trigger_data *data,
1147 struct trace_buffer *buffer, void *rec,
1148 struct ring_buffer_event *event)
1153 if (data->count != -1)
1156 snapshot_trigger(data, buffer, rec, event);
1160 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1161 struct event_trigger_data *data,
1162 struct trace_event_file *file)
1164 if (tracing_alloc_snapshot_instance(file->tr) != 0)
1167 return register_trigger(glob, ops, data, file);
1171 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1172 struct event_trigger_data *data)
1174 return event_trigger_print("snapshot", m, (void *)data->count,
1178 static struct event_trigger_ops snapshot_trigger_ops = {
1179 .func = snapshot_trigger,
1180 .print = snapshot_trigger_print,
1181 .init = event_trigger_init,
1182 .free = event_trigger_free,
1185 static struct event_trigger_ops snapshot_count_trigger_ops = {
1186 .func = snapshot_count_trigger,
1187 .print = snapshot_trigger_print,
1188 .init = event_trigger_init,
1189 .free = event_trigger_free,
1192 static struct event_trigger_ops *
1193 snapshot_get_trigger_ops(char *cmd, char *param)
1195 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1198 static struct event_command trigger_snapshot_cmd = {
1200 .trigger_type = ETT_SNAPSHOT,
1201 .func = event_trigger_callback,
1202 .reg = register_snapshot_trigger,
1203 .unreg = unregister_trigger,
1204 .get_trigger_ops = snapshot_get_trigger_ops,
1205 .set_filter = set_trigger_filter,
1208 static __init int register_trigger_snapshot_cmd(void)
1212 ret = register_event_command(&trigger_snapshot_cmd);
1218 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1219 #endif /* CONFIG_TRACER_SNAPSHOT */
1221 #ifdef CONFIG_STACKTRACE
1222 #ifdef CONFIG_UNWINDER_ORC
1224 * event_triggers_post_call()
1225 * trace_event_raw_event_xxx()
1227 # define STACK_SKIP 2
1231 * stacktrace_trigger()
1232 * event_triggers_post_call()
1233 * trace_event_buffer_commit()
1234 * trace_event_raw_event_xxx()
1236 #define STACK_SKIP 4
1240 stacktrace_trigger(struct event_trigger_data *data,
1241 struct trace_buffer *buffer, void *rec,
1242 struct ring_buffer_event *event)
1244 struct trace_event_file *file = data->private_data;
1247 __trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP);
1249 trace_dump_stack(STACK_SKIP);
1253 stacktrace_count_trigger(struct event_trigger_data *data,
1254 struct trace_buffer *buffer, void *rec,
1255 struct ring_buffer_event *event)
1260 if (data->count != -1)
1263 stacktrace_trigger(data, buffer, rec, event);
1267 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1268 struct event_trigger_data *data)
1270 return event_trigger_print("stacktrace", m, (void *)data->count,
1274 static struct event_trigger_ops stacktrace_trigger_ops = {
1275 .func = stacktrace_trigger,
1276 .print = stacktrace_trigger_print,
1277 .init = event_trigger_init,
1278 .free = event_trigger_free,
1281 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1282 .func = stacktrace_count_trigger,
1283 .print = stacktrace_trigger_print,
1284 .init = event_trigger_init,
1285 .free = event_trigger_free,
1288 static struct event_trigger_ops *
1289 stacktrace_get_trigger_ops(char *cmd, char *param)
1291 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1294 static struct event_command trigger_stacktrace_cmd = {
1295 .name = "stacktrace",
1296 .trigger_type = ETT_STACKTRACE,
1297 .flags = EVENT_CMD_FL_POST_TRIGGER,
1298 .func = event_trigger_callback,
1299 .reg = register_trigger,
1300 .unreg = unregister_trigger,
1301 .get_trigger_ops = stacktrace_get_trigger_ops,
1302 .set_filter = set_trigger_filter,
1305 static __init int register_trigger_stacktrace_cmd(void)
1309 ret = register_event_command(&trigger_stacktrace_cmd);
1315 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1316 #endif /* CONFIG_STACKTRACE */
1318 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1320 unregister_event_command(&trigger_traceon_cmd);
1321 unregister_event_command(&trigger_traceoff_cmd);
1325 event_enable_trigger(struct event_trigger_data *data,
1326 struct trace_buffer *buffer, void *rec,
1327 struct ring_buffer_event *event)
1329 struct enable_trigger_data *enable_data = data->private_data;
1331 if (enable_data->enable)
1332 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1334 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1338 event_enable_count_trigger(struct event_trigger_data *data,
1339 struct trace_buffer *buffer, void *rec,
1340 struct ring_buffer_event *event)
1342 struct enable_trigger_data *enable_data = data->private_data;
1347 /* Skip if the event is in a state we want to switch to */
1348 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1351 if (data->count != -1)
1354 event_enable_trigger(data, buffer, rec, event);
1357 int event_enable_trigger_print(struct seq_file *m,
1358 struct event_trigger_ops *ops,
1359 struct event_trigger_data *data)
1361 struct enable_trigger_data *enable_data = data->private_data;
1363 seq_printf(m, "%s:%s:%s",
1365 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1366 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1367 enable_data->file->event_call->class->system,
1368 trace_event_name(enable_data->file->event_call));
1370 if (data->count == -1)
1371 seq_puts(m, ":unlimited");
1373 seq_printf(m, ":count=%ld", data->count);
1375 if (data->filter_str)
1376 seq_printf(m, " if %s\n", data->filter_str);
1383 void event_enable_trigger_free(struct event_trigger_ops *ops,
1384 struct event_trigger_data *data)
1386 struct enable_trigger_data *enable_data = data->private_data;
1388 if (WARN_ON_ONCE(data->ref <= 0))
1393 /* Remove the SOFT_MODE flag */
1394 trace_event_enable_disable(enable_data->file, 0, 1);
1395 trace_event_put_ref(enable_data->file->event_call);
1396 trigger_data_free(data);
1401 static struct event_trigger_ops event_enable_trigger_ops = {
1402 .func = event_enable_trigger,
1403 .print = event_enable_trigger_print,
1404 .init = event_trigger_init,
1405 .free = event_enable_trigger_free,
1408 static struct event_trigger_ops event_enable_count_trigger_ops = {
1409 .func = event_enable_count_trigger,
1410 .print = event_enable_trigger_print,
1411 .init = event_trigger_init,
1412 .free = event_enable_trigger_free,
1415 static struct event_trigger_ops event_disable_trigger_ops = {
1416 .func = event_enable_trigger,
1417 .print = event_enable_trigger_print,
1418 .init = event_trigger_init,
1419 .free = event_enable_trigger_free,
1422 static struct event_trigger_ops event_disable_count_trigger_ops = {
1423 .func = event_enable_count_trigger,
1424 .print = event_enable_trigger_print,
1425 .init = event_trigger_init,
1426 .free = event_enable_trigger_free,
1429 int event_enable_trigger_func(struct event_command *cmd_ops,
1430 struct trace_event_file *file,
1431 char *glob, char *cmd, char *param)
1433 struct trace_event_file *event_enable_file;
1434 struct enable_trigger_data *enable_data;
1435 struct event_trigger_data *trigger_data;
1436 struct event_trigger_ops *trigger_ops;
1437 struct trace_array *tr = file->tr;
1449 /* separate the trigger from the filter (s:e:n [if filter]) */
1450 trigger = strsep(¶m, " \t");
1454 param = skip_spaces(param);
1459 system = strsep(&trigger, ":");
1463 event = strsep(&trigger, ":");
1466 event_enable_file = find_event_file(tr, system, event);
1467 if (!event_enable_file)
1470 #ifdef CONFIG_HIST_TRIGGERS
1471 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1472 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1474 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1475 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1477 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1479 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1482 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1486 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1488 kfree(trigger_data);
1492 trigger_data->count = -1;
1493 trigger_data->ops = trigger_ops;
1494 trigger_data->cmd_ops = cmd_ops;
1495 INIT_LIST_HEAD(&trigger_data->list);
1496 RCU_INIT_POINTER(trigger_data->filter, NULL);
1498 enable_data->hist = hist;
1499 enable_data->enable = enable;
1500 enable_data->file = event_enable_file;
1501 trigger_data->private_data = enable_data;
1503 if (glob[0] == '!') {
1504 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1505 kfree(trigger_data);
1511 /* Up the trigger_data count to make sure nothing frees it on failure */
1512 event_trigger_init(trigger_ops, trigger_data);
1515 number = strsep(&trigger, ":");
1518 if (!strlen(number))
1522 * We use the callback data field (which is a pointer)
1525 ret = kstrtoul(number, 0, &trigger_data->count);
1530 if (!param) /* if param is non-empty, it's supposed to be a filter */
1533 if (!cmd_ops->set_filter)
1536 ret = cmd_ops->set_filter(param, trigger_data, file);
1541 /* Don't let event modules unload while probe registered */
1542 ret = trace_event_try_get_ref(event_enable_file->event_call);
1548 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1551 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1553 * The above returns on success the # of functions enabled,
1554 * but if it didn't find any functions it returns zero.
1555 * Consider no functions a failure too.
1562 /* Just return zero, not the number of enabled functions */
1564 event_trigger_free(trigger_ops, trigger_data);
1569 trace_event_enable_disable(event_enable_file, 0, 1);
1571 trace_event_put_ref(event_enable_file->event_call);
1573 if (cmd_ops->set_filter)
1574 cmd_ops->set_filter(NULL, trigger_data, NULL);
1575 event_trigger_free(trigger_ops, trigger_data);
1580 int event_enable_register_trigger(char *glob,
1581 struct event_trigger_ops *ops,
1582 struct event_trigger_data *data,
1583 struct trace_event_file *file)
1585 struct enable_trigger_data *enable_data = data->private_data;
1586 struct enable_trigger_data *test_enable_data;
1587 struct event_trigger_data *test;
1590 lockdep_assert_held(&event_mutex);
1592 list_for_each_entry(test, &file->triggers, list) {
1593 test_enable_data = test->private_data;
1594 if (test_enable_data &&
1595 (test->cmd_ops->trigger_type ==
1596 data->cmd_ops->trigger_type) &&
1597 (test_enable_data->file == enable_data->file)) {
1603 if (data->ops->init) {
1604 ret = data->ops->init(data->ops, data);
1609 list_add_rcu(&data->list, &file->triggers);
1612 update_cond_flag(file);
1613 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1614 list_del_rcu(&data->list);
1615 update_cond_flag(file);
1622 void event_enable_unregister_trigger(char *glob,
1623 struct event_trigger_ops *ops,
1624 struct event_trigger_data *test,
1625 struct trace_event_file *file)
1627 struct enable_trigger_data *test_enable_data = test->private_data;
1628 struct enable_trigger_data *enable_data;
1629 struct event_trigger_data *data;
1630 bool unregistered = false;
1632 lockdep_assert_held(&event_mutex);
1634 list_for_each_entry(data, &file->triggers, list) {
1635 enable_data = data->private_data;
1637 (data->cmd_ops->trigger_type ==
1638 test->cmd_ops->trigger_type) &&
1639 (enable_data->file == test_enable_data->file)) {
1640 unregistered = true;
1641 list_del_rcu(&data->list);
1642 trace_event_trigger_enable_disable(file, 0);
1643 update_cond_flag(file);
1648 if (unregistered && data->ops->free)
1649 data->ops->free(data->ops, data);
1652 static struct event_trigger_ops *
1653 event_enable_get_trigger_ops(char *cmd, char *param)
1655 struct event_trigger_ops *ops;
1658 #ifdef CONFIG_HIST_TRIGGERS
1659 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1660 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1662 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1665 ops = param ? &event_enable_count_trigger_ops :
1666 &event_enable_trigger_ops;
1668 ops = param ? &event_disable_count_trigger_ops :
1669 &event_disable_trigger_ops;
1674 static struct event_command trigger_enable_cmd = {
1675 .name = ENABLE_EVENT_STR,
1676 .trigger_type = ETT_EVENT_ENABLE,
1677 .func = event_enable_trigger_func,
1678 .reg = event_enable_register_trigger,
1679 .unreg = event_enable_unregister_trigger,
1680 .get_trigger_ops = event_enable_get_trigger_ops,
1681 .set_filter = set_trigger_filter,
1684 static struct event_command trigger_disable_cmd = {
1685 .name = DISABLE_EVENT_STR,
1686 .trigger_type = ETT_EVENT_ENABLE,
1687 .func = event_enable_trigger_func,
1688 .reg = event_enable_register_trigger,
1689 .unreg = event_enable_unregister_trigger,
1690 .get_trigger_ops = event_enable_get_trigger_ops,
1691 .set_filter = set_trigger_filter,
1694 static __init void unregister_trigger_enable_disable_cmds(void)
1696 unregister_event_command(&trigger_enable_cmd);
1697 unregister_event_command(&trigger_disable_cmd);
1700 static __init int register_trigger_enable_disable_cmds(void)
1704 ret = register_event_command(&trigger_enable_cmd);
1705 if (WARN_ON(ret < 0))
1707 ret = register_event_command(&trigger_disable_cmd);
1708 if (WARN_ON(ret < 0))
1709 unregister_trigger_enable_disable_cmds();
1714 static __init int register_trigger_traceon_traceoff_cmds(void)
1718 ret = register_event_command(&trigger_traceon_cmd);
1719 if (WARN_ON(ret < 0))
1721 ret = register_event_command(&trigger_traceoff_cmd);
1722 if (WARN_ON(ret < 0))
1723 unregister_trigger_traceon_traceoff_cmds();
1728 __init int register_trigger_cmds(void)
1730 register_trigger_traceon_traceoff_cmds();
1731 register_trigger_snapshot_cmd();
1732 register_trigger_stacktrace_cmd();
1733 register_trigger_enable_disable_cmds();
1734 register_trigger_hist_enable_disable_cmds();
1735 register_trigger_hist_cmd();