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"
25 #include <linux/mtk_ftrace.h>
28 #define TRACE_SYSTEM "TRACE_SYSTEM"
30 DEFINE_MUTEX(event_mutex
);
32 LIST_HEAD(ftrace_events
);
33 static LIST_HEAD(ftrace_common_fields
);
35 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37 static struct kmem_cache
*field_cachep
;
38 static struct kmem_cache
*file_cachep
;
40 #define SYSTEM_FL_FREE_NAME (1 << 31)
42 static inline int system_refcount(struct event_subsystem
*system
)
44 return system
->ref_count
& ~SYSTEM_FL_FREE_NAME
;
47 static int system_refcount_inc(struct event_subsystem
*system
)
49 return (system
->ref_count
++) & ~SYSTEM_FL_FREE_NAME
;
52 static int system_refcount_dec(struct event_subsystem
*system
)
54 return (--system
->ref_count
) & ~SYSTEM_FL_FREE_NAME
;
57 /* Double loops, do not use break, only goto's work */
58 #define do_for_each_event_file(tr, file) \
59 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
60 list_for_each_entry(file, &tr->events, list)
62 #define do_for_each_event_file_safe(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 struct ftrace_event_file *___n; \
65 list_for_each_entry_safe(file, ___n, &tr->events, list)
67 #define while_for_each_event_file() \
70 static struct list_head
*
71 trace_get_fields(struct ftrace_event_call
*event_call
)
73 if (!event_call
->class->get_fields
)
74 return &event_call
->class->fields
;
75 return event_call
->class->get_fields(event_call
);
78 static struct ftrace_event_field
*
79 __find_event_field(struct list_head
*head
, char *name
)
81 struct ftrace_event_field
*field
;
83 list_for_each_entry(field
, head
, link
) {
84 if (!strcmp(field
->name
, name
))
91 struct ftrace_event_field
*
92 trace_find_event_field(struct ftrace_event_call
*call
, char *name
)
94 struct ftrace_event_field
*field
;
95 struct list_head
*head
;
97 field
= __find_event_field(&ftrace_common_fields
, name
);
101 head
= trace_get_fields(call
);
102 return __find_event_field(head
, name
);
105 static int __trace_define_field(struct list_head
*head
, const char *type
,
106 const char *name
, int offset
, int size
,
107 int is_signed
, int filter_type
)
109 struct ftrace_event_field
*field
;
111 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
118 if (filter_type
== FILTER_OTHER
)
119 field
->filter_type
= filter_assign_type(type
);
121 field
->filter_type
= filter_type
;
123 field
->offset
= offset
;
125 field
->is_signed
= is_signed
;
127 list_add(&field
->link
, head
);
132 int trace_define_field(struct ftrace_event_call
*call
, const char *type
,
133 const char *name
, int offset
, int size
, int is_signed
,
136 struct list_head
*head
;
138 if (WARN_ON(!call
->class))
141 head
= trace_get_fields(call
);
142 return __trace_define_field(head
, type
, name
, offset
, size
,
143 is_signed
, filter_type
);
145 EXPORT_SYMBOL_GPL(trace_define_field
);
147 #define __common_field(type, item) \
148 ret = __trace_define_field(&ftrace_common_fields, #type, \
150 offsetof(typeof(ent), item), \
152 is_signed_type(type), FILTER_OTHER); \
156 static int trace_define_common_fields(void)
159 struct trace_entry ent
;
161 __common_field(unsigned short, type
);
162 __common_field(unsigned char, flags
);
163 __common_field(unsigned char, preempt_count
);
164 __common_field(int, pid
);
169 static void trace_destroy_fields(struct ftrace_event_call
*call
)
171 struct ftrace_event_field
*field
, *next
;
172 struct list_head
*head
;
174 head
= trace_get_fields(call
);
175 list_for_each_entry_safe(field
, next
, head
, link
) {
176 list_del(&field
->link
);
177 kmem_cache_free(field_cachep
, field
);
181 int trace_event_raw_init(struct ftrace_event_call
*call
)
185 id
= register_ftrace_event(&call
->event
);
191 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
193 int ftrace_event_reg(struct ftrace_event_call
*call
,
194 enum trace_reg type
, void *data
)
196 struct ftrace_event_file
*file
= data
;
199 case TRACE_REG_REGISTER
:
200 return tracepoint_probe_register(call
->name
,
203 case TRACE_REG_UNREGISTER
:
204 tracepoint_probe_unregister(call
->name
,
209 #ifdef CONFIG_PERF_EVENTS
210 case TRACE_REG_PERF_REGISTER
:
211 return tracepoint_probe_register(call
->name
,
212 call
->class->perf_probe
,
214 case TRACE_REG_PERF_UNREGISTER
:
215 tracepoint_probe_unregister(call
->name
,
216 call
->class->perf_probe
,
219 case TRACE_REG_PERF_OPEN
:
220 case TRACE_REG_PERF_CLOSE
:
221 case TRACE_REG_PERF_ADD
:
222 case TRACE_REG_PERF_DEL
:
228 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
230 void trace_event_enable_cmd_record(bool enable
)
232 struct ftrace_event_file
*file
;
233 struct trace_array
*tr
;
235 mutex_lock(&event_mutex
);
236 do_for_each_event_file(tr
, file
) {
238 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
242 tracing_start_cmdline_record();
243 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
245 tracing_stop_cmdline_record();
246 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
248 } while_for_each_event_file();
249 mutex_unlock(&event_mutex
);
252 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
253 int enable
, int soft_disable
)
255 struct ftrace_event_call
*call
= file
->event_call
;
259 if(call
->name
&& ((file
->flags
& FTRACE_EVENT_FL_ENABLED
) ^ enable
))
260 printk(KERN_INFO
"[ftrace]event '%s' is %s\n", call
->name
, enable
?"enabled":"disabled");
265 * When soft_disable is set and enable is cleared, the sm_ref
266 * reference counter is decremented. If it reaches 0, we want
267 * to clear the SOFT_DISABLED flag but leave the event in the
268 * state that it was. That is, if the event was enabled and
269 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
270 * is set we do not want the event to be enabled before we
273 * When soft_disable is not set but the SOFT_MODE flag is,
274 * we do nothing. Do not disable the tracepoint, otherwise
275 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
278 if (atomic_dec_return(&file
->sm_ref
) > 0)
280 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
281 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
283 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
285 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
286 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
287 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
288 tracing_stop_cmdline_record();
289 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
291 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
293 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
294 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
295 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
299 * When soft_disable is set and enable is set, we want to
300 * register the tracepoint for the event, but leave the event
301 * as is. That means, if the event was already enabled, we do
302 * nothing (but set SOFT_MODE). If the event is disabled, we
303 * set SOFT_DISABLED before enabling the event tracepoint, so
304 * it still seems to be disabled.
307 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
309 if (atomic_inc_return(&file
->sm_ref
) > 1)
311 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
314 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
316 /* Keep the event disabled, when going to SOFT_MODE. */
318 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
320 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
321 tracing_start_cmdline_record();
322 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
324 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
326 tracing_stop_cmdline_record();
327 pr_info("event trace: Could not enable event "
331 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
333 /* WAS_ENABLED gets set but never cleared. */
334 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
342 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
345 return __ftrace_event_enable_disable(file
, enable
, 0);
348 static void ftrace_clear_events(struct trace_array
*tr
)
350 struct ftrace_event_file
*file
;
352 mutex_lock(&event_mutex
);
353 list_for_each_entry(file
, &tr
->events
, list
) {
354 ftrace_event_enable_disable(file
, 0);
356 mutex_unlock(&event_mutex
);
359 static void __put_system(struct event_subsystem
*system
)
361 struct event_filter
*filter
= system
->filter
;
363 WARN_ON_ONCE(system_refcount(system
) == 0);
364 if (system_refcount_dec(system
))
367 list_del(&system
->list
);
370 kfree(filter
->filter_string
);
373 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
378 static void __get_system(struct event_subsystem
*system
)
380 WARN_ON_ONCE(system_refcount(system
) == 0);
381 system_refcount_inc(system
);
384 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
386 WARN_ON_ONCE(dir
->ref_count
== 0);
388 __get_system(dir
->subsystem
);
391 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
393 WARN_ON_ONCE(dir
->ref_count
== 0);
394 /* If the subsystem is about to be freed, the dir must be too */
395 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
397 __put_system(dir
->subsystem
);
398 if (!--dir
->ref_count
)
402 static void put_system(struct ftrace_subsystem_dir
*dir
)
404 mutex_lock(&event_mutex
);
405 __put_system_dir(dir
);
406 mutex_unlock(&event_mutex
);
409 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
414 if (!--dir
->nr_events
) {
415 debugfs_remove_recursive(dir
->entry
);
416 list_del(&dir
->list
);
417 __put_system_dir(dir
);
421 static void *event_file_data(struct file
*filp
)
423 return ACCESS_ONCE(file_inode(filp
)->i_private
);
426 static void remove_event_file_dir(struct ftrace_event_file
*file
)
428 struct dentry
*dir
= file
->dir
;
429 struct dentry
*child
;
432 spin_lock(&dir
->d_lock
); /* probably unneeded */
433 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
434 if (child
->d_inode
) /* probably unneeded */
435 child
->d_inode
->i_private
= NULL
;
437 spin_unlock(&dir
->d_lock
);
439 debugfs_remove_recursive(dir
);
442 list_del(&file
->list
);
443 remove_subsystem(file
->system
);
444 kmem_cache_free(file_cachep
, file
);
448 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
451 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
452 const char *sub
, const char *event
, int set
)
454 struct ftrace_event_file
*file
;
455 struct ftrace_event_call
*call
;
458 list_for_each_entry(file
, &tr
->events
, list
) {
460 call
= file
->event_call
;
462 if (!call
->name
|| !call
->class || !call
->class->reg
)
465 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
469 strcmp(match
, call
->name
) != 0 &&
470 strcmp(match
, call
->class->system
) != 0)
473 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
476 if (event
&& strcmp(event
, call
->name
) != 0)
479 ftrace_event_enable_disable(file
, set
);
487 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
488 const char *sub
, const char *event
, int set
)
492 mutex_lock(&event_mutex
);
493 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
494 mutex_unlock(&event_mutex
);
499 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
501 char *event
= NULL
, *sub
= NULL
, *match
;
504 * The buf format can be <subsystem>:<event-name>
505 * *:<event-name> means any event by that name.
506 * :<event-name> is the same.
508 * <subsystem>:* means all events in that subsystem
509 * <subsystem>: means the same.
511 * <name> (no ':') means all events in a subsystem with
512 * the name <name> or any event that matches <name>
515 match
= strsep(&buf
, ":");
521 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
523 if (!strlen(event
) || strcmp(event
, "*") == 0)
527 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
531 * trace_set_clr_event - enable or disable an event
532 * @system: system name to match (NULL for any system)
533 * @event: event name to match (NULL for all events, within system)
534 * @set: 1 to enable, 0 to disable
536 * This is a way for other parts of the kernel to enable or disable
539 * Returns 0 on success, -EINVAL if the parameters do not match any
542 int trace_set_clr_event(const char *system
, const char *event
, int set
)
544 struct trace_array
*tr
= top_trace_array();
546 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
548 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
550 /* 128 should be much more than enough */
551 #define EVENT_BUF_SIZE 127
554 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
555 size_t cnt
, loff_t
*ppos
)
557 struct trace_parser parser
;
558 struct seq_file
*m
= file
->private_data
;
559 struct trace_array
*tr
= m
->private;
565 ret
= tracing_update_buffers();
569 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
572 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
574 if (read
>= 0 && trace_parser_loaded((&parser
))) {
577 if (*parser
.buffer
== '!')
580 parser
.buffer
[parser
.idx
] = 0;
582 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
590 trace_parser_put(&parser
);
596 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
598 struct ftrace_event_file
*file
= v
;
599 struct ftrace_event_call
*call
;
600 struct trace_array
*tr
= m
->private;
604 list_for_each_entry_continue(file
, &tr
->events
, list
) {
605 call
= file
->event_call
;
607 * The ftrace subsystem is for showing formats only.
608 * They can not be enabled or disabled via the event files.
610 if (call
->class && call
->class->reg
)
617 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
619 struct ftrace_event_file
*file
;
620 struct trace_array
*tr
= m
->private;
623 mutex_lock(&event_mutex
);
625 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
626 for (l
= 0; l
<= *pos
; ) {
627 file
= t_next(m
, file
, &l
);
635 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
637 struct ftrace_event_file
*file
= v
;
638 struct trace_array
*tr
= m
->private;
642 list_for_each_entry_continue(file
, &tr
->events
, list
) {
643 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
650 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
652 struct ftrace_event_file
*file
;
653 struct trace_array
*tr
= m
->private;
656 mutex_lock(&event_mutex
);
658 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
659 for (l
= 0; l
<= *pos
; ) {
660 file
= s_next(m
, file
, &l
);
667 static int t_show(struct seq_file
*m
, void *v
)
669 struct ftrace_event_file
*file
= v
;
670 struct ftrace_event_call
*call
= file
->event_call
;
672 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
673 seq_printf(m
, "%s:", call
->class->system
);
674 seq_printf(m
, "%s\n", call
->name
);
679 static void t_stop(struct seq_file
*m
, void *p
)
681 mutex_unlock(&event_mutex
);
685 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
688 struct ftrace_event_file
*file
;
692 mutex_lock(&event_mutex
);
693 file
= event_file_data(filp
);
696 mutex_unlock(&event_mutex
);
701 if (flags
& FTRACE_EVENT_FL_ENABLED
) {
702 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
)
704 else if (flags
& FTRACE_EVENT_FL_SOFT_MODE
)
711 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
715 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
718 struct ftrace_event_file
*file
;
722 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
726 ret
= tracing_update_buffers();
734 mutex_lock(&event_mutex
);
735 file
= event_file_data(filp
);
737 ret
= ftrace_event_enable_disable(file
, val
);
738 mutex_unlock(&event_mutex
);
747 return ret
? ret
: cnt
;
751 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
754 const char set_to_char
[4] = { '?', '0', '1', 'X' };
755 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
756 struct event_subsystem
*system
= dir
->subsystem
;
757 struct ftrace_event_call
*call
;
758 struct ftrace_event_file
*file
;
759 struct trace_array
*tr
= dir
->tr
;
764 mutex_lock(&event_mutex
);
765 list_for_each_entry(file
, &tr
->events
, list
) {
766 call
= file
->event_call
;
767 if (!call
->name
|| !call
->class || !call
->class->reg
)
770 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
774 * We need to find out if all the events are set
775 * or if all events or cleared, or if we have
778 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
781 * If we have a mixture, no need to look further.
786 mutex_unlock(&event_mutex
);
788 buf
[0] = set_to_char
[set
];
791 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
797 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
800 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
801 struct event_subsystem
*system
= dir
->subsystem
;
802 const char *name
= NULL
;
806 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
810 ret
= tracing_update_buffers();
814 if (val
!= 0 && val
!= 1)
818 * Opening of "enable" adds a ref count to system,
819 * so the name is safe to use.
824 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
838 FORMAT_FIELD_SEPERATOR
= 2,
842 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
844 struct ftrace_event_call
*call
= event_file_data(m
->private);
845 struct ftrace_event_field
*field
;
846 struct list_head
*common_head
= &ftrace_common_fields
;
847 struct list_head
*head
= trace_get_fields(call
);
851 switch ((unsigned long)v
) {
853 if (unlikely(list_empty(common_head
)))
856 field
= list_entry(common_head
->prev
,
857 struct ftrace_event_field
, link
);
860 case FORMAT_FIELD_SEPERATOR
:
861 if (unlikely(list_empty(head
)))
864 field
= list_entry(head
->prev
, struct ftrace_event_field
, link
);
867 case FORMAT_PRINTFMT
:
873 if (field
->link
.prev
== common_head
)
874 return (void *)FORMAT_FIELD_SEPERATOR
;
875 else if (field
->link
.prev
== head
)
876 return (void *)FORMAT_PRINTFMT
;
878 field
= list_entry(field
->link
.prev
, struct ftrace_event_field
, link
);
883 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
888 /* ->stop() is called even if ->start() fails */
889 mutex_lock(&event_mutex
);
890 if (!event_file_data(m
->private))
891 return ERR_PTR(-ENODEV
);
893 /* Start by showing the header */
895 return (void *)FORMAT_HEADER
;
897 p
= (void *)FORMAT_HEADER
;
899 p
= f_next(m
, p
, &l
);
900 } while (p
&& l
< *pos
);
905 static int f_show(struct seq_file
*m
, void *v
)
907 struct ftrace_event_call
*call
= event_file_data(m
->private);
908 struct ftrace_event_field
*field
;
909 const char *array_descriptor
;
911 switch ((unsigned long)v
) {
913 seq_printf(m
, "name: %s\n", call
->name
);
914 seq_printf(m
, "ID: %d\n", call
->event
.type
);
915 seq_printf(m
, "format:\n");
918 case FORMAT_FIELD_SEPERATOR
:
922 case FORMAT_PRINTFMT
:
923 seq_printf(m
, "\nprint fmt: %s\n",
931 * Smartly shows the array type(except dynamic array).
934 * If TYPE := TYPE[LEN], it is shown:
935 * field:TYPE VAR[LEN]
937 array_descriptor
= strchr(field
->type
, '[');
939 if (!strncmp(field
->type
, "__data_loc", 10))
940 array_descriptor
= NULL
;
942 if (!array_descriptor
)
943 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
944 field
->type
, field
->name
, field
->offset
,
945 field
->size
, !!field
->is_signed
);
947 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
948 (int)(array_descriptor
- field
->type
),
949 field
->type
, field
->name
,
950 array_descriptor
, field
->offset
,
951 field
->size
, !!field
->is_signed
);
956 static void f_stop(struct seq_file
*m
, void *p
)
958 mutex_unlock(&event_mutex
);
961 static const struct seq_operations trace_format_seq_ops
= {
968 static int trace_format_open(struct inode
*inode
, struct file
*file
)
973 ret
= seq_open(file
, &trace_format_seq_ops
);
977 m
= file
->private_data
;
984 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
986 int id
= (long)event_file_data(filp
);
996 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1001 trace_seq_printf(s
, "%d\n", id
);
1003 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1010 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1013 struct ftrace_event_call
*call
;
1014 struct trace_seq
*s
;
1020 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1027 mutex_lock(&event_mutex
);
1028 call
= event_file_data(filp
);
1030 print_event_filter(call
, s
);
1031 mutex_unlock(&event_mutex
);
1034 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1042 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1045 struct ftrace_event_call
*call
;
1049 if (cnt
>= PAGE_SIZE
)
1052 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1056 if (copy_from_user(buf
, ubuf
, cnt
)) {
1057 free_page((unsigned long) buf
);
1062 mutex_lock(&event_mutex
);
1063 call
= event_file_data(filp
);
1065 err
= apply_event_filter(call
, buf
);
1066 mutex_unlock(&event_mutex
);
1068 free_page((unsigned long) buf
);
1077 static LIST_HEAD(event_subsystems
);
1079 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1081 struct event_subsystem
*system
= NULL
;
1082 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1083 struct trace_array
*tr
;
1086 /* Make sure the system still exists */
1087 mutex_lock(&trace_types_lock
);
1088 mutex_lock(&event_mutex
);
1089 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1090 list_for_each_entry(dir
, &tr
->systems
, list
) {
1091 if (dir
== inode
->i_private
) {
1092 /* Don't open systems with no events */
1093 if (dir
->nr_events
) {
1094 __get_system_dir(dir
);
1095 system
= dir
->subsystem
;
1102 mutex_unlock(&event_mutex
);
1103 mutex_unlock(&trace_types_lock
);
1108 /* Some versions of gcc think dir can be uninitialized here */
1111 /* Still need to increment the ref count of the system */
1112 if (trace_array_get(tr
) < 0) {
1117 ret
= tracing_open_generic(inode
, filp
);
1119 trace_array_put(tr
);
1126 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1128 struct ftrace_subsystem_dir
*dir
;
1129 struct trace_array
*tr
= inode
->i_private
;
1132 if (trace_array_get(tr
) < 0)
1135 /* Make a temporary dir that has no system but points to tr */
1136 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1138 trace_array_put(tr
);
1144 ret
= tracing_open_generic(inode
, filp
);
1146 trace_array_put(tr
);
1150 filp
->private_data
= dir
;
1155 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1157 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1159 trace_array_put(dir
->tr
);
1162 * If dir->subsystem is NULL, then this is a temporary
1163 * descriptor that was made for a trace_array to enable
1175 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1178 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1179 struct event_subsystem
*system
= dir
->subsystem
;
1180 struct trace_seq
*s
;
1186 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1192 print_subsystem_event_filter(system
, s
);
1193 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1201 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1204 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1208 if (cnt
>= PAGE_SIZE
)
1211 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1215 if (copy_from_user(buf
, ubuf
, cnt
)) {
1216 free_page((unsigned long) buf
);
1221 err
= apply_subsystem_event_filter(dir
, buf
);
1222 free_page((unsigned long) buf
);
1232 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1234 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1235 struct trace_seq
*s
;
1241 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1248 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1255 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1256 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1257 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1259 static const struct seq_operations show_event_seq_ops
= {
1266 static const struct seq_operations show_set_event_seq_ops
= {
1273 static const struct file_operations ftrace_avail_fops
= {
1274 .open
= ftrace_event_avail_open
,
1276 .llseek
= seq_lseek
,
1277 .release
= seq_release
,
1280 static const struct file_operations ftrace_set_event_fops
= {
1281 .open
= ftrace_event_set_open
,
1283 .write
= ftrace_event_write
,
1284 .llseek
= seq_lseek
,
1285 .release
= ftrace_event_release
,
1288 static const struct file_operations ftrace_enable_fops
= {
1289 .open
= tracing_open_generic
,
1290 .read
= event_enable_read
,
1291 .write
= event_enable_write
,
1292 .llseek
= default_llseek
,
1295 static const struct file_operations ftrace_event_format_fops
= {
1296 .open
= trace_format_open
,
1298 .llseek
= seq_lseek
,
1299 .release
= seq_release
,
1302 static const struct file_operations ftrace_event_id_fops
= {
1303 .read
= event_id_read
,
1304 .llseek
= default_llseek
,
1307 static const struct file_operations ftrace_event_filter_fops
= {
1308 .open
= tracing_open_generic
,
1309 .read
= event_filter_read
,
1310 .write
= event_filter_write
,
1311 .llseek
= default_llseek
,
1314 static const struct file_operations ftrace_subsystem_filter_fops
= {
1315 .open
= subsystem_open
,
1316 .read
= subsystem_filter_read
,
1317 .write
= subsystem_filter_write
,
1318 .llseek
= default_llseek
,
1319 .release
= subsystem_release
,
1322 static const struct file_operations ftrace_system_enable_fops
= {
1323 .open
= subsystem_open
,
1324 .read
= system_enable_read
,
1325 .write
= system_enable_write
,
1326 .llseek
= default_llseek
,
1327 .release
= subsystem_release
,
1330 static const struct file_operations ftrace_tr_enable_fops
= {
1331 .open
= system_tr_open
,
1332 .read
= system_enable_read
,
1333 .write
= system_enable_write
,
1334 .llseek
= default_llseek
,
1335 .release
= subsystem_release
,
1338 static const struct file_operations ftrace_show_header_fops
= {
1339 .open
= tracing_open_generic
,
1340 .read
= show_header
,
1341 .llseek
= default_llseek
,
1345 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1346 const struct seq_operations
*seq_ops
)
1351 ret
= seq_open(file
, seq_ops
);
1354 m
= file
->private_data
;
1355 /* copy tr over to seq ops */
1356 m
->private = inode
->i_private
;
1361 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1363 struct trace_array
*tr
= inode
->i_private
;
1365 trace_array_put(tr
);
1367 return seq_release(inode
, file
);
1371 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1373 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1375 return ftrace_event_open(inode
, file
, seq_ops
);
1379 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1381 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1382 struct trace_array
*tr
= inode
->i_private
;
1385 if (trace_array_get(tr
) < 0)
1388 if ((file
->f_mode
& FMODE_WRITE
) &&
1389 (file
->f_flags
& O_TRUNC
))
1390 ftrace_clear_events(tr
);
1392 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1394 trace_array_put(tr
);
1398 static struct event_subsystem
*
1399 create_new_subsystem(const char *name
)
1401 struct event_subsystem
*system
;
1403 /* need to create new entry */
1404 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1408 system
->ref_count
= 1;
1410 /* Only allocate if dynamic (kprobes and modules) */
1411 if (!core_kernel_data((unsigned long)name
)) {
1412 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1413 system
->name
= kstrdup(name
, GFP_KERNEL
);
1417 system
->name
= name
;
1419 system
->filter
= NULL
;
1421 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1422 if (!system
->filter
)
1425 list_add(&system
->list
, &event_subsystems
);
1430 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1431 kfree(system
->name
);
1436 static struct dentry
*
1437 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1438 struct ftrace_event_file
*file
, struct dentry
*parent
)
1440 struct ftrace_subsystem_dir
*dir
;
1441 struct event_subsystem
*system
;
1442 struct dentry
*entry
;
1444 /* First see if we did not already create this dir */
1445 list_for_each_entry(dir
, &tr
->systems
, list
) {
1446 system
= dir
->subsystem
;
1447 if (strcmp(system
->name
, name
) == 0) {
1454 /* Now see if the system itself exists. */
1455 list_for_each_entry(system
, &event_subsystems
, list
) {
1456 if (strcmp(system
->name
, name
) == 0)
1459 /* Reset system variable when not found */
1460 if (&system
->list
== &event_subsystems
)
1463 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1468 system
= create_new_subsystem(name
);
1472 __get_system(system
);
1474 dir
->entry
= debugfs_create_dir(name
, parent
);
1476 pr_warning("Failed to create system directory %s\n", name
);
1477 __put_system(system
);
1484 dir
->subsystem
= system
;
1487 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1488 &ftrace_subsystem_filter_fops
);
1490 kfree(system
->filter
);
1491 system
->filter
= NULL
;
1492 pr_warning("Could not create debugfs '%s/filter' entry\n", name
);
1495 trace_create_file("enable", 0644, dir
->entry
, dir
,
1496 &ftrace_system_enable_fops
);
1498 list_add(&dir
->list
, &tr
->systems
);
1505 /* Only print this message if failed on memory allocation */
1506 if (!dir
|| !system
)
1507 pr_warning("No memory to create event subsystem %s\n",
1513 event_create_dir(struct dentry
*parent
,
1514 struct ftrace_event_file
*file
,
1515 const struct file_operations
*id
,
1516 const struct file_operations
*enable
,
1517 const struct file_operations
*filter
,
1518 const struct file_operations
*format
)
1520 struct ftrace_event_call
*call
= file
->event_call
;
1521 struct trace_array
*tr
= file
->tr
;
1522 struct list_head
*head
;
1523 struct dentry
*d_events
;
1527 * If the trace point header did not define TRACE_SYSTEM
1528 * then the system would be called "TRACE_SYSTEM".
1530 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1531 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1537 file
->dir
= debugfs_create_dir(call
->name
, d_events
);
1539 pr_warning("Could not create debugfs '%s' directory\n",
1544 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1545 trace_create_file("enable", 0644, file
->dir
, file
,
1548 #ifdef CONFIG_PERF_EVENTS
1549 if (call
->event
.type
&& call
->class->reg
)
1550 trace_create_file("id", 0444, file
->dir
,
1551 (void *)(long)call
->event
.type
, id
);
1555 * Other events may have the same class. Only update
1556 * the fields if they are not already defined.
1558 head
= trace_get_fields(call
);
1559 if (list_empty(head
)) {
1560 ret
= call
->class->define_fields(call
);
1562 pr_warning("Could not initialize trace point"
1563 " events/%s\n", call
->name
);
1567 trace_create_file("filter", 0644, file
->dir
, call
,
1570 trace_create_file("format", 0444, file
->dir
, call
,
1576 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1578 struct ftrace_event_file
*file
;
1579 struct trace_array
*tr
;
1581 do_for_each_event_file_safe(tr
, file
) {
1582 if (file
->event_call
!= call
)
1585 remove_event_file_dir(file
);
1587 * The do_for_each_event_file_safe() is
1588 * a double loop. After finding the call for this
1589 * trace_array, we use break to jump to the next
1593 } while_for_each_event_file();
1596 static void event_remove(struct ftrace_event_call
*call
)
1598 struct trace_array
*tr
;
1599 struct ftrace_event_file
*file
;
1601 do_for_each_event_file(tr
, file
) {
1602 if (file
->event_call
!= call
)
1604 ftrace_event_enable_disable(file
, 0);
1606 * The do_for_each_event_file() is
1607 * a double loop. After finding the call for this
1608 * trace_array, we use break to jump to the next
1612 } while_for_each_event_file();
1614 if (call
->event
.funcs
)
1615 __unregister_ftrace_event(&call
->event
);
1616 remove_event_from_tracers(call
);
1617 list_del(&call
->list
);
1620 static int event_init(struct ftrace_event_call
*call
)
1624 if (WARN_ON(!call
->name
))
1627 if (call
->class->raw_init
) {
1628 ret
= call
->class->raw_init(call
);
1629 if (ret
< 0 && ret
!= -ENOSYS
)
1630 pr_warn("Could not initialize trace events/%s\n",
1638 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1642 ret
= event_init(call
);
1646 list_add(&call
->list
, &ftrace_events
);
1652 static struct ftrace_event_file
*
1653 trace_create_new_event(struct ftrace_event_call
*call
,
1654 struct trace_array
*tr
)
1656 struct ftrace_event_file
*file
;
1658 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1662 file
->event_call
= call
;
1664 atomic_set(&file
->sm_ref
, 0);
1665 list_add(&file
->list
, &tr
->events
);
1670 /* Add an event to a trace directory */
1672 __trace_add_new_event(struct ftrace_event_call
*call
,
1673 struct trace_array
*tr
,
1674 const struct file_operations
*id
,
1675 const struct file_operations
*enable
,
1676 const struct file_operations
*filter
,
1677 const struct file_operations
*format
)
1679 struct ftrace_event_file
*file
;
1681 file
= trace_create_new_event(call
, tr
);
1685 return event_create_dir(tr
->event_dir
, file
, id
, enable
, filter
, format
);
1689 * Just create a decriptor for early init. A descriptor is required
1690 * for enabling events at boot. We want to enable events before
1691 * the filesystem is initialized.
1694 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1695 struct trace_array
*tr
)
1697 struct ftrace_event_file
*file
;
1699 file
= trace_create_new_event(call
, tr
);
1706 struct ftrace_module_file_ops
;
1707 static void __add_event_to_tracers(struct ftrace_event_call
*call
,
1708 struct ftrace_module_file_ops
*file_ops
);
1710 /* Add an additional event_call dynamically */
1711 int trace_add_event_call(struct ftrace_event_call
*call
)
1714 mutex_lock(&trace_types_lock
);
1715 mutex_lock(&event_mutex
);
1717 ret
= __register_event(call
, NULL
);
1719 __add_event_to_tracers(call
, NULL
);
1721 mutex_unlock(&event_mutex
);
1722 mutex_unlock(&trace_types_lock
);
1727 * Must be called under locking of trace_types_lock, event_mutex and
1730 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1733 trace_destroy_fields(call
);
1734 destroy_preds(call
);
1737 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1739 struct trace_array
*tr
;
1740 struct ftrace_event_file
*file
;
1742 #ifdef CONFIG_PERF_EVENTS
1743 if (call
->perf_refcount
)
1746 do_for_each_event_file(tr
, file
) {
1747 if (file
->event_call
!= call
)
1750 * We can't rely on ftrace_event_enable_disable(enable => 0)
1751 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1752 * TRACE_REG_UNREGISTER.
1754 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1757 } while_for_each_event_file();
1759 __trace_remove_event_call(call
);
1764 /* Remove an event_call */
1765 int trace_remove_event_call(struct ftrace_event_call
*call
)
1769 mutex_lock(&trace_types_lock
);
1770 mutex_lock(&event_mutex
);
1771 down_write(&trace_event_sem
);
1772 ret
= probe_remove_event_call(call
);
1773 up_write(&trace_event_sem
);
1774 mutex_unlock(&event_mutex
);
1775 mutex_unlock(&trace_types_lock
);
1780 #define for_each_event(event, start, end) \
1781 for (event = start; \
1782 (unsigned long)event < (unsigned long)end; \
1785 #ifdef CONFIG_MODULES
1787 static LIST_HEAD(ftrace_module_file_list
);
1790 * Modules must own their file_operations to keep up with
1791 * reference counting.
1793 struct ftrace_module_file_ops
{
1794 struct list_head list
;
1796 struct file_operations id
;
1797 struct file_operations enable
;
1798 struct file_operations format
;
1799 struct file_operations filter
;
1802 static struct ftrace_module_file_ops
*
1803 find_ftrace_file_ops(struct ftrace_module_file_ops
*file_ops
, struct module
*mod
)
1806 * As event_calls are added in groups by module,
1807 * when we find one file_ops, we don't need to search for
1808 * each call in that module, as the rest should be the
1809 * same. Only search for a new one if the last one did
1812 if (file_ops
&& mod
== file_ops
->mod
)
1815 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1816 if (file_ops
->mod
== mod
)
1822 static struct ftrace_module_file_ops
*
1823 trace_create_file_ops(struct module
*mod
)
1825 struct ftrace_module_file_ops
*file_ops
;
1828 * This is a bit of a PITA. To allow for correct reference
1829 * counting, modules must "own" their file_operations.
1830 * To do this, we allocate the file operations that will be
1831 * used in the event directory.
1834 file_ops
= kmalloc(sizeof(*file_ops
), GFP_KERNEL
);
1838 file_ops
->mod
= mod
;
1840 file_ops
->id
= ftrace_event_id_fops
;
1841 file_ops
->id
.owner
= mod
;
1843 file_ops
->enable
= ftrace_enable_fops
;
1844 file_ops
->enable
.owner
= mod
;
1846 file_ops
->filter
= ftrace_event_filter_fops
;
1847 file_ops
->filter
.owner
= mod
;
1849 file_ops
->format
= ftrace_event_format_fops
;
1850 file_ops
->format
.owner
= mod
;
1852 list_add(&file_ops
->list
, &ftrace_module_file_list
);
1857 static void trace_module_add_events(struct module
*mod
)
1859 struct ftrace_module_file_ops
*file_ops
= NULL
;
1860 struct ftrace_event_call
**call
, **start
, **end
;
1862 if (!mod
->num_trace_events
)
1865 /* Don't add infrastructure for mods without tracepoints */
1866 if (trace_module_has_bad_taint(mod
)) {
1867 pr_err("%s: module has bad taint, not creating trace events\n",
1872 start
= mod
->trace_events
;
1873 end
= mod
->trace_events
+ mod
->num_trace_events
;
1878 file_ops
= trace_create_file_ops(mod
);
1882 for_each_event(call
, start
, end
) {
1883 __register_event(*call
, mod
);
1884 __add_event_to_tracers(*call
, file_ops
);
1888 static void trace_module_remove_events(struct module
*mod
)
1890 struct ftrace_module_file_ops
*file_ops
;
1891 struct ftrace_event_call
*call
, *p
;
1892 bool clear_trace
= false;
1894 down_write(&trace_event_sem
);
1895 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1896 if (call
->mod
== mod
) {
1897 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1899 __trace_remove_event_call(call
);
1903 /* Now free the file_operations */
1904 list_for_each_entry(file_ops
, &ftrace_module_file_list
, list
) {
1905 if (file_ops
->mod
== mod
)
1908 if (&file_ops
->list
!= &ftrace_module_file_list
) {
1909 list_del(&file_ops
->list
);
1912 up_write(&trace_event_sem
);
1915 * It is safest to reset the ring buffer if the module being unloaded
1916 * registered any events that were used. The only worry is if
1917 * a new module gets loaded, and takes on the same id as the events
1918 * of this module. When printing out the buffer, traced events left
1919 * over from this module may be passed to the new module events and
1920 * unexpected results may occur.
1923 tracing_reset_all_online_cpus();
1926 static int trace_module_notify(struct notifier_block
*self
,
1927 unsigned long val
, void *data
)
1929 struct module
*mod
= data
;
1931 mutex_lock(&trace_types_lock
);
1932 mutex_lock(&event_mutex
);
1934 case MODULE_STATE_COMING
:
1935 trace_module_add_events(mod
);
1937 case MODULE_STATE_GOING
:
1938 trace_module_remove_events(mod
);
1941 mutex_unlock(&event_mutex
);
1942 mutex_unlock(&trace_types_lock
);
1948 __trace_add_new_mod_event(struct ftrace_event_call
*call
,
1949 struct trace_array
*tr
,
1950 struct ftrace_module_file_ops
*file_ops
)
1952 return __trace_add_new_event(call
, tr
,
1953 &file_ops
->id
, &file_ops
->enable
,
1954 &file_ops
->filter
, &file_ops
->format
);
1958 static inline struct ftrace_module_file_ops
*
1959 find_ftrace_file_ops(struct ftrace_module_file_ops
*file_ops
, struct module
*mod
)
1963 static inline int trace_module_notify(struct notifier_block
*self
,
1964 unsigned long val
, void *data
)
1969 __trace_add_new_mod_event(struct ftrace_event_call
*call
,
1970 struct trace_array
*tr
,
1971 struct ftrace_module_file_ops
*file_ops
)
1975 #endif /* CONFIG_MODULES */
1977 /* Create a new event directory structure for a trace directory. */
1979 __trace_add_event_dirs(struct trace_array
*tr
)
1981 struct ftrace_module_file_ops
*file_ops
= NULL
;
1982 struct ftrace_event_call
*call
;
1985 list_for_each_entry(call
, &ftrace_events
, list
) {
1988 * Directories for events by modules need to
1989 * keep module ref counts when opened (as we don't
1990 * want the module to disappear when reading one
1991 * of these files). The file_ops keep account of
1992 * the module ref count.
1994 file_ops
= find_ftrace_file_ops(file_ops
, call
->mod
);
1996 continue; /* Warn? */
1997 ret
= __trace_add_new_mod_event(call
, tr
, file_ops
);
1999 pr_warning("Could not create directory for event %s\n",
2003 ret
= __trace_add_new_event(call
, tr
,
2004 &ftrace_event_id_fops
,
2005 &ftrace_enable_fops
,
2006 &ftrace_event_filter_fops
,
2007 &ftrace_event_format_fops
);
2009 pr_warning("Could not create directory for event %s\n",
2014 #ifdef CONFIG_DYNAMIC_FTRACE
2017 #define ENABLE_EVENT_STR "enable_event"
2018 #define DISABLE_EVENT_STR "disable_event"
2020 struct event_probe_data
{
2021 struct ftrace_event_file
*file
;
2022 unsigned long count
;
2027 static struct ftrace_event_file
*
2028 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
2030 struct ftrace_event_file
*file
;
2031 struct ftrace_event_call
*call
;
2033 list_for_each_entry(file
, &tr
->events
, list
) {
2035 call
= file
->event_call
;
2037 if (!call
->name
|| !call
->class || !call
->class->reg
)
2040 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
2043 if (strcmp(event
, call
->name
) == 0 &&
2044 strcmp(system
, call
->class->system
) == 0)
2051 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2053 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2054 struct event_probe_data
*data
= *pdata
;
2060 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2062 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2066 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2068 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2069 struct event_probe_data
*data
= *pdata
;
2077 /* Skip if the event is in a state we want to switch to */
2078 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
2081 if (data
->count
!= -1)
2084 event_enable_probe(ip
, parent_ip
, _data
);
2088 event_enable_print(struct seq_file
*m
, unsigned long ip
,
2089 struct ftrace_probe_ops
*ops
, void *_data
)
2091 struct event_probe_data
*data
= _data
;
2093 seq_printf(m
, "%ps:", (void *)ip
);
2095 seq_printf(m
, "%s:%s:%s",
2096 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
2097 data
->file
->event_call
->class->system
,
2098 data
->file
->event_call
->name
);
2100 if (data
->count
== -1)
2101 seq_printf(m
, ":unlimited\n");
2103 seq_printf(m
, ":count=%ld\n", data
->count
);
2109 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2112 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2113 struct event_probe_data
*data
= *pdata
;
2120 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2123 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2124 struct event_probe_data
*data
= *pdata
;
2126 if (WARN_ON_ONCE(data
->ref
<= 0))
2131 /* Remove the SOFT_MODE flag */
2132 __ftrace_event_enable_disable(data
->file
, 0, 1);
2133 module_put(data
->file
->event_call
->mod
);
2139 static struct ftrace_probe_ops event_enable_probe_ops
= {
2140 .func
= event_enable_probe
,
2141 .print
= event_enable_print
,
2142 .init
= event_enable_init
,
2143 .free
= event_enable_free
,
2146 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2147 .func
= event_enable_count_probe
,
2148 .print
= event_enable_print
,
2149 .init
= event_enable_init
,
2150 .free
= event_enable_free
,
2153 static struct ftrace_probe_ops event_disable_probe_ops
= {
2154 .func
= event_enable_probe
,
2155 .print
= event_enable_print
,
2156 .init
= event_enable_init
,
2157 .free
= event_enable_free
,
2160 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2161 .func
= event_enable_count_probe
,
2162 .print
= event_enable_print
,
2163 .init
= event_enable_init
,
2164 .free
= event_enable_free
,
2168 event_enable_func(struct ftrace_hash
*hash
,
2169 char *glob
, char *cmd
, char *param
, int enabled
)
2171 struct trace_array
*tr
= top_trace_array();
2172 struct ftrace_event_file
*file
;
2173 struct ftrace_probe_ops
*ops
;
2174 struct event_probe_data
*data
;
2181 /* hash funcs only work with set_ftrace_filter */
2188 system
= strsep(¶m
, ":");
2192 event
= strsep(¶m
, ":");
2194 mutex_lock(&event_mutex
);
2197 file
= find_event_file(tr
, system
, event
);
2201 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2204 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2206 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2208 if (glob
[0] == '!') {
2209 unregister_ftrace_function_probe_func(glob
+1, ops
);
2215 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2219 data
->enable
= enable
;
2226 number
= strsep(¶m
, ":");
2229 if (!strlen(number
))
2233 * We use the callback data field (which is a pointer)
2236 ret
= kstrtoul(number
, 0, &data
->count
);
2241 /* Don't let event modules unload while probe registered */
2242 ret
= try_module_get(file
->event_call
->mod
);
2248 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2251 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2253 * The above returns on success the # of functions enabled,
2254 * but if it didn't find any functions it returns zero.
2255 * Consider no functions a failure too.
2262 /* Just return zero, not the number of enabled functions */
2265 mutex_unlock(&event_mutex
);
2269 __ftrace_event_enable_disable(file
, 0, 1);
2271 module_put(file
->event_call
->mod
);
2277 static struct ftrace_func_command event_enable_cmd
= {
2278 .name
= ENABLE_EVENT_STR
,
2279 .func
= event_enable_func
,
2282 static struct ftrace_func_command event_disable_cmd
= {
2283 .name
= DISABLE_EVENT_STR
,
2284 .func
= event_enable_func
,
2287 static __init
int register_event_cmds(void)
2291 ret
= register_ftrace_command(&event_enable_cmd
);
2292 if (WARN_ON(ret
< 0))
2294 ret
= register_ftrace_command(&event_disable_cmd
);
2295 if (WARN_ON(ret
< 0))
2296 unregister_ftrace_command(&event_enable_cmd
);
2300 static inline int register_event_cmds(void) { return 0; }
2301 #endif /* CONFIG_DYNAMIC_FTRACE */
2304 * The top level array has already had its ftrace_event_file
2305 * descriptors created in order to allow for early events to
2306 * be recorded. This function is called after the debugfs has been
2307 * initialized, and we now have to create the files associated
2311 __trace_early_add_event_dirs(struct trace_array
*tr
)
2313 struct ftrace_event_file
*file
;
2317 list_for_each_entry(file
, &tr
->events
, list
) {
2318 ret
= event_create_dir(tr
->event_dir
, file
,
2319 &ftrace_event_id_fops
,
2320 &ftrace_enable_fops
,
2321 &ftrace_event_filter_fops
,
2322 &ftrace_event_format_fops
);
2324 pr_warning("Could not create directory for event %s\n",
2325 file
->event_call
->name
);
2330 * For early boot up, the top trace array requires to have
2331 * a list of events that can be enabled. This must be done before
2332 * the filesystem is set up in order to allow events to be traced
2336 __trace_early_add_events(struct trace_array
*tr
)
2338 struct ftrace_event_call
*call
;
2341 list_for_each_entry(call
, &ftrace_events
, list
) {
2342 /* Early boot up should not have any modules loaded */
2343 if (WARN_ON_ONCE(call
->mod
))
2346 ret
= __trace_early_add_new_event(call
, tr
);
2348 pr_warning("Could not create early event %s\n",
2353 /* Remove the event directory structure for a trace directory. */
2355 __trace_remove_event_dirs(struct trace_array
*tr
)
2357 struct ftrace_event_file
*file
, *next
;
2359 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2360 remove_event_file_dir(file
);
2364 __add_event_to_tracers(struct ftrace_event_call
*call
,
2365 struct ftrace_module_file_ops
*file_ops
)
2367 struct trace_array
*tr
;
2369 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
2371 __trace_add_new_mod_event(call
, tr
, file_ops
);
2373 __trace_add_new_event(call
, tr
,
2374 &ftrace_event_id_fops
,
2375 &ftrace_enable_fops
,
2376 &ftrace_event_filter_fops
,
2377 &ftrace_event_format_fops
);
2381 static struct notifier_block trace_module_nb
= {
2382 .notifier_call
= trace_module_notify
,
2386 extern struct ftrace_event_call
*__start_ftrace_events
[];
2387 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2389 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2391 static __init
int setup_trace_event(char *str
)
2393 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2394 ring_buffer_expanded
= true;
2395 tracing_selftest_disabled
= true;
2399 __setup("trace_event=", setup_trace_event
);
2401 #ifdef CONFIG_MTK_SCHED_TRACERS
2402 // collect boot time ftrace, disabled by default
2403 static int boot_time_ftrace
= 0;
2405 static __init
int setup_boot_time_ftrace(char *str
)
2407 boot_time_ftrace
= 1;
2410 __setup("boot_time_ftrace", setup_boot_time_ftrace
);
2412 #ifdef CONFIG_MTK_FTRACE_DEFAULT_ENABLE
2414 // delay the ring buffer expand until lat_initcall stage
2415 // to avoid impacting the boot time
2416 static __init
int expand_ring_buffer_init(void){
2417 if(!boot_time_ftrace
)
2418 tracing_update_buffers();
2421 late_initcall(expand_ring_buffer_init
);
2423 #endif /* CONFIG_MTK_FTRACE_DEFAULT_ENABLE */
2424 #endif /* CONFIG_MTK_SCHED_TRACERS */
2426 /* Expects to have event_mutex held when called */
2428 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2430 struct dentry
*d_events
;
2431 struct dentry
*entry
;
2433 entry
= debugfs_create_file("set_event", 0644, parent
,
2434 tr
, &ftrace_set_event_fops
);
2436 pr_warning("Could not create debugfs 'set_event' entry\n");
2440 d_events
= debugfs_create_dir("events", parent
);
2442 pr_warning("Could not create debugfs 'events' directory\n");
2446 /* ring buffer internal formats */
2447 trace_create_file("header_page", 0444, d_events
,
2448 ring_buffer_print_page_header
,
2449 &ftrace_show_header_fops
);
2451 trace_create_file("header_event", 0444, d_events
,
2452 ring_buffer_print_entry_header
,
2453 &ftrace_show_header_fops
);
2455 trace_create_file("enable", 0644, d_events
,
2456 tr
, &ftrace_tr_enable_fops
);
2458 tr
->event_dir
= d_events
;
2464 * event_trace_add_tracer - add a instance of a trace_array to events
2465 * @parent: The parent dentry to place the files/directories for events in
2466 * @tr: The trace array associated with these events
2468 * When a new instance is created, it needs to set up its events
2469 * directory, as well as other files associated with events. It also
2470 * creates the event hierachry in the @parent/events directory.
2472 * Returns 0 on success.
2474 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2478 mutex_lock(&event_mutex
);
2480 ret
= create_event_toplevel_files(parent
, tr
);
2484 down_write(&trace_event_sem
);
2485 __trace_add_event_dirs(tr
);
2486 up_write(&trace_event_sem
);
2489 mutex_unlock(&event_mutex
);
2495 * The top trace array already had its file descriptors created.
2496 * Now the files themselves need to be created.
2499 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2503 mutex_lock(&event_mutex
);
2505 ret
= create_event_toplevel_files(parent
, tr
);
2509 down_write(&trace_event_sem
);
2510 __trace_early_add_event_dirs(tr
);
2511 up_write(&trace_event_sem
);
2514 mutex_unlock(&event_mutex
);
2519 int event_trace_del_tracer(struct trace_array
*tr
)
2521 mutex_lock(&event_mutex
);
2523 /* Disable any running events */
2524 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2526 down_write(&trace_event_sem
);
2527 __trace_remove_event_dirs(tr
);
2528 debugfs_remove_recursive(tr
->event_dir
);
2529 up_write(&trace_event_sem
);
2531 tr
->event_dir
= NULL
;
2533 mutex_unlock(&event_mutex
);
2538 static __init
int event_trace_memsetup(void)
2540 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2541 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2545 static __init
int event_trace_enable(void)
2547 struct trace_array
*tr
= top_trace_array();
2548 struct ftrace_event_call
**iter
, *call
;
2549 char *buf
= bootup_event_buf
;
2553 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2556 ret
= event_init(call
);
2558 list_add(&call
->list
, &ftrace_events
);
2562 * We need the top trace array to have a working set of trace
2563 * points at early init, before the debug files and directories
2564 * are created. Create the file entries now, and attach them
2565 * to the actual file dentries later.
2567 __trace_early_add_events(tr
);
2570 token
= strsep(&buf
, ",");
2577 ret
= ftrace_set_clr_event(tr
, token
, 1);
2579 pr_warn("Failed to enable trace event: %s\n", token
);
2582 trace_printk_start_comm();
2584 register_event_cmds();
2589 static __init
int event_trace_init(void)
2591 struct trace_array
*tr
;
2592 struct dentry
*d_tracer
;
2593 struct dentry
*entry
;
2596 tr
= top_trace_array();
2598 d_tracer
= tracing_init_dentry();
2602 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2603 tr
, &ftrace_avail_fops
);
2605 pr_warning("Could not create debugfs "
2606 "'available_events' entry\n");
2608 if (trace_define_common_fields())
2609 pr_warning("tracing: Failed to allocate common fields");
2611 ret
= early_event_add_tracer(d_tracer
, tr
);
2615 #ifdef CONFIG_MTK_FTRACE_DEFAULT_ENABLE
2616 // enable ftrace facilities
2617 mt_ftrace_enable_disable(1);
2619 // only update buffer eariler if we want to collect boot-time ftrace
2620 // to avoid the boot time impacted by early-expanded ring buffer
2621 if(boot_time_ftrace
)
2622 tracing_update_buffers();
2624 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, 1);
2625 printk(KERN_INFO
"[ftrace]ftrace ready...\n");
2628 ret
= register_module_notifier(&trace_module_nb
);
2630 pr_warning("Failed to register trace events module notifier\n");
2634 early_initcall(event_trace_memsetup
);
2635 core_initcall(event_trace_enable
);
2636 fs_initcall(event_trace_init
);
2638 #ifdef CONFIG_FTRACE_STARTUP_TEST
2640 static DEFINE_SPINLOCK(test_spinlock
);
2641 static DEFINE_SPINLOCK(test_spinlock_irq
);
2642 static DEFINE_MUTEX(test_mutex
);
2644 static __init
void test_work(struct work_struct
*dummy
)
2646 spin_lock(&test_spinlock
);
2647 spin_lock_irq(&test_spinlock_irq
);
2649 spin_unlock_irq(&test_spinlock_irq
);
2650 spin_unlock(&test_spinlock
);
2652 mutex_lock(&test_mutex
);
2654 mutex_unlock(&test_mutex
);
2657 static __init
int event_test_thread(void *unused
)
2661 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2663 pr_info("failed to kmalloc\n");
2665 schedule_on_each_cpu(test_work
);
2669 set_current_state(TASK_INTERRUPTIBLE
);
2670 while (!kthread_should_stop())
2677 * Do various things that may trigger events.
2679 static __init
void event_test_stuff(void)
2681 struct task_struct
*test_thread
;
2683 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2685 kthread_stop(test_thread
);
2689 * For every trace event defined, we will test each trace point separately,
2690 * and then by groups, and finally all trace points.
2692 static __init
void event_trace_self_tests(void)
2694 struct ftrace_subsystem_dir
*dir
;
2695 struct ftrace_event_file
*file
;
2696 struct ftrace_event_call
*call
;
2697 struct event_subsystem
*system
;
2698 struct trace_array
*tr
;
2701 tr
= top_trace_array();
2703 pr_info("Running tests on trace events:\n");
2705 list_for_each_entry(file
, &tr
->events
, list
) {
2707 call
= file
->event_call
;
2709 /* Only test those that have a probe */
2710 if (!call
->class || !call
->class->probe
)
2714 * Testing syscall events here is pretty useless, but
2715 * we still do it if configured. But this is time consuming.
2716 * What we really need is a user thread to perform the
2717 * syscalls as we test.
2719 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2720 if (call
->class->system
&&
2721 strcmp(call
->class->system
, "syscalls") == 0)
2725 pr_info("Testing event %s: ", call
->name
);
2728 * If an event is already enabled, someone is using
2729 * it and the self test should not be on.
2731 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2732 pr_warning("Enabled event during self test!\n");
2737 ftrace_event_enable_disable(file
, 1);
2739 ftrace_event_enable_disable(file
, 0);
2744 /* Now test at the sub system level */
2746 pr_info("Running tests on trace event systems:\n");
2748 list_for_each_entry(dir
, &tr
->systems
, list
) {
2750 system
= dir
->subsystem
;
2752 /* the ftrace system is special, skip it */
2753 if (strcmp(system
->name
, "ftrace") == 0)
2756 pr_info("Testing event system %s: ", system
->name
);
2758 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2759 if (WARN_ON_ONCE(ret
)) {
2760 pr_warning("error enabling system %s\n",
2767 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2768 if (WARN_ON_ONCE(ret
)) {
2769 pr_warning("error disabling system %s\n",
2777 /* Test with all events enabled */
2779 pr_info("Running tests on all trace events:\n");
2780 pr_info("Testing all events: ");
2782 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2783 if (WARN_ON_ONCE(ret
)) {
2784 pr_warning("error enabling all events\n");
2791 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2792 if (WARN_ON_ONCE(ret
)) {
2793 pr_warning("error disabling all events\n");
2800 #ifdef CONFIG_FUNCTION_TRACER
2802 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2805 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2806 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2808 struct ring_buffer_event
*event
;
2809 struct ring_buffer
*buffer
;
2810 struct ftrace_entry
*entry
;
2811 unsigned long flags
;
2816 pc
= preempt_count();
2817 preempt_disable_notrace();
2818 cpu
= raw_smp_processor_id();
2819 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2824 local_save_flags(flags
);
2826 event
= trace_current_buffer_lock_reserve(&buffer
,
2827 TRACE_FN
, sizeof(*entry
),
2831 entry
= ring_buffer_event_data(event
);
2833 entry
->parent_ip
= parent_ip
;
2835 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2838 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2839 preempt_enable_notrace();
2842 static struct ftrace_ops trace_ops __initdata
=
2844 .func
= function_test_events_call
,
2845 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2848 static __init
void event_trace_self_test_with_function(void)
2851 ret
= register_ftrace_function(&trace_ops
);
2852 if (WARN_ON(ret
< 0)) {
2853 pr_info("Failed to enable function tracer for event tests\n");
2856 pr_info("Running tests again, along with the function tracer\n");
2857 event_trace_self_tests();
2858 unregister_ftrace_function(&trace_ops
);
2861 static __init
void event_trace_self_test_with_function(void)
2866 static __init
int event_trace_self_tests_init(void)
2868 if (!tracing_selftest_disabled
) {
2869 event_trace_self_tests();
2870 event_trace_self_test_with_function();
2876 late_initcall(event_trace_self_tests_init
);