1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
7 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
12 #define pr_fmt(fmt) fmt
14 #include <linux/workqueue.h>
15 #include <linux/security.h>
16 #include <linux/spinlock.h>
17 #include <linux/kthread.h>
18 #include <linux/tracefs.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/ctype.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
29 #include <asm/setup.h>
31 #include "trace_output.h"
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
36 DEFINE_MUTEX(event_mutex
);
38 LIST_HEAD(ftrace_events
);
39 static LIST_HEAD(ftrace_generic_fields
);
40 static LIST_HEAD(ftrace_common_fields
);
42 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
44 static struct kmem_cache
*field_cachep
;
45 static struct kmem_cache
*file_cachep
;
47 static inline int system_refcount(struct event_subsystem
*system
)
49 return system
->ref_count
;
52 static int system_refcount_inc(struct event_subsystem
*system
)
54 return system
->ref_count
++;
57 static int system_refcount_dec(struct event_subsystem
*system
)
59 return --system
->ref_count
;
62 /* Double loops, do not use break, only goto's work */
63 #define do_for_each_event_file(tr, file) \
64 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
65 list_for_each_entry(file, &tr->events, list)
67 #define do_for_each_event_file_safe(tr, file) \
68 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
69 struct trace_event_file *___n; \
70 list_for_each_entry_safe(file, ___n, &tr->events, list)
72 #define while_for_each_event_file() \
75 static struct ftrace_event_field
*
76 __find_event_field(struct list_head
*head
, char *name
)
78 struct ftrace_event_field
*field
;
80 list_for_each_entry(field
, head
, link
) {
81 if (!strcmp(field
->name
, name
))
88 struct ftrace_event_field
*
89 trace_find_event_field(struct trace_event_call
*call
, char *name
)
91 struct ftrace_event_field
*field
;
92 struct list_head
*head
;
94 head
= trace_get_fields(call
);
95 field
= __find_event_field(head
, name
);
99 field
= __find_event_field(&ftrace_generic_fields
, name
);
103 return __find_event_field(&ftrace_common_fields
, name
);
106 static int __trace_define_field(struct list_head
*head
, const char *type
,
107 const char *name
, int offset
, int size
,
108 int is_signed
, int filter_type
)
110 struct ftrace_event_field
*field
;
112 field
= kmem_cache_alloc(field_cachep
, GFP_TRACE
);
119 if (filter_type
== FILTER_OTHER
)
120 field
->filter_type
= filter_assign_type(type
);
122 field
->filter_type
= filter_type
;
124 field
->offset
= offset
;
126 field
->is_signed
= is_signed
;
128 list_add(&field
->link
, head
);
133 int trace_define_field(struct trace_event_call
*call
, const char *type
,
134 const char *name
, int offset
, int size
, int is_signed
,
137 struct list_head
*head
;
139 if (WARN_ON(!call
->class))
142 head
= trace_get_fields(call
);
143 return __trace_define_field(head
, type
, name
, offset
, size
,
144 is_signed
, filter_type
);
146 EXPORT_SYMBOL_GPL(trace_define_field
);
148 #define __generic_field(type, item, filter_type) \
149 ret = __trace_define_field(&ftrace_generic_fields, #type, \
150 #item, 0, 0, is_signed_type(type), \
155 #define __common_field(type, item) \
156 ret = __trace_define_field(&ftrace_common_fields, #type, \
158 offsetof(typeof(ent), item), \
160 is_signed_type(type), FILTER_OTHER); \
164 static int trace_define_generic_fields(void)
168 __generic_field(int, CPU
, FILTER_CPU
);
169 __generic_field(int, cpu
, FILTER_CPU
);
170 __generic_field(char *, COMM
, FILTER_COMM
);
171 __generic_field(char *, comm
, FILTER_COMM
);
176 static int trace_define_common_fields(void)
179 struct trace_entry ent
;
181 __common_field(unsigned short, type
);
182 __common_field(unsigned char, flags
);
183 __common_field(unsigned char, preempt_count
);
184 __common_field(int, pid
);
189 static void trace_destroy_fields(struct trace_event_call
*call
)
191 struct ftrace_event_field
*field
, *next
;
192 struct list_head
*head
;
194 head
= trace_get_fields(call
);
195 list_for_each_entry_safe(field
, next
, head
, link
) {
196 list_del(&field
->link
);
197 kmem_cache_free(field_cachep
, field
);
202 * run-time version of trace_event_get_offsets_<call>() that returns the last
203 * accessible offset of trace fields excluding __dynamic_array bytes
205 int trace_event_get_offsets(struct trace_event_call
*call
)
207 struct ftrace_event_field
*tail
;
208 struct list_head
*head
;
210 head
= trace_get_fields(call
);
212 * head->next points to the last field with the largest offset,
213 * since it was added last by trace_define_field()
215 tail
= list_first_entry(head
, struct ftrace_event_field
, link
);
216 return tail
->offset
+ tail
->size
;
219 int trace_event_raw_init(struct trace_event_call
*call
)
223 id
= register_trace_event(&call
->event
);
229 EXPORT_SYMBOL_GPL(trace_event_raw_init
);
231 bool trace_event_ignore_this_pid(struct trace_event_file
*trace_file
)
233 struct trace_array
*tr
= trace_file
->tr
;
234 struct trace_array_cpu
*data
;
235 struct trace_pid_list
*pid_list
;
237 pid_list
= rcu_dereference_raw(tr
->filtered_pids
);
241 data
= this_cpu_ptr(tr
->array_buffer
.data
);
243 return data
->ignore_pid
;
245 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid
);
247 void *trace_event_buffer_reserve(struct trace_event_buffer
*fbuffer
,
248 struct trace_event_file
*trace_file
,
251 struct trace_event_call
*event_call
= trace_file
->event_call
;
253 if ((trace_file
->flags
& EVENT_FILE_FL_PID_FILTER
) &&
254 trace_event_ignore_this_pid(trace_file
))
257 local_save_flags(fbuffer
->flags
);
258 fbuffer
->pc
= preempt_count();
260 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
261 * preemption (adding one to the preempt_count). Since we are
262 * interested in the preempt_count at the time the tracepoint was
263 * hit, we need to subtract one to offset the increment.
265 if (IS_ENABLED(CONFIG_PREEMPTION
))
267 fbuffer
->trace_file
= trace_file
;
270 trace_event_buffer_lock_reserve(&fbuffer
->buffer
, trace_file
,
271 event_call
->event
.type
, len
,
272 fbuffer
->flags
, fbuffer
->pc
);
276 fbuffer
->regs
= NULL
;
277 fbuffer
->entry
= ring_buffer_event_data(fbuffer
->event
);
278 return fbuffer
->entry
;
280 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve
);
282 int trace_event_reg(struct trace_event_call
*call
,
283 enum trace_reg type
, void *data
)
285 struct trace_event_file
*file
= data
;
287 WARN_ON(!(call
->flags
& TRACE_EVENT_FL_TRACEPOINT
));
289 case TRACE_REG_REGISTER
:
290 return tracepoint_probe_register(call
->tp
,
293 case TRACE_REG_UNREGISTER
:
294 tracepoint_probe_unregister(call
->tp
,
299 #ifdef CONFIG_PERF_EVENTS
300 case TRACE_REG_PERF_REGISTER
:
301 return tracepoint_probe_register(call
->tp
,
302 call
->class->perf_probe
,
304 case TRACE_REG_PERF_UNREGISTER
:
305 tracepoint_probe_unregister(call
->tp
,
306 call
->class->perf_probe
,
309 case TRACE_REG_PERF_OPEN
:
310 case TRACE_REG_PERF_CLOSE
:
311 case TRACE_REG_PERF_ADD
:
312 case TRACE_REG_PERF_DEL
:
318 EXPORT_SYMBOL_GPL(trace_event_reg
);
320 void trace_event_enable_cmd_record(bool enable
)
322 struct trace_event_file
*file
;
323 struct trace_array
*tr
;
325 lockdep_assert_held(&event_mutex
);
327 do_for_each_event_file(tr
, file
) {
329 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
))
333 tracing_start_cmdline_record();
334 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
336 tracing_stop_cmdline_record();
337 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
339 } while_for_each_event_file();
342 void trace_event_enable_tgid_record(bool enable
)
344 struct trace_event_file
*file
;
345 struct trace_array
*tr
;
347 lockdep_assert_held(&event_mutex
);
349 do_for_each_event_file(tr
, file
) {
350 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
))
354 tracing_start_tgid_record();
355 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT
, &file
->flags
);
357 tracing_stop_tgid_record();
358 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT
,
361 } while_for_each_event_file();
364 static int __ftrace_event_enable_disable(struct trace_event_file
*file
,
365 int enable
, int soft_disable
)
367 struct trace_event_call
*call
= file
->event_call
;
368 struct trace_array
*tr
= file
->tr
;
369 unsigned long file_flags
= file
->flags
;
376 * When soft_disable is set and enable is cleared, the sm_ref
377 * reference counter is decremented. If it reaches 0, we want
378 * to clear the SOFT_DISABLED flag but leave the event in the
379 * state that it was. That is, if the event was enabled and
380 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
381 * is set we do not want the event to be enabled before we
384 * When soft_disable is not set but the SOFT_MODE flag is,
385 * we do nothing. Do not disable the tracepoint, otherwise
386 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
389 if (atomic_dec_return(&file
->sm_ref
) > 0)
391 disable
= file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
;
392 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
394 disable
= !(file
->flags
& EVENT_FILE_FL_SOFT_MODE
);
396 if (disable
&& (file
->flags
& EVENT_FILE_FL_ENABLED
)) {
397 clear_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
398 if (file
->flags
& EVENT_FILE_FL_RECORDED_CMD
) {
399 tracing_stop_cmdline_record();
400 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
403 if (file
->flags
& EVENT_FILE_FL_RECORDED_TGID
) {
404 tracing_stop_tgid_record();
405 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT
, &file
->flags
);
408 call
->class->reg(call
, TRACE_REG_UNREGISTER
, file
);
410 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
411 if (file
->flags
& EVENT_FILE_FL_SOFT_MODE
)
412 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
414 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
418 * When soft_disable is set and enable is set, we want to
419 * register the tracepoint for the event, but leave the event
420 * as is. That means, if the event was already enabled, we do
421 * nothing (but set SOFT_MODE). If the event is disabled, we
422 * set SOFT_DISABLED before enabling the event tracepoint, so
423 * it still seems to be disabled.
426 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
428 if (atomic_inc_return(&file
->sm_ref
) > 1)
430 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT
, &file
->flags
);
433 if (!(file
->flags
& EVENT_FILE_FL_ENABLED
)) {
434 bool cmd
= false, tgid
= false;
436 /* Keep the event disabled, when going to SOFT_MODE. */
438 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &file
->flags
);
440 if (tr
->trace_flags
& TRACE_ITER_RECORD_CMD
) {
442 tracing_start_cmdline_record();
443 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT
, &file
->flags
);
446 if (tr
->trace_flags
& TRACE_ITER_RECORD_TGID
) {
448 tracing_start_tgid_record();
449 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT
, &file
->flags
);
452 ret
= call
->class->reg(call
, TRACE_REG_REGISTER
, file
);
455 tracing_stop_cmdline_record();
457 tracing_stop_tgid_record();
458 pr_info("event trace: Could not enable event "
459 "%s\n", trace_event_name(call
));
462 set_bit(EVENT_FILE_FL_ENABLED_BIT
, &file
->flags
);
464 /* WAS_ENABLED gets set but never cleared. */
465 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT
, &file
->flags
);
470 /* Enable or disable use of trace_buffered_event */
471 if ((file_flags
& EVENT_FILE_FL_SOFT_DISABLED
) !=
472 (file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
)) {
473 if (file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
)
474 trace_buffered_event_enable();
476 trace_buffered_event_disable();
482 int trace_event_enable_disable(struct trace_event_file
*file
,
483 int enable
, int soft_disable
)
485 return __ftrace_event_enable_disable(file
, enable
, soft_disable
);
488 static int ftrace_event_enable_disable(struct trace_event_file
*file
,
491 return __ftrace_event_enable_disable(file
, enable
, 0);
494 static void ftrace_clear_events(struct trace_array
*tr
)
496 struct trace_event_file
*file
;
498 mutex_lock(&event_mutex
);
499 list_for_each_entry(file
, &tr
->events
, list
) {
500 ftrace_event_enable_disable(file
, 0);
502 mutex_unlock(&event_mutex
);
506 event_filter_pid_sched_process_exit(void *data
, struct task_struct
*task
)
508 struct trace_pid_list
*pid_list
;
509 struct trace_array
*tr
= data
;
511 pid_list
= rcu_dereference_raw(tr
->filtered_pids
);
512 trace_filter_add_remove_task(pid_list
, NULL
, task
);
516 event_filter_pid_sched_process_fork(void *data
,
517 struct task_struct
*self
,
518 struct task_struct
*task
)
520 struct trace_pid_list
*pid_list
;
521 struct trace_array
*tr
= data
;
523 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
524 trace_filter_add_remove_task(pid_list
, self
, task
);
527 void trace_event_follow_fork(struct trace_array
*tr
, bool enable
)
530 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork
,
532 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit
,
535 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork
,
537 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit
,
543 event_filter_pid_sched_switch_probe_pre(void *data
, bool preempt
,
544 struct task_struct
*prev
, struct task_struct
*next
)
546 struct trace_array
*tr
= data
;
547 struct trace_pid_list
*pid_list
;
549 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
551 this_cpu_write(tr
->array_buffer
.data
->ignore_pid
,
552 trace_ignore_this_task(pid_list
, prev
) &&
553 trace_ignore_this_task(pid_list
, next
));
557 event_filter_pid_sched_switch_probe_post(void *data
, bool preempt
,
558 struct task_struct
*prev
, struct task_struct
*next
)
560 struct trace_array
*tr
= data
;
561 struct trace_pid_list
*pid_list
;
563 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
565 this_cpu_write(tr
->array_buffer
.data
->ignore_pid
,
566 trace_ignore_this_task(pid_list
, next
));
570 event_filter_pid_sched_wakeup_probe_pre(void *data
, struct task_struct
*task
)
572 struct trace_array
*tr
= data
;
573 struct trace_pid_list
*pid_list
;
575 /* Nothing to do if we are already tracing */
576 if (!this_cpu_read(tr
->array_buffer
.data
->ignore_pid
))
579 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
581 this_cpu_write(tr
->array_buffer
.data
->ignore_pid
,
582 trace_ignore_this_task(pid_list
, task
));
586 event_filter_pid_sched_wakeup_probe_post(void *data
, struct task_struct
*task
)
588 struct trace_array
*tr
= data
;
589 struct trace_pid_list
*pid_list
;
591 /* Nothing to do if we are not tracing */
592 if (this_cpu_read(tr
->array_buffer
.data
->ignore_pid
))
595 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
597 /* Set tracing if current is enabled */
598 this_cpu_write(tr
->array_buffer
.data
->ignore_pid
,
599 trace_ignore_this_task(pid_list
, current
));
602 static void __ftrace_clear_event_pids(struct trace_array
*tr
)
604 struct trace_pid_list
*pid_list
;
605 struct trace_event_file
*file
;
608 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
609 lockdep_is_held(&event_mutex
));
613 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre
, tr
);
614 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post
, tr
);
616 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
, tr
);
617 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
, tr
);
619 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
, tr
);
620 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
, tr
);
622 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre
, tr
);
623 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post
, tr
);
625 list_for_each_entry(file
, &tr
->events
, list
) {
626 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
629 for_each_possible_cpu(cpu
)
630 per_cpu_ptr(tr
->array_buffer
.data
, cpu
)->ignore_pid
= false;
632 rcu_assign_pointer(tr
->filtered_pids
, NULL
);
634 /* Wait till all users are no longer using pid filtering */
635 tracepoint_synchronize_unregister();
637 trace_free_pid_list(pid_list
);
640 static void ftrace_clear_event_pids(struct trace_array
*tr
)
642 mutex_lock(&event_mutex
);
643 __ftrace_clear_event_pids(tr
);
644 mutex_unlock(&event_mutex
);
647 static void __put_system(struct event_subsystem
*system
)
649 struct event_filter
*filter
= system
->filter
;
651 WARN_ON_ONCE(system_refcount(system
) == 0);
652 if (system_refcount_dec(system
))
655 list_del(&system
->list
);
658 kfree(filter
->filter_string
);
661 kfree_const(system
->name
);
665 static void __get_system(struct event_subsystem
*system
)
667 WARN_ON_ONCE(system_refcount(system
) == 0);
668 system_refcount_inc(system
);
671 static void __get_system_dir(struct trace_subsystem_dir
*dir
)
673 WARN_ON_ONCE(dir
->ref_count
== 0);
675 __get_system(dir
->subsystem
);
678 static void __put_system_dir(struct trace_subsystem_dir
*dir
)
680 WARN_ON_ONCE(dir
->ref_count
== 0);
681 /* If the subsystem is about to be freed, the dir must be too */
682 WARN_ON_ONCE(system_refcount(dir
->subsystem
) == 1 && dir
->ref_count
!= 1);
684 __put_system(dir
->subsystem
);
685 if (!--dir
->ref_count
)
689 static void put_system(struct trace_subsystem_dir
*dir
)
691 mutex_lock(&event_mutex
);
692 __put_system_dir(dir
);
693 mutex_unlock(&event_mutex
);
696 static void remove_subsystem(struct trace_subsystem_dir
*dir
)
701 if (!--dir
->nr_events
) {
702 tracefs_remove(dir
->entry
);
703 list_del(&dir
->list
);
704 __put_system_dir(dir
);
708 static void remove_event_file_dir(struct trace_event_file
*file
)
710 struct dentry
*dir
= file
->dir
;
711 struct dentry
*child
;
714 spin_lock(&dir
->d_lock
); /* probably unneeded */
715 list_for_each_entry(child
, &dir
->d_subdirs
, d_child
) {
716 if (d_really_is_positive(child
)) /* probably unneeded */
717 d_inode(child
)->i_private
= NULL
;
719 spin_unlock(&dir
->d_lock
);
724 list_del(&file
->list
);
725 remove_subsystem(file
->system
);
726 free_event_filter(file
->filter
);
727 kmem_cache_free(file_cachep
, file
);
731 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
734 __ftrace_set_clr_event_nolock(struct trace_array
*tr
, const char *match
,
735 const char *sub
, const char *event
, int set
)
737 struct trace_event_file
*file
;
738 struct trace_event_call
*call
;
743 list_for_each_entry(file
, &tr
->events
, list
) {
745 call
= file
->event_call
;
746 name
= trace_event_name(call
);
748 if (!name
|| !call
->class || !call
->class->reg
)
751 if (call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
755 strcmp(match
, name
) != 0 &&
756 strcmp(match
, call
->class->system
) != 0)
759 if (sub
&& strcmp(sub
, call
->class->system
) != 0)
762 if (event
&& strcmp(event
, name
) != 0)
765 ret
= ftrace_event_enable_disable(file
, set
);
768 * Save the first error and return that. Some events
769 * may still have been enabled, but let the user
770 * know that something went wrong.
781 static int __ftrace_set_clr_event(struct trace_array
*tr
, const char *match
,
782 const char *sub
, const char *event
, int set
)
786 mutex_lock(&event_mutex
);
787 ret
= __ftrace_set_clr_event_nolock(tr
, match
, sub
, event
, set
);
788 mutex_unlock(&event_mutex
);
793 int ftrace_set_clr_event(struct trace_array
*tr
, char *buf
, int set
)
795 char *event
= NULL
, *sub
= NULL
, *match
;
801 * The buf format can be <subsystem>:<event-name>
802 * *:<event-name> means any event by that name.
803 * :<event-name> is the same.
805 * <subsystem>:* means all events in that subsystem
806 * <subsystem>: means the same.
808 * <name> (no ':') means all events in a subsystem with
809 * the name <name> or any event that matches <name>
812 match
= strsep(&buf
, ":");
818 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
820 if (!strlen(event
) || strcmp(event
, "*") == 0)
824 ret
= __ftrace_set_clr_event(tr
, match
, sub
, event
, set
);
826 /* Put back the colon to allow this to be called again */
834 * trace_set_clr_event - enable or disable an event
835 * @system: system name to match (NULL for any system)
836 * @event: event name to match (NULL for all events, within system)
837 * @set: 1 to enable, 0 to disable
839 * This is a way for other parts of the kernel to enable or disable
842 * Returns 0 on success, -EINVAL if the parameters do not match any
845 int trace_set_clr_event(const char *system
, const char *event
, int set
)
847 struct trace_array
*tr
= top_trace_array();
852 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
854 EXPORT_SYMBOL_GPL(trace_set_clr_event
);
857 * trace_array_set_clr_event - enable or disable an event for a trace array.
858 * @tr: concerned trace array.
859 * @system: system name to match (NULL for any system)
860 * @event: event name to match (NULL for all events, within system)
861 * @enable: true to enable, false to disable
863 * This is a way for other parts of the kernel to enable or disable
866 * Returns 0 on success, -EINVAL if the parameters do not match any
869 int trace_array_set_clr_event(struct trace_array
*tr
, const char *system
,
870 const char *event
, bool enable
)
877 set
= (enable
== true) ? 1 : 0;
878 return __ftrace_set_clr_event(tr
, NULL
, system
, event
, set
);
880 EXPORT_SYMBOL_GPL(trace_array_set_clr_event
);
882 /* 128 should be much more than enough */
883 #define EVENT_BUF_SIZE 127
886 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
887 size_t cnt
, loff_t
*ppos
)
889 struct trace_parser parser
;
890 struct seq_file
*m
= file
->private_data
;
891 struct trace_array
*tr
= m
->private;
897 ret
= tracing_update_buffers();
901 if (trace_parser_get_init(&parser
, EVENT_BUF_SIZE
+ 1))
904 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
906 if (read
>= 0 && trace_parser_loaded((&parser
))) {
909 if (*parser
.buffer
== '!')
912 ret
= ftrace_set_clr_event(tr
, parser
.buffer
+ !set
, set
);
920 trace_parser_put(&parser
);
926 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
928 struct trace_event_file
*file
= v
;
929 struct trace_event_call
*call
;
930 struct trace_array
*tr
= m
->private;
934 list_for_each_entry_continue(file
, &tr
->events
, list
) {
935 call
= file
->event_call
;
937 * The ftrace subsystem is for showing formats only.
938 * They can not be enabled or disabled via the event files.
940 if (call
->class && call
->class->reg
&&
941 !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
948 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
950 struct trace_event_file
*file
;
951 struct trace_array
*tr
= m
->private;
954 mutex_lock(&event_mutex
);
956 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
957 for (l
= 0; l
<= *pos
; ) {
958 file
= t_next(m
, file
, &l
);
966 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
968 struct trace_event_file
*file
= v
;
969 struct trace_array
*tr
= m
->private;
973 list_for_each_entry_continue(file
, &tr
->events
, list
) {
974 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
981 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
983 struct trace_event_file
*file
;
984 struct trace_array
*tr
= m
->private;
987 mutex_lock(&event_mutex
);
989 file
= list_entry(&tr
->events
, struct trace_event_file
, list
);
990 for (l
= 0; l
<= *pos
; ) {
991 file
= s_next(m
, file
, &l
);
998 static int t_show(struct seq_file
*m
, void *v
)
1000 struct trace_event_file
*file
= v
;
1001 struct trace_event_call
*call
= file
->event_call
;
1003 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0)
1004 seq_printf(m
, "%s:", call
->class->system
);
1005 seq_printf(m
, "%s\n", trace_event_name(call
));
1010 static void t_stop(struct seq_file
*m
, void *p
)
1012 mutex_unlock(&event_mutex
);
1016 p_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1018 struct trace_array
*tr
= m
->private;
1019 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1021 return trace_pid_next(pid_list
, v
, pos
);
1024 static void *p_start(struct seq_file
*m
, loff_t
*pos
)
1027 struct trace_pid_list
*pid_list
;
1028 struct trace_array
*tr
= m
->private;
1031 * Grab the mutex, to keep calls to p_next() having the same
1032 * tr->filtered_pids as p_start() has.
1033 * If we just passed the tr->filtered_pids around, then RCU would
1034 * have been enough, but doing that makes things more complex.
1036 mutex_lock(&event_mutex
);
1037 rcu_read_lock_sched();
1039 pid_list
= rcu_dereference_sched(tr
->filtered_pids
);
1044 return trace_pid_start(pid_list
, pos
);
1047 static void p_stop(struct seq_file
*m
, void *p
)
1050 rcu_read_unlock_sched();
1051 mutex_unlock(&event_mutex
);
1055 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1058 struct trace_event_file
*file
;
1059 unsigned long flags
;
1062 mutex_lock(&event_mutex
);
1063 file
= event_file_data(filp
);
1065 flags
= file
->flags
;
1066 mutex_unlock(&event_mutex
);
1071 if (flags
& EVENT_FILE_FL_ENABLED
&&
1072 !(flags
& EVENT_FILE_FL_SOFT_DISABLED
))
1075 if (flags
& EVENT_FILE_FL_SOFT_DISABLED
||
1076 flags
& EVENT_FILE_FL_SOFT_MODE
)
1081 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, strlen(buf
));
1085 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1088 struct trace_event_file
*file
;
1092 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1096 ret
= tracing_update_buffers();
1104 mutex_lock(&event_mutex
);
1105 file
= event_file_data(filp
);
1107 ret
= ftrace_event_enable_disable(file
, val
);
1108 mutex_unlock(&event_mutex
);
1117 return ret
? ret
: cnt
;
1121 system_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1124 const char set_to_char
[4] = { '?', '0', '1', 'X' };
1125 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1126 struct event_subsystem
*system
= dir
->subsystem
;
1127 struct trace_event_call
*call
;
1128 struct trace_event_file
*file
;
1129 struct trace_array
*tr
= dir
->tr
;
1134 mutex_lock(&event_mutex
);
1135 list_for_each_entry(file
, &tr
->events
, list
) {
1136 call
= file
->event_call
;
1137 if (!trace_event_name(call
) || !call
->class || !call
->class->reg
)
1140 if (system
&& strcmp(call
->class->system
, system
->name
) != 0)
1144 * We need to find out if all the events are set
1145 * or if all events or cleared, or if we have
1148 set
|= (1 << !!(file
->flags
& EVENT_FILE_FL_ENABLED
));
1151 * If we have a mixture, no need to look further.
1156 mutex_unlock(&event_mutex
);
1158 buf
[0] = set_to_char
[set
];
1161 ret
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
1167 system_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1170 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1171 struct event_subsystem
*system
= dir
->subsystem
;
1172 const char *name
= NULL
;
1176 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1180 ret
= tracing_update_buffers();
1184 if (val
!= 0 && val
!= 1)
1188 * Opening of "enable" adds a ref count to system,
1189 * so the name is safe to use.
1192 name
= system
->name
;
1194 ret
= __ftrace_set_clr_event(dir
->tr
, NULL
, name
, NULL
, val
);
1208 FORMAT_FIELD_SEPERATOR
= 2,
1209 FORMAT_PRINTFMT
= 3,
1212 static void *f_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1214 struct trace_event_call
*call
= event_file_data(m
->private);
1215 struct list_head
*common_head
= &ftrace_common_fields
;
1216 struct list_head
*head
= trace_get_fields(call
);
1217 struct list_head
*node
= v
;
1221 switch ((unsigned long)v
) {
1226 case FORMAT_FIELD_SEPERATOR
:
1230 case FORMAT_PRINTFMT
:
1236 if (node
== common_head
)
1237 return (void *)FORMAT_FIELD_SEPERATOR
;
1238 else if (node
== head
)
1239 return (void *)FORMAT_PRINTFMT
;
1244 static int f_show(struct seq_file
*m
, void *v
)
1246 struct trace_event_call
*call
= event_file_data(m
->private);
1247 struct ftrace_event_field
*field
;
1248 const char *array_descriptor
;
1250 switch ((unsigned long)v
) {
1252 seq_printf(m
, "name: %s\n", trace_event_name(call
));
1253 seq_printf(m
, "ID: %d\n", call
->event
.type
);
1254 seq_puts(m
, "format:\n");
1257 case FORMAT_FIELD_SEPERATOR
:
1261 case FORMAT_PRINTFMT
:
1262 seq_printf(m
, "\nprint fmt: %s\n",
1267 field
= list_entry(v
, struct ftrace_event_field
, link
);
1269 * Smartly shows the array type(except dynamic array).
1272 * If TYPE := TYPE[LEN], it is shown:
1273 * field:TYPE VAR[LEN]
1275 array_descriptor
= strchr(field
->type
, '[');
1277 if (str_has_prefix(field
->type
, "__data_loc"))
1278 array_descriptor
= NULL
;
1280 if (!array_descriptor
)
1281 seq_printf(m
, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1282 field
->type
, field
->name
, field
->offset
,
1283 field
->size
, !!field
->is_signed
);
1285 seq_printf(m
, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1286 (int)(array_descriptor
- field
->type
),
1287 field
->type
, field
->name
,
1288 array_descriptor
, field
->offset
,
1289 field
->size
, !!field
->is_signed
);
1294 static void *f_start(struct seq_file
*m
, loff_t
*pos
)
1296 void *p
= (void *)FORMAT_HEADER
;
1299 /* ->stop() is called even if ->start() fails */
1300 mutex_lock(&event_mutex
);
1301 if (!event_file_data(m
->private))
1302 return ERR_PTR(-ENODEV
);
1304 while (l
< *pos
&& p
)
1305 p
= f_next(m
, p
, &l
);
1310 static void f_stop(struct seq_file
*m
, void *p
)
1312 mutex_unlock(&event_mutex
);
1315 static const struct seq_operations trace_format_seq_ops
= {
1322 static int trace_format_open(struct inode
*inode
, struct file
*file
)
1327 /* Do we want to hide event format files on tracefs lockdown? */
1329 ret
= seq_open(file
, &trace_format_seq_ops
);
1333 m
= file
->private_data
;
1340 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1342 int id
= (long)event_file_data(filp
);
1349 len
= sprintf(buf
, "%d\n", id
);
1351 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
1355 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1358 struct trace_event_file
*file
;
1359 struct trace_seq
*s
;
1365 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1372 mutex_lock(&event_mutex
);
1373 file
= event_file_data(filp
);
1375 print_event_filter(file
, s
);
1376 mutex_unlock(&event_mutex
);
1379 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1380 s
->buffer
, trace_seq_used(s
));
1388 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1391 struct trace_event_file
*file
;
1395 if (cnt
>= PAGE_SIZE
)
1398 buf
= memdup_user_nul(ubuf
, cnt
);
1400 return PTR_ERR(buf
);
1402 mutex_lock(&event_mutex
);
1403 file
= event_file_data(filp
);
1405 err
= apply_event_filter(file
, buf
);
1406 mutex_unlock(&event_mutex
);
1417 static LIST_HEAD(event_subsystems
);
1419 static int subsystem_open(struct inode
*inode
, struct file
*filp
)
1421 struct event_subsystem
*system
= NULL
;
1422 struct trace_subsystem_dir
*dir
= NULL
; /* Initialize for gcc */
1423 struct trace_array
*tr
;
1426 if (tracing_is_disabled())
1429 /* Make sure the system still exists */
1430 mutex_lock(&event_mutex
);
1431 mutex_lock(&trace_types_lock
);
1432 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
1433 list_for_each_entry(dir
, &tr
->systems
, list
) {
1434 if (dir
== inode
->i_private
) {
1435 /* Don't open systems with no events */
1436 if (dir
->nr_events
) {
1437 __get_system_dir(dir
);
1438 system
= dir
->subsystem
;
1445 mutex_unlock(&trace_types_lock
);
1446 mutex_unlock(&event_mutex
);
1451 /* Some versions of gcc think dir can be uninitialized here */
1454 /* Still need to increment the ref count of the system */
1455 if (trace_array_get(tr
) < 0) {
1460 ret
= tracing_open_generic(inode
, filp
);
1462 trace_array_put(tr
);
1469 static int system_tr_open(struct inode
*inode
, struct file
*filp
)
1471 struct trace_subsystem_dir
*dir
;
1472 struct trace_array
*tr
= inode
->i_private
;
1475 /* Make a temporary dir that has no system but points to tr */
1476 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1480 ret
= tracing_open_generic_tr(inode
, filp
);
1486 filp
->private_data
= dir
;
1491 static int subsystem_release(struct inode
*inode
, struct file
*file
)
1493 struct trace_subsystem_dir
*dir
= file
->private_data
;
1495 trace_array_put(dir
->tr
);
1498 * If dir->subsystem is NULL, then this is a temporary
1499 * descriptor that was made for a trace_array to enable
1511 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
1514 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1515 struct event_subsystem
*system
= dir
->subsystem
;
1516 struct trace_seq
*s
;
1522 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1528 print_subsystem_event_filter(system
, s
);
1529 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1530 s
->buffer
, trace_seq_used(s
));
1538 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
1541 struct trace_subsystem_dir
*dir
= filp
->private_data
;
1545 if (cnt
>= PAGE_SIZE
)
1548 buf
= memdup_user_nul(ubuf
, cnt
);
1550 return PTR_ERR(buf
);
1552 err
= apply_subsystem_event_filter(dir
, buf
);
1563 show_header(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
1565 int (*func
)(struct trace_seq
*s
) = filp
->private_data
;
1566 struct trace_seq
*s
;
1572 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1579 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
1580 s
->buffer
, trace_seq_used(s
));
1587 static void ignore_task_cpu(void *data
)
1589 struct trace_array
*tr
= data
;
1590 struct trace_pid_list
*pid_list
;
1593 * This function is called by on_each_cpu() while the
1594 * event_mutex is held.
1596 pid_list
= rcu_dereference_protected(tr
->filtered_pids
,
1597 mutex_is_locked(&event_mutex
));
1599 this_cpu_write(tr
->array_buffer
.data
->ignore_pid
,
1600 trace_ignore_this_task(pid_list
, current
));
1604 ftrace_event_pid_write(struct file
*filp
, const char __user
*ubuf
,
1605 size_t cnt
, loff_t
*ppos
)
1607 struct seq_file
*m
= filp
->private_data
;
1608 struct trace_array
*tr
= m
->private;
1609 struct trace_pid_list
*filtered_pids
= NULL
;
1610 struct trace_pid_list
*pid_list
;
1611 struct trace_event_file
*file
;
1617 ret
= tracing_update_buffers();
1621 mutex_lock(&event_mutex
);
1623 filtered_pids
= rcu_dereference_protected(tr
->filtered_pids
,
1624 lockdep_is_held(&event_mutex
));
1626 ret
= trace_pid_write(filtered_pids
, &pid_list
, ubuf
, cnt
);
1630 rcu_assign_pointer(tr
->filtered_pids
, pid_list
);
1632 list_for_each_entry(file
, &tr
->events
, list
) {
1633 set_bit(EVENT_FILE_FL_PID_FILTER_BIT
, &file
->flags
);
1636 if (filtered_pids
) {
1637 tracepoint_synchronize_unregister();
1638 trace_free_pid_list(filtered_pids
);
1639 } else if (pid_list
) {
1641 * Register a probe that is called before all other probes
1642 * to set ignore_pid if next or prev do not match.
1643 * Register a probe this is called after all other probes
1644 * to only keep ignore_pid set if next pid matches.
1646 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre
,
1648 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post
,
1651 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre
,
1653 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post
,
1656 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre
,
1658 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post
,
1661 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre
,
1663 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post
,
1668 * Ignoring of pids is done at task switch. But we have to
1669 * check for those tasks that are currently running.
1670 * Always do this in case a pid was appended or removed.
1672 on_each_cpu(ignore_task_cpu
, tr
, 1);
1675 mutex_unlock(&event_mutex
);
1683 static int ftrace_event_avail_open(struct inode
*inode
, struct file
*file
);
1684 static int ftrace_event_set_open(struct inode
*inode
, struct file
*file
);
1685 static int ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
);
1686 static int ftrace_event_release(struct inode
*inode
, struct file
*file
);
1688 static const struct seq_operations show_event_seq_ops
= {
1695 static const struct seq_operations show_set_event_seq_ops
= {
1702 static const struct seq_operations show_set_pid_seq_ops
= {
1705 .show
= trace_pid_show
,
1709 static const struct file_operations ftrace_avail_fops
= {
1710 .open
= ftrace_event_avail_open
,
1712 .llseek
= seq_lseek
,
1713 .release
= seq_release
,
1716 static const struct file_operations ftrace_set_event_fops
= {
1717 .open
= ftrace_event_set_open
,
1719 .write
= ftrace_event_write
,
1720 .llseek
= seq_lseek
,
1721 .release
= ftrace_event_release
,
1724 static const struct file_operations ftrace_set_event_pid_fops
= {
1725 .open
= ftrace_event_set_pid_open
,
1727 .write
= ftrace_event_pid_write
,
1728 .llseek
= seq_lseek
,
1729 .release
= ftrace_event_release
,
1732 static const struct file_operations ftrace_enable_fops
= {
1733 .open
= tracing_open_generic
,
1734 .read
= event_enable_read
,
1735 .write
= event_enable_write
,
1736 .llseek
= default_llseek
,
1739 static const struct file_operations ftrace_event_format_fops
= {
1740 .open
= trace_format_open
,
1742 .llseek
= seq_lseek
,
1743 .release
= seq_release
,
1746 static const struct file_operations ftrace_event_id_fops
= {
1747 .read
= event_id_read
,
1748 .llseek
= default_llseek
,
1751 static const struct file_operations ftrace_event_filter_fops
= {
1752 .open
= tracing_open_generic
,
1753 .read
= event_filter_read
,
1754 .write
= event_filter_write
,
1755 .llseek
= default_llseek
,
1758 static const struct file_operations ftrace_subsystem_filter_fops
= {
1759 .open
= subsystem_open
,
1760 .read
= subsystem_filter_read
,
1761 .write
= subsystem_filter_write
,
1762 .llseek
= default_llseek
,
1763 .release
= subsystem_release
,
1766 static const struct file_operations ftrace_system_enable_fops
= {
1767 .open
= subsystem_open
,
1768 .read
= system_enable_read
,
1769 .write
= system_enable_write
,
1770 .llseek
= default_llseek
,
1771 .release
= subsystem_release
,
1774 static const struct file_operations ftrace_tr_enable_fops
= {
1775 .open
= system_tr_open
,
1776 .read
= system_enable_read
,
1777 .write
= system_enable_write
,
1778 .llseek
= default_llseek
,
1779 .release
= subsystem_release
,
1782 static const struct file_operations ftrace_show_header_fops
= {
1783 .open
= tracing_open_generic
,
1784 .read
= show_header
,
1785 .llseek
= default_llseek
,
1789 ftrace_event_open(struct inode
*inode
, struct file
*file
,
1790 const struct seq_operations
*seq_ops
)
1795 ret
= security_locked_down(LOCKDOWN_TRACEFS
);
1799 ret
= seq_open(file
, seq_ops
);
1802 m
= file
->private_data
;
1803 /* copy tr over to seq ops */
1804 m
->private = inode
->i_private
;
1809 static int ftrace_event_release(struct inode
*inode
, struct file
*file
)
1811 struct trace_array
*tr
= inode
->i_private
;
1813 trace_array_put(tr
);
1815 return seq_release(inode
, file
);
1819 ftrace_event_avail_open(struct inode
*inode
, struct file
*file
)
1821 const struct seq_operations
*seq_ops
= &show_event_seq_ops
;
1823 /* Checks for tracefs lockdown */
1824 return ftrace_event_open(inode
, file
, seq_ops
);
1828 ftrace_event_set_open(struct inode
*inode
, struct file
*file
)
1830 const struct seq_operations
*seq_ops
= &show_set_event_seq_ops
;
1831 struct trace_array
*tr
= inode
->i_private
;
1834 ret
= tracing_check_open_get_tr(tr
);
1838 if ((file
->f_mode
& FMODE_WRITE
) &&
1839 (file
->f_flags
& O_TRUNC
))
1840 ftrace_clear_events(tr
);
1842 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1844 trace_array_put(tr
);
1849 ftrace_event_set_pid_open(struct inode
*inode
, struct file
*file
)
1851 const struct seq_operations
*seq_ops
= &show_set_pid_seq_ops
;
1852 struct trace_array
*tr
= inode
->i_private
;
1855 ret
= tracing_check_open_get_tr(tr
);
1859 if ((file
->f_mode
& FMODE_WRITE
) &&
1860 (file
->f_flags
& O_TRUNC
))
1861 ftrace_clear_event_pids(tr
);
1863 ret
= ftrace_event_open(inode
, file
, seq_ops
);
1865 trace_array_put(tr
);
1869 static struct event_subsystem
*
1870 create_new_subsystem(const char *name
)
1872 struct event_subsystem
*system
;
1874 /* need to create new entry */
1875 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
1879 system
->ref_count
= 1;
1881 /* Only allocate if dynamic (kprobes and modules) */
1882 system
->name
= kstrdup_const(name
, GFP_KERNEL
);
1886 system
->filter
= NULL
;
1888 system
->filter
= kzalloc(sizeof(struct event_filter
), GFP_KERNEL
);
1889 if (!system
->filter
)
1892 list_add(&system
->list
, &event_subsystems
);
1897 kfree_const(system
->name
);
1902 static struct dentry
*
1903 event_subsystem_dir(struct trace_array
*tr
, const char *name
,
1904 struct trace_event_file
*file
, struct dentry
*parent
)
1906 struct trace_subsystem_dir
*dir
;
1907 struct event_subsystem
*system
;
1908 struct dentry
*entry
;
1910 /* First see if we did not already create this dir */
1911 list_for_each_entry(dir
, &tr
->systems
, list
) {
1912 system
= dir
->subsystem
;
1913 if (strcmp(system
->name
, name
) == 0) {
1920 /* Now see if the system itself exists. */
1921 list_for_each_entry(system
, &event_subsystems
, list
) {
1922 if (strcmp(system
->name
, name
) == 0)
1925 /* Reset system variable when not found */
1926 if (&system
->list
== &event_subsystems
)
1929 dir
= kmalloc(sizeof(*dir
), GFP_KERNEL
);
1934 system
= create_new_subsystem(name
);
1938 __get_system(system
);
1940 dir
->entry
= tracefs_create_dir(name
, parent
);
1942 pr_warn("Failed to create system directory %s\n", name
);
1943 __put_system(system
);
1950 dir
->subsystem
= system
;
1953 entry
= tracefs_create_file("filter", 0644, dir
->entry
, dir
,
1954 &ftrace_subsystem_filter_fops
);
1956 kfree(system
->filter
);
1957 system
->filter
= NULL
;
1958 pr_warn("Could not create tracefs '%s/filter' entry\n", name
);
1961 trace_create_file("enable", 0644, dir
->entry
, dir
,
1962 &ftrace_system_enable_fops
);
1964 list_add(&dir
->list
, &tr
->systems
);
1971 /* Only print this message if failed on memory allocation */
1972 if (!dir
|| !system
)
1973 pr_warn("No memory to create event subsystem %s\n", name
);
1978 event_create_dir(struct dentry
*parent
, struct trace_event_file
*file
)
1980 struct trace_event_call
*call
= file
->event_call
;
1981 struct trace_array
*tr
= file
->tr
;
1982 struct list_head
*head
;
1983 struct dentry
*d_events
;
1988 * If the trace point header did not define TRACE_SYSTEM
1989 * then the system would be called "TRACE_SYSTEM".
1991 if (strcmp(call
->class->system
, TRACE_SYSTEM
) != 0) {
1992 d_events
= event_subsystem_dir(tr
, call
->class->system
, file
, parent
);
1998 name
= trace_event_name(call
);
1999 file
->dir
= tracefs_create_dir(name
, d_events
);
2001 pr_warn("Could not create tracefs '%s' directory\n", name
);
2005 if (call
->class->reg
&& !(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
))
2006 trace_create_file("enable", 0644, file
->dir
, file
,
2007 &ftrace_enable_fops
);
2009 #ifdef CONFIG_PERF_EVENTS
2010 if (call
->event
.type
&& call
->class->reg
)
2011 trace_create_file("id", 0444, file
->dir
,
2012 (void *)(long)call
->event
.type
,
2013 &ftrace_event_id_fops
);
2017 * Other events may have the same class. Only update
2018 * the fields if they are not already defined.
2020 head
= trace_get_fields(call
);
2021 if (list_empty(head
)) {
2022 struct trace_event_fields
*field
= call
->class->fields_array
;
2023 unsigned int offset
= sizeof(struct trace_entry
);
2025 for (; field
->type
; field
++) {
2026 if (field
->type
== TRACE_FUNCTION_TYPE
) {
2027 ret
= field
->define_fields(call
);
2031 offset
= ALIGN(offset
, field
->align
);
2032 ret
= trace_define_field(call
, field
->type
, field
->name
,
2033 offset
, field
->size
,
2034 field
->is_signed
, field
->filter_type
);
2038 offset
+= field
->size
;
2041 pr_warn("Could not initialize trace point events/%s\n",
2048 * Only event directories that can be enabled should have
2049 * triggers or filters.
2051 if (!(call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)) {
2052 trace_create_file("filter", 0644, file
->dir
, file
,
2053 &ftrace_event_filter_fops
);
2055 trace_create_file("trigger", 0644, file
->dir
, file
,
2056 &event_trigger_fops
);
2059 #ifdef CONFIG_HIST_TRIGGERS
2060 trace_create_file("hist", 0444, file
->dir
, file
,
2063 trace_create_file("format", 0444, file
->dir
, call
,
2064 &ftrace_event_format_fops
);
2066 #ifdef CONFIG_TRACE_EVENT_INJECT
2067 if (call
->event
.type
&& call
->class->reg
)
2068 trace_create_file("inject", 0200, file
->dir
, file
,
2069 &event_inject_fops
);
2075 static void remove_event_from_tracers(struct trace_event_call
*call
)
2077 struct trace_event_file
*file
;
2078 struct trace_array
*tr
;
2080 do_for_each_event_file_safe(tr
, file
) {
2081 if (file
->event_call
!= call
)
2084 remove_event_file_dir(file
);
2086 * The do_for_each_event_file_safe() is
2087 * a double loop. After finding the call for this
2088 * trace_array, we use break to jump to the next
2092 } while_for_each_event_file();
2095 static void event_remove(struct trace_event_call
*call
)
2097 struct trace_array
*tr
;
2098 struct trace_event_file
*file
;
2100 do_for_each_event_file(tr
, file
) {
2101 if (file
->event_call
!= call
)
2104 if (file
->flags
& EVENT_FILE_FL_WAS_ENABLED
)
2105 tr
->clear_trace
= true;
2107 ftrace_event_enable_disable(file
, 0);
2109 * The do_for_each_event_file() is
2110 * a double loop. After finding the call for this
2111 * trace_array, we use break to jump to the next
2115 } while_for_each_event_file();
2117 if (call
->event
.funcs
)
2118 __unregister_trace_event(&call
->event
);
2119 remove_event_from_tracers(call
);
2120 list_del(&call
->list
);
2123 static int event_init(struct trace_event_call
*call
)
2128 name
= trace_event_name(call
);
2132 if (call
->class->raw_init
) {
2133 ret
= call
->class->raw_init(call
);
2134 if (ret
< 0 && ret
!= -ENOSYS
)
2135 pr_warn("Could not initialize trace events/%s\n", name
);
2142 __register_event(struct trace_event_call
*call
, struct module
*mod
)
2146 ret
= event_init(call
);
2150 list_add(&call
->list
, &ftrace_events
);
2156 static char *eval_replace(char *ptr
, struct trace_eval_map
*map
, int len
)
2161 /* Find the length of the eval value as a string */
2162 elen
= snprintf(ptr
, 0, "%ld", map
->eval_value
);
2163 /* Make sure there's enough room to replace the string with the value */
2167 snprintf(ptr
, elen
+ 1, "%ld", map
->eval_value
);
2169 /* Get the rest of the string of ptr */
2170 rlen
= strlen(ptr
+ len
);
2171 memmove(ptr
+ elen
, ptr
+ len
, rlen
);
2172 /* Make sure we end the new string */
2173 ptr
[elen
+ rlen
] = 0;
2178 static void update_event_printk(struct trace_event_call
*call
,
2179 struct trace_eval_map
*map
)
2183 int len
= strlen(map
->eval_string
);
2185 for (ptr
= call
->print_fmt
; *ptr
; ptr
++) {
2199 if (isdigit(*ptr
)) {
2203 /* Check for alpha chars like ULL */
2204 } while (isalnum(*ptr
));
2208 * A number must have some kind of delimiter after
2209 * it, and we can ignore that too.
2213 if (isalpha(*ptr
) || *ptr
== '_') {
2214 if (strncmp(map
->eval_string
, ptr
, len
) == 0 &&
2215 !isalnum(ptr
[len
]) && ptr
[len
] != '_') {
2216 ptr
= eval_replace(ptr
, map
, len
);
2217 /* enum/sizeof string smaller than value */
2218 if (WARN_ON_ONCE(!ptr
))
2221 * No need to decrement here, as eval_replace()
2222 * returns the pointer to the character passed
2223 * the eval, and two evals can not be placed
2224 * back to back without something in between.
2225 * We can skip that something in between.
2232 } while (isalnum(*ptr
) || *ptr
== '_');
2236 * If what comes after this variable is a '.' or
2237 * '->' then we can continue to ignore that string.
2239 if (*ptr
== '.' || (ptr
[0] == '-' && ptr
[1] == '>')) {
2240 ptr
+= *ptr
== '.' ? 1 : 2;
2246 * Once again, we can skip the delimiter that came
2254 void trace_event_eval_update(struct trace_eval_map
**map
, int len
)
2256 struct trace_event_call
*call
, *p
;
2257 const char *last_system
= NULL
;
2262 down_write(&trace_event_sem
);
2263 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2264 /* events are usually grouped together with systems */
2265 if (!last_system
|| call
->class->system
!= last_system
) {
2268 last_system
= call
->class->system
;
2272 * Since calls are grouped by systems, the likelyhood that the
2273 * next call in the iteration belongs to the same system as the
2274 * previous call is high. As an optimization, we skip seaching
2275 * for a map[] that matches the call's system if the last call
2276 * was from the same system. That's what last_i is for. If the
2277 * call has the same system as the previous call, then last_i
2278 * will be the index of the first map[] that has a matching
2281 for (i
= last_i
; i
< len
; i
++) {
2282 if (call
->class->system
== map
[i
]->system
) {
2283 /* Save the first system if need be */
2288 update_event_printk(call
, map
[i
]);
2292 up_write(&trace_event_sem
);
2295 static struct trace_event_file
*
2296 trace_create_new_event(struct trace_event_call
*call
,
2297 struct trace_array
*tr
)
2299 struct trace_event_file
*file
;
2301 file
= kmem_cache_alloc(file_cachep
, GFP_TRACE
);
2305 file
->event_call
= call
;
2307 atomic_set(&file
->sm_ref
, 0);
2308 atomic_set(&file
->tm_ref
, 0);
2309 INIT_LIST_HEAD(&file
->triggers
);
2310 list_add(&file
->list
, &tr
->events
);
2315 /* Add an event to a trace directory */
2317 __trace_add_new_event(struct trace_event_call
*call
, struct trace_array
*tr
)
2319 struct trace_event_file
*file
;
2321 file
= trace_create_new_event(call
, tr
);
2325 return event_create_dir(tr
->event_dir
, file
);
2329 * Just create a decriptor for early init. A descriptor is required
2330 * for enabling events at boot. We want to enable events before
2331 * the filesystem is initialized.
2334 __trace_early_add_new_event(struct trace_event_call
*call
,
2335 struct trace_array
*tr
)
2337 struct trace_event_file
*file
;
2339 file
= trace_create_new_event(call
, tr
);
2346 struct ftrace_module_file_ops
;
2347 static void __add_event_to_tracers(struct trace_event_call
*call
);
2349 /* Add an additional event_call dynamically */
2350 int trace_add_event_call(struct trace_event_call
*call
)
2353 lockdep_assert_held(&event_mutex
);
2355 mutex_lock(&trace_types_lock
);
2357 ret
= __register_event(call
, NULL
);
2359 __add_event_to_tracers(call
);
2361 mutex_unlock(&trace_types_lock
);
2366 * Must be called under locking of trace_types_lock, event_mutex and
2369 static void __trace_remove_event_call(struct trace_event_call
*call
)
2372 trace_destroy_fields(call
);
2373 free_event_filter(call
->filter
);
2374 call
->filter
= NULL
;
2377 static int probe_remove_event_call(struct trace_event_call
*call
)
2379 struct trace_array
*tr
;
2380 struct trace_event_file
*file
;
2382 #ifdef CONFIG_PERF_EVENTS
2383 if (call
->perf_refcount
)
2386 do_for_each_event_file(tr
, file
) {
2387 if (file
->event_call
!= call
)
2390 * We can't rely on ftrace_event_enable_disable(enable => 0)
2391 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2392 * TRACE_REG_UNREGISTER.
2394 if (file
->flags
& EVENT_FILE_FL_ENABLED
)
2397 * The do_for_each_event_file_safe() is
2398 * a double loop. After finding the call for this
2399 * trace_array, we use break to jump to the next
2403 } while_for_each_event_file();
2405 __trace_remove_event_call(call
);
2410 /* Remove an event_call */
2411 int trace_remove_event_call(struct trace_event_call
*call
)
2415 lockdep_assert_held(&event_mutex
);
2417 mutex_lock(&trace_types_lock
);
2418 down_write(&trace_event_sem
);
2419 ret
= probe_remove_event_call(call
);
2420 up_write(&trace_event_sem
);
2421 mutex_unlock(&trace_types_lock
);
2426 #define for_each_event(event, start, end) \
2427 for (event = start; \
2428 (unsigned long)event < (unsigned long)end; \
2431 #ifdef CONFIG_MODULES
2433 static void trace_module_add_events(struct module
*mod
)
2435 struct trace_event_call
**call
, **start
, **end
;
2437 if (!mod
->num_trace_events
)
2440 /* Don't add infrastructure for mods without tracepoints */
2441 if (trace_module_has_bad_taint(mod
)) {
2442 pr_err("%s: module has bad taint, not creating trace events\n",
2447 start
= mod
->trace_events
;
2448 end
= mod
->trace_events
+ mod
->num_trace_events
;
2450 for_each_event(call
, start
, end
) {
2451 __register_event(*call
, mod
);
2452 __add_event_to_tracers(*call
);
2456 static void trace_module_remove_events(struct module
*mod
)
2458 struct trace_event_call
*call
, *p
;
2460 down_write(&trace_event_sem
);
2461 list_for_each_entry_safe(call
, p
, &ftrace_events
, list
) {
2462 if (call
->mod
== mod
)
2463 __trace_remove_event_call(call
);
2465 up_write(&trace_event_sem
);
2468 * It is safest to reset the ring buffer if the module being unloaded
2469 * registered any events that were used. The only worry is if
2470 * a new module gets loaded, and takes on the same id as the events
2471 * of this module. When printing out the buffer, traced events left
2472 * over from this module may be passed to the new module events and
2473 * unexpected results may occur.
2475 tracing_reset_all_online_cpus();
2478 static int trace_module_notify(struct notifier_block
*self
,
2479 unsigned long val
, void *data
)
2481 struct module
*mod
= data
;
2483 mutex_lock(&event_mutex
);
2484 mutex_lock(&trace_types_lock
);
2486 case MODULE_STATE_COMING
:
2487 trace_module_add_events(mod
);
2489 case MODULE_STATE_GOING
:
2490 trace_module_remove_events(mod
);
2493 mutex_unlock(&trace_types_lock
);
2494 mutex_unlock(&event_mutex
);
2499 static struct notifier_block trace_module_nb
= {
2500 .notifier_call
= trace_module_notify
,
2501 .priority
= 1, /* higher than trace.c module notify */
2503 #endif /* CONFIG_MODULES */
2505 /* Create a new event directory structure for a trace directory. */
2507 __trace_add_event_dirs(struct trace_array
*tr
)
2509 struct trace_event_call
*call
;
2512 list_for_each_entry(call
, &ftrace_events
, list
) {
2513 ret
= __trace_add_new_event(call
, tr
);
2515 pr_warn("Could not create directory for event %s\n",
2516 trace_event_name(call
));
2520 /* Returns any file that matches the system and event */
2521 struct trace_event_file
*
2522 __find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
2524 struct trace_event_file
*file
;
2525 struct trace_event_call
*call
;
2528 list_for_each_entry(file
, &tr
->events
, list
) {
2530 call
= file
->event_call
;
2531 name
= trace_event_name(call
);
2533 if (!name
|| !call
->class)
2536 if (strcmp(event
, name
) == 0 &&
2537 strcmp(system
, call
->class->system
) == 0)
2543 /* Returns valid trace event files that match system and event */
2544 struct trace_event_file
*
2545 find_event_file(struct trace_array
*tr
, const char *system
, const char *event
)
2547 struct trace_event_file
*file
;
2549 file
= __find_event_file(tr
, system
, event
);
2550 if (!file
|| !file
->event_call
->class->reg
||
2551 file
->event_call
->flags
& TRACE_EVENT_FL_IGNORE_ENABLE
)
2558 * trace_get_event_file - Find and return a trace event file
2559 * @instance: The name of the trace instance containing the event
2560 * @system: The name of the system containing the event
2561 * @event: The name of the event
2563 * Return a trace event file given the trace instance name, trace
2564 * system, and trace event name. If the instance name is NULL, it
2565 * refers to the top-level trace array.
2567 * This function will look it up and return it if found, after calling
2568 * trace_array_get() to prevent the instance from going away, and
2569 * increment the event's module refcount to prevent it from being
2572 * To release the file, call trace_put_event_file(), which will call
2573 * trace_array_put() and decrement the event's module refcount.
2575 * Return: The trace event on success, ERR_PTR otherwise.
2577 struct trace_event_file
*trace_get_event_file(const char *instance
,
2581 struct trace_array
*tr
= top_trace_array();
2582 struct trace_event_file
*file
= NULL
;
2586 tr
= trace_array_find_get(instance
);
2588 return ERR_PTR(-ENOENT
);
2590 ret
= trace_array_get(tr
);
2592 return ERR_PTR(ret
);
2595 mutex_lock(&event_mutex
);
2597 file
= find_event_file(tr
, system
, event
);
2599 trace_array_put(tr
);
2604 /* Don't let event modules unload while in use */
2605 ret
= try_module_get(file
->event_call
->mod
);
2607 trace_array_put(tr
);
2614 mutex_unlock(&event_mutex
);
2617 file
= ERR_PTR(ret
);
2621 EXPORT_SYMBOL_GPL(trace_get_event_file
);
2624 * trace_put_event_file - Release a file from trace_get_event_file()
2625 * @file: The trace event file
2627 * If a file was retrieved using trace_get_event_file(), this should
2628 * be called when it's no longer needed. It will cancel the previous
2629 * trace_array_get() called by that function, and decrement the
2630 * event's module refcount.
2632 void trace_put_event_file(struct trace_event_file
*file
)
2634 mutex_lock(&event_mutex
);
2635 module_put(file
->event_call
->mod
);
2636 mutex_unlock(&event_mutex
);
2638 trace_array_put(file
->tr
);
2640 EXPORT_SYMBOL_GPL(trace_put_event_file
);
2642 #ifdef CONFIG_DYNAMIC_FTRACE
2645 #define ENABLE_EVENT_STR "enable_event"
2646 #define DISABLE_EVENT_STR "disable_event"
2648 struct event_probe_data
{
2649 struct trace_event_file
*file
;
2650 unsigned long count
;
2655 static void update_event_probe(struct event_probe_data
*data
)
2658 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2660 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT
, &data
->file
->flags
);
2664 event_enable_probe(unsigned long ip
, unsigned long parent_ip
,
2665 struct trace_array
*tr
, struct ftrace_probe_ops
*ops
,
2668 struct ftrace_func_mapper
*mapper
= data
;
2669 struct event_probe_data
*edata
;
2672 pdata
= ftrace_func_mapper_find_ip(mapper
, ip
);
2673 if (!pdata
|| !*pdata
)
2677 update_event_probe(edata
);
2681 event_enable_count_probe(unsigned long ip
, unsigned long parent_ip
,
2682 struct trace_array
*tr
, struct ftrace_probe_ops
*ops
,
2685 struct ftrace_func_mapper
*mapper
= data
;
2686 struct event_probe_data
*edata
;
2689 pdata
= ftrace_func_mapper_find_ip(mapper
, ip
);
2690 if (!pdata
|| !*pdata
)
2698 /* Skip if the event is in a state we want to switch to */
2699 if (edata
->enable
== !(edata
->file
->flags
& EVENT_FILE_FL_SOFT_DISABLED
))
2702 if (edata
->count
!= -1)
2705 update_event_probe(edata
);
2709 event_enable_print(struct seq_file
*m
, unsigned long ip
,
2710 struct ftrace_probe_ops
*ops
, void *data
)
2712 struct ftrace_func_mapper
*mapper
= data
;
2713 struct event_probe_data
*edata
;
2716 pdata
= ftrace_func_mapper_find_ip(mapper
, ip
);
2718 if (WARN_ON_ONCE(!pdata
|| !*pdata
))
2723 seq_printf(m
, "%ps:", (void *)ip
);
2725 seq_printf(m
, "%s:%s:%s",
2726 edata
->enable
? ENABLE_EVENT_STR
: DISABLE_EVENT_STR
,
2727 edata
->file
->event_call
->class->system
,
2728 trace_event_name(edata
->file
->event_call
));
2730 if (edata
->count
== -1)
2731 seq_puts(m
, ":unlimited\n");
2733 seq_printf(m
, ":count=%ld\n", edata
->count
);
2739 event_enable_init(struct ftrace_probe_ops
*ops
, struct trace_array
*tr
,
2740 unsigned long ip
, void *init_data
, void **data
)
2742 struct ftrace_func_mapper
*mapper
= *data
;
2743 struct event_probe_data
*edata
= init_data
;
2747 mapper
= allocate_ftrace_func_mapper();
2753 ret
= ftrace_func_mapper_add_ip(mapper
, ip
, edata
);
2762 static int free_probe_data(void *data
)
2764 struct event_probe_data
*edata
= data
;
2768 /* Remove the SOFT_MODE flag */
2769 __ftrace_event_enable_disable(edata
->file
, 0, 1);
2770 module_put(edata
->file
->event_call
->mod
);
2777 event_enable_free(struct ftrace_probe_ops
*ops
, struct trace_array
*tr
,
2778 unsigned long ip
, void *data
)
2780 struct ftrace_func_mapper
*mapper
= data
;
2781 struct event_probe_data
*edata
;
2786 free_ftrace_func_mapper(mapper
, free_probe_data
);
2790 edata
= ftrace_func_mapper_remove_ip(mapper
, ip
);
2792 if (WARN_ON_ONCE(!edata
))
2795 if (WARN_ON_ONCE(edata
->ref
<= 0))
2798 free_probe_data(edata
);
2801 static struct ftrace_probe_ops event_enable_probe_ops
= {
2802 .func
= event_enable_probe
,
2803 .print
= event_enable_print
,
2804 .init
= event_enable_init
,
2805 .free
= event_enable_free
,
2808 static struct ftrace_probe_ops event_enable_count_probe_ops
= {
2809 .func
= event_enable_count_probe
,
2810 .print
= event_enable_print
,
2811 .init
= event_enable_init
,
2812 .free
= event_enable_free
,
2815 static struct ftrace_probe_ops event_disable_probe_ops
= {
2816 .func
= event_enable_probe
,
2817 .print
= event_enable_print
,
2818 .init
= event_enable_init
,
2819 .free
= event_enable_free
,
2822 static struct ftrace_probe_ops event_disable_count_probe_ops
= {
2823 .func
= event_enable_count_probe
,
2824 .print
= event_enable_print
,
2825 .init
= event_enable_init
,
2826 .free
= event_enable_free
,
2830 event_enable_func(struct trace_array
*tr
, struct ftrace_hash
*hash
,
2831 char *glob
, char *cmd
, char *param
, int enabled
)
2833 struct trace_event_file
*file
;
2834 struct ftrace_probe_ops
*ops
;
2835 struct event_probe_data
*data
;
2845 /* hash funcs only work with set_ftrace_filter */
2846 if (!enabled
|| !param
)
2849 system
= strsep(¶m
, ":");
2853 event
= strsep(¶m
, ":");
2855 mutex_lock(&event_mutex
);
2858 file
= find_event_file(tr
, system
, event
);
2862 enable
= strcmp(cmd
, ENABLE_EVENT_STR
) == 0;
2865 ops
= param
? &event_enable_count_probe_ops
: &event_enable_probe_ops
;
2867 ops
= param
? &event_disable_count_probe_ops
: &event_disable_probe_ops
;
2869 if (glob
[0] == '!') {
2870 ret
= unregister_ftrace_function_probe_func(glob
+1, tr
, ops
);
2876 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2880 data
->enable
= enable
;
2887 number
= strsep(¶m
, ":");
2890 if (!strlen(number
))
2894 * We use the callback data field (which is a pointer)
2897 ret
= kstrtoul(number
, 0, &data
->count
);
2902 /* Don't let event modules unload while probe registered */
2903 ret
= try_module_get(file
->event_call
->mod
);
2909 ret
= __ftrace_event_enable_disable(file
, 1, 1);
2913 ret
= register_ftrace_function_probe(glob
, tr
, ops
, data
);
2915 * The above returns on success the # of functions enabled,
2916 * but if it didn't find any functions it returns zero.
2917 * Consider no functions a failure too.
2924 /* Just return zero, not the number of enabled functions */
2927 mutex_unlock(&event_mutex
);
2931 __ftrace_event_enable_disable(file
, 0, 1);
2933 module_put(file
->event_call
->mod
);
2939 static struct ftrace_func_command event_enable_cmd
= {
2940 .name
= ENABLE_EVENT_STR
,
2941 .func
= event_enable_func
,
2944 static struct ftrace_func_command event_disable_cmd
= {
2945 .name
= DISABLE_EVENT_STR
,
2946 .func
= event_enable_func
,
2949 static __init
int register_event_cmds(void)
2953 ret
= register_ftrace_command(&event_enable_cmd
);
2954 if (WARN_ON(ret
< 0))
2956 ret
= register_ftrace_command(&event_disable_cmd
);
2957 if (WARN_ON(ret
< 0))
2958 unregister_ftrace_command(&event_enable_cmd
);
2962 static inline int register_event_cmds(void) { return 0; }
2963 #endif /* CONFIG_DYNAMIC_FTRACE */
2966 * The top level array has already had its trace_event_file
2967 * descriptors created in order to allow for early events to
2968 * be recorded. This function is called after the tracefs has been
2969 * initialized, and we now have to create the files associated
2973 __trace_early_add_event_dirs(struct trace_array
*tr
)
2975 struct trace_event_file
*file
;
2979 list_for_each_entry(file
, &tr
->events
, list
) {
2980 ret
= event_create_dir(tr
->event_dir
, file
);
2982 pr_warn("Could not create directory for event %s\n",
2983 trace_event_name(file
->event_call
));
2988 * For early boot up, the top trace array requires to have
2989 * a list of events that can be enabled. This must be done before
2990 * the filesystem is set up in order to allow events to be traced
2994 __trace_early_add_events(struct trace_array
*tr
)
2996 struct trace_event_call
*call
;
2999 list_for_each_entry(call
, &ftrace_events
, list
) {
3000 /* Early boot up should not have any modules loaded */
3001 if (WARN_ON_ONCE(call
->mod
))
3004 ret
= __trace_early_add_new_event(call
, tr
);
3006 pr_warn("Could not create early event %s\n",
3007 trace_event_name(call
));
3011 /* Remove the event directory structure for a trace directory. */
3013 __trace_remove_event_dirs(struct trace_array
*tr
)
3015 struct trace_event_file
*file
, *next
;
3017 list_for_each_entry_safe(file
, next
, &tr
->events
, list
)
3018 remove_event_file_dir(file
);
3021 static void __add_event_to_tracers(struct trace_event_call
*call
)
3023 struct trace_array
*tr
;
3025 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
)
3026 __trace_add_new_event(call
, tr
);
3029 extern struct trace_event_call
*__start_ftrace_events
[];
3030 extern struct trace_event_call
*__stop_ftrace_events
[];
3032 static char bootup_event_buf
[COMMAND_LINE_SIZE
] __initdata
;
3034 static __init
int setup_trace_event(char *str
)
3036 strlcpy(bootup_event_buf
, str
, COMMAND_LINE_SIZE
);
3037 ring_buffer_expanded
= true;
3038 tracing_selftest_disabled
= true;
3042 __setup("trace_event=", setup_trace_event
);
3044 /* Expects to have event_mutex held when called */
3046 create_event_toplevel_files(struct dentry
*parent
, struct trace_array
*tr
)
3048 struct dentry
*d_events
;
3049 struct dentry
*entry
;
3051 entry
= tracefs_create_file("set_event", 0644, parent
,
3052 tr
, &ftrace_set_event_fops
);
3054 pr_warn("Could not create tracefs 'set_event' entry\n");
3058 d_events
= tracefs_create_dir("events", parent
);
3060 pr_warn("Could not create tracefs 'events' directory\n");
3064 entry
= trace_create_file("enable", 0644, d_events
,
3065 tr
, &ftrace_tr_enable_fops
);
3067 pr_warn("Could not create tracefs 'enable' entry\n");
3071 /* There are not as crucial, just warn if they are not created */
3073 entry
= tracefs_create_file("set_event_pid", 0644, parent
,
3074 tr
, &ftrace_set_event_pid_fops
);
3076 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3078 /* ring buffer internal formats */
3079 entry
= trace_create_file("header_page", 0444, d_events
,
3080 ring_buffer_print_page_header
,
3081 &ftrace_show_header_fops
);
3083 pr_warn("Could not create tracefs 'header_page' entry\n");
3085 entry
= trace_create_file("header_event", 0444, d_events
,
3086 ring_buffer_print_entry_header
,
3087 &ftrace_show_header_fops
);
3089 pr_warn("Could not create tracefs 'header_event' entry\n");
3091 tr
->event_dir
= d_events
;
3097 * event_trace_add_tracer - add a instance of a trace_array to events
3098 * @parent: The parent dentry to place the files/directories for events in
3099 * @tr: The trace array associated with these events
3101 * When a new instance is created, it needs to set up its events
3102 * directory, as well as other files associated with events. It also
3103 * creates the event hierachry in the @parent/events directory.
3105 * Returns 0 on success.
3107 * Must be called with event_mutex held.
3109 int event_trace_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3113 lockdep_assert_held(&event_mutex
);
3115 ret
= create_event_toplevel_files(parent
, tr
);
3119 down_write(&trace_event_sem
);
3120 __trace_add_event_dirs(tr
);
3121 up_write(&trace_event_sem
);
3128 * The top trace array already had its file descriptors created.
3129 * Now the files themselves need to be created.
3132 early_event_add_tracer(struct dentry
*parent
, struct trace_array
*tr
)
3136 mutex_lock(&event_mutex
);
3138 ret
= create_event_toplevel_files(parent
, tr
);
3142 down_write(&trace_event_sem
);
3143 __trace_early_add_event_dirs(tr
);
3144 up_write(&trace_event_sem
);
3147 mutex_unlock(&event_mutex
);
3152 /* Must be called with event_mutex held */
3153 int event_trace_del_tracer(struct trace_array
*tr
)
3155 lockdep_assert_held(&event_mutex
);
3157 /* Disable any event triggers and associated soft-disabled events */
3158 clear_event_triggers(tr
);
3160 /* Clear the pid list */
3161 __ftrace_clear_event_pids(tr
);
3163 /* Disable any running events */
3164 __ftrace_set_clr_event_nolock(tr
, NULL
, NULL
, NULL
, 0);
3166 /* Make sure no more events are being executed */
3167 tracepoint_synchronize_unregister();
3169 down_write(&trace_event_sem
);
3170 __trace_remove_event_dirs(tr
);
3171 tracefs_remove(tr
->event_dir
);
3172 up_write(&trace_event_sem
);
3174 tr
->event_dir
= NULL
;
3179 static __init
int event_trace_memsetup(void)
3181 field_cachep
= KMEM_CACHE(ftrace_event_field
, SLAB_PANIC
);
3182 file_cachep
= KMEM_CACHE(trace_event_file
, SLAB_PANIC
);
3187 early_enable_events(struct trace_array
*tr
, bool disable_first
)
3189 char *buf
= bootup_event_buf
;
3194 token
= strsep(&buf
, ",");
3200 /* Restarting syscalls requires that we stop them first */
3202 ftrace_set_clr_event(tr
, token
, 0);
3204 ret
= ftrace_set_clr_event(tr
, token
, 1);
3206 pr_warn("Failed to enable trace event: %s\n", token
);
3209 /* Put back the comma to allow this to be called again */
3215 static __init
int event_trace_enable(void)
3217 struct trace_array
*tr
= top_trace_array();
3218 struct trace_event_call
**iter
, *call
;
3224 for_each_event(iter
, __start_ftrace_events
, __stop_ftrace_events
) {
3227 ret
= event_init(call
);
3229 list_add(&call
->list
, &ftrace_events
);
3233 * We need the top trace array to have a working set of trace
3234 * points at early init, before the debug files and directories
3235 * are created. Create the file entries now, and attach them
3236 * to the actual file dentries later.
3238 __trace_early_add_events(tr
);
3240 early_enable_events(tr
, false);
3242 trace_printk_start_comm();
3244 register_event_cmds();
3246 register_trigger_cmds();
3252 * event_trace_enable() is called from trace_event_init() first to
3253 * initialize events and perhaps start any events that are on the
3254 * command line. Unfortunately, there are some events that will not
3255 * start this early, like the system call tracepoints that need
3256 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3257 * is called before pid 1 starts, and this flag is never set, making
3258 * the syscall tracepoint never get reached, but the event is enabled
3259 * regardless (and not doing anything).
3261 static __init
int event_trace_enable_again(void)
3263 struct trace_array
*tr
;
3265 tr
= top_trace_array();
3269 early_enable_events(tr
, true);
3274 early_initcall(event_trace_enable_again
);
3276 __init
int event_trace_init(void)
3278 struct trace_array
*tr
;
3279 struct dentry
*d_tracer
;
3280 struct dentry
*entry
;
3283 tr
= top_trace_array();
3287 d_tracer
= tracing_init_dentry();
3288 if (IS_ERR(d_tracer
))
3291 entry
= tracefs_create_file("available_events", 0444, d_tracer
,
3292 tr
, &ftrace_avail_fops
);
3294 pr_warn("Could not create tracefs 'available_events' entry\n");
3296 if (trace_define_generic_fields())
3297 pr_warn("tracing: Failed to allocated generic fields");
3299 if (trace_define_common_fields())
3300 pr_warn("tracing: Failed to allocate common fields");
3302 ret
= early_event_add_tracer(d_tracer
, tr
);
3306 #ifdef CONFIG_MODULES
3307 ret
= register_module_notifier(&trace_module_nb
);
3309 pr_warn("Failed to register trace events module notifier\n");
3314 void __init
trace_event_init(void)
3316 event_trace_memsetup();
3317 init_ftrace_syscalls();
3318 event_trace_enable();
3321 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3323 static DEFINE_SPINLOCK(test_spinlock
);
3324 static DEFINE_SPINLOCK(test_spinlock_irq
);
3325 static DEFINE_MUTEX(test_mutex
);
3327 static __init
void test_work(struct work_struct
*dummy
)
3329 spin_lock(&test_spinlock
);
3330 spin_lock_irq(&test_spinlock_irq
);
3332 spin_unlock_irq(&test_spinlock_irq
);
3333 spin_unlock(&test_spinlock
);
3335 mutex_lock(&test_mutex
);
3337 mutex_unlock(&test_mutex
);
3340 static __init
int event_test_thread(void *unused
)
3344 test_malloc
= kmalloc(1234, GFP_KERNEL
);
3346 pr_info("failed to kmalloc\n");
3348 schedule_on_each_cpu(test_work
);
3352 set_current_state(TASK_INTERRUPTIBLE
);
3353 while (!kthread_should_stop()) {
3355 set_current_state(TASK_INTERRUPTIBLE
);
3357 __set_current_state(TASK_RUNNING
);
3363 * Do various things that may trigger events.
3365 static __init
void event_test_stuff(void)
3367 struct task_struct
*test_thread
;
3369 test_thread
= kthread_run(event_test_thread
, NULL
, "test-events");
3371 kthread_stop(test_thread
);
3375 * For every trace event defined, we will test each trace point separately,
3376 * and then by groups, and finally all trace points.
3378 static __init
void event_trace_self_tests(void)
3380 struct trace_subsystem_dir
*dir
;
3381 struct trace_event_file
*file
;
3382 struct trace_event_call
*call
;
3383 struct event_subsystem
*system
;
3384 struct trace_array
*tr
;
3387 tr
= top_trace_array();
3391 pr_info("Running tests on trace events:\n");
3393 list_for_each_entry(file
, &tr
->events
, list
) {
3395 call
= file
->event_call
;
3397 /* Only test those that have a probe */
3398 if (!call
->class || !call
->class->probe
)
3402 * Testing syscall events here is pretty useless, but
3403 * we still do it if configured. But this is time consuming.
3404 * What we really need is a user thread to perform the
3405 * syscalls as we test.
3407 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3408 if (call
->class->system
&&
3409 strcmp(call
->class->system
, "syscalls") == 0)
3413 pr_info("Testing event %s: ", trace_event_name(call
));
3416 * If an event is already enabled, someone is using
3417 * it and the self test should not be on.
3419 if (file
->flags
& EVENT_FILE_FL_ENABLED
) {
3420 pr_warn("Enabled event during self test!\n");
3425 ftrace_event_enable_disable(file
, 1);
3427 ftrace_event_enable_disable(file
, 0);
3432 /* Now test at the sub system level */
3434 pr_info("Running tests on trace event systems:\n");
3436 list_for_each_entry(dir
, &tr
->systems
, list
) {
3438 system
= dir
->subsystem
;
3440 /* the ftrace system is special, skip it */
3441 if (strcmp(system
->name
, "ftrace") == 0)
3444 pr_info("Testing event system %s: ", system
->name
);
3446 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 1);
3447 if (WARN_ON_ONCE(ret
)) {
3448 pr_warn("error enabling system %s\n",
3455 ret
= __ftrace_set_clr_event(tr
, NULL
, system
->name
, NULL
, 0);
3456 if (WARN_ON_ONCE(ret
)) {
3457 pr_warn("error disabling system %s\n",
3465 /* Test with all events enabled */
3467 pr_info("Running tests on all trace events:\n");
3468 pr_info("Testing all events: ");
3470 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 1);
3471 if (WARN_ON_ONCE(ret
)) {
3472 pr_warn("error enabling all events\n");
3479 ret
= __ftrace_set_clr_event(tr
, NULL
, NULL
, NULL
, 0);
3480 if (WARN_ON_ONCE(ret
)) {
3481 pr_warn("error disabling all events\n");
3488 #ifdef CONFIG_FUNCTION_TRACER
3490 static DEFINE_PER_CPU(atomic_t
, ftrace_test_event_disable
);
3492 static struct trace_event_file event_trace_file __initdata
;
3495 function_test_events_call(unsigned long ip
, unsigned long parent_ip
,
3496 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
3498 struct trace_buffer
*buffer
;
3499 struct ring_buffer_event
*event
;
3500 struct ftrace_entry
*entry
;
3501 unsigned long flags
;
3506 pc
= preempt_count();
3507 preempt_disable_notrace();
3508 cpu
= raw_smp_processor_id();
3509 disabled
= atomic_inc_return(&per_cpu(ftrace_test_event_disable
, cpu
));
3514 local_save_flags(flags
);
3516 event
= trace_event_buffer_lock_reserve(&buffer
, &event_trace_file
,
3517 TRACE_FN
, sizeof(*entry
),
3521 entry
= ring_buffer_event_data(event
);
3523 entry
->parent_ip
= parent_ip
;
3525 event_trigger_unlock_commit(&event_trace_file
, buffer
, event
,
3528 atomic_dec(&per_cpu(ftrace_test_event_disable
, cpu
));
3529 preempt_enable_notrace();
3532 static struct ftrace_ops trace_ops __initdata
=
3534 .func
= function_test_events_call
,
3535 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
3538 static __init
void event_trace_self_test_with_function(void)
3542 event_trace_file
.tr
= top_trace_array();
3543 if (WARN_ON(!event_trace_file
.tr
))
3546 ret
= register_ftrace_function(&trace_ops
);
3547 if (WARN_ON(ret
< 0)) {
3548 pr_info("Failed to enable function tracer for event tests\n");
3551 pr_info("Running tests again, along with the function tracer\n");
3552 event_trace_self_tests();
3553 unregister_ftrace_function(&trace_ops
);
3556 static __init
void event_trace_self_test_with_function(void)
3561 static __init
int event_trace_self_tests_init(void)
3563 if (!tracing_selftest_disabled
) {
3564 event_trace_self_tests();
3565 event_trace_self_test_with_function();
3571 late_initcall(event_trace_self_tests_init
);