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();
249 fbuffer->trace_file = trace_file;
252 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
253 event_call->event.type, len,
254 fbuffer->flags, fbuffer->pc);
258 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
259 return fbuffer->entry;
261 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
263 static DEFINE_SPINLOCK(tracepoint_iter_lock);
265 static void output_printk(struct trace_event_buffer *fbuffer)
267 struct trace_event_call *event_call;
268 struct trace_event *event;
270 struct trace_iterator *iter = tracepoint_print_iter;
275 event_call = fbuffer->trace_file->event_call;
276 if (!event_call || !event_call->event.funcs ||
277 !event_call->event.funcs->trace)
280 event = &fbuffer->trace_file->event_call->event;
282 spin_lock_irqsave(&tracepoint_iter_lock, flags);
283 trace_seq_init(&iter->seq);
284 iter->ent = fbuffer->entry;
285 event_call->event.funcs->trace(iter, 0, event);
286 trace_seq_putc(&iter->seq, 0);
287 printk("%s", iter->seq.buffer);
289 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
292 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
294 if (tracepoint_printk)
295 output_printk(fbuffer);
297 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
298 fbuffer->event, fbuffer->entry,
299 fbuffer->flags, fbuffer->pc);
301 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
303 int trace_event_reg(struct trace_event_call *call,
304 enum trace_reg type, void *data)
306 struct trace_event_file *file = data;
308 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
310 case TRACE_REG_REGISTER:
311 return tracepoint_probe_register(call->tp,
314 case TRACE_REG_UNREGISTER:
315 tracepoint_probe_unregister(call->tp,
320 #ifdef CONFIG_PERF_EVENTS
321 case TRACE_REG_PERF_REGISTER:
322 return tracepoint_probe_register(call->tp,
323 call->class->perf_probe,
325 case TRACE_REG_PERF_UNREGISTER:
326 tracepoint_probe_unregister(call->tp,
327 call->class->perf_probe,
330 case TRACE_REG_PERF_OPEN:
331 case TRACE_REG_PERF_CLOSE:
332 case TRACE_REG_PERF_ADD:
333 case TRACE_REG_PERF_DEL:
339 EXPORT_SYMBOL_GPL(trace_event_reg);
341 void trace_event_enable_cmd_record(bool enable)
343 struct trace_event_file *file;
344 struct trace_array *tr;
346 mutex_lock(&event_mutex);
347 do_for_each_event_file(tr, file) {
349 if (!(file->flags & EVENT_FILE_FL_ENABLED))
353 tracing_start_cmdline_record();
354 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
356 tracing_stop_cmdline_record();
357 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
359 } while_for_each_event_file();
360 mutex_unlock(&event_mutex);
363 static int __ftrace_event_enable_disable(struct trace_event_file *file,
364 int enable, int soft_disable)
366 struct trace_event_call *call = file->event_call;
367 struct trace_array *tr = file->tr;
374 * When soft_disable is set and enable is cleared, the sm_ref
375 * reference counter is decremented. If it reaches 0, we want
376 * to clear the SOFT_DISABLED flag but leave the event in the
377 * state that it was. That is, if the event was enabled and
378 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
379 * is set we do not want the event to be enabled before we
382 * When soft_disable is not set but the SOFT_MODE flag is,
383 * we do nothing. Do not disable the tracepoint, otherwise
384 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
387 if (atomic_dec_return(&file->sm_ref) > 0)
389 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
390 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
392 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
394 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
395 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
396 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
397 tracing_stop_cmdline_record();
398 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
400 call->class->reg(call, TRACE_REG_UNREGISTER, file);
402 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
403 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
404 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
406 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
410 * When soft_disable is set and enable is set, we want to
411 * register the tracepoint for the event, but leave the event
412 * as is. That means, if the event was already enabled, we do
413 * nothing (but set SOFT_MODE). If the event is disabled, we
414 * set SOFT_DISABLED before enabling the event tracepoint, so
415 * it still seems to be disabled.
418 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
420 if (atomic_inc_return(&file->sm_ref) > 1)
422 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
425 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
427 /* Keep the event disabled, when going to SOFT_MODE. */
429 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
431 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
432 tracing_start_cmdline_record();
433 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
435 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
437 tracing_stop_cmdline_record();
438 pr_info("event trace: Could not enable event "
439 "%s\n", trace_event_name(call));
442 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
444 /* WAS_ENABLED gets set but never cleared. */
445 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
453 int trace_event_enable_disable(struct trace_event_file *file,
454 int enable, int soft_disable)
456 return __ftrace_event_enable_disable(file, enable, soft_disable);
459 static int ftrace_event_enable_disable(struct trace_event_file *file,
462 return __ftrace_event_enable_disable(file, enable, 0);
465 static void ftrace_clear_events(struct trace_array *tr)
467 struct trace_event_file *file;
469 mutex_lock(&event_mutex);
470 list_for_each_entry(file, &tr->events, list) {
471 ftrace_event_enable_disable(file, 0);
473 mutex_unlock(&event_mutex);
476 static int cmp_pid(const void *key, const void *elt)
478 const pid_t *search_pid = key;
479 const pid_t *pid = elt;
481 if (*search_pid == *pid)
483 if (*search_pid < *pid)
489 check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
495 * Return false, because if filtered_pids does not exist,
496 * all pids are good to trace.
501 search_pid = task->pid;
503 pid = bsearch(&search_pid, filtered_pids->pids,
504 filtered_pids->nr_pids, sizeof(pid_t),
513 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
514 struct task_struct *prev, struct task_struct *next)
516 struct trace_array *tr = data;
517 struct trace_pid_list *pid_list;
519 pid_list = rcu_dereference_sched(tr->filtered_pids);
521 this_cpu_write(tr->trace_buffer.data->ignore_pid,
522 check_ignore_pid(pid_list, prev) &&
523 check_ignore_pid(pid_list, next));
527 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
528 struct task_struct *prev, struct task_struct *next)
530 struct trace_array *tr = data;
531 struct trace_pid_list *pid_list;
533 pid_list = rcu_dereference_sched(tr->filtered_pids);
535 this_cpu_write(tr->trace_buffer.data->ignore_pid,
536 check_ignore_pid(pid_list, next));
540 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
542 struct trace_array *tr = data;
543 struct trace_pid_list *pid_list;
545 /* Nothing to do if we are already tracing */
546 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
549 pid_list = rcu_dereference_sched(tr->filtered_pids);
551 this_cpu_write(tr->trace_buffer.data->ignore_pid,
552 check_ignore_pid(pid_list, task));
556 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
558 struct trace_array *tr = data;
559 struct trace_pid_list *pid_list;
561 /* Nothing to do if we are not tracing */
562 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
565 pid_list = rcu_dereference_sched(tr->filtered_pids);
567 /* Set tracing if current is enabled */
568 this_cpu_write(tr->trace_buffer.data->ignore_pid,
569 check_ignore_pid(pid_list, current));
572 static void __ftrace_clear_event_pids(struct trace_array *tr)
574 struct trace_pid_list *pid_list;
575 struct trace_event_file *file;
578 pid_list = rcu_dereference_protected(tr->filtered_pids,
579 lockdep_is_held(&event_mutex));
583 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
584 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
586 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
587 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
589 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
590 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
592 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
593 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
595 list_for_each_entry(file, &tr->events, list) {
596 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
599 for_each_possible_cpu(cpu)
600 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
602 rcu_assign_pointer(tr->filtered_pids, NULL);
604 /* Wait till all users are no longer using pid filtering */
607 free_pages((unsigned long)pid_list->pids, pid_list->order);
611 static void ftrace_clear_event_pids(struct trace_array *tr)
613 mutex_lock(&event_mutex);
614 __ftrace_clear_event_pids(tr);
615 mutex_unlock(&event_mutex);
618 static void __put_system(struct event_subsystem *system)
620 struct event_filter *filter = system->filter;
622 WARN_ON_ONCE(system_refcount(system) == 0);
623 if (system_refcount_dec(system))
626 list_del(&system->list);
629 kfree(filter->filter_string);
632 kfree_const(system->name);
636 static void __get_system(struct event_subsystem *system)
638 WARN_ON_ONCE(system_refcount(system) == 0);
639 system_refcount_inc(system);
642 static void __get_system_dir(struct trace_subsystem_dir *dir)
644 WARN_ON_ONCE(dir->ref_count == 0);
646 __get_system(dir->subsystem);
649 static void __put_system_dir(struct trace_subsystem_dir *dir)
651 WARN_ON_ONCE(dir->ref_count == 0);
652 /* If the subsystem is about to be freed, the dir must be too */
653 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
655 __put_system(dir->subsystem);
656 if (!--dir->ref_count)
660 static void put_system(struct trace_subsystem_dir *dir)
662 mutex_lock(&event_mutex);
663 __put_system_dir(dir);
664 mutex_unlock(&event_mutex);
667 static void remove_subsystem(struct trace_subsystem_dir *dir)
672 if (!--dir->nr_events) {
673 tracefs_remove_recursive(dir->entry);
674 list_del(&dir->list);
675 __put_system_dir(dir);
679 static void remove_event_file_dir(struct trace_event_file *file)
681 struct dentry *dir = file->dir;
682 struct dentry *child;
685 spin_lock(&dir->d_lock); /* probably unneeded */
686 list_for_each_entry(child, &dir->d_subdirs, d_child) {
687 if (d_really_is_positive(child)) /* probably unneeded */
688 d_inode(child)->i_private = NULL;
690 spin_unlock(&dir->d_lock);
692 tracefs_remove_recursive(dir);
695 list_del(&file->list);
696 remove_subsystem(file->system);
697 free_event_filter(file->filter);
698 kmem_cache_free(file_cachep, file);
702 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
705 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
706 const char *sub, const char *event, int set)
708 struct trace_event_file *file;
709 struct trace_event_call *call;
713 list_for_each_entry(file, &tr->events, list) {
715 call = file->event_call;
716 name = trace_event_name(call);
718 if (!name || !call->class || !call->class->reg)
721 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
725 strcmp(match, name) != 0 &&
726 strcmp(match, call->class->system) != 0)
729 if (sub && strcmp(sub, call->class->system) != 0)
732 if (event && strcmp(event, name) != 0)
735 ftrace_event_enable_disable(file, set);
743 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
744 const char *sub, const char *event, int set)
748 mutex_lock(&event_mutex);
749 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
750 mutex_unlock(&event_mutex);
755 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
757 char *event = NULL, *sub = NULL, *match;
761 * The buf format can be <subsystem>:<event-name>
762 * *:<event-name> means any event by that name.
763 * :<event-name> is the same.
765 * <subsystem>:* means all events in that subsystem
766 * <subsystem>: means the same.
768 * <name> (no ':') means all events in a subsystem with
769 * the name <name> or any event that matches <name>
772 match = strsep(&buf, ":");
778 if (!strlen(sub) || strcmp(sub, "*") == 0)
780 if (!strlen(event) || strcmp(event, "*") == 0)
784 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
786 /* Put back the colon to allow this to be called again */
794 * trace_set_clr_event - enable or disable an event
795 * @system: system name to match (NULL for any system)
796 * @event: event name to match (NULL for all events, within system)
797 * @set: 1 to enable, 0 to disable
799 * This is a way for other parts of the kernel to enable or disable
802 * Returns 0 on success, -EINVAL if the parameters do not match any
805 int trace_set_clr_event(const char *system, const char *event, int set)
807 struct trace_array *tr = top_trace_array();
812 return __ftrace_set_clr_event(tr, NULL, system, event, set);
814 EXPORT_SYMBOL_GPL(trace_set_clr_event);
816 /* 128 should be much more than enough */
817 #define EVENT_BUF_SIZE 127
820 ftrace_event_write(struct file *file, const char __user *ubuf,
821 size_t cnt, loff_t *ppos)
823 struct trace_parser parser;
824 struct seq_file *m = file->private_data;
825 struct trace_array *tr = m->private;
831 ret = tracing_update_buffers();
835 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
838 read = trace_get_user(&parser, ubuf, cnt, ppos);
840 if (read >= 0 && trace_parser_loaded((&parser))) {
843 if (*parser.buffer == '!')
846 parser.buffer[parser.idx] = 0;
848 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
856 trace_parser_put(&parser);
862 t_next(struct seq_file *m, void *v, loff_t *pos)
864 struct trace_event_file *file = v;
865 struct trace_event_call *call;
866 struct trace_array *tr = m->private;
870 list_for_each_entry_continue(file, &tr->events, list) {
871 call = file->event_call;
873 * The ftrace subsystem is for showing formats only.
874 * They can not be enabled or disabled via the event files.
876 if (call->class && call->class->reg &&
877 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
884 static void *t_start(struct seq_file *m, loff_t *pos)
886 struct trace_event_file *file;
887 struct trace_array *tr = m->private;
890 mutex_lock(&event_mutex);
892 file = list_entry(&tr->events, struct trace_event_file, list);
893 for (l = 0; l <= *pos; ) {
894 file = t_next(m, file, &l);
902 s_next(struct seq_file *m, void *v, loff_t *pos)
904 struct trace_event_file *file = v;
905 struct trace_array *tr = m->private;
909 list_for_each_entry_continue(file, &tr->events, list) {
910 if (file->flags & EVENT_FILE_FL_ENABLED)
917 static void *s_start(struct seq_file *m, loff_t *pos)
919 struct trace_event_file *file;
920 struct trace_array *tr = m->private;
923 mutex_lock(&event_mutex);
925 file = list_entry(&tr->events, struct trace_event_file, list);
926 for (l = 0; l <= *pos; ) {
927 file = s_next(m, file, &l);
934 static int t_show(struct seq_file *m, void *v)
936 struct trace_event_file *file = v;
937 struct trace_event_call *call = file->event_call;
939 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
940 seq_printf(m, "%s:", call->class->system);
941 seq_printf(m, "%s\n", trace_event_name(call));
946 static void t_stop(struct seq_file *m, void *p)
948 mutex_unlock(&event_mutex);
951 static void *p_start(struct seq_file *m, loff_t *pos)
954 struct trace_pid_list *pid_list;
955 struct trace_array *tr = m->private;
958 * Grab the mutex, to keep calls to p_next() having the same
959 * tr->filtered_pids as p_start() has.
960 * If we just passed the tr->filtered_pids around, then RCU would
961 * have been enough, but doing that makes things more complex.
963 mutex_lock(&event_mutex);
964 rcu_read_lock_sched();
966 pid_list = rcu_dereference_sched(tr->filtered_pids);
968 if (!pid_list || *pos >= pid_list->nr_pids)
971 return (void *)&pid_list->pids[*pos];
974 static void p_stop(struct seq_file *m, void *p)
977 rcu_read_unlock_sched();
978 mutex_unlock(&event_mutex);
982 p_next(struct seq_file *m, void *v, loff_t *pos)
984 struct trace_array *tr = m->private;
985 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
989 if (*pos >= pid_list->nr_pids)
992 return (void *)&pid_list->pids[*pos];
995 static int p_show(struct seq_file *m, void *v)
999 seq_printf(m, "%d\n", *pid);
1004 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1007 struct trace_event_file *file;
1008 unsigned long flags;
1011 mutex_lock(&event_mutex);
1012 file = event_file_data(filp);
1014 flags = file->flags;
1015 mutex_unlock(&event_mutex);
1020 if (flags & EVENT_FILE_FL_ENABLED &&
1021 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1024 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1025 flags & EVENT_FILE_FL_SOFT_MODE)
1030 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1034 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1037 struct trace_event_file *file;
1041 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1045 ret = tracing_update_buffers();
1053 mutex_lock(&event_mutex);
1054 file = event_file_data(filp);
1056 ret = ftrace_event_enable_disable(file, val);
1057 mutex_unlock(&event_mutex);
1066 return ret ? ret : cnt;
1070 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1073 const char set_to_char[4] = { '?', '0', '1', 'X' };
1074 struct trace_subsystem_dir *dir = filp->private_data;
1075 struct event_subsystem *system = dir->subsystem;
1076 struct trace_event_call *call;
1077 struct trace_event_file *file;
1078 struct trace_array *tr = dir->tr;
1083 mutex_lock(&event_mutex);
1084 list_for_each_entry(file, &tr->events, list) {
1085 call = file->event_call;
1086 if (!trace_event_name(call) || !call->class || !call->class->reg)
1089 if (system && strcmp(call->class->system, system->name) != 0)
1093 * We need to find out if all the events are set
1094 * or if all events or cleared, or if we have
1097 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1100 * If we have a mixture, no need to look further.
1105 mutex_unlock(&event_mutex);
1107 buf[0] = set_to_char[set];
1110 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1116 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1119 struct trace_subsystem_dir *dir = filp->private_data;
1120 struct event_subsystem *system = dir->subsystem;
1121 const char *name = NULL;
1125 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1129 ret = tracing_update_buffers();
1133 if (val != 0 && val != 1)
1137 * Opening of "enable" adds a ref count to system,
1138 * so the name is safe to use.
1141 name = system->name;
1143 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1157 FORMAT_FIELD_SEPERATOR = 2,
1158 FORMAT_PRINTFMT = 3,
1161 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1163 struct trace_event_call *call = event_file_data(m->private);
1164 struct list_head *common_head = &ftrace_common_fields;
1165 struct list_head *head = trace_get_fields(call);
1166 struct list_head *node = v;
1170 switch ((unsigned long)v) {
1175 case FORMAT_FIELD_SEPERATOR:
1179 case FORMAT_PRINTFMT:
1185 if (node == common_head)
1186 return (void *)FORMAT_FIELD_SEPERATOR;
1187 else if (node == head)
1188 return (void *)FORMAT_PRINTFMT;
1193 static int f_show(struct seq_file *m, void *v)
1195 struct trace_event_call *call = event_file_data(m->private);
1196 struct ftrace_event_field *field;
1197 const char *array_descriptor;
1199 switch ((unsigned long)v) {
1201 seq_printf(m, "name: %s\n", trace_event_name(call));
1202 seq_printf(m, "ID: %d\n", call->event.type);
1203 seq_puts(m, "format:\n");
1206 case FORMAT_FIELD_SEPERATOR:
1210 case FORMAT_PRINTFMT:
1211 seq_printf(m, "\nprint fmt: %s\n",
1216 field = list_entry(v, struct ftrace_event_field, link);
1218 * Smartly shows the array type(except dynamic array).
1221 * If TYPE := TYPE[LEN], it is shown:
1222 * field:TYPE VAR[LEN]
1224 array_descriptor = strchr(field->type, '[');
1226 if (!strncmp(field->type, "__data_loc", 10))
1227 array_descriptor = NULL;
1229 if (!array_descriptor)
1230 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1231 field->type, field->name, field->offset,
1232 field->size, !!field->is_signed);
1234 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1235 (int)(array_descriptor - field->type),
1236 field->type, field->name,
1237 array_descriptor, field->offset,
1238 field->size, !!field->is_signed);
1243 static void *f_start(struct seq_file *m, loff_t *pos)
1245 void *p = (void *)FORMAT_HEADER;
1248 /* ->stop() is called even if ->start() fails */
1249 mutex_lock(&event_mutex);
1250 if (!event_file_data(m->private))
1251 return ERR_PTR(-ENODEV);
1253 while (l < *pos && p)
1254 p = f_next(m, p, &l);
1259 static void f_stop(struct seq_file *m, void *p)
1261 mutex_unlock(&event_mutex);
1264 static const struct seq_operations trace_format_seq_ops = {
1271 static int trace_format_open(struct inode *inode, struct file *file)
1276 ret = seq_open(file, &trace_format_seq_ops);
1280 m = file->private_data;
1287 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1289 int id = (long)event_file_data(filp);
1299 len = sprintf(buf, "%d\n", id);
1301 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1305 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1308 struct trace_event_file *file;
1309 struct trace_seq *s;
1315 s = kmalloc(sizeof(*s), GFP_KERNEL);
1322 mutex_lock(&event_mutex);
1323 file = event_file_data(filp);
1325 print_event_filter(file, s);
1326 mutex_unlock(&event_mutex);
1329 r = simple_read_from_buffer(ubuf, cnt, ppos,
1330 s->buffer, trace_seq_used(s));
1338 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1341 struct trace_event_file *file;
1345 if (cnt >= PAGE_SIZE)
1348 buf = (char *)__get_free_page(GFP_TEMPORARY);
1352 if (copy_from_user(buf, ubuf, cnt)) {
1353 free_page((unsigned long) buf);
1358 mutex_lock(&event_mutex);
1359 file = event_file_data(filp);
1361 err = apply_event_filter(file, buf);
1362 mutex_unlock(&event_mutex);
1364 free_page((unsigned long) buf);
1373 static LIST_HEAD(event_subsystems);
1375 static int subsystem_open(struct inode *inode, struct file *filp)
1377 struct event_subsystem *system = NULL;
1378 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1379 struct trace_array *tr;
1382 if (tracing_is_disabled())
1385 /* Make sure the system still exists */
1386 mutex_lock(&trace_types_lock);
1387 mutex_lock(&event_mutex);
1388 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1389 list_for_each_entry(dir, &tr->systems, list) {
1390 if (dir == inode->i_private) {
1391 /* Don't open systems with no events */
1392 if (dir->nr_events) {
1393 __get_system_dir(dir);
1394 system = dir->subsystem;
1401 mutex_unlock(&event_mutex);
1402 mutex_unlock(&trace_types_lock);
1407 /* Some versions of gcc think dir can be uninitialized here */
1410 /* Still need to increment the ref count of the system */
1411 if (trace_array_get(tr) < 0) {
1416 ret = tracing_open_generic(inode, filp);
1418 trace_array_put(tr);
1425 static int system_tr_open(struct inode *inode, struct file *filp)
1427 struct trace_subsystem_dir *dir;
1428 struct trace_array *tr = inode->i_private;
1431 if (tracing_is_disabled())
1434 if (trace_array_get(tr) < 0)
1437 /* Make a temporary dir that has no system but points to tr */
1438 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1440 trace_array_put(tr);
1446 ret = tracing_open_generic(inode, filp);
1448 trace_array_put(tr);
1453 filp->private_data = dir;
1458 static int subsystem_release(struct inode *inode, struct file *file)
1460 struct trace_subsystem_dir *dir = file->private_data;
1462 trace_array_put(dir->tr);
1465 * If dir->subsystem is NULL, then this is a temporary
1466 * descriptor that was made for a trace_array to enable
1478 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1481 struct trace_subsystem_dir *dir = filp->private_data;
1482 struct event_subsystem *system = dir->subsystem;
1483 struct trace_seq *s;
1489 s = kmalloc(sizeof(*s), GFP_KERNEL);
1495 print_subsystem_event_filter(system, s);
1496 r = simple_read_from_buffer(ubuf, cnt, ppos,
1497 s->buffer, trace_seq_used(s));
1505 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1508 struct trace_subsystem_dir *dir = filp->private_data;
1512 if (cnt >= PAGE_SIZE)
1515 buf = (char *)__get_free_page(GFP_TEMPORARY);
1519 if (copy_from_user(buf, ubuf, cnt)) {
1520 free_page((unsigned long) buf);
1525 err = apply_subsystem_event_filter(dir, buf);
1526 free_page((unsigned long) buf);
1536 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1538 int (*func)(struct trace_seq *s) = filp->private_data;
1539 struct trace_seq *s;
1545 s = kmalloc(sizeof(*s), GFP_KERNEL);
1552 r = simple_read_from_buffer(ubuf, cnt, ppos,
1553 s->buffer, trace_seq_used(s));
1560 static int max_pids(struct trace_pid_list *pid_list)
1562 return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
1565 static void ignore_task_cpu(void *data)
1567 struct trace_array *tr = data;
1568 struct trace_pid_list *pid_list;
1571 * This function is called by on_each_cpu() while the
1572 * event_mutex is held.
1574 pid_list = rcu_dereference_protected(tr->filtered_pids,
1575 mutex_is_locked(&event_mutex));
1577 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1578 check_ignore_pid(pid_list, current));
1582 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1583 size_t cnt, loff_t *ppos)
1585 struct seq_file *m = filp->private_data;
1586 struct trace_array *tr = m->private;
1587 struct trace_pid_list *filtered_pids = NULL;
1588 struct trace_pid_list *pid_list = NULL;
1589 struct trace_event_file *file;
1590 struct trace_parser parser;
1601 ret = tracing_update_buffers();
1605 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1608 mutex_lock(&event_mutex);
1610 * Load as many pids into the array before doing a
1611 * swap from the tr->filtered_pids to the new list.
1617 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1618 if (ret < 0 || !trace_parser_loaded(&parser))
1625 parser.buffer[parser.idx] = 0;
1628 if (kstrtoul(parser.buffer, 0, &val))
1637 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1641 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1642 lockdep_is_held(&event_mutex));
1644 pid_list->order = filtered_pids->order;
1646 pid_list->order = 0;
1648 pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
1650 if (!pid_list->pids)
1653 if (filtered_pids) {
1654 pid_list->nr_pids = filtered_pids->nr_pids;
1655 memcpy(pid_list->pids, filtered_pids->pids,
1656 pid_list->nr_pids * sizeof(pid_t));
1658 pid_list->nr_pids = 0;
1661 if (pid_list->nr_pids >= max_pids(pid_list)) {
1664 pid_page = (void *)__get_free_pages(GFP_KERNEL,
1665 pid_list->order + 1);
1668 memcpy(pid_page, pid_list->pids,
1669 pid_list->nr_pids * sizeof(pid_t));
1670 free_pages((unsigned long)pid_list->pids, pid_list->order);
1673 pid_list->pids = pid_page;
1676 pid_list->pids[pid_list->nr_pids++] = pid;
1677 trace_parser_clear(&parser);
1680 trace_parser_put(&parser);
1684 free_pages((unsigned long)pid_list->pids, pid_list->order);
1686 mutex_unlock(&event_mutex);
1691 mutex_unlock(&event_mutex);
1695 sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
1697 /* Remove duplicates */
1698 for (i = 1; i < pid_list->nr_pids; i++) {
1701 while (i < pid_list->nr_pids &&
1702 pid_list->pids[i - 1] == pid_list->pids[i])
1706 if (i < pid_list->nr_pids) {
1707 memmove(&pid_list->pids[start], &pid_list->pids[i],
1708 (pid_list->nr_pids - i) * sizeof(pid_t));
1709 pid_list->nr_pids -= i - start;
1712 pid_list->nr_pids = start;
1716 rcu_assign_pointer(tr->filtered_pids, pid_list);
1718 list_for_each_entry(file, &tr->events, list) {
1719 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1722 if (filtered_pids) {
1723 synchronize_sched();
1725 free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
1726 kfree(filtered_pids);
1729 * Register a probe that is called before all other probes
1730 * to set ignore_pid if next or prev do not match.
1731 * Register a probe this is called after all other probes
1732 * to only keep ignore_pid set if next pid matches.
1734 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1736 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1739 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1741 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1744 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1746 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1749 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1751 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1756 * Ignoring of pids is done at task switch. But we have to
1757 * check for those tasks that are currently running.
1758 * Always do this in case a pid was appended or removed.
1760 on_each_cpu(ignore_task_cpu, tr, 1);
1762 mutex_unlock(&event_mutex);
1770 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1771 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1772 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1773 static int ftrace_event_release(struct inode *inode, struct file *file);
1775 static const struct seq_operations show_event_seq_ops = {
1782 static const struct seq_operations show_set_event_seq_ops = {
1789 static const struct seq_operations show_set_pid_seq_ops = {
1796 static const struct file_operations ftrace_avail_fops = {
1797 .open = ftrace_event_avail_open,
1799 .llseek = seq_lseek,
1800 .release = seq_release,
1803 static const struct file_operations ftrace_set_event_fops = {
1804 .open = ftrace_event_set_open,
1806 .write = ftrace_event_write,
1807 .llseek = seq_lseek,
1808 .release = ftrace_event_release,
1811 static const struct file_operations ftrace_set_event_pid_fops = {
1812 .open = ftrace_event_set_pid_open,
1814 .write = ftrace_event_pid_write,
1815 .llseek = seq_lseek,
1816 .release = ftrace_event_release,
1819 static const struct file_operations ftrace_enable_fops = {
1820 .open = tracing_open_generic,
1821 .read = event_enable_read,
1822 .write = event_enable_write,
1823 .llseek = default_llseek,
1826 static const struct file_operations ftrace_event_format_fops = {
1827 .open = trace_format_open,
1829 .llseek = seq_lseek,
1830 .release = seq_release,
1833 static const struct file_operations ftrace_event_id_fops = {
1834 .read = event_id_read,
1835 .llseek = default_llseek,
1838 static const struct file_operations ftrace_event_filter_fops = {
1839 .open = tracing_open_generic,
1840 .read = event_filter_read,
1841 .write = event_filter_write,
1842 .llseek = default_llseek,
1845 static const struct file_operations ftrace_subsystem_filter_fops = {
1846 .open = subsystem_open,
1847 .read = subsystem_filter_read,
1848 .write = subsystem_filter_write,
1849 .llseek = default_llseek,
1850 .release = subsystem_release,
1853 static const struct file_operations ftrace_system_enable_fops = {
1854 .open = subsystem_open,
1855 .read = system_enable_read,
1856 .write = system_enable_write,
1857 .llseek = default_llseek,
1858 .release = subsystem_release,
1861 static const struct file_operations ftrace_tr_enable_fops = {
1862 .open = system_tr_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_show_header_fops = {
1870 .open = tracing_open_generic,
1871 .read = show_header,
1872 .llseek = default_llseek,
1876 ftrace_event_open(struct inode *inode, struct file *file,
1877 const struct seq_operations *seq_ops)
1882 ret = seq_open(file, seq_ops);
1885 m = file->private_data;
1886 /* copy tr over to seq ops */
1887 m->private = inode->i_private;
1892 static int ftrace_event_release(struct inode *inode, struct file *file)
1894 struct trace_array *tr = inode->i_private;
1896 trace_array_put(tr);
1898 return seq_release(inode, file);
1902 ftrace_event_avail_open(struct inode *inode, struct file *file)
1904 const struct seq_operations *seq_ops = &show_event_seq_ops;
1906 return ftrace_event_open(inode, file, seq_ops);
1910 ftrace_event_set_open(struct inode *inode, struct file *file)
1912 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1913 struct trace_array *tr = inode->i_private;
1916 if (trace_array_get(tr) < 0)
1919 if ((file->f_mode & FMODE_WRITE) &&
1920 (file->f_flags & O_TRUNC))
1921 ftrace_clear_events(tr);
1923 ret = ftrace_event_open(inode, file, seq_ops);
1925 trace_array_put(tr);
1930 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1932 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1933 struct trace_array *tr = inode->i_private;
1936 if (trace_array_get(tr) < 0)
1939 if ((file->f_mode & FMODE_WRITE) &&
1940 (file->f_flags & O_TRUNC))
1941 ftrace_clear_event_pids(tr);
1943 ret = ftrace_event_open(inode, file, seq_ops);
1945 trace_array_put(tr);
1949 static struct event_subsystem *
1950 create_new_subsystem(const char *name)
1952 struct event_subsystem *system;
1954 /* need to create new entry */
1955 system = kmalloc(sizeof(*system), GFP_KERNEL);
1959 system->ref_count = 1;
1961 /* Only allocate if dynamic (kprobes and modules) */
1962 system->name = kstrdup_const(name, GFP_KERNEL);
1966 system->filter = NULL;
1968 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1969 if (!system->filter)
1972 list_add(&system->list, &event_subsystems);
1977 kfree_const(system->name);
1982 static struct dentry *
1983 event_subsystem_dir(struct trace_array *tr, const char *name,
1984 struct trace_event_file *file, struct dentry *parent)
1986 struct trace_subsystem_dir *dir;
1987 struct event_subsystem *system;
1988 struct dentry *entry;
1990 /* First see if we did not already create this dir */
1991 list_for_each_entry(dir, &tr->systems, list) {
1992 system = dir->subsystem;
1993 if (strcmp(system->name, name) == 0) {
2000 /* Now see if the system itself exists. */
2001 list_for_each_entry(system, &event_subsystems, list) {
2002 if (strcmp(system->name, name) == 0)
2005 /* Reset system variable when not found */
2006 if (&system->list == &event_subsystems)
2009 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2014 system = create_new_subsystem(name);
2018 __get_system(system);
2020 dir->entry = tracefs_create_dir(name, parent);
2022 pr_warn("Failed to create system directory %s\n", name);
2023 __put_system(system);
2030 dir->subsystem = system;
2033 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2034 &ftrace_subsystem_filter_fops);
2036 kfree(system->filter);
2037 system->filter = NULL;
2038 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2041 trace_create_file("enable", 0644, dir->entry, dir,
2042 &ftrace_system_enable_fops);
2044 list_add(&dir->list, &tr->systems);
2051 /* Only print this message if failed on memory allocation */
2052 if (!dir || !system)
2053 pr_warn("No memory to create event subsystem %s\n", name);
2058 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2060 struct trace_event_call *call = file->event_call;
2061 struct trace_array *tr = file->tr;
2062 struct list_head *head;
2063 struct dentry *d_events;
2068 * If the trace point header did not define TRACE_SYSTEM
2069 * then the system would be called "TRACE_SYSTEM".
2071 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2072 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2078 name = trace_event_name(call);
2079 file->dir = tracefs_create_dir(name, d_events);
2081 pr_warn("Could not create tracefs '%s' directory\n", name);
2085 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2086 trace_create_file("enable", 0644, file->dir, file,
2087 &ftrace_enable_fops);
2089 #ifdef CONFIG_PERF_EVENTS
2090 if (call->event.type && call->class->reg)
2091 trace_create_file("id", 0444, file->dir,
2092 (void *)(long)call->event.type,
2093 &ftrace_event_id_fops);
2097 * Other events may have the same class. Only update
2098 * the fields if they are not already defined.
2100 head = trace_get_fields(call);
2101 if (list_empty(head)) {
2102 ret = call->class->define_fields(call);
2104 pr_warn("Could not initialize trace point events/%s\n",
2109 trace_create_file("filter", 0644, file->dir, file,
2110 &ftrace_event_filter_fops);
2112 trace_create_file("trigger", 0644, file->dir, file,
2113 &event_trigger_fops);
2115 trace_create_file("format", 0444, file->dir, call,
2116 &ftrace_event_format_fops);
2121 static void remove_event_from_tracers(struct trace_event_call *call)
2123 struct trace_event_file *file;
2124 struct trace_array *tr;
2126 do_for_each_event_file_safe(tr, file) {
2127 if (file->event_call != call)
2130 remove_event_file_dir(file);
2132 * The do_for_each_event_file_safe() is
2133 * a double loop. After finding the call for this
2134 * trace_array, we use break to jump to the next
2138 } while_for_each_event_file();
2141 static void event_remove(struct trace_event_call *call)
2143 struct trace_array *tr;
2144 struct trace_event_file *file;
2146 do_for_each_event_file(tr, file) {
2147 if (file->event_call != call)
2149 ftrace_event_enable_disable(file, 0);
2151 * The do_for_each_event_file() is
2152 * a double loop. After finding the call for this
2153 * trace_array, we use break to jump to the next
2157 } while_for_each_event_file();
2159 if (call->event.funcs)
2160 __unregister_trace_event(&call->event);
2161 remove_event_from_tracers(call);
2162 list_del(&call->list);
2165 static int event_init(struct trace_event_call *call)
2170 name = trace_event_name(call);
2174 if (call->class->raw_init) {
2175 ret = call->class->raw_init(call);
2176 if (ret < 0 && ret != -ENOSYS)
2177 pr_warn("Could not initialize trace events/%s\n", name);
2184 __register_event(struct trace_event_call *call, struct module *mod)
2188 ret = event_init(call);
2192 list_add(&call->list, &ftrace_events);
2198 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2203 /* Find the length of the enum value as a string */
2204 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2205 /* Make sure there's enough room to replace the string with the value */
2209 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2211 /* Get the rest of the string of ptr */
2212 rlen = strlen(ptr + len);
2213 memmove(ptr + elen, ptr + len, rlen);
2214 /* Make sure we end the new string */
2215 ptr[elen + rlen] = 0;
2220 static void update_event_printk(struct trace_event_call *call,
2221 struct trace_enum_map *map)
2225 int len = strlen(map->enum_string);
2227 for (ptr = call->print_fmt; *ptr; ptr++) {
2241 if (isdigit(*ptr)) {
2245 /* Check for alpha chars like ULL */
2246 } while (isalnum(*ptr));
2250 * A number must have some kind of delimiter after
2251 * it, and we can ignore that too.
2255 if (isalpha(*ptr) || *ptr == '_') {
2256 if (strncmp(map->enum_string, ptr, len) == 0 &&
2257 !isalnum(ptr[len]) && ptr[len] != '_') {
2258 ptr = enum_replace(ptr, map, len);
2259 /* Hmm, enum string smaller than value */
2260 if (WARN_ON_ONCE(!ptr))
2263 * No need to decrement here, as enum_replace()
2264 * returns the pointer to the character passed
2265 * the enum, and two enums can not be placed
2266 * back to back without something in between.
2267 * We can skip that something in between.
2274 } while (isalnum(*ptr) || *ptr == '_');
2278 * If what comes after this variable is a '.' or
2279 * '->' then we can continue to ignore that string.
2281 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2282 ptr += *ptr == '.' ? 1 : 2;
2288 * Once again, we can skip the delimiter that came
2296 void trace_event_enum_update(struct trace_enum_map **map, int len)
2298 struct trace_event_call *call, *p;
2299 const char *last_system = NULL;
2303 down_write(&trace_event_sem);
2304 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2305 /* events are usually grouped together with systems */
2306 if (!last_system || call->class->system != last_system) {
2308 last_system = call->class->system;
2311 for (i = last_i; i < len; i++) {
2312 if (call->class->system == map[i]->system) {
2313 /* Save the first system if need be */
2316 update_event_printk(call, map[i]);
2320 up_write(&trace_event_sem);
2323 static struct trace_event_file *
2324 trace_create_new_event(struct trace_event_call *call,
2325 struct trace_array *tr)
2327 struct trace_event_file *file;
2329 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2333 file->event_call = call;
2335 atomic_set(&file->sm_ref, 0);
2336 atomic_set(&file->tm_ref, 0);
2337 INIT_LIST_HEAD(&file->triggers);
2338 list_add(&file->list, &tr->events);
2343 /* Add an event to a trace directory */
2345 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2347 struct trace_event_file *file;
2349 file = trace_create_new_event(call, tr);
2353 return event_create_dir(tr->event_dir, file);
2357 * Just create a decriptor for early init. A descriptor is required
2358 * for enabling events at boot. We want to enable events before
2359 * the filesystem is initialized.
2362 __trace_early_add_new_event(struct trace_event_call *call,
2363 struct trace_array *tr)
2365 struct trace_event_file *file;
2367 file = trace_create_new_event(call, tr);
2374 struct ftrace_module_file_ops;
2375 static void __add_event_to_tracers(struct trace_event_call *call);
2377 /* Add an additional event_call dynamically */
2378 int trace_add_event_call(struct trace_event_call *call)
2381 mutex_lock(&trace_types_lock);
2382 mutex_lock(&event_mutex);
2384 ret = __register_event(call, NULL);
2386 __add_event_to_tracers(call);
2388 mutex_unlock(&event_mutex);
2389 mutex_unlock(&trace_types_lock);
2394 * Must be called under locking of trace_types_lock, event_mutex and
2397 static void __trace_remove_event_call(struct trace_event_call *call)
2400 trace_destroy_fields(call);
2401 free_event_filter(call->filter);
2402 call->filter = NULL;
2405 static int probe_remove_event_call(struct trace_event_call *call)
2407 struct trace_array *tr;
2408 struct trace_event_file *file;
2410 #ifdef CONFIG_PERF_EVENTS
2411 if (call->perf_refcount)
2414 do_for_each_event_file(tr, file) {
2415 if (file->event_call != call)
2418 * We can't rely on ftrace_event_enable_disable(enable => 0)
2419 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2420 * TRACE_REG_UNREGISTER.
2422 if (file->flags & EVENT_FILE_FL_ENABLED)
2425 * The do_for_each_event_file_safe() is
2426 * a double loop. After finding the call for this
2427 * trace_array, we use break to jump to the next
2431 } while_for_each_event_file();
2433 __trace_remove_event_call(call);
2438 /* Remove an event_call */
2439 int trace_remove_event_call(struct trace_event_call *call)
2443 mutex_lock(&trace_types_lock);
2444 mutex_lock(&event_mutex);
2445 down_write(&trace_event_sem);
2446 ret = probe_remove_event_call(call);
2447 up_write(&trace_event_sem);
2448 mutex_unlock(&event_mutex);
2449 mutex_unlock(&trace_types_lock);
2454 #define for_each_event(event, start, end) \
2455 for (event = start; \
2456 (unsigned long)event < (unsigned long)end; \
2459 #ifdef CONFIG_MODULES
2461 static void trace_module_add_events(struct module *mod)
2463 struct trace_event_call **call, **start, **end;
2465 if (!mod->num_trace_events)
2468 /* Don't add infrastructure for mods without tracepoints */
2469 if (trace_module_has_bad_taint(mod)) {
2470 pr_err("%s: module has bad taint, not creating trace events\n",
2475 start = mod->trace_events;
2476 end = mod->trace_events + mod->num_trace_events;
2478 for_each_event(call, start, end) {
2479 __register_event(*call, mod);
2480 __add_event_to_tracers(*call);
2484 static void trace_module_remove_events(struct module *mod)
2486 struct trace_event_call *call, *p;
2487 bool clear_trace = false;
2489 down_write(&trace_event_sem);
2490 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2491 if (call->mod == mod) {
2492 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2494 __trace_remove_event_call(call);
2497 up_write(&trace_event_sem);
2500 * It is safest to reset the ring buffer if the module being unloaded
2501 * registered any events that were used. The only worry is if
2502 * a new module gets loaded, and takes on the same id as the events
2503 * of this module. When printing out the buffer, traced events left
2504 * over from this module may be passed to the new module events and
2505 * unexpected results may occur.
2508 tracing_reset_all_online_cpus();
2511 static int trace_module_notify(struct notifier_block *self,
2512 unsigned long val, void *data)
2514 struct module *mod = data;
2516 mutex_lock(&trace_types_lock);
2517 mutex_lock(&event_mutex);
2519 case MODULE_STATE_COMING:
2520 trace_module_add_events(mod);
2522 case MODULE_STATE_GOING:
2523 trace_module_remove_events(mod);
2526 mutex_unlock(&event_mutex);
2527 mutex_unlock(&trace_types_lock);
2532 static struct notifier_block trace_module_nb = {
2533 .notifier_call = trace_module_notify,
2534 .priority = 1, /* higher than trace.c module notify */
2536 #endif /* CONFIG_MODULES */
2538 /* Create a new event directory structure for a trace directory. */
2540 __trace_add_event_dirs(struct trace_array *tr)
2542 struct trace_event_call *call;
2545 list_for_each_entry(call, &ftrace_events, list) {
2546 ret = __trace_add_new_event(call, tr);
2548 pr_warn("Could not create directory for event %s\n",
2549 trace_event_name(call));
2553 struct trace_event_file *
2554 find_event_file(struct trace_array *tr, const char *system, const char *event)
2556 struct trace_event_file *file;
2557 struct trace_event_call *call;
2560 list_for_each_entry(file, &tr->events, list) {
2562 call = file->event_call;
2563 name = trace_event_name(call);
2565 if (!name || !call->class || !call->class->reg)
2568 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2571 if (strcmp(event, name) == 0 &&
2572 strcmp(system, call->class->system) == 0)
2578 #ifdef CONFIG_DYNAMIC_FTRACE
2581 #define ENABLE_EVENT_STR "enable_event"
2582 #define DISABLE_EVENT_STR "disable_event"
2584 struct event_probe_data {
2585 struct trace_event_file *file;
2586 unsigned long count;
2592 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2594 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2595 struct event_probe_data *data = *pdata;
2601 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2603 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2607 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2609 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2610 struct event_probe_data *data = *pdata;
2618 /* Skip if the event is in a state we want to switch to */
2619 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2622 if (data->count != -1)
2625 event_enable_probe(ip, parent_ip, _data);
2629 event_enable_print(struct seq_file *m, unsigned long ip,
2630 struct ftrace_probe_ops *ops, void *_data)
2632 struct event_probe_data *data = _data;
2634 seq_printf(m, "%ps:", (void *)ip);
2636 seq_printf(m, "%s:%s:%s",
2637 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2638 data->file->event_call->class->system,
2639 trace_event_name(data->file->event_call));
2641 if (data->count == -1)
2642 seq_puts(m, ":unlimited\n");
2644 seq_printf(m, ":count=%ld\n", data->count);
2650 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2653 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2654 struct event_probe_data *data = *pdata;
2661 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2664 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2665 struct event_probe_data *data = *pdata;
2667 if (WARN_ON_ONCE(data->ref <= 0))
2672 /* Remove the SOFT_MODE flag */
2673 __ftrace_event_enable_disable(data->file, 0, 1);
2674 module_put(data->file->event_call->mod);
2680 static struct ftrace_probe_ops event_enable_probe_ops = {
2681 .func = event_enable_probe,
2682 .print = event_enable_print,
2683 .init = event_enable_init,
2684 .free = event_enable_free,
2687 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2688 .func = event_enable_count_probe,
2689 .print = event_enable_print,
2690 .init = event_enable_init,
2691 .free = event_enable_free,
2694 static struct ftrace_probe_ops event_disable_probe_ops = {
2695 .func = event_enable_probe,
2696 .print = event_enable_print,
2697 .init = event_enable_init,
2698 .free = event_enable_free,
2701 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2702 .func = event_enable_count_probe,
2703 .print = event_enable_print,
2704 .init = event_enable_init,
2705 .free = event_enable_free,
2709 event_enable_func(struct ftrace_hash *hash,
2710 char *glob, char *cmd, char *param, int enabled)
2712 struct trace_array *tr = top_trace_array();
2713 struct trace_event_file *file;
2714 struct ftrace_probe_ops *ops;
2715 struct event_probe_data *data;
2725 /* hash funcs only work with set_ftrace_filter */
2726 if (!enabled || !param)
2729 system = strsep(¶m, ":");
2733 event = strsep(¶m, ":");
2735 mutex_lock(&event_mutex);
2738 file = find_event_file(tr, system, event);
2742 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2745 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2747 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2749 if (glob[0] == '!') {
2750 unregister_ftrace_function_probe_func(glob+1, ops);
2756 data = kzalloc(sizeof(*data), GFP_KERNEL);
2760 data->enable = enable;
2767 number = strsep(¶m, ":");
2770 if (!strlen(number))
2774 * We use the callback data field (which is a pointer)
2777 ret = kstrtoul(number, 0, &data->count);
2782 /* Don't let event modules unload while probe registered */
2783 ret = try_module_get(file->event_call->mod);
2789 ret = __ftrace_event_enable_disable(file, 1, 1);
2792 ret = register_ftrace_function_probe(glob, ops, data);
2794 * The above returns on success the # of functions enabled,
2795 * but if it didn't find any functions it returns zero.
2796 * Consider no functions a failure too.
2803 /* Just return zero, not the number of enabled functions */
2806 mutex_unlock(&event_mutex);
2810 __ftrace_event_enable_disable(file, 0, 1);
2812 module_put(file->event_call->mod);
2818 static struct ftrace_func_command event_enable_cmd = {
2819 .name = ENABLE_EVENT_STR,
2820 .func = event_enable_func,
2823 static struct ftrace_func_command event_disable_cmd = {
2824 .name = DISABLE_EVENT_STR,
2825 .func = event_enable_func,
2828 static __init int register_event_cmds(void)
2832 ret = register_ftrace_command(&event_enable_cmd);
2833 if (WARN_ON(ret < 0))
2835 ret = register_ftrace_command(&event_disable_cmd);
2836 if (WARN_ON(ret < 0))
2837 unregister_ftrace_command(&event_enable_cmd);
2841 static inline int register_event_cmds(void) { return 0; }
2842 #endif /* CONFIG_DYNAMIC_FTRACE */
2845 * The top level array has already had its trace_event_file
2846 * descriptors created in order to allow for early events to
2847 * be recorded. This function is called after the tracefs has been
2848 * initialized, and we now have to create the files associated
2852 __trace_early_add_event_dirs(struct trace_array *tr)
2854 struct trace_event_file *file;
2858 list_for_each_entry(file, &tr->events, list) {
2859 ret = event_create_dir(tr->event_dir, file);
2861 pr_warn("Could not create directory for event %s\n",
2862 trace_event_name(file->event_call));
2867 * For early boot up, the top trace array requires to have
2868 * a list of events that can be enabled. This must be done before
2869 * the filesystem is set up in order to allow events to be traced
2873 __trace_early_add_events(struct trace_array *tr)
2875 struct trace_event_call *call;
2878 list_for_each_entry(call, &ftrace_events, list) {
2879 /* Early boot up should not have any modules loaded */
2880 if (WARN_ON_ONCE(call->mod))
2883 ret = __trace_early_add_new_event(call, tr);
2885 pr_warn("Could not create early event %s\n",
2886 trace_event_name(call));
2890 /* Remove the event directory structure for a trace directory. */
2892 __trace_remove_event_dirs(struct trace_array *tr)
2894 struct trace_event_file *file, *next;
2896 list_for_each_entry_safe(file, next, &tr->events, list)
2897 remove_event_file_dir(file);
2900 static void __add_event_to_tracers(struct trace_event_call *call)
2902 struct trace_array *tr;
2904 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2905 __trace_add_new_event(call, tr);
2908 extern struct trace_event_call *__start_ftrace_events[];
2909 extern struct trace_event_call *__stop_ftrace_events[];
2911 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2913 static __init int setup_trace_event(char *str)
2915 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2916 ring_buffer_expanded = true;
2917 tracing_selftest_disabled = true;
2921 __setup("trace_event=", setup_trace_event);
2923 /* Expects to have event_mutex held when called */
2925 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2927 struct dentry *d_events;
2928 struct dentry *entry;
2930 entry = tracefs_create_file("set_event", 0644, parent,
2931 tr, &ftrace_set_event_fops);
2933 pr_warn("Could not create tracefs 'set_event' entry\n");
2937 d_events = tracefs_create_dir("events", parent);
2939 pr_warn("Could not create tracefs 'events' directory\n");
2943 entry = tracefs_create_file("set_event_pid", 0644, parent,
2944 tr, &ftrace_set_event_pid_fops);
2946 /* ring buffer internal formats */
2947 trace_create_file("header_page", 0444, d_events,
2948 ring_buffer_print_page_header,
2949 &ftrace_show_header_fops);
2951 trace_create_file("header_event", 0444, d_events,
2952 ring_buffer_print_entry_header,
2953 &ftrace_show_header_fops);
2955 trace_create_file("enable", 0644, d_events,
2956 tr, &ftrace_tr_enable_fops);
2958 tr->event_dir = d_events;
2964 * event_trace_add_tracer - add a instance of a trace_array to events
2965 * @parent: The parent dentry to place the files/directories for events in
2966 * @tr: The trace array associated with these events
2968 * When a new instance is created, it needs to set up its events
2969 * directory, as well as other files associated with events. It also
2970 * creates the event hierachry in the @parent/events directory.
2972 * Returns 0 on success.
2974 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2978 mutex_lock(&event_mutex);
2980 ret = create_event_toplevel_files(parent, tr);
2984 down_write(&trace_event_sem);
2985 __trace_add_event_dirs(tr);
2986 up_write(&trace_event_sem);
2989 mutex_unlock(&event_mutex);
2995 * The top trace array already had its file descriptors created.
2996 * Now the files themselves need to be created.
2999 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3003 mutex_lock(&event_mutex);
3005 ret = create_event_toplevel_files(parent, tr);
3009 down_write(&trace_event_sem);
3010 __trace_early_add_event_dirs(tr);
3011 up_write(&trace_event_sem);
3014 mutex_unlock(&event_mutex);
3019 int event_trace_del_tracer(struct trace_array *tr)
3021 mutex_lock(&event_mutex);
3023 /* Disable any event triggers and associated soft-disabled events */
3024 clear_event_triggers(tr);
3026 /* Clear the pid list */
3027 __ftrace_clear_event_pids(tr);
3029 /* Disable any running events */
3030 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3032 /* Access to events are within rcu_read_lock_sched() */
3033 synchronize_sched();
3035 down_write(&trace_event_sem);
3036 __trace_remove_event_dirs(tr);
3037 tracefs_remove_recursive(tr->event_dir);
3038 up_write(&trace_event_sem);
3040 tr->event_dir = NULL;
3042 mutex_unlock(&event_mutex);
3047 static __init int event_trace_memsetup(void)
3049 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3050 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3055 early_enable_events(struct trace_array *tr, bool disable_first)
3057 char *buf = bootup_event_buf;
3062 token = strsep(&buf, ",");
3068 /* Restarting syscalls requires that we stop them first */
3070 ftrace_set_clr_event(tr, token, 0);
3072 ret = ftrace_set_clr_event(tr, token, 1);
3074 pr_warn("Failed to enable trace event: %s\n", token);
3077 /* Put back the comma to allow this to be called again */
3083 static __init int event_trace_enable(void)
3085 struct trace_array *tr = top_trace_array();
3086 struct trace_event_call **iter, *call;
3092 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3095 ret = event_init(call);
3097 list_add(&call->list, &ftrace_events);
3101 * We need the top trace array to have a working set of trace
3102 * points at early init, before the debug files and directories
3103 * are created. Create the file entries now, and attach them
3104 * to the actual file dentries later.
3106 __trace_early_add_events(tr);
3108 early_enable_events(tr, false);
3110 trace_printk_start_comm();
3112 register_event_cmds();
3114 register_trigger_cmds();
3120 * event_trace_enable() is called from trace_event_init() first to
3121 * initialize events and perhaps start any events that are on the
3122 * command line. Unfortunately, there are some events that will not
3123 * start this early, like the system call tracepoints that need
3124 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3125 * is called before pid 1 starts, and this flag is never set, making
3126 * the syscall tracepoint never get reached, but the event is enabled
3127 * regardless (and not doing anything).
3129 static __init int event_trace_enable_again(void)
3131 struct trace_array *tr;
3133 tr = top_trace_array();
3137 early_enable_events(tr, true);
3142 early_initcall(event_trace_enable_again);
3144 static __init int event_trace_init(void)
3146 struct trace_array *tr;
3147 struct dentry *d_tracer;
3148 struct dentry *entry;
3151 tr = top_trace_array();
3155 d_tracer = tracing_init_dentry();
3156 if (IS_ERR(d_tracer))
3159 entry = tracefs_create_file("available_events", 0444, d_tracer,
3160 tr, &ftrace_avail_fops);
3162 pr_warn("Could not create tracefs 'available_events' entry\n");
3164 if (trace_define_generic_fields())
3165 pr_warn("tracing: Failed to allocated generic fields");
3167 if (trace_define_common_fields())
3168 pr_warn("tracing: Failed to allocate common fields");
3170 ret = early_event_add_tracer(d_tracer, tr);
3174 #ifdef CONFIG_MODULES
3175 ret = register_module_notifier(&trace_module_nb);
3177 pr_warn("Failed to register trace events module notifier\n");
3182 void __init trace_event_init(void)
3184 event_trace_memsetup();
3185 init_ftrace_syscalls();
3186 event_trace_enable();
3189 fs_initcall(event_trace_init);
3191 #ifdef CONFIG_FTRACE_STARTUP_TEST
3193 static DEFINE_SPINLOCK(test_spinlock);
3194 static DEFINE_SPINLOCK(test_spinlock_irq);
3195 static DEFINE_MUTEX(test_mutex);
3197 static __init void test_work(struct work_struct *dummy)
3199 spin_lock(&test_spinlock);
3200 spin_lock_irq(&test_spinlock_irq);
3202 spin_unlock_irq(&test_spinlock_irq);
3203 spin_unlock(&test_spinlock);
3205 mutex_lock(&test_mutex);
3207 mutex_unlock(&test_mutex);
3210 static __init int event_test_thread(void *unused)
3214 test_malloc = kmalloc(1234, GFP_KERNEL);
3216 pr_info("failed to kmalloc\n");
3218 schedule_on_each_cpu(test_work);
3222 set_current_state(TASK_INTERRUPTIBLE);
3223 while (!kthread_should_stop()) {
3225 set_current_state(TASK_INTERRUPTIBLE);
3227 __set_current_state(TASK_RUNNING);
3233 * Do various things that may trigger events.
3235 static __init void event_test_stuff(void)
3237 struct task_struct *test_thread;
3239 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3241 kthread_stop(test_thread);
3245 * For every trace event defined, we will test each trace point separately,
3246 * and then by groups, and finally all trace points.
3248 static __init void event_trace_self_tests(void)
3250 struct trace_subsystem_dir *dir;
3251 struct trace_event_file *file;
3252 struct trace_event_call *call;
3253 struct event_subsystem *system;
3254 struct trace_array *tr;
3257 tr = top_trace_array();
3261 pr_info("Running tests on trace events:\n");
3263 list_for_each_entry(file, &tr->events, list) {
3265 call = file->event_call;
3267 /* Only test those that have a probe */
3268 if (!call->class || !call->class->probe)
3272 * Testing syscall events here is pretty useless, but
3273 * we still do it if configured. But this is time consuming.
3274 * What we really need is a user thread to perform the
3275 * syscalls as we test.
3277 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3278 if (call->class->system &&
3279 strcmp(call->class->system, "syscalls") == 0)
3283 pr_info("Testing event %s: ", trace_event_name(call));
3286 * If an event is already enabled, someone is using
3287 * it and the self test should not be on.
3289 if (file->flags & EVENT_FILE_FL_ENABLED) {
3290 pr_warn("Enabled event during self test!\n");
3295 ftrace_event_enable_disable(file, 1);
3297 ftrace_event_enable_disable(file, 0);
3302 /* Now test at the sub system level */
3304 pr_info("Running tests on trace event systems:\n");
3306 list_for_each_entry(dir, &tr->systems, list) {
3308 system = dir->subsystem;
3310 /* the ftrace system is special, skip it */
3311 if (strcmp(system->name, "ftrace") == 0)
3314 pr_info("Testing event system %s: ", system->name);
3316 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3317 if (WARN_ON_ONCE(ret)) {
3318 pr_warn("error enabling system %s\n",
3325 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3326 if (WARN_ON_ONCE(ret)) {
3327 pr_warn("error disabling system %s\n",
3335 /* Test with all events enabled */
3337 pr_info("Running tests on all trace events:\n");
3338 pr_info("Testing all events: ");
3340 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3341 if (WARN_ON_ONCE(ret)) {
3342 pr_warn("error enabling all events\n");
3349 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3350 if (WARN_ON_ONCE(ret)) {
3351 pr_warn("error disabling all events\n");
3358 #ifdef CONFIG_FUNCTION_TRACER
3360 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3362 static struct trace_array *event_tr;
3365 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3366 struct ftrace_ops *op, struct pt_regs *pt_regs)
3368 struct ring_buffer_event *event;
3369 struct ring_buffer *buffer;
3370 struct ftrace_entry *entry;
3371 unsigned long flags;
3376 pc = preempt_count();
3377 preempt_disable_notrace();
3378 cpu = raw_smp_processor_id();
3379 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3384 local_save_flags(flags);
3386 event = trace_current_buffer_lock_reserve(&buffer,
3387 TRACE_FN, sizeof(*entry),
3391 entry = ring_buffer_event_data(event);
3393 entry->parent_ip = parent_ip;
3395 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
3398 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3399 preempt_enable_notrace();
3402 static struct ftrace_ops trace_ops __initdata =
3404 .func = function_test_events_call,
3405 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3408 static __init void event_trace_self_test_with_function(void)
3411 event_tr = top_trace_array();
3412 if (WARN_ON(!event_tr))
3414 ret = register_ftrace_function(&trace_ops);
3415 if (WARN_ON(ret < 0)) {
3416 pr_info("Failed to enable function tracer for event tests\n");
3419 pr_info("Running tests again, along with the function tracer\n");
3420 event_trace_self_tests();
3421 unregister_ftrace_function(&trace_ops);
3424 static __init void event_trace_self_test_with_function(void)
3429 static __init int event_trace_self_tests_init(void)
3431 if (!tracing_selftest_disabled) {
3432 event_trace_self_tests();
3433 event_trace_self_test_with_function();
3439 late_initcall(event_trace_self_tests_init);