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 head = trace_get_fields(call);
101 field = __find_event_field(head, name);
105 field = __find_event_field(&ftrace_generic_fields, name);
109 return __find_event_field(&ftrace_common_fields, name);
112 static int __trace_define_field(struct list_head *head, const char *type,
113 const char *name, int offset, int size,
114 int is_signed, int filter_type)
116 struct ftrace_event_field *field;
118 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
125 if (filter_type == FILTER_OTHER)
126 field->filter_type = filter_assign_type(type);
128 field->filter_type = filter_type;
130 field->offset = offset;
132 field->is_signed = is_signed;
134 list_add(&field->link, head);
139 int trace_define_field(struct trace_event_call *call, const char *type,
140 const char *name, int offset, int size, int is_signed,
143 struct list_head *head;
145 if (WARN_ON(!call->class))
148 head = trace_get_fields(call);
149 return __trace_define_field(head, type, name, offset, size,
150 is_signed, filter_type);
152 EXPORT_SYMBOL_GPL(trace_define_field);
154 #define __generic_field(type, item, filter_type) \
155 ret = __trace_define_field(&ftrace_generic_fields, #type, \
156 #item, 0, 0, is_signed_type(type), \
161 #define __common_field(type, item) \
162 ret = __trace_define_field(&ftrace_common_fields, #type, \
164 offsetof(typeof(ent), item), \
166 is_signed_type(type), FILTER_OTHER); \
170 static int trace_define_generic_fields(void)
174 __generic_field(int, CPU, FILTER_CPU);
175 __generic_field(int, cpu, FILTER_CPU);
176 __generic_field(char *, COMM, FILTER_COMM);
177 __generic_field(char *, comm, FILTER_COMM);
182 static int trace_define_common_fields(void)
185 struct trace_entry ent;
187 __common_field(unsigned short, type);
188 __common_field(unsigned char, flags);
189 __common_field(unsigned char, preempt_count);
190 __common_field(int, pid);
191 __common_field(unsigned short, migrate_disable);
192 __common_field(unsigned short, padding);
197 static void trace_destroy_fields(struct trace_event_call *call)
199 struct ftrace_event_field *field, *next;
200 struct list_head *head;
202 head = trace_get_fields(call);
203 list_for_each_entry_safe(field, next, head, link) {
204 list_del(&field->link);
205 kmem_cache_free(field_cachep, field);
209 int trace_event_raw_init(struct trace_event_call *call)
213 id = register_trace_event(&call->event);
219 EXPORT_SYMBOL_GPL(trace_event_raw_init);
221 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
223 struct trace_array *tr = trace_file->tr;
224 struct trace_array_cpu *data;
225 struct trace_pid_list *pid_list;
227 pid_list = rcu_dereference_sched(tr->filtered_pids);
231 data = this_cpu_ptr(tr->trace_buffer.data);
233 return data->ignore_pid;
235 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
237 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
238 struct trace_event_file *trace_file,
241 struct trace_event_call *event_call = trace_file->event_call;
243 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
244 trace_event_ignore_this_pid(trace_file))
247 local_save_flags(fbuffer->flags);
248 fbuffer->pc = preempt_count();
250 * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
251 * preemption (adding one to the preempt_count). Since we are
252 * interested in the preempt_count at the time the tracepoint was
253 * hit, we need to subtract one to offset the increment.
255 if (IS_ENABLED(CONFIG_PREEMPT))
257 fbuffer->trace_file = trace_file;
260 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
261 event_call->event.type, len,
262 fbuffer->flags, fbuffer->pc);
266 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
267 return fbuffer->entry;
269 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
271 static DEFINE_SPINLOCK(tracepoint_iter_lock);
273 static void output_printk(struct trace_event_buffer *fbuffer)
275 struct trace_event_call *event_call;
276 struct trace_event *event;
278 struct trace_iterator *iter = tracepoint_print_iter;
283 event_call = fbuffer->trace_file->event_call;
284 if (!event_call || !event_call->event.funcs ||
285 !event_call->event.funcs->trace)
288 event = &fbuffer->trace_file->event_call->event;
290 spin_lock_irqsave(&tracepoint_iter_lock, flags);
291 trace_seq_init(&iter->seq);
292 iter->ent = fbuffer->entry;
293 event_call->event.funcs->trace(iter, 0, event);
294 trace_seq_putc(&iter->seq, 0);
295 printk("%s", iter->seq.buffer);
297 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
300 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
302 if (tracepoint_printk)
303 output_printk(fbuffer);
305 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
306 fbuffer->event, fbuffer->entry,
307 fbuffer->flags, fbuffer->pc);
309 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
311 int trace_event_reg(struct trace_event_call *call,
312 enum trace_reg type, void *data)
314 struct trace_event_file *file = data;
316 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
318 case TRACE_REG_REGISTER:
319 return tracepoint_probe_register(call->tp,
322 case TRACE_REG_UNREGISTER:
323 tracepoint_probe_unregister(call->tp,
328 #ifdef CONFIG_PERF_EVENTS
329 case TRACE_REG_PERF_REGISTER:
330 return tracepoint_probe_register(call->tp,
331 call->class->perf_probe,
333 case TRACE_REG_PERF_UNREGISTER:
334 tracepoint_probe_unregister(call->tp,
335 call->class->perf_probe,
338 case TRACE_REG_PERF_OPEN:
339 case TRACE_REG_PERF_CLOSE:
340 case TRACE_REG_PERF_ADD:
341 case TRACE_REG_PERF_DEL:
347 EXPORT_SYMBOL_GPL(trace_event_reg);
349 void trace_event_enable_cmd_record(bool enable)
351 struct trace_event_file *file;
352 struct trace_array *tr;
354 mutex_lock(&event_mutex);
355 do_for_each_event_file(tr, file) {
357 if (!(file->flags & EVENT_FILE_FL_ENABLED))
361 tracing_start_cmdline_record();
362 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
364 tracing_stop_cmdline_record();
365 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
367 } while_for_each_event_file();
368 mutex_unlock(&event_mutex);
371 static int __ftrace_event_enable_disable(struct trace_event_file *file,
372 int enable, int soft_disable)
374 struct trace_event_call *call = file->event_call;
375 struct trace_array *tr = file->tr;
382 * When soft_disable is set and enable is cleared, the sm_ref
383 * reference counter is decremented. If it reaches 0, we want
384 * to clear the SOFT_DISABLED flag but leave the event in the
385 * state that it was. That is, if the event was enabled and
386 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
387 * is set we do not want the event to be enabled before we
390 * When soft_disable is not set but the SOFT_MODE flag is,
391 * we do nothing. Do not disable the tracepoint, otherwise
392 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
395 if (atomic_dec_return(&file->sm_ref) > 0)
397 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
398 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
400 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
402 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
403 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
404 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
405 tracing_stop_cmdline_record();
406 clear_bit(EVENT_FILE_FL_RECORDED_CMD_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)) {
435 /* Keep the event disabled, when going to SOFT_MODE. */
437 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
439 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
440 tracing_start_cmdline_record();
441 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
443 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
445 tracing_stop_cmdline_record();
446 pr_info("event trace: Could not enable event "
447 "%s\n", trace_event_name(call));
450 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
452 /* WAS_ENABLED gets set but never cleared. */
453 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
461 int trace_event_enable_disable(struct trace_event_file *file,
462 int enable, int soft_disable)
464 return __ftrace_event_enable_disable(file, enable, soft_disable);
467 static int ftrace_event_enable_disable(struct trace_event_file *file,
470 return __ftrace_event_enable_disable(file, enable, 0);
473 static void ftrace_clear_events(struct trace_array *tr)
475 struct trace_event_file *file;
477 mutex_lock(&event_mutex);
478 list_for_each_entry(file, &tr->events, list) {
479 ftrace_event_enable_disable(file, 0);
481 mutex_unlock(&event_mutex);
484 static int cmp_pid(const void *key, const void *elt)
486 const pid_t *search_pid = key;
487 const pid_t *pid = elt;
489 if (*search_pid == *pid)
491 if (*search_pid < *pid)
497 check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
503 * Return false, because if filtered_pids does not exist,
504 * all pids are good to trace.
509 search_pid = task->pid;
511 pid = bsearch(&search_pid, filtered_pids->pids,
512 filtered_pids->nr_pids, sizeof(pid_t),
521 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
522 struct task_struct *prev, struct task_struct *next)
524 struct trace_array *tr = data;
525 struct trace_pid_list *pid_list;
527 pid_list = rcu_dereference_sched(tr->filtered_pids);
529 this_cpu_write(tr->trace_buffer.data->ignore_pid,
530 check_ignore_pid(pid_list, prev) &&
531 check_ignore_pid(pid_list, next));
535 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
536 struct task_struct *prev, struct task_struct *next)
538 struct trace_array *tr = data;
539 struct trace_pid_list *pid_list;
541 pid_list = rcu_dereference_sched(tr->filtered_pids);
543 this_cpu_write(tr->trace_buffer.data->ignore_pid,
544 check_ignore_pid(pid_list, next));
548 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
550 struct trace_array *tr = data;
551 struct trace_pid_list *pid_list;
553 /* Nothing to do if we are already tracing */
554 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
557 pid_list = rcu_dereference_sched(tr->filtered_pids);
559 this_cpu_write(tr->trace_buffer.data->ignore_pid,
560 check_ignore_pid(pid_list, task));
564 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
566 struct trace_array *tr = data;
567 struct trace_pid_list *pid_list;
569 /* Nothing to do if we are not tracing */
570 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
573 pid_list = rcu_dereference_sched(tr->filtered_pids);
575 /* Set tracing if current is enabled */
576 this_cpu_write(tr->trace_buffer.data->ignore_pid,
577 check_ignore_pid(pid_list, current));
580 static void __ftrace_clear_event_pids(struct trace_array *tr)
582 struct trace_pid_list *pid_list;
583 struct trace_event_file *file;
586 pid_list = rcu_dereference_protected(tr->filtered_pids,
587 lockdep_is_held(&event_mutex));
591 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
592 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
594 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
595 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
597 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
598 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
600 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
601 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
603 list_for_each_entry(file, &tr->events, list) {
604 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
607 for_each_possible_cpu(cpu)
608 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
610 rcu_assign_pointer(tr->filtered_pids, NULL);
612 /* Wait till all users are no longer using pid filtering */
615 free_pages((unsigned long)pid_list->pids, pid_list->order);
619 static void ftrace_clear_event_pids(struct trace_array *tr)
621 mutex_lock(&event_mutex);
622 __ftrace_clear_event_pids(tr);
623 mutex_unlock(&event_mutex);
626 static void __put_system(struct event_subsystem *system)
628 struct event_filter *filter = system->filter;
630 WARN_ON_ONCE(system_refcount(system) == 0);
631 if (system_refcount_dec(system))
634 list_del(&system->list);
637 kfree(filter->filter_string);
640 kfree_const(system->name);
644 static void __get_system(struct event_subsystem *system)
646 WARN_ON_ONCE(system_refcount(system) == 0);
647 system_refcount_inc(system);
650 static void __get_system_dir(struct trace_subsystem_dir *dir)
652 WARN_ON_ONCE(dir->ref_count == 0);
654 __get_system(dir->subsystem);
657 static void __put_system_dir(struct trace_subsystem_dir *dir)
659 WARN_ON_ONCE(dir->ref_count == 0);
660 /* If the subsystem is about to be freed, the dir must be too */
661 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
663 __put_system(dir->subsystem);
664 if (!--dir->ref_count)
668 static void put_system(struct trace_subsystem_dir *dir)
670 mutex_lock(&event_mutex);
671 __put_system_dir(dir);
672 mutex_unlock(&event_mutex);
675 static void remove_subsystem(struct trace_subsystem_dir *dir)
680 if (!--dir->nr_events) {
681 tracefs_remove_recursive(dir->entry);
682 list_del(&dir->list);
683 __put_system_dir(dir);
687 static void remove_event_file_dir(struct trace_event_file *file)
689 struct dentry *dir = file->dir;
690 struct dentry *child;
693 spin_lock(&dir->d_lock); /* probably unneeded */
694 list_for_each_entry(child, &dir->d_subdirs, d_child) {
695 if (d_really_is_positive(child)) /* probably unneeded */
696 d_inode(child)->i_private = NULL;
698 spin_unlock(&dir->d_lock);
700 tracefs_remove_recursive(dir);
703 list_del(&file->list);
704 remove_subsystem(file->system);
705 free_event_filter(file->filter);
706 kmem_cache_free(file_cachep, file);
710 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
713 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
714 const char *sub, const char *event, int set)
716 struct trace_event_file *file;
717 struct trace_event_call *call;
721 list_for_each_entry(file, &tr->events, list) {
723 call = file->event_call;
724 name = trace_event_name(call);
726 if (!name || !call->class || !call->class->reg)
729 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
733 strcmp(match, name) != 0 &&
734 strcmp(match, call->class->system) != 0)
737 if (sub && strcmp(sub, call->class->system) != 0)
740 if (event && strcmp(event, name) != 0)
743 ftrace_event_enable_disable(file, set);
751 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
752 const char *sub, const char *event, int set)
756 mutex_lock(&event_mutex);
757 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
758 mutex_unlock(&event_mutex);
763 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
765 char *event = NULL, *sub = NULL, *match;
769 * The buf format can be <subsystem>:<event-name>
770 * *:<event-name> means any event by that name.
771 * :<event-name> is the same.
773 * <subsystem>:* means all events in that subsystem
774 * <subsystem>: means the same.
776 * <name> (no ':') means all events in a subsystem with
777 * the name <name> or any event that matches <name>
780 match = strsep(&buf, ":");
786 if (!strlen(sub) || strcmp(sub, "*") == 0)
788 if (!strlen(event) || strcmp(event, "*") == 0)
792 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
794 /* Put back the colon to allow this to be called again */
802 * trace_set_clr_event - enable or disable an event
803 * @system: system name to match (NULL for any system)
804 * @event: event name to match (NULL for all events, within system)
805 * @set: 1 to enable, 0 to disable
807 * This is a way for other parts of the kernel to enable or disable
810 * Returns 0 on success, -EINVAL if the parameters do not match any
813 int trace_set_clr_event(const char *system, const char *event, int set)
815 struct trace_array *tr = top_trace_array();
820 return __ftrace_set_clr_event(tr, NULL, system, event, set);
822 EXPORT_SYMBOL_GPL(trace_set_clr_event);
824 /* 128 should be much more than enough */
825 #define EVENT_BUF_SIZE 127
828 ftrace_event_write(struct file *file, const char __user *ubuf,
829 size_t cnt, loff_t *ppos)
831 struct trace_parser parser;
832 struct seq_file *m = file->private_data;
833 struct trace_array *tr = m->private;
839 ret = tracing_update_buffers();
843 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
846 read = trace_get_user(&parser, ubuf, cnt, ppos);
848 if (read >= 0 && trace_parser_loaded((&parser))) {
851 if (*parser.buffer == '!')
854 parser.buffer[parser.idx] = 0;
856 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
864 trace_parser_put(&parser);
870 t_next(struct seq_file *m, void *v, loff_t *pos)
872 struct trace_event_file *file = v;
873 struct trace_event_call *call;
874 struct trace_array *tr = m->private;
878 list_for_each_entry_continue(file, &tr->events, list) {
879 call = file->event_call;
881 * The ftrace subsystem is for showing formats only.
882 * They can not be enabled or disabled via the event files.
884 if (call->class && call->class->reg &&
885 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
892 static void *t_start(struct seq_file *m, loff_t *pos)
894 struct trace_event_file *file;
895 struct trace_array *tr = m->private;
898 mutex_lock(&event_mutex);
900 file = list_entry(&tr->events, struct trace_event_file, list);
901 for (l = 0; l <= *pos; ) {
902 file = t_next(m, file, &l);
910 s_next(struct seq_file *m, void *v, loff_t *pos)
912 struct trace_event_file *file = v;
913 struct trace_array *tr = m->private;
917 list_for_each_entry_continue(file, &tr->events, list) {
918 if (file->flags & EVENT_FILE_FL_ENABLED)
925 static void *s_start(struct seq_file *m, loff_t *pos)
927 struct trace_event_file *file;
928 struct trace_array *tr = m->private;
931 mutex_lock(&event_mutex);
933 file = list_entry(&tr->events, struct trace_event_file, list);
934 for (l = 0; l <= *pos; ) {
935 file = s_next(m, file, &l);
942 static int t_show(struct seq_file *m, void *v)
944 struct trace_event_file *file = v;
945 struct trace_event_call *call = file->event_call;
947 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
948 seq_printf(m, "%s:", call->class->system);
949 seq_printf(m, "%s\n", trace_event_name(call));
954 static void t_stop(struct seq_file *m, void *p)
956 mutex_unlock(&event_mutex);
959 static void *p_start(struct seq_file *m, loff_t *pos)
962 struct trace_pid_list *pid_list;
963 struct trace_array *tr = m->private;
966 * Grab the mutex, to keep calls to p_next() having the same
967 * tr->filtered_pids as p_start() has.
968 * If we just passed the tr->filtered_pids around, then RCU would
969 * have been enough, but doing that makes things more complex.
971 mutex_lock(&event_mutex);
972 rcu_read_lock_sched();
974 pid_list = rcu_dereference_sched(tr->filtered_pids);
976 if (!pid_list || *pos >= pid_list->nr_pids)
979 return (void *)&pid_list->pids[*pos];
982 static void p_stop(struct seq_file *m, void *p)
985 rcu_read_unlock_sched();
986 mutex_unlock(&event_mutex);
990 p_next(struct seq_file *m, void *v, loff_t *pos)
992 struct trace_array *tr = m->private;
993 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
997 if (*pos >= pid_list->nr_pids)
1000 return (void *)&pid_list->pids[*pos];
1003 static int p_show(struct seq_file *m, void *v)
1007 seq_printf(m, "%d\n", *pid);
1012 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1015 struct trace_event_file *file;
1016 unsigned long flags;
1019 mutex_lock(&event_mutex);
1020 file = event_file_data(filp);
1022 flags = file->flags;
1023 mutex_unlock(&event_mutex);
1028 if (flags & EVENT_FILE_FL_ENABLED &&
1029 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1032 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1033 flags & EVENT_FILE_FL_SOFT_MODE)
1038 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1042 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1045 struct trace_event_file *file;
1049 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1053 ret = tracing_update_buffers();
1061 mutex_lock(&event_mutex);
1062 file = event_file_data(filp);
1064 ret = ftrace_event_enable_disable(file, val);
1065 mutex_unlock(&event_mutex);
1074 return ret ? ret : cnt;
1078 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1081 const char set_to_char[4] = { '?', '0', '1', 'X' };
1082 struct trace_subsystem_dir *dir = filp->private_data;
1083 struct event_subsystem *system = dir->subsystem;
1084 struct trace_event_call *call;
1085 struct trace_event_file *file;
1086 struct trace_array *tr = dir->tr;
1091 mutex_lock(&event_mutex);
1092 list_for_each_entry(file, &tr->events, list) {
1093 call = file->event_call;
1094 if (!trace_event_name(call) || !call->class || !call->class->reg)
1097 if (system && strcmp(call->class->system, system->name) != 0)
1101 * We need to find out if all the events are set
1102 * or if all events or cleared, or if we have
1105 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1108 * If we have a mixture, no need to look further.
1113 mutex_unlock(&event_mutex);
1115 buf[0] = set_to_char[set];
1118 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1124 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1127 struct trace_subsystem_dir *dir = filp->private_data;
1128 struct event_subsystem *system = dir->subsystem;
1129 const char *name = NULL;
1133 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1137 ret = tracing_update_buffers();
1141 if (val != 0 && val != 1)
1145 * Opening of "enable" adds a ref count to system,
1146 * so the name is safe to use.
1149 name = system->name;
1151 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1165 FORMAT_FIELD_SEPERATOR = 2,
1166 FORMAT_PRINTFMT = 3,
1169 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1171 struct trace_event_call *call = event_file_data(m->private);
1172 struct list_head *common_head = &ftrace_common_fields;
1173 struct list_head *head = trace_get_fields(call);
1174 struct list_head *node = v;
1178 switch ((unsigned long)v) {
1183 case FORMAT_FIELD_SEPERATOR:
1187 case FORMAT_PRINTFMT:
1193 if (node == common_head)
1194 return (void *)FORMAT_FIELD_SEPERATOR;
1195 else if (node == head)
1196 return (void *)FORMAT_PRINTFMT;
1201 static int f_show(struct seq_file *m, void *v)
1203 struct trace_event_call *call = event_file_data(m->private);
1204 struct ftrace_event_field *field;
1205 const char *array_descriptor;
1207 switch ((unsigned long)v) {
1209 seq_printf(m, "name: %s\n", trace_event_name(call));
1210 seq_printf(m, "ID: %d\n", call->event.type);
1211 seq_puts(m, "format:\n");
1214 case FORMAT_FIELD_SEPERATOR:
1218 case FORMAT_PRINTFMT:
1219 seq_printf(m, "\nprint fmt: %s\n",
1224 field = list_entry(v, struct ftrace_event_field, link);
1226 * Smartly shows the array type(except dynamic array).
1229 * If TYPE := TYPE[LEN], it is shown:
1230 * field:TYPE VAR[LEN]
1232 array_descriptor = strchr(field->type, '[');
1234 if (!strncmp(field->type, "__data_loc", 10))
1235 array_descriptor = NULL;
1237 if (!array_descriptor)
1238 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1239 field->type, field->name, field->offset,
1240 field->size, !!field->is_signed);
1242 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1243 (int)(array_descriptor - field->type),
1244 field->type, field->name,
1245 array_descriptor, field->offset,
1246 field->size, !!field->is_signed);
1251 static void *f_start(struct seq_file *m, loff_t *pos)
1253 void *p = (void *)FORMAT_HEADER;
1256 /* ->stop() is called even if ->start() fails */
1257 mutex_lock(&event_mutex);
1258 if (!event_file_data(m->private))
1259 return ERR_PTR(-ENODEV);
1261 while (l < *pos && p)
1262 p = f_next(m, p, &l);
1267 static void f_stop(struct seq_file *m, void *p)
1269 mutex_unlock(&event_mutex);
1272 static const struct seq_operations trace_format_seq_ops = {
1279 static int trace_format_open(struct inode *inode, struct file *file)
1284 ret = seq_open(file, &trace_format_seq_ops);
1288 m = file->private_data;
1295 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1297 int id = (long)event_file_data(filp);
1307 len = sprintf(buf, "%d\n", id);
1309 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1313 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1316 struct trace_event_file *file;
1317 struct trace_seq *s;
1323 s = kmalloc(sizeof(*s), GFP_KERNEL);
1330 mutex_lock(&event_mutex);
1331 file = event_file_data(filp);
1333 print_event_filter(file, s);
1334 mutex_unlock(&event_mutex);
1337 r = simple_read_from_buffer(ubuf, cnt, ppos,
1338 s->buffer, trace_seq_used(s));
1346 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1349 struct trace_event_file *file;
1353 if (cnt >= PAGE_SIZE)
1356 buf = (char *)__get_free_page(GFP_TEMPORARY);
1360 if (copy_from_user(buf, ubuf, cnt)) {
1361 free_page((unsigned long) buf);
1366 mutex_lock(&event_mutex);
1367 file = event_file_data(filp);
1369 err = apply_event_filter(file, buf);
1370 mutex_unlock(&event_mutex);
1372 free_page((unsigned long) buf);
1381 static LIST_HEAD(event_subsystems);
1383 static int subsystem_open(struct inode *inode, struct file *filp)
1385 struct event_subsystem *system = NULL;
1386 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1387 struct trace_array *tr;
1390 if (tracing_is_disabled())
1393 /* Make sure the system still exists */
1394 mutex_lock(&trace_types_lock);
1395 mutex_lock(&event_mutex);
1396 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1397 list_for_each_entry(dir, &tr->systems, list) {
1398 if (dir == inode->i_private) {
1399 /* Don't open systems with no events */
1400 if (dir->nr_events) {
1401 __get_system_dir(dir);
1402 system = dir->subsystem;
1409 mutex_unlock(&event_mutex);
1410 mutex_unlock(&trace_types_lock);
1415 /* Some versions of gcc think dir can be uninitialized here */
1418 /* Still need to increment the ref count of the system */
1419 if (trace_array_get(tr) < 0) {
1424 ret = tracing_open_generic(inode, filp);
1426 trace_array_put(tr);
1433 static int system_tr_open(struct inode *inode, struct file *filp)
1435 struct trace_subsystem_dir *dir;
1436 struct trace_array *tr = inode->i_private;
1439 if (tracing_is_disabled())
1442 if (trace_array_get(tr) < 0)
1445 /* Make a temporary dir that has no system but points to tr */
1446 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1448 trace_array_put(tr);
1454 ret = tracing_open_generic(inode, filp);
1456 trace_array_put(tr);
1461 filp->private_data = dir;
1466 static int subsystem_release(struct inode *inode, struct file *file)
1468 struct trace_subsystem_dir *dir = file->private_data;
1470 trace_array_put(dir->tr);
1473 * If dir->subsystem is NULL, then this is a temporary
1474 * descriptor that was made for a trace_array to enable
1486 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1489 struct trace_subsystem_dir *dir = filp->private_data;
1490 struct event_subsystem *system = dir->subsystem;
1491 struct trace_seq *s;
1497 s = kmalloc(sizeof(*s), GFP_KERNEL);
1503 print_subsystem_event_filter(system, s);
1504 r = simple_read_from_buffer(ubuf, cnt, ppos,
1505 s->buffer, trace_seq_used(s));
1513 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1516 struct trace_subsystem_dir *dir = filp->private_data;
1520 if (cnt >= PAGE_SIZE)
1523 buf = (char *)__get_free_page(GFP_TEMPORARY);
1527 if (copy_from_user(buf, ubuf, cnt)) {
1528 free_page((unsigned long) buf);
1533 err = apply_subsystem_event_filter(dir, buf);
1534 free_page((unsigned long) buf);
1544 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1546 int (*func)(struct trace_seq *s) = filp->private_data;
1547 struct trace_seq *s;
1553 s = kmalloc(sizeof(*s), GFP_KERNEL);
1560 r = simple_read_from_buffer(ubuf, cnt, ppos,
1561 s->buffer, trace_seq_used(s));
1568 static int max_pids(struct trace_pid_list *pid_list)
1570 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1573 static void ignore_task_cpu(void *data)
1575 struct trace_array *tr = data;
1576 struct trace_pid_list *pid_list;
1579 * This function is called by on_each_cpu() while the
1580 * event_mutex is held.
1582 pid_list = rcu_dereference_protected(tr->filtered_pids,
1583 mutex_is_locked(&event_mutex));
1585 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1586 check_ignore_pid(pid_list, current));
1590 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1591 size_t cnt, loff_t *ppos)
1593 struct seq_file *m = filp->private_data;
1594 struct trace_array *tr = m->private;
1595 struct trace_pid_list *filtered_pids = NULL;
1596 struct trace_pid_list *pid_list = NULL;
1597 struct trace_event_file *file;
1598 struct trace_parser parser;
1609 ret = tracing_update_buffers();
1613 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1616 mutex_lock(&event_mutex);
1618 * Load as many pids into the array before doing a
1619 * swap from the tr->filtered_pids to the new list.
1625 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1626 if (ret < 0 || !trace_parser_loaded(&parser))
1633 parser.buffer[parser.idx] = 0;
1636 if (kstrtoul(parser.buffer, 0, &val))
1645 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1649 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1650 lockdep_is_held(&event_mutex));
1652 pid_list->order = filtered_pids->order;
1654 pid_list->order = 0;
1656 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1658 if (!pid_list->pids)
1661 if (filtered_pids) {
1662 pid_list->nr_pids = filtered_pids->nr_pids;
1663 memcpy(pid_list->pids, filtered_pids->pids,
1664 pid_list->nr_pids * sizeof(pid_t));
1666 pid_list->nr_pids = 0;
1669 if (pid_list->nr_pids >= max_pids(pid_list)) {
1672 pid_page = (void *)__get_free_pages(GFP_KERNEL,
1673 pid_list->order + 1);
1676 memcpy(pid_page, pid_list->pids,
1677 pid_list->nr_pids * sizeof(pid_t));
1678 free_pages((unsigned long)pid_list->pids, pid_list->order);
1681 pid_list->pids = pid_page;
1684 pid_list->pids[pid_list->nr_pids++] = pid;
1685 trace_parser_clear(&parser);
1688 trace_parser_put(&parser);
1692 free_pages((unsigned long)pid_list->pids, pid_list->order);
1694 mutex_unlock(&event_mutex);
1699 mutex_unlock(&event_mutex);
1703 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1705 /* Remove duplicates */
1706 for (i = 1; i < pid_list->nr_pids; i++) {
1709 while (i < pid_list->nr_pids &&
1710 pid_list->pids[i - 1] == pid_list->pids[i])
1714 if (i < pid_list->nr_pids) {
1715 memmove(&pid_list->pids[start], &pid_list->pids[i],
1716 (pid_list->nr_pids - i) * sizeof(pid_t));
1717 pid_list->nr_pids -= i - start;
1720 pid_list->nr_pids = start;
1724 rcu_assign_pointer(tr->filtered_pids, pid_list);
1726 list_for_each_entry(file, &tr->events, list) {
1727 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1730 if (filtered_pids) {
1731 synchronize_sched();
1733 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1734 kfree(filtered_pids);
1737 * Register a probe that is called before all other probes
1738 * to set ignore_pid if next or prev do not match.
1739 * Register a probe this is called after all other probes
1740 * to only keep ignore_pid set if next pid matches.
1742 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1744 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1747 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1749 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1752 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1754 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1757 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1759 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1764 * Ignoring of pids is done at task switch. But we have to
1765 * check for those tasks that are currently running.
1766 * Always do this in case a pid was appended or removed.
1768 on_each_cpu(ignore_task_cpu, tr, 1);
1770 mutex_unlock(&event_mutex);
1778 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1779 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1780 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1781 static int ftrace_event_release(struct inode *inode, struct file *file);
1783 static const struct seq_operations show_event_seq_ops = {
1790 static const struct seq_operations show_set_event_seq_ops = {
1797 static const struct seq_operations show_set_pid_seq_ops = {
1804 static const struct file_operations ftrace_avail_fops = {
1805 .open = ftrace_event_avail_open,
1807 .llseek = seq_lseek,
1808 .release = seq_release,
1811 static const struct file_operations ftrace_set_event_fops = {
1812 .open = ftrace_event_set_open,
1814 .write = ftrace_event_write,
1815 .llseek = seq_lseek,
1816 .release = ftrace_event_release,
1819 static const struct file_operations ftrace_set_event_pid_fops = {
1820 .open = ftrace_event_set_pid_open,
1822 .write = ftrace_event_pid_write,
1823 .llseek = seq_lseek,
1824 .release = ftrace_event_release,
1827 static const struct file_operations ftrace_enable_fops = {
1828 .open = tracing_open_generic,
1829 .read = event_enable_read,
1830 .write = event_enable_write,
1831 .llseek = default_llseek,
1834 static const struct file_operations ftrace_event_format_fops = {
1835 .open = trace_format_open,
1837 .llseek = seq_lseek,
1838 .release = seq_release,
1841 static const struct file_operations ftrace_event_id_fops = {
1842 .read = event_id_read,
1843 .llseek = default_llseek,
1846 static const struct file_operations ftrace_event_filter_fops = {
1847 .open = tracing_open_generic,
1848 .read = event_filter_read,
1849 .write = event_filter_write,
1850 .llseek = default_llseek,
1853 static const struct file_operations ftrace_subsystem_filter_fops = {
1854 .open = subsystem_open,
1855 .read = subsystem_filter_read,
1856 .write = subsystem_filter_write,
1857 .llseek = default_llseek,
1858 .release = subsystem_release,
1861 static const struct file_operations ftrace_system_enable_fops = {
1862 .open = subsystem_open,
1863 .read = system_enable_read,
1864 .write = system_enable_write,
1865 .llseek = default_llseek,
1866 .release = subsystem_release,
1869 static const struct file_operations ftrace_tr_enable_fops = {
1870 .open = system_tr_open,
1871 .read = system_enable_read,
1872 .write = system_enable_write,
1873 .llseek = default_llseek,
1874 .release = subsystem_release,
1877 static const struct file_operations ftrace_show_header_fops = {
1878 .open = tracing_open_generic,
1879 .read = show_header,
1880 .llseek = default_llseek,
1884 ftrace_event_open(struct inode *inode, struct file *file,
1885 const struct seq_operations *seq_ops)
1890 ret = seq_open(file, seq_ops);
1893 m = file->private_data;
1894 /* copy tr over to seq ops */
1895 m->private = inode->i_private;
1900 static int ftrace_event_release(struct inode *inode, struct file *file)
1902 struct trace_array *tr = inode->i_private;
1904 trace_array_put(tr);
1906 return seq_release(inode, file);
1910 ftrace_event_avail_open(struct inode *inode, struct file *file)
1912 const struct seq_operations *seq_ops = &show_event_seq_ops;
1914 return ftrace_event_open(inode, file, seq_ops);
1918 ftrace_event_set_open(struct inode *inode, struct file *file)
1920 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1921 struct trace_array *tr = inode->i_private;
1924 if (trace_array_get(tr) < 0)
1927 if ((file->f_mode & FMODE_WRITE) &&
1928 (file->f_flags & O_TRUNC))
1929 ftrace_clear_events(tr);
1931 ret = ftrace_event_open(inode, file, seq_ops);
1933 trace_array_put(tr);
1938 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1940 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1941 struct trace_array *tr = inode->i_private;
1944 if (trace_array_get(tr) < 0)
1947 if ((file->f_mode & FMODE_WRITE) &&
1948 (file->f_flags & O_TRUNC))
1949 ftrace_clear_event_pids(tr);
1951 ret = ftrace_event_open(inode, file, seq_ops);
1953 trace_array_put(tr);
1957 static struct event_subsystem *
1958 create_new_subsystem(const char *name)
1960 struct event_subsystem *system;
1962 /* need to create new entry */
1963 system = kmalloc(sizeof(*system), GFP_KERNEL);
1967 system->ref_count = 1;
1969 /* Only allocate if dynamic (kprobes and modules) */
1970 system->name = kstrdup_const(name, GFP_KERNEL);
1974 system->filter = NULL;
1976 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1977 if (!system->filter)
1980 list_add(&system->list, &event_subsystems);
1985 kfree_const(system->name);
1990 static struct dentry *
1991 event_subsystem_dir(struct trace_array *tr, const char *name,
1992 struct trace_event_file *file, struct dentry *parent)
1994 struct trace_subsystem_dir *dir;
1995 struct event_subsystem *system;
1996 struct dentry *entry;
1998 /* First see if we did not already create this dir */
1999 list_for_each_entry(dir, &tr->systems, list) {
2000 system = dir->subsystem;
2001 if (strcmp(system->name, name) == 0) {
2008 /* Now see if the system itself exists. */
2009 list_for_each_entry(system, &event_subsystems, list) {
2010 if (strcmp(system->name, name) == 0)
2013 /* Reset system variable when not found */
2014 if (&system->list == &event_subsystems)
2017 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2022 system = create_new_subsystem(name);
2026 __get_system(system);
2028 dir->entry = tracefs_create_dir(name, parent);
2030 pr_warn("Failed to create system directory %s\n", name);
2031 __put_system(system);
2038 dir->subsystem = system;
2041 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2042 &ftrace_subsystem_filter_fops);
2044 kfree(system->filter);
2045 system->filter = NULL;
2046 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2049 trace_create_file("enable", 0644, dir->entry, dir,
2050 &ftrace_system_enable_fops);
2052 list_add(&dir->list, &tr->systems);
2059 /* Only print this message if failed on memory allocation */
2060 if (!dir || !system)
2061 pr_warn("No memory to create event subsystem %s\n", name);
2066 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2068 struct trace_event_call *call = file->event_call;
2069 struct trace_array *tr = file->tr;
2070 struct list_head *head;
2071 struct dentry *d_events;
2076 * If the trace point header did not define TRACE_SYSTEM
2077 * then the system would be called "TRACE_SYSTEM".
2079 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2080 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2086 name = trace_event_name(call);
2087 file->dir = tracefs_create_dir(name, d_events);
2089 pr_warn("Could not create tracefs '%s' directory\n", name);
2093 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2094 trace_create_file("enable", 0644, file->dir, file,
2095 &ftrace_enable_fops);
2097 #ifdef CONFIG_PERF_EVENTS
2098 if (call->event.type && call->class->reg)
2099 trace_create_file("id", 0444, file->dir,
2100 (void *)(long)call->event.type,
2101 &ftrace_event_id_fops);
2105 * Other events may have the same class. Only update
2106 * the fields if they are not already defined.
2108 head = trace_get_fields(call);
2109 if (list_empty(head)) {
2110 ret = call->class->define_fields(call);
2112 pr_warn("Could not initialize trace point events/%s\n",
2117 trace_create_file("filter", 0644, file->dir, file,
2118 &ftrace_event_filter_fops);
2121 * Only event directories that can be enabled should have
2124 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2125 trace_create_file("trigger", 0644, file->dir, file,
2126 &event_trigger_fops);
2128 trace_create_file("format", 0444, file->dir, call,
2129 &ftrace_event_format_fops);
2134 static void remove_event_from_tracers(struct trace_event_call *call)
2136 struct trace_event_file *file;
2137 struct trace_array *tr;
2139 do_for_each_event_file_safe(tr, file) {
2140 if (file->event_call != call)
2143 remove_event_file_dir(file);
2145 * The do_for_each_event_file_safe() is
2146 * a double loop. After finding the call for this
2147 * trace_array, we use break to jump to the next
2151 } while_for_each_event_file();
2154 static void event_remove(struct trace_event_call *call)
2156 struct trace_array *tr;
2157 struct trace_event_file *file;
2159 do_for_each_event_file(tr, file) {
2160 if (file->event_call != call)
2162 ftrace_event_enable_disable(file, 0);
2164 * The do_for_each_event_file() is
2165 * a double loop. After finding the call for this
2166 * trace_array, we use break to jump to the next
2170 } while_for_each_event_file();
2172 if (call->event.funcs)
2173 __unregister_trace_event(&call->event);
2174 remove_event_from_tracers(call);
2175 list_del(&call->list);
2178 static int event_init(struct trace_event_call *call)
2183 name = trace_event_name(call);
2187 if (call->class->raw_init) {
2188 ret = call->class->raw_init(call);
2189 if (ret < 0 && ret != -ENOSYS)
2190 pr_warn("Could not initialize trace events/%s\n", name);
2197 __register_event(struct trace_event_call *call, struct module *mod)
2201 ret = event_init(call);
2205 list_add(&call->list, &ftrace_events);
2211 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2216 /* Find the length of the enum value as a string */
2217 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2218 /* Make sure there's enough room to replace the string with the value */
2222 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2224 /* Get the rest of the string of ptr */
2225 rlen = strlen(ptr + len);
2226 memmove(ptr + elen, ptr + len, rlen);
2227 /* Make sure we end the new string */
2228 ptr[elen + rlen] = 0;
2233 static void update_event_printk(struct trace_event_call *call,
2234 struct trace_enum_map *map)
2238 int len = strlen(map->enum_string);
2240 for (ptr = call->print_fmt; *ptr; ptr++) {
2254 if (isdigit(*ptr)) {
2258 /* Check for alpha chars like ULL */
2259 } while (isalnum(*ptr));
2263 * A number must have some kind of delimiter after
2264 * it, and we can ignore that too.
2268 if (isalpha(*ptr) || *ptr == '_') {
2269 if (strncmp(map->enum_string, ptr, len) == 0 &&
2270 !isalnum(ptr[len]) && ptr[len] != '_') {
2271 ptr = enum_replace(ptr, map, len);
2272 /* Hmm, enum string smaller than value */
2273 if (WARN_ON_ONCE(!ptr))
2276 * No need to decrement here, as enum_replace()
2277 * returns the pointer to the character passed
2278 * the enum, and two enums can not be placed
2279 * back to back without something in between.
2280 * We can skip that something in between.
2287 } while (isalnum(*ptr) || *ptr == '_');
2291 * If what comes after this variable is a '.' or
2292 * '->' then we can continue to ignore that string.
2294 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2295 ptr += *ptr == '.' ? 1 : 2;
2301 * Once again, we can skip the delimiter that came
2309 void trace_event_enum_update(struct trace_enum_map **map, int len)
2311 struct trace_event_call *call, *p;
2312 const char *last_system = NULL;
2316 down_write(&trace_event_sem);
2317 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2318 /* events are usually grouped together with systems */
2319 if (!last_system || call->class->system != last_system) {
2321 last_system = call->class->system;
2324 for (i = last_i; i < len; i++) {
2325 if (call->class->system == map[i]->system) {
2326 /* Save the first system if need be */
2329 update_event_printk(call, map[i]);
2333 up_write(&trace_event_sem);
2336 static struct trace_event_file *
2337 trace_create_new_event(struct trace_event_call *call,
2338 struct trace_array *tr)
2340 struct trace_event_file *file;
2342 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2346 file->event_call = call;
2348 atomic_set(&file->sm_ref, 0);
2349 atomic_set(&file->tm_ref, 0);
2350 INIT_LIST_HEAD(&file->triggers);
2351 list_add(&file->list, &tr->events);
2356 /* Add an event to a trace directory */
2358 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2360 struct trace_event_file *file;
2362 file = trace_create_new_event(call, tr);
2366 return event_create_dir(tr->event_dir, file);
2370 * Just create a decriptor for early init. A descriptor is required
2371 * for enabling events at boot. We want to enable events before
2372 * the filesystem is initialized.
2375 __trace_early_add_new_event(struct trace_event_call *call,
2376 struct trace_array *tr)
2378 struct trace_event_file *file;
2380 file = trace_create_new_event(call, tr);
2387 struct ftrace_module_file_ops;
2388 static void __add_event_to_tracers(struct trace_event_call *call);
2390 /* Add an additional event_call dynamically */
2391 int trace_add_event_call(struct trace_event_call *call)
2394 mutex_lock(&trace_types_lock);
2395 mutex_lock(&event_mutex);
2397 ret = __register_event(call, NULL);
2399 __add_event_to_tracers(call);
2401 mutex_unlock(&event_mutex);
2402 mutex_unlock(&trace_types_lock);
2407 * Must be called under locking of trace_types_lock, event_mutex and
2410 static void __trace_remove_event_call(struct trace_event_call *call)
2413 trace_destroy_fields(call);
2414 free_event_filter(call->filter);
2415 call->filter = NULL;
2418 static int probe_remove_event_call(struct trace_event_call *call)
2420 struct trace_array *tr;
2421 struct trace_event_file *file;
2423 #ifdef CONFIG_PERF_EVENTS
2424 if (call->perf_refcount)
2427 do_for_each_event_file(tr, file) {
2428 if (file->event_call != call)
2431 * We can't rely on ftrace_event_enable_disable(enable => 0)
2432 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2433 * TRACE_REG_UNREGISTER.
2435 if (file->flags & EVENT_FILE_FL_ENABLED)
2438 * The do_for_each_event_file_safe() is
2439 * a double loop. After finding the call for this
2440 * trace_array, we use break to jump to the next
2444 } while_for_each_event_file();
2446 __trace_remove_event_call(call);
2451 /* Remove an event_call */
2452 int trace_remove_event_call(struct trace_event_call *call)
2456 mutex_lock(&trace_types_lock);
2457 mutex_lock(&event_mutex);
2458 down_write(&trace_event_sem);
2459 ret = probe_remove_event_call(call);
2460 up_write(&trace_event_sem);
2461 mutex_unlock(&event_mutex);
2462 mutex_unlock(&trace_types_lock);
2467 #define for_each_event(event, start, end) \
2468 for (event = start; \
2469 (unsigned long)event < (unsigned long)end; \
2472 #ifdef CONFIG_MODULES
2474 static void trace_module_add_events(struct module *mod)
2476 struct trace_event_call **call, **start, **end;
2478 if (!mod->num_trace_events)
2481 /* Don't add infrastructure for mods without tracepoints */
2482 if (trace_module_has_bad_taint(mod)) {
2483 pr_err("%s: module has bad taint, not creating trace events\n",
2488 start = mod->trace_events;
2489 end = mod->trace_events + mod->num_trace_events;
2491 for_each_event(call, start, end) {
2492 __register_event(*call, mod);
2493 __add_event_to_tracers(*call);
2497 static void trace_module_remove_events(struct module *mod)
2499 struct trace_event_call *call, *p;
2500 bool clear_trace = false;
2502 down_write(&trace_event_sem);
2503 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2504 if (call->mod == mod) {
2505 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2507 __trace_remove_event_call(call);
2510 up_write(&trace_event_sem);
2513 * It is safest to reset the ring buffer if the module being unloaded
2514 * registered any events that were used. The only worry is if
2515 * a new module gets loaded, and takes on the same id as the events
2516 * of this module. When printing out the buffer, traced events left
2517 * over from this module may be passed to the new module events and
2518 * unexpected results may occur.
2521 tracing_reset_all_online_cpus();
2524 static int trace_module_notify(struct notifier_block *self,
2525 unsigned long val, void *data)
2527 struct module *mod = data;
2529 mutex_lock(&trace_types_lock);
2530 mutex_lock(&event_mutex);
2532 case MODULE_STATE_COMING:
2533 trace_module_add_events(mod);
2535 case MODULE_STATE_GOING:
2536 trace_module_remove_events(mod);
2539 mutex_unlock(&event_mutex);
2540 mutex_unlock(&trace_types_lock);
2545 static struct notifier_block trace_module_nb = {
2546 .notifier_call = trace_module_notify,
2547 .priority = 1, /* higher than trace.c module notify */
2549 #endif /* CONFIG_MODULES */
2551 /* Create a new event directory structure for a trace directory. */
2553 __trace_add_event_dirs(struct trace_array *tr)
2555 struct trace_event_call *call;
2558 list_for_each_entry(call, &ftrace_events, list) {
2559 ret = __trace_add_new_event(call, tr);
2561 pr_warn("Could not create directory for event %s\n",
2562 trace_event_name(call));
2566 struct trace_event_file *
2567 find_event_file(struct trace_array *tr, const char *system, const char *event)
2569 struct trace_event_file *file;
2570 struct trace_event_call *call;
2573 list_for_each_entry(file, &tr->events, list) {
2575 call = file->event_call;
2576 name = trace_event_name(call);
2578 if (!name || !call->class || !call->class->reg)
2581 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2584 if (strcmp(event, name) == 0 &&
2585 strcmp(system, call->class->system) == 0)
2591 #ifdef CONFIG_DYNAMIC_FTRACE
2594 #define ENABLE_EVENT_STR "enable_event"
2595 #define DISABLE_EVENT_STR "disable_event"
2597 struct event_probe_data {
2598 struct trace_event_file *file;
2599 unsigned long count;
2605 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2607 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2608 struct event_probe_data *data = *pdata;
2614 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2616 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2620 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2622 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2623 struct event_probe_data *data = *pdata;
2631 /* Skip if the event is in a state we want to switch to */
2632 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2635 if (data->count != -1)
2638 event_enable_probe(ip, parent_ip, _data);
2642 event_enable_print(struct seq_file *m, unsigned long ip,
2643 struct ftrace_probe_ops *ops, void *_data)
2645 struct event_probe_data *data = _data;
2647 seq_printf(m, "%ps:", (void *)ip);
2649 seq_printf(m, "%s:%s:%s",
2650 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2651 data->file->event_call->class->system,
2652 trace_event_name(data->file->event_call));
2654 if (data->count == -1)
2655 seq_puts(m, ":unlimited\n");
2657 seq_printf(m, ":count=%ld\n", data->count);
2663 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2666 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2667 struct event_probe_data *data = *pdata;
2674 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2677 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2678 struct event_probe_data *data = *pdata;
2680 if (WARN_ON_ONCE(data->ref <= 0))
2685 /* Remove the SOFT_MODE flag */
2686 __ftrace_event_enable_disable(data->file, 0, 1);
2687 module_put(data->file->event_call->mod);
2693 static struct ftrace_probe_ops event_enable_probe_ops = {
2694 .func = event_enable_probe,
2695 .print = event_enable_print,
2696 .init = event_enable_init,
2697 .free = event_enable_free,
2700 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2701 .func = event_enable_count_probe,
2702 .print = event_enable_print,
2703 .init = event_enable_init,
2704 .free = event_enable_free,
2707 static struct ftrace_probe_ops event_disable_probe_ops = {
2708 .func = event_enable_probe,
2709 .print = event_enable_print,
2710 .init = event_enable_init,
2711 .free = event_enable_free,
2714 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2715 .func = event_enable_count_probe,
2716 .print = event_enable_print,
2717 .init = event_enable_init,
2718 .free = event_enable_free,
2722 event_enable_func(struct ftrace_hash *hash,
2723 char *glob, char *cmd, char *param, int enabled)
2725 struct trace_array *tr = top_trace_array();
2726 struct trace_event_file *file;
2727 struct ftrace_probe_ops *ops;
2728 struct event_probe_data *data;
2738 /* hash funcs only work with set_ftrace_filter */
2739 if (!enabled || !param)
2742 system = strsep(¶m, ":");
2746 event = strsep(¶m, ":");
2748 mutex_lock(&event_mutex);
2751 file = find_event_file(tr, system, event);
2755 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2758 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2760 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2762 if (glob[0] == '!') {
2763 unregister_ftrace_function_probe_func(glob+1, ops);
2769 data = kzalloc(sizeof(*data), GFP_KERNEL);
2773 data->enable = enable;
2780 number = strsep(¶m, ":");
2783 if (!strlen(number))
2787 * We use the callback data field (which is a pointer)
2790 ret = kstrtoul(number, 0, &data->count);
2795 /* Don't let event modules unload while probe registered */
2796 ret = try_module_get(file->event_call->mod);
2802 ret = __ftrace_event_enable_disable(file, 1, 1);
2805 ret = register_ftrace_function_probe(glob, ops, data);
2807 * The above returns on success the # of functions enabled,
2808 * but if it didn't find any functions it returns zero.
2809 * Consider no functions a failure too.
2816 /* Just return zero, not the number of enabled functions */
2819 mutex_unlock(&event_mutex);
2823 __ftrace_event_enable_disable(file, 0, 1);
2825 module_put(file->event_call->mod);
2831 static struct ftrace_func_command event_enable_cmd = {
2832 .name = ENABLE_EVENT_STR,
2833 .func = event_enable_func,
2836 static struct ftrace_func_command event_disable_cmd = {
2837 .name = DISABLE_EVENT_STR,
2838 .func = event_enable_func,
2841 static __init int register_event_cmds(void)
2845 ret = register_ftrace_command(&event_enable_cmd);
2846 if (WARN_ON(ret < 0))
2848 ret = register_ftrace_command(&event_disable_cmd);
2849 if (WARN_ON(ret < 0))
2850 unregister_ftrace_command(&event_enable_cmd);
2854 static inline int register_event_cmds(void) { return 0; }
2855 #endif /* CONFIG_DYNAMIC_FTRACE */
2858 * The top level array has already had its trace_event_file
2859 * descriptors created in order to allow for early events to
2860 * be recorded. This function is called after the tracefs has been
2861 * initialized, and we now have to create the files associated
2865 __trace_early_add_event_dirs(struct trace_array *tr)
2867 struct trace_event_file *file;
2871 list_for_each_entry(file, &tr->events, list) {
2872 ret = event_create_dir(tr->event_dir, file);
2874 pr_warn("Could not create directory for event %s\n",
2875 trace_event_name(file->event_call));
2880 * For early boot up, the top trace array requires to have
2881 * a list of events that can be enabled. This must be done before
2882 * the filesystem is set up in order to allow events to be traced
2886 __trace_early_add_events(struct trace_array *tr)
2888 struct trace_event_call *call;
2891 list_for_each_entry(call, &ftrace_events, list) {
2892 /* Early boot up should not have any modules loaded */
2893 if (WARN_ON_ONCE(call->mod))
2896 ret = __trace_early_add_new_event(call, tr);
2898 pr_warn("Could not create early event %s\n",
2899 trace_event_name(call));
2903 /* Remove the event directory structure for a trace directory. */
2905 __trace_remove_event_dirs(struct trace_array *tr)
2907 struct trace_event_file *file, *next;
2909 list_for_each_entry_safe(file, next, &tr->events, list)
2910 remove_event_file_dir(file);
2913 static void __add_event_to_tracers(struct trace_event_call *call)
2915 struct trace_array *tr;
2917 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2918 __trace_add_new_event(call, tr);
2921 extern struct trace_event_call *__start_ftrace_events[];
2922 extern struct trace_event_call *__stop_ftrace_events[];
2924 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2926 static __init int setup_trace_event(char *str)
2928 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2929 ring_buffer_expanded = true;
2930 tracing_selftest_disabled = true;
2934 __setup("trace_event=", setup_trace_event);
2936 /* Expects to have event_mutex held when called */
2938 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2940 struct dentry *d_events;
2941 struct dentry *entry;
2943 entry = tracefs_create_file("set_event", 0644, parent,
2944 tr, &ftrace_set_event_fops);
2946 pr_warn("Could not create tracefs 'set_event' entry\n");
2950 d_events = tracefs_create_dir("events", parent);
2952 pr_warn("Could not create tracefs 'events' directory\n");
2956 entry = tracefs_create_file("set_event_pid", 0644, parent,
2957 tr, &ftrace_set_event_pid_fops);
2959 /* ring buffer internal formats */
2960 trace_create_file("header_page", 0444, d_events,
2961 ring_buffer_print_page_header,
2962 &ftrace_show_header_fops);
2964 trace_create_file("header_event", 0444, d_events,
2965 ring_buffer_print_entry_header,
2966 &ftrace_show_header_fops);
2968 trace_create_file("enable", 0644, d_events,
2969 tr, &ftrace_tr_enable_fops);
2971 tr->event_dir = d_events;
2977 * event_trace_add_tracer - add a instance of a trace_array to events
2978 * @parent: The parent dentry to place the files/directories for events in
2979 * @tr: The trace array associated with these events
2981 * When a new instance is created, it needs to set up its events
2982 * directory, as well as other files associated with events. It also
2983 * creates the event hierachry in the @parent/events directory.
2985 * Returns 0 on success.
2987 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2991 mutex_lock(&event_mutex);
2993 ret = create_event_toplevel_files(parent, tr);
2997 down_write(&trace_event_sem);
2998 __trace_add_event_dirs(tr);
2999 up_write(&trace_event_sem);
3002 mutex_unlock(&event_mutex);
3008 * The top trace array already had its file descriptors created.
3009 * Now the files themselves need to be created.
3012 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3016 mutex_lock(&event_mutex);
3018 ret = create_event_toplevel_files(parent, tr);
3022 down_write(&trace_event_sem);
3023 __trace_early_add_event_dirs(tr);
3024 up_write(&trace_event_sem);
3027 mutex_unlock(&event_mutex);
3032 int event_trace_del_tracer(struct trace_array *tr)
3034 mutex_lock(&event_mutex);
3036 /* Disable any event triggers and associated soft-disabled events */
3037 clear_event_triggers(tr);
3039 /* Clear the pid list */
3040 __ftrace_clear_event_pids(tr);
3042 /* Disable any running events */
3043 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3045 /* Access to events are within rcu_read_lock_sched() */
3046 synchronize_sched();
3048 down_write(&trace_event_sem);
3049 __trace_remove_event_dirs(tr);
3050 tracefs_remove_recursive(tr->event_dir);
3051 up_write(&trace_event_sem);
3053 tr->event_dir = NULL;
3055 mutex_unlock(&event_mutex);
3060 static __init int event_trace_memsetup(void)
3062 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3063 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3068 early_enable_events(struct trace_array *tr, bool disable_first)
3070 char *buf = bootup_event_buf;
3075 token = strsep(&buf, ",");
3081 /* Restarting syscalls requires that we stop them first */
3083 ftrace_set_clr_event(tr, token, 0);
3085 ret = ftrace_set_clr_event(tr, token, 1);
3087 pr_warn("Failed to enable trace event: %s\n", token);
3090 /* Put back the comma to allow this to be called again */
3096 static __init int event_trace_enable(void)
3098 struct trace_array *tr = top_trace_array();
3099 struct trace_event_call **iter, *call;
3105 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3108 ret = event_init(call);
3110 list_add(&call->list, &ftrace_events);
3114 * We need the top trace array to have a working set of trace
3115 * points at early init, before the debug files and directories
3116 * are created. Create the file entries now, and attach them
3117 * to the actual file dentries later.
3119 __trace_early_add_events(tr);
3121 early_enable_events(tr, false);
3123 trace_printk_start_comm();
3125 register_event_cmds();
3127 register_trigger_cmds();
3133 * event_trace_enable() is called from trace_event_init() first to
3134 * initialize events and perhaps start any events that are on the
3135 * command line. Unfortunately, there are some events that will not
3136 * start this early, like the system call tracepoints that need
3137 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3138 * is called before pid 1 starts, and this flag is never set, making
3139 * the syscall tracepoint never get reached, but the event is enabled
3140 * regardless (and not doing anything).
3142 static __init int event_trace_enable_again(void)
3144 struct trace_array *tr;
3146 tr = top_trace_array();
3150 early_enable_events(tr, true);
3155 early_initcall(event_trace_enable_again);
3157 static __init int event_trace_init(void)
3159 struct trace_array *tr;
3160 struct dentry *d_tracer;
3161 struct dentry *entry;
3164 tr = top_trace_array();
3168 d_tracer = tracing_init_dentry();
3169 if (IS_ERR(d_tracer))
3172 entry = tracefs_create_file("available_events", 0444, d_tracer,
3173 tr, &ftrace_avail_fops);
3175 pr_warn("Could not create tracefs 'available_events' entry\n");
3177 if (trace_define_generic_fields())
3178 pr_warn("tracing: Failed to allocated generic fields");
3180 if (trace_define_common_fields())
3181 pr_warn("tracing: Failed to allocate common fields");
3183 ret = early_event_add_tracer(d_tracer, tr);
3187 #ifdef CONFIG_MODULES
3188 ret = register_module_notifier(&trace_module_nb);
3190 pr_warn("Failed to register trace events module notifier\n");
3195 void __init trace_event_init(void)
3197 event_trace_memsetup();
3198 init_ftrace_syscalls();
3199 event_trace_enable();
3202 fs_initcall(event_trace_init);
3204 #ifdef CONFIG_FTRACE_STARTUP_TEST
3206 static DEFINE_SPINLOCK(test_spinlock);
3207 static DEFINE_SPINLOCK(test_spinlock_irq);
3208 static DEFINE_MUTEX(test_mutex);
3210 static __init void test_work(struct work_struct *dummy)
3212 spin_lock(&test_spinlock);
3213 spin_lock_irq(&test_spinlock_irq);
3215 spin_unlock_irq(&test_spinlock_irq);
3216 spin_unlock(&test_spinlock);
3218 mutex_lock(&test_mutex);
3220 mutex_unlock(&test_mutex);
3223 static __init int event_test_thread(void *unused)
3227 test_malloc = kmalloc(1234, GFP_KERNEL);
3229 pr_info("failed to kmalloc\n");
3231 schedule_on_each_cpu(test_work);
3235 set_current_state(TASK_INTERRUPTIBLE);
3236 while (!kthread_should_stop()) {
3238 set_current_state(TASK_INTERRUPTIBLE);
3240 __set_current_state(TASK_RUNNING);
3246 * Do various things that may trigger events.
3248 static __init void event_test_stuff(void)
3250 struct task_struct *test_thread;
3252 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3254 kthread_stop(test_thread);
3258 * For every trace event defined, we will test each trace point separately,
3259 * and then by groups, and finally all trace points.
3261 static __init void event_trace_self_tests(void)
3263 struct trace_subsystem_dir *dir;
3264 struct trace_event_file *file;
3265 struct trace_event_call *call;
3266 struct event_subsystem *system;
3267 struct trace_array *tr;
3270 tr = top_trace_array();
3274 pr_info("Running tests on trace events:\n");
3276 list_for_each_entry(file, &tr->events, list) {
3278 call = file->event_call;
3280 /* Only test those that have a probe */
3281 if (!call->class || !call->class->probe)
3285 * Testing syscall events here is pretty useless, but
3286 * we still do it if configured. But this is time consuming.
3287 * What we really need is a user thread to perform the
3288 * syscalls as we test.
3290 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3291 if (call->class->system &&
3292 strcmp(call->class->system, "syscalls") == 0)
3296 pr_info("Testing event %s: ", trace_event_name(call));
3299 * If an event is already enabled, someone is using
3300 * it and the self test should not be on.
3302 if (file->flags & EVENT_FILE_FL_ENABLED) {
3303 pr_warn("Enabled event during self test!\n");
3308 ftrace_event_enable_disable(file, 1);
3310 ftrace_event_enable_disable(file, 0);
3315 /* Now test at the sub system level */
3317 pr_info("Running tests on trace event systems:\n");
3319 list_for_each_entry(dir, &tr->systems, list) {
3321 system = dir->subsystem;
3323 /* the ftrace system is special, skip it */
3324 if (strcmp(system->name, "ftrace") == 0)
3327 pr_info("Testing event system %s: ", system->name);
3329 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3330 if (WARN_ON_ONCE(ret)) {
3331 pr_warn("error enabling system %s\n",
3338 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3339 if (WARN_ON_ONCE(ret)) {
3340 pr_warn("error disabling system %s\n",
3348 /* Test with all events enabled */
3350 pr_info("Running tests on all trace events:\n");
3351 pr_info("Testing all events: ");
3353 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3354 if (WARN_ON_ONCE(ret)) {
3355 pr_warn("error enabling all events\n");
3362 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3363 if (WARN_ON_ONCE(ret)) {
3364 pr_warn("error disabling all events\n");
3371 #ifdef CONFIG_FUNCTION_TRACER
3373 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3375 static struct trace_array *event_tr;
3378 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3379 struct ftrace_ops *op, struct pt_regs *pt_regs)
3381 struct ring_buffer_event *event;
3382 struct ring_buffer *buffer;
3383 struct ftrace_entry *entry;
3384 unsigned long flags;
3389 pc = preempt_count();
3390 preempt_disable_notrace();
3391 cpu = raw_smp_processor_id();
3392 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3397 local_save_flags(flags);
3399 event = trace_current_buffer_lock_reserve(&buffer,
3400 TRACE_FN, sizeof(*entry),
3404 entry = ring_buffer_event_data(event);
3406 entry->parent_ip = parent_ip;
3408 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
3411 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3412 preempt_enable_notrace();
3415 static struct ftrace_ops trace_ops __initdata =
3417 .func = function_test_events_call,
3418 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3421 static __init void event_trace_self_test_with_function(void)
3424 event_tr = top_trace_array();
3425 if (WARN_ON(!event_tr))
3427 ret = register_ftrace_function(&trace_ops);
3428 if (WARN_ON(ret < 0)) {
3429 pr_info("Failed to enable function tracer for event tests\n");
3432 pr_info("Running tests again, along with the function tracer\n");
3433 event_trace_self_tests();
3434 unregister_ftrace_function(&trace_ops);
3437 static __init void event_trace_self_test_with_function(void)
3442 static __init int event_trace_self_tests_init(void)
3444 if (!tracing_selftest_disabled) {
3445 event_trace_self_tests();
3446 event_trace_self_test_with_function();
3452 late_initcall(event_trace_self_tests_init);