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/bsearch.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 field = __find_event_field(&ftrace_generic_fields, name);
104 field = __find_event_field(&ftrace_common_fields, name);
108 head = trace_get_fields(call);
109 return __find_event_field(head, 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_OTHER);
175 __generic_field(char *, comm, FILTER_PTR_STRING);
180 static int trace_define_common_fields(void)
183 struct trace_entry ent;
185 __common_field(unsigned short, type);
186 __common_field(unsigned char, flags);
187 __common_field(unsigned char, preempt_count);
188 __common_field(int, pid);
193 static void trace_destroy_fields(struct trace_event_call *call)
195 struct ftrace_event_field *field, *next;
196 struct list_head *head;
198 head = trace_get_fields(call);
199 list_for_each_entry_safe(field, next, head, link) {
200 list_del(&field->link);
201 kmem_cache_free(field_cachep, field);
205 int trace_event_raw_init(struct trace_event_call *call)
209 id = register_trace_event(&call->event);
215 EXPORT_SYMBOL_GPL(trace_event_raw_init);
217 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
219 struct trace_array *tr = trace_file->tr;
220 struct trace_array_cpu *data;
221 struct trace_pid_list *pid_list;
223 pid_list = rcu_dereference_sched(tr->filtered_pids);
227 data = this_cpu_ptr(tr->trace_buffer.data);
229 return data->ignore_pid;
231 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
233 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
234 struct trace_event_file *trace_file,
237 struct trace_event_call *event_call = trace_file->event_call;
239 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
240 trace_event_ignore_this_pid(trace_file))
243 local_save_flags(fbuffer->flags);
244 fbuffer->pc = preempt_count();
245 fbuffer->trace_file = trace_file;
248 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
249 event_call->event.type, len,
250 fbuffer->flags, fbuffer->pc);
254 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
255 return fbuffer->entry;
257 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
259 static DEFINE_SPINLOCK(tracepoint_iter_lock);
261 static void output_printk(struct trace_event_buffer *fbuffer)
263 struct trace_event_call *event_call;
264 struct trace_event *event;
266 struct trace_iterator *iter = tracepoint_print_iter;
271 event_call = fbuffer->trace_file->event_call;
272 if (!event_call || !event_call->event.funcs ||
273 !event_call->event.funcs->trace)
276 event = &fbuffer->trace_file->event_call->event;
278 spin_lock_irqsave(&tracepoint_iter_lock, flags);
279 trace_seq_init(&iter->seq);
280 iter->ent = fbuffer->entry;
281 event_call->event.funcs->trace(iter, 0, event);
282 trace_seq_putc(&iter->seq, 0);
283 printk("%s", iter->seq.buffer);
285 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
288 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
290 if (tracepoint_printk)
291 output_printk(fbuffer);
293 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
294 fbuffer->event, fbuffer->entry,
295 fbuffer->flags, fbuffer->pc);
297 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
299 int trace_event_reg(struct trace_event_call *call,
300 enum trace_reg type, void *data)
302 struct trace_event_file *file = data;
304 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
306 case TRACE_REG_REGISTER:
307 return tracepoint_probe_register(call->tp,
310 case TRACE_REG_UNREGISTER:
311 tracepoint_probe_unregister(call->tp,
316 #ifdef CONFIG_PERF_EVENTS
317 case TRACE_REG_PERF_REGISTER:
318 return tracepoint_probe_register(call->tp,
319 call->class->perf_probe,
321 case TRACE_REG_PERF_UNREGISTER:
322 tracepoint_probe_unregister(call->tp,
323 call->class->perf_probe,
326 case TRACE_REG_PERF_OPEN:
327 case TRACE_REG_PERF_CLOSE:
328 case TRACE_REG_PERF_ADD:
329 case TRACE_REG_PERF_DEL:
335 EXPORT_SYMBOL_GPL(trace_event_reg);
337 void trace_event_enable_cmd_record(bool enable)
339 struct trace_event_file *file;
340 struct trace_array *tr;
342 mutex_lock(&event_mutex);
343 do_for_each_event_file(tr, file) {
345 if (!(file->flags & EVENT_FILE_FL_ENABLED))
349 tracing_start_cmdline_record();
350 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
352 tracing_stop_cmdline_record();
353 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
355 } while_for_each_event_file();
356 mutex_unlock(&event_mutex);
359 static int __ftrace_event_enable_disable(struct trace_event_file *file,
360 int enable, int soft_disable)
362 struct trace_event_call *call = file->event_call;
363 struct trace_array *tr = file->tr;
370 * When soft_disable is set and enable is cleared, the sm_ref
371 * reference counter is decremented. If it reaches 0, we want
372 * to clear the SOFT_DISABLED flag but leave the event in the
373 * state that it was. That is, if the event was enabled and
374 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
375 * is set we do not want the event to be enabled before we
378 * When soft_disable is not set but the SOFT_MODE flag is,
379 * we do nothing. Do not disable the tracepoint, otherwise
380 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
383 if (atomic_dec_return(&file->sm_ref) > 0)
385 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
386 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
388 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
390 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
391 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
392 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
393 tracing_stop_cmdline_record();
394 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
396 call->class->reg(call, TRACE_REG_UNREGISTER, file);
398 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
399 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
400 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
402 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
406 * When soft_disable is set and enable is set, we want to
407 * register the tracepoint for the event, but leave the event
408 * as is. That means, if the event was already enabled, we do
409 * nothing (but set SOFT_MODE). If the event is disabled, we
410 * set SOFT_DISABLED before enabling the event tracepoint, so
411 * it still seems to be disabled.
414 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
416 if (atomic_inc_return(&file->sm_ref) > 1)
418 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
421 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
423 /* Keep the event disabled, when going to SOFT_MODE. */
425 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
427 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
428 tracing_start_cmdline_record();
429 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
431 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
433 tracing_stop_cmdline_record();
434 pr_info("event trace: Could not enable event "
435 "%s\n", trace_event_name(call));
438 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
440 /* WAS_ENABLED gets set but never cleared. */
441 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
449 int trace_event_enable_disable(struct trace_event_file *file,
450 int enable, int soft_disable)
452 return __ftrace_event_enable_disable(file, enable, soft_disable);
455 static int ftrace_event_enable_disable(struct trace_event_file *file,
458 return __ftrace_event_enable_disable(file, enable, 0);
461 static void ftrace_clear_events(struct trace_array *tr)
463 struct trace_event_file *file;
465 mutex_lock(&event_mutex);
466 list_for_each_entry(file, &tr->events, list) {
467 ftrace_event_enable_disable(file, 0);
469 mutex_unlock(&event_mutex);
472 static int cmp_pid(const void *key, const void *elt)
474 const pid_t *search_pid = key;
475 const pid_t *pid = elt;
477 if (*search_pid == *pid)
479 if (*search_pid < *pid)
485 check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
491 * Return false, because if filtered_pids does not exist,
492 * all pids are good to trace.
497 search_pid = task->pid;
499 pid = bsearch(&search_pid, filtered_pids->pids,
500 filtered_pids->nr_pids, sizeof(pid_t),
509 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
510 struct task_struct *prev, struct task_struct *next)
512 struct trace_array *tr = data;
513 struct trace_pid_list *pid_list;
515 pid_list = rcu_dereference_sched(tr->filtered_pids);
517 this_cpu_write(tr->trace_buffer.data->ignore_pid,
518 check_ignore_pid(pid_list, prev) &&
519 check_ignore_pid(pid_list, next));
523 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
524 struct task_struct *prev, struct task_struct *next)
526 struct trace_array *tr = data;
527 struct trace_pid_list *pid_list;
529 pid_list = rcu_dereference_sched(tr->filtered_pids);
531 this_cpu_write(tr->trace_buffer.data->ignore_pid,
532 check_ignore_pid(pid_list, next));
536 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
538 struct trace_array *tr = data;
539 struct trace_pid_list *pid_list;
541 /* Nothing to do if we are already tracing */
542 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
545 pid_list = rcu_dereference_sched(tr->filtered_pids);
547 this_cpu_write(tr->trace_buffer.data->ignore_pid,
548 check_ignore_pid(pid_list, task));
552 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
554 struct trace_array *tr = data;
555 struct trace_pid_list *pid_list;
557 /* Nothing to do if we are not tracing */
558 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
561 pid_list = rcu_dereference_sched(tr->filtered_pids);
563 /* Set tracing if current is enabled */
564 this_cpu_write(tr->trace_buffer.data->ignore_pid,
565 check_ignore_pid(pid_list, current));
568 static void __ftrace_clear_event_pids(struct trace_array *tr)
570 struct trace_pid_list *pid_list;
571 struct trace_event_file *file;
574 pid_list = rcu_dereference_protected(tr->filtered_pids,
575 lockdep_is_held(&event_mutex));
579 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
580 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
582 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
583 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
585 list_for_each_entry(file, &tr->events, list) {
586 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
589 for_each_possible_cpu(cpu)
590 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
592 rcu_assign_pointer(tr->filtered_pids, NULL);
594 /* Wait till all users are no longer using pid filtering */
597 free_pages((unsigned long)pid_list->pids, pid_list->order);
601 static void ftrace_clear_event_pids(struct trace_array *tr)
603 mutex_lock(&event_mutex);
604 __ftrace_clear_event_pids(tr);
605 mutex_unlock(&event_mutex);
608 static void __put_system(struct event_subsystem *system)
610 struct event_filter *filter = system->filter;
612 WARN_ON_ONCE(system_refcount(system) == 0);
613 if (system_refcount_dec(system))
616 list_del(&system->list);
619 kfree(filter->filter_string);
622 kfree_const(system->name);
626 static void __get_system(struct event_subsystem *system)
628 WARN_ON_ONCE(system_refcount(system) == 0);
629 system_refcount_inc(system);
632 static void __get_system_dir(struct trace_subsystem_dir *dir)
634 WARN_ON_ONCE(dir->ref_count == 0);
636 __get_system(dir->subsystem);
639 static void __put_system_dir(struct trace_subsystem_dir *dir)
641 WARN_ON_ONCE(dir->ref_count == 0);
642 /* If the subsystem is about to be freed, the dir must be too */
643 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
645 __put_system(dir->subsystem);
646 if (!--dir->ref_count)
650 static void put_system(struct trace_subsystem_dir *dir)
652 mutex_lock(&event_mutex);
653 __put_system_dir(dir);
654 mutex_unlock(&event_mutex);
657 static void remove_subsystem(struct trace_subsystem_dir *dir)
662 if (!--dir->nr_events) {
663 tracefs_remove_recursive(dir->entry);
664 list_del(&dir->list);
665 __put_system_dir(dir);
669 static void remove_event_file_dir(struct trace_event_file *file)
671 struct dentry *dir = file->dir;
672 struct dentry *child;
675 spin_lock(&dir->d_lock); /* probably unneeded */
676 list_for_each_entry(child, &dir->d_subdirs, d_child) {
677 if (d_really_is_positive(child)) /* probably unneeded */
678 d_inode(child)->i_private = NULL;
680 spin_unlock(&dir->d_lock);
682 tracefs_remove_recursive(dir);
685 list_del(&file->list);
686 remove_subsystem(file->system);
687 free_event_filter(file->filter);
688 kmem_cache_free(file_cachep, file);
692 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
695 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
696 const char *sub, const char *event, int set)
698 struct trace_event_file *file;
699 struct trace_event_call *call;
703 list_for_each_entry(file, &tr->events, list) {
705 call = file->event_call;
706 name = trace_event_name(call);
708 if (!name || !call->class || !call->class->reg)
711 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
715 strcmp(match, name) != 0 &&
716 strcmp(match, call->class->system) != 0)
719 if (sub && strcmp(sub, call->class->system) != 0)
722 if (event && strcmp(event, name) != 0)
725 ftrace_event_enable_disable(file, set);
733 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
734 const char *sub, const char *event, int set)
738 mutex_lock(&event_mutex);
739 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
740 mutex_unlock(&event_mutex);
745 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
747 char *event = NULL, *sub = NULL, *match;
751 * The buf format can be <subsystem>:<event-name>
752 * *:<event-name> means any event by that name.
753 * :<event-name> is the same.
755 * <subsystem>:* means all events in that subsystem
756 * <subsystem>: means the same.
758 * <name> (no ':') means all events in a subsystem with
759 * the name <name> or any event that matches <name>
762 match = strsep(&buf, ":");
768 if (!strlen(sub) || strcmp(sub, "*") == 0)
770 if (!strlen(event) || strcmp(event, "*") == 0)
774 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
776 /* Put back the colon to allow this to be called again */
784 * trace_set_clr_event - enable or disable an event
785 * @system: system name to match (NULL for any system)
786 * @event: event name to match (NULL for all events, within system)
787 * @set: 1 to enable, 0 to disable
789 * This is a way for other parts of the kernel to enable or disable
792 * Returns 0 on success, -EINVAL if the parameters do not match any
795 int trace_set_clr_event(const char *system, const char *event, int set)
797 struct trace_array *tr = top_trace_array();
802 return __ftrace_set_clr_event(tr, NULL, system, event, set);
804 EXPORT_SYMBOL_GPL(trace_set_clr_event);
806 /* 128 should be much more than enough */
807 #define EVENT_BUF_SIZE 127
810 ftrace_event_write(struct file *file, const char __user *ubuf,
811 size_t cnt, loff_t *ppos)
813 struct trace_parser parser;
814 struct seq_file *m = file->private_data;
815 struct trace_array *tr = m->private;
821 ret = tracing_update_buffers();
825 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
828 read = trace_get_user(&parser, ubuf, cnt, ppos);
830 if (read >= 0 && trace_parser_loaded((&parser))) {
833 if (*parser.buffer == '!')
836 parser.buffer[parser.idx] = 0;
838 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
846 trace_parser_put(&parser);
852 t_next(struct seq_file *m, void *v, loff_t *pos)
854 struct trace_event_file *file = v;
855 struct trace_event_call *call;
856 struct trace_array *tr = m->private;
860 list_for_each_entry_continue(file, &tr->events, list) {
861 call = file->event_call;
863 * The ftrace subsystem is for showing formats only.
864 * They can not be enabled or disabled via the event files.
866 if (call->class && call->class->reg)
873 static void *t_start(struct seq_file *m, loff_t *pos)
875 struct trace_event_file *file;
876 struct trace_array *tr = m->private;
879 mutex_lock(&event_mutex);
881 file = list_entry(&tr->events, struct trace_event_file, list);
882 for (l = 0; l <= *pos; ) {
883 file = t_next(m, file, &l);
891 s_next(struct seq_file *m, void *v, loff_t *pos)
893 struct trace_event_file *file = v;
894 struct trace_array *tr = m->private;
898 list_for_each_entry_continue(file, &tr->events, list) {
899 if (file->flags & EVENT_FILE_FL_ENABLED)
906 static void *s_start(struct seq_file *m, loff_t *pos)
908 struct trace_event_file *file;
909 struct trace_array *tr = m->private;
912 mutex_lock(&event_mutex);
914 file = list_entry(&tr->events, struct trace_event_file, list);
915 for (l = 0; l <= *pos; ) {
916 file = s_next(m, file, &l);
923 static int t_show(struct seq_file *m, void *v)
925 struct trace_event_file *file = v;
926 struct trace_event_call *call = file->event_call;
928 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
929 seq_printf(m, "%s:", call->class->system);
930 seq_printf(m, "%s\n", trace_event_name(call));
935 static void t_stop(struct seq_file *m, void *p)
937 mutex_unlock(&event_mutex);
940 static void *p_start(struct seq_file *m, loff_t *pos)
943 struct trace_pid_list *pid_list;
944 struct trace_array *tr = m->private;
947 * Grab the mutex, to keep calls to p_next() having the same
948 * tr->filtered_pids as p_start() has.
949 * If we just passed the tr->filtered_pids around, then RCU would
950 * have been enough, but doing that makes things more complex.
952 mutex_lock(&event_mutex);
953 rcu_read_lock_sched();
955 pid_list = rcu_dereference_sched(tr->filtered_pids);
957 if (!pid_list || *pos >= pid_list->nr_pids)
960 return (void *)&pid_list->pids[*pos];
963 static void p_stop(struct seq_file *m, void *p)
966 rcu_read_unlock_sched();
967 mutex_unlock(&event_mutex);
971 p_next(struct seq_file *m, void *v, loff_t *pos)
973 struct trace_array *tr = m->private;
974 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
978 if (*pos >= pid_list->nr_pids)
981 return (void *)&pid_list->pids[*pos];
984 static int p_show(struct seq_file *m, void *v)
988 seq_printf(m, "%d\n", *pid);
993 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
996 struct trace_event_file *file;
1000 mutex_lock(&event_mutex);
1001 file = event_file_data(filp);
1003 flags = file->flags;
1004 mutex_unlock(&event_mutex);
1009 if (flags & EVENT_FILE_FL_ENABLED &&
1010 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1013 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1014 flags & EVENT_FILE_FL_SOFT_MODE)
1019 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1023 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1026 struct trace_event_file *file;
1030 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1034 ret = tracing_update_buffers();
1042 mutex_lock(&event_mutex);
1043 file = event_file_data(filp);
1045 ret = ftrace_event_enable_disable(file, val);
1046 mutex_unlock(&event_mutex);
1055 return ret ? ret : cnt;
1059 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1062 const char set_to_char[4] = { '?', '0', '1', 'X' };
1063 struct trace_subsystem_dir *dir = filp->private_data;
1064 struct event_subsystem *system = dir->subsystem;
1065 struct trace_event_call *call;
1066 struct trace_event_file *file;
1067 struct trace_array *tr = dir->tr;
1072 mutex_lock(&event_mutex);
1073 list_for_each_entry(file, &tr->events, list) {
1074 call = file->event_call;
1075 if (!trace_event_name(call) || !call->class || !call->class->reg)
1078 if (system && strcmp(call->class->system, system->name) != 0)
1082 * We need to find out if all the events are set
1083 * or if all events or cleared, or if we have
1086 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1089 * If we have a mixture, no need to look further.
1094 mutex_unlock(&event_mutex);
1096 buf[0] = set_to_char[set];
1099 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1105 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1108 struct trace_subsystem_dir *dir = filp->private_data;
1109 struct event_subsystem *system = dir->subsystem;
1110 const char *name = NULL;
1114 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1118 ret = tracing_update_buffers();
1122 if (val != 0 && val != 1)
1126 * Opening of "enable" adds a ref count to system,
1127 * so the name is safe to use.
1130 name = system->name;
1132 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1146 FORMAT_FIELD_SEPERATOR = 2,
1147 FORMAT_PRINTFMT = 3,
1150 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1152 struct trace_event_call *call = event_file_data(m->private);
1153 struct list_head *common_head = &ftrace_common_fields;
1154 struct list_head *head = trace_get_fields(call);
1155 struct list_head *node = v;
1159 switch ((unsigned long)v) {
1164 case FORMAT_FIELD_SEPERATOR:
1168 case FORMAT_PRINTFMT:
1174 if (node == common_head)
1175 return (void *)FORMAT_FIELD_SEPERATOR;
1176 else if (node == head)
1177 return (void *)FORMAT_PRINTFMT;
1182 static int f_show(struct seq_file *m, void *v)
1184 struct trace_event_call *call = event_file_data(m->private);
1185 struct ftrace_event_field *field;
1186 const char *array_descriptor;
1188 switch ((unsigned long)v) {
1190 seq_printf(m, "name: %s\n", trace_event_name(call));
1191 seq_printf(m, "ID: %d\n", call->event.type);
1192 seq_puts(m, "format:\n");
1195 case FORMAT_FIELD_SEPERATOR:
1199 case FORMAT_PRINTFMT:
1200 seq_printf(m, "\nprint fmt: %s\n",
1205 field = list_entry(v, struct ftrace_event_field, link);
1207 * Smartly shows the array type(except dynamic array).
1210 * If TYPE := TYPE[LEN], it is shown:
1211 * field:TYPE VAR[LEN]
1213 array_descriptor = strchr(field->type, '[');
1215 if (!strncmp(field->type, "__data_loc", 10))
1216 array_descriptor = NULL;
1218 if (!array_descriptor)
1219 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1220 field->type, field->name, field->offset,
1221 field->size, !!field->is_signed);
1223 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1224 (int)(array_descriptor - field->type),
1225 field->type, field->name,
1226 array_descriptor, field->offset,
1227 field->size, !!field->is_signed);
1232 static void *f_start(struct seq_file *m, loff_t *pos)
1234 void *p = (void *)FORMAT_HEADER;
1237 /* ->stop() is called even if ->start() fails */
1238 mutex_lock(&event_mutex);
1239 if (!event_file_data(m->private))
1240 return ERR_PTR(-ENODEV);
1242 while (l < *pos && p)
1243 p = f_next(m, p, &l);
1248 static void f_stop(struct seq_file *m, void *p)
1250 mutex_unlock(&event_mutex);
1253 static const struct seq_operations trace_format_seq_ops = {
1260 static int trace_format_open(struct inode *inode, struct file *file)
1265 ret = seq_open(file, &trace_format_seq_ops);
1269 m = file->private_data;
1276 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1278 int id = (long)event_file_data(filp);
1288 len = sprintf(buf, "%d\n", id);
1290 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1294 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1297 struct trace_event_file *file;
1298 struct trace_seq *s;
1304 s = kmalloc(sizeof(*s), GFP_KERNEL);
1311 mutex_lock(&event_mutex);
1312 file = event_file_data(filp);
1314 print_event_filter(file, s);
1315 mutex_unlock(&event_mutex);
1318 r = simple_read_from_buffer(ubuf, cnt, ppos,
1319 s->buffer, trace_seq_used(s));
1327 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1330 struct trace_event_file *file;
1334 if (cnt >= PAGE_SIZE)
1337 buf = (char *)__get_free_page(GFP_TEMPORARY);
1341 if (copy_from_user(buf, ubuf, cnt)) {
1342 free_page((unsigned long) buf);
1347 mutex_lock(&event_mutex);
1348 file = event_file_data(filp);
1350 err = apply_event_filter(file, buf);
1351 mutex_unlock(&event_mutex);
1353 free_page((unsigned long) buf);
1362 static LIST_HEAD(event_subsystems);
1364 static int subsystem_open(struct inode *inode, struct file *filp)
1366 struct event_subsystem *system = NULL;
1367 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1368 struct trace_array *tr;
1371 if (tracing_is_disabled())
1374 /* Make sure the system still exists */
1375 mutex_lock(&trace_types_lock);
1376 mutex_lock(&event_mutex);
1377 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1378 list_for_each_entry(dir, &tr->systems, list) {
1379 if (dir == inode->i_private) {
1380 /* Don't open systems with no events */
1381 if (dir->nr_events) {
1382 __get_system_dir(dir);
1383 system = dir->subsystem;
1390 mutex_unlock(&event_mutex);
1391 mutex_unlock(&trace_types_lock);
1396 /* Some versions of gcc think dir can be uninitialized here */
1399 /* Still need to increment the ref count of the system */
1400 if (trace_array_get(tr) < 0) {
1405 ret = tracing_open_generic(inode, filp);
1407 trace_array_put(tr);
1414 static int system_tr_open(struct inode *inode, struct file *filp)
1416 struct trace_subsystem_dir *dir;
1417 struct trace_array *tr = inode->i_private;
1420 if (tracing_is_disabled())
1423 if (trace_array_get(tr) < 0)
1426 /* Make a temporary dir that has no system but points to tr */
1427 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1429 trace_array_put(tr);
1435 ret = tracing_open_generic(inode, filp);
1437 trace_array_put(tr);
1442 filp->private_data = dir;
1447 static int subsystem_release(struct inode *inode, struct file *file)
1449 struct trace_subsystem_dir *dir = file->private_data;
1451 trace_array_put(dir->tr);
1454 * If dir->subsystem is NULL, then this is a temporary
1455 * descriptor that was made for a trace_array to enable
1467 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1470 struct trace_subsystem_dir *dir = filp->private_data;
1471 struct event_subsystem *system = dir->subsystem;
1472 struct trace_seq *s;
1478 s = kmalloc(sizeof(*s), GFP_KERNEL);
1484 print_subsystem_event_filter(system, s);
1485 r = simple_read_from_buffer(ubuf, cnt, ppos,
1486 s->buffer, trace_seq_used(s));
1494 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1497 struct trace_subsystem_dir *dir = filp->private_data;
1501 if (cnt >= PAGE_SIZE)
1504 buf = (char *)__get_free_page(GFP_TEMPORARY);
1508 if (copy_from_user(buf, ubuf, cnt)) {
1509 free_page((unsigned long) buf);
1514 err = apply_subsystem_event_filter(dir, buf);
1515 free_page((unsigned long) buf);
1525 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1527 int (*func)(struct trace_seq *s) = filp->private_data;
1528 struct trace_seq *s;
1534 s = kmalloc(sizeof(*s), GFP_KERNEL);
1541 r = simple_read_from_buffer(ubuf, cnt, ppos,
1542 s->buffer, trace_seq_used(s));
1549 static int max_pids(struct trace_pid_list *pid_list)
1551 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1554 static void ignore_task_cpu(void *data)
1556 struct trace_array *tr = data;
1557 struct trace_pid_list *pid_list;
1560 * This function is called by on_each_cpu() while the
1561 * event_mutex is held.
1563 pid_list = rcu_dereference_protected(tr->filtered_pids,
1564 mutex_is_locked(&event_mutex));
1566 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1567 check_ignore_pid(pid_list, current));
1571 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1572 size_t cnt, loff_t *ppos)
1574 struct seq_file *m = filp->private_data;
1575 struct trace_array *tr = m->private;
1576 struct trace_pid_list *filtered_pids = NULL;
1577 struct trace_pid_list *pid_list = NULL;
1578 struct trace_event_file *file;
1579 struct trace_parser parser;
1590 ret = tracing_update_buffers();
1594 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1597 mutex_lock(&event_mutex);
1599 * Load as many pids into the array before doing a
1600 * swap from the tr->filtered_pids to the new list.
1606 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1607 if (ret < 0 || !trace_parser_loaded(&parser))
1614 parser.buffer[parser.idx] = 0;
1617 if (kstrtoul(parser.buffer, 0, &val))
1626 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1630 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1631 lockdep_is_held(&event_mutex));
1633 pid_list->order = filtered_pids->order;
1635 pid_list->order = 0;
1637 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1639 if (!pid_list->pids)
1642 if (filtered_pids) {
1643 pid_list->nr_pids = filtered_pids->nr_pids;
1644 memcpy(pid_list->pids, filtered_pids->pids,
1645 pid_list->nr_pids * sizeof(pid_t));
1647 pid_list->nr_pids = 0;
1650 if (pid_list->nr_pids >= max_pids(pid_list)) {
1653 pid_page = (void *)__get_free_pages(GFP_KERNEL,
1654 pid_list->order + 1);
1657 memcpy(pid_page, pid_list->pids,
1658 pid_list->nr_pids * sizeof(pid_t));
1659 free_pages((unsigned long)pid_list->pids, pid_list->order);
1662 pid_list->pids = pid_page;
1665 pid_list->pids[pid_list->nr_pids++] = pid;
1666 trace_parser_clear(&parser);
1669 trace_parser_put(&parser);
1673 free_pages((unsigned long)pid_list->pids, pid_list->order);
1675 mutex_unlock(&event_mutex);
1680 mutex_unlock(&event_mutex);
1684 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1686 /* Remove duplicates */
1687 for (i = 1; i < pid_list->nr_pids; i++) {
1690 while (i < pid_list->nr_pids &&
1691 pid_list->pids[i - 1] == pid_list->pids[i])
1695 if (i < pid_list->nr_pids) {
1696 memmove(&pid_list->pids[start], &pid_list->pids[i],
1697 (pid_list->nr_pids - i) * sizeof(pid_t));
1698 pid_list->nr_pids -= i - start;
1701 pid_list->nr_pids = start;
1705 rcu_assign_pointer(tr->filtered_pids, pid_list);
1707 list_for_each_entry(file, &tr->events, list) {
1708 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1711 if (filtered_pids) {
1712 synchronize_sched();
1714 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1715 kfree(filtered_pids);
1718 * Register a probe that is called before all other probes
1719 * to set ignore_pid if next or prev do not match.
1720 * Register a probe this is called after all other probes
1721 * to only keep ignore_pid set if next pid matches.
1723 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1725 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1728 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1730 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1735 * Ignoring of pids is done at task switch. But we have to
1736 * check for those tasks that are currently running.
1737 * Always do this in case a pid was appended or removed.
1739 on_each_cpu(ignore_task_cpu, tr, 1);
1741 mutex_unlock(&event_mutex);
1749 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1750 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1751 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1752 static int ftrace_event_release(struct inode *inode, struct file *file);
1754 static const struct seq_operations show_event_seq_ops = {
1761 static const struct seq_operations show_set_event_seq_ops = {
1768 static const struct seq_operations show_set_pid_seq_ops = {
1775 static const struct file_operations ftrace_avail_fops = {
1776 .open = ftrace_event_avail_open,
1778 .llseek = seq_lseek,
1779 .release = seq_release,
1782 static const struct file_operations ftrace_set_event_fops = {
1783 .open = ftrace_event_set_open,
1785 .write = ftrace_event_write,
1786 .llseek = seq_lseek,
1787 .release = ftrace_event_release,
1790 static const struct file_operations ftrace_set_event_pid_fops = {
1791 .open = ftrace_event_set_pid_open,
1793 .write = ftrace_event_pid_write,
1794 .llseek = seq_lseek,
1795 .release = ftrace_event_release,
1798 static const struct file_operations ftrace_enable_fops = {
1799 .open = tracing_open_generic,
1800 .read = event_enable_read,
1801 .write = event_enable_write,
1802 .llseek = default_llseek,
1805 static const struct file_operations ftrace_event_format_fops = {
1806 .open = trace_format_open,
1808 .llseek = seq_lseek,
1809 .release = seq_release,
1812 static const struct file_operations ftrace_event_id_fops = {
1813 .read = event_id_read,
1814 .llseek = default_llseek,
1817 static const struct file_operations ftrace_event_filter_fops = {
1818 .open = tracing_open_generic,
1819 .read = event_filter_read,
1820 .write = event_filter_write,
1821 .llseek = default_llseek,
1824 static const struct file_operations ftrace_subsystem_filter_fops = {
1825 .open = subsystem_open,
1826 .read = subsystem_filter_read,
1827 .write = subsystem_filter_write,
1828 .llseek = default_llseek,
1829 .release = subsystem_release,
1832 static const struct file_operations ftrace_system_enable_fops = {
1833 .open = subsystem_open,
1834 .read = system_enable_read,
1835 .write = system_enable_write,
1836 .llseek = default_llseek,
1837 .release = subsystem_release,
1840 static const struct file_operations ftrace_tr_enable_fops = {
1841 .open = system_tr_open,
1842 .read = system_enable_read,
1843 .write = system_enable_write,
1844 .llseek = default_llseek,
1845 .release = subsystem_release,
1848 static const struct file_operations ftrace_show_header_fops = {
1849 .open = tracing_open_generic,
1850 .read = show_header,
1851 .llseek = default_llseek,
1855 ftrace_event_open(struct inode *inode, struct file *file,
1856 const struct seq_operations *seq_ops)
1861 ret = seq_open(file, seq_ops);
1864 m = file->private_data;
1865 /* copy tr over to seq ops */
1866 m->private = inode->i_private;
1871 static int ftrace_event_release(struct inode *inode, struct file *file)
1873 struct trace_array *tr = inode->i_private;
1875 trace_array_put(tr);
1877 return seq_release(inode, file);
1881 ftrace_event_avail_open(struct inode *inode, struct file *file)
1883 const struct seq_operations *seq_ops = &show_event_seq_ops;
1885 return ftrace_event_open(inode, file, seq_ops);
1889 ftrace_event_set_open(struct inode *inode, struct file *file)
1891 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1892 struct trace_array *tr = inode->i_private;
1895 if (trace_array_get(tr) < 0)
1898 if ((file->f_mode & FMODE_WRITE) &&
1899 (file->f_flags & O_TRUNC))
1900 ftrace_clear_events(tr);
1902 ret = ftrace_event_open(inode, file, seq_ops);
1904 trace_array_put(tr);
1909 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1911 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1912 struct trace_array *tr = inode->i_private;
1915 if (trace_array_get(tr) < 0)
1918 if ((file->f_mode & FMODE_WRITE) &&
1919 (file->f_flags & O_TRUNC))
1920 ftrace_clear_event_pids(tr);
1922 ret = ftrace_event_open(inode, file, seq_ops);
1924 trace_array_put(tr);
1928 static struct event_subsystem *
1929 create_new_subsystem(const char *name)
1931 struct event_subsystem *system;
1933 /* need to create new entry */
1934 system = kmalloc(sizeof(*system), GFP_KERNEL);
1938 system->ref_count = 1;
1940 /* Only allocate if dynamic (kprobes and modules) */
1941 system->name = kstrdup_const(name, GFP_KERNEL);
1945 system->filter = NULL;
1947 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1948 if (!system->filter)
1951 list_add(&system->list, &event_subsystems);
1956 kfree_const(system->name);
1961 static struct dentry *
1962 event_subsystem_dir(struct trace_array *tr, const char *name,
1963 struct trace_event_file *file, struct dentry *parent)
1965 struct trace_subsystem_dir *dir;
1966 struct event_subsystem *system;
1967 struct dentry *entry;
1969 /* First see if we did not already create this dir */
1970 list_for_each_entry(dir, &tr->systems, list) {
1971 system = dir->subsystem;
1972 if (strcmp(system->name, name) == 0) {
1979 /* Now see if the system itself exists. */
1980 list_for_each_entry(system, &event_subsystems, list) {
1981 if (strcmp(system->name, name) == 0)
1984 /* Reset system variable when not found */
1985 if (&system->list == &event_subsystems)
1988 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1993 system = create_new_subsystem(name);
1997 __get_system(system);
1999 dir->entry = tracefs_create_dir(name, parent);
2001 pr_warn("Failed to create system directory %s\n", name);
2002 __put_system(system);
2009 dir->subsystem = system;
2012 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2013 &ftrace_subsystem_filter_fops);
2015 kfree(system->filter);
2016 system->filter = NULL;
2017 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2020 trace_create_file("enable", 0644, dir->entry, dir,
2021 &ftrace_system_enable_fops);
2023 list_add(&dir->list, &tr->systems);
2030 /* Only print this message if failed on memory allocation */
2031 if (!dir || !system)
2032 pr_warn("No memory to create event subsystem %s\n", name);
2037 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2039 struct trace_event_call *call = file->event_call;
2040 struct trace_array *tr = file->tr;
2041 struct list_head *head;
2042 struct dentry *d_events;
2047 * If the trace point header did not define TRACE_SYSTEM
2048 * then the system would be called "TRACE_SYSTEM".
2050 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2051 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2057 name = trace_event_name(call);
2058 file->dir = tracefs_create_dir(name, d_events);
2060 pr_warn("Could not create tracefs '%s' directory\n", name);
2064 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2065 trace_create_file("enable", 0644, file->dir, file,
2066 &ftrace_enable_fops);
2068 #ifdef CONFIG_PERF_EVENTS
2069 if (call->event.type && call->class->reg)
2070 trace_create_file("id", 0444, file->dir,
2071 (void *)(long)call->event.type,
2072 &ftrace_event_id_fops);
2076 * Other events may have the same class. Only update
2077 * the fields if they are not already defined.
2079 head = trace_get_fields(call);
2080 if (list_empty(head)) {
2081 ret = call->class->define_fields(call);
2083 pr_warn("Could not initialize trace point events/%s\n",
2088 trace_create_file("filter", 0644, file->dir, file,
2089 &ftrace_event_filter_fops);
2091 trace_create_file("trigger", 0644, file->dir, file,
2092 &event_trigger_fops);
2094 trace_create_file("format", 0444, file->dir, call,
2095 &ftrace_event_format_fops);
2100 static void remove_event_from_tracers(struct trace_event_call *call)
2102 struct trace_event_file *file;
2103 struct trace_array *tr;
2105 do_for_each_event_file_safe(tr, file) {
2106 if (file->event_call != call)
2109 remove_event_file_dir(file);
2111 * The do_for_each_event_file_safe() is
2112 * a double loop. After finding the call for this
2113 * trace_array, we use break to jump to the next
2117 } while_for_each_event_file();
2120 static void event_remove(struct trace_event_call *call)
2122 struct trace_array *tr;
2123 struct trace_event_file *file;
2125 do_for_each_event_file(tr, file) {
2126 if (file->event_call != call)
2128 ftrace_event_enable_disable(file, 0);
2130 * The do_for_each_event_file() is
2131 * a double loop. After finding the call for this
2132 * trace_array, we use break to jump to the next
2136 } while_for_each_event_file();
2138 if (call->event.funcs)
2139 __unregister_trace_event(&call->event);
2140 remove_event_from_tracers(call);
2141 list_del(&call->list);
2144 static int event_init(struct trace_event_call *call)
2149 name = trace_event_name(call);
2153 if (call->class->raw_init) {
2154 ret = call->class->raw_init(call);
2155 if (ret < 0 && ret != -ENOSYS)
2156 pr_warn("Could not initialize trace events/%s\n", name);
2163 __register_event(struct trace_event_call *call, struct module *mod)
2167 ret = event_init(call);
2171 list_add(&call->list, &ftrace_events);
2177 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2182 /* Find the length of the enum value as a string */
2183 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2184 /* Make sure there's enough room to replace the string with the value */
2188 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2190 /* Get the rest of the string of ptr */
2191 rlen = strlen(ptr + len);
2192 memmove(ptr + elen, ptr + len, rlen);
2193 /* Make sure we end the new string */
2194 ptr[elen + rlen] = 0;
2199 static void update_event_printk(struct trace_event_call *call,
2200 struct trace_enum_map *map)
2204 int len = strlen(map->enum_string);
2206 for (ptr = call->print_fmt; *ptr; ptr++) {
2220 if (isdigit(*ptr)) {
2224 /* Check for alpha chars like ULL */
2225 } while (isalnum(*ptr));
2229 * A number must have some kind of delimiter after
2230 * it, and we can ignore that too.
2234 if (isalpha(*ptr) || *ptr == '_') {
2235 if (strncmp(map->enum_string, ptr, len) == 0 &&
2236 !isalnum(ptr[len]) && ptr[len] != '_') {
2237 ptr = enum_replace(ptr, map, len);
2238 /* Hmm, enum string smaller than value */
2239 if (WARN_ON_ONCE(!ptr))
2242 * No need to decrement here, as enum_replace()
2243 * returns the pointer to the character passed
2244 * the enum, and two enums can not be placed
2245 * back to back without something in between.
2246 * We can skip that something in between.
2253 } while (isalnum(*ptr) || *ptr == '_');
2257 * If what comes after this variable is a '.' or
2258 * '->' then we can continue to ignore that string.
2260 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2261 ptr += *ptr == '.' ? 1 : 2;
2267 * Once again, we can skip the delimiter that came
2275 void trace_event_enum_update(struct trace_enum_map **map, int len)
2277 struct trace_event_call *call, *p;
2278 const char *last_system = NULL;
2282 down_write(&trace_event_sem);
2283 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2284 /* events are usually grouped together with systems */
2285 if (!last_system || call->class->system != last_system) {
2287 last_system = call->class->system;
2290 for (i = last_i; i < len; i++) {
2291 if (call->class->system == map[i]->system) {
2292 /* Save the first system if need be */
2295 update_event_printk(call, map[i]);
2299 up_write(&trace_event_sem);
2302 static struct trace_event_file *
2303 trace_create_new_event(struct trace_event_call *call,
2304 struct trace_array *tr)
2306 struct trace_event_file *file;
2308 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2312 file->event_call = call;
2314 atomic_set(&file->sm_ref, 0);
2315 atomic_set(&file->tm_ref, 0);
2316 INIT_LIST_HEAD(&file->triggers);
2317 list_add(&file->list, &tr->events);
2322 /* Add an event to a trace directory */
2324 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2326 struct trace_event_file *file;
2328 file = trace_create_new_event(call, tr);
2332 return event_create_dir(tr->event_dir, file);
2336 * Just create a decriptor for early init. A descriptor is required
2337 * for enabling events at boot. We want to enable events before
2338 * the filesystem is initialized.
2341 __trace_early_add_new_event(struct trace_event_call *call,
2342 struct trace_array *tr)
2344 struct trace_event_file *file;
2346 file = trace_create_new_event(call, tr);
2353 struct ftrace_module_file_ops;
2354 static void __add_event_to_tracers(struct trace_event_call *call);
2356 /* Add an additional event_call dynamically */
2357 int trace_add_event_call(struct trace_event_call *call)
2360 mutex_lock(&trace_types_lock);
2361 mutex_lock(&event_mutex);
2363 ret = __register_event(call, NULL);
2365 __add_event_to_tracers(call);
2367 mutex_unlock(&event_mutex);
2368 mutex_unlock(&trace_types_lock);
2373 * Must be called under locking of trace_types_lock, event_mutex and
2376 static void __trace_remove_event_call(struct trace_event_call *call)
2379 trace_destroy_fields(call);
2380 free_event_filter(call->filter);
2381 call->filter = NULL;
2384 static int probe_remove_event_call(struct trace_event_call *call)
2386 struct trace_array *tr;
2387 struct trace_event_file *file;
2389 #ifdef CONFIG_PERF_EVENTS
2390 if (call->perf_refcount)
2393 do_for_each_event_file(tr, file) {
2394 if (file->event_call != call)
2397 * We can't rely on ftrace_event_enable_disable(enable => 0)
2398 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2399 * TRACE_REG_UNREGISTER.
2401 if (file->flags & EVENT_FILE_FL_ENABLED)
2404 * The do_for_each_event_file_safe() is
2405 * a double loop. After finding the call for this
2406 * trace_array, we use break to jump to the next
2410 } while_for_each_event_file();
2412 __trace_remove_event_call(call);
2417 /* Remove an event_call */
2418 int trace_remove_event_call(struct trace_event_call *call)
2422 mutex_lock(&trace_types_lock);
2423 mutex_lock(&event_mutex);
2424 down_write(&trace_event_sem);
2425 ret = probe_remove_event_call(call);
2426 up_write(&trace_event_sem);
2427 mutex_unlock(&event_mutex);
2428 mutex_unlock(&trace_types_lock);
2433 #define for_each_event(event, start, end) \
2434 for (event = start; \
2435 (unsigned long)event < (unsigned long)end; \
2438 #ifdef CONFIG_MODULES
2440 static void trace_module_add_events(struct module *mod)
2442 struct trace_event_call **call, **start, **end;
2444 if (!mod->num_trace_events)
2447 /* Don't add infrastructure for mods without tracepoints */
2448 if (trace_module_has_bad_taint(mod)) {
2449 pr_err("%s: module has bad taint, not creating trace events\n",
2454 start = mod->trace_events;
2455 end = mod->trace_events + mod->num_trace_events;
2457 for_each_event(call, start, end) {
2458 __register_event(*call, mod);
2459 __add_event_to_tracers(*call);
2463 static void trace_module_remove_events(struct module *mod)
2465 struct trace_event_call *call, *p;
2466 bool clear_trace = false;
2468 down_write(&trace_event_sem);
2469 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2470 if (call->mod == mod) {
2471 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2473 __trace_remove_event_call(call);
2476 up_write(&trace_event_sem);
2479 * It is safest to reset the ring buffer if the module being unloaded
2480 * registered any events that were used. The only worry is if
2481 * a new module gets loaded, and takes on the same id as the events
2482 * of this module. When printing out the buffer, traced events left
2483 * over from this module may be passed to the new module events and
2484 * unexpected results may occur.
2487 tracing_reset_all_online_cpus();
2490 static int trace_module_notify(struct notifier_block *self,
2491 unsigned long val, void *data)
2493 struct module *mod = data;
2495 mutex_lock(&trace_types_lock);
2496 mutex_lock(&event_mutex);
2498 case MODULE_STATE_COMING:
2499 trace_module_add_events(mod);
2501 case MODULE_STATE_GOING:
2502 trace_module_remove_events(mod);
2505 mutex_unlock(&event_mutex);
2506 mutex_unlock(&trace_types_lock);
2511 static struct notifier_block trace_module_nb = {
2512 .notifier_call = trace_module_notify,
2513 .priority = 1, /* higher than trace.c module notify */
2515 #endif /* CONFIG_MODULES */
2517 /* Create a new event directory structure for a trace directory. */
2519 __trace_add_event_dirs(struct trace_array *tr)
2521 struct trace_event_call *call;
2524 list_for_each_entry(call, &ftrace_events, list) {
2525 ret = __trace_add_new_event(call, tr);
2527 pr_warn("Could not create directory for event %s\n",
2528 trace_event_name(call));
2532 struct trace_event_file *
2533 find_event_file(struct trace_array *tr, const char *system, const char *event)
2535 struct trace_event_file *file;
2536 struct trace_event_call *call;
2539 list_for_each_entry(file, &tr->events, list) {
2541 call = file->event_call;
2542 name = trace_event_name(call);
2544 if (!name || !call->class || !call->class->reg)
2547 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2550 if (strcmp(event, name) == 0 &&
2551 strcmp(system, call->class->system) == 0)
2557 #ifdef CONFIG_DYNAMIC_FTRACE
2560 #define ENABLE_EVENT_STR "enable_event"
2561 #define DISABLE_EVENT_STR "disable_event"
2563 struct event_probe_data {
2564 struct trace_event_file *file;
2565 unsigned long count;
2571 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2573 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2574 struct event_probe_data *data = *pdata;
2580 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2582 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2586 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2588 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2589 struct event_probe_data *data = *pdata;
2597 /* Skip if the event is in a state we want to switch to */
2598 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2601 if (data->count != -1)
2604 event_enable_probe(ip, parent_ip, _data);
2608 event_enable_print(struct seq_file *m, unsigned long ip,
2609 struct ftrace_probe_ops *ops, void *_data)
2611 struct event_probe_data *data = _data;
2613 seq_printf(m, "%ps:", (void *)ip);
2615 seq_printf(m, "%s:%s:%s",
2616 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2617 data->file->event_call->class->system,
2618 trace_event_name(data->file->event_call));
2620 if (data->count == -1)
2621 seq_puts(m, ":unlimited\n");
2623 seq_printf(m, ":count=%ld\n", data->count);
2629 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2632 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2633 struct event_probe_data *data = *pdata;
2640 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2643 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2644 struct event_probe_data *data = *pdata;
2646 if (WARN_ON_ONCE(data->ref <= 0))
2651 /* Remove the SOFT_MODE flag */
2652 __ftrace_event_enable_disable(data->file, 0, 1);
2653 module_put(data->file->event_call->mod);
2659 static struct ftrace_probe_ops event_enable_probe_ops = {
2660 .func = event_enable_probe,
2661 .print = event_enable_print,
2662 .init = event_enable_init,
2663 .free = event_enable_free,
2666 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2667 .func = event_enable_count_probe,
2668 .print = event_enable_print,
2669 .init = event_enable_init,
2670 .free = event_enable_free,
2673 static struct ftrace_probe_ops event_disable_probe_ops = {
2674 .func = event_enable_probe,
2675 .print = event_enable_print,
2676 .init = event_enable_init,
2677 .free = event_enable_free,
2680 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2681 .func = event_enable_count_probe,
2682 .print = event_enable_print,
2683 .init = event_enable_init,
2684 .free = event_enable_free,
2688 event_enable_func(struct ftrace_hash *hash,
2689 char *glob, char *cmd, char *param, int enabled)
2691 struct trace_array *tr = top_trace_array();
2692 struct trace_event_file *file;
2693 struct ftrace_probe_ops *ops;
2694 struct event_probe_data *data;
2704 /* hash funcs only work with set_ftrace_filter */
2705 if (!enabled || !param)
2708 system = strsep(¶m, ":");
2712 event = strsep(¶m, ":");
2714 mutex_lock(&event_mutex);
2717 file = find_event_file(tr, system, event);
2721 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2724 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2726 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2728 if (glob[0] == '!') {
2729 unregister_ftrace_function_probe_func(glob+1, ops);
2735 data = kzalloc(sizeof(*data), GFP_KERNEL);
2739 data->enable = enable;
2746 number = strsep(¶m, ":");
2749 if (!strlen(number))
2753 * We use the callback data field (which is a pointer)
2756 ret = kstrtoul(number, 0, &data->count);
2761 /* Don't let event modules unload while probe registered */
2762 ret = try_module_get(file->event_call->mod);
2768 ret = __ftrace_event_enable_disable(file, 1, 1);
2771 ret = register_ftrace_function_probe(glob, ops, data);
2773 * The above returns on success the # of functions enabled,
2774 * but if it didn't find any functions it returns zero.
2775 * Consider no functions a failure too.
2782 /* Just return zero, not the number of enabled functions */
2785 mutex_unlock(&event_mutex);
2789 __ftrace_event_enable_disable(file, 0, 1);
2791 module_put(file->event_call->mod);
2797 static struct ftrace_func_command event_enable_cmd = {
2798 .name = ENABLE_EVENT_STR,
2799 .func = event_enable_func,
2802 static struct ftrace_func_command event_disable_cmd = {
2803 .name = DISABLE_EVENT_STR,
2804 .func = event_enable_func,
2807 static __init int register_event_cmds(void)
2811 ret = register_ftrace_command(&event_enable_cmd);
2812 if (WARN_ON(ret < 0))
2814 ret = register_ftrace_command(&event_disable_cmd);
2815 if (WARN_ON(ret < 0))
2816 unregister_ftrace_command(&event_enable_cmd);
2820 static inline int register_event_cmds(void) { return 0; }
2821 #endif /* CONFIG_DYNAMIC_FTRACE */
2824 * The top level array has already had its trace_event_file
2825 * descriptors created in order to allow for early events to
2826 * be recorded. This function is called after the tracefs has been
2827 * initialized, and we now have to create the files associated
2831 __trace_early_add_event_dirs(struct trace_array *tr)
2833 struct trace_event_file *file;
2837 list_for_each_entry(file, &tr->events, list) {
2838 ret = event_create_dir(tr->event_dir, file);
2840 pr_warn("Could not create directory for event %s\n",
2841 trace_event_name(file->event_call));
2846 * For early boot up, the top trace array requires to have
2847 * a list of events that can be enabled. This must be done before
2848 * the filesystem is set up in order to allow events to be traced
2852 __trace_early_add_events(struct trace_array *tr)
2854 struct trace_event_call *call;
2857 list_for_each_entry(call, &ftrace_events, list) {
2858 /* Early boot up should not have any modules loaded */
2859 if (WARN_ON_ONCE(call->mod))
2862 ret = __trace_early_add_new_event(call, tr);
2864 pr_warn("Could not create early event %s\n",
2865 trace_event_name(call));
2869 /* Remove the event directory structure for a trace directory. */
2871 __trace_remove_event_dirs(struct trace_array *tr)
2873 struct trace_event_file *file, *next;
2875 list_for_each_entry_safe(file, next, &tr->events, list)
2876 remove_event_file_dir(file);
2879 static void __add_event_to_tracers(struct trace_event_call *call)
2881 struct trace_array *tr;
2883 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2884 __trace_add_new_event(call, tr);
2887 extern struct trace_event_call *__start_ftrace_events[];
2888 extern struct trace_event_call *__stop_ftrace_events[];
2890 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2892 static __init int setup_trace_event(char *str)
2894 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2895 ring_buffer_expanded = true;
2896 tracing_selftest_disabled = true;
2900 __setup("trace_event=", setup_trace_event);
2902 /* Expects to have event_mutex held when called */
2904 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2906 struct dentry *d_events;
2907 struct dentry *entry;
2909 entry = tracefs_create_file("set_event", 0644, parent,
2910 tr, &ftrace_set_event_fops);
2912 pr_warn("Could not create tracefs 'set_event' entry\n");
2916 d_events = tracefs_create_dir("events", parent);
2918 pr_warn("Could not create tracefs 'events' directory\n");
2922 entry = tracefs_create_file("set_event_pid", 0644, parent,
2923 tr, &ftrace_set_event_pid_fops);
2925 /* ring buffer internal formats */
2926 trace_create_file("header_page", 0444, d_events,
2927 ring_buffer_print_page_header,
2928 &ftrace_show_header_fops);
2930 trace_create_file("header_event", 0444, d_events,
2931 ring_buffer_print_entry_header,
2932 &ftrace_show_header_fops);
2934 trace_create_file("enable", 0644, d_events,
2935 tr, &ftrace_tr_enable_fops);
2937 tr->event_dir = d_events;
2943 * event_trace_add_tracer - add a instance of a trace_array to events
2944 * @parent: The parent dentry to place the files/directories for events in
2945 * @tr: The trace array associated with these events
2947 * When a new instance is created, it needs to set up its events
2948 * directory, as well as other files associated with events. It also
2949 * creates the event hierachry in the @parent/events directory.
2951 * Returns 0 on success.
2953 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2957 mutex_lock(&event_mutex);
2959 ret = create_event_toplevel_files(parent, tr);
2963 down_write(&trace_event_sem);
2964 __trace_add_event_dirs(tr);
2965 up_write(&trace_event_sem);
2968 mutex_unlock(&event_mutex);
2974 * The top trace array already had its file descriptors created.
2975 * Now the files themselves need to be created.
2978 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2982 mutex_lock(&event_mutex);
2984 ret = create_event_toplevel_files(parent, tr);
2988 down_write(&trace_event_sem);
2989 __trace_early_add_event_dirs(tr);
2990 up_write(&trace_event_sem);
2993 mutex_unlock(&event_mutex);
2998 int event_trace_del_tracer(struct trace_array *tr)
3000 mutex_lock(&event_mutex);
3002 /* Disable any event triggers and associated soft-disabled events */
3003 clear_event_triggers(tr);
3005 /* Clear the pid list */
3006 __ftrace_clear_event_pids(tr);
3008 /* Disable any running events */
3009 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3011 /* Access to events are within rcu_read_lock_sched() */
3012 synchronize_sched();
3014 down_write(&trace_event_sem);
3015 __trace_remove_event_dirs(tr);
3016 tracefs_remove_recursive(tr->event_dir);
3017 up_write(&trace_event_sem);
3019 tr->event_dir = NULL;
3021 mutex_unlock(&event_mutex);
3026 static __init int event_trace_memsetup(void)
3028 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3029 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3034 early_enable_events(struct trace_array *tr, bool disable_first)
3036 char *buf = bootup_event_buf;
3041 token = strsep(&buf, ",");
3047 /* Restarting syscalls requires that we stop them first */
3049 ftrace_set_clr_event(tr, token, 0);
3051 ret = ftrace_set_clr_event(tr, token, 1);
3053 pr_warn("Failed to enable trace event: %s\n", token);
3056 /* Put back the comma to allow this to be called again */
3062 static __init int event_trace_enable(void)
3064 struct trace_array *tr = top_trace_array();
3065 struct trace_event_call **iter, *call;
3071 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3074 ret = event_init(call);
3076 list_add(&call->list, &ftrace_events);
3080 * We need the top trace array to have a working set of trace
3081 * points at early init, before the debug files and directories
3082 * are created. Create the file entries now, and attach them
3083 * to the actual file dentries later.
3085 __trace_early_add_events(tr);
3087 early_enable_events(tr, false);
3089 trace_printk_start_comm();
3091 register_event_cmds();
3093 register_trigger_cmds();
3099 * event_trace_enable() is called from trace_event_init() first to
3100 * initialize events and perhaps start any events that are on the
3101 * command line. Unfortunately, there are some events that will not
3102 * start this early, like the system call tracepoints that need
3103 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3104 * is called before pid 1 starts, and this flag is never set, making
3105 * the syscall tracepoint never get reached, but the event is enabled
3106 * regardless (and not doing anything).
3108 static __init int event_trace_enable_again(void)
3110 struct trace_array *tr;
3112 tr = top_trace_array();
3116 early_enable_events(tr, true);
3121 early_initcall(event_trace_enable_again);
3123 static __init int event_trace_init(void)
3125 struct trace_array *tr;
3126 struct dentry *d_tracer;
3127 struct dentry *entry;
3130 tr = top_trace_array();
3134 d_tracer = tracing_init_dentry();
3135 if (IS_ERR(d_tracer))
3138 entry = tracefs_create_file("available_events", 0444, d_tracer,
3139 tr, &ftrace_avail_fops);
3141 pr_warn("Could not create tracefs 'available_events' entry\n");
3143 if (trace_define_generic_fields())
3144 pr_warn("tracing: Failed to allocated generic fields");
3146 if (trace_define_common_fields())
3147 pr_warn("tracing: Failed to allocate common fields");
3149 ret = early_event_add_tracer(d_tracer, tr);
3153 #ifdef CONFIG_MODULES
3154 ret = register_module_notifier(&trace_module_nb);
3156 pr_warn("Failed to register trace events module notifier\n");
3161 void __init trace_event_init(void)
3163 event_trace_memsetup();
3164 init_ftrace_syscalls();
3165 event_trace_enable();
3168 fs_initcall(event_trace_init);
3170 #ifdef CONFIG_FTRACE_STARTUP_TEST
3172 static DEFINE_SPINLOCK(test_spinlock);
3173 static DEFINE_SPINLOCK(test_spinlock_irq);
3174 static DEFINE_MUTEX(test_mutex);
3176 static __init void test_work(struct work_struct *dummy)
3178 spin_lock(&test_spinlock);
3179 spin_lock_irq(&test_spinlock_irq);
3181 spin_unlock_irq(&test_spinlock_irq);
3182 spin_unlock(&test_spinlock);
3184 mutex_lock(&test_mutex);
3186 mutex_unlock(&test_mutex);
3189 static __init int event_test_thread(void *unused)
3193 test_malloc = kmalloc(1234, GFP_KERNEL);
3195 pr_info("failed to kmalloc\n");
3197 schedule_on_each_cpu(test_work);
3201 set_current_state(TASK_INTERRUPTIBLE);
3202 while (!kthread_should_stop()) {
3204 set_current_state(TASK_INTERRUPTIBLE);
3206 __set_current_state(TASK_RUNNING);
3212 * Do various things that may trigger events.
3214 static __init void event_test_stuff(void)
3216 struct task_struct *test_thread;
3218 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3220 kthread_stop(test_thread);
3224 * For every trace event defined, we will test each trace point separately,
3225 * and then by groups, and finally all trace points.
3227 static __init void event_trace_self_tests(void)
3229 struct trace_subsystem_dir *dir;
3230 struct trace_event_file *file;
3231 struct trace_event_call *call;
3232 struct event_subsystem *system;
3233 struct trace_array *tr;
3236 tr = top_trace_array();
3240 pr_info("Running tests on trace events:\n");
3242 list_for_each_entry(file, &tr->events, list) {
3244 call = file->event_call;
3246 /* Only test those that have a probe */
3247 if (!call->class || !call->class->probe)
3251 * Testing syscall events here is pretty useless, but
3252 * we still do it if configured. But this is time consuming.
3253 * What we really need is a user thread to perform the
3254 * syscalls as we test.
3256 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3257 if (call->class->system &&
3258 strcmp(call->class->system, "syscalls") == 0)
3262 pr_info("Testing event %s: ", trace_event_name(call));
3265 * If an event is already enabled, someone is using
3266 * it and the self test should not be on.
3268 if (file->flags & EVENT_FILE_FL_ENABLED) {
3269 pr_warn("Enabled event during self test!\n");
3274 ftrace_event_enable_disable(file, 1);
3276 ftrace_event_enable_disable(file, 0);
3281 /* Now test at the sub system level */
3283 pr_info("Running tests on trace event systems:\n");
3285 list_for_each_entry(dir, &tr->systems, list) {
3287 system = dir->subsystem;
3289 /* the ftrace system is special, skip it */
3290 if (strcmp(system->name, "ftrace") == 0)
3293 pr_info("Testing event system %s: ", system->name);
3295 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3296 if (WARN_ON_ONCE(ret)) {
3297 pr_warn("error enabling system %s\n",
3304 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3305 if (WARN_ON_ONCE(ret)) {
3306 pr_warn("error disabling system %s\n",
3314 /* Test with all events enabled */
3316 pr_info("Running tests on all trace events:\n");
3317 pr_info("Testing all events: ");
3319 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3320 if (WARN_ON_ONCE(ret)) {
3321 pr_warn("error enabling all events\n");
3328 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3329 if (WARN_ON_ONCE(ret)) {
3330 pr_warn("error disabling all events\n");
3337 #ifdef CONFIG_FUNCTION_TRACER
3339 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3341 static struct trace_array *event_tr;
3344 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3345 struct ftrace_ops *op, struct pt_regs *pt_regs)
3347 struct ring_buffer_event *event;
3348 struct ring_buffer *buffer;
3349 struct ftrace_entry *entry;
3350 unsigned long flags;
3355 pc = preempt_count();
3356 preempt_disable_notrace();
3357 cpu = raw_smp_processor_id();
3358 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3363 local_save_flags(flags);
3365 event = trace_current_buffer_lock_reserve(&buffer,
3366 TRACE_FN, sizeof(*entry),
3370 entry = ring_buffer_event_data(event);
3372 entry->parent_ip = parent_ip;
3374 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
3377 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3378 preempt_enable_notrace();
3381 static struct ftrace_ops trace_ops __initdata =
3383 .func = function_test_events_call,
3384 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3387 static __init void event_trace_self_test_with_function(void)
3390 event_tr = top_trace_array();
3391 if (WARN_ON(!event_tr))
3393 ret = register_ftrace_function(&trace_ops);
3394 if (WARN_ON(ret < 0)) {
3395 pr_info("Failed to enable function tracer for event tests\n");
3398 pr_info("Running tests again, along with the function tracer\n");
3399 event_trace_self_tests();
3400 unregister_ftrace_function(&trace_ops);
3403 static __init void event_trace_self_test_with_function(void)
3408 static __init int event_trace_self_tests_init(void)
3410 if (!tracing_selftest_disabled) {
3411 event_trace_self_tests();
3412 event_trace_self_test_with_function();
3418 late_initcall(event_trace_self_tests_init);