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 int ftrace_event_reg(struct ftrace_event_call
*call
,
192 enum trace_reg type
, void *data
)
194 struct ftrace_event_file
*file
= data
;
197 case TRACE_REG_REGISTER
:
198 return tracepoint_probe_register(call
->name
,
201 case TRACE_REG_UNREGISTER
:
202 tracepoint_probe_unregister(call
->name
,
207 #ifdef CONFIG_PERF_EVENTS
208 case TRACE_REG_PERF_REGISTER
:
209 return tracepoint_probe_register(call
->name
,
210 call
->class->perf_probe
,
212 case TRACE_REG_PERF_UNREGISTER
:
213 tracepoint_probe_unregister(call
->name
,
214 call
->class->perf_probe
,
217 case TRACE_REG_PERF_OPEN
:
218 case TRACE_REG_PERF_CLOSE
:
219 case TRACE_REG_PERF_ADD
:
220 case TRACE_REG_PERF_DEL
:
226 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
228 void trace_event_enable_cmd_record(bool enable
)
230 struct ftrace_event_file
*file
;
231 struct trace_array
*tr
;
233 mutex_lock(&event_mutex
);
234 do_for_each_event_file(tr
, file
) {
236 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
240 tracing_start_cmdline_record();
241 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
243 tracing_stop_cmdline_record();
244 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
246 } while_for_each_event_file();
247 mutex_unlock(&event_mutex
);
250 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
251 int enable
, int soft_disable
)
253 struct ftrace_event_call
*call
= file
->event_call
;
260 * When soft_disable is set and enable is cleared, the sm_ref
261 * reference counter is decremented. If it reaches 0, we want
262 * to clear the SOFT_DISABLED flag but leave the event in the
263 * state that it was. That is, if the event was enabled and
264 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265 * is set we do not want the event to be enabled before we
268 * When soft_disable is not set but the SOFT_MODE flag is,
269 * we do nothing. Do not disable the tracepoint, otherwise
270 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
273 if (atomic_dec_return(&file
->sm_ref
) > 0)
275 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
276 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
278 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
280 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
281 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
282 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
283 tracing_stop_cmdline_record();
284 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
286 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
288 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
289 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
290 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
292 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
296 * When soft_disable is set and enable is set, we want to
297 * register the tracepoint for the event, but leave the event
298 * as is. That means, if the event was already enabled, we do
299 * nothing (but set SOFT_MODE). If the event is disabled, we
300 * set SOFT_DISABLED before enabling the event tracepoint, so
301 * it still seems to be disabled.
304 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
306 if (atomic_inc_return(&file
->sm_ref
) > 1)
308 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
311 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
313 /* Keep the event disabled, when going to SOFT_MODE. */
315 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
317 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
318 tracing_start_cmdline_record();
319 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
321 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
323 tracing_stop_cmdline_record();
324 pr_info("event trace: Could not enable event "
328 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
330 /* WAS_ENABLED gets set but never cleared. */
331 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
339 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
342 return __ftrace_event_enable_disable(file
, enable
, 0);
345 static void ftrace_clear_events(struct trace_array
*tr
)
347 struct ftrace_event_file
*file
;
349 mutex_lock(&event_mutex
);
350 list_for_each_entry(file
, &tr
->events
, list
) {
351 ftrace_event_enable_disable(file
, 0);
353 mutex_unlock(&event_mutex
);
356 static void __put_system(struct event_subsystem
*system
)
358 struct event_filter
*filter
= system
->filter
;
360 WARN_ON_ONCE(system_refcount(system
) == 0);
361 if (system_refcount_dec(system
))
364 list_del(&system
->list
);
367 kfree(filter
->filter_string
);
370 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
375 static void __get_system(struct event_subsystem
*system
)
377 WARN_ON_ONCE(system_refcount(system
) == 0);
378 system_refcount_inc(system
);
381 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
383 WARN_ON_ONCE(dir
->ref_count
== 0);
385 __get_system(dir
->subsystem
);
388 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
390 WARN_ON_ONCE(dir
->ref_count
== 0);
391 /* If the subsystem is about to be freed, the dir must be too */
392 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
394 __put_system(dir
->subsystem
);
395 if (!--dir
->ref_count
)
399 static void put_system(struct ftrace_subsystem_dir
*dir
)
401 mutex_lock(&event_mutex
);
402 __put_system_dir(dir
);
403 mutex_unlock(&event_mutex
);
406 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
411 if (!--dir
->nr_events
) {
412 debugfs_remove_recursive(dir
->entry
);
413 list_del(&dir
->list
);
414 __put_system_dir(dir
);
418 static void *event_file_data(struct file
*filp
)
420 return ACCESS_ONCE(file_inode(filp
)->i_private
);
423 static void remove_event_file_dir(struct ftrace_event_file
*file
)
425 struct dentry
*dir
= file
->dir
;
426 struct dentry
*child
;
429 spin_lock(&dir
->d_lock
); /* probably unneeded */
430 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
431 if (child
->d_inode
) /* probably unneeded */
432 child
->d_inode
->i_private
= NULL
;
434 spin_unlock(&dir
->d_lock
);
436 debugfs_remove_recursive(dir
);
439 list_del(&file
->list
);
440 remove_subsystem(file
->system
);
441 kmem_cache_free(file_cachep
, file
);
445 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
448 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
449 const char *sub
, const char *event
, int set
)
451 struct ftrace_event_file
*file
;
452 struct ftrace_event_call
*call
;
455 list_for_each_entry(file
, &tr
->events
, list
) {
457 call
= file
->event_call
;
459 if (!call
->name
|| !call
->class || !call
->class->reg
)
462 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
466 strcmp(match
, call
->name
) != 0 &&
467 strcmp(match
, call
->class->system
) != 0)
470 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
473 if (event
&& strcmp(event
, call
->name
) != 0)
476 ftrace_event_enable_disable(file
, set
);
484 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
485 const char *sub
, const char *event
, int set
)
489 mutex_lock(&event_mutex
);
490 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
491 mutex_unlock(&event_mutex
);
496 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
498 char *event
= NULL
, *sub
= NULL
, *match
;
501 * The buf format can be <subsystem>:<event-name>
502 * *:<event-name> means any event by that name.
503 * :<event-name> is the same.
505 * <subsystem>:* means all events in that subsystem
506 * <subsystem>: means the same.
508 * <name> (no ':') means all events in a subsystem with
509 * the name <name> or any event that matches <name>
512 match
= strsep(&buf
, ":");
518 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
520 if (!strlen(event
) || strcmp(event
, "*") == 0)
524 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
528 * trace_set_clr_event - enable or disable an event
529 * @system: system name to match (NULL for any system)
530 * @event: event name to match (NULL for all events, within system)
531 * @set: 1 to enable, 0 to disable
533 * This is a way for other parts of the kernel to enable or disable
536 * Returns 0 on success, -EINVAL if the parameters do not match any
539 int trace_set_clr_event(const char *system
, const char *event
, int set
)
541 struct trace_array
*tr
= top_trace_array();
543 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
545 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
547 /* 128 should be much more than enough */
548 #define EVENT_BUF_SIZE 127
551 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
552 size_t cnt
, loff_t
*ppos
)
554 struct trace_parser parser
;
555 struct seq_file
*m
= file
->private_data
;
556 struct trace_array
*tr
= m
->private;
562 ret
= tracing_update_buffers();
566 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
569 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
571 if (read
>= 0 && trace_parser_loaded((&parser
))) {
574 if (*parser
.buffer
== '!')
577 parser
.buffer
[parser
.idx
] = 0;
579 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
587 trace_parser_put(&parser
);
593 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
595 struct ftrace_event_file
*file
= v
;
596 struct ftrace_event_call
*call
;
597 struct trace_array
*tr
= m
->private;
601 list_for_each_entry_continue(file
, &tr
->events
, list
) {
602 call
= file
->event_call
;
604 * The ftrace subsystem is for showing formats only.
605 * They can not be enabled or disabled via the event files.
607 if (call
->class && call
->class->reg
)
614 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
616 struct ftrace_event_file
*file
;
617 struct trace_array
*tr
= m
->private;
620 mutex_lock(&event_mutex
);
622 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
623 for (l
= 0; l
<= *pos
; ) {
624 file
= t_next(m
, file
, &l
);
632 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
634 struct ftrace_event_file
*file
= v
;
635 struct trace_array
*tr
= m
->private;
639 list_for_each_entry_continue(file
, &tr
->events
, list
) {
640 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
647 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
649 struct ftrace_event_file
*file
;
650 struct trace_array
*tr
= m
->private;
653 mutex_lock(&event_mutex
);
655 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
656 for (l
= 0; l
<= *pos
; ) {
657 file
= s_next(m
, file
, &l
);
664 static int t_show(struct seq_file
*m
, void *v
)
666 struct ftrace_event_file
*file
= v
;
667 struct ftrace_event_call
*call
= file
->event_call
;
669 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
670 seq_printf(m
, "%s:", call
->class->system
);
671 seq_printf(m
, "%s\n", call
->name
);
676 static void t_stop(struct seq_file
*m
, void *p
)
678 mutex_unlock(&event_mutex
);
682 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
685 struct ftrace_event_file
*file
;
689 mutex_lock(&event_mutex
);
690 file
= event_file_data(filp
);
693 mutex_unlock(&event_mutex
);
698 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
699 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
702 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
703 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
708 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
712 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
715 struct ftrace_event_file
*file
;
719 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
723 ret
= tracing_update_buffers();
731 mutex_lock(&event_mutex
);
732 file
= event_file_data(filp
);
734 ret
= ftrace_event_enable_disable(file
, val
);
735 mutex_unlock(&event_mutex
);
744 return ret
? ret
: cnt
;
748 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
751 const char set_to_char
[4] = { '?', '0', '1', 'X' };
752 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
753 struct event_subsystem
*system
= dir
->subsystem
;
754 struct ftrace_event_call
*call
;
755 struct ftrace_event_file
*file
;
756 struct trace_array
*tr
= dir
->tr
;
761 mutex_lock(&event_mutex
);
762 list_for_each_entry(file
, &tr
->events
, list
) {
763 call
= file
->event_call
;
764 if (!call
->name
|| !call
->class || !call
->class->reg
)
767 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
771 * We need to find out if all the events are set
772 * or if all events or cleared, or if we have
775 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
778 * If we have a mixture, no need to look further.
783 mutex_unlock(&event_mutex
);
785 buf
[0] = set_to_char
[set
];
788 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
794 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
797 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
798 struct event_subsystem
*system
= dir
->subsystem
;
799 const char *name
= NULL
;
803 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
807 ret
= tracing_update_buffers();
811 if (val
!= 0 && val
!= 1)
815 * Opening of "enable" adds a ref count to system,
816 * so the name is safe to use.
821 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
835 FORMAT_FIELD_SEPERATOR
= 2,
839 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
841 struct ftrace_event_call
*call
= event_file_data(m
->private);
842 struct list_head
*common_head
= &ftrace_common_fields
;
843 struct list_head
*head
= trace_get_fields(call
);
844 struct list_head
*node
= v
;
848 switch ((unsigned long)v
) {
853 case FORMAT_FIELD_SEPERATOR
:
857 case FORMAT_PRINTFMT
:
863 if (node
== common_head
)
864 return (void *)FORMAT_FIELD_SEPERATOR
;
865 else if (node
== head
)
866 return (void *)FORMAT_PRINTFMT
;
871 static int f_show(struct seq_file
*m
, void *v
)
873 struct ftrace_event_call
*call
= event_file_data(m
->private);
874 struct ftrace_event_field
*field
;
875 const char *array_descriptor
;
877 switch ((unsigned long)v
) {
879 seq_printf(m
, "name: %s\n", call
->name
);
880 seq_printf(m
, "ID: %d\n", call
->event
.type
);
881 seq_printf(m
, "format:\n");
884 case FORMAT_FIELD_SEPERATOR
:
888 case FORMAT_PRINTFMT
:
889 seq_printf(m
, "\nprint fmt: %s\n",
894 field
= list_entry(v
, struct ftrace_event_field
, link
);
896 * Smartly shows the array type(except dynamic array).
899 * If TYPE := TYPE[LEN], it is shown:
900 * field:TYPE VAR[LEN]
902 array_descriptor
= strchr(field
->type
, '[');
904 if (!strncmp(field
->type
, "__data_loc", 10))
905 array_descriptor
= NULL
;
907 if (!array_descriptor
)
908 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
909 field
->type
, field
->name
, field
->offset
,
910 field
->size
, !!field
->is_signed
);
912 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
913 (int)(array_descriptor
- field
->type
),
914 field
->type
, field
->name
,
915 array_descriptor
, field
->offset
,
916 field
->size
, !!field
->is_signed
);
921 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
923 void *p
= (void *)FORMAT_HEADER
;
926 /* ->stop() is called even if ->start() fails */
927 mutex_lock(&event_mutex
);
928 if (!event_file_data(m
->private))
929 return ERR_PTR(-ENODEV
);
931 while (l
< *pos
&& p
)
932 p
= f_next(m
, p
, &l
);
937 static void f_stop(struct seq_file
*m
, void *p
)
939 mutex_unlock(&event_mutex
);
942 static const struct seq_operations trace_format_seq_ops
= {
949 static int trace_format_open(struct inode
*inode
, struct file
*file
)
954 ret
= seq_open(file
, &trace_format_seq_ops
);
958 m
= file
->private_data
;
965 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
967 int id
= (long)event_file_data(filp
);
977 len
= sprintf(buf
, "%d\n", id
);
979 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
983 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
986 struct ftrace_event_call
*call
;
993 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1000 mutex_lock(&event_mutex
);
1001 call
= event_file_data(filp
);
1003 print_event_filter(call
, s
);
1004 mutex_unlock(&event_mutex
);
1007 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1015 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1018 struct ftrace_event_call
*call
;
1022 if (cnt
>= PAGE_SIZE
)
1025 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1029 if (copy_from_user(buf
, ubuf
, cnt
)) {
1030 free_page((unsigned long) buf
);
1035 mutex_lock(&event_mutex
);
1036 call
= event_file_data(filp
);
1038 err
= apply_event_filter(call
, buf
);
1039 mutex_unlock(&event_mutex
);
1041 free_page((unsigned long) buf
);
1050 static LIST_HEAD(event_subsystems
);
1052 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1054 struct event_subsystem
*system
= NULL
;
1055 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1056 struct trace_array
*tr
;
1059 /* Make sure the system still exists */
1060 mutex_lock(&trace_types_lock
);
1061 mutex_lock(&event_mutex
);
1062 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1063 list_for_each_entry(dir
, &tr
->systems
, list
) {
1064 if (dir
== inode
->i_private
) {
1065 /* Don't open systems with no events */
1066 if (dir
->nr_events
) {
1067 __get_system_dir(dir
);
1068 system
= dir
->subsystem
;
1075 mutex_unlock(&event_mutex
);
1076 mutex_unlock(&trace_types_lock
);
1081 /* Some versions of gcc think dir can be uninitialized here */
1084 /* Still need to increment the ref count of the system */
1085 if (trace_array_get(tr
) < 0) {
1090 ret
= tracing_open_generic(inode
, filp
);
1092 trace_array_put(tr
);
1099 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1101 struct ftrace_subsystem_dir
*dir
;
1102 struct trace_array
*tr
= inode
->i_private
;
1105 if (trace_array_get(tr
) < 0)
1108 /* Make a temporary dir that has no system but points to tr */
1109 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1111 trace_array_put(tr
);
1117 ret
= tracing_open_generic(inode
, filp
);
1119 trace_array_put(tr
);
1123 filp
->private_data
= dir
;
1128 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1130 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1132 trace_array_put(dir
->tr
);
1135 * If dir->subsystem is NULL, then this is a temporary
1136 * descriptor that was made for a trace_array to enable
1148 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1151 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1152 struct event_subsystem
*system
= dir
->subsystem
;
1153 struct trace_seq
*s
;
1159 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1165 print_subsystem_event_filter(system
, s
);
1166 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1174 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1177 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1181 if (cnt
>= PAGE_SIZE
)
1184 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1188 if (copy_from_user(buf
, ubuf
, cnt
)) {
1189 free_page((unsigned long) buf
);
1194 err
= apply_subsystem_event_filter(dir
, buf
);
1195 free_page((unsigned long) buf
);
1205 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1207 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1208 struct trace_seq
*s
;
1214 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1221 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1228 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1229 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1230 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1232 static const struct seq_operations show_event_seq_ops
= {
1239 static const struct seq_operations show_set_event_seq_ops
= {
1246 static const struct file_operations ftrace_avail_fops
= {
1247 .open
= ftrace_event_avail_open
,
1249 .llseek
= seq_lseek
,
1250 .release
= seq_release
,
1253 static const struct file_operations ftrace_set_event_fops
= {
1254 .open
= ftrace_event_set_open
,
1256 .write
= ftrace_event_write
,
1257 .llseek
= seq_lseek
,
1258 .release
= ftrace_event_release
,
1261 static const struct file_operations ftrace_enable_fops
= {
1262 .open
= tracing_open_generic
,
1263 .read
= event_enable_read
,
1264 .write
= event_enable_write
,
1265 .llseek
= default_llseek
,
1268 static const struct file_operations ftrace_event_format_fops
= {
1269 .open
= trace_format_open
,
1271 .llseek
= seq_lseek
,
1272 .release
= seq_release
,
1275 static const struct file_operations ftrace_event_id_fops
= {
1276 .read
= event_id_read
,
1277 .llseek
= default_llseek
,
1280 static const struct file_operations ftrace_event_filter_fops
= {
1281 .open
= tracing_open_generic
,
1282 .read
= event_filter_read
,
1283 .write
= event_filter_write
,
1284 .llseek
= default_llseek
,
1287 static const struct file_operations ftrace_subsystem_filter_fops
= {
1288 .open
= subsystem_open
,
1289 .read
= subsystem_filter_read
,
1290 .write
= subsystem_filter_write
,
1291 .llseek
= default_llseek
,
1292 .release
= subsystem_release
,
1295 static const struct file_operations ftrace_system_enable_fops
= {
1296 .open
= subsystem_open
,
1297 .read
= system_enable_read
,
1298 .write
= system_enable_write
,
1299 .llseek
= default_llseek
,
1300 .release
= subsystem_release
,
1303 static const struct file_operations ftrace_tr_enable_fops
= {
1304 .open
= system_tr_open
,
1305 .read
= system_enable_read
,
1306 .write
= system_enable_write
,
1307 .llseek
= default_llseek
,
1308 .release
= subsystem_release
,
1311 static const struct file_operations ftrace_show_header_fops
= {
1312 .open
= tracing_open_generic
,
1313 .read
= show_header
,
1314 .llseek
= default_llseek
,
1318 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1319 const struct seq_operations
*seq_ops
)
1324 ret
= seq_open(file
, seq_ops
);
1327 m
= file
->private_data
;
1328 /* copy tr over to seq ops */
1329 m
->private = inode
->i_private
;
1334 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1336 struct trace_array
*tr
= inode
->i_private
;
1338 trace_array_put(tr
);
1340 return seq_release(inode
, file
);
1344 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1346 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1348 return ftrace_event_open(inode
, file
, seq_ops
);
1352 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1354 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1355 struct trace_array
*tr
= inode
->i_private
;
1358 if (trace_array_get(tr
) < 0)
1361 if ((file
->f_mode
& FMODE_WRITE
) &&
1362 (file
->f_flags
& O_TRUNC
))
1363 ftrace_clear_events(tr
);
1365 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1367 trace_array_put(tr
);
1371 static struct event_subsystem
*
1372 create_new_subsystem(const char *name
)
1374 struct event_subsystem
*system
;
1376 /* need to create new entry */
1377 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1381 system
->ref_count
= 1;
1383 /* Only allocate if dynamic (kprobes and modules) */
1384 if (!core_kernel_data((unsigned long)name
)) {
1385 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1386 system
->name
= kstrdup(name
, GFP_KERNEL
);
1390 system
->name
= name
;
1392 system
->filter
= NULL
;
1394 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1395 if (!system
->filter
)
1398 list_add(&system
->list
, &event_subsystems
);
1403 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1404 kfree(system
->name
);
1409 static struct dentry
*
1410 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1411 struct ftrace_event_file
*file
, struct dentry
*parent
)
1413 struct ftrace_subsystem_dir
*dir
;
1414 struct event_subsystem
*system
;
1415 struct dentry
*entry
;
1417 /* First see if we did not already create this dir */
1418 list_for_each_entry(dir
, &tr
->systems
, list
) {
1419 system
= dir
->subsystem
;
1420 if (strcmp(system
->name
, name
) == 0) {
1427 /* Now see if the system itself exists. */
1428 list_for_each_entry(system
, &event_subsystems
, list
) {
1429 if (strcmp(system
->name
, name
) == 0)
1432 /* Reset system variable when not found */
1433 if (&system
->list
== &event_subsystems
)
1436 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1441 system
= create_new_subsystem(name
);
1445 __get_system(system
);
1447 dir
->entry
= debugfs_create_dir(name
, parent
);
1449 pr_warning("Failed to create system directory %s\n", name
);
1450 __put_system(system
);
1457 dir
->subsystem
= system
;
1460 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1461 &ftrace_subsystem_filter_fops
);
1463 kfree(system
->filter
);
1464 system
->filter
= NULL
;
1465 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1468 trace_create_file("enable", 0644, dir
->entry
, dir
,
1469 &ftrace_system_enable_fops
);
1471 list_add(&dir
->list
, &tr
->systems
);
1478 /* Only print this message if failed on memory allocation */
1479 if (!dir
|| !system
)
1480 pr_warning("No memory to create event subsystem %s\n",
1486 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1488 struct ftrace_event_call
*call
= file
->event_call
;
1489 struct trace_array
*tr
= file
->tr
;
1490 struct list_head
*head
;
1491 struct dentry
*d_events
;
1495 * If the trace point header did not define TRACE_SYSTEM
1496 * then the system would be called "TRACE_SYSTEM".
1498 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1499 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1505 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1507 pr_warning("Could not create debugfs '%s' directory\n",
1512 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1513 trace_create_file("enable", 0644, file
->dir
, file
,
1514 &ftrace_enable_fops
);
1516 #ifdef CONFIG_PERF_EVENTS
1517 if (call
->event
.type
&& call
->class->reg
)
1518 trace_create_file("id", 0444, file
->dir
,
1519 (void *)(long)call
->event
.type
,
1520 &ftrace_event_id_fops
);
1524 * Other events may have the same class. Only update
1525 * the fields if they are not already defined.
1527 head
= trace_get_fields(call
);
1528 if (list_empty(head
)) {
1529 ret
= call
->class->define_fields(call
);
1531 pr_warning("Could not initialize trace point"
1532 " events/%s\n", call
->name
);
1536 trace_create_file("filter", 0644, file
->dir
, call
,
1537 &ftrace_event_filter_fops
);
1539 trace_create_file("format", 0444, file
->dir
, call
,
1540 &ftrace_event_format_fops
);
1545 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1547 struct ftrace_event_file
*file
;
1548 struct trace_array
*tr
;
1550 do_for_each_event_file_safe(tr
, file
) {
1551 if (file
->event_call
!= call
)
1554 remove_event_file_dir(file
);
1556 * The do_for_each_event_file_safe() is
1557 * a double loop. After finding the call for this
1558 * trace_array, we use break to jump to the next
1562 } while_for_each_event_file();
1565 static void event_remove(struct ftrace_event_call
*call
)
1567 struct trace_array
*tr
;
1568 struct ftrace_event_file
*file
;
1570 do_for_each_event_file(tr
, file
) {
1571 if (file
->event_call
!= call
)
1573 ftrace_event_enable_disable(file
, 0);
1575 * The do_for_each_event_file() is
1576 * a double loop. After finding the call for this
1577 * trace_array, we use break to jump to the next
1581 } while_for_each_event_file();
1583 if (call
->event
.funcs
)
1584 __unregister_ftrace_event(&call
->event
);
1585 remove_event_from_tracers(call
);
1586 list_del(&call
->list
);
1589 static int event_init(struct ftrace_event_call
*call
)
1593 if (WARN_ON(!call
->name
))
1596 if (call
->class->raw_init
) {
1597 ret
= call
->class->raw_init(call
);
1598 if (ret
< 0 && ret
!= -ENOSYS
)
1599 pr_warn("Could not initialize trace events/%s\n",
1607 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1611 ret
= event_init(call
);
1615 list_add(&call
->list
, &ftrace_events
);
1621 static struct ftrace_event_file
*
1622 trace_create_new_event(struct ftrace_event_call
*call
,
1623 struct trace_array
*tr
)
1625 struct ftrace_event_file
*file
;
1627 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1631 file
->event_call
= call
;
1633 atomic_set(&file
->sm_ref
, 0);
1634 list_add(&file
->list
, &tr
->events
);
1639 /* Add an event to a trace directory */
1641 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1643 struct ftrace_event_file
*file
;
1645 file
= trace_create_new_event(call
, tr
);
1649 return event_create_dir(tr
->event_dir
, file
);
1653 * Just create a decriptor for early init. A descriptor is required
1654 * for enabling events at boot. We want to enable events before
1655 * the filesystem is initialized.
1658 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1659 struct trace_array
*tr
)
1661 struct ftrace_event_file
*file
;
1663 file
= trace_create_new_event(call
, tr
);
1670 struct ftrace_module_file_ops
;
1671 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1673 /* Add an additional event_call dynamically */
1674 int trace_add_event_call(struct ftrace_event_call
*call
)
1677 mutex_lock(&trace_types_lock
);
1678 mutex_lock(&event_mutex
);
1680 ret
= __register_event(call
, NULL
);
1682 __add_event_to_tracers(call
);
1684 mutex_unlock(&event_mutex
);
1685 mutex_unlock(&trace_types_lock
);
1690 * Must be called under locking of trace_types_lock, event_mutex and
1693 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1696 trace_destroy_fields(call
);
1697 destroy_preds(call
);
1700 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1702 struct trace_array
*tr
;
1703 struct ftrace_event_file
*file
;
1705 #ifdef CONFIG_PERF_EVENTS
1706 if (call
->perf_refcount
)
1709 do_for_each_event_file(tr
, file
) {
1710 if (file
->event_call
!= call
)
1713 * We can't rely on ftrace_event_enable_disable(enable => 0)
1714 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1715 * TRACE_REG_UNREGISTER.
1717 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1720 * The do_for_each_event_file_safe() is
1721 * a double loop. After finding the call for this
1722 * trace_array, we use break to jump to the next
1726 } while_for_each_event_file();
1728 __trace_remove_event_call(call
);
1733 /* Remove an event_call */
1734 int trace_remove_event_call(struct ftrace_event_call
*call
)
1738 mutex_lock(&trace_types_lock
);
1739 mutex_lock(&event_mutex
);
1740 down_write(&trace_event_sem
);
1741 ret
= probe_remove_event_call(call
);
1742 up_write(&trace_event_sem
);
1743 mutex_unlock(&event_mutex
);
1744 mutex_unlock(&trace_types_lock
);
1749 #define for_each_event(event, start, end) \
1750 for (event = start; \
1751 (unsigned long)event < (unsigned long)end; \
1754 #ifdef CONFIG_MODULES
1756 static void trace_module_add_events(struct module
*mod
)
1758 struct ftrace_event_call
**call
, **start
, **end
;
1760 if (!mod
->num_trace_events
)
1763 /* Don't add infrastructure for mods without tracepoints */
1764 if (trace_module_has_bad_taint(mod
)) {
1765 pr_err("%s: module has bad taint, not creating trace events\n",
1770 start
= mod
->trace_events
;
1771 end
= mod
->trace_events
+ mod
->num_trace_events
;
1773 for_each_event(call
, start
, end
) {
1774 __register_event(*call
, mod
);
1775 __add_event_to_tracers(*call
);
1779 static void trace_module_remove_events(struct module
*mod
)
1781 struct ftrace_event_call
*call
, *p
;
1782 bool clear_trace
= false;
1784 down_write(&trace_event_sem
);
1785 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1786 if (call
->mod
== mod
) {
1787 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1789 __trace_remove_event_call(call
);
1792 up_write(&trace_event_sem
);
1795 * It is safest to reset the ring buffer if the module being unloaded
1796 * registered any events that were used. The only worry is if
1797 * a new module gets loaded, and takes on the same id as the events
1798 * of this module. When printing out the buffer, traced events left
1799 * over from this module may be passed to the new module events and
1800 * unexpected results may occur.
1803 tracing_reset_all_online_cpus();
1806 static int trace_module_notify(struct notifier_block
*self
,
1807 unsigned long val
, void *data
)
1809 struct module
*mod
= data
;
1811 mutex_lock(&trace_types_lock
);
1812 mutex_lock(&event_mutex
);
1814 case MODULE_STATE_COMING
:
1815 trace_module_add_events(mod
);
1817 case MODULE_STATE_GOING
:
1818 trace_module_remove_events(mod
);
1821 mutex_unlock(&event_mutex
);
1822 mutex_unlock(&trace_types_lock
);
1827 static struct notifier_block trace_module_nb
= {
1828 .notifier_call
= trace_module_notify
,
1831 #endif /* CONFIG_MODULES */
1833 /* Create a new event directory structure for a trace directory. */
1835 __trace_add_event_dirs(struct trace_array
*tr
)
1837 struct ftrace_event_call
*call
;
1840 list_for_each_entry(call
, &ftrace_events
, list
) {
1841 ret
= __trace_add_new_event(call
, tr
);
1843 pr_warning("Could not create directory for event %s\n",
1848 #ifdef CONFIG_DYNAMIC_FTRACE
1851 #define ENABLE_EVENT_STR "enable_event"
1852 #define DISABLE_EVENT_STR "disable_event"
1854 struct event_probe_data
{
1855 struct ftrace_event_file
*file
;
1856 unsigned long count
;
1861 static struct ftrace_event_file
*
1862 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1864 struct ftrace_event_file
*file
;
1865 struct ftrace_event_call
*call
;
1867 list_for_each_entry(file
, &tr
->events
, list
) {
1869 call
= file
->event_call
;
1871 if (!call
->name
|| !call
->class || !call
->class->reg
)
1874 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1877 if (strcmp(event
, call
->name
) == 0 &&
1878 strcmp(system
, call
->class->system
) == 0)
1885 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1887 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1888 struct event_probe_data
*data
= *pdata
;
1894 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1896 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1900 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1902 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1903 struct event_probe_data
*data
= *pdata
;
1911 /* Skip if the event is in a state we want to switch to */
1912 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1915 if (data
->count
!= -1)
1918 event_enable_probe(ip
, parent_ip
, _data
);
1922 event_enable_print(struct seq_file
*m
, unsigned long ip
,
1923 struct ftrace_probe_ops
*ops
, void *_data
)
1925 struct event_probe_data
*data
= _data
;
1927 seq_printf(m
, "%ps:", (void *)ip
);
1929 seq_printf(m
, "%s:%s:%s",
1930 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1931 data
->file
->event_call
->class->system
,
1932 data
->file
->event_call
->name
);
1934 if (data
->count
== -1)
1935 seq_printf(m
, ":unlimited\n");
1937 seq_printf(m
, ":count=%ld\n", data
->count
);
1943 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1946 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1947 struct event_probe_data
*data
= *pdata
;
1954 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1957 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1958 struct event_probe_data
*data
= *pdata
;
1960 if (WARN_ON_ONCE(data
->ref
<= 0))
1965 /* Remove the SOFT_MODE flag */
1966 __ftrace_event_enable_disable(data
->file
, 0, 1);
1967 module_put(data
->file
->event_call
->mod
);
1973 static struct ftrace_probe_ops event_enable_probe_ops
= {
1974 .func
= event_enable_probe
,
1975 .print
= event_enable_print
,
1976 .init
= event_enable_init
,
1977 .free
= event_enable_free
,
1980 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
1981 .func
= event_enable_count_probe
,
1982 .print
= event_enable_print
,
1983 .init
= event_enable_init
,
1984 .free
= event_enable_free
,
1987 static struct ftrace_probe_ops event_disable_probe_ops
= {
1988 .func
= event_enable_probe
,
1989 .print
= event_enable_print
,
1990 .init
= event_enable_init
,
1991 .free
= event_enable_free
,
1994 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
1995 .func
= event_enable_count_probe
,
1996 .print
= event_enable_print
,
1997 .init
= event_enable_init
,
1998 .free
= event_enable_free
,
2002 event_enable_func(struct ftrace_hash
*hash
,
2003 char *glob
, char *cmd
, char *param
, int enabled
)
2005 struct trace_array
*tr
= top_trace_array();
2006 struct ftrace_event_file
*file
;
2007 struct ftrace_probe_ops
*ops
;
2008 struct event_probe_data
*data
;
2015 /* hash funcs only work with set_ftrace_filter */
2016 if (!enabled
|| !param
)
2019 system
= strsep(¶m
, ":");
2023 event
= strsep(¶m
, ":");
2025 mutex_lock(&event_mutex
);
2028 file
= find_event_file(tr
, system
, event
);
2032 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2035 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2037 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2039 if (glob
[0] == '!') {
2040 unregister_ftrace_function_probe_func(glob
+1, ops
);
2046 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2050 data
->enable
= enable
;
2057 number
= strsep(¶m
, ":");
2060 if (!strlen(number
))
2064 * We use the callback data field (which is a pointer)
2067 ret
= kstrtoul(number
, 0, &data
->count
);
2072 /* Don't let event modules unload while probe registered */
2073 ret
= try_module_get(file
->event_call
->mod
);
2079 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2082 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2084 * The above returns on success the # of functions enabled,
2085 * but if it didn't find any functions it returns zero.
2086 * Consider no functions a failure too.
2093 /* Just return zero, not the number of enabled functions */
2096 mutex_unlock(&event_mutex
);
2100 __ftrace_event_enable_disable(file
, 0, 1);
2102 module_put(file
->event_call
->mod
);
2108 static struct ftrace_func_command event_enable_cmd
= {
2109 .name
= ENABLE_EVENT_STR
,
2110 .func
= event_enable_func
,
2113 static struct ftrace_func_command event_disable_cmd
= {
2114 .name
= DISABLE_EVENT_STR
,
2115 .func
= event_enable_func
,
2118 static __init
int register_event_cmds(void)
2122 ret
= register_ftrace_command(&event_enable_cmd
);
2123 if (WARN_ON(ret
< 0))
2125 ret
= register_ftrace_command(&event_disable_cmd
);
2126 if (WARN_ON(ret
< 0))
2127 unregister_ftrace_command(&event_enable_cmd
);
2131 static inline int register_event_cmds(void) { return 0; }
2132 #endif /* CONFIG_DYNAMIC_FTRACE */
2135 * The top level array has already had its ftrace_event_file
2136 * descriptors created in order to allow for early events to
2137 * be recorded. This function is called after the debugfs has been
2138 * initialized, and we now have to create the files associated
2142 __trace_early_add_event_dirs(struct trace_array
*tr
)
2144 struct ftrace_event_file
*file
;
2148 list_for_each_entry(file
, &tr
->events
, list
) {
2149 ret
= event_create_dir(tr
->event_dir
, file
);
2151 pr_warning("Could not create directory for event %s\n",
2152 file
->event_call
->name
);
2157 * For early boot up, the top trace array requires to have
2158 * a list of events that can be enabled. This must be done before
2159 * the filesystem is set up in order to allow events to be traced
2163 __trace_early_add_events(struct trace_array
*tr
)
2165 struct ftrace_event_call
*call
;
2168 list_for_each_entry(call
, &ftrace_events
, list
) {
2169 /* Early boot up should not have any modules loaded */
2170 if (WARN_ON_ONCE(call
->mod
))
2173 ret
= __trace_early_add_new_event(call
, tr
);
2175 pr_warning("Could not create early event %s\n",
2180 /* Remove the event directory structure for a trace directory. */
2182 __trace_remove_event_dirs(struct trace_array
*tr
)
2184 struct ftrace_event_file
*file
, *next
;
2186 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2187 remove_event_file_dir(file
);
2190 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2192 struct trace_array
*tr
;
2194 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2195 __trace_add_new_event(call
, tr
);
2198 extern struct ftrace_event_call
*__start_ftrace_events
[];
2199 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2201 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2203 static __init
int setup_trace_event(char *str
)
2205 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2206 ring_buffer_expanded
= true;
2207 tracing_selftest_disabled
= true;
2211 __setup("trace_event=", setup_trace_event
);
2213 /* Expects to have event_mutex held when called */
2215 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2217 struct dentry
*d_events
;
2218 struct dentry
*entry
;
2220 entry
= debugfs_create_file("set_event", 0644, parent
,
2221 tr
, &ftrace_set_event_fops
);
2223 pr_warning("Could not create debugfs 'set_event' entry\n");
2227 d_events
= debugfs_create_dir("events", parent
);
2229 pr_warning("Could not create debugfs 'events' directory\n");
2233 /* ring buffer internal formats */
2234 trace_create_file("header_page", 0444, d_events
,
2235 ring_buffer_print_page_header
,
2236 &ftrace_show_header_fops
);
2238 trace_create_file("header_event", 0444, d_events
,
2239 ring_buffer_print_entry_header
,
2240 &ftrace_show_header_fops
);
2242 trace_create_file("enable", 0644, d_events
,
2243 tr
, &ftrace_tr_enable_fops
);
2245 tr
->event_dir
= d_events
;
2251 * event_trace_add_tracer - add a instance of a trace_array to events
2252 * @parent: The parent dentry to place the files/directories for events in
2253 * @tr: The trace array associated with these events
2255 * When a new instance is created, it needs to set up its events
2256 * directory, as well as other files associated with events. It also
2257 * creates the event hierachry in the @parent/events directory.
2259 * Returns 0 on success.
2261 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2265 mutex_lock(&event_mutex
);
2267 ret
= create_event_toplevel_files(parent
, tr
);
2271 down_write(&trace_event_sem
);
2272 __trace_add_event_dirs(tr
);
2273 up_write(&trace_event_sem
);
2276 mutex_unlock(&event_mutex
);
2282 * The top trace array already had its file descriptors created.
2283 * Now the files themselves need to be created.
2286 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2290 mutex_lock(&event_mutex
);
2292 ret
= create_event_toplevel_files(parent
, tr
);
2296 down_write(&trace_event_sem
);
2297 __trace_early_add_event_dirs(tr
);
2298 up_write(&trace_event_sem
);
2301 mutex_unlock(&event_mutex
);
2306 int event_trace_del_tracer(struct trace_array
*tr
)
2308 mutex_lock(&event_mutex
);
2310 /* Disable any running events */
2311 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2313 down_write(&trace_event_sem
);
2314 __trace_remove_event_dirs(tr
);
2315 debugfs_remove_recursive(tr
->event_dir
);
2316 up_write(&trace_event_sem
);
2318 tr
->event_dir
= NULL
;
2320 mutex_unlock(&event_mutex
);
2325 static __init
int event_trace_memsetup(void)
2327 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2328 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2332 static __init
int event_trace_enable(void)
2334 struct trace_array
*tr
= top_trace_array();
2335 struct ftrace_event_call
**iter
, *call
;
2336 char *buf
= bootup_event_buf
;
2340 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2343 ret
= event_init(call
);
2345 list_add(&call
->list
, &ftrace_events
);
2349 * We need the top trace array to have a working set of trace
2350 * points at early init, before the debug files and directories
2351 * are created. Create the file entries now, and attach them
2352 * to the actual file dentries later.
2354 __trace_early_add_events(tr
);
2357 token
= strsep(&buf
, ",");
2364 ret
= ftrace_set_clr_event(tr
, token
, 1);
2366 pr_warn("Failed to enable trace event: %s\n", token
);
2369 trace_printk_start_comm();
2371 register_event_cmds();
2376 static __init
int event_trace_init(void)
2378 struct trace_array
*tr
;
2379 struct dentry
*d_tracer
;
2380 struct dentry
*entry
;
2383 tr
= top_trace_array();
2385 d_tracer
= tracing_init_dentry();
2389 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2390 tr
, &ftrace_avail_fops
);
2392 pr_warning("Could not create debugfs "
2393 "'available_events' entry\n");
2395 if (trace_define_common_fields())
2396 pr_warning("tracing: Failed to allocate common fields");
2398 ret
= early_event_add_tracer(d_tracer
, tr
);
2402 #ifdef CONFIG_MODULES
2403 ret
= register_module_notifier(&trace_module_nb
);
2405 pr_warning("Failed to register trace events module notifier\n");
2409 early_initcall(event_trace_memsetup
);
2410 core_initcall(event_trace_enable
);
2411 fs_initcall(event_trace_init
);
2413 #ifdef CONFIG_FTRACE_STARTUP_TEST
2415 static DEFINE_SPINLOCK(test_spinlock
);
2416 static DEFINE_SPINLOCK(test_spinlock_irq
);
2417 static DEFINE_MUTEX(test_mutex
);
2419 static __init
void test_work(struct work_struct
*dummy
)
2421 spin_lock(&test_spinlock
);
2422 spin_lock_irq(&test_spinlock_irq
);
2424 spin_unlock_irq(&test_spinlock_irq
);
2425 spin_unlock(&test_spinlock
);
2427 mutex_lock(&test_mutex
);
2429 mutex_unlock(&test_mutex
);
2432 static __init
int event_test_thread(void *unused
)
2436 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2438 pr_info("failed to kmalloc\n");
2440 schedule_on_each_cpu(test_work
);
2444 set_current_state(TASK_INTERRUPTIBLE
);
2445 while (!kthread_should_stop())
2452 * Do various things that may trigger events.
2454 static __init
void event_test_stuff(void)
2456 struct task_struct
*test_thread
;
2458 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2460 kthread_stop(test_thread
);
2464 * For every trace event defined, we will test each trace point separately,
2465 * and then by groups, and finally all trace points.
2467 static __init
void event_trace_self_tests(void)
2469 struct ftrace_subsystem_dir
*dir
;
2470 struct ftrace_event_file
*file
;
2471 struct ftrace_event_call
*call
;
2472 struct event_subsystem
*system
;
2473 struct trace_array
*tr
;
2476 tr
= top_trace_array();
2478 pr_info("Running tests on trace events:\n");
2480 list_for_each_entry(file
, &tr
->events
, list
) {
2482 call
= file
->event_call
;
2484 /* Only test those that have a probe */
2485 if (!call
->class || !call
->class->probe
)
2489 * Testing syscall events here is pretty useless, but
2490 * we still do it if configured. But this is time consuming.
2491 * What we really need is a user thread to perform the
2492 * syscalls as we test.
2494 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2495 if (call
->class->system
&&
2496 strcmp(call
->class->system
, "syscalls") == 0)
2500 pr_info("Testing event %s: ", call
->name
);
2503 * If an event is already enabled, someone is using
2504 * it and the self test should not be on.
2506 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2507 pr_warning("Enabled event during self test!\n");
2512 ftrace_event_enable_disable(file
, 1);
2514 ftrace_event_enable_disable(file
, 0);
2519 /* Now test at the sub system level */
2521 pr_info("Running tests on trace event systems:\n");
2523 list_for_each_entry(dir
, &tr
->systems
, list
) {
2525 system
= dir
->subsystem
;
2527 /* the ftrace system is special, skip it */
2528 if (strcmp(system
->name
, "ftrace") == 0)
2531 pr_info("Testing event system %s: ", system
->name
);
2533 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2534 if (WARN_ON_ONCE(ret
)) {
2535 pr_warning("error enabling system %s\n",
2542 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2543 if (WARN_ON_ONCE(ret
)) {
2544 pr_warning("error disabling system %s\n",
2552 /* Test with all events enabled */
2554 pr_info("Running tests on all trace events:\n");
2555 pr_info("Testing all events: ");
2557 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2558 if (WARN_ON_ONCE(ret
)) {
2559 pr_warning("error enabling all events\n");
2566 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2567 if (WARN_ON_ONCE(ret
)) {
2568 pr_warning("error disabling all events\n");
2575 #ifdef CONFIG_FUNCTION_TRACER
2577 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2580 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2581 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2583 struct ring_buffer_event
*event
;
2584 struct ring_buffer
*buffer
;
2585 struct ftrace_entry
*entry
;
2586 unsigned long flags
;
2591 pc
= preempt_count();
2592 preempt_disable_notrace();
2593 cpu
= raw_smp_processor_id();
2594 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2599 local_save_flags(flags
);
2601 event
= trace_current_buffer_lock_reserve(&buffer
,
2602 TRACE_FN
, sizeof(*entry
),
2606 entry
= ring_buffer_event_data(event
);
2608 entry
->parent_ip
= parent_ip
;
2610 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2613 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2614 preempt_enable_notrace();
2617 static struct ftrace_ops trace_ops __initdata
=
2619 .func
= function_test_events_call
,
2620 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2623 static __init
void event_trace_self_test_with_function(void)
2626 ret
= register_ftrace_function(&trace_ops
);
2627 if (WARN_ON(ret
< 0)) {
2628 pr_info("Failed to enable function tracer for event tests\n");
2631 pr_info("Running tests again, along with the function tracer\n");
2632 event_trace_self_tests();
2633 unregister_ftrace_function(&trace_ops
);
2636 static __init
void event_trace_self_test_with_function(void)
2641 static __init
int event_trace_self_tests_init(void)
2643 if (!tracing_selftest_disabled
) {
2644 event_trace_self_tests();
2645 event_trace_self_test_with_function();
2651 late_initcall(event_trace_self_tests_init
);