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/delay.h>
20 #include <asm/setup.h>
22 #include "trace_output.h"
25 #define TRACE_SYSTEM "TRACE_SYSTEM"
27 DEFINE_MUTEX(event_mutex
);
29 LIST_HEAD(ftrace_events
);
31 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
32 const char *name
, int offset
, int size
, int is_signed
,
35 struct ftrace_event_field
*field
;
37 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
41 field
->name
= kstrdup(name
, GFP_KERNEL
);
45 field
->type
= kstrdup(type
, GFP_KERNEL
);
49 if (filter_type
== FILTER_OTHER
)
50 field
->filter_type
= filter_assign_type(type
);
52 field
->filter_type
= filter_type
;
54 field
->offset
= offset
;
56 field
->is_signed
= is_signed
;
58 list_add(&field
->link
, &call
->fields
);
71 EXPORT_SYMBOL_GPL(trace_define_field
);
73 #define __common_field(type, item) \
74 ret = trace_define_field(call, #type, "common_" #item, \
75 offsetof(typeof(ent), item), \
77 is_signed_type(type), FILTER_OTHER); \
81 int trace_define_common_fields(struct ftrace_event_call
*call
)
84 struct trace_entry ent
;
86 __common_field(unsigned short, type
);
87 __common_field(unsigned char, flags
);
88 __common_field(unsigned char, preempt_count
);
89 __common_field(int, pid
);
90 __common_field(int, lock_depth
);
94 EXPORT_SYMBOL_GPL(trace_define_common_fields
);
98 static void trace_destroy_fields(struct ftrace_event_call
*call
)
100 struct ftrace_event_field
*field
, *next
;
102 list_for_each_entry_safe(field
, next
, &call
->fields
, link
) {
103 list_del(&field
->link
);
110 #endif /* CONFIG_MODULES */
112 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
119 tracing_stop_cmdline_record();
120 call
->unregfunc(call
->data
);
124 if (!call
->enabled
) {
126 tracing_start_cmdline_record();
127 call
->regfunc(call
->data
);
133 static void ftrace_clear_events(void)
135 struct ftrace_event_call
*call
;
137 mutex_lock(&event_mutex
);
138 list_for_each_entry(call
, &ftrace_events
, list
) {
139 ftrace_event_enable_disable(call
, 0);
141 mutex_unlock(&event_mutex
);
145 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
147 static int __ftrace_set_clr_event(const char *match
, const char *sub
,
148 const char *event
, int set
)
150 struct ftrace_event_call
*call
;
153 mutex_lock(&event_mutex
);
154 list_for_each_entry(call
, &ftrace_events
, list
) {
156 if (!call
->name
|| !call
->regfunc
)
160 strcmp(match
, call
->name
) != 0 &&
161 strcmp(match
, call
->system
) != 0)
164 if (sub
&& strcmp(sub
, call
->system
) != 0)
167 if (event
&& strcmp(event
, call
->name
) != 0)
170 ftrace_event_enable_disable(call
, set
);
174 mutex_unlock(&event_mutex
);
179 static int ftrace_set_clr_event(char *buf
, int set
)
181 char *event
= NULL
, *sub
= NULL
, *match
;
184 * The buf format can be <subsystem>:<event-name>
185 * *:<event-name> means any event by that name.
186 * :<event-name> is the same.
188 * <subsystem>:* means all events in that subsystem
189 * <subsystem>: means the same.
191 * <name> (no ':') means all events in a subsystem with
192 * the name <name> or any event that matches <name>
195 match
= strsep(&buf
, ":");
201 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
203 if (!strlen(event
) || strcmp(event
, "*") == 0)
207 return __ftrace_set_clr_event(match
, sub
, event
, set
);
211 * trace_set_clr_event - enable or disable an event
212 * @system: system name to match (NULL for any system)
213 * @event: event name to match (NULL for all events, within system)
214 * @set: 1 to enable, 0 to disable
216 * This is a way for other parts of the kernel to enable or disable
219 * Returns 0 on success, -EINVAL if the parameters do not match any
222 int trace_set_clr_event(const char *system
, const char *event
, int set
)
224 return __ftrace_set_clr_event(NULL
, system
, event
, set
);
227 /* 128 should be much more than enough */
228 #define EVENT_BUF_SIZE 127
231 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
232 size_t cnt
, loff_t
*ppos
)
234 struct trace_parser parser
;
240 ret
= tracing_update_buffers();
244 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
247 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
249 if (read
>= 0 && trace_parser_loaded((&parser
))) {
252 if (*parser
.buffer
== '!')
255 parser
.buffer
[parser
.idx
] = 0;
257 ret
= ftrace_set_clr_event(parser
.buffer
+ !set
, set
);
265 trace_parser_put(&parser
);
271 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
273 struct ftrace_event_call
*call
= v
;
277 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
279 * The ftrace subsystem is for showing formats only.
280 * They can not be enabled or disabled via the event files.
289 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
291 struct ftrace_event_call
*call
;
294 mutex_lock(&event_mutex
);
296 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
297 for (l
= 0; l
<= *pos
; ) {
298 call
= t_next(m
, call
, &l
);
306 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
308 struct ftrace_event_call
*call
= v
;
312 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
320 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
322 struct ftrace_event_call
*call
;
325 mutex_lock(&event_mutex
);
327 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
328 for (l
= 0; l
<= *pos
; ) {
329 call
= s_next(m
, call
, &l
);
336 static int t_show(struct seq_file
*m
, void *v
)
338 struct ftrace_event_call
*call
= v
;
340 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
341 seq_printf(m
, "%s:", call
->system
);
342 seq_printf(m
, "%s\n", call
->name
);
347 static void t_stop(struct seq_file
*m
, void *p
)
349 mutex_unlock(&event_mutex
);
353 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
355 const struct seq_operations
*seq_ops
;
357 if ((file
->f_mode
& FMODE_WRITE
) &&
358 (file
->f_flags
& O_TRUNC
))
359 ftrace_clear_events();
361 seq_ops
= inode
->i_private
;
362 return seq_open(file
, seq_ops
);
366 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
369 struct ftrace_event_call
*call
= filp
->private_data
;
377 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
381 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
384 struct ftrace_event_call
*call
= filp
->private_data
;
389 if (cnt
>= sizeof(buf
))
392 if (copy_from_user(&buf
, ubuf
, cnt
))
397 ret
= strict_strtoul(buf
, 10, &val
);
401 ret
= tracing_update_buffers();
408 mutex_lock(&event_mutex
);
409 ftrace_event_enable_disable(call
, val
);
410 mutex_unlock(&event_mutex
);
423 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
426 const char set_to_char
[4] = { '?', '0', '1', 'X' };
427 const char *system
= filp
->private_data
;
428 struct ftrace_event_call
*call
;
433 mutex_lock(&event_mutex
);
434 list_for_each_entry(call
, &ftrace_events
, list
) {
435 if (!call
->name
|| !call
->regfunc
)
438 if (system
&& strcmp(call
->system
, system
) != 0)
442 * We need to find out if all the events are set
443 * or if all events or cleared, or if we have
446 set
|= (1 << !!call
->enabled
);
449 * If we have a mixture, no need to look further.
454 mutex_unlock(&event_mutex
);
456 buf
[0] = set_to_char
[set
];
459 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
465 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
468 const char *system
= filp
->private_data
;
473 if (cnt
>= sizeof(buf
))
476 if (copy_from_user(&buf
, ubuf
, cnt
))
481 ret
= strict_strtoul(buf
, 10, &val
);
485 ret
= tracing_update_buffers();
489 if (val
!= 0 && val
!= 1)
492 ret
= __ftrace_set_clr_event(NULL
, system
, NULL
, val
);
504 extern char *__bad_type_size(void);
507 #define FIELD(type, name) \
508 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
509 #type, "common_" #name, offsetof(typeof(field), name), \
512 static int trace_write_header(struct trace_seq
*s
)
514 struct trace_entry field
;
516 /* struct trace_entry */
517 return trace_seq_printf(s
,
518 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
519 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
520 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
521 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
522 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
524 FIELD(unsigned short, type
),
525 FIELD(unsigned char, flags
),
526 FIELD(unsigned char, preempt_count
),
528 FIELD(int, lock_depth
));
532 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
535 struct ftrace_event_call
*call
= filp
->private_data
;
543 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
549 /* If any of the first writes fail, so will the show_format. */
551 trace_seq_printf(s
, "name: %s\n", call
->name
);
552 trace_seq_printf(s
, "ID: %d\n", call
->id
);
553 trace_seq_printf(s
, "format:\n");
554 trace_write_header(s
);
556 r
= call
->show_format(call
, s
);
559 * ug! The format output is bigger than a PAGE!!
561 buf
= "FORMAT TOO BIG\n";
562 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
567 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
575 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
577 struct ftrace_event_call
*call
= filp
->private_data
;
584 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
589 trace_seq_printf(s
, "%d\n", call
->id
);
591 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
598 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
601 struct ftrace_event_call
*call
= filp
->private_data
;
608 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
614 print_event_filter(call
, s
);
615 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
623 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
626 struct ftrace_event_call
*call
= filp
->private_data
;
630 if (cnt
>= PAGE_SIZE
)
633 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
637 if (copy_from_user(buf
, ubuf
, cnt
)) {
638 free_page((unsigned long) buf
);
643 err
= apply_event_filter(call
, buf
);
644 free_page((unsigned long) buf
);
654 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
657 struct event_subsystem
*system
= filp
->private_data
;
664 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
670 print_subsystem_event_filter(system
, s
);
671 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
679 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
682 struct event_subsystem
*system
= filp
->private_data
;
686 if (cnt
>= PAGE_SIZE
)
689 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
693 if (copy_from_user(buf
, ubuf
, cnt
)) {
694 free_page((unsigned long) buf
);
699 err
= apply_subsystem_event_filter(system
, buf
);
700 free_page((unsigned long) buf
);
710 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
712 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
719 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
726 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
733 static const struct seq_operations show_event_seq_ops
= {
740 static const struct seq_operations show_set_event_seq_ops
= {
747 static const struct file_operations ftrace_avail_fops
= {
748 .open
= ftrace_event_seq_open
,
751 .release
= seq_release
,
754 static const struct file_operations ftrace_set_event_fops
= {
755 .open
= ftrace_event_seq_open
,
757 .write
= ftrace_event_write
,
759 .release
= seq_release
,
762 static const struct file_operations ftrace_enable_fops
= {
763 .open
= tracing_open_generic
,
764 .read
= event_enable_read
,
765 .write
= event_enable_write
,
768 static const struct file_operations ftrace_event_format_fops
= {
769 .open
= tracing_open_generic
,
770 .read
= event_format_read
,
773 static const struct file_operations ftrace_event_id_fops
= {
774 .open
= tracing_open_generic
,
775 .read
= event_id_read
,
778 static const struct file_operations ftrace_event_filter_fops
= {
779 .open
= tracing_open_generic
,
780 .read
= event_filter_read
,
781 .write
= event_filter_write
,
784 static const struct file_operations ftrace_subsystem_filter_fops
= {
785 .open
= tracing_open_generic
,
786 .read
= subsystem_filter_read
,
787 .write
= subsystem_filter_write
,
790 static const struct file_operations ftrace_system_enable_fops
= {
791 .open
= tracing_open_generic
,
792 .read
= system_enable_read
,
793 .write
= system_enable_write
,
796 static const struct file_operations ftrace_show_header_fops
= {
797 .open
= tracing_open_generic
,
801 static struct dentry
*event_trace_events_dir(void)
803 static struct dentry
*d_tracer
;
804 static struct dentry
*d_events
;
809 d_tracer
= tracing_init_dentry();
813 d_events
= debugfs_create_dir("events", d_tracer
);
815 pr_warning("Could not create debugfs "
816 "'events' directory\n");
821 static LIST_HEAD(event_subsystems
);
823 static struct dentry
*
824 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
826 struct event_subsystem
*system
;
827 struct dentry
*entry
;
829 /* First see if we did not already create this dir */
830 list_for_each_entry(system
, &event_subsystems
, list
) {
831 if (strcmp(system
->name
, name
) == 0) {
833 return system
->entry
;
837 /* need to create new entry */
838 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
840 pr_warning("No memory to create event subsystem %s\n",
845 system
->entry
= debugfs_create_dir(name
, d_events
);
846 if (!system
->entry
) {
847 pr_warning("Could not create event subsystem %s\n",
853 system
->nr_events
= 1;
854 system
->name
= kstrdup(name
, GFP_KERNEL
);
856 debugfs_remove(system
->entry
);
861 list_add(&system
->list
, &event_subsystems
);
863 system
->filter
= NULL
;
865 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
866 if (!system
->filter
) {
867 pr_warning("Could not allocate filter for subsystem "
869 return system
->entry
;
872 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
873 &ftrace_subsystem_filter_fops
);
875 kfree(system
->filter
);
876 system
->filter
= NULL
;
877 pr_warning("Could not create debugfs "
878 "'%s/filter' entry\n", name
);
881 entry
= trace_create_file("enable", 0644, system
->entry
,
882 (void *)system
->name
,
883 &ftrace_system_enable_fops
);
885 return system
->entry
;
889 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
,
890 const struct file_operations
*id
,
891 const struct file_operations
*enable
,
892 const struct file_operations
*filter
,
893 const struct file_operations
*format
)
895 struct dentry
*entry
;
899 * If the trace point header did not define TRACE_SYSTEM
900 * then the system would be called "TRACE_SYSTEM".
902 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
903 d_events
= event_subsystem_dir(call
->system
, d_events
);
905 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
907 pr_warning("Could not create debugfs "
908 "'%s' directory\n", call
->name
);
913 entry
= trace_create_file("enable", 0644, call
->dir
, call
,
916 if (call
->id
&& call
->profile_enable
)
917 entry
= trace_create_file("id", 0444, call
->dir
, call
,
920 if (call
->define_fields
) {
921 ret
= call
->define_fields(call
);
923 pr_warning("Could not initialize trace point"
924 " events/%s\n", call
->name
);
927 entry
= trace_create_file("filter", 0644, call
->dir
, call
,
931 /* A trace may not want to export its format */
932 if (!call
->show_format
)
935 entry
= trace_create_file("format", 0444, call
->dir
, call
,
941 #define for_each_event(event, start, end) \
942 for (event = start; \
943 (unsigned long)event < (unsigned long)end; \
946 #ifdef CONFIG_MODULES
948 static LIST_HEAD(ftrace_module_file_list
);
951 * Modules must own their file_operations to keep up with
952 * reference counting.
954 struct ftrace_module_file_ops
{
955 struct list_head list
;
957 struct file_operations id
;
958 struct file_operations enable
;
959 struct file_operations format
;
960 struct file_operations filter
;
963 static void remove_subsystem_dir(const char *name
)
965 struct event_subsystem
*system
;
967 if (strcmp(name
, TRACE_SYSTEM
) == 0)
970 list_for_each_entry(system
, &event_subsystems
, list
) {
971 if (strcmp(system
->name
, name
) == 0) {
972 if (!--system
->nr_events
) {
973 struct event_filter
*filter
= system
->filter
;
975 debugfs_remove_recursive(system
->entry
);
976 list_del(&system
->list
);
978 kfree(filter
->filter_string
);
989 static struct ftrace_module_file_ops
*
990 trace_create_file_ops(struct module
*mod
)
992 struct ftrace_module_file_ops
*file_ops
;
995 * This is a bit of a PITA. To allow for correct reference
996 * counting, modules must "own" their file_operations.
997 * To do this, we allocate the file operations that will be
998 * used in the event directory.
1001 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1005 file_ops
->mod
= mod
;
1007 file_ops
->id
= ftrace_event_id_fops
;
1008 file_ops
->id
.owner
= mod
;
1010 file_ops
->enable
= ftrace_enable_fops
;
1011 file_ops
->enable
.owner
= mod
;
1013 file_ops
->filter
= ftrace_event_filter_fops
;
1014 file_ops
->filter
.owner
= mod
;
1016 file_ops
->format
= ftrace_event_format_fops
;
1017 file_ops
->format
.owner
= mod
;
1019 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1024 static void trace_module_add_events(struct module
*mod
)
1026 struct ftrace_module_file_ops
*file_ops
= NULL
;
1027 struct ftrace_event_call
*call
, *start
, *end
;
1028 struct dentry
*d_events
;
1031 start
= mod
->trace_events
;
1032 end
= mod
->trace_events
+ mod
->num_trace_events
;
1037 d_events
= event_trace_events_dir();
1041 for_each_event(call
, start
, end
) {
1042 /* The linker may leave blanks */
1045 if (call
->raw_init
) {
1046 ret
= call
->raw_init();
1049 pr_warning("Could not initialize trace "
1050 "point events/%s\n", call
->name
);
1055 * This module has events, create file ops for this module
1056 * if not already done.
1059 file_ops
= trace_create_file_ops(mod
);
1064 list_add(&call
->list
, &ftrace_events
);
1065 event_create_dir(call
, d_events
,
1066 &file_ops
->id
, &file_ops
->enable
,
1067 &file_ops
->filter
, &file_ops
->format
);
1071 static void trace_module_remove_events(struct module
*mod
)
1073 struct ftrace_module_file_ops
*file_ops
;
1074 struct ftrace_event_call
*call
, *p
;
1077 down_write(&trace_event_mutex
);
1078 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1079 if (call
->mod
== mod
) {
1081 ftrace_event_enable_disable(call
, 0);
1083 __unregister_ftrace_event(call
->event
);
1084 debugfs_remove_recursive(call
->dir
);
1085 list_del(&call
->list
);
1086 trace_destroy_fields(call
);
1087 destroy_preds(call
);
1088 remove_subsystem_dir(call
->system
);
1092 /* Now free the file_operations */
1093 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1094 if (file_ops
->mod
== mod
)
1097 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1098 list_del(&file_ops
->list
);
1103 * It is safest to reset the ring buffer if the module being unloaded
1104 * registered any events.
1107 tracing_reset_current_online_cpus();
1108 up_write(&trace_event_mutex
);
1111 static int trace_module_notify(struct notifier_block
*self
,
1112 unsigned long val
, void *data
)
1114 struct module
*mod
= data
;
1116 mutex_lock(&event_mutex
);
1118 case MODULE_STATE_COMING
:
1119 trace_module_add_events(mod
);
1121 case MODULE_STATE_GOING
:
1122 trace_module_remove_events(mod
);
1125 mutex_unlock(&event_mutex
);
1130 static int trace_module_notify(struct notifier_block
*self
,
1131 unsigned long val
, void *data
)
1135 #endif /* CONFIG_MODULES */
1137 static struct notifier_block trace_module_nb
= {
1138 .notifier_call
= trace_module_notify
,
1142 extern struct ftrace_event_call __start_ftrace_events
[];
1143 extern struct ftrace_event_call __stop_ftrace_events
[];
1145 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
1147 static __init
int setup_trace_event(char *str
)
1149 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
1150 ring_buffer_expanded
= 1;
1151 tracing_selftest_disabled
= 1;
1155 __setup("trace_event=", setup_trace_event
);
1157 static __init
int event_trace_init(void)
1159 struct ftrace_event_call
*call
;
1160 struct dentry
*d_tracer
;
1161 struct dentry
*entry
;
1162 struct dentry
*d_events
;
1164 char *buf
= bootup_event_buf
;
1167 d_tracer
= tracing_init_dentry();
1171 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
1172 (void *)&show_event_seq_ops
,
1173 &ftrace_avail_fops
);
1175 pr_warning("Could not create debugfs "
1176 "'available_events' entry\n");
1178 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
1179 (void *)&show_set_event_seq_ops
,
1180 &ftrace_set_event_fops
);
1182 pr_warning("Could not create debugfs "
1183 "'set_event' entry\n");
1185 d_events
= event_trace_events_dir();
1189 /* ring buffer internal formats */
1190 trace_create_file("header_page", 0444, d_events
,
1191 ring_buffer_print_page_header
,
1192 &ftrace_show_header_fops
);
1194 trace_create_file("header_event", 0444, d_events
,
1195 ring_buffer_print_entry_header
,
1196 &ftrace_show_header_fops
);
1198 trace_create_file("enable", 0644, d_events
,
1199 NULL
, &ftrace_system_enable_fops
);
1201 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
1202 /* The linker may leave blanks */
1205 if (call
->raw_init
) {
1206 ret
= call
->raw_init();
1209 pr_warning("Could not initialize trace "
1210 "point events/%s\n", call
->name
);
1214 list_add(&call
->list
, &ftrace_events
);
1215 event_create_dir(call
, d_events
, &ftrace_event_id_fops
,
1216 &ftrace_enable_fops
, &ftrace_event_filter_fops
,
1217 &ftrace_event_format_fops
);
1221 token
= strsep(&buf
, ",");
1228 ret
= ftrace_set_clr_event(token
, 1);
1230 pr_warning("Failed to enable trace event: %s\n", token
);
1233 ret
= register_module_notifier(&trace_module_nb
);
1235 pr_warning("Failed to register trace events module notifier\n");
1239 fs_initcall(event_trace_init
);
1241 #ifdef CONFIG_FTRACE_STARTUP_TEST
1243 static DEFINE_SPINLOCK(test_spinlock
);
1244 static DEFINE_SPINLOCK(test_spinlock_irq
);
1245 static DEFINE_MUTEX(test_mutex
);
1247 static __init
void test_work(struct work_struct
*dummy
)
1249 spin_lock(&test_spinlock
);
1250 spin_lock_irq(&test_spinlock_irq
);
1252 spin_unlock_irq(&test_spinlock_irq
);
1253 spin_unlock(&test_spinlock
);
1255 mutex_lock(&test_mutex
);
1257 mutex_unlock(&test_mutex
);
1260 static __init
int event_test_thread(void *unused
)
1264 test_malloc
= kmalloc(1234, GFP_KERNEL
);
1266 pr_info("failed to kmalloc\n");
1268 schedule_on_each_cpu(test_work
);
1272 set_current_state(TASK_INTERRUPTIBLE
);
1273 while (!kthread_should_stop())
1280 * Do various things that may trigger events.
1282 static __init
void event_test_stuff(void)
1284 struct task_struct
*test_thread
;
1286 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
1288 kthread_stop(test_thread
);
1292 * For every trace event defined, we will test each trace point separately,
1293 * and then by groups, and finally all trace points.
1295 static __init
void event_trace_self_tests(void)
1297 struct ftrace_event_call
*call
;
1298 struct event_subsystem
*system
;
1301 pr_info("Running tests on trace events:\n");
1303 list_for_each_entry(call
, &ftrace_events
, list
) {
1305 /* Only test those that have a regfunc */
1310 * Testing syscall events here is pretty useless, but
1311 * we still do it if configured. But this is time consuming.
1312 * What we really need is a user thread to perform the
1313 * syscalls as we test.
1315 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1317 strcmp(call
->system
, "syscalls") == 0)
1321 pr_info("Testing event %s: ", call
->name
);
1324 * If an event is already enabled, someone is using
1325 * it and the self test should not be on.
1327 if (call
->enabled
) {
1328 pr_warning("Enabled event during self test!\n");
1333 ftrace_event_enable_disable(call
, 1);
1335 ftrace_event_enable_disable(call
, 0);
1340 /* Now test at the sub system level */
1342 pr_info("Running tests on trace event systems:\n");
1344 list_for_each_entry(system
, &event_subsystems
, list
) {
1346 /* the ftrace system is special, skip it */
1347 if (strcmp(system
->name
, "ftrace") == 0)
1350 pr_info("Testing event system %s: ", system
->name
);
1352 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 1);
1353 if (WARN_ON_ONCE(ret
)) {
1354 pr_warning("error enabling system %s\n",
1361 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 0);
1362 if (WARN_ON_ONCE(ret
))
1363 pr_warning("error disabling system %s\n",
1369 /* Test with all events enabled */
1371 pr_info("Running tests on all trace events:\n");
1372 pr_info("Testing all events: ");
1374 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 1);
1375 if (WARN_ON_ONCE(ret
)) {
1376 pr_warning("error enabling all events\n");
1383 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 0);
1384 if (WARN_ON_ONCE(ret
)) {
1385 pr_warning("error disabling all events\n");
1392 #ifdef CONFIG_FUNCTION_TRACER
1394 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
1397 function_test_events_call(unsigned long ip
, unsigned long parent_ip
)
1399 struct ring_buffer_event
*event
;
1400 struct ring_buffer
*buffer
;
1401 struct ftrace_entry
*entry
;
1402 unsigned long flags
;
1408 pc
= preempt_count();
1409 resched
= ftrace_preempt_disable();
1410 cpu
= raw_smp_processor_id();
1411 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
1416 local_save_flags(flags
);
1418 event
= trace_current_buffer_lock_reserve(&buffer
,
1419 TRACE_FN
, sizeof(*entry
),
1423 entry
= ring_buffer_event_data(event
);
1425 entry
->parent_ip
= parent_ip
;
1427 trace_nowake_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1430 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
1431 ftrace_preempt_enable(resched
);
1434 static struct ftrace_ops trace_ops __initdata
=
1436 .func
= function_test_events_call
,
1439 static __init
void event_trace_self_test_with_function(void)
1441 register_ftrace_function(&trace_ops
);
1442 pr_info("Running tests again, along with the function tracer\n");
1443 event_trace_self_tests();
1444 unregister_ftrace_function(&trace_ops
);
1447 static __init
void event_trace_self_test_with_function(void)
1452 static __init
int event_trace_self_tests_init(void)
1454 if (!tracing_selftest_disabled
) {
1455 event_trace_self_tests();
1456 event_trace_self_test_with_function();
1462 late_initcall(event_trace_self_tests_init
);