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
);
40 trace_get_fields(struct ftrace_event_call
*event_call
)
42 if (!event_call
->class->get_fields
)
43 return &event_call
->class->fields
;
44 return event_call
->class->get_fields(event_call
);
47 static int __trace_define_field(struct list_head
*head
, const char *type
,
48 const char *name
, int offset
, int size
,
49 int is_signed
, int filter_type
)
51 struct ftrace_event_field
*field
;
53 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
57 field
->name
= kstrdup(name
, GFP_KERNEL
);
61 field
->type
= kstrdup(type
, GFP_KERNEL
);
65 if (filter_type
== FILTER_OTHER
)
66 field
->filter_type
= filter_assign_type(type
);
68 field
->filter_type
= filter_type
;
70 field
->offset
= offset
;
72 field
->is_signed
= is_signed
;
74 list_add(&field
->link
, head
);
86 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
87 const char *name
, int offset
, int size
, int is_signed
,
90 struct list_head
*head
;
92 if (WARN_ON(!call
->class))
95 head
= trace_get_fields(call
);
96 return __trace_define_field(head
, type
, name
, offset
, size
,
97 is_signed
, filter_type
);
99 EXPORT_SYMBOL_GPL(trace_define_field
);
101 #define __common_field(type, item) \
102 ret = __trace_define_field(&ftrace_common_fields, #type, \
104 offsetof(typeof(ent), item), \
106 is_signed_type(type), FILTER_OTHER); \
110 static int trace_define_common_fields(void)
113 struct trace_entry ent
;
115 __common_field(unsigned short, type
);
116 __common_field(unsigned char, flags
);
117 __common_field(unsigned char, preempt_count
);
118 __common_field(int, pid
);
119 __common_field(int, padding
);
124 void trace_destroy_fields(struct ftrace_event_call
*call
)
126 struct ftrace_event_field
*field
, *next
;
127 struct list_head
*head
;
129 head
= trace_get_fields(call
);
130 list_for_each_entry_safe(field
, next
, head
, link
) {
131 list_del(&field
->link
);
138 int trace_event_raw_init(struct ftrace_event_call
*call
)
142 id
= register_ftrace_event(&call
->event
);
148 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
150 int ftrace_event_reg(struct ftrace_event_call
*call
, enum trace_reg type
)
153 case TRACE_REG_REGISTER
:
154 return tracepoint_probe_register(call
->name
,
157 case TRACE_REG_UNREGISTER
:
158 tracepoint_probe_unregister(call
->name
,
163 #ifdef CONFIG_PERF_EVENTS
164 case TRACE_REG_PERF_REGISTER
:
165 return tracepoint_probe_register(call
->name
,
166 call
->class->perf_probe
,
168 case TRACE_REG_PERF_UNREGISTER
:
169 tracepoint_probe_unregister(call
->name
,
170 call
->class->perf_probe
,
177 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
179 void trace_event_enable_cmd_record(bool enable
)
181 struct ftrace_event_call
*call
;
183 mutex_lock(&event_mutex
);
184 list_for_each_entry(call
, &ftrace_events
, list
) {
185 if (!(call
->flags
& TRACE_EVENT_FL_ENABLED
))
189 tracing_start_cmdline_record();
190 call
->flags
|= TRACE_EVENT_FL_RECORDED_CMD
;
192 tracing_stop_cmdline_record();
193 call
->flags
&= ~TRACE_EVENT_FL_RECORDED_CMD
;
196 mutex_unlock(&event_mutex
);
199 static int ftrace_event_enable_disable(struct ftrace_event_call
*call
,
206 if (call
->flags
& TRACE_EVENT_FL_ENABLED
) {
207 call
->flags
&= ~TRACE_EVENT_FL_ENABLED
;
208 if (call
->flags
& TRACE_EVENT_FL_RECORDED_CMD
) {
209 tracing_stop_cmdline_record();
210 call
->flags
&= ~TRACE_EVENT_FL_RECORDED_CMD
;
212 call
->class->reg(call
, TRACE_REG_UNREGISTER
);
216 if (!(call
->flags
& TRACE_EVENT_FL_ENABLED
)) {
217 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
218 tracing_start_cmdline_record();
219 call
->flags
|= TRACE_EVENT_FL_RECORDED_CMD
;
221 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
);
223 tracing_stop_cmdline_record();
224 pr_info("event trace: Could not enable event "
228 call
->flags
|= TRACE_EVENT_FL_ENABLED
;
236 static void ftrace_clear_events(void)
238 struct ftrace_event_call
*call
;
240 mutex_lock(&event_mutex
);
241 list_for_each_entry(call
, &ftrace_events
, list
) {
242 ftrace_event_enable_disable(call
, 0);
244 mutex_unlock(&event_mutex
);
247 static void __put_system(struct event_subsystem
*system
)
249 struct event_filter
*filter
= system
->filter
;
251 WARN_ON_ONCE(system
->ref_count
== 0);
252 if (--system
->ref_count
)
256 kfree(filter
->filter_string
);
263 static void __get_system(struct event_subsystem
*system
)
265 WARN_ON_ONCE(system
->ref_count
== 0);
269 static void put_system(struct event_subsystem
*system
)
271 mutex_lock(&event_mutex
);
272 __put_system(system
);
273 mutex_unlock(&event_mutex
);
277 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
279 static int __ftrace_set_clr_event(const char *match
, const char *sub
,
280 const char *event
, int set
)
282 struct ftrace_event_call
*call
;
285 mutex_lock(&event_mutex
);
286 list_for_each_entry(call
, &ftrace_events
, list
) {
288 if (!call
->name
|| !call
->class || !call
->class->reg
)
292 strcmp(match
, call
->name
) != 0 &&
293 strcmp(match
, call
->class->system
) != 0)
296 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
299 if (event
&& strcmp(event
, call
->name
) != 0)
302 ftrace_event_enable_disable(call
, set
);
306 mutex_unlock(&event_mutex
);
311 static int ftrace_set_clr_event(char *buf
, int set
)
313 char *event
= NULL
, *sub
= NULL
, *match
;
316 * The buf format can be <subsystem>:<event-name>
317 * *:<event-name> means any event by that name.
318 * :<event-name> is the same.
320 * <subsystem>:* means all events in that subsystem
321 * <subsystem>: means the same.
323 * <name> (no ':') means all events in a subsystem with
324 * the name <name> or any event that matches <name>
327 match
= strsep(&buf
, ":");
333 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
335 if (!strlen(event
) || strcmp(event
, "*") == 0)
339 return __ftrace_set_clr_event(match
, sub
, event
, set
);
343 * trace_set_clr_event - enable or disable an event
344 * @system: system name to match (NULL for any system)
345 * @event: event name to match (NULL for all events, within system)
346 * @set: 1 to enable, 0 to disable
348 * This is a way for other parts of the kernel to enable or disable
351 * Returns 0 on success, -EINVAL if the parameters do not match any
354 int trace_set_clr_event(const char *system
, const char *event
, int set
)
356 return __ftrace_set_clr_event(NULL
, system
, event
, set
);
358 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
360 /* 128 should be much more than enough */
361 #define EVENT_BUF_SIZE 127
364 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
365 size_t cnt
, loff_t
*ppos
)
367 struct trace_parser parser
;
373 ret
= tracing_update_buffers();
377 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
380 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
382 if (read
>= 0 && trace_parser_loaded((&parser
))) {
385 if (*parser
.buffer
== '!')
388 parser
.buffer
[parser
.idx
] = 0;
390 ret
= ftrace_set_clr_event(parser
.buffer
+ !set
, set
);
398 trace_parser_put(&parser
);
404 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
406 struct ftrace_event_call
*call
= v
;
410 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
412 * The ftrace subsystem is for showing formats only.
413 * They can not be enabled or disabled via the event files.
415 if (call
->class && call
->class->reg
)
422 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
424 struct ftrace_event_call
*call
;
427 mutex_lock(&event_mutex
);
429 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
430 for (l
= 0; l
<= *pos
; ) {
431 call
= t_next(m
, call
, &l
);
439 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
441 struct ftrace_event_call
*call
= v
;
445 list_for_each_entry_continue(call
, &ftrace_events
, list
) {
446 if (call
->flags
& TRACE_EVENT_FL_ENABLED
)
453 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
455 struct ftrace_event_call
*call
;
458 mutex_lock(&event_mutex
);
460 call
= list_entry(&ftrace_events
, struct ftrace_event_call
, list
);
461 for (l
= 0; l
<= *pos
; ) {
462 call
= s_next(m
, call
, &l
);
469 static int t_show(struct seq_file
*m
, void *v
)
471 struct ftrace_event_call
*call
= v
;
473 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
474 seq_printf(m
, "%s:", call
->class->system
);
475 seq_printf(m
, "%s\n", call
->name
);
480 static void t_stop(struct seq_file
*m
, void *p
)
482 mutex_unlock(&event_mutex
);
486 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
488 const struct seq_operations
*seq_ops
;
490 if ((file
->f_mode
& FMODE_WRITE
) &&
491 (file
->f_flags
& O_TRUNC
))
492 ftrace_clear_events();
494 seq_ops
= inode
->i_private
;
495 return seq_open(file
, seq_ops
);
499 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
502 struct ftrace_event_call
*call
= filp
->private_data
;
505 if (call
->flags
& TRACE_EVENT_FL_ENABLED
)
510 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
514 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
517 struct ftrace_event_call
*call
= filp
->private_data
;
522 if (cnt
>= sizeof(buf
))
525 if (copy_from_user(&buf
, ubuf
, cnt
))
530 ret
= strict_strtoul(buf
, 10, &val
);
534 ret
= tracing_update_buffers();
541 mutex_lock(&event_mutex
);
542 ret
= ftrace_event_enable_disable(call
, val
);
543 mutex_unlock(&event_mutex
);
552 return ret
? ret
: cnt
;
556 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
559 const char set_to_char
[4] = { '?', '0', '1', 'X' };
560 struct event_subsystem
*system
= filp
->private_data
;
561 struct ftrace_event_call
*call
;
566 mutex_lock(&event_mutex
);
567 list_for_each_entry(call
, &ftrace_events
, list
) {
568 if (!call
->name
|| !call
->class || !call
->class->reg
)
571 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
575 * We need to find out if all the events are set
576 * or if all events or cleared, or if we have
579 set
|= (1 << !!(call
->flags
& TRACE_EVENT_FL_ENABLED
));
582 * If we have a mixture, no need to look further.
587 mutex_unlock(&event_mutex
);
589 buf
[0] = set_to_char
[set
];
592 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
598 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
601 struct event_subsystem
*system
= filp
->private_data
;
602 const char *name
= NULL
;
607 if (cnt
>= sizeof(buf
))
610 if (copy_from_user(&buf
, ubuf
, cnt
))
615 ret
= strict_strtoul(buf
, 10, &val
);
619 ret
= tracing_update_buffers();
623 if (val
!= 0 && val
!= 1)
627 * Opening of "enable" adds a ref count to system,
628 * so the name is safe to use.
633 ret
= __ftrace_set_clr_event(NULL
, name
, NULL
, val
);
647 FORMAT_FIELD_SEPERATOR
= 2,
651 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
653 struct ftrace_event_call
*call
= m
->private;
654 struct ftrace_event_field
*field
;
655 struct list_head
*common_head
= &ftrace_common_fields
;
656 struct list_head
*head
= trace_get_fields(call
);
660 switch ((unsigned long)v
) {
662 if (unlikely(list_empty(common_head
)))
665 field
= list_entry(common_head
->prev
,
666 struct ftrace_event_field
, link
);
669 case FORMAT_FIELD_SEPERATOR
:
670 if (unlikely(list_empty(head
)))
673 field
= list_entry(head
->prev
, struct ftrace_event_field
, link
);
676 case FORMAT_PRINTFMT
:
682 if (field
->link
.prev
== common_head
)
683 return (void *)FORMAT_FIELD_SEPERATOR
;
684 else if (field
->link
.prev
== head
)
685 return (void *)FORMAT_PRINTFMT
;
687 field
= list_entry(field
->link
.prev
, struct ftrace_event_field
, link
);
692 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
697 /* Start by showing the header */
699 return (void *)FORMAT_HEADER
;
701 p
= (void *)FORMAT_HEADER
;
703 p
= f_next(m
, p
, &l
);
704 } while (p
&& l
< *pos
);
709 static int f_show(struct seq_file
*m
, void *v
)
711 struct ftrace_event_call
*call
= m
->private;
712 struct ftrace_event_field
*field
;
713 const char *array_descriptor
;
715 switch ((unsigned long)v
) {
717 seq_printf(m
, "name: %s\n", call
->name
);
718 seq_printf(m
, "ID: %d\n", call
->event
.type
);
719 seq_printf(m
, "format:\n");
722 case FORMAT_FIELD_SEPERATOR
:
726 case FORMAT_PRINTFMT
:
727 seq_printf(m
, "\nprint fmt: %s\n",
735 * Smartly shows the array type(except dynamic array).
738 * If TYPE := TYPE[LEN], it is shown:
739 * field:TYPE VAR[LEN]
741 array_descriptor
= strchr(field
->type
, '[');
743 if (!strncmp(field
->type
, "__data_loc", 10))
744 array_descriptor
= NULL
;
746 if (!array_descriptor
)
747 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
748 field
->type
, field
->name
, field
->offset
,
749 field
->size
, !!field
->is_signed
);
751 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
752 (int)(array_descriptor
- field
->type
),
753 field
->type
, field
->name
,
754 array_descriptor
, field
->offset
,
755 field
->size
, !!field
->is_signed
);
760 static void f_stop(struct seq_file
*m
, void *p
)
764 static const struct seq_operations trace_format_seq_ops
= {
771 static int trace_format_open(struct inode
*inode
, struct file
*file
)
773 struct ftrace_event_call
*call
= inode
->i_private
;
777 ret
= seq_open(file
, &trace_format_seq_ops
);
781 m
= file
->private_data
;
788 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
790 struct ftrace_event_call
*call
= filp
->private_data
;
797 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
802 trace_seq_printf(s
, "%d\n", call
->event
.type
);
804 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
811 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
814 struct ftrace_event_call
*call
= filp
->private_data
;
821 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
827 print_event_filter(call
, s
);
828 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
836 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
839 struct ftrace_event_call
*call
= filp
->private_data
;
843 if (cnt
>= PAGE_SIZE
)
846 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
850 if (copy_from_user(buf
, ubuf
, cnt
)) {
851 free_page((unsigned long) buf
);
856 err
= apply_event_filter(call
, buf
);
857 free_page((unsigned long) buf
);
866 static LIST_HEAD(event_subsystems
);
868 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
870 struct event_subsystem
*system
= NULL
;
873 if (!inode
->i_private
)
876 /* Make sure the system still exists */
877 mutex_lock(&event_mutex
);
878 list_for_each_entry(system
, &event_subsystems
, list
) {
879 if (system
== inode
->i_private
) {
880 /* Don't open systems with no events */
881 if (!system
->nr_events
) {
885 __get_system(system
);
889 mutex_unlock(&event_mutex
);
891 if (system
!= inode
->i_private
)
895 ret
= tracing_open_generic(inode
, filp
);
896 if (ret
< 0 && system
)
902 static int subsystem_release(struct inode
*inode
, struct file
*file
)
904 struct event_subsystem
*system
= inode
->i_private
;
913 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
916 struct event_subsystem
*system
= filp
->private_data
;
923 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
929 print_subsystem_event_filter(system
, s
);
930 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
938 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
941 struct event_subsystem
*system
= filp
->private_data
;
945 if (cnt
>= PAGE_SIZE
)
948 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
952 if (copy_from_user(buf
, ubuf
, cnt
)) {
953 free_page((unsigned long) buf
);
958 err
= apply_subsystem_event_filter(system
, buf
);
959 free_page((unsigned long) buf
);
969 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
971 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
978 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
985 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
992 static const struct seq_operations show_event_seq_ops
= {
999 static const struct seq_operations show_set_event_seq_ops
= {
1006 static const struct file_operations ftrace_avail_fops
= {
1007 .open
= ftrace_event_seq_open
,
1009 .llseek
= seq_lseek
,
1010 .release
= seq_release
,
1013 static const struct file_operations ftrace_set_event_fops
= {
1014 .open
= ftrace_event_seq_open
,
1016 .write
= ftrace_event_write
,
1017 .llseek
= seq_lseek
,
1018 .release
= seq_release
,
1021 static const struct file_operations ftrace_enable_fops
= {
1022 .open
= tracing_open_generic
,
1023 .read
= event_enable_read
,
1024 .write
= event_enable_write
,
1025 .llseek
= default_llseek
,
1028 static const struct file_operations ftrace_event_format_fops
= {
1029 .open
= trace_format_open
,
1031 .llseek
= seq_lseek
,
1032 .release
= seq_release
,
1035 static const struct file_operations ftrace_event_id_fops
= {
1036 .open
= tracing_open_generic
,
1037 .read
= event_id_read
,
1038 .llseek
= default_llseek
,
1041 static const struct file_operations ftrace_event_filter_fops
= {
1042 .open
= tracing_open_generic
,
1043 .read
= event_filter_read
,
1044 .write
= event_filter_write
,
1045 .llseek
= default_llseek
,
1048 static const struct file_operations ftrace_subsystem_filter_fops
= {
1049 .open
= subsystem_open
,
1050 .read
= subsystem_filter_read
,
1051 .write
= subsystem_filter_write
,
1052 .llseek
= default_llseek
,
1053 .release
= subsystem_release
,
1056 static const struct file_operations ftrace_system_enable_fops
= {
1057 .open
= subsystem_open
,
1058 .read
= system_enable_read
,
1059 .write
= system_enable_write
,
1060 .llseek
= default_llseek
,
1061 .release
= subsystem_release
,
1064 static const struct file_operations ftrace_show_header_fops
= {
1065 .open
= tracing_open_generic
,
1066 .read
= show_header
,
1067 .llseek
= default_llseek
,
1070 static struct dentry
*event_trace_events_dir(void)
1072 static struct dentry
*d_tracer
;
1073 static struct dentry
*d_events
;
1078 d_tracer
= tracing_init_dentry();
1082 d_events
= debugfs_create_dir("events", d_tracer
);
1084 pr_warning("Could not create debugfs "
1085 "'events' directory\n");
1090 static struct dentry
*
1091 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
1093 struct event_subsystem
*system
;
1094 struct dentry
*entry
;
1096 /* First see if we did not already create this dir */
1097 list_for_each_entry(system
, &event_subsystems
, list
) {
1098 if (strcmp(system
->name
, name
) == 0) {
1099 system
->nr_events
++;
1100 return system
->entry
;
1104 /* need to create new entry */
1105 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1107 pr_warning("No memory to create event subsystem %s\n",
1112 system
->entry
= debugfs_create_dir(name
, d_events
);
1113 if (!system
->entry
) {
1114 pr_warning("Could not create event subsystem %s\n",
1120 system
->nr_events
= 1;
1121 system
->ref_count
= 1;
1122 system
->name
= kstrdup(name
, GFP_KERNEL
);
1123 if (!system
->name
) {
1124 debugfs_remove(system
->entry
);
1129 list_add(&system
->list
, &event_subsystems
);
1131 system
->filter
= NULL
;
1133 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1134 if (!system
->filter
) {
1135 pr_warning("Could not allocate filter for subsystem "
1137 return system
->entry
;
1140 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
1141 &ftrace_subsystem_filter_fops
);
1143 kfree(system
->filter
);
1144 system
->filter
= NULL
;
1145 pr_warning("Could not create debugfs "
1146 "'%s/filter' entry\n", name
);
1149 trace_create_file("enable", 0644, system
->entry
, system
,
1150 &ftrace_system_enable_fops
);
1152 return system
->entry
;
1156 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
,
1157 const struct file_operations
*id
,
1158 const struct file_operations
*enable
,
1159 const struct file_operations
*filter
,
1160 const struct file_operations
*format
)
1162 struct list_head
*head
;
1166 * If the trace point header did not define TRACE_SYSTEM
1167 * then the system would be called "TRACE_SYSTEM".
1169 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
1170 d_events
= event_subsystem_dir(call
->class->system
, d_events
);
1172 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
1174 pr_warning("Could not create debugfs "
1175 "'%s' directory\n", call
->name
);
1179 if (call
->class->reg
)
1180 trace_create_file("enable", 0644, call
->dir
, call
,
1183 #ifdef CONFIG_PERF_EVENTS
1184 if (call
->event
.type
&& call
->class->reg
)
1185 trace_create_file("id", 0444, call
->dir
, call
,
1190 * Other events may have the same class. Only update
1191 * the fields if they are not already defined.
1193 head
= trace_get_fields(call
);
1194 if (list_empty(head
)) {
1195 ret
= call
->class->define_fields(call
);
1197 pr_warning("Could not initialize trace point"
1198 " events/%s\n", call
->name
);
1202 trace_create_file("filter", 0644, call
->dir
, call
,
1205 trace_create_file("format", 0444, call
->dir
, call
,
1212 __trace_add_event_call(struct ftrace_event_call
*call
, struct module
*mod
,
1213 const struct file_operations
*id
,
1214 const struct file_operations
*enable
,
1215 const struct file_operations
*filter
,
1216 const struct file_operations
*format
)
1218 struct dentry
*d_events
;
1221 /* The linker may leave blanks */
1225 if (call
->class->raw_init
) {
1226 ret
= call
->class->raw_init(call
);
1229 pr_warning("Could not initialize trace events/%s\n",
1235 d_events
= event_trace_events_dir();
1239 ret
= event_create_dir(call
, d_events
, id
, enable
, filter
, format
);
1241 list_add(&call
->list
, &ftrace_events
);
1247 /* Add an additional event_call dynamically */
1248 int trace_add_event_call(struct ftrace_event_call
*call
)
1251 mutex_lock(&event_mutex
);
1252 ret
= __trace_add_event_call(call
, NULL
, &ftrace_event_id_fops
,
1253 &ftrace_enable_fops
,
1254 &ftrace_event_filter_fops
,
1255 &ftrace_event_format_fops
);
1256 mutex_unlock(&event_mutex
);
1260 static void remove_subsystem_dir(const char *name
)
1262 struct event_subsystem
*system
;
1264 if (strcmp(name
, TRACE_SYSTEM
) == 0)
1267 list_for_each_entry(system
, &event_subsystems
, list
) {
1268 if (strcmp(system
->name
, name
) == 0) {
1269 if (!--system
->nr_events
) {
1270 debugfs_remove_recursive(system
->entry
);
1271 list_del(&system
->list
);
1272 __put_system(system
);
1280 * Must be called under locking both of event_mutex and trace_event_mutex.
1282 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1284 ftrace_event_enable_disable(call
, 0);
1285 if (call
->event
.funcs
)
1286 __unregister_ftrace_event(&call
->event
);
1287 debugfs_remove_recursive(call
->dir
);
1288 list_del(&call
->list
);
1289 trace_destroy_fields(call
);
1290 destroy_preds(call
);
1291 remove_subsystem_dir(call
->class->system
);
1294 /* Remove an event_call */
1295 void trace_remove_event_call(struct ftrace_event_call
*call
)
1297 mutex_lock(&event_mutex
);
1298 down_write(&trace_event_mutex
);
1299 __trace_remove_event_call(call
);
1300 up_write(&trace_event_mutex
);
1301 mutex_unlock(&event_mutex
);
1304 #define for_each_event(event, start, end) \
1305 for (event = start; \
1306 (unsigned long)event < (unsigned long)end; \
1309 #ifdef CONFIG_MODULES
1311 static LIST_HEAD(ftrace_module_file_list
);
1314 * Modules must own their file_operations to keep up with
1315 * reference counting.
1317 struct ftrace_module_file_ops
{
1318 struct list_head list
;
1320 struct file_operations id
;
1321 struct file_operations enable
;
1322 struct file_operations format
;
1323 struct file_operations filter
;
1326 static struct ftrace_module_file_ops
*
1327 trace_create_file_ops(struct module
*mod
)
1329 struct ftrace_module_file_ops
*file_ops
;
1332 * This is a bit of a PITA. To allow for correct reference
1333 * counting, modules must "own" their file_operations.
1334 * To do this, we allocate the file operations that will be
1335 * used in the event directory.
1338 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1342 file_ops
->mod
= mod
;
1344 file_ops
->id
= ftrace_event_id_fops
;
1345 file_ops
->id
.owner
= mod
;
1347 file_ops
->enable
= ftrace_enable_fops
;
1348 file_ops
->enable
.owner
= mod
;
1350 file_ops
->filter
= ftrace_event_filter_fops
;
1351 file_ops
->filter
.owner
= mod
;
1353 file_ops
->format
= ftrace_event_format_fops
;
1354 file_ops
->format
.owner
= mod
;
1356 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1361 static void trace_module_add_events(struct module
*mod
)
1363 struct ftrace_module_file_ops
*file_ops
= NULL
;
1364 struct ftrace_event_call
**call
, **start
, **end
;
1366 start
= mod
->trace_events
;
1367 end
= mod
->trace_events
+ mod
->num_trace_events
;
1372 file_ops
= trace_create_file_ops(mod
);
1376 for_each_event(call
, start
, end
) {
1377 __trace_add_event_call(*call
, mod
,
1378 &file_ops
->id
, &file_ops
->enable
,
1379 &file_ops
->filter
, &file_ops
->format
);
1383 static void trace_module_remove_events(struct module
*mod
)
1385 struct ftrace_module_file_ops
*file_ops
;
1386 struct ftrace_event_call
*call
, *p
;
1389 down_write(&trace_event_mutex
);
1390 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1391 if (call
->mod
== mod
) {
1393 __trace_remove_event_call(call
);
1397 /* Now free the file_operations */
1398 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1399 if (file_ops
->mod
== mod
)
1402 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1403 list_del(&file_ops
->list
);
1408 * It is safest to reset the ring buffer if the module being unloaded
1409 * registered any events.
1412 tracing_reset_current_online_cpus();
1413 up_write(&trace_event_mutex
);
1416 static int trace_module_notify(struct notifier_block
*self
,
1417 unsigned long val
, void *data
)
1419 struct module
*mod
= data
;
1421 mutex_lock(&event_mutex
);
1423 case MODULE_STATE_COMING
:
1424 trace_module_add_events(mod
);
1426 case MODULE_STATE_GOING
:
1427 trace_module_remove_events(mod
);
1430 mutex_unlock(&event_mutex
);
1435 static int trace_module_notify(struct notifier_block
*self
,
1436 unsigned long val
, void *data
)
1440 #endif /* CONFIG_MODULES */
1442 static struct notifier_block trace_module_nb
= {
1443 .notifier_call
= trace_module_notify
,
1447 extern struct ftrace_event_call
*__start_ftrace_events
[];
1448 extern struct ftrace_event_call
*__stop_ftrace_events
[];
1450 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
1452 static __init
int setup_trace_event(char *str
)
1454 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
1455 ring_buffer_expanded
= 1;
1456 tracing_selftest_disabled
= 1;
1460 __setup("trace_event=", setup_trace_event
);
1462 static __init
int event_trace_init(void)
1464 struct ftrace_event_call
**call
;
1465 struct dentry
*d_tracer
;
1466 struct dentry
*entry
;
1467 struct dentry
*d_events
;
1469 char *buf
= bootup_event_buf
;
1472 d_tracer
= tracing_init_dentry();
1476 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
1477 (void *)&show_event_seq_ops
,
1478 &ftrace_avail_fops
);
1480 pr_warning("Could not create debugfs "
1481 "'available_events' entry\n");
1483 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
1484 (void *)&show_set_event_seq_ops
,
1485 &ftrace_set_event_fops
);
1487 pr_warning("Could not create debugfs "
1488 "'set_event' entry\n");
1490 d_events
= event_trace_events_dir();
1494 /* ring buffer internal formats */
1495 trace_create_file("header_page", 0444, d_events
,
1496 ring_buffer_print_page_header
,
1497 &ftrace_show_header_fops
);
1499 trace_create_file("header_event", 0444, d_events
,
1500 ring_buffer_print_entry_header
,
1501 &ftrace_show_header_fops
);
1503 trace_create_file("enable", 0644, d_events
,
1504 NULL
, &ftrace_system_enable_fops
);
1506 if (trace_define_common_fields())
1507 pr_warning("tracing: Failed to allocate common fields");
1509 for_each_event(call
, __start_ftrace_events
, __stop_ftrace_events
) {
1510 __trace_add_event_call(*call
, NULL
, &ftrace_event_id_fops
,
1511 &ftrace_enable_fops
,
1512 &ftrace_event_filter_fops
,
1513 &ftrace_event_format_fops
);
1517 token
= strsep(&buf
, ",");
1524 ret
= ftrace_set_clr_event(token
, 1);
1526 pr_warning("Failed to enable trace event: %s\n", token
);
1529 ret
= register_module_notifier(&trace_module_nb
);
1531 pr_warning("Failed to register trace events module notifier\n");
1535 fs_initcall(event_trace_init
);
1537 #ifdef CONFIG_FTRACE_STARTUP_TEST
1539 static DEFINE_SPINLOCK(test_spinlock
);
1540 static DEFINE_SPINLOCK(test_spinlock_irq
);
1541 static DEFINE_MUTEX(test_mutex
);
1543 static __init
void test_work(struct work_struct
*dummy
)
1545 spin_lock(&test_spinlock
);
1546 spin_lock_irq(&test_spinlock_irq
);
1548 spin_unlock_irq(&test_spinlock_irq
);
1549 spin_unlock(&test_spinlock
);
1551 mutex_lock(&test_mutex
);
1553 mutex_unlock(&test_mutex
);
1556 static __init
int event_test_thread(void *unused
)
1560 test_malloc
= kmalloc(1234, GFP_KERNEL
);
1562 pr_info("failed to kmalloc\n");
1564 schedule_on_each_cpu(test_work
);
1568 set_current_state(TASK_INTERRUPTIBLE
);
1569 while (!kthread_should_stop())
1576 * Do various things that may trigger events.
1578 static __init
void event_test_stuff(void)
1580 struct task_struct
*test_thread
;
1582 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
1584 kthread_stop(test_thread
);
1588 * For every trace event defined, we will test each trace point separately,
1589 * and then by groups, and finally all trace points.
1591 static __init
void event_trace_self_tests(void)
1593 struct ftrace_event_call
*call
;
1594 struct event_subsystem
*system
;
1597 pr_info("Running tests on trace events:\n");
1599 list_for_each_entry(call
, &ftrace_events
, list
) {
1601 /* Only test those that have a probe */
1602 if (!call
->class || !call
->class->probe
)
1606 * Testing syscall events here is pretty useless, but
1607 * we still do it if configured. But this is time consuming.
1608 * What we really need is a user thread to perform the
1609 * syscalls as we test.
1611 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1612 if (call
->class->system
&&
1613 strcmp(call
->class->system
, "syscalls") == 0)
1617 pr_info("Testing event %s: ", call
->name
);
1620 * If an event is already enabled, someone is using
1621 * it and the self test should not be on.
1623 if (call
->flags
& TRACE_EVENT_FL_ENABLED
) {
1624 pr_warning("Enabled event during self test!\n");
1629 ftrace_event_enable_disable(call
, 1);
1631 ftrace_event_enable_disable(call
, 0);
1636 /* Now test at the sub system level */
1638 pr_info("Running tests on trace event systems:\n");
1640 list_for_each_entry(system
, &event_subsystems
, list
) {
1642 /* the ftrace system is special, skip it */
1643 if (strcmp(system
->name
, "ftrace") == 0)
1646 pr_info("Testing event system %s: ", system
->name
);
1648 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 1);
1649 if (WARN_ON_ONCE(ret
)) {
1650 pr_warning("error enabling system %s\n",
1657 ret
= __ftrace_set_clr_event(NULL
, system
->name
, NULL
, 0);
1658 if (WARN_ON_ONCE(ret
))
1659 pr_warning("error disabling system %s\n",
1665 /* Test with all events enabled */
1667 pr_info("Running tests on all trace events:\n");
1668 pr_info("Testing all events: ");
1670 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 1);
1671 if (WARN_ON_ONCE(ret
)) {
1672 pr_warning("error enabling all events\n");
1679 ret
= __ftrace_set_clr_event(NULL
, NULL
, NULL
, 0);
1680 if (WARN_ON_ONCE(ret
)) {
1681 pr_warning("error disabling all events\n");
1688 #ifdef CONFIG_FUNCTION_TRACER
1690 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
1693 function_test_events_call(unsigned long ip
, unsigned long parent_ip
)
1695 struct ring_buffer_event
*event
;
1696 struct ring_buffer
*buffer
;
1697 struct ftrace_entry
*entry
;
1698 unsigned long flags
;
1703 pc
= preempt_count();
1704 preempt_disable_notrace();
1705 cpu
= raw_smp_processor_id();
1706 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
1711 local_save_flags(flags
);
1713 event
= trace_current_buffer_lock_reserve(&buffer
,
1714 TRACE_FN
, sizeof(*entry
),
1718 entry
= ring_buffer_event_data(event
);
1720 entry
->parent_ip
= parent_ip
;
1722 trace_nowake_buffer_unlock_commit(buffer
, event
, flags
, pc
);
1725 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
1726 preempt_enable_notrace();
1729 static struct ftrace_ops trace_ops __initdata
=
1731 .func
= function_test_events_call
,
1734 static __init
void event_trace_self_test_with_function(void)
1737 ret
= register_ftrace_function(&trace_ops
);
1738 if (WARN_ON(ret
< 0)) {
1739 pr_info("Failed to enable function tracer for event tests\n");
1742 pr_info("Running tests again, along with the function tracer\n");
1743 event_trace_self_tests();
1744 unregister_ftrace_function(&trace_ops
);
1747 static __init
void event_trace_self_test_with_function(void)
1752 static __init
int event_trace_self_tests_init(void)
1754 if (!tracing_selftest_disabled
) {
1755 event_trace_self_tests();
1756 event_trace_self_test_with_function();
1762 late_initcall(event_trace_self_tests_init
);