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);
32 trigger_data_free(struct event_trigger_data *data)
34 synchronize_sched(); /* make sure current triggers exit before free */
39 * event_triggers_call - Call triggers associated with a trace event
40 * @file: The ftrace_event_file associated with the event
42 * For each trigger associated with an event, invoke the trigger
43 * function registered with the associated trigger command.
45 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
47 * Return: 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 void event_triggers_call(struct ftrace_event_file *file)
52 struct event_trigger_data *data;
54 if (list_empty(&file->triggers))
57 list_for_each_entry_rcu(data, &file->triggers, list)
58 data->ops->func(data);
60 EXPORT_SYMBOL_GPL(event_triggers_call);
62 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
64 struct ftrace_event_file *event_file = event_file_data(m->private);
66 return seq_list_next(t, &event_file->triggers, pos);
69 static void *trigger_start(struct seq_file *m, loff_t *pos)
71 struct ftrace_event_file *event_file;
73 /* ->stop() is called even if ->start() fails */
74 mutex_lock(&event_mutex);
75 event_file = event_file_data(m->private);
76 if (unlikely(!event_file))
77 return ERR_PTR(-ENODEV);
79 return seq_list_start(&event_file->triggers, *pos);
82 static void trigger_stop(struct seq_file *m, void *t)
84 mutex_unlock(&event_mutex);
87 static int trigger_show(struct seq_file *m, void *v)
89 struct event_trigger_data *data;
91 data = list_entry(v, struct event_trigger_data, list);
92 data->ops->print(m, data->ops, data);
97 static const struct seq_operations event_triggers_seq_ops = {
98 .start = trigger_start,
100 .stop = trigger_stop,
101 .show = trigger_show,
104 static int event_trigger_regex_open(struct inode *inode, struct file *file)
108 mutex_lock(&event_mutex);
110 if (unlikely(!event_file_data(file))) {
111 mutex_unlock(&event_mutex);
115 if (file->f_mode & FMODE_READ) {
116 ret = seq_open(file, &event_triggers_seq_ops);
118 struct seq_file *m = file->private_data;
123 mutex_unlock(&event_mutex);
128 static int trigger_process_regex(struct ftrace_event_file *file, char *buff)
130 char *command, *next = buff;
131 struct event_command *p;
134 command = strsep(&next, ": \t");
135 command = (command[0] != '!') ? command : command + 1;
137 mutex_lock(&trigger_cmd_mutex);
138 list_for_each_entry(p, &trigger_commands, list) {
139 if (strcmp(p->name, command) == 0) {
140 ret = p->func(p, file, buff, command, next);
145 mutex_unlock(&trigger_cmd_mutex);
150 static ssize_t event_trigger_regex_write(struct file *file,
151 const char __user *ubuf,
152 size_t cnt, loff_t *ppos)
154 struct ftrace_event_file *event_file;
161 if (cnt >= PAGE_SIZE)
164 buf = (char *)__get_free_page(GFP_TEMPORARY);
168 if (copy_from_user(buf, ubuf, cnt)) {
169 free_page((unsigned long)buf);
175 mutex_lock(&event_mutex);
176 event_file = event_file_data(file);
177 if (unlikely(!event_file)) {
178 mutex_unlock(&event_mutex);
179 free_page((unsigned long)buf);
182 ret = trigger_process_regex(event_file, buf);
183 mutex_unlock(&event_mutex);
185 free_page((unsigned long)buf);
195 static int event_trigger_regex_release(struct inode *inode, struct file *file)
197 mutex_lock(&event_mutex);
199 if (file->f_mode & FMODE_READ)
200 seq_release(inode, file);
202 mutex_unlock(&event_mutex);
208 event_trigger_write(struct file *filp, const char __user *ubuf,
209 size_t cnt, loff_t *ppos)
211 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
215 event_trigger_open(struct inode *inode, struct file *filp)
217 return event_trigger_regex_open(inode, filp);
221 event_trigger_release(struct inode *inode, struct file *file)
223 return event_trigger_regex_release(inode, file);
226 const struct file_operations event_trigger_fops = {
227 .open = event_trigger_open,
229 .write = event_trigger_write,
230 .llseek = ftrace_filter_lseek,
231 .release = event_trigger_release,
235 * Currently we only register event commands from __init, so mark this
238 static __init int register_event_command(struct event_command *cmd)
240 struct event_command *p;
243 mutex_lock(&trigger_cmd_mutex);
244 list_for_each_entry(p, &trigger_commands, list) {
245 if (strcmp(cmd->name, p->name) == 0) {
250 list_add(&cmd->list, &trigger_commands);
252 mutex_unlock(&trigger_cmd_mutex);
258 * Currently we only unregister event commands from __init, so mark
261 static __init int unregister_event_command(struct event_command *cmd)
263 struct event_command *p, *n;
266 mutex_lock(&trigger_cmd_mutex);
267 list_for_each_entry_safe(p, n, &trigger_commands, list) {
268 if (strcmp(cmd->name, p->name) == 0) {
270 list_del_init(&p->list);
275 mutex_unlock(&trigger_cmd_mutex);
281 * event_trigger_print - Generic event_trigger_ops @print implementation
282 * @name: The name of the event trigger
283 * @m: The seq_file being printed to
284 * @data: Trigger-specific data
285 * @filter_str: filter_str to print, if present
287 * Common implementation for event triggers to print themselves.
289 * Usually wrapped by a function that simply sets the @name of the
290 * trigger command and then invokes this.
292 * Return: 0 on success, errno otherwise
295 event_trigger_print(const char *name, struct seq_file *m,
296 void *data, char *filter_str)
298 long count = (long)data;
300 seq_printf(m, "%s", name);
303 seq_puts(m, ":unlimited");
305 seq_printf(m, ":count=%ld", count);
308 seq_printf(m, " if %s\n", filter_str);
316 * event_trigger_init - Generic event_trigger_ops @init implementation
317 * @ops: The trigger ops associated with the trigger
318 * @data: Trigger-specific data
320 * Common implementation of event trigger initialization.
322 * Usually used directly as the @init method in event trigger
325 * Return: 0 on success, errno otherwise
328 event_trigger_init(struct event_trigger_ops *ops,
329 struct event_trigger_data *data)
336 * event_trigger_free - Generic event_trigger_ops @free implementation
337 * @ops: The trigger ops associated with the trigger
338 * @data: Trigger-specific data
340 * Common implementation of event trigger de-initialization.
342 * Usually used directly as the @free method in event trigger
346 event_trigger_free(struct event_trigger_ops *ops,
347 struct event_trigger_data *data)
349 if (WARN_ON_ONCE(data->ref <= 0))
354 trigger_data_free(data);
357 static int trace_event_trigger_enable_disable(struct ftrace_event_file *file,
362 if (trigger_enable) {
363 if (atomic_inc_return(&file->tm_ref) > 1)
365 set_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags);
366 ret = trace_event_enable_disable(file, 1, 1);
368 if (atomic_dec_return(&file->tm_ref) > 0)
370 clear_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags);
371 ret = trace_event_enable_disable(file, 0, 1);
378 * clear_event_triggers - Clear all triggers associated with a trace array
379 * @tr: The trace array to clear
381 * For each trigger, the triggering event has its tm_ref decremented
382 * via trace_event_trigger_enable_disable(), and any associated event
383 * (in the case of enable/disable_event triggers) will have its sm_ref
384 * decremented via free()->trace_event_enable_disable(). That
385 * combination effectively reverses the soft-mode/trigger state added
386 * by trigger registration.
388 * Must be called with event_mutex held.
391 clear_event_triggers(struct trace_array *tr)
393 struct ftrace_event_file *file;
395 list_for_each_entry(file, &tr->events, list) {
396 struct event_trigger_data *data;
397 list_for_each_entry_rcu(data, &file->triggers, list) {
398 trace_event_trigger_enable_disable(file, 0);
400 data->ops->free(data->ops, data);
406 * register_trigger - Generic event_command @reg implementation
407 * @glob: The raw string used to register the trigger
408 * @ops: The trigger ops associated with the trigger
409 * @data: Trigger-specific data to associate with the trigger
410 * @file: The ftrace_event_file associated with the event
412 * Common implementation for event trigger registration.
414 * Usually used directly as the @reg method in event command
417 * Return: 0 on success, errno otherwise
419 static int register_trigger(char *glob, struct event_trigger_ops *ops,
420 struct event_trigger_data *data,
421 struct ftrace_event_file *file)
423 struct event_trigger_data *test;
426 list_for_each_entry_rcu(test, &file->triggers, list) {
427 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
433 if (data->ops->init) {
434 ret = data->ops->init(data->ops, data);
439 list_add_rcu(&data->list, &file->triggers);
442 if (trace_event_trigger_enable_disable(file, 1) < 0) {
443 list_del_rcu(&data->list);
451 * unregister_trigger - Generic event_command @unreg implementation
452 * @glob: The raw string used to register the trigger
453 * @ops: The trigger ops associated with the trigger
454 * @test: Trigger-specific data used to find the trigger to remove
455 * @file: The ftrace_event_file associated with the event
457 * Common implementation for event trigger unregistration.
459 * Usually used directly as the @unreg method in event command
462 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
463 struct event_trigger_data *test,
464 struct ftrace_event_file *file)
466 struct event_trigger_data *data;
467 bool unregistered = false;
469 list_for_each_entry_rcu(data, &file->triggers, list) {
470 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
472 list_del_rcu(&data->list);
473 trace_event_trigger_enable_disable(file, 0);
478 if (unregistered && data->ops->free)
479 data->ops->free(data->ops, data);
483 * event_trigger_callback - Generic event_command @func implementation
484 * @cmd_ops: The command ops, used for trigger registration
485 * @file: The ftrace_event_file associated with the event
486 * @glob: The raw string used to register the trigger
487 * @cmd: The cmd portion of the string used to register the trigger
488 * @param: The params portion of the string used to register the trigger
490 * Common implementation for event command parsing and trigger
493 * Usually used directly as the @func method in event command
496 * Return: 0 on success, errno otherwise
499 event_trigger_callback(struct event_command *cmd_ops,
500 struct ftrace_event_file *file,
501 char *glob, char *cmd, char *param)
503 struct event_trigger_data *trigger_data;
504 struct event_trigger_ops *trigger_ops;
505 char *trigger = NULL;
509 /* separate the trigger from the filter (t:n [if filter]) */
510 if (param && isdigit(param[0]))
511 trigger = strsep(¶m, " \t");
513 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
516 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
520 trigger_data->count = -1;
521 trigger_data->ops = trigger_ops;
522 trigger_data->cmd_ops = cmd_ops;
523 INIT_LIST_HEAD(&trigger_data->list);
525 if (glob[0] == '!') {
526 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
533 number = strsep(&trigger, ":");
540 * We use the callback data field (which is a pointer)
543 ret = kstrtoul(number, 0, &trigger_data->count);
548 if (!param) /* if param is non-empty, it's supposed to be a filter */
551 if (!cmd_ops->set_filter)
554 ret = cmd_ops->set_filter(param, trigger_data, file);
559 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
561 * The above returns on success the # of functions enabled,
562 * but if it didn't find any functions it returns zero.
563 * Consider no functions a failure too.
580 traceon_trigger(struct event_trigger_data *data)
589 traceon_count_trigger(struct event_trigger_data *data)
594 if (data->count != -1)
597 traceon_trigger(data);
601 traceoff_trigger(struct event_trigger_data *data)
603 if (!tracing_is_on())
610 traceoff_count_trigger(struct event_trigger_data *data)
615 if (data->count != -1)
618 traceoff_trigger(data);
622 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
623 struct event_trigger_data *data)
625 return event_trigger_print("traceon", m, (void *)data->count,
630 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
631 struct event_trigger_data *data)
633 return event_trigger_print("traceoff", m, (void *)data->count,
637 static struct event_trigger_ops traceon_trigger_ops = {
638 .func = traceon_trigger,
639 .print = traceon_trigger_print,
640 .init = event_trigger_init,
641 .free = event_trigger_free,
644 static struct event_trigger_ops traceon_count_trigger_ops = {
645 .func = traceon_count_trigger,
646 .print = traceon_trigger_print,
647 .init = event_trigger_init,
648 .free = event_trigger_free,
651 static struct event_trigger_ops traceoff_trigger_ops = {
652 .func = traceoff_trigger,
653 .print = traceoff_trigger_print,
654 .init = event_trigger_init,
655 .free = event_trigger_free,
658 static struct event_trigger_ops traceoff_count_trigger_ops = {
659 .func = traceoff_count_trigger,
660 .print = traceoff_trigger_print,
661 .init = event_trigger_init,
662 .free = event_trigger_free,
665 static struct event_trigger_ops *
666 onoff_get_trigger_ops(char *cmd, char *param)
668 struct event_trigger_ops *ops;
670 /* we register both traceon and traceoff to this callback */
671 if (strcmp(cmd, "traceon") == 0)
672 ops = param ? &traceon_count_trigger_ops :
673 &traceon_trigger_ops;
675 ops = param ? &traceoff_count_trigger_ops :
676 &traceoff_trigger_ops;
681 static struct event_command trigger_traceon_cmd = {
683 .trigger_type = ETT_TRACE_ONOFF,
684 .func = event_trigger_callback,
685 .reg = register_trigger,
686 .unreg = unregister_trigger,
687 .get_trigger_ops = onoff_get_trigger_ops,
690 static struct event_command trigger_traceoff_cmd = {
692 .trigger_type = ETT_TRACE_ONOFF,
693 .func = event_trigger_callback,
694 .reg = register_trigger,
695 .unreg = unregister_trigger,
696 .get_trigger_ops = onoff_get_trigger_ops,
699 #ifdef CONFIG_TRACER_SNAPSHOT
701 snapshot_trigger(struct event_trigger_data *data)
707 snapshot_count_trigger(struct event_trigger_data *data)
712 if (data->count != -1)
715 snapshot_trigger(data);
719 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
720 struct event_trigger_data *data,
721 struct ftrace_event_file *file)
723 int ret = register_trigger(glob, ops, data, file);
725 if (ret > 0 && tracing_alloc_snapshot() != 0) {
726 unregister_trigger(glob, ops, data, file);
734 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
735 struct event_trigger_data *data)
737 return event_trigger_print("snapshot", m, (void *)data->count,
741 static struct event_trigger_ops snapshot_trigger_ops = {
742 .func = snapshot_trigger,
743 .print = snapshot_trigger_print,
744 .init = event_trigger_init,
745 .free = event_trigger_free,
748 static struct event_trigger_ops snapshot_count_trigger_ops = {
749 .func = snapshot_count_trigger,
750 .print = snapshot_trigger_print,
751 .init = event_trigger_init,
752 .free = event_trigger_free,
755 static struct event_trigger_ops *
756 snapshot_get_trigger_ops(char *cmd, char *param)
758 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
761 static struct event_command trigger_snapshot_cmd = {
763 .trigger_type = ETT_SNAPSHOT,
764 .func = event_trigger_callback,
765 .reg = register_snapshot_trigger,
766 .unreg = unregister_trigger,
767 .get_trigger_ops = snapshot_get_trigger_ops,
770 static __init int register_trigger_snapshot_cmd(void)
774 ret = register_event_command(&trigger_snapshot_cmd);
780 static __init int register_trigger_snapshot_cmd(void) { return 0; }
781 #endif /* CONFIG_TRACER_SNAPSHOT */
783 #ifdef CONFIG_STACKTRACE
786 * stacktrace_trigger()
787 * event_triggers_post_call()
788 * ftrace_raw_event_xxx()
793 stacktrace_trigger(struct event_trigger_data *data)
795 trace_dump_stack(STACK_SKIP);
799 stacktrace_count_trigger(struct event_trigger_data *data)
804 if (data->count != -1)
807 stacktrace_trigger(data);
811 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
812 struct event_trigger_data *data)
814 return event_trigger_print("stacktrace", m, (void *)data->count,
818 static struct event_trigger_ops stacktrace_trigger_ops = {
819 .func = stacktrace_trigger,
820 .print = stacktrace_trigger_print,
821 .init = event_trigger_init,
822 .free = event_trigger_free,
825 static struct event_trigger_ops stacktrace_count_trigger_ops = {
826 .func = stacktrace_count_trigger,
827 .print = stacktrace_trigger_print,
828 .init = event_trigger_init,
829 .free = event_trigger_free,
832 static struct event_trigger_ops *
833 stacktrace_get_trigger_ops(char *cmd, char *param)
835 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
838 static struct event_command trigger_stacktrace_cmd = {
839 .name = "stacktrace",
840 .trigger_type = ETT_STACKTRACE,
841 .post_trigger = true,
842 .func = event_trigger_callback,
843 .reg = register_trigger,
844 .unreg = unregister_trigger,
845 .get_trigger_ops = stacktrace_get_trigger_ops,
848 static __init int register_trigger_stacktrace_cmd(void)
852 ret = register_event_command(&trigger_stacktrace_cmd);
858 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
859 #endif /* CONFIG_STACKTRACE */
861 static __init void unregister_trigger_traceon_traceoff_cmds(void)
863 unregister_event_command(&trigger_traceon_cmd);
864 unregister_event_command(&trigger_traceoff_cmd);
867 static __init int register_trigger_traceon_traceoff_cmds(void)
871 ret = register_event_command(&trigger_traceon_cmd);
872 if (WARN_ON(ret < 0))
874 ret = register_event_command(&trigger_traceoff_cmd);
875 if (WARN_ON(ret < 0))
876 unregister_trigger_traceon_traceoff_cmds();
881 __init int register_trigger_cmds(void)
883 register_trigger_traceon_traceoff_cmds();
884 register_trigger_snapshot_cmd();
885 register_trigger_stacktrace_cmd();