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/tracefs.h>
17 #include <linux/uaccess.h>
18 #include <linux/vmalloc.h>
19 #include <linux/module.h>
20 #include <linux/ctype.h>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
25 #include <trace/events/sched.h>
27 #include <asm/setup.h>
29 #include "trace_output.h"
32 #define TRACE_SYSTEM "TRACE_SYSTEM"
34 DEFINE_MUTEX(event_mutex
);
36 LIST_HEAD(ftrace_events
);
37 static LIST_HEAD(ftrace_generic_fields
);
38 static LIST_HEAD(ftrace_common_fields
);
40 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
42 static struct kmem_cache
*field_cachep
;
43 static struct kmem_cache
*file_cachep
;
45 static inline int system_refcount(struct event_subsystem
*system
)
47 return system
->ref_count
;
50 static int system_refcount_inc(struct event_subsystem
*system
)
52 return system
->ref_count
++;
55 static int system_refcount_dec(struct event_subsystem
*system
)
57 return --system
->ref_count
;
60 /* Double loops, do not use break, only goto's work */
61 #define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
65 #define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
67 struct trace_event_file *___n; \
68 list_for_each_entry_safe(file, ___n, &tr->events, list)
70 #define while_for_each_event_file() \
73 static struct list_head
*
74 trace_get_fields(struct trace_event_call
*event_call
)
76 if (!event_call
->class->get_fields
)
77 return &event_call
->class->fields
;
78 return event_call
->class->get_fields(event_call
);
81 static struct ftrace_event_field
*
82 __find_event_field(struct list_head
*head
, char *name
)
84 struct ftrace_event_field
*field
;
86 list_for_each_entry(field
, head
, link
) {
87 if (!strcmp(field
->name
, name
))
94 struct ftrace_event_field
*
95 trace_find_event_field(struct trace_event_call
*call
, char *name
)
97 struct ftrace_event_field
*field
;
98 struct list_head
*head
;
100 head
= trace_get_fields(call
);
101 field
= __find_event_field(head
, name
);
105 field
= __find_event_field(&ftrace_generic_fields
, name
);
109 return __find_event_field(&ftrace_common_fields
, name
);
112 static int __trace_define_field(struct list_head
*head
, const char *type
,
113 const char *name
, int offset
, int size
,
114 int is_signed
, int filter_type
)
116 struct ftrace_event_field
*field
;
118 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
125 if (filter_type
== FILTER_OTHER
)
126 field
->filter_type
= filter_assign_type(type
);
128 field
->filter_type
= filter_type
;
130 field
->offset
= offset
;
132 field
->is_signed
= is_signed
;
134 list_add(&field
->link
, head
);
139 int trace_define_field(struct trace_event_call
*call
, const char *type
,
140 const char *name
, int offset
, int size
, int is_signed
,
143 struct list_head
*head
;
145 if (WARN_ON(!call
->class))
148 head
= trace_get_fields(call
);
149 return __trace_define_field(head
, type
, name
, offset
, size
,
150 is_signed
, filter_type
);
152 EXPORT_SYMBOL_GPL(trace_define_field
);
154 #define __generic_field(type, item, filter_type) \
155 ret = __trace_define_field(&ftrace_generic_fields, #type, \
156 #item, 0, 0, is_signed_type(type), \
161 #define __common_field(type, item) \
162 ret = __trace_define_field(&ftrace_common_fields, #type, \
164 offsetof(typeof(ent), item), \
166 is_signed_type(type), FILTER_OTHER); \
170 static int trace_define_generic_fields(void)
174 __generic_field(int, CPU
, FILTER_CPU
);
175 __generic_field(int, cpu
, FILTER_CPU
);
176 __generic_field(char *, COMM
, FILTER_COMM
);
177 __generic_field(char *, comm
, FILTER_COMM
);
182 static int trace_define_common_fields(void)
185 struct trace_entry ent
;
187 __common_field(unsigned short, type
);
188 __common_field(unsigned char, flags
);
189 __common_field(unsigned char, preempt_count
);
190 __common_field(int, pid
);
195 static void trace_destroy_fields(struct trace_event_call
*call
)
197 struct ftrace_event_field
*field
, *next
;
198 struct list_head
*head
;
200 head
= trace_get_fields(call
);
201 list_for_each_entry_safe(field
, next
, head
, link
) {
202 list_del(&field
->link
);
203 kmem_cache_free(field_cachep
, field
);
208 * run-time version of trace_event_get_offsets_<call>() that returns the last
209 * accessible offset of trace fields excluding __dynamic_array bytes
211 int trace_event_get_offsets(struct trace_event_call
*call
)
213 struct ftrace_event_field
*tail
;
214 struct list_head
*head
;
216 head
= trace_get_fields(call
);
218 * head->next points to the last field with the largest offset,
219 * since it was added last by trace_define_field()
221 tail
= list_first_entry(head
, struct ftrace_event_field
, link
);
222 return tail
->offset
+ tail
->size
;
225 int trace_event_raw_init(struct trace_event_call
*call
)
229 id
= register_trace_event(&call
->event
);
235 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
237 bool trace_event_ignore_this_pid(struct trace_event_file
*trace_file
)
239 struct trace_array
*tr
= trace_file
->tr
;
240 struct trace_array_cpu
*data
;
241 struct trace_pid_list
*pid_list
;
243 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
247 data
= this_cpu_ptr(tr
->trace_buffer
.data
);
249 return data
->ignore_pid
;
251 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid
);
253 void *trace_event_buffer_reserve(struct trace_event_buffer
*fbuffer
,
254 struct trace_event_file
*trace_file
,
257 struct trace_event_call
*event_call
= trace_file
->event_call
;
259 if ((trace_file
->flags
& EVENT_FILE_FL_PID_FILTER
) &&
260 trace_event_ignore_this_pid(trace_file
))
263 local_save_flags(fbuffer
->flags
);
264 fbuffer
->pc
= preempt_count();
265 fbuffer
->trace_file
= trace_file
;
268 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, trace_file
,
269 event_call
->event
.type
, len
,
270 fbuffer
->flags
, fbuffer
->pc
);
274 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
275 return fbuffer
->entry
;
277 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve
);
279 static DEFINE_SPINLOCK(tracepoint_iter_lock
);
281 static void output_printk(struct trace_event_buffer
*fbuffer
)
283 struct trace_event_call
*event_call
;
284 struct trace_event
*event
;
286 struct trace_iterator
*iter
= tracepoint_print_iter
;
291 event_call
= fbuffer
->trace_file
->event_call
;
292 if (!event_call
|| !event_call
->event
.funcs
||
293 !event_call
->event
.funcs
->trace
)
296 event
= &fbuffer
->trace_file
->event_call
->event
;
298 spin_lock_irqsave(&tracepoint_iter_lock
, flags
);
299 trace_seq_init(&iter
->seq
);
300 iter
->ent
= fbuffer
->entry
;
301 event_call
->event
.funcs
->trace(iter
, 0, event
);
302 trace_seq_putc(&iter
->seq
, 0);
303 printk("%s", iter
->seq
.buffer
);
305 spin_unlock_irqrestore(&tracepoint_iter_lock
, flags
);
308 void trace_event_buffer_commit(struct trace_event_buffer
*fbuffer
)
310 if (tracepoint_printk
)
311 output_printk(fbuffer
);
313 event_trigger_unlock_commit(fbuffer
->trace_file
, fbuffer
->buffer
,
314 fbuffer
->event
, fbuffer
->entry
,
315 fbuffer
->flags
, fbuffer
->pc
);
317 EXPORT_SYMBOL_GPL(trace_event_buffer_commit
);
319 int trace_event_reg(struct trace_event_call
*call
,
320 enum trace_reg type
, void *data
)
322 struct trace_event_file
*file
= data
;
324 WARN_ON(!(call
->flags
& TRACE_EVENT_FL_TRACEPOINT
));
326 case TRACE_REG_REGISTER
:
327 return tracepoint_probe_register(call
->tp
,
330 case TRACE_REG_UNREGISTER
:
331 tracepoint_probe_unregister(call
->tp
,
336 #ifdef CONFIG_PERF_EVENTS
337 case TRACE_REG_PERF_REGISTER
:
338 return tracepoint_probe_register(call
->tp
,
339 call
->class->perf_probe
,
341 case TRACE_REG_PERF_UNREGISTER
:
342 tracepoint_probe_unregister(call
->tp
,
343 call
->class->perf_probe
,
346 case TRACE_REG_PERF_OPEN
:
347 case TRACE_REG_PERF_CLOSE
:
348 case TRACE_REG_PERF_ADD
:
349 case TRACE_REG_PERF_DEL
:
355 EXPORT_SYMBOL_GPL(trace_event_reg
);
357 void trace_event_enable_cmd_record(bool enable
)
359 struct trace_event_file
*file
;
360 struct trace_array
*tr
;
362 mutex_lock(&event_mutex
);
363 do_for_each_event_file(tr
, file
) {
365 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
))
369 tracing_start_cmdline_record();
370 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
372 tracing_stop_cmdline_record();
373 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
375 } while_for_each_event_file();
376 mutex_unlock(&event_mutex
);
379 static int __ftrace_event_enable_disable(struct trace_event_file
*file
,
380 int enable
, int soft_disable
)
382 struct trace_event_call
*call
= file
->event_call
;
383 struct trace_array
*tr
= file
->tr
;
384 unsigned long file_flags
= file
->flags
;
391 * When soft_disable is set and enable is cleared, the sm_ref
392 * reference counter is decremented. If it reaches 0, we want
393 * to clear the SOFT_DISABLED flag but leave the event in the
394 * state that it was. That is, if the event was enabled and
395 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
396 * is set we do not want the event to be enabled before we
399 * When soft_disable is not set but the SOFT_MODE flag is,
400 * we do nothing. Do not disable the tracepoint, otherwise
401 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
404 if (atomic_dec_return(&file
->sm_ref
) > 0)
406 disable
= file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
;
407 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
409 disable
= !(file
->flags
& EVENT_FILE_FL_SOFT_MODE
);
411 if (disable
&& (file
->flags
& EVENT_FILE_FL_ENABLED
)) {
412 clear_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
413 if (file
->flags
& EVENT_FILE_FL_RECORDED_CMD
) {
414 tracing_stop_cmdline_record();
415 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
417 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
419 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
420 if (file
->flags
& EVENT_FILE_FL_SOFT_MODE
)
421 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
423 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
427 * When soft_disable is set and enable is set, we want to
428 * register the tracepoint for the event, but leave the event
429 * as is. That means, if the event was already enabled, we do
430 * nothing (but set SOFT_MODE). If the event is disabled, we
431 * set SOFT_DISABLED before enabling the event tracepoint, so
432 * it still seems to be disabled.
435 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
437 if (atomic_inc_return(&file
->sm_ref
) > 1)
439 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
442 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
)) {
444 /* Keep the event disabled, when going to SOFT_MODE. */
446 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
448 if (tr
->trace_flags
& TRACE_ITER_RECORD_CMD
) {
449 tracing_start_cmdline_record();
450 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
452 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
454 tracing_stop_cmdline_record();
455 pr_info("event trace: Could not enable event "
456 "%s\n", trace_event_name(call
));
459 set_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
461 /* WAS_ENABLED gets set but never cleared. */
462 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
467 /* Enable or disable use of trace_buffered_event */
468 if ((file_flags
& EVENT_FILE_FL_SOFT_DISABLED
) !=
469 (file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
)) {
470 if (file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
)
471 trace_buffered_event_enable();
473 trace_buffered_event_disable();
479 int trace_event_enable_disable(struct trace_event_file
*file
,
480 int enable
, int soft_disable
)
482 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
485 static int ftrace_event_enable_disable(struct trace_event_file
*file
,
488 return __ftrace_event_enable_disable(file
, enable
, 0);
491 static void ftrace_clear_events(struct trace_array
*tr
)
493 struct trace_event_file
*file
;
495 mutex_lock(&event_mutex
);
496 list_for_each_entry(file
, &tr
->events
, list
) {
497 ftrace_event_enable_disable(file
, 0);
499 mutex_unlock(&event_mutex
);
502 /* Shouldn't this be in a header? */
505 /* Returns true if found in filter */
507 find_filtered_pid(struct trace_pid_list
*filtered_pids
, pid_t search_pid
)
510 * If pid_max changed after filtered_pids was created, we
511 * by default ignore all pids greater than the previous pid_max.
513 if (search_pid
>= filtered_pids
->pid_max
)
516 return test_bit(search_pid
, filtered_pids
->pids
);
520 ignore_this_task(struct trace_pid_list
*filtered_pids
, struct task_struct
*task
)
523 * Return false, because if filtered_pids does not exist,
524 * all pids are good to trace.
529 return !find_filtered_pid(filtered_pids
, task
->pid
);
532 static void filter_add_remove_task(struct trace_pid_list
*pid_list
,
533 struct task_struct
*self
,
534 struct task_struct
*task
)
539 /* For forks, we only add if the forking task is listed */
541 if (!find_filtered_pid(pid_list
, self
->pid
))
545 /* Sorry, but we don't support pid_max changing after setting */
546 if (task
->pid
>= pid_list
->pid_max
)
549 /* "self" is set for forks, and NULL for exits */
551 set_bit(task
->pid
, pid_list
->pids
);
553 clear_bit(task
->pid
, pid_list
->pids
);
557 event_filter_pid_sched_process_exit(void *data
, struct task_struct
*task
)
559 struct trace_pid_list
*pid_list
;
560 struct trace_array
*tr
= data
;
562 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
563 filter_add_remove_task(pid_list
, NULL
, task
);
567 event_filter_pid_sched_process_fork(void *data
,
568 struct task_struct
*self
,
569 struct task_struct
*task
)
571 struct trace_pid_list
*pid_list
;
572 struct trace_array
*tr
= data
;
574 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
575 filter_add_remove_task(pid_list
, self
, task
);
578 void trace_event_follow_fork(struct trace_array
*tr
, bool enable
)
581 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork
,
583 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit
,
586 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork
,
588 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit
,
594 event_filter_pid_sched_switch_probe_pre(void *data
, bool preempt
,
595 struct task_struct
*prev
, struct task_struct
*next
)
597 struct trace_array
*tr
= data
;
598 struct trace_pid_list
*pid_list
;
600 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
602 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
603 ignore_this_task(pid_list
, prev
) &&
604 ignore_this_task(pid_list
, next
));
608 event_filter_pid_sched_switch_probe_post(void *data
, bool preempt
,
609 struct task_struct
*prev
, struct task_struct
*next
)
611 struct trace_array
*tr
= data
;
612 struct trace_pid_list
*pid_list
;
614 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
616 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
617 ignore_this_task(pid_list
, next
));
621 event_filter_pid_sched_wakeup_probe_pre(void *data
, struct task_struct
*task
)
623 struct trace_array
*tr
= data
;
624 struct trace_pid_list
*pid_list
;
626 /* Nothing to do if we are already tracing */
627 if (!this_cpu_read(tr
->trace_buffer
.data
->ignore_pid
))
630 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
632 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
633 ignore_this_task(pid_list
, task
));
637 event_filter_pid_sched_wakeup_probe_post(void *data
, struct task_struct
*task
)
639 struct trace_array
*tr
= data
;
640 struct trace_pid_list
*pid_list
;
642 /* Nothing to do if we are not tracing */
643 if (this_cpu_read(tr
->trace_buffer
.data
->ignore_pid
))
646 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
648 /* Set tracing if current is enabled */
649 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
650 ignore_this_task(pid_list
, current
));
653 static void __ftrace_clear_event_pids(struct trace_array
*tr
)
655 struct trace_pid_list
*pid_list
;
656 struct trace_event_file
*file
;
659 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
660 lockdep_is_held(&event_mutex
));
664 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre
, tr
);
665 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post
, tr
);
667 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
, tr
);
668 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
, tr
);
670 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
, tr
);
671 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
, tr
);
673 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre
, tr
);
674 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post
, tr
);
676 list_for_each_entry(file
, &tr
->events
, list
) {
677 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
680 for_each_possible_cpu(cpu
)
681 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->ignore_pid
= false;
683 rcu_assign_pointer(tr
->filtered_pids
, NULL
);
685 /* Wait till all users are no longer using pid filtering */
688 vfree(pid_list
->pids
);
692 static void ftrace_clear_event_pids(struct trace_array
*tr
)
694 mutex_lock(&event_mutex
);
695 __ftrace_clear_event_pids(tr
);
696 mutex_unlock(&event_mutex
);
699 static void __put_system(struct event_subsystem
*system
)
701 struct event_filter
*filter
= system
->filter
;
703 WARN_ON_ONCE(system_refcount(system
) == 0);
704 if (system_refcount_dec(system
))
707 list_del(&system
->list
);
710 kfree(filter
->filter_string
);
713 kfree_const(system
->name
);
717 static void __get_system(struct event_subsystem
*system
)
719 WARN_ON_ONCE(system_refcount(system
) == 0);
720 system_refcount_inc(system
);
723 static void __get_system_dir(struct trace_subsystem_dir
*dir
)
725 WARN_ON_ONCE(dir
->ref_count
== 0);
727 __get_system(dir
->subsystem
);
730 static void __put_system_dir(struct trace_subsystem_dir
*dir
)
732 WARN_ON_ONCE(dir
->ref_count
== 0);
733 /* If the subsystem is about to be freed, the dir must be too */
734 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
736 __put_system(dir
->subsystem
);
737 if (!--dir
->ref_count
)
741 static void put_system(struct trace_subsystem_dir
*dir
)
743 mutex_lock(&event_mutex
);
744 __put_system_dir(dir
);
745 mutex_unlock(&event_mutex
);
748 static void remove_subsystem(struct trace_subsystem_dir
*dir
)
753 if (!--dir
->nr_events
) {
754 tracefs_remove_recursive(dir
->entry
);
755 list_del(&dir
->list
);
756 __put_system_dir(dir
);
760 static void remove_event_file_dir(struct trace_event_file
*file
)
762 struct dentry
*dir
= file
->dir
;
763 struct dentry
*child
;
766 spin_lock(&dir
->d_lock
); /* probably unneeded */
767 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
768 if (d_really_is_positive(child
)) /* probably unneeded */
769 d_inode(child
)->i_private
= NULL
;
771 spin_unlock(&dir
->d_lock
);
773 tracefs_remove_recursive(dir
);
776 list_del(&file
->list
);
777 remove_subsystem(file
->system
);
778 free_event_filter(file
->filter
);
779 kmem_cache_free(file_cachep
, file
);
783 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
786 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
787 const char *sub
, const char *event
, int set
)
789 struct trace_event_file
*file
;
790 struct trace_event_call
*call
;
794 list_for_each_entry(file
, &tr
->events
, list
) {
796 call
= file
->event_call
;
797 name
= trace_event_name(call
);
799 if (!name
|| !call
->class || !call
->class->reg
)
802 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
806 strcmp(match
, name
) != 0 &&
807 strcmp(match
, call
->class->system
) != 0)
810 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
813 if (event
&& strcmp(event
, name
) != 0)
816 ftrace_event_enable_disable(file
, set
);
824 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
825 const char *sub
, const char *event
, int set
)
829 mutex_lock(&event_mutex
);
830 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
831 mutex_unlock(&event_mutex
);
836 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
838 char *event
= NULL
, *sub
= NULL
, *match
;
842 * The buf format can be <subsystem>:<event-name>
843 * *:<event-name> means any event by that name.
844 * :<event-name> is the same.
846 * <subsystem>:* means all events in that subsystem
847 * <subsystem>: means the same.
849 * <name> (no ':') means all events in a subsystem with
850 * the name <name> or any event that matches <name>
853 match
= strsep(&buf
, ":");
859 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
861 if (!strlen(event
) || strcmp(event
, "*") == 0)
865 ret
= __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
867 /* Put back the colon to allow this to be called again */
875 * trace_set_clr_event - enable or disable an event
876 * @system: system name to match (NULL for any system)
877 * @event: event name to match (NULL for all events, within system)
878 * @set: 1 to enable, 0 to disable
880 * This is a way for other parts of the kernel to enable or disable
883 * Returns 0 on success, -EINVAL if the parameters do not match any
886 int trace_set_clr_event(const char *system
, const char *event
, int set
)
888 struct trace_array
*tr
= top_trace_array();
893 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
895 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
897 /* 128 should be much more than enough */
898 #define EVENT_BUF_SIZE 127
901 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
902 size_t cnt
, loff_t
*ppos
)
904 struct trace_parser parser
;
905 struct seq_file
*m
= file
->private_data
;
906 struct trace_array
*tr
= m
->private;
912 ret
= tracing_update_buffers();
916 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
919 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
921 if (read
>= 0 && trace_parser_loaded((&parser
))) {
924 if (*parser
.buffer
== '!')
927 parser
.buffer
[parser
.idx
] = 0;
929 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
937 trace_parser_put(&parser
);
943 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
945 struct trace_event_file
*file
= v
;
946 struct trace_event_call
*call
;
947 struct trace_array
*tr
= m
->private;
951 list_for_each_entry_continue(file
, &tr
->events
, list
) {
952 call
= file
->event_call
;
954 * The ftrace subsystem is for showing formats only.
955 * They can not be enabled or disabled via the event files.
957 if (call
->class && call
->class->reg
&&
958 !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
965 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
967 struct trace_event_file
*file
;
968 struct trace_array
*tr
= m
->private;
971 mutex_lock(&event_mutex
);
973 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
974 for (l
= 0; l
<= *pos
; ) {
975 file
= t_next(m
, file
, &l
);
983 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
985 struct trace_event_file
*file
= v
;
986 struct trace_array
*tr
= m
->private;
990 list_for_each_entry_continue(file
, &tr
->events
, list
) {
991 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
998 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1000 struct trace_event_file
*file
;
1001 struct trace_array
*tr
= m
->private;
1004 mutex_lock(&event_mutex
);
1006 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
1007 for (l
= 0; l
<= *pos
; ) {
1008 file
= s_next(m
, file
, &l
);
1015 static int t_show(struct seq_file
*m
, void *v
)
1017 struct trace_event_file
*file
= v
;
1018 struct trace_event_call
*call
= file
->event_call
;
1020 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
1021 seq_printf(m
, "%s:", call
->class->system
);
1022 seq_printf(m
, "%s\n", trace_event_name(call
));
1027 static void t_stop(struct seq_file
*m
, void *p
)
1029 mutex_unlock(&event_mutex
);
1033 p_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1035 struct trace_array
*tr
= m
->private;
1036 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1037 unsigned long pid
= (unsigned long)v
;
1041 /* pid already is +1 of the actual prevous bit */
1042 pid
= find_next_bit(pid_list
->pids
, pid_list
->pid_max
, pid
);
1044 /* Return pid + 1 to allow zero to be represented */
1045 if (pid
< pid_list
->pid_max
)
1046 return (void *)(pid
+ 1);
1051 static void *p_start(struct seq_file
*m
, loff_t
*pos
)
1054 struct trace_pid_list
*pid_list
;
1055 struct trace_array
*tr
= m
->private;
1060 * Grab the mutex, to keep calls to p_next() having the same
1061 * tr->filtered_pids as p_start() has.
1062 * If we just passed the tr->filtered_pids around, then RCU would
1063 * have been enough, but doing that makes things more complex.
1065 mutex_lock(&event_mutex
);
1066 rcu_read_lock_sched();
1068 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1073 pid
= find_first_bit(pid_list
->pids
, pid_list
->pid_max
);
1074 if (pid
>= pid_list
->pid_max
)
1077 /* Return pid + 1 so that zero can be the exit value */
1078 for (pid
++; pid
&& l
< *pos
;
1079 pid
= (unsigned long)p_next(m
, (void *)pid
, &l
))
1084 static void p_stop(struct seq_file
*m
, void *p
)
1087 rcu_read_unlock_sched();
1088 mutex_unlock(&event_mutex
);
1091 static int p_show(struct seq_file
*m
, void *v
)
1093 unsigned long pid
= (unsigned long)v
- 1;
1095 seq_printf(m
, "%lu\n", pid
);
1100 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1103 struct trace_event_file
*file
;
1104 unsigned long flags
;
1107 mutex_lock(&event_mutex
);
1108 file
= event_file_data(filp
);
1110 flags
= file
->flags
;
1111 mutex_unlock(&event_mutex
);
1116 if (flags
& EVENT_FILE_FL_ENABLED
&&
1117 !(flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1120 if (flags
& EVENT_FILE_FL_SOFT_DISABLED
||
1121 flags
& EVENT_FILE_FL_SOFT_MODE
)
1126 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
1130 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1133 struct trace_event_file
*file
;
1137 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1141 ret
= tracing_update_buffers();
1149 mutex_lock(&event_mutex
);
1150 file
= event_file_data(filp
);
1152 ret
= ftrace_event_enable_disable(file
, val
);
1153 mutex_unlock(&event_mutex
);
1162 return ret
? ret
: cnt
;
1166 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1169 const char set_to_char
[4] = { '?', '0', '1', 'X' };
1170 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1171 struct event_subsystem
*system
= dir
->subsystem
;
1172 struct trace_event_call
*call
;
1173 struct trace_event_file
*file
;
1174 struct trace_array
*tr
= dir
->tr
;
1179 mutex_lock(&event_mutex
);
1180 list_for_each_entry(file
, &tr
->events
, list
) {
1181 call
= file
->event_call
;
1182 if (!trace_event_name(call
) || !call
->class || !call
->class->reg
)
1185 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
1189 * We need to find out if all the events are set
1190 * or if all events or cleared, or if we have
1193 set
|= (1 << !!(file
->flags
& EVENT_FILE_FL_ENABLED
));
1196 * If we have a mixture, no need to look further.
1201 mutex_unlock(&event_mutex
);
1203 buf
[0] = set_to_char
[set
];
1206 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
1212 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1215 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1216 struct event_subsystem
*system
= dir
->subsystem
;
1217 const char *name
= NULL
;
1221 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1225 ret
= tracing_update_buffers();
1229 if (val
!= 0 && val
!= 1)
1233 * Opening of "enable" adds a ref count to system,
1234 * so the name is safe to use.
1237 name
= system
->name
;
1239 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
1253 FORMAT_FIELD_SEPERATOR
= 2,
1254 FORMAT_PRINTFMT
= 3,
1257 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1259 struct trace_event_call
*call
= event_file_data(m
->private);
1260 struct list_head
*common_head
= &ftrace_common_fields
;
1261 struct list_head
*head
= trace_get_fields(call
);
1262 struct list_head
*node
= v
;
1266 switch ((unsigned long)v
) {
1271 case FORMAT_FIELD_SEPERATOR
:
1275 case FORMAT_PRINTFMT
:
1281 if (node
== common_head
)
1282 return (void *)FORMAT_FIELD_SEPERATOR
;
1283 else if (node
== head
)
1284 return (void *)FORMAT_PRINTFMT
;
1289 static int f_show(struct seq_file
*m
, void *v
)
1291 struct trace_event_call
*call
= event_file_data(m
->private);
1292 struct ftrace_event_field
*field
;
1293 const char *array_descriptor
;
1295 switch ((unsigned long)v
) {
1297 seq_printf(m
, "name: %s\n", trace_event_name(call
));
1298 seq_printf(m
, "ID: %d\n", call
->event
.type
);
1299 seq_puts(m
, "format:\n");
1302 case FORMAT_FIELD_SEPERATOR
:
1306 case FORMAT_PRINTFMT
:
1307 seq_printf(m
, "\nprint fmt: %s\n",
1312 field
= list_entry(v
, struct ftrace_event_field
, link
);
1314 * Smartly shows the array type(except dynamic array).
1317 * If TYPE := TYPE[LEN], it is shown:
1318 * field:TYPE VAR[LEN]
1320 array_descriptor
= strchr(field
->type
, '[');
1322 if (!strncmp(field
->type
, "__data_loc", 10))
1323 array_descriptor
= NULL
;
1325 if (!array_descriptor
)
1326 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1327 field
->type
, field
->name
, field
->offset
,
1328 field
->size
, !!field
->is_signed
);
1330 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1331 (int)(array_descriptor
- field
->type
),
1332 field
->type
, field
->name
,
1333 array_descriptor
, field
->offset
,
1334 field
->size
, !!field
->is_signed
);
1339 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
1341 void *p
= (void *)FORMAT_HEADER
;
1344 /* ->stop() is called even if ->start() fails */
1345 mutex_lock(&event_mutex
);
1346 if (!event_file_data(m
->private))
1347 return ERR_PTR(-ENODEV
);
1349 while (l
< *pos
&& p
)
1350 p
= f_next(m
, p
, &l
);
1355 static void f_stop(struct seq_file
*m
, void *p
)
1357 mutex_unlock(&event_mutex
);
1360 static const struct seq_operations trace_format_seq_ops
= {
1367 static int trace_format_open(struct inode
*inode
, struct file
*file
)
1372 ret
= seq_open(file
, &trace_format_seq_ops
);
1376 m
= file
->private_data
;
1383 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1385 int id
= (long)event_file_data(filp
);
1395 len
= sprintf(buf
, "%d\n", id
);
1397 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1401 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1404 struct trace_event_file
*file
;
1405 struct trace_seq
*s
;
1411 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1418 mutex_lock(&event_mutex
);
1419 file
= event_file_data(filp
);
1421 print_event_filter(file
, s
);
1422 mutex_unlock(&event_mutex
);
1425 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1426 s
->buffer
, trace_seq_used(s
));
1434 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1437 struct trace_event_file
*file
;
1441 if (cnt
>= PAGE_SIZE
)
1444 buf
= memdup_user_nul(ubuf
, cnt
);
1446 return PTR_ERR(buf
);
1448 mutex_lock(&event_mutex
);
1449 file
= event_file_data(filp
);
1451 err
= apply_event_filter(file
, buf
);
1452 mutex_unlock(&event_mutex
);
1463 static LIST_HEAD(event_subsystems
);
1465 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1467 struct event_subsystem
*system
= NULL
;
1468 struct trace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1469 struct trace_array
*tr
;
1472 if (tracing_is_disabled())
1475 /* Make sure the system still exists */
1476 mutex_lock(&trace_types_lock
);
1477 mutex_lock(&event_mutex
);
1478 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1479 list_for_each_entry(dir
, &tr
->systems
, list
) {
1480 if (dir
== inode
->i_private
) {
1481 /* Don't open systems with no events */
1482 if (dir
->nr_events
) {
1483 __get_system_dir(dir
);
1484 system
= dir
->subsystem
;
1491 mutex_unlock(&event_mutex
);
1492 mutex_unlock(&trace_types_lock
);
1497 /* Some versions of gcc think dir can be uninitialized here */
1500 /* Still need to increment the ref count of the system */
1501 if (trace_array_get(tr
) < 0) {
1506 ret
= tracing_open_generic(inode
, filp
);
1508 trace_array_put(tr
);
1515 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1517 struct trace_subsystem_dir
*dir
;
1518 struct trace_array
*tr
= inode
->i_private
;
1521 if (tracing_is_disabled())
1524 if (trace_array_get(tr
) < 0)
1527 /* Make a temporary dir that has no system but points to tr */
1528 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1530 trace_array_put(tr
);
1536 ret
= tracing_open_generic(inode
, filp
);
1538 trace_array_put(tr
);
1543 filp
->private_data
= dir
;
1548 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1550 struct trace_subsystem_dir
*dir
= file
->private_data
;
1552 trace_array_put(dir
->tr
);
1555 * If dir->subsystem is NULL, then this is a temporary
1556 * descriptor that was made for a trace_array to enable
1568 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1571 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1572 struct event_subsystem
*system
= dir
->subsystem
;
1573 struct trace_seq
*s
;
1579 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1585 print_subsystem_event_filter(system
, s
);
1586 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1587 s
->buffer
, trace_seq_used(s
));
1595 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1598 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1602 if (cnt
>= PAGE_SIZE
)
1605 buf
= memdup_user_nul(ubuf
, cnt
);
1607 return PTR_ERR(buf
);
1609 err
= apply_subsystem_event_filter(dir
, buf
);
1620 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1622 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1623 struct trace_seq
*s
;
1629 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1636 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1637 s
->buffer
, trace_seq_used(s
));
1644 static void ignore_task_cpu(void *data
)
1646 struct trace_array
*tr
= data
;
1647 struct trace_pid_list
*pid_list
;
1650 * This function is called by on_each_cpu() while the
1651 * event_mutex is held.
1653 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
1654 mutex_is_locked(&event_mutex
));
1656 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
1657 ignore_this_task(pid_list
, current
));
1661 ftrace_event_pid_write(struct file
*filp
, const char __user
*ubuf
,
1662 size_t cnt
, loff_t
*ppos
)
1664 struct seq_file
*m
= filp
->private_data
;
1665 struct trace_array
*tr
= m
->private;
1666 struct trace_pid_list
*filtered_pids
= NULL
;
1667 struct trace_pid_list
*pid_list
;
1668 struct trace_event_file
*file
;
1669 struct trace_parser parser
;
1680 ret
= tracing_update_buffers();
1684 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
1687 mutex_lock(&event_mutex
);
1688 filtered_pids
= rcu_dereference_protected(tr
->filtered_pids
,
1689 lockdep_is_held(&event_mutex
));
1692 * Always recreate a new array. The write is an all or nothing
1693 * operation. Always create a new array when adding new pids by
1694 * the user. If the operation fails, then the current list is
1697 pid_list
= kmalloc(sizeof(*pid_list
), GFP_KERNEL
);
1702 pid_list
->pid_max
= READ_ONCE(pid_max
);
1703 /* Only truncating will shrink pid_max */
1704 if (filtered_pids
&& filtered_pids
->pid_max
> pid_list
->pid_max
)
1705 pid_list
->pid_max
= filtered_pids
->pid_max
;
1706 pid_list
->pids
= vzalloc((pid_list
->pid_max
+ 7) >> 3);
1707 if (!pid_list
->pids
) {
1712 if (filtered_pids
) {
1713 /* copy the current bits to the new max */
1714 pid
= find_first_bit(filtered_pids
->pids
,
1715 filtered_pids
->pid_max
);
1716 while (pid
< filtered_pids
->pid_max
) {
1717 set_bit(pid
, pid_list
->pids
);
1718 pid
= find_next_bit(filtered_pids
->pids
,
1719 filtered_pids
->pid_max
,
1729 ret
= trace_get_user(&parser
, ubuf
, cnt
, &this_pos
);
1730 if (ret
< 0 || !trace_parser_loaded(&parser
))
1737 parser
.buffer
[parser
.idx
] = 0;
1740 if (kstrtoul(parser
.buffer
, 0, &val
))
1742 if (val
>= pid_list
->pid_max
)
1747 set_bit(pid
, pid_list
->pids
);
1750 trace_parser_clear(&parser
);
1753 trace_parser_put(&parser
);
1756 vfree(pid_list
->pids
);
1763 /* Cleared the list of pids */
1764 vfree(pid_list
->pids
);
1771 rcu_assign_pointer(tr
->filtered_pids
, pid_list
);
1773 list_for_each_entry(file
, &tr
->events
, list
) {
1774 set_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
1777 if (filtered_pids
) {
1778 synchronize_sched();
1780 vfree(filtered_pids
->pids
);
1781 kfree(filtered_pids
);
1784 * Register a probe that is called before all other probes
1785 * to set ignore_pid if next or prev do not match.
1786 * Register a probe this is called after all other probes
1787 * to only keep ignore_pid set if next pid matches.
1789 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre
,
1791 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post
,
1794 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
,
1796 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
,
1799 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
,
1801 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
,
1804 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre
,
1806 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post
,
1811 * Ignoring of pids is done at task switch. But we have to
1812 * check for those tasks that are currently running.
1813 * Always do this in case a pid was appended or removed.
1815 on_each_cpu(ignore_task_cpu
, tr
, 1);
1818 mutex_unlock(&event_mutex
);
1827 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1828 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1829 static int ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
);
1830 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1832 static const struct seq_operations show_event_seq_ops
= {
1839 static const struct seq_operations show_set_event_seq_ops
= {
1846 static const struct seq_operations show_set_pid_seq_ops
= {
1853 static const struct file_operations ftrace_avail_fops
= {
1854 .open
= ftrace_event_avail_open
,
1856 .llseek
= seq_lseek
,
1857 .release
= seq_release
,
1860 static const struct file_operations ftrace_set_event_fops
= {
1861 .open
= ftrace_event_set_open
,
1863 .write
= ftrace_event_write
,
1864 .llseek
= seq_lseek
,
1865 .release
= ftrace_event_release
,
1868 static const struct file_operations ftrace_set_event_pid_fops
= {
1869 .open
= ftrace_event_set_pid_open
,
1871 .write
= ftrace_event_pid_write
,
1872 .llseek
= seq_lseek
,
1873 .release
= ftrace_event_release
,
1876 static const struct file_operations ftrace_enable_fops
= {
1877 .open
= tracing_open_generic
,
1878 .read
= event_enable_read
,
1879 .write
= event_enable_write
,
1880 .llseek
= default_llseek
,
1883 static const struct file_operations ftrace_event_format_fops
= {
1884 .open
= trace_format_open
,
1886 .llseek
= seq_lseek
,
1887 .release
= seq_release
,
1890 static const struct file_operations ftrace_event_id_fops
= {
1891 .read
= event_id_read
,
1892 .llseek
= default_llseek
,
1895 static const struct file_operations ftrace_event_filter_fops
= {
1896 .open
= tracing_open_generic
,
1897 .read
= event_filter_read
,
1898 .write
= event_filter_write
,
1899 .llseek
= default_llseek
,
1902 static const struct file_operations ftrace_subsystem_filter_fops
= {
1903 .open
= subsystem_open
,
1904 .read
= subsystem_filter_read
,
1905 .write
= subsystem_filter_write
,
1906 .llseek
= default_llseek
,
1907 .release
= subsystem_release
,
1910 static const struct file_operations ftrace_system_enable_fops
= {
1911 .open
= subsystem_open
,
1912 .read
= system_enable_read
,
1913 .write
= system_enable_write
,
1914 .llseek
= default_llseek
,
1915 .release
= subsystem_release
,
1918 static const struct file_operations ftrace_tr_enable_fops
= {
1919 .open
= system_tr_open
,
1920 .read
= system_enable_read
,
1921 .write
= system_enable_write
,
1922 .llseek
= default_llseek
,
1923 .release
= subsystem_release
,
1926 static const struct file_operations ftrace_show_header_fops
= {
1927 .open
= tracing_open_generic
,
1928 .read
= show_header
,
1929 .llseek
= default_llseek
,
1933 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1934 const struct seq_operations
*seq_ops
)
1939 ret
= seq_open(file
, seq_ops
);
1942 m
= file
->private_data
;
1943 /* copy tr over to seq ops */
1944 m
->private = inode
->i_private
;
1949 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1951 struct trace_array
*tr
= inode
->i_private
;
1953 trace_array_put(tr
);
1955 return seq_release(inode
, file
);
1959 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1961 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1963 return ftrace_event_open(inode
, file
, seq_ops
);
1967 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1969 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1970 struct trace_array
*tr
= inode
->i_private
;
1973 if (trace_array_get(tr
) < 0)
1976 if ((file
->f_mode
& FMODE_WRITE
) &&
1977 (file
->f_flags
& O_TRUNC
))
1978 ftrace_clear_events(tr
);
1980 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1982 trace_array_put(tr
);
1987 ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
)
1989 const struct seq_operations
*seq_ops
= &show_set_pid_seq_ops
;
1990 struct trace_array
*tr
= inode
->i_private
;
1993 if (trace_array_get(tr
) < 0)
1996 if ((file
->f_mode
& FMODE_WRITE
) &&
1997 (file
->f_flags
& O_TRUNC
))
1998 ftrace_clear_event_pids(tr
);
2000 ret
= ftrace_event_open(inode
, file
, seq_ops
);
2002 trace_array_put(tr
);
2006 static struct event_subsystem
*
2007 create_new_subsystem(const char *name
)
2009 struct event_subsystem
*system
;
2011 /* need to create new entry */
2012 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
2016 system
->ref_count
= 1;
2018 /* Only allocate if dynamic (kprobes and modules) */
2019 system
->name
= kstrdup_const(name
, GFP_KERNEL
);
2023 system
->filter
= NULL
;
2025 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
2026 if (!system
->filter
)
2029 list_add(&system
->list
, &event_subsystems
);
2034 kfree_const(system
->name
);
2039 static struct dentry
*
2040 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
2041 struct trace_event_file
*file
, struct dentry
*parent
)
2043 struct trace_subsystem_dir
*dir
;
2044 struct event_subsystem
*system
;
2045 struct dentry
*entry
;
2047 /* First see if we did not already create this dir */
2048 list_for_each_entry(dir
, &tr
->systems
, list
) {
2049 system
= dir
->subsystem
;
2050 if (strcmp(system
->name
, name
) == 0) {
2057 /* Now see if the system itself exists. */
2058 list_for_each_entry(system
, &event_subsystems
, list
) {
2059 if (strcmp(system
->name
, name
) == 0)
2062 /* Reset system variable when not found */
2063 if (&system
->list
== &event_subsystems
)
2066 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
2071 system
= create_new_subsystem(name
);
2075 __get_system(system
);
2077 dir
->entry
= tracefs_create_dir(name
, parent
);
2079 pr_warn("Failed to create system directory %s\n", name
);
2080 __put_system(system
);
2087 dir
->subsystem
= system
;
2090 entry
= tracefs_create_file("filter", 0644, dir
->entry
, dir
,
2091 &ftrace_subsystem_filter_fops
);
2093 kfree(system
->filter
);
2094 system
->filter
= NULL
;
2095 pr_warn("Could not create tracefs '%s/filter' entry\n", name
);
2098 trace_create_file("enable", 0644, dir
->entry
, dir
,
2099 &ftrace_system_enable_fops
);
2101 list_add(&dir
->list
, &tr
->systems
);
2108 /* Only print this message if failed on memory allocation */
2109 if (!dir
|| !system
)
2110 pr_warn("No memory to create event subsystem %s\n", name
);
2115 event_create_dir(struct dentry
*parent
, struct trace_event_file
*file
)
2117 struct trace_event_call
*call
= file
->event_call
;
2118 struct trace_array
*tr
= file
->tr
;
2119 struct list_head
*head
;
2120 struct dentry
*d_events
;
2125 * If the trace point header did not define TRACE_SYSTEM
2126 * then the system would be called "TRACE_SYSTEM".
2128 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
2129 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
2135 name
= trace_event_name(call
);
2136 file
->dir
= tracefs_create_dir(name
, d_events
);
2138 pr_warn("Could not create tracefs '%s' directory\n", name
);
2142 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
2143 trace_create_file("enable", 0644, file
->dir
, file
,
2144 &ftrace_enable_fops
);
2146 #ifdef CONFIG_PERF_EVENTS
2147 if (call
->event
.type
&& call
->class->reg
)
2148 trace_create_file("id", 0444, file
->dir
,
2149 (void *)(long)call
->event
.type
,
2150 &ftrace_event_id_fops
);
2154 * Other events may have the same class. Only update
2155 * the fields if they are not already defined.
2157 head
= trace_get_fields(call
);
2158 if (list_empty(head
)) {
2159 ret
= call
->class->define_fields(call
);
2161 pr_warn("Could not initialize trace point events/%s\n",
2166 trace_create_file("filter", 0644, file
->dir
, file
,
2167 &ftrace_event_filter_fops
);
2170 * Only event directories that can be enabled should have
2173 if (!(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
2174 trace_create_file("trigger", 0644, file
->dir
, file
,
2175 &event_trigger_fops
);
2177 #ifdef CONFIG_HIST_TRIGGERS
2178 trace_create_file("hist", 0444, file
->dir
, file
,
2181 trace_create_file("format", 0444, file
->dir
, call
,
2182 &ftrace_event_format_fops
);
2187 static void remove_event_from_tracers(struct trace_event_call
*call
)
2189 struct trace_event_file
*file
;
2190 struct trace_array
*tr
;
2192 do_for_each_event_file_safe(tr
, file
) {
2193 if (file
->event_call
!= call
)
2196 remove_event_file_dir(file
);
2198 * The do_for_each_event_file_safe() is
2199 * a double loop. After finding the call for this
2200 * trace_array, we use break to jump to the next
2204 } while_for_each_event_file();
2207 static void event_remove(struct trace_event_call
*call
)
2209 struct trace_array
*tr
;
2210 struct trace_event_file
*file
;
2212 do_for_each_event_file(tr
, file
) {
2213 if (file
->event_call
!= call
)
2215 ftrace_event_enable_disable(file
, 0);
2217 * The do_for_each_event_file() is
2218 * a double loop. After finding the call for this
2219 * trace_array, we use break to jump to the next
2223 } while_for_each_event_file();
2225 if (call
->event
.funcs
)
2226 __unregister_trace_event(&call
->event
);
2227 remove_event_from_tracers(call
);
2228 list_del(&call
->list
);
2231 static int event_init(struct trace_event_call
*call
)
2236 name
= trace_event_name(call
);
2240 if (call
->class->raw_init
) {
2241 ret
= call
->class->raw_init(call
);
2242 if (ret
< 0 && ret
!= -ENOSYS
)
2243 pr_warn("Could not initialize trace events/%s\n", name
);
2250 __register_event(struct trace_event_call
*call
, struct module
*mod
)
2254 ret
= event_init(call
);
2258 list_add(&call
->list
, &ftrace_events
);
2264 static char *enum_replace(char *ptr
, struct trace_enum_map
*map
, int len
)
2269 /* Find the length of the enum value as a string */
2270 elen
= snprintf(ptr
, 0, "%ld", map
->enum_value
);
2271 /* Make sure there's enough room to replace the string with the value */
2275 snprintf(ptr
, elen
+ 1, "%ld", map
->enum_value
);
2277 /* Get the rest of the string of ptr */
2278 rlen
= strlen(ptr
+ len
);
2279 memmove(ptr
+ elen
, ptr
+ len
, rlen
);
2280 /* Make sure we end the new string */
2281 ptr
[elen
+ rlen
] = 0;
2286 static void update_event_printk(struct trace_event_call
*call
,
2287 struct trace_enum_map
*map
)
2291 int len
= strlen(map
->enum_string
);
2293 for (ptr
= call
->print_fmt
; *ptr
; ptr
++) {
2307 if (isdigit(*ptr
)) {
2311 /* Check for alpha chars like ULL */
2312 } while (isalnum(*ptr
));
2316 * A number must have some kind of delimiter after
2317 * it, and we can ignore that too.
2321 if (isalpha(*ptr
) || *ptr
== '_') {
2322 if (strncmp(map
->enum_string
, ptr
, len
) == 0 &&
2323 !isalnum(ptr
[len
]) && ptr
[len
] != '_') {
2324 ptr
= enum_replace(ptr
, map
, len
);
2325 /* Hmm, enum string smaller than value */
2326 if (WARN_ON_ONCE(!ptr
))
2329 * No need to decrement here, as enum_replace()
2330 * returns the pointer to the character passed
2331 * the enum, and two enums can not be placed
2332 * back to back without something in between.
2333 * We can skip that something in between.
2340 } while (isalnum(*ptr
) || *ptr
== '_');
2344 * If what comes after this variable is a '.' or
2345 * '->' then we can continue to ignore that string.
2347 if (*ptr
== '.' || (ptr
[0] == '-' && ptr
[1] == '>')) {
2348 ptr
+= *ptr
== '.' ? 1 : 2;
2354 * Once again, we can skip the delimiter that came
2362 void trace_event_enum_update(struct trace_enum_map
**map
, int len
)
2364 struct trace_event_call
*call
, *p
;
2365 const char *last_system
= NULL
;
2369 down_write(&trace_event_sem
);
2370 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2371 /* events are usually grouped together with systems */
2372 if (!last_system
|| call
->class->system
!= last_system
) {
2374 last_system
= call
->class->system
;
2377 for (i
= last_i
; i
< len
; i
++) {
2378 if (call
->class->system
== map
[i
]->system
) {
2379 /* Save the first system if need be */
2382 update_event_printk(call
, map
[i
]);
2386 up_write(&trace_event_sem
);
2389 static struct trace_event_file
*
2390 trace_create_new_event(struct trace_event_call
*call
,
2391 struct trace_array
*tr
)
2393 struct trace_event_file
*file
;
2395 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
2399 file
->event_call
= call
;
2401 atomic_set(&file
->sm_ref
, 0);
2402 atomic_set(&file
->tm_ref
, 0);
2403 INIT_LIST_HEAD(&file
->triggers
);
2404 list_add(&file
->list
, &tr
->events
);
2409 /* Add an event to a trace directory */
2411 __trace_add_new_event(struct trace_event_call
*call
, struct trace_array
*tr
)
2413 struct trace_event_file
*file
;
2415 file
= trace_create_new_event(call
, tr
);
2419 return event_create_dir(tr
->event_dir
, file
);
2423 * Just create a decriptor for early init. A descriptor is required
2424 * for enabling events at boot. We want to enable events before
2425 * the filesystem is initialized.
2428 __trace_early_add_new_event(struct trace_event_call
*call
,
2429 struct trace_array
*tr
)
2431 struct trace_event_file
*file
;
2433 file
= trace_create_new_event(call
, tr
);
2440 struct ftrace_module_file_ops
;
2441 static void __add_event_to_tracers(struct trace_event_call
*call
);
2443 /* Add an additional event_call dynamically */
2444 int trace_add_event_call(struct trace_event_call
*call
)
2447 mutex_lock(&trace_types_lock
);
2448 mutex_lock(&event_mutex
);
2450 ret
= __register_event(call
, NULL
);
2452 __add_event_to_tracers(call
);
2454 mutex_unlock(&event_mutex
);
2455 mutex_unlock(&trace_types_lock
);
2460 * Must be called under locking of trace_types_lock, event_mutex and
2463 static void __trace_remove_event_call(struct trace_event_call
*call
)
2466 trace_destroy_fields(call
);
2467 free_event_filter(call
->filter
);
2468 call
->filter
= NULL
;
2471 static int probe_remove_event_call(struct trace_event_call
*call
)
2473 struct trace_array
*tr
;
2474 struct trace_event_file
*file
;
2476 #ifdef CONFIG_PERF_EVENTS
2477 if (call
->perf_refcount
)
2480 do_for_each_event_file(tr
, file
) {
2481 if (file
->event_call
!= call
)
2484 * We can't rely on ftrace_event_enable_disable(enable => 0)
2485 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2486 * TRACE_REG_UNREGISTER.
2488 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
2491 * The do_for_each_event_file_safe() is
2492 * a double loop. After finding the call for this
2493 * trace_array, we use break to jump to the next
2497 } while_for_each_event_file();
2499 __trace_remove_event_call(call
);
2504 /* Remove an event_call */
2505 int trace_remove_event_call(struct trace_event_call
*call
)
2509 mutex_lock(&trace_types_lock
);
2510 mutex_lock(&event_mutex
);
2511 down_write(&trace_event_sem
);
2512 ret
= probe_remove_event_call(call
);
2513 up_write(&trace_event_sem
);
2514 mutex_unlock(&event_mutex
);
2515 mutex_unlock(&trace_types_lock
);
2520 #define for_each_event(event, start, end) \
2521 for (event = start; \
2522 (unsigned long)event < (unsigned long)end; \
2525 #ifdef CONFIG_MODULES
2527 static void trace_module_add_events(struct module
*mod
)
2529 struct trace_event_call
**call
, **start
, **end
;
2531 if (!mod
->num_trace_events
)
2534 /* Don't add infrastructure for mods without tracepoints */
2535 if (trace_module_has_bad_taint(mod
)) {
2536 pr_err("%s: module has bad taint, not creating trace events\n",
2541 start
= mod
->trace_events
;
2542 end
= mod
->trace_events
+ mod
->num_trace_events
;
2544 for_each_event(call
, start
, end
) {
2545 __register_event(*call
, mod
);
2546 __add_event_to_tracers(*call
);
2550 static void trace_module_remove_events(struct module
*mod
)
2552 struct trace_event_call
*call
, *p
;
2553 bool clear_trace
= false;
2555 down_write(&trace_event_sem
);
2556 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2557 if (call
->mod
== mod
) {
2558 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
2560 __trace_remove_event_call(call
);
2563 up_write(&trace_event_sem
);
2566 * It is safest to reset the ring buffer if the module being unloaded
2567 * registered any events that were used. The only worry is if
2568 * a new module gets loaded, and takes on the same id as the events
2569 * of this module. When printing out the buffer, traced events left
2570 * over from this module may be passed to the new module events and
2571 * unexpected results may occur.
2574 tracing_reset_all_online_cpus();
2577 static int trace_module_notify(struct notifier_block
*self
,
2578 unsigned long val
, void *data
)
2580 struct module
*mod
= data
;
2582 mutex_lock(&trace_types_lock
);
2583 mutex_lock(&event_mutex
);
2585 case MODULE_STATE_COMING
:
2586 trace_module_add_events(mod
);
2588 case MODULE_STATE_GOING
:
2589 trace_module_remove_events(mod
);
2592 mutex_unlock(&event_mutex
);
2593 mutex_unlock(&trace_types_lock
);
2598 static struct notifier_block trace_module_nb
= {
2599 .notifier_call
= trace_module_notify
,
2600 .priority
= 1, /* higher than trace.c module notify */
2602 #endif /* CONFIG_MODULES */
2604 /* Create a new event directory structure for a trace directory. */
2606 __trace_add_event_dirs(struct trace_array
*tr
)
2608 struct trace_event_call
*call
;
2611 list_for_each_entry(call
, &ftrace_events
, list
) {
2612 ret
= __trace_add_new_event(call
, tr
);
2614 pr_warn("Could not create directory for event %s\n",
2615 trace_event_name(call
));
2619 struct trace_event_file
*
2620 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
2622 struct trace_event_file
*file
;
2623 struct trace_event_call
*call
;
2626 list_for_each_entry(file
, &tr
->events
, list
) {
2628 call
= file
->event_call
;
2629 name
= trace_event_name(call
);
2631 if (!name
|| !call
->class || !call
->class->reg
)
2634 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
2637 if (strcmp(event
, name
) == 0 &&
2638 strcmp(system
, call
->class->system
) == 0)
2644 #ifdef CONFIG_DYNAMIC_FTRACE
2647 #define ENABLE_EVENT_STR "enable_event"
2648 #define DISABLE_EVENT_STR "disable_event"
2650 struct event_probe_data
{
2651 struct trace_event_file
*file
;
2652 unsigned long count
;
2658 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2660 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2661 struct event_probe_data
*data
= *pdata
;
2667 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2669 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2673 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2675 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2676 struct event_probe_data
*data
= *pdata
;
2684 /* Skip if the event is in a state we want to switch to */
2685 if (data
->enable
== !(data
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
2688 if (data
->count
!= -1)
2691 event_enable_probe(ip
, parent_ip
, _data
);
2695 event_enable_print(struct seq_file
*m
, unsigned long ip
,
2696 struct ftrace_probe_ops
*ops
, void *_data
)
2698 struct event_probe_data
*data
= _data
;
2700 seq_printf(m
, "%ps:", (void *)ip
);
2702 seq_printf(m
, "%s:%s:%s",
2703 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
2704 data
->file
->event_call
->class->system
,
2705 trace_event_name(data
->file
->event_call
));
2707 if (data
->count
== -1)
2708 seq_puts(m
, ":unlimited\n");
2710 seq_printf(m
, ":count=%ld\n", data
->count
);
2716 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2719 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2720 struct event_probe_data
*data
= *pdata
;
2727 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2730 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2731 struct event_probe_data
*data
= *pdata
;
2733 if (WARN_ON_ONCE(data
->ref
<= 0))
2738 /* Remove the SOFT_MODE flag */
2739 __ftrace_event_enable_disable(data
->file
, 0, 1);
2740 module_put(data
->file
->event_call
->mod
);
2746 static struct ftrace_probe_ops event_enable_probe_ops
= {
2747 .func
= event_enable_probe
,
2748 .print
= event_enable_print
,
2749 .init
= event_enable_init
,
2750 .free
= event_enable_free
,
2753 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2754 .func
= event_enable_count_probe
,
2755 .print
= event_enable_print
,
2756 .init
= event_enable_init
,
2757 .free
= event_enable_free
,
2760 static struct ftrace_probe_ops event_disable_probe_ops
= {
2761 .func
= event_enable_probe
,
2762 .print
= event_enable_print
,
2763 .init
= event_enable_init
,
2764 .free
= event_enable_free
,
2767 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2768 .func
= event_enable_count_probe
,
2769 .print
= event_enable_print
,
2770 .init
= event_enable_init
,
2771 .free
= event_enable_free
,
2775 event_enable_func(struct ftrace_hash
*hash
,
2776 char *glob
, char *cmd
, char *param
, int enabled
)
2778 struct trace_array
*tr
= top_trace_array();
2779 struct trace_event_file
*file
;
2780 struct ftrace_probe_ops
*ops
;
2781 struct event_probe_data
*data
;
2791 /* hash funcs only work with set_ftrace_filter */
2792 if (!enabled
|| !param
)
2795 system
= strsep(¶m
, ":");
2799 event
= strsep(¶m
, ":");
2801 mutex_lock(&event_mutex
);
2804 file
= find_event_file(tr
, system
, event
);
2808 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2811 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2813 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2815 if (glob
[0] == '!') {
2816 unregister_ftrace_function_probe_func(glob
+1, ops
);
2822 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2826 data
->enable
= enable
;
2833 number
= strsep(¶m
, ":");
2836 if (!strlen(number
))
2840 * We use the callback data field (which is a pointer)
2843 ret
= kstrtoul(number
, 0, &data
->count
);
2848 /* Don't let event modules unload while probe registered */
2849 ret
= try_module_get(file
->event_call
->mod
);
2855 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2858 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2860 * The above returns on success the # of functions enabled,
2861 * but if it didn't find any functions it returns zero.
2862 * Consider no functions a failure too.
2869 /* Just return zero, not the number of enabled functions */
2872 mutex_unlock(&event_mutex
);
2876 __ftrace_event_enable_disable(file
, 0, 1);
2878 module_put(file
->event_call
->mod
);
2884 static struct ftrace_func_command event_enable_cmd
= {
2885 .name
= ENABLE_EVENT_STR
,
2886 .func
= event_enable_func
,
2889 static struct ftrace_func_command event_disable_cmd
= {
2890 .name
= DISABLE_EVENT_STR
,
2891 .func
= event_enable_func
,
2894 static __init
int register_event_cmds(void)
2898 ret
= register_ftrace_command(&event_enable_cmd
);
2899 if (WARN_ON(ret
< 0))
2901 ret
= register_ftrace_command(&event_disable_cmd
);
2902 if (WARN_ON(ret
< 0))
2903 unregister_ftrace_command(&event_enable_cmd
);
2907 static inline int register_event_cmds(void) { return 0; }
2908 #endif /* CONFIG_DYNAMIC_FTRACE */
2911 * The top level array has already had its trace_event_file
2912 * descriptors created in order to allow for early events to
2913 * be recorded. This function is called after the tracefs has been
2914 * initialized, and we now have to create the files associated
2918 __trace_early_add_event_dirs(struct trace_array
*tr
)
2920 struct trace_event_file
*file
;
2924 list_for_each_entry(file
, &tr
->events
, list
) {
2925 ret
= event_create_dir(tr
->event_dir
, file
);
2927 pr_warn("Could not create directory for event %s\n",
2928 trace_event_name(file
->event_call
));
2933 * For early boot up, the top trace array requires to have
2934 * a list of events that can be enabled. This must be done before
2935 * the filesystem is set up in order to allow events to be traced
2939 __trace_early_add_events(struct trace_array
*tr
)
2941 struct trace_event_call
*call
;
2944 list_for_each_entry(call
, &ftrace_events
, list
) {
2945 /* Early boot up should not have any modules loaded */
2946 if (WARN_ON_ONCE(call
->mod
))
2949 ret
= __trace_early_add_new_event(call
, tr
);
2951 pr_warn("Could not create early event %s\n",
2952 trace_event_name(call
));
2956 /* Remove the event directory structure for a trace directory. */
2958 __trace_remove_event_dirs(struct trace_array
*tr
)
2960 struct trace_event_file
*file
, *next
;
2962 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2963 remove_event_file_dir(file
);
2966 static void __add_event_to_tracers(struct trace_event_call
*call
)
2968 struct trace_array
*tr
;
2970 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2971 __trace_add_new_event(call
, tr
);
2974 extern struct trace_event_call
*__start_ftrace_events
[];
2975 extern struct trace_event_call
*__stop_ftrace_events
[];
2977 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2979 static __init
int setup_trace_event(char *str
)
2981 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2982 ring_buffer_expanded
= true;
2983 tracing_selftest_disabled
= true;
2987 __setup("trace_event=", setup_trace_event
);
2989 /* Expects to have event_mutex held when called */
2991 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2993 struct dentry
*d_events
;
2994 struct dentry
*entry
;
2996 entry
= tracefs_create_file("set_event", 0644, parent
,
2997 tr
, &ftrace_set_event_fops
);
2999 pr_warn("Could not create tracefs 'set_event' entry\n");
3003 d_events
= tracefs_create_dir("events", parent
);
3005 pr_warn("Could not create tracefs 'events' directory\n");
3009 entry
= tracefs_create_file("set_event_pid", 0644, parent
,
3010 tr
, &ftrace_set_event_pid_fops
);
3012 /* ring buffer internal formats */
3013 trace_create_file("header_page", 0444, d_events
,
3014 ring_buffer_print_page_header
,
3015 &ftrace_show_header_fops
);
3017 trace_create_file("header_event", 0444, d_events
,
3018 ring_buffer_print_entry_header
,
3019 &ftrace_show_header_fops
);
3021 trace_create_file("enable", 0644, d_events
,
3022 tr
, &ftrace_tr_enable_fops
);
3024 tr
->event_dir
= d_events
;
3030 * event_trace_add_tracer - add a instance of a trace_array to events
3031 * @parent: The parent dentry to place the files/directories for events in
3032 * @tr: The trace array associated with these events
3034 * When a new instance is created, it needs to set up its events
3035 * directory, as well as other files associated with events. It also
3036 * creates the event hierachry in the @parent/events directory.
3038 * Returns 0 on success.
3040 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3044 mutex_lock(&event_mutex
);
3046 ret
= create_event_toplevel_files(parent
, tr
);
3050 down_write(&trace_event_sem
);
3051 __trace_add_event_dirs(tr
);
3052 up_write(&trace_event_sem
);
3055 mutex_unlock(&event_mutex
);
3061 * The top trace array already had its file descriptors created.
3062 * Now the files themselves need to be created.
3065 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3069 mutex_lock(&event_mutex
);
3071 ret
= create_event_toplevel_files(parent
, tr
);
3075 down_write(&trace_event_sem
);
3076 __trace_early_add_event_dirs(tr
);
3077 up_write(&trace_event_sem
);
3080 mutex_unlock(&event_mutex
);
3085 int event_trace_del_tracer(struct trace_array
*tr
)
3087 mutex_lock(&event_mutex
);
3089 /* Disable any event triggers and associated soft-disabled events */
3090 clear_event_triggers(tr
);
3092 /* Clear the pid list */
3093 __ftrace_clear_event_pids(tr
);
3095 /* Disable any running events */
3096 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
3098 /* Access to events are within rcu_read_lock_sched() */
3099 synchronize_sched();
3101 down_write(&trace_event_sem
);
3102 __trace_remove_event_dirs(tr
);
3103 tracefs_remove_recursive(tr
->event_dir
);
3104 up_write(&trace_event_sem
);
3106 tr
->event_dir
= NULL
;
3108 mutex_unlock(&event_mutex
);
3113 static __init
int event_trace_memsetup(void)
3115 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
3116 file_cachep
= KMEM_CACHE(trace_event_file
, SLAB_PANIC
);
3121 early_enable_events(struct trace_array
*tr
, bool disable_first
)
3123 char *buf
= bootup_event_buf
;
3128 token
= strsep(&buf
, ",");
3134 /* Restarting syscalls requires that we stop them first */
3136 ftrace_set_clr_event(tr
, token
, 0);
3138 ret
= ftrace_set_clr_event(tr
, token
, 1);
3140 pr_warn("Failed to enable trace event: %s\n", token
);
3143 /* Put back the comma to allow this to be called again */
3149 static __init
int event_trace_enable(void)
3151 struct trace_array
*tr
= top_trace_array();
3152 struct trace_event_call
**iter
, *call
;
3158 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
3161 ret
= event_init(call
);
3163 list_add(&call
->list
, &ftrace_events
);
3167 * We need the top trace array to have a working set of trace
3168 * points at early init, before the debug files and directories
3169 * are created. Create the file entries now, and attach them
3170 * to the actual file dentries later.
3172 __trace_early_add_events(tr
);
3174 early_enable_events(tr
, false);
3176 trace_printk_start_comm();
3178 register_event_cmds();
3180 register_trigger_cmds();
3186 * event_trace_enable() is called from trace_event_init() first to
3187 * initialize events and perhaps start any events that are on the
3188 * command line. Unfortunately, there are some events that will not
3189 * start this early, like the system call tracepoints that need
3190 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3191 * is called before pid 1 starts, and this flag is never set, making
3192 * the syscall tracepoint never get reached, but the event is enabled
3193 * regardless (and not doing anything).
3195 static __init
int event_trace_enable_again(void)
3197 struct trace_array
*tr
;
3199 tr
= top_trace_array();
3203 early_enable_events(tr
, true);
3208 early_initcall(event_trace_enable_again
);
3210 static __init
int event_trace_init(void)
3212 struct trace_array
*tr
;
3213 struct dentry
*d_tracer
;
3214 struct dentry
*entry
;
3217 tr
= top_trace_array();
3221 d_tracer
= tracing_init_dentry();
3222 if (IS_ERR(d_tracer
))
3225 entry
= tracefs_create_file("available_events", 0444, d_tracer
,
3226 tr
, &ftrace_avail_fops
);
3228 pr_warn("Could not create tracefs 'available_events' entry\n");
3230 if (trace_define_generic_fields())
3231 pr_warn("tracing: Failed to allocated generic fields");
3233 if (trace_define_common_fields())
3234 pr_warn("tracing: Failed to allocate common fields");
3236 ret
= early_event_add_tracer(d_tracer
, tr
);
3240 #ifdef CONFIG_MODULES
3241 ret
= register_module_notifier(&trace_module_nb
);
3243 pr_warn("Failed to register trace events module notifier\n");
3248 void __init
trace_event_init(void)
3250 event_trace_memsetup();
3251 init_ftrace_syscalls();
3252 event_trace_enable();
3255 fs_initcall(event_trace_init
);
3257 #ifdef CONFIG_FTRACE_STARTUP_TEST
3259 static DEFINE_SPINLOCK(test_spinlock
);
3260 static DEFINE_SPINLOCK(test_spinlock_irq
);
3261 static DEFINE_MUTEX(test_mutex
);
3263 static __init
void test_work(struct work_struct
*dummy
)
3265 spin_lock(&test_spinlock
);
3266 spin_lock_irq(&test_spinlock_irq
);
3268 spin_unlock_irq(&test_spinlock_irq
);
3269 spin_unlock(&test_spinlock
);
3271 mutex_lock(&test_mutex
);
3273 mutex_unlock(&test_mutex
);
3276 static __init
int event_test_thread(void *unused
)
3280 test_malloc
= kmalloc(1234, GFP_KERNEL
);
3282 pr_info("failed to kmalloc\n");
3284 schedule_on_each_cpu(test_work
);
3288 set_current_state(TASK_INTERRUPTIBLE
);
3289 while (!kthread_should_stop()) {
3291 set_current_state(TASK_INTERRUPTIBLE
);
3293 __set_current_state(TASK_RUNNING
);
3299 * Do various things that may trigger events.
3301 static __init
void event_test_stuff(void)
3303 struct task_struct
*test_thread
;
3305 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
3307 kthread_stop(test_thread
);
3311 * For every trace event defined, we will test each trace point separately,
3312 * and then by groups, and finally all trace points.
3314 static __init
void event_trace_self_tests(void)
3316 struct trace_subsystem_dir
*dir
;
3317 struct trace_event_file
*file
;
3318 struct trace_event_call
*call
;
3319 struct event_subsystem
*system
;
3320 struct trace_array
*tr
;
3323 tr
= top_trace_array();
3327 pr_info("Running tests on trace events:\n");
3329 list_for_each_entry(file
, &tr
->events
, list
) {
3331 call
= file
->event_call
;
3333 /* Only test those that have a probe */
3334 if (!call
->class || !call
->class->probe
)
3338 * Testing syscall events here is pretty useless, but
3339 * we still do it if configured. But this is time consuming.
3340 * What we really need is a user thread to perform the
3341 * syscalls as we test.
3343 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3344 if (call
->class->system
&&
3345 strcmp(call
->class->system
, "syscalls") == 0)
3349 pr_info("Testing event %s: ", trace_event_name(call
));
3352 * If an event is already enabled, someone is using
3353 * it and the self test should not be on.
3355 if (file
->flags
& EVENT_FILE_FL_ENABLED
) {
3356 pr_warn("Enabled event during self test!\n");
3361 ftrace_event_enable_disable(file
, 1);
3363 ftrace_event_enable_disable(file
, 0);
3368 /* Now test at the sub system level */
3370 pr_info("Running tests on trace event systems:\n");
3372 list_for_each_entry(dir
, &tr
->systems
, list
) {
3374 system
= dir
->subsystem
;
3376 /* the ftrace system is special, skip it */
3377 if (strcmp(system
->name
, "ftrace") == 0)
3380 pr_info("Testing event system %s: ", system
->name
);
3382 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
3383 if (WARN_ON_ONCE(ret
)) {
3384 pr_warn("error enabling system %s\n",
3391 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
3392 if (WARN_ON_ONCE(ret
)) {
3393 pr_warn("error disabling system %s\n",
3401 /* Test with all events enabled */
3403 pr_info("Running tests on all trace events:\n");
3404 pr_info("Testing all events: ");
3406 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
3407 if (WARN_ON_ONCE(ret
)) {
3408 pr_warn("error enabling all events\n");
3415 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
3416 if (WARN_ON_ONCE(ret
)) {
3417 pr_warn("error disabling all events\n");
3424 #ifdef CONFIG_FUNCTION_TRACER
3426 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
3428 static struct trace_event_file event_trace_file __initdata
;
3431 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
3432 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
3434 struct ring_buffer_event
*event
;
3435 struct ring_buffer
*buffer
;
3436 struct ftrace_entry
*entry
;
3437 unsigned long flags
;
3442 pc
= preempt_count();
3443 preempt_disable_notrace();
3444 cpu
= raw_smp_processor_id();
3445 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
3450 local_save_flags(flags
);
3452 event
= trace_event_buffer_lock_reserve(&buffer
, &event_trace_file
,
3453 TRACE_FN
, sizeof(*entry
),
3457 entry
= ring_buffer_event_data(event
);
3459 entry
->parent_ip
= parent_ip
;
3461 event_trigger_unlock_commit(&event_trace_file
, buffer
, event
,
3464 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
3465 preempt_enable_notrace();
3468 static struct ftrace_ops trace_ops __initdata
=
3470 .func
= function_test_events_call
,
3471 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
3474 static __init
void event_trace_self_test_with_function(void)
3478 event_trace_file
.tr
= top_trace_array();
3479 if (WARN_ON(!event_trace_file
.tr
))
3482 ret
= register_ftrace_function(&trace_ops
);
3483 if (WARN_ON(ret
< 0)) {
3484 pr_info("Failed to enable function tracer for event tests\n");
3487 pr_info("Running tests again, along with the function tracer\n");
3488 event_trace_self_tests();
3489 unregister_ftrace_function(&trace_ops
);
3492 static __init
void event_trace_self_test_with_function(void)
3497 static __init
int event_trace_self_tests_init(void)
3499 if (!tracing_selftest_disabled
) {
3500 event_trace_self_tests();
3501 event_trace_self_test_with_function();
3507 late_initcall(event_trace_self_tests_init
);