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 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex
);
30 DEFINE_MUTEX(event_storage_mutex
);
31 EXPORT_SYMBOL_GPL(event_storage_mutex
);
33 char event_storage
[EVENT_STORAGE_SIZE
];
34 EXPORT_SYMBOL_GPL(event_storage
);
36 LIST_HEAD(ftrace_events
);
37 LIST_HEAD(ftrace_common_fields
);
39 /* Double loops, do not use break, only goto's work */
40 #define do_for_each_event_file(tr, file) \
41 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
42 list_for_each_entry(file, &tr->events, list)
44 #define do_for_each_event_file_safe(tr, file) \
45 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
46 struct ftrace_event_file *___n; \
47 list_for_each_entry_safe(file, ___n, &tr->events, list)
49 #define while_for_each_event_file() \
53 trace_get_fields(struct ftrace_event_call
*event_call
)
55 if (!event_call
->class->get_fields
)
56 return &event_call
->class->fields
;
57 return event_call
->class->get_fields(event_call
);
60 static int __trace_define_field(struct list_head
*head
, const char *type
,
61 const char *name
, int offset
, int size
,
62 int is_signed
, int filter_type
)
64 struct ftrace_event_field
*field
;
66 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
70 field
->name
= kstrdup(name
, GFP_KERNEL
);
74 field
->type
= kstrdup(type
, GFP_KERNEL
);
78 if (filter_type
== FILTER_OTHER
)
79 field
->filter_type
= filter_assign_type(type
);
81 field
->filter_type
= filter_type
;
83 field
->offset
= offset
;
85 field
->is_signed
= is_signed
;
87 list_add(&field
->link
, head
);
99 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
100 const char *name
, int offset
, int size
, int is_signed
,
103 struct list_head
*head
;
105 if (WARN_ON(!call
->class))
108 head
= trace_get_fields(call
);
109 return __trace_define_field(head
, type
, name
, offset
, size
,
110 is_signed
, filter_type
);
112 EXPORT_SYMBOL_GPL(trace_define_field
);
114 #define __common_field(type, item) \
115 ret = __trace_define_field(&ftrace_common_fields, #type, \
117 offsetof(typeof(ent), item), \
119 is_signed_type(type), FILTER_OTHER); \
123 static int trace_define_common_fields(void)
126 struct trace_entry ent
;
128 __common_field(unsigned short, type
);
129 __common_field(unsigned char, flags
);
130 __common_field(unsigned char, preempt_count
);
131 __common_field(int, pid
);
136 void trace_destroy_fields(struct ftrace_event_call
*call
)
138 struct ftrace_event_field
*field
, *next
;
139 struct list_head
*head
;
141 head
= trace_get_fields(call
);
142 list_for_each_entry_safe(field
, next
, head
, link
) {
143 list_del(&field
->link
);
150 int trace_event_raw_init(struct ftrace_event_call
*call
)
154 id
= register_ftrace_event(&call
->event
);
160 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
162 int ftrace_event_reg(struct ftrace_event_call
*call
,
163 enum trace_reg type
, void *data
)
165 struct ftrace_event_file
*file
= data
;
168 case TRACE_REG_REGISTER
:
169 return tracepoint_probe_register(call
->name
,
172 case TRACE_REG_UNREGISTER
:
173 tracepoint_probe_unregister(call
->name
,
178 #ifdef CONFIG_PERF_EVENTS
179 case TRACE_REG_PERF_REGISTER
:
180 return tracepoint_probe_register(call
->name
,
181 call
->class->perf_probe
,
183 case TRACE_REG_PERF_UNREGISTER
:
184 tracepoint_probe_unregister(call
->name
,
185 call
->class->perf_probe
,
188 case TRACE_REG_PERF_OPEN
:
189 case TRACE_REG_PERF_CLOSE
:
190 case TRACE_REG_PERF_ADD
:
191 case TRACE_REG_PERF_DEL
:
197 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
199 void trace_event_enable_cmd_record(bool enable
)
201 struct ftrace_event_file
*file
;
202 struct trace_array
*tr
;
204 mutex_lock(&event_mutex
);
205 do_for_each_event_file(tr
, file
) {
207 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
211 tracing_start_cmdline_record();
212 file
->flags
|= FTRACE_EVENT_FL_RECORDED_CMD
;
214 tracing_stop_cmdline_record();
215 file
->flags
&= ~FTRACE_EVENT_FL_RECORDED_CMD
;
217 } while_for_each_event_file();
218 mutex_unlock(&event_mutex
);
221 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
224 struct ftrace_event_call
*call
= file
->event_call
;
229 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
230 file
->flags
&= ~FTRACE_EVENT_FL_ENABLED
;
231 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
232 tracing_stop_cmdline_record();
233 file
->flags
&= ~FTRACE_EVENT_FL_RECORDED_CMD
;
235 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
239 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
240 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
241 tracing_start_cmdline_record();
242 file
->flags
|= FTRACE_EVENT_FL_RECORDED_CMD
;
244 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
246 tracing_stop_cmdline_record();
247 pr_info("event trace: Could not enable event "
251 file
->flags
|= FTRACE_EVENT_FL_ENABLED
;
259 static void ftrace_clear_events(struct trace_array
*tr
)
261 struct ftrace_event_file
*file
;
263 mutex_lock(&event_mutex
);
264 list_for_each_entry(file
, &tr
->events
, list
) {
265 ftrace_event_enable_disable(file
, 0);
267 mutex_unlock(&event_mutex
);
270 static void __put_system(struct event_subsystem
*system
)
272 struct event_filter
*filter
= system
->filter
;
274 WARN_ON_ONCE(system
->ref_count
== 0);
275 if (--system
->ref_count
)
278 list_del(&system
->list
);
281 kfree(filter
->filter_string
);
288 static void __get_system(struct event_subsystem
*system
)
290 WARN_ON_ONCE(system
->ref_count
== 0);
294 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
296 WARN_ON_ONCE(dir
->ref_count
== 0);
298 __get_system(dir
->subsystem
);
301 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
303 WARN_ON_ONCE(dir
->ref_count
== 0);
304 /* If the subsystem is about to be freed, the dir must be too */
305 WARN_ON_ONCE(dir
->subsystem
->ref_count
== 1 && dir
->ref_count
!= 1);
307 __put_system(dir
->subsystem
);
308 if (!--dir
->ref_count
)
312 static void put_system(struct ftrace_subsystem_dir
*dir
)
314 mutex_lock(&event_mutex
);
315 __put_system_dir(dir
);
316 mutex_unlock(&event_mutex
);
320 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
322 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
323 const char *sub
, const char *event
, int set
)
325 struct ftrace_event_file
*file
;
326 struct ftrace_event_call
*call
;
329 mutex_lock(&event_mutex
);
330 list_for_each_entry(file
, &tr
->events
, list
) {
332 call
= file
->event_call
;
334 if (!call
->name
|| !call
->class || !call
->class->reg
)
337 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
341 strcmp(match
, call
->name
) != 0 &&
342 strcmp(match
, call
->class->system
) != 0)
345 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
348 if (event
&& strcmp(event
, call
->name
) != 0)
351 ftrace_event_enable_disable(file
, set
);
355 mutex_unlock(&event_mutex
);
360 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
362 char *event
= NULL
, *sub
= NULL
, *match
;
365 * The buf format can be <subsystem>:<event-name>
366 * *:<event-name> means any event by that name.
367 * :<event-name> is the same.
369 * <subsystem>:* means all events in that subsystem
370 * <subsystem>: means the same.
372 * <name> (no ':') means all events in a subsystem with
373 * the name <name> or any event that matches <name>
376 match
= strsep(&buf
, ":");
382 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
384 if (!strlen(event
) || strcmp(event
, "*") == 0)
388 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
392 * trace_set_clr_event - enable or disable an event
393 * @system: system name to match (NULL for any system)
394 * @event: event name to match (NULL for all events, within system)
395 * @set: 1 to enable, 0 to disable
397 * This is a way for other parts of the kernel to enable or disable
400 * Returns 0 on success, -EINVAL if the parameters do not match any
403 int trace_set_clr_event(const char *system
, const char *event
, int set
)
405 struct trace_array
*tr
= top_trace_array();
407 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
409 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
411 /* 128 should be much more than enough */
412 #define EVENT_BUF_SIZE 127
415 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
416 size_t cnt
, loff_t
*ppos
)
418 struct trace_parser parser
;
419 struct seq_file
*m
= file
->private_data
;
420 struct trace_array
*tr
= m
->private;
426 ret
= tracing_update_buffers();
430 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
433 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
435 if (read
>= 0 && trace_parser_loaded((&parser
))) {
438 if (*parser
.buffer
== '!')
441 parser
.buffer
[parser
.idx
] = 0;
443 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
451 trace_parser_put(&parser
);
457 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
459 struct ftrace_event_file
*file
= v
;
460 struct ftrace_event_call
*call
;
461 struct trace_array
*tr
= m
->private;
465 list_for_each_entry_continue(file
, &tr
->events
, list
) {
466 call
= file
->event_call
;
468 * The ftrace subsystem is for showing formats only.
469 * They can not be enabled or disabled via the event files.
471 if (call
->class && call
->class->reg
)
478 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
480 struct ftrace_event_file
*file
;
481 struct trace_array
*tr
= m
->private;
484 mutex_lock(&event_mutex
);
486 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
487 for (l
= 0; l
<= *pos
; ) {
488 file
= t_next(m
, file
, &l
);
496 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
498 struct ftrace_event_file
*file
= v
;
499 struct trace_array
*tr
= m
->private;
503 list_for_each_entry_continue(file
, &tr
->events
, list
) {
504 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
511 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
513 struct ftrace_event_file
*file
;
514 struct trace_array
*tr
= m
->private;
517 mutex_lock(&event_mutex
);
519 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
520 for (l
= 0; l
<= *pos
; ) {
521 file
= s_next(m
, file
, &l
);
528 static int t_show(struct seq_file
*m
, void *v
)
530 struct ftrace_event_file
*file
= v
;
531 struct ftrace_event_call
*call
= file
->event_call
;
533 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
534 seq_printf(m
, "%s:", call
->class->system
);
535 seq_printf(m
, "%s\n", call
->name
);
540 static void t_stop(struct seq_file
*m
, void *p
)
542 mutex_unlock(&event_mutex
);
546 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
549 struct ftrace_event_file
*file
= filp
->private_data
;
552 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
557 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
561 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
564 struct ftrace_event_file
*file
= filp
->private_data
;
571 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
575 ret
= tracing_update_buffers();
582 mutex_lock(&event_mutex
);
583 ret
= ftrace_event_enable_disable(file
, val
);
584 mutex_unlock(&event_mutex
);
593 return ret
? ret
: cnt
;
597 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
600 const char set_to_char
[4] = { '?', '0', '1', 'X' };
601 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
602 struct event_subsystem
*system
= dir
->subsystem
;
603 struct ftrace_event_call
*call
;
604 struct ftrace_event_file
*file
;
605 struct trace_array
*tr
= dir
->tr
;
610 mutex_lock(&event_mutex
);
611 list_for_each_entry(file
, &tr
->events
, list
) {
612 call
= file
->event_call
;
613 if (!call
->name
|| !call
->class || !call
->class->reg
)
616 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
620 * We need to find out if all the events are set
621 * or if all events or cleared, or if we have
624 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
627 * If we have a mixture, no need to look further.
632 mutex_unlock(&event_mutex
);
634 buf
[0] = set_to_char
[set
];
637 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
643 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
646 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
647 struct event_subsystem
*system
= dir
->subsystem
;
648 const char *name
= NULL
;
652 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
656 ret
= tracing_update_buffers();
660 if (val
!= 0 && val
!= 1)
664 * Opening of "enable" adds a ref count to system,
665 * so the name is safe to use.
670 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
684 FORMAT_FIELD_SEPERATOR
= 2,
688 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
690 struct ftrace_event_call
*call
= m
->private;
691 struct ftrace_event_field
*field
;
692 struct list_head
*common_head
= &ftrace_common_fields
;
693 struct list_head
*head
= trace_get_fields(call
);
697 switch ((unsigned long)v
) {
699 if (unlikely(list_empty(common_head
)))
702 field
= list_entry(common_head
->prev
,
703 struct ftrace_event_field
, link
);
706 case FORMAT_FIELD_SEPERATOR
:
707 if (unlikely(list_empty(head
)))
710 field
= list_entry(head
->prev
, struct ftrace_event_field
, link
);
713 case FORMAT_PRINTFMT
:
719 if (field
->link
.prev
== common_head
)
720 return (void *)FORMAT_FIELD_SEPERATOR
;
721 else if (field
->link
.prev
== head
)
722 return (void *)FORMAT_PRINTFMT
;
724 field
= list_entry(field
->link
.prev
, struct ftrace_event_field
, link
);
729 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
734 /* Start by showing the header */
736 return (void *)FORMAT_HEADER
;
738 p
= (void *)FORMAT_HEADER
;
740 p
= f_next(m
, p
, &l
);
741 } while (p
&& l
< *pos
);
746 static int f_show(struct seq_file
*m
, void *v
)
748 struct ftrace_event_call
*call
= m
->private;
749 struct ftrace_event_field
*field
;
750 const char *array_descriptor
;
752 switch ((unsigned long)v
) {
754 seq_printf(m
, "name: %s\n", call
->name
);
755 seq_printf(m
, "ID: %d\n", call
->event
.type
);
756 seq_printf(m
, "format:\n");
759 case FORMAT_FIELD_SEPERATOR
:
763 case FORMAT_PRINTFMT
:
764 seq_printf(m
, "\nprint fmt: %s\n",
772 * Smartly shows the array type(except dynamic array).
775 * If TYPE := TYPE[LEN], it is shown:
776 * field:TYPE VAR[LEN]
778 array_descriptor
= strchr(field
->type
, '[');
780 if (!strncmp(field
->type
, "__data_loc", 10))
781 array_descriptor
= NULL
;
783 if (!array_descriptor
)
784 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
785 field
->type
, field
->name
, field
->offset
,
786 field
->size
, !!field
->is_signed
);
788 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
789 (int)(array_descriptor
- field
->type
),
790 field
->type
, field
->name
,
791 array_descriptor
, field
->offset
,
792 field
->size
, !!field
->is_signed
);
797 static void f_stop(struct seq_file
*m
, void *p
)
801 static const struct seq_operations trace_format_seq_ops
= {
808 static int trace_format_open(struct inode
*inode
, struct file
*file
)
810 struct ftrace_event_call
*call
= inode
->i_private
;
814 ret
= seq_open(file
, &trace_format_seq_ops
);
818 m
= file
->private_data
;
825 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
827 struct ftrace_event_call
*call
= filp
->private_data
;
834 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
839 trace_seq_printf(s
, "%d\n", call
->event
.type
);
841 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
848 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
851 struct ftrace_event_call
*call
= filp
->private_data
;
858 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
864 print_event_filter(call
, s
);
865 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
873 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
876 struct ftrace_event_call
*call
= filp
->private_data
;
880 if (cnt
>= PAGE_SIZE
)
883 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
887 if (copy_from_user(buf
, ubuf
, cnt
)) {
888 free_page((unsigned long) buf
);
893 err
= apply_event_filter(call
, buf
);
894 free_page((unsigned long) buf
);
903 static LIST_HEAD(event_subsystems
);
905 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
907 struct event_subsystem
*system
= NULL
;
908 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
909 struct trace_array
*tr
;
912 /* Make sure the system still exists */
913 mutex_lock(&event_mutex
);
914 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
915 list_for_each_entry(dir
, &tr
->systems
, list
) {
916 if (dir
== inode
->i_private
) {
917 /* Don't open systems with no events */
918 if (dir
->nr_events
) {
919 __get_system_dir(dir
);
920 system
= dir
->subsystem
;
927 mutex_unlock(&event_mutex
);
932 /* Some versions of gcc think dir can be uninitialized here */
935 ret
= tracing_open_generic(inode
, filp
);
942 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
944 struct ftrace_subsystem_dir
*dir
;
945 struct trace_array
*tr
= inode
->i_private
;
948 /* Make a temporary dir that has no system but points to tr */
949 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
955 ret
= tracing_open_generic(inode
, filp
);
959 filp
->private_data
= dir
;
964 static int subsystem_release(struct inode
*inode
, struct file
*file
)
966 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
969 * If dir->subsystem is NULL, then this is a temporary
970 * descriptor that was made for a trace_array to enable
982 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
985 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
986 struct event_subsystem
*system
= dir
->subsystem
;
993 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
999 print_subsystem_event_filter(system
, s
);
1000 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1008 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1011 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1015 if (cnt
>= PAGE_SIZE
)
1018 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1022 if (copy_from_user(buf
, ubuf
, cnt
)) {
1023 free_page((unsigned long) buf
);
1028 err
= apply_subsystem_event_filter(dir
, buf
);
1029 free_page((unsigned long) buf
);
1039 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1041 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1042 struct trace_seq
*s
;
1048 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1055 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1062 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1063 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1065 static const struct seq_operations show_event_seq_ops
= {
1072 static const struct seq_operations show_set_event_seq_ops
= {
1079 static const struct file_operations ftrace_avail_fops
= {
1080 .open
= ftrace_event_avail_open
,
1082 .llseek
= seq_lseek
,
1083 .release
= seq_release
,
1086 static const struct file_operations ftrace_set_event_fops
= {
1087 .open
= ftrace_event_set_open
,
1089 .write
= ftrace_event_write
,
1090 .llseek
= seq_lseek
,
1091 .release
= seq_release
,
1094 static const struct file_operations ftrace_enable_fops
= {
1095 .open
= tracing_open_generic
,
1096 .read
= event_enable_read
,
1097 .write
= event_enable_write
,
1098 .llseek
= default_llseek
,
1101 static const struct file_operations ftrace_event_format_fops
= {
1102 .open
= trace_format_open
,
1104 .llseek
= seq_lseek
,
1105 .release
= seq_release
,
1108 static const struct file_operations ftrace_event_id_fops
= {
1109 .open
= tracing_open_generic
,
1110 .read
= event_id_read
,
1111 .llseek
= default_llseek
,
1114 static const struct file_operations ftrace_event_filter_fops
= {
1115 .open
= tracing_open_generic
,
1116 .read
= event_filter_read
,
1117 .write
= event_filter_write
,
1118 .llseek
= default_llseek
,
1121 static const struct file_operations ftrace_subsystem_filter_fops
= {
1122 .open
= subsystem_open
,
1123 .read
= subsystem_filter_read
,
1124 .write
= subsystem_filter_write
,
1125 .llseek
= default_llseek
,
1126 .release
= subsystem_release
,
1129 static const struct file_operations ftrace_system_enable_fops
= {
1130 .open
= subsystem_open
,
1131 .read
= system_enable_read
,
1132 .write
= system_enable_write
,
1133 .llseek
= default_llseek
,
1134 .release
= subsystem_release
,
1137 static const struct file_operations ftrace_tr_enable_fops
= {
1138 .open
= system_tr_open
,
1139 .read
= system_enable_read
,
1140 .write
= system_enable_write
,
1141 .llseek
= default_llseek
,
1142 .release
= subsystem_release
,
1145 static const struct file_operations ftrace_show_header_fops
= {
1146 .open
= tracing_open_generic
,
1147 .read
= show_header
,
1148 .llseek
= default_llseek
,
1152 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1153 const struct seq_operations
*seq_ops
)
1158 ret
= seq_open(file
, seq_ops
);
1161 m
= file
->private_data
;
1162 /* copy tr over to seq ops */
1163 m
->private = inode
->i_private
;
1169 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1171 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1173 return ftrace_event_open(inode
, file
, seq_ops
);
1177 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1179 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1180 struct trace_array
*tr
= inode
->i_private
;
1182 if ((file
->f_mode
& FMODE_WRITE
) &&
1183 (file
->f_flags
& O_TRUNC
))
1184 ftrace_clear_events(tr
);
1186 return ftrace_event_open(inode
, file
, seq_ops
);
1189 static struct event_subsystem
*
1190 create_new_subsystem(const char *name
)
1192 struct event_subsystem
*system
;
1194 /* need to create new entry */
1195 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1199 system
->ref_count
= 1;
1200 system
->name
= kstrdup(name
, GFP_KERNEL
);
1205 system
->filter
= NULL
;
1207 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1208 if (!system
->filter
)
1211 list_add(&system
->list
, &event_subsystems
);
1216 kfree(system
->name
);
1221 static struct dentry
*
1222 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1223 struct ftrace_event_file
*file
, struct dentry
*parent
)
1225 struct ftrace_subsystem_dir
*dir
;
1226 struct event_subsystem
*system
;
1227 struct dentry
*entry
;
1229 /* First see if we did not already create this dir */
1230 list_for_each_entry(dir
, &tr
->systems
, list
) {
1231 system
= dir
->subsystem
;
1232 if (strcmp(system
->name
, name
) == 0) {
1239 /* Now see if the system itself exists. */
1240 list_for_each_entry(system
, &event_subsystems
, list
) {
1241 if (strcmp(system
->name
, name
) == 0)
1244 /* Reset system variable when not found */
1245 if (&system
->list
== &event_subsystems
)
1248 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1253 system
= create_new_subsystem(name
);
1257 __get_system(system
);
1259 dir
->entry
= debugfs_create_dir(name
, parent
);
1261 pr_warning("Failed to create system directory %s\n", name
);
1262 __put_system(system
);
1269 dir
->subsystem
= system
;
1272 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1273 &ftrace_subsystem_filter_fops
);
1275 kfree(system
->filter
);
1276 system
->filter
= NULL
;
1277 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1280 trace_create_file("enable", 0644, dir
->entry
, dir
,
1281 &ftrace_system_enable_fops
);
1283 list_add(&dir
->list
, &tr
->systems
);
1290 /* Only print this message if failed on memory allocation */
1291 if (!dir
|| !system
)
1292 pr_warning("No memory to create event subsystem %s\n",
1298 event_create_dir(struct dentry
*parent
,
1299 struct ftrace_event_file
*file
,
1300 const struct file_operations
*id
,
1301 const struct file_operations
*enable
,
1302 const struct file_operations
*filter
,
1303 const struct file_operations
*format
)
1305 struct ftrace_event_call
*call
= file
->event_call
;
1306 struct trace_array
*tr
= file
->tr
;
1307 struct list_head
*head
;
1308 struct dentry
*d_events
;
1312 * If the trace point header did not define TRACE_SYSTEM
1313 * then the system would be called "TRACE_SYSTEM".
1315 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1316 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1322 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1324 pr_warning("Could not create debugfs '%s' directory\n",
1329 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1330 trace_create_file("enable", 0644, file
->dir
, file
,
1333 #ifdef CONFIG_PERF_EVENTS
1334 if (call
->event
.type
&& call
->class->reg
)
1335 trace_create_file("id", 0444, file
->dir
, call
,
1340 * Other events may have the same class. Only update
1341 * the fields if they are not already defined.
1343 head
= trace_get_fields(call
);
1344 if (list_empty(head
)) {
1345 ret
= call
->class->define_fields(call
);
1347 pr_warning("Could not initialize trace point"
1348 " events/%s\n", call
->name
);
1352 trace_create_file("filter", 0644, file
->dir
, call
,
1355 trace_create_file("format", 0444, file
->dir
, call
,
1361 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
1366 if (!--dir
->nr_events
) {
1367 debugfs_remove_recursive(dir
->entry
);
1368 list_del(&dir
->list
);
1369 __put_system_dir(dir
);
1373 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1375 struct ftrace_event_file
*file
;
1376 struct trace_array
*tr
;
1378 do_for_each_event_file_safe(tr
, file
) {
1380 if (file
->event_call
!= call
)
1383 list_del(&file
->list
);
1384 debugfs_remove_recursive(file
->dir
);
1385 remove_subsystem(file
->system
);
1389 * The do_for_each_event_file_safe() is
1390 * a double loop. After finding the call for this
1391 * trace_array, we use break to jump to the next
1395 } while_for_each_event_file();
1398 static void event_remove(struct ftrace_event_call
*call
)
1400 struct trace_array
*tr
;
1401 struct ftrace_event_file
*file
;
1403 do_for_each_event_file(tr
, file
) {
1404 if (file
->event_call
!= call
)
1406 ftrace_event_enable_disable(file
, 0);
1408 * The do_for_each_event_file() is
1409 * a double loop. After finding the call for this
1410 * trace_array, we use break to jump to the next
1414 } while_for_each_event_file();
1416 if (call
->event
.funcs
)
1417 __unregister_ftrace_event(&call
->event
);
1418 remove_event_from_tracers(call
);
1419 list_del(&call
->list
);
1422 static int event_init(struct ftrace_event_call
*call
)
1426 if (WARN_ON(!call
->name
))
1429 if (call
->class->raw_init
) {
1430 ret
= call
->class->raw_init(call
);
1431 if (ret
< 0 && ret
!= -ENOSYS
)
1432 pr_warn("Could not initialize trace events/%s\n",
1440 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1444 ret
= event_init(call
);
1448 list_add(&call
->list
, &ftrace_events
);
1454 /* Add an event to a trace directory */
1456 __trace_add_new_event(struct ftrace_event_call
*call
,
1457 struct trace_array
*tr
,
1458 const struct file_operations
*id
,
1459 const struct file_operations
*enable
,
1460 const struct file_operations
*filter
,
1461 const struct file_operations
*format
)
1463 struct ftrace_event_file
*file
;
1465 file
= kzalloc(sizeof(*file
), GFP_KERNEL
);
1469 file
->event_call
= call
;
1471 list_add(&file
->list
, &tr
->events
);
1473 return event_create_dir(tr
->event_dir
, file
, id
, enable
, filter
, format
);
1477 * Just create a decriptor for early init. A descriptor is required
1478 * for enabling events at boot. We want to enable events before
1479 * the filesystem is initialized.
1482 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1483 struct trace_array
*tr
)
1485 struct ftrace_event_file
*file
;
1487 file
= kzalloc(sizeof(*file
), GFP_KERNEL
);
1491 file
->event_call
= call
;
1493 list_add(&file
->list
, &tr
->events
);
1498 struct ftrace_module_file_ops
;
1499 static void __add_event_to_tracers(struct ftrace_event_call
*call
,
1500 struct ftrace_module_file_ops
*file_ops
);
1502 /* Add an additional event_call dynamically */
1503 int trace_add_event_call(struct ftrace_event_call
*call
)
1506 mutex_lock(&event_mutex
);
1508 ret
= __register_event(call
, NULL
);
1510 __add_event_to_tracers(call
, NULL
);
1512 mutex_unlock(&event_mutex
);
1517 * Must be called under locking both of event_mutex and trace_event_mutex.
1519 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1522 trace_destroy_fields(call
);
1523 destroy_preds(call
);
1526 /* Remove an event_call */
1527 void trace_remove_event_call(struct ftrace_event_call
*call
)
1529 mutex_lock(&event_mutex
);
1530 down_write(&trace_event_mutex
);
1531 __trace_remove_event_call(call
);
1532 up_write(&trace_event_mutex
);
1533 mutex_unlock(&event_mutex
);
1536 #define for_each_event(event, start, end) \
1537 for (event = start; \
1538 (unsigned long)event < (unsigned long)end; \
1541 #ifdef CONFIG_MODULES
1543 static LIST_HEAD(ftrace_module_file_list
);
1546 * Modules must own their file_operations to keep up with
1547 * reference counting.
1549 struct ftrace_module_file_ops
{
1550 struct list_head list
;
1552 struct file_operations id
;
1553 struct file_operations enable
;
1554 struct file_operations format
;
1555 struct file_operations filter
;
1558 static struct ftrace_module_file_ops
*find_ftrace_file_ops(struct module
*mod
)
1560 struct ftrace_module_file_ops
*file_ops
;
1562 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1563 if (file_ops
->mod
== mod
)
1569 static struct ftrace_module_file_ops
*
1570 trace_create_file_ops(struct module
*mod
)
1572 struct ftrace_module_file_ops
*file_ops
;
1575 * This is a bit of a PITA. To allow for correct reference
1576 * counting, modules must "own" their file_operations.
1577 * To do this, we allocate the file operations that will be
1578 * used in the event directory.
1581 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1585 file_ops
->mod
= mod
;
1587 file_ops
->id
= ftrace_event_id_fops
;
1588 file_ops
->id
.owner
= mod
;
1590 file_ops
->enable
= ftrace_enable_fops
;
1591 file_ops
->enable
.owner
= mod
;
1593 file_ops
->filter
= ftrace_event_filter_fops
;
1594 file_ops
->filter
.owner
= mod
;
1596 file_ops
->format
= ftrace_event_format_fops
;
1597 file_ops
->format
.owner
= mod
;
1599 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1604 static void trace_module_add_events(struct module
*mod
)
1606 struct ftrace_module_file_ops
*file_ops
= NULL
;
1607 struct ftrace_event_call
**call
, **start
, **end
;
1609 start
= mod
->trace_events
;
1610 end
= mod
->trace_events
+ mod
->num_trace_events
;
1615 file_ops
= trace_create_file_ops(mod
);
1619 for_each_event(call
, start
, end
) {
1620 __register_event(*call
, mod
);
1621 __add_event_to_tracers(*call
, file_ops
);
1625 static void trace_module_remove_events(struct module
*mod
)
1627 struct ftrace_module_file_ops
*file_ops
;
1628 struct ftrace_event_call
*call
, *p
;
1631 down_write(&trace_event_mutex
);
1632 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1633 if (call
->mod
== mod
) {
1635 __trace_remove_event_call(call
);
1639 /* Now free the file_operations */
1640 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1641 if (file_ops
->mod
== mod
)
1644 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1645 list_del(&file_ops
->list
);
1650 * It is safest to reset the ring buffer if the module being unloaded
1651 * registered any events.
1654 tracing_reset_current_online_cpus();
1655 up_write(&trace_event_mutex
);
1658 static int trace_module_notify(struct notifier_block
*self
,
1659 unsigned long val
, void *data
)
1661 struct module
*mod
= data
;
1663 mutex_lock(&event_mutex
);
1665 case MODULE_STATE_COMING
:
1666 trace_module_add_events(mod
);
1668 case MODULE_STATE_GOING
:
1669 trace_module_remove_events(mod
);
1672 mutex_unlock(&event_mutex
);
1677 static struct ftrace_module_file_ops
*find_ftrace_file_ops(struct module
*mod
)
1681 static int trace_module_notify(struct notifier_block
*self
,
1682 unsigned long val
, void *data
)
1686 #endif /* CONFIG_MODULES */
1688 /* Create a new event directory structure for a trace directory. */
1690 __trace_add_event_dirs(struct trace_array
*tr
)
1692 struct ftrace_module_file_ops
*file_ops
= NULL
;
1693 struct ftrace_event_call
*call
;
1696 list_for_each_entry(call
, &ftrace_events
, list
) {
1699 * Directories for events by modules need to
1700 * keep module ref counts when opened (as we don't
1701 * want the module to disappear when reading one
1702 * of these files). The file_ops keep account of
1703 * the module ref count.
1705 * As event_calls are added in groups by module,
1706 * when we find one file_ops, we don't need to search for
1707 * each call in that module, as the rest should be the
1708 * same. Only search for a new one if the last one did
1711 if (!file_ops
|| call
->mod
!= file_ops
->mod
)
1712 file_ops
= find_ftrace_file_ops(call
->mod
);
1714 continue; /* Warn? */
1715 ret
= __trace_add_new_event(call
, tr
,
1716 &file_ops
->id
, &file_ops
->enable
,
1717 &file_ops
->filter
, &file_ops
->format
);
1719 pr_warning("Could not create directory for event %s\n",
1723 ret
= __trace_add_new_event(call
, tr
,
1724 &ftrace_event_id_fops
,
1725 &ftrace_enable_fops
,
1726 &ftrace_event_filter_fops
,
1727 &ftrace_event_format_fops
);
1729 pr_warning("Could not create directory for event %s\n",
1735 * The top level array has already had its ftrace_event_file
1736 * descriptors created in order to allow for early events to
1737 * be recorded. This function is called after the debugfs has been
1738 * initialized, and we now have to create the files associated
1742 __trace_early_add_event_dirs(struct trace_array
*tr
)
1744 struct ftrace_event_file
*file
;
1748 list_for_each_entry(file
, &tr
->events
, list
) {
1749 ret
= event_create_dir(tr
->event_dir
, file
,
1750 &ftrace_event_id_fops
,
1751 &ftrace_enable_fops
,
1752 &ftrace_event_filter_fops
,
1753 &ftrace_event_format_fops
);
1755 pr_warning("Could not create directory for event %s\n",
1756 file
->event_call
->name
);
1761 * For early boot up, the top trace array requires to have
1762 * a list of events that can be enabled. This must be done before
1763 * the filesystem is set up in order to allow events to be traced
1767 __trace_early_add_events(struct trace_array
*tr
)
1769 struct ftrace_event_call
*call
;
1772 list_for_each_entry(call
, &ftrace_events
, list
) {
1773 /* Early boot up should not have any modules loaded */
1774 if (WARN_ON_ONCE(call
->mod
))
1777 ret
= __trace_early_add_new_event(call
, tr
);
1779 pr_warning("Could not create early event %s\n",
1784 /* Remove the event directory structure for a trace directory. */
1786 __trace_remove_event_dirs(struct trace_array
*tr
)
1788 struct ftrace_event_file
*file
, *next
;
1790 list_for_each_entry_safe(file
, next
, &tr
->events
, list
) {
1791 list_del(&file
->list
);
1792 debugfs_remove_recursive(file
->dir
);
1793 remove_subsystem(file
->system
);
1799 __add_event_to_tracers(struct ftrace_event_call
*call
,
1800 struct ftrace_module_file_ops
*file_ops
)
1802 struct trace_array
*tr
;
1804 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1806 __trace_add_new_event(call
, tr
,
1807 &file_ops
->id
, &file_ops
->enable
,
1808 &file_ops
->filter
, &file_ops
->format
);
1810 __trace_add_new_event(call
, tr
,
1811 &ftrace_event_id_fops
,
1812 &ftrace_enable_fops
,
1813 &ftrace_event_filter_fops
,
1814 &ftrace_event_format_fops
);
1818 static struct notifier_block trace_module_nb
= {
1819 .notifier_call
= trace_module_notify
,
1823 extern struct ftrace_event_call
*__start_ftrace_events
[];
1824 extern struct ftrace_event_call
*__stop_ftrace_events
[];
1826 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
1828 static __init
int setup_trace_event(char *str
)
1830 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
1831 ring_buffer_expanded
= 1;
1832 tracing_selftest_disabled
= 1;
1836 __setup("trace_event=", setup_trace_event
);
1838 /* Expects to have event_mutex held when called */
1840 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
1842 struct dentry
*d_events
;
1843 struct dentry
*entry
;
1845 entry
= debugfs_create_file("set_event", 0644, parent
,
1846 tr
, &ftrace_set_event_fops
);
1848 pr_warning("Could not create debugfs 'set_event' entry\n");
1852 d_events
= debugfs_create_dir("events", parent
);
1854 pr_warning("Could not create debugfs 'events' directory\n");
1858 /* ring buffer internal formats */
1859 trace_create_file("header_page", 0444, d_events
,
1860 ring_buffer_print_page_header
,
1861 &ftrace_show_header_fops
);
1863 trace_create_file("header_event", 0444, d_events
,
1864 ring_buffer_print_entry_header
,
1865 &ftrace_show_header_fops
);
1867 trace_create_file("enable", 0644, d_events
,
1868 tr
, &ftrace_tr_enable_fops
);
1870 tr
->event_dir
= d_events
;
1876 * event_trace_add_tracer - add a instance of a trace_array to events
1877 * @parent: The parent dentry to place the files/directories for events in
1878 * @tr: The trace array associated with these events
1880 * When a new instance is created, it needs to set up its events
1881 * directory, as well as other files associated with events. It also
1882 * creates the event hierachry in the @parent/events directory.
1884 * Returns 0 on success.
1886 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
1890 mutex_lock(&event_mutex
);
1892 ret
= create_event_toplevel_files(parent
, tr
);
1896 down_write(&trace_event_mutex
);
1897 __trace_add_event_dirs(tr
);
1898 up_write(&trace_event_mutex
);
1901 mutex_unlock(&event_mutex
);
1907 * The top trace array already had its file descriptors created.
1908 * Now the files themselves need to be created.
1911 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
1915 mutex_lock(&event_mutex
);
1917 ret
= create_event_toplevel_files(parent
, tr
);
1921 down_write(&trace_event_mutex
);
1922 __trace_early_add_event_dirs(tr
);
1923 up_write(&trace_event_mutex
);
1926 mutex_unlock(&event_mutex
);
1931 int event_trace_del_tracer(struct trace_array
*tr
)
1933 /* Disable any running events */
1934 __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
1936 mutex_lock(&event_mutex
);
1938 down_write(&trace_event_mutex
);
1939 __trace_remove_event_dirs(tr
);
1940 debugfs_remove_recursive(tr
->event_dir
);
1941 up_write(&trace_event_mutex
);
1943 tr
->event_dir
= NULL
;
1945 mutex_unlock(&event_mutex
);
1950 static __init
int event_trace_enable(void)
1952 struct trace_array
*tr
= top_trace_array();
1953 struct ftrace_event_call
**iter
, *call
;
1954 char *buf
= bootup_event_buf
;
1958 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
1961 ret
= event_init(call
);
1963 list_add(&call
->list
, &ftrace_events
);
1967 * We need the top trace array to have a working set of trace
1968 * points at early init, before the debug files and directories
1969 * are created. Create the file entries now, and attach them
1970 * to the actual file dentries later.
1972 __trace_early_add_events(tr
);
1975 token
= strsep(&buf
, ",");
1982 ret
= ftrace_set_clr_event(tr
, token
, 1);
1984 pr_warn("Failed to enable trace event: %s\n", token
);
1987 trace_printk_start_comm();
1992 static __init
int event_trace_init(void)
1994 struct trace_array
*tr
;
1995 struct dentry
*d_tracer
;
1996 struct dentry
*entry
;
1999 tr
= top_trace_array();
2001 d_tracer
= tracing_init_dentry();
2005 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2006 tr
, &ftrace_avail_fops
);
2008 pr_warning("Could not create debugfs "
2009 "'available_events' entry\n");
2011 if (trace_define_common_fields())
2012 pr_warning("tracing: Failed to allocate common fields");
2014 ret
= early_event_add_tracer(d_tracer
, tr
);
2018 ret
= register_module_notifier(&trace_module_nb
);
2020 pr_warning("Failed to register trace events module notifier\n");
2024 core_initcall(event_trace_enable
);
2025 fs_initcall(event_trace_init
);
2027 #ifdef CONFIG_FTRACE_STARTUP_TEST
2029 static DEFINE_SPINLOCK(test_spinlock
);
2030 static DEFINE_SPINLOCK(test_spinlock_irq
);
2031 static DEFINE_MUTEX(test_mutex
);
2033 static __init
void test_work(struct work_struct
*dummy
)
2035 spin_lock(&test_spinlock
);
2036 spin_lock_irq(&test_spinlock_irq
);
2038 spin_unlock_irq(&test_spinlock_irq
);
2039 spin_unlock(&test_spinlock
);
2041 mutex_lock(&test_mutex
);
2043 mutex_unlock(&test_mutex
);
2046 static __init
int event_test_thread(void *unused
)
2050 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2052 pr_info("failed to kmalloc\n");
2054 schedule_on_each_cpu(test_work
);
2058 set_current_state(TASK_INTERRUPTIBLE
);
2059 while (!kthread_should_stop())
2066 * Do various things that may trigger events.
2068 static __init
void event_test_stuff(void)
2070 struct task_struct
*test_thread
;
2072 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2074 kthread_stop(test_thread
);
2078 * For every trace event defined, we will test each trace point separately,
2079 * and then by groups, and finally all trace points.
2081 static __init
void event_trace_self_tests(void)
2083 struct ftrace_subsystem_dir
*dir
;
2084 struct ftrace_event_file
*file
;
2085 struct ftrace_event_call
*call
;
2086 struct event_subsystem
*system
;
2087 struct trace_array
*tr
;
2090 tr
= top_trace_array();
2092 pr_info("Running tests on trace events:\n");
2094 list_for_each_entry(file
, &tr
->events
, list
) {
2096 call
= file
->event_call
;
2098 /* Only test those that have a probe */
2099 if (!call
->class || !call
->class->probe
)
2103 * Testing syscall events here is pretty useless, but
2104 * we still do it if configured. But this is time consuming.
2105 * What we really need is a user thread to perform the
2106 * syscalls as we test.
2108 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2109 if (call
->class->system
&&
2110 strcmp(call
->class->system
, "syscalls") == 0)
2114 pr_info("Testing event %s: ", call
->name
);
2117 * If an event is already enabled, someone is using
2118 * it and the self test should not be on.
2120 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2121 pr_warning("Enabled event during self test!\n");
2126 ftrace_event_enable_disable(file
, 1);
2128 ftrace_event_enable_disable(file
, 0);
2133 /* Now test at the sub system level */
2135 pr_info("Running tests on trace event systems:\n");
2137 list_for_each_entry(dir
, &tr
->systems
, list
) {
2139 system
= dir
->subsystem
;
2141 /* the ftrace system is special, skip it */
2142 if (strcmp(system
->name
, "ftrace") == 0)
2145 pr_info("Testing event system %s: ", system
->name
);
2147 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2148 if (WARN_ON_ONCE(ret
)) {
2149 pr_warning("error enabling system %s\n",
2156 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2157 if (WARN_ON_ONCE(ret
)) {
2158 pr_warning("error disabling system %s\n",
2166 /* Test with all events enabled */
2168 pr_info("Running tests on all trace events:\n");
2169 pr_info("Testing all events: ");
2171 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2172 if (WARN_ON_ONCE(ret
)) {
2173 pr_warning("error enabling all events\n");
2180 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2181 if (WARN_ON_ONCE(ret
)) {
2182 pr_warning("error disabling all events\n");
2189 #ifdef CONFIG_FUNCTION_TRACER
2191 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2194 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2195 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2197 struct ring_buffer_event
*event
;
2198 struct ring_buffer
*buffer
;
2199 struct ftrace_entry
*entry
;
2200 unsigned long flags
;
2205 pc
= preempt_count();
2206 preempt_disable_notrace();
2207 cpu
= raw_smp_processor_id();
2208 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2213 local_save_flags(flags
);
2215 event
= trace_current_buffer_lock_reserve(&buffer
,
2216 TRACE_FN
, sizeof(*entry
),
2220 entry
= ring_buffer_event_data(event
);
2222 entry
->parent_ip
= parent_ip
;
2224 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2227 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2228 preempt_enable_notrace();
2231 static struct ftrace_ops trace_ops __initdata
=
2233 .func
= function_test_events_call
,
2234 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2237 static __init
void event_trace_self_test_with_function(void)
2240 ret
= register_ftrace_function(&trace_ops
);
2241 if (WARN_ON(ret
< 0)) {
2242 pr_info("Failed to enable function tracer for event tests\n");
2245 pr_info("Running tests again, along with the function tracer\n");
2246 event_trace_self_tests();
2247 unregister_ftrace_function(&trace_ops
);
2250 static __init
void event_trace_self_test_with_function(void)
2255 static __init
int event_trace_self_tests_init(void)
2257 if (!tracing_selftest_disabled
) {
2258 event_trace_self_tests();
2259 event_trace_self_test_with_function();
2265 late_initcall(event_trace_self_tests_init
);