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 int trace_event_enable_disable(struct ftrace_event_file
*file
,
346 int enable
, int soft_disable
)
348 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
351 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
354 return __ftrace_event_enable_disable(file
, enable
, 0);
357 static void ftrace_clear_events(struct trace_array
*tr
)
359 struct ftrace_event_file
*file
;
361 mutex_lock(&event_mutex
);
362 list_for_each_entry(file
, &tr
->events
, list
) {
363 ftrace_event_enable_disable(file
, 0);
365 mutex_unlock(&event_mutex
);
368 static void __put_system(struct event_subsystem
*system
)
370 struct event_filter
*filter
= system
->filter
;
372 WARN_ON_ONCE(system_refcount(system
) == 0);
373 if (system_refcount_dec(system
))
376 list_del(&system
->list
);
379 kfree(filter
->filter_string
);
382 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
387 static void __get_system(struct event_subsystem
*system
)
389 WARN_ON_ONCE(system_refcount(system
) == 0);
390 system_refcount_inc(system
);
393 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
395 WARN_ON_ONCE(dir
->ref_count
== 0);
397 __get_system(dir
->subsystem
);
400 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
402 WARN_ON_ONCE(dir
->ref_count
== 0);
403 /* If the subsystem is about to be freed, the dir must be too */
404 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
406 __put_system(dir
->subsystem
);
407 if (!--dir
->ref_count
)
411 static void put_system(struct ftrace_subsystem_dir
*dir
)
413 mutex_lock(&event_mutex
);
414 __put_system_dir(dir
);
415 mutex_unlock(&event_mutex
);
418 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
423 if (!--dir
->nr_events
) {
424 debugfs_remove_recursive(dir
->entry
);
425 list_del(&dir
->list
);
426 __put_system_dir(dir
);
430 static void remove_event_file_dir(struct ftrace_event_file
*file
)
432 struct dentry
*dir
= file
->dir
;
433 struct dentry
*child
;
436 spin_lock(&dir
->d_lock
); /* probably unneeded */
437 list_for_each_entry(child
, &dir
->d_subdirs
, d_u
.d_child
) {
438 if (child
->d_inode
) /* probably unneeded */
439 child
->d_inode
->i_private
= NULL
;
441 spin_unlock(&dir
->d_lock
);
443 debugfs_remove_recursive(dir
);
446 list_del(&file
->list
);
447 remove_subsystem(file
->system
);
448 kmem_cache_free(file_cachep
, file
);
452 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
455 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
456 const char *sub
, const char *event
, int set
)
458 struct ftrace_event_file
*file
;
459 struct ftrace_event_call
*call
;
462 list_for_each_entry(file
, &tr
->events
, list
) {
464 call
= file
->event_call
;
466 if (!call
->name
|| !call
->class || !call
->class->reg
)
469 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
473 strcmp(match
, call
->name
) != 0 &&
474 strcmp(match
, call
->class->system
) != 0)
477 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
480 if (event
&& strcmp(event
, call
->name
) != 0)
483 ftrace_event_enable_disable(file
, set
);
491 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
492 const char *sub
, const char *event
, int set
)
496 mutex_lock(&event_mutex
);
497 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
498 mutex_unlock(&event_mutex
);
503 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
505 char *event
= NULL
, *sub
= NULL
, *match
;
508 * The buf format can be <subsystem>:<event-name>
509 * *:<event-name> means any event by that name.
510 * :<event-name> is the same.
512 * <subsystem>:* means all events in that subsystem
513 * <subsystem>: means the same.
515 * <name> (no ':') means all events in a subsystem with
516 * the name <name> or any event that matches <name>
519 match
= strsep(&buf
, ":");
525 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
527 if (!strlen(event
) || strcmp(event
, "*") == 0)
531 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
535 * trace_set_clr_event - enable or disable an event
536 * @system: system name to match (NULL for any system)
537 * @event: event name to match (NULL for all events, within system)
538 * @set: 1 to enable, 0 to disable
540 * This is a way for other parts of the kernel to enable or disable
543 * Returns 0 on success, -EINVAL if the parameters do not match any
546 int trace_set_clr_event(const char *system
, const char *event
, int set
)
548 struct trace_array
*tr
= top_trace_array();
550 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
552 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
554 /* 128 should be much more than enough */
555 #define EVENT_BUF_SIZE 127
558 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
559 size_t cnt
, loff_t
*ppos
)
561 struct trace_parser parser
;
562 struct seq_file
*m
= file
->private_data
;
563 struct trace_array
*tr
= m
->private;
569 ret
= tracing_update_buffers();
573 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
576 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
578 if (read
>= 0 && trace_parser_loaded((&parser
))) {
581 if (*parser
.buffer
== '!')
584 parser
.buffer
[parser
.idx
] = 0;
586 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
594 trace_parser_put(&parser
);
600 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
602 struct ftrace_event_file
*file
= v
;
603 struct ftrace_event_call
*call
;
604 struct trace_array
*tr
= m
->private;
608 list_for_each_entry_continue(file
, &tr
->events
, list
) {
609 call
= file
->event_call
;
611 * The ftrace subsystem is for showing formats only.
612 * They can not be enabled or disabled via the event files.
614 if (call
->class && call
->class->reg
)
621 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
623 struct ftrace_event_file
*file
;
624 struct trace_array
*tr
= m
->private;
627 mutex_lock(&event_mutex
);
629 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
630 for (l
= 0; l
<= *pos
; ) {
631 file
= t_next(m
, file
, &l
);
639 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
641 struct ftrace_event_file
*file
= v
;
642 struct trace_array
*tr
= m
->private;
646 list_for_each_entry_continue(file
, &tr
->events
, list
) {
647 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
654 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
656 struct ftrace_event_file
*file
;
657 struct trace_array
*tr
= m
->private;
660 mutex_lock(&event_mutex
);
662 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
663 for (l
= 0; l
<= *pos
; ) {
664 file
= s_next(m
, file
, &l
);
671 static int t_show(struct seq_file
*m
, void *v
)
673 struct ftrace_event_file
*file
= v
;
674 struct ftrace_event_call
*call
= file
->event_call
;
676 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
677 seq_printf(m
, "%s:", call
->class->system
);
678 seq_printf(m
, "%s\n", call
->name
);
683 static void t_stop(struct seq_file
*m
, void *p
)
685 mutex_unlock(&event_mutex
);
689 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
692 struct ftrace_event_file
*file
;
696 mutex_lock(&event_mutex
);
697 file
= event_file_data(filp
);
700 mutex_unlock(&event_mutex
);
705 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
706 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
709 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
710 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
715 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
719 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
722 struct ftrace_event_file
*file
;
726 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
730 ret
= tracing_update_buffers();
738 mutex_lock(&event_mutex
);
739 file
= event_file_data(filp
);
741 ret
= ftrace_event_enable_disable(file
, val
);
742 mutex_unlock(&event_mutex
);
751 return ret
? ret
: cnt
;
755 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
758 const char set_to_char
[4] = { '?', '0', '1', 'X' };
759 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
760 struct event_subsystem
*system
= dir
->subsystem
;
761 struct ftrace_event_call
*call
;
762 struct ftrace_event_file
*file
;
763 struct trace_array
*tr
= dir
->tr
;
768 mutex_lock(&event_mutex
);
769 list_for_each_entry(file
, &tr
->events
, list
) {
770 call
= file
->event_call
;
771 if (!call
->name
|| !call
->class || !call
->class->reg
)
774 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
778 * We need to find out if all the events are set
779 * or if all events or cleared, or if we have
782 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
785 * If we have a mixture, no need to look further.
790 mutex_unlock(&event_mutex
);
792 buf
[0] = set_to_char
[set
];
795 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
801 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
804 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
805 struct event_subsystem
*system
= dir
->subsystem
;
806 const char *name
= NULL
;
810 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
814 ret
= tracing_update_buffers();
818 if (val
!= 0 && val
!= 1)
822 * Opening of "enable" adds a ref count to system,
823 * so the name is safe to use.
828 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
842 FORMAT_FIELD_SEPERATOR
= 2,
846 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
848 struct ftrace_event_call
*call
= event_file_data(m
->private);
849 struct list_head
*common_head
= &ftrace_common_fields
;
850 struct list_head
*head
= trace_get_fields(call
);
851 struct list_head
*node
= v
;
855 switch ((unsigned long)v
) {
860 case FORMAT_FIELD_SEPERATOR
:
864 case FORMAT_PRINTFMT
:
870 if (node
== common_head
)
871 return (void *)FORMAT_FIELD_SEPERATOR
;
872 else if (node
== head
)
873 return (void *)FORMAT_PRINTFMT
;
878 static int f_show(struct seq_file
*m
, void *v
)
880 struct ftrace_event_call
*call
= event_file_data(m
->private);
881 struct ftrace_event_field
*field
;
882 const char *array_descriptor
;
884 switch ((unsigned long)v
) {
886 seq_printf(m
, "name: %s\n", call
->name
);
887 seq_printf(m
, "ID: %d\n", call
->event
.type
);
888 seq_printf(m
, "format:\n");
891 case FORMAT_FIELD_SEPERATOR
:
895 case FORMAT_PRINTFMT
:
896 seq_printf(m
, "\nprint fmt: %s\n",
901 field
= list_entry(v
, struct ftrace_event_field
, link
);
903 * Smartly shows the array type(except dynamic array).
906 * If TYPE := TYPE[LEN], it is shown:
907 * field:TYPE VAR[LEN]
909 array_descriptor
= strchr(field
->type
, '[');
911 if (!strncmp(field
->type
, "__data_loc", 10))
912 array_descriptor
= NULL
;
914 if (!array_descriptor
)
915 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
916 field
->type
, field
->name
, field
->offset
,
917 field
->size
, !!field
->is_signed
);
919 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
920 (int)(array_descriptor
- field
->type
),
921 field
->type
, field
->name
,
922 array_descriptor
, field
->offset
,
923 field
->size
, !!field
->is_signed
);
928 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
930 void *p
= (void *)FORMAT_HEADER
;
933 /* ->stop() is called even if ->start() fails */
934 mutex_lock(&event_mutex
);
935 if (!event_file_data(m
->private))
936 return ERR_PTR(-ENODEV
);
938 while (l
< *pos
&& p
)
939 p
= f_next(m
, p
, &l
);
944 static void f_stop(struct seq_file
*m
, void *p
)
946 mutex_unlock(&event_mutex
);
949 static const struct seq_operations trace_format_seq_ops
= {
956 static int trace_format_open(struct inode
*inode
, struct file
*file
)
961 ret
= seq_open(file
, &trace_format_seq_ops
);
965 m
= file
->private_data
;
972 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
974 int id
= (long)event_file_data(filp
);
984 len
= sprintf(buf
, "%d\n", id
);
986 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
990 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
993 struct ftrace_event_file
*file
;
1000 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1007 mutex_lock(&event_mutex
);
1008 file
= event_file_data(filp
);
1010 print_event_filter(file
, s
);
1011 mutex_unlock(&event_mutex
);
1014 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1022 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1025 struct ftrace_event_file
*file
;
1029 if (cnt
>= PAGE_SIZE
)
1032 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1036 if (copy_from_user(buf
, ubuf
, cnt
)) {
1037 free_page((unsigned long) buf
);
1042 mutex_lock(&event_mutex
);
1043 file
= event_file_data(filp
);
1045 err
= apply_event_filter(file
, buf
);
1046 mutex_unlock(&event_mutex
);
1048 free_page((unsigned long) buf
);
1057 static LIST_HEAD(event_subsystems
);
1059 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1061 struct event_subsystem
*system
= NULL
;
1062 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1063 struct trace_array
*tr
;
1066 if (tracing_is_disabled())
1069 /* Make sure the system still exists */
1070 mutex_lock(&trace_types_lock
);
1071 mutex_lock(&event_mutex
);
1072 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1073 list_for_each_entry(dir
, &tr
->systems
, list
) {
1074 if (dir
== inode
->i_private
) {
1075 /* Don't open systems with no events */
1076 if (dir
->nr_events
) {
1077 __get_system_dir(dir
);
1078 system
= dir
->subsystem
;
1085 mutex_unlock(&event_mutex
);
1086 mutex_unlock(&trace_types_lock
);
1091 /* Some versions of gcc think dir can be uninitialized here */
1094 /* Still need to increment the ref count of the system */
1095 if (trace_array_get(tr
) < 0) {
1100 ret
= tracing_open_generic(inode
, filp
);
1102 trace_array_put(tr
);
1109 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1111 struct ftrace_subsystem_dir
*dir
;
1112 struct trace_array
*tr
= inode
->i_private
;
1115 if (tracing_is_disabled())
1118 if (trace_array_get(tr
) < 0)
1121 /* Make a temporary dir that has no system but points to tr */
1122 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1124 trace_array_put(tr
);
1130 ret
= tracing_open_generic(inode
, filp
);
1132 trace_array_put(tr
);
1137 filp
->private_data
= dir
;
1142 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1144 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1146 trace_array_put(dir
->tr
);
1149 * If dir->subsystem is NULL, then this is a temporary
1150 * descriptor that was made for a trace_array to enable
1162 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1165 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1166 struct event_subsystem
*system
= dir
->subsystem
;
1167 struct trace_seq
*s
;
1173 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1179 print_subsystem_event_filter(system
, s
);
1180 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1188 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1191 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1195 if (cnt
>= PAGE_SIZE
)
1198 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1202 if (copy_from_user(buf
, ubuf
, cnt
)) {
1203 free_page((unsigned long) buf
);
1208 err
= apply_subsystem_event_filter(dir
, buf
);
1209 free_page((unsigned long) buf
);
1219 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1221 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1222 struct trace_seq
*s
;
1228 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1235 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1242 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1243 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1244 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1246 static const struct seq_operations show_event_seq_ops
= {
1253 static const struct seq_operations show_set_event_seq_ops
= {
1260 static const struct file_operations ftrace_avail_fops
= {
1261 .open
= ftrace_event_avail_open
,
1263 .llseek
= seq_lseek
,
1264 .release
= seq_release
,
1267 static const struct file_operations ftrace_set_event_fops
= {
1268 .open
= ftrace_event_set_open
,
1270 .write
= ftrace_event_write
,
1271 .llseek
= seq_lseek
,
1272 .release
= ftrace_event_release
,
1275 static const struct file_operations ftrace_enable_fops
= {
1276 .open
= tracing_open_generic
,
1277 .read
= event_enable_read
,
1278 .write
= event_enable_write
,
1279 .llseek
= default_llseek
,
1282 static const struct file_operations ftrace_event_format_fops
= {
1283 .open
= trace_format_open
,
1285 .llseek
= seq_lseek
,
1286 .release
= seq_release
,
1289 static const struct file_operations ftrace_event_id_fops
= {
1290 .read
= event_id_read
,
1291 .llseek
= default_llseek
,
1294 static const struct file_operations ftrace_event_filter_fops
= {
1295 .open
= tracing_open_generic
,
1296 .read
= event_filter_read
,
1297 .write
= event_filter_write
,
1298 .llseek
= default_llseek
,
1301 static const struct file_operations ftrace_subsystem_filter_fops
= {
1302 .open
= subsystem_open
,
1303 .read
= subsystem_filter_read
,
1304 .write
= subsystem_filter_write
,
1305 .llseek
= default_llseek
,
1306 .release
= subsystem_release
,
1309 static const struct file_operations ftrace_system_enable_fops
= {
1310 .open
= subsystem_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_tr_enable_fops
= {
1318 .open
= system_tr_open
,
1319 .read
= system_enable_read
,
1320 .write
= system_enable_write
,
1321 .llseek
= default_llseek
,
1322 .release
= subsystem_release
,
1325 static const struct file_operations ftrace_show_header_fops
= {
1326 .open
= tracing_open_generic
,
1327 .read
= show_header
,
1328 .llseek
= default_llseek
,
1332 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1333 const struct seq_operations
*seq_ops
)
1338 ret
= seq_open(file
, seq_ops
);
1341 m
= file
->private_data
;
1342 /* copy tr over to seq ops */
1343 m
->private = inode
->i_private
;
1348 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1350 struct trace_array
*tr
= inode
->i_private
;
1352 trace_array_put(tr
);
1354 return seq_release(inode
, file
);
1358 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1360 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1362 return ftrace_event_open(inode
, file
, seq_ops
);
1366 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1368 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1369 struct trace_array
*tr
= inode
->i_private
;
1372 if (trace_array_get(tr
) < 0)
1375 if ((file
->f_mode
& FMODE_WRITE
) &&
1376 (file
->f_flags
& O_TRUNC
))
1377 ftrace_clear_events(tr
);
1379 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1381 trace_array_put(tr
);
1385 static struct event_subsystem
*
1386 create_new_subsystem(const char *name
)
1388 struct event_subsystem
*system
;
1390 /* need to create new entry */
1391 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1395 system
->ref_count
= 1;
1397 /* Only allocate if dynamic (kprobes and modules) */
1398 if (!core_kernel_data((unsigned long)name
)) {
1399 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1400 system
->name
= kstrdup(name
, GFP_KERNEL
);
1404 system
->name
= name
;
1406 system
->filter
= NULL
;
1408 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1409 if (!system
->filter
)
1412 list_add(&system
->list
, &event_subsystems
);
1417 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1418 kfree(system
->name
);
1423 static struct dentry
*
1424 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1425 struct ftrace_event_file
*file
, struct dentry
*parent
)
1427 struct ftrace_subsystem_dir
*dir
;
1428 struct event_subsystem
*system
;
1429 struct dentry
*entry
;
1431 /* First see if we did not already create this dir */
1432 list_for_each_entry(dir
, &tr
->systems
, list
) {
1433 system
= dir
->subsystem
;
1434 if (strcmp(system
->name
, name
) == 0) {
1441 /* Now see if the system itself exists. */
1442 list_for_each_entry(system
, &event_subsystems
, list
) {
1443 if (strcmp(system
->name
, name
) == 0)
1446 /* Reset system variable when not found */
1447 if (&system
->list
== &event_subsystems
)
1450 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1455 system
= create_new_subsystem(name
);
1459 __get_system(system
);
1461 dir
->entry
= debugfs_create_dir(name
, parent
);
1463 pr_warning("Failed to create system directory %s\n", name
);
1464 __put_system(system
);
1471 dir
->subsystem
= system
;
1474 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1475 &ftrace_subsystem_filter_fops
);
1477 kfree(system
->filter
);
1478 system
->filter
= NULL
;
1479 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1482 trace_create_file("enable", 0644, dir
->entry
, dir
,
1483 &ftrace_system_enable_fops
);
1485 list_add(&dir
->list
, &tr
->systems
);
1492 /* Only print this message if failed on memory allocation */
1493 if (!dir
|| !system
)
1494 pr_warning("No memory to create event subsystem %s\n",
1500 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1502 struct ftrace_event_call
*call
= file
->event_call
;
1503 struct trace_array
*tr
= file
->tr
;
1504 struct list_head
*head
;
1505 struct dentry
*d_events
;
1509 * If the trace point header did not define TRACE_SYSTEM
1510 * then the system would be called "TRACE_SYSTEM".
1512 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1513 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1519 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1521 pr_warning("Could not create debugfs '%s' directory\n",
1526 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1527 trace_create_file("enable", 0644, file
->dir
, file
,
1528 &ftrace_enable_fops
);
1530 #ifdef CONFIG_PERF_EVENTS
1531 if (call
->event
.type
&& call
->class->reg
)
1532 trace_create_file("id", 0444, file
->dir
,
1533 (void *)(long)call
->event
.type
,
1534 &ftrace_event_id_fops
);
1538 * Other events may have the same class. Only update
1539 * the fields if they are not already defined.
1541 head
= trace_get_fields(call
);
1542 if (list_empty(head
)) {
1543 ret
= call
->class->define_fields(call
);
1545 pr_warning("Could not initialize trace point"
1546 " events/%s\n", call
->name
);
1550 trace_create_file("filter", 0644, file
->dir
, file
,
1551 &ftrace_event_filter_fops
);
1553 trace_create_file("trigger", 0644, file
->dir
, file
,
1554 &event_trigger_fops
);
1556 trace_create_file("format", 0444, file
->dir
, call
,
1557 &ftrace_event_format_fops
);
1562 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1564 struct ftrace_event_file
*file
;
1565 struct trace_array
*tr
;
1567 do_for_each_event_file_safe(tr
, file
) {
1568 if (file
->event_call
!= call
)
1571 remove_event_file_dir(file
);
1573 * The do_for_each_event_file_safe() is
1574 * a double loop. After finding the call for this
1575 * trace_array, we use break to jump to the next
1579 } while_for_each_event_file();
1582 static void event_remove(struct ftrace_event_call
*call
)
1584 struct trace_array
*tr
;
1585 struct ftrace_event_file
*file
;
1587 do_for_each_event_file(tr
, file
) {
1588 if (file
->event_call
!= call
)
1590 ftrace_event_enable_disable(file
, 0);
1591 destroy_preds(file
);
1593 * The do_for_each_event_file() is
1594 * a double loop. After finding the call for this
1595 * trace_array, we use break to jump to the next
1599 } while_for_each_event_file();
1601 if (call
->event
.funcs
)
1602 __unregister_ftrace_event(&call
->event
);
1603 remove_event_from_tracers(call
);
1604 list_del(&call
->list
);
1607 static int event_init(struct ftrace_event_call
*call
)
1611 if (WARN_ON(!call
->name
))
1614 if (call
->class->raw_init
) {
1615 ret
= call
->class->raw_init(call
);
1616 if (ret
< 0 && ret
!= -ENOSYS
)
1617 pr_warn("Could not initialize trace events/%s\n",
1625 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1629 ret
= event_init(call
);
1633 list_add(&call
->list
, &ftrace_events
);
1639 static struct ftrace_event_file
*
1640 trace_create_new_event(struct ftrace_event_call
*call
,
1641 struct trace_array
*tr
)
1643 struct ftrace_event_file
*file
;
1645 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1649 file
->event_call
= call
;
1651 atomic_set(&file
->sm_ref
, 0);
1652 atomic_set(&file
->tm_ref
, 0);
1653 INIT_LIST_HEAD(&file
->triggers
);
1654 list_add(&file
->list
, &tr
->events
);
1659 /* Add an event to a trace directory */
1661 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1663 struct ftrace_event_file
*file
;
1665 file
= trace_create_new_event(call
, tr
);
1669 return event_create_dir(tr
->event_dir
, file
);
1673 * Just create a decriptor for early init. A descriptor is required
1674 * for enabling events at boot. We want to enable events before
1675 * the filesystem is initialized.
1678 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1679 struct trace_array
*tr
)
1681 struct ftrace_event_file
*file
;
1683 file
= trace_create_new_event(call
, tr
);
1690 struct ftrace_module_file_ops
;
1691 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1693 /* Add an additional event_call dynamically */
1694 int trace_add_event_call(struct ftrace_event_call
*call
)
1697 mutex_lock(&trace_types_lock
);
1698 mutex_lock(&event_mutex
);
1700 ret
= __register_event(call
, NULL
);
1702 __add_event_to_tracers(call
);
1704 mutex_unlock(&event_mutex
);
1705 mutex_unlock(&trace_types_lock
);
1710 * Must be called under locking of trace_types_lock, event_mutex and
1713 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1716 trace_destroy_fields(call
);
1717 destroy_call_preds(call
);
1720 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1722 struct trace_array
*tr
;
1723 struct ftrace_event_file
*file
;
1725 #ifdef CONFIG_PERF_EVENTS
1726 if (call
->perf_refcount
)
1729 do_for_each_event_file(tr
, file
) {
1730 if (file
->event_call
!= call
)
1733 * We can't rely on ftrace_event_enable_disable(enable => 0)
1734 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1735 * TRACE_REG_UNREGISTER.
1737 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1740 * The do_for_each_event_file_safe() is
1741 * a double loop. After finding the call for this
1742 * trace_array, we use break to jump to the next
1746 } while_for_each_event_file();
1748 __trace_remove_event_call(call
);
1753 /* Remove an event_call */
1754 int trace_remove_event_call(struct ftrace_event_call
*call
)
1758 mutex_lock(&trace_types_lock
);
1759 mutex_lock(&event_mutex
);
1760 down_write(&trace_event_sem
);
1761 ret
= probe_remove_event_call(call
);
1762 up_write(&trace_event_sem
);
1763 mutex_unlock(&event_mutex
);
1764 mutex_unlock(&trace_types_lock
);
1769 #define for_each_event(event, start, end) \
1770 for (event = start; \
1771 (unsigned long)event < (unsigned long)end; \
1774 #ifdef CONFIG_MODULES
1776 static void trace_module_add_events(struct module
*mod
)
1778 struct ftrace_event_call
**call
, **start
, **end
;
1780 start
= mod
->trace_events
;
1781 end
= mod
->trace_events
+ mod
->num_trace_events
;
1783 for_each_event(call
, start
, end
) {
1784 __register_event(*call
, mod
);
1785 __add_event_to_tracers(*call
);
1789 static void trace_module_remove_events(struct module
*mod
)
1791 struct ftrace_event_call
*call
, *p
;
1792 bool clear_trace
= false;
1794 down_write(&trace_event_sem
);
1795 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1796 if (call
->mod
== mod
) {
1797 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1799 __trace_remove_event_call(call
);
1802 up_write(&trace_event_sem
);
1805 * It is safest to reset the ring buffer if the module being unloaded
1806 * registered any events that were used. The only worry is if
1807 * a new module gets loaded, and takes on the same id as the events
1808 * of this module. When printing out the buffer, traced events left
1809 * over from this module may be passed to the new module events and
1810 * unexpected results may occur.
1813 tracing_reset_all_online_cpus();
1816 static int trace_module_notify(struct notifier_block
*self
,
1817 unsigned long val
, void *data
)
1819 struct module
*mod
= data
;
1821 mutex_lock(&trace_types_lock
);
1822 mutex_lock(&event_mutex
);
1824 case MODULE_STATE_COMING
:
1825 trace_module_add_events(mod
);
1827 case MODULE_STATE_GOING
:
1828 trace_module_remove_events(mod
);
1831 mutex_unlock(&event_mutex
);
1832 mutex_unlock(&trace_types_lock
);
1837 static struct notifier_block trace_module_nb
= {
1838 .notifier_call
= trace_module_notify
,
1841 #endif /* CONFIG_MODULES */
1843 /* Create a new event directory structure for a trace directory. */
1845 __trace_add_event_dirs(struct trace_array
*tr
)
1847 struct ftrace_event_call
*call
;
1850 list_for_each_entry(call
, &ftrace_events
, list
) {
1851 ret
= __trace_add_new_event(call
, tr
);
1853 pr_warning("Could not create directory for event %s\n",
1858 struct ftrace_event_file
*
1859 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1861 struct ftrace_event_file
*file
;
1862 struct ftrace_event_call
*call
;
1864 list_for_each_entry(file
, &tr
->events
, list
) {
1866 call
= file
->event_call
;
1868 if (!call
->name
|| !call
->class || !call
->class->reg
)
1871 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1874 if (strcmp(event
, call
->name
) == 0 &&
1875 strcmp(system
, call
->class->system
) == 0)
1881 #ifdef CONFIG_DYNAMIC_FTRACE
1884 #define ENABLE_EVENT_STR "enable_event"
1885 #define DISABLE_EVENT_STR "disable_event"
1887 struct event_probe_data
{
1888 struct ftrace_event_file
*file
;
1889 unsigned long count
;
1895 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1897 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1898 struct event_probe_data
*data
= *pdata
;
1904 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1906 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1910 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1912 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1913 struct event_probe_data
*data
= *pdata
;
1921 /* Skip if the event is in a state we want to switch to */
1922 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1925 if (data
->count
!= -1)
1928 event_enable_probe(ip
, parent_ip
, _data
);
1932 event_enable_print(struct seq_file
*m
, unsigned long ip
,
1933 struct ftrace_probe_ops
*ops
, void *_data
)
1935 struct event_probe_data
*data
= _data
;
1937 seq_printf(m
, "%ps:", (void *)ip
);
1939 seq_printf(m
, "%s:%s:%s",
1940 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1941 data
->file
->event_call
->class->system
,
1942 data
->file
->event_call
->name
);
1944 if (data
->count
== -1)
1945 seq_printf(m
, ":unlimited\n");
1947 seq_printf(m
, ":count=%ld\n", data
->count
);
1953 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1956 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1957 struct event_probe_data
*data
= *pdata
;
1964 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
1967 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1968 struct event_probe_data
*data
= *pdata
;
1970 if (WARN_ON_ONCE(data
->ref
<= 0))
1975 /* Remove the SOFT_MODE flag */
1976 __ftrace_event_enable_disable(data
->file
, 0, 1);
1977 module_put(data
->file
->event_call
->mod
);
1983 static struct ftrace_probe_ops event_enable_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_enable_count_probe_ops
= {
1991 .func
= event_enable_count_probe
,
1992 .print
= event_enable_print
,
1993 .init
= event_enable_init
,
1994 .free
= event_enable_free
,
1997 static struct ftrace_probe_ops event_disable_probe_ops
= {
1998 .func
= event_enable_probe
,
1999 .print
= event_enable_print
,
2000 .init
= event_enable_init
,
2001 .free
= event_enable_free
,
2004 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2005 .func
= event_enable_count_probe
,
2006 .print
= event_enable_print
,
2007 .init
= event_enable_init
,
2008 .free
= event_enable_free
,
2012 event_enable_func(struct ftrace_hash
*hash
,
2013 char *glob
, char *cmd
, char *param
, int enabled
)
2015 struct trace_array
*tr
= top_trace_array();
2016 struct ftrace_event_file
*file
;
2017 struct ftrace_probe_ops
*ops
;
2018 struct event_probe_data
*data
;
2025 /* hash funcs only work with set_ftrace_filter */
2026 if (!enabled
|| !param
)
2029 system
= strsep(¶m
, ":");
2033 event
= strsep(¶m
, ":");
2035 mutex_lock(&event_mutex
);
2038 file
= find_event_file(tr
, system
, event
);
2042 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2045 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2047 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2049 if (glob
[0] == '!') {
2050 unregister_ftrace_function_probe_func(glob
+1, ops
);
2056 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2060 data
->enable
= enable
;
2067 number
= strsep(¶m
, ":");
2070 if (!strlen(number
))
2074 * We use the callback data field (which is a pointer)
2077 ret
= kstrtoul(number
, 0, &data
->count
);
2082 /* Don't let event modules unload while probe registered */
2083 ret
= try_module_get(file
->event_call
->mod
);
2089 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2092 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2094 * The above returns on success the # of functions enabled,
2095 * but if it didn't find any functions it returns zero.
2096 * Consider no functions a failure too.
2103 /* Just return zero, not the number of enabled functions */
2106 mutex_unlock(&event_mutex
);
2110 __ftrace_event_enable_disable(file
, 0, 1);
2112 module_put(file
->event_call
->mod
);
2118 static struct ftrace_func_command event_enable_cmd
= {
2119 .name
= ENABLE_EVENT_STR
,
2120 .func
= event_enable_func
,
2123 static struct ftrace_func_command event_disable_cmd
= {
2124 .name
= DISABLE_EVENT_STR
,
2125 .func
= event_enable_func
,
2128 static __init
int register_event_cmds(void)
2132 ret
= register_ftrace_command(&event_enable_cmd
);
2133 if (WARN_ON(ret
< 0))
2135 ret
= register_ftrace_command(&event_disable_cmd
);
2136 if (WARN_ON(ret
< 0))
2137 unregister_ftrace_command(&event_enable_cmd
);
2141 static inline int register_event_cmds(void) { return 0; }
2142 #endif /* CONFIG_DYNAMIC_FTRACE */
2145 * The top level array has already had its ftrace_event_file
2146 * descriptors created in order to allow for early events to
2147 * be recorded. This function is called after the debugfs has been
2148 * initialized, and we now have to create the files associated
2152 __trace_early_add_event_dirs(struct trace_array
*tr
)
2154 struct ftrace_event_file
*file
;
2158 list_for_each_entry(file
, &tr
->events
, list
) {
2159 ret
= event_create_dir(tr
->event_dir
, file
);
2161 pr_warning("Could not create directory for event %s\n",
2162 file
->event_call
->name
);
2167 * For early boot up, the top trace array requires to have
2168 * a list of events that can be enabled. This must be done before
2169 * the filesystem is set up in order to allow events to be traced
2173 __trace_early_add_events(struct trace_array
*tr
)
2175 struct ftrace_event_call
*call
;
2178 list_for_each_entry(call
, &ftrace_events
, list
) {
2179 /* Early boot up should not have any modules loaded */
2180 if (WARN_ON_ONCE(call
->mod
))
2183 ret
= __trace_early_add_new_event(call
, tr
);
2185 pr_warning("Could not create early event %s\n",
2190 /* Remove the event directory structure for a trace directory. */
2192 __trace_remove_event_dirs(struct trace_array
*tr
)
2194 struct ftrace_event_file
*file
, *next
;
2196 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2197 remove_event_file_dir(file
);
2200 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2202 struct trace_array
*tr
;
2204 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2205 __trace_add_new_event(call
, tr
);
2208 extern struct ftrace_event_call
*__start_ftrace_events
[];
2209 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2211 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2213 static __init
int setup_trace_event(char *str
)
2215 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2216 ring_buffer_expanded
= true;
2217 tracing_selftest_disabled
= true;
2221 __setup("trace_event=", setup_trace_event
);
2223 /* Expects to have event_mutex held when called */
2225 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2227 struct dentry
*d_events
;
2228 struct dentry
*entry
;
2230 entry
= debugfs_create_file("set_event", 0644, parent
,
2231 tr
, &ftrace_set_event_fops
);
2233 pr_warning("Could not create debugfs 'set_event' entry\n");
2237 d_events
= debugfs_create_dir("events", parent
);
2239 pr_warning("Could not create debugfs 'events' directory\n");
2243 /* ring buffer internal formats */
2244 trace_create_file("header_page", 0444, d_events
,
2245 ring_buffer_print_page_header
,
2246 &ftrace_show_header_fops
);
2248 trace_create_file("header_event", 0444, d_events
,
2249 ring_buffer_print_entry_header
,
2250 &ftrace_show_header_fops
);
2252 trace_create_file("enable", 0644, d_events
,
2253 tr
, &ftrace_tr_enable_fops
);
2255 tr
->event_dir
= d_events
;
2261 * event_trace_add_tracer - add a instance of a trace_array to events
2262 * @parent: The parent dentry to place the files/directories for events in
2263 * @tr: The trace array associated with these events
2265 * When a new instance is created, it needs to set up its events
2266 * directory, as well as other files associated with events. It also
2267 * creates the event hierachry in the @parent/events directory.
2269 * Returns 0 on success.
2271 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2275 mutex_lock(&event_mutex
);
2277 ret
= create_event_toplevel_files(parent
, tr
);
2281 down_write(&trace_event_sem
);
2282 __trace_add_event_dirs(tr
);
2283 up_write(&trace_event_sem
);
2286 mutex_unlock(&event_mutex
);
2292 * The top trace array already had its file descriptors created.
2293 * Now the files themselves need to be created.
2296 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2300 mutex_lock(&event_mutex
);
2302 ret
= create_event_toplevel_files(parent
, tr
);
2306 down_write(&trace_event_sem
);
2307 __trace_early_add_event_dirs(tr
);
2308 up_write(&trace_event_sem
);
2311 mutex_unlock(&event_mutex
);
2316 int event_trace_del_tracer(struct trace_array
*tr
)
2318 mutex_lock(&event_mutex
);
2320 /* Disable any event triggers and associated soft-disabled events */
2321 clear_event_triggers(tr
);
2323 /* Disable any running events */
2324 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2326 /* Access to events are within rcu_read_lock_sched() */
2327 synchronize_sched();
2329 down_write(&trace_event_sem
);
2330 __trace_remove_event_dirs(tr
);
2331 debugfs_remove_recursive(tr
->event_dir
);
2332 up_write(&trace_event_sem
);
2334 tr
->event_dir
= NULL
;
2336 mutex_unlock(&event_mutex
);
2341 static __init
int event_trace_memsetup(void)
2343 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2344 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2348 static __init
int event_trace_enable(void)
2350 struct trace_array
*tr
= top_trace_array();
2351 struct ftrace_event_call
**iter
, *call
;
2352 char *buf
= bootup_event_buf
;
2356 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2359 ret
= event_init(call
);
2361 list_add(&call
->list
, &ftrace_events
);
2365 * We need the top trace array to have a working set of trace
2366 * points at early init, before the debug files and directories
2367 * are created. Create the file entries now, and attach them
2368 * to the actual file dentries later.
2370 __trace_early_add_events(tr
);
2373 token
= strsep(&buf
, ",");
2380 ret
= ftrace_set_clr_event(tr
, token
, 1);
2382 pr_warn("Failed to enable trace event: %s\n", token
);
2385 trace_printk_start_comm();
2387 register_event_cmds();
2389 register_trigger_cmds();
2394 static __init
int event_trace_init(void)
2396 struct trace_array
*tr
;
2397 struct dentry
*d_tracer
;
2398 struct dentry
*entry
;
2401 tr
= top_trace_array();
2403 d_tracer
= tracing_init_dentry();
2407 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2408 tr
, &ftrace_avail_fops
);
2410 pr_warning("Could not create debugfs "
2411 "'available_events' entry\n");
2413 if (trace_define_common_fields())
2414 pr_warning("tracing: Failed to allocate common fields");
2416 ret
= early_event_add_tracer(d_tracer
, tr
);
2420 #ifdef CONFIG_MODULES
2421 ret
= register_module_notifier(&trace_module_nb
);
2423 pr_warning("Failed to register trace events module notifier\n");
2427 early_initcall(event_trace_memsetup
);
2428 core_initcall(event_trace_enable
);
2429 fs_initcall(event_trace_init
);
2431 #ifdef CONFIG_FTRACE_STARTUP_TEST
2433 static DEFINE_SPINLOCK(test_spinlock
);
2434 static DEFINE_SPINLOCK(test_spinlock_irq
);
2435 static DEFINE_MUTEX(test_mutex
);
2437 static __init
void test_work(struct work_struct
*dummy
)
2439 spin_lock(&test_spinlock
);
2440 spin_lock_irq(&test_spinlock_irq
);
2442 spin_unlock_irq(&test_spinlock_irq
);
2443 spin_unlock(&test_spinlock
);
2445 mutex_lock(&test_mutex
);
2447 mutex_unlock(&test_mutex
);
2450 static __init
int event_test_thread(void *unused
)
2454 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2456 pr_info("failed to kmalloc\n");
2458 schedule_on_each_cpu(test_work
);
2462 set_current_state(TASK_INTERRUPTIBLE
);
2463 while (!kthread_should_stop())
2470 * Do various things that may trigger events.
2472 static __init
void event_test_stuff(void)
2474 struct task_struct
*test_thread
;
2476 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2478 kthread_stop(test_thread
);
2482 * For every trace event defined, we will test each trace point separately,
2483 * and then by groups, and finally all trace points.
2485 static __init
void event_trace_self_tests(void)
2487 struct ftrace_subsystem_dir
*dir
;
2488 struct ftrace_event_file
*file
;
2489 struct ftrace_event_call
*call
;
2490 struct event_subsystem
*system
;
2491 struct trace_array
*tr
;
2494 tr
= top_trace_array();
2496 pr_info("Running tests on trace events:\n");
2498 list_for_each_entry(file
, &tr
->events
, list
) {
2500 call
= file
->event_call
;
2502 /* Only test those that have a probe */
2503 if (!call
->class || !call
->class->probe
)
2507 * Testing syscall events here is pretty useless, but
2508 * we still do it if configured. But this is time consuming.
2509 * What we really need is a user thread to perform the
2510 * syscalls as we test.
2512 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2513 if (call
->class->system
&&
2514 strcmp(call
->class->system
, "syscalls") == 0)
2518 pr_info("Testing event %s: ", call
->name
);
2521 * If an event is already enabled, someone is using
2522 * it and the self test should not be on.
2524 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2525 pr_warning("Enabled event during self test!\n");
2530 ftrace_event_enable_disable(file
, 1);
2532 ftrace_event_enable_disable(file
, 0);
2537 /* Now test at the sub system level */
2539 pr_info("Running tests on trace event systems:\n");
2541 list_for_each_entry(dir
, &tr
->systems
, list
) {
2543 system
= dir
->subsystem
;
2545 /* the ftrace system is special, skip it */
2546 if (strcmp(system
->name
, "ftrace") == 0)
2549 pr_info("Testing event system %s: ", system
->name
);
2551 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2552 if (WARN_ON_ONCE(ret
)) {
2553 pr_warning("error enabling system %s\n",
2560 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2561 if (WARN_ON_ONCE(ret
)) {
2562 pr_warning("error disabling system %s\n",
2570 /* Test with all events enabled */
2572 pr_info("Running tests on all trace events:\n");
2573 pr_info("Testing all events: ");
2575 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2576 if (WARN_ON_ONCE(ret
)) {
2577 pr_warning("error enabling all events\n");
2584 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2585 if (WARN_ON_ONCE(ret
)) {
2586 pr_warning("error disabling all events\n");
2593 #ifdef CONFIG_FUNCTION_TRACER
2595 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2598 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2599 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2601 struct ring_buffer_event
*event
;
2602 struct ring_buffer
*buffer
;
2603 struct ftrace_entry
*entry
;
2604 unsigned long flags
;
2609 pc
= preempt_count();
2610 preempt_disable_notrace();
2611 cpu
= raw_smp_processor_id();
2612 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2617 local_save_flags(flags
);
2619 event
= trace_current_buffer_lock_reserve(&buffer
,
2620 TRACE_FN
, sizeof(*entry
),
2624 entry
= ring_buffer_event_data(event
);
2626 entry
->parent_ip
= parent_ip
;
2628 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2631 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2632 preempt_enable_notrace();
2635 static struct ftrace_ops trace_ops __initdata
=
2637 .func
= function_test_events_call
,
2638 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2641 static __init
void event_trace_self_test_with_function(void)
2644 ret
= register_ftrace_function(&trace_ops
);
2645 if (WARN_ON(ret
< 0)) {
2646 pr_info("Failed to enable function tracer for event tests\n");
2649 pr_info("Running tests again, along with the function tracer\n");
2650 event_trace_self_tests();
2651 unregister_ftrace_function(&trace_ops
);
2654 static __init
void event_trace_self_test_with_function(void)
2659 static __init
int event_trace_self_tests_init(void)
2661 if (!tracing_selftest_disabled
) {
2662 event_trace_self_tests();
2663 event_trace_self_test_with_function();
2669 late_initcall(event_trace_self_tests_init
);