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 "trace_output.h"
22 #define TRACE_SYSTEM "TRACE_SYSTEM"
24 DEFINE_MUTEX(event_mutex
);
26 LIST_HEAD(ftrace_events
);
28 int trace_define_field(struct ftrace_event_call
*call
, char *type
,
29 char *name
, int offset
, int size
, int is_signed
)
31 struct ftrace_event_field
*field
;
33 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
37 field
->name
= kstrdup(name
, GFP_KERNEL
);
41 field
->type
= kstrdup(type
, GFP_KERNEL
);
45 field
->offset
= offset
;
47 field
->is_signed
= is_signed
;
48 list_add(&field
->link
, &call
->fields
);
61 EXPORT_SYMBOL_GPL(trace_define_field
);
65 static void trace_destroy_fields(struct ftrace_event_call
*call
)
67 struct ftrace_event_field
*field
, *next
;
69 list_for_each_entry_safe(field
, next
, &call
->fields
, link
) {
70 list_del(&field
->link
);
77 #endif /* CONFIG_MODULES */
79 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
86 tracing_stop_cmdline_record();
93 tracing_start_cmdline_record();
100 static void ftrace_clear_events(void)
102 struct ftrace_event_call
*call
;
104 mutex_lock(&event_mutex
);
105 list_for_each_entry(call
, &ftrace_events
, list
) {
106 ftrace_event_enable_disable(call
, 0);
108 mutex_unlock(&event_mutex
);
112 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
114 static int __ftrace_set_clr_event(const char *match
, const char *sub
,
115 const char *event
, int set
)
117 struct ftrace_event_call
*call
;
120 mutex_lock(&event_mutex
);
121 list_for_each_entry(call
, &ftrace_events
, list
) {
123 if (!call
->name
|| !call
->regfunc
)
127 strcmp(match
, call
->name
) != 0 &&
128 strcmp(match
, call
->system
) != 0)
131 if (sub
&& strcmp(sub
, call
->system
) != 0)
134 if (event
&& strcmp(event
, call
->name
) != 0)
137 ftrace_event_enable_disable(call
, set
);
141 mutex_unlock(&event_mutex
);
146 static int ftrace_set_clr_event(char *buf
, int set
)
148 char *event
= NULL
, *sub
= NULL
, *match
;
151 * The buf format can be <subsystem>:<event-name>
152 * *:<event-name> means any event by that name.
153 * :<event-name> is the same.
155 * <subsystem>:* means all events in that subsystem
156 * <subsystem>: means the same.
158 * <name> (no ':') means all events in a subsystem with
159 * the name <name> or any event that matches <name>
162 match
= strsep(&buf
, ":");
168 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
170 if (!strlen(event
) || strcmp(event
, "*") == 0)
174 return __ftrace_set_clr_event(match
, sub
, event
, set
);
178 * trace_set_clr_event - enable or disable an event
179 * @system: system name to match (NULL for any system)
180 * @event: event name to match (NULL for all events, within system)
181 * @set: 1 to enable, 0 to disable
183 * This is a way for other parts of the kernel to enable or disable
186 * Returns 0 on success, -EINVAL if the parameters do not match any
189 int trace_set_clr_event(const char *system
, const char *event
, int set
)
191 return __ftrace_set_clr_event(NULL
, system
, event
, set
);
194 /* 128 should be much more than enough */
195 #define EVENT_BUF_SIZE 127
198 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
199 size_t cnt
, loff_t
*ppos
)
210 ret
= tracing_update_buffers();
214 ret
= get_user(ch
, ubuf
++);
220 /* skip white space */
221 while (cnt
&& isspace(ch
)) {
222 ret
= get_user(ch
, ubuf
++);
229 /* Only white space found? */
236 buf
= kmalloc(EVENT_BUF_SIZE
+1, GFP_KERNEL
);
240 if (cnt
> EVENT_BUF_SIZE
)
241 cnt
= EVENT_BUF_SIZE
;
244 while (cnt
&& !isspace(ch
)) {
250 ret
= get_user(ch
, ubuf
++);
260 ret
= ftrace_set_clr_event(buf
, set
);
273 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
275 struct list_head
*list
= m
->private;
276 struct ftrace_event_call
*call
;
281 if (list
== &ftrace_events
)
284 call
= list_entry(list
, struct ftrace_event_call
, list
);
287 * The ftrace subsystem is for showing formats only.
288 * They can not be enabled or disabled via the event files.
296 m
->private = list
->next
;
301 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
303 struct ftrace_event_call
*call
= NULL
;
306 mutex_lock(&event_mutex
);
308 m
->private = ftrace_events
.next
;
309 for (l
= 0; l
<= *pos
; ) {
310 call
= t_next(m
, NULL
, &l
);
318 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
320 struct list_head
*list
= m
->private;
321 struct ftrace_event_call
*call
;
326 if (list
== &ftrace_events
)
329 call
= list_entry(list
, struct ftrace_event_call
, list
);
331 if (!call
->enabled
) {
336 m
->private = list
->next
;
341 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
343 struct ftrace_event_call
*call
= NULL
;
346 mutex_lock(&event_mutex
);
348 m
->private = ftrace_events
.next
;
349 for (l
= 0; l
<= *pos
; ) {
350 call
= s_next(m
, NULL
, &l
);
357 static int t_show(struct seq_file
*m
, void *v
)
359 struct ftrace_event_call
*call
= v
;
361 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
362 seq_printf(m
, "%s:", call
->system
);
363 seq_printf(m
, "%s\n", call
->name
);
368 static void t_stop(struct seq_file
*m
, void *p
)
370 mutex_unlock(&event_mutex
);
374 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
376 const struct seq_operations
*seq_ops
;
378 if ((file
->f_mode
& FMODE_WRITE
) &&
379 !(file
->f_flags
& O_APPEND
))
380 ftrace_clear_events();
382 seq_ops
= inode
->i_private
;
383 return seq_open(file
, seq_ops
);
387 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
390 struct ftrace_event_call
*call
= filp
->private_data
;
398 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
402 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
405 struct ftrace_event_call
*call
= filp
->private_data
;
410 if (cnt
>= sizeof(buf
))
413 if (copy_from_user(&buf
, ubuf
, cnt
))
418 ret
= strict_strtoul(buf
, 10, &val
);
422 ret
= tracing_update_buffers();
429 mutex_lock(&event_mutex
);
430 ftrace_event_enable_disable(call
, val
);
431 mutex_unlock(&event_mutex
);
444 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
447 const char set_to_char
[4] = { '?', '0', '1', 'X' };
448 const char *system
= filp
->private_data
;
449 struct ftrace_event_call
*call
;
454 mutex_lock(&event_mutex
);
455 list_for_each_entry(call
, &ftrace_events
, list
) {
456 if (!call
->name
|| !call
->regfunc
)
459 if (system
&& strcmp(call
->system
, system
) != 0)
463 * We need to find out if all the events are set
464 * or if all events or cleared, or if we have
467 set
|= (1 << !!call
->enabled
);
470 * If we have a mixture, no need to look further.
475 mutex_unlock(&event_mutex
);
477 buf
[0] = set_to_char
[set
];
480 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
486 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
489 const char *system
= filp
->private_data
;
494 if (cnt
>= sizeof(buf
))
497 if (copy_from_user(&buf
, ubuf
, cnt
))
502 ret
= strict_strtoul(buf
, 10, &val
);
506 ret
= tracing_update_buffers();
510 if (val
!= 0 && val
!= 1)
513 ret
= __ftrace_set_clr_event(NULL
, system
, NULL
, val
);
525 extern char *__bad_type_size(void);
528 #define FIELD(type, name) \
529 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
530 #type, "common_" #name, offsetof(typeof(field), name), \
533 static int trace_write_header(struct trace_seq
*s
)
535 struct trace_entry field
;
537 /* struct trace_entry */
538 return trace_seq_printf(s
,
539 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
540 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
541 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
542 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
543 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
545 FIELD(unsigned short, type
),
546 FIELD(unsigned char, flags
),
547 FIELD(unsigned char, preempt_count
),
553 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
556 struct ftrace_event_call
*call
= filp
->private_data
;
564 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
570 /* If any of the first writes fail, so will the show_format. */
572 trace_seq_printf(s
, "name: %s\n", call
->name
);
573 trace_seq_printf(s
, "ID: %d\n", call
->id
);
574 trace_seq_printf(s
, "format:\n");
575 trace_write_header(s
);
577 r
= call
->show_format(s
);
580 * ug! The format output is bigger than a PAGE!!
582 buf
= "FORMAT TOO BIG\n";
583 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
588 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
596 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
598 struct ftrace_event_call
*call
= filp
->private_data
;
605 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
610 trace_seq_printf(s
, "%d\n", call
->id
);
612 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
619 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
622 struct ftrace_event_call
*call
= filp
->private_data
;
629 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
635 print_event_filter(call
, s
);
636 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
644 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
647 struct ftrace_event_call
*call
= filp
->private_data
;
651 if (cnt
>= PAGE_SIZE
)
654 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
658 if (copy_from_user(buf
, ubuf
, cnt
)) {
659 free_page((unsigned long) buf
);
664 err
= apply_event_filter(call
, buf
);
665 free_page((unsigned long) buf
);
675 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
678 struct event_subsystem
*system
= filp
->private_data
;
685 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
691 print_subsystem_event_filter(system
, s
);
692 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
700 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
703 struct event_subsystem
*system
= filp
->private_data
;
707 if (cnt
>= PAGE_SIZE
)
710 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
714 if (copy_from_user(buf
, ubuf
, cnt
)) {
715 free_page((unsigned long) buf
);
720 err
= apply_subsystem_event_filter(system
, buf
);
721 free_page((unsigned long) buf
);
731 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
733 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
740 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
747 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
754 static const struct seq_operations show_event_seq_ops
= {
761 static const struct seq_operations show_set_event_seq_ops
= {
768 static const struct file_operations ftrace_avail_fops
= {
769 .open
= ftrace_event_seq_open
,
772 .release
= seq_release
,
775 static const struct file_operations ftrace_set_event_fops
= {
776 .open
= ftrace_event_seq_open
,
778 .write
= ftrace_event_write
,
780 .release
= seq_release
,
783 static const struct file_operations ftrace_enable_fops
= {
784 .open
= tracing_open_generic
,
785 .read
= event_enable_read
,
786 .write
= event_enable_write
,
789 static const struct file_operations ftrace_event_format_fops
= {
790 .open
= tracing_open_generic
,
791 .read
= event_format_read
,
794 static const struct file_operations ftrace_event_id_fops
= {
795 .open
= tracing_open_generic
,
796 .read
= event_id_read
,
799 static const struct file_operations ftrace_event_filter_fops
= {
800 .open
= tracing_open_generic
,
801 .read
= event_filter_read
,
802 .write
= event_filter_write
,
805 static const struct file_operations ftrace_subsystem_filter_fops
= {
806 .open
= tracing_open_generic
,
807 .read
= subsystem_filter_read
,
808 .write
= subsystem_filter_write
,
811 static const struct file_operations ftrace_system_enable_fops
= {
812 .open
= tracing_open_generic
,
813 .read
= system_enable_read
,
814 .write
= system_enable_write
,
817 static const struct file_operations ftrace_show_header_fops
= {
818 .open
= tracing_open_generic
,
822 static struct dentry
*event_trace_events_dir(void)
824 static struct dentry
*d_tracer
;
825 static struct dentry
*d_events
;
830 d_tracer
= tracing_init_dentry();
834 d_events
= debugfs_create_dir("events", d_tracer
);
836 pr_warning("Could not create debugfs "
837 "'events' directory\n");
842 static LIST_HEAD(event_subsystems
);
844 static struct dentry
*
845 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
847 struct event_subsystem
*system
;
848 struct dentry
*entry
;
850 /* First see if we did not already create this dir */
851 list_for_each_entry(system
, &event_subsystems
, list
) {
852 if (strcmp(system
->name
, name
) == 0)
853 return system
->entry
;
856 /* need to create new entry */
857 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
859 pr_warning("No memory to create event subsystem %s\n",
864 system
->entry
= debugfs_create_dir(name
, d_events
);
865 if (!system
->entry
) {
866 pr_warning("Could not create event subsystem %s\n",
872 system
->name
= kstrdup(name
, GFP_KERNEL
);
874 debugfs_remove(system
->entry
);
879 list_add(&system
->list
, &event_subsystems
);
881 system
->filter
= NULL
;
883 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
884 if (!system
->filter
) {
885 pr_warning("Could not allocate filter for subsystem "
887 return system
->entry
;
890 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
891 &ftrace_subsystem_filter_fops
);
893 kfree(system
->filter
);
894 system
->filter
= NULL
;
895 pr_warning("Could not create debugfs "
896 "'%s/filter' entry\n", name
);
899 entry
= trace_create_file("enable", 0644, system
->entry
,
900 (void *)system
->name
,
901 &ftrace_system_enable_fops
);
903 return system
->entry
;
907 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
,
908 const struct file_operations
*id
,
909 const struct file_operations
*enable
,
910 const struct file_operations
*filter
,
911 const struct file_operations
*format
)
913 struct dentry
*entry
;
917 * If the trace point header did not define TRACE_SYSTEM
918 * then the system would be called "TRACE_SYSTEM".
920 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
921 d_events
= event_subsystem_dir(call
->system
, d_events
);
923 if (call
->raw_init
) {
924 ret
= call
->raw_init();
926 pr_warning("Could not initialize trace point"
927 " events/%s\n", call
->name
);
932 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
934 pr_warning("Could not create debugfs "
935 "'%s' directory\n", call
->name
);
940 entry
= trace_create_file("enable", 0644, call
->dir
, call
,
944 entry
= trace_create_file("id", 0444, call
->dir
, call
,
947 if (call
->define_fields
) {
948 ret
= call
->define_fields();
950 pr_warning("Could not initialize trace point"
951 " events/%s\n", call
->name
);
954 entry
= trace_create_file("filter", 0644, call
->dir
, call
,
958 /* A trace may not want to export its format */
959 if (!call
->show_format
)
962 entry
= trace_create_file("format", 0444, call
->dir
, call
,
968 #define for_each_event(event, start, end) \
969 for (event = start; \
970 (unsigned long)event < (unsigned long)end; \
973 #ifdef CONFIG_MODULES
975 static LIST_HEAD(ftrace_module_file_list
);
978 * Modules must own their file_operations to keep up with
979 * reference counting.
981 struct ftrace_module_file_ops
{
982 struct list_head list
;
984 struct file_operations id
;
985 struct file_operations enable
;
986 struct file_operations format
;
987 struct file_operations filter
;
990 static struct ftrace_module_file_ops
*
991 trace_create_file_ops(struct module
*mod
)
993 struct ftrace_module_file_ops
*file_ops
;
996 * This is a bit of a PITA. To allow for correct reference
997 * counting, modules must "own" their file_operations.
998 * To do this, we allocate the file operations that will be
999 * used in the event directory.
1002 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1006 file_ops
->mod
= mod
;
1008 file_ops
->id
= ftrace_event_id_fops
;
1009 file_ops
->id
.owner
= mod
;
1011 file_ops
->enable
= ftrace_enable_fops
;
1012 file_ops
->enable
.owner
= mod
;
1014 file_ops
->filter
= ftrace_event_filter_fops
;
1015 file_ops
->filter
.owner
= mod
;
1017 file_ops
->format
= ftrace_event_format_fops
;
1018 file_ops
->format
.owner
= mod
;
1020 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1025 static void trace_module_add_events(struct module
*mod
)
1027 struct ftrace_module_file_ops
*file_ops
= NULL
;
1028 struct ftrace_event_call
*call
, *start
, *end
;
1029 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 */
1047 * This module has events, create file ops for this module
1048 * if not already done.
1051 file_ops
= trace_create_file_ops(mod
);
1056 list_add(&call
->list
, &ftrace_events
);
1057 event_create_dir(call
, d_events
,
1058 &file_ops
->id
, &file_ops
->enable
,
1059 &file_ops
->filter
, &file_ops
->format
);
1063 static void trace_module_remove_events(struct module
*mod
)
1065 struct ftrace_module_file_ops
*file_ops
;
1066 struct ftrace_event_call
*call
, *p
;
1069 down_write(&trace_event_mutex
);
1070 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1071 if (call
->mod
== mod
) {
1073 ftrace_event_enable_disable(call
, 0);
1075 __unregister_ftrace_event(call
->event
);
1076 debugfs_remove_recursive(call
->dir
);
1077 list_del(&call
->list
);
1078 trace_destroy_fields(call
);
1079 destroy_preds(call
);
1083 /* Now free the file_operations */
1084 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1085 if (file_ops
->mod
== mod
)
1088 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1089 list_del(&file_ops
->list
);
1094 * It is safest to reset the ring buffer if the module being unloaded
1095 * registered any events.
1098 tracing_reset_current_online_cpus();
1099 up_write(&trace_event_mutex
);
1102 static int trace_module_notify(struct notifier_block
*self
,
1103 unsigned long val
, void *data
)
1105 struct module
*mod
= data
;
1107 mutex_lock(&event_mutex
);
1109 case MODULE_STATE_COMING
:
1110 trace_module_add_events(mod
);
1112 case MODULE_STATE_GOING
:
1113 trace_module_remove_events(mod
);
1116 mutex_unlock(&event_mutex
);
1121 static int trace_module_notify(struct notifier_block
*self
,
1122 unsigned long val
, void *data
)
1126 #endif /* CONFIG_MODULES */
1128 struct notifier_block trace_module_nb
= {
1129 .notifier_call
= trace_module_notify
,
1133 extern struct ftrace_event_call __start_ftrace_events
[];
1134 extern struct ftrace_event_call __stop_ftrace_events
[];
1136 static __init
int event_trace_init(void)
1138 struct ftrace_event_call
*call
;
1139 struct dentry
*d_tracer
;
1140 struct dentry
*entry
;
1141 struct dentry
*d_events
;
1144 d_tracer
= tracing_init_dentry();
1148 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
1149 (void *)&show_event_seq_ops
,
1150 &ftrace_avail_fops
);
1152 pr_warning("Could not create debugfs "
1153 "'available_events' entry\n");
1155 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
1156 (void *)&show_set_event_seq_ops
,
1157 &ftrace_set_event_fops
);
1159 pr_warning("Could not create debugfs "
1160 "'set_event' entry\n");
1162 d_events
= event_trace_events_dir();
1166 /* ring buffer internal formats */
1167 trace_create_file("header_page", 0444, d_events
,
1168 ring_buffer_print_page_header
,
1169 &ftrace_show_header_fops
);
1171 trace_create_file("header_event", 0444, d_events
,
1172 ring_buffer_print_entry_header
,
1173 &ftrace_show_header_fops
);
1175 trace_create_file("enable", 0644, d_events
,
1176 NULL
, &ftrace_system_enable_fops
);
1178 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
1179 /* The linker may leave blanks */
1182 list_add(&call
->list
, &ftrace_events
);
1183 event_create_dir(call
, d_events
, &ftrace_event_id_fops
,
1184 &ftrace_enable_fops
, &ftrace_event_filter_fops
,
1185 &ftrace_event_format_fops
);
1188 ret
= register_module_notifier(&trace_module_nb
);
1190 pr_warning("Failed to register trace events module notifier\n");
1194 fs_initcall(event_trace_init
);
1196 #ifdef CONFIG_FTRACE_STARTUP_TEST
1198 static DEFINE_SPINLOCK(test_spinlock
);
1199 static DEFINE_SPINLOCK(test_spinlock_irq
);
1200 static DEFINE_MUTEX(test_mutex
);
1202 static __init
void test_work(struct work_struct
*dummy
)
1204 spin_lock(&test_spinlock
);
1205 spin_lock_irq(&test_spinlock_irq
);
1207 spin_unlock_irq(&test_spinlock_irq
);
1208 spin_unlock(&test_spinlock
);
1210 mutex_lock(&test_mutex
);
1212 mutex_unlock(&test_mutex
);
1215 static __init
int event_test_thread(void *unused
)
1219 test_malloc
= kmalloc(1234, GFP_KERNEL
);
1221 pr_info("failed to kmalloc\n");
1223 schedule_on_each_cpu(test_work
);
1227 set_current_state(TASK_INTERRUPTIBLE
);
1228 while (!kthread_should_stop())
1235 * Do various things that may trigger events.
1237 static __init
void event_test_stuff(void)
1239 struct task_struct
*test_thread
;
1241 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
1243 kthread_stop(test_thread
);
1247 * For every trace event defined, we will test each trace point separately,
1248 * and then by groups, and finally all trace points.
1250 static __init
void event_trace_self_tests(void)
1252 struct ftrace_event_call
*call
;
1253 struct event_subsystem
*system
;
1256 pr_info("Running tests on trace events:\n");
1258 list_for_each_entry(call
, &ftrace_events
, list
) {
1260 /* Only test those that have a regfunc */
1264 pr_info("Testing event %s: ", call
->name
);
1267 * If an event is already enabled, someone is using
1268 * it and the self test should not be on.
1270 if (call
->enabled
) {
1271 pr_warning("Enabled event during self test!\n");
1276 ftrace_event_enable_disable(call
, 1);
1278 ftrace_event_enable_disable(call
, 0);
1283 /* Now test at the sub system level */
1285 pr_info("Running tests on trace event systems:\n");
1287 list_for_each_entry(system
, &event_subsystems
, list
) {
1289 /* the ftrace system is special, skip it */
1290 if (strcmp(system
->name
, "ftrace") == 0)
1293 pr_info("Testing event system %s: ", system
->name
);
1295 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 1);
1296 if (WARN_ON_ONCE(ret
)) {
1297 pr_warning("error enabling system %s\n",
1304 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 0);
1305 if (WARN_ON_ONCE(ret
))
1306 pr_warning("error disabling system %s\n",
1312 /* Test with all events enabled */
1314 pr_info("Running tests on all trace events:\n");
1315 pr_info("Testing all events: ");
1317 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 1);
1318 if (WARN_ON_ONCE(ret
)) {
1319 pr_warning("error enabling all events\n");
1326 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 0);
1327 if (WARN_ON_ONCE(ret
)) {
1328 pr_warning("error disabling all events\n");
1335 #ifdef CONFIG_FUNCTION_TRACER
1337 static DEFINE_PER_CPU(atomic_t
, test_event_disable
);
1340 function_test_events_call(unsigned long ip
, unsigned long parent_ip
)
1342 struct ring_buffer_event
*event
;
1343 struct ftrace_entry
*entry
;
1344 unsigned long flags
;
1350 pc
= preempt_count();
1351 resched
= ftrace_preempt_disable();
1352 cpu
= raw_smp_processor_id();
1353 disabled
= atomic_inc_return(&per_cpu(test_event_disable
, cpu
));
1358 local_save_flags(flags
);
1360 event
= trace_current_buffer_lock_reserve(TRACE_FN
, sizeof(*entry
),
1364 entry
= ring_buffer_event_data(event
);
1366 entry
->parent_ip
= parent_ip
;
1368 trace_nowake_buffer_unlock_commit(event
, flags
, pc
);
1371 atomic_dec(&per_cpu(test_event_disable
, cpu
));
1372 ftrace_preempt_enable(resched
);
1375 static struct ftrace_ops trace_ops __initdata
=
1377 .func
= function_test_events_call
,
1380 static __init
void event_trace_self_test_with_function(void)
1382 register_ftrace_function(&trace_ops
);
1383 pr_info("Running tests again, along with the function tracer\n");
1384 event_trace_self_tests();
1385 unregister_ftrace_function(&trace_ops
);
1388 static __init
void event_trace_self_test_with_function(void)
1393 static __init
int event_trace_self_tests_init(void)
1396 event_trace_self_tests();
1398 event_trace_self_test_with_function();
1403 late_initcall(event_trace_self_tests_init
);