4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #define pr_fmt(fmt) fmt
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/debugfs.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
23 #include <asm/setup.h>
25 #include "trace_output.h"
28 #define TRACE_SYSTEM "TRACE_SYSTEM"
30 DEFINE_MUTEX(event_mutex
);
32 LIST_HEAD(ftrace_events
);
33 static LIST_HEAD(ftrace_common_fields
);
35 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37 static struct kmem_cache
*field_cachep
;
38 static struct kmem_cache
*file_cachep
;
40 #define SYSTEM_FL_FREE_NAME (1 << 31)
42 static inline int system_refcount(struct event_subsystem
*system
)
44 return system
->ref_count
& ~SYSTEM_FL_FREE_NAME
;
47 static int system_refcount_inc(struct event_subsystem
*system
)
49 return (system
->ref_count
++) & ~SYSTEM_FL_FREE_NAME
;
52 static int system_refcount_dec(struct event_subsystem
*system
)
54 return (--system
->ref_count
) & ~SYSTEM_FL_FREE_NAME
;
57 /* Double loops, do not use break, only goto's work */
58 #define do_for_each_event_file(tr, file) \
59 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
60 list_for_each_entry(file, &tr->events, list)
62 #define do_for_each_event_file_safe(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 struct ftrace_event_file *___n; \
65 list_for_each_entry_safe(file, ___n, &tr->events, list)
67 #define while_for_each_event_file() \
70 static struct list_head
*
71 trace_get_fields(struct ftrace_event_call
*event_call
)
73 if (!event_call
->class->get_fields
)
74 return &event_call
->class->fields
;
75 return event_call
->class->get_fields(event_call
);
78 static struct ftrace_event_field
*
79 __find_event_field(struct list_head
*head
, char *name
)
81 struct ftrace_event_field
*field
;
83 list_for_each_entry(field
, head
, link
) {
84 if (!strcmp(field
->name
, name
))
91 struct ftrace_event_field
*
92 trace_find_event_field(struct ftrace_event_call
*call
, char *name
)
94 struct ftrace_event_field
*field
;
95 struct list_head
*head
;
97 field
= __find_event_field(&ftrace_common_fields
, name
);
101 head
= trace_get_fields(call
);
102 return __find_event_field(head
, name
);
105 static int __trace_define_field(struct list_head
*head
, const char *type
,
106 const char *name
, int offset
, int size
,
107 int is_signed
, int filter_type
)
109 struct ftrace_event_field
*field
;
111 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
118 if (filter_type
== FILTER_OTHER
)
119 field
->filter_type
= filter_assign_type(type
);
121 field
->filter_type
= filter_type
;
123 field
->offset
= offset
;
125 field
->is_signed
= is_signed
;
127 list_add(&field
->link
, head
);
132 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
133 const char *name
, int offset
, int size
, int is_signed
,
136 struct list_head
*head
;
138 if (WARN_ON(!call
->class))
141 head
= trace_get_fields(call
);
142 return __trace_define_field(head
, type
, name
, offset
, size
,
143 is_signed
, filter_type
);
145 EXPORT_SYMBOL_GPL(trace_define_field
);
147 #define __common_field(type, item) \
148 ret = __trace_define_field(&ftrace_common_fields, #type, \
150 offsetof(typeof(ent), item), \
152 is_signed_type(type), FILTER_OTHER); \
156 static int trace_define_common_fields(void)
159 struct trace_entry ent
;
161 __common_field(unsigned short, type
);
162 __common_field(unsigned char, flags
);
163 __common_field(unsigned char, preempt_count
);
164 __common_field(int, pid
);
169 static void trace_destroy_fields(struct ftrace_event_call
*call
)
171 struct ftrace_event_field
*field
, *next
;
172 struct list_head
*head
;
174 head
= trace_get_fields(call
);
175 list_for_each_entry_safe(field
, next
, head
, link
) {
176 list_del(&field
->link
);
177 kmem_cache_free(field_cachep
, field
);
181 int trace_event_raw_init(struct ftrace_event_call
*call
)
185 id
= register_ftrace_event(&call
->event
);
191 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
193 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer
*fbuffer
,
194 struct ftrace_event_file
*ftrace_file
,
197 struct ftrace_event_call
*event_call
= ftrace_file
->event_call
;
199 local_save_flags(fbuffer
->flags
);
200 fbuffer
->pc
= preempt_count();
201 fbuffer
->ftrace_file
= ftrace_file
;
204 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, ftrace_file
,
205 event_call
->event
.type
, len
,
206 fbuffer
->flags
, fbuffer
->pc
);
210 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
211 return fbuffer
->entry
;
213 EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve
);
215 static DEFINE_SPINLOCK(tracepoint_iter_lock
);
217 static void output_printk(struct ftrace_event_buffer
*fbuffer
)
219 struct ftrace_event_call
*event_call
;
220 struct trace_event
*event
;
222 struct trace_iterator
*iter
= tracepoint_print_iter
;
227 event_call
= fbuffer
->ftrace_file
->event_call
;
228 if (!event_call
|| !event_call
->event
.funcs
||
229 !event_call
->event
.funcs
->trace
)
232 event
= &fbuffer
->ftrace_file
->event_call
->event
;
234 spin_lock_irqsave(&tracepoint_iter_lock
, flags
);
235 trace_seq_init(&iter
->seq
);
236 iter
->ent
= fbuffer
->entry
;
237 event_call
->event
.funcs
->trace(iter
, 0, event
);
238 trace_seq_putc(&iter
->seq
, 0);
239 printk("%s", iter
->seq
.buffer
);
241 spin_unlock_irqrestore(&tracepoint_iter_lock
, flags
);
244 void ftrace_event_buffer_commit(struct ftrace_event_buffer
*fbuffer
)
246 if (tracepoint_printk
)
247 output_printk(fbuffer
);
249 event_trigger_unlock_commit(fbuffer
->ftrace_file
, fbuffer
->buffer
,
250 fbuffer
->event
, fbuffer
->entry
,
251 fbuffer
->flags
, fbuffer
->pc
);
253 EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit
);
255 int ftrace_event_reg(struct ftrace_event_call
*call
,
256 enum trace_reg type
, void *data
)
258 struct ftrace_event_file
*file
= data
;
260 WARN_ON(!(call
->flags
& TRACE_EVENT_FL_TRACEPOINT
));
262 case TRACE_REG_REGISTER
:
263 return tracepoint_probe_register(call
->tp
,
266 case TRACE_REG_UNREGISTER
:
267 tracepoint_probe_unregister(call
->tp
,
272 #ifdef CONFIG_PERF_EVENTS
273 case TRACE_REG_PERF_REGISTER
:
274 return tracepoint_probe_register(call
->tp
,
275 call
->class->perf_probe
,
277 case TRACE_REG_PERF_UNREGISTER
:
278 tracepoint_probe_unregister(call
->tp
,
279 call
->class->perf_probe
,
282 case TRACE_REG_PERF_OPEN
:
283 case TRACE_REG_PERF_CLOSE
:
284 case TRACE_REG_PERF_ADD
:
285 case TRACE_REG_PERF_DEL
:
291 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
293 void trace_event_enable_cmd_record(bool enable
)
295 struct ftrace_event_file
*file
;
296 struct trace_array
*tr
;
298 mutex_lock(&event_mutex
);
299 do_for_each_event_file(tr
, file
) {
301 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
305 tracing_start_cmdline_record();
306 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
308 tracing_stop_cmdline_record();
309 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
311 } while_for_each_event_file();
312 mutex_unlock(&event_mutex
);
315 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
316 int enable
, int soft_disable
)
318 struct ftrace_event_call
*call
= file
->event_call
;
325 * When soft_disable is set and enable is cleared, the sm_ref
326 * reference counter is decremented. If it reaches 0, we want
327 * to clear the SOFT_DISABLED flag but leave the event in the
328 * state that it was. That is, if the event was enabled and
329 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
330 * is set we do not want the event to be enabled before we
333 * When soft_disable is not set but the SOFT_MODE flag is,
334 * we do nothing. Do not disable the tracepoint, otherwise
335 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
338 if (atomic_dec_return(&file
->sm_ref
) > 0)
340 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
341 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
343 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
345 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
346 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
347 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
348 tracing_stop_cmdline_record();
349 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
351 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
353 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
354 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
355 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
357 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
361 * When soft_disable is set and enable is set, we want to
362 * register the tracepoint for the event, but leave the event
363 * as is. That means, if the event was already enabled, we do
364 * nothing (but set SOFT_MODE). If the event is disabled, we
365 * set SOFT_DISABLED before enabling the event tracepoint, so
366 * it still seems to be disabled.
369 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
371 if (atomic_inc_return(&file
->sm_ref
) > 1)
373 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
376 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
378 /* Keep the event disabled, when going to SOFT_MODE. */
380 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
382 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
383 tracing_start_cmdline_record();
384 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
386 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
388 tracing_stop_cmdline_record();
389 pr_info("event trace: Could not enable event "
390 "%s\n", ftrace_event_name(call
));
393 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
395 /* WAS_ENABLED gets set but never cleared. */
396 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
404 int trace_event_enable_disable(struct ftrace_event_file
*file
,
405 int enable
, int soft_disable
)
407 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
410 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
413 return __ftrace_event_enable_disable(file
, enable
, 0);
416 static void ftrace_clear_events(struct trace_array
*tr
)
418 struct ftrace_event_file
*file
;
420 mutex_lock(&event_mutex
);
421 list_for_each_entry(file
, &tr
->events
, list
) {
422 ftrace_event_enable_disable(file
, 0);
424 mutex_unlock(&event_mutex
);
427 static void __put_system(struct event_subsystem
*system
)
429 struct event_filter
*filter
= system
->filter
;
431 WARN_ON_ONCE(system_refcount(system
) == 0);
432 if (system_refcount_dec(system
))
435 list_del(&system
->list
);
438 kfree(filter
->filter_string
);
441 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
446 static void __get_system(struct event_subsystem
*system
)
448 WARN_ON_ONCE(system_refcount(system
) == 0);
449 system_refcount_inc(system
);
452 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
454 WARN_ON_ONCE(dir
->ref_count
== 0);
456 __get_system(dir
->subsystem
);
459 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
461 WARN_ON_ONCE(dir
->ref_count
== 0);
462 /* If the subsystem is about to be freed, the dir must be too */
463 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
465 __put_system(dir
->subsystem
);
466 if (!--dir
->ref_count
)
470 static void put_system(struct ftrace_subsystem_dir
*dir
)
472 mutex_lock(&event_mutex
);
473 __put_system_dir(dir
);
474 mutex_unlock(&event_mutex
);
477 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
482 if (!--dir
->nr_events
) {
483 debugfs_remove_recursive(dir
->entry
);
484 list_del(&dir
->list
);
485 __put_system_dir(dir
);
489 static void remove_event_file_dir(struct ftrace_event_file
*file
)
491 struct dentry
*dir
= file
->dir
;
492 struct dentry
*child
;
495 spin_lock(&dir
->d_lock
); /* probably unneeded */
496 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
497 if (child
->d_inode
) /* probably unneeded */
498 child
->d_inode
->i_private
= NULL
;
500 spin_unlock(&dir
->d_lock
);
502 debugfs_remove_recursive(dir
);
505 list_del(&file
->list
);
506 remove_subsystem(file
->system
);
507 free_event_filter(file
->filter
);
508 kmem_cache_free(file_cachep
, file
);
512 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
515 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
516 const char *sub
, const char *event
, int set
)
518 struct ftrace_event_file
*file
;
519 struct ftrace_event_call
*call
;
523 list_for_each_entry(file
, &tr
->events
, list
) {
525 call
= file
->event_call
;
526 name
= ftrace_event_name(call
);
528 if (!name
|| !call
->class || !call
->class->reg
)
531 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
535 strcmp(match
, name
) != 0 &&
536 strcmp(match
, call
->class->system
) != 0)
539 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
542 if (event
&& strcmp(event
, name
) != 0)
545 ftrace_event_enable_disable(file
, set
);
553 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
554 const char *sub
, const char *event
, int set
)
558 mutex_lock(&event_mutex
);
559 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
560 mutex_unlock(&event_mutex
);
565 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
567 char *event
= NULL
, *sub
= NULL
, *match
;
570 * The buf format can be <subsystem>:<event-name>
571 * *:<event-name> means any event by that name.
572 * :<event-name> is the same.
574 * <subsystem>:* means all events in that subsystem
575 * <subsystem>: means the same.
577 * <name> (no ':') means all events in a subsystem with
578 * the name <name> or any event that matches <name>
581 match
= strsep(&buf
, ":");
587 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
589 if (!strlen(event
) || strcmp(event
, "*") == 0)
593 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
597 * trace_set_clr_event - enable or disable an event
598 * @system: system name to match (NULL for any system)
599 * @event: event name to match (NULL for all events, within system)
600 * @set: 1 to enable, 0 to disable
602 * This is a way for other parts of the kernel to enable or disable
605 * Returns 0 on success, -EINVAL if the parameters do not match any
608 int trace_set_clr_event(const char *system
, const char *event
, int set
)
610 struct trace_array
*tr
= top_trace_array();
615 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
617 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
619 /* 128 should be much more than enough */
620 #define EVENT_BUF_SIZE 127
623 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
624 size_t cnt
, loff_t
*ppos
)
626 struct trace_parser parser
;
627 struct seq_file
*m
= file
->private_data
;
628 struct trace_array
*tr
= m
->private;
634 ret
= tracing_update_buffers();
638 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
641 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
643 if (read
>= 0 && trace_parser_loaded((&parser
))) {
646 if (*parser
.buffer
== '!')
649 parser
.buffer
[parser
.idx
] = 0;
651 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
659 trace_parser_put(&parser
);
665 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
667 struct ftrace_event_file
*file
= v
;
668 struct ftrace_event_call
*call
;
669 struct trace_array
*tr
= m
->private;
673 list_for_each_entry_continue(file
, &tr
->events
, list
) {
674 call
= file
->event_call
;
676 * The ftrace subsystem is for showing formats only.
677 * They can not be enabled or disabled via the event files.
679 if (call
->class && call
->class->reg
)
686 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
688 struct ftrace_event_file
*file
;
689 struct trace_array
*tr
= m
->private;
692 mutex_lock(&event_mutex
);
694 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
695 for (l
= 0; l
<= *pos
; ) {
696 file
= t_next(m
, file
, &l
);
704 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
706 struct ftrace_event_file
*file
= v
;
707 struct trace_array
*tr
= m
->private;
711 list_for_each_entry_continue(file
, &tr
->events
, list
) {
712 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
719 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
721 struct ftrace_event_file
*file
;
722 struct trace_array
*tr
= m
->private;
725 mutex_lock(&event_mutex
);
727 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
728 for (l
= 0; l
<= *pos
; ) {
729 file
= s_next(m
, file
, &l
);
736 static int t_show(struct seq_file
*m
, void *v
)
738 struct ftrace_event_file
*file
= v
;
739 struct ftrace_event_call
*call
= file
->event_call
;
741 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
742 seq_printf(m
, "%s:", call
->class->system
);
743 seq_printf(m
, "%s\n", ftrace_event_name(call
));
748 static void t_stop(struct seq_file
*m
, void *p
)
750 mutex_unlock(&event_mutex
);
754 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
757 struct ftrace_event_file
*file
;
761 mutex_lock(&event_mutex
);
762 file
= event_file_data(filp
);
765 mutex_unlock(&event_mutex
);
770 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
771 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
774 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
775 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
780 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
784 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
787 struct ftrace_event_file
*file
;
791 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
795 ret
= tracing_update_buffers();
803 mutex_lock(&event_mutex
);
804 file
= event_file_data(filp
);
806 ret
= ftrace_event_enable_disable(file
, val
);
807 mutex_unlock(&event_mutex
);
816 return ret
? ret
: cnt
;
820 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
823 const char set_to_char
[4] = { '?', '0', '1', 'X' };
824 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
825 struct event_subsystem
*system
= dir
->subsystem
;
826 struct ftrace_event_call
*call
;
827 struct ftrace_event_file
*file
;
828 struct trace_array
*tr
= dir
->tr
;
833 mutex_lock(&event_mutex
);
834 list_for_each_entry(file
, &tr
->events
, list
) {
835 call
= file
->event_call
;
836 if (!ftrace_event_name(call
) || !call
->class || !call
->class->reg
)
839 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
843 * We need to find out if all the events are set
844 * or if all events or cleared, or if we have
847 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
850 * If we have a mixture, no need to look further.
855 mutex_unlock(&event_mutex
);
857 buf
[0] = set_to_char
[set
];
860 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
866 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
869 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
870 struct event_subsystem
*system
= dir
->subsystem
;
871 const char *name
= NULL
;
875 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
879 ret
= tracing_update_buffers();
883 if (val
!= 0 && val
!= 1)
887 * Opening of "enable" adds a ref count to system,
888 * so the name is safe to use.
893 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
907 FORMAT_FIELD_SEPERATOR
= 2,
911 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
913 struct ftrace_event_call
*call
= event_file_data(m
->private);
914 struct list_head
*common_head
= &ftrace_common_fields
;
915 struct list_head
*head
= trace_get_fields(call
);
916 struct list_head
*node
= v
;
920 switch ((unsigned long)v
) {
925 case FORMAT_FIELD_SEPERATOR
:
929 case FORMAT_PRINTFMT
:
935 if (node
== common_head
)
936 return (void *)FORMAT_FIELD_SEPERATOR
;
937 else if (node
== head
)
938 return (void *)FORMAT_PRINTFMT
;
943 static int f_show(struct seq_file
*m
, void *v
)
945 struct ftrace_event_call
*call
= event_file_data(m
->private);
946 struct ftrace_event_field
*field
;
947 const char *array_descriptor
;
949 switch ((unsigned long)v
) {
951 seq_printf(m
, "name: %s\n", ftrace_event_name(call
));
952 seq_printf(m
, "ID: %d\n", call
->event
.type
);
953 seq_puts(m
, "format:\n");
956 case FORMAT_FIELD_SEPERATOR
:
960 case FORMAT_PRINTFMT
:
961 seq_printf(m
, "\nprint fmt: %s\n",
966 field
= list_entry(v
, struct ftrace_event_field
, link
);
968 * Smartly shows the array type(except dynamic array).
971 * If TYPE := TYPE[LEN], it is shown:
972 * field:TYPE VAR[LEN]
974 array_descriptor
= strchr(field
->type
, '[');
976 if (!strncmp(field
->type
, "__data_loc", 10))
977 array_descriptor
= NULL
;
979 if (!array_descriptor
)
980 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
981 field
->type
, field
->name
, field
->offset
,
982 field
->size
, !!field
->is_signed
);
984 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
985 (int)(array_descriptor
- field
->type
),
986 field
->type
, field
->name
,
987 array_descriptor
, field
->offset
,
988 field
->size
, !!field
->is_signed
);
993 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
995 void *p
= (void *)FORMAT_HEADER
;
998 /* ->stop() is called even if ->start() fails */
999 mutex_lock(&event_mutex
);
1000 if (!event_file_data(m
->private))
1001 return ERR_PTR(-ENODEV
);
1003 while (l
< *pos
&& p
)
1004 p
= f_next(m
, p
, &l
);
1009 static void f_stop(struct seq_file
*m
, void *p
)
1011 mutex_unlock(&event_mutex
);
1014 static const struct seq_operations trace_format_seq_ops
= {
1021 static int trace_format_open(struct inode
*inode
, struct file
*file
)
1026 ret
= seq_open(file
, &trace_format_seq_ops
);
1030 m
= file
->private_data
;
1037 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1039 int id
= (long)event_file_data(filp
);
1049 len
= sprintf(buf
, "%d\n", id
);
1051 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1055 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1058 struct ftrace_event_file
*file
;
1059 struct trace_seq
*s
;
1065 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1072 mutex_lock(&event_mutex
);
1073 file
= event_file_data(filp
);
1075 print_event_filter(file
, s
);
1076 mutex_unlock(&event_mutex
);
1079 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1080 s
->buffer
, trace_seq_used(s
));
1088 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1091 struct ftrace_event_file
*file
;
1095 if (cnt
>= PAGE_SIZE
)
1098 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1102 if (copy_from_user(buf
, ubuf
, cnt
)) {
1103 free_page((unsigned long) buf
);
1108 mutex_lock(&event_mutex
);
1109 file
= event_file_data(filp
);
1111 err
= apply_event_filter(file
, buf
);
1112 mutex_unlock(&event_mutex
);
1114 free_page((unsigned long) buf
);
1123 static LIST_HEAD(event_subsystems
);
1125 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1127 struct event_subsystem
*system
= NULL
;
1128 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1129 struct trace_array
*tr
;
1132 if (tracing_is_disabled())
1135 /* Make sure the system still exists */
1136 mutex_lock(&trace_types_lock
);
1137 mutex_lock(&event_mutex
);
1138 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1139 list_for_each_entry(dir
, &tr
->systems
, list
) {
1140 if (dir
== inode
->i_private
) {
1141 /* Don't open systems with no events */
1142 if (dir
->nr_events
) {
1143 __get_system_dir(dir
);
1144 system
= dir
->subsystem
;
1151 mutex_unlock(&event_mutex
);
1152 mutex_unlock(&trace_types_lock
);
1157 /* Some versions of gcc think dir can be uninitialized here */
1160 /* Still need to increment the ref count of the system */
1161 if (trace_array_get(tr
) < 0) {
1166 ret
= tracing_open_generic(inode
, filp
);
1168 trace_array_put(tr
);
1175 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1177 struct ftrace_subsystem_dir
*dir
;
1178 struct trace_array
*tr
= inode
->i_private
;
1181 if (tracing_is_disabled())
1184 if (trace_array_get(tr
) < 0)
1187 /* Make a temporary dir that has no system but points to tr */
1188 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1190 trace_array_put(tr
);
1196 ret
= tracing_open_generic(inode
, filp
);
1198 trace_array_put(tr
);
1203 filp
->private_data
= dir
;
1208 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1210 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1212 trace_array_put(dir
->tr
);
1215 * If dir->subsystem is NULL, then this is a temporary
1216 * descriptor that was made for a trace_array to enable
1228 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1231 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1232 struct event_subsystem
*system
= dir
->subsystem
;
1233 struct trace_seq
*s
;
1239 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1245 print_subsystem_event_filter(system
, s
);
1246 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1247 s
->buffer
, trace_seq_used(s
));
1255 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1258 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1262 if (cnt
>= PAGE_SIZE
)
1265 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1269 if (copy_from_user(buf
, ubuf
, cnt
)) {
1270 free_page((unsigned long) buf
);
1275 err
= apply_subsystem_event_filter(dir
, buf
);
1276 free_page((unsigned long) buf
);
1286 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1288 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1289 struct trace_seq
*s
;
1295 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1302 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1303 s
->buffer
, trace_seq_used(s
));
1310 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1311 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1312 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1314 static const struct seq_operations show_event_seq_ops
= {
1321 static const struct seq_operations show_set_event_seq_ops
= {
1328 static const struct file_operations ftrace_avail_fops
= {
1329 .open
= ftrace_event_avail_open
,
1331 .llseek
= seq_lseek
,
1332 .release
= seq_release
,
1335 static const struct file_operations ftrace_set_event_fops
= {
1336 .open
= ftrace_event_set_open
,
1338 .write
= ftrace_event_write
,
1339 .llseek
= seq_lseek
,
1340 .release
= ftrace_event_release
,
1343 static const struct file_operations ftrace_enable_fops
= {
1344 .open
= tracing_open_generic
,
1345 .read
= event_enable_read
,
1346 .write
= event_enable_write
,
1347 .llseek
= default_llseek
,
1350 static const struct file_operations ftrace_event_format_fops
= {
1351 .open
= trace_format_open
,
1353 .llseek
= seq_lseek
,
1354 .release
= seq_release
,
1357 static const struct file_operations ftrace_event_id_fops
= {
1358 .read
= event_id_read
,
1359 .llseek
= default_llseek
,
1362 static const struct file_operations ftrace_event_filter_fops
= {
1363 .open
= tracing_open_generic
,
1364 .read
= event_filter_read
,
1365 .write
= event_filter_write
,
1366 .llseek
= default_llseek
,
1369 static const struct file_operations ftrace_subsystem_filter_fops
= {
1370 .open
= subsystem_open
,
1371 .read
= subsystem_filter_read
,
1372 .write
= subsystem_filter_write
,
1373 .llseek
= default_llseek
,
1374 .release
= subsystem_release
,
1377 static const struct file_operations ftrace_system_enable_fops
= {
1378 .open
= subsystem_open
,
1379 .read
= system_enable_read
,
1380 .write
= system_enable_write
,
1381 .llseek
= default_llseek
,
1382 .release
= subsystem_release
,
1385 static const struct file_operations ftrace_tr_enable_fops
= {
1386 .open
= system_tr_open
,
1387 .read
= system_enable_read
,
1388 .write
= system_enable_write
,
1389 .llseek
= default_llseek
,
1390 .release
= subsystem_release
,
1393 static const struct file_operations ftrace_show_header_fops
= {
1394 .open
= tracing_open_generic
,
1395 .read
= show_header
,
1396 .llseek
= default_llseek
,
1400 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1401 const struct seq_operations
*seq_ops
)
1406 ret
= seq_open(file
, seq_ops
);
1409 m
= file
->private_data
;
1410 /* copy tr over to seq ops */
1411 m
->private = inode
->i_private
;
1416 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1418 struct trace_array
*tr
= inode
->i_private
;
1420 trace_array_put(tr
);
1422 return seq_release(inode
, file
);
1426 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1428 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1430 return ftrace_event_open(inode
, file
, seq_ops
);
1434 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1436 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1437 struct trace_array
*tr
= inode
->i_private
;
1440 if (trace_array_get(tr
) < 0)
1443 if ((file
->f_mode
& FMODE_WRITE
) &&
1444 (file
->f_flags
& O_TRUNC
))
1445 ftrace_clear_events(tr
);
1447 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1449 trace_array_put(tr
);
1453 static struct event_subsystem
*
1454 create_new_subsystem(const char *name
)
1456 struct event_subsystem
*system
;
1458 /* need to create new entry */
1459 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1463 system
->ref_count
= 1;
1465 /* Only allocate if dynamic (kprobes and modules) */
1466 if (!core_kernel_data((unsigned long)name
)) {
1467 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1468 system
->name
= kstrdup(name
, GFP_KERNEL
);
1472 system
->name
= name
;
1474 system
->filter
= NULL
;
1476 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1477 if (!system
->filter
)
1480 list_add(&system
->list
, &event_subsystems
);
1485 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1486 kfree(system
->name
);
1491 static struct dentry
*
1492 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1493 struct ftrace_event_file
*file
, struct dentry
*parent
)
1495 struct ftrace_subsystem_dir
*dir
;
1496 struct event_subsystem
*system
;
1497 struct dentry
*entry
;
1499 /* First see if we did not already create this dir */
1500 list_for_each_entry(dir
, &tr
->systems
, list
) {
1501 system
= dir
->subsystem
;
1502 if (strcmp(system
->name
, name
) == 0) {
1509 /* Now see if the system itself exists. */
1510 list_for_each_entry(system
, &event_subsystems
, list
) {
1511 if (strcmp(system
->name
, name
) == 0)
1514 /* Reset system variable when not found */
1515 if (&system
->list
== &event_subsystems
)
1518 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1523 system
= create_new_subsystem(name
);
1527 __get_system(system
);
1529 dir
->entry
= debugfs_create_dir(name
, parent
);
1531 pr_warn("Failed to create system directory %s\n", name
);
1532 __put_system(system
);
1539 dir
->subsystem
= system
;
1542 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1543 &ftrace_subsystem_filter_fops
);
1545 kfree(system
->filter
);
1546 system
->filter
= NULL
;
1547 pr_warn("Could not create debugfs '%s/filter' entry\n", name
);
1550 trace_create_file("enable", 0644, dir
->entry
, dir
,
1551 &ftrace_system_enable_fops
);
1553 list_add(&dir
->list
, &tr
->systems
);
1560 /* Only print this message if failed on memory allocation */
1561 if (!dir
|| !system
)
1562 pr_warn("No memory to create event subsystem %s\n", name
);
1567 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1569 struct ftrace_event_call
*call
= file
->event_call
;
1570 struct trace_array
*tr
= file
->tr
;
1571 struct list_head
*head
;
1572 struct dentry
*d_events
;
1577 * If the trace point header did not define TRACE_SYSTEM
1578 * then the system would be called "TRACE_SYSTEM".
1580 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1581 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1587 name
= ftrace_event_name(call
);
1588 file
->dir
= debugfs_create_dir(name
, d_events
);
1590 pr_warn("Could not create debugfs '%s' directory\n", name
);
1594 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1595 trace_create_file("enable", 0644, file
->dir
, file
,
1596 &ftrace_enable_fops
);
1598 #ifdef CONFIG_PERF_EVENTS
1599 if (call
->event
.type
&& call
->class->reg
)
1600 trace_create_file("id", 0444, file
->dir
,
1601 (void *)(long)call
->event
.type
,
1602 &ftrace_event_id_fops
);
1606 * Other events may have the same class. Only update
1607 * the fields if they are not already defined.
1609 head
= trace_get_fields(call
);
1610 if (list_empty(head
)) {
1611 ret
= call
->class->define_fields(call
);
1613 pr_warn("Could not initialize trace point events/%s\n",
1618 trace_create_file("filter", 0644, file
->dir
, file
,
1619 &ftrace_event_filter_fops
);
1621 trace_create_file("trigger", 0644, file
->dir
, file
,
1622 &event_trigger_fops
);
1624 trace_create_file("format", 0444, file
->dir
, call
,
1625 &ftrace_event_format_fops
);
1630 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1632 struct ftrace_event_file
*file
;
1633 struct trace_array
*tr
;
1635 do_for_each_event_file_safe(tr
, file
) {
1636 if (file
->event_call
!= call
)
1639 remove_event_file_dir(file
);
1641 * The do_for_each_event_file_safe() is
1642 * a double loop. After finding the call for this
1643 * trace_array, we use break to jump to the next
1647 } while_for_each_event_file();
1650 static void event_remove(struct ftrace_event_call
*call
)
1652 struct trace_array
*tr
;
1653 struct ftrace_event_file
*file
;
1655 do_for_each_event_file(tr
, file
) {
1656 if (file
->event_call
!= call
)
1658 ftrace_event_enable_disable(file
, 0);
1660 * The do_for_each_event_file() is
1661 * a double loop. After finding the call for this
1662 * trace_array, we use break to jump to the next
1666 } while_for_each_event_file();
1668 if (call
->event
.funcs
)
1669 __unregister_ftrace_event(&call
->event
);
1670 remove_event_from_tracers(call
);
1671 list_del(&call
->list
);
1674 static int event_init(struct ftrace_event_call
*call
)
1679 name
= ftrace_event_name(call
);
1683 if (call
->class->raw_init
) {
1684 ret
= call
->class->raw_init(call
);
1685 if (ret
< 0 && ret
!= -ENOSYS
)
1686 pr_warn("Could not initialize trace events/%s\n", name
);
1693 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1697 ret
= event_init(call
);
1701 list_add(&call
->list
, &ftrace_events
);
1707 static struct ftrace_event_file
*
1708 trace_create_new_event(struct ftrace_event_call
*call
,
1709 struct trace_array
*tr
)
1711 struct ftrace_event_file
*file
;
1713 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1717 file
->event_call
= call
;
1719 atomic_set(&file
->sm_ref
, 0);
1720 atomic_set(&file
->tm_ref
, 0);
1721 INIT_LIST_HEAD(&file
->triggers
);
1722 list_add(&file
->list
, &tr
->events
);
1727 /* Add an event to a trace directory */
1729 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1731 struct ftrace_event_file
*file
;
1733 file
= trace_create_new_event(call
, tr
);
1737 return event_create_dir(tr
->event_dir
, file
);
1741 * Just create a decriptor for early init. A descriptor is required
1742 * for enabling events at boot. We want to enable events before
1743 * the filesystem is initialized.
1746 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1747 struct trace_array
*tr
)
1749 struct ftrace_event_file
*file
;
1751 file
= trace_create_new_event(call
, tr
);
1758 struct ftrace_module_file_ops
;
1759 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1761 /* Add an additional event_call dynamically */
1762 int trace_add_event_call(struct ftrace_event_call
*call
)
1765 mutex_lock(&trace_types_lock
);
1766 mutex_lock(&event_mutex
);
1768 ret
= __register_event(call
, NULL
);
1770 __add_event_to_tracers(call
);
1772 mutex_unlock(&event_mutex
);
1773 mutex_unlock(&trace_types_lock
);
1778 * Must be called under locking of trace_types_lock, event_mutex and
1781 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1784 trace_destroy_fields(call
);
1785 free_event_filter(call
->filter
);
1786 call
->filter
= NULL
;
1789 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1791 struct trace_array
*tr
;
1792 struct ftrace_event_file
*file
;
1794 #ifdef CONFIG_PERF_EVENTS
1795 if (call
->perf_refcount
)
1798 do_for_each_event_file(tr
, file
) {
1799 if (file
->event_call
!= call
)
1802 * We can't rely on ftrace_event_enable_disable(enable => 0)
1803 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1804 * TRACE_REG_UNREGISTER.
1806 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1809 * The do_for_each_event_file_safe() is
1810 * a double loop. After finding the call for this
1811 * trace_array, we use break to jump to the next
1815 } while_for_each_event_file();
1817 __trace_remove_event_call(call
);
1822 /* Remove an event_call */
1823 int trace_remove_event_call(struct ftrace_event_call
*call
)
1827 mutex_lock(&trace_types_lock
);
1828 mutex_lock(&event_mutex
);
1829 down_write(&trace_event_sem
);
1830 ret
= probe_remove_event_call(call
);
1831 up_write(&trace_event_sem
);
1832 mutex_unlock(&event_mutex
);
1833 mutex_unlock(&trace_types_lock
);
1838 #define for_each_event(event, start, end) \
1839 for (event = start; \
1840 (unsigned long)event < (unsigned long)end; \
1843 #ifdef CONFIG_MODULES
1845 static void trace_module_add_events(struct module
*mod
)
1847 struct ftrace_event_call
**call
, **start
, **end
;
1849 if (!mod
->num_trace_events
)
1852 /* Don't add infrastructure for mods without tracepoints */
1853 if (trace_module_has_bad_taint(mod
)) {
1854 pr_err("%s: module has bad taint, not creating trace events\n",
1859 start
= mod
->trace_events
;
1860 end
= mod
->trace_events
+ mod
->num_trace_events
;
1862 for_each_event(call
, start
, end
) {
1863 __register_event(*call
, mod
);
1864 __add_event_to_tracers(*call
);
1868 static void trace_module_remove_events(struct module
*mod
)
1870 struct ftrace_event_call
*call
, *p
;
1871 bool clear_trace
= false;
1873 down_write(&trace_event_sem
);
1874 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1875 if (call
->mod
== mod
) {
1876 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1878 __trace_remove_event_call(call
);
1881 up_write(&trace_event_sem
);
1884 * It is safest to reset the ring buffer if the module being unloaded
1885 * registered any events that were used. The only worry is if
1886 * a new module gets loaded, and takes on the same id as the events
1887 * of this module. When printing out the buffer, traced events left
1888 * over from this module may be passed to the new module events and
1889 * unexpected results may occur.
1892 tracing_reset_all_online_cpus();
1895 static int trace_module_notify(struct notifier_block
*self
,
1896 unsigned long val
, void *data
)
1898 struct module
*mod
= data
;
1900 mutex_lock(&trace_types_lock
);
1901 mutex_lock(&event_mutex
);
1903 case MODULE_STATE_COMING
:
1904 trace_module_add_events(mod
);
1906 case MODULE_STATE_GOING
:
1907 trace_module_remove_events(mod
);
1910 mutex_unlock(&event_mutex
);
1911 mutex_unlock(&trace_types_lock
);
1916 static struct notifier_block trace_module_nb
= {
1917 .notifier_call
= trace_module_notify
,
1920 #endif /* CONFIG_MODULES */
1922 /* Create a new event directory structure for a trace directory. */
1924 __trace_add_event_dirs(struct trace_array
*tr
)
1926 struct ftrace_event_call
*call
;
1929 list_for_each_entry(call
, &ftrace_events
, list
) {
1930 ret
= __trace_add_new_event(call
, tr
);
1932 pr_warn("Could not create directory for event %s\n",
1933 ftrace_event_name(call
));
1937 struct ftrace_event_file
*
1938 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1940 struct ftrace_event_file
*file
;
1941 struct ftrace_event_call
*call
;
1944 list_for_each_entry(file
, &tr
->events
, list
) {
1946 call
= file
->event_call
;
1947 name
= ftrace_event_name(call
);
1949 if (!name
|| !call
->class || !call
->class->reg
)
1952 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1955 if (strcmp(event
, name
) == 0 &&
1956 strcmp(system
, call
->class->system
) == 0)
1962 #ifdef CONFIG_DYNAMIC_FTRACE
1965 #define ENABLE_EVENT_STR "enable_event"
1966 #define DISABLE_EVENT_STR "disable_event"
1968 struct event_probe_data
{
1969 struct ftrace_event_file
*file
;
1970 unsigned long count
;
1976 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1978 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1979 struct event_probe_data
*data
= *pdata
;
1985 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1987 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1991 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1993 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1994 struct event_probe_data
*data
= *pdata
;
2002 /* Skip if the event is in a state we want to switch to */
2003 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
2006 if (data
->count
!= -1)
2009 event_enable_probe(ip
, parent_ip
, _data
);
2013 event_enable_print(struct seq_file
*m
, unsigned long ip
,
2014 struct ftrace_probe_ops
*ops
, void *_data
)
2016 struct event_probe_data
*data
= _data
;
2018 seq_printf(m
, "%ps:", (void *)ip
);
2020 seq_printf(m
, "%s:%s:%s",
2021 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
2022 data
->file
->event_call
->class->system
,
2023 ftrace_event_name(data
->file
->event_call
));
2025 if (data
->count
== -1)
2026 seq_puts(m
, ":unlimited\n");
2028 seq_printf(m
, ":count=%ld\n", data
->count
);
2034 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2037 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2038 struct event_probe_data
*data
= *pdata
;
2045 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2048 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2049 struct event_probe_data
*data
= *pdata
;
2051 if (WARN_ON_ONCE(data
->ref
<= 0))
2056 /* Remove the SOFT_MODE flag */
2057 __ftrace_event_enable_disable(data
->file
, 0, 1);
2058 module_put(data
->file
->event_call
->mod
);
2064 static struct ftrace_probe_ops event_enable_probe_ops
= {
2065 .func
= event_enable_probe
,
2066 .print
= event_enable_print
,
2067 .init
= event_enable_init
,
2068 .free
= event_enable_free
,
2071 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2072 .func
= event_enable_count_probe
,
2073 .print
= event_enable_print
,
2074 .init
= event_enable_init
,
2075 .free
= event_enable_free
,
2078 static struct ftrace_probe_ops event_disable_probe_ops
= {
2079 .func
= event_enable_probe
,
2080 .print
= event_enable_print
,
2081 .init
= event_enable_init
,
2082 .free
= event_enable_free
,
2085 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2086 .func
= event_enable_count_probe
,
2087 .print
= event_enable_print
,
2088 .init
= event_enable_init
,
2089 .free
= event_enable_free
,
2093 event_enable_func(struct ftrace_hash
*hash
,
2094 char *glob
, char *cmd
, char *param
, int enabled
)
2096 struct trace_array
*tr
= top_trace_array();
2097 struct ftrace_event_file
*file
;
2098 struct ftrace_probe_ops
*ops
;
2099 struct event_probe_data
*data
;
2109 /* hash funcs only work with set_ftrace_filter */
2110 if (!enabled
|| !param
)
2113 system
= strsep(¶m
, ":");
2117 event
= strsep(¶m
, ":");
2119 mutex_lock(&event_mutex
);
2122 file
= find_event_file(tr
, system
, event
);
2126 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2129 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2131 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2133 if (glob
[0] == '!') {
2134 unregister_ftrace_function_probe_func(glob
+1, ops
);
2140 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2144 data
->enable
= enable
;
2151 number
= strsep(¶m
, ":");
2154 if (!strlen(number
))
2158 * We use the callback data field (which is a pointer)
2161 ret
= kstrtoul(number
, 0, &data
->count
);
2166 /* Don't let event modules unload while probe registered */
2167 ret
= try_module_get(file
->event_call
->mod
);
2173 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2176 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2178 * The above returns on success the # of functions enabled,
2179 * but if it didn't find any functions it returns zero.
2180 * Consider no functions a failure too.
2187 /* Just return zero, not the number of enabled functions */
2190 mutex_unlock(&event_mutex
);
2194 __ftrace_event_enable_disable(file
, 0, 1);
2196 module_put(file
->event_call
->mod
);
2202 static struct ftrace_func_command event_enable_cmd
= {
2203 .name
= ENABLE_EVENT_STR
,
2204 .func
= event_enable_func
,
2207 static struct ftrace_func_command event_disable_cmd
= {
2208 .name
= DISABLE_EVENT_STR
,
2209 .func
= event_enable_func
,
2212 static __init
int register_event_cmds(void)
2216 ret
= register_ftrace_command(&event_enable_cmd
);
2217 if (WARN_ON(ret
< 0))
2219 ret
= register_ftrace_command(&event_disable_cmd
);
2220 if (WARN_ON(ret
< 0))
2221 unregister_ftrace_command(&event_enable_cmd
);
2225 static inline int register_event_cmds(void) { return 0; }
2226 #endif /* CONFIG_DYNAMIC_FTRACE */
2229 * The top level array has already had its ftrace_event_file
2230 * descriptors created in order to allow for early events to
2231 * be recorded. This function is called after the debugfs has been
2232 * initialized, and we now have to create the files associated
2236 __trace_early_add_event_dirs(struct trace_array
*tr
)
2238 struct ftrace_event_file
*file
;
2242 list_for_each_entry(file
, &tr
->events
, list
) {
2243 ret
= event_create_dir(tr
->event_dir
, file
);
2245 pr_warn("Could not create directory for event %s\n",
2246 ftrace_event_name(file
->event_call
));
2251 * For early boot up, the top trace array requires to have
2252 * a list of events that can be enabled. This must be done before
2253 * the filesystem is set up in order to allow events to be traced
2257 __trace_early_add_events(struct trace_array
*tr
)
2259 struct ftrace_event_call
*call
;
2262 list_for_each_entry(call
, &ftrace_events
, list
) {
2263 /* Early boot up should not have any modules loaded */
2264 if (WARN_ON_ONCE(call
->mod
))
2267 ret
= __trace_early_add_new_event(call
, tr
);
2269 pr_warn("Could not create early event %s\n",
2270 ftrace_event_name(call
));
2274 /* Remove the event directory structure for a trace directory. */
2276 __trace_remove_event_dirs(struct trace_array
*tr
)
2278 struct ftrace_event_file
*file
, *next
;
2280 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2281 remove_event_file_dir(file
);
2284 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2286 struct trace_array
*tr
;
2288 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2289 __trace_add_new_event(call
, tr
);
2292 extern struct ftrace_event_call
*__start_ftrace_events
[];
2293 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2295 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2297 static __init
int setup_trace_event(char *str
)
2299 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2300 ring_buffer_expanded
= true;
2301 tracing_selftest_disabled
= true;
2305 __setup("trace_event=", setup_trace_event
);
2307 /* Expects to have event_mutex held when called */
2309 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2311 struct dentry
*d_events
;
2312 struct dentry
*entry
;
2314 entry
= debugfs_create_file("set_event", 0644, parent
,
2315 tr
, &ftrace_set_event_fops
);
2317 pr_warn("Could not create debugfs 'set_event' entry\n");
2321 d_events
= debugfs_create_dir("events", parent
);
2323 pr_warn("Could not create debugfs 'events' directory\n");
2327 /* ring buffer internal formats */
2328 trace_create_file("header_page", 0444, d_events
,
2329 ring_buffer_print_page_header
,
2330 &ftrace_show_header_fops
);
2332 trace_create_file("header_event", 0444, d_events
,
2333 ring_buffer_print_entry_header
,
2334 &ftrace_show_header_fops
);
2336 trace_create_file("enable", 0644, d_events
,
2337 tr
, &ftrace_tr_enable_fops
);
2339 tr
->event_dir
= d_events
;
2345 * event_trace_add_tracer - add a instance of a trace_array to events
2346 * @parent: The parent dentry to place the files/directories for events in
2347 * @tr: The trace array associated with these events
2349 * When a new instance is created, it needs to set up its events
2350 * directory, as well as other files associated with events. It also
2351 * creates the event hierachry in the @parent/events directory.
2353 * Returns 0 on success.
2355 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2359 mutex_lock(&event_mutex
);
2361 ret
= create_event_toplevel_files(parent
, tr
);
2365 down_write(&trace_event_sem
);
2366 __trace_add_event_dirs(tr
);
2367 up_write(&trace_event_sem
);
2370 mutex_unlock(&event_mutex
);
2376 * The top trace array already had its file descriptors created.
2377 * Now the files themselves need to be created.
2380 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2384 mutex_lock(&event_mutex
);
2386 ret
= create_event_toplevel_files(parent
, tr
);
2390 down_write(&trace_event_sem
);
2391 __trace_early_add_event_dirs(tr
);
2392 up_write(&trace_event_sem
);
2395 mutex_unlock(&event_mutex
);
2400 int event_trace_del_tracer(struct trace_array
*tr
)
2402 mutex_lock(&event_mutex
);
2404 /* Disable any event triggers and associated soft-disabled events */
2405 clear_event_triggers(tr
);
2407 /* Disable any running events */
2408 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2410 /* Access to events are within rcu_read_lock_sched() */
2411 synchronize_sched();
2413 down_write(&trace_event_sem
);
2414 __trace_remove_event_dirs(tr
);
2415 debugfs_remove_recursive(tr
->event_dir
);
2416 up_write(&trace_event_sem
);
2418 tr
->event_dir
= NULL
;
2420 mutex_unlock(&event_mutex
);
2425 static __init
int event_trace_memsetup(void)
2427 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2428 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2432 static __init
int event_trace_enable(void)
2434 struct trace_array
*tr
= top_trace_array();
2435 struct ftrace_event_call
**iter
, *call
;
2436 char *buf
= bootup_event_buf
;
2443 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2446 ret
= event_init(call
);
2448 list_add(&call
->list
, &ftrace_events
);
2452 * We need the top trace array to have a working set of trace
2453 * points at early init, before the debug files and directories
2454 * are created. Create the file entries now, and attach them
2455 * to the actual file dentries later.
2457 __trace_early_add_events(tr
);
2460 token
= strsep(&buf
, ",");
2467 ret
= ftrace_set_clr_event(tr
, token
, 1);
2469 pr_warn("Failed to enable trace event: %s\n", token
);
2472 trace_printk_start_comm();
2474 register_event_cmds();
2476 register_trigger_cmds();
2481 static __init
int event_trace_init(void)
2483 struct trace_array
*tr
;
2484 struct dentry
*d_tracer
;
2485 struct dentry
*entry
;
2488 tr
= top_trace_array();
2492 d_tracer
= tracing_init_dentry();
2496 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2497 tr
, &ftrace_avail_fops
);
2499 pr_warn("Could not create debugfs 'available_events' entry\n");
2501 if (trace_define_common_fields())
2502 pr_warn("tracing: Failed to allocate common fields");
2504 ret
= early_event_add_tracer(d_tracer
, tr
);
2508 #ifdef CONFIG_MODULES
2509 ret
= register_module_notifier(&trace_module_nb
);
2511 pr_warn("Failed to register trace events module notifier\n");
2516 void __init
trace_event_init(void)
2518 event_trace_memsetup();
2519 init_ftrace_syscalls();
2520 event_trace_enable();
2523 fs_initcall(event_trace_init
);
2525 #ifdef CONFIG_FTRACE_STARTUP_TEST
2527 static DEFINE_SPINLOCK(test_spinlock
);
2528 static DEFINE_SPINLOCK(test_spinlock_irq
);
2529 static DEFINE_MUTEX(test_mutex
);
2531 static __init
void test_work(struct work_struct
*dummy
)
2533 spin_lock(&test_spinlock
);
2534 spin_lock_irq(&test_spinlock_irq
);
2536 spin_unlock_irq(&test_spinlock_irq
);
2537 spin_unlock(&test_spinlock
);
2539 mutex_lock(&test_mutex
);
2541 mutex_unlock(&test_mutex
);
2544 static __init
int event_test_thread(void *unused
)
2548 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2550 pr_info("failed to kmalloc\n");
2552 schedule_on_each_cpu(test_work
);
2556 set_current_state(TASK_INTERRUPTIBLE
);
2557 while (!kthread_should_stop()) {
2559 set_current_state(TASK_INTERRUPTIBLE
);
2561 __set_current_state(TASK_RUNNING
);
2567 * Do various things that may trigger events.
2569 static __init
void event_test_stuff(void)
2571 struct task_struct
*test_thread
;
2573 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2575 kthread_stop(test_thread
);
2579 * For every trace event defined, we will test each trace point separately,
2580 * and then by groups, and finally all trace points.
2582 static __init
void event_trace_self_tests(void)
2584 struct ftrace_subsystem_dir
*dir
;
2585 struct ftrace_event_file
*file
;
2586 struct ftrace_event_call
*call
;
2587 struct event_subsystem
*system
;
2588 struct trace_array
*tr
;
2591 tr
= top_trace_array();
2595 pr_info("Running tests on trace events:\n");
2597 list_for_each_entry(file
, &tr
->events
, list
) {
2599 call
= file
->event_call
;
2601 /* Only test those that have a probe */
2602 if (!call
->class || !call
->class->probe
)
2606 * Testing syscall events here is pretty useless, but
2607 * we still do it if configured. But this is time consuming.
2608 * What we really need is a user thread to perform the
2609 * syscalls as we test.
2611 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2612 if (call
->class->system
&&
2613 strcmp(call
->class->system
, "syscalls") == 0)
2617 pr_info("Testing event %s: ", ftrace_event_name(call
));
2620 * If an event is already enabled, someone is using
2621 * it and the self test should not be on.
2623 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2624 pr_warn("Enabled event during self test!\n");
2629 ftrace_event_enable_disable(file
, 1);
2631 ftrace_event_enable_disable(file
, 0);
2636 /* Now test at the sub system level */
2638 pr_info("Running tests on trace event systems:\n");
2640 list_for_each_entry(dir
, &tr
->systems
, list
) {
2642 system
= dir
->subsystem
;
2644 /* the ftrace system is special, skip it */
2645 if (strcmp(system
->name
, "ftrace") == 0)
2648 pr_info("Testing event system %s: ", system
->name
);
2650 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2651 if (WARN_ON_ONCE(ret
)) {
2652 pr_warn("error enabling system %s\n",
2659 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2660 if (WARN_ON_ONCE(ret
)) {
2661 pr_warn("error disabling system %s\n",
2669 /* Test with all events enabled */
2671 pr_info("Running tests on all trace events:\n");
2672 pr_info("Testing all events: ");
2674 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2675 if (WARN_ON_ONCE(ret
)) {
2676 pr_warn("error enabling all events\n");
2683 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2684 if (WARN_ON_ONCE(ret
)) {
2685 pr_warn("error disabling all events\n");
2692 #ifdef CONFIG_FUNCTION_TRACER
2694 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2697 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2698 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2700 struct ring_buffer_event
*event
;
2701 struct ring_buffer
*buffer
;
2702 struct ftrace_entry
*entry
;
2703 unsigned long flags
;
2708 pc
= preempt_count();
2709 preempt_disable_notrace();
2710 cpu
= raw_smp_processor_id();
2711 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2716 local_save_flags(flags
);
2718 event
= trace_current_buffer_lock_reserve(&buffer
,
2719 TRACE_FN
, sizeof(*entry
),
2723 entry
= ring_buffer_event_data(event
);
2725 entry
->parent_ip
= parent_ip
;
2727 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2730 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2731 preempt_enable_notrace();
2734 static struct ftrace_ops trace_ops __initdata
=
2736 .func
= function_test_events_call
,
2737 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2740 static __init
void event_trace_self_test_with_function(void)
2743 ret
= register_ftrace_function(&trace_ops
);
2744 if (WARN_ON(ret
< 0)) {
2745 pr_info("Failed to enable function tracer for event tests\n");
2748 pr_info("Running tests again, along with the function tracer\n");
2749 event_trace_self_tests();
2750 unregister_ftrace_function(&trace_ops
);
2753 static __init
void event_trace_self_test_with_function(void)
2758 static __init
int event_trace_self_tests_init(void)
2760 if (!tracing_selftest_disabled
) {
2761 event_trace_self_tests();
2762 event_trace_self_test_with_function();
2768 late_initcall(event_trace_self_tests_init
);