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
= (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
;
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 trace_event_file
*file
,
438 if (trigger_enable
) {
439 if (atomic_inc_return(&file
->tm_ref
) > 1)
441 set_bit(EVENT_FILE_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(EVENT_FILE_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 trace_event_file
*file
;
471 list_for_each_entry(file
, &tr
->events
, list
) {
472 struct event_trigger_data
*data
, *n
;
473 list_for_each_entry_safe(data
, n
, &file
->triggers
, list
) {
474 trace_event_trigger_enable_disable(file
, 0);
475 list_del_rcu(&data
->list
);
477 data
->ops
->free(data
->ops
, data
);
483 * update_cond_flag - Set or reset the TRIGGER_COND bit
484 * @file: The trace_event_file associated with the event
486 * If an event has triggers and any of those triggers has a filter or
487 * a post_trigger, trigger invocation needs to be deferred until after
488 * the current event has logged its data, and the event should have
489 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
492 static void update_cond_flag(struct trace_event_file
*file
)
494 struct event_trigger_data
*data
;
495 bool set_cond
= false;
497 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
498 if (data
->filter
|| data
->cmd_ops
->post_trigger
) {
505 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
507 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
511 * register_trigger - Generic event_command @reg implementation
512 * @glob: The raw string used to register the trigger
513 * @ops: The trigger ops associated with the trigger
514 * @data: Trigger-specific data to associate with the trigger
515 * @file: The trace_event_file associated with the event
517 * Common implementation for event trigger registration.
519 * Usually used directly as the @reg method in event command
522 * Return: 0 on success, errno otherwise
524 static int register_trigger(char *glob
, struct event_trigger_ops
*ops
,
525 struct event_trigger_data
*data
,
526 struct trace_event_file
*file
)
528 struct event_trigger_data
*test
;
531 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
532 if (test
->cmd_ops
->trigger_type
== data
->cmd_ops
->trigger_type
) {
538 if (data
->ops
->init
) {
539 ret
= data
->ops
->init(data
->ops
, data
);
544 list_add_rcu(&data
->list
, &file
->triggers
);
547 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
548 list_del_rcu(&data
->list
);
551 update_cond_flag(file
);
557 * unregister_trigger - Generic event_command @unreg implementation
558 * @glob: The raw string used to register the trigger
559 * @ops: The trigger ops associated with the trigger
560 * @test: Trigger-specific data used to find the trigger to remove
561 * @file: The trace_event_file associated with the event
563 * Common implementation for event trigger unregistration.
565 * Usually used directly as the @unreg method in event command
568 static void unregister_trigger(char *glob
, struct event_trigger_ops
*ops
,
569 struct event_trigger_data
*test
,
570 struct trace_event_file
*file
)
572 struct event_trigger_data
*data
;
573 bool unregistered
= false;
575 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
576 if (data
->cmd_ops
->trigger_type
== test
->cmd_ops
->trigger_type
) {
578 list_del_rcu(&data
->list
);
579 update_cond_flag(file
);
580 trace_event_trigger_enable_disable(file
, 0);
585 if (unregistered
&& data
->ops
->free
)
586 data
->ops
->free(data
->ops
, data
);
590 * event_trigger_callback - Generic event_command @func implementation
591 * @cmd_ops: The command ops, used for trigger registration
592 * @file: The trace_event_file associated with the event
593 * @glob: The raw string used to register the trigger
594 * @cmd: The cmd portion of the string used to register the trigger
595 * @param: The params portion of the string used to register the trigger
597 * Common implementation for event command parsing and trigger
600 * Usually used directly as the @func method in event command
603 * Return: 0 on success, errno otherwise
606 event_trigger_callback(struct event_command
*cmd_ops
,
607 struct trace_event_file
*file
,
608 char *glob
, char *cmd
, char *param
)
610 struct event_trigger_data
*trigger_data
;
611 struct event_trigger_ops
*trigger_ops
;
612 char *trigger
= NULL
;
616 /* separate the trigger from the filter (t:n [if filter]) */
617 if (param
&& isdigit(param
[0]))
618 trigger
= strsep(¶m
, " \t");
620 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
623 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
627 trigger_data
->count
= -1;
628 trigger_data
->ops
= trigger_ops
;
629 trigger_data
->cmd_ops
= cmd_ops
;
630 INIT_LIST_HEAD(&trigger_data
->list
);
632 if (glob
[0] == '!') {
633 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
640 number
= strsep(&trigger
, ":");
647 * We use the callback data field (which is a pointer)
650 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
655 if (!param
) /* if param is non-empty, it's supposed to be a filter */
658 if (!cmd_ops
->set_filter
)
661 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
666 /* Up the trigger_data count to make sure reg doesn't free it on failure */
667 event_trigger_init(trigger_ops
, trigger_data
);
668 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
670 * The above returns on success the # of functions enabled,
671 * but if it didn't find any functions it returns zero.
672 * Consider no functions a failure too.
675 cmd_ops
->unreg(glob
, trigger_ops
, trigger_data
, file
);
680 /* Down the counter of trigger_data or free it if not used anymore */
681 event_trigger_free(trigger_ops
, trigger_data
);
686 if (cmd_ops
->set_filter
)
687 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
693 * set_trigger_filter - Generic event_command @set_filter implementation
694 * @filter_str: The filter string for the trigger, NULL to remove filter
695 * @trigger_data: Trigger-specific data
696 * @file: The trace_event_file associated with the event
698 * Common implementation for event command filter parsing and filter
701 * Usually used directly as the @set_filter method in event command
704 * Also used to remove a filter (if filter_str = NULL).
706 * Return: 0 on success, errno otherwise
708 static int set_trigger_filter(char *filter_str
,
709 struct event_trigger_data
*trigger_data
,
710 struct trace_event_file
*file
)
712 struct event_trigger_data
*data
= trigger_data
;
713 struct event_filter
*filter
= NULL
, *tmp
;
717 if (!filter_str
) /* clear the current filter */
720 s
= strsep(&filter_str
, " \t");
722 if (!strlen(s
) || strcmp(s
, "if") != 0)
728 /* The filter is for the 'trigger' event, not the triggered event */
729 ret
= create_event_filter(file
->event_call
, filter_str
, false, &filter
);
733 tmp
= rcu_access_pointer(data
->filter
);
735 rcu_assign_pointer(data
->filter
, filter
);
738 /* Make sure the call is done with the filter */
740 free_event_filter(tmp
);
743 kfree(data
->filter_str
);
744 data
->filter_str
= NULL
;
747 data
->filter_str
= kstrdup(filter_str
, GFP_KERNEL
);
748 if (!data
->filter_str
) {
749 free_event_filter(rcu_access_pointer(data
->filter
));
759 traceon_trigger(struct event_trigger_data
*data
)
768 traceon_count_trigger(struct event_trigger_data
*data
)
776 if (data
->count
!= -1)
783 traceoff_trigger(struct event_trigger_data
*data
)
785 if (!tracing_is_on())
792 traceoff_count_trigger(struct event_trigger_data
*data
)
794 if (!tracing_is_on())
800 if (data
->count
!= -1)
807 traceon_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
808 struct event_trigger_data
*data
)
810 return event_trigger_print("traceon", m
, (void *)data
->count
,
815 traceoff_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
816 struct event_trigger_data
*data
)
818 return event_trigger_print("traceoff", m
, (void *)data
->count
,
822 static struct event_trigger_ops traceon_trigger_ops
= {
823 .func
= traceon_trigger
,
824 .print
= traceon_trigger_print
,
825 .init
= event_trigger_init
,
826 .free
= event_trigger_free
,
829 static struct event_trigger_ops traceon_count_trigger_ops
= {
830 .func
= traceon_count_trigger
,
831 .print
= traceon_trigger_print
,
832 .init
= event_trigger_init
,
833 .free
= event_trigger_free
,
836 static struct event_trigger_ops traceoff_trigger_ops
= {
837 .func
= traceoff_trigger
,
838 .print
= traceoff_trigger_print
,
839 .init
= event_trigger_init
,
840 .free
= event_trigger_free
,
843 static struct event_trigger_ops traceoff_count_trigger_ops
= {
844 .func
= traceoff_count_trigger
,
845 .print
= traceoff_trigger_print
,
846 .init
= event_trigger_init
,
847 .free
= event_trigger_free
,
850 static struct event_trigger_ops
*
851 onoff_get_trigger_ops(char *cmd
, char *param
)
853 struct event_trigger_ops
*ops
;
855 /* we register both traceon and traceoff to this callback */
856 if (strcmp(cmd
, "traceon") == 0)
857 ops
= param
? &traceon_count_trigger_ops
:
858 &traceon_trigger_ops
;
860 ops
= param
? &traceoff_count_trigger_ops
:
861 &traceoff_trigger_ops
;
866 static struct event_command trigger_traceon_cmd
= {
868 .trigger_type
= ETT_TRACE_ONOFF
,
869 .func
= event_trigger_callback
,
870 .reg
= register_trigger
,
871 .unreg
= unregister_trigger
,
872 .get_trigger_ops
= onoff_get_trigger_ops
,
873 .set_filter
= set_trigger_filter
,
876 static struct event_command trigger_traceoff_cmd
= {
878 .trigger_type
= ETT_TRACE_ONOFF
,
879 .func
= event_trigger_callback
,
880 .reg
= register_trigger
,
881 .unreg
= unregister_trigger
,
882 .get_trigger_ops
= onoff_get_trigger_ops
,
883 .set_filter
= set_trigger_filter
,
886 #ifdef CONFIG_TRACER_SNAPSHOT
888 snapshot_trigger(struct event_trigger_data
*data
)
894 snapshot_count_trigger(struct event_trigger_data
*data
)
899 if (data
->count
!= -1)
902 snapshot_trigger(data
);
906 register_snapshot_trigger(char *glob
, struct event_trigger_ops
*ops
,
907 struct event_trigger_data
*data
,
908 struct trace_event_file
*file
)
910 int ret
= register_trigger(glob
, ops
, data
, file
);
912 if (ret
> 0 && tracing_alloc_snapshot() != 0) {
913 unregister_trigger(glob
, ops
, data
, file
);
921 snapshot_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
922 struct event_trigger_data
*data
)
924 return event_trigger_print("snapshot", m
, (void *)data
->count
,
928 static struct event_trigger_ops snapshot_trigger_ops
= {
929 .func
= snapshot_trigger
,
930 .print
= snapshot_trigger_print
,
931 .init
= event_trigger_init
,
932 .free
= event_trigger_free
,
935 static struct event_trigger_ops snapshot_count_trigger_ops
= {
936 .func
= snapshot_count_trigger
,
937 .print
= snapshot_trigger_print
,
938 .init
= event_trigger_init
,
939 .free
= event_trigger_free
,
942 static struct event_trigger_ops
*
943 snapshot_get_trigger_ops(char *cmd
, char *param
)
945 return param
? &snapshot_count_trigger_ops
: &snapshot_trigger_ops
;
948 static struct event_command trigger_snapshot_cmd
= {
950 .trigger_type
= ETT_SNAPSHOT
,
951 .func
= event_trigger_callback
,
952 .reg
= register_snapshot_trigger
,
953 .unreg
= unregister_trigger
,
954 .get_trigger_ops
= snapshot_get_trigger_ops
,
955 .set_filter
= set_trigger_filter
,
958 static __init
int register_trigger_snapshot_cmd(void)
962 ret
= register_event_command(&trigger_snapshot_cmd
);
968 static __init
int register_trigger_snapshot_cmd(void) { return 0; }
969 #endif /* CONFIG_TRACER_SNAPSHOT */
971 #ifdef CONFIG_STACKTRACE
974 * stacktrace_trigger()
975 * event_triggers_post_call()
976 * trace_event_raw_event_xxx()
981 stacktrace_trigger(struct event_trigger_data
*data
)
983 trace_dump_stack(STACK_SKIP
);
987 stacktrace_count_trigger(struct event_trigger_data
*data
)
992 if (data
->count
!= -1)
995 stacktrace_trigger(data
);
999 stacktrace_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1000 struct event_trigger_data
*data
)
1002 return event_trigger_print("stacktrace", m
, (void *)data
->count
,
1006 static struct event_trigger_ops stacktrace_trigger_ops
= {
1007 .func
= stacktrace_trigger
,
1008 .print
= stacktrace_trigger_print
,
1009 .init
= event_trigger_init
,
1010 .free
= event_trigger_free
,
1013 static struct event_trigger_ops stacktrace_count_trigger_ops
= {
1014 .func
= stacktrace_count_trigger
,
1015 .print
= stacktrace_trigger_print
,
1016 .init
= event_trigger_init
,
1017 .free
= event_trigger_free
,
1020 static struct event_trigger_ops
*
1021 stacktrace_get_trigger_ops(char *cmd
, char *param
)
1023 return param
? &stacktrace_count_trigger_ops
: &stacktrace_trigger_ops
;
1026 static struct event_command trigger_stacktrace_cmd
= {
1027 .name
= "stacktrace",
1028 .trigger_type
= ETT_STACKTRACE
,
1029 .post_trigger
= true,
1030 .func
= event_trigger_callback
,
1031 .reg
= register_trigger
,
1032 .unreg
= unregister_trigger
,
1033 .get_trigger_ops
= stacktrace_get_trigger_ops
,
1034 .set_filter
= set_trigger_filter
,
1037 static __init
int register_trigger_stacktrace_cmd(void)
1041 ret
= register_event_command(&trigger_stacktrace_cmd
);
1047 static __init
int register_trigger_stacktrace_cmd(void) { return 0; }
1048 #endif /* CONFIG_STACKTRACE */
1050 static __init
void unregister_trigger_traceon_traceoff_cmds(void)
1052 unregister_event_command(&trigger_traceon_cmd
);
1053 unregister_event_command(&trigger_traceoff_cmd
);
1057 #define ENABLE_EVENT_STR "enable_event"
1058 #define DISABLE_EVENT_STR "disable_event"
1060 struct enable_trigger_data
{
1061 struct trace_event_file
*file
;
1066 event_enable_trigger(struct event_trigger_data
*data
)
1068 struct enable_trigger_data
*enable_data
= data
->private_data
;
1070 if (enable_data
->enable
)
1071 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1073 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1077 event_enable_count_trigger(struct event_trigger_data
*data
)
1079 struct enable_trigger_data
*enable_data
= data
->private_data
;
1084 /* Skip if the event is in a state we want to switch to */
1085 if (enable_data
->enable
== !(enable_data
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1088 if (data
->count
!= -1)
1091 event_enable_trigger(data
);
1095 event_enable_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1096 struct event_trigger_data
*data
)
1098 struct enable_trigger_data
*enable_data
= data
->private_data
;
1100 seq_printf(m
, "%s:%s:%s",
1101 enable_data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1102 enable_data
->file
->event_call
->class->system
,
1103 trace_event_name(enable_data
->file
->event_call
));
1105 if (data
->count
== -1)
1106 seq_puts(m
, ":unlimited");
1108 seq_printf(m
, ":count=%ld", data
->count
);
1110 if (data
->filter_str
)
1111 seq_printf(m
, " if %s\n", data
->filter_str
);
1119 event_enable_trigger_free(struct event_trigger_ops
*ops
,
1120 struct event_trigger_data
*data
)
1122 struct enable_trigger_data
*enable_data
= data
->private_data
;
1124 if (WARN_ON_ONCE(data
->ref
<= 0))
1129 /* Remove the SOFT_MODE flag */
1130 trace_event_enable_disable(enable_data
->file
, 0, 1);
1131 module_put(enable_data
->file
->event_call
->mod
);
1132 trigger_data_free(data
);
1137 static struct event_trigger_ops event_enable_trigger_ops
= {
1138 .func
= event_enable_trigger
,
1139 .print
= event_enable_trigger_print
,
1140 .init
= event_trigger_init
,
1141 .free
= event_enable_trigger_free
,
1144 static struct event_trigger_ops event_enable_count_trigger_ops
= {
1145 .func
= event_enable_count_trigger
,
1146 .print
= event_enable_trigger_print
,
1147 .init
= event_trigger_init
,
1148 .free
= event_enable_trigger_free
,
1151 static struct event_trigger_ops event_disable_trigger_ops
= {
1152 .func
= event_enable_trigger
,
1153 .print
= event_enable_trigger_print
,
1154 .init
= event_trigger_init
,
1155 .free
= event_enable_trigger_free
,
1158 static struct event_trigger_ops event_disable_count_trigger_ops
= {
1159 .func
= event_enable_count_trigger
,
1160 .print
= event_enable_trigger_print
,
1161 .init
= event_trigger_init
,
1162 .free
= event_enable_trigger_free
,
1166 event_enable_trigger_func(struct event_command
*cmd_ops
,
1167 struct trace_event_file
*file
,
1168 char *glob
, char *cmd
, char *param
)
1170 struct trace_event_file
*event_enable_file
;
1171 struct enable_trigger_data
*enable_data
;
1172 struct event_trigger_data
*trigger_data
;
1173 struct event_trigger_ops
*trigger_ops
;
1174 struct trace_array
*tr
= file
->tr
;
1185 /* separate the trigger from the filter (s:e:n [if filter]) */
1186 trigger
= strsep(¶m
, " \t");
1190 system
= strsep(&trigger
, ":");
1194 event
= strsep(&trigger
, ":");
1197 event_enable_file
= find_event_file(tr
, system
, event
);
1198 if (!event_enable_file
)
1201 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1203 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
1206 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
1210 enable_data
= kzalloc(sizeof(*enable_data
), GFP_KERNEL
);
1212 kfree(trigger_data
);
1216 trigger_data
->count
= -1;
1217 trigger_data
->ops
= trigger_ops
;
1218 trigger_data
->cmd_ops
= cmd_ops
;
1219 INIT_LIST_HEAD(&trigger_data
->list
);
1220 RCU_INIT_POINTER(trigger_data
->filter
, NULL
);
1222 enable_data
->enable
= enable
;
1223 enable_data
->file
= event_enable_file
;
1224 trigger_data
->private_data
= enable_data
;
1226 if (glob
[0] == '!') {
1227 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
1228 kfree(trigger_data
);
1234 /* Up the trigger_data count to make sure nothing frees it on failure */
1235 event_trigger_init(trigger_ops
, trigger_data
);
1238 number
= strsep(&trigger
, ":");
1241 if (!strlen(number
))
1245 * We use the callback data field (which is a pointer)
1248 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
1253 if (!param
) /* if param is non-empty, it's supposed to be a filter */
1256 if (!cmd_ops
->set_filter
)
1259 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
1264 /* Don't let event modules unload while probe registered */
1265 ret
= try_module_get(event_enable_file
->event_call
->mod
);
1271 ret
= trace_event_enable_disable(event_enable_file
, 1, 1);
1274 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
1276 * The above returns on success the # of functions enabled,
1277 * but if it didn't find any functions it returns zero.
1278 * Consider no functions a failure too.
1285 /* Just return zero, not the number of enabled functions */
1287 event_trigger_free(trigger_ops
, trigger_data
);
1292 trace_event_enable_disable(event_enable_file
, 0, 1);
1294 module_put(event_enable_file
->event_call
->mod
);
1296 if (cmd_ops
->set_filter
)
1297 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
1298 event_trigger_free(trigger_ops
, trigger_data
);
1303 static int event_enable_register_trigger(char *glob
,
1304 struct event_trigger_ops
*ops
,
1305 struct event_trigger_data
*data
,
1306 struct trace_event_file
*file
)
1308 struct enable_trigger_data
*enable_data
= data
->private_data
;
1309 struct enable_trigger_data
*test_enable_data
;
1310 struct event_trigger_data
*test
;
1313 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
1314 test_enable_data
= test
->private_data
;
1315 if (test_enable_data
&&
1316 (test_enable_data
->file
== enable_data
->file
)) {
1322 if (data
->ops
->init
) {
1323 ret
= data
->ops
->init(data
->ops
, data
);
1328 list_add_rcu(&data
->list
, &file
->triggers
);
1331 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
1332 list_del_rcu(&data
->list
);
1335 update_cond_flag(file
);
1340 static void event_enable_unregister_trigger(char *glob
,
1341 struct event_trigger_ops
*ops
,
1342 struct event_trigger_data
*test
,
1343 struct trace_event_file
*file
)
1345 struct enable_trigger_data
*test_enable_data
= test
->private_data
;
1346 struct enable_trigger_data
*enable_data
;
1347 struct event_trigger_data
*data
;
1348 bool unregistered
= false;
1350 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
1351 enable_data
= data
->private_data
;
1353 (enable_data
->file
== test_enable_data
->file
)) {
1354 unregistered
= true;
1355 list_del_rcu(&data
->list
);
1356 update_cond_flag(file
);
1357 trace_event_trigger_enable_disable(file
, 0);
1362 if (unregistered
&& data
->ops
->free
)
1363 data
->ops
->free(data
->ops
, data
);
1366 static struct event_trigger_ops
*
1367 event_enable_get_trigger_ops(char *cmd
, char *param
)
1369 struct event_trigger_ops
*ops
;
1372 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1375 ops
= param
? &event_enable_count_trigger_ops
:
1376 &event_enable_trigger_ops
;
1378 ops
= param
? &event_disable_count_trigger_ops
:
1379 &event_disable_trigger_ops
;
1384 static struct event_command trigger_enable_cmd
= {
1385 .name
= ENABLE_EVENT_STR
,
1386 .trigger_type
= ETT_EVENT_ENABLE
,
1387 .func
= event_enable_trigger_func
,
1388 .reg
= event_enable_register_trigger
,
1389 .unreg
= event_enable_unregister_trigger
,
1390 .get_trigger_ops
= event_enable_get_trigger_ops
,
1391 .set_filter
= set_trigger_filter
,
1394 static struct event_command trigger_disable_cmd
= {
1395 .name
= DISABLE_EVENT_STR
,
1396 .trigger_type
= ETT_EVENT_ENABLE
,
1397 .func
= event_enable_trigger_func
,
1398 .reg
= event_enable_register_trigger
,
1399 .unreg
= event_enable_unregister_trigger
,
1400 .get_trigger_ops
= event_enable_get_trigger_ops
,
1401 .set_filter
= set_trigger_filter
,
1404 static __init
void unregister_trigger_enable_disable_cmds(void)
1406 unregister_event_command(&trigger_enable_cmd
);
1407 unregister_event_command(&trigger_disable_cmd
);
1410 static __init
int register_trigger_enable_disable_cmds(void)
1414 ret
= register_event_command(&trigger_enable_cmd
);
1415 if (WARN_ON(ret
< 0))
1417 ret
= register_event_command(&trigger_disable_cmd
);
1418 if (WARN_ON(ret
< 0))
1419 unregister_trigger_enable_disable_cmds();
1424 static __init
int register_trigger_traceon_traceoff_cmds(void)
1428 ret
= register_event_command(&trigger_traceon_cmd
);
1429 if (WARN_ON(ret
< 0))
1431 ret
= register_event_command(&trigger_traceoff_cmd
);
1432 if (WARN_ON(ret
< 0))
1433 unregister_trigger_traceon_traceoff_cmds();
1438 __init
int register_trigger_cmds(void)
1440 register_trigger_traceon_traceoff_cmds();
1441 register_trigger_snapshot_cmd();
1442 register_trigger_stacktrace_cmd();
1443 register_trigger_enable_disable_cmds();