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 #define pr_fmt(fmt) fmt
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/debugfs.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
23 #include <asm/setup.h>
25 #include "trace_output.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 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer
*fbuffer
,
194 struct ftrace_event_file
*ftrace_file
,
197 struct ftrace_event_call
*event_call
= ftrace_file
->event_call
;
199 local_save_flags(fbuffer
->flags
);
200 fbuffer
->pc
= preempt_count();
201 fbuffer
->ftrace_file
= ftrace_file
;
204 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, ftrace_file
,
205 event_call
->event
.type
, len
,
206 fbuffer
->flags
, fbuffer
->pc
);
210 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
211 return fbuffer
->entry
;
213 EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve
);
215 void ftrace_event_buffer_commit(struct ftrace_event_buffer
*fbuffer
)
217 event_trigger_unlock_commit(fbuffer
->ftrace_file
, fbuffer
->buffer
,
218 fbuffer
->event
, fbuffer
->entry
,
219 fbuffer
->flags
, fbuffer
->pc
);
221 EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit
);
223 int ftrace_event_reg(struct ftrace_event_call
*call
,
224 enum trace_reg type
, void *data
)
226 struct ftrace_event_file
*file
= data
;
228 WARN_ON(!(call
->flags
& TRACE_EVENT_FL_TRACEPOINT
));
230 case TRACE_REG_REGISTER
:
231 return tracepoint_probe_register(call
->tp
,
234 case TRACE_REG_UNREGISTER
:
235 tracepoint_probe_unregister(call
->tp
,
240 #ifdef CONFIG_PERF_EVENTS
241 case TRACE_REG_PERF_REGISTER
:
242 return tracepoint_probe_register(call
->tp
,
243 call
->class->perf_probe
,
245 case TRACE_REG_PERF_UNREGISTER
:
246 tracepoint_probe_unregister(call
->tp
,
247 call
->class->perf_probe
,
250 case TRACE_REG_PERF_OPEN
:
251 case TRACE_REG_PERF_CLOSE
:
252 case TRACE_REG_PERF_ADD
:
253 case TRACE_REG_PERF_DEL
:
259 EXPORT_SYMBOL_GPL(ftrace_event_reg
);
261 void trace_event_enable_cmd_record(bool enable
)
263 struct ftrace_event_file
*file
;
264 struct trace_array
*tr
;
266 mutex_lock(&event_mutex
);
267 do_for_each_event_file(tr
, file
) {
269 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
))
273 tracing_start_cmdline_record();
274 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
276 tracing_stop_cmdline_record();
277 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
279 } while_for_each_event_file();
280 mutex_unlock(&event_mutex
);
283 static int __ftrace_event_enable_disable(struct ftrace_event_file
*file
,
284 int enable
, int soft_disable
)
286 struct ftrace_event_call
*call
= file
->event_call
;
293 * When soft_disable is set and enable is cleared, the sm_ref
294 * reference counter is decremented. If it reaches 0, we want
295 * to clear the SOFT_DISABLED flag but leave the event in the
296 * state that it was. That is, if the event was enabled and
297 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
298 * is set we do not want the event to be enabled before we
301 * When soft_disable is not set but the SOFT_MODE flag is,
302 * we do nothing. Do not disable the tracepoint, otherwise
303 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
306 if (atomic_dec_return(&file
->sm_ref
) > 0)
308 disable
= file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
;
309 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
311 disable
= !(file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
);
313 if (disable
&& (file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
314 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
315 if (file
->flags
& FTRACE_EVENT_FL_RECORDED_CMD
) {
316 tracing_stop_cmdline_record();
317 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
319 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
321 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
322 if (file
->flags
& FTRACE_EVENT_FL_SOFT_MODE
)
323 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
325 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
329 * When soft_disable is set and enable is set, we want to
330 * register the tracepoint for the event, but leave the event
331 * as is. That means, if the event was already enabled, we do
332 * nothing (but set SOFT_MODE). If the event is disabled, we
333 * set SOFT_DISABLED before enabling the event tracepoint, so
334 * it still seems to be disabled.
337 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
339 if (atomic_inc_return(&file
->sm_ref
) > 1)
341 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT
, &file
->flags
);
344 if (!(file
->flags
& FTRACE_EVENT_FL_ENABLED
)) {
346 /* Keep the event disabled, when going to SOFT_MODE. */
348 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &file
->flags
);
350 if (trace_flags
& TRACE_ITER_RECORD_CMD
) {
351 tracing_start_cmdline_record();
352 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT
, &file
->flags
);
354 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
356 tracing_stop_cmdline_record();
357 pr_info("event trace: Could not enable event "
358 "%s\n", ftrace_event_name(call
));
361 set_bit(FTRACE_EVENT_FL_ENABLED_BIT
, &file
->flags
);
363 /* WAS_ENABLED gets set but never cleared. */
364 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
372 int trace_event_enable_disable(struct ftrace_event_file
*file
,
373 int enable
, int soft_disable
)
375 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
378 static int ftrace_event_enable_disable(struct ftrace_event_file
*file
,
381 return __ftrace_event_enable_disable(file
, enable
, 0);
384 static void ftrace_clear_events(struct trace_array
*tr
)
386 struct ftrace_event_file
*file
;
388 mutex_lock(&event_mutex
);
389 list_for_each_entry(file
, &tr
->events
, list
) {
390 ftrace_event_enable_disable(file
, 0);
392 mutex_unlock(&event_mutex
);
395 static void __put_system(struct event_subsystem
*system
)
397 struct event_filter
*filter
= system
->filter
;
399 WARN_ON_ONCE(system_refcount(system
) == 0);
400 if (system_refcount_dec(system
))
403 list_del(&system
->list
);
406 kfree(filter
->filter_string
);
409 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
414 static void __get_system(struct event_subsystem
*system
)
416 WARN_ON_ONCE(system_refcount(system
) == 0);
417 system_refcount_inc(system
);
420 static void __get_system_dir(struct ftrace_subsystem_dir
*dir
)
422 WARN_ON_ONCE(dir
->ref_count
== 0);
424 __get_system(dir
->subsystem
);
427 static void __put_system_dir(struct ftrace_subsystem_dir
*dir
)
429 WARN_ON_ONCE(dir
->ref_count
== 0);
430 /* If the subsystem is about to be freed, the dir must be too */
431 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
433 __put_system(dir
->subsystem
);
434 if (!--dir
->ref_count
)
438 static void put_system(struct ftrace_subsystem_dir
*dir
)
440 mutex_lock(&event_mutex
);
441 __put_system_dir(dir
);
442 mutex_unlock(&event_mutex
);
445 static void remove_subsystem(struct ftrace_subsystem_dir
*dir
)
450 if (!--dir
->nr_events
) {
451 debugfs_remove_recursive(dir
->entry
);
452 list_del(&dir
->list
);
453 __put_system_dir(dir
);
457 static void remove_event_file_dir(struct ftrace_event_file
*file
)
459 struct dentry
*dir
= file
->dir
;
460 struct dentry
*child
;
463 spin_lock(&dir
->d_lock
); /* probably unneeded */
464 list_for_each_entry(child
, &dir
->d_subdirs
, d_u
.d_child
) {
465 if (child
->d_inode
) /* probably unneeded */
466 child
->d_inode
->i_private
= NULL
;
468 spin_unlock(&dir
->d_lock
);
470 debugfs_remove_recursive(dir
);
473 list_del(&file
->list
);
474 remove_subsystem(file
->system
);
475 free_event_filter(file
->filter
);
476 kmem_cache_free(file_cachep
, file
);
480 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
483 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
484 const char *sub
, const char *event
, int set
)
486 struct ftrace_event_file
*file
;
487 struct ftrace_event_call
*call
;
491 list_for_each_entry(file
, &tr
->events
, list
) {
493 call
= file
->event_call
;
494 name
= ftrace_event_name(call
);
496 if (!name
|| !call
->class || !call
->class->reg
)
499 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
503 strcmp(match
, name
) != 0 &&
504 strcmp(match
, call
->class->system
) != 0)
507 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
510 if (event
&& strcmp(event
, name
) != 0)
513 ftrace_event_enable_disable(file
, set
);
521 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
522 const char *sub
, const char *event
, int set
)
526 mutex_lock(&event_mutex
);
527 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
528 mutex_unlock(&event_mutex
);
533 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
535 char *event
= NULL
, *sub
= NULL
, *match
;
538 * The buf format can be <subsystem>:<event-name>
539 * *:<event-name> means any event by that name.
540 * :<event-name> is the same.
542 * <subsystem>:* means all events in that subsystem
543 * <subsystem>: means the same.
545 * <name> (no ':') means all events in a subsystem with
546 * the name <name> or any event that matches <name>
549 match
= strsep(&buf
, ":");
555 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
557 if (!strlen(event
) || strcmp(event
, "*") == 0)
561 return __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
565 * trace_set_clr_event - enable or disable an event
566 * @system: system name to match (NULL for any system)
567 * @event: event name to match (NULL for all events, within system)
568 * @set: 1 to enable, 0 to disable
570 * This is a way for other parts of the kernel to enable or disable
573 * Returns 0 on success, -EINVAL if the parameters do not match any
576 int trace_set_clr_event(const char *system
, const char *event
, int set
)
578 struct trace_array
*tr
= top_trace_array();
583 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
585 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
587 /* 128 should be much more than enough */
588 #define EVENT_BUF_SIZE 127
591 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
592 size_t cnt
, loff_t
*ppos
)
594 struct trace_parser parser
;
595 struct seq_file
*m
= file
->private_data
;
596 struct trace_array
*tr
= m
->private;
602 ret
= tracing_update_buffers();
606 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
609 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
611 if (read
>= 0 && trace_parser_loaded((&parser
))) {
614 if (*parser
.buffer
== '!')
617 parser
.buffer
[parser
.idx
] = 0;
619 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
627 trace_parser_put(&parser
);
633 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
635 struct ftrace_event_file
*file
= v
;
636 struct ftrace_event_call
*call
;
637 struct trace_array
*tr
= m
->private;
641 list_for_each_entry_continue(file
, &tr
->events
, list
) {
642 call
= file
->event_call
;
644 * The ftrace subsystem is for showing formats only.
645 * They can not be enabled or disabled via the event files.
647 if (call
->class && call
->class->reg
)
654 static void *t_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
= t_next(m
, file
, &l
);
672 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
674 struct ftrace_event_file
*file
= v
;
675 struct trace_array
*tr
= m
->private;
679 list_for_each_entry_continue(file
, &tr
->events
, list
) {
680 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
687 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
689 struct ftrace_event_file
*file
;
690 struct trace_array
*tr
= m
->private;
693 mutex_lock(&event_mutex
);
695 file
= list_entry(&tr
->events
, struct ftrace_event_file
, list
);
696 for (l
= 0; l
<= *pos
; ) {
697 file
= s_next(m
, file
, &l
);
704 static int t_show(struct seq_file
*m
, void *v
)
706 struct ftrace_event_file
*file
= v
;
707 struct ftrace_event_call
*call
= file
->event_call
;
709 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
710 seq_printf(m
, "%s:", call
->class->system
);
711 seq_printf(m
, "%s\n", ftrace_event_name(call
));
716 static void t_stop(struct seq_file
*m
, void *p
)
718 mutex_unlock(&event_mutex
);
722 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
725 struct ftrace_event_file
*file
;
729 mutex_lock(&event_mutex
);
730 file
= event_file_data(filp
);
733 mutex_unlock(&event_mutex
);
738 if (flags
& FTRACE_EVENT_FL_ENABLED
&&
739 !(flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
742 if (flags
& FTRACE_EVENT_FL_SOFT_DISABLED
||
743 flags
& FTRACE_EVENT_FL_SOFT_MODE
)
748 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
752 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
755 struct ftrace_event_file
*file
;
759 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
763 ret
= tracing_update_buffers();
771 mutex_lock(&event_mutex
);
772 file
= event_file_data(filp
);
774 ret
= ftrace_event_enable_disable(file
, val
);
775 mutex_unlock(&event_mutex
);
784 return ret
? ret
: cnt
;
788 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
791 const char set_to_char
[4] = { '?', '0', '1', 'X' };
792 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
793 struct event_subsystem
*system
= dir
->subsystem
;
794 struct ftrace_event_call
*call
;
795 struct ftrace_event_file
*file
;
796 struct trace_array
*tr
= dir
->tr
;
801 mutex_lock(&event_mutex
);
802 list_for_each_entry(file
, &tr
->events
, list
) {
803 call
= file
->event_call
;
804 if (!ftrace_event_name(call
) || !call
->class || !call
->class->reg
)
807 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
811 * We need to find out if all the events are set
812 * or if all events or cleared, or if we have
815 set
|= (1 << !!(file
->flags
& FTRACE_EVENT_FL_ENABLED
));
818 * If we have a mixture, no need to look further.
823 mutex_unlock(&event_mutex
);
825 buf
[0] = set_to_char
[set
];
828 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
834 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
837 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
838 struct event_subsystem
*system
= dir
->subsystem
;
839 const char *name
= NULL
;
843 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
847 ret
= tracing_update_buffers();
851 if (val
!= 0 && val
!= 1)
855 * Opening of "enable" adds a ref count to system,
856 * so the name is safe to use.
861 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
875 FORMAT_FIELD_SEPERATOR
= 2,
879 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
881 struct ftrace_event_call
*call
= event_file_data(m
->private);
882 struct list_head
*common_head
= &ftrace_common_fields
;
883 struct list_head
*head
= trace_get_fields(call
);
884 struct list_head
*node
= v
;
888 switch ((unsigned long)v
) {
893 case FORMAT_FIELD_SEPERATOR
:
897 case FORMAT_PRINTFMT
:
903 if (node
== common_head
)
904 return (void *)FORMAT_FIELD_SEPERATOR
;
905 else if (node
== head
)
906 return (void *)FORMAT_PRINTFMT
;
911 static int f_show(struct seq_file
*m
, void *v
)
913 struct ftrace_event_call
*call
= event_file_data(m
->private);
914 struct ftrace_event_field
*field
;
915 const char *array_descriptor
;
917 switch ((unsigned long)v
) {
919 seq_printf(m
, "name: %s\n", ftrace_event_name(call
));
920 seq_printf(m
, "ID: %d\n", call
->event
.type
);
921 seq_printf(m
, "format:\n");
924 case FORMAT_FIELD_SEPERATOR
:
928 case FORMAT_PRINTFMT
:
929 seq_printf(m
, "\nprint fmt: %s\n",
934 field
= list_entry(v
, struct ftrace_event_field
, link
);
936 * Smartly shows the array type(except dynamic array).
939 * If TYPE := TYPE[LEN], it is shown:
940 * field:TYPE VAR[LEN]
942 array_descriptor
= strchr(field
->type
, '[');
944 if (!strncmp(field
->type
, "__data_loc", 10))
945 array_descriptor
= NULL
;
947 if (!array_descriptor
)
948 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
949 field
->type
, field
->name
, field
->offset
,
950 field
->size
, !!field
->is_signed
);
952 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
953 (int)(array_descriptor
- field
->type
),
954 field
->type
, field
->name
,
955 array_descriptor
, field
->offset
,
956 field
->size
, !!field
->is_signed
);
961 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
963 void *p
= (void *)FORMAT_HEADER
;
966 /* ->stop() is called even if ->start() fails */
967 mutex_lock(&event_mutex
);
968 if (!event_file_data(m
->private))
969 return ERR_PTR(-ENODEV
);
971 while (l
< *pos
&& p
)
972 p
= f_next(m
, p
, &l
);
977 static void f_stop(struct seq_file
*m
, void *p
)
979 mutex_unlock(&event_mutex
);
982 static const struct seq_operations trace_format_seq_ops
= {
989 static int trace_format_open(struct inode
*inode
, struct file
*file
)
994 ret
= seq_open(file
, &trace_format_seq_ops
);
998 m
= file
->private_data
;
1005 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1007 int id
= (long)event_file_data(filp
);
1017 len
= sprintf(buf
, "%d\n", id
);
1019 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1023 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1026 struct ftrace_event_file
*file
;
1027 struct trace_seq
*s
;
1033 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1040 mutex_lock(&event_mutex
);
1041 file
= event_file_data(filp
);
1043 print_event_filter(file
, s
);
1044 mutex_unlock(&event_mutex
);
1047 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1055 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1058 struct ftrace_event_file
*file
;
1062 if (cnt
>= PAGE_SIZE
)
1065 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1069 if (copy_from_user(buf
, ubuf
, cnt
)) {
1070 free_page((unsigned long) buf
);
1075 mutex_lock(&event_mutex
);
1076 file
= event_file_data(filp
);
1078 err
= apply_event_filter(file
, buf
);
1079 mutex_unlock(&event_mutex
);
1081 free_page((unsigned long) buf
);
1090 static LIST_HEAD(event_subsystems
);
1092 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1094 struct event_subsystem
*system
= NULL
;
1095 struct ftrace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1096 struct trace_array
*tr
;
1099 if (tracing_is_disabled())
1102 /* Make sure the system still exists */
1103 mutex_lock(&trace_types_lock
);
1104 mutex_lock(&event_mutex
);
1105 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1106 list_for_each_entry(dir
, &tr
->systems
, list
) {
1107 if (dir
== inode
->i_private
) {
1108 /* Don't open systems with no events */
1109 if (dir
->nr_events
) {
1110 __get_system_dir(dir
);
1111 system
= dir
->subsystem
;
1118 mutex_unlock(&event_mutex
);
1119 mutex_unlock(&trace_types_lock
);
1124 /* Some versions of gcc think dir can be uninitialized here */
1127 /* Still need to increment the ref count of the system */
1128 if (trace_array_get(tr
) < 0) {
1133 ret
= tracing_open_generic(inode
, filp
);
1135 trace_array_put(tr
);
1142 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1144 struct ftrace_subsystem_dir
*dir
;
1145 struct trace_array
*tr
= inode
->i_private
;
1148 if (tracing_is_disabled())
1151 if (trace_array_get(tr
) < 0)
1154 /* Make a temporary dir that has no system but points to tr */
1155 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1157 trace_array_put(tr
);
1163 ret
= tracing_open_generic(inode
, filp
);
1165 trace_array_put(tr
);
1170 filp
->private_data
= dir
;
1175 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1177 struct ftrace_subsystem_dir
*dir
= file
->private_data
;
1179 trace_array_put(dir
->tr
);
1182 * If dir->subsystem is NULL, then this is a temporary
1183 * descriptor that was made for a trace_array to enable
1195 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1198 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1199 struct event_subsystem
*system
= dir
->subsystem
;
1200 struct trace_seq
*s
;
1206 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1212 print_subsystem_event_filter(system
, s
);
1213 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1221 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1224 struct ftrace_subsystem_dir
*dir
= filp
->private_data
;
1228 if (cnt
>= PAGE_SIZE
)
1231 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
1235 if (copy_from_user(buf
, ubuf
, cnt
)) {
1236 free_page((unsigned long) buf
);
1241 err
= apply_subsystem_event_filter(dir
, buf
);
1242 free_page((unsigned long) buf
);
1252 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1254 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1255 struct trace_seq
*s
;
1261 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1268 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
1275 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1276 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1277 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1279 static const struct seq_operations show_event_seq_ops
= {
1286 static const struct seq_operations show_set_event_seq_ops
= {
1293 static const struct file_operations ftrace_avail_fops
= {
1294 .open
= ftrace_event_avail_open
,
1296 .llseek
= seq_lseek
,
1297 .release
= seq_release
,
1300 static const struct file_operations ftrace_set_event_fops
= {
1301 .open
= ftrace_event_set_open
,
1303 .write
= ftrace_event_write
,
1304 .llseek
= seq_lseek
,
1305 .release
= ftrace_event_release
,
1308 static const struct file_operations ftrace_enable_fops
= {
1309 .open
= tracing_open_generic
,
1310 .read
= event_enable_read
,
1311 .write
= event_enable_write
,
1312 .llseek
= default_llseek
,
1315 static const struct file_operations ftrace_event_format_fops
= {
1316 .open
= trace_format_open
,
1318 .llseek
= seq_lseek
,
1319 .release
= seq_release
,
1322 static const struct file_operations ftrace_event_id_fops
= {
1323 .read
= event_id_read
,
1324 .llseek
= default_llseek
,
1327 static const struct file_operations ftrace_event_filter_fops
= {
1328 .open
= tracing_open_generic
,
1329 .read
= event_filter_read
,
1330 .write
= event_filter_write
,
1331 .llseek
= default_llseek
,
1334 static const struct file_operations ftrace_subsystem_filter_fops
= {
1335 .open
= subsystem_open
,
1336 .read
= subsystem_filter_read
,
1337 .write
= subsystem_filter_write
,
1338 .llseek
= default_llseek
,
1339 .release
= subsystem_release
,
1342 static const struct file_operations ftrace_system_enable_fops
= {
1343 .open
= subsystem_open
,
1344 .read
= system_enable_read
,
1345 .write
= system_enable_write
,
1346 .llseek
= default_llseek
,
1347 .release
= subsystem_release
,
1350 static const struct file_operations ftrace_tr_enable_fops
= {
1351 .open
= system_tr_open
,
1352 .read
= system_enable_read
,
1353 .write
= system_enable_write
,
1354 .llseek
= default_llseek
,
1355 .release
= subsystem_release
,
1358 static const struct file_operations ftrace_show_header_fops
= {
1359 .open
= tracing_open_generic
,
1360 .read
= show_header
,
1361 .llseek
= default_llseek
,
1365 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1366 const struct seq_operations
*seq_ops
)
1371 ret
= seq_open(file
, seq_ops
);
1374 m
= file
->private_data
;
1375 /* copy tr over to seq ops */
1376 m
->private = inode
->i_private
;
1381 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1383 struct trace_array
*tr
= inode
->i_private
;
1385 trace_array_put(tr
);
1387 return seq_release(inode
, file
);
1391 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1393 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1395 return ftrace_event_open(inode
, file
, seq_ops
);
1399 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1401 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1402 struct trace_array
*tr
= inode
->i_private
;
1405 if (trace_array_get(tr
) < 0)
1408 if ((file
->f_mode
& FMODE_WRITE
) &&
1409 (file
->f_flags
& O_TRUNC
))
1410 ftrace_clear_events(tr
);
1412 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1414 trace_array_put(tr
);
1418 static struct event_subsystem
*
1419 create_new_subsystem(const char *name
)
1421 struct event_subsystem
*system
;
1423 /* need to create new entry */
1424 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1428 system
->ref_count
= 1;
1430 /* Only allocate if dynamic (kprobes and modules) */
1431 if (!core_kernel_data((unsigned long)name
)) {
1432 system
->ref_count
|= SYSTEM_FL_FREE_NAME
;
1433 system
->name
= kstrdup(name
, GFP_KERNEL
);
1437 system
->name
= name
;
1439 system
->filter
= NULL
;
1441 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1442 if (!system
->filter
)
1445 list_add(&system
->list
, &event_subsystems
);
1450 if (system
->ref_count
& SYSTEM_FL_FREE_NAME
)
1451 kfree(system
->name
);
1456 static struct dentry
*
1457 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1458 struct ftrace_event_file
*file
, struct dentry
*parent
)
1460 struct ftrace_subsystem_dir
*dir
;
1461 struct event_subsystem
*system
;
1462 struct dentry
*entry
;
1464 /* First see if we did not already create this dir */
1465 list_for_each_entry(dir
, &tr
->systems
, list
) {
1466 system
= dir
->subsystem
;
1467 if (strcmp(system
->name
, name
) == 0) {
1474 /* Now see if the system itself exists. */
1475 list_for_each_entry(system
, &event_subsystems
, list
) {
1476 if (strcmp(system
->name
, name
) == 0)
1479 /* Reset system variable when not found */
1480 if (&system
->list
== &event_subsystems
)
1483 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1488 system
= create_new_subsystem(name
);
1492 __get_system(system
);
1494 dir
->entry
= debugfs_create_dir(name
, parent
);
1496 pr_warn("Failed to create system directory %s\n", name
);
1497 __put_system(system
);
1504 dir
->subsystem
= system
;
1507 entry
= debugfs_create_file("filter", 0644, dir
->entry
, dir
,
1508 &ftrace_subsystem_filter_fops
);
1510 kfree(system
->filter
);
1511 system
->filter
= NULL
;
1512 pr_warn("Could not create debugfs '%s/filter' entry\n", name
);
1515 trace_create_file("enable", 0644, dir
->entry
, dir
,
1516 &ftrace_system_enable_fops
);
1518 list_add(&dir
->list
, &tr
->systems
);
1525 /* Only print this message if failed on memory allocation */
1526 if (!dir
|| !system
)
1527 pr_warn("No memory to create event subsystem %s\n", name
);
1532 event_create_dir(struct dentry
*parent
, struct ftrace_event_file
*file
)
1534 struct ftrace_event_call
*call
= file
->event_call
;
1535 struct trace_array
*tr
= file
->tr
;
1536 struct list_head
*head
;
1537 struct dentry
*d_events
;
1542 * If the trace point header did not define TRACE_SYSTEM
1543 * then the system would be called "TRACE_SYSTEM".
1545 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1546 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1552 name
= ftrace_event_name(call
);
1553 file
->dir
= debugfs_create_dir(name
, d_events
);
1555 pr_warn("Could not create debugfs '%s' directory\n", name
);
1559 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
1560 trace_create_file("enable", 0644, file
->dir
, file
,
1561 &ftrace_enable_fops
);
1563 #ifdef CONFIG_PERF_EVENTS
1564 if (call
->event
.type
&& call
->class->reg
)
1565 trace_create_file("id", 0444, file
->dir
,
1566 (void *)(long)call
->event
.type
,
1567 &ftrace_event_id_fops
);
1571 * Other events may have the same class. Only update
1572 * the fields if they are not already defined.
1574 head
= trace_get_fields(call
);
1575 if (list_empty(head
)) {
1576 ret
= call
->class->define_fields(call
);
1578 pr_warn("Could not initialize trace point events/%s\n",
1583 trace_create_file("filter", 0644, file
->dir
, file
,
1584 &ftrace_event_filter_fops
);
1586 trace_create_file("trigger", 0644, file
->dir
, file
,
1587 &event_trigger_fops
);
1589 trace_create_file("format", 0444, file
->dir
, call
,
1590 &ftrace_event_format_fops
);
1595 static void remove_event_from_tracers(struct ftrace_event_call
*call
)
1597 struct ftrace_event_file
*file
;
1598 struct trace_array
*tr
;
1600 do_for_each_event_file_safe(tr
, file
) {
1601 if (file
->event_call
!= call
)
1604 remove_event_file_dir(file
);
1606 * The do_for_each_event_file_safe() 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();
1615 static void event_remove(struct ftrace_event_call
*call
)
1617 struct trace_array
*tr
;
1618 struct ftrace_event_file
*file
;
1620 do_for_each_event_file(tr
, file
) {
1621 if (file
->event_call
!= call
)
1623 ftrace_event_enable_disable(file
, 0);
1625 * The do_for_each_event_file() is
1626 * a double loop. After finding the call for this
1627 * trace_array, we use break to jump to the next
1631 } while_for_each_event_file();
1633 if (call
->event
.funcs
)
1634 __unregister_ftrace_event(&call
->event
);
1635 remove_event_from_tracers(call
);
1636 list_del(&call
->list
);
1639 static int event_init(struct ftrace_event_call
*call
)
1644 name
= ftrace_event_name(call
);
1648 if (call
->class->raw_init
) {
1649 ret
= call
->class->raw_init(call
);
1650 if (ret
< 0 && ret
!= -ENOSYS
)
1651 pr_warn("Could not initialize trace events/%s\n", name
);
1658 __register_event(struct ftrace_event_call
*call
, struct module
*mod
)
1662 ret
= event_init(call
);
1666 list_add(&call
->list
, &ftrace_events
);
1672 static struct ftrace_event_file
*
1673 trace_create_new_event(struct ftrace_event_call
*call
,
1674 struct trace_array
*tr
)
1676 struct ftrace_event_file
*file
;
1678 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
1682 file
->event_call
= call
;
1684 atomic_set(&file
->sm_ref
, 0);
1685 atomic_set(&file
->tm_ref
, 0);
1686 INIT_LIST_HEAD(&file
->triggers
);
1687 list_add(&file
->list
, &tr
->events
);
1692 /* Add an event to a trace directory */
1694 __trace_add_new_event(struct ftrace_event_call
*call
, struct trace_array
*tr
)
1696 struct ftrace_event_file
*file
;
1698 file
= trace_create_new_event(call
, tr
);
1702 return event_create_dir(tr
->event_dir
, file
);
1706 * Just create a decriptor for early init. A descriptor is required
1707 * for enabling events at boot. We want to enable events before
1708 * the filesystem is initialized.
1711 __trace_early_add_new_event(struct ftrace_event_call
*call
,
1712 struct trace_array
*tr
)
1714 struct ftrace_event_file
*file
;
1716 file
= trace_create_new_event(call
, tr
);
1723 struct ftrace_module_file_ops
;
1724 static void __add_event_to_tracers(struct ftrace_event_call
*call
);
1726 /* Add an additional event_call dynamically */
1727 int trace_add_event_call(struct ftrace_event_call
*call
)
1730 mutex_lock(&trace_types_lock
);
1731 mutex_lock(&event_mutex
);
1733 ret
= __register_event(call
, NULL
);
1735 __add_event_to_tracers(call
);
1737 mutex_unlock(&event_mutex
);
1738 mutex_unlock(&trace_types_lock
);
1743 * Must be called under locking of trace_types_lock, event_mutex and
1746 static void __trace_remove_event_call(struct ftrace_event_call
*call
)
1749 trace_destroy_fields(call
);
1750 free_event_filter(call
->filter
);
1751 call
->filter
= NULL
;
1754 static int probe_remove_event_call(struct ftrace_event_call
*call
)
1756 struct trace_array
*tr
;
1757 struct ftrace_event_file
*file
;
1759 #ifdef CONFIG_PERF_EVENTS
1760 if (call
->perf_refcount
)
1763 do_for_each_event_file(tr
, file
) {
1764 if (file
->event_call
!= call
)
1767 * We can't rely on ftrace_event_enable_disable(enable => 0)
1768 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1769 * TRACE_REG_UNREGISTER.
1771 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
)
1774 * The do_for_each_event_file_safe() is
1775 * a double loop. After finding the call for this
1776 * trace_array, we use break to jump to the next
1780 } while_for_each_event_file();
1782 __trace_remove_event_call(call
);
1787 /* Remove an event_call */
1788 int trace_remove_event_call(struct ftrace_event_call
*call
)
1792 mutex_lock(&trace_types_lock
);
1793 mutex_lock(&event_mutex
);
1794 down_write(&trace_event_sem
);
1795 ret
= probe_remove_event_call(call
);
1796 up_write(&trace_event_sem
);
1797 mutex_unlock(&event_mutex
);
1798 mutex_unlock(&trace_types_lock
);
1803 #define for_each_event(event, start, end) \
1804 for (event = start; \
1805 (unsigned long)event < (unsigned long)end; \
1808 #ifdef CONFIG_MODULES
1810 static void trace_module_add_events(struct module
*mod
)
1812 struct ftrace_event_call
**call
, **start
, **end
;
1814 if (!mod
->num_trace_events
)
1817 /* Don't add infrastructure for mods without tracepoints */
1818 if (trace_module_has_bad_taint(mod
)) {
1819 pr_err("%s: module has bad taint, not creating trace events\n",
1824 start
= mod
->trace_events
;
1825 end
= mod
->trace_events
+ mod
->num_trace_events
;
1827 for_each_event(call
, start
, end
) {
1828 __register_event(*call
, mod
);
1829 __add_event_to_tracers(*call
);
1833 static void trace_module_remove_events(struct module
*mod
)
1835 struct ftrace_event_call
*call
, *p
;
1836 bool clear_trace
= false;
1838 down_write(&trace_event_sem
);
1839 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
1840 if (call
->mod
== mod
) {
1841 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
1843 __trace_remove_event_call(call
);
1846 up_write(&trace_event_sem
);
1849 * It is safest to reset the ring buffer if the module being unloaded
1850 * registered any events that were used. The only worry is if
1851 * a new module gets loaded, and takes on the same id as the events
1852 * of this module. When printing out the buffer, traced events left
1853 * over from this module may be passed to the new module events and
1854 * unexpected results may occur.
1857 tracing_reset_all_online_cpus();
1860 static int trace_module_notify(struct notifier_block
*self
,
1861 unsigned long val
, void *data
)
1863 struct module
*mod
= data
;
1865 mutex_lock(&trace_types_lock
);
1866 mutex_lock(&event_mutex
);
1868 case MODULE_STATE_COMING
:
1869 trace_module_add_events(mod
);
1871 case MODULE_STATE_GOING
:
1872 trace_module_remove_events(mod
);
1875 mutex_unlock(&event_mutex
);
1876 mutex_unlock(&trace_types_lock
);
1881 static struct notifier_block trace_module_nb
= {
1882 .notifier_call
= trace_module_notify
,
1885 #endif /* CONFIG_MODULES */
1887 /* Create a new event directory structure for a trace directory. */
1889 __trace_add_event_dirs(struct trace_array
*tr
)
1891 struct ftrace_event_call
*call
;
1894 list_for_each_entry(call
, &ftrace_events
, list
) {
1895 ret
= __trace_add_new_event(call
, tr
);
1897 pr_warn("Could not create directory for event %s\n",
1898 ftrace_event_name(call
));
1902 struct ftrace_event_file
*
1903 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
1905 struct ftrace_event_file
*file
;
1906 struct ftrace_event_call
*call
;
1909 list_for_each_entry(file
, &tr
->events
, list
) {
1911 call
= file
->event_call
;
1912 name
= ftrace_event_name(call
);
1914 if (!name
|| !call
->class || !call
->class->reg
)
1917 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
1920 if (strcmp(event
, name
) == 0 &&
1921 strcmp(system
, call
->class->system
) == 0)
1927 #ifdef CONFIG_DYNAMIC_FTRACE
1930 #define ENABLE_EVENT_STR "enable_event"
1931 #define DISABLE_EVENT_STR "disable_event"
1933 struct event_probe_data
{
1934 struct ftrace_event_file
*file
;
1935 unsigned long count
;
1941 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1943 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1944 struct event_probe_data
*data
= *pdata
;
1950 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1952 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
1956 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
1958 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
1959 struct event_probe_data
*data
= *pdata
;
1967 /* Skip if the event is in a state we want to switch to */
1968 if (data
->enable
== !(data
->file
->flags
& FTRACE_EVENT_FL_SOFT_DISABLED
))
1971 if (data
->count
!= -1)
1974 event_enable_probe(ip
, parent_ip
, _data
);
1978 event_enable_print(struct seq_file
*m
, unsigned long ip
,
1979 struct ftrace_probe_ops
*ops
, void *_data
)
1981 struct event_probe_data
*data
= _data
;
1983 seq_printf(m
, "%ps:", (void *)ip
);
1985 seq_printf(m
, "%s:%s:%s",
1986 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
1987 data
->file
->event_call
->class->system
,
1988 ftrace_event_name(data
->file
->event_call
));
1990 if (data
->count
== -1)
1991 seq_printf(m
, ":unlimited\n");
1993 seq_printf(m
, ":count=%ld\n", data
->count
);
1999 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2002 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2003 struct event_probe_data
*data
= *pdata
;
2010 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2013 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2014 struct event_probe_data
*data
= *pdata
;
2016 if (WARN_ON_ONCE(data
->ref
<= 0))
2021 /* Remove the SOFT_MODE flag */
2022 __ftrace_event_enable_disable(data
->file
, 0, 1);
2023 module_put(data
->file
->event_call
->mod
);
2029 static struct ftrace_probe_ops event_enable_probe_ops
= {
2030 .func
= event_enable_probe
,
2031 .print
= event_enable_print
,
2032 .init
= event_enable_init
,
2033 .free
= event_enable_free
,
2036 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2037 .func
= event_enable_count_probe
,
2038 .print
= event_enable_print
,
2039 .init
= event_enable_init
,
2040 .free
= event_enable_free
,
2043 static struct ftrace_probe_ops event_disable_probe_ops
= {
2044 .func
= event_enable_probe
,
2045 .print
= event_enable_print
,
2046 .init
= event_enable_init
,
2047 .free
= event_enable_free
,
2050 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2051 .func
= event_enable_count_probe
,
2052 .print
= event_enable_print
,
2053 .init
= event_enable_init
,
2054 .free
= event_enable_free
,
2058 event_enable_func(struct ftrace_hash
*hash
,
2059 char *glob
, char *cmd
, char *param
, int enabled
)
2061 struct trace_array
*tr
= top_trace_array();
2062 struct ftrace_event_file
*file
;
2063 struct ftrace_probe_ops
*ops
;
2064 struct event_probe_data
*data
;
2074 /* hash funcs only work with set_ftrace_filter */
2075 if (!enabled
|| !param
)
2078 system
= strsep(¶m
, ":");
2082 event
= strsep(¶m
, ":");
2084 mutex_lock(&event_mutex
);
2087 file
= find_event_file(tr
, system
, event
);
2091 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2094 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2096 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2098 if (glob
[0] == '!') {
2099 unregister_ftrace_function_probe_func(glob
+1, ops
);
2105 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2109 data
->enable
= enable
;
2116 number
= strsep(¶m
, ":");
2119 if (!strlen(number
))
2123 * We use the callback data field (which is a pointer)
2126 ret
= kstrtoul(number
, 0, &data
->count
);
2131 /* Don't let event modules unload while probe registered */
2132 ret
= try_module_get(file
->event_call
->mod
);
2138 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2141 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2143 * The above returns on success the # of functions enabled,
2144 * but if it didn't find any functions it returns zero.
2145 * Consider no functions a failure too.
2152 /* Just return zero, not the number of enabled functions */
2155 mutex_unlock(&event_mutex
);
2159 __ftrace_event_enable_disable(file
, 0, 1);
2161 module_put(file
->event_call
->mod
);
2167 static struct ftrace_func_command event_enable_cmd
= {
2168 .name
= ENABLE_EVENT_STR
,
2169 .func
= event_enable_func
,
2172 static struct ftrace_func_command event_disable_cmd
= {
2173 .name
= DISABLE_EVENT_STR
,
2174 .func
= event_enable_func
,
2177 static __init
int register_event_cmds(void)
2181 ret
= register_ftrace_command(&event_enable_cmd
);
2182 if (WARN_ON(ret
< 0))
2184 ret
= register_ftrace_command(&event_disable_cmd
);
2185 if (WARN_ON(ret
< 0))
2186 unregister_ftrace_command(&event_enable_cmd
);
2190 static inline int register_event_cmds(void) { return 0; }
2191 #endif /* CONFIG_DYNAMIC_FTRACE */
2194 * The top level array has already had its ftrace_event_file
2195 * descriptors created in order to allow for early events to
2196 * be recorded. This function is called after the debugfs has been
2197 * initialized, and we now have to create the files associated
2201 __trace_early_add_event_dirs(struct trace_array
*tr
)
2203 struct ftrace_event_file
*file
;
2207 list_for_each_entry(file
, &tr
->events
, list
) {
2208 ret
= event_create_dir(tr
->event_dir
, file
);
2210 pr_warn("Could not create directory for event %s\n",
2211 ftrace_event_name(file
->event_call
));
2216 * For early boot up, the top trace array requires to have
2217 * a list of events that can be enabled. This must be done before
2218 * the filesystem is set up in order to allow events to be traced
2222 __trace_early_add_events(struct trace_array
*tr
)
2224 struct ftrace_event_call
*call
;
2227 list_for_each_entry(call
, &ftrace_events
, list
) {
2228 /* Early boot up should not have any modules loaded */
2229 if (WARN_ON_ONCE(call
->mod
))
2232 ret
= __trace_early_add_new_event(call
, tr
);
2234 pr_warn("Could not create early event %s\n",
2235 ftrace_event_name(call
));
2239 /* Remove the event directory structure for a trace directory. */
2241 __trace_remove_event_dirs(struct trace_array
*tr
)
2243 struct ftrace_event_file
*file
, *next
;
2245 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2246 remove_event_file_dir(file
);
2249 static void __add_event_to_tracers(struct ftrace_event_call
*call
)
2251 struct trace_array
*tr
;
2253 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2254 __trace_add_new_event(call
, tr
);
2257 extern struct ftrace_event_call
*__start_ftrace_events
[];
2258 extern struct ftrace_event_call
*__stop_ftrace_events
[];
2260 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2262 static __init
int setup_trace_event(char *str
)
2264 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2265 ring_buffer_expanded
= true;
2266 tracing_selftest_disabled
= true;
2270 __setup("trace_event=", setup_trace_event
);
2272 /* Expects to have event_mutex held when called */
2274 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2276 struct dentry
*d_events
;
2277 struct dentry
*entry
;
2279 entry
= debugfs_create_file("set_event", 0644, parent
,
2280 tr
, &ftrace_set_event_fops
);
2282 pr_warn("Could not create debugfs 'set_event' entry\n");
2286 d_events
= debugfs_create_dir("events", parent
);
2288 pr_warn("Could not create debugfs 'events' directory\n");
2292 /* ring buffer internal formats */
2293 trace_create_file("header_page", 0444, d_events
,
2294 ring_buffer_print_page_header
,
2295 &ftrace_show_header_fops
);
2297 trace_create_file("header_event", 0444, d_events
,
2298 ring_buffer_print_entry_header
,
2299 &ftrace_show_header_fops
);
2301 trace_create_file("enable", 0644, d_events
,
2302 tr
, &ftrace_tr_enable_fops
);
2304 tr
->event_dir
= d_events
;
2310 * event_trace_add_tracer - add a instance of a trace_array to events
2311 * @parent: The parent dentry to place the files/directories for events in
2312 * @tr: The trace array associated with these events
2314 * When a new instance is created, it needs to set up its events
2315 * directory, as well as other files associated with events. It also
2316 * creates the event hierachry in the @parent/events directory.
2318 * Returns 0 on success.
2320 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2324 mutex_lock(&event_mutex
);
2326 ret
= create_event_toplevel_files(parent
, tr
);
2330 down_write(&trace_event_sem
);
2331 __trace_add_event_dirs(tr
);
2332 up_write(&trace_event_sem
);
2335 mutex_unlock(&event_mutex
);
2341 * The top trace array already had its file descriptors created.
2342 * Now the files themselves need to be created.
2345 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
2349 mutex_lock(&event_mutex
);
2351 ret
= create_event_toplevel_files(parent
, tr
);
2355 down_write(&trace_event_sem
);
2356 __trace_early_add_event_dirs(tr
);
2357 up_write(&trace_event_sem
);
2360 mutex_unlock(&event_mutex
);
2365 int event_trace_del_tracer(struct trace_array
*tr
)
2367 mutex_lock(&event_mutex
);
2369 /* Disable any event triggers and associated soft-disabled events */
2370 clear_event_triggers(tr
);
2372 /* Disable any running events */
2373 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
2375 /* Access to events are within rcu_read_lock_sched() */
2376 synchronize_sched();
2378 down_write(&trace_event_sem
);
2379 __trace_remove_event_dirs(tr
);
2380 debugfs_remove_recursive(tr
->event_dir
);
2381 up_write(&trace_event_sem
);
2383 tr
->event_dir
= NULL
;
2385 mutex_unlock(&event_mutex
);
2390 static __init
int event_trace_memsetup(void)
2392 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
2393 file_cachep
= KMEM_CACHE(ftrace_event_file
, SLAB_PANIC
);
2397 static __init
int event_trace_enable(void)
2399 struct trace_array
*tr
= top_trace_array();
2400 struct ftrace_event_call
**iter
, *call
;
2401 char *buf
= bootup_event_buf
;
2408 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
2411 ret
= event_init(call
);
2413 list_add(&call
->list
, &ftrace_events
);
2417 * We need the top trace array to have a working set of trace
2418 * points at early init, before the debug files and directories
2419 * are created. Create the file entries now, and attach them
2420 * to the actual file dentries later.
2422 __trace_early_add_events(tr
);
2425 token
= strsep(&buf
, ",");
2432 ret
= ftrace_set_clr_event(tr
, token
, 1);
2434 pr_warn("Failed to enable trace event: %s\n", token
);
2437 trace_printk_start_comm();
2439 register_event_cmds();
2441 register_trigger_cmds();
2446 static __init
int event_trace_init(void)
2448 struct trace_array
*tr
;
2449 struct dentry
*d_tracer
;
2450 struct dentry
*entry
;
2453 tr
= top_trace_array();
2457 d_tracer
= tracing_init_dentry();
2461 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
2462 tr
, &ftrace_avail_fops
);
2464 pr_warn("Could not create debugfs 'available_events' entry\n");
2466 if (trace_define_common_fields())
2467 pr_warn("tracing: Failed to allocate common fields");
2469 ret
= early_event_add_tracer(d_tracer
, tr
);
2473 #ifdef CONFIG_MODULES
2474 ret
= register_module_notifier(&trace_module_nb
);
2476 pr_warn("Failed to register trace events module notifier\n");
2480 early_initcall(event_trace_memsetup
);
2481 core_initcall(event_trace_enable
);
2482 fs_initcall(event_trace_init
);
2484 #ifdef CONFIG_FTRACE_STARTUP_TEST
2486 static DEFINE_SPINLOCK(test_spinlock
);
2487 static DEFINE_SPINLOCK(test_spinlock_irq
);
2488 static DEFINE_MUTEX(test_mutex
);
2490 static __init
void test_work(struct work_struct
*dummy
)
2492 spin_lock(&test_spinlock
);
2493 spin_lock_irq(&test_spinlock_irq
);
2495 spin_unlock_irq(&test_spinlock_irq
);
2496 spin_unlock(&test_spinlock
);
2498 mutex_lock(&test_mutex
);
2500 mutex_unlock(&test_mutex
);
2503 static __init
int event_test_thread(void *unused
)
2507 test_malloc
= kmalloc(1234, GFP_KERNEL
);
2509 pr_info("failed to kmalloc\n");
2511 schedule_on_each_cpu(test_work
);
2515 set_current_state(TASK_INTERRUPTIBLE
);
2516 while (!kthread_should_stop())
2523 * Do various things that may trigger events.
2525 static __init
void event_test_stuff(void)
2527 struct task_struct
*test_thread
;
2529 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
2531 kthread_stop(test_thread
);
2535 * For every trace event defined, we will test each trace point separately,
2536 * and then by groups, and finally all trace points.
2538 static __init
void event_trace_self_tests(void)
2540 struct ftrace_subsystem_dir
*dir
;
2541 struct ftrace_event_file
*file
;
2542 struct ftrace_event_call
*call
;
2543 struct event_subsystem
*system
;
2544 struct trace_array
*tr
;
2547 tr
= top_trace_array();
2551 pr_info("Running tests on trace events:\n");
2553 list_for_each_entry(file
, &tr
->events
, list
) {
2555 call
= file
->event_call
;
2557 /* Only test those that have a probe */
2558 if (!call
->class || !call
->class->probe
)
2562 * Testing syscall events here is pretty useless, but
2563 * we still do it if configured. But this is time consuming.
2564 * What we really need is a user thread to perform the
2565 * syscalls as we test.
2567 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2568 if (call
->class->system
&&
2569 strcmp(call
->class->system
, "syscalls") == 0)
2573 pr_info("Testing event %s: ", ftrace_event_name(call
));
2576 * If an event is already enabled, someone is using
2577 * it and the self test should not be on.
2579 if (file
->flags
& FTRACE_EVENT_FL_ENABLED
) {
2580 pr_warn("Enabled event during self test!\n");
2585 ftrace_event_enable_disable(file
, 1);
2587 ftrace_event_enable_disable(file
, 0);
2592 /* Now test at the sub system level */
2594 pr_info("Running tests on trace event systems:\n");
2596 list_for_each_entry(dir
, &tr
->systems
, list
) {
2598 system
= dir
->subsystem
;
2600 /* the ftrace system is special, skip it */
2601 if (strcmp(system
->name
, "ftrace") == 0)
2604 pr_info("Testing event system %s: ", system
->name
);
2606 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
2607 if (WARN_ON_ONCE(ret
)) {
2608 pr_warn("error enabling system %s\n",
2615 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
2616 if (WARN_ON_ONCE(ret
)) {
2617 pr_warn("error disabling system %s\n",
2625 /* Test with all events enabled */
2627 pr_info("Running tests on all trace events:\n");
2628 pr_info("Testing all events: ");
2630 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
2631 if (WARN_ON_ONCE(ret
)) {
2632 pr_warn("error enabling all events\n");
2639 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
2640 if (WARN_ON_ONCE(ret
)) {
2641 pr_warn("error disabling all events\n");
2648 #ifdef CONFIG_FUNCTION_TRACER
2650 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
2653 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
2654 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2656 struct ring_buffer_event
*event
;
2657 struct ring_buffer
*buffer
;
2658 struct ftrace_entry
*entry
;
2659 unsigned long flags
;
2664 pc
= preempt_count();
2665 preempt_disable_notrace();
2666 cpu
= raw_smp_processor_id();
2667 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
2672 local_save_flags(flags
);
2674 event
= trace_current_buffer_lock_reserve(&buffer
,
2675 TRACE_FN
, sizeof(*entry
),
2679 entry
= ring_buffer_event_data(event
);
2681 entry
->parent_ip
= parent_ip
;
2683 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
2686 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
2687 preempt_enable_notrace();
2690 static struct ftrace_ops trace_ops __initdata
=
2692 .func
= function_test_events_call
,
2693 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
2696 static __init
void event_trace_self_test_with_function(void)
2699 ret
= register_ftrace_function(&trace_ops
);
2700 if (WARN_ON(ret
< 0)) {
2701 pr_info("Failed to enable function tracer for event tests\n");
2704 pr_info("Running tests again, along with the function tracer\n");
2705 event_trace_self_tests();
2706 unregister_ftrace_function(&trace_ops
);
2709 static __init
void event_trace_self_test_with_function(void)
2714 static __init
int event_trace_self_tests_init(void)
2716 if (!tracing_selftest_disabled
) {
2717 event_trace_self_tests();
2718 event_trace_self_test_with_function();
2724 late_initcall(event_trace_self_tests_init
);