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 ftrace_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 ftrace_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 ftrace_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 ftrace_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 ftrace_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 ftrace_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 ftrace_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 ftrace_event_file
*event_file
;
237 if (cnt
>= PAGE_SIZE
)
240 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
244 if (copy_from_user(buf
, ubuf
, cnt
)) {
245 free_page((unsigned long)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
);
258 ret
= trigger_process_regex(event_file
, buf
);
259 mutex_unlock(&event_mutex
);
261 free_page((unsigned long)buf
);
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
);
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
);
291 event_trigger_open(struct inode
*inode
, struct file
*filp
)
293 return event_trigger_regex_open(inode
, filp
);
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
,
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
314 static __init
int register_event_command(struct event_command
*cmd
)
316 struct event_command
*p
;
319 mutex_lock(&trigger_cmd_mutex
);
320 list_for_each_entry(p
, &trigger_commands
, list
) {
321 if (strcmp(cmd
->name
, p
->name
) == 0) {
326 list_add(&cmd
->list
, &trigger_commands
);
328 mutex_unlock(&trigger_cmd_mutex
);
334 * Currently we only unregister event commands from __init, so mark
337 static __init
int unregister_event_command(struct event_command
*cmd
)
339 struct event_command
*p
, *n
;
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) {
346 list_del_init(&p
->list
);
351 mutex_unlock(&trigger_cmd_mutex
);
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
371 event_trigger_print(const char *name
, struct seq_file
*m
,
372 void *data
, char *filter_str
)
374 long count
= (long)data
;
376 seq_printf(m
, "%s", name
);
379 seq_puts(m
, ":unlimited");
381 seq_printf(m
, ":count=%ld", count
);
384 seq_printf(m
, " if %s\n", filter_str
);
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
401 * Return: 0 on success, errno otherwise
404 event_trigger_init(struct event_trigger_ops
*ops
,
405 struct event_trigger_data
*data
)
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
422 event_trigger_free(struct event_trigger_ops
*ops
,
423 struct event_trigger_data
*data
)
425 if (WARN_ON_ONCE(data
->ref
<= 0))
430 trigger_data_free(data
);
433 static int trace_event_trigger_enable_disable(struct ftrace_event_file
*file
,
438 if (trigger_enable
) {
439 if (atomic_inc_return(&file
->tm_ref
) > 1)
441 set_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT
, &file
->flags
);
442 ret
= trace_event_enable_disable(file
, 1, 1);
444 if (atomic_dec_return(&file
->tm_ref
) > 0)
446 clear_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT
, &file
->flags
);
447 ret
= trace_event_enable_disable(file
, 0, 1);
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.
467 clear_event_triggers(struct trace_array
*tr
)
469 struct ftrace_event_file
*file
;
471 list_for_each_entry(file
, &tr
->events
, list
) {
472 struct event_trigger_data
*data
;
473 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
474 trace_event_trigger_enable_disable(file
, 0);
476 data
->ops
->free(data
->ops
, data
);
482 * update_cond_flag - Set or reset the TRIGGER_COND bit
483 * @file: The ftrace_event_file associated with the event
485 * If an event has triggers and any of those triggers has a filter or
486 * a post_trigger, trigger invocation needs to be deferred until after
487 * the current event has logged its data, and the event should have
488 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
491 static void update_cond_flag(struct ftrace_event_file
*file
)
493 struct event_trigger_data
*data
;
494 bool set_cond
= false;
496 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
497 if (data
->filter
|| data
->cmd_ops
->post_trigger
) {
504 set_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT
, &file
->flags
);
506 clear_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT
, &file
->flags
);
510 * register_trigger - Generic event_command @reg implementation
511 * @glob: The raw string used to register the trigger
512 * @ops: The trigger ops associated with the trigger
513 * @data: Trigger-specific data to associate with the trigger
514 * @file: The ftrace_event_file associated with the event
516 * Common implementation for event trigger registration.
518 * Usually used directly as the @reg method in event command
521 * Return: 0 on success, errno otherwise
523 static int register_trigger(char *glob
, struct event_trigger_ops
*ops
,
524 struct event_trigger_data
*data
,
525 struct ftrace_event_file
*file
)
527 struct event_trigger_data
*test
;
530 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
531 if (test
->cmd_ops
->trigger_type
== data
->cmd_ops
->trigger_type
) {
537 if (data
->ops
->init
) {
538 ret
= data
->ops
->init(data
->ops
, data
);
543 list_add_rcu(&data
->list
, &file
->triggers
);
546 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
547 list_del_rcu(&data
->list
);
550 update_cond_flag(file
);
556 * unregister_trigger - Generic event_command @unreg implementation
557 * @glob: The raw string used to register the trigger
558 * @ops: The trigger ops associated with the trigger
559 * @test: Trigger-specific data used to find the trigger to remove
560 * @file: The ftrace_event_file associated with the event
562 * Common implementation for event trigger unregistration.
564 * Usually used directly as the @unreg method in event command
567 static void unregister_trigger(char *glob
, struct event_trigger_ops
*ops
,
568 struct event_trigger_data
*test
,
569 struct ftrace_event_file
*file
)
571 struct event_trigger_data
*data
;
572 bool unregistered
= false;
574 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
575 if (data
->cmd_ops
->trigger_type
== test
->cmd_ops
->trigger_type
) {
577 list_del_rcu(&data
->list
);
578 update_cond_flag(file
);
579 trace_event_trigger_enable_disable(file
, 0);
584 if (unregistered
&& data
->ops
->free
)
585 data
->ops
->free(data
->ops
, data
);
589 * event_trigger_callback - Generic event_command @func implementation
590 * @cmd_ops: The command ops, used for trigger registration
591 * @file: The ftrace_event_file associated with the event
592 * @glob: The raw string used to register the trigger
593 * @cmd: The cmd portion of the string used to register the trigger
594 * @param: The params portion of the string used to register the trigger
596 * Common implementation for event command parsing and trigger
599 * Usually used directly as the @func method in event command
602 * Return: 0 on success, errno otherwise
605 event_trigger_callback(struct event_command
*cmd_ops
,
606 struct ftrace_event_file
*file
,
607 char *glob
, char *cmd
, char *param
)
609 struct event_trigger_data
*trigger_data
;
610 struct event_trigger_ops
*trigger_ops
;
611 char *trigger
= NULL
;
615 /* separate the trigger from the filter (t:n [if filter]) */
616 if (param
&& isdigit(param
[0]))
617 trigger
= strsep(¶m
, " \t");
619 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
622 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
626 trigger_data
->count
= -1;
627 trigger_data
->ops
= trigger_ops
;
628 trigger_data
->cmd_ops
= cmd_ops
;
629 INIT_LIST_HEAD(&trigger_data
->list
);
631 if (glob
[0] == '!') {
632 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
639 number
= strsep(&trigger
, ":");
646 * We use the callback data field (which is a pointer)
649 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
654 if (!param
) /* if param is non-empty, it's supposed to be a filter */
657 if (!cmd_ops
->set_filter
)
660 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
665 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
667 * The above returns on success the # of functions enabled,
668 * but if it didn't find any functions it returns zero.
669 * Consider no functions a failure too.
681 if (cmd_ops
->set_filter
)
682 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
688 * set_trigger_filter - Generic event_command @set_filter implementation
689 * @filter_str: The filter string for the trigger, NULL to remove filter
690 * @trigger_data: Trigger-specific data
691 * @file: The ftrace_event_file associated with the event
693 * Common implementation for event command filter parsing and filter
696 * Usually used directly as the @set_filter method in event command
699 * Also used to remove a filter (if filter_str = NULL).
701 * Return: 0 on success, errno otherwise
703 static int set_trigger_filter(char *filter_str
,
704 struct event_trigger_data
*trigger_data
,
705 struct ftrace_event_file
*file
)
707 struct event_trigger_data
*data
= trigger_data
;
708 struct event_filter
*filter
= NULL
, *tmp
;
712 if (!filter_str
) /* clear the current filter */
715 s
= strsep(&filter_str
, " \t");
717 if (!strlen(s
) || strcmp(s
, "if") != 0)
723 /* The filter is for the 'trigger' event, not the triggered event */
724 ret
= create_event_filter(file
->event_call
, filter_str
, false, &filter
);
728 tmp
= rcu_access_pointer(data
->filter
);
730 rcu_assign_pointer(data
->filter
, filter
);
733 /* Make sure the call is done with the filter */
735 free_event_filter(tmp
);
738 kfree(data
->filter_str
);
739 data
->filter_str
= NULL
;
742 data
->filter_str
= kstrdup(filter_str
, GFP_KERNEL
);
743 if (!data
->filter_str
) {
744 free_event_filter(rcu_access_pointer(data
->filter
));
754 traceon_trigger(struct event_trigger_data
*data
)
763 traceon_count_trigger(struct event_trigger_data
*data
)
771 if (data
->count
!= -1)
778 traceoff_trigger(struct event_trigger_data
*data
)
780 if (!tracing_is_on())
787 traceoff_count_trigger(struct event_trigger_data
*data
)
789 if (!tracing_is_on())
795 if (data
->count
!= -1)
802 traceon_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
803 struct event_trigger_data
*data
)
805 return event_trigger_print("traceon", m
, (void *)data
->count
,
810 traceoff_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
811 struct event_trigger_data
*data
)
813 return event_trigger_print("traceoff", m
, (void *)data
->count
,
817 static struct event_trigger_ops traceon_trigger_ops
= {
818 .func
= traceon_trigger
,
819 .print
= traceon_trigger_print
,
820 .init
= event_trigger_init
,
821 .free
= event_trigger_free
,
824 static struct event_trigger_ops traceon_count_trigger_ops
= {
825 .func
= traceon_count_trigger
,
826 .print
= traceon_trigger_print
,
827 .init
= event_trigger_init
,
828 .free
= event_trigger_free
,
831 static struct event_trigger_ops traceoff_trigger_ops
= {
832 .func
= traceoff_trigger
,
833 .print
= traceoff_trigger_print
,
834 .init
= event_trigger_init
,
835 .free
= event_trigger_free
,
838 static struct event_trigger_ops traceoff_count_trigger_ops
= {
839 .func
= traceoff_count_trigger
,
840 .print
= traceoff_trigger_print
,
841 .init
= event_trigger_init
,
842 .free
= event_trigger_free
,
845 static struct event_trigger_ops
*
846 onoff_get_trigger_ops(char *cmd
, char *param
)
848 struct event_trigger_ops
*ops
;
850 /* we register both traceon and traceoff to this callback */
851 if (strcmp(cmd
, "traceon") == 0)
852 ops
= param
? &traceon_count_trigger_ops
:
853 &traceon_trigger_ops
;
855 ops
= param
? &traceoff_count_trigger_ops
:
856 &traceoff_trigger_ops
;
861 static struct event_command trigger_traceon_cmd
= {
863 .trigger_type
= ETT_TRACE_ONOFF
,
864 .func
= event_trigger_callback
,
865 .reg
= register_trigger
,
866 .unreg
= unregister_trigger
,
867 .get_trigger_ops
= onoff_get_trigger_ops
,
868 .set_filter
= set_trigger_filter
,
871 static struct event_command trigger_traceoff_cmd
= {
873 .trigger_type
= ETT_TRACE_ONOFF
,
874 .func
= event_trigger_callback
,
875 .reg
= register_trigger
,
876 .unreg
= unregister_trigger
,
877 .get_trigger_ops
= onoff_get_trigger_ops
,
878 .set_filter
= set_trigger_filter
,
881 #ifdef CONFIG_TRACER_SNAPSHOT
883 snapshot_trigger(struct event_trigger_data
*data
)
889 snapshot_count_trigger(struct event_trigger_data
*data
)
894 if (data
->count
!= -1)
897 snapshot_trigger(data
);
901 register_snapshot_trigger(char *glob
, struct event_trigger_ops
*ops
,
902 struct event_trigger_data
*data
,
903 struct ftrace_event_file
*file
)
905 int ret
= register_trigger(glob
, ops
, data
, file
);
907 if (ret
> 0 && tracing_alloc_snapshot() != 0) {
908 unregister_trigger(glob
, ops
, data
, file
);
916 snapshot_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
917 struct event_trigger_data
*data
)
919 return event_trigger_print("snapshot", m
, (void *)data
->count
,
923 static struct event_trigger_ops snapshot_trigger_ops
= {
924 .func
= snapshot_trigger
,
925 .print
= snapshot_trigger_print
,
926 .init
= event_trigger_init
,
927 .free
= event_trigger_free
,
930 static struct event_trigger_ops snapshot_count_trigger_ops
= {
931 .func
= snapshot_count_trigger
,
932 .print
= snapshot_trigger_print
,
933 .init
= event_trigger_init
,
934 .free
= event_trigger_free
,
937 static struct event_trigger_ops
*
938 snapshot_get_trigger_ops(char *cmd
, char *param
)
940 return param
? &snapshot_count_trigger_ops
: &snapshot_trigger_ops
;
943 static struct event_command trigger_snapshot_cmd
= {
945 .trigger_type
= ETT_SNAPSHOT
,
946 .func
= event_trigger_callback
,
947 .reg
= register_snapshot_trigger
,
948 .unreg
= unregister_trigger
,
949 .get_trigger_ops
= snapshot_get_trigger_ops
,
950 .set_filter
= set_trigger_filter
,
953 static __init
int register_trigger_snapshot_cmd(void)
957 ret
= register_event_command(&trigger_snapshot_cmd
);
963 static __init
int register_trigger_snapshot_cmd(void) { return 0; }
964 #endif /* CONFIG_TRACER_SNAPSHOT */
966 #ifdef CONFIG_STACKTRACE
969 * stacktrace_trigger()
970 * event_triggers_post_call()
971 * ftrace_raw_event_xxx()
976 stacktrace_trigger(struct event_trigger_data
*data
)
978 trace_dump_stack(STACK_SKIP
);
982 stacktrace_count_trigger(struct event_trigger_data
*data
)
987 if (data
->count
!= -1)
990 stacktrace_trigger(data
);
994 stacktrace_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
995 struct event_trigger_data
*data
)
997 return event_trigger_print("stacktrace", m
, (void *)data
->count
,
1001 static struct event_trigger_ops stacktrace_trigger_ops
= {
1002 .func
= stacktrace_trigger
,
1003 .print
= stacktrace_trigger_print
,
1004 .init
= event_trigger_init
,
1005 .free
= event_trigger_free
,
1008 static struct event_trigger_ops stacktrace_count_trigger_ops
= {
1009 .func
= stacktrace_count_trigger
,
1010 .print
= stacktrace_trigger_print
,
1011 .init
= event_trigger_init
,
1012 .free
= event_trigger_free
,
1015 static struct event_trigger_ops
*
1016 stacktrace_get_trigger_ops(char *cmd
, char *param
)
1018 return param
? &stacktrace_count_trigger_ops
: &stacktrace_trigger_ops
;
1021 static struct event_command trigger_stacktrace_cmd
= {
1022 .name
= "stacktrace",
1023 .trigger_type
= ETT_STACKTRACE
,
1024 .post_trigger
= true,
1025 .func
= event_trigger_callback
,
1026 .reg
= register_trigger
,
1027 .unreg
= unregister_trigger
,
1028 .get_trigger_ops
= stacktrace_get_trigger_ops
,
1029 .set_filter
= set_trigger_filter
,
1032 static __init
int register_trigger_stacktrace_cmd(void)
1036 ret
= register_event_command(&trigger_stacktrace_cmd
);
1042 static __init
int register_trigger_stacktrace_cmd(void) { return 0; }
1043 #endif /* CONFIG_STACKTRACE */
1045 static __init
void unregister_trigger_traceon_traceoff_cmds(void)
1047 unregister_event_command(&trigger_traceon_cmd
);
1048 unregister_event_command(&trigger_traceoff_cmd
);
1052 #define ENABLE_EVENT_STR "enable_event"
1053 #define DISABLE_EVENT_STR "disable_event"
1055 struct enable_trigger_data
{
1056 struct ftrace_event_file
*file
;
1061 event_enable_trigger(struct event_trigger_data
*data
)
1063 struct enable_trigger_data
*enable_data
= data
->private_data
;
1065 if (enable_data
->enable
)
1066 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1068 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1072 event_enable_count_trigger(struct event_trigger_data
*data
)
1074 struct enable_trigger_data
*enable_data
= data
->private_data
;
1079 /* Skip if the event is in a state we want to switch to */
1080 if (enable_data
->enable
== !(enable_data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1083 if (data
->count
!= -1)
1086 event_enable_trigger(data
);
1090 event_enable_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1091 struct event_trigger_data
*data
)
1093 struct enable_trigger_data
*enable_data
= data
->private_data
;
1095 seq_printf(m
, "%s:%s:%s",
1096 enable_data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1097 enable_data
->file
->event_call
->class->system
,
1098 enable_data
->file
->event_call
->name
);
1100 if (data
->count
== -1)
1101 seq_puts(m
, ":unlimited");
1103 seq_printf(m
, ":count=%ld", data
->count
);
1105 if (data
->filter_str
)
1106 seq_printf(m
, " if %s\n", data
->filter_str
);
1114 event_enable_trigger_free(struct event_trigger_ops
*ops
,
1115 struct event_trigger_data
*data
)
1117 struct enable_trigger_data
*enable_data
= data
->private_data
;
1119 if (WARN_ON_ONCE(data
->ref
<= 0))
1124 /* Remove the SOFT_MODE flag */
1125 trace_event_enable_disable(enable_data
->file
, 0, 1);
1126 module_put(enable_data
->file
->event_call
->mod
);
1127 trigger_data_free(data
);
1132 static struct event_trigger_ops event_enable_trigger_ops
= {
1133 .func
= event_enable_trigger
,
1134 .print
= event_enable_trigger_print
,
1135 .init
= event_trigger_init
,
1136 .free
= event_enable_trigger_free
,
1139 static struct event_trigger_ops event_enable_count_trigger_ops
= {
1140 .func
= event_enable_count_trigger
,
1141 .print
= event_enable_trigger_print
,
1142 .init
= event_trigger_init
,
1143 .free
= event_enable_trigger_free
,
1146 static struct event_trigger_ops event_disable_trigger_ops
= {
1147 .func
= event_enable_trigger
,
1148 .print
= event_enable_trigger_print
,
1149 .init
= event_trigger_init
,
1150 .free
= event_enable_trigger_free
,
1153 static struct event_trigger_ops event_disable_count_trigger_ops
= {
1154 .func
= event_enable_count_trigger
,
1155 .print
= event_enable_trigger_print
,
1156 .init
= event_trigger_init
,
1157 .free
= event_enable_trigger_free
,
1161 event_enable_trigger_func(struct event_command
*cmd_ops
,
1162 struct ftrace_event_file
*file
,
1163 char *glob
, char *cmd
, char *param
)
1165 struct ftrace_event_file
*event_enable_file
;
1166 struct enable_trigger_data
*enable_data
;
1167 struct event_trigger_data
*trigger_data
;
1168 struct event_trigger_ops
*trigger_ops
;
1169 struct trace_array
*tr
= file
->tr
;
1180 /* separate the trigger from the filter (s:e:n [if filter]) */
1181 trigger
= strsep(¶m
, " \t");
1185 system
= strsep(&trigger
, ":");
1189 event
= strsep(&trigger
, ":");
1192 event_enable_file
= find_event_file(tr
, system
, event
);
1193 if (!event_enable_file
)
1196 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1198 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
1201 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
1205 enable_data
= kzalloc(sizeof(*enable_data
), GFP_KERNEL
);
1207 kfree(trigger_data
);
1211 trigger_data
->count
= -1;
1212 trigger_data
->ops
= trigger_ops
;
1213 trigger_data
->cmd_ops
= cmd_ops
;
1214 INIT_LIST_HEAD(&trigger_data
->list
);
1215 RCU_INIT_POINTER(trigger_data
->filter
, NULL
);
1217 enable_data
->enable
= enable
;
1218 enable_data
->file
= event_enable_file
;
1219 trigger_data
->private_data
= enable_data
;
1221 if (glob
[0] == '!') {
1222 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
1223 kfree(trigger_data
);
1230 number
= strsep(&trigger
, ":");
1233 if (!strlen(number
))
1237 * We use the callback data field (which is a pointer)
1240 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
1245 if (!param
) /* if param is non-empty, it's supposed to be a filter */
1248 if (!cmd_ops
->set_filter
)
1251 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
1256 /* Don't let event modules unload while probe registered */
1257 ret
= try_module_get(event_enable_file
->event_call
->mod
);
1263 ret
= trace_event_enable_disable(event_enable_file
, 1, 1);
1266 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
1268 * The above returns on success the # of functions enabled,
1269 * but if it didn't find any functions it returns zero.
1270 * Consider no functions a failure too.
1277 /* Just return zero, not the number of enabled functions */
1283 trace_event_enable_disable(event_enable_file
, 0, 1);
1285 module_put(event_enable_file
->event_call
->mod
);
1287 if (cmd_ops
->set_filter
)
1288 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
1289 kfree(trigger_data
);
1294 static int event_enable_register_trigger(char *glob
,
1295 struct event_trigger_ops
*ops
,
1296 struct event_trigger_data
*data
,
1297 struct ftrace_event_file
*file
)
1299 struct enable_trigger_data
*enable_data
= data
->private_data
;
1300 struct enable_trigger_data
*test_enable_data
;
1301 struct event_trigger_data
*test
;
1304 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
1305 test_enable_data
= test
->private_data
;
1306 if (test_enable_data
&&
1307 (test_enable_data
->file
== enable_data
->file
)) {
1313 if (data
->ops
->init
) {
1314 ret
= data
->ops
->init(data
->ops
, data
);
1319 list_add_rcu(&data
->list
, &file
->triggers
);
1322 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
1323 list_del_rcu(&data
->list
);
1326 update_cond_flag(file
);
1331 static void event_enable_unregister_trigger(char *glob
,
1332 struct event_trigger_ops
*ops
,
1333 struct event_trigger_data
*test
,
1334 struct ftrace_event_file
*file
)
1336 struct enable_trigger_data
*test_enable_data
= test
->private_data
;
1337 struct enable_trigger_data
*enable_data
;
1338 struct event_trigger_data
*data
;
1339 bool unregistered
= false;
1341 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
1342 enable_data
= data
->private_data
;
1344 (enable_data
->file
== test_enable_data
->file
)) {
1345 unregistered
= true;
1346 list_del_rcu(&data
->list
);
1347 update_cond_flag(file
);
1348 trace_event_trigger_enable_disable(file
, 0);
1353 if (unregistered
&& data
->ops
->free
)
1354 data
->ops
->free(data
->ops
, data
);
1357 static struct event_trigger_ops
*
1358 event_enable_get_trigger_ops(char *cmd
, char *param
)
1360 struct event_trigger_ops
*ops
;
1363 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1366 ops
= param
? &event_enable_count_trigger_ops
:
1367 &event_enable_trigger_ops
;
1369 ops
= param
? &event_disable_count_trigger_ops
:
1370 &event_disable_trigger_ops
;
1375 static struct event_command trigger_enable_cmd
= {
1376 .name
= ENABLE_EVENT_STR
,
1377 .trigger_type
= ETT_EVENT_ENABLE
,
1378 .func
= event_enable_trigger_func
,
1379 .reg
= event_enable_register_trigger
,
1380 .unreg
= event_enable_unregister_trigger
,
1381 .get_trigger_ops
= event_enable_get_trigger_ops
,
1382 .set_filter
= set_trigger_filter
,
1385 static struct event_command trigger_disable_cmd
= {
1386 .name
= DISABLE_EVENT_STR
,
1387 .trigger_type
= ETT_EVENT_ENABLE
,
1388 .func
= event_enable_trigger_func
,
1389 .reg
= event_enable_register_trigger
,
1390 .unreg
= event_enable_unregister_trigger
,
1391 .get_trigger_ops
= event_enable_get_trigger_ops
,
1392 .set_filter
= set_trigger_filter
,
1395 static __init
void unregister_trigger_enable_disable_cmds(void)
1397 unregister_event_command(&trigger_enable_cmd
);
1398 unregister_event_command(&trigger_disable_cmd
);
1401 static __init
int register_trigger_enable_disable_cmds(void)
1405 ret
= register_event_command(&trigger_enable_cmd
);
1406 if (WARN_ON(ret
< 0))
1408 ret
= register_event_command(&trigger_disable_cmd
);
1409 if (WARN_ON(ret
< 0))
1410 unregister_trigger_enable_disable_cmds();
1415 static __init
int register_trigger_traceon_traceoff_cmds(void)
1419 ret
= register_event_command(&trigger_traceon_cmd
);
1420 if (WARN_ON(ret
< 0))
1422 ret
= register_event_command(&trigger_traceoff_cmd
);
1423 if (WARN_ON(ret
< 0))
1424 unregister_trigger_traceon_traceoff_cmds();
1429 __init
int register_trigger_cmds(void)
1431 register_trigger_traceon_traceoff_cmds();
1432 register_trigger_snapshot_cmd();
1433 register_trigger_stacktrace_cmd();
1434 register_trigger_enable_disable_cmds();