Staging: most: Do not print message if kzalloc() failed.
[linux/fpc-iii.git] / kernel / trace / trace_events_trigger.c
blobb38f617b618185d907670ca77774b3332bdb63db
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 static void
32 trigger_data_free(struct event_trigger_data *data)
34 if (data->cmd_ops->set_filter)
35 data->cmd_ops->set_filter(NULL, data, NULL);
37 synchronize_sched(); /* make sure current triggers exit before free */
38 kfree(data);
41 /**
42 * event_triggers_call - Call triggers associated with a trace event
43 * @file: The trace_event_file associated with the event
44 * @rec: The trace entry for the event, NULL for unconditional invocation
46 * For each trigger associated with an event, invoke the trigger
47 * function registered with the associated trigger command. If rec is
48 * non-NULL, it means that the trigger requires further processing and
49 * shouldn't be unconditionally invoked. If rec is non-NULL and the
50 * trigger has a filter associated with it, rec will checked against
51 * the filter and if the record matches the trigger will be invoked.
52 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
53 * in any case until the current event is written, the trigger
54 * function isn't invoked but the bit associated with the deferred
55 * trigger is set in the return value.
57 * Returns an enum event_trigger_type value containing a set bit for
58 * any trigger that should be deferred, ETT_NONE if nothing to defer.
60 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
62 * Return: an enum event_trigger_type value containing a set bit for
63 * any trigger that should be deferred, ETT_NONE if nothing to defer.
65 enum event_trigger_type
66 event_triggers_call(struct trace_event_file *file, void *rec)
68 struct event_trigger_data *data;
69 enum event_trigger_type tt = ETT_NONE;
70 struct event_filter *filter;
72 if (list_empty(&file->triggers))
73 return tt;
75 list_for_each_entry_rcu(data, &file->triggers, list) {
76 if (!rec) {
77 data->ops->func(data);
78 continue;
80 filter = rcu_dereference_sched(data->filter);
81 if (filter && !filter_match_preds(filter, rec))
82 continue;
83 if (data->cmd_ops->post_trigger) {
84 tt |= data->cmd_ops->trigger_type;
85 continue;
87 data->ops->func(data);
89 return tt;
91 EXPORT_SYMBOL_GPL(event_triggers_call);
93 /**
94 * event_triggers_post_call - Call 'post_triggers' for a trace event
95 * @file: The trace_event_file associated with the event
96 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
98 * For each trigger associated with an event, invoke the trigger
99 * function registered with the associated trigger command, if the
100 * corresponding bit is set in the tt enum passed into this function.
101 * See @event_triggers_call for details on how those bits are set.
103 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
105 void
106 event_triggers_post_call(struct trace_event_file *file,
107 enum event_trigger_type tt)
109 struct event_trigger_data *data;
111 list_for_each_entry_rcu(data, &file->triggers, list) {
112 if (data->cmd_ops->trigger_type & tt)
113 data->ops->func(data);
116 EXPORT_SYMBOL_GPL(event_triggers_post_call);
118 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
120 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
122 struct trace_event_file *event_file = event_file_data(m->private);
124 if (t == SHOW_AVAILABLE_TRIGGERS)
125 return NULL;
127 return seq_list_next(t, &event_file->triggers, pos);
130 static void *trigger_start(struct seq_file *m, loff_t *pos)
132 struct trace_event_file *event_file;
134 /* ->stop() is called even if ->start() fails */
135 mutex_lock(&event_mutex);
136 event_file = event_file_data(m->private);
137 if (unlikely(!event_file))
138 return ERR_PTR(-ENODEV);
140 if (list_empty(&event_file->triggers))
141 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
143 return seq_list_start(&event_file->triggers, *pos);
146 static void trigger_stop(struct seq_file *m, void *t)
148 mutex_unlock(&event_mutex);
151 static int trigger_show(struct seq_file *m, void *v)
153 struct event_trigger_data *data;
154 struct event_command *p;
156 if (v == SHOW_AVAILABLE_TRIGGERS) {
157 seq_puts(m, "# Available triggers:\n");
158 seq_putc(m, '#');
159 mutex_lock(&trigger_cmd_mutex);
160 list_for_each_entry_reverse(p, &trigger_commands, list)
161 seq_printf(m, " %s", p->name);
162 seq_putc(m, '\n');
163 mutex_unlock(&trigger_cmd_mutex);
164 return 0;
167 data = list_entry(v, struct event_trigger_data, list);
168 data->ops->print(m, data->ops, data);
170 return 0;
173 static const struct seq_operations event_triggers_seq_ops = {
174 .start = trigger_start,
175 .next = trigger_next,
176 .stop = trigger_stop,
177 .show = trigger_show,
180 static int event_trigger_regex_open(struct inode *inode, struct file *file)
182 int ret = 0;
184 mutex_lock(&event_mutex);
186 if (unlikely(!event_file_data(file))) {
187 mutex_unlock(&event_mutex);
188 return -ENODEV;
191 if (file->f_mode & FMODE_READ) {
192 ret = seq_open(file, &event_triggers_seq_ops);
193 if (!ret) {
194 struct seq_file *m = file->private_data;
195 m->private = file;
199 mutex_unlock(&event_mutex);
201 return ret;
204 static int trigger_process_regex(struct trace_event_file *file, char *buff)
206 char *command, *next = buff;
207 struct event_command *p;
208 int ret = -EINVAL;
210 command = strsep(&next, ": \t");
211 command = (command[0] != '!') ? command : command + 1;
213 mutex_lock(&trigger_cmd_mutex);
214 list_for_each_entry(p, &trigger_commands, list) {
215 if (strcmp(p->name, command) == 0) {
216 ret = p->func(p, file, buff, command, next);
217 goto out_unlock;
220 out_unlock:
221 mutex_unlock(&trigger_cmd_mutex);
223 return ret;
226 static ssize_t event_trigger_regex_write(struct file *file,
227 const char __user *ubuf,
228 size_t cnt, loff_t *ppos)
230 struct trace_event_file *event_file;
231 ssize_t ret;
232 char *buf;
234 if (!cnt)
235 return 0;
237 if (cnt >= PAGE_SIZE)
238 return -EINVAL;
240 buf = memdup_user_nul(ubuf, cnt);
241 if (IS_ERR(buf))
242 return PTR_ERR(buf);
244 strim(buf);
246 mutex_lock(&event_mutex);
247 event_file = event_file_data(file);
248 if (unlikely(!event_file)) {
249 mutex_unlock(&event_mutex);
250 kfree(buf);
251 return -ENODEV;
253 ret = trigger_process_regex(event_file, buf);
254 mutex_unlock(&event_mutex);
256 kfree(buf);
257 if (ret < 0)
258 goto out;
260 *ppos += cnt;
261 ret = cnt;
262 out:
263 return ret;
266 static int event_trigger_regex_release(struct inode *inode, struct file *file)
268 mutex_lock(&event_mutex);
270 if (file->f_mode & FMODE_READ)
271 seq_release(inode, file);
273 mutex_unlock(&event_mutex);
275 return 0;
278 static ssize_t
279 event_trigger_write(struct file *filp, const char __user *ubuf,
280 size_t cnt, loff_t *ppos)
282 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
285 static int
286 event_trigger_open(struct inode *inode, struct file *filp)
288 return event_trigger_regex_open(inode, filp);
291 static int
292 event_trigger_release(struct inode *inode, struct file *file)
294 return event_trigger_regex_release(inode, file);
297 const struct file_operations event_trigger_fops = {
298 .open = event_trigger_open,
299 .read = seq_read,
300 .write = event_trigger_write,
301 .llseek = tracing_lseek,
302 .release = event_trigger_release,
306 * Currently we only register event commands from __init, so mark this
307 * __init too.
309 static __init int register_event_command(struct event_command *cmd)
311 struct event_command *p;
312 int ret = 0;
314 mutex_lock(&trigger_cmd_mutex);
315 list_for_each_entry(p, &trigger_commands, list) {
316 if (strcmp(cmd->name, p->name) == 0) {
317 ret = -EBUSY;
318 goto out_unlock;
321 list_add(&cmd->list, &trigger_commands);
322 out_unlock:
323 mutex_unlock(&trigger_cmd_mutex);
325 return ret;
329 * Currently we only unregister event commands from __init, so mark
330 * this __init too.
332 static __init int unregister_event_command(struct event_command *cmd)
334 struct event_command *p, *n;
335 int ret = -ENODEV;
337 mutex_lock(&trigger_cmd_mutex);
338 list_for_each_entry_safe(p, n, &trigger_commands, list) {
339 if (strcmp(cmd->name, p->name) == 0) {
340 ret = 0;
341 list_del_init(&p->list);
342 goto out_unlock;
345 out_unlock:
346 mutex_unlock(&trigger_cmd_mutex);
348 return ret;
352 * event_trigger_print - Generic event_trigger_ops @print implementation
353 * @name: The name of the event trigger
354 * @m: The seq_file being printed to
355 * @data: Trigger-specific data
356 * @filter_str: filter_str to print, if present
358 * Common implementation for event triggers to print themselves.
360 * Usually wrapped by a function that simply sets the @name of the
361 * trigger command and then invokes this.
363 * Return: 0 on success, errno otherwise
365 static int
366 event_trigger_print(const char *name, struct seq_file *m,
367 void *data, char *filter_str)
369 long count = (long)data;
371 seq_puts(m, name);
373 if (count == -1)
374 seq_puts(m, ":unlimited");
375 else
376 seq_printf(m, ":count=%ld", count);
378 if (filter_str)
379 seq_printf(m, " if %s\n", filter_str);
380 else
381 seq_putc(m, '\n');
383 return 0;
387 * event_trigger_init - Generic event_trigger_ops @init implementation
388 * @ops: The trigger ops associated with the trigger
389 * @data: Trigger-specific data
391 * Common implementation of event trigger initialization.
393 * Usually used directly as the @init method in event trigger
394 * implementations.
396 * Return: 0 on success, errno otherwise
398 static int
399 event_trigger_init(struct event_trigger_ops *ops,
400 struct event_trigger_data *data)
402 data->ref++;
403 return 0;
407 * event_trigger_free - Generic event_trigger_ops @free implementation
408 * @ops: The trigger ops associated with the trigger
409 * @data: Trigger-specific data
411 * Common implementation of event trigger de-initialization.
413 * Usually used directly as the @free method in event trigger
414 * implementations.
416 static void
417 event_trigger_free(struct event_trigger_ops *ops,
418 struct event_trigger_data *data)
420 if (WARN_ON_ONCE(data->ref <= 0))
421 return;
423 data->ref--;
424 if (!data->ref)
425 trigger_data_free(data);
428 static int trace_event_trigger_enable_disable(struct trace_event_file *file,
429 int trigger_enable)
431 int ret = 0;
433 if (trigger_enable) {
434 if (atomic_inc_return(&file->tm_ref) > 1)
435 return ret;
436 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
437 ret = trace_event_enable_disable(file, 1, 1);
438 } else {
439 if (atomic_dec_return(&file->tm_ref) > 0)
440 return ret;
441 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
442 ret = trace_event_enable_disable(file, 0, 1);
445 return ret;
449 * clear_event_triggers - Clear all triggers associated with a trace array
450 * @tr: The trace array to clear
452 * For each trigger, the triggering event has its tm_ref decremented
453 * via trace_event_trigger_enable_disable(), and any associated event
454 * (in the case of enable/disable_event triggers) will have its sm_ref
455 * decremented via free()->trace_event_enable_disable(). That
456 * combination effectively reverses the soft-mode/trigger state added
457 * by trigger registration.
459 * Must be called with event_mutex held.
461 void
462 clear_event_triggers(struct trace_array *tr)
464 struct trace_event_file *file;
466 list_for_each_entry(file, &tr->events, list) {
467 struct event_trigger_data *data;
468 list_for_each_entry_rcu(data, &file->triggers, list) {
469 trace_event_trigger_enable_disable(file, 0);
470 if (data->ops->free)
471 data->ops->free(data->ops, data);
477 * update_cond_flag - Set or reset the TRIGGER_COND bit
478 * @file: The trace_event_file associated with the event
480 * If an event has triggers and any of those triggers has a filter or
481 * a post_trigger, trigger invocation needs to be deferred until after
482 * the current event has logged its data, and the event should have
483 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
484 * cleared.
486 static void update_cond_flag(struct trace_event_file *file)
488 struct event_trigger_data *data;
489 bool set_cond = false;
491 list_for_each_entry_rcu(data, &file->triggers, list) {
492 if (data->filter || data->cmd_ops->post_trigger) {
493 set_cond = true;
494 break;
498 if (set_cond)
499 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
500 else
501 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
505 * register_trigger - Generic event_command @reg implementation
506 * @glob: The raw string used to register the trigger
507 * @ops: The trigger ops associated with the trigger
508 * @data: Trigger-specific data to associate with the trigger
509 * @file: The trace_event_file associated with the event
511 * Common implementation for event trigger registration.
513 * Usually used directly as the @reg method in event command
514 * implementations.
516 * Return: 0 on success, errno otherwise
518 static int register_trigger(char *glob, struct event_trigger_ops *ops,
519 struct event_trigger_data *data,
520 struct trace_event_file *file)
522 struct event_trigger_data *test;
523 int ret = 0;
525 list_for_each_entry_rcu(test, &file->triggers, list) {
526 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
527 ret = -EEXIST;
528 goto out;
532 if (data->ops->init) {
533 ret = data->ops->init(data->ops, data);
534 if (ret < 0)
535 goto out;
538 list_add_rcu(&data->list, &file->triggers);
539 ret++;
541 update_cond_flag(file);
542 if (trace_event_trigger_enable_disable(file, 1) < 0) {
543 list_del_rcu(&data->list);
544 update_cond_flag(file);
545 ret--;
547 out:
548 return ret;
552 * unregister_trigger - Generic event_command @unreg implementation
553 * @glob: The raw string used to register the trigger
554 * @ops: The trigger ops associated with the trigger
555 * @test: Trigger-specific data used to find the trigger to remove
556 * @file: The trace_event_file associated with the event
558 * Common implementation for event trigger unregistration.
560 * Usually used directly as the @unreg method in event command
561 * implementations.
563 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
564 struct event_trigger_data *test,
565 struct trace_event_file *file)
567 struct event_trigger_data *data;
568 bool unregistered = false;
570 list_for_each_entry_rcu(data, &file->triggers, list) {
571 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
572 unregistered = true;
573 list_del_rcu(&data->list);
574 trace_event_trigger_enable_disable(file, 0);
575 update_cond_flag(file);
576 break;
580 if (unregistered && data->ops->free)
581 data->ops->free(data->ops, data);
585 * event_trigger_callback - Generic event_command @func implementation
586 * @cmd_ops: The command ops, used for trigger registration
587 * @file: The trace_event_file associated with the event
588 * @glob: The raw string used to register the trigger
589 * @cmd: The cmd portion of the string used to register the trigger
590 * @param: The params portion of the string used to register the trigger
592 * Common implementation for event command parsing and trigger
593 * instantiation.
595 * Usually used directly as the @func method in event command
596 * implementations.
598 * Return: 0 on success, errno otherwise
600 static int
601 event_trigger_callback(struct event_command *cmd_ops,
602 struct trace_event_file *file,
603 char *glob, char *cmd, char *param)
605 struct event_trigger_data *trigger_data;
606 struct event_trigger_ops *trigger_ops;
607 char *trigger = NULL;
608 char *number;
609 int ret;
611 /* separate the trigger from the filter (t:n [if filter]) */
612 if (param && isdigit(param[0]))
613 trigger = strsep(&param, " \t");
615 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
617 ret = -ENOMEM;
618 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
619 if (!trigger_data)
620 goto out;
622 trigger_data->count = -1;
623 trigger_data->ops = trigger_ops;
624 trigger_data->cmd_ops = cmd_ops;
625 INIT_LIST_HEAD(&trigger_data->list);
627 if (glob[0] == '!') {
628 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
629 kfree(trigger_data);
630 ret = 0;
631 goto out;
634 if (trigger) {
635 number = strsep(&trigger, ":");
637 ret = -EINVAL;
638 if (!strlen(number))
639 goto out_free;
642 * We use the callback data field (which is a pointer)
643 * as our counter.
645 ret = kstrtoul(number, 0, &trigger_data->count);
646 if (ret)
647 goto out_free;
650 if (!param) /* if param is non-empty, it's supposed to be a filter */
651 goto out_reg;
653 if (!cmd_ops->set_filter)
654 goto out_reg;
656 ret = cmd_ops->set_filter(param, trigger_data, file);
657 if (ret < 0)
658 goto out_free;
660 out_reg:
661 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
663 * The above returns on success the # of functions enabled,
664 * but if it didn't find any functions it returns zero.
665 * Consider no functions a failure too.
667 if (!ret) {
668 ret = -ENOENT;
669 goto out_free;
670 } else if (ret < 0)
671 goto out_free;
672 ret = 0;
673 out:
674 return ret;
676 out_free:
677 if (cmd_ops->set_filter)
678 cmd_ops->set_filter(NULL, trigger_data, NULL);
679 kfree(trigger_data);
680 goto out;
684 * set_trigger_filter - Generic event_command @set_filter implementation
685 * @filter_str: The filter string for the trigger, NULL to remove filter
686 * @trigger_data: Trigger-specific data
687 * @file: The trace_event_file associated with the event
689 * Common implementation for event command filter parsing and filter
690 * instantiation.
692 * Usually used directly as the @set_filter method in event command
693 * implementations.
695 * Also used to remove a filter (if filter_str = NULL).
697 * Return: 0 on success, errno otherwise
699 static int set_trigger_filter(char *filter_str,
700 struct event_trigger_data *trigger_data,
701 struct trace_event_file *file)
703 struct event_trigger_data *data = trigger_data;
704 struct event_filter *filter = NULL, *tmp;
705 int ret = -EINVAL;
706 char *s;
708 if (!filter_str) /* clear the current filter */
709 goto assign;
711 s = strsep(&filter_str, " \t");
713 if (!strlen(s) || strcmp(s, "if") != 0)
714 goto out;
716 if (!filter_str)
717 goto out;
719 /* The filter is for the 'trigger' event, not the triggered event */
720 ret = create_event_filter(file->event_call, filter_str, false, &filter);
721 if (ret)
722 goto out;
723 assign:
724 tmp = rcu_access_pointer(data->filter);
726 rcu_assign_pointer(data->filter, filter);
728 if (tmp) {
729 /* Make sure the call is done with the filter */
730 synchronize_sched();
731 free_event_filter(tmp);
734 kfree(data->filter_str);
735 data->filter_str = NULL;
737 if (filter_str) {
738 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
739 if (!data->filter_str) {
740 free_event_filter(rcu_access_pointer(data->filter));
741 data->filter = NULL;
742 ret = -ENOMEM;
745 out:
746 return ret;
749 static void
750 traceon_trigger(struct event_trigger_data *data)
752 if (tracing_is_on())
753 return;
755 tracing_on();
758 static void
759 traceon_count_trigger(struct event_trigger_data *data)
761 if (tracing_is_on())
762 return;
764 if (!data->count)
765 return;
767 if (data->count != -1)
768 (data->count)--;
770 tracing_on();
773 static void
774 traceoff_trigger(struct event_trigger_data *data)
776 if (!tracing_is_on())
777 return;
779 tracing_off();
782 static void
783 traceoff_count_trigger(struct event_trigger_data *data)
785 if (!tracing_is_on())
786 return;
788 if (!data->count)
789 return;
791 if (data->count != -1)
792 (data->count)--;
794 tracing_off();
797 static int
798 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
799 struct event_trigger_data *data)
801 return event_trigger_print("traceon", m, (void *)data->count,
802 data->filter_str);
805 static int
806 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
807 struct event_trigger_data *data)
809 return event_trigger_print("traceoff", m, (void *)data->count,
810 data->filter_str);
813 static struct event_trigger_ops traceon_trigger_ops = {
814 .func = traceon_trigger,
815 .print = traceon_trigger_print,
816 .init = event_trigger_init,
817 .free = event_trigger_free,
820 static struct event_trigger_ops traceon_count_trigger_ops = {
821 .func = traceon_count_trigger,
822 .print = traceon_trigger_print,
823 .init = event_trigger_init,
824 .free = event_trigger_free,
827 static struct event_trigger_ops traceoff_trigger_ops = {
828 .func = traceoff_trigger,
829 .print = traceoff_trigger_print,
830 .init = event_trigger_init,
831 .free = event_trigger_free,
834 static struct event_trigger_ops traceoff_count_trigger_ops = {
835 .func = traceoff_count_trigger,
836 .print = traceoff_trigger_print,
837 .init = event_trigger_init,
838 .free = event_trigger_free,
841 static struct event_trigger_ops *
842 onoff_get_trigger_ops(char *cmd, char *param)
844 struct event_trigger_ops *ops;
846 /* we register both traceon and traceoff to this callback */
847 if (strcmp(cmd, "traceon") == 0)
848 ops = param ? &traceon_count_trigger_ops :
849 &traceon_trigger_ops;
850 else
851 ops = param ? &traceoff_count_trigger_ops :
852 &traceoff_trigger_ops;
854 return ops;
857 static struct event_command trigger_traceon_cmd = {
858 .name = "traceon",
859 .trigger_type = ETT_TRACE_ONOFF,
860 .func = event_trigger_callback,
861 .reg = register_trigger,
862 .unreg = unregister_trigger,
863 .get_trigger_ops = onoff_get_trigger_ops,
864 .set_filter = set_trigger_filter,
867 static struct event_command trigger_traceoff_cmd = {
868 .name = "traceoff",
869 .trigger_type = ETT_TRACE_ONOFF,
870 .func = event_trigger_callback,
871 .reg = register_trigger,
872 .unreg = unregister_trigger,
873 .get_trigger_ops = onoff_get_trigger_ops,
874 .set_filter = set_trigger_filter,
877 #ifdef CONFIG_TRACER_SNAPSHOT
878 static void
879 snapshot_trigger(struct event_trigger_data *data)
881 tracing_snapshot();
884 static void
885 snapshot_count_trigger(struct event_trigger_data *data)
887 if (!data->count)
888 return;
890 if (data->count != -1)
891 (data->count)--;
893 snapshot_trigger(data);
896 static int
897 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
898 struct event_trigger_data *data,
899 struct trace_event_file *file)
901 int ret = register_trigger(glob, ops, data, file);
903 if (ret > 0 && tracing_alloc_snapshot() != 0) {
904 unregister_trigger(glob, ops, data, file);
905 ret = 0;
908 return ret;
911 static int
912 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
913 struct event_trigger_data *data)
915 return event_trigger_print("snapshot", m, (void *)data->count,
916 data->filter_str);
919 static struct event_trigger_ops snapshot_trigger_ops = {
920 .func = snapshot_trigger,
921 .print = snapshot_trigger_print,
922 .init = event_trigger_init,
923 .free = event_trigger_free,
926 static struct event_trigger_ops snapshot_count_trigger_ops = {
927 .func = snapshot_count_trigger,
928 .print = snapshot_trigger_print,
929 .init = event_trigger_init,
930 .free = event_trigger_free,
933 static struct event_trigger_ops *
934 snapshot_get_trigger_ops(char *cmd, char *param)
936 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
939 static struct event_command trigger_snapshot_cmd = {
940 .name = "snapshot",
941 .trigger_type = ETT_SNAPSHOT,
942 .func = event_trigger_callback,
943 .reg = register_snapshot_trigger,
944 .unreg = unregister_trigger,
945 .get_trigger_ops = snapshot_get_trigger_ops,
946 .set_filter = set_trigger_filter,
949 static __init int register_trigger_snapshot_cmd(void)
951 int ret;
953 ret = register_event_command(&trigger_snapshot_cmd);
954 WARN_ON(ret < 0);
956 return ret;
958 #else
959 static __init int register_trigger_snapshot_cmd(void) { return 0; }
960 #endif /* CONFIG_TRACER_SNAPSHOT */
962 #ifdef CONFIG_STACKTRACE
964 * Skip 3:
965 * stacktrace_trigger()
966 * event_triggers_post_call()
967 * trace_event_raw_event_xxx()
969 #define STACK_SKIP 3
971 static void
972 stacktrace_trigger(struct event_trigger_data *data)
974 trace_dump_stack(STACK_SKIP);
977 static void
978 stacktrace_count_trigger(struct event_trigger_data *data)
980 if (!data->count)
981 return;
983 if (data->count != -1)
984 (data->count)--;
986 stacktrace_trigger(data);
989 static int
990 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
991 struct event_trigger_data *data)
993 return event_trigger_print("stacktrace", m, (void *)data->count,
994 data->filter_str);
997 static struct event_trigger_ops stacktrace_trigger_ops = {
998 .func = stacktrace_trigger,
999 .print = stacktrace_trigger_print,
1000 .init = event_trigger_init,
1001 .free = event_trigger_free,
1004 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1005 .func = stacktrace_count_trigger,
1006 .print = stacktrace_trigger_print,
1007 .init = event_trigger_init,
1008 .free = event_trigger_free,
1011 static struct event_trigger_ops *
1012 stacktrace_get_trigger_ops(char *cmd, char *param)
1014 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1017 static struct event_command trigger_stacktrace_cmd = {
1018 .name = "stacktrace",
1019 .trigger_type = ETT_STACKTRACE,
1020 .post_trigger = true,
1021 .func = event_trigger_callback,
1022 .reg = register_trigger,
1023 .unreg = unregister_trigger,
1024 .get_trigger_ops = stacktrace_get_trigger_ops,
1025 .set_filter = set_trigger_filter,
1028 static __init int register_trigger_stacktrace_cmd(void)
1030 int ret;
1032 ret = register_event_command(&trigger_stacktrace_cmd);
1033 WARN_ON(ret < 0);
1035 return ret;
1037 #else
1038 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1039 #endif /* CONFIG_STACKTRACE */
1041 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1043 unregister_event_command(&trigger_traceon_cmd);
1044 unregister_event_command(&trigger_traceoff_cmd);
1047 /* Avoid typos */
1048 #define ENABLE_EVENT_STR "enable_event"
1049 #define DISABLE_EVENT_STR "disable_event"
1051 struct enable_trigger_data {
1052 struct trace_event_file *file;
1053 bool enable;
1056 static void
1057 event_enable_trigger(struct event_trigger_data *data)
1059 struct enable_trigger_data *enable_data = data->private_data;
1061 if (enable_data->enable)
1062 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1063 else
1064 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1067 static void
1068 event_enable_count_trigger(struct event_trigger_data *data)
1070 struct enable_trigger_data *enable_data = data->private_data;
1072 if (!data->count)
1073 return;
1075 /* Skip if the event is in a state we want to switch to */
1076 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1077 return;
1079 if (data->count != -1)
1080 (data->count)--;
1082 event_enable_trigger(data);
1085 static int
1086 event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1087 struct event_trigger_data *data)
1089 struct enable_trigger_data *enable_data = data->private_data;
1091 seq_printf(m, "%s:%s:%s",
1092 enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1093 enable_data->file->event_call->class->system,
1094 trace_event_name(enable_data->file->event_call));
1096 if (data->count == -1)
1097 seq_puts(m, ":unlimited");
1098 else
1099 seq_printf(m, ":count=%ld", data->count);
1101 if (data->filter_str)
1102 seq_printf(m, " if %s\n", data->filter_str);
1103 else
1104 seq_putc(m, '\n');
1106 return 0;
1109 static void
1110 event_enable_trigger_free(struct event_trigger_ops *ops,
1111 struct event_trigger_data *data)
1113 struct enable_trigger_data *enable_data = data->private_data;
1115 if (WARN_ON_ONCE(data->ref <= 0))
1116 return;
1118 data->ref--;
1119 if (!data->ref) {
1120 /* Remove the SOFT_MODE flag */
1121 trace_event_enable_disable(enable_data->file, 0, 1);
1122 module_put(enable_data->file->event_call->mod);
1123 trigger_data_free(data);
1124 kfree(enable_data);
1128 static struct event_trigger_ops event_enable_trigger_ops = {
1129 .func = event_enable_trigger,
1130 .print = event_enable_trigger_print,
1131 .init = event_trigger_init,
1132 .free = event_enable_trigger_free,
1135 static struct event_trigger_ops event_enable_count_trigger_ops = {
1136 .func = event_enable_count_trigger,
1137 .print = event_enable_trigger_print,
1138 .init = event_trigger_init,
1139 .free = event_enable_trigger_free,
1142 static struct event_trigger_ops event_disable_trigger_ops = {
1143 .func = event_enable_trigger,
1144 .print = event_enable_trigger_print,
1145 .init = event_trigger_init,
1146 .free = event_enable_trigger_free,
1149 static struct event_trigger_ops event_disable_count_trigger_ops = {
1150 .func = event_enable_count_trigger,
1151 .print = event_enable_trigger_print,
1152 .init = event_trigger_init,
1153 .free = event_enable_trigger_free,
1156 static int
1157 event_enable_trigger_func(struct event_command *cmd_ops,
1158 struct trace_event_file *file,
1159 char *glob, char *cmd, char *param)
1161 struct trace_event_file *event_enable_file;
1162 struct enable_trigger_data *enable_data;
1163 struct event_trigger_data *trigger_data;
1164 struct event_trigger_ops *trigger_ops;
1165 struct trace_array *tr = file->tr;
1166 const char *system;
1167 const char *event;
1168 char *trigger;
1169 char *number;
1170 bool enable;
1171 int ret;
1173 if (!param)
1174 return -EINVAL;
1176 /* separate the trigger from the filter (s:e:n [if filter]) */
1177 trigger = strsep(&param, " \t");
1178 if (!trigger)
1179 return -EINVAL;
1181 system = strsep(&trigger, ":");
1182 if (!trigger)
1183 return -EINVAL;
1185 event = strsep(&trigger, ":");
1187 ret = -EINVAL;
1188 event_enable_file = find_event_file(tr, system, event);
1189 if (!event_enable_file)
1190 goto out;
1192 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1194 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1196 ret = -ENOMEM;
1197 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1198 if (!trigger_data)
1199 goto out;
1201 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1202 if (!enable_data) {
1203 kfree(trigger_data);
1204 goto out;
1207 trigger_data->count = -1;
1208 trigger_data->ops = trigger_ops;
1209 trigger_data->cmd_ops = cmd_ops;
1210 INIT_LIST_HEAD(&trigger_data->list);
1211 RCU_INIT_POINTER(trigger_data->filter, NULL);
1213 enable_data->enable = enable;
1214 enable_data->file = event_enable_file;
1215 trigger_data->private_data = enable_data;
1217 if (glob[0] == '!') {
1218 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1219 kfree(trigger_data);
1220 kfree(enable_data);
1221 ret = 0;
1222 goto out;
1225 if (trigger) {
1226 number = strsep(&trigger, ":");
1228 ret = -EINVAL;
1229 if (!strlen(number))
1230 goto out_free;
1233 * We use the callback data field (which is a pointer)
1234 * as our counter.
1236 ret = kstrtoul(number, 0, &trigger_data->count);
1237 if (ret)
1238 goto out_free;
1241 if (!param) /* if param is non-empty, it's supposed to be a filter */
1242 goto out_reg;
1244 if (!cmd_ops->set_filter)
1245 goto out_reg;
1247 ret = cmd_ops->set_filter(param, trigger_data, file);
1248 if (ret < 0)
1249 goto out_free;
1251 out_reg:
1252 /* Don't let event modules unload while probe registered */
1253 ret = try_module_get(event_enable_file->event_call->mod);
1254 if (!ret) {
1255 ret = -EBUSY;
1256 goto out_free;
1259 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1260 if (ret < 0)
1261 goto out_put;
1262 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1264 * The above returns on success the # of functions enabled,
1265 * but if it didn't find any functions it returns zero.
1266 * Consider no functions a failure too.
1268 if (!ret) {
1269 ret = -ENOENT;
1270 goto out_disable;
1271 } else if (ret < 0)
1272 goto out_disable;
1273 /* Just return zero, not the number of enabled functions */
1274 ret = 0;
1275 out:
1276 return ret;
1278 out_disable:
1279 trace_event_enable_disable(event_enable_file, 0, 1);
1280 out_put:
1281 module_put(event_enable_file->event_call->mod);
1282 out_free:
1283 if (cmd_ops->set_filter)
1284 cmd_ops->set_filter(NULL, trigger_data, NULL);
1285 kfree(trigger_data);
1286 kfree(enable_data);
1287 goto out;
1290 static int event_enable_register_trigger(char *glob,
1291 struct event_trigger_ops *ops,
1292 struct event_trigger_data *data,
1293 struct trace_event_file *file)
1295 struct enable_trigger_data *enable_data = data->private_data;
1296 struct enable_trigger_data *test_enable_data;
1297 struct event_trigger_data *test;
1298 int ret = 0;
1300 list_for_each_entry_rcu(test, &file->triggers, list) {
1301 test_enable_data = test->private_data;
1302 if (test_enable_data &&
1303 (test_enable_data->file == enable_data->file)) {
1304 ret = -EEXIST;
1305 goto out;
1309 if (data->ops->init) {
1310 ret = data->ops->init(data->ops, data);
1311 if (ret < 0)
1312 goto out;
1315 list_add_rcu(&data->list, &file->triggers);
1316 ret++;
1318 update_cond_flag(file);
1319 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1320 list_del_rcu(&data->list);
1321 update_cond_flag(file);
1322 ret--;
1324 out:
1325 return ret;
1328 static void event_enable_unregister_trigger(char *glob,
1329 struct event_trigger_ops *ops,
1330 struct event_trigger_data *test,
1331 struct trace_event_file *file)
1333 struct enable_trigger_data *test_enable_data = test->private_data;
1334 struct enable_trigger_data *enable_data;
1335 struct event_trigger_data *data;
1336 bool unregistered = false;
1338 list_for_each_entry_rcu(data, &file->triggers, list) {
1339 enable_data = data->private_data;
1340 if (enable_data &&
1341 (enable_data->file == test_enable_data->file)) {
1342 unregistered = true;
1343 list_del_rcu(&data->list);
1344 trace_event_trigger_enable_disable(file, 0);
1345 update_cond_flag(file);
1346 break;
1350 if (unregistered && data->ops->free)
1351 data->ops->free(data->ops, data);
1354 static struct event_trigger_ops *
1355 event_enable_get_trigger_ops(char *cmd, char *param)
1357 struct event_trigger_ops *ops;
1358 bool enable;
1360 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1362 if (enable)
1363 ops = param ? &event_enable_count_trigger_ops :
1364 &event_enable_trigger_ops;
1365 else
1366 ops = param ? &event_disable_count_trigger_ops :
1367 &event_disable_trigger_ops;
1369 return ops;
1372 static struct event_command trigger_enable_cmd = {
1373 .name = ENABLE_EVENT_STR,
1374 .trigger_type = ETT_EVENT_ENABLE,
1375 .func = event_enable_trigger_func,
1376 .reg = event_enable_register_trigger,
1377 .unreg = event_enable_unregister_trigger,
1378 .get_trigger_ops = event_enable_get_trigger_ops,
1379 .set_filter = set_trigger_filter,
1382 static struct event_command trigger_disable_cmd = {
1383 .name = DISABLE_EVENT_STR,
1384 .trigger_type = ETT_EVENT_ENABLE,
1385 .func = event_enable_trigger_func,
1386 .reg = event_enable_register_trigger,
1387 .unreg = event_enable_unregister_trigger,
1388 .get_trigger_ops = event_enable_get_trigger_ops,
1389 .set_filter = set_trigger_filter,
1392 static __init void unregister_trigger_enable_disable_cmds(void)
1394 unregister_event_command(&trigger_enable_cmd);
1395 unregister_event_command(&trigger_disable_cmd);
1398 static __init int register_trigger_enable_disable_cmds(void)
1400 int ret;
1402 ret = register_event_command(&trigger_enable_cmd);
1403 if (WARN_ON(ret < 0))
1404 return ret;
1405 ret = register_event_command(&trigger_disable_cmd);
1406 if (WARN_ON(ret < 0))
1407 unregister_trigger_enable_disable_cmds();
1409 return ret;
1412 static __init int register_trigger_traceon_traceoff_cmds(void)
1414 int ret;
1416 ret = register_event_command(&trigger_traceon_cmd);
1417 if (WARN_ON(ret < 0))
1418 return ret;
1419 ret = register_event_command(&trigger_traceoff_cmd);
1420 if (WARN_ON(ret < 0))
1421 unregister_trigger_traceon_traceoff_cmds();
1423 return ret;
1426 __init int register_trigger_cmds(void)
1428 register_trigger_traceon_traceoff_cmds();
1429 register_trigger_snapshot_cmd();
1430 register_trigger_stacktrace_cmd();
1431 register_trigger_enable_disable_cmds();
1433 return 0;