2 * trace_events_trigger - trace event triggers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
28 static LIST_HEAD(trigger_commands
);
29 static DEFINE_MUTEX(trigger_cmd_mutex
);
32 trigger_data_free(struct event_trigger_data
*data
)
34 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
) {
77 data
->ops
->func(data
);
80 filter
= rcu_dereference_sched(data
->filter
);
81 if (filter
&& !filter_match_preds(filter
, rec
))
83 if (data
->cmd_ops
->post_trigger
) {
84 tt
|= data
->cmd_ops
->trigger_type
;
87 data
->ops
->func(data
);
91 EXPORT_SYMBOL_GPL(event_triggers_call
);
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).
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
)
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");
159 mutex_lock(&trigger_cmd_mutex
);
160 list_for_each_entry_reverse(p
, &trigger_commands
, list
)
161 seq_printf(m
, " %s", p
->name
);
163 mutex_unlock(&trigger_cmd_mutex
);
167 data
= list_entry(v
, struct event_trigger_data
, list
);
168 data
->ops
->print(m
, data
->ops
, data
);
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
)
184 mutex_lock(&event_mutex
);
186 if (unlikely(!event_file_data(file
))) {
187 mutex_unlock(&event_mutex
);
191 if (file
->f_mode
& FMODE_READ
) {
192 ret
= seq_open(file
, &event_triggers_seq_ops
);
194 struct seq_file
*m
= file
->private_data
;
199 mutex_unlock(&event_mutex
);
204 static int trigger_process_regex(struct trace_event_file
*file
, char *buff
)
206 char *command
, *next
= buff
;
207 struct event_command
*p
;
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
);
221 mutex_unlock(&trigger_cmd_mutex
);
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
;
237 if (cnt
>= PAGE_SIZE
)
240 buf
= memdup_user_nul(ubuf
, cnt
);
246 mutex_lock(&event_mutex
);
247 event_file
= event_file_data(file
);
248 if (unlikely(!event_file
)) {
249 mutex_unlock(&event_mutex
);
253 ret
= trigger_process_regex(event_file
, buf
);
254 mutex_unlock(&event_mutex
);
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
);
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
);
286 event_trigger_open(struct inode
*inode
, struct file
*filp
)
288 return event_trigger_regex_open(inode
, filp
);
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
,
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
309 static __init
int register_event_command(struct event_command
*cmd
)
311 struct event_command
*p
;
314 mutex_lock(&trigger_cmd_mutex
);
315 list_for_each_entry(p
, &trigger_commands
, list
) {
316 if (strcmp(cmd
->name
, p
->name
) == 0) {
321 list_add(&cmd
->list
, &trigger_commands
);
323 mutex_unlock(&trigger_cmd_mutex
);
329 * Currently we only unregister event commands from __init, so mark
332 static __init
int unregister_event_command(struct event_command
*cmd
)
334 struct event_command
*p
, *n
;
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) {
341 list_del_init(&p
->list
);
346 mutex_unlock(&trigger_cmd_mutex
);
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
366 event_trigger_print(const char *name
, struct seq_file
*m
,
367 void *data
, char *filter_str
)
369 long count
= (long)data
;
374 seq_puts(m
, ":unlimited");
376 seq_printf(m
, ":count=%ld", count
);
379 seq_printf(m
, " if %s\n", filter_str
);
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
396 * Return: 0 on success, errno otherwise
399 event_trigger_init(struct event_trigger_ops
*ops
,
400 struct event_trigger_data
*data
)
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
417 event_trigger_free(struct event_trigger_ops
*ops
,
418 struct event_trigger_data
*data
)
420 if (WARN_ON_ONCE(data
->ref
<= 0))
425 trigger_data_free(data
);
428 static int trace_event_trigger_enable_disable(struct trace_event_file
*file
,
433 if (trigger_enable
) {
434 if (atomic_inc_return(&file
->tm_ref
) > 1)
436 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
437 ret
= trace_event_enable_disable(file
, 1, 1);
439 if (atomic_dec_return(&file
->tm_ref
) > 0)
441 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
442 ret
= trace_event_enable_disable(file
, 0, 1);
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.
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);
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
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
) {
499 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
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
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
;
525 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
526 if (test
->cmd_ops
->trigger_type
== data
->cmd_ops
->trigger_type
) {
532 if (data
->ops
->init
) {
533 ret
= data
->ops
->init(data
->ops
, data
);
538 list_add_rcu(&data
->list
, &file
->triggers
);
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
);
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
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
) {
573 list_del_rcu(&data
->list
);
574 trace_event_trigger_enable_disable(file
, 0);
575 update_cond_flag(file
);
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
595 * Usually used directly as the @func method in event command
598 * Return: 0 on success, errno otherwise
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
;
611 /* separate the trigger from the filter (t:n [if filter]) */
612 if (param
&& isdigit(param
[0]))
613 trigger
= strsep(¶m
, " \t");
615 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
618 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
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
);
635 number
= strsep(&trigger
, ":");
642 * We use the callback data field (which is a pointer)
645 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
650 if (!param
) /* if param is non-empty, it's supposed to be a filter */
653 if (!cmd_ops
->set_filter
)
656 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
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.
677 if (cmd_ops
->set_filter
)
678 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
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
692 * Usually used directly as the @set_filter method in event command
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
;
708 if (!filter_str
) /* clear the current filter */
711 s
= strsep(&filter_str
, " \t");
713 if (!strlen(s
) || strcmp(s
, "if") != 0)
719 /* The filter is for the 'trigger' event, not the triggered event */
720 ret
= create_event_filter(file
->event_call
, filter_str
, false, &filter
);
724 tmp
= rcu_access_pointer(data
->filter
);
726 rcu_assign_pointer(data
->filter
, filter
);
729 /* Make sure the call is done with the filter */
731 free_event_filter(tmp
);
734 kfree(data
->filter_str
);
735 data
->filter_str
= NULL
;
738 data
->filter_str
= kstrdup(filter_str
, GFP_KERNEL
);
739 if (!data
->filter_str
) {
740 free_event_filter(rcu_access_pointer(data
->filter
));
750 traceon_trigger(struct event_trigger_data
*data
)
759 traceon_count_trigger(struct event_trigger_data
*data
)
767 if (data
->count
!= -1)
774 traceoff_trigger(struct event_trigger_data
*data
)
776 if (!tracing_is_on())
783 traceoff_count_trigger(struct event_trigger_data
*data
)
785 if (!tracing_is_on())
791 if (data
->count
!= -1)
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
,
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
,
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
;
851 ops
= param
? &traceoff_count_trigger_ops
:
852 &traceoff_trigger_ops
;
857 static struct event_command trigger_traceon_cmd
= {
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
= {
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
879 snapshot_trigger(struct event_trigger_data
*data
)
885 snapshot_count_trigger(struct event_trigger_data
*data
)
890 if (data
->count
!= -1)
893 snapshot_trigger(data
);
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
);
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
,
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
= {
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)
953 ret
= register_event_command(&trigger_snapshot_cmd
);
959 static __init
int register_trigger_snapshot_cmd(void) { return 0; }
960 #endif /* CONFIG_TRACER_SNAPSHOT */
962 #ifdef CONFIG_STACKTRACE
965 * stacktrace_trigger()
966 * event_triggers_post_call()
967 * trace_event_raw_event_xxx()
972 stacktrace_trigger(struct event_trigger_data
*data
)
974 trace_dump_stack(STACK_SKIP
);
978 stacktrace_count_trigger(struct event_trigger_data
*data
)
983 if (data
->count
!= -1)
986 stacktrace_trigger(data
);
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
,
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)
1032 ret
= register_event_command(&trigger_stacktrace_cmd
);
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
);
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
;
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
);
1064 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1068 event_enable_count_trigger(struct event_trigger_data
*data
)
1070 struct enable_trigger_data
*enable_data
= data
->private_data
;
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
))
1079 if (data
->count
!= -1)
1082 event_enable_trigger(data
);
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");
1099 seq_printf(m
, ":count=%ld", data
->count
);
1101 if (data
->filter_str
)
1102 seq_printf(m
, " if %s\n", data
->filter_str
);
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))
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
);
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
,
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
;
1176 /* separate the trigger from the filter (s:e:n [if filter]) */
1177 trigger
= strsep(¶m
, " \t");
1181 system
= strsep(&trigger
, ":");
1185 event
= strsep(&trigger
, ":");
1188 event_enable_file
= find_event_file(tr
, system
, event
);
1189 if (!event_enable_file
)
1192 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1194 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
1197 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
1201 enable_data
= kzalloc(sizeof(*enable_data
), GFP_KERNEL
);
1203 kfree(trigger_data
);
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
);
1226 number
= strsep(&trigger
, ":");
1229 if (!strlen(number
))
1233 * We use the callback data field (which is a pointer)
1236 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
1241 if (!param
) /* if param is non-empty, it's supposed to be a filter */
1244 if (!cmd_ops
->set_filter
)
1247 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
1252 /* Don't let event modules unload while probe registered */
1253 ret
= try_module_get(event_enable_file
->event_call
->mod
);
1259 ret
= trace_event_enable_disable(event_enable_file
, 1, 1);
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.
1273 /* Just return zero, not the number of enabled functions */
1279 trace_event_enable_disable(event_enable_file
, 0, 1);
1281 module_put(event_enable_file
->event_call
->mod
);
1283 if (cmd_ops
->set_filter
)
1284 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
1285 kfree(trigger_data
);
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
;
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
)) {
1309 if (data
->ops
->init
) {
1310 ret
= data
->ops
->init(data
->ops
, data
);
1315 list_add_rcu(&data
->list
, &file
->triggers
);
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
);
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
;
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
);
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
;
1360 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1363 ops
= param
? &event_enable_count_trigger_ops
:
1364 &event_enable_trigger_ops
;
1366 ops
= param
? &event_disable_count_trigger_ops
:
1367 &event_disable_trigger_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)
1402 ret
= register_event_command(&trigger_enable_cmd
);
1403 if (WARN_ON(ret
< 0))
1405 ret
= register_event_command(&trigger_disable_cmd
);
1406 if (WARN_ON(ret
< 0))
1407 unregister_trigger_enable_disable_cmds();
1412 static __init
int register_trigger_traceon_traceoff_cmds(void)
1416 ret
= register_event_command(&trigger_traceon_cmd
);
1417 if (WARN_ON(ret
< 0))
1419 ret
= register_event_command(&trigger_traceoff_cmd
);
1420 if (WARN_ON(ret
< 0))
1421 unregister_trigger_traceon_traceoff_cmds();
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();