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>
25 #include <linux/rculist.h>
29 static LIST_HEAD(trigger_commands
);
30 static DEFINE_MUTEX(trigger_cmd_mutex
);
32 void 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 */
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
))
75 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
79 data
->ops
->func(data
, rec
);
82 filter
= rcu_dereference_sched(data
->filter
);
83 if (filter
&& !filter_match_preds(filter
, rec
))
85 if (event_command_post_trigger(data
->cmd_ops
)) {
86 tt
|= data
->cmd_ops
->trigger_type
;
89 data
->ops
->func(data
, rec
);
93 EXPORT_SYMBOL_GPL(event_triggers_call
);
96 * event_triggers_post_call - Call 'post_triggers' for a trace event
97 * @file: The trace_event_file associated with the event
98 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
99 * @rec: The trace entry for the event
101 * For each trigger associated with an event, invoke the trigger
102 * function registered with the associated trigger command, if the
103 * corresponding bit is set in the tt enum passed into this function.
104 * See @event_triggers_call for details on how those bits are set.
106 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
109 event_triggers_post_call(struct trace_event_file
*file
,
110 enum event_trigger_type tt
,
113 struct event_trigger_data
*data
;
115 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
118 if (data
->cmd_ops
->trigger_type
& tt
)
119 data
->ops
->func(data
, rec
);
122 EXPORT_SYMBOL_GPL(event_triggers_post_call
);
124 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
126 static void *trigger_next(struct seq_file
*m
, void *t
, loff_t
*pos
)
128 struct trace_event_file
*event_file
= event_file_data(m
->private);
130 if (t
== SHOW_AVAILABLE_TRIGGERS
)
133 return seq_list_next(t
, &event_file
->triggers
, pos
);
136 static void *trigger_start(struct seq_file
*m
, loff_t
*pos
)
138 struct trace_event_file
*event_file
;
140 /* ->stop() is called even if ->start() fails */
141 mutex_lock(&event_mutex
);
142 event_file
= event_file_data(m
->private);
143 if (unlikely(!event_file
))
144 return ERR_PTR(-ENODEV
);
146 if (list_empty(&event_file
->triggers
))
147 return *pos
== 0 ? SHOW_AVAILABLE_TRIGGERS
: NULL
;
149 return seq_list_start(&event_file
->triggers
, *pos
);
152 static void trigger_stop(struct seq_file
*m
, void *t
)
154 mutex_unlock(&event_mutex
);
157 static int trigger_show(struct seq_file
*m
, void *v
)
159 struct event_trigger_data
*data
;
160 struct event_command
*p
;
162 if (v
== SHOW_AVAILABLE_TRIGGERS
) {
163 seq_puts(m
, "# Available triggers:\n");
165 mutex_lock(&trigger_cmd_mutex
);
166 list_for_each_entry_reverse(p
, &trigger_commands
, list
)
167 seq_printf(m
, " %s", p
->name
);
169 mutex_unlock(&trigger_cmd_mutex
);
173 data
= list_entry(v
, struct event_trigger_data
, list
);
174 data
->ops
->print(m
, data
->ops
, data
);
179 static const struct seq_operations event_triggers_seq_ops
= {
180 .start
= trigger_start
,
181 .next
= trigger_next
,
182 .stop
= trigger_stop
,
183 .show
= trigger_show
,
186 static int event_trigger_regex_open(struct inode
*inode
, struct file
*file
)
190 mutex_lock(&event_mutex
);
192 if (unlikely(!event_file_data(file
))) {
193 mutex_unlock(&event_mutex
);
197 if ((file
->f_mode
& FMODE_WRITE
) &&
198 (file
->f_flags
& O_TRUNC
)) {
199 struct trace_event_file
*event_file
;
200 struct event_command
*p
;
202 event_file
= event_file_data(file
);
204 list_for_each_entry(p
, &trigger_commands
, list
) {
206 p
->unreg_all(event_file
);
210 if (file
->f_mode
& FMODE_READ
) {
211 ret
= seq_open(file
, &event_triggers_seq_ops
);
213 struct seq_file
*m
= file
->private_data
;
218 mutex_unlock(&event_mutex
);
223 static int trigger_process_regex(struct trace_event_file
*file
, char *buff
)
225 char *command
, *next
= buff
;
226 struct event_command
*p
;
229 command
= strsep(&next
, ": \t");
230 command
= (command
[0] != '!') ? command
: command
+ 1;
232 mutex_lock(&trigger_cmd_mutex
);
233 list_for_each_entry(p
, &trigger_commands
, list
) {
234 if (strcmp(p
->name
, command
) == 0) {
235 ret
= p
->func(p
, file
, buff
, command
, next
);
240 mutex_unlock(&trigger_cmd_mutex
);
245 static ssize_t
event_trigger_regex_write(struct file
*file
,
246 const char __user
*ubuf
,
247 size_t cnt
, loff_t
*ppos
)
249 struct trace_event_file
*event_file
;
256 if (cnt
>= PAGE_SIZE
)
259 buf
= memdup_user_nul(ubuf
, cnt
);
265 mutex_lock(&event_mutex
);
266 event_file
= event_file_data(file
);
267 if (unlikely(!event_file
)) {
268 mutex_unlock(&event_mutex
);
272 ret
= trigger_process_regex(event_file
, buf
);
273 mutex_unlock(&event_mutex
);
285 static int event_trigger_regex_release(struct inode
*inode
, struct file
*file
)
287 mutex_lock(&event_mutex
);
289 if (file
->f_mode
& FMODE_READ
)
290 seq_release(inode
, file
);
292 mutex_unlock(&event_mutex
);
298 event_trigger_write(struct file
*filp
, const char __user
*ubuf
,
299 size_t cnt
, loff_t
*ppos
)
301 return event_trigger_regex_write(filp
, ubuf
, cnt
, ppos
);
305 event_trigger_open(struct inode
*inode
, struct file
*filp
)
307 return event_trigger_regex_open(inode
, filp
);
311 event_trigger_release(struct inode
*inode
, struct file
*file
)
313 return event_trigger_regex_release(inode
, file
);
316 const struct file_operations event_trigger_fops
= {
317 .open
= event_trigger_open
,
319 .write
= event_trigger_write
,
320 .llseek
= tracing_lseek
,
321 .release
= event_trigger_release
,
325 * Currently we only register event commands from __init, so mark this
328 __init
int register_event_command(struct event_command
*cmd
)
330 struct event_command
*p
;
333 mutex_lock(&trigger_cmd_mutex
);
334 list_for_each_entry(p
, &trigger_commands
, list
) {
335 if (strcmp(cmd
->name
, p
->name
) == 0) {
340 list_add(&cmd
->list
, &trigger_commands
);
342 mutex_unlock(&trigger_cmd_mutex
);
348 * Currently we only unregister event commands from __init, so mark
351 __init
int unregister_event_command(struct event_command
*cmd
)
353 struct event_command
*p
, *n
;
356 mutex_lock(&trigger_cmd_mutex
);
357 list_for_each_entry_safe(p
, n
, &trigger_commands
, list
) {
358 if (strcmp(cmd
->name
, p
->name
) == 0) {
360 list_del_init(&p
->list
);
365 mutex_unlock(&trigger_cmd_mutex
);
371 * event_trigger_print - Generic event_trigger_ops @print implementation
372 * @name: The name of the event trigger
373 * @m: The seq_file being printed to
374 * @data: Trigger-specific data
375 * @filter_str: filter_str to print, if present
377 * Common implementation for event triggers to print themselves.
379 * Usually wrapped by a function that simply sets the @name of the
380 * trigger command and then invokes this.
382 * Return: 0 on success, errno otherwise
385 event_trigger_print(const char *name
, struct seq_file
*m
,
386 void *data
, char *filter_str
)
388 long count
= (long)data
;
393 seq_puts(m
, ":unlimited");
395 seq_printf(m
, ":count=%ld", count
);
398 seq_printf(m
, " if %s\n", filter_str
);
406 * event_trigger_init - Generic event_trigger_ops @init implementation
407 * @ops: The trigger ops associated with the trigger
408 * @data: Trigger-specific data
410 * Common implementation of event trigger initialization.
412 * Usually used directly as the @init method in event trigger
415 * Return: 0 on success, errno otherwise
417 int event_trigger_init(struct event_trigger_ops
*ops
,
418 struct event_trigger_data
*data
)
425 * event_trigger_free - Generic event_trigger_ops @free implementation
426 * @ops: The trigger ops associated with the trigger
427 * @data: Trigger-specific data
429 * Common implementation of event trigger de-initialization.
431 * Usually used directly as the @free method in event trigger
435 event_trigger_free(struct event_trigger_ops
*ops
,
436 struct event_trigger_data
*data
)
438 if (WARN_ON_ONCE(data
->ref
<= 0))
443 trigger_data_free(data
);
446 int trace_event_trigger_enable_disable(struct trace_event_file
*file
,
451 if (trigger_enable
) {
452 if (atomic_inc_return(&file
->tm_ref
) > 1)
454 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
455 ret
= trace_event_enable_disable(file
, 1, 1);
457 if (atomic_dec_return(&file
->tm_ref
) > 0)
459 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
460 ret
= trace_event_enable_disable(file
, 0, 1);
467 * clear_event_triggers - Clear all triggers associated with a trace array
468 * @tr: The trace array to clear
470 * For each trigger, the triggering event has its tm_ref decremented
471 * via trace_event_trigger_enable_disable(), and any associated event
472 * (in the case of enable/disable_event triggers) will have its sm_ref
473 * decremented via free()->trace_event_enable_disable(). That
474 * combination effectively reverses the soft-mode/trigger state added
475 * by trigger registration.
477 * Must be called with event_mutex held.
480 clear_event_triggers(struct trace_array
*tr
)
482 struct trace_event_file
*file
;
484 list_for_each_entry(file
, &tr
->events
, list
) {
485 struct event_trigger_data
*data
;
486 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
487 trace_event_trigger_enable_disable(file
, 0);
489 data
->ops
->free(data
->ops
, data
);
495 * update_cond_flag - Set or reset the TRIGGER_COND bit
496 * @file: The trace_event_file associated with the event
498 * If an event has triggers and any of those triggers has a filter or
499 * a post_trigger, trigger invocation needs to be deferred until after
500 * the current event has logged its data, and the event should have
501 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
504 void update_cond_flag(struct trace_event_file
*file
)
506 struct event_trigger_data
*data
;
507 bool set_cond
= false;
509 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
510 if (data
->filter
|| event_command_post_trigger(data
->cmd_ops
) ||
511 event_command_needs_rec(data
->cmd_ops
)) {
518 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
520 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
524 * register_trigger - Generic event_command @reg implementation
525 * @glob: The raw string used to register the trigger
526 * @ops: The trigger ops associated with the trigger
527 * @data: Trigger-specific data to associate with the trigger
528 * @file: The trace_event_file associated with the event
530 * Common implementation for event trigger registration.
532 * Usually used directly as the @reg method in event command
535 * Return: 0 on success, errno otherwise
537 static int register_trigger(char *glob
, struct event_trigger_ops
*ops
,
538 struct event_trigger_data
*data
,
539 struct trace_event_file
*file
)
541 struct event_trigger_data
*test
;
544 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
545 if (test
->cmd_ops
->trigger_type
== data
->cmd_ops
->trigger_type
) {
551 if (data
->ops
->init
) {
552 ret
= data
->ops
->init(data
->ops
, data
);
557 list_add_rcu(&data
->list
, &file
->triggers
);
560 update_cond_flag(file
);
561 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
562 list_del_rcu(&data
->list
);
563 update_cond_flag(file
);
571 * unregister_trigger - Generic event_command @unreg implementation
572 * @glob: The raw string used to register the trigger
573 * @ops: The trigger ops associated with the trigger
574 * @test: Trigger-specific data used to find the trigger to remove
575 * @file: The trace_event_file associated with the event
577 * Common implementation for event trigger unregistration.
579 * Usually used directly as the @unreg method in event command
582 void unregister_trigger(char *glob
, struct event_trigger_ops
*ops
,
583 struct event_trigger_data
*test
,
584 struct trace_event_file
*file
)
586 struct event_trigger_data
*data
;
587 bool unregistered
= false;
589 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
590 if (data
->cmd_ops
->trigger_type
== test
->cmd_ops
->trigger_type
) {
592 list_del_rcu(&data
->list
);
593 trace_event_trigger_enable_disable(file
, 0);
594 update_cond_flag(file
);
599 if (unregistered
&& data
->ops
->free
)
600 data
->ops
->free(data
->ops
, data
);
604 * event_trigger_callback - Generic event_command @func implementation
605 * @cmd_ops: The command ops, used for trigger registration
606 * @file: The trace_event_file associated with the event
607 * @glob: The raw string used to register the trigger
608 * @cmd: The cmd portion of the string used to register the trigger
609 * @param: The params portion of the string used to register the trigger
611 * Common implementation for event command parsing and trigger
614 * Usually used directly as the @func method in event command
617 * Return: 0 on success, errno otherwise
620 event_trigger_callback(struct event_command
*cmd_ops
,
621 struct trace_event_file
*file
,
622 char *glob
, char *cmd
, char *param
)
624 struct event_trigger_data
*trigger_data
;
625 struct event_trigger_ops
*trigger_ops
;
626 char *trigger
= NULL
;
630 /* separate the trigger from the filter (t:n [if filter]) */
631 if (param
&& isdigit(param
[0]))
632 trigger
= strsep(¶m
, " \t");
634 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
637 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
641 trigger_data
->count
= -1;
642 trigger_data
->ops
= trigger_ops
;
643 trigger_data
->cmd_ops
= cmd_ops
;
644 INIT_LIST_HEAD(&trigger_data
->list
);
645 INIT_LIST_HEAD(&trigger_data
->named_list
);
647 if (glob
[0] == '!') {
648 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
655 number
= strsep(&trigger
, ":");
662 * We use the callback data field (which is a pointer)
665 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
670 if (!param
) /* if param is non-empty, it's supposed to be a filter */
673 if (!cmd_ops
->set_filter
)
676 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
681 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
683 * The above returns on success the # of functions enabled,
684 * but if it didn't find any functions it returns zero.
685 * Consider no functions a failure too.
697 if (cmd_ops
->set_filter
)
698 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
704 * set_trigger_filter - Generic event_command @set_filter implementation
705 * @filter_str: The filter string for the trigger, NULL to remove filter
706 * @trigger_data: Trigger-specific data
707 * @file: The trace_event_file associated with the event
709 * Common implementation for event command filter parsing and filter
712 * Usually used directly as the @set_filter method in event command
715 * Also used to remove a filter (if filter_str = NULL).
717 * Return: 0 on success, errno otherwise
719 int set_trigger_filter(char *filter_str
,
720 struct event_trigger_data
*trigger_data
,
721 struct trace_event_file
*file
)
723 struct event_trigger_data
*data
= trigger_data
;
724 struct event_filter
*filter
= NULL
, *tmp
;
728 if (!filter_str
) /* clear the current filter */
731 s
= strsep(&filter_str
, " \t");
733 if (!strlen(s
) || strcmp(s
, "if") != 0)
739 /* The filter is for the 'trigger' event, not the triggered event */
740 ret
= create_event_filter(file
->event_call
, filter_str
, false, &filter
);
744 tmp
= rcu_access_pointer(data
->filter
);
746 rcu_assign_pointer(data
->filter
, filter
);
749 /* Make sure the call is done with the filter */
751 free_event_filter(tmp
);
754 kfree(data
->filter_str
);
755 data
->filter_str
= NULL
;
758 data
->filter_str
= kstrdup(filter_str
, GFP_KERNEL
);
759 if (!data
->filter_str
) {
760 free_event_filter(rcu_access_pointer(data
->filter
));
769 static LIST_HEAD(named_triggers
);
772 * find_named_trigger - Find the common named trigger associated with @name
773 * @name: The name of the set of named triggers to find the common data for
775 * Named triggers are sets of triggers that share a common set of
776 * trigger data. The first named trigger registered with a given name
777 * owns the common trigger data that the others subsequently
778 * registered with the same name will reference. This function
779 * returns the common trigger data associated with that first
780 * registered instance.
782 * Return: the common trigger data for the given named trigger on
783 * success, NULL otherwise.
785 struct event_trigger_data
*find_named_trigger(const char *name
)
787 struct event_trigger_data
*data
;
792 list_for_each_entry(data
, &named_triggers
, named_list
) {
793 if (data
->named_data
)
795 if (strcmp(data
->name
, name
) == 0)
803 * is_named_trigger - determine if a given trigger is a named trigger
804 * @test: The trigger data to test
806 * Return: true if 'test' is a named trigger, false otherwise.
808 bool is_named_trigger(struct event_trigger_data
*test
)
810 struct event_trigger_data
*data
;
812 list_for_each_entry(data
, &named_triggers
, named_list
) {
821 * save_named_trigger - save the trigger in the named trigger list
822 * @name: The name of the named trigger set
823 * @data: The trigger data to save
825 * Return: 0 if successful, negative error otherwise.
827 int save_named_trigger(const char *name
, struct event_trigger_data
*data
)
829 data
->name
= kstrdup(name
, GFP_KERNEL
);
833 list_add(&data
->named_list
, &named_triggers
);
839 * del_named_trigger - delete a trigger from the named trigger list
840 * @data: The trigger data to delete
842 void del_named_trigger(struct event_trigger_data
*data
)
847 list_del(&data
->named_list
);
850 static void __pause_named_trigger(struct event_trigger_data
*data
, bool pause
)
852 struct event_trigger_data
*test
;
854 list_for_each_entry(test
, &named_triggers
, named_list
) {
855 if (strcmp(test
->name
, data
->name
) == 0) {
857 test
->paused_tmp
= test
->paused
;
860 test
->paused
= test
->paused_tmp
;
867 * pause_named_trigger - Pause all named triggers with the same name
868 * @data: The trigger data of a named trigger to pause
870 * Pauses a named trigger along with all other triggers having the
871 * same name. Because named triggers share a common set of data,
872 * pausing only one is meaningless, so pausing one named trigger needs
873 * to pause all triggers with the same name.
875 void pause_named_trigger(struct event_trigger_data
*data
)
877 __pause_named_trigger(data
, true);
881 * unpause_named_trigger - Un-pause all named triggers with the same name
882 * @data: The trigger data of a named trigger to unpause
884 * Un-pauses a named trigger along with all other triggers having the
885 * same name. Because named triggers share a common set of data,
886 * unpausing only one is meaningless, so unpausing one named trigger
887 * needs to unpause all triggers with the same name.
889 void unpause_named_trigger(struct event_trigger_data
*data
)
891 __pause_named_trigger(data
, false);
895 * set_named_trigger_data - Associate common named trigger data
896 * @data: The trigger data of a named trigger to unpause
898 * Named triggers are sets of triggers that share a common set of
899 * trigger data. The first named trigger registered with a given name
900 * owns the common trigger data that the others subsequently
901 * registered with the same name will reference. This function
902 * associates the common trigger data from the first trigger with the
905 void set_named_trigger_data(struct event_trigger_data
*data
,
906 struct event_trigger_data
*named_data
)
908 data
->named_data
= named_data
;
912 traceon_trigger(struct event_trigger_data
*data
, void *rec
)
921 traceon_count_trigger(struct event_trigger_data
*data
, void *rec
)
929 if (data
->count
!= -1)
936 traceoff_trigger(struct event_trigger_data
*data
, void *rec
)
938 if (!tracing_is_on())
945 traceoff_count_trigger(struct event_trigger_data
*data
, void *rec
)
947 if (!tracing_is_on())
953 if (data
->count
!= -1)
960 traceon_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
961 struct event_trigger_data
*data
)
963 return event_trigger_print("traceon", m
, (void *)data
->count
,
968 traceoff_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
969 struct event_trigger_data
*data
)
971 return event_trigger_print("traceoff", m
, (void *)data
->count
,
975 static struct event_trigger_ops traceon_trigger_ops
= {
976 .func
= traceon_trigger
,
977 .print
= traceon_trigger_print
,
978 .init
= event_trigger_init
,
979 .free
= event_trigger_free
,
982 static struct event_trigger_ops traceon_count_trigger_ops
= {
983 .func
= traceon_count_trigger
,
984 .print
= traceon_trigger_print
,
985 .init
= event_trigger_init
,
986 .free
= event_trigger_free
,
989 static struct event_trigger_ops traceoff_trigger_ops
= {
990 .func
= traceoff_trigger
,
991 .print
= traceoff_trigger_print
,
992 .init
= event_trigger_init
,
993 .free
= event_trigger_free
,
996 static struct event_trigger_ops traceoff_count_trigger_ops
= {
997 .func
= traceoff_count_trigger
,
998 .print
= traceoff_trigger_print
,
999 .init
= event_trigger_init
,
1000 .free
= event_trigger_free
,
1003 static struct event_trigger_ops
*
1004 onoff_get_trigger_ops(char *cmd
, char *param
)
1006 struct event_trigger_ops
*ops
;
1008 /* we register both traceon and traceoff to this callback */
1009 if (strcmp(cmd
, "traceon") == 0)
1010 ops
= param
? &traceon_count_trigger_ops
:
1011 &traceon_trigger_ops
;
1013 ops
= param
? &traceoff_count_trigger_ops
:
1014 &traceoff_trigger_ops
;
1019 static struct event_command trigger_traceon_cmd
= {
1021 .trigger_type
= ETT_TRACE_ONOFF
,
1022 .func
= event_trigger_callback
,
1023 .reg
= register_trigger
,
1024 .unreg
= unregister_trigger
,
1025 .get_trigger_ops
= onoff_get_trigger_ops
,
1026 .set_filter
= set_trigger_filter
,
1029 static struct event_command trigger_traceoff_cmd
= {
1031 .trigger_type
= ETT_TRACE_ONOFF
,
1032 .flags
= EVENT_CMD_FL_POST_TRIGGER
,
1033 .func
= event_trigger_callback
,
1034 .reg
= register_trigger
,
1035 .unreg
= unregister_trigger
,
1036 .get_trigger_ops
= onoff_get_trigger_ops
,
1037 .set_filter
= set_trigger_filter
,
1040 #ifdef CONFIG_TRACER_SNAPSHOT
1042 snapshot_trigger(struct event_trigger_data
*data
, void *rec
)
1048 snapshot_count_trigger(struct event_trigger_data
*data
, void *rec
)
1053 if (data
->count
!= -1)
1056 snapshot_trigger(data
, rec
);
1060 register_snapshot_trigger(char *glob
, struct event_trigger_ops
*ops
,
1061 struct event_trigger_data
*data
,
1062 struct trace_event_file
*file
)
1064 int ret
= register_trigger(glob
, ops
, data
, file
);
1066 if (ret
> 0 && tracing_alloc_snapshot() != 0) {
1067 unregister_trigger(glob
, ops
, data
, file
);
1075 snapshot_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1076 struct event_trigger_data
*data
)
1078 return event_trigger_print("snapshot", m
, (void *)data
->count
,
1082 static struct event_trigger_ops snapshot_trigger_ops
= {
1083 .func
= snapshot_trigger
,
1084 .print
= snapshot_trigger_print
,
1085 .init
= event_trigger_init
,
1086 .free
= event_trigger_free
,
1089 static struct event_trigger_ops snapshot_count_trigger_ops
= {
1090 .func
= snapshot_count_trigger
,
1091 .print
= snapshot_trigger_print
,
1092 .init
= event_trigger_init
,
1093 .free
= event_trigger_free
,
1096 static struct event_trigger_ops
*
1097 snapshot_get_trigger_ops(char *cmd
, char *param
)
1099 return param
? &snapshot_count_trigger_ops
: &snapshot_trigger_ops
;
1102 static struct event_command trigger_snapshot_cmd
= {
1104 .trigger_type
= ETT_SNAPSHOT
,
1105 .func
= event_trigger_callback
,
1106 .reg
= register_snapshot_trigger
,
1107 .unreg
= unregister_trigger
,
1108 .get_trigger_ops
= snapshot_get_trigger_ops
,
1109 .set_filter
= set_trigger_filter
,
1112 static __init
int register_trigger_snapshot_cmd(void)
1116 ret
= register_event_command(&trigger_snapshot_cmd
);
1122 static __init
int register_trigger_snapshot_cmd(void) { return 0; }
1123 #endif /* CONFIG_TRACER_SNAPSHOT */
1125 #ifdef CONFIG_STACKTRACE
1126 #ifdef CONFIG_UNWINDER_ORC
1128 * event_triggers_post_call()
1129 * trace_event_raw_event_xxx()
1131 # define STACK_SKIP 2
1135 * stacktrace_trigger()
1136 * event_triggers_post_call()
1137 * trace_event_buffer_commit()
1138 * trace_event_raw_event_xxx()
1140 #define STACK_SKIP 4
1144 stacktrace_trigger(struct event_trigger_data
*data
, void *rec
)
1146 trace_dump_stack(STACK_SKIP
);
1150 stacktrace_count_trigger(struct event_trigger_data
*data
, void *rec
)
1155 if (data
->count
!= -1)
1158 stacktrace_trigger(data
, rec
);
1162 stacktrace_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1163 struct event_trigger_data
*data
)
1165 return event_trigger_print("stacktrace", m
, (void *)data
->count
,
1169 static struct event_trigger_ops stacktrace_trigger_ops
= {
1170 .func
= stacktrace_trigger
,
1171 .print
= stacktrace_trigger_print
,
1172 .init
= event_trigger_init
,
1173 .free
= event_trigger_free
,
1176 static struct event_trigger_ops stacktrace_count_trigger_ops
= {
1177 .func
= stacktrace_count_trigger
,
1178 .print
= stacktrace_trigger_print
,
1179 .init
= event_trigger_init
,
1180 .free
= event_trigger_free
,
1183 static struct event_trigger_ops
*
1184 stacktrace_get_trigger_ops(char *cmd
, char *param
)
1186 return param
? &stacktrace_count_trigger_ops
: &stacktrace_trigger_ops
;
1189 static struct event_command trigger_stacktrace_cmd
= {
1190 .name
= "stacktrace",
1191 .trigger_type
= ETT_STACKTRACE
,
1192 .flags
= EVENT_CMD_FL_POST_TRIGGER
,
1193 .func
= event_trigger_callback
,
1194 .reg
= register_trigger
,
1195 .unreg
= unregister_trigger
,
1196 .get_trigger_ops
= stacktrace_get_trigger_ops
,
1197 .set_filter
= set_trigger_filter
,
1200 static __init
int register_trigger_stacktrace_cmd(void)
1204 ret
= register_event_command(&trigger_stacktrace_cmd
);
1210 static __init
int register_trigger_stacktrace_cmd(void) { return 0; }
1211 #endif /* CONFIG_STACKTRACE */
1213 static __init
void unregister_trigger_traceon_traceoff_cmds(void)
1215 unregister_event_command(&trigger_traceon_cmd
);
1216 unregister_event_command(&trigger_traceoff_cmd
);
1220 event_enable_trigger(struct event_trigger_data
*data
, void *rec
)
1222 struct enable_trigger_data
*enable_data
= data
->private_data
;
1224 if (enable_data
->enable
)
1225 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1227 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1231 event_enable_count_trigger(struct event_trigger_data
*data
, void *rec
)
1233 struct enable_trigger_data
*enable_data
= data
->private_data
;
1238 /* Skip if the event is in a state we want to switch to */
1239 if (enable_data
->enable
== !(enable_data
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1242 if (data
->count
!= -1)
1245 event_enable_trigger(data
, rec
);
1248 int event_enable_trigger_print(struct seq_file
*m
,
1249 struct event_trigger_ops
*ops
,
1250 struct event_trigger_data
*data
)
1252 struct enable_trigger_data
*enable_data
= data
->private_data
;
1254 seq_printf(m
, "%s:%s:%s",
1256 (enable_data
->enable
? ENABLE_HIST_STR
: DISABLE_HIST_STR
) :
1257 (enable_data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
),
1258 enable_data
->file
->event_call
->class->system
,
1259 trace_event_name(enable_data
->file
->event_call
));
1261 if (data
->count
== -1)
1262 seq_puts(m
, ":unlimited");
1264 seq_printf(m
, ":count=%ld", data
->count
);
1266 if (data
->filter_str
)
1267 seq_printf(m
, " if %s\n", data
->filter_str
);
1274 void event_enable_trigger_free(struct event_trigger_ops
*ops
,
1275 struct event_trigger_data
*data
)
1277 struct enable_trigger_data
*enable_data
= data
->private_data
;
1279 if (WARN_ON_ONCE(data
->ref
<= 0))
1284 /* Remove the SOFT_MODE flag */
1285 trace_event_enable_disable(enable_data
->file
, 0, 1);
1286 module_put(enable_data
->file
->event_call
->mod
);
1287 trigger_data_free(data
);
1292 static struct event_trigger_ops event_enable_trigger_ops
= {
1293 .func
= event_enable_trigger
,
1294 .print
= event_enable_trigger_print
,
1295 .init
= event_trigger_init
,
1296 .free
= event_enable_trigger_free
,
1299 static struct event_trigger_ops event_enable_count_trigger_ops
= {
1300 .func
= event_enable_count_trigger
,
1301 .print
= event_enable_trigger_print
,
1302 .init
= event_trigger_init
,
1303 .free
= event_enable_trigger_free
,
1306 static struct event_trigger_ops event_disable_trigger_ops
= {
1307 .func
= event_enable_trigger
,
1308 .print
= event_enable_trigger_print
,
1309 .init
= event_trigger_init
,
1310 .free
= event_enable_trigger_free
,
1313 static struct event_trigger_ops event_disable_count_trigger_ops
= {
1314 .func
= event_enable_count_trigger
,
1315 .print
= event_enable_trigger_print
,
1316 .init
= event_trigger_init
,
1317 .free
= event_enable_trigger_free
,
1320 int event_enable_trigger_func(struct event_command
*cmd_ops
,
1321 struct trace_event_file
*file
,
1322 char *glob
, char *cmd
, char *param
)
1324 struct trace_event_file
*event_enable_file
;
1325 struct enable_trigger_data
*enable_data
;
1326 struct event_trigger_data
*trigger_data
;
1327 struct event_trigger_ops
*trigger_ops
;
1328 struct trace_array
*tr
= file
->tr
;
1340 /* separate the trigger from the filter (s:e:n [if filter]) */
1341 trigger
= strsep(¶m
, " \t");
1345 system
= strsep(&trigger
, ":");
1349 event
= strsep(&trigger
, ":");
1352 event_enable_file
= find_event_file(tr
, system
, event
);
1353 if (!event_enable_file
)
1356 #ifdef CONFIG_HIST_TRIGGERS
1357 hist
= ((strcmp(cmd
, ENABLE_HIST_STR
) == 0) ||
1358 (strcmp(cmd
, DISABLE_HIST_STR
) == 0));
1360 enable
= ((strcmp(cmd
, ENABLE_EVENT_STR
) == 0) ||
1361 (strcmp(cmd
, ENABLE_HIST_STR
) == 0));
1363 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1365 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
1368 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
1372 enable_data
= kzalloc(sizeof(*enable_data
), GFP_KERNEL
);
1374 kfree(trigger_data
);
1378 trigger_data
->count
= -1;
1379 trigger_data
->ops
= trigger_ops
;
1380 trigger_data
->cmd_ops
= cmd_ops
;
1381 INIT_LIST_HEAD(&trigger_data
->list
);
1382 RCU_INIT_POINTER(trigger_data
->filter
, NULL
);
1384 enable_data
->hist
= hist
;
1385 enable_data
->enable
= enable
;
1386 enable_data
->file
= event_enable_file
;
1387 trigger_data
->private_data
= enable_data
;
1389 if (glob
[0] == '!') {
1390 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
1391 kfree(trigger_data
);
1398 number
= strsep(&trigger
, ":");
1401 if (!strlen(number
))
1405 * We use the callback data field (which is a pointer)
1408 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
1413 if (!param
) /* if param is non-empty, it's supposed to be a filter */
1416 if (!cmd_ops
->set_filter
)
1419 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
1424 /* Don't let event modules unload while probe registered */
1425 ret
= try_module_get(event_enable_file
->event_call
->mod
);
1431 ret
= trace_event_enable_disable(event_enable_file
, 1, 1);
1434 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
1436 * The above returns on success the # of functions enabled,
1437 * but if it didn't find any functions it returns zero.
1438 * Consider no functions a failure too.
1445 /* Just return zero, not the number of enabled functions */
1451 trace_event_enable_disable(event_enable_file
, 0, 1);
1453 module_put(event_enable_file
->event_call
->mod
);
1455 if (cmd_ops
->set_filter
)
1456 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
1457 kfree(trigger_data
);
1462 int event_enable_register_trigger(char *glob
,
1463 struct event_trigger_ops
*ops
,
1464 struct event_trigger_data
*data
,
1465 struct trace_event_file
*file
)
1467 struct enable_trigger_data
*enable_data
= data
->private_data
;
1468 struct enable_trigger_data
*test_enable_data
;
1469 struct event_trigger_data
*test
;
1472 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
1473 test_enable_data
= test
->private_data
;
1474 if (test_enable_data
&&
1475 (test
->cmd_ops
->trigger_type
==
1476 data
->cmd_ops
->trigger_type
) &&
1477 (test_enable_data
->file
== enable_data
->file
)) {
1483 if (data
->ops
->init
) {
1484 ret
= data
->ops
->init(data
->ops
, data
);
1489 list_add_rcu(&data
->list
, &file
->triggers
);
1492 update_cond_flag(file
);
1493 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
1494 list_del_rcu(&data
->list
);
1495 update_cond_flag(file
);
1502 void event_enable_unregister_trigger(char *glob
,
1503 struct event_trigger_ops
*ops
,
1504 struct event_trigger_data
*test
,
1505 struct trace_event_file
*file
)
1507 struct enable_trigger_data
*test_enable_data
= test
->private_data
;
1508 struct enable_trigger_data
*enable_data
;
1509 struct event_trigger_data
*data
;
1510 bool unregistered
= false;
1512 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
1513 enable_data
= data
->private_data
;
1515 (data
->cmd_ops
->trigger_type
==
1516 test
->cmd_ops
->trigger_type
) &&
1517 (enable_data
->file
== test_enable_data
->file
)) {
1518 unregistered
= true;
1519 list_del_rcu(&data
->list
);
1520 trace_event_trigger_enable_disable(file
, 0);
1521 update_cond_flag(file
);
1526 if (unregistered
&& data
->ops
->free
)
1527 data
->ops
->free(data
->ops
, data
);
1530 static struct event_trigger_ops
*
1531 event_enable_get_trigger_ops(char *cmd
, char *param
)
1533 struct event_trigger_ops
*ops
;
1536 #ifdef CONFIG_HIST_TRIGGERS
1537 enable
= ((strcmp(cmd
, ENABLE_EVENT_STR
) == 0) ||
1538 (strcmp(cmd
, ENABLE_HIST_STR
) == 0));
1540 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1543 ops
= param
? &event_enable_count_trigger_ops
:
1544 &event_enable_trigger_ops
;
1546 ops
= param
? &event_disable_count_trigger_ops
:
1547 &event_disable_trigger_ops
;
1552 static struct event_command trigger_enable_cmd
= {
1553 .name
= ENABLE_EVENT_STR
,
1554 .trigger_type
= ETT_EVENT_ENABLE
,
1555 .func
= event_enable_trigger_func
,
1556 .reg
= event_enable_register_trigger
,
1557 .unreg
= event_enable_unregister_trigger
,
1558 .get_trigger_ops
= event_enable_get_trigger_ops
,
1559 .set_filter
= set_trigger_filter
,
1562 static struct event_command trigger_disable_cmd
= {
1563 .name
= DISABLE_EVENT_STR
,
1564 .trigger_type
= ETT_EVENT_ENABLE
,
1565 .func
= event_enable_trigger_func
,
1566 .reg
= event_enable_register_trigger
,
1567 .unreg
= event_enable_unregister_trigger
,
1568 .get_trigger_ops
= event_enable_get_trigger_ops
,
1569 .set_filter
= set_trigger_filter
,
1572 static __init
void unregister_trigger_enable_disable_cmds(void)
1574 unregister_event_command(&trigger_enable_cmd
);
1575 unregister_event_command(&trigger_disable_cmd
);
1578 static __init
int register_trigger_enable_disable_cmds(void)
1582 ret
= register_event_command(&trigger_enable_cmd
);
1583 if (WARN_ON(ret
< 0))
1585 ret
= register_event_command(&trigger_disable_cmd
);
1586 if (WARN_ON(ret
< 0))
1587 unregister_trigger_enable_disable_cmds();
1592 static __init
int register_trigger_traceon_traceoff_cmds(void)
1596 ret
= register_event_command(&trigger_traceon_cmd
);
1597 if (WARN_ON(ret
< 0))
1599 ret
= register_event_command(&trigger_traceoff_cmd
);
1600 if (WARN_ON(ret
< 0))
1601 unregister_trigger_traceon_traceoff_cmds();
1606 __init
int register_trigger_cmds(void)
1608 register_trigger_traceon_traceoff_cmds();
1609 register_trigger_snapshot_cmd();
1610 register_trigger_stacktrace_cmd();
1611 register_trigger_enable_disable_cmds();
1612 register_trigger_hist_enable_disable_cmds();
1613 register_trigger_hist_cmd();