4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #define pr_fmt(fmt) fmt
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/tracefs.h>
17 #include <linux/uaccess.h>
18 #include <linux/vmalloc.h>
19 #include <linux/module.h>
20 #include <linux/ctype.h>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
25 #include <trace/events/sched.h>
27 #include <asm/setup.h>
29 #include "trace_output.h"
32 #define TRACE_SYSTEM "TRACE_SYSTEM"
34 DEFINE_MUTEX(event_mutex);
36 LIST_HEAD(ftrace_events);
37 static LIST_HEAD(ftrace_generic_fields);
38 static LIST_HEAD(ftrace_common_fields);
40 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
42 static struct kmem_cache *field_cachep;
43 static struct kmem_cache *file_cachep;
45 static inline int system_refcount(struct event_subsystem *system)
47 return system->ref_count;
50 static int system_refcount_inc(struct event_subsystem *system)
52 return system->ref_count++;
55 static int system_refcount_dec(struct event_subsystem *system)
57 return --system->ref_count;
60 /* Double loops, do not use break, only goto's work */
61 #define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
65 #define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
67 struct trace_event_file *___n; \
68 list_for_each_entry_safe(file, ___n, &tr->events, list)
70 #define while_for_each_event_file() \
73 static struct list_head *
74 trace_get_fields(struct trace_event_call *event_call)
76 if (!event_call->class->get_fields)
77 return &event_call->class->fields;
78 return event_call->class->get_fields(event_call);
81 static struct ftrace_event_field *
82 __find_event_field(struct list_head *head, char *name)
84 struct ftrace_event_field *field;
86 list_for_each_entry(field, head, link) {
87 if (!strcmp(field->name, name))
94 struct ftrace_event_field *
95 trace_find_event_field(struct trace_event_call *call, char *name)
97 struct ftrace_event_field *field;
98 struct list_head *head;
100 head = trace_get_fields(call);
101 field = __find_event_field(head, name);
105 field = __find_event_field(&ftrace_generic_fields, name);
109 return __find_event_field(&ftrace_common_fields, name);
112 static int __trace_define_field(struct list_head *head, const char *type,
113 const char *name, int offset, int size,
114 int is_signed, int filter_type)
116 struct ftrace_event_field *field;
118 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
125 if (filter_type == FILTER_OTHER)
126 field->filter_type = filter_assign_type(type);
128 field->filter_type = filter_type;
130 field->offset = offset;
132 field->is_signed = is_signed;
134 list_add(&field->link, head);
139 int trace_define_field(struct trace_event_call *call, const char *type,
140 const char *name, int offset, int size, int is_signed,
143 struct list_head *head;
145 if (WARN_ON(!call->class))
148 head = trace_get_fields(call);
149 return __trace_define_field(head, type, name, offset, size,
150 is_signed, filter_type);
152 EXPORT_SYMBOL_GPL(trace_define_field);
154 #define __generic_field(type, item, filter_type) \
155 ret = __trace_define_field(&ftrace_generic_fields, #type, \
156 #item, 0, 0, is_signed_type(type), \
161 #define __common_field(type, item) \
162 ret = __trace_define_field(&ftrace_common_fields, #type, \
164 offsetof(typeof(ent), item), \
166 is_signed_type(type), FILTER_OTHER); \
170 static int trace_define_generic_fields(void)
174 __generic_field(int, CPU, FILTER_CPU);
175 __generic_field(int, cpu, FILTER_CPU);
176 __generic_field(char *, COMM, FILTER_COMM);
177 __generic_field(char *, comm, FILTER_COMM);
182 static int trace_define_common_fields(void)
185 struct trace_entry ent;
187 __common_field(unsigned short, type);
188 __common_field(unsigned char, flags);
189 __common_field(unsigned char, preempt_count);
190 __common_field(int, pid);
195 static void trace_destroy_fields(struct trace_event_call *call)
197 struct ftrace_event_field *field, *next;
198 struct list_head *head;
200 head = trace_get_fields(call);
201 list_for_each_entry_safe(field, next, head, link) {
202 list_del(&field->link);
203 kmem_cache_free(field_cachep, field);
207 int trace_event_raw_init(struct trace_event_call *call)
211 id = register_trace_event(&call->event);
217 EXPORT_SYMBOL_GPL(trace_event_raw_init);
219 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
221 struct trace_array *tr = trace_file->tr;
222 struct trace_array_cpu *data;
223 struct trace_pid_list *pid_list;
225 pid_list = rcu_dereference_sched(tr->filtered_pids);
229 data = this_cpu_ptr(tr->trace_buffer.data);
231 return data->ignore_pid;
233 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
235 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
236 struct trace_event_file *trace_file,
239 struct trace_event_call *event_call = trace_file->event_call;
241 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
242 trace_event_ignore_this_pid(trace_file))
245 local_save_flags(fbuffer->flags);
246 fbuffer->pc = preempt_count();
247 fbuffer->trace_file = trace_file;
250 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
251 event_call->event.type, len,
252 fbuffer->flags, fbuffer->pc);
256 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
257 return fbuffer->entry;
259 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
261 static DEFINE_SPINLOCK(tracepoint_iter_lock);
263 static void output_printk(struct trace_event_buffer *fbuffer)
265 struct trace_event_call *event_call;
266 struct trace_event *event;
268 struct trace_iterator *iter = tracepoint_print_iter;
273 event_call = fbuffer->trace_file->event_call;
274 if (!event_call || !event_call->event.funcs ||
275 !event_call->event.funcs->trace)
278 event = &fbuffer->trace_file->event_call->event;
280 spin_lock_irqsave(&tracepoint_iter_lock, flags);
281 trace_seq_init(&iter->seq);
282 iter->ent = fbuffer->entry;
283 event_call->event.funcs->trace(iter, 0, event);
284 trace_seq_putc(&iter->seq, 0);
285 printk("%s", iter->seq.buffer);
287 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
290 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
292 if (tracepoint_printk)
293 output_printk(fbuffer);
295 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
296 fbuffer->event, fbuffer->entry,
297 fbuffer->flags, fbuffer->pc);
299 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
301 int trace_event_reg(struct trace_event_call *call,
302 enum trace_reg type, void *data)
304 struct trace_event_file *file = data;
306 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
308 case TRACE_REG_REGISTER:
309 return tracepoint_probe_register(call->tp,
312 case TRACE_REG_UNREGISTER:
313 tracepoint_probe_unregister(call->tp,
318 #ifdef CONFIG_PERF_EVENTS
319 case TRACE_REG_PERF_REGISTER:
320 return tracepoint_probe_register(call->tp,
321 call->class->perf_probe,
323 case TRACE_REG_PERF_UNREGISTER:
324 tracepoint_probe_unregister(call->tp,
325 call->class->perf_probe,
328 case TRACE_REG_PERF_OPEN:
329 case TRACE_REG_PERF_CLOSE:
330 case TRACE_REG_PERF_ADD:
331 case TRACE_REG_PERF_DEL:
337 EXPORT_SYMBOL_GPL(trace_event_reg);
339 void trace_event_enable_cmd_record(bool enable)
341 struct trace_event_file *file;
342 struct trace_array *tr;
344 mutex_lock(&event_mutex);
345 do_for_each_event_file(tr, file) {
347 if (!(file->flags & EVENT_FILE_FL_ENABLED))
351 tracing_start_cmdline_record();
352 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
354 tracing_stop_cmdline_record();
355 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
357 } while_for_each_event_file();
358 mutex_unlock(&event_mutex);
361 static int __ftrace_event_enable_disable(struct trace_event_file *file,
362 int enable, int soft_disable)
364 struct trace_event_call *call = file->event_call;
365 struct trace_array *tr = file->tr;
366 unsigned long file_flags = file->flags;
373 * When soft_disable is set and enable is cleared, the sm_ref
374 * reference counter is decremented. If it reaches 0, we want
375 * to clear the SOFT_DISABLED flag but leave the event in the
376 * state that it was. That is, if the event was enabled and
377 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
378 * is set we do not want the event to be enabled before we
381 * When soft_disable is not set but the SOFT_MODE flag is,
382 * we do nothing. Do not disable the tracepoint, otherwise
383 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
386 if (atomic_dec_return(&file->sm_ref) > 0)
388 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
389 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
391 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
393 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
394 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
395 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
396 tracing_stop_cmdline_record();
397 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
399 call->class->reg(call, TRACE_REG_UNREGISTER, file);
401 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
402 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
403 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
405 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
409 * When soft_disable is set and enable is set, we want to
410 * register the tracepoint for the event, but leave the event
411 * as is. That means, if the event was already enabled, we do
412 * nothing (but set SOFT_MODE). If the event is disabled, we
413 * set SOFT_DISABLED before enabling the event tracepoint, so
414 * it still seems to be disabled.
417 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
419 if (atomic_inc_return(&file->sm_ref) > 1)
421 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
424 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
426 /* Keep the event disabled, when going to SOFT_MODE. */
428 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
430 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
431 tracing_start_cmdline_record();
432 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
434 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
436 tracing_stop_cmdline_record();
437 pr_info("event trace: Could not enable event "
438 "%s\n", trace_event_name(call));
441 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
443 /* WAS_ENABLED gets set but never cleared. */
444 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
449 /* Enable or disable use of trace_buffered_event */
450 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
451 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
452 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
453 trace_buffered_event_enable();
455 trace_buffered_event_disable();
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 /* Shouldn't this be in a header? */
487 /* Returns true if found in filter */
489 find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
492 * If pid_max changed after filtered_pids was created, we
493 * by default ignore all pids greater than the previous pid_max.
495 if (search_pid >= filtered_pids->pid_max)
498 return test_bit(search_pid, filtered_pids->pids);
502 ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
505 * Return false, because if filtered_pids does not exist,
506 * all pids are good to trace.
511 return !find_filtered_pid(filtered_pids, task->pid);
514 static void filter_add_remove_task(struct trace_pid_list *pid_list,
515 struct task_struct *self,
516 struct task_struct *task)
521 /* For forks, we only add if the forking task is listed */
523 if (!find_filtered_pid(pid_list, self->pid))
527 /* Sorry, but we don't support pid_max changing after setting */
528 if (task->pid >= pid_list->pid_max)
531 /* "self" is set for forks, and NULL for exits */
533 set_bit(task->pid, pid_list->pids);
535 clear_bit(task->pid, pid_list->pids);
539 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
541 struct trace_pid_list *pid_list;
542 struct trace_array *tr = data;
544 pid_list = rcu_dereference_sched(tr->filtered_pids);
545 filter_add_remove_task(pid_list, NULL, task);
549 event_filter_pid_sched_process_fork(void *data,
550 struct task_struct *self,
551 struct task_struct *task)
553 struct trace_pid_list *pid_list;
554 struct trace_array *tr = data;
556 pid_list = rcu_dereference_sched(tr->filtered_pids);
557 filter_add_remove_task(pid_list, self, task);
560 void trace_event_follow_fork(struct trace_array *tr, bool enable)
563 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
565 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
568 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
570 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
576 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
577 struct task_struct *prev, struct task_struct *next)
579 struct trace_array *tr = data;
580 struct trace_pid_list *pid_list;
582 pid_list = rcu_dereference_sched(tr->filtered_pids);
584 this_cpu_write(tr->trace_buffer.data->ignore_pid,
585 ignore_this_task(pid_list, prev) &&
586 ignore_this_task(pid_list, next));
590 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
591 struct task_struct *prev, struct task_struct *next)
593 struct trace_array *tr = data;
594 struct trace_pid_list *pid_list;
596 pid_list = rcu_dereference_sched(tr->filtered_pids);
598 this_cpu_write(tr->trace_buffer.data->ignore_pid,
599 ignore_this_task(pid_list, next));
603 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
605 struct trace_array *tr = data;
606 struct trace_pid_list *pid_list;
608 /* Nothing to do if we are already tracing */
609 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
612 pid_list = rcu_dereference_sched(tr->filtered_pids);
614 this_cpu_write(tr->trace_buffer.data->ignore_pid,
615 ignore_this_task(pid_list, task));
619 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
621 struct trace_array *tr = data;
622 struct trace_pid_list *pid_list;
624 /* Nothing to do if we are not tracing */
625 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
628 pid_list = rcu_dereference_sched(tr->filtered_pids);
630 /* Set tracing if current is enabled */
631 this_cpu_write(tr->trace_buffer.data->ignore_pid,
632 ignore_this_task(pid_list, current));
635 static void __ftrace_clear_event_pids(struct trace_array *tr)
637 struct trace_pid_list *pid_list;
638 struct trace_event_file *file;
641 pid_list = rcu_dereference_protected(tr->filtered_pids,
642 lockdep_is_held(&event_mutex));
646 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
647 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
649 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
650 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
652 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
653 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
655 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
656 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
658 list_for_each_entry(file, &tr->events, list) {
659 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
662 for_each_possible_cpu(cpu)
663 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
665 rcu_assign_pointer(tr->filtered_pids, NULL);
667 /* Wait till all users are no longer using pid filtering */
670 vfree(pid_list->pids);
674 static void ftrace_clear_event_pids(struct trace_array *tr)
676 mutex_lock(&event_mutex);
677 __ftrace_clear_event_pids(tr);
678 mutex_unlock(&event_mutex);
681 static void __put_system(struct event_subsystem *system)
683 struct event_filter *filter = system->filter;
685 WARN_ON_ONCE(system_refcount(system) == 0);
686 if (system_refcount_dec(system))
689 list_del(&system->list);
692 kfree(filter->filter_string);
695 kfree_const(system->name);
699 static void __get_system(struct event_subsystem *system)
701 WARN_ON_ONCE(system_refcount(system) == 0);
702 system_refcount_inc(system);
705 static void __get_system_dir(struct trace_subsystem_dir *dir)
707 WARN_ON_ONCE(dir->ref_count == 0);
709 __get_system(dir->subsystem);
712 static void __put_system_dir(struct trace_subsystem_dir *dir)
714 WARN_ON_ONCE(dir->ref_count == 0);
715 /* If the subsystem is about to be freed, the dir must be too */
716 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
718 __put_system(dir->subsystem);
719 if (!--dir->ref_count)
723 static void put_system(struct trace_subsystem_dir *dir)
725 mutex_lock(&event_mutex);
726 __put_system_dir(dir);
727 mutex_unlock(&event_mutex);
730 static void remove_subsystem(struct trace_subsystem_dir *dir)
735 if (!--dir->nr_events) {
736 tracefs_remove_recursive(dir->entry);
737 list_del(&dir->list);
738 __put_system_dir(dir);
742 static void remove_event_file_dir(struct trace_event_file *file)
744 struct dentry *dir = file->dir;
745 struct dentry *child;
748 spin_lock(&dir->d_lock); /* probably unneeded */
749 list_for_each_entry(child, &dir->d_subdirs, d_child) {
750 if (d_really_is_positive(child)) /* probably unneeded */
751 d_inode(child)->i_private = NULL;
753 spin_unlock(&dir->d_lock);
755 tracefs_remove_recursive(dir);
758 list_del(&file->list);
759 remove_subsystem(file->system);
760 free_event_filter(file->filter);
761 kmem_cache_free(file_cachep, file);
765 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
768 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
769 const char *sub, const char *event, int set)
771 struct trace_event_file *file;
772 struct trace_event_call *call;
776 list_for_each_entry(file, &tr->events, list) {
778 call = file->event_call;
779 name = trace_event_name(call);
781 if (!name || !call->class || !call->class->reg)
784 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
788 strcmp(match, name) != 0 &&
789 strcmp(match, call->class->system) != 0)
792 if (sub && strcmp(sub, call->class->system) != 0)
795 if (event && strcmp(event, name) != 0)
798 ftrace_event_enable_disable(file, set);
806 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
807 const char *sub, const char *event, int set)
811 mutex_lock(&event_mutex);
812 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
813 mutex_unlock(&event_mutex);
818 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
820 char *event = NULL, *sub = NULL, *match;
824 * The buf format can be <subsystem>:<event-name>
825 * *:<event-name> means any event by that name.
826 * :<event-name> is the same.
828 * <subsystem>:* means all events in that subsystem
829 * <subsystem>: means the same.
831 * <name> (no ':') means all events in a subsystem with
832 * the name <name> or any event that matches <name>
835 match = strsep(&buf, ":");
841 if (!strlen(sub) || strcmp(sub, "*") == 0)
843 if (!strlen(event) || strcmp(event, "*") == 0)
847 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
849 /* Put back the colon to allow this to be called again */
857 * trace_set_clr_event - enable or disable an event
858 * @system: system name to match (NULL for any system)
859 * @event: event name to match (NULL for all events, within system)
860 * @set: 1 to enable, 0 to disable
862 * This is a way for other parts of the kernel to enable or disable
865 * Returns 0 on success, -EINVAL if the parameters do not match any
868 int trace_set_clr_event(const char *system, const char *event, int set)
870 struct trace_array *tr = top_trace_array();
875 return __ftrace_set_clr_event(tr, NULL, system, event, set);
877 EXPORT_SYMBOL_GPL(trace_set_clr_event);
879 /* 128 should be much more than enough */
880 #define EVENT_BUF_SIZE 127
883 ftrace_event_write(struct file *file, const char __user *ubuf,
884 size_t cnt, loff_t *ppos)
886 struct trace_parser parser;
887 struct seq_file *m = file->private_data;
888 struct trace_array *tr = m->private;
894 ret = tracing_update_buffers();
898 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
901 read = trace_get_user(&parser, ubuf, cnt, ppos);
903 if (read >= 0 && trace_parser_loaded((&parser))) {
906 if (*parser.buffer == '!')
909 parser.buffer[parser.idx] = 0;
911 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
919 trace_parser_put(&parser);
925 t_next(struct seq_file *m, void *v, loff_t *pos)
927 struct trace_event_file *file = v;
928 struct trace_event_call *call;
929 struct trace_array *tr = m->private;
933 list_for_each_entry_continue(file, &tr->events, list) {
934 call = file->event_call;
936 * The ftrace subsystem is for showing formats only.
937 * They can not be enabled or disabled via the event files.
939 if (call->class && call->class->reg &&
940 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
947 static void *t_start(struct seq_file *m, loff_t *pos)
949 struct trace_event_file *file;
950 struct trace_array *tr = m->private;
953 mutex_lock(&event_mutex);
955 file = list_entry(&tr->events, struct trace_event_file, list);
956 for (l = 0; l <= *pos; ) {
957 file = t_next(m, file, &l);
965 s_next(struct seq_file *m, void *v, loff_t *pos)
967 struct trace_event_file *file = v;
968 struct trace_array *tr = m->private;
972 list_for_each_entry_continue(file, &tr->events, list) {
973 if (file->flags & EVENT_FILE_FL_ENABLED)
980 static void *s_start(struct seq_file *m, loff_t *pos)
982 struct trace_event_file *file;
983 struct trace_array *tr = m->private;
986 mutex_lock(&event_mutex);
988 file = list_entry(&tr->events, struct trace_event_file, list);
989 for (l = 0; l <= *pos; ) {
990 file = s_next(m, file, &l);
997 static int t_show(struct seq_file *m, void *v)
999 struct trace_event_file *file = v;
1000 struct trace_event_call *call = file->event_call;
1002 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1003 seq_printf(m, "%s:", call->class->system);
1004 seq_printf(m, "%s\n", trace_event_name(call));
1009 static void t_stop(struct seq_file *m, void *p)
1011 mutex_unlock(&event_mutex);
1015 p_next(struct seq_file *m, void *v, loff_t *pos)
1017 struct trace_array *tr = m->private;
1018 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
1019 unsigned long pid = (unsigned long)v;
1023 /* pid already is +1 of the actual prevous bit */
1024 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
1026 /* Return pid + 1 to allow zero to be represented */
1027 if (pid < pid_list->pid_max)
1028 return (void *)(pid + 1);
1033 static void *p_start(struct seq_file *m, loff_t *pos)
1036 struct trace_pid_list *pid_list;
1037 struct trace_array *tr = m->private;
1042 * Grab the mutex, to keep calls to p_next() having the same
1043 * tr->filtered_pids as p_start() has.
1044 * If we just passed the tr->filtered_pids around, then RCU would
1045 * have been enough, but doing that makes things more complex.
1047 mutex_lock(&event_mutex);
1048 rcu_read_lock_sched();
1050 pid_list = rcu_dereference_sched(tr->filtered_pids);
1055 pid = find_first_bit(pid_list->pids, pid_list->pid_max);
1056 if (pid >= pid_list->pid_max)
1059 /* Return pid + 1 so that zero can be the exit value */
1060 for (pid++; pid && l < *pos;
1061 pid = (unsigned long)p_next(m, (void *)pid, &l))
1066 static void p_stop(struct seq_file *m, void *p)
1069 rcu_read_unlock_sched();
1070 mutex_unlock(&event_mutex);
1073 static int p_show(struct seq_file *m, void *v)
1075 unsigned long pid = (unsigned long)v - 1;
1077 seq_printf(m, "%lu\n", pid);
1082 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1085 struct trace_event_file *file;
1086 unsigned long flags;
1089 mutex_lock(&event_mutex);
1090 file = event_file_data(filp);
1092 flags = file->flags;
1093 mutex_unlock(&event_mutex);
1098 if (flags & EVENT_FILE_FL_ENABLED &&
1099 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1102 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1103 flags & EVENT_FILE_FL_SOFT_MODE)
1108 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1112 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1115 struct trace_event_file *file;
1119 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1123 ret = tracing_update_buffers();
1131 mutex_lock(&event_mutex);
1132 file = event_file_data(filp);
1134 ret = ftrace_event_enable_disable(file, val);
1135 mutex_unlock(&event_mutex);
1144 return ret ? ret : cnt;
1148 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1151 const char set_to_char[4] = { '?', '0', '1', 'X' };
1152 struct trace_subsystem_dir *dir = filp->private_data;
1153 struct event_subsystem *system = dir->subsystem;
1154 struct trace_event_call *call;
1155 struct trace_event_file *file;
1156 struct trace_array *tr = dir->tr;
1161 mutex_lock(&event_mutex);
1162 list_for_each_entry(file, &tr->events, list) {
1163 call = file->event_call;
1164 if (!trace_event_name(call) || !call->class || !call->class->reg)
1167 if (system && strcmp(call->class->system, system->name) != 0)
1171 * We need to find out if all the events are set
1172 * or if all events or cleared, or if we have
1175 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1178 * If we have a mixture, no need to look further.
1183 mutex_unlock(&event_mutex);
1185 buf[0] = set_to_char[set];
1188 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1194 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1197 struct trace_subsystem_dir *dir = filp->private_data;
1198 struct event_subsystem *system = dir->subsystem;
1199 const char *name = NULL;
1203 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1207 ret = tracing_update_buffers();
1211 if (val != 0 && val != 1)
1215 * Opening of "enable" adds a ref count to system,
1216 * so the name is safe to use.
1219 name = system->name;
1221 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1235 FORMAT_FIELD_SEPERATOR = 2,
1236 FORMAT_PRINTFMT = 3,
1239 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1241 struct trace_event_call *call = event_file_data(m->private);
1242 struct list_head *common_head = &ftrace_common_fields;
1243 struct list_head *head = trace_get_fields(call);
1244 struct list_head *node = v;
1248 switch ((unsigned long)v) {
1253 case FORMAT_FIELD_SEPERATOR:
1257 case FORMAT_PRINTFMT:
1263 if (node == common_head)
1264 return (void *)FORMAT_FIELD_SEPERATOR;
1265 else if (node == head)
1266 return (void *)FORMAT_PRINTFMT;
1271 static int f_show(struct seq_file *m, void *v)
1273 struct trace_event_call *call = event_file_data(m->private);
1274 struct ftrace_event_field *field;
1275 const char *array_descriptor;
1277 switch ((unsigned long)v) {
1279 seq_printf(m, "name: %s\n", trace_event_name(call));
1280 seq_printf(m, "ID: %d\n", call->event.type);
1281 seq_puts(m, "format:\n");
1284 case FORMAT_FIELD_SEPERATOR:
1288 case FORMAT_PRINTFMT:
1289 seq_printf(m, "\nprint fmt: %s\n",
1294 field = list_entry(v, struct ftrace_event_field, link);
1296 * Smartly shows the array type(except dynamic array).
1299 * If TYPE := TYPE[LEN], it is shown:
1300 * field:TYPE VAR[LEN]
1302 array_descriptor = strchr(field->type, '[');
1304 if (!strncmp(field->type, "__data_loc", 10))
1305 array_descriptor = NULL;
1307 if (!array_descriptor)
1308 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1309 field->type, field->name, field->offset,
1310 field->size, !!field->is_signed);
1312 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1313 (int)(array_descriptor - field->type),
1314 field->type, field->name,
1315 array_descriptor, field->offset,
1316 field->size, !!field->is_signed);
1321 static void *f_start(struct seq_file *m, loff_t *pos)
1323 void *p = (void *)FORMAT_HEADER;
1326 /* ->stop() is called even if ->start() fails */
1327 mutex_lock(&event_mutex);
1328 if (!event_file_data(m->private))
1329 return ERR_PTR(-ENODEV);
1331 while (l < *pos && p)
1332 p = f_next(m, p, &l);
1337 static void f_stop(struct seq_file *m, void *p)
1339 mutex_unlock(&event_mutex);
1342 static const struct seq_operations trace_format_seq_ops = {
1349 static int trace_format_open(struct inode *inode, struct file *file)
1354 ret = seq_open(file, &trace_format_seq_ops);
1358 m = file->private_data;
1365 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1367 int id = (long)event_file_data(filp);
1377 len = sprintf(buf, "%d\n", id);
1379 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1383 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1386 struct trace_event_file *file;
1387 struct trace_seq *s;
1393 s = kmalloc(sizeof(*s), GFP_KERNEL);
1400 mutex_lock(&event_mutex);
1401 file = event_file_data(filp);
1403 print_event_filter(file, s);
1404 mutex_unlock(&event_mutex);
1407 r = simple_read_from_buffer(ubuf, cnt, ppos,
1408 s->buffer, trace_seq_used(s));
1416 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1419 struct trace_event_file *file;
1423 if (cnt >= PAGE_SIZE)
1426 buf = memdup_user_nul(ubuf, cnt);
1428 return PTR_ERR(buf);
1430 mutex_lock(&event_mutex);
1431 file = event_file_data(filp);
1433 err = apply_event_filter(file, buf);
1434 mutex_unlock(&event_mutex);
1445 static LIST_HEAD(event_subsystems);
1447 static int subsystem_open(struct inode *inode, struct file *filp)
1449 struct event_subsystem *system = NULL;
1450 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1451 struct trace_array *tr;
1454 if (tracing_is_disabled())
1457 /* Make sure the system still exists */
1458 mutex_lock(&trace_types_lock);
1459 mutex_lock(&event_mutex);
1460 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1461 list_for_each_entry(dir, &tr->systems, list) {
1462 if (dir == inode->i_private) {
1463 /* Don't open systems with no events */
1464 if (dir->nr_events) {
1465 __get_system_dir(dir);
1466 system = dir->subsystem;
1473 mutex_unlock(&event_mutex);
1474 mutex_unlock(&trace_types_lock);
1479 /* Some versions of gcc think dir can be uninitialized here */
1482 /* Still need to increment the ref count of the system */
1483 if (trace_array_get(tr) < 0) {
1488 ret = tracing_open_generic(inode, filp);
1490 trace_array_put(tr);
1497 static int system_tr_open(struct inode *inode, struct file *filp)
1499 struct trace_subsystem_dir *dir;
1500 struct trace_array *tr = inode->i_private;
1503 if (tracing_is_disabled())
1506 if (trace_array_get(tr) < 0)
1509 /* Make a temporary dir that has no system but points to tr */
1510 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1512 trace_array_put(tr);
1518 ret = tracing_open_generic(inode, filp);
1520 trace_array_put(tr);
1525 filp->private_data = dir;
1530 static int subsystem_release(struct inode *inode, struct file *file)
1532 struct trace_subsystem_dir *dir = file->private_data;
1534 trace_array_put(dir->tr);
1537 * If dir->subsystem is NULL, then this is a temporary
1538 * descriptor that was made for a trace_array to enable
1550 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1553 struct trace_subsystem_dir *dir = filp->private_data;
1554 struct event_subsystem *system = dir->subsystem;
1555 struct trace_seq *s;
1561 s = kmalloc(sizeof(*s), GFP_KERNEL);
1567 print_subsystem_event_filter(system, s);
1568 r = simple_read_from_buffer(ubuf, cnt, ppos,
1569 s->buffer, trace_seq_used(s));
1577 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1580 struct trace_subsystem_dir *dir = filp->private_data;
1584 if (cnt >= PAGE_SIZE)
1587 buf = memdup_user_nul(ubuf, cnt);
1589 return PTR_ERR(buf);
1591 err = apply_subsystem_event_filter(dir, buf);
1602 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1604 int (*func)(struct trace_seq *s) = filp->private_data;
1605 struct trace_seq *s;
1611 s = kmalloc(sizeof(*s), GFP_KERNEL);
1618 r = simple_read_from_buffer(ubuf, cnt, ppos,
1619 s->buffer, trace_seq_used(s));
1626 static void ignore_task_cpu(void *data)
1628 struct trace_array *tr = data;
1629 struct trace_pid_list *pid_list;
1632 * This function is called by on_each_cpu() while the
1633 * event_mutex is held.
1635 pid_list = rcu_dereference_protected(tr->filtered_pids,
1636 mutex_is_locked(&event_mutex));
1638 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1639 ignore_this_task(pid_list, current));
1643 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1644 size_t cnt, loff_t *ppos)
1646 struct seq_file *m = filp->private_data;
1647 struct trace_array *tr = m->private;
1648 struct trace_pid_list *filtered_pids = NULL;
1649 struct trace_pid_list *pid_list;
1650 struct trace_event_file *file;
1651 struct trace_parser parser;
1662 ret = tracing_update_buffers();
1666 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1669 mutex_lock(&event_mutex);
1670 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1671 lockdep_is_held(&event_mutex));
1674 * Always recreate a new array. The write is an all or nothing
1675 * operation. Always create a new array when adding new pids by
1676 * the user. If the operation fails, then the current list is
1679 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1684 pid_list->pid_max = READ_ONCE(pid_max);
1685 /* Only truncating will shrink pid_max */
1686 if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
1687 pid_list->pid_max = filtered_pids->pid_max;
1688 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
1689 if (!pid_list->pids) {
1694 if (filtered_pids) {
1695 /* copy the current bits to the new max */
1696 pid = find_first_bit(filtered_pids->pids,
1697 filtered_pids->pid_max);
1698 while (pid < filtered_pids->pid_max) {
1699 set_bit(pid, pid_list->pids);
1700 pid = find_next_bit(filtered_pids->pids,
1701 filtered_pids->pid_max,
1711 ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1712 if (ret < 0 || !trace_parser_loaded(&parser))
1719 parser.buffer[parser.idx] = 0;
1722 if (kstrtoul(parser.buffer, 0, &val))
1724 if (val >= pid_list->pid_max)
1729 set_bit(pid, pid_list->pids);
1732 trace_parser_clear(&parser);
1735 trace_parser_put(&parser);
1738 vfree(pid_list->pids);
1745 /* Cleared the list of pids */
1746 vfree(pid_list->pids);
1753 rcu_assign_pointer(tr->filtered_pids, pid_list);
1755 list_for_each_entry(file, &tr->events, list) {
1756 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1759 if (filtered_pids) {
1760 synchronize_sched();
1762 vfree(filtered_pids->pids);
1763 kfree(filtered_pids);
1766 * Register a probe that is called before all other probes
1767 * to set ignore_pid if next or prev do not match.
1768 * Register a probe this is called after all other probes
1769 * to only keep ignore_pid set if next pid matches.
1771 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1773 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1776 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1778 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1781 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1783 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1786 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1788 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1793 * Ignoring of pids is done at task switch. But we have to
1794 * check for those tasks that are currently running.
1795 * Always do this in case a pid was appended or removed.
1797 on_each_cpu(ignore_task_cpu, tr, 1);
1800 mutex_unlock(&event_mutex);
1809 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1810 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1811 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1812 static int ftrace_event_release(struct inode *inode, struct file *file);
1814 static const struct seq_operations show_event_seq_ops = {
1821 static const struct seq_operations show_set_event_seq_ops = {
1828 static const struct seq_operations show_set_pid_seq_ops = {
1835 static const struct file_operations ftrace_avail_fops = {
1836 .open = ftrace_event_avail_open,
1838 .llseek = seq_lseek,
1839 .release = seq_release,
1842 static const struct file_operations ftrace_set_event_fops = {
1843 .open = ftrace_event_set_open,
1845 .write = ftrace_event_write,
1846 .llseek = seq_lseek,
1847 .release = ftrace_event_release,
1850 static const struct file_operations ftrace_set_event_pid_fops = {
1851 .open = ftrace_event_set_pid_open,
1853 .write = ftrace_event_pid_write,
1854 .llseek = seq_lseek,
1855 .release = ftrace_event_release,
1858 static const struct file_operations ftrace_enable_fops = {
1859 .open = tracing_open_generic,
1860 .read = event_enable_read,
1861 .write = event_enable_write,
1862 .llseek = default_llseek,
1865 static const struct file_operations ftrace_event_format_fops = {
1866 .open = trace_format_open,
1868 .llseek = seq_lseek,
1869 .release = seq_release,
1872 static const struct file_operations ftrace_event_id_fops = {
1873 .read = event_id_read,
1874 .llseek = default_llseek,
1877 static const struct file_operations ftrace_event_filter_fops = {
1878 .open = tracing_open_generic,
1879 .read = event_filter_read,
1880 .write = event_filter_write,
1881 .llseek = default_llseek,
1884 static const struct file_operations ftrace_subsystem_filter_fops = {
1885 .open = subsystem_open,
1886 .read = subsystem_filter_read,
1887 .write = subsystem_filter_write,
1888 .llseek = default_llseek,
1889 .release = subsystem_release,
1892 static const struct file_operations ftrace_system_enable_fops = {
1893 .open = subsystem_open,
1894 .read = system_enable_read,
1895 .write = system_enable_write,
1896 .llseek = default_llseek,
1897 .release = subsystem_release,
1900 static const struct file_operations ftrace_tr_enable_fops = {
1901 .open = system_tr_open,
1902 .read = system_enable_read,
1903 .write = system_enable_write,
1904 .llseek = default_llseek,
1905 .release = subsystem_release,
1908 static const struct file_operations ftrace_show_header_fops = {
1909 .open = tracing_open_generic,
1910 .read = show_header,
1911 .llseek = default_llseek,
1915 ftrace_event_open(struct inode *inode, struct file *file,
1916 const struct seq_operations *seq_ops)
1921 ret = seq_open(file, seq_ops);
1924 m = file->private_data;
1925 /* copy tr over to seq ops */
1926 m->private = inode->i_private;
1931 static int ftrace_event_release(struct inode *inode, struct file *file)
1933 struct trace_array *tr = inode->i_private;
1935 trace_array_put(tr);
1937 return seq_release(inode, file);
1941 ftrace_event_avail_open(struct inode *inode, struct file *file)
1943 const struct seq_operations *seq_ops = &show_event_seq_ops;
1945 return ftrace_event_open(inode, file, seq_ops);
1949 ftrace_event_set_open(struct inode *inode, struct file *file)
1951 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1952 struct trace_array *tr = inode->i_private;
1955 if (trace_array_get(tr) < 0)
1958 if ((file->f_mode & FMODE_WRITE) &&
1959 (file->f_flags & O_TRUNC))
1960 ftrace_clear_events(tr);
1962 ret = ftrace_event_open(inode, file, seq_ops);
1964 trace_array_put(tr);
1969 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1971 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1972 struct trace_array *tr = inode->i_private;
1975 if (trace_array_get(tr) < 0)
1978 if ((file->f_mode & FMODE_WRITE) &&
1979 (file->f_flags & O_TRUNC))
1980 ftrace_clear_event_pids(tr);
1982 ret = ftrace_event_open(inode, file, seq_ops);
1984 trace_array_put(tr);
1988 static struct event_subsystem *
1989 create_new_subsystem(const char *name)
1991 struct event_subsystem *system;
1993 /* need to create new entry */
1994 system = kmalloc(sizeof(*system), GFP_KERNEL);
1998 system->ref_count = 1;
2000 /* Only allocate if dynamic (kprobes and modules) */
2001 system->name = kstrdup_const(name, GFP_KERNEL);
2005 system->filter = NULL;
2007 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2008 if (!system->filter)
2011 list_add(&system->list, &event_subsystems);
2016 kfree_const(system->name);
2021 static struct dentry *
2022 event_subsystem_dir(struct trace_array *tr, const char *name,
2023 struct trace_event_file *file, struct dentry *parent)
2025 struct trace_subsystem_dir *dir;
2026 struct event_subsystem *system;
2027 struct dentry *entry;
2029 /* First see if we did not already create this dir */
2030 list_for_each_entry(dir, &tr->systems, list) {
2031 system = dir->subsystem;
2032 if (strcmp(system->name, name) == 0) {
2039 /* Now see if the system itself exists. */
2040 list_for_each_entry(system, &event_subsystems, list) {
2041 if (strcmp(system->name, name) == 0)
2044 /* Reset system variable when not found */
2045 if (&system->list == &event_subsystems)
2048 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2053 system = create_new_subsystem(name);
2057 __get_system(system);
2059 dir->entry = tracefs_create_dir(name, parent);
2061 pr_warn("Failed to create system directory %s\n", name);
2062 __put_system(system);
2069 dir->subsystem = system;
2072 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2073 &ftrace_subsystem_filter_fops);
2075 kfree(system->filter);
2076 system->filter = NULL;
2077 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2080 trace_create_file("enable", 0644, dir->entry, dir,
2081 &ftrace_system_enable_fops);
2083 list_add(&dir->list, &tr->systems);
2090 /* Only print this message if failed on memory allocation */
2091 if (!dir || !system)
2092 pr_warn("No memory to create event subsystem %s\n", name);
2097 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2099 struct trace_event_call *call = file->event_call;
2100 struct trace_array *tr = file->tr;
2101 struct list_head *head;
2102 struct dentry *d_events;
2107 * If the trace point header did not define TRACE_SYSTEM
2108 * then the system would be called "TRACE_SYSTEM".
2110 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2111 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2117 name = trace_event_name(call);
2118 file->dir = tracefs_create_dir(name, d_events);
2120 pr_warn("Could not create tracefs '%s' directory\n", name);
2124 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2125 trace_create_file("enable", 0644, file->dir, file,
2126 &ftrace_enable_fops);
2128 #ifdef CONFIG_PERF_EVENTS
2129 if (call->event.type && call->class->reg)
2130 trace_create_file("id", 0444, file->dir,
2131 (void *)(long)call->event.type,
2132 &ftrace_event_id_fops);
2136 * Other events may have the same class. Only update
2137 * the fields if they are not already defined.
2139 head = trace_get_fields(call);
2140 if (list_empty(head)) {
2141 ret = call->class->define_fields(call);
2143 pr_warn("Could not initialize trace point events/%s\n",
2148 trace_create_file("filter", 0644, file->dir, file,
2149 &ftrace_event_filter_fops);
2151 trace_create_file("trigger", 0644, file->dir, file,
2152 &event_trigger_fops);
2154 #ifdef CONFIG_HIST_TRIGGERS
2155 trace_create_file("hist", 0444, file->dir, file,
2158 trace_create_file("format", 0444, file->dir, call,
2159 &ftrace_event_format_fops);
2164 static void remove_event_from_tracers(struct trace_event_call *call)
2166 struct trace_event_file *file;
2167 struct trace_array *tr;
2169 do_for_each_event_file_safe(tr, file) {
2170 if (file->event_call != call)
2173 remove_event_file_dir(file);
2175 * The do_for_each_event_file_safe() is
2176 * a double loop. After finding the call for this
2177 * trace_array, we use break to jump to the next
2181 } while_for_each_event_file();
2184 static void event_remove(struct trace_event_call *call)
2186 struct trace_array *tr;
2187 struct trace_event_file *file;
2189 do_for_each_event_file(tr, file) {
2190 if (file->event_call != call)
2192 ftrace_event_enable_disable(file, 0);
2194 * The do_for_each_event_file() is
2195 * a double loop. After finding the call for this
2196 * trace_array, we use break to jump to the next
2200 } while_for_each_event_file();
2202 if (call->event.funcs)
2203 __unregister_trace_event(&call->event);
2204 remove_event_from_tracers(call);
2205 list_del(&call->list);
2208 static int event_init(struct trace_event_call *call)
2213 name = trace_event_name(call);
2217 if (call->class->raw_init) {
2218 ret = call->class->raw_init(call);
2219 if (ret < 0 && ret != -ENOSYS)
2220 pr_warn("Could not initialize trace events/%s\n", name);
2227 __register_event(struct trace_event_call *call, struct module *mod)
2231 ret = event_init(call);
2235 list_add(&call->list, &ftrace_events);
2241 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
2246 /* Find the length of the enum value as a string */
2247 elen = snprintf(ptr, 0, "%ld", map->enum_value);
2248 /* Make sure there's enough room to replace the string with the value */
2252 snprintf(ptr, elen + 1, "%ld", map->enum_value);
2254 /* Get the rest of the string of ptr */
2255 rlen = strlen(ptr + len);
2256 memmove(ptr + elen, ptr + len, rlen);
2257 /* Make sure we end the new string */
2258 ptr[elen + rlen] = 0;
2263 static void update_event_printk(struct trace_event_call *call,
2264 struct trace_enum_map *map)
2268 int len = strlen(map->enum_string);
2270 for (ptr = call->print_fmt; *ptr; ptr++) {
2284 if (isdigit(*ptr)) {
2288 /* Check for alpha chars like ULL */
2289 } while (isalnum(*ptr));
2293 * A number must have some kind of delimiter after
2294 * it, and we can ignore that too.
2298 if (isalpha(*ptr) || *ptr == '_') {
2299 if (strncmp(map->enum_string, ptr, len) == 0 &&
2300 !isalnum(ptr[len]) && ptr[len] != '_') {
2301 ptr = enum_replace(ptr, map, len);
2302 /* Hmm, enum string smaller than value */
2303 if (WARN_ON_ONCE(!ptr))
2306 * No need to decrement here, as enum_replace()
2307 * returns the pointer to the character passed
2308 * the enum, and two enums can not be placed
2309 * back to back without something in between.
2310 * We can skip that something in between.
2317 } while (isalnum(*ptr) || *ptr == '_');
2321 * If what comes after this variable is a '.' or
2322 * '->' then we can continue to ignore that string.
2324 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2325 ptr += *ptr == '.' ? 1 : 2;
2331 * Once again, we can skip the delimiter that came
2339 void trace_event_enum_update(struct trace_enum_map **map, int len)
2341 struct trace_event_call *call, *p;
2342 const char *last_system = NULL;
2346 down_write(&trace_event_sem);
2347 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2348 /* events are usually grouped together with systems */
2349 if (!last_system || call->class->system != last_system) {
2351 last_system = call->class->system;
2354 for (i = last_i; i < len; i++) {
2355 if (call->class->system == map[i]->system) {
2356 /* Save the first system if need be */
2359 update_event_printk(call, map[i]);
2363 up_write(&trace_event_sem);
2366 static struct trace_event_file *
2367 trace_create_new_event(struct trace_event_call *call,
2368 struct trace_array *tr)
2370 struct trace_event_file *file;
2372 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2376 file->event_call = call;
2378 atomic_set(&file->sm_ref, 0);
2379 atomic_set(&file->tm_ref, 0);
2380 INIT_LIST_HEAD(&file->triggers);
2381 list_add(&file->list, &tr->events);
2386 /* Add an event to a trace directory */
2388 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2390 struct trace_event_file *file;
2392 file = trace_create_new_event(call, tr);
2396 return event_create_dir(tr->event_dir, file);
2400 * Just create a decriptor for early init. A descriptor is required
2401 * for enabling events at boot. We want to enable events before
2402 * the filesystem is initialized.
2405 __trace_early_add_new_event(struct trace_event_call *call,
2406 struct trace_array *tr)
2408 struct trace_event_file *file;
2410 file = trace_create_new_event(call, tr);
2417 struct ftrace_module_file_ops;
2418 static void __add_event_to_tracers(struct trace_event_call *call);
2420 /* Add an additional event_call dynamically */
2421 int trace_add_event_call(struct trace_event_call *call)
2424 mutex_lock(&trace_types_lock);
2425 mutex_lock(&event_mutex);
2427 ret = __register_event(call, NULL);
2429 __add_event_to_tracers(call);
2431 mutex_unlock(&event_mutex);
2432 mutex_unlock(&trace_types_lock);
2437 * Must be called under locking of trace_types_lock, event_mutex and
2440 static void __trace_remove_event_call(struct trace_event_call *call)
2443 trace_destroy_fields(call);
2444 free_event_filter(call->filter);
2445 call->filter = NULL;
2448 static int probe_remove_event_call(struct trace_event_call *call)
2450 struct trace_array *tr;
2451 struct trace_event_file *file;
2453 #ifdef CONFIG_PERF_EVENTS
2454 if (call->perf_refcount)
2457 do_for_each_event_file(tr, file) {
2458 if (file->event_call != call)
2461 * We can't rely on ftrace_event_enable_disable(enable => 0)
2462 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2463 * TRACE_REG_UNREGISTER.
2465 if (file->flags & EVENT_FILE_FL_ENABLED)
2468 * The do_for_each_event_file_safe() is
2469 * a double loop. After finding the call for this
2470 * trace_array, we use break to jump to the next
2474 } while_for_each_event_file();
2476 __trace_remove_event_call(call);
2481 /* Remove an event_call */
2482 int trace_remove_event_call(struct trace_event_call *call)
2486 mutex_lock(&trace_types_lock);
2487 mutex_lock(&event_mutex);
2488 down_write(&trace_event_sem);
2489 ret = probe_remove_event_call(call);
2490 up_write(&trace_event_sem);
2491 mutex_unlock(&event_mutex);
2492 mutex_unlock(&trace_types_lock);
2497 #define for_each_event(event, start, end) \
2498 for (event = start; \
2499 (unsigned long)event < (unsigned long)end; \
2502 #ifdef CONFIG_MODULES
2504 static void trace_module_add_events(struct module *mod)
2506 struct trace_event_call **call, **start, **end;
2508 if (!mod->num_trace_events)
2511 /* Don't add infrastructure for mods without tracepoints */
2512 if (trace_module_has_bad_taint(mod)) {
2513 pr_err("%s: module has bad taint, not creating trace events\n",
2518 start = mod->trace_events;
2519 end = mod->trace_events + mod->num_trace_events;
2521 for_each_event(call, start, end) {
2522 __register_event(*call, mod);
2523 __add_event_to_tracers(*call);
2527 static void trace_module_remove_events(struct module *mod)
2529 struct trace_event_call *call, *p;
2530 bool clear_trace = false;
2532 down_write(&trace_event_sem);
2533 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2534 if (call->mod == mod) {
2535 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2537 __trace_remove_event_call(call);
2540 up_write(&trace_event_sem);
2543 * It is safest to reset the ring buffer if the module being unloaded
2544 * registered any events that were used. The only worry is if
2545 * a new module gets loaded, and takes on the same id as the events
2546 * of this module. When printing out the buffer, traced events left
2547 * over from this module may be passed to the new module events and
2548 * unexpected results may occur.
2551 tracing_reset_all_online_cpus();
2554 static int trace_module_notify(struct notifier_block *self,
2555 unsigned long val, void *data)
2557 struct module *mod = data;
2559 mutex_lock(&trace_types_lock);
2560 mutex_lock(&event_mutex);
2562 case MODULE_STATE_COMING:
2563 trace_module_add_events(mod);
2565 case MODULE_STATE_GOING:
2566 trace_module_remove_events(mod);
2569 mutex_unlock(&event_mutex);
2570 mutex_unlock(&trace_types_lock);
2575 static struct notifier_block trace_module_nb = {
2576 .notifier_call = trace_module_notify,
2577 .priority = 1, /* higher than trace.c module notify */
2579 #endif /* CONFIG_MODULES */
2581 /* Create a new event directory structure for a trace directory. */
2583 __trace_add_event_dirs(struct trace_array *tr)
2585 struct trace_event_call *call;
2588 list_for_each_entry(call, &ftrace_events, list) {
2589 ret = __trace_add_new_event(call, tr);
2591 pr_warn("Could not create directory for event %s\n",
2592 trace_event_name(call));
2596 struct trace_event_file *
2597 find_event_file(struct trace_array *tr, const char *system, const char *event)
2599 struct trace_event_file *file;
2600 struct trace_event_call *call;
2603 list_for_each_entry(file, &tr->events, list) {
2605 call = file->event_call;
2606 name = trace_event_name(call);
2608 if (!name || !call->class || !call->class->reg)
2611 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2614 if (strcmp(event, name) == 0 &&
2615 strcmp(system, call->class->system) == 0)
2621 #ifdef CONFIG_DYNAMIC_FTRACE
2624 #define ENABLE_EVENT_STR "enable_event"
2625 #define DISABLE_EVENT_STR "disable_event"
2627 struct event_probe_data {
2628 struct trace_event_file *file;
2629 unsigned long count;
2635 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2637 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2638 struct event_probe_data *data = *pdata;
2644 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2646 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2650 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2652 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2653 struct event_probe_data *data = *pdata;
2661 /* Skip if the event is in a state we want to switch to */
2662 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2665 if (data->count != -1)
2668 event_enable_probe(ip, parent_ip, _data);
2672 event_enable_print(struct seq_file *m, unsigned long ip,
2673 struct ftrace_probe_ops *ops, void *_data)
2675 struct event_probe_data *data = _data;
2677 seq_printf(m, "%ps:", (void *)ip);
2679 seq_printf(m, "%s:%s:%s",
2680 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2681 data->file->event_call->class->system,
2682 trace_event_name(data->file->event_call));
2684 if (data->count == -1)
2685 seq_puts(m, ":unlimited\n");
2687 seq_printf(m, ":count=%ld\n", data->count);
2693 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2696 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2697 struct event_probe_data *data = *pdata;
2704 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2707 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2708 struct event_probe_data *data = *pdata;
2710 if (WARN_ON_ONCE(data->ref <= 0))
2715 /* Remove the SOFT_MODE flag */
2716 __ftrace_event_enable_disable(data->file, 0, 1);
2717 module_put(data->file->event_call->mod);
2723 static struct ftrace_probe_ops event_enable_probe_ops = {
2724 .func = event_enable_probe,
2725 .print = event_enable_print,
2726 .init = event_enable_init,
2727 .free = event_enable_free,
2730 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2731 .func = event_enable_count_probe,
2732 .print = event_enable_print,
2733 .init = event_enable_init,
2734 .free = event_enable_free,
2737 static struct ftrace_probe_ops event_disable_probe_ops = {
2738 .func = event_enable_probe,
2739 .print = event_enable_print,
2740 .init = event_enable_init,
2741 .free = event_enable_free,
2744 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2745 .func = event_enable_count_probe,
2746 .print = event_enable_print,
2747 .init = event_enable_init,
2748 .free = event_enable_free,
2752 event_enable_func(struct ftrace_hash *hash,
2753 char *glob, char *cmd, char *param, int enabled)
2755 struct trace_array *tr = top_trace_array();
2756 struct trace_event_file *file;
2757 struct ftrace_probe_ops *ops;
2758 struct event_probe_data *data;
2768 /* hash funcs only work with set_ftrace_filter */
2769 if (!enabled || !param)
2772 system = strsep(¶m, ":");
2776 event = strsep(¶m, ":");
2778 mutex_lock(&event_mutex);
2781 file = find_event_file(tr, system, event);
2785 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2788 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2790 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2792 if (glob[0] == '!') {
2793 unregister_ftrace_function_probe_func(glob+1, ops);
2799 data = kzalloc(sizeof(*data), GFP_KERNEL);
2803 data->enable = enable;
2810 number = strsep(¶m, ":");
2813 if (!strlen(number))
2817 * We use the callback data field (which is a pointer)
2820 ret = kstrtoul(number, 0, &data->count);
2825 /* Don't let event modules unload while probe registered */
2826 ret = try_module_get(file->event_call->mod);
2832 ret = __ftrace_event_enable_disable(file, 1, 1);
2835 ret = register_ftrace_function_probe(glob, ops, data);
2837 * The above returns on success the # of functions enabled,
2838 * but if it didn't find any functions it returns zero.
2839 * Consider no functions a failure too.
2846 /* Just return zero, not the number of enabled functions */
2849 mutex_unlock(&event_mutex);
2853 __ftrace_event_enable_disable(file, 0, 1);
2855 module_put(file->event_call->mod);
2861 static struct ftrace_func_command event_enable_cmd = {
2862 .name = ENABLE_EVENT_STR,
2863 .func = event_enable_func,
2866 static struct ftrace_func_command event_disable_cmd = {
2867 .name = DISABLE_EVENT_STR,
2868 .func = event_enable_func,
2871 static __init int register_event_cmds(void)
2875 ret = register_ftrace_command(&event_enable_cmd);
2876 if (WARN_ON(ret < 0))
2878 ret = register_ftrace_command(&event_disable_cmd);
2879 if (WARN_ON(ret < 0))
2880 unregister_ftrace_command(&event_enable_cmd);
2884 static inline int register_event_cmds(void) { return 0; }
2885 #endif /* CONFIG_DYNAMIC_FTRACE */
2888 * The top level array has already had its trace_event_file
2889 * descriptors created in order to allow for early events to
2890 * be recorded. This function is called after the tracefs has been
2891 * initialized, and we now have to create the files associated
2895 __trace_early_add_event_dirs(struct trace_array *tr)
2897 struct trace_event_file *file;
2901 list_for_each_entry(file, &tr->events, list) {
2902 ret = event_create_dir(tr->event_dir, file);
2904 pr_warn("Could not create directory for event %s\n",
2905 trace_event_name(file->event_call));
2910 * For early boot up, the top trace array requires to have
2911 * a list of events that can be enabled. This must be done before
2912 * the filesystem is set up in order to allow events to be traced
2916 __trace_early_add_events(struct trace_array *tr)
2918 struct trace_event_call *call;
2921 list_for_each_entry(call, &ftrace_events, list) {
2922 /* Early boot up should not have any modules loaded */
2923 if (WARN_ON_ONCE(call->mod))
2926 ret = __trace_early_add_new_event(call, tr);
2928 pr_warn("Could not create early event %s\n",
2929 trace_event_name(call));
2933 /* Remove the event directory structure for a trace directory. */
2935 __trace_remove_event_dirs(struct trace_array *tr)
2937 struct trace_event_file *file, *next;
2939 list_for_each_entry_safe(file, next, &tr->events, list)
2940 remove_event_file_dir(file);
2943 static void __add_event_to_tracers(struct trace_event_call *call)
2945 struct trace_array *tr;
2947 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2948 __trace_add_new_event(call, tr);
2951 extern struct trace_event_call *__start_ftrace_events[];
2952 extern struct trace_event_call *__stop_ftrace_events[];
2954 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2956 static __init int setup_trace_event(char *str)
2958 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2959 ring_buffer_expanded = true;
2960 tracing_selftest_disabled = true;
2964 __setup("trace_event=", setup_trace_event);
2966 /* Expects to have event_mutex held when called */
2968 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2970 struct dentry *d_events;
2971 struct dentry *entry;
2973 entry = tracefs_create_file("set_event", 0644, parent,
2974 tr, &ftrace_set_event_fops);
2976 pr_warn("Could not create tracefs 'set_event' entry\n");
2980 d_events = tracefs_create_dir("events", parent);
2982 pr_warn("Could not create tracefs 'events' directory\n");
2986 entry = tracefs_create_file("set_event_pid", 0644, parent,
2987 tr, &ftrace_set_event_pid_fops);
2989 /* ring buffer internal formats */
2990 trace_create_file("header_page", 0444, d_events,
2991 ring_buffer_print_page_header,
2992 &ftrace_show_header_fops);
2994 trace_create_file("header_event", 0444, d_events,
2995 ring_buffer_print_entry_header,
2996 &ftrace_show_header_fops);
2998 trace_create_file("enable", 0644, d_events,
2999 tr, &ftrace_tr_enable_fops);
3001 tr->event_dir = d_events;
3007 * event_trace_add_tracer - add a instance of a trace_array to events
3008 * @parent: The parent dentry to place the files/directories for events in
3009 * @tr: The trace array associated with these events
3011 * When a new instance is created, it needs to set up its events
3012 * directory, as well as other files associated with events. It also
3013 * creates the event hierachry in the @parent/events directory.
3015 * Returns 0 on success.
3017 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3021 mutex_lock(&event_mutex);
3023 ret = create_event_toplevel_files(parent, tr);
3027 down_write(&trace_event_sem);
3028 __trace_add_event_dirs(tr);
3029 up_write(&trace_event_sem);
3032 mutex_unlock(&event_mutex);
3038 * The top trace array already had its file descriptors created.
3039 * Now the files themselves need to be created.
3042 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3046 mutex_lock(&event_mutex);
3048 ret = create_event_toplevel_files(parent, tr);
3052 down_write(&trace_event_sem);
3053 __trace_early_add_event_dirs(tr);
3054 up_write(&trace_event_sem);
3057 mutex_unlock(&event_mutex);
3062 int event_trace_del_tracer(struct trace_array *tr)
3064 mutex_lock(&event_mutex);
3066 /* Disable any event triggers and associated soft-disabled events */
3067 clear_event_triggers(tr);
3069 /* Clear the pid list */
3070 __ftrace_clear_event_pids(tr);
3072 /* Disable any running events */
3073 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3075 /* Access to events are within rcu_read_lock_sched() */
3076 synchronize_sched();
3078 down_write(&trace_event_sem);
3079 __trace_remove_event_dirs(tr);
3080 tracefs_remove_recursive(tr->event_dir);
3081 up_write(&trace_event_sem);
3083 tr->event_dir = NULL;
3085 mutex_unlock(&event_mutex);
3090 static __init int event_trace_memsetup(void)
3092 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3093 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3098 early_enable_events(struct trace_array *tr, bool disable_first)
3100 char *buf = bootup_event_buf;
3105 token = strsep(&buf, ",");
3111 /* Restarting syscalls requires that we stop them first */
3113 ftrace_set_clr_event(tr, token, 0);
3115 ret = ftrace_set_clr_event(tr, token, 1);
3117 pr_warn("Failed to enable trace event: %s\n", token);
3120 /* Put back the comma to allow this to be called again */
3126 static __init int event_trace_enable(void)
3128 struct trace_array *tr = top_trace_array();
3129 struct trace_event_call **iter, *call;
3135 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3138 ret = event_init(call);
3140 list_add(&call->list, &ftrace_events);
3144 * We need the top trace array to have a working set of trace
3145 * points at early init, before the debug files and directories
3146 * are created. Create the file entries now, and attach them
3147 * to the actual file dentries later.
3149 __trace_early_add_events(tr);
3151 early_enable_events(tr, false);
3153 trace_printk_start_comm();
3155 register_event_cmds();
3157 register_trigger_cmds();
3163 * event_trace_enable() is called from trace_event_init() first to
3164 * initialize events and perhaps start any events that are on the
3165 * command line. Unfortunately, there are some events that will not
3166 * start this early, like the system call tracepoints that need
3167 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3168 * is called before pid 1 starts, and this flag is never set, making
3169 * the syscall tracepoint never get reached, but the event is enabled
3170 * regardless (and not doing anything).
3172 static __init int event_trace_enable_again(void)
3174 struct trace_array *tr;
3176 tr = top_trace_array();
3180 early_enable_events(tr, true);
3185 early_initcall(event_trace_enable_again);
3187 static __init int event_trace_init(void)
3189 struct trace_array *tr;
3190 struct dentry *d_tracer;
3191 struct dentry *entry;
3194 tr = top_trace_array();
3198 d_tracer = tracing_init_dentry();
3199 if (IS_ERR(d_tracer))
3202 entry = tracefs_create_file("available_events", 0444, d_tracer,
3203 tr, &ftrace_avail_fops);
3205 pr_warn("Could not create tracefs 'available_events' entry\n");
3207 if (trace_define_generic_fields())
3208 pr_warn("tracing: Failed to allocated generic fields");
3210 if (trace_define_common_fields())
3211 pr_warn("tracing: Failed to allocate common fields");
3213 ret = early_event_add_tracer(d_tracer, tr);
3217 #ifdef CONFIG_MODULES
3218 ret = register_module_notifier(&trace_module_nb);
3220 pr_warn("Failed to register trace events module notifier\n");
3225 void __init trace_event_init(void)
3227 event_trace_memsetup();
3228 init_ftrace_syscalls();
3229 event_trace_enable();
3232 fs_initcall(event_trace_init);
3234 #ifdef CONFIG_FTRACE_STARTUP_TEST
3236 static DEFINE_SPINLOCK(test_spinlock);
3237 static DEFINE_SPINLOCK(test_spinlock_irq);
3238 static DEFINE_MUTEX(test_mutex);
3240 static __init void test_work(struct work_struct *dummy)
3242 spin_lock(&test_spinlock);
3243 spin_lock_irq(&test_spinlock_irq);
3245 spin_unlock_irq(&test_spinlock_irq);
3246 spin_unlock(&test_spinlock);
3248 mutex_lock(&test_mutex);
3250 mutex_unlock(&test_mutex);
3253 static __init int event_test_thread(void *unused)
3257 test_malloc = kmalloc(1234, GFP_KERNEL);
3259 pr_info("failed to kmalloc\n");
3261 schedule_on_each_cpu(test_work);
3265 set_current_state(TASK_INTERRUPTIBLE);
3266 while (!kthread_should_stop()) {
3268 set_current_state(TASK_INTERRUPTIBLE);
3270 __set_current_state(TASK_RUNNING);
3276 * Do various things that may trigger events.
3278 static __init void event_test_stuff(void)
3280 struct task_struct *test_thread;
3282 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3284 kthread_stop(test_thread);
3288 * For every trace event defined, we will test each trace point separately,
3289 * and then by groups, and finally all trace points.
3291 static __init void event_trace_self_tests(void)
3293 struct trace_subsystem_dir *dir;
3294 struct trace_event_file *file;
3295 struct trace_event_call *call;
3296 struct event_subsystem *system;
3297 struct trace_array *tr;
3300 tr = top_trace_array();
3304 pr_info("Running tests on trace events:\n");
3306 list_for_each_entry(file, &tr->events, list) {
3308 call = file->event_call;
3310 /* Only test those that have a probe */
3311 if (!call->class || !call->class->probe)
3315 * Testing syscall events here is pretty useless, but
3316 * we still do it if configured. But this is time consuming.
3317 * What we really need is a user thread to perform the
3318 * syscalls as we test.
3320 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3321 if (call->class->system &&
3322 strcmp(call->class->system, "syscalls") == 0)
3326 pr_info("Testing event %s: ", trace_event_name(call));
3329 * If an event is already enabled, someone is using
3330 * it and the self test should not be on.
3332 if (file->flags & EVENT_FILE_FL_ENABLED) {
3333 pr_warn("Enabled event during self test!\n");
3338 ftrace_event_enable_disable(file, 1);
3340 ftrace_event_enable_disable(file, 0);
3345 /* Now test at the sub system level */
3347 pr_info("Running tests on trace event systems:\n");
3349 list_for_each_entry(dir, &tr->systems, list) {
3351 system = dir->subsystem;
3353 /* the ftrace system is special, skip it */
3354 if (strcmp(system->name, "ftrace") == 0)
3357 pr_info("Testing event system %s: ", system->name);
3359 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3360 if (WARN_ON_ONCE(ret)) {
3361 pr_warn("error enabling system %s\n",
3368 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3369 if (WARN_ON_ONCE(ret)) {
3370 pr_warn("error disabling system %s\n",
3378 /* Test with all events enabled */
3380 pr_info("Running tests on all trace events:\n");
3381 pr_info("Testing all events: ");
3383 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3384 if (WARN_ON_ONCE(ret)) {
3385 pr_warn("error enabling all events\n");
3392 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3393 if (WARN_ON_ONCE(ret)) {
3394 pr_warn("error disabling all events\n");
3401 #ifdef CONFIG_FUNCTION_TRACER
3403 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3405 static struct trace_event_file event_trace_file __initdata;
3408 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3409 struct ftrace_ops *op, struct pt_regs *pt_regs)
3411 struct ring_buffer_event *event;
3412 struct ring_buffer *buffer;
3413 struct ftrace_entry *entry;
3414 unsigned long flags;
3419 pc = preempt_count();
3420 preempt_disable_notrace();
3421 cpu = raw_smp_processor_id();
3422 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3427 local_save_flags(flags);
3429 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3430 TRACE_FN, sizeof(*entry),
3434 entry = ring_buffer_event_data(event);
3436 entry->parent_ip = parent_ip;
3438 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3441 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3442 preempt_enable_notrace();
3445 static struct ftrace_ops trace_ops __initdata =
3447 .func = function_test_events_call,
3448 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3451 static __init void event_trace_self_test_with_function(void)
3455 event_trace_file.tr = top_trace_array();
3456 if (WARN_ON(!event_trace_file.tr))
3459 ret = register_ftrace_function(&trace_ops);
3460 if (WARN_ON(ret < 0)) {
3461 pr_info("Failed to enable function tracer for event tests\n");
3464 pr_info("Running tests again, along with the function tracer\n");
3465 event_trace_self_tests();
3466 unregister_ftrace_function(&trace_ops);
3469 static __init void event_trace_self_test_with_function(void)
3474 static __init int event_trace_self_tests_init(void)
3476 if (!tracing_selftest_disabled) {
3477 event_trace_self_tests();
3478 event_trace_self_test_with_function();
3484 late_initcall(event_trace_self_tests_init);