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
);
31 static LIST_HEAD(ftrace_common_fields
);
33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
35 static struct kmem_cache
*field_cachep
;
36 static struct kmem_cache
*file_cachep
;
38 #define SYSTEM_FL_FREE_NAME (1 << 31)
40 static inline int system_refcount(struct event_subsystem
*system
)
42 return system
->ref_count
& ~SYSTEM_FL_FREE_NAME
;
45 static int system_refcount_inc(struct event_subsystem
*system
)
47 return (system
->ref_count
++) & ~SYSTEM_FL_FREE_NAME
;
50 static int system_refcount_dec(struct event_subsystem
*system
)
52 return (--system
->ref_count
) & ~SYSTEM_FL_FREE_NAME
;
55 /* Double loops, do not use break, only goto's work */
56 #define do_for_each_event_file(tr, file) \
57 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 list_for_each_entry(file, &tr->events, list)
60 #define do_for_each_event_file_safe(tr, file) \
61 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 struct ftrace_event_file *___n; \
63 list_for_each_entry_safe(file, ___n, &tr->events, list)
65 #define while_for_each_event_file() \
68 static struct list_head
*
69 trace_get_fields(struct ftrace_event_call
*event_call
)
71 if (!event_call
->class->get_fields
)
72 return &event_call
->class->fields
;
73 return event_call
->class->get_fields(event_call
);
76 static struct ftrace_event_field
*
77 __find_event_field(struct list_head
*head
, char *name
)
79 struct ftrace_event_field
*field
;
81 list_for_each_entry(field
, head
, link
) {
82 if (!strcmp(field
->name
, name
))
89 struct ftrace_event_field
*
90 trace_find_event_field(struct ftrace_event_call
*call
, char *name
)
92 struct ftrace_event_field
*field
;
93 struct list_head
*head
;
95 field
= __find_event_field(&ftrace_common_fields
, name
);
99 head
= trace_get_fields(call
);
100 return __find_event_field(head
, name
);
103 static int __trace_define_field(struct list_head
*head
, const char *type
,
104 const char *name
, int offset
, int size
,
105 int is_signed
, int filter_type
)
107 struct ftrace_event_field
*field
;
109 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
116 if (filter_type
== FILTER_OTHER
)
117 field
->filter_type
= filter_assign_type(type
);
119 field
->filter_type
= filter_type
;
121 field
->offset
= offset
;
123 field
->is_signed
= is_signed
;
125 list_add(&field
->link
, head
);
130 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
131 const char *name
, int offset
, int size
, int is_signed
,
134 struct list_head
*head
;
136 if (WARN_ON(!call
->class))
139 head
= trace_get_fields(call
);
140 return __trace_define_field(head
, type
, name
, offset
, size
,
141 is_signed
, filter_type
);
143 EXPORT_SYMBOL_GPL(trace_define_field
);
145 #define __common_field(type, item) \
146 ret = __trace_define_field(&ftrace_common_fields, #type, \
148 offsetof(typeof(ent), item), \
150 is_signed_type(type), FILTER_OTHER); \
154 static int trace_define_common_fields(void)
157 struct trace_entry ent
;
159 __common_field(unsigned short, type
);
160 __common_field(unsigned char, flags
);
161 __common_field(unsigned char, preempt_count
);
162 __common_field(int, pid
);
167 static void trace_destroy_fields(struct ftrace_event_call
*call
)
169 struct ftrace_event_field
*field
, *next
;
170 struct list_head
*head
;
172 head
= trace_get_fields(call
);
173 list_for_each_entry_safe(field
, next
, head
, link
) {
174 list_del(&field
->link
);
175 kmem_cache_free(field_cachep
, field
);
179 int trace_event_raw_init(struct ftrace_event_call
*call
)
183 id
= register_ftrace_event(&call
->event
);
189 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
191 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer
*fbuffer
,
192 struct ftrace_event_file
*ftrace_file
,
195 struct ftrace_event_call
*event_call
= ftrace_file
->event_call
;
197 local_save_flags(fbuffer
->flags
);
198 fbuffer
->pc
= preempt_count();
199 fbuffer
->ftrace_file
= ftrace_file
;
202 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, ftrace_file
,
203 event_call
->event
.type
, len
,
204 fbuffer
->flags
, fbuffer
->pc
);
208 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
209 return fbuffer
->entry
;
211 EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve
);
213 void ftrace_event_buffer_commit(struct ftrace_event_buffer
*fbuffer
)
215 event_trigger_unlock_commit(fbuffer
->ftrace_file
, fbuffer
->buffer
,
216 fbuffer
->event
, fbuffer
->entry
,
217 fbuffer
->flags
, fbuffer
->pc
);
219 EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit
);
221 int ftrace_event_reg(struct ftrace_event_call
*call
,
222 enum trace_reg type
, void *data
)
224 struct ftrace_event_file
*file
= data
;
227 case TRACE_REG_REGISTER
:
228 return tracepoint_probe_register(call
->name
,
231 case TRACE_REG_UNREGISTER
:
232 tracepoint_probe_unregister(call
->name
,
237 #ifdef CONFIG_PERF_EVENTS
238 case TRACE_REG_PERF_REGISTER
:
239 return tracepoint_probe_register(call
->name
,
240 call
->class->perf_probe
,
242 case TRACE_REG_PERF_UNREGISTER
:
243 tracepoint_probe_unregister(call
->name
,
244 call
->class->perf_probe
,
247 case TRACE_REG_PERF_OPEN
:
248 case TRACE_REG_PERF_CLOSE
:
249 case TRACE_REG_PERF_ADD
:
250 case TRACE_REG_PERF_DEL
:
256 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
258 void trace_event_enable_cmd_record(bool enable
)
260 struct ftrace_event_file
*file
;
261 struct trace_array
*tr
;
263 mutex_lock(&event_mutex
);
264 do_for_each_event_file(tr
, file
) {
266 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
270 tracing_start_cmdline_record();
271 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
273 tracing_stop_cmdline_record();
274 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
276 } while_for_each_event_file();
277 mutex_unlock(&event_mutex
);
280 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
281 int enable
, int soft_disable
)
283 struct ftrace_event_call
*call
= file
->event_call
;
290 * When soft_disable is set and enable is cleared, the sm_ref
291 * reference counter is decremented. If it reaches 0, we want
292 * to clear the SOFT_DISABLED flag but leave the event in the
293 * state that it was. That is, if the event was enabled and
294 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
295 * is set we do not want the event to be enabled before we
298 * When soft_disable is not set but the SOFT_MODE flag is,
299 * we do nothing. Do not disable the tracepoint, otherwise
300 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
303 if (atomic_dec_return(&file
->sm_ref
) > 0)
305 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
306 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
308 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
310 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
311 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
312 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
313 tracing_stop_cmdline_record();
314 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
316 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
318 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
319 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
320 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
322 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
326 * When soft_disable is set and enable is set, we want to
327 * register the tracepoint for the event, but leave the event
328 * as is. That means, if the event was already enabled, we do
329 * nothing (but set SOFT_MODE). If the event is disabled, we
330 * set SOFT_DISABLED before enabling the event tracepoint, so
331 * it still seems to be disabled.
334 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
336 if (atomic_inc_return(&file
->sm_ref
) > 1)
338 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
341 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
343 /* Keep the event disabled, when going to SOFT_MODE. */
345 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
347 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
348 tracing_start_cmdline_record();
349 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
351 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
353 tracing_stop_cmdline_record();
354 pr_info("event trace: Could not enable event "
358 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
360 /* WAS_ENABLED gets set but never cleared. */
361 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
369 int trace_event_enable_disable(struct ftrace_event_file
*file
,
370 int enable
, int soft_disable
)
372 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
375 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
378 return __ftrace_event_enable_disable(file
, enable
, 0);
381 static void ftrace_clear_events(struct trace_array
*tr
)
383 struct ftrace_event_file
*file
;
385 mutex_lock(&event_mutex
);
386 list_for_each_entry(file
, &tr
->events
, list
) {
387 ftrace_event_enable_disable(file
, 0);
389 mutex_unlock(&event_mutex
);
392 static void __put_system(struct event_subsystem
*system
)
394 struct event_filter
*filter
= system
->filter
;
396 WARN_ON_ONCE(system_refcount(system
) == 0);
397 if (system_refcount_dec(system
))
400 list_del(&system
->list
);
403 kfree(filter
->filter_string
);
406 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
411 static void __get_system(struct event_subsystem
*system
)
413 WARN_ON_ONCE(system_refcount(system
) == 0);
414 system_refcount_inc(system
);
417 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
419 WARN_ON_ONCE(dir
->ref_count
== 0);
421 __get_system(dir
->subsystem
);
424 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
426 WARN_ON_ONCE(dir
->ref_count
== 0);
427 /* If the subsystem is about to be freed, the dir must be too */
428 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
430 __put_system(dir
->subsystem
);
431 if (!--dir
->ref_count
)
435 static void put_system(struct ftrace_subsystem_dir
*dir
)
437 mutex_lock(&event_mutex
);
438 __put_system_dir(dir
);
439 mutex_unlock(&event_mutex
);
442 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
447 if (!--dir
->nr_events
) {
448 debugfs_remove_recursive(dir
->entry
);
449 list_del(&dir
->list
);
450 __put_system_dir(dir
);
454 static void remove_event_file_dir(struct ftrace_event_file
*file
)
456 struct dentry
*dir
= file
->dir
;
457 struct dentry
*child
;
460 spin_lock(&dir
->d_lock
); /* probably unneeded */
461 list_for_each_entry(child
, &dir
->d_subdirs
, d_u
.d_child
) {
462 if (child
->d_inode
) /* probably unneeded */
463 child
->d_inode
->i_private
= NULL
;
465 spin_unlock(&dir
->d_lock
);
467 debugfs_remove_recursive(dir
);
470 list_del(&file
->list
);
471 remove_subsystem(file
->system
);
472 kmem_cache_free(file_cachep
, file
);
476 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
479 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
480 const char *sub
, const char *event
, int set
)
482 struct ftrace_event_file
*file
;
483 struct ftrace_event_call
*call
;
486 list_for_each_entry(file
, &tr
->events
, list
) {
488 call
= file
->event_call
;
490 if (!call
->name
|| !call
->class || !call
->class->reg
)
493 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
497 strcmp(match
, call
->name
) != 0 &&
498 strcmp(match
, call
->class->system
) != 0)
501 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
504 if (event
&& strcmp(event
, call
->name
) != 0)
507 ftrace_event_enable_disable(file
, set
);
515 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
516 const char *sub
, const char *event
, int set
)
520 mutex_lock(&event_mutex
);
521 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
522 mutex_unlock(&event_mutex
);
527 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
529 char *event
= NULL
, *sub
= NULL
, *match
;
532 * The buf format can be <subsystem>:<event-name>
533 * *:<event-name> means any event by that name.
534 * :<event-name> is the same.
536 * <subsystem>:* means all events in that subsystem
537 * <subsystem>: means the same.
539 * <name> (no ':') means all events in a subsystem with
540 * the name <name> or any event that matches <name>
543 match
= strsep(&buf
, ":");
549 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
551 if (!strlen(event
) || strcmp(event
, "*") == 0)
555 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
559 * trace_set_clr_event - enable or disable an event
560 * @system: system name to match (NULL for any system)
561 * @event: event name to match (NULL for all events, within system)
562 * @set: 1 to enable, 0 to disable
564 * This is a way for other parts of the kernel to enable or disable
567 * Returns 0 on success, -EINVAL if the parameters do not match any
570 int trace_set_clr_event(const char *system
, const char *event
, int set
)
572 struct trace_array
*tr
= top_trace_array();
574 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
576 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
578 /* 128 should be much more than enough */
579 #define EVENT_BUF_SIZE 127
582 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
583 size_t cnt
, loff_t
*ppos
)
585 struct trace_parser parser
;
586 struct seq_file
*m
= file
->private_data
;
587 struct trace_array
*tr
= m
->private;
593 ret
= tracing_update_buffers();
597 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
600 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
602 if (read
>= 0 && trace_parser_loaded((&parser
))) {
605 if (*parser
.buffer
== '!')
608 parser
.buffer
[parser
.idx
] = 0;
610 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
618 trace_parser_put(&parser
);
624 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
626 struct ftrace_event_file
*file
= v
;
627 struct ftrace_event_call
*call
;
628 struct trace_array
*tr
= m
->private;
632 list_for_each_entry_continue(file
, &tr
->events
, list
) {
633 call
= file
->event_call
;
635 * The ftrace subsystem is for showing formats only.
636 * They can not be enabled or disabled via the event files.
638 if (call
->class && call
->class->reg
)
645 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
647 struct ftrace_event_file
*file
;
648 struct trace_array
*tr
= m
->private;
651 mutex_lock(&event_mutex
);
653 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
654 for (l
= 0; l
<= *pos
; ) {
655 file
= t_next(m
, file
, &l
);
663 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
665 struct ftrace_event_file
*file
= v
;
666 struct trace_array
*tr
= m
->private;
670 list_for_each_entry_continue(file
, &tr
->events
, list
) {
671 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
678 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
680 struct ftrace_event_file
*file
;
681 struct trace_array
*tr
= m
->private;
684 mutex_lock(&event_mutex
);
686 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
687 for (l
= 0; l
<= *pos
; ) {
688 file
= s_next(m
, file
, &l
);
695 static int t_show(struct seq_file
*m
, void *v
)
697 struct ftrace_event_file
*file
= v
;
698 struct ftrace_event_call
*call
= file
->event_call
;
700 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
701 seq_printf(m
, "%s:", call
->class->system
);
702 seq_printf(m
, "%s\n", call
->name
);
707 static void t_stop(struct seq_file
*m
, void *p
)
709 mutex_unlock(&event_mutex
);
713 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
716 struct ftrace_event_file
*file
;
720 mutex_lock(&event_mutex
);
721 file
= event_file_data(filp
);
724 mutex_unlock(&event_mutex
);
729 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
730 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
733 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
734 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
739 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
743 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
746 struct ftrace_event_file
*file
;
750 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
754 ret
= tracing_update_buffers();
762 mutex_lock(&event_mutex
);
763 file
= event_file_data(filp
);
765 ret
= ftrace_event_enable_disable(file
, val
);
766 mutex_unlock(&event_mutex
);
775 return ret
? ret
: cnt
;
779 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
782 const char set_to_char
[4] = { '?', '0', '1', 'X' };
783 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
784 struct event_subsystem
*system
= dir
->subsystem
;
785 struct ftrace_event_call
*call
;
786 struct ftrace_event_file
*file
;
787 struct trace_array
*tr
= dir
->tr
;
792 mutex_lock(&event_mutex
);
793 list_for_each_entry(file
, &tr
->events
, list
) {
794 call
= file
->event_call
;
795 if (!call
->name
|| !call
->class || !call
->class->reg
)
798 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
802 * We need to find out if all the events are set
803 * or if all events or cleared, or if we have
806 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
809 * If we have a mixture, no need to look further.
814 mutex_unlock(&event_mutex
);
816 buf
[0] = set_to_char
[set
];
819 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
825 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
828 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
829 struct event_subsystem
*system
= dir
->subsystem
;
830 const char *name
= NULL
;
834 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
838 ret
= tracing_update_buffers();
842 if (val
!= 0 && val
!= 1)
846 * Opening of "enable" adds a ref count to system,
847 * so the name is safe to use.
852 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
866 FORMAT_FIELD_SEPERATOR
= 2,
870 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
872 struct ftrace_event_call
*call
= event_file_data(m
->private);
873 struct list_head
*common_head
= &ftrace_common_fields
;
874 struct list_head
*head
= trace_get_fields(call
);
875 struct list_head
*node
= v
;
879 switch ((unsigned long)v
) {
884 case FORMAT_FIELD_SEPERATOR
:
888 case FORMAT_PRINTFMT
:
894 if (node
== common_head
)
895 return (void *)FORMAT_FIELD_SEPERATOR
;
896 else if (node
== head
)
897 return (void *)FORMAT_PRINTFMT
;
902 static int f_show(struct seq_file
*m
, void *v
)
904 struct ftrace_event_call
*call
= event_file_data(m
->private);
905 struct ftrace_event_field
*field
;
906 const char *array_descriptor
;
908 switch ((unsigned long)v
) {
910 seq_printf(m
, "name: %s\n", call
->name
);
911 seq_printf(m
, "ID: %d\n", call
->event
.type
);
912 seq_printf(m
, "format:\n");
915 case FORMAT_FIELD_SEPERATOR
:
919 case FORMAT_PRINTFMT
:
920 seq_printf(m
, "\nprint fmt: %s\n",
925 field
= list_entry(v
, struct ftrace_event_field
, link
);
927 * Smartly shows the array type(except dynamic array).
930 * If TYPE := TYPE[LEN], it is shown:
931 * field:TYPE VAR[LEN]
933 array_descriptor
= strchr(field
->type
, '[');
935 if (!strncmp(field
->type
, "__data_loc", 10))
936 array_descriptor
= NULL
;
938 if (!array_descriptor
)
939 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
940 field
->type
, field
->name
, field
->offset
,
941 field
->size
, !!field
->is_signed
);
943 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
944 (int)(array_descriptor
- field
->type
),
945 field
->type
, field
->name
,
946 array_descriptor
, field
->offset
,
947 field
->size
, !!field
->is_signed
);
952 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
954 void *p
= (void *)FORMAT_HEADER
;
957 /* ->stop() is called even if ->start() fails */
958 mutex_lock(&event_mutex
);
959 if (!event_file_data(m
->private))
960 return ERR_PTR(-ENODEV
);
962 while (l
< *pos
&& p
)
963 p
= f_next(m
, p
, &l
);
968 static void f_stop(struct seq_file
*m
, void *p
)
970 mutex_unlock(&event_mutex
);
973 static const struct seq_operations trace_format_seq_ops
= {
980 static int trace_format_open(struct inode
*inode
, struct file
*file
)
985 ret
= seq_open(file
, &trace_format_seq_ops
);
989 m
= file
->private_data
;
996 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
998 int id
= (long)event_file_data(filp
);
1008 len
= sprintf(buf
, "%d\n", id
);
1010 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1014 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1017 struct ftrace_event_file
*file
;
1018 struct trace_seq
*s
;
1024 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1031 mutex_lock(&event_mutex
);
1032 file
= event_file_data(filp
);
1034 print_event_filter(file
, s
);
1035 mutex_unlock(&event_mutex
);
1038 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1046 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1049 struct ftrace_event_file
*file
;
1053 if (cnt
>= PAGE_SIZE
)
1056 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1060 if (copy_from_user(buf
, ubuf
, cnt
)) {
1061 free_page((unsigned long) buf
);
1066 mutex_lock(&event_mutex
);
1067 file
= event_file_data(filp
);
1069 err
= apply_event_filter(file
, buf
);
1070 mutex_unlock(&event_mutex
);
1072 free_page((unsigned long) buf
);
1081 static LIST_HEAD(event_subsystems
);
1083 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1085 struct event_subsystem
*system
= NULL
;
1086 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1087 struct trace_array
*tr
;
1090 if (tracing_is_disabled())
1093 /* Make sure the system still exists */
1094 mutex_lock(&trace_types_lock
);
1095 mutex_lock(&event_mutex
);
1096 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1097 list_for_each_entry(dir
, &tr
->systems
, list
) {
1098 if (dir
== inode
->i_private
) {
1099 /* Don't open systems with no events */
1100 if (dir
->nr_events
) {
1101 __get_system_dir(dir
);
1102 system
= dir
->subsystem
;
1109 mutex_unlock(&event_mutex
);
1110 mutex_unlock(&trace_types_lock
);
1115 /* Some versions of gcc think dir can be uninitialized here */
1118 /* Still need to increment the ref count of the system */
1119 if (trace_array_get(tr
) < 0) {
1124 ret
= tracing_open_generic(inode
, filp
);
1126 trace_array_put(tr
);
1133 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1135 struct ftrace_subsystem_dir
*dir
;
1136 struct trace_array
*tr
= inode
->i_private
;
1139 if (tracing_is_disabled())
1142 if (trace_array_get(tr
) < 0)
1145 /* Make a temporary dir that has no system but points to tr */
1146 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1148 trace_array_put(tr
);
1154 ret
= tracing_open_generic(inode
, filp
);
1156 trace_array_put(tr
);
1161 filp
->private_data
= dir
;
1166 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1168 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1170 trace_array_put(dir
->tr
);
1173 * If dir->subsystem is NULL, then this is a temporary
1174 * descriptor that was made for a trace_array to enable
1186 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1189 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1190 struct event_subsystem
*system
= dir
->subsystem
;
1191 struct trace_seq
*s
;
1197 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1203 print_subsystem_event_filter(system
, s
);
1204 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1212 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1215 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1219 if (cnt
>= PAGE_SIZE
)
1222 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1226 if (copy_from_user(buf
, ubuf
, cnt
)) {
1227 free_page((unsigned long) buf
);
1232 err
= apply_subsystem_event_filter(dir
, buf
);
1233 free_page((unsigned long) buf
);
1243 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1245 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1246 struct trace_seq
*s
;
1252 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1259 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1266 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1267 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1268 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1270 static const struct seq_operations show_event_seq_ops
= {
1277 static const struct seq_operations show_set_event_seq_ops
= {
1284 static const struct file_operations ftrace_avail_fops
= {
1285 .open
= ftrace_event_avail_open
,
1287 .llseek
= seq_lseek
,
1288 .release
= seq_release
,
1291 static const struct file_operations ftrace_set_event_fops
= {
1292 .open
= ftrace_event_set_open
,
1294 .write
= ftrace_event_write
,
1295 .llseek
= seq_lseek
,
1296 .release
= ftrace_event_release
,
1299 static const struct file_operations ftrace_enable_fops
= {
1300 .open
= tracing_open_generic
,
1301 .read
= event_enable_read
,
1302 .write
= event_enable_write
,
1303 .llseek
= default_llseek
,
1306 static const struct file_operations ftrace_event_format_fops
= {
1307 .open
= trace_format_open
,
1309 .llseek
= seq_lseek
,
1310 .release
= seq_release
,
1313 static const struct file_operations ftrace_event_id_fops
= {
1314 .read
= event_id_read
,
1315 .llseek
= default_llseek
,
1318 static const struct file_operations ftrace_event_filter_fops
= {
1319 .open
= tracing_open_generic
,
1320 .read
= event_filter_read
,
1321 .write
= event_filter_write
,
1322 .llseek
= default_llseek
,
1325 static const struct file_operations ftrace_subsystem_filter_fops
= {
1326 .open
= subsystem_open
,
1327 .read
= subsystem_filter_read
,
1328 .write
= subsystem_filter_write
,
1329 .llseek
= default_llseek
,
1330 .release
= subsystem_release
,
1333 static const struct file_operations ftrace_system_enable_fops
= {
1334 .open
= subsystem_open
,
1335 .read
= system_enable_read
,
1336 .write
= system_enable_write
,
1337 .llseek
= default_llseek
,
1338 .release
= subsystem_release
,
1341 static const struct file_operations ftrace_tr_enable_fops
= {
1342 .open
= system_tr_open
,
1343 .read
= system_enable_read
,
1344 .write
= system_enable_write
,
1345 .llseek
= default_llseek
,
1346 .release
= subsystem_release
,
1349 static const struct file_operations ftrace_show_header_fops
= {
1350 .open
= tracing_open_generic
,
1351 .read
= show_header
,
1352 .llseek
= default_llseek
,
1356 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1357 const struct seq_operations
*seq_ops
)
1362 ret
= seq_open(file
, seq_ops
);
1365 m
= file
->private_data
;
1366 /* copy tr over to seq ops */
1367 m
->private = inode
->i_private
;
1372 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1374 struct trace_array
*tr
= inode
->i_private
;
1376 trace_array_put(tr
);
1378 return seq_release(inode
, file
);
1382 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1384 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1386 return ftrace_event_open(inode
, file
, seq_ops
);
1390 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1392 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1393 struct trace_array
*tr
= inode
->i_private
;
1396 if (trace_array_get(tr
) < 0)
1399 if ((file
->f_mode
& FMODE_WRITE
) &&
1400 (file
->f_flags
& O_TRUNC
))
1401 ftrace_clear_events(tr
);
1403 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1405 trace_array_put(tr
);
1409 static struct event_subsystem
*
1410 create_new_subsystem(const char *name
)
1412 struct event_subsystem
*system
;
1414 /* need to create new entry */
1415 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1419 system
->ref_count
= 1;
1421 /* Only allocate if dynamic (kprobes and modules) */
1422 if (!core_kernel_data((unsigned long)name
)) {
1423 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1424 system
->name
= kstrdup(name
, GFP_KERNEL
);
1428 system
->name
= name
;
1430 system
->filter
= NULL
;
1432 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1433 if (!system
->filter
)
1436 list_add(&system
->list
, &event_subsystems
);
1441 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1442 kfree(system
->name
);
1447 static struct dentry
*
1448 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1449 struct ftrace_event_file
*file
, struct dentry
*parent
)
1451 struct ftrace_subsystem_dir
*dir
;
1452 struct event_subsystem
*system
;
1453 struct dentry
*entry
;
1455 /* First see if we did not already create this dir */
1456 list_for_each_entry(dir
, &tr
->systems
, list
) {
1457 system
= dir
->subsystem
;
1458 if (strcmp(system
->name
, name
) == 0) {
1465 /* Now see if the system itself exists. */
1466 list_for_each_entry(system
, &event_subsystems
, list
) {
1467 if (strcmp(system
->name
, name
) == 0)
1470 /* Reset system variable when not found */
1471 if (&system
->list
== &event_subsystems
)
1474 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1479 system
= create_new_subsystem(name
);
1483 __get_system(system
);
1485 dir
->entry
= debugfs_create_dir(name
, parent
);
1487 pr_warning("Failed to create system directory %s\n", name
);
1488 __put_system(system
);
1495 dir
->subsystem
= system
;
1498 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1499 &ftrace_subsystem_filter_fops
);
1501 kfree(system
->filter
);
1502 system
->filter
= NULL
;
1503 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1506 trace_create_file("enable", 0644, dir
->entry
, dir
,
1507 &ftrace_system_enable_fops
);
1509 list_add(&dir
->list
, &tr
->systems
);
1516 /* Only print this message if failed on memory allocation */
1517 if (!dir
|| !system
)
1518 pr_warning("No memory to create event subsystem %s\n",
1524 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1526 struct ftrace_event_call
*call
= file
->event_call
;
1527 struct trace_array
*tr
= file
->tr
;
1528 struct list_head
*head
;
1529 struct dentry
*d_events
;
1533 * If the trace point header did not define TRACE_SYSTEM
1534 * then the system would be called "TRACE_SYSTEM".
1536 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1537 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1543 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1545 pr_warning("Could not create debugfs '%s' directory\n",
1550 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1551 trace_create_file("enable", 0644, file
->dir
, file
,
1552 &ftrace_enable_fops
);
1554 #ifdef CONFIG_PERF_EVENTS
1555 if (call
->event
.type
&& call
->class->reg
)
1556 trace_create_file("id", 0444, file
->dir
,
1557 (void *)(long)call
->event
.type
,
1558 &ftrace_event_id_fops
);
1562 * Other events may have the same class. Only update
1563 * the fields if they are not already defined.
1565 head
= trace_get_fields(call
);
1566 if (list_empty(head
)) {
1567 ret
= call
->class->define_fields(call
);
1569 pr_warning("Could not initialize trace point"
1570 " events/%s\n", call
->name
);
1574 trace_create_file("filter", 0644, file
->dir
, file
,
1575 &ftrace_event_filter_fops
);
1577 trace_create_file("trigger", 0644, file
->dir
, file
,
1578 &event_trigger_fops
);
1580 trace_create_file("format", 0444, file
->dir
, call
,
1581 &ftrace_event_format_fops
);
1586 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1588 struct ftrace_event_file
*file
;
1589 struct trace_array
*tr
;
1591 do_for_each_event_file_safe(tr
, file
) {
1592 if (file
->event_call
!= call
)
1595 remove_event_file_dir(file
);
1597 * The do_for_each_event_file_safe() is
1598 * a double loop. After finding the call for this
1599 * trace_array, we use break to jump to the next
1603 } while_for_each_event_file();
1606 static void event_remove(struct ftrace_event_call
*call
)
1608 struct trace_array
*tr
;
1609 struct ftrace_event_file
*file
;
1611 do_for_each_event_file(tr
, file
) {
1612 if (file
->event_call
!= call
)
1614 ftrace_event_enable_disable(file
, 0);
1615 destroy_preds(file
);
1617 * The do_for_each_event_file() is
1618 * a double loop. After finding the call for this
1619 * trace_array, we use break to jump to the next
1623 } while_for_each_event_file();
1625 if (call
->event
.funcs
)
1626 __unregister_ftrace_event(&call
->event
);
1627 remove_event_from_tracers(call
);
1628 list_del(&call
->list
);
1631 static int event_init(struct ftrace_event_call
*call
)
1635 if (WARN_ON(!call
->name
))
1638 if (call
->class->raw_init
) {
1639 ret
= call
->class->raw_init(call
);
1640 if (ret
< 0 && ret
!= -ENOSYS
)
1641 pr_warn("Could not initialize trace events/%s\n",
1649 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1653 ret
= event_init(call
);
1657 list_add(&call
->list
, &ftrace_events
);
1663 static struct ftrace_event_file
*
1664 trace_create_new_event(struct ftrace_event_call
*call
,
1665 struct trace_array
*tr
)
1667 struct ftrace_event_file
*file
;
1669 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1673 file
->event_call
= call
;
1675 atomic_set(&file
->sm_ref
, 0);
1676 atomic_set(&file
->tm_ref
, 0);
1677 INIT_LIST_HEAD(&file
->triggers
);
1678 list_add(&file
->list
, &tr
->events
);
1683 /* Add an event to a trace directory */
1685 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1687 struct ftrace_event_file
*file
;
1689 file
= trace_create_new_event(call
, tr
);
1693 return event_create_dir(tr
->event_dir
, file
);
1697 * Just create a decriptor for early init. A descriptor is required
1698 * for enabling events at boot. We want to enable events before
1699 * the filesystem is initialized.
1702 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1703 struct trace_array
*tr
)
1705 struct ftrace_event_file
*file
;
1707 file
= trace_create_new_event(call
, tr
);
1714 struct ftrace_module_file_ops
;
1715 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1717 /* Add an additional event_call dynamically */
1718 int trace_add_event_call(struct ftrace_event_call
*call
)
1721 mutex_lock(&trace_types_lock
);
1722 mutex_lock(&event_mutex
);
1724 ret
= __register_event(call
, NULL
);
1726 __add_event_to_tracers(call
);
1728 mutex_unlock(&event_mutex
);
1729 mutex_unlock(&trace_types_lock
);
1734 * Must be called under locking of trace_types_lock, event_mutex and
1737 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1740 trace_destroy_fields(call
);
1741 destroy_call_preds(call
);
1744 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1746 struct trace_array
*tr
;
1747 struct ftrace_event_file
*file
;
1749 #ifdef CONFIG_PERF_EVENTS
1750 if (call
->perf_refcount
)
1753 do_for_each_event_file(tr
, file
) {
1754 if (file
->event_call
!= call
)
1757 * We can't rely on ftrace_event_enable_disable(enable => 0)
1758 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1759 * TRACE_REG_UNREGISTER.
1761 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1764 * The do_for_each_event_file_safe() is
1765 * a double loop. After finding the call for this
1766 * trace_array, we use break to jump to the next
1770 } while_for_each_event_file();
1772 __trace_remove_event_call(call
);
1777 /* Remove an event_call */
1778 int trace_remove_event_call(struct ftrace_event_call
*call
)
1782 mutex_lock(&trace_types_lock
);
1783 mutex_lock(&event_mutex
);
1784 down_write(&trace_event_sem
);
1785 ret
= probe_remove_event_call(call
);
1786 up_write(&trace_event_sem
);
1787 mutex_unlock(&event_mutex
);
1788 mutex_unlock(&trace_types_lock
);
1793 #define for_each_event(event, start, end) \
1794 for (event = start; \
1795 (unsigned long)event < (unsigned long)end; \
1798 #ifdef CONFIG_MODULES
1800 static void trace_module_add_events(struct module
*mod
)
1802 struct ftrace_event_call
**call
, **start
, **end
;
1804 start
= mod
->trace_events
;
1805 end
= mod
->trace_events
+ mod
->num_trace_events
;
1807 for_each_event(call
, start
, end
) {
1808 __register_event(*call
, mod
);
1809 __add_event_to_tracers(*call
);
1813 static void trace_module_remove_events(struct module
*mod
)
1815 struct ftrace_event_call
*call
, *p
;
1816 bool clear_trace
= false;
1818 down_write(&trace_event_sem
);
1819 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1820 if (call
->mod
== mod
) {
1821 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1823 __trace_remove_event_call(call
);
1826 up_write(&trace_event_sem
);
1829 * It is safest to reset the ring buffer if the module being unloaded
1830 * registered any events that were used. The only worry is if
1831 * a new module gets loaded, and takes on the same id as the events
1832 * of this module. When printing out the buffer, traced events left
1833 * over from this module may be passed to the new module events and
1834 * unexpected results may occur.
1837 tracing_reset_all_online_cpus();
1840 static int trace_module_notify(struct notifier_block
*self
,
1841 unsigned long val
, void *data
)
1843 struct module
*mod
= data
;
1845 mutex_lock(&trace_types_lock
);
1846 mutex_lock(&event_mutex
);
1848 case MODULE_STATE_COMING
:
1849 trace_module_add_events(mod
);
1851 case MODULE_STATE_GOING
:
1852 trace_module_remove_events(mod
);
1855 mutex_unlock(&event_mutex
);
1856 mutex_unlock(&trace_types_lock
);
1861 static struct notifier_block trace_module_nb
= {
1862 .notifier_call
= trace_module_notify
,
1865 #endif /* CONFIG_MODULES */
1867 /* Create a new event directory structure for a trace directory. */
1869 __trace_add_event_dirs(struct trace_array
*tr
)
1871 struct ftrace_event_call
*call
;
1874 list_for_each_entry(call
, &ftrace_events
, list
) {
1875 ret
= __trace_add_new_event(call
, tr
);
1877 pr_warning("Could not create directory for event %s\n",
1882 struct ftrace_event_file
*
1883 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1885 struct ftrace_event_file
*file
;
1886 struct ftrace_event_call
*call
;
1888 list_for_each_entry(file
, &tr
->events
, list
) {
1890 call
= file
->event_call
;
1892 if (!call
->name
|| !call
->class || !call
->class->reg
)
1895 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1898 if (strcmp(event
, call
->name
) == 0 &&
1899 strcmp(system
, call
->class->system
) == 0)
1905 #ifdef CONFIG_DYNAMIC_FTRACE
1908 #define ENABLE_EVENT_STR "enable_event"
1909 #define DISABLE_EVENT_STR "disable_event"
1911 struct event_probe_data
{
1912 struct ftrace_event_file
*file
;
1913 unsigned long count
;
1919 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1921 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1922 struct event_probe_data
*data
= *pdata
;
1928 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1930 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1934 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1936 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1937 struct event_probe_data
*data
= *pdata
;
1945 /* Skip if the event is in a state we want to switch to */
1946 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1949 if (data
->count
!= -1)
1952 event_enable_probe(ip
, parent_ip
, _data
);
1956 event_enable_print(struct seq_file
*m
, unsigned long ip
,
1957 struct ftrace_probe_ops
*ops
, void *_data
)
1959 struct event_probe_data
*data
= _data
;
1961 seq_printf(m
, "%ps:", (void *)ip
);
1963 seq_printf(m
, "%s:%s:%s",
1964 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1965 data
->file
->event_call
->class->system
,
1966 data
->file
->event_call
->name
);
1968 if (data
->count
== -1)
1969 seq_printf(m
, ":unlimited\n");
1971 seq_printf(m
, ":count=%ld\n", data
->count
);
1977 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1980 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1981 struct event_probe_data
*data
= *pdata
;
1988 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1991 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1992 struct event_probe_data
*data
= *pdata
;
1994 if (WARN_ON_ONCE(data
->ref
<= 0))
1999 /* Remove the SOFT_MODE flag */
2000 __ftrace_event_enable_disable(data
->file
, 0, 1);
2001 module_put(data
->file
->event_call
->mod
);
2007 static struct ftrace_probe_ops event_enable_probe_ops
= {
2008 .func
= event_enable_probe
,
2009 .print
= event_enable_print
,
2010 .init
= event_enable_init
,
2011 .free
= event_enable_free
,
2014 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2015 .func
= event_enable_count_probe
,
2016 .print
= event_enable_print
,
2017 .init
= event_enable_init
,
2018 .free
= event_enable_free
,
2021 static struct ftrace_probe_ops event_disable_probe_ops
= {
2022 .func
= event_enable_probe
,
2023 .print
= event_enable_print
,
2024 .init
= event_enable_init
,
2025 .free
= event_enable_free
,
2028 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2029 .func
= event_enable_count_probe
,
2030 .print
= event_enable_print
,
2031 .init
= event_enable_init
,
2032 .free
= event_enable_free
,
2036 event_enable_func(struct ftrace_hash
*hash
,
2037 char *glob
, char *cmd
, char *param
, int enabled
)
2039 struct trace_array
*tr
= top_trace_array();
2040 struct ftrace_event_file
*file
;
2041 struct ftrace_probe_ops
*ops
;
2042 struct event_probe_data
*data
;
2049 /* hash funcs only work with set_ftrace_filter */
2050 if (!enabled
|| !param
)
2053 system
= strsep(¶m
, ":");
2057 event
= strsep(¶m
, ":");
2059 mutex_lock(&event_mutex
);
2062 file
= find_event_file(tr
, system
, event
);
2066 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2069 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2071 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2073 if (glob
[0] == '!') {
2074 unregister_ftrace_function_probe_func(glob
+1, ops
);
2080 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2084 data
->enable
= enable
;
2091 number
= strsep(¶m
, ":");
2094 if (!strlen(number
))
2098 * We use the callback data field (which is a pointer)
2101 ret
= kstrtoul(number
, 0, &data
->count
);
2106 /* Don't let event modules unload while probe registered */
2107 ret
= try_module_get(file
->event_call
->mod
);
2113 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2116 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2118 * The above returns on success the # of functions enabled,
2119 * but if it didn't find any functions it returns zero.
2120 * Consider no functions a failure too.
2127 /* Just return zero, not the number of enabled functions */
2130 mutex_unlock(&event_mutex
);
2134 __ftrace_event_enable_disable(file
, 0, 1);
2136 module_put(file
->event_call
->mod
);
2142 static struct ftrace_func_command event_enable_cmd
= {
2143 .name
= ENABLE_EVENT_STR
,
2144 .func
= event_enable_func
,
2147 static struct ftrace_func_command event_disable_cmd
= {
2148 .name
= DISABLE_EVENT_STR
,
2149 .func
= event_enable_func
,
2152 static __init
int register_event_cmds(void)
2156 ret
= register_ftrace_command(&event_enable_cmd
);
2157 if (WARN_ON(ret
< 0))
2159 ret
= register_ftrace_command(&event_disable_cmd
);
2160 if (WARN_ON(ret
< 0))
2161 unregister_ftrace_command(&event_enable_cmd
);
2165 static inline int register_event_cmds(void) { return 0; }
2166 #endif /* CONFIG_DYNAMIC_FTRACE */
2169 * The top level array has already had its ftrace_event_file
2170 * descriptors created in order to allow for early events to
2171 * be recorded. This function is called after the debugfs has been
2172 * initialized, and we now have to create the files associated
2176 __trace_early_add_event_dirs(struct trace_array
*tr
)
2178 struct ftrace_event_file
*file
;
2182 list_for_each_entry(file
, &tr
->events
, list
) {
2183 ret
= event_create_dir(tr
->event_dir
, file
);
2185 pr_warning("Could not create directory for event %s\n",
2186 file
->event_call
->name
);
2191 * For early boot up, the top trace array requires to have
2192 * a list of events that can be enabled. This must be done before
2193 * the filesystem is set up in order to allow events to be traced
2197 __trace_early_add_events(struct trace_array
*tr
)
2199 struct ftrace_event_call
*call
;
2202 list_for_each_entry(call
, &ftrace_events
, list
) {
2203 /* Early boot up should not have any modules loaded */
2204 if (WARN_ON_ONCE(call
->mod
))
2207 ret
= __trace_early_add_new_event(call
, tr
);
2209 pr_warning("Could not create early event %s\n",
2214 /* Remove the event directory structure for a trace directory. */
2216 __trace_remove_event_dirs(struct trace_array
*tr
)
2218 struct ftrace_event_file
*file
, *next
;
2220 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2221 remove_event_file_dir(file
);
2224 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2226 struct trace_array
*tr
;
2228 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2229 __trace_add_new_event(call
, tr
);
2232 extern struct ftrace_event_call
*__start_ftrace_events
[];
2233 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2235 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2237 static __init
int setup_trace_event(char *str
)
2239 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2240 ring_buffer_expanded
= true;
2241 tracing_selftest_disabled
= true;
2245 __setup("trace_event=", setup_trace_event
);
2247 /* Expects to have event_mutex held when called */
2249 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2251 struct dentry
*d_events
;
2252 struct dentry
*entry
;
2254 entry
= debugfs_create_file("set_event", 0644, parent
,
2255 tr
, &ftrace_set_event_fops
);
2257 pr_warning("Could not create debugfs 'set_event' entry\n");
2261 d_events
= debugfs_create_dir("events", parent
);
2263 pr_warning("Could not create debugfs 'events' directory\n");
2267 /* ring buffer internal formats */
2268 trace_create_file("header_page", 0444, d_events
,
2269 ring_buffer_print_page_header
,
2270 &ftrace_show_header_fops
);
2272 trace_create_file("header_event", 0444, d_events
,
2273 ring_buffer_print_entry_header
,
2274 &ftrace_show_header_fops
);
2276 trace_create_file("enable", 0644, d_events
,
2277 tr
, &ftrace_tr_enable_fops
);
2279 tr
->event_dir
= d_events
;
2285 * event_trace_add_tracer - add a instance of a trace_array to events
2286 * @parent: The parent dentry to place the files/directories for events in
2287 * @tr: The trace array associated with these events
2289 * When a new instance is created, it needs to set up its events
2290 * directory, as well as other files associated with events. It also
2291 * creates the event hierachry in the @parent/events directory.
2293 * Returns 0 on success.
2295 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2299 mutex_lock(&event_mutex
);
2301 ret
= create_event_toplevel_files(parent
, tr
);
2305 down_write(&trace_event_sem
);
2306 __trace_add_event_dirs(tr
);
2307 up_write(&trace_event_sem
);
2310 mutex_unlock(&event_mutex
);
2316 * The top trace array already had its file descriptors created.
2317 * Now the files themselves need to be created.
2320 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2324 mutex_lock(&event_mutex
);
2326 ret
= create_event_toplevel_files(parent
, tr
);
2330 down_write(&trace_event_sem
);
2331 __trace_early_add_event_dirs(tr
);
2332 up_write(&trace_event_sem
);
2335 mutex_unlock(&event_mutex
);
2340 int event_trace_del_tracer(struct trace_array
*tr
)
2342 mutex_lock(&event_mutex
);
2344 /* Disable any event triggers and associated soft-disabled events */
2345 clear_event_triggers(tr
);
2347 /* Disable any running events */
2348 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2350 /* Access to events are within rcu_read_lock_sched() */
2351 synchronize_sched();
2353 down_write(&trace_event_sem
);
2354 __trace_remove_event_dirs(tr
);
2355 debugfs_remove_recursive(tr
->event_dir
);
2356 up_write(&trace_event_sem
);
2358 tr
->event_dir
= NULL
;
2360 mutex_unlock(&event_mutex
);
2365 static __init
int event_trace_memsetup(void)
2367 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2368 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2372 static __init
int event_trace_enable(void)
2374 struct trace_array
*tr
= top_trace_array();
2375 struct ftrace_event_call
**iter
, *call
;
2376 char *buf
= bootup_event_buf
;
2380 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2383 ret
= event_init(call
);
2385 list_add(&call
->list
, &ftrace_events
);
2389 * We need the top trace array to have a working set of trace
2390 * points at early init, before the debug files and directories
2391 * are created. Create the file entries now, and attach them
2392 * to the actual file dentries later.
2394 __trace_early_add_events(tr
);
2397 token
= strsep(&buf
, ",");
2404 ret
= ftrace_set_clr_event(tr
, token
, 1);
2406 pr_warn("Failed to enable trace event: %s\n", token
);
2409 trace_printk_start_comm();
2411 register_event_cmds();
2413 register_trigger_cmds();
2418 static __init
int event_trace_init(void)
2420 struct trace_array
*tr
;
2421 struct dentry
*d_tracer
;
2422 struct dentry
*entry
;
2425 tr
= top_trace_array();
2427 d_tracer
= tracing_init_dentry();
2431 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2432 tr
, &ftrace_avail_fops
);
2434 pr_warning("Could not create debugfs "
2435 "'available_events' entry\n");
2437 if (trace_define_common_fields())
2438 pr_warning("tracing: Failed to allocate common fields");
2440 ret
= early_event_add_tracer(d_tracer
, tr
);
2444 #ifdef CONFIG_MODULES
2445 ret
= register_module_notifier(&trace_module_nb
);
2447 pr_warning("Failed to register trace events module notifier\n");
2451 early_initcall(event_trace_memsetup
);
2452 core_initcall(event_trace_enable
);
2453 fs_initcall(event_trace_init
);
2455 #ifdef CONFIG_FTRACE_STARTUP_TEST
2457 static DEFINE_SPINLOCK(test_spinlock
);
2458 static DEFINE_SPINLOCK(test_spinlock_irq
);
2459 static DEFINE_MUTEX(test_mutex
);
2461 static __init
void test_work(struct work_struct
*dummy
)
2463 spin_lock(&test_spinlock
);
2464 spin_lock_irq(&test_spinlock_irq
);
2466 spin_unlock_irq(&test_spinlock_irq
);
2467 spin_unlock(&test_spinlock
);
2469 mutex_lock(&test_mutex
);
2471 mutex_unlock(&test_mutex
);
2474 static __init
int event_test_thread(void *unused
)
2478 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2480 pr_info("failed to kmalloc\n");
2482 schedule_on_each_cpu(test_work
);
2486 set_current_state(TASK_INTERRUPTIBLE
);
2487 while (!kthread_should_stop())
2494 * Do various things that may trigger events.
2496 static __init
void event_test_stuff(void)
2498 struct task_struct
*test_thread
;
2500 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2502 kthread_stop(test_thread
);
2506 * For every trace event defined, we will test each trace point separately,
2507 * and then by groups, and finally all trace points.
2509 static __init
void event_trace_self_tests(void)
2511 struct ftrace_subsystem_dir
*dir
;
2512 struct ftrace_event_file
*file
;
2513 struct ftrace_event_call
*call
;
2514 struct event_subsystem
*system
;
2515 struct trace_array
*tr
;
2518 tr
= top_trace_array();
2520 pr_info("Running tests on trace events:\n");
2522 list_for_each_entry(file
, &tr
->events
, list
) {
2524 call
= file
->event_call
;
2526 /* Only test those that have a probe */
2527 if (!call
->class || !call
->class->probe
)
2531 * Testing syscall events here is pretty useless, but
2532 * we still do it if configured. But this is time consuming.
2533 * What we really need is a user thread to perform the
2534 * syscalls as we test.
2536 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2537 if (call
->class->system
&&
2538 strcmp(call
->class->system
, "syscalls") == 0)
2542 pr_info("Testing event %s: ", call
->name
);
2545 * If an event is already enabled, someone is using
2546 * it and the self test should not be on.
2548 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2549 pr_warning("Enabled event during self test!\n");
2554 ftrace_event_enable_disable(file
, 1);
2556 ftrace_event_enable_disable(file
, 0);
2561 /* Now test at the sub system level */
2563 pr_info("Running tests on trace event systems:\n");
2565 list_for_each_entry(dir
, &tr
->systems
, list
) {
2567 system
= dir
->subsystem
;
2569 /* the ftrace system is special, skip it */
2570 if (strcmp(system
->name
, "ftrace") == 0)
2573 pr_info("Testing event system %s: ", system
->name
);
2575 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2576 if (WARN_ON_ONCE(ret
)) {
2577 pr_warning("error enabling system %s\n",
2584 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2585 if (WARN_ON_ONCE(ret
)) {
2586 pr_warning("error disabling system %s\n",
2594 /* Test with all events enabled */
2596 pr_info("Running tests on all trace events:\n");
2597 pr_info("Testing all events: ");
2599 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2600 if (WARN_ON_ONCE(ret
)) {
2601 pr_warning("error enabling all events\n");
2608 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2609 if (WARN_ON_ONCE(ret
)) {
2610 pr_warning("error disabling all events\n");
2617 #ifdef CONFIG_FUNCTION_TRACER
2619 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2622 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2623 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2625 struct ring_buffer_event
*event
;
2626 struct ring_buffer
*buffer
;
2627 struct ftrace_entry
*entry
;
2628 unsigned long flags
;
2633 pc
= preempt_count();
2634 preempt_disable_notrace();
2635 cpu
= raw_smp_processor_id();
2636 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2641 local_save_flags(flags
);
2643 event
= trace_current_buffer_lock_reserve(&buffer
,
2644 TRACE_FN
, sizeof(*entry
),
2648 entry
= ring_buffer_event_data(event
);
2650 entry
->parent_ip
= parent_ip
;
2652 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2655 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2656 preempt_enable_notrace();
2659 static struct ftrace_ops trace_ops __initdata
=
2661 .func
= function_test_events_call
,
2662 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2665 static __init
void event_trace_self_test_with_function(void)
2668 ret
= register_ftrace_function(&trace_ops
);
2669 if (WARN_ON(ret
< 0)) {
2670 pr_info("Failed to enable function tracer for event tests\n");
2673 pr_info("Running tests again, along with the function tracer\n");
2674 event_trace_self_tests();
2675 unregister_ftrace_function(&trace_ops
);
2678 static __init
void event_trace_self_test_with_function(void)
2683 static __init
int event_trace_self_tests_init(void)
2685 if (!tracing_selftest_disabled
) {
2686 event_trace_self_tests();
2687 event_trace_self_test_with_function();
2693 late_initcall(event_trace_self_tests_init
);