dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / kernel / trace / trace_events_trigger.c
blobb8a894adab2ca348aef674858d349e0653a4b7f6
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 = (char *)__get_free_page(GFP_TEMPORARY);
241 if (!buf)
242 return -ENOMEM;
244 if (copy_from_user(buf, ubuf, cnt)) {
245 free_page((unsigned long)buf);
246 return -EFAULT;
248 buf[cnt] = '\0';
249 strim(buf);
251 mutex_lock(&event_mutex);
252 event_file = event_file_data(file);
253 if (unlikely(!event_file)) {
254 mutex_unlock(&event_mutex);
255 free_page((unsigned long)buf);
256 return -ENODEV;
258 ret = trigger_process_regex(event_file, buf);
259 mutex_unlock(&event_mutex);
261 free_page((unsigned long)buf);
262 if (ret < 0)
263 goto out;
265 *ppos += cnt;
266 ret = cnt;
267 out:
268 return ret;
271 static int event_trigger_regex_release(struct inode *inode, struct file *file)
273 mutex_lock(&event_mutex);
275 if (file->f_mode & FMODE_READ)
276 seq_release(inode, file);
278 mutex_unlock(&event_mutex);
280 return 0;
283 static ssize_t
284 event_trigger_write(struct file *filp, const char __user *ubuf,
285 size_t cnt, loff_t *ppos)
287 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
290 static int
291 event_trigger_open(struct inode *inode, struct file *filp)
293 return event_trigger_regex_open(inode, filp);
296 static int
297 event_trigger_release(struct inode *inode, struct file *file)
299 return event_trigger_regex_release(inode, file);
302 const struct file_operations event_trigger_fops = {
303 .open = event_trigger_open,
304 .read = seq_read,
305 .write = event_trigger_write,
306 .llseek = tracing_lseek,
307 .release = event_trigger_release,
311 * Currently we only register event commands from __init, so mark this
312 * __init too.
314 static __init int register_event_command(struct event_command *cmd)
316 struct event_command *p;
317 int ret = 0;
319 mutex_lock(&trigger_cmd_mutex);
320 list_for_each_entry(p, &trigger_commands, list) {
321 if (strcmp(cmd->name, p->name) == 0) {
322 ret = -EBUSY;
323 goto out_unlock;
326 list_add(&cmd->list, &trigger_commands);
327 out_unlock:
328 mutex_unlock(&trigger_cmd_mutex);
330 return ret;
334 * Currently we only unregister event commands from __init, so mark
335 * this __init too.
337 static __init int unregister_event_command(struct event_command *cmd)
339 struct event_command *p, *n;
340 int ret = -ENODEV;
342 mutex_lock(&trigger_cmd_mutex);
343 list_for_each_entry_safe(p, n, &trigger_commands, list) {
344 if (strcmp(cmd->name, p->name) == 0) {
345 ret = 0;
346 list_del_init(&p->list);
347 goto out_unlock;
350 out_unlock:
351 mutex_unlock(&trigger_cmd_mutex);
353 return ret;
357 * event_trigger_print - Generic event_trigger_ops @print implementation
358 * @name: The name of the event trigger
359 * @m: The seq_file being printed to
360 * @data: Trigger-specific data
361 * @filter_str: filter_str to print, if present
363 * Common implementation for event triggers to print themselves.
365 * Usually wrapped by a function that simply sets the @name of the
366 * trigger command and then invokes this.
368 * Return: 0 on success, errno otherwise
370 static int
371 event_trigger_print(const char *name, struct seq_file *m,
372 void *data, char *filter_str)
374 long count = (long)data;
376 seq_puts(m, name);
378 if (count == -1)
379 seq_puts(m, ":unlimited");
380 else
381 seq_printf(m, ":count=%ld", count);
383 if (filter_str)
384 seq_printf(m, " if %s\n", filter_str);
385 else
386 seq_putc(m, '\n');
388 return 0;
392 * event_trigger_init - Generic event_trigger_ops @init implementation
393 * @ops: The trigger ops associated with the trigger
394 * @data: Trigger-specific data
396 * Common implementation of event trigger initialization.
398 * Usually used directly as the @init method in event trigger
399 * implementations.
401 * Return: 0 on success, errno otherwise
403 static int
404 event_trigger_init(struct event_trigger_ops *ops,
405 struct event_trigger_data *data)
407 data->ref++;
408 return 0;
412 * event_trigger_free - Generic event_trigger_ops @free implementation
413 * @ops: The trigger ops associated with the trigger
414 * @data: Trigger-specific data
416 * Common implementation of event trigger de-initialization.
418 * Usually used directly as the @free method in event trigger
419 * implementations.
421 static void
422 event_trigger_free(struct event_trigger_ops *ops,
423 struct event_trigger_data *data)
425 if (WARN_ON_ONCE(data->ref <= 0))
426 return;
428 data->ref--;
429 if (!data->ref)
430 trigger_data_free(data);
433 static int trace_event_trigger_enable_disable(struct trace_event_file *file,
434 int trigger_enable)
436 int ret = 0;
438 if (trigger_enable) {
439 if (atomic_inc_return(&file->tm_ref) > 1)
440 return ret;
441 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
442 ret = trace_event_enable_disable(file, 1, 1);
443 } else {
444 if (atomic_dec_return(&file->tm_ref) > 0)
445 return ret;
446 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
447 ret = trace_event_enable_disable(file, 0, 1);
450 return ret;
454 * clear_event_triggers - Clear all triggers associated with a trace array
455 * @tr: The trace array to clear
457 * For each trigger, the triggering event has its tm_ref decremented
458 * via trace_event_trigger_enable_disable(), and any associated event
459 * (in the case of enable/disable_event triggers) will have its sm_ref
460 * decremented via free()->trace_event_enable_disable(). That
461 * combination effectively reverses the soft-mode/trigger state added
462 * by trigger registration.
464 * Must be called with event_mutex held.
466 void
467 clear_event_triggers(struct trace_array *tr)
469 struct trace_event_file *file;
471 list_for_each_entry(file, &tr->events, list) {
472 struct event_trigger_data *data, *n;
473 list_for_each_entry_safe(data, n, &file->triggers, list) {
474 trace_event_trigger_enable_disable(file, 0);
475 list_del_rcu(&data->list);
476 if (data->ops->free)
477 data->ops->free(data->ops, data);
483 * update_cond_flag - Set or reset the TRIGGER_COND bit
484 * @file: The trace_event_file associated with the event
486 * If an event has triggers and any of those triggers has a filter or
487 * a post_trigger, trigger invocation needs to be deferred until after
488 * the current event has logged its data, and the event should have
489 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
490 * cleared.
492 static void update_cond_flag(struct trace_event_file *file)
494 struct event_trigger_data *data;
495 bool set_cond = false;
497 list_for_each_entry_rcu(data, &file->triggers, list) {
498 if (data->filter || data->cmd_ops->post_trigger) {
499 set_cond = true;
500 break;
504 if (set_cond)
505 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
506 else
507 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
511 * register_trigger - Generic event_command @reg implementation
512 * @glob: The raw string used to register the trigger
513 * @ops: The trigger ops associated with the trigger
514 * @data: Trigger-specific data to associate with the trigger
515 * @file: The trace_event_file associated with the event
517 * Common implementation for event trigger registration.
519 * Usually used directly as the @reg method in event command
520 * implementations.
522 * Return: 0 on success, errno otherwise
524 static int register_trigger(char *glob, struct event_trigger_ops *ops,
525 struct event_trigger_data *data,
526 struct trace_event_file *file)
528 struct event_trigger_data *test;
529 int ret = 0;
531 list_for_each_entry_rcu(test, &file->triggers, list) {
532 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
533 ret = -EEXIST;
534 goto out;
538 if (data->ops->init) {
539 ret = data->ops->init(data->ops, data);
540 if (ret < 0)
541 goto out;
544 list_add_rcu(&data->list, &file->triggers);
545 ret++;
547 if (trace_event_trigger_enable_disable(file, 1) < 0) {
548 list_del_rcu(&data->list);
549 ret--;
551 update_cond_flag(file);
552 out:
553 return ret;
557 * unregister_trigger - Generic event_command @unreg implementation
558 * @glob: The raw string used to register the trigger
559 * @ops: The trigger ops associated with the trigger
560 * @test: Trigger-specific data used to find the trigger to remove
561 * @file: The trace_event_file associated with the event
563 * Common implementation for event trigger unregistration.
565 * Usually used directly as the @unreg method in event command
566 * implementations.
568 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
569 struct event_trigger_data *test,
570 struct trace_event_file *file)
572 struct event_trigger_data *data;
573 bool unregistered = false;
575 list_for_each_entry_rcu(data, &file->triggers, list) {
576 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
577 unregistered = true;
578 list_del_rcu(&data->list);
579 update_cond_flag(file);
580 trace_event_trigger_enable_disable(file, 0);
581 break;
585 if (unregistered && data->ops->free)
586 data->ops->free(data->ops, data);
590 * event_trigger_callback - Generic event_command @func implementation
591 * @cmd_ops: The command ops, used for trigger registration
592 * @file: The trace_event_file associated with the event
593 * @glob: The raw string used to register the trigger
594 * @cmd: The cmd portion of the string used to register the trigger
595 * @param: The params portion of the string used to register the trigger
597 * Common implementation for event command parsing and trigger
598 * instantiation.
600 * Usually used directly as the @func method in event command
601 * implementations.
603 * Return: 0 on success, errno otherwise
605 static int
606 event_trigger_callback(struct event_command *cmd_ops,
607 struct trace_event_file *file,
608 char *glob, char *cmd, char *param)
610 struct event_trigger_data *trigger_data;
611 struct event_trigger_ops *trigger_ops;
612 char *trigger = NULL;
613 char *number;
614 int ret;
616 /* separate the trigger from the filter (t:n [if filter]) */
617 if (param && isdigit(param[0]))
618 trigger = strsep(&param, " \t");
620 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
622 ret = -ENOMEM;
623 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
624 if (!trigger_data)
625 goto out;
627 trigger_data->count = -1;
628 trigger_data->ops = trigger_ops;
629 trigger_data->cmd_ops = cmd_ops;
630 INIT_LIST_HEAD(&trigger_data->list);
632 if (glob[0] == '!') {
633 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
634 kfree(trigger_data);
635 ret = 0;
636 goto out;
639 if (trigger) {
640 number = strsep(&trigger, ":");
642 ret = -EINVAL;
643 if (!strlen(number))
644 goto out_free;
647 * We use the callback data field (which is a pointer)
648 * as our counter.
650 ret = kstrtoul(number, 0, &trigger_data->count);
651 if (ret)
652 goto out_free;
655 if (!param) /* if param is non-empty, it's supposed to be a filter */
656 goto out_reg;
658 if (!cmd_ops->set_filter)
659 goto out_reg;
661 ret = cmd_ops->set_filter(param, trigger_data, file);
662 if (ret < 0)
663 goto out_free;
665 out_reg:
666 /* Up the trigger_data count to make sure reg doesn't free it on failure */
667 event_trigger_init(trigger_ops, trigger_data);
668 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
670 * The above returns on success the # of functions enabled,
671 * but if it didn't find any functions it returns zero.
672 * Consider no functions a failure too.
674 if (!ret) {
675 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
676 ret = -ENOENT;
677 } else if (ret > 0)
678 ret = 0;
680 /* Down the counter of trigger_data or free it if not used anymore */
681 event_trigger_free(trigger_ops, trigger_data);
682 out:
683 return ret;
685 out_free:
686 if (cmd_ops->set_filter)
687 cmd_ops->set_filter(NULL, trigger_data, NULL);
688 kfree(trigger_data);
689 goto out;
693 * set_trigger_filter - Generic event_command @set_filter implementation
694 * @filter_str: The filter string for the trigger, NULL to remove filter
695 * @trigger_data: Trigger-specific data
696 * @file: The trace_event_file associated with the event
698 * Common implementation for event command filter parsing and filter
699 * instantiation.
701 * Usually used directly as the @set_filter method in event command
702 * implementations.
704 * Also used to remove a filter (if filter_str = NULL).
706 * Return: 0 on success, errno otherwise
708 static int set_trigger_filter(char *filter_str,
709 struct event_trigger_data *trigger_data,
710 struct trace_event_file *file)
712 struct event_trigger_data *data = trigger_data;
713 struct event_filter *filter = NULL, *tmp;
714 int ret = -EINVAL;
715 char *s;
717 if (!filter_str) /* clear the current filter */
718 goto assign;
720 s = strsep(&filter_str, " \t");
722 if (!strlen(s) || strcmp(s, "if") != 0)
723 goto out;
725 if (!filter_str)
726 goto out;
728 /* The filter is for the 'trigger' event, not the triggered event */
729 ret = create_event_filter(file->event_call, filter_str, false, &filter);
730 if (ret)
731 goto out;
732 assign:
733 tmp = rcu_access_pointer(data->filter);
735 rcu_assign_pointer(data->filter, filter);
737 if (tmp) {
738 /* Make sure the call is done with the filter */
739 synchronize_sched();
740 free_event_filter(tmp);
743 kfree(data->filter_str);
744 data->filter_str = NULL;
746 if (filter_str) {
747 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
748 if (!data->filter_str) {
749 free_event_filter(rcu_access_pointer(data->filter));
750 data->filter = NULL;
751 ret = -ENOMEM;
754 out:
755 return ret;
758 static void
759 traceon_trigger(struct event_trigger_data *data)
761 if (tracing_is_on())
762 return;
764 tracing_on();
767 static void
768 traceon_count_trigger(struct event_trigger_data *data)
770 if (tracing_is_on())
771 return;
773 if (!data->count)
774 return;
776 if (data->count != -1)
777 (data->count)--;
779 tracing_on();
782 static void
783 traceoff_trigger(struct event_trigger_data *data)
785 if (!tracing_is_on())
786 return;
788 tracing_off();
791 static void
792 traceoff_count_trigger(struct event_trigger_data *data)
794 if (!tracing_is_on())
795 return;
797 if (!data->count)
798 return;
800 if (data->count != -1)
801 (data->count)--;
803 tracing_off();
806 static int
807 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
808 struct event_trigger_data *data)
810 return event_trigger_print("traceon", m, (void *)data->count,
811 data->filter_str);
814 static int
815 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
816 struct event_trigger_data *data)
818 return event_trigger_print("traceoff", m, (void *)data->count,
819 data->filter_str);
822 static struct event_trigger_ops traceon_trigger_ops = {
823 .func = traceon_trigger,
824 .print = traceon_trigger_print,
825 .init = event_trigger_init,
826 .free = event_trigger_free,
829 static struct event_trigger_ops traceon_count_trigger_ops = {
830 .func = traceon_count_trigger,
831 .print = traceon_trigger_print,
832 .init = event_trigger_init,
833 .free = event_trigger_free,
836 static struct event_trigger_ops traceoff_trigger_ops = {
837 .func = traceoff_trigger,
838 .print = traceoff_trigger_print,
839 .init = event_trigger_init,
840 .free = event_trigger_free,
843 static struct event_trigger_ops traceoff_count_trigger_ops = {
844 .func = traceoff_count_trigger,
845 .print = traceoff_trigger_print,
846 .init = event_trigger_init,
847 .free = event_trigger_free,
850 static struct event_trigger_ops *
851 onoff_get_trigger_ops(char *cmd, char *param)
853 struct event_trigger_ops *ops;
855 /* we register both traceon and traceoff to this callback */
856 if (strcmp(cmd, "traceon") == 0)
857 ops = param ? &traceon_count_trigger_ops :
858 &traceon_trigger_ops;
859 else
860 ops = param ? &traceoff_count_trigger_ops :
861 &traceoff_trigger_ops;
863 return ops;
866 static struct event_command trigger_traceon_cmd = {
867 .name = "traceon",
868 .trigger_type = ETT_TRACE_ONOFF,
869 .func = event_trigger_callback,
870 .reg = register_trigger,
871 .unreg = unregister_trigger,
872 .get_trigger_ops = onoff_get_trigger_ops,
873 .set_filter = set_trigger_filter,
876 static struct event_command trigger_traceoff_cmd = {
877 .name = "traceoff",
878 .trigger_type = ETT_TRACE_ONOFF,
879 .func = event_trigger_callback,
880 .reg = register_trigger,
881 .unreg = unregister_trigger,
882 .get_trigger_ops = onoff_get_trigger_ops,
883 .set_filter = set_trigger_filter,
886 #ifdef CONFIG_TRACER_SNAPSHOT
887 static void
888 snapshot_trigger(struct event_trigger_data *data)
890 tracing_snapshot();
893 static void
894 snapshot_count_trigger(struct event_trigger_data *data)
896 if (!data->count)
897 return;
899 if (data->count != -1)
900 (data->count)--;
902 snapshot_trigger(data);
905 static int
906 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
907 struct event_trigger_data *data,
908 struct trace_event_file *file)
910 int ret = register_trigger(glob, ops, data, file);
912 if (ret > 0 && tracing_alloc_snapshot() != 0) {
913 unregister_trigger(glob, ops, data, file);
914 ret = 0;
917 return ret;
920 static int
921 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
922 struct event_trigger_data *data)
924 return event_trigger_print("snapshot", m, (void *)data->count,
925 data->filter_str);
928 static struct event_trigger_ops snapshot_trigger_ops = {
929 .func = snapshot_trigger,
930 .print = snapshot_trigger_print,
931 .init = event_trigger_init,
932 .free = event_trigger_free,
935 static struct event_trigger_ops snapshot_count_trigger_ops = {
936 .func = snapshot_count_trigger,
937 .print = snapshot_trigger_print,
938 .init = event_trigger_init,
939 .free = event_trigger_free,
942 static struct event_trigger_ops *
943 snapshot_get_trigger_ops(char *cmd, char *param)
945 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
948 static struct event_command trigger_snapshot_cmd = {
949 .name = "snapshot",
950 .trigger_type = ETT_SNAPSHOT,
951 .func = event_trigger_callback,
952 .reg = register_snapshot_trigger,
953 .unreg = unregister_trigger,
954 .get_trigger_ops = snapshot_get_trigger_ops,
955 .set_filter = set_trigger_filter,
958 static __init int register_trigger_snapshot_cmd(void)
960 int ret;
962 ret = register_event_command(&trigger_snapshot_cmd);
963 WARN_ON(ret < 0);
965 return ret;
967 #else
968 static __init int register_trigger_snapshot_cmd(void) { return 0; }
969 #endif /* CONFIG_TRACER_SNAPSHOT */
971 #ifdef CONFIG_STACKTRACE
973 * Skip 3:
974 * stacktrace_trigger()
975 * event_triggers_post_call()
976 * trace_event_raw_event_xxx()
978 #define STACK_SKIP 3
980 static void
981 stacktrace_trigger(struct event_trigger_data *data)
983 trace_dump_stack(STACK_SKIP);
986 static void
987 stacktrace_count_trigger(struct event_trigger_data *data)
989 if (!data->count)
990 return;
992 if (data->count != -1)
993 (data->count)--;
995 stacktrace_trigger(data);
998 static int
999 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1000 struct event_trigger_data *data)
1002 return event_trigger_print("stacktrace", m, (void *)data->count,
1003 data->filter_str);
1006 static struct event_trigger_ops stacktrace_trigger_ops = {
1007 .func = stacktrace_trigger,
1008 .print = stacktrace_trigger_print,
1009 .init = event_trigger_init,
1010 .free = event_trigger_free,
1013 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1014 .func = stacktrace_count_trigger,
1015 .print = stacktrace_trigger_print,
1016 .init = event_trigger_init,
1017 .free = event_trigger_free,
1020 static struct event_trigger_ops *
1021 stacktrace_get_trigger_ops(char *cmd, char *param)
1023 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1026 static struct event_command trigger_stacktrace_cmd = {
1027 .name = "stacktrace",
1028 .trigger_type = ETT_STACKTRACE,
1029 .post_trigger = true,
1030 .func = event_trigger_callback,
1031 .reg = register_trigger,
1032 .unreg = unregister_trigger,
1033 .get_trigger_ops = stacktrace_get_trigger_ops,
1034 .set_filter = set_trigger_filter,
1037 static __init int register_trigger_stacktrace_cmd(void)
1039 int ret;
1041 ret = register_event_command(&trigger_stacktrace_cmd);
1042 WARN_ON(ret < 0);
1044 return ret;
1046 #else
1047 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1048 #endif /* CONFIG_STACKTRACE */
1050 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1052 unregister_event_command(&trigger_traceon_cmd);
1053 unregister_event_command(&trigger_traceoff_cmd);
1056 /* Avoid typos */
1057 #define ENABLE_EVENT_STR "enable_event"
1058 #define DISABLE_EVENT_STR "disable_event"
1060 struct enable_trigger_data {
1061 struct trace_event_file *file;
1062 bool enable;
1065 static void
1066 event_enable_trigger(struct event_trigger_data *data)
1068 struct enable_trigger_data *enable_data = data->private_data;
1070 if (enable_data->enable)
1071 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1072 else
1073 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1076 static void
1077 event_enable_count_trigger(struct event_trigger_data *data)
1079 struct enable_trigger_data *enable_data = data->private_data;
1081 if (!data->count)
1082 return;
1084 /* Skip if the event is in a state we want to switch to */
1085 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1086 return;
1088 if (data->count != -1)
1089 (data->count)--;
1091 event_enable_trigger(data);
1094 static int
1095 event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1096 struct event_trigger_data *data)
1098 struct enable_trigger_data *enable_data = data->private_data;
1100 seq_printf(m, "%s:%s:%s",
1101 enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1102 enable_data->file->event_call->class->system,
1103 trace_event_name(enable_data->file->event_call));
1105 if (data->count == -1)
1106 seq_puts(m, ":unlimited");
1107 else
1108 seq_printf(m, ":count=%ld", data->count);
1110 if (data->filter_str)
1111 seq_printf(m, " if %s\n", data->filter_str);
1112 else
1113 seq_putc(m, '\n');
1115 return 0;
1118 static void
1119 event_enable_trigger_free(struct event_trigger_ops *ops,
1120 struct event_trigger_data *data)
1122 struct enable_trigger_data *enable_data = data->private_data;
1124 if (WARN_ON_ONCE(data->ref <= 0))
1125 return;
1127 data->ref--;
1128 if (!data->ref) {
1129 /* Remove the SOFT_MODE flag */
1130 trace_event_enable_disable(enable_data->file, 0, 1);
1131 module_put(enable_data->file->event_call->mod);
1132 trigger_data_free(data);
1133 kfree(enable_data);
1137 static struct event_trigger_ops event_enable_trigger_ops = {
1138 .func = event_enable_trigger,
1139 .print = event_enable_trigger_print,
1140 .init = event_trigger_init,
1141 .free = event_enable_trigger_free,
1144 static struct event_trigger_ops event_enable_count_trigger_ops = {
1145 .func = event_enable_count_trigger,
1146 .print = event_enable_trigger_print,
1147 .init = event_trigger_init,
1148 .free = event_enable_trigger_free,
1151 static struct event_trigger_ops event_disable_trigger_ops = {
1152 .func = event_enable_trigger,
1153 .print = event_enable_trigger_print,
1154 .init = event_trigger_init,
1155 .free = event_enable_trigger_free,
1158 static struct event_trigger_ops event_disable_count_trigger_ops = {
1159 .func = event_enable_count_trigger,
1160 .print = event_enable_trigger_print,
1161 .init = event_trigger_init,
1162 .free = event_enable_trigger_free,
1165 static int
1166 event_enable_trigger_func(struct event_command *cmd_ops,
1167 struct trace_event_file *file,
1168 char *glob, char *cmd, char *param)
1170 struct trace_event_file *event_enable_file;
1171 struct enable_trigger_data *enable_data;
1172 struct event_trigger_data *trigger_data;
1173 struct event_trigger_ops *trigger_ops;
1174 struct trace_array *tr = file->tr;
1175 const char *system;
1176 const char *event;
1177 char *trigger;
1178 char *number;
1179 bool enable;
1180 int ret;
1182 if (!param)
1183 return -EINVAL;
1185 /* separate the trigger from the filter (s:e:n [if filter]) */
1186 trigger = strsep(&param, " \t");
1187 if (!trigger)
1188 return -EINVAL;
1190 system = strsep(&trigger, ":");
1191 if (!trigger)
1192 return -EINVAL;
1194 event = strsep(&trigger, ":");
1196 ret = -EINVAL;
1197 event_enable_file = find_event_file(tr, system, event);
1198 if (!event_enable_file)
1199 goto out;
1201 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1203 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1205 ret = -ENOMEM;
1206 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1207 if (!trigger_data)
1208 goto out;
1210 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1211 if (!enable_data) {
1212 kfree(trigger_data);
1213 goto out;
1216 trigger_data->count = -1;
1217 trigger_data->ops = trigger_ops;
1218 trigger_data->cmd_ops = cmd_ops;
1219 INIT_LIST_HEAD(&trigger_data->list);
1220 RCU_INIT_POINTER(trigger_data->filter, NULL);
1222 enable_data->enable = enable;
1223 enable_data->file = event_enable_file;
1224 trigger_data->private_data = enable_data;
1226 if (glob[0] == '!') {
1227 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1228 kfree(trigger_data);
1229 kfree(enable_data);
1230 ret = 0;
1231 goto out;
1234 /* Up the trigger_data count to make sure nothing frees it on failure */
1235 event_trigger_init(trigger_ops, trigger_data);
1237 if (trigger) {
1238 number = strsep(&trigger, ":");
1240 ret = -EINVAL;
1241 if (!strlen(number))
1242 goto out_free;
1245 * We use the callback data field (which is a pointer)
1246 * as our counter.
1248 ret = kstrtoul(number, 0, &trigger_data->count);
1249 if (ret)
1250 goto out_free;
1253 if (!param) /* if param is non-empty, it's supposed to be a filter */
1254 goto out_reg;
1256 if (!cmd_ops->set_filter)
1257 goto out_reg;
1259 ret = cmd_ops->set_filter(param, trigger_data, file);
1260 if (ret < 0)
1261 goto out_free;
1263 out_reg:
1264 /* Don't let event modules unload while probe registered */
1265 ret = try_module_get(event_enable_file->event_call->mod);
1266 if (!ret) {
1267 ret = -EBUSY;
1268 goto out_free;
1271 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1272 if (ret < 0)
1273 goto out_put;
1274 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1276 * The above returns on success the # of functions enabled,
1277 * but if it didn't find any functions it returns zero.
1278 * Consider no functions a failure too.
1280 if (!ret) {
1281 ret = -ENOENT;
1282 goto out_disable;
1283 } else if (ret < 0)
1284 goto out_disable;
1285 /* Just return zero, not the number of enabled functions */
1286 ret = 0;
1287 event_trigger_free(trigger_ops, trigger_data);
1288 out:
1289 return ret;
1291 out_disable:
1292 trace_event_enable_disable(event_enable_file, 0, 1);
1293 out_put:
1294 module_put(event_enable_file->event_call->mod);
1295 out_free:
1296 if (cmd_ops->set_filter)
1297 cmd_ops->set_filter(NULL, trigger_data, NULL);
1298 event_trigger_free(trigger_ops, trigger_data);
1299 kfree(enable_data);
1300 goto out;
1303 static int event_enable_register_trigger(char *glob,
1304 struct event_trigger_ops *ops,
1305 struct event_trigger_data *data,
1306 struct trace_event_file *file)
1308 struct enable_trigger_data *enable_data = data->private_data;
1309 struct enable_trigger_data *test_enable_data;
1310 struct event_trigger_data *test;
1311 int ret = 0;
1313 list_for_each_entry_rcu(test, &file->triggers, list) {
1314 test_enable_data = test->private_data;
1315 if (test_enable_data &&
1316 (test_enable_data->file == enable_data->file)) {
1317 ret = -EEXIST;
1318 goto out;
1322 if (data->ops->init) {
1323 ret = data->ops->init(data->ops, data);
1324 if (ret < 0)
1325 goto out;
1328 list_add_rcu(&data->list, &file->triggers);
1329 ret++;
1331 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1332 list_del_rcu(&data->list);
1333 ret--;
1335 update_cond_flag(file);
1336 out:
1337 return ret;
1340 static void event_enable_unregister_trigger(char *glob,
1341 struct event_trigger_ops *ops,
1342 struct event_trigger_data *test,
1343 struct trace_event_file *file)
1345 struct enable_trigger_data *test_enable_data = test->private_data;
1346 struct enable_trigger_data *enable_data;
1347 struct event_trigger_data *data;
1348 bool unregistered = false;
1350 list_for_each_entry_rcu(data, &file->triggers, list) {
1351 enable_data = data->private_data;
1352 if (enable_data &&
1353 (enable_data->file == test_enable_data->file)) {
1354 unregistered = true;
1355 list_del_rcu(&data->list);
1356 update_cond_flag(file);
1357 trace_event_trigger_enable_disable(file, 0);
1358 break;
1362 if (unregistered && data->ops->free)
1363 data->ops->free(data->ops, data);
1366 static struct event_trigger_ops *
1367 event_enable_get_trigger_ops(char *cmd, char *param)
1369 struct event_trigger_ops *ops;
1370 bool enable;
1372 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1374 if (enable)
1375 ops = param ? &event_enable_count_trigger_ops :
1376 &event_enable_trigger_ops;
1377 else
1378 ops = param ? &event_disable_count_trigger_ops :
1379 &event_disable_trigger_ops;
1381 return ops;
1384 static struct event_command trigger_enable_cmd = {
1385 .name = ENABLE_EVENT_STR,
1386 .trigger_type = ETT_EVENT_ENABLE,
1387 .func = event_enable_trigger_func,
1388 .reg = event_enable_register_trigger,
1389 .unreg = event_enable_unregister_trigger,
1390 .get_trigger_ops = event_enable_get_trigger_ops,
1391 .set_filter = set_trigger_filter,
1394 static struct event_command trigger_disable_cmd = {
1395 .name = DISABLE_EVENT_STR,
1396 .trigger_type = ETT_EVENT_ENABLE,
1397 .func = event_enable_trigger_func,
1398 .reg = event_enable_register_trigger,
1399 .unreg = event_enable_unregister_trigger,
1400 .get_trigger_ops = event_enable_get_trigger_ops,
1401 .set_filter = set_trigger_filter,
1404 static __init void unregister_trigger_enable_disable_cmds(void)
1406 unregister_event_command(&trigger_enable_cmd);
1407 unregister_event_command(&trigger_disable_cmd);
1410 static __init int register_trigger_enable_disable_cmds(void)
1412 int ret;
1414 ret = register_event_command(&trigger_enable_cmd);
1415 if (WARN_ON(ret < 0))
1416 return ret;
1417 ret = register_event_command(&trigger_disable_cmd);
1418 if (WARN_ON(ret < 0))
1419 unregister_trigger_enable_disable_cmds();
1421 return ret;
1424 static __init int register_trigger_traceon_traceoff_cmds(void)
1426 int ret;
1428 ret = register_event_command(&trigger_traceon_cmd);
1429 if (WARN_ON(ret < 0))
1430 return ret;
1431 ret = register_event_command(&trigger_traceoff_cmd);
1432 if (WARN_ON(ret < 0))
1433 unregister_trigger_traceon_traceoff_cmds();
1435 return ret;
1438 __init int register_trigger_cmds(void)
1440 register_trigger_traceon_traceoff_cmds();
1441 register_trigger_snapshot_cmd();
1442 register_trigger_stacktrace_cmd();
1443 register_trigger_enable_disable_cmds();
1445 return 0;