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
);
207 int trace_event_raw_init(struct trace_event_call
*call
)
211 id
= register_trace_event(&call
->event
);
217 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
219 bool trace_event_ignore_this_pid(struct trace_event_file
*trace_file
)
221 struct trace_array
*tr
= trace_file
->tr
;
222 struct trace_array_cpu
*data
;
223 struct trace_pid_list
*pid_list
;
225 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
229 data
= this_cpu_ptr(tr
->trace_buffer
.data
);
231 return data
->ignore_pid
;
233 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid
);
235 void *trace_event_buffer_reserve(struct trace_event_buffer
*fbuffer
,
236 struct trace_event_file
*trace_file
,
239 struct trace_event_call
*event_call
= trace_file
->event_call
;
241 if ((trace_file
->flags
& EVENT_FILE_FL_PID_FILTER
) &&
242 trace_event_ignore_this_pid(trace_file
))
245 local_save_flags(fbuffer
->flags
);
246 fbuffer
->pc
= preempt_count();
247 fbuffer
->trace_file
= trace_file
;
250 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, trace_file
,
251 event_call
->event
.type
, len
,
252 fbuffer
->flags
, fbuffer
->pc
);
256 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
257 return fbuffer
->entry
;
259 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve
);
261 static DEFINE_SPINLOCK(tracepoint_iter_lock
);
263 static void output_printk(struct trace_event_buffer
*fbuffer
)
265 struct trace_event_call
*event_call
;
266 struct trace_event
*event
;
268 struct trace_iterator
*iter
= tracepoint_print_iter
;
273 event_call
= fbuffer
->trace_file
->event_call
;
274 if (!event_call
|| !event_call
->event
.funcs
||
275 !event_call
->event
.funcs
->trace
)
278 event
= &fbuffer
->trace_file
->event_call
->event
;
280 spin_lock_irqsave(&tracepoint_iter_lock
, flags
);
281 trace_seq_init(&iter
->seq
);
282 iter
->ent
= fbuffer
->entry
;
283 event_call
->event
.funcs
->trace(iter
, 0, event
);
284 trace_seq_putc(&iter
->seq
, 0);
285 printk("%s", iter
->seq
.buffer
);
287 spin_unlock_irqrestore(&tracepoint_iter_lock
, flags
);
290 void trace_event_buffer_commit(struct trace_event_buffer
*fbuffer
)
292 if (tracepoint_printk
)
293 output_printk(fbuffer
);
295 event_trigger_unlock_commit(fbuffer
->trace_file
, fbuffer
->buffer
,
296 fbuffer
->event
, fbuffer
->entry
,
297 fbuffer
->flags
, fbuffer
->pc
);
299 EXPORT_SYMBOL_GPL(trace_event_buffer_commit
);
301 int trace_event_reg(struct trace_event_call
*call
,
302 enum trace_reg type
, void *data
)
304 struct trace_event_file
*file
= data
;
306 WARN_ON(!(call
->flags
& TRACE_EVENT_FL_TRACEPOINT
));
308 case TRACE_REG_REGISTER
:
309 return tracepoint_probe_register(call
->tp
,
312 case TRACE_REG_UNREGISTER
:
313 tracepoint_probe_unregister(call
->tp
,
318 #ifdef CONFIG_PERF_EVENTS
319 case TRACE_REG_PERF_REGISTER
:
320 return tracepoint_probe_register(call
->tp
,
321 call
->class->perf_probe
,
323 case TRACE_REG_PERF_UNREGISTER
:
324 tracepoint_probe_unregister(call
->tp
,
325 call
->class->perf_probe
,
328 case TRACE_REG_PERF_OPEN
:
329 case TRACE_REG_PERF_CLOSE
:
330 case TRACE_REG_PERF_ADD
:
331 case TRACE_REG_PERF_DEL
:
337 EXPORT_SYMBOL_GPL(trace_event_reg
);
339 void trace_event_enable_cmd_record(bool enable
)
341 struct trace_event_file
*file
;
342 struct trace_array
*tr
;
344 mutex_lock(&event_mutex
);
345 do_for_each_event_file(tr
, file
) {
347 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
))
351 tracing_start_cmdline_record();
352 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
354 tracing_stop_cmdline_record();
355 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
357 } while_for_each_event_file();
358 mutex_unlock(&event_mutex
);
361 static int __ftrace_event_enable_disable(struct trace_event_file
*file
,
362 int enable
, int soft_disable
)
364 struct trace_event_call
*call
= file
->event_call
;
365 struct trace_array
*tr
= file
->tr
;
372 * When soft_disable is set and enable is cleared, the sm_ref
373 * reference counter is decremented. If it reaches 0, we want
374 * to clear the SOFT_DISABLED flag but leave the event in the
375 * state that it was. That is, if the event was enabled and
376 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
377 * is set we do not want the event to be enabled before we
380 * When soft_disable is not set but the SOFT_MODE flag is,
381 * we do nothing. Do not disable the tracepoint, otherwise
382 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
385 if (atomic_dec_return(&file
->sm_ref
) > 0)
387 disable
= file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
;
388 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
390 disable
= !(file
->flags
& EVENT_FILE_FL_SOFT_MODE
);
392 if (disable
&& (file
->flags
& EVENT_FILE_FL_ENABLED
)) {
393 clear_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
394 if (file
->flags
& EVENT_FILE_FL_RECORDED_CMD
) {
395 tracing_stop_cmdline_record();
396 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
398 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
400 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
401 if (file
->flags
& EVENT_FILE_FL_SOFT_MODE
)
402 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
404 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
408 * When soft_disable is set and enable is set, we want to
409 * register the tracepoint for the event, but leave the event
410 * as is. That means, if the event was already enabled, we do
411 * nothing (but set SOFT_MODE). If the event is disabled, we
412 * set SOFT_DISABLED before enabling the event tracepoint, so
413 * it still seems to be disabled.
416 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
418 if (atomic_inc_return(&file
->sm_ref
) > 1)
420 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
423 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
)) {
425 /* Keep the event disabled, when going to SOFT_MODE. */
427 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
429 if (tr
->trace_flags
& TRACE_ITER_RECORD_CMD
) {
430 tracing_start_cmdline_record();
431 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
433 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
435 tracing_stop_cmdline_record();
436 pr_info("event trace: Could not enable event "
437 "%s\n", trace_event_name(call
));
440 set_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
442 /* WAS_ENABLED gets set but never cleared. */
443 call
->flags
|= TRACE_EVENT_FL_WAS_ENABLED
;
451 int trace_event_enable_disable(struct trace_event_file
*file
,
452 int enable
, int soft_disable
)
454 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
457 static int ftrace_event_enable_disable(struct trace_event_file
*file
,
460 return __ftrace_event_enable_disable(file
, enable
, 0);
463 static void ftrace_clear_events(struct trace_array
*tr
)
465 struct trace_event_file
*file
;
467 mutex_lock(&event_mutex
);
468 list_for_each_entry(file
, &tr
->events
, list
) {
469 ftrace_event_enable_disable(file
, 0);
471 mutex_unlock(&event_mutex
);
474 /* Shouldn't this be in a header? */
477 /* Returns true if found in filter */
479 find_filtered_pid(struct trace_pid_list
*filtered_pids
, pid_t search_pid
)
482 * If pid_max changed after filtered_pids was created, we
483 * by default ignore all pids greater than the previous pid_max.
485 if (search_pid
>= filtered_pids
->pid_max
)
488 return test_bit(search_pid
, filtered_pids
->pids
);
492 ignore_this_task(struct trace_pid_list
*filtered_pids
, struct task_struct
*task
)
495 * Return false, because if filtered_pids does not exist,
496 * all pids are good to trace.
501 return !find_filtered_pid(filtered_pids
, task
->pid
);
504 static void filter_add_remove_task(struct trace_pid_list
*pid_list
,
505 struct task_struct
*self
,
506 struct task_struct
*task
)
511 /* For forks, we only add if the forking task is listed */
513 if (!find_filtered_pid(pid_list
, self
->pid
))
517 /* Sorry, but we don't support pid_max changing after setting */
518 if (task
->pid
>= pid_list
->pid_max
)
521 /* "self" is set for forks, and NULL for exits */
523 set_bit(task
->pid
, pid_list
->pids
);
525 clear_bit(task
->pid
, pid_list
->pids
);
529 event_filter_pid_sched_process_exit(void *data
, struct task_struct
*task
)
531 struct trace_pid_list
*pid_list
;
532 struct trace_array
*tr
= data
;
534 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
535 filter_add_remove_task(pid_list
, NULL
, task
);
539 event_filter_pid_sched_process_fork(void *data
,
540 struct task_struct
*self
,
541 struct task_struct
*task
)
543 struct trace_pid_list
*pid_list
;
544 struct trace_array
*tr
= data
;
546 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
547 filter_add_remove_task(pid_list
, self
, task
);
550 void trace_event_follow_fork(struct trace_array
*tr
, bool enable
)
553 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork
,
555 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit
,
558 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork
,
560 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit
,
566 event_filter_pid_sched_switch_probe_pre(void *data
, bool preempt
,
567 struct task_struct
*prev
, struct task_struct
*next
)
569 struct trace_array
*tr
= data
;
570 struct trace_pid_list
*pid_list
;
572 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
574 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
575 ignore_this_task(pid_list
, prev
) &&
576 ignore_this_task(pid_list
, next
));
580 event_filter_pid_sched_switch_probe_post(void *data
, bool preempt
,
581 struct task_struct
*prev
, struct task_struct
*next
)
583 struct trace_array
*tr
= data
;
584 struct trace_pid_list
*pid_list
;
586 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
588 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
589 ignore_this_task(pid_list
, next
));
593 event_filter_pid_sched_wakeup_probe_pre(void *data
, struct task_struct
*task
)
595 struct trace_array
*tr
= data
;
596 struct trace_pid_list
*pid_list
;
598 /* Nothing to do if we are already tracing */
599 if (!this_cpu_read(tr
->trace_buffer
.data
->ignore_pid
))
602 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
604 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
605 ignore_this_task(pid_list
, task
));
609 event_filter_pid_sched_wakeup_probe_post(void *data
, struct task_struct
*task
)
611 struct trace_array
*tr
= data
;
612 struct trace_pid_list
*pid_list
;
614 /* Nothing to do if we are not tracing */
615 if (this_cpu_read(tr
->trace_buffer
.data
->ignore_pid
))
618 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
620 /* Set tracing if current is enabled */
621 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
622 ignore_this_task(pid_list
, current
));
625 static void __ftrace_clear_event_pids(struct trace_array
*tr
)
627 struct trace_pid_list
*pid_list
;
628 struct trace_event_file
*file
;
631 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
632 lockdep_is_held(&event_mutex
));
636 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre
, tr
);
637 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post
, tr
);
639 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
, tr
);
640 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
, tr
);
642 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
, tr
);
643 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
, tr
);
645 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre
, tr
);
646 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post
, tr
);
648 list_for_each_entry(file
, &tr
->events
, list
) {
649 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
652 for_each_possible_cpu(cpu
)
653 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->ignore_pid
= false;
655 rcu_assign_pointer(tr
->filtered_pids
, NULL
);
657 /* Wait till all users are no longer using pid filtering */
660 vfree(pid_list
->pids
);
664 static void ftrace_clear_event_pids(struct trace_array
*tr
)
666 mutex_lock(&event_mutex
);
667 __ftrace_clear_event_pids(tr
);
668 mutex_unlock(&event_mutex
);
671 static void __put_system(struct event_subsystem
*system
)
673 struct event_filter
*filter
= system
->filter
;
675 WARN_ON_ONCE(system_refcount(system
) == 0);
676 if (system_refcount_dec(system
))
679 list_del(&system
->list
);
682 kfree(filter
->filter_string
);
685 kfree_const(system
->name
);
689 static void __get_system(struct event_subsystem
*system
)
691 WARN_ON_ONCE(system_refcount(system
) == 0);
692 system_refcount_inc(system
);
695 static void __get_system_dir(struct trace_subsystem_dir
*dir
)
697 WARN_ON_ONCE(dir
->ref_count
== 0);
699 __get_system(dir
->subsystem
);
702 static void __put_system_dir(struct trace_subsystem_dir
*dir
)
704 WARN_ON_ONCE(dir
->ref_count
== 0);
705 /* If the subsystem is about to be freed, the dir must be too */
706 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
708 __put_system(dir
->subsystem
);
709 if (!--dir
->ref_count
)
713 static void put_system(struct trace_subsystem_dir
*dir
)
715 mutex_lock(&event_mutex
);
716 __put_system_dir(dir
);
717 mutex_unlock(&event_mutex
);
720 static void remove_subsystem(struct trace_subsystem_dir
*dir
)
725 if (!--dir
->nr_events
) {
726 tracefs_remove_recursive(dir
->entry
);
727 list_del(&dir
->list
);
728 __put_system_dir(dir
);
732 static void remove_event_file_dir(struct trace_event_file
*file
)
734 struct dentry
*dir
= file
->dir
;
735 struct dentry
*child
;
738 spin_lock(&dir
->d_lock
); /* probably unneeded */
739 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
740 if (d_really_is_positive(child
)) /* probably unneeded */
741 d_inode(child
)->i_private
= NULL
;
743 spin_unlock(&dir
->d_lock
);
745 tracefs_remove_recursive(dir
);
748 list_del(&file
->list
);
749 remove_subsystem(file
->system
);
750 free_event_filter(file
->filter
);
751 kmem_cache_free(file_cachep
, file
);
755 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
758 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
759 const char *sub
, const char *event
, int set
)
761 struct trace_event_file
*file
;
762 struct trace_event_call
*call
;
766 list_for_each_entry(file
, &tr
->events
, list
) {
768 call
= file
->event_call
;
769 name
= trace_event_name(call
);
771 if (!name
|| !call
->class || !call
->class->reg
)
774 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
778 strcmp(match
, name
) != 0 &&
779 strcmp(match
, call
->class->system
) != 0)
782 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
785 if (event
&& strcmp(event
, name
) != 0)
788 ftrace_event_enable_disable(file
, set
);
796 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
797 const char *sub
, const char *event
, int set
)
801 mutex_lock(&event_mutex
);
802 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
803 mutex_unlock(&event_mutex
);
808 static int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
810 char *event
= NULL
, *sub
= NULL
, *match
;
814 * The buf format can be <subsystem>:<event-name>
815 * *:<event-name> means any event by that name.
816 * :<event-name> is the same.
818 * <subsystem>:* means all events in that subsystem
819 * <subsystem>: means the same.
821 * <name> (no ':') means all events in a subsystem with
822 * the name <name> or any event that matches <name>
825 match
= strsep(&buf
, ":");
831 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
833 if (!strlen(event
) || strcmp(event
, "*") == 0)
837 ret
= __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
839 /* Put back the colon to allow this to be called again */
847 * trace_set_clr_event - enable or disable an event
848 * @system: system name to match (NULL for any system)
849 * @event: event name to match (NULL for all events, within system)
850 * @set: 1 to enable, 0 to disable
852 * This is a way for other parts of the kernel to enable or disable
855 * Returns 0 on success, -EINVAL if the parameters do not match any
858 int trace_set_clr_event(const char *system
, const char *event
, int set
)
860 struct trace_array
*tr
= top_trace_array();
865 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
867 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
869 /* 128 should be much more than enough */
870 #define EVENT_BUF_SIZE 127
873 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
874 size_t cnt
, loff_t
*ppos
)
876 struct trace_parser parser
;
877 struct seq_file
*m
= file
->private_data
;
878 struct trace_array
*tr
= m
->private;
884 ret
= tracing_update_buffers();
888 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
891 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
893 if (read
>= 0 && trace_parser_loaded((&parser
))) {
896 if (*parser
.buffer
== '!')
899 parser
.buffer
[parser
.idx
] = 0;
901 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
909 trace_parser_put(&parser
);
915 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
917 struct trace_event_file
*file
= v
;
918 struct trace_event_call
*call
;
919 struct trace_array
*tr
= m
->private;
923 list_for_each_entry_continue(file
, &tr
->events
, list
) {
924 call
= file
->event_call
;
926 * The ftrace subsystem is for showing formats only.
927 * They can not be enabled or disabled via the event files.
929 if (call
->class && call
->class->reg
&&
930 !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
937 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
939 struct trace_event_file
*file
;
940 struct trace_array
*tr
= m
->private;
943 mutex_lock(&event_mutex
);
945 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
946 for (l
= 0; l
<= *pos
; ) {
947 file
= t_next(m
, file
, &l
);
955 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
957 struct trace_event_file
*file
= v
;
958 struct trace_array
*tr
= m
->private;
962 list_for_each_entry_continue(file
, &tr
->events
, list
) {
963 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
970 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
972 struct trace_event_file
*file
;
973 struct trace_array
*tr
= m
->private;
976 mutex_lock(&event_mutex
);
978 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
979 for (l
= 0; l
<= *pos
; ) {
980 file
= s_next(m
, file
, &l
);
987 static int t_show(struct seq_file
*m
, void *v
)
989 struct trace_event_file
*file
= v
;
990 struct trace_event_call
*call
= file
->event_call
;
992 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
993 seq_printf(m
, "%s:", call
->class->system
);
994 seq_printf(m
, "%s\n", trace_event_name(call
));
999 static void t_stop(struct seq_file
*m
, void *p
)
1001 mutex_unlock(&event_mutex
);
1005 p_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1007 struct trace_array
*tr
= m
->private;
1008 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1009 unsigned long pid
= (unsigned long)v
;
1013 /* pid already is +1 of the actual prevous bit */
1014 pid
= find_next_bit(pid_list
->pids
, pid_list
->pid_max
, pid
);
1016 /* Return pid + 1 to allow zero to be represented */
1017 if (pid
< pid_list
->pid_max
)
1018 return (void *)(pid
+ 1);
1023 static void *p_start(struct seq_file
*m
, loff_t
*pos
)
1026 struct trace_pid_list
*pid_list
;
1027 struct trace_array
*tr
= m
->private;
1032 * Grab the mutex, to keep calls to p_next() having the same
1033 * tr->filtered_pids as p_start() has.
1034 * If we just passed the tr->filtered_pids around, then RCU would
1035 * have been enough, but doing that makes things more complex.
1037 mutex_lock(&event_mutex
);
1038 rcu_read_lock_sched();
1040 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1045 pid
= find_first_bit(pid_list
->pids
, pid_list
->pid_max
);
1046 if (pid
>= pid_list
->pid_max
)
1049 /* Return pid + 1 so that zero can be the exit value */
1050 for (pid
++; pid
&& l
< *pos
;
1051 pid
= (unsigned long)p_next(m
, (void *)pid
, &l
))
1056 static void p_stop(struct seq_file
*m
, void *p
)
1059 rcu_read_unlock_sched();
1060 mutex_unlock(&event_mutex
);
1063 static int p_show(struct seq_file
*m
, void *v
)
1065 unsigned long pid
= (unsigned long)v
- 1;
1067 seq_printf(m
, "%lu\n", pid
);
1072 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1075 struct trace_event_file
*file
;
1076 unsigned long flags
;
1079 mutex_lock(&event_mutex
);
1080 file
= event_file_data(filp
);
1082 flags
= file
->flags
;
1083 mutex_unlock(&event_mutex
);
1088 if (flags
& EVENT_FILE_FL_ENABLED
&&
1089 !(flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1092 if (flags
& EVENT_FILE_FL_SOFT_DISABLED
||
1093 flags
& EVENT_FILE_FL_SOFT_MODE
)
1098 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
1102 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1105 struct trace_event_file
*file
;
1109 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1113 ret
= tracing_update_buffers();
1121 mutex_lock(&event_mutex
);
1122 file
= event_file_data(filp
);
1124 ret
= ftrace_event_enable_disable(file
, val
);
1125 mutex_unlock(&event_mutex
);
1134 return ret
? ret
: cnt
;
1138 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1141 const char set_to_char
[4] = { '?', '0', '1', 'X' };
1142 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1143 struct event_subsystem
*system
= dir
->subsystem
;
1144 struct trace_event_call
*call
;
1145 struct trace_event_file
*file
;
1146 struct trace_array
*tr
= dir
->tr
;
1151 mutex_lock(&event_mutex
);
1152 list_for_each_entry(file
, &tr
->events
, list
) {
1153 call
= file
->event_call
;
1154 if (!trace_event_name(call
) || !call
->class || !call
->class->reg
)
1157 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
1161 * We need to find out if all the events are set
1162 * or if all events or cleared, or if we have
1165 set
|= (1 << !!(file
->flags
& EVENT_FILE_FL_ENABLED
));
1168 * If we have a mixture, no need to look further.
1173 mutex_unlock(&event_mutex
);
1175 buf
[0] = set_to_char
[set
];
1178 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
1184 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1187 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1188 struct event_subsystem
*system
= dir
->subsystem
;
1189 const char *name
= NULL
;
1193 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1197 ret
= tracing_update_buffers();
1201 if (val
!= 0 && val
!= 1)
1205 * Opening of "enable" adds a ref count to system,
1206 * so the name is safe to use.
1209 name
= system
->name
;
1211 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
1225 FORMAT_FIELD_SEPERATOR
= 2,
1226 FORMAT_PRINTFMT
= 3,
1229 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1231 struct trace_event_call
*call
= event_file_data(m
->private);
1232 struct list_head
*common_head
= &ftrace_common_fields
;
1233 struct list_head
*head
= trace_get_fields(call
);
1234 struct list_head
*node
= v
;
1238 switch ((unsigned long)v
) {
1243 case FORMAT_FIELD_SEPERATOR
:
1247 case FORMAT_PRINTFMT
:
1253 if (node
== common_head
)
1254 return (void *)FORMAT_FIELD_SEPERATOR
;
1255 else if (node
== head
)
1256 return (void *)FORMAT_PRINTFMT
;
1261 static int f_show(struct seq_file
*m
, void *v
)
1263 struct trace_event_call
*call
= event_file_data(m
->private);
1264 struct ftrace_event_field
*field
;
1265 const char *array_descriptor
;
1267 switch ((unsigned long)v
) {
1269 seq_printf(m
, "name: %s\n", trace_event_name(call
));
1270 seq_printf(m
, "ID: %d\n", call
->event
.type
);
1271 seq_puts(m
, "format:\n");
1274 case FORMAT_FIELD_SEPERATOR
:
1278 case FORMAT_PRINTFMT
:
1279 seq_printf(m
, "\nprint fmt: %s\n",
1284 field
= list_entry(v
, struct ftrace_event_field
, link
);
1286 * Smartly shows the array type(except dynamic array).
1289 * If TYPE := TYPE[LEN], it is shown:
1290 * field:TYPE VAR[LEN]
1292 array_descriptor
= strchr(field
->type
, '[');
1294 if (!strncmp(field
->type
, "__data_loc", 10))
1295 array_descriptor
= NULL
;
1297 if (!array_descriptor
)
1298 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1299 field
->type
, field
->name
, field
->offset
,
1300 field
->size
, !!field
->is_signed
);
1302 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1303 (int)(array_descriptor
- field
->type
),
1304 field
->type
, field
->name
,
1305 array_descriptor
, field
->offset
,
1306 field
->size
, !!field
->is_signed
);
1311 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
1313 void *p
= (void *)FORMAT_HEADER
;
1316 /* ->stop() is called even if ->start() fails */
1317 mutex_lock(&event_mutex
);
1318 if (!event_file_data(m
->private))
1319 return ERR_PTR(-ENODEV
);
1321 while (l
< *pos
&& p
)
1322 p
= f_next(m
, p
, &l
);
1327 static void f_stop(struct seq_file
*m
, void *p
)
1329 mutex_unlock(&event_mutex
);
1332 static const struct seq_operations trace_format_seq_ops
= {
1339 static int trace_format_open(struct inode
*inode
, struct file
*file
)
1344 ret
= seq_open(file
, &trace_format_seq_ops
);
1348 m
= file
->private_data
;
1355 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1357 int id
= (long)event_file_data(filp
);
1367 len
= sprintf(buf
, "%d\n", id
);
1369 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1373 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1376 struct trace_event_file
*file
;
1377 struct trace_seq
*s
;
1383 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1390 mutex_lock(&event_mutex
);
1391 file
= event_file_data(filp
);
1393 print_event_filter(file
, s
);
1394 mutex_unlock(&event_mutex
);
1397 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1398 s
->buffer
, trace_seq_used(s
));
1406 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1409 struct trace_event_file
*file
;
1413 if (cnt
>= PAGE_SIZE
)
1416 buf
= memdup_user_nul(ubuf
, cnt
);
1418 return PTR_ERR(buf
);
1420 mutex_lock(&event_mutex
);
1421 file
= event_file_data(filp
);
1423 err
= apply_event_filter(file
, buf
);
1424 mutex_unlock(&event_mutex
);
1435 static LIST_HEAD(event_subsystems
);
1437 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1439 struct event_subsystem
*system
= NULL
;
1440 struct trace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1441 struct trace_array
*tr
;
1444 if (tracing_is_disabled())
1447 /* Make sure the system still exists */
1448 mutex_lock(&trace_types_lock
);
1449 mutex_lock(&event_mutex
);
1450 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1451 list_for_each_entry(dir
, &tr
->systems
, list
) {
1452 if (dir
== inode
->i_private
) {
1453 /* Don't open systems with no events */
1454 if (dir
->nr_events
) {
1455 __get_system_dir(dir
);
1456 system
= dir
->subsystem
;
1463 mutex_unlock(&event_mutex
);
1464 mutex_unlock(&trace_types_lock
);
1469 /* Some versions of gcc think dir can be uninitialized here */
1472 /* Still need to increment the ref count of the system */
1473 if (trace_array_get(tr
) < 0) {
1478 ret
= tracing_open_generic(inode
, filp
);
1480 trace_array_put(tr
);
1487 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1489 struct trace_subsystem_dir
*dir
;
1490 struct trace_array
*tr
= inode
->i_private
;
1493 if (tracing_is_disabled())
1496 if (trace_array_get(tr
) < 0)
1499 /* Make a temporary dir that has no system but points to tr */
1500 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1502 trace_array_put(tr
);
1508 ret
= tracing_open_generic(inode
, filp
);
1510 trace_array_put(tr
);
1515 filp
->private_data
= dir
;
1520 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1522 struct trace_subsystem_dir
*dir
= file
->private_data
;
1524 trace_array_put(dir
->tr
);
1527 * If dir->subsystem is NULL, then this is a temporary
1528 * descriptor that was made for a trace_array to enable
1540 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1543 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1544 struct event_subsystem
*system
= dir
->subsystem
;
1545 struct trace_seq
*s
;
1551 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1557 print_subsystem_event_filter(system
, s
);
1558 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1559 s
->buffer
, trace_seq_used(s
));
1567 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1570 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1574 if (cnt
>= PAGE_SIZE
)
1577 buf
= memdup_user_nul(ubuf
, cnt
);
1579 return PTR_ERR(buf
);
1581 err
= apply_subsystem_event_filter(dir
, buf
);
1592 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1594 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1595 struct trace_seq
*s
;
1601 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1608 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1609 s
->buffer
, trace_seq_used(s
));
1616 static void ignore_task_cpu(void *data
)
1618 struct trace_array
*tr
= data
;
1619 struct trace_pid_list
*pid_list
;
1622 * This function is called by on_each_cpu() while the
1623 * event_mutex is held.
1625 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
1626 mutex_is_locked(&event_mutex
));
1628 this_cpu_write(tr
->trace_buffer
.data
->ignore_pid
,
1629 ignore_this_task(pid_list
, current
));
1633 ftrace_event_pid_write(struct file
*filp
, const char __user
*ubuf
,
1634 size_t cnt
, loff_t
*ppos
)
1636 struct seq_file
*m
= filp
->private_data
;
1637 struct trace_array
*tr
= m
->private;
1638 struct trace_pid_list
*filtered_pids
= NULL
;
1639 struct trace_pid_list
*pid_list
;
1640 struct trace_event_file
*file
;
1641 struct trace_parser parser
;
1652 ret
= tracing_update_buffers();
1656 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
1659 mutex_lock(&event_mutex
);
1660 filtered_pids
= rcu_dereference_protected(tr
->filtered_pids
,
1661 lockdep_is_held(&event_mutex
));
1664 * Always recreate a new array. The write is an all or nothing
1665 * operation. Always create a new array when adding new pids by
1666 * the user. If the operation fails, then the current list is
1669 pid_list
= kmalloc(sizeof(*pid_list
), GFP_KERNEL
);
1674 pid_list
->pid_max
= READ_ONCE(pid_max
);
1675 /* Only truncating will shrink pid_max */
1676 if (filtered_pids
&& filtered_pids
->pid_max
> pid_list
->pid_max
)
1677 pid_list
->pid_max
= filtered_pids
->pid_max
;
1678 pid_list
->pids
= vzalloc((pid_list
->pid_max
+ 7) >> 3);
1679 if (!pid_list
->pids
) {
1684 if (filtered_pids
) {
1685 /* copy the current bits to the new max */
1686 pid
= find_first_bit(filtered_pids
->pids
,
1687 filtered_pids
->pid_max
);
1688 while (pid
< filtered_pids
->pid_max
) {
1689 set_bit(pid
, pid_list
->pids
);
1690 pid
= find_next_bit(filtered_pids
->pids
,
1691 filtered_pids
->pid_max
,
1701 ret
= trace_get_user(&parser
, ubuf
, cnt
, &this_pos
);
1702 if (ret
< 0 || !trace_parser_loaded(&parser
))
1709 parser
.buffer
[parser
.idx
] = 0;
1712 if (kstrtoul(parser
.buffer
, 0, &val
))
1714 if (val
>= pid_list
->pid_max
)
1719 set_bit(pid
, pid_list
->pids
);
1722 trace_parser_clear(&parser
);
1725 trace_parser_put(&parser
);
1728 vfree(pid_list
->pids
);
1735 /* Cleared the list of pids */
1736 vfree(pid_list
->pids
);
1743 rcu_assign_pointer(tr
->filtered_pids
, pid_list
);
1745 list_for_each_entry(file
, &tr
->events
, list
) {
1746 set_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
1749 if (filtered_pids
) {
1750 synchronize_sched();
1752 vfree(filtered_pids
->pids
);
1753 kfree(filtered_pids
);
1756 * Register a probe that is called before all other probes
1757 * to set ignore_pid if next or prev do not match.
1758 * Register a probe this is called after all other probes
1759 * to only keep ignore_pid set if next pid matches.
1761 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre
,
1763 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post
,
1766 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
,
1768 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
,
1771 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
,
1773 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
,
1776 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre
,
1778 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post
,
1783 * Ignoring of pids is done at task switch. But we have to
1784 * check for those tasks that are currently running.
1785 * Always do this in case a pid was appended or removed.
1787 on_each_cpu(ignore_task_cpu
, tr
, 1);
1790 mutex_unlock(&event_mutex
);
1799 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1800 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1801 static int ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
);
1802 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1804 static const struct seq_operations show_event_seq_ops
= {
1811 static const struct seq_operations show_set_event_seq_ops
= {
1818 static const struct seq_operations show_set_pid_seq_ops
= {
1825 static const struct file_operations ftrace_avail_fops
= {
1826 .open
= ftrace_event_avail_open
,
1828 .llseek
= seq_lseek
,
1829 .release
= seq_release
,
1832 static const struct file_operations ftrace_set_event_fops
= {
1833 .open
= ftrace_event_set_open
,
1835 .write
= ftrace_event_write
,
1836 .llseek
= seq_lseek
,
1837 .release
= ftrace_event_release
,
1840 static const struct file_operations ftrace_set_event_pid_fops
= {
1841 .open
= ftrace_event_set_pid_open
,
1843 .write
= ftrace_event_pid_write
,
1844 .llseek
= seq_lseek
,
1845 .release
= ftrace_event_release
,
1848 static const struct file_operations ftrace_enable_fops
= {
1849 .open
= tracing_open_generic
,
1850 .read
= event_enable_read
,
1851 .write
= event_enable_write
,
1852 .llseek
= default_llseek
,
1855 static const struct file_operations ftrace_event_format_fops
= {
1856 .open
= trace_format_open
,
1858 .llseek
= seq_lseek
,
1859 .release
= seq_release
,
1862 static const struct file_operations ftrace_event_id_fops
= {
1863 .read
= event_id_read
,
1864 .llseek
= default_llseek
,
1867 static const struct file_operations ftrace_event_filter_fops
= {
1868 .open
= tracing_open_generic
,
1869 .read
= event_filter_read
,
1870 .write
= event_filter_write
,
1871 .llseek
= default_llseek
,
1874 static const struct file_operations ftrace_subsystem_filter_fops
= {
1875 .open
= subsystem_open
,
1876 .read
= subsystem_filter_read
,
1877 .write
= subsystem_filter_write
,
1878 .llseek
= default_llseek
,
1879 .release
= subsystem_release
,
1882 static const struct file_operations ftrace_system_enable_fops
= {
1883 .open
= subsystem_open
,
1884 .read
= system_enable_read
,
1885 .write
= system_enable_write
,
1886 .llseek
= default_llseek
,
1887 .release
= subsystem_release
,
1890 static const struct file_operations ftrace_tr_enable_fops
= {
1891 .open
= system_tr_open
,
1892 .read
= system_enable_read
,
1893 .write
= system_enable_write
,
1894 .llseek
= default_llseek
,
1895 .release
= subsystem_release
,
1898 static const struct file_operations ftrace_show_header_fops
= {
1899 .open
= tracing_open_generic
,
1900 .read
= show_header
,
1901 .llseek
= default_llseek
,
1905 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1906 const struct seq_operations
*seq_ops
)
1911 ret
= seq_open(file
, seq_ops
);
1914 m
= file
->private_data
;
1915 /* copy tr over to seq ops */
1916 m
->private = inode
->i_private
;
1921 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1923 struct trace_array
*tr
= inode
->i_private
;
1925 trace_array_put(tr
);
1927 return seq_release(inode
, file
);
1931 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1933 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1935 return ftrace_event_open(inode
, file
, seq_ops
);
1939 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1941 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1942 struct trace_array
*tr
= inode
->i_private
;
1945 if (trace_array_get(tr
) < 0)
1948 if ((file
->f_mode
& FMODE_WRITE
) &&
1949 (file
->f_flags
& O_TRUNC
))
1950 ftrace_clear_events(tr
);
1952 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1954 trace_array_put(tr
);
1959 ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
)
1961 const struct seq_operations
*seq_ops
= &show_set_pid_seq_ops
;
1962 struct trace_array
*tr
= inode
->i_private
;
1965 if (trace_array_get(tr
) < 0)
1968 if ((file
->f_mode
& FMODE_WRITE
) &&
1969 (file
->f_flags
& O_TRUNC
))
1970 ftrace_clear_event_pids(tr
);
1972 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1974 trace_array_put(tr
);
1978 static struct event_subsystem
*
1979 create_new_subsystem(const char *name
)
1981 struct event_subsystem
*system
;
1983 /* need to create new entry */
1984 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1988 system
->ref_count
= 1;
1990 /* Only allocate if dynamic (kprobes and modules) */
1991 system
->name
= kstrdup_const(name
, GFP_KERNEL
);
1995 system
->filter
= NULL
;
1997 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1998 if (!system
->filter
)
2001 list_add(&system
->list
, &event_subsystems
);
2006 kfree_const(system
->name
);
2011 static struct dentry
*
2012 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
2013 struct trace_event_file
*file
, struct dentry
*parent
)
2015 struct trace_subsystem_dir
*dir
;
2016 struct event_subsystem
*system
;
2017 struct dentry
*entry
;
2019 /* First see if we did not already create this dir */
2020 list_for_each_entry(dir
, &tr
->systems
, list
) {
2021 system
= dir
->subsystem
;
2022 if (strcmp(system
->name
, name
) == 0) {
2029 /* Now see if the system itself exists. */
2030 list_for_each_entry(system
, &event_subsystems
, list
) {
2031 if (strcmp(system
->name
, name
) == 0)
2034 /* Reset system variable when not found */
2035 if (&system
->list
== &event_subsystems
)
2038 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
2043 system
= create_new_subsystem(name
);
2047 __get_system(system
);
2049 dir
->entry
= tracefs_create_dir(name
, parent
);
2051 pr_warn("Failed to create system directory %s\n", name
);
2052 __put_system(system
);
2059 dir
->subsystem
= system
;
2062 entry
= tracefs_create_file("filter", 0644, dir
->entry
, dir
,
2063 &ftrace_subsystem_filter_fops
);
2065 kfree(system
->filter
);
2066 system
->filter
= NULL
;
2067 pr_warn("Could not create tracefs '%s/filter' entry\n", name
);
2070 trace_create_file("enable", 0644, dir
->entry
, dir
,
2071 &ftrace_system_enable_fops
);
2073 list_add(&dir
->list
, &tr
->systems
);
2080 /* Only print this message if failed on memory allocation */
2081 if (!dir
|| !system
)
2082 pr_warn("No memory to create event subsystem %s\n", name
);
2087 event_create_dir(struct dentry
*parent
, struct trace_event_file
*file
)
2089 struct trace_event_call
*call
= file
->event_call
;
2090 struct trace_array
*tr
= file
->tr
;
2091 struct list_head
*head
;
2092 struct dentry
*d_events
;
2097 * If the trace point header did not define TRACE_SYSTEM
2098 * then the system would be called "TRACE_SYSTEM".
2100 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
2101 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
2107 name
= trace_event_name(call
);
2108 file
->dir
= tracefs_create_dir(name
, d_events
);
2110 pr_warn("Could not create tracefs '%s' directory\n", name
);
2114 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
2115 trace_create_file("enable", 0644, file
->dir
, file
,
2116 &ftrace_enable_fops
);
2118 #ifdef CONFIG_PERF_EVENTS
2119 if (call
->event
.type
&& call
->class->reg
)
2120 trace_create_file("id", 0444, file
->dir
,
2121 (void *)(long)call
->event
.type
,
2122 &ftrace_event_id_fops
);
2126 * Other events may have the same class. Only update
2127 * the fields if they are not already defined.
2129 head
= trace_get_fields(call
);
2130 if (list_empty(head
)) {
2131 ret
= call
->class->define_fields(call
);
2133 pr_warn("Could not initialize trace point events/%s\n",
2138 trace_create_file("filter", 0644, file
->dir
, file
,
2139 &ftrace_event_filter_fops
);
2141 trace_create_file("trigger", 0644, file
->dir
, file
,
2142 &event_trigger_fops
);
2144 trace_create_file("format", 0444, file
->dir
, call
,
2145 &ftrace_event_format_fops
);
2150 static void remove_event_from_tracers(struct trace_event_call
*call
)
2152 struct trace_event_file
*file
;
2153 struct trace_array
*tr
;
2155 do_for_each_event_file_safe(tr
, file
) {
2156 if (file
->event_call
!= call
)
2159 remove_event_file_dir(file
);
2161 * The do_for_each_event_file_safe() is
2162 * a double loop. After finding the call for this
2163 * trace_array, we use break to jump to the next
2167 } while_for_each_event_file();
2170 static void event_remove(struct trace_event_call
*call
)
2172 struct trace_array
*tr
;
2173 struct trace_event_file
*file
;
2175 do_for_each_event_file(tr
, file
) {
2176 if (file
->event_call
!= call
)
2178 ftrace_event_enable_disable(file
, 0);
2180 * The do_for_each_event_file() is
2181 * a double loop. After finding the call for this
2182 * trace_array, we use break to jump to the next
2186 } while_for_each_event_file();
2188 if (call
->event
.funcs
)
2189 __unregister_trace_event(&call
->event
);
2190 remove_event_from_tracers(call
);
2191 list_del(&call
->list
);
2194 static int event_init(struct trace_event_call
*call
)
2199 name
= trace_event_name(call
);
2203 if (call
->class->raw_init
) {
2204 ret
= call
->class->raw_init(call
);
2205 if (ret
< 0 && ret
!= -ENOSYS
)
2206 pr_warn("Could not initialize trace events/%s\n", name
);
2213 __register_event(struct trace_event_call
*call
, struct module
*mod
)
2217 ret
= event_init(call
);
2221 list_add(&call
->list
, &ftrace_events
);
2227 static char *enum_replace(char *ptr
, struct trace_enum_map
*map
, int len
)
2232 /* Find the length of the enum value as a string */
2233 elen
= snprintf(ptr
, 0, "%ld", map
->enum_value
);
2234 /* Make sure there's enough room to replace the string with the value */
2238 snprintf(ptr
, elen
+ 1, "%ld", map
->enum_value
);
2240 /* Get the rest of the string of ptr */
2241 rlen
= strlen(ptr
+ len
);
2242 memmove(ptr
+ elen
, ptr
+ len
, rlen
);
2243 /* Make sure we end the new string */
2244 ptr
[elen
+ rlen
] = 0;
2249 static void update_event_printk(struct trace_event_call
*call
,
2250 struct trace_enum_map
*map
)
2254 int len
= strlen(map
->enum_string
);
2256 for (ptr
= call
->print_fmt
; *ptr
; ptr
++) {
2270 if (isdigit(*ptr
)) {
2274 /* Check for alpha chars like ULL */
2275 } while (isalnum(*ptr
));
2279 * A number must have some kind of delimiter after
2280 * it, and we can ignore that too.
2284 if (isalpha(*ptr
) || *ptr
== '_') {
2285 if (strncmp(map
->enum_string
, ptr
, len
) == 0 &&
2286 !isalnum(ptr
[len
]) && ptr
[len
] != '_') {
2287 ptr
= enum_replace(ptr
, map
, len
);
2288 /* Hmm, enum string smaller than value */
2289 if (WARN_ON_ONCE(!ptr
))
2292 * No need to decrement here, as enum_replace()
2293 * returns the pointer to the character passed
2294 * the enum, and two enums can not be placed
2295 * back to back without something in between.
2296 * We can skip that something in between.
2303 } while (isalnum(*ptr
) || *ptr
== '_');
2307 * If what comes after this variable is a '.' or
2308 * '->' then we can continue to ignore that string.
2310 if (*ptr
== '.' || (ptr
[0] == '-' && ptr
[1] == '>')) {
2311 ptr
+= *ptr
== '.' ? 1 : 2;
2317 * Once again, we can skip the delimiter that came
2325 void trace_event_enum_update(struct trace_enum_map
**map
, int len
)
2327 struct trace_event_call
*call
, *p
;
2328 const char *last_system
= NULL
;
2332 down_write(&trace_event_sem
);
2333 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2334 /* events are usually grouped together with systems */
2335 if (!last_system
|| call
->class->system
!= last_system
) {
2337 last_system
= call
->class->system
;
2340 for (i
= last_i
; i
< len
; i
++) {
2341 if (call
->class->system
== map
[i
]->system
) {
2342 /* Save the first system if need be */
2345 update_event_printk(call
, map
[i
]);
2349 up_write(&trace_event_sem
);
2352 static struct trace_event_file
*
2353 trace_create_new_event(struct trace_event_call
*call
,
2354 struct trace_array
*tr
)
2356 struct trace_event_file
*file
;
2358 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
2362 file
->event_call
= call
;
2364 atomic_set(&file
->sm_ref
, 0);
2365 atomic_set(&file
->tm_ref
, 0);
2366 INIT_LIST_HEAD(&file
->triggers
);
2367 list_add(&file
->list
, &tr
->events
);
2372 /* Add an event to a trace directory */
2374 __trace_add_new_event(struct trace_event_call
*call
, struct trace_array
*tr
)
2376 struct trace_event_file
*file
;
2378 file
= trace_create_new_event(call
, tr
);
2382 return event_create_dir(tr
->event_dir
, file
);
2386 * Just create a decriptor for early init. A descriptor is required
2387 * for enabling events at boot. We want to enable events before
2388 * the filesystem is initialized.
2391 __trace_early_add_new_event(struct trace_event_call
*call
,
2392 struct trace_array
*tr
)
2394 struct trace_event_file
*file
;
2396 file
= trace_create_new_event(call
, tr
);
2403 struct ftrace_module_file_ops
;
2404 static void __add_event_to_tracers(struct trace_event_call
*call
);
2406 /* Add an additional event_call dynamically */
2407 int trace_add_event_call(struct trace_event_call
*call
)
2410 mutex_lock(&trace_types_lock
);
2411 mutex_lock(&event_mutex
);
2413 ret
= __register_event(call
, NULL
);
2415 __add_event_to_tracers(call
);
2417 mutex_unlock(&event_mutex
);
2418 mutex_unlock(&trace_types_lock
);
2423 * Must be called under locking of trace_types_lock, event_mutex and
2426 static void __trace_remove_event_call(struct trace_event_call
*call
)
2429 trace_destroy_fields(call
);
2430 free_event_filter(call
->filter
);
2431 call
->filter
= NULL
;
2434 static int probe_remove_event_call(struct trace_event_call
*call
)
2436 struct trace_array
*tr
;
2437 struct trace_event_file
*file
;
2439 #ifdef CONFIG_PERF_EVENTS
2440 if (call
->perf_refcount
)
2443 do_for_each_event_file(tr
, file
) {
2444 if (file
->event_call
!= call
)
2447 * We can't rely on ftrace_event_enable_disable(enable => 0)
2448 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2449 * TRACE_REG_UNREGISTER.
2451 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
2454 * The do_for_each_event_file_safe() is
2455 * a double loop. After finding the call for this
2456 * trace_array, we use break to jump to the next
2460 } while_for_each_event_file();
2462 __trace_remove_event_call(call
);
2467 /* Remove an event_call */
2468 int trace_remove_event_call(struct trace_event_call
*call
)
2472 mutex_lock(&trace_types_lock
);
2473 mutex_lock(&event_mutex
);
2474 down_write(&trace_event_sem
);
2475 ret
= probe_remove_event_call(call
);
2476 up_write(&trace_event_sem
);
2477 mutex_unlock(&event_mutex
);
2478 mutex_unlock(&trace_types_lock
);
2483 #define for_each_event(event, start, end) \
2484 for (event = start; \
2485 (unsigned long)event < (unsigned long)end; \
2488 #ifdef CONFIG_MODULES
2490 static void trace_module_add_events(struct module
*mod
)
2492 struct trace_event_call
**call
, **start
, **end
;
2494 if (!mod
->num_trace_events
)
2497 /* Don't add infrastructure for mods without tracepoints */
2498 if (trace_module_has_bad_taint(mod
)) {
2499 pr_err("%s: module has bad taint, not creating trace events\n",
2504 start
= mod
->trace_events
;
2505 end
= mod
->trace_events
+ mod
->num_trace_events
;
2507 for_each_event(call
, start
, end
) {
2508 __register_event(*call
, mod
);
2509 __add_event_to_tracers(*call
);
2513 static void trace_module_remove_events(struct module
*mod
)
2515 struct trace_event_call
*call
, *p
;
2516 bool clear_trace
= false;
2518 down_write(&trace_event_sem
);
2519 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2520 if (call
->mod
== mod
) {
2521 if (call
->flags
& TRACE_EVENT_FL_WAS_ENABLED
)
2523 __trace_remove_event_call(call
);
2526 up_write(&trace_event_sem
);
2529 * It is safest to reset the ring buffer if the module being unloaded
2530 * registered any events that were used. The only worry is if
2531 * a new module gets loaded, and takes on the same id as the events
2532 * of this module. When printing out the buffer, traced events left
2533 * over from this module may be passed to the new module events and
2534 * unexpected results may occur.
2537 tracing_reset_all_online_cpus();
2540 static int trace_module_notify(struct notifier_block
*self
,
2541 unsigned long val
, void *data
)
2543 struct module
*mod
= data
;
2545 mutex_lock(&trace_types_lock
);
2546 mutex_lock(&event_mutex
);
2548 case MODULE_STATE_COMING
:
2549 trace_module_add_events(mod
);
2551 case MODULE_STATE_GOING
:
2552 trace_module_remove_events(mod
);
2555 mutex_unlock(&event_mutex
);
2556 mutex_unlock(&trace_types_lock
);
2561 static struct notifier_block trace_module_nb
= {
2562 .notifier_call
= trace_module_notify
,
2563 .priority
= 1, /* higher than trace.c module notify */
2565 #endif /* CONFIG_MODULES */
2567 /* Create a new event directory structure for a trace directory. */
2569 __trace_add_event_dirs(struct trace_array
*tr
)
2571 struct trace_event_call
*call
;
2574 list_for_each_entry(call
, &ftrace_events
, list
) {
2575 ret
= __trace_add_new_event(call
, tr
);
2577 pr_warn("Could not create directory for event %s\n",
2578 trace_event_name(call
));
2582 struct trace_event_file
*
2583 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
2585 struct trace_event_file
*file
;
2586 struct trace_event_call
*call
;
2589 list_for_each_entry(file
, &tr
->events
, list
) {
2591 call
= file
->event_call
;
2592 name
= trace_event_name(call
);
2594 if (!name
|| !call
->class || !call
->class->reg
)
2597 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
2600 if (strcmp(event
, name
) == 0 &&
2601 strcmp(system
, call
->class->system
) == 0)
2607 #ifdef CONFIG_DYNAMIC_FTRACE
2610 #define ENABLE_EVENT_STR "enable_event"
2611 #define DISABLE_EVENT_STR "disable_event"
2613 struct event_probe_data
{
2614 struct trace_event_file
*file
;
2615 unsigned long count
;
2621 event_enable_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2623 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2624 struct event_probe_data
*data
= *pdata
;
2630 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2632 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2636 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
, void **_data
)
2638 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2639 struct event_probe_data
*data
= *pdata
;
2647 /* Skip if the event is in a state we want to switch to */
2648 if (data
->enable
== !(data
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
2651 if (data
->count
!= -1)
2654 event_enable_probe(ip
, parent_ip
, _data
);
2658 event_enable_print(struct seq_file
*m
, unsigned long ip
,
2659 struct ftrace_probe_ops
*ops
, void *_data
)
2661 struct event_probe_data
*data
= _data
;
2663 seq_printf(m
, "%ps:", (void *)ip
);
2665 seq_printf(m
, "%s:%s:%s",
2666 data
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
2667 data
->file
->event_call
->class->system
,
2668 trace_event_name(data
->file
->event_call
));
2670 if (data
->count
== -1)
2671 seq_puts(m
, ":unlimited\n");
2673 seq_printf(m
, ":count=%ld\n", data
->count
);
2679 event_enable_init(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2682 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2683 struct event_probe_data
*data
= *pdata
;
2690 event_enable_free(struct ftrace_probe_ops
*ops
, unsigned long ip
,
2693 struct event_probe_data
**pdata
= (struct event_probe_data
**)_data
;
2694 struct event_probe_data
*data
= *pdata
;
2696 if (WARN_ON_ONCE(data
->ref
<= 0))
2701 /* Remove the SOFT_MODE flag */
2702 __ftrace_event_enable_disable(data
->file
, 0, 1);
2703 module_put(data
->file
->event_call
->mod
);
2709 static struct ftrace_probe_ops event_enable_probe_ops
= {
2710 .func
= event_enable_probe
,
2711 .print
= event_enable_print
,
2712 .init
= event_enable_init
,
2713 .free
= event_enable_free
,
2716 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2717 .func
= event_enable_count_probe
,
2718 .print
= event_enable_print
,
2719 .init
= event_enable_init
,
2720 .free
= event_enable_free
,
2723 static struct ftrace_probe_ops event_disable_probe_ops
= {
2724 .func
= event_enable_probe
,
2725 .print
= event_enable_print
,
2726 .init
= event_enable_init
,
2727 .free
= event_enable_free
,
2730 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2731 .func
= event_enable_count_probe
,
2732 .print
= event_enable_print
,
2733 .init
= event_enable_init
,
2734 .free
= event_enable_free
,
2738 event_enable_func(struct ftrace_hash
*hash
,
2739 char *glob
, char *cmd
, char *param
, int enabled
)
2741 struct trace_array
*tr
= top_trace_array();
2742 struct trace_event_file
*file
;
2743 struct ftrace_probe_ops
*ops
;
2744 struct event_probe_data
*data
;
2754 /* hash funcs only work with set_ftrace_filter */
2755 if (!enabled
|| !param
)
2758 system
= strsep(¶m
, ":");
2762 event
= strsep(¶m
, ":");
2764 mutex_lock(&event_mutex
);
2767 file
= find_event_file(tr
, system
, event
);
2771 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2774 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2776 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2778 if (glob
[0] == '!') {
2779 unregister_ftrace_function_probe_func(glob
+1, ops
);
2785 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2789 data
->enable
= enable
;
2796 number
= strsep(¶m
, ":");
2799 if (!strlen(number
))
2803 * We use the callback data field (which is a pointer)
2806 ret
= kstrtoul(number
, 0, &data
->count
);
2811 /* Don't let event modules unload while probe registered */
2812 ret
= try_module_get(file
->event_call
->mod
);
2818 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2821 ret
= register_ftrace_function_probe(glob
, ops
, data
);
2823 * The above returns on success the # of functions enabled,
2824 * but if it didn't find any functions it returns zero.
2825 * Consider no functions a failure too.
2832 /* Just return zero, not the number of enabled functions */
2835 mutex_unlock(&event_mutex
);
2839 __ftrace_event_enable_disable(file
, 0, 1);
2841 module_put(file
->event_call
->mod
);
2847 static struct ftrace_func_command event_enable_cmd
= {
2848 .name
= ENABLE_EVENT_STR
,
2849 .func
= event_enable_func
,
2852 static struct ftrace_func_command event_disable_cmd
= {
2853 .name
= DISABLE_EVENT_STR
,
2854 .func
= event_enable_func
,
2857 static __init
int register_event_cmds(void)
2861 ret
= register_ftrace_command(&event_enable_cmd
);
2862 if (WARN_ON(ret
< 0))
2864 ret
= register_ftrace_command(&event_disable_cmd
);
2865 if (WARN_ON(ret
< 0))
2866 unregister_ftrace_command(&event_enable_cmd
);
2870 static inline int register_event_cmds(void) { return 0; }
2871 #endif /* CONFIG_DYNAMIC_FTRACE */
2874 * The top level array has already had its trace_event_file
2875 * descriptors created in order to allow for early events to
2876 * be recorded. This function is called after the tracefs has been
2877 * initialized, and we now have to create the files associated
2881 __trace_early_add_event_dirs(struct trace_array
*tr
)
2883 struct trace_event_file
*file
;
2887 list_for_each_entry(file
, &tr
->events
, list
) {
2888 ret
= event_create_dir(tr
->event_dir
, file
);
2890 pr_warn("Could not create directory for event %s\n",
2891 trace_event_name(file
->event_call
));
2896 * For early boot up, the top trace array requires to have
2897 * a list of events that can be enabled. This must be done before
2898 * the filesystem is set up in order to allow events to be traced
2902 __trace_early_add_events(struct trace_array
*tr
)
2904 struct trace_event_call
*call
;
2907 list_for_each_entry(call
, &ftrace_events
, list
) {
2908 /* Early boot up should not have any modules loaded */
2909 if (WARN_ON_ONCE(call
->mod
))
2912 ret
= __trace_early_add_new_event(call
, tr
);
2914 pr_warn("Could not create early event %s\n",
2915 trace_event_name(call
));
2919 /* Remove the event directory structure for a trace directory. */
2921 __trace_remove_event_dirs(struct trace_array
*tr
)
2923 struct trace_event_file
*file
, *next
;
2925 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
2926 remove_event_file_dir(file
);
2929 static void __add_event_to_tracers(struct trace_event_call
*call
)
2931 struct trace_array
*tr
;
2933 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
2934 __trace_add_new_event(call
, tr
);
2937 extern struct trace_event_call
*__start_ftrace_events
[];
2938 extern struct trace_event_call
*__stop_ftrace_events
[];
2940 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
2942 static __init
int setup_trace_event(char *str
)
2944 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
2945 ring_buffer_expanded
= true;
2946 tracing_selftest_disabled
= true;
2950 __setup("trace_event=", setup_trace_event
);
2952 /* Expects to have event_mutex held when called */
2954 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
2956 struct dentry
*d_events
;
2957 struct dentry
*entry
;
2959 entry
= tracefs_create_file("set_event", 0644, parent
,
2960 tr
, &ftrace_set_event_fops
);
2962 pr_warn("Could not create tracefs 'set_event' entry\n");
2966 d_events
= tracefs_create_dir("events", parent
);
2968 pr_warn("Could not create tracefs 'events' directory\n");
2972 entry
= tracefs_create_file("set_event_pid", 0644, parent
,
2973 tr
, &ftrace_set_event_pid_fops
);
2975 /* ring buffer internal formats */
2976 trace_create_file("header_page", 0444, d_events
,
2977 ring_buffer_print_page_header
,
2978 &ftrace_show_header_fops
);
2980 trace_create_file("header_event", 0444, d_events
,
2981 ring_buffer_print_entry_header
,
2982 &ftrace_show_header_fops
);
2984 trace_create_file("enable", 0644, d_events
,
2985 tr
, &ftrace_tr_enable_fops
);
2987 tr
->event_dir
= d_events
;
2993 * event_trace_add_tracer - add a instance of a trace_array to events
2994 * @parent: The parent dentry to place the files/directories for events in
2995 * @tr: The trace array associated with these events
2997 * When a new instance is created, it needs to set up its events
2998 * directory, as well as other files associated with events. It also
2999 * creates the event hierachry in the @parent/events directory.
3001 * Returns 0 on success.
3003 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3007 mutex_lock(&event_mutex
);
3009 ret
= create_event_toplevel_files(parent
, tr
);
3013 down_write(&trace_event_sem
);
3014 __trace_add_event_dirs(tr
);
3015 up_write(&trace_event_sem
);
3018 mutex_unlock(&event_mutex
);
3024 * The top trace array already had its file descriptors created.
3025 * Now the files themselves need to be created.
3028 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3032 mutex_lock(&event_mutex
);
3034 ret
= create_event_toplevel_files(parent
, tr
);
3038 down_write(&trace_event_sem
);
3039 __trace_early_add_event_dirs(tr
);
3040 up_write(&trace_event_sem
);
3043 mutex_unlock(&event_mutex
);
3048 int event_trace_del_tracer(struct trace_array
*tr
)
3050 mutex_lock(&event_mutex
);
3052 /* Disable any event triggers and associated soft-disabled events */
3053 clear_event_triggers(tr
);
3055 /* Clear the pid list */
3056 __ftrace_clear_event_pids(tr
);
3058 /* Disable any running events */
3059 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
3061 /* Access to events are within rcu_read_lock_sched() */
3062 synchronize_sched();
3064 down_write(&trace_event_sem
);
3065 __trace_remove_event_dirs(tr
);
3066 tracefs_remove_recursive(tr
->event_dir
);
3067 up_write(&trace_event_sem
);
3069 tr
->event_dir
= NULL
;
3071 mutex_unlock(&event_mutex
);
3076 static __init
int event_trace_memsetup(void)
3078 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
3079 file_cachep
= KMEM_CACHE(trace_event_file
, SLAB_PANIC
);
3084 early_enable_events(struct trace_array
*tr
, bool disable_first
)
3086 char *buf
= bootup_event_buf
;
3091 token
= strsep(&buf
, ",");
3097 /* Restarting syscalls requires that we stop them first */
3099 ftrace_set_clr_event(tr
, token
, 0);
3101 ret
= ftrace_set_clr_event(tr
, token
, 1);
3103 pr_warn("Failed to enable trace event: %s\n", token
);
3106 /* Put back the comma to allow this to be called again */
3112 static __init
int event_trace_enable(void)
3114 struct trace_array
*tr
= top_trace_array();
3115 struct trace_event_call
**iter
, *call
;
3121 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
3124 ret
= event_init(call
);
3126 list_add(&call
->list
, &ftrace_events
);
3130 * We need the top trace array to have a working set of trace
3131 * points at early init, before the debug files and directories
3132 * are created. Create the file entries now, and attach them
3133 * to the actual file dentries later.
3135 __trace_early_add_events(tr
);
3137 early_enable_events(tr
, false);
3139 trace_printk_start_comm();
3141 register_event_cmds();
3143 register_trigger_cmds();
3149 * event_trace_enable() is called from trace_event_init() first to
3150 * initialize events and perhaps start any events that are on the
3151 * command line. Unfortunately, there are some events that will not
3152 * start this early, like the system call tracepoints that need
3153 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3154 * is called before pid 1 starts, and this flag is never set, making
3155 * the syscall tracepoint never get reached, but the event is enabled
3156 * regardless (and not doing anything).
3158 static __init
int event_trace_enable_again(void)
3160 struct trace_array
*tr
;
3162 tr
= top_trace_array();
3166 early_enable_events(tr
, true);
3171 early_initcall(event_trace_enable_again
);
3173 static __init
int event_trace_init(void)
3175 struct trace_array
*tr
;
3176 struct dentry
*d_tracer
;
3177 struct dentry
*entry
;
3180 tr
= top_trace_array();
3184 d_tracer
= tracing_init_dentry();
3185 if (IS_ERR(d_tracer
))
3188 entry
= tracefs_create_file("available_events", 0444, d_tracer
,
3189 tr
, &ftrace_avail_fops
);
3191 pr_warn("Could not create tracefs 'available_events' entry\n");
3193 if (trace_define_generic_fields())
3194 pr_warn("tracing: Failed to allocated generic fields");
3196 if (trace_define_common_fields())
3197 pr_warn("tracing: Failed to allocate common fields");
3199 ret
= early_event_add_tracer(d_tracer
, tr
);
3203 #ifdef CONFIG_MODULES
3204 ret
= register_module_notifier(&trace_module_nb
);
3206 pr_warn("Failed to register trace events module notifier\n");
3211 void __init
trace_event_init(void)
3213 event_trace_memsetup();
3214 init_ftrace_syscalls();
3215 event_trace_enable();
3218 fs_initcall(event_trace_init
);
3220 #ifdef CONFIG_FTRACE_STARTUP_TEST
3222 static DEFINE_SPINLOCK(test_spinlock
);
3223 static DEFINE_SPINLOCK(test_spinlock_irq
);
3224 static DEFINE_MUTEX(test_mutex
);
3226 static __init
void test_work(struct work_struct
*dummy
)
3228 spin_lock(&test_spinlock
);
3229 spin_lock_irq(&test_spinlock_irq
);
3231 spin_unlock_irq(&test_spinlock_irq
);
3232 spin_unlock(&test_spinlock
);
3234 mutex_lock(&test_mutex
);
3236 mutex_unlock(&test_mutex
);
3239 static __init
int event_test_thread(void *unused
)
3243 test_malloc
= kmalloc(1234, GFP_KERNEL
);
3245 pr_info("failed to kmalloc\n");
3247 schedule_on_each_cpu(test_work
);
3251 set_current_state(TASK_INTERRUPTIBLE
);
3252 while (!kthread_should_stop()) {
3254 set_current_state(TASK_INTERRUPTIBLE
);
3256 __set_current_state(TASK_RUNNING
);
3262 * Do various things that may trigger events.
3264 static __init
void event_test_stuff(void)
3266 struct task_struct
*test_thread
;
3268 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
3270 kthread_stop(test_thread
);
3274 * For every trace event defined, we will test each trace point separately,
3275 * and then by groups, and finally all trace points.
3277 static __init
void event_trace_self_tests(void)
3279 struct trace_subsystem_dir
*dir
;
3280 struct trace_event_file
*file
;
3281 struct trace_event_call
*call
;
3282 struct event_subsystem
*system
;
3283 struct trace_array
*tr
;
3286 tr
= top_trace_array();
3290 pr_info("Running tests on trace events:\n");
3292 list_for_each_entry(file
, &tr
->events
, list
) {
3294 call
= file
->event_call
;
3296 /* Only test those that have a probe */
3297 if (!call
->class || !call
->class->probe
)
3301 * Testing syscall events here is pretty useless, but
3302 * we still do it if configured. But this is time consuming.
3303 * What we really need is a user thread to perform the
3304 * syscalls as we test.
3306 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3307 if (call
->class->system
&&
3308 strcmp(call
->class->system
, "syscalls") == 0)
3312 pr_info("Testing event %s: ", trace_event_name(call
));
3315 * If an event is already enabled, someone is using
3316 * it and the self test should not be on.
3318 if (file
->flags
& EVENT_FILE_FL_ENABLED
) {
3319 pr_warn("Enabled event during self test!\n");
3324 ftrace_event_enable_disable(file
, 1);
3326 ftrace_event_enable_disable(file
, 0);
3331 /* Now test at the sub system level */
3333 pr_info("Running tests on trace event systems:\n");
3335 list_for_each_entry(dir
, &tr
->systems
, list
) {
3337 system
= dir
->subsystem
;
3339 /* the ftrace system is special, skip it */
3340 if (strcmp(system
->name
, "ftrace") == 0)
3343 pr_info("Testing event system %s: ", system
->name
);
3345 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
3346 if (WARN_ON_ONCE(ret
)) {
3347 pr_warn("error enabling system %s\n",
3354 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
3355 if (WARN_ON_ONCE(ret
)) {
3356 pr_warn("error disabling system %s\n",
3364 /* Test with all events enabled */
3366 pr_info("Running tests on all trace events:\n");
3367 pr_info("Testing all events: ");
3369 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
3370 if (WARN_ON_ONCE(ret
)) {
3371 pr_warn("error enabling all events\n");
3378 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
3379 if (WARN_ON_ONCE(ret
)) {
3380 pr_warn("error disabling all events\n");
3387 #ifdef CONFIG_FUNCTION_TRACER
3389 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
3391 static struct trace_array
*event_tr
;
3394 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
3395 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
3397 struct ring_buffer_event
*event
;
3398 struct ring_buffer
*buffer
;
3399 struct ftrace_entry
*entry
;
3400 unsigned long flags
;
3405 pc
= preempt_count();
3406 preempt_disable_notrace();
3407 cpu
= raw_smp_processor_id();
3408 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
3413 local_save_flags(flags
);
3415 event
= trace_current_buffer_lock_reserve(&buffer
,
3416 TRACE_FN
, sizeof(*entry
),
3420 entry
= ring_buffer_event_data(event
);
3422 entry
->parent_ip
= parent_ip
;
3424 trace_buffer_unlock_commit(event_tr
, buffer
, event
, flags
, pc
);
3427 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
3428 preempt_enable_notrace();
3431 static struct ftrace_ops trace_ops __initdata
=
3433 .func
= function_test_events_call
,
3434 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
3437 static __init
void event_trace_self_test_with_function(void)
3440 event_tr
= top_trace_array();
3441 if (WARN_ON(!event_tr
))
3443 ret
= register_ftrace_function(&trace_ops
);
3444 if (WARN_ON(ret
< 0)) {
3445 pr_info("Failed to enable function tracer for event tests\n");
3448 pr_info("Running tests again, along with the function tracer\n");
3449 event_trace_self_tests();
3450 unregister_ftrace_function(&trace_ops
);
3453 static __init
void event_trace_self_test_with_function(void)
3458 static __init
int event_trace_self_tests_init(void)
3460 if (!tracing_selftest_disabled
) {
3461 event_trace_self_tests();
3462 event_trace_self_test_with_function();
3468 late_initcall(event_trace_self_tests_init
);