4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex
);
30 DEFINE_MUTEX(event_storage_mutex
);
31 EXPORT_SYMBOL_GPL(event_storage_mutex
);
33 char event_storage
[EVENT_STORAGE_SIZE
];
34 EXPORT_SYMBOL_GPL(event_storage
);
36 LIST_HEAD(ftrace_events
);
37 LIST_HEAD(ftrace_common_fields
);
39 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41 static struct kmem_cache
*field_cachep
;
42 static struct kmem_cache
*file_cachep
;
44 /* Double loops, do not use break, only goto's work */
45 #define do_for_each_event_file(tr, file) \
46 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
47 list_for_each_entry(file, &tr->events, list)
49 #define do_for_each_event_file_safe(tr, file) \
50 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
51 struct ftrace_event_file *___n; \
52 list_for_each_entry_safe(file, ___n, &tr->events, list)
54 #define while_for_each_event_file() \
58 trace_get_fields(struct ftrace_event_call
*event_call
)
60 if (!event_call
->class->get_fields
)
61 return &event_call
->class->fields
;
62 return event_call
->class->get_fields(event_call
);
65 static int __trace_define_field(struct list_head
*head
, const char *type
,
66 const char *name
, int offset
, int size
,
67 int is_signed
, int filter_type
)
69 struct ftrace_event_field
*field
;
71 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
78 if (filter_type
== FILTER_OTHER
)
79 field
->filter_type
= filter_assign_type(type
);
81 field
->filter_type
= filter_type
;
83 field
->offset
= offset
;
85 field
->is_signed
= is_signed
;
87 list_add(&field
->link
, head
);
92 kmem_cache_free(field_cachep
, field
);
97 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
98 const char *name
, int offset
, int size
, int is_signed
,
101 struct list_head
*head
;
103 if (WARN_ON(!call
->class))
106 head
= trace_get_fields(call
);
107 return __trace_define_field(head
, type
, name
, offset
, size
,
108 is_signed
, filter_type
);
110 EXPORT_SYMBOL_GPL(trace_define_field
);
112 #define __common_field(type, item) \
113 ret = __trace_define_field(&ftrace_common_fields, #type, \
115 offsetof(typeof(ent), item), \
117 is_signed_type(type), FILTER_OTHER); \
121 static int trace_define_common_fields(void)
124 struct trace_entry ent
;
126 __common_field(unsigned short, type
);
127 __common_field(unsigned char, flags
);
128 __common_field(unsigned char, preempt_count
);
129 __common_field(int, pid
);
134 void trace_destroy_fields(struct ftrace_event_call
*call
)
136 struct ftrace_event_field
*field
, *next
;
137 struct list_head
*head
;
139 head
= trace_get_fields(call
);
140 list_for_each_entry_safe(field
, next
, head
, link
) {
141 list_del(&field
->link
);
142 kmem_cache_free(field_cachep
, field
);
146 int trace_event_raw_init(struct ftrace_event_call
*call
)
150 id
= register_ftrace_event(&call
->event
);
156 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
158 int ftrace_event_reg(struct ftrace_event_call
*call
,
159 enum trace_reg type
, void *data
)
161 struct ftrace_event_file
*file
= data
;
164 case TRACE_REG_REGISTER
:
165 return tracepoint_probe_register(call
->name
,
168 case TRACE_REG_UNREGISTER
:
169 tracepoint_probe_unregister(call
->name
,
174 #ifdef CONFIG_PERF_EVENTS
175 case TRACE_REG_PERF_REGISTER
:
176 return tracepoint_probe_register(call
->name
,
177 call
->class->perf_probe
,
179 case TRACE_REG_PERF_UNREGISTER
:
180 tracepoint_probe_unregister(call
->name
,
181 call
->class->perf_probe
,
184 case TRACE_REG_PERF_OPEN
:
185 case TRACE_REG_PERF_CLOSE
:
186 case TRACE_REG_PERF_ADD
:
187 case TRACE_REG_PERF_DEL
:
193 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
195 void trace_event_enable_cmd_record(bool enable
)
197 struct ftrace_event_file
*file
;
198 struct trace_array
*tr
;
200 mutex_lock(&event_mutex
);
201 do_for_each_event_file(tr
, file
) {
203 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
207 tracing_start_cmdline_record();
208 file
->flags
|= FTRACE_EVENT_FL_RECORDED_CMD
;
210 tracing_stop_cmdline_record();
211 file
->flags
&= ~FTRACE_EVENT_FL_RECORDED_CMD
;
213 } while_for_each_event_file();
214 mutex_unlock(&event_mutex
);
217 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
220 struct ftrace_event_call
*call
= file
->event_call
;
225 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
226 file
->flags
&= ~FTRACE_EVENT_FL_ENABLED
;
227 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
228 tracing_stop_cmdline_record();
229 file
->flags
&= ~FTRACE_EVENT_FL_RECORDED_CMD
;
231 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
235 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
236 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
237 tracing_start_cmdline_record();
238 file
->flags
|= FTRACE_EVENT_FL_RECORDED_CMD
;
240 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
242 tracing_stop_cmdline_record();
243 pr_info("event trace: Could not enable event "
247 file
->flags
|= FTRACE_EVENT_FL_ENABLED
;
249 /* WAS_ENABLED gets set but never cleared. */
250 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
258 static void ftrace_clear_events(struct trace_array
*tr
)
260 struct ftrace_event_file
*file
;
262 mutex_lock(&event_mutex
);
263 list_for_each_entry(file
, &tr
->events
, list
) {
264 ftrace_event_enable_disable(file
, 0);
266 mutex_unlock(&event_mutex
);
269 static void __put_system(struct event_subsystem
*system
)
271 struct event_filter
*filter
= system
->filter
;
273 WARN_ON_ONCE(system
->ref_count
== 0);
274 if (--system
->ref_count
)
277 list_del(&system
->list
);
280 kfree(filter
->filter_string
);
286 static void __get_system(struct event_subsystem
*system
)
288 WARN_ON_ONCE(system
->ref_count
== 0);
292 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
294 WARN_ON_ONCE(dir
->ref_count
== 0);
296 __get_system(dir
->subsystem
);
299 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
301 WARN_ON_ONCE(dir
->ref_count
== 0);
302 /* If the subsystem is about to be freed, the dir must be too */
303 WARN_ON_ONCE(dir
->subsystem
->ref_count
== 1 && dir
->ref_count
!= 1);
305 __put_system(dir
->subsystem
);
306 if (!--dir
->ref_count
)
310 static void put_system(struct ftrace_subsystem_dir
*dir
)
312 mutex_lock(&event_mutex
);
313 __put_system_dir(dir
);
314 mutex_unlock(&event_mutex
);
318 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
320 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
321 const char *sub
, const char *event
, int set
)
323 struct ftrace_event_file
*file
;
324 struct ftrace_event_call
*call
;
327 mutex_lock(&event_mutex
);
328 list_for_each_entry(file
, &tr
->events
, list
) {
330 call
= file
->event_call
;
332 if (!call
->name
|| !call
->class || !call
->class->reg
)
335 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
339 strcmp(match
, call
->name
) != 0 &&
340 strcmp(match
, call
->class->system
) != 0)
343 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
346 if (event
&& strcmp(event
, call
->name
) != 0)
349 ftrace_event_enable_disable(file
, set
);
353 mutex_unlock(&event_mutex
);
358 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
360 char *event
= NULL
, *sub
= NULL
, *match
;
363 * The buf format can be <subsystem>:<event-name>
364 * *:<event-name> means any event by that name.
365 * :<event-name> is the same.
367 * <subsystem>:* means all events in that subsystem
368 * <subsystem>: means the same.
370 * <name> (no ':') means all events in a subsystem with
371 * the name <name> or any event that matches <name>
374 match
= strsep(&buf
, ":");
380 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
382 if (!strlen(event
) || strcmp(event
, "*") == 0)
386 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
390 * trace_set_clr_event - enable or disable an event
391 * @system: system name to match (NULL for any system)
392 * @event: event name to match (NULL for all events, within system)
393 * @set: 1 to enable, 0 to disable
395 * This is a way for other parts of the kernel to enable or disable
398 * Returns 0 on success, -EINVAL if the parameters do not match any
401 int trace_set_clr_event(const char *system
, const char *event
, int set
)
403 struct trace_array
*tr
= top_trace_array();
405 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
407 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
409 /* 128 should be much more than enough */
410 #define EVENT_BUF_SIZE 127
413 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
414 size_t cnt
, loff_t
*ppos
)
416 struct trace_parser parser
;
417 struct seq_file
*m
= file
->private_data
;
418 struct trace_array
*tr
= m
->private;
424 ret
= tracing_update_buffers();
428 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
431 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
433 if (read
>= 0 && trace_parser_loaded((&parser
))) {
436 if (*parser
.buffer
== '!')
439 parser
.buffer
[parser
.idx
] = 0;
441 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
449 trace_parser_put(&parser
);
455 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
457 struct ftrace_event_file
*file
= v
;
458 struct ftrace_event_call
*call
;
459 struct trace_array
*tr
= m
->private;
463 list_for_each_entry_continue(file
, &tr
->events
, list
) {
464 call
= file
->event_call
;
466 * The ftrace subsystem is for showing formats only.
467 * They can not be enabled or disabled via the event files.
469 if (call
->class && call
->class->reg
)
476 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
478 struct ftrace_event_file
*file
;
479 struct trace_array
*tr
= m
->private;
482 mutex_lock(&event_mutex
);
484 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
485 for (l
= 0; l
<= *pos
; ) {
486 file
= t_next(m
, file
, &l
);
494 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
496 struct ftrace_event_file
*file
= v
;
497 struct trace_array
*tr
= m
->private;
501 list_for_each_entry_continue(file
, &tr
->events
, list
) {
502 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
509 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
511 struct ftrace_event_file
*file
;
512 struct trace_array
*tr
= m
->private;
515 mutex_lock(&event_mutex
);
517 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
518 for (l
= 0; l
<= *pos
; ) {
519 file
= s_next(m
, file
, &l
);
526 static int t_show(struct seq_file
*m
, void *v
)
528 struct ftrace_event_file
*file
= v
;
529 struct ftrace_event_call
*call
= file
->event_call
;
531 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
532 seq_printf(m
, "%s:", call
->class->system
);
533 seq_printf(m
, "%s\n", call
->name
);
538 static void t_stop(struct seq_file
*m
, void *p
)
540 mutex_unlock(&event_mutex
);
544 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
547 struct ftrace_event_file
*file
= filp
->private_data
;
550 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
555 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
559 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
562 struct ftrace_event_file
*file
= filp
->private_data
;
569 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
573 ret
= tracing_update_buffers();
580 mutex_lock(&event_mutex
);
581 ret
= ftrace_event_enable_disable(file
, val
);
582 mutex_unlock(&event_mutex
);
591 return ret
? ret
: cnt
;
595 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
598 const char set_to_char
[4] = { '?', '0', '1', 'X' };
599 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
600 struct event_subsystem
*system
= dir
->subsystem
;
601 struct ftrace_event_call
*call
;
602 struct ftrace_event_file
*file
;
603 struct trace_array
*tr
= dir
->tr
;
608 mutex_lock(&event_mutex
);
609 list_for_each_entry(file
, &tr
->events
, list
) {
610 call
= file
->event_call
;
611 if (!call
->name
|| !call
->class || !call
->class->reg
)
614 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
618 * We need to find out if all the events are set
619 * or if all events or cleared, or if we have
622 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
625 * If we have a mixture, no need to look further.
630 mutex_unlock(&event_mutex
);
632 buf
[0] = set_to_char
[set
];
635 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
641 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
644 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
645 struct event_subsystem
*system
= dir
->subsystem
;
646 const char *name
= NULL
;
650 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
654 ret
= tracing_update_buffers();
658 if (val
!= 0 && val
!= 1)
662 * Opening of "enable" adds a ref count to system,
663 * so the name is safe to use.
668 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
682 FORMAT_FIELD_SEPERATOR
= 2,
686 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
688 struct ftrace_event_call
*call
= m
->private;
689 struct ftrace_event_field
*field
;
690 struct list_head
*common_head
= &ftrace_common_fields
;
691 struct list_head
*head
= trace_get_fields(call
);
695 switch ((unsigned long)v
) {
697 if (unlikely(list_empty(common_head
)))
700 field
= list_entry(common_head
->prev
,
701 struct ftrace_event_field
, link
);
704 case FORMAT_FIELD_SEPERATOR
:
705 if (unlikely(list_empty(head
)))
708 field
= list_entry(head
->prev
, struct ftrace_event_field
, link
);
711 case FORMAT_PRINTFMT
:
717 if (field
->link
.prev
== common_head
)
718 return (void *)FORMAT_FIELD_SEPERATOR
;
719 else if (field
->link
.prev
== head
)
720 return (void *)FORMAT_PRINTFMT
;
722 field
= list_entry(field
->link
.prev
, struct ftrace_event_field
, link
);
727 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
732 /* Start by showing the header */
734 return (void *)FORMAT_HEADER
;
736 p
= (void *)FORMAT_HEADER
;
738 p
= f_next(m
, p
, &l
);
739 } while (p
&& l
< *pos
);
744 static int f_show(struct seq_file
*m
, void *v
)
746 struct ftrace_event_call
*call
= m
->private;
747 struct ftrace_event_field
*field
;
748 const char *array_descriptor
;
750 switch ((unsigned long)v
) {
752 seq_printf(m
, "name: %s\n", call
->name
);
753 seq_printf(m
, "ID: %d\n", call
->event
.type
);
754 seq_printf(m
, "format:\n");
757 case FORMAT_FIELD_SEPERATOR
:
761 case FORMAT_PRINTFMT
:
762 seq_printf(m
, "\nprint fmt: %s\n",
770 * Smartly shows the array type(except dynamic array).
773 * If TYPE := TYPE[LEN], it is shown:
774 * field:TYPE VAR[LEN]
776 array_descriptor
= strchr(field
->type
, '[');
778 if (!strncmp(field
->type
, "__data_loc", 10))
779 array_descriptor
= NULL
;
781 if (!array_descriptor
)
782 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
783 field
->type
, field
->name
, field
->offset
,
784 field
->size
, !!field
->is_signed
);
786 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
787 (int)(array_descriptor
- field
->type
),
788 field
->type
, field
->name
,
789 array_descriptor
, field
->offset
,
790 field
->size
, !!field
->is_signed
);
795 static void f_stop(struct seq_file
*m
, void *p
)
799 static const struct seq_operations trace_format_seq_ops
= {
806 static int trace_format_open(struct inode
*inode
, struct file
*file
)
808 struct ftrace_event_call
*call
= inode
->i_private
;
812 ret
= seq_open(file
, &trace_format_seq_ops
);
816 m
= file
->private_data
;
823 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
825 struct ftrace_event_call
*call
= filp
->private_data
;
832 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
837 trace_seq_printf(s
, "%d\n", call
->event
.type
);
839 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
846 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
849 struct ftrace_event_call
*call
= filp
->private_data
;
856 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
862 print_event_filter(call
, s
);
863 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
871 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
874 struct ftrace_event_call
*call
= filp
->private_data
;
878 if (cnt
>= PAGE_SIZE
)
881 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
885 if (copy_from_user(buf
, ubuf
, cnt
)) {
886 free_page((unsigned long) buf
);
891 err
= apply_event_filter(call
, buf
);
892 free_page((unsigned long) buf
);
901 static LIST_HEAD(event_subsystems
);
903 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
905 struct event_subsystem
*system
= NULL
;
906 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
907 struct trace_array
*tr
;
910 /* Make sure the system still exists */
911 mutex_lock(&event_mutex
);
912 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
913 list_for_each_entry(dir
, &tr
->systems
, list
) {
914 if (dir
== inode
->i_private
) {
915 /* Don't open systems with no events */
916 if (dir
->nr_events
) {
917 __get_system_dir(dir
);
918 system
= dir
->subsystem
;
925 mutex_unlock(&event_mutex
);
930 /* Some versions of gcc think dir can be uninitialized here */
933 ret
= tracing_open_generic(inode
, filp
);
940 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
942 struct ftrace_subsystem_dir
*dir
;
943 struct trace_array
*tr
= inode
->i_private
;
946 /* Make a temporary dir that has no system but points to tr */
947 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
953 ret
= tracing_open_generic(inode
, filp
);
957 filp
->private_data
= dir
;
962 static int subsystem_release(struct inode
*inode
, struct file
*file
)
964 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
967 * If dir->subsystem is NULL, then this is a temporary
968 * descriptor that was made for a trace_array to enable
980 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
983 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
984 struct event_subsystem
*system
= dir
->subsystem
;
991 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
997 print_subsystem_event_filter(system
, s
);
998 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1006 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1009 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1013 if (cnt
>= PAGE_SIZE
)
1016 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1020 if (copy_from_user(buf
, ubuf
, cnt
)) {
1021 free_page((unsigned long) buf
);
1026 err
= apply_subsystem_event_filter(dir
, buf
);
1027 free_page((unsigned long) buf
);
1037 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1039 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1040 struct trace_seq
*s
;
1046 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1053 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1060 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1061 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1063 static const struct seq_operations show_event_seq_ops
= {
1070 static const struct seq_operations show_set_event_seq_ops
= {
1077 static const struct file_operations ftrace_avail_fops
= {
1078 .open
= ftrace_event_avail_open
,
1080 .llseek
= seq_lseek
,
1081 .release
= seq_release
,
1084 static const struct file_operations ftrace_set_event_fops
= {
1085 .open
= ftrace_event_set_open
,
1087 .write
= ftrace_event_write
,
1088 .llseek
= seq_lseek
,
1089 .release
= seq_release
,
1092 static const struct file_operations ftrace_enable_fops
= {
1093 .open
= tracing_open_generic
,
1094 .read
= event_enable_read
,
1095 .write
= event_enable_write
,
1096 .llseek
= default_llseek
,
1099 static const struct file_operations ftrace_event_format_fops
= {
1100 .open
= trace_format_open
,
1102 .llseek
= seq_lseek
,
1103 .release
= seq_release
,
1106 static const struct file_operations ftrace_event_id_fops
= {
1107 .open
= tracing_open_generic
,
1108 .read
= event_id_read
,
1109 .llseek
= default_llseek
,
1112 static const struct file_operations ftrace_event_filter_fops
= {
1113 .open
= tracing_open_generic
,
1114 .read
= event_filter_read
,
1115 .write
= event_filter_write
,
1116 .llseek
= default_llseek
,
1119 static const struct file_operations ftrace_subsystem_filter_fops
= {
1120 .open
= subsystem_open
,
1121 .read
= subsystem_filter_read
,
1122 .write
= subsystem_filter_write
,
1123 .llseek
= default_llseek
,
1124 .release
= subsystem_release
,
1127 static const struct file_operations ftrace_system_enable_fops
= {
1128 .open
= subsystem_open
,
1129 .read
= system_enable_read
,
1130 .write
= system_enable_write
,
1131 .llseek
= default_llseek
,
1132 .release
= subsystem_release
,
1135 static const struct file_operations ftrace_tr_enable_fops
= {
1136 .open
= system_tr_open
,
1137 .read
= system_enable_read
,
1138 .write
= system_enable_write
,
1139 .llseek
= default_llseek
,
1140 .release
= subsystem_release
,
1143 static const struct file_operations ftrace_show_header_fops
= {
1144 .open
= tracing_open_generic
,
1145 .read
= show_header
,
1146 .llseek
= default_llseek
,
1150 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1151 const struct seq_operations
*seq_ops
)
1156 ret
= seq_open(file
, seq_ops
);
1159 m
= file
->private_data
;
1160 /* copy tr over to seq ops */
1161 m
->private = inode
->i_private
;
1167 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1169 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1171 return ftrace_event_open(inode
, file
, seq_ops
);
1175 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1177 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1178 struct trace_array
*tr
= inode
->i_private
;
1180 if ((file
->f_mode
& FMODE_WRITE
) &&
1181 (file
->f_flags
& O_TRUNC
))
1182 ftrace_clear_events(tr
);
1184 return ftrace_event_open(inode
, file
, seq_ops
);
1187 static struct event_subsystem
*
1188 create_new_subsystem(const char *name
)
1190 struct event_subsystem
*system
;
1192 /* need to create new entry */
1193 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1197 system
->ref_count
= 1;
1198 system
->name
= name
;
1200 system
->filter
= NULL
;
1202 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1203 if (!system
->filter
)
1206 list_add(&system
->list
, &event_subsystems
);
1215 static struct dentry
*
1216 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1217 struct ftrace_event_file
*file
, struct dentry
*parent
)
1219 struct ftrace_subsystem_dir
*dir
;
1220 struct event_subsystem
*system
;
1221 struct dentry
*entry
;
1223 /* First see if we did not already create this dir */
1224 list_for_each_entry(dir
, &tr
->systems
, list
) {
1225 system
= dir
->subsystem
;
1226 if (strcmp(system
->name
, name
) == 0) {
1233 /* Now see if the system itself exists. */
1234 list_for_each_entry(system
, &event_subsystems
, list
) {
1235 if (strcmp(system
->name
, name
) == 0)
1238 /* Reset system variable when not found */
1239 if (&system
->list
== &event_subsystems
)
1242 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1247 system
= create_new_subsystem(name
);
1251 __get_system(system
);
1253 dir
->entry
= debugfs_create_dir(name
, parent
);
1255 pr_warning("Failed to create system directory %s\n", name
);
1256 __put_system(system
);
1263 dir
->subsystem
= system
;
1266 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1267 &ftrace_subsystem_filter_fops
);
1269 kfree(system
->filter
);
1270 system
->filter
= NULL
;
1271 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1274 trace_create_file("enable", 0644, dir
->entry
, dir
,
1275 &ftrace_system_enable_fops
);
1277 list_add(&dir
->list
, &tr
->systems
);
1284 /* Only print this message if failed on memory allocation */
1285 if (!dir
|| !system
)
1286 pr_warning("No memory to create event subsystem %s\n",
1292 event_create_dir(struct dentry
*parent
,
1293 struct ftrace_event_file
*file
,
1294 const struct file_operations
*id
,
1295 const struct file_operations
*enable
,
1296 const struct file_operations
*filter
,
1297 const struct file_operations
*format
)
1299 struct ftrace_event_call
*call
= file
->event_call
;
1300 struct trace_array
*tr
= file
->tr
;
1301 struct list_head
*head
;
1302 struct dentry
*d_events
;
1306 * If the trace point header did not define TRACE_SYSTEM
1307 * then the system would be called "TRACE_SYSTEM".
1309 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1310 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1316 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1318 pr_warning("Could not create debugfs '%s' directory\n",
1323 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1324 trace_create_file("enable", 0644, file
->dir
, file
,
1327 #ifdef CONFIG_PERF_EVENTS
1328 if (call
->event
.type
&& call
->class->reg
)
1329 trace_create_file("id", 0444, file
->dir
, call
,
1334 * Other events may have the same class. Only update
1335 * the fields if they are not already defined.
1337 head
= trace_get_fields(call
);
1338 if (list_empty(head
)) {
1339 ret
= call
->class->define_fields(call
);
1341 pr_warning("Could not initialize trace point"
1342 " events/%s\n", call
->name
);
1346 trace_create_file("filter", 0644, file
->dir
, call
,
1349 trace_create_file("format", 0444, file
->dir
, call
,
1355 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
1360 if (!--dir
->nr_events
) {
1361 debugfs_remove_recursive(dir
->entry
);
1362 list_del(&dir
->list
);
1363 __put_system_dir(dir
);
1367 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1369 struct ftrace_event_file
*file
;
1370 struct trace_array
*tr
;
1372 do_for_each_event_file_safe(tr
, file
) {
1374 if (file
->event_call
!= call
)
1377 list_del(&file
->list
);
1378 debugfs_remove_recursive(file
->dir
);
1379 remove_subsystem(file
->system
);
1380 kmem_cache_free(file_cachep
, file
);
1383 * The do_for_each_event_file_safe() is
1384 * a double loop. After finding the call for this
1385 * trace_array, we use break to jump to the next
1389 } while_for_each_event_file();
1392 static void event_remove(struct ftrace_event_call
*call
)
1394 struct trace_array
*tr
;
1395 struct ftrace_event_file
*file
;
1397 do_for_each_event_file(tr
, file
) {
1398 if (file
->event_call
!= call
)
1400 ftrace_event_enable_disable(file
, 0);
1402 * The do_for_each_event_file() is
1403 * a double loop. After finding the call for this
1404 * trace_array, we use break to jump to the next
1408 } while_for_each_event_file();
1410 if (call
->event
.funcs
)
1411 __unregister_ftrace_event(&call
->event
);
1412 remove_event_from_tracers(call
);
1413 list_del(&call
->list
);
1416 static int event_init(struct ftrace_event_call
*call
)
1420 if (WARN_ON(!call
->name
))
1423 if (call
->class->raw_init
) {
1424 ret
= call
->class->raw_init(call
);
1425 if (ret
< 0 && ret
!= -ENOSYS
)
1426 pr_warn("Could not initialize trace events/%s\n",
1434 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1438 ret
= event_init(call
);
1442 list_add(&call
->list
, &ftrace_events
);
1448 /* Add an event to a trace directory */
1450 __trace_add_new_event(struct ftrace_event_call
*call
,
1451 struct trace_array
*tr
,
1452 const struct file_operations
*id
,
1453 const struct file_operations
*enable
,
1454 const struct file_operations
*filter
,
1455 const struct file_operations
*format
)
1457 struct ftrace_event_file
*file
;
1459 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1463 file
->event_call
= call
;
1465 list_add(&file
->list
, &tr
->events
);
1467 return event_create_dir(tr
->event_dir
, file
, id
, enable
, filter
, format
);
1471 * Just create a decriptor for early init. A descriptor is required
1472 * for enabling events at boot. We want to enable events before
1473 * the filesystem is initialized.
1476 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1477 struct trace_array
*tr
)
1479 struct ftrace_event_file
*file
;
1481 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1485 file
->event_call
= call
;
1487 list_add(&file
->list
, &tr
->events
);
1492 struct ftrace_module_file_ops
;
1493 static void __add_event_to_tracers(struct ftrace_event_call
*call
,
1494 struct ftrace_module_file_ops
*file_ops
);
1496 /* Add an additional event_call dynamically */
1497 int trace_add_event_call(struct ftrace_event_call
*call
)
1500 mutex_lock(&event_mutex
);
1502 ret
= __register_event(call
, NULL
);
1504 __add_event_to_tracers(call
, NULL
);
1506 mutex_unlock(&event_mutex
);
1511 * Must be called under locking both of event_mutex and trace_event_mutex.
1513 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1516 trace_destroy_fields(call
);
1517 destroy_preds(call
);
1520 /* Remove an event_call */
1521 void trace_remove_event_call(struct ftrace_event_call
*call
)
1523 mutex_lock(&event_mutex
);
1524 down_write(&trace_event_mutex
);
1525 __trace_remove_event_call(call
);
1526 up_write(&trace_event_mutex
);
1527 mutex_unlock(&event_mutex
);
1530 #define for_each_event(event, start, end) \
1531 for (event = start; \
1532 (unsigned long)event < (unsigned long)end; \
1535 #ifdef CONFIG_MODULES
1537 static LIST_HEAD(ftrace_module_file_list
);
1540 * Modules must own their file_operations to keep up with
1541 * reference counting.
1543 struct ftrace_module_file_ops
{
1544 struct list_head list
;
1546 struct file_operations id
;
1547 struct file_operations enable
;
1548 struct file_operations format
;
1549 struct file_operations filter
;
1552 static struct ftrace_module_file_ops
*
1553 find_ftrace_file_ops(struct ftrace_module_file_ops
*file_ops
, struct module
*mod
)
1556 * As event_calls are added in groups by module,
1557 * when we find one file_ops, we don't need to search for
1558 * each call in that module, as the rest should be the
1559 * same. Only search for a new one if the last one did
1562 if (file_ops
&& mod
== file_ops
->mod
)
1565 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1566 if (file_ops
->mod
== mod
)
1572 static struct ftrace_module_file_ops
*
1573 trace_create_file_ops(struct module
*mod
)
1575 struct ftrace_module_file_ops
*file_ops
;
1578 * This is a bit of a PITA. To allow for correct reference
1579 * counting, modules must "own" their file_operations.
1580 * To do this, we allocate the file operations that will be
1581 * used in the event directory.
1584 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1588 file_ops
->mod
= mod
;
1590 file_ops
->id
= ftrace_event_id_fops
;
1591 file_ops
->id
.owner
= mod
;
1593 file_ops
->enable
= ftrace_enable_fops
;
1594 file_ops
->enable
.owner
= mod
;
1596 file_ops
->filter
= ftrace_event_filter_fops
;
1597 file_ops
->filter
.owner
= mod
;
1599 file_ops
->format
= ftrace_event_format_fops
;
1600 file_ops
->format
.owner
= mod
;
1602 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1607 static void trace_module_add_events(struct module
*mod
)
1609 struct ftrace_module_file_ops
*file_ops
= NULL
;
1610 struct ftrace_event_call
**call
, **start
, **end
;
1612 start
= mod
->trace_events
;
1613 end
= mod
->trace_events
+ mod
->num_trace_events
;
1618 file_ops
= trace_create_file_ops(mod
);
1622 for_each_event(call
, start
, end
) {
1623 __register_event(*call
, mod
);
1624 __add_event_to_tracers(*call
, file_ops
);
1628 static void trace_module_remove_events(struct module
*mod
)
1630 struct ftrace_module_file_ops
*file_ops
;
1631 struct ftrace_event_call
*call
, *p
;
1632 bool clear_trace
= false;
1634 down_write(&trace_event_mutex
);
1635 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1636 if (call
->mod
== mod
) {
1637 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1639 __trace_remove_event_call(call
);
1643 /* Now free the file_operations */
1644 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1645 if (file_ops
->mod
== mod
)
1648 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1649 list_del(&file_ops
->list
);
1652 up_write(&trace_event_mutex
);
1655 * It is safest to reset the ring buffer if the module being unloaded
1656 * registered any events that were used. The only worry is if
1657 * a new module gets loaded, and takes on the same id as the events
1658 * of this module. When printing out the buffer, traced events left
1659 * over from this module may be passed to the new module events and
1660 * unexpected results may occur.
1663 tracing_reset_all_online_cpus();
1666 static int trace_module_notify(struct notifier_block
*self
,
1667 unsigned long val
, void *data
)
1669 struct module
*mod
= data
;
1671 mutex_lock(&event_mutex
);
1673 case MODULE_STATE_COMING
:
1674 trace_module_add_events(mod
);
1676 case MODULE_STATE_GOING
:
1677 trace_module_remove_events(mod
);
1680 mutex_unlock(&event_mutex
);
1686 __trace_add_new_mod_event(struct ftrace_event_call
*call
,
1687 struct trace_array
*tr
,
1688 struct ftrace_module_file_ops
*file_ops
)
1690 return __trace_add_new_event(call
, tr
,
1691 &file_ops
->id
, &file_ops
->enable
,
1692 &file_ops
->filter
, &file_ops
->format
);
1696 static inline struct ftrace_module_file_ops
*
1697 find_ftrace_file_ops(struct ftrace_module_file_ops
*file_ops
, struct module
*mod
)
1701 static inline int trace_module_notify(struct notifier_block
*self
,
1702 unsigned long val
, void *data
)
1707 __trace_add_new_mod_event(struct ftrace_event_call
*call
,
1708 struct trace_array
*tr
,
1709 struct ftrace_module_file_ops
*file_ops
)
1713 #endif /* CONFIG_MODULES */
1715 /* Create a new event directory structure for a trace directory. */
1717 __trace_add_event_dirs(struct trace_array
*tr
)
1719 struct ftrace_module_file_ops
*file_ops
= NULL
;
1720 struct ftrace_event_call
*call
;
1723 list_for_each_entry(call
, &ftrace_events
, list
) {
1726 * Directories for events by modules need to
1727 * keep module ref counts when opened (as we don't
1728 * want the module to disappear when reading one
1729 * of these files). The file_ops keep account of
1730 * the module ref count.
1732 file_ops
= find_ftrace_file_ops(file_ops
, call
->mod
);
1734 continue; /* Warn? */
1735 ret
= __trace_add_new_mod_event(call
, tr
, file_ops
);
1737 pr_warning("Could not create directory for event %s\n",
1741 ret
= __trace_add_new_event(call
, tr
,
1742 &ftrace_event_id_fops
,
1743 &ftrace_enable_fops
,
1744 &ftrace_event_filter_fops
,
1745 &ftrace_event_format_fops
);
1747 pr_warning("Could not create directory for event %s\n",
1753 * The top level array has already had its ftrace_event_file
1754 * descriptors created in order to allow for early events to
1755 * be recorded. This function is called after the debugfs has been
1756 * initialized, and we now have to create the files associated
1760 __trace_early_add_event_dirs(struct trace_array
*tr
)
1762 struct ftrace_event_file
*file
;
1766 list_for_each_entry(file
, &tr
->events
, list
) {
1767 ret
= event_create_dir(tr
->event_dir
, file
,
1768 &ftrace_event_id_fops
,
1769 &ftrace_enable_fops
,
1770 &ftrace_event_filter_fops
,
1771 &ftrace_event_format_fops
);
1773 pr_warning("Could not create directory for event %s\n",
1774 file
->event_call
->name
);
1779 * For early boot up, the top trace array requires to have
1780 * a list of events that can be enabled. This must be done before
1781 * the filesystem is set up in order to allow events to be traced
1785 __trace_early_add_events(struct trace_array
*tr
)
1787 struct ftrace_event_call
*call
;
1790 list_for_each_entry(call
, &ftrace_events
, list
) {
1791 /* Early boot up should not have any modules loaded */
1792 if (WARN_ON_ONCE(call
->mod
))
1795 ret
= __trace_early_add_new_event(call
, tr
);
1797 pr_warning("Could not create early event %s\n",
1802 /* Remove the event directory structure for a trace directory. */
1804 __trace_remove_event_dirs(struct trace_array
*tr
)
1806 struct ftrace_event_file
*file
, *next
;
1808 list_for_each_entry_safe(file
, next
, &tr
->events
, list
) {
1809 list_del(&file
->list
);
1810 debugfs_remove_recursive(file
->dir
);
1811 remove_subsystem(file
->system
);
1812 kmem_cache_free(file_cachep
, file
);
1817 __add_event_to_tracers(struct ftrace_event_call
*call
,
1818 struct ftrace_module_file_ops
*file_ops
)
1820 struct trace_array
*tr
;
1822 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1824 __trace_add_new_mod_event(call
, tr
, file_ops
);
1826 __trace_add_new_event(call
, tr
,
1827 &ftrace_event_id_fops
,
1828 &ftrace_enable_fops
,
1829 &ftrace_event_filter_fops
,
1830 &ftrace_event_format_fops
);
1834 static struct notifier_block trace_module_nb
= {
1835 .notifier_call
= trace_module_notify
,
1839 extern struct ftrace_event_call
*__start_ftrace_events
[];
1840 extern struct ftrace_event_call
*__stop_ftrace_events
[];
1842 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
1844 static __init
int setup_trace_event(char *str
)
1846 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
1847 ring_buffer_expanded
= true;
1848 tracing_selftest_disabled
= true;
1852 __setup("trace_event=", setup_trace_event
);
1854 /* Expects to have event_mutex held when called */
1856 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
1858 struct dentry
*d_events
;
1859 struct dentry
*entry
;
1861 entry
= debugfs_create_file("set_event", 0644, parent
,
1862 tr
, &ftrace_set_event_fops
);
1864 pr_warning("Could not create debugfs 'set_event' entry\n");
1868 d_events
= debugfs_create_dir("events", parent
);
1870 pr_warning("Could not create debugfs 'events' directory\n");
1874 /* ring buffer internal formats */
1875 trace_create_file("header_page", 0444, d_events
,
1876 ring_buffer_print_page_header
,
1877 &ftrace_show_header_fops
);
1879 trace_create_file("header_event", 0444, d_events
,
1880 ring_buffer_print_entry_header
,
1881 &ftrace_show_header_fops
);
1883 trace_create_file("enable", 0644, d_events
,
1884 tr
, &ftrace_tr_enable_fops
);
1886 tr
->event_dir
= d_events
;
1892 * event_trace_add_tracer - add a instance of a trace_array to events
1893 * @parent: The parent dentry to place the files/directories for events in
1894 * @tr: The trace array associated with these events
1896 * When a new instance is created, it needs to set up its events
1897 * directory, as well as other files associated with events. It also
1898 * creates the event hierachry in the @parent/events directory.
1900 * Returns 0 on success.
1902 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
1906 mutex_lock(&event_mutex
);
1908 ret
= create_event_toplevel_files(parent
, tr
);
1912 down_write(&trace_event_mutex
);
1913 __trace_add_event_dirs(tr
);
1914 up_write(&trace_event_mutex
);
1917 mutex_unlock(&event_mutex
);
1923 * The top trace array already had its file descriptors created.
1924 * Now the files themselves need to be created.
1927 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
1931 mutex_lock(&event_mutex
);
1933 ret
= create_event_toplevel_files(parent
, tr
);
1937 down_write(&trace_event_mutex
);
1938 __trace_early_add_event_dirs(tr
);
1939 up_write(&trace_event_mutex
);
1942 mutex_unlock(&event_mutex
);
1947 int event_trace_del_tracer(struct trace_array
*tr
)
1949 /* Disable any running events */
1950 __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
1952 mutex_lock(&event_mutex
);
1954 down_write(&trace_event_mutex
);
1955 __trace_remove_event_dirs(tr
);
1956 debugfs_remove_recursive(tr
->event_dir
);
1957 up_write(&trace_event_mutex
);
1959 tr
->event_dir
= NULL
;
1961 mutex_unlock(&event_mutex
);
1966 static __init
int event_trace_memsetup(void)
1968 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
1969 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
1973 static __init
int event_trace_enable(void)
1975 struct trace_array
*tr
= top_trace_array();
1976 struct ftrace_event_call
**iter
, *call
;
1977 char *buf
= bootup_event_buf
;
1981 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
1984 ret
= event_init(call
);
1986 list_add(&call
->list
, &ftrace_events
);
1990 * We need the top trace array to have a working set of trace
1991 * points at early init, before the debug files and directories
1992 * are created. Create the file entries now, and attach them
1993 * to the actual file dentries later.
1995 __trace_early_add_events(tr
);
1998 token
= strsep(&buf
, ",");
2005 ret
= ftrace_set_clr_event(tr
, token
, 1);
2007 pr_warn("Failed to enable trace event: %s\n", token
);
2010 trace_printk_start_comm();
2015 static __init
int event_trace_init(void)
2017 struct trace_array
*tr
;
2018 struct dentry
*d_tracer
;
2019 struct dentry
*entry
;
2022 tr
= top_trace_array();
2024 d_tracer
= tracing_init_dentry();
2028 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2029 tr
, &ftrace_avail_fops
);
2031 pr_warning("Could not create debugfs "
2032 "'available_events' entry\n");
2034 if (trace_define_common_fields())
2035 pr_warning("tracing: Failed to allocate common fields");
2037 ret
= early_event_add_tracer(d_tracer
, tr
);
2041 ret
= register_module_notifier(&trace_module_nb
);
2043 pr_warning("Failed to register trace events module notifier\n");
2047 early_initcall(event_trace_memsetup
);
2048 core_initcall(event_trace_enable
);
2049 fs_initcall(event_trace_init
);
2051 #ifdef CONFIG_FTRACE_STARTUP_TEST
2053 static DEFINE_SPINLOCK(test_spinlock
);
2054 static DEFINE_SPINLOCK(test_spinlock_irq
);
2055 static DEFINE_MUTEX(test_mutex
);
2057 static __init
void test_work(struct work_struct
*dummy
)
2059 spin_lock(&test_spinlock
);
2060 spin_lock_irq(&test_spinlock_irq
);
2062 spin_unlock_irq(&test_spinlock_irq
);
2063 spin_unlock(&test_spinlock
);
2065 mutex_lock(&test_mutex
);
2067 mutex_unlock(&test_mutex
);
2070 static __init
int event_test_thread(void *unused
)
2074 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2076 pr_info("failed to kmalloc\n");
2078 schedule_on_each_cpu(test_work
);
2082 set_current_state(TASK_INTERRUPTIBLE
);
2083 while (!kthread_should_stop())
2090 * Do various things that may trigger events.
2092 static __init
void event_test_stuff(void)
2094 struct task_struct
*test_thread
;
2096 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2098 kthread_stop(test_thread
);
2102 * For every trace event defined, we will test each trace point separately,
2103 * and then by groups, and finally all trace points.
2105 static __init
void event_trace_self_tests(void)
2107 struct ftrace_subsystem_dir
*dir
;
2108 struct ftrace_event_file
*file
;
2109 struct ftrace_event_call
*call
;
2110 struct event_subsystem
*system
;
2111 struct trace_array
*tr
;
2114 tr
= top_trace_array();
2116 pr_info("Running tests on trace events:\n");
2118 list_for_each_entry(file
, &tr
->events
, list
) {
2120 call
= file
->event_call
;
2122 /* Only test those that have a probe */
2123 if (!call
->class || !call
->class->probe
)
2127 * Testing syscall events here is pretty useless, but
2128 * we still do it if configured. But this is time consuming.
2129 * What we really need is a user thread to perform the
2130 * syscalls as we test.
2132 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2133 if (call
->class->system
&&
2134 strcmp(call
->class->system
, "syscalls") == 0)
2138 pr_info("Testing event %s: ", call
->name
);
2141 * If an event is already enabled, someone is using
2142 * it and the self test should not be on.
2144 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2145 pr_warning("Enabled event during self test!\n");
2150 ftrace_event_enable_disable(file
, 1);
2152 ftrace_event_enable_disable(file
, 0);
2157 /* Now test at the sub system level */
2159 pr_info("Running tests on trace event systems:\n");
2161 list_for_each_entry(dir
, &tr
->systems
, list
) {
2163 system
= dir
->subsystem
;
2165 /* the ftrace system is special, skip it */
2166 if (strcmp(system
->name
, "ftrace") == 0)
2169 pr_info("Testing event system %s: ", system
->name
);
2171 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2172 if (WARN_ON_ONCE(ret
)) {
2173 pr_warning("error enabling system %s\n",
2180 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2181 if (WARN_ON_ONCE(ret
)) {
2182 pr_warning("error disabling system %s\n",
2190 /* Test with all events enabled */
2192 pr_info("Running tests on all trace events:\n");
2193 pr_info("Testing all events: ");
2195 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2196 if (WARN_ON_ONCE(ret
)) {
2197 pr_warning("error enabling all events\n");
2204 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2205 if (WARN_ON_ONCE(ret
)) {
2206 pr_warning("error disabling all events\n");
2213 #ifdef CONFIG_FUNCTION_TRACER
2215 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2218 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2219 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2221 struct ring_buffer_event
*event
;
2222 struct ring_buffer
*buffer
;
2223 struct ftrace_entry
*entry
;
2224 unsigned long flags
;
2229 pc
= preempt_count();
2230 preempt_disable_notrace();
2231 cpu
= raw_smp_processor_id();
2232 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2237 local_save_flags(flags
);
2239 event
= trace_current_buffer_lock_reserve(&buffer
,
2240 TRACE_FN
, sizeof(*entry
),
2244 entry
= ring_buffer_event_data(event
);
2246 entry
->parent_ip
= parent_ip
;
2248 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2251 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2252 preempt_enable_notrace();
2255 static struct ftrace_ops trace_ops __initdata
=
2257 .func
= function_test_events_call
,
2258 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2261 static __init
void event_trace_self_test_with_function(void)
2264 ret
= register_ftrace_function(&trace_ops
);
2265 if (WARN_ON(ret
< 0)) {
2266 pr_info("Failed to enable function tracer for event tests\n");
2269 pr_info("Running tests again, along with the function tracer\n");
2270 event_trace_self_tests();
2271 unregister_ftrace_function(&trace_ops
);
2274 static __init
void event_trace_self_test_with_function(void)
2279 static __init
int event_trace_self_tests_init(void)
2281 if (!tracing_selftest_disabled
) {
2282 event_trace_self_tests();
2283 event_trace_self_test_with_function();
2289 late_initcall(event_trace_self_tests_init
);