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
);
31 void trigger_data_free(struct event_trigger_data
*data
)
33 if (data
->cmd_ops
->set_filter
)
34 data
->cmd_ops
->set_filter(NULL
, data
, NULL
);
36 synchronize_sched(); /* make sure current triggers exit before free */
41 * event_triggers_call - Call triggers associated with a trace event
42 * @file: The trace_event_file associated with the event
43 * @rec: The trace entry for the event, NULL for unconditional invocation
45 * For each trigger associated with an event, invoke the trigger
46 * function registered with the associated trigger command. If rec is
47 * non-NULL, it means that the trigger requires further processing and
48 * shouldn't be unconditionally invoked. If rec is non-NULL and the
49 * trigger has a filter associated with it, rec will checked against
50 * the filter and if the record matches the trigger will be invoked.
51 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
52 * in any case until the current event is written, the trigger
53 * function isn't invoked but the bit associated with the deferred
54 * trigger is set in the return value.
56 * Returns an enum event_trigger_type value containing a set bit for
57 * any trigger that should be deferred, ETT_NONE if nothing to defer.
59 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
61 * Return: an enum event_trigger_type value containing a set bit for
62 * any trigger that should be deferred, ETT_NONE if nothing to defer.
64 enum event_trigger_type
65 event_triggers_call(struct trace_event_file
*file
, void *rec
)
67 struct event_trigger_data
*data
;
68 enum event_trigger_type tt
= ETT_NONE
;
69 struct event_filter
*filter
;
71 if (list_empty(&file
->triggers
))
74 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
78 data
->ops
->func(data
, rec
);
81 filter
= rcu_dereference_sched(data
->filter
);
82 if (filter
&& !filter_match_preds(filter
, rec
))
84 if (event_command_post_trigger(data
->cmd_ops
)) {
85 tt
|= data
->cmd_ops
->trigger_type
;
88 data
->ops
->func(data
, rec
);
92 EXPORT_SYMBOL_GPL(event_triggers_call
);
95 * event_triggers_post_call - Call 'post_triggers' for a trace event
96 * @file: The trace_event_file associated with the event
97 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
98 * @rec: The trace entry for the event
100 * For each trigger associated with an event, invoke the trigger
101 * function registered with the associated trigger command, if the
102 * corresponding bit is set in the tt enum passed into this function.
103 * See @event_triggers_call for details on how those bits are set.
105 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
108 event_triggers_post_call(struct trace_event_file
*file
,
109 enum event_trigger_type tt
,
112 struct event_trigger_data
*data
;
114 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
117 if (data
->cmd_ops
->trigger_type
& tt
)
118 data
->ops
->func(data
, rec
);
121 EXPORT_SYMBOL_GPL(event_triggers_post_call
);
123 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
125 static void *trigger_next(struct seq_file
*m
, void *t
, loff_t
*pos
)
127 struct trace_event_file
*event_file
= event_file_data(m
->private);
129 if (t
== SHOW_AVAILABLE_TRIGGERS
)
132 return seq_list_next(t
, &event_file
->triggers
, pos
);
135 static void *trigger_start(struct seq_file
*m
, loff_t
*pos
)
137 struct trace_event_file
*event_file
;
139 /* ->stop() is called even if ->start() fails */
140 mutex_lock(&event_mutex
);
141 event_file
= event_file_data(m
->private);
142 if (unlikely(!event_file
))
143 return ERR_PTR(-ENODEV
);
145 if (list_empty(&event_file
->triggers
))
146 return *pos
== 0 ? SHOW_AVAILABLE_TRIGGERS
: NULL
;
148 return seq_list_start(&event_file
->triggers
, *pos
);
151 static void trigger_stop(struct seq_file
*m
, void *t
)
153 mutex_unlock(&event_mutex
);
156 static int trigger_show(struct seq_file
*m
, void *v
)
158 struct event_trigger_data
*data
;
159 struct event_command
*p
;
161 if (v
== SHOW_AVAILABLE_TRIGGERS
) {
162 seq_puts(m
, "# Available triggers:\n");
164 mutex_lock(&trigger_cmd_mutex
);
165 list_for_each_entry_reverse(p
, &trigger_commands
, list
)
166 seq_printf(m
, " %s", p
->name
);
168 mutex_unlock(&trigger_cmd_mutex
);
172 data
= list_entry(v
, struct event_trigger_data
, list
);
173 data
->ops
->print(m
, data
->ops
, data
);
178 static const struct seq_operations event_triggers_seq_ops
= {
179 .start
= trigger_start
,
180 .next
= trigger_next
,
181 .stop
= trigger_stop
,
182 .show
= trigger_show
,
185 static int event_trigger_regex_open(struct inode
*inode
, struct file
*file
)
189 mutex_lock(&event_mutex
);
191 if (unlikely(!event_file_data(file
))) {
192 mutex_unlock(&event_mutex
);
196 if ((file
->f_mode
& FMODE_WRITE
) &&
197 (file
->f_flags
& O_TRUNC
)) {
198 struct trace_event_file
*event_file
;
199 struct event_command
*p
;
201 event_file
= event_file_data(file
);
203 list_for_each_entry(p
, &trigger_commands
, list
) {
205 p
->unreg_all(event_file
);
209 if (file
->f_mode
& FMODE_READ
) {
210 ret
= seq_open(file
, &event_triggers_seq_ops
);
212 struct seq_file
*m
= file
->private_data
;
217 mutex_unlock(&event_mutex
);
222 static int trigger_process_regex(struct trace_event_file
*file
, char *buff
)
224 char *command
, *next
= buff
;
225 struct event_command
*p
;
228 command
= strsep(&next
, ": \t");
229 command
= (command
[0] != '!') ? command
: command
+ 1;
231 mutex_lock(&trigger_cmd_mutex
);
232 list_for_each_entry(p
, &trigger_commands
, list
) {
233 if (strcmp(p
->name
, command
) == 0) {
234 ret
= p
->func(p
, file
, buff
, command
, next
);
239 mutex_unlock(&trigger_cmd_mutex
);
244 static ssize_t
event_trigger_regex_write(struct file
*file
,
245 const char __user
*ubuf
,
246 size_t cnt
, loff_t
*ppos
)
248 struct trace_event_file
*event_file
;
255 if (cnt
>= PAGE_SIZE
)
258 buf
= memdup_user_nul(ubuf
, cnt
);
264 mutex_lock(&event_mutex
);
265 event_file
= event_file_data(file
);
266 if (unlikely(!event_file
)) {
267 mutex_unlock(&event_mutex
);
271 ret
= trigger_process_regex(event_file
, buf
);
272 mutex_unlock(&event_mutex
);
284 static int event_trigger_regex_release(struct inode
*inode
, struct file
*file
)
286 mutex_lock(&event_mutex
);
288 if (file
->f_mode
& FMODE_READ
)
289 seq_release(inode
, file
);
291 mutex_unlock(&event_mutex
);
297 event_trigger_write(struct file
*filp
, const char __user
*ubuf
,
298 size_t cnt
, loff_t
*ppos
)
300 return event_trigger_regex_write(filp
, ubuf
, cnt
, ppos
);
304 event_trigger_open(struct inode
*inode
, struct file
*filp
)
306 return event_trigger_regex_open(inode
, filp
);
310 event_trigger_release(struct inode
*inode
, struct file
*file
)
312 return event_trigger_regex_release(inode
, file
);
315 const struct file_operations event_trigger_fops
= {
316 .open
= event_trigger_open
,
318 .write
= event_trigger_write
,
319 .llseek
= tracing_lseek
,
320 .release
= event_trigger_release
,
324 * Currently we only register event commands from __init, so mark this
327 __init
int register_event_command(struct event_command
*cmd
)
329 struct event_command
*p
;
332 mutex_lock(&trigger_cmd_mutex
);
333 list_for_each_entry(p
, &trigger_commands
, list
) {
334 if (strcmp(cmd
->name
, p
->name
) == 0) {
339 list_add(&cmd
->list
, &trigger_commands
);
341 mutex_unlock(&trigger_cmd_mutex
);
347 * Currently we only unregister event commands from __init, so mark
350 static __init
int unregister_event_command(struct event_command
*cmd
)
352 struct event_command
*p
, *n
;
355 mutex_lock(&trigger_cmd_mutex
);
356 list_for_each_entry_safe(p
, n
, &trigger_commands
, list
) {
357 if (strcmp(cmd
->name
, p
->name
) == 0) {
359 list_del_init(&p
->list
);
364 mutex_unlock(&trigger_cmd_mutex
);
370 * event_trigger_print - Generic event_trigger_ops @print implementation
371 * @name: The name of the event trigger
372 * @m: The seq_file being printed to
373 * @data: Trigger-specific data
374 * @filter_str: filter_str to print, if present
376 * Common implementation for event triggers to print themselves.
378 * Usually wrapped by a function that simply sets the @name of the
379 * trigger command and then invokes this.
381 * Return: 0 on success, errno otherwise
384 event_trigger_print(const char *name
, struct seq_file
*m
,
385 void *data
, char *filter_str
)
387 long count
= (long)data
;
392 seq_puts(m
, ":unlimited");
394 seq_printf(m
, ":count=%ld", count
);
397 seq_printf(m
, " if %s\n", filter_str
);
405 * event_trigger_init - Generic event_trigger_ops @init implementation
406 * @ops: The trigger ops associated with the trigger
407 * @data: Trigger-specific data
409 * Common implementation of event trigger initialization.
411 * Usually used directly as the @init method in event trigger
414 * Return: 0 on success, errno otherwise
416 int event_trigger_init(struct event_trigger_ops
*ops
,
417 struct event_trigger_data
*data
)
424 * event_trigger_free - Generic event_trigger_ops @free implementation
425 * @ops: The trigger ops associated with the trigger
426 * @data: Trigger-specific data
428 * Common implementation of event trigger de-initialization.
430 * Usually used directly as the @free method in event trigger
434 event_trigger_free(struct event_trigger_ops
*ops
,
435 struct event_trigger_data
*data
)
437 if (WARN_ON_ONCE(data
->ref
<= 0))
442 trigger_data_free(data
);
445 int trace_event_trigger_enable_disable(struct trace_event_file
*file
,
450 if (trigger_enable
) {
451 if (atomic_inc_return(&file
->tm_ref
) > 1)
453 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
454 ret
= trace_event_enable_disable(file
, 1, 1);
456 if (atomic_dec_return(&file
->tm_ref
) > 0)
458 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT
, &file
->flags
);
459 ret
= trace_event_enable_disable(file
, 0, 1);
466 * clear_event_triggers - Clear all triggers associated with a trace array
467 * @tr: The trace array to clear
469 * For each trigger, the triggering event has its tm_ref decremented
470 * via trace_event_trigger_enable_disable(), and any associated event
471 * (in the case of enable/disable_event triggers) will have its sm_ref
472 * decremented via free()->trace_event_enable_disable(). That
473 * combination effectively reverses the soft-mode/trigger state added
474 * by trigger registration.
476 * Must be called with event_mutex held.
479 clear_event_triggers(struct trace_array
*tr
)
481 struct trace_event_file
*file
;
483 list_for_each_entry(file
, &tr
->events
, list
) {
484 struct event_trigger_data
*data
;
485 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
486 trace_event_trigger_enable_disable(file
, 0);
488 data
->ops
->free(data
->ops
, data
);
494 * update_cond_flag - Set or reset the TRIGGER_COND bit
495 * @file: The trace_event_file associated with the event
497 * If an event has triggers and any of those triggers has a filter or
498 * a post_trigger, trigger invocation needs to be deferred until after
499 * the current event has logged its data, and the event should have
500 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
503 void update_cond_flag(struct trace_event_file
*file
)
505 struct event_trigger_data
*data
;
506 bool set_cond
= false;
508 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
509 if (data
->filter
|| event_command_post_trigger(data
->cmd_ops
) ||
510 event_command_needs_rec(data
->cmd_ops
)) {
517 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
519 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT
, &file
->flags
);
523 * register_trigger - Generic event_command @reg implementation
524 * @glob: The raw string used to register the trigger
525 * @ops: The trigger ops associated with the trigger
526 * @data: Trigger-specific data to associate with the trigger
527 * @file: The trace_event_file associated with the event
529 * Common implementation for event trigger registration.
531 * Usually used directly as the @reg method in event command
534 * Return: 0 on success, errno otherwise
536 static int register_trigger(char *glob
, struct event_trigger_ops
*ops
,
537 struct event_trigger_data
*data
,
538 struct trace_event_file
*file
)
540 struct event_trigger_data
*test
;
543 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
544 if (test
->cmd_ops
->trigger_type
== data
->cmd_ops
->trigger_type
) {
550 if (data
->ops
->init
) {
551 ret
= data
->ops
->init(data
->ops
, data
);
556 list_add_rcu(&data
->list
, &file
->triggers
);
559 update_cond_flag(file
);
560 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
561 list_del_rcu(&data
->list
);
562 update_cond_flag(file
);
570 * unregister_trigger - Generic event_command @unreg implementation
571 * @glob: The raw string used to register the trigger
572 * @ops: The trigger ops associated with the trigger
573 * @test: Trigger-specific data used to find the trigger to remove
574 * @file: The trace_event_file associated with the event
576 * Common implementation for event trigger unregistration.
578 * Usually used directly as the @unreg method in event command
581 void unregister_trigger(char *glob
, struct event_trigger_ops
*ops
,
582 struct event_trigger_data
*test
,
583 struct trace_event_file
*file
)
585 struct event_trigger_data
*data
;
586 bool unregistered
= false;
588 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
589 if (data
->cmd_ops
->trigger_type
== test
->cmd_ops
->trigger_type
) {
591 list_del_rcu(&data
->list
);
592 trace_event_trigger_enable_disable(file
, 0);
593 update_cond_flag(file
);
598 if (unregistered
&& data
->ops
->free
)
599 data
->ops
->free(data
->ops
, data
);
603 * event_trigger_callback - Generic event_command @func implementation
604 * @cmd_ops: The command ops, used for trigger registration
605 * @file: The trace_event_file associated with the event
606 * @glob: The raw string used to register the trigger
607 * @cmd: The cmd portion of the string used to register the trigger
608 * @param: The params portion of the string used to register the trigger
610 * Common implementation for event command parsing and trigger
613 * Usually used directly as the @func method in event command
616 * Return: 0 on success, errno otherwise
619 event_trigger_callback(struct event_command
*cmd_ops
,
620 struct trace_event_file
*file
,
621 char *glob
, char *cmd
, char *param
)
623 struct event_trigger_data
*trigger_data
;
624 struct event_trigger_ops
*trigger_ops
;
625 char *trigger
= NULL
;
629 /* separate the trigger from the filter (t:n [if filter]) */
630 if (param
&& isdigit(param
[0]))
631 trigger
= strsep(¶m
, " \t");
633 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
636 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
640 trigger_data
->count
= -1;
641 trigger_data
->ops
= trigger_ops
;
642 trigger_data
->cmd_ops
= cmd_ops
;
643 INIT_LIST_HEAD(&trigger_data
->list
);
645 if (glob
[0] == '!') {
646 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
653 number
= strsep(&trigger
, ":");
660 * We use the callback data field (which is a pointer)
663 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
668 if (!param
) /* if param is non-empty, it's supposed to be a filter */
671 if (!cmd_ops
->set_filter
)
674 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
679 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
681 * The above returns on success the # of functions enabled,
682 * but if it didn't find any functions it returns zero.
683 * Consider no functions a failure too.
695 if (cmd_ops
->set_filter
)
696 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
702 * set_trigger_filter - Generic event_command @set_filter implementation
703 * @filter_str: The filter string for the trigger, NULL to remove filter
704 * @trigger_data: Trigger-specific data
705 * @file: The trace_event_file associated with the event
707 * Common implementation for event command filter parsing and filter
710 * Usually used directly as the @set_filter method in event command
713 * Also used to remove a filter (if filter_str = NULL).
715 * Return: 0 on success, errno otherwise
717 int set_trigger_filter(char *filter_str
,
718 struct event_trigger_data
*trigger_data
,
719 struct trace_event_file
*file
)
721 struct event_trigger_data
*data
= trigger_data
;
722 struct event_filter
*filter
= NULL
, *tmp
;
726 if (!filter_str
) /* clear the current filter */
729 s
= strsep(&filter_str
, " \t");
731 if (!strlen(s
) || strcmp(s
, "if") != 0)
737 /* The filter is for the 'trigger' event, not the triggered event */
738 ret
= create_event_filter(file
->event_call
, filter_str
, false, &filter
);
742 tmp
= rcu_access_pointer(data
->filter
);
744 rcu_assign_pointer(data
->filter
, filter
);
747 /* Make sure the call is done with the filter */
749 free_event_filter(tmp
);
752 kfree(data
->filter_str
);
753 data
->filter_str
= NULL
;
756 data
->filter_str
= kstrdup(filter_str
, GFP_KERNEL
);
757 if (!data
->filter_str
) {
758 free_event_filter(rcu_access_pointer(data
->filter
));
768 traceon_trigger(struct event_trigger_data
*data
, void *rec
)
777 traceon_count_trigger(struct event_trigger_data
*data
, void *rec
)
785 if (data
->count
!= -1)
792 traceoff_trigger(struct event_trigger_data
*data
, void *rec
)
794 if (!tracing_is_on())
801 traceoff_count_trigger(struct event_trigger_data
*data
, void *rec
)
803 if (!tracing_is_on())
809 if (data
->count
!= -1)
816 traceon_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
817 struct event_trigger_data
*data
)
819 return event_trigger_print("traceon", m
, (void *)data
->count
,
824 traceoff_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
825 struct event_trigger_data
*data
)
827 return event_trigger_print("traceoff", m
, (void *)data
->count
,
831 static struct event_trigger_ops traceon_trigger_ops
= {
832 .func
= traceon_trigger
,
833 .print
= traceon_trigger_print
,
834 .init
= event_trigger_init
,
835 .free
= event_trigger_free
,
838 static struct event_trigger_ops traceon_count_trigger_ops
= {
839 .func
= traceon_count_trigger
,
840 .print
= traceon_trigger_print
,
841 .init
= event_trigger_init
,
842 .free
= event_trigger_free
,
845 static struct event_trigger_ops traceoff_trigger_ops
= {
846 .func
= traceoff_trigger
,
847 .print
= traceoff_trigger_print
,
848 .init
= event_trigger_init
,
849 .free
= event_trigger_free
,
852 static struct event_trigger_ops traceoff_count_trigger_ops
= {
853 .func
= traceoff_count_trigger
,
854 .print
= traceoff_trigger_print
,
855 .init
= event_trigger_init
,
856 .free
= event_trigger_free
,
859 static struct event_trigger_ops
*
860 onoff_get_trigger_ops(char *cmd
, char *param
)
862 struct event_trigger_ops
*ops
;
864 /* we register both traceon and traceoff to this callback */
865 if (strcmp(cmd
, "traceon") == 0)
866 ops
= param
? &traceon_count_trigger_ops
:
867 &traceon_trigger_ops
;
869 ops
= param
? &traceoff_count_trigger_ops
:
870 &traceoff_trigger_ops
;
875 static struct event_command trigger_traceon_cmd
= {
877 .trigger_type
= ETT_TRACE_ONOFF
,
878 .func
= event_trigger_callback
,
879 .reg
= register_trigger
,
880 .unreg
= unregister_trigger
,
881 .get_trigger_ops
= onoff_get_trigger_ops
,
882 .set_filter
= set_trigger_filter
,
885 static struct event_command trigger_traceoff_cmd
= {
887 .trigger_type
= ETT_TRACE_ONOFF
,
888 .func
= event_trigger_callback
,
889 .reg
= register_trigger
,
890 .unreg
= unregister_trigger
,
891 .get_trigger_ops
= onoff_get_trigger_ops
,
892 .set_filter
= set_trigger_filter
,
895 #ifdef CONFIG_TRACER_SNAPSHOT
897 snapshot_trigger(struct event_trigger_data
*data
, void *rec
)
903 snapshot_count_trigger(struct event_trigger_data
*data
, void *rec
)
908 if (data
->count
!= -1)
911 snapshot_trigger(data
, rec
);
915 register_snapshot_trigger(char *glob
, struct event_trigger_ops
*ops
,
916 struct event_trigger_data
*data
,
917 struct trace_event_file
*file
)
919 int ret
= register_trigger(glob
, ops
, data
, file
);
921 if (ret
> 0 && tracing_alloc_snapshot() != 0) {
922 unregister_trigger(glob
, ops
, data
, file
);
930 snapshot_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
931 struct event_trigger_data
*data
)
933 return event_trigger_print("snapshot", m
, (void *)data
->count
,
937 static struct event_trigger_ops snapshot_trigger_ops
= {
938 .func
= snapshot_trigger
,
939 .print
= snapshot_trigger_print
,
940 .init
= event_trigger_init
,
941 .free
= event_trigger_free
,
944 static struct event_trigger_ops snapshot_count_trigger_ops
= {
945 .func
= snapshot_count_trigger
,
946 .print
= snapshot_trigger_print
,
947 .init
= event_trigger_init
,
948 .free
= event_trigger_free
,
951 static struct event_trigger_ops
*
952 snapshot_get_trigger_ops(char *cmd
, char *param
)
954 return param
? &snapshot_count_trigger_ops
: &snapshot_trigger_ops
;
957 static struct event_command trigger_snapshot_cmd
= {
959 .trigger_type
= ETT_SNAPSHOT
,
960 .func
= event_trigger_callback
,
961 .reg
= register_snapshot_trigger
,
962 .unreg
= unregister_trigger
,
963 .get_trigger_ops
= snapshot_get_trigger_ops
,
964 .set_filter
= set_trigger_filter
,
967 static __init
int register_trigger_snapshot_cmd(void)
971 ret
= register_event_command(&trigger_snapshot_cmd
);
977 static __init
int register_trigger_snapshot_cmd(void) { return 0; }
978 #endif /* CONFIG_TRACER_SNAPSHOT */
980 #ifdef CONFIG_STACKTRACE
983 * stacktrace_trigger()
984 * event_triggers_post_call()
985 * trace_event_raw_event_xxx()
990 stacktrace_trigger(struct event_trigger_data
*data
, void *rec
)
992 trace_dump_stack(STACK_SKIP
);
996 stacktrace_count_trigger(struct event_trigger_data
*data
, void *rec
)
1001 if (data
->count
!= -1)
1004 stacktrace_trigger(data
, rec
);
1008 stacktrace_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1009 struct event_trigger_data
*data
)
1011 return event_trigger_print("stacktrace", m
, (void *)data
->count
,
1015 static struct event_trigger_ops stacktrace_trigger_ops
= {
1016 .func
= stacktrace_trigger
,
1017 .print
= stacktrace_trigger_print
,
1018 .init
= event_trigger_init
,
1019 .free
= event_trigger_free
,
1022 static struct event_trigger_ops stacktrace_count_trigger_ops
= {
1023 .func
= stacktrace_count_trigger
,
1024 .print
= stacktrace_trigger_print
,
1025 .init
= event_trigger_init
,
1026 .free
= event_trigger_free
,
1029 static struct event_trigger_ops
*
1030 stacktrace_get_trigger_ops(char *cmd
, char *param
)
1032 return param
? &stacktrace_count_trigger_ops
: &stacktrace_trigger_ops
;
1035 static struct event_command trigger_stacktrace_cmd
= {
1036 .name
= "stacktrace",
1037 .trigger_type
= ETT_STACKTRACE
,
1038 .flags
= EVENT_CMD_FL_POST_TRIGGER
,
1039 .func
= event_trigger_callback
,
1040 .reg
= register_trigger
,
1041 .unreg
= unregister_trigger
,
1042 .get_trigger_ops
= stacktrace_get_trigger_ops
,
1043 .set_filter
= set_trigger_filter
,
1046 static __init
int register_trigger_stacktrace_cmd(void)
1050 ret
= register_event_command(&trigger_stacktrace_cmd
);
1056 static __init
int register_trigger_stacktrace_cmd(void) { return 0; }
1057 #endif /* CONFIG_STACKTRACE */
1059 static __init
void unregister_trigger_traceon_traceoff_cmds(void)
1061 unregister_event_command(&trigger_traceon_cmd
);
1062 unregister_event_command(&trigger_traceoff_cmd
);
1066 #define ENABLE_EVENT_STR "enable_event"
1067 #define DISABLE_EVENT_STR "disable_event"
1069 struct enable_trigger_data
{
1070 struct trace_event_file
*file
;
1075 event_enable_trigger(struct event_trigger_data
*data
, void *rec
)
1077 struct enable_trigger_data
*enable_data
= data
->private_data
;
1079 if (enable_data
->enable
)
1080 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1082 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &enable_data
->file
->flags
);
1086 event_enable_count_trigger(struct event_trigger_data
*data
, void *rec
)
1088 struct enable_trigger_data
*enable_data
= data
->private_data
;
1093 /* Skip if the event is in a state we want to switch to */
1094 if (enable_data
->enable
== !(enable_data
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1097 if (data
->count
!= -1)
1100 event_enable_trigger(data
, rec
);
1104 event_enable_trigger_print(struct seq_file
*m
, struct event_trigger_ops
*ops
,
1105 struct event_trigger_data
*data
)
1107 struct enable_trigger_data
*enable_data
= data
->private_data
;
1109 seq_printf(m
, "%s:%s:%s",
1110 enable_data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1111 enable_data
->file
->event_call
->class->system
,
1112 trace_event_name(enable_data
->file
->event_call
));
1114 if (data
->count
== -1)
1115 seq_puts(m
, ":unlimited");
1117 seq_printf(m
, ":count=%ld", data
->count
);
1119 if (data
->filter_str
)
1120 seq_printf(m
, " if %s\n", data
->filter_str
);
1128 event_enable_trigger_free(struct event_trigger_ops
*ops
,
1129 struct event_trigger_data
*data
)
1131 struct enable_trigger_data
*enable_data
= data
->private_data
;
1133 if (WARN_ON_ONCE(data
->ref
<= 0))
1138 /* Remove the SOFT_MODE flag */
1139 trace_event_enable_disable(enable_data
->file
, 0, 1);
1140 module_put(enable_data
->file
->event_call
->mod
);
1141 trigger_data_free(data
);
1146 static struct event_trigger_ops event_enable_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_enable_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
,
1160 static struct event_trigger_ops event_disable_trigger_ops
= {
1161 .func
= event_enable_trigger
,
1162 .print
= event_enable_trigger_print
,
1163 .init
= event_trigger_init
,
1164 .free
= event_enable_trigger_free
,
1167 static struct event_trigger_ops event_disable_count_trigger_ops
= {
1168 .func
= event_enable_count_trigger
,
1169 .print
= event_enable_trigger_print
,
1170 .init
= event_trigger_init
,
1171 .free
= event_enable_trigger_free
,
1175 event_enable_trigger_func(struct event_command
*cmd_ops
,
1176 struct trace_event_file
*file
,
1177 char *glob
, char *cmd
, char *param
)
1179 struct trace_event_file
*event_enable_file
;
1180 struct enable_trigger_data
*enable_data
;
1181 struct event_trigger_data
*trigger_data
;
1182 struct event_trigger_ops
*trigger_ops
;
1183 struct trace_array
*tr
= file
->tr
;
1194 /* separate the trigger from the filter (s:e:n [if filter]) */
1195 trigger
= strsep(¶m
, " \t");
1199 system
= strsep(&trigger
, ":");
1203 event
= strsep(&trigger
, ":");
1206 event_enable_file
= find_event_file(tr
, system
, event
);
1207 if (!event_enable_file
)
1210 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1212 trigger_ops
= cmd_ops
->get_trigger_ops(cmd
, trigger
);
1215 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
1219 enable_data
= kzalloc(sizeof(*enable_data
), GFP_KERNEL
);
1221 kfree(trigger_data
);
1225 trigger_data
->count
= -1;
1226 trigger_data
->ops
= trigger_ops
;
1227 trigger_data
->cmd_ops
= cmd_ops
;
1228 INIT_LIST_HEAD(&trigger_data
->list
);
1229 RCU_INIT_POINTER(trigger_data
->filter
, NULL
);
1231 enable_data
->enable
= enable
;
1232 enable_data
->file
= event_enable_file
;
1233 trigger_data
->private_data
= enable_data
;
1235 if (glob
[0] == '!') {
1236 cmd_ops
->unreg(glob
+1, trigger_ops
, trigger_data
, file
);
1237 kfree(trigger_data
);
1244 number
= strsep(&trigger
, ":");
1247 if (!strlen(number
))
1251 * We use the callback data field (which is a pointer)
1254 ret
= kstrtoul(number
, 0, &trigger_data
->count
);
1259 if (!param
) /* if param is non-empty, it's supposed to be a filter */
1262 if (!cmd_ops
->set_filter
)
1265 ret
= cmd_ops
->set_filter(param
, trigger_data
, file
);
1270 /* Don't let event modules unload while probe registered */
1271 ret
= try_module_get(event_enable_file
->event_call
->mod
);
1277 ret
= trace_event_enable_disable(event_enable_file
, 1, 1);
1280 ret
= cmd_ops
->reg(glob
, trigger_ops
, trigger_data
, file
);
1282 * The above returns on success the # of functions enabled,
1283 * but if it didn't find any functions it returns zero.
1284 * Consider no functions a failure too.
1291 /* Just return zero, not the number of enabled functions */
1297 trace_event_enable_disable(event_enable_file
, 0, 1);
1299 module_put(event_enable_file
->event_call
->mod
);
1301 if (cmd_ops
->set_filter
)
1302 cmd_ops
->set_filter(NULL
, trigger_data
, NULL
);
1303 kfree(trigger_data
);
1308 static int event_enable_register_trigger(char *glob
,
1309 struct event_trigger_ops
*ops
,
1310 struct event_trigger_data
*data
,
1311 struct trace_event_file
*file
)
1313 struct enable_trigger_data
*enable_data
= data
->private_data
;
1314 struct enable_trigger_data
*test_enable_data
;
1315 struct event_trigger_data
*test
;
1318 list_for_each_entry_rcu(test
, &file
->triggers
, list
) {
1319 test_enable_data
= test
->private_data
;
1320 if (test_enable_data
&&
1321 (test_enable_data
->file
== enable_data
->file
)) {
1327 if (data
->ops
->init
) {
1328 ret
= data
->ops
->init(data
->ops
, data
);
1333 list_add_rcu(&data
->list
, &file
->triggers
);
1336 update_cond_flag(file
);
1337 if (trace_event_trigger_enable_disable(file
, 1) < 0) {
1338 list_del_rcu(&data
->list
);
1339 update_cond_flag(file
);
1346 static void event_enable_unregister_trigger(char *glob
,
1347 struct event_trigger_ops
*ops
,
1348 struct event_trigger_data
*test
,
1349 struct trace_event_file
*file
)
1351 struct enable_trigger_data
*test_enable_data
= test
->private_data
;
1352 struct enable_trigger_data
*enable_data
;
1353 struct event_trigger_data
*data
;
1354 bool unregistered
= false;
1356 list_for_each_entry_rcu(data
, &file
->triggers
, list
) {
1357 enable_data
= data
->private_data
;
1359 (enable_data
->file
== test_enable_data
->file
)) {
1360 unregistered
= true;
1361 list_del_rcu(&data
->list
);
1362 trace_event_trigger_enable_disable(file
, 0);
1363 update_cond_flag(file
);
1368 if (unregistered
&& data
->ops
->free
)
1369 data
->ops
->free(data
->ops
, data
);
1372 static struct event_trigger_ops
*
1373 event_enable_get_trigger_ops(char *cmd
, char *param
)
1375 struct event_trigger_ops
*ops
;
1378 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
1381 ops
= param
? &event_enable_count_trigger_ops
:
1382 &event_enable_trigger_ops
;
1384 ops
= param
? &event_disable_count_trigger_ops
:
1385 &event_disable_trigger_ops
;
1390 static struct event_command trigger_enable_cmd
= {
1391 .name
= ENABLE_EVENT_STR
,
1392 .trigger_type
= ETT_EVENT_ENABLE
,
1393 .func
= event_enable_trigger_func
,
1394 .reg
= event_enable_register_trigger
,
1395 .unreg
= event_enable_unregister_trigger
,
1396 .get_trigger_ops
= event_enable_get_trigger_ops
,
1397 .set_filter
= set_trigger_filter
,
1400 static struct event_command trigger_disable_cmd
= {
1401 .name
= DISABLE_EVENT_STR
,
1402 .trigger_type
= ETT_EVENT_ENABLE
,
1403 .func
= event_enable_trigger_func
,
1404 .reg
= event_enable_register_trigger
,
1405 .unreg
= event_enable_unregister_trigger
,
1406 .get_trigger_ops
= event_enable_get_trigger_ops
,
1407 .set_filter
= set_trigger_filter
,
1410 static __init
void unregister_trigger_enable_disable_cmds(void)
1412 unregister_event_command(&trigger_enable_cmd
);
1413 unregister_event_command(&trigger_disable_cmd
);
1416 static __init
int register_trigger_enable_disable_cmds(void)
1420 ret
= register_event_command(&trigger_enable_cmd
);
1421 if (WARN_ON(ret
< 0))
1423 ret
= register_event_command(&trigger_disable_cmd
);
1424 if (WARN_ON(ret
< 0))
1425 unregister_trigger_enable_disable_cmds();
1430 static __init
int register_trigger_traceon_traceoff_cmds(void)
1434 ret
= register_event_command(&trigger_traceon_cmd
);
1435 if (WARN_ON(ret
< 0))
1437 ret
= register_event_command(&trigger_traceoff_cmd
);
1438 if (WARN_ON(ret
< 0))
1439 unregister_trigger_traceon_traceoff_cmds();
1444 __init
int register_trigger_cmds(void)
1446 register_trigger_traceon_traceoff_cmds();
1447 register_trigger_snapshot_cmd();
1448 register_trigger_stacktrace_cmd();
1449 register_trigger_enable_disable_cmds();