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 LIST_HEAD(ftrace_events
);
33 trace_get_fields(struct ftrace_event_call
*event_call
)
35 if (!event_call
->class->get_fields
)
36 return &event_call
->class->fields
;
37 return event_call
->class->get_fields(event_call
);
40 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
41 const char *name
, int offset
, int size
, int is_signed
,
44 struct ftrace_event_field
*field
;
45 struct list_head
*head
;
47 if (WARN_ON(!call
->class))
50 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
54 field
->name
= kstrdup(name
, GFP_KERNEL
);
58 field
->type
= kstrdup(type
, GFP_KERNEL
);
62 if (filter_type
== FILTER_OTHER
)
63 field
->filter_type
= filter_assign_type(type
);
65 field
->filter_type
= filter_type
;
67 field
->offset
= offset
;
69 field
->is_signed
= is_signed
;
71 head
= trace_get_fields(call
);
72 list_add(&field
->link
, head
);
83 EXPORT_SYMBOL_GPL(trace_define_field
);
85 #define __common_field(type, item) \
86 ret = trace_define_field(call, #type, "common_" #item, \
87 offsetof(typeof(ent), item), \
89 is_signed_type(type), FILTER_OTHER); \
93 static int trace_define_common_fields(struct ftrace_event_call
*call
)
96 struct trace_entry ent
;
98 __common_field(unsigned short, type
);
99 __common_field(unsigned char, flags
);
100 __common_field(unsigned char, preempt_count
);
101 __common_field(int, pid
);
102 __common_field(int, lock_depth
);
107 void trace_destroy_fields(struct ftrace_event_call
*call
)
109 struct ftrace_event_field
*field
, *next
;
110 struct list_head
*head
;
112 head
= trace_get_fields(call
);
113 list_for_each_entry_safe(field
, next
, head
, link
) {
114 list_del(&field
->link
);
121 int trace_event_raw_init(struct ftrace_event_call
*call
)
125 id
= register_ftrace_event(&call
->event
);
131 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
133 static int ftrace_event_enable_disable(struct ftrace_event_call
*call
,
140 if (call
->flags
& TRACE_EVENT_FL_ENABLED
) {
141 call
->flags
&= ~TRACE_EVENT_FL_ENABLED
;
142 tracing_stop_cmdline_record();
143 if (call
->class->reg
)
144 call
->class->reg(call
, TRACE_REG_UNREGISTER
);
146 tracepoint_probe_unregister(call
->name
,
152 if (!(call
->flags
& TRACE_EVENT_FL_ENABLED
)) {
153 tracing_start_cmdline_record();
154 if (call
->class->reg
)
155 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
);
157 ret
= tracepoint_probe_register(call
->name
,
161 tracing_stop_cmdline_record();
162 pr_info("event trace: Could not enable event "
166 call
->flags
|= TRACE_EVENT_FL_ENABLED
;
174 static void ftrace_clear_events(void)
176 struct ftrace_event_call
*call
;
178 mutex_lock(&event_mutex
);
179 list_for_each_entry(call
, &ftrace_events
, list
) {
180 ftrace_event_enable_disable(call
, 0);
182 mutex_unlock(&event_mutex
);
185 static void __put_system(struct event_subsystem
*system
)
187 struct event_filter
*filter
= system
->filter
;
189 WARN_ON_ONCE(system
->ref_count
== 0);
190 if (--system
->ref_count
)
194 kfree(filter
->filter_string
);
201 static void __get_system(struct event_subsystem
*system
)
203 WARN_ON_ONCE(system
->ref_count
== 0);
207 static void put_system(struct event_subsystem
*system
)
209 mutex_lock(&event_mutex
);
210 __put_system(system
);
211 mutex_unlock(&event_mutex
);
215 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
217 static int __ftrace_set_clr_event(const char *match
, const char *sub
,
218 const char *event
, int set
)
220 struct ftrace_event_call
*call
;
223 mutex_lock(&event_mutex
);
224 list_for_each_entry(call
, &ftrace_events
, list
) {
226 if (!call
->name
|| !call
->class ||
227 (!call
->class->probe
&& !call
->class->reg
))
231 strcmp(match
, call
->name
) != 0 &&
232 strcmp(match
, call
->class->system
) != 0)
235 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
238 if (event
&& strcmp(event
, call
->name
) != 0)
241 ftrace_event_enable_disable(call
, set
);
245 mutex_unlock(&event_mutex
);
250 static int ftrace_set_clr_event(char *buf
, int set
)
252 char *event
= NULL
, *sub
= NULL
, *match
;
255 * The buf format can be <subsystem>:<event-name>
256 * *:<event-name> means any event by that name.
257 * :<event-name> is the same.
259 * <subsystem>:* means all events in that subsystem
260 * <subsystem>: means the same.
262 * <name> (no ':') means all events in a subsystem with
263 * the name <name> or any event that matches <name>
266 match
= strsep(&buf
, ":");
272 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
274 if (!strlen(event
) || strcmp(event
, "*") == 0)
278 return __ftrace_set_clr_event(match
, sub
, event
, set
);
282 * trace_set_clr_event - enable or disable an event
283 * @system: system name to match (NULL for any system)
284 * @event: event name to match (NULL for all events, within system)
285 * @set: 1 to enable, 0 to disable
287 * This is a way for other parts of the kernel to enable or disable
290 * Returns 0 on success, -EINVAL if the parameters do not match any
293 int trace_set_clr_event(const char *system
, const char *event
, int set
)
295 return __ftrace_set_clr_event(NULL
, system
, event
, set
);
298 /* 128 should be much more than enough */
299 #define EVENT_BUF_SIZE 127
302 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
303 size_t cnt
, loff_t
*ppos
)
305 struct trace_parser parser
;
311 ret
= tracing_update_buffers();
315 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
318 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
320 if (read
>= 0 && trace_parser_loaded((&parser
))) {
323 if (*parser
.buffer
== '!')
326 parser
.buffer
[parser
.idx
] = 0;
328 ret
= ftrace_set_clr_event(parser
.buffer
+ !set
, set
);
336 trace_parser_put(&parser
);
342 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
344 struct ftrace_event_call
*call
= v
;
348 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
350 * The ftrace subsystem is for showing formats only.
351 * They can not be enabled or disabled via the event files.
353 if (call
->class && (call
->class->probe
|| call
->class->reg
))
360 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
362 struct ftrace_event_call
*call
;
365 mutex_lock(&event_mutex
);
367 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
368 for (l
= 0; l
<= *pos
; ) {
369 call
= t_next(m
, call
, &l
);
377 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
379 struct ftrace_event_call
*call
= v
;
383 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
384 if (call
->flags
& TRACE_EVENT_FL_ENABLED
)
391 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
393 struct ftrace_event_call
*call
;
396 mutex_lock(&event_mutex
);
398 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
399 for (l
= 0; l
<= *pos
; ) {
400 call
= s_next(m
, call
, &l
);
407 static int t_show(struct seq_file
*m
, void *v
)
409 struct ftrace_event_call
*call
= v
;
411 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
412 seq_printf(m
, "%s:", call
->class->system
);
413 seq_printf(m
, "%s\n", call
->name
);
418 static void t_stop(struct seq_file
*m
, void *p
)
420 mutex_unlock(&event_mutex
);
424 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
426 const struct seq_operations
*seq_ops
;
428 if ((file
->f_mode
& FMODE_WRITE
) &&
429 (file
->f_flags
& O_TRUNC
))
430 ftrace_clear_events();
432 seq_ops
= inode
->i_private
;
433 return seq_open(file
, seq_ops
);
437 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
440 struct ftrace_event_call
*call
= filp
->private_data
;
443 if (call
->flags
& TRACE_EVENT_FL_ENABLED
)
448 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
452 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
455 struct ftrace_event_call
*call
= filp
->private_data
;
460 if (cnt
>= sizeof(buf
))
463 if (copy_from_user(&buf
, ubuf
, cnt
))
468 ret
= strict_strtoul(buf
, 10, &val
);
472 ret
= tracing_update_buffers();
479 mutex_lock(&event_mutex
);
480 ret
= ftrace_event_enable_disable(call
, val
);
481 mutex_unlock(&event_mutex
);
490 return ret
? ret
: cnt
;
494 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
497 const char set_to_char
[4] = { '?', '0', '1', 'X' };
498 struct event_subsystem
*system
= filp
->private_data
;
499 struct ftrace_event_call
*call
;
504 mutex_lock(&event_mutex
);
505 list_for_each_entry(call
, &ftrace_events
, list
) {
506 if (!call
->name
|| !call
->class ||
507 (!call
->class->probe
&& !call
->class->reg
))
510 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
514 * We need to find out if all the events are set
515 * or if all events or cleared, or if we have
518 set
|= (1 << !!(call
->flags
& TRACE_EVENT_FL_ENABLED
));
521 * If we have a mixture, no need to look further.
526 mutex_unlock(&event_mutex
);
528 buf
[0] = set_to_char
[set
];
531 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
537 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
540 struct event_subsystem
*system
= filp
->private_data
;
541 const char *name
= NULL
;
546 if (cnt
>= sizeof(buf
))
549 if (copy_from_user(&buf
, ubuf
, cnt
))
554 ret
= strict_strtoul(buf
, 10, &val
);
558 ret
= tracing_update_buffers();
562 if (val
!= 0 && val
!= 1)
566 * Opening of "enable" adds a ref count to system,
567 * so the name is safe to use.
572 ret
= __ftrace_set_clr_event(NULL
, name
, NULL
, val
);
585 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
588 struct ftrace_event_call
*call
= filp
->private_data
;
589 struct ftrace_event_field
*field
;
590 struct list_head
*head
;
592 int common_field_count
= 5;
599 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
605 trace_seq_printf(s
, "name: %s\n", call
->name
);
606 trace_seq_printf(s
, "ID: %d\n", call
->event
.type
);
607 trace_seq_printf(s
, "format:\n");
609 head
= trace_get_fields(call
);
610 list_for_each_entry_reverse(field
, head
, link
) {
612 * Smartly shows the array type(except dynamic array).
615 * If TYPE := TYPE[LEN], it is shown:
616 * field:TYPE VAR[LEN]
618 const char *array_descriptor
= strchr(field
->type
, '[');
620 if (!strncmp(field
->type
, "__data_loc", 10))
621 array_descriptor
= NULL
;
623 if (!array_descriptor
) {
624 r
= trace_seq_printf(s
, "\tfield:%s %s;\toffset:%u;"
625 "\tsize:%u;\tsigned:%d;\n",
626 field
->type
, field
->name
, field
->offset
,
627 field
->size
, !!field
->is_signed
);
629 r
= trace_seq_printf(s
, "\tfield:%.*s %s%s;\toffset:%u;"
630 "\tsize:%u;\tsigned:%d;\n",
631 (int)(array_descriptor
- field
->type
),
632 field
->type
, field
->name
,
633 array_descriptor
, field
->offset
,
634 field
->size
, !!field
->is_signed
);
637 if (--common_field_count
== 0)
638 r
= trace_seq_printf(s
, "\n");
645 r
= trace_seq_printf(s
, "\nprint fmt: %s\n",
650 * ug! The format output is bigger than a PAGE!!
652 buf
= "FORMAT TOO BIG\n";
653 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
658 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
666 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
668 struct ftrace_event_call
*call
= filp
->private_data
;
675 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
680 trace_seq_printf(s
, "%d\n", call
->event
.type
);
682 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
689 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
692 struct ftrace_event_call
*call
= filp
->private_data
;
699 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
705 print_event_filter(call
, s
);
706 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
714 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
717 struct ftrace_event_call
*call
= filp
->private_data
;
721 if (cnt
>= PAGE_SIZE
)
724 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
728 if (copy_from_user(buf
, ubuf
, cnt
)) {
729 free_page((unsigned long) buf
);
734 err
= apply_event_filter(call
, buf
);
735 free_page((unsigned long) buf
);
744 static LIST_HEAD(event_subsystems
);
746 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
748 struct event_subsystem
*system
= NULL
;
751 if (!inode
->i_private
)
754 /* Make sure the system still exists */
755 mutex_lock(&event_mutex
);
756 list_for_each_entry(system
, &event_subsystems
, list
) {
757 if (system
== inode
->i_private
) {
758 /* Don't open systems with no events */
759 if (!system
->nr_events
) {
763 __get_system(system
);
767 mutex_unlock(&event_mutex
);
769 if (system
!= inode
->i_private
)
773 ret
= tracing_open_generic(inode
, filp
);
774 if (ret
< 0 && system
)
780 static int subsystem_release(struct inode
*inode
, struct file
*file
)
782 struct event_subsystem
*system
= inode
->i_private
;
791 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
794 struct event_subsystem
*system
= filp
->private_data
;
801 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
807 print_subsystem_event_filter(system
, s
);
808 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
816 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
819 struct event_subsystem
*system
= filp
->private_data
;
823 if (cnt
>= PAGE_SIZE
)
826 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
830 if (copy_from_user(buf
, ubuf
, cnt
)) {
831 free_page((unsigned long) buf
);
836 err
= apply_subsystem_event_filter(system
, buf
);
837 free_page((unsigned long) buf
);
847 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
849 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
856 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
863 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
870 static const struct seq_operations show_event_seq_ops
= {
877 static const struct seq_operations show_set_event_seq_ops
= {
884 static const struct file_operations ftrace_avail_fops
= {
885 .open
= ftrace_event_seq_open
,
888 .release
= seq_release
,
891 static const struct file_operations ftrace_set_event_fops
= {
892 .open
= ftrace_event_seq_open
,
894 .write
= ftrace_event_write
,
896 .release
= seq_release
,
899 static const struct file_operations ftrace_enable_fops
= {
900 .open
= tracing_open_generic
,
901 .read
= event_enable_read
,
902 .write
= event_enable_write
,
905 static const struct file_operations ftrace_event_format_fops
= {
906 .open
= tracing_open_generic
,
907 .read
= event_format_read
,
910 static const struct file_operations ftrace_event_id_fops
= {
911 .open
= tracing_open_generic
,
912 .read
= event_id_read
,
915 static const struct file_operations ftrace_event_filter_fops
= {
916 .open
= tracing_open_generic
,
917 .read
= event_filter_read
,
918 .write
= event_filter_write
,
921 static const struct file_operations ftrace_subsystem_filter_fops
= {
922 .open
= subsystem_open
,
923 .read
= subsystem_filter_read
,
924 .write
= subsystem_filter_write
,
925 .release
= subsystem_release
,
928 static const struct file_operations ftrace_system_enable_fops
= {
929 .open
= subsystem_open
,
930 .read
= system_enable_read
,
931 .write
= system_enable_write
,
932 .release
= subsystem_release
,
935 static const struct file_operations ftrace_show_header_fops
= {
936 .open
= tracing_open_generic
,
940 static struct dentry
*event_trace_events_dir(void)
942 static struct dentry
*d_tracer
;
943 static struct dentry
*d_events
;
948 d_tracer
= tracing_init_dentry();
952 d_events
= debugfs_create_dir("events", d_tracer
);
954 pr_warning("Could not create debugfs "
955 "'events' directory\n");
960 static struct dentry
*
961 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
963 struct event_subsystem
*system
;
964 struct dentry
*entry
;
966 /* First see if we did not already create this dir */
967 list_for_each_entry(system
, &event_subsystems
, list
) {
968 if (strcmp(system
->name
, name
) == 0) {
969 __get_system(system
);
971 return system
->entry
;
975 /* need to create new entry */
976 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
978 pr_warning("No memory to create event subsystem %s\n",
983 system
->entry
= debugfs_create_dir(name
, d_events
);
984 if (!system
->entry
) {
985 pr_warning("Could not create event subsystem %s\n",
991 system
->nr_events
= 1;
992 system
->ref_count
= 1;
993 system
->name
= kstrdup(name
, GFP_KERNEL
);
995 debugfs_remove(system
->entry
);
1000 list_add(&system
->list
, &event_subsystems
);
1002 system
->filter
= NULL
;
1004 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1005 if (!system
->filter
) {
1006 pr_warning("Could not allocate filter for subsystem "
1008 return system
->entry
;
1011 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
1012 &ftrace_subsystem_filter_fops
);
1014 kfree(system
->filter
);
1015 system
->filter
= NULL
;
1016 pr_warning("Could not create debugfs "
1017 "'%s/filter' entry\n", name
);
1020 trace_create_file("enable", 0644, system
->entry
, system
,
1021 &ftrace_system_enable_fops
);
1023 return system
->entry
;
1027 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
,
1028 const struct file_operations
*id
,
1029 const struct file_operations
*enable
,
1030 const struct file_operations
*filter
,
1031 const struct file_operations
*format
)
1033 struct list_head
*head
;
1037 * If the trace point header did not define TRACE_SYSTEM
1038 * then the system would be called "TRACE_SYSTEM".
1040 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
1041 d_events
= event_subsystem_dir(call
->class->system
, d_events
);
1043 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
1045 pr_warning("Could not create debugfs "
1046 "'%s' directory\n", call
->name
);
1050 if (call
->class->probe
|| call
->class->reg
)
1051 trace_create_file("enable", 0644, call
->dir
, call
,
1054 #ifdef CONFIG_PERF_EVENTS
1055 if (call
->event
.type
&& (call
->class->perf_probe
|| call
->class->reg
))
1056 trace_create_file("id", 0444, call
->dir
, call
,
1060 if (call
->class->define_fields
) {
1062 * Other events may have the same class. Only update
1063 * the fields if they are not already defined.
1065 head
= trace_get_fields(call
);
1066 if (list_empty(head
)) {
1067 ret
= trace_define_common_fields(call
);
1069 ret
= call
->class->define_fields(call
);
1071 pr_warning("Could not initialize trace point"
1072 " events/%s\n", call
->name
);
1076 trace_create_file("filter", 0644, call
->dir
, call
,
1080 trace_create_file("format", 0444, call
->dir
, call
,
1086 static int __trace_add_event_call(struct ftrace_event_call
*call
)
1088 struct dentry
*d_events
;
1094 if (call
->class->raw_init
) {
1095 ret
= call
->class->raw_init(call
);
1098 pr_warning("Could not initialize trace "
1099 "events/%s\n", call
->name
);
1104 d_events
= event_trace_events_dir();
1108 ret
= event_create_dir(call
, d_events
, &ftrace_event_id_fops
,
1109 &ftrace_enable_fops
, &ftrace_event_filter_fops
,
1110 &ftrace_event_format_fops
);
1112 list_add(&call
->list
, &ftrace_events
);
1117 /* Add an additional event_call dynamically */
1118 int trace_add_event_call(struct ftrace_event_call
*call
)
1121 mutex_lock(&event_mutex
);
1122 ret
= __trace_add_event_call(call
);
1123 mutex_unlock(&event_mutex
);
1127 static void remove_subsystem_dir(const char *name
)
1129 struct event_subsystem
*system
;
1131 if (strcmp(name
, TRACE_SYSTEM
) == 0)
1134 list_for_each_entry(system
, &event_subsystems
, list
) {
1135 if (strcmp(system
->name
, name
) == 0) {
1136 if (!--system
->nr_events
) {
1137 debugfs_remove_recursive(system
->entry
);
1138 list_del(&system
->list
);
1139 __put_system(system
);
1147 * Must be called under locking both of event_mutex and trace_event_mutex.
1149 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1151 ftrace_event_enable_disable(call
, 0);
1152 if (call
->event
.funcs
)
1153 __unregister_ftrace_event(&call
->event
);
1154 debugfs_remove_recursive(call
->dir
);
1155 list_del(&call
->list
);
1156 trace_destroy_fields(call
);
1157 destroy_preds(call
);
1158 remove_subsystem_dir(call
->class->system
);
1161 /* Remove an event_call */
1162 void trace_remove_event_call(struct ftrace_event_call
*call
)
1164 mutex_lock(&event_mutex
);
1165 down_write(&trace_event_mutex
);
1166 __trace_remove_event_call(call
);
1167 up_write(&trace_event_mutex
);
1168 mutex_unlock(&event_mutex
);
1171 #define for_each_event(event, start, end) \
1172 for (event = start; \
1173 (unsigned long)event < (unsigned long)end; \
1176 #ifdef CONFIG_MODULES
1178 static LIST_HEAD(ftrace_module_file_list
);
1181 * Modules must own their file_operations to keep up with
1182 * reference counting.
1184 struct ftrace_module_file_ops
{
1185 struct list_head list
;
1187 struct file_operations id
;
1188 struct file_operations enable
;
1189 struct file_operations format
;
1190 struct file_operations filter
;
1193 static struct ftrace_module_file_ops
*
1194 trace_create_file_ops(struct module
*mod
)
1196 struct ftrace_module_file_ops
*file_ops
;
1199 * This is a bit of a PITA. To allow for correct reference
1200 * counting, modules must "own" their file_operations.
1201 * To do this, we allocate the file operations that will be
1202 * used in the event directory.
1205 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1209 file_ops
->mod
= mod
;
1211 file_ops
->id
= ftrace_event_id_fops
;
1212 file_ops
->id
.owner
= mod
;
1214 file_ops
->enable
= ftrace_enable_fops
;
1215 file_ops
->enable
.owner
= mod
;
1217 file_ops
->filter
= ftrace_event_filter_fops
;
1218 file_ops
->filter
.owner
= mod
;
1220 file_ops
->format
= ftrace_event_format_fops
;
1221 file_ops
->format
.owner
= mod
;
1223 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1228 static void trace_module_add_events(struct module
*mod
)
1230 struct ftrace_module_file_ops
*file_ops
= NULL
;
1231 struct ftrace_event_call
*call
, *start
, *end
;
1232 struct dentry
*d_events
;
1235 start
= mod
->trace_events
;
1236 end
= mod
->trace_events
+ mod
->num_trace_events
;
1241 d_events
= event_trace_events_dir();
1245 for_each_event(call
, start
, end
) {
1246 /* The linker may leave blanks */
1249 if (call
->class->raw_init
) {
1250 ret
= call
->class->raw_init(call
);
1253 pr_warning("Could not initialize trace "
1254 "point events/%s\n", call
->name
);
1259 * This module has events, create file ops for this module
1260 * if not already done.
1263 file_ops
= trace_create_file_ops(mod
);
1268 ret
= event_create_dir(call
, d_events
,
1269 &file_ops
->id
, &file_ops
->enable
,
1270 &file_ops
->filter
, &file_ops
->format
);
1272 list_add(&call
->list
, &ftrace_events
);
1276 static void trace_module_remove_events(struct module
*mod
)
1278 struct ftrace_module_file_ops
*file_ops
;
1279 struct ftrace_event_call
*call
, *p
;
1282 down_write(&trace_event_mutex
);
1283 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1284 if (call
->mod
== mod
) {
1286 __trace_remove_event_call(call
);
1290 /* Now free the file_operations */
1291 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1292 if (file_ops
->mod
== mod
)
1295 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1296 list_del(&file_ops
->list
);
1301 * It is safest to reset the ring buffer if the module being unloaded
1302 * registered any events.
1305 tracing_reset_current_online_cpus();
1306 up_write(&trace_event_mutex
);
1309 static int trace_module_notify(struct notifier_block
*self
,
1310 unsigned long val
, void *data
)
1312 struct module
*mod
= data
;
1314 mutex_lock(&event_mutex
);
1316 case MODULE_STATE_COMING
:
1317 trace_module_add_events(mod
);
1319 case MODULE_STATE_GOING
:
1320 trace_module_remove_events(mod
);
1323 mutex_unlock(&event_mutex
);
1328 static int trace_module_notify(struct notifier_block
*self
,
1329 unsigned long val
, void *data
)
1333 #endif /* CONFIG_MODULES */
1335 static struct notifier_block trace_module_nb
= {
1336 .notifier_call
= trace_module_notify
,
1340 extern struct ftrace_event_call __start_ftrace_events
[];
1341 extern struct ftrace_event_call __stop_ftrace_events
[];
1343 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
1345 static __init
int setup_trace_event(char *str
)
1347 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
1348 ring_buffer_expanded
= 1;
1349 tracing_selftest_disabled
= 1;
1353 __setup("trace_event=", setup_trace_event
);
1355 static __init
int event_trace_init(void)
1357 struct ftrace_event_call
*call
;
1358 struct dentry
*d_tracer
;
1359 struct dentry
*entry
;
1360 struct dentry
*d_events
;
1362 char *buf
= bootup_event_buf
;
1365 d_tracer
= tracing_init_dentry();
1369 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
1370 (void *)&show_event_seq_ops
,
1371 &ftrace_avail_fops
);
1373 pr_warning("Could not create debugfs "
1374 "'available_events' entry\n");
1376 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
1377 (void *)&show_set_event_seq_ops
,
1378 &ftrace_set_event_fops
);
1380 pr_warning("Could not create debugfs "
1381 "'set_event' entry\n");
1383 d_events
= event_trace_events_dir();
1387 /* ring buffer internal formats */
1388 trace_create_file("header_page", 0444, d_events
,
1389 ring_buffer_print_page_header
,
1390 &ftrace_show_header_fops
);
1392 trace_create_file("header_event", 0444, d_events
,
1393 ring_buffer_print_entry_header
,
1394 &ftrace_show_header_fops
);
1396 trace_create_file("enable", 0644, d_events
,
1397 NULL
, &ftrace_system_enable_fops
);
1399 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
1400 /* The linker may leave blanks */
1403 if (call
->class->raw_init
) {
1404 ret
= call
->class->raw_init(call
);
1407 pr_warning("Could not initialize trace "
1408 "point events/%s\n", call
->name
);
1412 ret
= event_create_dir(call
, d_events
, &ftrace_event_id_fops
,
1413 &ftrace_enable_fops
,
1414 &ftrace_event_filter_fops
,
1415 &ftrace_event_format_fops
);
1417 list_add(&call
->list
, &ftrace_events
);
1421 token
= strsep(&buf
, ",");
1428 ret
= ftrace_set_clr_event(token
, 1);
1430 pr_warning("Failed to enable trace event: %s\n", token
);
1433 ret
= register_module_notifier(&trace_module_nb
);
1435 pr_warning("Failed to register trace events module notifier\n");
1439 fs_initcall(event_trace_init
);
1441 #ifdef CONFIG_FTRACE_STARTUP_TEST
1443 static DEFINE_SPINLOCK(test_spinlock
);
1444 static DEFINE_SPINLOCK(test_spinlock_irq
);
1445 static DEFINE_MUTEX(test_mutex
);
1447 static __init
void test_work(struct work_struct
*dummy
)
1449 spin_lock(&test_spinlock
);
1450 spin_lock_irq(&test_spinlock_irq
);
1452 spin_unlock_irq(&test_spinlock_irq
);
1453 spin_unlock(&test_spinlock
);
1455 mutex_lock(&test_mutex
);
1457 mutex_unlock(&test_mutex
);
1460 static __init
int event_test_thread(void *unused
)
1464 test_malloc
= kmalloc(1234, GFP_KERNEL
);
1466 pr_info("failed to kmalloc\n");
1468 schedule_on_each_cpu(test_work
);
1472 set_current_state(TASK_INTERRUPTIBLE
);
1473 while (!kthread_should_stop())
1480 * Do various things that may trigger events.
1482 static __init
void event_test_stuff(void)
1484 struct task_struct
*test_thread
;
1486 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
1488 kthread_stop(test_thread
);
1492 * For every trace event defined, we will test each trace point separately,
1493 * and then by groups, and finally all trace points.
1495 static __init
void event_trace_self_tests(void)
1497 struct ftrace_event_call
*call
;
1498 struct event_subsystem
*system
;
1501 pr_info("Running tests on trace events:\n");
1503 list_for_each_entry(call
, &ftrace_events
, list
) {
1505 /* Only test those that have a probe */
1506 if (!call
->class || !call
->class->probe
)
1510 * Testing syscall events here is pretty useless, but
1511 * we still do it if configured. But this is time consuming.
1512 * What we really need is a user thread to perform the
1513 * syscalls as we test.
1515 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1516 if (call
->class->system
&&
1517 strcmp(call
->class->system
, "syscalls") == 0)
1521 pr_info("Testing event %s: ", call
->name
);
1524 * If an event is already enabled, someone is using
1525 * it and the self test should not be on.
1527 if (call
->flags
& TRACE_EVENT_FL_ENABLED
) {
1528 pr_warning("Enabled event during self test!\n");
1533 ftrace_event_enable_disable(call
, 1);
1535 ftrace_event_enable_disable(call
, 0);
1540 /* Now test at the sub system level */
1542 pr_info("Running tests on trace event systems:\n");
1544 list_for_each_entry(system
, &event_subsystems
, list
) {
1546 /* the ftrace system is special, skip it */
1547 if (strcmp(system
->name
, "ftrace") == 0)
1550 pr_info("Testing event system %s: ", system
->name
);
1552 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 1);
1553 if (WARN_ON_ONCE(ret
)) {
1554 pr_warning("error enabling system %s\n",
1561 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 0);
1562 if (WARN_ON_ONCE(ret
))
1563 pr_warning("error disabling system %s\n",
1569 /* Test with all events enabled */
1571 pr_info("Running tests on all trace events:\n");
1572 pr_info("Testing all events: ");
1574 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 1);
1575 if (WARN_ON_ONCE(ret
)) {
1576 pr_warning("error enabling all events\n");
1583 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 0);
1584 if (WARN_ON_ONCE(ret
)) {
1585 pr_warning("error disabling all events\n");
1592 #ifdef CONFIG_FUNCTION_TRACER
1594 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
1597 function_test_events_call(unsigned long ip
, unsigned long parent_ip
)
1599 struct ring_buffer_event
*event
;
1600 struct ring_buffer
*buffer
;
1601 struct ftrace_entry
*entry
;
1602 unsigned long flags
;
1608 pc
= preempt_count();
1609 resched
= ftrace_preempt_disable();
1610 cpu
= raw_smp_processor_id();
1611 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
1616 local_save_flags(flags
);
1618 event
= trace_current_buffer_lock_reserve(&buffer
,
1619 TRACE_FN
, sizeof(*entry
),
1623 entry
= ring_buffer_event_data(event
);
1625 entry
->parent_ip
= parent_ip
;
1627 trace_nowake_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1630 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
1631 ftrace_preempt_enable(resched
);
1634 static struct ftrace_ops trace_ops __initdata
=
1636 .func
= function_test_events_call
,
1639 static __init
void event_trace_self_test_with_function(void)
1641 register_ftrace_function(&trace_ops
);
1642 pr_info("Running tests again, along with the function tracer\n");
1643 event_trace_self_tests();
1644 unregister_ftrace_function(&trace_ops
);
1647 static __init
void event_trace_self_test_with_function(void)
1652 static __init
int event_trace_self_tests_init(void)
1654 if (!tracing_selftest_disabled
) {
1655 event_trace_self_tests();
1656 event_trace_self_test_with_function();
1662 late_initcall(event_trace_self_tests_init
);