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 static 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 #define SYSTEM_FL_FREE_NAME (1 << 31)
46 static inline int system_refcount(struct event_subsystem
*system
)
48 return system
->ref_count
& ~SYSTEM_FL_FREE_NAME
;
51 static int system_refcount_inc(struct event_subsystem
*system
)
53 return (system
->ref_count
++) & ~SYSTEM_FL_FREE_NAME
;
56 static int system_refcount_dec(struct event_subsystem
*system
)
58 return (--system
->ref_count
) & ~SYSTEM_FL_FREE_NAME
;
61 /* Double loops, do not use break, only goto's work */
62 #define do_for_each_event_file(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 list_for_each_entry(file, &tr->events, list)
66 #define do_for_each_event_file_safe(tr, file) \
67 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
68 struct ftrace_event_file *___n; \
69 list_for_each_entry_safe(file, ___n, &tr->events, list)
71 #define while_for_each_event_file() \
74 static struct list_head
*
75 trace_get_fields(struct ftrace_event_call
*event_call
)
77 if (!event_call
->class->get_fields
)
78 return &event_call
->class->fields
;
79 return event_call
->class->get_fields(event_call
);
82 static struct ftrace_event_field
*
83 __find_event_field(struct list_head
*head
, char *name
)
85 struct ftrace_event_field
*field
;
87 list_for_each_entry(field
, head
, link
) {
88 if (!strcmp(field
->name
, name
))
95 struct ftrace_event_field
*
96 trace_find_event_field(struct ftrace_event_call
*call
, char *name
)
98 struct ftrace_event_field
*field
;
99 struct list_head
*head
;
101 field
= __find_event_field(&ftrace_common_fields
, name
);
105 head
= trace_get_fields(call
);
106 return __find_event_field(head
, name
);
109 static int __trace_define_field(struct list_head
*head
, const char *type
,
110 const char *name
, int offset
, int size
,
111 int is_signed
, int filter_type
)
113 struct ftrace_event_field
*field
;
115 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
122 if (filter_type
== FILTER_OTHER
)
123 field
->filter_type
= filter_assign_type(type
);
125 field
->filter_type
= filter_type
;
127 field
->offset
= offset
;
129 field
->is_signed
= is_signed
;
131 list_add(&field
->link
, head
);
136 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
137 const char *name
, int offset
, int size
, int is_signed
,
140 struct list_head
*head
;
142 if (WARN_ON(!call
->class))
145 head
= trace_get_fields(call
);
146 return __trace_define_field(head
, type
, name
, offset
, size
,
147 is_signed
, filter_type
);
149 EXPORT_SYMBOL_GPL(trace_define_field
);
151 #define __common_field(type, item) \
152 ret = __trace_define_field(&ftrace_common_fields, #type, \
154 offsetof(typeof(ent), item), \
156 is_signed_type(type), FILTER_OTHER); \
160 static int trace_define_common_fields(void)
163 struct trace_entry ent
;
165 __common_field(unsigned short, type
);
166 __common_field(unsigned char, flags
);
167 __common_field(unsigned char, preempt_count
);
168 __common_field(int, pid
);
173 static void trace_destroy_fields(struct ftrace_event_call
*call
)
175 struct ftrace_event_field
*field
, *next
;
176 struct list_head
*head
;
178 head
= trace_get_fields(call
);
179 list_for_each_entry_safe(field
, next
, head
, link
) {
180 list_del(&field
->link
);
181 kmem_cache_free(field_cachep
, field
);
185 int trace_event_raw_init(struct ftrace_event_call
*call
)
189 id
= register_ftrace_event(&call
->event
);
195 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
197 int ftrace_event_reg(struct ftrace_event_call
*call
,
198 enum trace_reg type
, void *data
)
200 struct ftrace_event_file
*file
= data
;
203 case TRACE_REG_REGISTER
:
204 return tracepoint_probe_register(call
->name
,
207 case TRACE_REG_UNREGISTER
:
208 tracepoint_probe_unregister(call
->name
,
213 #ifdef CONFIG_PERF_EVENTS
214 case TRACE_REG_PERF_REGISTER
:
215 return tracepoint_probe_register(call
->name
,
216 call
->class->perf_probe
,
218 case TRACE_REG_PERF_UNREGISTER
:
219 tracepoint_probe_unregister(call
->name
,
220 call
->class->perf_probe
,
223 case TRACE_REG_PERF_OPEN
:
224 case TRACE_REG_PERF_CLOSE
:
225 case TRACE_REG_PERF_ADD
:
226 case TRACE_REG_PERF_DEL
:
232 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
234 void trace_event_enable_cmd_record(bool enable
)
236 struct ftrace_event_file
*file
;
237 struct trace_array
*tr
;
239 mutex_lock(&event_mutex
);
240 do_for_each_event_file(tr
, file
) {
242 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
246 tracing_start_cmdline_record();
247 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
249 tracing_stop_cmdline_record();
250 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
252 } while_for_each_event_file();
253 mutex_unlock(&event_mutex
);
256 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
257 int enable
, int soft_disable
)
259 struct ftrace_event_call
*call
= file
->event_call
;
266 * When soft_disable is set and enable is cleared, the sm_ref
267 * reference counter is decremented. If it reaches 0, we want
268 * to clear the SOFT_DISABLED flag but leave the event in the
269 * state that it was. That is, if the event was enabled and
270 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271 * is set we do not want the event to be enabled before we
274 * When soft_disable is not set but the SOFT_MODE flag is,
275 * we do nothing. Do not disable the tracepoint, otherwise
276 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
279 if (atomic_dec_return(&file
->sm_ref
) > 0)
281 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
282 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
284 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
286 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
287 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
288 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
289 tracing_stop_cmdline_record();
290 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
292 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
294 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
295 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
296 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
298 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
302 * When soft_disable is set and enable is set, we want to
303 * register the tracepoint for the event, but leave the event
304 * as is. That means, if the event was already enabled, we do
305 * nothing (but set SOFT_MODE). If the event is disabled, we
306 * set SOFT_DISABLED before enabling the event tracepoint, so
307 * it still seems to be disabled.
310 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
312 if (atomic_inc_return(&file
->sm_ref
) > 1)
314 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
317 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
319 /* Keep the event disabled, when going to SOFT_MODE. */
321 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
323 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
324 tracing_start_cmdline_record();
325 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
327 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
329 tracing_stop_cmdline_record();
330 pr_info("event trace: Could not enable event "
334 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
336 /* WAS_ENABLED gets set but never cleared. */
337 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
345 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
348 return __ftrace_event_enable_disable(file
, enable
, 0);
351 static void ftrace_clear_events(struct trace_array
*tr
)
353 struct ftrace_event_file
*file
;
355 mutex_lock(&event_mutex
);
356 list_for_each_entry(file
, &tr
->events
, list
) {
357 ftrace_event_enable_disable(file
, 0);
359 mutex_unlock(&event_mutex
);
362 static void __put_system(struct event_subsystem
*system
)
364 struct event_filter
*filter
= system
->filter
;
366 WARN_ON_ONCE(system_refcount(system
) == 0);
367 if (system_refcount_dec(system
))
370 list_del(&system
->list
);
373 kfree(filter
->filter_string
);
376 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
381 static void __get_system(struct event_subsystem
*system
)
383 WARN_ON_ONCE(system_refcount(system
) == 0);
384 system_refcount_inc(system
);
387 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
389 WARN_ON_ONCE(dir
->ref_count
== 0);
391 __get_system(dir
->subsystem
);
394 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
396 WARN_ON_ONCE(dir
->ref_count
== 0);
397 /* If the subsystem is about to be freed, the dir must be too */
398 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
400 __put_system(dir
->subsystem
);
401 if (!--dir
->ref_count
)
405 static void put_system(struct ftrace_subsystem_dir
*dir
)
407 mutex_lock(&event_mutex
);
408 __put_system_dir(dir
);
409 mutex_unlock(&event_mutex
);
412 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
417 if (!--dir
->nr_events
) {
418 debugfs_remove_recursive(dir
->entry
);
419 list_del(&dir
->list
);
420 __put_system_dir(dir
);
424 static void *event_file_data(struct file
*filp
)
426 return ACCESS_ONCE(file_inode(filp
)->i_private
);
429 static void remove_event_file_dir(struct ftrace_event_file
*file
)
431 struct dentry
*dir
= file
->dir
;
432 struct dentry
*child
;
435 spin_lock(&dir
->d_lock
); /* probably unneeded */
436 list_for_each_entry(child
, &dir
->d_subdirs
, d_u
.d_child
) {
437 if (child
->d_inode
) /* probably unneeded */
438 child
->d_inode
->i_private
= NULL
;
440 spin_unlock(&dir
->d_lock
);
442 debugfs_remove_recursive(dir
);
445 list_del(&file
->list
);
446 remove_subsystem(file
->system
);
447 kmem_cache_free(file_cachep
, file
);
451 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
454 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
455 const char *sub
, const char *event
, int set
)
457 struct ftrace_event_file
*file
;
458 struct ftrace_event_call
*call
;
461 list_for_each_entry(file
, &tr
->events
, list
) {
463 call
= file
->event_call
;
465 if (!call
->name
|| !call
->class || !call
->class->reg
)
468 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
472 strcmp(match
, call
->name
) != 0 &&
473 strcmp(match
, call
->class->system
) != 0)
476 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
479 if (event
&& strcmp(event
, call
->name
) != 0)
482 ftrace_event_enable_disable(file
, set
);
490 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
491 const char *sub
, const char *event
, int set
)
495 mutex_lock(&event_mutex
);
496 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
497 mutex_unlock(&event_mutex
);
502 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
504 char *event
= NULL
, *sub
= NULL
, *match
;
507 * The buf format can be <subsystem>:<event-name>
508 * *:<event-name> means any event by that name.
509 * :<event-name> is the same.
511 * <subsystem>:* means all events in that subsystem
512 * <subsystem>: means the same.
514 * <name> (no ':') means all events in a subsystem with
515 * the name <name> or any event that matches <name>
518 match
= strsep(&buf
, ":");
524 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
526 if (!strlen(event
) || strcmp(event
, "*") == 0)
530 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
534 * trace_set_clr_event - enable or disable an event
535 * @system: system name to match (NULL for any system)
536 * @event: event name to match (NULL for all events, within system)
537 * @set: 1 to enable, 0 to disable
539 * This is a way for other parts of the kernel to enable or disable
542 * Returns 0 on success, -EINVAL if the parameters do not match any
545 int trace_set_clr_event(const char *system
, const char *event
, int set
)
547 struct trace_array
*tr
= top_trace_array();
549 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
551 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
553 /* 128 should be much more than enough */
554 #define EVENT_BUF_SIZE 127
557 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
558 size_t cnt
, loff_t
*ppos
)
560 struct trace_parser parser
;
561 struct seq_file
*m
= file
->private_data
;
562 struct trace_array
*tr
= m
->private;
568 ret
= tracing_update_buffers();
572 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
575 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
577 if (read
>= 0 && trace_parser_loaded((&parser
))) {
580 if (*parser
.buffer
== '!')
583 parser
.buffer
[parser
.idx
] = 0;
585 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
593 trace_parser_put(&parser
);
599 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
601 struct ftrace_event_file
*file
= v
;
602 struct ftrace_event_call
*call
;
603 struct trace_array
*tr
= m
->private;
607 list_for_each_entry_continue(file
, &tr
->events
, list
) {
608 call
= file
->event_call
;
610 * The ftrace subsystem is for showing formats only.
611 * They can not be enabled or disabled via the event files.
613 if (call
->class && call
->class->reg
)
620 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
622 struct ftrace_event_file
*file
;
623 struct trace_array
*tr
= m
->private;
626 mutex_lock(&event_mutex
);
628 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
629 for (l
= 0; l
<= *pos
; ) {
630 file
= t_next(m
, file
, &l
);
638 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
640 struct ftrace_event_file
*file
= v
;
641 struct trace_array
*tr
= m
->private;
645 list_for_each_entry_continue(file
, &tr
->events
, list
) {
646 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
653 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
655 struct ftrace_event_file
*file
;
656 struct trace_array
*tr
= m
->private;
659 mutex_lock(&event_mutex
);
661 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
662 for (l
= 0; l
<= *pos
; ) {
663 file
= s_next(m
, file
, &l
);
670 static int t_show(struct seq_file
*m
, void *v
)
672 struct ftrace_event_file
*file
= v
;
673 struct ftrace_event_call
*call
= file
->event_call
;
675 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
676 seq_printf(m
, "%s:", call
->class->system
);
677 seq_printf(m
, "%s\n", call
->name
);
682 static void t_stop(struct seq_file
*m
, void *p
)
684 mutex_unlock(&event_mutex
);
688 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
691 struct ftrace_event_file
*file
;
695 mutex_lock(&event_mutex
);
696 file
= event_file_data(filp
);
699 mutex_unlock(&event_mutex
);
704 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
705 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
708 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
709 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
714 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
718 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
721 struct ftrace_event_file
*file
;
725 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
729 ret
= tracing_update_buffers();
737 mutex_lock(&event_mutex
);
738 file
= event_file_data(filp
);
740 ret
= ftrace_event_enable_disable(file
, val
);
741 mutex_unlock(&event_mutex
);
750 return ret
? ret
: cnt
;
754 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
757 const char set_to_char
[4] = { '?', '0', '1', 'X' };
758 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
759 struct event_subsystem
*system
= dir
->subsystem
;
760 struct ftrace_event_call
*call
;
761 struct ftrace_event_file
*file
;
762 struct trace_array
*tr
= dir
->tr
;
767 mutex_lock(&event_mutex
);
768 list_for_each_entry(file
, &tr
->events
, list
) {
769 call
= file
->event_call
;
770 if (!call
->name
|| !call
->class || !call
->class->reg
)
773 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
777 * We need to find out if all the events are set
778 * or if all events or cleared, or if we have
781 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
784 * If we have a mixture, no need to look further.
789 mutex_unlock(&event_mutex
);
791 buf
[0] = set_to_char
[set
];
794 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
800 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
803 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
804 struct event_subsystem
*system
= dir
->subsystem
;
805 const char *name
= NULL
;
809 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
813 ret
= tracing_update_buffers();
817 if (val
!= 0 && val
!= 1)
821 * Opening of "enable" adds a ref count to system,
822 * so the name is safe to use.
827 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
841 FORMAT_FIELD_SEPERATOR
= 2,
845 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
847 struct ftrace_event_call
*call
= event_file_data(m
->private);
848 struct list_head
*common_head
= &ftrace_common_fields
;
849 struct list_head
*head
= trace_get_fields(call
);
850 struct list_head
*node
= v
;
854 switch ((unsigned long)v
) {
859 case FORMAT_FIELD_SEPERATOR
:
863 case FORMAT_PRINTFMT
:
869 if (node
== common_head
)
870 return (void *)FORMAT_FIELD_SEPERATOR
;
871 else if (node
== head
)
872 return (void *)FORMAT_PRINTFMT
;
877 static int f_show(struct seq_file
*m
, void *v
)
879 struct ftrace_event_call
*call
= event_file_data(m
->private);
880 struct ftrace_event_field
*field
;
881 const char *array_descriptor
;
883 switch ((unsigned long)v
) {
885 seq_printf(m
, "name: %s\n", call
->name
);
886 seq_printf(m
, "ID: %d\n", call
->event
.type
);
887 seq_printf(m
, "format:\n");
890 case FORMAT_FIELD_SEPERATOR
:
894 case FORMAT_PRINTFMT
:
895 seq_printf(m
, "\nprint fmt: %s\n",
900 field
= list_entry(v
, struct ftrace_event_field
, link
);
902 * Smartly shows the array type(except dynamic array).
905 * If TYPE := TYPE[LEN], it is shown:
906 * field:TYPE VAR[LEN]
908 array_descriptor
= strchr(field
->type
, '[');
910 if (!strncmp(field
->type
, "__data_loc", 10))
911 array_descriptor
= NULL
;
913 if (!array_descriptor
)
914 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
915 field
->type
, field
->name
, field
->offset
,
916 field
->size
, !!field
->is_signed
);
918 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
919 (int)(array_descriptor
- field
->type
),
920 field
->type
, field
->name
,
921 array_descriptor
, field
->offset
,
922 field
->size
, !!field
->is_signed
);
927 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
929 void *p
= (void *)FORMAT_HEADER
;
932 /* ->stop() is called even if ->start() fails */
933 mutex_lock(&event_mutex
);
934 if (!event_file_data(m
->private))
935 return ERR_PTR(-ENODEV
);
937 while (l
< *pos
&& p
)
938 p
= f_next(m
, p
, &l
);
943 static void f_stop(struct seq_file
*m
, void *p
)
945 mutex_unlock(&event_mutex
);
948 static const struct seq_operations trace_format_seq_ops
= {
955 static int trace_format_open(struct inode
*inode
, struct file
*file
)
960 ret
= seq_open(file
, &trace_format_seq_ops
);
964 m
= file
->private_data
;
971 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
973 int id
= (long)event_file_data(filp
);
983 len
= sprintf(buf
, "%d\n", id
);
985 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
989 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
992 struct ftrace_event_call
*call
;
999 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1006 mutex_lock(&event_mutex
);
1007 call
= event_file_data(filp
);
1009 print_event_filter(call
, s
);
1010 mutex_unlock(&event_mutex
);
1013 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1021 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1024 struct ftrace_event_call
*call
;
1028 if (cnt
>= PAGE_SIZE
)
1031 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1035 if (copy_from_user(buf
, ubuf
, cnt
)) {
1036 free_page((unsigned long) buf
);
1041 mutex_lock(&event_mutex
);
1042 call
= event_file_data(filp
);
1044 err
= apply_event_filter(call
, buf
);
1045 mutex_unlock(&event_mutex
);
1047 free_page((unsigned long) buf
);
1056 static LIST_HEAD(event_subsystems
);
1058 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1060 struct event_subsystem
*system
= NULL
;
1061 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1062 struct trace_array
*tr
;
1065 /* Make sure the system still exists */
1066 mutex_lock(&trace_types_lock
);
1067 mutex_lock(&event_mutex
);
1068 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1069 list_for_each_entry(dir
, &tr
->systems
, list
) {
1070 if (dir
== inode
->i_private
) {
1071 /* Don't open systems with no events */
1072 if (dir
->nr_events
) {
1073 __get_system_dir(dir
);
1074 system
= dir
->subsystem
;
1081 mutex_unlock(&event_mutex
);
1082 mutex_unlock(&trace_types_lock
);
1087 /* Some versions of gcc think dir can be uninitialized here */
1090 /* Still need to increment the ref count of the system */
1091 if (trace_array_get(tr
) < 0) {
1096 ret
= tracing_open_generic(inode
, filp
);
1098 trace_array_put(tr
);
1105 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1107 struct ftrace_subsystem_dir
*dir
;
1108 struct trace_array
*tr
= inode
->i_private
;
1111 if (trace_array_get(tr
) < 0)
1114 /* Make a temporary dir that has no system but points to tr */
1115 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1117 trace_array_put(tr
);
1123 ret
= tracing_open_generic(inode
, filp
);
1125 trace_array_put(tr
);
1129 filp
->private_data
= dir
;
1134 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1136 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1138 trace_array_put(dir
->tr
);
1141 * If dir->subsystem is NULL, then this is a temporary
1142 * descriptor that was made for a trace_array to enable
1154 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1157 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1158 struct event_subsystem
*system
= dir
->subsystem
;
1159 struct trace_seq
*s
;
1165 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1171 print_subsystem_event_filter(system
, s
);
1172 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1180 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1183 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1187 if (cnt
>= PAGE_SIZE
)
1190 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1194 if (copy_from_user(buf
, ubuf
, cnt
)) {
1195 free_page((unsigned long) buf
);
1200 err
= apply_subsystem_event_filter(dir
, buf
);
1201 free_page((unsigned long) buf
);
1211 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1213 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1214 struct trace_seq
*s
;
1220 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1227 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1234 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1235 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1236 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1238 static const struct seq_operations show_event_seq_ops
= {
1245 static const struct seq_operations show_set_event_seq_ops
= {
1252 static const struct file_operations ftrace_avail_fops
= {
1253 .open
= ftrace_event_avail_open
,
1255 .llseek
= seq_lseek
,
1256 .release
= seq_release
,
1259 static const struct file_operations ftrace_set_event_fops
= {
1260 .open
= ftrace_event_set_open
,
1262 .write
= ftrace_event_write
,
1263 .llseek
= seq_lseek
,
1264 .release
= ftrace_event_release
,
1267 static const struct file_operations ftrace_enable_fops
= {
1268 .open
= tracing_open_generic
,
1269 .read
= event_enable_read
,
1270 .write
= event_enable_write
,
1271 .llseek
= default_llseek
,
1274 static const struct file_operations ftrace_event_format_fops
= {
1275 .open
= trace_format_open
,
1277 .llseek
= seq_lseek
,
1278 .release
= seq_release
,
1281 static const struct file_operations ftrace_event_id_fops
= {
1282 .read
= event_id_read
,
1283 .llseek
= default_llseek
,
1286 static const struct file_operations ftrace_event_filter_fops
= {
1287 .open
= tracing_open_generic
,
1288 .read
= event_filter_read
,
1289 .write
= event_filter_write
,
1290 .llseek
= default_llseek
,
1293 static const struct file_operations ftrace_subsystem_filter_fops
= {
1294 .open
= subsystem_open
,
1295 .read
= subsystem_filter_read
,
1296 .write
= subsystem_filter_write
,
1297 .llseek
= default_llseek
,
1298 .release
= subsystem_release
,
1301 static const struct file_operations ftrace_system_enable_fops
= {
1302 .open
= subsystem_open
,
1303 .read
= system_enable_read
,
1304 .write
= system_enable_write
,
1305 .llseek
= default_llseek
,
1306 .release
= subsystem_release
,
1309 static const struct file_operations ftrace_tr_enable_fops
= {
1310 .open
= system_tr_open
,
1311 .read
= system_enable_read
,
1312 .write
= system_enable_write
,
1313 .llseek
= default_llseek
,
1314 .release
= subsystem_release
,
1317 static const struct file_operations ftrace_show_header_fops
= {
1318 .open
= tracing_open_generic
,
1319 .read
= show_header
,
1320 .llseek
= default_llseek
,
1324 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1325 const struct seq_operations
*seq_ops
)
1330 ret
= seq_open(file
, seq_ops
);
1333 m
= file
->private_data
;
1334 /* copy tr over to seq ops */
1335 m
->private = inode
->i_private
;
1340 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1342 struct trace_array
*tr
= inode
->i_private
;
1344 trace_array_put(tr
);
1346 return seq_release(inode
, file
);
1350 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1352 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1354 return ftrace_event_open(inode
, file
, seq_ops
);
1358 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1360 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1361 struct trace_array
*tr
= inode
->i_private
;
1364 if (trace_array_get(tr
) < 0)
1367 if ((file
->f_mode
& FMODE_WRITE
) &&
1368 (file
->f_flags
& O_TRUNC
))
1369 ftrace_clear_events(tr
);
1371 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1373 trace_array_put(tr
);
1377 static struct event_subsystem
*
1378 create_new_subsystem(const char *name
)
1380 struct event_subsystem
*system
;
1382 /* need to create new entry */
1383 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1387 system
->ref_count
= 1;
1389 /* Only allocate if dynamic (kprobes and modules) */
1390 if (!core_kernel_data((unsigned long)name
)) {
1391 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1392 system
->name
= kstrdup(name
, GFP_KERNEL
);
1396 system
->name
= name
;
1398 system
->filter
= NULL
;
1400 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1401 if (!system
->filter
)
1404 list_add(&system
->list
, &event_subsystems
);
1409 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1410 kfree(system
->name
);
1415 static struct dentry
*
1416 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1417 struct ftrace_event_file
*file
, struct dentry
*parent
)
1419 struct ftrace_subsystem_dir
*dir
;
1420 struct event_subsystem
*system
;
1421 struct dentry
*entry
;
1423 /* First see if we did not already create this dir */
1424 list_for_each_entry(dir
, &tr
->systems
, list
) {
1425 system
= dir
->subsystem
;
1426 if (strcmp(system
->name
, name
) == 0) {
1433 /* Now see if the system itself exists. */
1434 list_for_each_entry(system
, &event_subsystems
, list
) {
1435 if (strcmp(system
->name
, name
) == 0)
1438 /* Reset system variable when not found */
1439 if (&system
->list
== &event_subsystems
)
1442 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1447 system
= create_new_subsystem(name
);
1451 __get_system(system
);
1453 dir
->entry
= debugfs_create_dir(name
, parent
);
1455 pr_warning("Failed to create system directory %s\n", name
);
1456 __put_system(system
);
1463 dir
->subsystem
= system
;
1466 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1467 &ftrace_subsystem_filter_fops
);
1469 kfree(system
->filter
);
1470 system
->filter
= NULL
;
1471 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1474 trace_create_file("enable", 0644, dir
->entry
, dir
,
1475 &ftrace_system_enable_fops
);
1477 list_add(&dir
->list
, &tr
->systems
);
1484 /* Only print this message if failed on memory allocation */
1485 if (!dir
|| !system
)
1486 pr_warning("No memory to create event subsystem %s\n",
1492 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1494 struct ftrace_event_call
*call
= file
->event_call
;
1495 struct trace_array
*tr
= file
->tr
;
1496 struct list_head
*head
;
1497 struct dentry
*d_events
;
1501 * If the trace point header did not define TRACE_SYSTEM
1502 * then the system would be called "TRACE_SYSTEM".
1504 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1505 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1511 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1513 pr_warning("Could not create debugfs '%s' directory\n",
1518 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1519 trace_create_file("enable", 0644, file
->dir
, file
,
1520 &ftrace_enable_fops
);
1522 #ifdef CONFIG_PERF_EVENTS
1523 if (call
->event
.type
&& call
->class->reg
)
1524 trace_create_file("id", 0444, file
->dir
,
1525 (void *)(long)call
->event
.type
,
1526 &ftrace_event_id_fops
);
1530 * Other events may have the same class. Only update
1531 * the fields if they are not already defined.
1533 head
= trace_get_fields(call
);
1534 if (list_empty(head
)) {
1535 ret
= call
->class->define_fields(call
);
1537 pr_warning("Could not initialize trace point"
1538 " events/%s\n", call
->name
);
1542 trace_create_file("filter", 0644, file
->dir
, call
,
1543 &ftrace_event_filter_fops
);
1545 trace_create_file("format", 0444, file
->dir
, call
,
1546 &ftrace_event_format_fops
);
1551 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1553 struct ftrace_event_file
*file
;
1554 struct trace_array
*tr
;
1556 do_for_each_event_file_safe(tr
, file
) {
1557 if (file
->event_call
!= call
)
1560 remove_event_file_dir(file
);
1562 * The do_for_each_event_file_safe() is
1563 * a double loop. After finding the call for this
1564 * trace_array, we use break to jump to the next
1568 } while_for_each_event_file();
1571 static void event_remove(struct ftrace_event_call
*call
)
1573 struct trace_array
*tr
;
1574 struct ftrace_event_file
*file
;
1576 do_for_each_event_file(tr
, file
) {
1577 if (file
->event_call
!= call
)
1579 ftrace_event_enable_disable(file
, 0);
1581 * The do_for_each_event_file() is
1582 * a double loop. After finding the call for this
1583 * trace_array, we use break to jump to the next
1587 } while_for_each_event_file();
1589 if (call
->event
.funcs
)
1590 __unregister_ftrace_event(&call
->event
);
1591 remove_event_from_tracers(call
);
1592 list_del(&call
->list
);
1595 static int event_init(struct ftrace_event_call
*call
)
1599 if (WARN_ON(!call
->name
))
1602 if (call
->class->raw_init
) {
1603 ret
= call
->class->raw_init(call
);
1604 if (ret
< 0 && ret
!= -ENOSYS
)
1605 pr_warn("Could not initialize trace events/%s\n",
1613 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1617 ret
= event_init(call
);
1621 list_add(&call
->list
, &ftrace_events
);
1627 static struct ftrace_event_file
*
1628 trace_create_new_event(struct ftrace_event_call
*call
,
1629 struct trace_array
*tr
)
1631 struct ftrace_event_file
*file
;
1633 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1637 file
->event_call
= call
;
1639 atomic_set(&file
->sm_ref
, 0);
1640 list_add(&file
->list
, &tr
->events
);
1645 /* Add an event to a trace directory */
1647 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1649 struct ftrace_event_file
*file
;
1651 file
= trace_create_new_event(call
, tr
);
1655 return event_create_dir(tr
->event_dir
, file
);
1659 * Just create a decriptor for early init. A descriptor is required
1660 * for enabling events at boot. We want to enable events before
1661 * the filesystem is initialized.
1664 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1665 struct trace_array
*tr
)
1667 struct ftrace_event_file
*file
;
1669 file
= trace_create_new_event(call
, tr
);
1676 struct ftrace_module_file_ops
;
1677 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1679 /* Add an additional event_call dynamically */
1680 int trace_add_event_call(struct ftrace_event_call
*call
)
1683 mutex_lock(&trace_types_lock
);
1684 mutex_lock(&event_mutex
);
1686 ret
= __register_event(call
, NULL
);
1688 __add_event_to_tracers(call
);
1690 mutex_unlock(&event_mutex
);
1691 mutex_unlock(&trace_types_lock
);
1696 * Must be called under locking of trace_types_lock, event_mutex and
1699 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1702 trace_destroy_fields(call
);
1703 destroy_preds(call
);
1706 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1708 struct trace_array
*tr
;
1709 struct ftrace_event_file
*file
;
1711 #ifdef CONFIG_PERF_EVENTS
1712 if (call
->perf_refcount
)
1715 do_for_each_event_file(tr
, file
) {
1716 if (file
->event_call
!= call
)
1719 * We can't rely on ftrace_event_enable_disable(enable => 0)
1720 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1721 * TRACE_REG_UNREGISTER.
1723 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1726 * The do_for_each_event_file_safe() is
1727 * a double loop. After finding the call for this
1728 * trace_array, we use break to jump to the next
1732 } while_for_each_event_file();
1734 __trace_remove_event_call(call
);
1739 /* Remove an event_call */
1740 int trace_remove_event_call(struct ftrace_event_call
*call
)
1744 mutex_lock(&trace_types_lock
);
1745 mutex_lock(&event_mutex
);
1746 down_write(&trace_event_sem
);
1747 ret
= probe_remove_event_call(call
);
1748 up_write(&trace_event_sem
);
1749 mutex_unlock(&event_mutex
);
1750 mutex_unlock(&trace_types_lock
);
1755 #define for_each_event(event, start, end) \
1756 for (event = start; \
1757 (unsigned long)event < (unsigned long)end; \
1760 #ifdef CONFIG_MODULES
1762 static void trace_module_add_events(struct module
*mod
)
1764 struct ftrace_event_call
**call
, **start
, **end
;
1766 start
= mod
->trace_events
;
1767 end
= mod
->trace_events
+ mod
->num_trace_events
;
1769 for_each_event(call
, start
, end
) {
1770 __register_event(*call
, mod
);
1771 __add_event_to_tracers(*call
);
1775 static void trace_module_remove_events(struct module
*mod
)
1777 struct ftrace_event_call
*call
, *p
;
1778 bool clear_trace
= false;
1780 down_write(&trace_event_sem
);
1781 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1782 if (call
->mod
== mod
) {
1783 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1785 __trace_remove_event_call(call
);
1788 up_write(&trace_event_sem
);
1791 * It is safest to reset the ring buffer if the module being unloaded
1792 * registered any events that were used. The only worry is if
1793 * a new module gets loaded, and takes on the same id as the events
1794 * of this module. When printing out the buffer, traced events left
1795 * over from this module may be passed to the new module events and
1796 * unexpected results may occur.
1799 tracing_reset_all_online_cpus();
1802 static int trace_module_notify(struct notifier_block
*self
,
1803 unsigned long val
, void *data
)
1805 struct module
*mod
= data
;
1807 mutex_lock(&trace_types_lock
);
1808 mutex_lock(&event_mutex
);
1810 case MODULE_STATE_COMING
:
1811 trace_module_add_events(mod
);
1813 case MODULE_STATE_GOING
:
1814 trace_module_remove_events(mod
);
1817 mutex_unlock(&event_mutex
);
1818 mutex_unlock(&trace_types_lock
);
1823 static struct notifier_block trace_module_nb
= {
1824 .notifier_call
= trace_module_notify
,
1827 #endif /* CONFIG_MODULES */
1829 /* Create a new event directory structure for a trace directory. */
1831 __trace_add_event_dirs(struct trace_array
*tr
)
1833 struct ftrace_event_call
*call
;
1836 list_for_each_entry(call
, &ftrace_events
, list
) {
1837 ret
= __trace_add_new_event(call
, tr
);
1839 pr_warning("Could not create directory for event %s\n",
1844 #ifdef CONFIG_DYNAMIC_FTRACE
1847 #define ENABLE_EVENT_STR "enable_event"
1848 #define DISABLE_EVENT_STR "disable_event"
1850 struct event_probe_data
{
1851 struct ftrace_event_file
*file
;
1852 unsigned long count
;
1857 static struct ftrace_event_file
*
1858 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1860 struct ftrace_event_file
*file
;
1861 struct ftrace_event_call
*call
;
1863 list_for_each_entry(file
, &tr
->events
, list
) {
1865 call
= file
->event_call
;
1867 if (!call
->name
|| !call
->class || !call
->class->reg
)
1870 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1873 if (strcmp(event
, call
->name
) == 0 &&
1874 strcmp(system
, call
->class->system
) == 0)
1881 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1883 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1884 struct event_probe_data
*data
= *pdata
;
1890 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1892 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1896 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1898 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1899 struct event_probe_data
*data
= *pdata
;
1907 /* Skip if the event is in a state we want to switch to */
1908 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1911 if (data
->count
!= -1)
1914 event_enable_probe(ip
, parent_ip
, _data
);
1918 event_enable_print(struct seq_file
*m
, unsigned long ip
,
1919 struct ftrace_probe_ops
*ops
, void *_data
)
1921 struct event_probe_data
*data
= _data
;
1923 seq_printf(m
, "%ps:", (void *)ip
);
1925 seq_printf(m
, "%s:%s:%s",
1926 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1927 data
->file
->event_call
->class->system
,
1928 data
->file
->event_call
->name
);
1930 if (data
->count
== -1)
1931 seq_printf(m
, ":unlimited\n");
1933 seq_printf(m
, ":count=%ld\n", data
->count
);
1939 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1942 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1943 struct event_probe_data
*data
= *pdata
;
1950 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1953 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1954 struct event_probe_data
*data
= *pdata
;
1956 if (WARN_ON_ONCE(data
->ref
<= 0))
1961 /* Remove the SOFT_MODE flag */
1962 __ftrace_event_enable_disable(data
->file
, 0, 1);
1963 module_put(data
->file
->event_call
->mod
);
1969 static struct ftrace_probe_ops event_enable_probe_ops
= {
1970 .func
= event_enable_probe
,
1971 .print
= event_enable_print
,
1972 .init
= event_enable_init
,
1973 .free
= event_enable_free
,
1976 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
1977 .func
= event_enable_count_probe
,
1978 .print
= event_enable_print
,
1979 .init
= event_enable_init
,
1980 .free
= event_enable_free
,
1983 static struct ftrace_probe_ops event_disable_probe_ops
= {
1984 .func
= event_enable_probe
,
1985 .print
= event_enable_print
,
1986 .init
= event_enable_init
,
1987 .free
= event_enable_free
,
1990 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
1991 .func
= event_enable_count_probe
,
1992 .print
= event_enable_print
,
1993 .init
= event_enable_init
,
1994 .free
= event_enable_free
,
1998 event_enable_func(struct ftrace_hash
*hash
,
1999 char *glob
, char *cmd
, char *param
, int enabled
)
2001 struct trace_array
*tr
= top_trace_array();
2002 struct ftrace_event_file
*file
;
2003 struct ftrace_probe_ops
*ops
;
2004 struct event_probe_data
*data
;
2011 /* hash funcs only work with set_ftrace_filter */
2012 if (!enabled
|| !param
)
2015 system
= strsep(¶m
, ":");
2019 event
= strsep(¶m
, ":");
2021 mutex_lock(&event_mutex
);
2024 file
= find_event_file(tr
, system
, event
);
2028 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2031 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2033 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2035 if (glob
[0] == '!') {
2036 unregister_ftrace_function_probe_func(glob
+1, ops
);
2042 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2046 data
->enable
= enable
;
2053 number
= strsep(¶m
, ":");
2056 if (!strlen(number
))
2060 * We use the callback data field (which is a pointer)
2063 ret
= kstrtoul(number
, 0, &data
->count
);
2068 /* Don't let event modules unload while probe registered */
2069 ret
= try_module_get(file
->event_call
->mod
);
2075 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2078 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2080 * The above returns on success the # of functions enabled,
2081 * but if it didn't find any functions it returns zero.
2082 * Consider no functions a failure too.
2089 /* Just return zero, not the number of enabled functions */
2092 mutex_unlock(&event_mutex
);
2096 __ftrace_event_enable_disable(file
, 0, 1);
2098 module_put(file
->event_call
->mod
);
2104 static struct ftrace_func_command event_enable_cmd
= {
2105 .name
= ENABLE_EVENT_STR
,
2106 .func
= event_enable_func
,
2109 static struct ftrace_func_command event_disable_cmd
= {
2110 .name
= DISABLE_EVENT_STR
,
2111 .func
= event_enable_func
,
2114 static __init
int register_event_cmds(void)
2118 ret
= register_ftrace_command(&event_enable_cmd
);
2119 if (WARN_ON(ret
< 0))
2121 ret
= register_ftrace_command(&event_disable_cmd
);
2122 if (WARN_ON(ret
< 0))
2123 unregister_ftrace_command(&event_enable_cmd
);
2127 static inline int register_event_cmds(void) { return 0; }
2128 #endif /* CONFIG_DYNAMIC_FTRACE */
2131 * The top level array has already had its ftrace_event_file
2132 * descriptors created in order to allow for early events to
2133 * be recorded. This function is called after the debugfs has been
2134 * initialized, and we now have to create the files associated
2138 __trace_early_add_event_dirs(struct trace_array
*tr
)
2140 struct ftrace_event_file
*file
;
2144 list_for_each_entry(file
, &tr
->events
, list
) {
2145 ret
= event_create_dir(tr
->event_dir
, file
);
2147 pr_warning("Could not create directory for event %s\n",
2148 file
->event_call
->name
);
2153 * For early boot up, the top trace array requires to have
2154 * a list of events that can be enabled. This must be done before
2155 * the filesystem is set up in order to allow events to be traced
2159 __trace_early_add_events(struct trace_array
*tr
)
2161 struct ftrace_event_call
*call
;
2164 list_for_each_entry(call
, &ftrace_events
, list
) {
2165 /* Early boot up should not have any modules loaded */
2166 if (WARN_ON_ONCE(call
->mod
))
2169 ret
= __trace_early_add_new_event(call
, tr
);
2171 pr_warning("Could not create early event %s\n",
2176 /* Remove the event directory structure for a trace directory. */
2178 __trace_remove_event_dirs(struct trace_array
*tr
)
2180 struct ftrace_event_file
*file
, *next
;
2182 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2183 remove_event_file_dir(file
);
2186 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2188 struct trace_array
*tr
;
2190 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2191 __trace_add_new_event(call
, tr
);
2194 extern struct ftrace_event_call
*__start_ftrace_events
[];
2195 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2197 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2199 static __init
int setup_trace_event(char *str
)
2201 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2202 ring_buffer_expanded
= true;
2203 tracing_selftest_disabled
= true;
2207 __setup("trace_event=", setup_trace_event
);
2209 /* Expects to have event_mutex held when called */
2211 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2213 struct dentry
*d_events
;
2214 struct dentry
*entry
;
2216 entry
= debugfs_create_file("set_event", 0644, parent
,
2217 tr
, &ftrace_set_event_fops
);
2219 pr_warning("Could not create debugfs 'set_event' entry\n");
2223 d_events
= debugfs_create_dir("events", parent
);
2225 pr_warning("Could not create debugfs 'events' directory\n");
2229 /* ring buffer internal formats */
2230 trace_create_file("header_page", 0444, d_events
,
2231 ring_buffer_print_page_header
,
2232 &ftrace_show_header_fops
);
2234 trace_create_file("header_event", 0444, d_events
,
2235 ring_buffer_print_entry_header
,
2236 &ftrace_show_header_fops
);
2238 trace_create_file("enable", 0644, d_events
,
2239 tr
, &ftrace_tr_enable_fops
);
2241 tr
->event_dir
= d_events
;
2247 * event_trace_add_tracer - add a instance of a trace_array to events
2248 * @parent: The parent dentry to place the files/directories for events in
2249 * @tr: The trace array associated with these events
2251 * When a new instance is created, it needs to set up its events
2252 * directory, as well as other files associated with events. It also
2253 * creates the event hierachry in the @parent/events directory.
2255 * Returns 0 on success.
2257 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2261 mutex_lock(&event_mutex
);
2263 ret
= create_event_toplevel_files(parent
, tr
);
2267 down_write(&trace_event_sem
);
2268 __trace_add_event_dirs(tr
);
2269 up_write(&trace_event_sem
);
2272 mutex_unlock(&event_mutex
);
2278 * The top trace array already had its file descriptors created.
2279 * Now the files themselves need to be created.
2282 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2286 mutex_lock(&event_mutex
);
2288 ret
= create_event_toplevel_files(parent
, tr
);
2292 down_write(&trace_event_sem
);
2293 __trace_early_add_event_dirs(tr
);
2294 up_write(&trace_event_sem
);
2297 mutex_unlock(&event_mutex
);
2302 int event_trace_del_tracer(struct trace_array
*tr
)
2304 mutex_lock(&event_mutex
);
2306 /* Disable any running events */
2307 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2309 down_write(&trace_event_sem
);
2310 __trace_remove_event_dirs(tr
);
2311 debugfs_remove_recursive(tr
->event_dir
);
2312 up_write(&trace_event_sem
);
2314 tr
->event_dir
= NULL
;
2316 mutex_unlock(&event_mutex
);
2321 static __init
int event_trace_memsetup(void)
2323 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2324 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2328 static __init
int event_trace_enable(void)
2330 struct trace_array
*tr
= top_trace_array();
2331 struct ftrace_event_call
**iter
, *call
;
2332 char *buf
= bootup_event_buf
;
2336 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2339 ret
= event_init(call
);
2341 list_add(&call
->list
, &ftrace_events
);
2345 * We need the top trace array to have a working set of trace
2346 * points at early init, before the debug files and directories
2347 * are created. Create the file entries now, and attach them
2348 * to the actual file dentries later.
2350 __trace_early_add_events(tr
);
2353 token
= strsep(&buf
, ",");
2360 ret
= ftrace_set_clr_event(tr
, token
, 1);
2362 pr_warn("Failed to enable trace event: %s\n", token
);
2365 trace_printk_start_comm();
2367 register_event_cmds();
2372 static __init
int event_trace_init(void)
2374 struct trace_array
*tr
;
2375 struct dentry
*d_tracer
;
2376 struct dentry
*entry
;
2379 tr
= top_trace_array();
2381 d_tracer
= tracing_init_dentry();
2385 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2386 tr
, &ftrace_avail_fops
);
2388 pr_warning("Could not create debugfs "
2389 "'available_events' entry\n");
2391 if (trace_define_common_fields())
2392 pr_warning("tracing: Failed to allocate common fields");
2394 ret
= early_event_add_tracer(d_tracer
, tr
);
2398 #ifdef CONFIG_MODULES
2399 ret
= register_module_notifier(&trace_module_nb
);
2401 pr_warning("Failed to register trace events module notifier\n");
2405 early_initcall(event_trace_memsetup
);
2406 core_initcall(event_trace_enable
);
2407 fs_initcall(event_trace_init
);
2409 #ifdef CONFIG_FTRACE_STARTUP_TEST
2411 static DEFINE_SPINLOCK(test_spinlock
);
2412 static DEFINE_SPINLOCK(test_spinlock_irq
);
2413 static DEFINE_MUTEX(test_mutex
);
2415 static __init
void test_work(struct work_struct
*dummy
)
2417 spin_lock(&test_spinlock
);
2418 spin_lock_irq(&test_spinlock_irq
);
2420 spin_unlock_irq(&test_spinlock_irq
);
2421 spin_unlock(&test_spinlock
);
2423 mutex_lock(&test_mutex
);
2425 mutex_unlock(&test_mutex
);
2428 static __init
int event_test_thread(void *unused
)
2432 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2434 pr_info("failed to kmalloc\n");
2436 schedule_on_each_cpu(test_work
);
2440 set_current_state(TASK_INTERRUPTIBLE
);
2441 while (!kthread_should_stop())
2448 * Do various things that may trigger events.
2450 static __init
void event_test_stuff(void)
2452 struct task_struct
*test_thread
;
2454 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2456 kthread_stop(test_thread
);
2460 * For every trace event defined, we will test each trace point separately,
2461 * and then by groups, and finally all trace points.
2463 static __init
void event_trace_self_tests(void)
2465 struct ftrace_subsystem_dir
*dir
;
2466 struct ftrace_event_file
*file
;
2467 struct ftrace_event_call
*call
;
2468 struct event_subsystem
*system
;
2469 struct trace_array
*tr
;
2472 tr
= top_trace_array();
2474 pr_info("Running tests on trace events:\n");
2476 list_for_each_entry(file
, &tr
->events
, list
) {
2478 call
= file
->event_call
;
2480 /* Only test those that have a probe */
2481 if (!call
->class || !call
->class->probe
)
2485 * Testing syscall events here is pretty useless, but
2486 * we still do it if configured. But this is time consuming.
2487 * What we really need is a user thread to perform the
2488 * syscalls as we test.
2490 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2491 if (call
->class->system
&&
2492 strcmp(call
->class->system
, "syscalls") == 0)
2496 pr_info("Testing event %s: ", call
->name
);
2499 * If an event is already enabled, someone is using
2500 * it and the self test should not be on.
2502 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2503 pr_warning("Enabled event during self test!\n");
2508 ftrace_event_enable_disable(file
, 1);
2510 ftrace_event_enable_disable(file
, 0);
2515 /* Now test at the sub system level */
2517 pr_info("Running tests on trace event systems:\n");
2519 list_for_each_entry(dir
, &tr
->systems
, list
) {
2521 system
= dir
->subsystem
;
2523 /* the ftrace system is special, skip it */
2524 if (strcmp(system
->name
, "ftrace") == 0)
2527 pr_info("Testing event system %s: ", system
->name
);
2529 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2530 if (WARN_ON_ONCE(ret
)) {
2531 pr_warning("error enabling system %s\n",
2538 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2539 if (WARN_ON_ONCE(ret
)) {
2540 pr_warning("error disabling system %s\n",
2548 /* Test with all events enabled */
2550 pr_info("Running tests on all trace events:\n");
2551 pr_info("Testing all events: ");
2553 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2554 if (WARN_ON_ONCE(ret
)) {
2555 pr_warning("error enabling all events\n");
2562 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2563 if (WARN_ON_ONCE(ret
)) {
2564 pr_warning("error disabling all events\n");
2571 #ifdef CONFIG_FUNCTION_TRACER
2573 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2576 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2577 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2579 struct ring_buffer_event
*event
;
2580 struct ring_buffer
*buffer
;
2581 struct ftrace_entry
*entry
;
2582 unsigned long flags
;
2587 pc
= preempt_count();
2588 preempt_disable_notrace();
2589 cpu
= raw_smp_processor_id();
2590 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2595 local_save_flags(flags
);
2597 event
= trace_current_buffer_lock_reserve(&buffer
,
2598 TRACE_FN
, sizeof(*entry
),
2602 entry
= ring_buffer_event_data(event
);
2604 entry
->parent_ip
= parent_ip
;
2606 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2609 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2610 preempt_enable_notrace();
2613 static struct ftrace_ops trace_ops __initdata
=
2615 .func
= function_test_events_call
,
2616 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2619 static __init
void event_trace_self_test_with_function(void)
2622 ret
= register_ftrace_function(&trace_ops
);
2623 if (WARN_ON(ret
< 0)) {
2624 pr_info("Failed to enable function tracer for event tests\n");
2627 pr_info("Running tests again, along with the function tracer\n");
2628 event_trace_self_tests();
2629 unregister_ftrace_function(&trace_ops
);
2632 static __init
void event_trace_self_test_with_function(void)
2637 static __init
int event_trace_self_tests_init(void)
2639 if (!tracing_selftest_disabled
) {
2640 event_trace_self_tests();
2641 event_trace_self_test_with_function();
2647 late_initcall(event_trace_self_tests_init
);