Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / kernel / trace / trace_events_trigger.c
blobd67992f3bb0ee02551487dfa9a87fc39e924a96c
1 /*
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>
26 #include "trace.h"
28 static LIST_HEAD(trigger_commands);
29 static DEFINE_MUTEX(trigger_cmd_mutex);
31 void trigger_data_free(struct event_trigger_data *data)
33 if (data->cmd_ops->set_filter)
34 data->cmd_ops->set_filter(NULL, data, NULL);
36 synchronize_sched(); /* make sure current triggers exit before free */
37 kfree(data);
40 /**
41 * event_triggers_call - Call triggers associated with a trace event
42 * @file: The trace_event_file associated with the event
43 * @rec: The trace entry for the event, NULL for unconditional invocation
45 * For each trigger associated with an event, invoke the trigger
46 * function registered with the associated trigger command. If rec is
47 * non-NULL, it means that the trigger requires further processing and
48 * shouldn't be unconditionally invoked. If rec is non-NULL and the
49 * trigger has a filter associated with it, rec will checked against
50 * the filter and if the record matches the trigger will be invoked.
51 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
52 * in any case until the current event is written, the trigger
53 * function isn't invoked but the bit associated with the deferred
54 * trigger is set in the return value.
56 * Returns an enum event_trigger_type value containing a set bit for
57 * any trigger that should be deferred, ETT_NONE if nothing to defer.
59 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
61 * Return: an enum event_trigger_type value containing a set bit for
62 * any trigger that should be deferred, ETT_NONE if nothing to defer.
64 enum event_trigger_type
65 event_triggers_call(struct trace_event_file *file, void *rec)
67 struct event_trigger_data *data;
68 enum event_trigger_type tt = ETT_NONE;
69 struct event_filter *filter;
71 if (list_empty(&file->triggers))
72 return tt;
74 list_for_each_entry_rcu(data, &file->triggers, list) {
75 if (data->paused)
76 continue;
77 if (!rec) {
78 data->ops->func(data, rec);
79 continue;
81 filter = rcu_dereference_sched(data->filter);
82 if (filter && !filter_match_preds(filter, rec))
83 continue;
84 if (event_command_post_trigger(data->cmd_ops)) {
85 tt |= data->cmd_ops->trigger_type;
86 continue;
88 data->ops->func(data, rec);
90 return tt;
92 EXPORT_SYMBOL_GPL(event_triggers_call);
94 /**
95 * event_triggers_post_call - Call 'post_triggers' for a trace event
96 * @file: The trace_event_file associated with the event
97 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
98 * @rec: The trace entry for the event
100 * For each trigger associated with an event, invoke the trigger
101 * function registered with the associated trigger command, if the
102 * corresponding bit is set in the tt enum passed into this function.
103 * See @event_triggers_call for details on how those bits are set.
105 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
107 void
108 event_triggers_post_call(struct trace_event_file *file,
109 enum event_trigger_type tt,
110 void *rec)
112 struct event_trigger_data *data;
114 list_for_each_entry_rcu(data, &file->triggers, list) {
115 if (data->paused)
116 continue;
117 if (data->cmd_ops->trigger_type & tt)
118 data->ops->func(data, rec);
121 EXPORT_SYMBOL_GPL(event_triggers_post_call);
123 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
125 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
127 struct trace_event_file *event_file = event_file_data(m->private);
129 if (t == SHOW_AVAILABLE_TRIGGERS)
130 return NULL;
132 return seq_list_next(t, &event_file->triggers, pos);
135 static void *trigger_start(struct seq_file *m, loff_t *pos)
137 struct trace_event_file *event_file;
139 /* ->stop() is called even if ->start() fails */
140 mutex_lock(&event_mutex);
141 event_file = event_file_data(m->private);
142 if (unlikely(!event_file))
143 return ERR_PTR(-ENODEV);
145 if (list_empty(&event_file->triggers))
146 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
148 return seq_list_start(&event_file->triggers, *pos);
151 static void trigger_stop(struct seq_file *m, void *t)
153 mutex_unlock(&event_mutex);
156 static int trigger_show(struct seq_file *m, void *v)
158 struct event_trigger_data *data;
159 struct event_command *p;
161 if (v == SHOW_AVAILABLE_TRIGGERS) {
162 seq_puts(m, "# Available triggers:\n");
163 seq_putc(m, '#');
164 mutex_lock(&trigger_cmd_mutex);
165 list_for_each_entry_reverse(p, &trigger_commands, list)
166 seq_printf(m, " %s", p->name);
167 seq_putc(m, '\n');
168 mutex_unlock(&trigger_cmd_mutex);
169 return 0;
172 data = list_entry(v, struct event_trigger_data, list);
173 data->ops->print(m, data->ops, data);
175 return 0;
178 static const struct seq_operations event_triggers_seq_ops = {
179 .start = trigger_start,
180 .next = trigger_next,
181 .stop = trigger_stop,
182 .show = trigger_show,
185 static int event_trigger_regex_open(struct inode *inode, struct file *file)
187 int ret = 0;
189 mutex_lock(&event_mutex);
191 if (unlikely(!event_file_data(file))) {
192 mutex_unlock(&event_mutex);
193 return -ENODEV;
196 if ((file->f_mode & FMODE_WRITE) &&
197 (file->f_flags & O_TRUNC)) {
198 struct trace_event_file *event_file;
199 struct event_command *p;
201 event_file = event_file_data(file);
203 list_for_each_entry(p, &trigger_commands, list) {
204 if (p->unreg_all)
205 p->unreg_all(event_file);
209 if (file->f_mode & FMODE_READ) {
210 ret = seq_open(file, &event_triggers_seq_ops);
211 if (!ret) {
212 struct seq_file *m = file->private_data;
213 m->private = file;
217 mutex_unlock(&event_mutex);
219 return ret;
222 static int trigger_process_regex(struct trace_event_file *file, char *buff)
224 char *command, *next = buff;
225 struct event_command *p;
226 int ret = -EINVAL;
228 command = strsep(&next, ": \t");
229 command = (command[0] != '!') ? command : command + 1;
231 mutex_lock(&trigger_cmd_mutex);
232 list_for_each_entry(p, &trigger_commands, list) {
233 if (strcmp(p->name, command) == 0) {
234 ret = p->func(p, file, buff, command, next);
235 goto out_unlock;
238 out_unlock:
239 mutex_unlock(&trigger_cmd_mutex);
241 return ret;
244 static ssize_t event_trigger_regex_write(struct file *file,
245 const char __user *ubuf,
246 size_t cnt, loff_t *ppos)
248 struct trace_event_file *event_file;
249 ssize_t ret;
250 char *buf;
252 if (!cnt)
253 return 0;
255 if (cnt >= PAGE_SIZE)
256 return -EINVAL;
258 buf = memdup_user_nul(ubuf, cnt);
259 if (IS_ERR(buf))
260 return PTR_ERR(buf);
262 strim(buf);
264 mutex_lock(&event_mutex);
265 event_file = event_file_data(file);
266 if (unlikely(!event_file)) {
267 mutex_unlock(&event_mutex);
268 kfree(buf);
269 return -ENODEV;
271 ret = trigger_process_regex(event_file, buf);
272 mutex_unlock(&event_mutex);
274 kfree(buf);
275 if (ret < 0)
276 goto out;
278 *ppos += cnt;
279 ret = cnt;
280 out:
281 return ret;
284 static int event_trigger_regex_release(struct inode *inode, struct file *file)
286 mutex_lock(&event_mutex);
288 if (file->f_mode & FMODE_READ)
289 seq_release(inode, file);
291 mutex_unlock(&event_mutex);
293 return 0;
296 static ssize_t
297 event_trigger_write(struct file *filp, const char __user *ubuf,
298 size_t cnt, loff_t *ppos)
300 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
303 static int
304 event_trigger_open(struct inode *inode, struct file *filp)
306 return event_trigger_regex_open(inode, filp);
309 static int
310 event_trigger_release(struct inode *inode, struct file *file)
312 return event_trigger_regex_release(inode, file);
315 const struct file_operations event_trigger_fops = {
316 .open = event_trigger_open,
317 .read = seq_read,
318 .write = event_trigger_write,
319 .llseek = tracing_lseek,
320 .release = event_trigger_release,
324 * Currently we only register event commands from __init, so mark this
325 * __init too.
327 __init int register_event_command(struct event_command *cmd)
329 struct event_command *p;
330 int ret = 0;
332 mutex_lock(&trigger_cmd_mutex);
333 list_for_each_entry(p, &trigger_commands, list) {
334 if (strcmp(cmd->name, p->name) == 0) {
335 ret = -EBUSY;
336 goto out_unlock;
339 list_add(&cmd->list, &trigger_commands);
340 out_unlock:
341 mutex_unlock(&trigger_cmd_mutex);
343 return ret;
347 * Currently we only unregister event commands from __init, so mark
348 * this __init too.
350 static __init int unregister_event_command(struct event_command *cmd)
352 struct event_command *p, *n;
353 int ret = -ENODEV;
355 mutex_lock(&trigger_cmd_mutex);
356 list_for_each_entry_safe(p, n, &trigger_commands, list) {
357 if (strcmp(cmd->name, p->name) == 0) {
358 ret = 0;
359 list_del_init(&p->list);
360 goto out_unlock;
363 out_unlock:
364 mutex_unlock(&trigger_cmd_mutex);
366 return ret;
370 * event_trigger_print - Generic event_trigger_ops @print implementation
371 * @name: The name of the event trigger
372 * @m: The seq_file being printed to
373 * @data: Trigger-specific data
374 * @filter_str: filter_str to print, if present
376 * Common implementation for event triggers to print themselves.
378 * Usually wrapped by a function that simply sets the @name of the
379 * trigger command and then invokes this.
381 * Return: 0 on success, errno otherwise
383 static int
384 event_trigger_print(const char *name, struct seq_file *m,
385 void *data, char *filter_str)
387 long count = (long)data;
389 seq_puts(m, name);
391 if (count == -1)
392 seq_puts(m, ":unlimited");
393 else
394 seq_printf(m, ":count=%ld", count);
396 if (filter_str)
397 seq_printf(m, " if %s\n", filter_str);
398 else
399 seq_putc(m, '\n');
401 return 0;
405 * event_trigger_init - Generic event_trigger_ops @init implementation
406 * @ops: The trigger ops associated with the trigger
407 * @data: Trigger-specific data
409 * Common implementation of event trigger initialization.
411 * Usually used directly as the @init method in event trigger
412 * implementations.
414 * Return: 0 on success, errno otherwise
416 int event_trigger_init(struct event_trigger_ops *ops,
417 struct event_trigger_data *data)
419 data->ref++;
420 return 0;
424 * event_trigger_free - Generic event_trigger_ops @free implementation
425 * @ops: The trigger ops associated with the trigger
426 * @data: Trigger-specific data
428 * Common implementation of event trigger de-initialization.
430 * Usually used directly as the @free method in event trigger
431 * implementations.
433 static void
434 event_trigger_free(struct event_trigger_ops *ops,
435 struct event_trigger_data *data)
437 if (WARN_ON_ONCE(data->ref <= 0))
438 return;
440 data->ref--;
441 if (!data->ref)
442 trigger_data_free(data);
445 int trace_event_trigger_enable_disable(struct trace_event_file *file,
446 int trigger_enable)
448 int ret = 0;
450 if (trigger_enable) {
451 if (atomic_inc_return(&file->tm_ref) > 1)
452 return ret;
453 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
454 ret = trace_event_enable_disable(file, 1, 1);
455 } else {
456 if (atomic_dec_return(&file->tm_ref) > 0)
457 return ret;
458 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
459 ret = trace_event_enable_disable(file, 0, 1);
462 return ret;
466 * clear_event_triggers - Clear all triggers associated with a trace array
467 * @tr: The trace array to clear
469 * For each trigger, the triggering event has its tm_ref decremented
470 * via trace_event_trigger_enable_disable(), and any associated event
471 * (in the case of enable/disable_event triggers) will have its sm_ref
472 * decremented via free()->trace_event_enable_disable(). That
473 * combination effectively reverses the soft-mode/trigger state added
474 * by trigger registration.
476 * Must be called with event_mutex held.
478 void
479 clear_event_triggers(struct trace_array *tr)
481 struct trace_event_file *file;
483 list_for_each_entry(file, &tr->events, list) {
484 struct event_trigger_data *data;
485 list_for_each_entry_rcu(data, &file->triggers, list) {
486 trace_event_trigger_enable_disable(file, 0);
487 if (data->ops->free)
488 data->ops->free(data->ops, data);
494 * update_cond_flag - Set or reset the TRIGGER_COND bit
495 * @file: The trace_event_file associated with the event
497 * If an event has triggers and any of those triggers has a filter or
498 * a post_trigger, trigger invocation needs to be deferred until after
499 * the current event has logged its data, and the event should have
500 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
501 * cleared.
503 void update_cond_flag(struct trace_event_file *file)
505 struct event_trigger_data *data;
506 bool set_cond = false;
508 list_for_each_entry_rcu(data, &file->triggers, list) {
509 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
510 event_command_needs_rec(data->cmd_ops)) {
511 set_cond = true;
512 break;
516 if (set_cond)
517 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
518 else
519 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
523 * register_trigger - Generic event_command @reg implementation
524 * @glob: The raw string used to register the trigger
525 * @ops: The trigger ops associated with the trigger
526 * @data: Trigger-specific data to associate with the trigger
527 * @file: The trace_event_file associated with the event
529 * Common implementation for event trigger registration.
531 * Usually used directly as the @reg method in event command
532 * implementations.
534 * Return: 0 on success, errno otherwise
536 static int register_trigger(char *glob, struct event_trigger_ops *ops,
537 struct event_trigger_data *data,
538 struct trace_event_file *file)
540 struct event_trigger_data *test;
541 int ret = 0;
543 list_for_each_entry_rcu(test, &file->triggers, list) {
544 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
545 ret = -EEXIST;
546 goto out;
550 if (data->ops->init) {
551 ret = data->ops->init(data->ops, data);
552 if (ret < 0)
553 goto out;
556 list_add_rcu(&data->list, &file->triggers);
557 ret++;
559 update_cond_flag(file);
560 if (trace_event_trigger_enable_disable(file, 1) < 0) {
561 list_del_rcu(&data->list);
562 update_cond_flag(file);
563 ret--;
565 out:
566 return ret;
570 * unregister_trigger - Generic event_command @unreg implementation
571 * @glob: The raw string used to register the trigger
572 * @ops: The trigger ops associated with the trigger
573 * @test: Trigger-specific data used to find the trigger to remove
574 * @file: The trace_event_file associated with the event
576 * Common implementation for event trigger unregistration.
578 * Usually used directly as the @unreg method in event command
579 * implementations.
581 void unregister_trigger(char *glob, struct event_trigger_ops *ops,
582 struct event_trigger_data *test,
583 struct trace_event_file *file)
585 struct event_trigger_data *data;
586 bool unregistered = false;
588 list_for_each_entry_rcu(data, &file->triggers, list) {
589 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
590 unregistered = true;
591 list_del_rcu(&data->list);
592 trace_event_trigger_enable_disable(file, 0);
593 update_cond_flag(file);
594 break;
598 if (unregistered && data->ops->free)
599 data->ops->free(data->ops, data);
603 * event_trigger_callback - Generic event_command @func implementation
604 * @cmd_ops: The command ops, used for trigger registration
605 * @file: The trace_event_file associated with the event
606 * @glob: The raw string used to register the trigger
607 * @cmd: The cmd portion of the string used to register the trigger
608 * @param: The params portion of the string used to register the trigger
610 * Common implementation for event command parsing and trigger
611 * instantiation.
613 * Usually used directly as the @func method in event command
614 * implementations.
616 * Return: 0 on success, errno otherwise
618 static int
619 event_trigger_callback(struct event_command *cmd_ops,
620 struct trace_event_file *file,
621 char *glob, char *cmd, char *param)
623 struct event_trigger_data *trigger_data;
624 struct event_trigger_ops *trigger_ops;
625 char *trigger = NULL;
626 char *number;
627 int ret;
629 /* separate the trigger from the filter (t:n [if filter]) */
630 if (param && isdigit(param[0]))
631 trigger = strsep(&param, " \t");
633 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
635 ret = -ENOMEM;
636 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
637 if (!trigger_data)
638 goto out;
640 trigger_data->count = -1;
641 trigger_data->ops = trigger_ops;
642 trigger_data->cmd_ops = cmd_ops;
643 INIT_LIST_HEAD(&trigger_data->list);
645 if (glob[0] == '!') {
646 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
647 kfree(trigger_data);
648 ret = 0;
649 goto out;
652 if (trigger) {
653 number = strsep(&trigger, ":");
655 ret = -EINVAL;
656 if (!strlen(number))
657 goto out_free;
660 * We use the callback data field (which is a pointer)
661 * as our counter.
663 ret = kstrtoul(number, 0, &trigger_data->count);
664 if (ret)
665 goto out_free;
668 if (!param) /* if param is non-empty, it's supposed to be a filter */
669 goto out_reg;
671 if (!cmd_ops->set_filter)
672 goto out_reg;
674 ret = cmd_ops->set_filter(param, trigger_data, file);
675 if (ret < 0)
676 goto out_free;
678 out_reg:
679 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
681 * The above returns on success the # of functions enabled,
682 * but if it didn't find any functions it returns zero.
683 * Consider no functions a failure too.
685 if (!ret) {
686 ret = -ENOENT;
687 goto out_free;
688 } else if (ret < 0)
689 goto out_free;
690 ret = 0;
691 out:
692 return ret;
694 out_free:
695 if (cmd_ops->set_filter)
696 cmd_ops->set_filter(NULL, trigger_data, NULL);
697 kfree(trigger_data);
698 goto out;
702 * set_trigger_filter - Generic event_command @set_filter implementation
703 * @filter_str: The filter string for the trigger, NULL to remove filter
704 * @trigger_data: Trigger-specific data
705 * @file: The trace_event_file associated with the event
707 * Common implementation for event command filter parsing and filter
708 * instantiation.
710 * Usually used directly as the @set_filter method in event command
711 * implementations.
713 * Also used to remove a filter (if filter_str = NULL).
715 * Return: 0 on success, errno otherwise
717 int set_trigger_filter(char *filter_str,
718 struct event_trigger_data *trigger_data,
719 struct trace_event_file *file)
721 struct event_trigger_data *data = trigger_data;
722 struct event_filter *filter = NULL, *tmp;
723 int ret = -EINVAL;
724 char *s;
726 if (!filter_str) /* clear the current filter */
727 goto assign;
729 s = strsep(&filter_str, " \t");
731 if (!strlen(s) || strcmp(s, "if") != 0)
732 goto out;
734 if (!filter_str)
735 goto out;
737 /* The filter is for the 'trigger' event, not the triggered event */
738 ret = create_event_filter(file->event_call, filter_str, false, &filter);
739 if (ret)
740 goto out;
741 assign:
742 tmp = rcu_access_pointer(data->filter);
744 rcu_assign_pointer(data->filter, filter);
746 if (tmp) {
747 /* Make sure the call is done with the filter */
748 synchronize_sched();
749 free_event_filter(tmp);
752 kfree(data->filter_str);
753 data->filter_str = NULL;
755 if (filter_str) {
756 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
757 if (!data->filter_str) {
758 free_event_filter(rcu_access_pointer(data->filter));
759 data->filter = NULL;
760 ret = -ENOMEM;
763 out:
764 return ret;
767 static void
768 traceon_trigger(struct event_trigger_data *data, void *rec)
770 if (tracing_is_on())
771 return;
773 tracing_on();
776 static void
777 traceon_count_trigger(struct event_trigger_data *data, void *rec)
779 if (tracing_is_on())
780 return;
782 if (!data->count)
783 return;
785 if (data->count != -1)
786 (data->count)--;
788 tracing_on();
791 static void
792 traceoff_trigger(struct event_trigger_data *data, void *rec)
794 if (!tracing_is_on())
795 return;
797 tracing_off();
800 static void
801 traceoff_count_trigger(struct event_trigger_data *data, void *rec)
803 if (!tracing_is_on())
804 return;
806 if (!data->count)
807 return;
809 if (data->count != -1)
810 (data->count)--;
812 tracing_off();
815 static int
816 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
817 struct event_trigger_data *data)
819 return event_trigger_print("traceon", m, (void *)data->count,
820 data->filter_str);
823 static int
824 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
825 struct event_trigger_data *data)
827 return event_trigger_print("traceoff", m, (void *)data->count,
828 data->filter_str);
831 static struct event_trigger_ops traceon_trigger_ops = {
832 .func = traceon_trigger,
833 .print = traceon_trigger_print,
834 .init = event_trigger_init,
835 .free = event_trigger_free,
838 static struct event_trigger_ops traceon_count_trigger_ops = {
839 .func = traceon_count_trigger,
840 .print = traceon_trigger_print,
841 .init = event_trigger_init,
842 .free = event_trigger_free,
845 static struct event_trigger_ops traceoff_trigger_ops = {
846 .func = traceoff_trigger,
847 .print = traceoff_trigger_print,
848 .init = event_trigger_init,
849 .free = event_trigger_free,
852 static struct event_trigger_ops traceoff_count_trigger_ops = {
853 .func = traceoff_count_trigger,
854 .print = traceoff_trigger_print,
855 .init = event_trigger_init,
856 .free = event_trigger_free,
859 static struct event_trigger_ops *
860 onoff_get_trigger_ops(char *cmd, char *param)
862 struct event_trigger_ops *ops;
864 /* we register both traceon and traceoff to this callback */
865 if (strcmp(cmd, "traceon") == 0)
866 ops = param ? &traceon_count_trigger_ops :
867 &traceon_trigger_ops;
868 else
869 ops = param ? &traceoff_count_trigger_ops :
870 &traceoff_trigger_ops;
872 return ops;
875 static struct event_command trigger_traceon_cmd = {
876 .name = "traceon",
877 .trigger_type = ETT_TRACE_ONOFF,
878 .func = event_trigger_callback,
879 .reg = register_trigger,
880 .unreg = unregister_trigger,
881 .get_trigger_ops = onoff_get_trigger_ops,
882 .set_filter = set_trigger_filter,
885 static struct event_command trigger_traceoff_cmd = {
886 .name = "traceoff",
887 .trigger_type = ETT_TRACE_ONOFF,
888 .func = event_trigger_callback,
889 .reg = register_trigger,
890 .unreg = unregister_trigger,
891 .get_trigger_ops = onoff_get_trigger_ops,
892 .set_filter = set_trigger_filter,
895 #ifdef CONFIG_TRACER_SNAPSHOT
896 static void
897 snapshot_trigger(struct event_trigger_data *data, void *rec)
899 tracing_snapshot();
902 static void
903 snapshot_count_trigger(struct event_trigger_data *data, void *rec)
905 if (!data->count)
906 return;
908 if (data->count != -1)
909 (data->count)--;
911 snapshot_trigger(data, rec);
914 static int
915 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
916 struct event_trigger_data *data,
917 struct trace_event_file *file)
919 int ret = register_trigger(glob, ops, data, file);
921 if (ret > 0 && tracing_alloc_snapshot() != 0) {
922 unregister_trigger(glob, ops, data, file);
923 ret = 0;
926 return ret;
929 static int
930 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
931 struct event_trigger_data *data)
933 return event_trigger_print("snapshot", m, (void *)data->count,
934 data->filter_str);
937 static struct event_trigger_ops snapshot_trigger_ops = {
938 .func = snapshot_trigger,
939 .print = snapshot_trigger_print,
940 .init = event_trigger_init,
941 .free = event_trigger_free,
944 static struct event_trigger_ops snapshot_count_trigger_ops = {
945 .func = snapshot_count_trigger,
946 .print = snapshot_trigger_print,
947 .init = event_trigger_init,
948 .free = event_trigger_free,
951 static struct event_trigger_ops *
952 snapshot_get_trigger_ops(char *cmd, char *param)
954 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
957 static struct event_command trigger_snapshot_cmd = {
958 .name = "snapshot",
959 .trigger_type = ETT_SNAPSHOT,
960 .func = event_trigger_callback,
961 .reg = register_snapshot_trigger,
962 .unreg = unregister_trigger,
963 .get_trigger_ops = snapshot_get_trigger_ops,
964 .set_filter = set_trigger_filter,
967 static __init int register_trigger_snapshot_cmd(void)
969 int ret;
971 ret = register_event_command(&trigger_snapshot_cmd);
972 WARN_ON(ret < 0);
974 return ret;
976 #else
977 static __init int register_trigger_snapshot_cmd(void) { return 0; }
978 #endif /* CONFIG_TRACER_SNAPSHOT */
980 #ifdef CONFIG_STACKTRACE
982 * Skip 3:
983 * stacktrace_trigger()
984 * event_triggers_post_call()
985 * trace_event_raw_event_xxx()
987 #define STACK_SKIP 3
989 static void
990 stacktrace_trigger(struct event_trigger_data *data, void *rec)
992 trace_dump_stack(STACK_SKIP);
995 static void
996 stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
998 if (!data->count)
999 return;
1001 if (data->count != -1)
1002 (data->count)--;
1004 stacktrace_trigger(data, rec);
1007 static int
1008 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1009 struct event_trigger_data *data)
1011 return event_trigger_print("stacktrace", m, (void *)data->count,
1012 data->filter_str);
1015 static struct event_trigger_ops stacktrace_trigger_ops = {
1016 .func = stacktrace_trigger,
1017 .print = stacktrace_trigger_print,
1018 .init = event_trigger_init,
1019 .free = event_trigger_free,
1022 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1023 .func = stacktrace_count_trigger,
1024 .print = stacktrace_trigger_print,
1025 .init = event_trigger_init,
1026 .free = event_trigger_free,
1029 static struct event_trigger_ops *
1030 stacktrace_get_trigger_ops(char *cmd, char *param)
1032 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1035 static struct event_command trigger_stacktrace_cmd = {
1036 .name = "stacktrace",
1037 .trigger_type = ETT_STACKTRACE,
1038 .flags = EVENT_CMD_FL_POST_TRIGGER,
1039 .func = event_trigger_callback,
1040 .reg = register_trigger,
1041 .unreg = unregister_trigger,
1042 .get_trigger_ops = stacktrace_get_trigger_ops,
1043 .set_filter = set_trigger_filter,
1046 static __init int register_trigger_stacktrace_cmd(void)
1048 int ret;
1050 ret = register_event_command(&trigger_stacktrace_cmd);
1051 WARN_ON(ret < 0);
1053 return ret;
1055 #else
1056 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1057 #endif /* CONFIG_STACKTRACE */
1059 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1061 unregister_event_command(&trigger_traceon_cmd);
1062 unregister_event_command(&trigger_traceoff_cmd);
1065 /* Avoid typos */
1066 #define ENABLE_EVENT_STR "enable_event"
1067 #define DISABLE_EVENT_STR "disable_event"
1069 struct enable_trigger_data {
1070 struct trace_event_file *file;
1071 bool enable;
1074 static void
1075 event_enable_trigger(struct event_trigger_data *data, void *rec)
1077 struct enable_trigger_data *enable_data = data->private_data;
1079 if (enable_data->enable)
1080 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1081 else
1082 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1085 static void
1086 event_enable_count_trigger(struct event_trigger_data *data, void *rec)
1088 struct enable_trigger_data *enable_data = data->private_data;
1090 if (!data->count)
1091 return;
1093 /* Skip if the event is in a state we want to switch to */
1094 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1095 return;
1097 if (data->count != -1)
1098 (data->count)--;
1100 event_enable_trigger(data, rec);
1103 static int
1104 event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1105 struct event_trigger_data *data)
1107 struct enable_trigger_data *enable_data = data->private_data;
1109 seq_printf(m, "%s:%s:%s",
1110 enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1111 enable_data->file->event_call->class->system,
1112 trace_event_name(enable_data->file->event_call));
1114 if (data->count == -1)
1115 seq_puts(m, ":unlimited");
1116 else
1117 seq_printf(m, ":count=%ld", data->count);
1119 if (data->filter_str)
1120 seq_printf(m, " if %s\n", data->filter_str);
1121 else
1122 seq_putc(m, '\n');
1124 return 0;
1127 static void
1128 event_enable_trigger_free(struct event_trigger_ops *ops,
1129 struct event_trigger_data *data)
1131 struct enable_trigger_data *enable_data = data->private_data;
1133 if (WARN_ON_ONCE(data->ref <= 0))
1134 return;
1136 data->ref--;
1137 if (!data->ref) {
1138 /* Remove the SOFT_MODE flag */
1139 trace_event_enable_disable(enable_data->file, 0, 1);
1140 module_put(enable_data->file->event_call->mod);
1141 trigger_data_free(data);
1142 kfree(enable_data);
1146 static struct event_trigger_ops event_enable_trigger_ops = {
1147 .func = event_enable_trigger,
1148 .print = event_enable_trigger_print,
1149 .init = event_trigger_init,
1150 .free = event_enable_trigger_free,
1153 static struct event_trigger_ops event_enable_count_trigger_ops = {
1154 .func = event_enable_count_trigger,
1155 .print = event_enable_trigger_print,
1156 .init = event_trigger_init,
1157 .free = event_enable_trigger_free,
1160 static struct event_trigger_ops event_disable_trigger_ops = {
1161 .func = event_enable_trigger,
1162 .print = event_enable_trigger_print,
1163 .init = event_trigger_init,
1164 .free = event_enable_trigger_free,
1167 static struct event_trigger_ops event_disable_count_trigger_ops = {
1168 .func = event_enable_count_trigger,
1169 .print = event_enable_trigger_print,
1170 .init = event_trigger_init,
1171 .free = event_enable_trigger_free,
1174 static int
1175 event_enable_trigger_func(struct event_command *cmd_ops,
1176 struct trace_event_file *file,
1177 char *glob, char *cmd, char *param)
1179 struct trace_event_file *event_enable_file;
1180 struct enable_trigger_data *enable_data;
1181 struct event_trigger_data *trigger_data;
1182 struct event_trigger_ops *trigger_ops;
1183 struct trace_array *tr = file->tr;
1184 const char *system;
1185 const char *event;
1186 char *trigger;
1187 char *number;
1188 bool enable;
1189 int ret;
1191 if (!param)
1192 return -EINVAL;
1194 /* separate the trigger from the filter (s:e:n [if filter]) */
1195 trigger = strsep(&param, " \t");
1196 if (!trigger)
1197 return -EINVAL;
1199 system = strsep(&trigger, ":");
1200 if (!trigger)
1201 return -EINVAL;
1203 event = strsep(&trigger, ":");
1205 ret = -EINVAL;
1206 event_enable_file = find_event_file(tr, system, event);
1207 if (!event_enable_file)
1208 goto out;
1210 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1212 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1214 ret = -ENOMEM;
1215 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1216 if (!trigger_data)
1217 goto out;
1219 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1220 if (!enable_data) {
1221 kfree(trigger_data);
1222 goto out;
1225 trigger_data->count = -1;
1226 trigger_data->ops = trigger_ops;
1227 trigger_data->cmd_ops = cmd_ops;
1228 INIT_LIST_HEAD(&trigger_data->list);
1229 RCU_INIT_POINTER(trigger_data->filter, NULL);
1231 enable_data->enable = enable;
1232 enable_data->file = event_enable_file;
1233 trigger_data->private_data = enable_data;
1235 if (glob[0] == '!') {
1236 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1237 kfree(trigger_data);
1238 kfree(enable_data);
1239 ret = 0;
1240 goto out;
1243 if (trigger) {
1244 number = strsep(&trigger, ":");
1246 ret = -EINVAL;
1247 if (!strlen(number))
1248 goto out_free;
1251 * We use the callback data field (which is a pointer)
1252 * as our counter.
1254 ret = kstrtoul(number, 0, &trigger_data->count);
1255 if (ret)
1256 goto out_free;
1259 if (!param) /* if param is non-empty, it's supposed to be a filter */
1260 goto out_reg;
1262 if (!cmd_ops->set_filter)
1263 goto out_reg;
1265 ret = cmd_ops->set_filter(param, trigger_data, file);
1266 if (ret < 0)
1267 goto out_free;
1269 out_reg:
1270 /* Don't let event modules unload while probe registered */
1271 ret = try_module_get(event_enable_file->event_call->mod);
1272 if (!ret) {
1273 ret = -EBUSY;
1274 goto out_free;
1277 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1278 if (ret < 0)
1279 goto out_put;
1280 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1282 * The above returns on success the # of functions enabled,
1283 * but if it didn't find any functions it returns zero.
1284 * Consider no functions a failure too.
1286 if (!ret) {
1287 ret = -ENOENT;
1288 goto out_disable;
1289 } else if (ret < 0)
1290 goto out_disable;
1291 /* Just return zero, not the number of enabled functions */
1292 ret = 0;
1293 out:
1294 return ret;
1296 out_disable:
1297 trace_event_enable_disable(event_enable_file, 0, 1);
1298 out_put:
1299 module_put(event_enable_file->event_call->mod);
1300 out_free:
1301 if (cmd_ops->set_filter)
1302 cmd_ops->set_filter(NULL, trigger_data, NULL);
1303 kfree(trigger_data);
1304 kfree(enable_data);
1305 goto out;
1308 static int event_enable_register_trigger(char *glob,
1309 struct event_trigger_ops *ops,
1310 struct event_trigger_data *data,
1311 struct trace_event_file *file)
1313 struct enable_trigger_data *enable_data = data->private_data;
1314 struct enable_trigger_data *test_enable_data;
1315 struct event_trigger_data *test;
1316 int ret = 0;
1318 list_for_each_entry_rcu(test, &file->triggers, list) {
1319 test_enable_data = test->private_data;
1320 if (test_enable_data &&
1321 (test_enable_data->file == enable_data->file)) {
1322 ret = -EEXIST;
1323 goto out;
1327 if (data->ops->init) {
1328 ret = data->ops->init(data->ops, data);
1329 if (ret < 0)
1330 goto out;
1333 list_add_rcu(&data->list, &file->triggers);
1334 ret++;
1336 update_cond_flag(file);
1337 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1338 list_del_rcu(&data->list);
1339 update_cond_flag(file);
1340 ret--;
1342 out:
1343 return ret;
1346 static void event_enable_unregister_trigger(char *glob,
1347 struct event_trigger_ops *ops,
1348 struct event_trigger_data *test,
1349 struct trace_event_file *file)
1351 struct enable_trigger_data *test_enable_data = test->private_data;
1352 struct enable_trigger_data *enable_data;
1353 struct event_trigger_data *data;
1354 bool unregistered = false;
1356 list_for_each_entry_rcu(data, &file->triggers, list) {
1357 enable_data = data->private_data;
1358 if (enable_data &&
1359 (enable_data->file == test_enable_data->file)) {
1360 unregistered = true;
1361 list_del_rcu(&data->list);
1362 trace_event_trigger_enable_disable(file, 0);
1363 update_cond_flag(file);
1364 break;
1368 if (unregistered && data->ops->free)
1369 data->ops->free(data->ops, data);
1372 static struct event_trigger_ops *
1373 event_enable_get_trigger_ops(char *cmd, char *param)
1375 struct event_trigger_ops *ops;
1376 bool enable;
1378 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1380 if (enable)
1381 ops = param ? &event_enable_count_trigger_ops :
1382 &event_enable_trigger_ops;
1383 else
1384 ops = param ? &event_disable_count_trigger_ops :
1385 &event_disable_trigger_ops;
1387 return ops;
1390 static struct event_command trigger_enable_cmd = {
1391 .name = ENABLE_EVENT_STR,
1392 .trigger_type = ETT_EVENT_ENABLE,
1393 .func = event_enable_trigger_func,
1394 .reg = event_enable_register_trigger,
1395 .unreg = event_enable_unregister_trigger,
1396 .get_trigger_ops = event_enable_get_trigger_ops,
1397 .set_filter = set_trigger_filter,
1400 static struct event_command trigger_disable_cmd = {
1401 .name = DISABLE_EVENT_STR,
1402 .trigger_type = ETT_EVENT_ENABLE,
1403 .func = event_enable_trigger_func,
1404 .reg = event_enable_register_trigger,
1405 .unreg = event_enable_unregister_trigger,
1406 .get_trigger_ops = event_enable_get_trigger_ops,
1407 .set_filter = set_trigger_filter,
1410 static __init void unregister_trigger_enable_disable_cmds(void)
1412 unregister_event_command(&trigger_enable_cmd);
1413 unregister_event_command(&trigger_disable_cmd);
1416 static __init int register_trigger_enable_disable_cmds(void)
1418 int ret;
1420 ret = register_event_command(&trigger_enable_cmd);
1421 if (WARN_ON(ret < 0))
1422 return ret;
1423 ret = register_event_command(&trigger_disable_cmd);
1424 if (WARN_ON(ret < 0))
1425 unregister_trigger_enable_disable_cmds();
1427 return ret;
1430 static __init int register_trigger_traceon_traceoff_cmds(void)
1432 int ret;
1434 ret = register_event_command(&trigger_traceon_cmd);
1435 if (WARN_ON(ret < 0))
1436 return ret;
1437 ret = register_event_command(&trigger_traceoff_cmd);
1438 if (WARN_ON(ret < 0))
1439 unregister_trigger_traceon_traceoff_cmds();
1441 return ret;
1444 __init int register_trigger_cmds(void)
1446 register_trigger_traceon_traceoff_cmds();
1447 register_trigger_snapshot_cmd();
1448 register_trigger_stacktrace_cmd();
1449 register_trigger_enable_disable_cmds();
1451 return 0;