1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
7 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
12 #define pr_fmt(fmt) fmt
14 #include <linux/workqueue.h>
15 #include <linux/spinlock.h>
16 #include <linux/kthread.h>
17 #include <linux/tracefs.h>
18 #include <linux/uaccess.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 ftrace_event_field *
74 __find_event_field(struct list_head *head, char *name)
76 struct ftrace_event_field *field;
78 list_for_each_entry(field, head, link) {
79 if (!strcmp(field->name, name))
86 struct ftrace_event_field *
87 trace_find_event_field(struct trace_event_call *call, char *name)
89 struct ftrace_event_field *field;
90 struct list_head *head;
92 head = trace_get_fields(call);
93 field = __find_event_field(head, name);
97 field = __find_event_field(&ftrace_generic_fields, name);
101 return __find_event_field(&ftrace_common_fields, name);
104 static int __trace_define_field(struct list_head *head, const char *type,
105 const char *name, int offset, int size,
106 int is_signed, int filter_type)
108 struct ftrace_event_field *field;
110 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
117 if (filter_type == FILTER_OTHER)
118 field->filter_type = filter_assign_type(type);
120 field->filter_type = filter_type;
122 field->offset = offset;
124 field->is_signed = is_signed;
126 list_add(&field->link, head);
131 int trace_define_field(struct trace_event_call *call, const char *type,
132 const char *name, int offset, int size, int is_signed,
135 struct list_head *head;
137 if (WARN_ON(!call->class))
140 head = trace_get_fields(call);
141 return __trace_define_field(head, type, name, offset, size,
142 is_signed, filter_type);
144 EXPORT_SYMBOL_GPL(trace_define_field);
146 #define __generic_field(type, item, filter_type) \
147 ret = __trace_define_field(&ftrace_generic_fields, #type, \
148 #item, 0, 0, is_signed_type(type), \
153 #define __common_field(type, item) \
154 ret = __trace_define_field(&ftrace_common_fields, #type, \
156 offsetof(typeof(ent), item), \
158 is_signed_type(type), FILTER_OTHER); \
162 static int trace_define_generic_fields(void)
166 __generic_field(int, CPU, FILTER_CPU);
167 __generic_field(int, cpu, FILTER_CPU);
168 __generic_field(char *, COMM, FILTER_COMM);
169 __generic_field(char *, comm, FILTER_COMM);
174 static int trace_define_common_fields(void)
177 struct trace_entry ent;
179 __common_field(unsigned short, type);
180 __common_field(unsigned char, flags);
181 __common_field(unsigned char, preempt_count);
182 __common_field(int, pid);
187 static void trace_destroy_fields(struct trace_event_call *call)
189 struct ftrace_event_field *field, *next;
190 struct list_head *head;
192 head = trace_get_fields(call);
193 list_for_each_entry_safe(field, next, head, link) {
194 list_del(&field->link);
195 kmem_cache_free(field_cachep, field);
200 * run-time version of trace_event_get_offsets_<call>() that returns the last
201 * accessible offset of trace fields excluding __dynamic_array bytes
203 int trace_event_get_offsets(struct trace_event_call *call)
205 struct ftrace_event_field *tail;
206 struct list_head *head;
208 head = trace_get_fields(call);
210 * head->next points to the last field with the largest offset,
211 * since it was added last by trace_define_field()
213 tail = list_first_entry(head, struct ftrace_event_field, link);
214 return tail->offset + tail->size;
217 int trace_event_raw_init(struct trace_event_call *call)
221 id = register_trace_event(&call->event);
227 EXPORT_SYMBOL_GPL(trace_event_raw_init);
229 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
231 struct trace_array *tr = trace_file->tr;
232 struct trace_array_cpu *data;
233 struct trace_pid_list *pid_list;
235 pid_list = rcu_dereference_raw(tr->filtered_pids);
239 data = this_cpu_ptr(tr->trace_buffer.data);
241 return data->ignore_pid;
243 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
245 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
246 struct trace_event_file *trace_file,
249 struct trace_event_call *event_call = trace_file->event_call;
251 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
252 trace_event_ignore_this_pid(trace_file))
255 local_save_flags(fbuffer->flags);
256 fbuffer->pc = preempt_count();
258 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
259 * preemption (adding one to the preempt_count). Since we are
260 * interested in the preempt_count at the time the tracepoint was
261 * hit, we need to subtract one to offset the increment.
263 if (IS_ENABLED(CONFIG_PREEMPTION))
265 fbuffer->trace_file = trace_file;
268 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
269 event_call->event.type, len,
270 fbuffer->flags, fbuffer->pc);
274 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
275 return fbuffer->entry;
277 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
279 int trace_event_reg(struct trace_event_call *call,
280 enum trace_reg type, void *data)
282 struct trace_event_file *file = data;
284 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
286 case TRACE_REG_REGISTER:
287 return tracepoint_probe_register(call->tp,
290 case TRACE_REG_UNREGISTER:
291 tracepoint_probe_unregister(call->tp,
296 #ifdef CONFIG_PERF_EVENTS
297 case TRACE_REG_PERF_REGISTER:
298 return tracepoint_probe_register(call->tp,
299 call->class->perf_probe,
301 case TRACE_REG_PERF_UNREGISTER:
302 tracepoint_probe_unregister(call->tp,
303 call->class->perf_probe,
306 case TRACE_REG_PERF_OPEN:
307 case TRACE_REG_PERF_CLOSE:
308 case TRACE_REG_PERF_ADD:
309 case TRACE_REG_PERF_DEL:
315 EXPORT_SYMBOL_GPL(trace_event_reg);
317 void trace_event_enable_cmd_record(bool enable)
319 struct trace_event_file *file;
320 struct trace_array *tr;
322 mutex_lock(&event_mutex);
323 do_for_each_event_file(tr, file) {
325 if (!(file->flags & EVENT_FILE_FL_ENABLED))
329 tracing_start_cmdline_record();
330 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
332 tracing_stop_cmdline_record();
333 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
335 } while_for_each_event_file();
336 mutex_unlock(&event_mutex);
339 void trace_event_enable_tgid_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) {
346 if (!(file->flags & EVENT_FILE_FL_ENABLED))
350 tracing_start_tgid_record();
351 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
353 tracing_stop_tgid_record();
354 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
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);
400 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
401 tracing_stop_tgid_record();
402 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
405 call->class->reg(call, TRACE_REG_UNREGISTER, file);
407 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
408 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
409 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
411 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
415 * When soft_disable is set and enable is set, we want to
416 * register the tracepoint for the event, but leave the event
417 * as is. That means, if the event was already enabled, we do
418 * nothing (but set SOFT_MODE). If the event is disabled, we
419 * set SOFT_DISABLED before enabling the event tracepoint, so
420 * it still seems to be disabled.
423 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
425 if (atomic_inc_return(&file->sm_ref) > 1)
427 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
430 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
431 bool cmd = false, tgid = false;
433 /* Keep the event disabled, when going to SOFT_MODE. */
435 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
437 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
439 tracing_start_cmdline_record();
440 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
443 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
445 tracing_start_tgid_record();
446 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
449 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
452 tracing_stop_cmdline_record();
454 tracing_stop_tgid_record();
455 pr_info("event trace: Could not enable event "
456 "%s\n", trace_event_name(call));
459 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
461 /* WAS_ENABLED gets set but never cleared. */
462 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
467 /* Enable or disable use of trace_buffered_event */
468 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
469 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
470 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
471 trace_buffered_event_enable();
473 trace_buffered_event_disable();
479 int trace_event_enable_disable(struct trace_event_file *file,
480 int enable, int soft_disable)
482 return __ftrace_event_enable_disable(file, enable, soft_disable);
485 static int ftrace_event_enable_disable(struct trace_event_file *file,
488 return __ftrace_event_enable_disable(file, enable, 0);
491 static void ftrace_clear_events(struct trace_array *tr)
493 struct trace_event_file *file;
495 mutex_lock(&event_mutex);
496 list_for_each_entry(file, &tr->events, list) {
497 ftrace_event_enable_disable(file, 0);
499 mutex_unlock(&event_mutex);
503 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
505 struct trace_pid_list *pid_list;
506 struct trace_array *tr = data;
508 pid_list = rcu_dereference_raw(tr->filtered_pids);
509 trace_filter_add_remove_task(pid_list, NULL, task);
513 event_filter_pid_sched_process_fork(void *data,
514 struct task_struct *self,
515 struct task_struct *task)
517 struct trace_pid_list *pid_list;
518 struct trace_array *tr = data;
520 pid_list = rcu_dereference_sched(tr->filtered_pids);
521 trace_filter_add_remove_task(pid_list, self, task);
524 void trace_event_follow_fork(struct trace_array *tr, bool enable)
527 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
529 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
532 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
534 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
540 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
541 struct task_struct *prev, struct task_struct *next)
543 struct trace_array *tr = data;
544 struct trace_pid_list *pid_list;
546 pid_list = rcu_dereference_sched(tr->filtered_pids);
548 this_cpu_write(tr->trace_buffer.data->ignore_pid,
549 trace_ignore_this_task(pid_list, prev) &&
550 trace_ignore_this_task(pid_list, next));
554 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
555 struct task_struct *prev, struct task_struct *next)
557 struct trace_array *tr = data;
558 struct trace_pid_list *pid_list;
560 pid_list = rcu_dereference_sched(tr->filtered_pids);
562 this_cpu_write(tr->trace_buffer.data->ignore_pid,
563 trace_ignore_this_task(pid_list, next));
567 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
569 struct trace_array *tr = data;
570 struct trace_pid_list *pid_list;
572 /* Nothing to do if we are already tracing */
573 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
576 pid_list = rcu_dereference_sched(tr->filtered_pids);
578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
579 trace_ignore_this_task(pid_list, task));
583 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
585 struct trace_array *tr = data;
586 struct trace_pid_list *pid_list;
588 /* Nothing to do if we are not tracing */
589 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
592 pid_list = rcu_dereference_sched(tr->filtered_pids);
594 /* Set tracing if current is enabled */
595 this_cpu_write(tr->trace_buffer.data->ignore_pid,
596 trace_ignore_this_task(pid_list, current));
599 static void __ftrace_clear_event_pids(struct trace_array *tr)
601 struct trace_pid_list *pid_list;
602 struct trace_event_file *file;
605 pid_list = rcu_dereference_protected(tr->filtered_pids,
606 lockdep_is_held(&event_mutex));
610 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
611 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
613 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
614 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
616 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
617 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
619 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
620 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
622 list_for_each_entry(file, &tr->events, list) {
623 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
626 for_each_possible_cpu(cpu)
627 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
629 rcu_assign_pointer(tr->filtered_pids, NULL);
631 /* Wait till all users are no longer using pid filtering */
632 tracepoint_synchronize_unregister();
634 trace_free_pid_list(pid_list);
637 static void ftrace_clear_event_pids(struct trace_array *tr)
639 mutex_lock(&event_mutex);
640 __ftrace_clear_event_pids(tr);
641 mutex_unlock(&event_mutex);
644 static void __put_system(struct event_subsystem *system)
646 struct event_filter *filter = system->filter;
648 WARN_ON_ONCE(system_refcount(system) == 0);
649 if (system_refcount_dec(system))
652 list_del(&system->list);
655 kfree(filter->filter_string);
658 kfree_const(system->name);
662 static void __get_system(struct event_subsystem *system)
664 WARN_ON_ONCE(system_refcount(system) == 0);
665 system_refcount_inc(system);
668 static void __get_system_dir(struct trace_subsystem_dir *dir)
670 WARN_ON_ONCE(dir->ref_count == 0);
672 __get_system(dir->subsystem);
675 static void __put_system_dir(struct trace_subsystem_dir *dir)
677 WARN_ON_ONCE(dir->ref_count == 0);
678 /* If the subsystem is about to be freed, the dir must be too */
679 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
681 __put_system(dir->subsystem);
682 if (!--dir->ref_count)
686 static void put_system(struct trace_subsystem_dir *dir)
688 mutex_lock(&event_mutex);
689 __put_system_dir(dir);
690 mutex_unlock(&event_mutex);
693 static void remove_subsystem(struct trace_subsystem_dir *dir)
698 if (!--dir->nr_events) {
699 tracefs_remove_recursive(dir->entry);
700 list_del(&dir->list);
701 __put_system_dir(dir);
705 static void remove_event_file_dir(struct trace_event_file *file)
707 struct dentry *dir = file->dir;
708 struct dentry *child;
711 spin_lock(&dir->d_lock); /* probably unneeded */
712 list_for_each_entry(child, &dir->d_subdirs, d_child) {
713 if (d_really_is_positive(child)) /* probably unneeded */
714 d_inode(child)->i_private = NULL;
716 spin_unlock(&dir->d_lock);
718 tracefs_remove_recursive(dir);
721 list_del(&file->list);
722 remove_subsystem(file->system);
723 free_event_filter(file->filter);
724 kmem_cache_free(file_cachep, file);
728 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
731 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
732 const char *sub, const char *event, int set)
734 struct trace_event_file *file;
735 struct trace_event_call *call;
740 list_for_each_entry(file, &tr->events, list) {
742 call = file->event_call;
743 name = trace_event_name(call);
745 if (!name || !call->class || !call->class->reg)
748 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
752 strcmp(match, name) != 0 &&
753 strcmp(match, call->class->system) != 0)
756 if (sub && strcmp(sub, call->class->system) != 0)
759 if (event && strcmp(event, name) != 0)
762 ret = ftrace_event_enable_disable(file, set);
765 * Save the first error and return that. Some events
766 * may still have been enabled, but let the user
767 * know that something went wrong.
778 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
779 const char *sub, const char *event, int set)
783 mutex_lock(&event_mutex);
784 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
785 mutex_unlock(&event_mutex);
790 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
792 char *event = NULL, *sub = NULL, *match;
796 * The buf format can be <subsystem>:<event-name>
797 * *:<event-name> means any event by that name.
798 * :<event-name> is the same.
800 * <subsystem>:* means all events in that subsystem
801 * <subsystem>: means the same.
803 * <name> (no ':') means all events in a subsystem with
804 * the name <name> or any event that matches <name>
807 match = strsep(&buf, ":");
813 if (!strlen(sub) || strcmp(sub, "*") == 0)
815 if (!strlen(event) || strcmp(event, "*") == 0)
819 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
821 /* Put back the colon to allow this to be called again */
827 EXPORT_SYMBOL_GPL(ftrace_set_clr_event);
830 * trace_set_clr_event - enable or disable an event
831 * @system: system name to match (NULL for any system)
832 * @event: event name to match (NULL for all events, within system)
833 * @set: 1 to enable, 0 to disable
835 * This is a way for other parts of the kernel to enable or disable
838 * Returns 0 on success, -EINVAL if the parameters do not match any
841 int trace_set_clr_event(const char *system, const char *event, int set)
843 struct trace_array *tr = top_trace_array();
848 return __ftrace_set_clr_event(tr, NULL, system, event, set);
850 EXPORT_SYMBOL_GPL(trace_set_clr_event);
852 /* 128 should be much more than enough */
853 #define EVENT_BUF_SIZE 127
856 ftrace_event_write(struct file *file, const char __user *ubuf,
857 size_t cnt, loff_t *ppos)
859 struct trace_parser parser;
860 struct seq_file *m = file->private_data;
861 struct trace_array *tr = m->private;
867 ret = tracing_update_buffers();
871 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
874 read = trace_get_user(&parser, ubuf, cnt, ppos);
876 if (read >= 0 && trace_parser_loaded((&parser))) {
879 if (*parser.buffer == '!')
882 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
890 trace_parser_put(&parser);
896 t_next(struct seq_file *m, void *v, loff_t *pos)
898 struct trace_event_file *file = v;
899 struct trace_event_call *call;
900 struct trace_array *tr = m->private;
904 list_for_each_entry_continue(file, &tr->events, list) {
905 call = file->event_call;
907 * The ftrace subsystem is for showing formats only.
908 * They can not be enabled or disabled via the event files.
910 if (call->class && call->class->reg &&
911 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
918 static void *t_start(struct seq_file *m, loff_t *pos)
920 struct trace_event_file *file;
921 struct trace_array *tr = m->private;
924 mutex_lock(&event_mutex);
926 file = list_entry(&tr->events, struct trace_event_file, list);
927 for (l = 0; l <= *pos; ) {
928 file = t_next(m, file, &l);
936 s_next(struct seq_file *m, void *v, loff_t *pos)
938 struct trace_event_file *file = v;
939 struct trace_array *tr = m->private;
943 list_for_each_entry_continue(file, &tr->events, list) {
944 if (file->flags & EVENT_FILE_FL_ENABLED)
951 static void *s_start(struct seq_file *m, loff_t *pos)
953 struct trace_event_file *file;
954 struct trace_array *tr = m->private;
957 mutex_lock(&event_mutex);
959 file = list_entry(&tr->events, struct trace_event_file, list);
960 for (l = 0; l <= *pos; ) {
961 file = s_next(m, file, &l);
968 static int t_show(struct seq_file *m, void *v)
970 struct trace_event_file *file = v;
971 struct trace_event_call *call = file->event_call;
973 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
974 seq_printf(m, "%s:", call->class->system);
975 seq_printf(m, "%s\n", trace_event_name(call));
980 static void t_stop(struct seq_file *m, void *p)
982 mutex_unlock(&event_mutex);
986 p_next(struct seq_file *m, void *v, loff_t *pos)
988 struct trace_array *tr = m->private;
989 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
991 return trace_pid_next(pid_list, v, pos);
994 static void *p_start(struct seq_file *m, loff_t *pos)
997 struct trace_pid_list *pid_list;
998 struct trace_array *tr = m->private;
1001 * Grab the mutex, to keep calls to p_next() having the same
1002 * tr->filtered_pids as p_start() has.
1003 * If we just passed the tr->filtered_pids around, then RCU would
1004 * have been enough, but doing that makes things more complex.
1006 mutex_lock(&event_mutex);
1007 rcu_read_lock_sched();
1009 pid_list = rcu_dereference_sched(tr->filtered_pids);
1014 return trace_pid_start(pid_list, pos);
1017 static void p_stop(struct seq_file *m, void *p)
1020 rcu_read_unlock_sched();
1021 mutex_unlock(&event_mutex);
1025 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1028 struct trace_event_file *file;
1029 unsigned long flags;
1032 mutex_lock(&event_mutex);
1033 file = event_file_data(filp);
1035 flags = file->flags;
1036 mutex_unlock(&event_mutex);
1041 if (flags & EVENT_FILE_FL_ENABLED &&
1042 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1045 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1046 flags & EVENT_FILE_FL_SOFT_MODE)
1051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1055 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1058 struct trace_event_file *file;
1062 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1066 ret = tracing_update_buffers();
1074 mutex_lock(&event_mutex);
1075 file = event_file_data(filp);
1077 ret = ftrace_event_enable_disable(file, val);
1078 mutex_unlock(&event_mutex);
1087 return ret ? ret : cnt;
1091 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1094 const char set_to_char[4] = { '?', '0', '1', 'X' };
1095 struct trace_subsystem_dir *dir = filp->private_data;
1096 struct event_subsystem *system = dir->subsystem;
1097 struct trace_event_call *call;
1098 struct trace_event_file *file;
1099 struct trace_array *tr = dir->tr;
1104 mutex_lock(&event_mutex);
1105 list_for_each_entry(file, &tr->events, list) {
1106 call = file->event_call;
1107 if (!trace_event_name(call) || !call->class || !call->class->reg)
1110 if (system && strcmp(call->class->system, system->name) != 0)
1114 * We need to find out if all the events are set
1115 * or if all events or cleared, or if we have
1118 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1121 * If we have a mixture, no need to look further.
1126 mutex_unlock(&event_mutex);
1128 buf[0] = set_to_char[set];
1131 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1137 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1140 struct trace_subsystem_dir *dir = filp->private_data;
1141 struct event_subsystem *system = dir->subsystem;
1142 const char *name = NULL;
1146 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1150 ret = tracing_update_buffers();
1154 if (val != 0 && val != 1)
1158 * Opening of "enable" adds a ref count to system,
1159 * so the name is safe to use.
1162 name = system->name;
1164 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1178 FORMAT_FIELD_SEPERATOR = 2,
1179 FORMAT_PRINTFMT = 3,
1182 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1184 struct trace_event_call *call = event_file_data(m->private);
1185 struct list_head *common_head = &ftrace_common_fields;
1186 struct list_head *head = trace_get_fields(call);
1187 struct list_head *node = v;
1191 switch ((unsigned long)v) {
1196 case FORMAT_FIELD_SEPERATOR:
1200 case FORMAT_PRINTFMT:
1206 if (node == common_head)
1207 return (void *)FORMAT_FIELD_SEPERATOR;
1208 else if (node == head)
1209 return (void *)FORMAT_PRINTFMT;
1214 static int f_show(struct seq_file *m, void *v)
1216 struct trace_event_call *call = event_file_data(m->private);
1217 struct ftrace_event_field *field;
1218 const char *array_descriptor;
1220 switch ((unsigned long)v) {
1222 seq_printf(m, "name: %s\n", trace_event_name(call));
1223 seq_printf(m, "ID: %d\n", call->event.type);
1224 seq_puts(m, "format:\n");
1227 case FORMAT_FIELD_SEPERATOR:
1231 case FORMAT_PRINTFMT:
1232 seq_printf(m, "\nprint fmt: %s\n",
1237 field = list_entry(v, struct ftrace_event_field, link);
1239 * Smartly shows the array type(except dynamic array).
1242 * If TYPE := TYPE[LEN], it is shown:
1243 * field:TYPE VAR[LEN]
1245 array_descriptor = strchr(field->type, '[');
1247 if (str_has_prefix(field->type, "__data_loc"))
1248 array_descriptor = NULL;
1250 if (!array_descriptor)
1251 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1252 field->type, field->name, field->offset,
1253 field->size, !!field->is_signed);
1255 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1256 (int)(array_descriptor - field->type),
1257 field->type, field->name,
1258 array_descriptor, field->offset,
1259 field->size, !!field->is_signed);
1264 static void *f_start(struct seq_file *m, loff_t *pos)
1266 void *p = (void *)FORMAT_HEADER;
1269 /* ->stop() is called even if ->start() fails */
1270 mutex_lock(&event_mutex);
1271 if (!event_file_data(m->private))
1272 return ERR_PTR(-ENODEV);
1274 while (l < *pos && p)
1275 p = f_next(m, p, &l);
1280 static void f_stop(struct seq_file *m, void *p)
1282 mutex_unlock(&event_mutex);
1285 static const struct seq_operations trace_format_seq_ops = {
1292 static int trace_format_open(struct inode *inode, struct file *file)
1297 ret = seq_open(file, &trace_format_seq_ops);
1301 m = file->private_data;
1308 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1310 int id = (long)event_file_data(filp);
1317 len = sprintf(buf, "%d\n", id);
1319 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1323 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1326 struct trace_event_file *file;
1327 struct trace_seq *s;
1333 s = kmalloc(sizeof(*s), GFP_KERNEL);
1340 mutex_lock(&event_mutex);
1341 file = event_file_data(filp);
1343 print_event_filter(file, s);
1344 mutex_unlock(&event_mutex);
1347 r = simple_read_from_buffer(ubuf, cnt, ppos,
1348 s->buffer, trace_seq_used(s));
1356 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1359 struct trace_event_file *file;
1363 if (cnt >= PAGE_SIZE)
1366 buf = memdup_user_nul(ubuf, cnt);
1368 return PTR_ERR(buf);
1370 mutex_lock(&event_mutex);
1371 file = event_file_data(filp);
1373 err = apply_event_filter(file, buf);
1374 mutex_unlock(&event_mutex);
1385 static LIST_HEAD(event_subsystems);
1387 static int subsystem_open(struct inode *inode, struct file *filp)
1389 struct event_subsystem *system = NULL;
1390 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1391 struct trace_array *tr;
1394 if (tracing_is_disabled())
1397 /* Make sure the system still exists */
1398 mutex_lock(&event_mutex);
1399 mutex_lock(&trace_types_lock);
1400 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1401 list_for_each_entry(dir, &tr->systems, list) {
1402 if (dir == inode->i_private) {
1403 /* Don't open systems with no events */
1404 if (dir->nr_events) {
1405 __get_system_dir(dir);
1406 system = dir->subsystem;
1413 mutex_unlock(&trace_types_lock);
1414 mutex_unlock(&event_mutex);
1419 /* Some versions of gcc think dir can be uninitialized here */
1422 /* Still need to increment the ref count of the system */
1423 if (trace_array_get(tr) < 0) {
1428 ret = tracing_open_generic(inode, filp);
1430 trace_array_put(tr);
1437 static int system_tr_open(struct inode *inode, struct file *filp)
1439 struct trace_subsystem_dir *dir;
1440 struct trace_array *tr = inode->i_private;
1443 if (tracing_is_disabled())
1446 if (trace_array_get(tr) < 0)
1449 /* Make a temporary dir that has no system but points to tr */
1450 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1452 trace_array_put(tr);
1458 ret = tracing_open_generic(inode, filp);
1460 trace_array_put(tr);
1465 filp->private_data = dir;
1470 static int subsystem_release(struct inode *inode, struct file *file)
1472 struct trace_subsystem_dir *dir = file->private_data;
1474 trace_array_put(dir->tr);
1477 * If dir->subsystem is NULL, then this is a temporary
1478 * descriptor that was made for a trace_array to enable
1490 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1493 struct trace_subsystem_dir *dir = filp->private_data;
1494 struct event_subsystem *system = dir->subsystem;
1495 struct trace_seq *s;
1501 s = kmalloc(sizeof(*s), GFP_KERNEL);
1507 print_subsystem_event_filter(system, s);
1508 r = simple_read_from_buffer(ubuf, cnt, ppos,
1509 s->buffer, trace_seq_used(s));
1517 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1520 struct trace_subsystem_dir *dir = filp->private_data;
1524 if (cnt >= PAGE_SIZE)
1527 buf = memdup_user_nul(ubuf, cnt);
1529 return PTR_ERR(buf);
1531 err = apply_subsystem_event_filter(dir, buf);
1542 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1544 int (*func)(struct trace_seq *s) = filp->private_data;
1545 struct trace_seq *s;
1551 s = kmalloc(sizeof(*s), GFP_KERNEL);
1558 r = simple_read_from_buffer(ubuf, cnt, ppos,
1559 s->buffer, trace_seq_used(s));
1566 static void ignore_task_cpu(void *data)
1568 struct trace_array *tr = data;
1569 struct trace_pid_list *pid_list;
1572 * This function is called by on_each_cpu() while the
1573 * event_mutex is held.
1575 pid_list = rcu_dereference_protected(tr->filtered_pids,
1576 mutex_is_locked(&event_mutex));
1578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1579 trace_ignore_this_task(pid_list, current));
1583 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1584 size_t cnt, loff_t *ppos)
1586 struct seq_file *m = filp->private_data;
1587 struct trace_array *tr = m->private;
1588 struct trace_pid_list *filtered_pids = NULL;
1589 struct trace_pid_list *pid_list;
1590 struct trace_event_file *file;
1596 ret = tracing_update_buffers();
1600 mutex_lock(&event_mutex);
1602 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1603 lockdep_is_held(&event_mutex));
1605 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1609 rcu_assign_pointer(tr->filtered_pids, pid_list);
1611 list_for_each_entry(file, &tr->events, list) {
1612 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1615 if (filtered_pids) {
1616 tracepoint_synchronize_unregister();
1617 trace_free_pid_list(filtered_pids);
1618 } else if (pid_list) {
1620 * Register a probe that is called before all other probes
1621 * to set ignore_pid if next or prev do not match.
1622 * Register a probe this is called after all other probes
1623 * to only keep ignore_pid set if next pid matches.
1625 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1627 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1630 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1632 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1635 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1637 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1640 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1642 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1647 * Ignoring of pids is done at task switch. But we have to
1648 * check for those tasks that are currently running.
1649 * Always do this in case a pid was appended or removed.
1651 on_each_cpu(ignore_task_cpu, tr, 1);
1654 mutex_unlock(&event_mutex);
1662 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1663 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1664 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1665 static int ftrace_event_release(struct inode *inode, struct file *file);
1667 static const struct seq_operations show_event_seq_ops = {
1674 static const struct seq_operations show_set_event_seq_ops = {
1681 static const struct seq_operations show_set_pid_seq_ops = {
1684 .show = trace_pid_show,
1688 static const struct file_operations ftrace_avail_fops = {
1689 .open = ftrace_event_avail_open,
1691 .llseek = seq_lseek,
1692 .release = seq_release,
1695 static const struct file_operations ftrace_set_event_fops = {
1696 .open = ftrace_event_set_open,
1698 .write = ftrace_event_write,
1699 .llseek = seq_lseek,
1700 .release = ftrace_event_release,
1703 static const struct file_operations ftrace_set_event_pid_fops = {
1704 .open = ftrace_event_set_pid_open,
1706 .write = ftrace_event_pid_write,
1707 .llseek = seq_lseek,
1708 .release = ftrace_event_release,
1711 static const struct file_operations ftrace_enable_fops = {
1712 .open = tracing_open_generic,
1713 .read = event_enable_read,
1714 .write = event_enable_write,
1715 .llseek = default_llseek,
1718 static const struct file_operations ftrace_event_format_fops = {
1719 .open = trace_format_open,
1721 .llseek = seq_lseek,
1722 .release = seq_release,
1725 static const struct file_operations ftrace_event_id_fops = {
1726 .read = event_id_read,
1727 .llseek = default_llseek,
1730 static const struct file_operations ftrace_event_filter_fops = {
1731 .open = tracing_open_generic,
1732 .read = event_filter_read,
1733 .write = event_filter_write,
1734 .llseek = default_llseek,
1737 static const struct file_operations ftrace_subsystem_filter_fops = {
1738 .open = subsystem_open,
1739 .read = subsystem_filter_read,
1740 .write = subsystem_filter_write,
1741 .llseek = default_llseek,
1742 .release = subsystem_release,
1745 static const struct file_operations ftrace_system_enable_fops = {
1746 .open = subsystem_open,
1747 .read = system_enable_read,
1748 .write = system_enable_write,
1749 .llseek = default_llseek,
1750 .release = subsystem_release,
1753 static const struct file_operations ftrace_tr_enable_fops = {
1754 .open = system_tr_open,
1755 .read = system_enable_read,
1756 .write = system_enable_write,
1757 .llseek = default_llseek,
1758 .release = subsystem_release,
1761 static const struct file_operations ftrace_show_header_fops = {
1762 .open = tracing_open_generic,
1763 .read = show_header,
1764 .llseek = default_llseek,
1768 ftrace_event_open(struct inode *inode, struct file *file,
1769 const struct seq_operations *seq_ops)
1774 ret = seq_open(file, seq_ops);
1777 m = file->private_data;
1778 /* copy tr over to seq ops */
1779 m->private = inode->i_private;
1784 static int ftrace_event_release(struct inode *inode, struct file *file)
1786 struct trace_array *tr = inode->i_private;
1788 trace_array_put(tr);
1790 return seq_release(inode, file);
1794 ftrace_event_avail_open(struct inode *inode, struct file *file)
1796 const struct seq_operations *seq_ops = &show_event_seq_ops;
1798 return ftrace_event_open(inode, file, seq_ops);
1802 ftrace_event_set_open(struct inode *inode, struct file *file)
1804 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1805 struct trace_array *tr = inode->i_private;
1808 if (trace_array_get(tr) < 0)
1811 if ((file->f_mode & FMODE_WRITE) &&
1812 (file->f_flags & O_TRUNC))
1813 ftrace_clear_events(tr);
1815 ret = ftrace_event_open(inode, file, seq_ops);
1817 trace_array_put(tr);
1822 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1824 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1825 struct trace_array *tr = inode->i_private;
1828 if (trace_array_get(tr) < 0)
1831 if ((file->f_mode & FMODE_WRITE) &&
1832 (file->f_flags & O_TRUNC))
1833 ftrace_clear_event_pids(tr);
1835 ret = ftrace_event_open(inode, file, seq_ops);
1837 trace_array_put(tr);
1841 static struct event_subsystem *
1842 create_new_subsystem(const char *name)
1844 struct event_subsystem *system;
1846 /* need to create new entry */
1847 system = kmalloc(sizeof(*system), GFP_KERNEL);
1851 system->ref_count = 1;
1853 /* Only allocate if dynamic (kprobes and modules) */
1854 system->name = kstrdup_const(name, GFP_KERNEL);
1858 system->filter = NULL;
1860 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1861 if (!system->filter)
1864 list_add(&system->list, &event_subsystems);
1869 kfree_const(system->name);
1874 static struct dentry *
1875 event_subsystem_dir(struct trace_array *tr, const char *name,
1876 struct trace_event_file *file, struct dentry *parent)
1878 struct trace_subsystem_dir *dir;
1879 struct event_subsystem *system;
1880 struct dentry *entry;
1882 /* First see if we did not already create this dir */
1883 list_for_each_entry(dir, &tr->systems, list) {
1884 system = dir->subsystem;
1885 if (strcmp(system->name, name) == 0) {
1892 /* Now see if the system itself exists. */
1893 list_for_each_entry(system, &event_subsystems, list) {
1894 if (strcmp(system->name, name) == 0)
1897 /* Reset system variable when not found */
1898 if (&system->list == &event_subsystems)
1901 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1906 system = create_new_subsystem(name);
1910 __get_system(system);
1912 dir->entry = tracefs_create_dir(name, parent);
1914 pr_warn("Failed to create system directory %s\n", name);
1915 __put_system(system);
1922 dir->subsystem = system;
1925 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1926 &ftrace_subsystem_filter_fops);
1928 kfree(system->filter);
1929 system->filter = NULL;
1930 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1933 trace_create_file("enable", 0644, dir->entry, dir,
1934 &ftrace_system_enable_fops);
1936 list_add(&dir->list, &tr->systems);
1943 /* Only print this message if failed on memory allocation */
1944 if (!dir || !system)
1945 pr_warn("No memory to create event subsystem %s\n", name);
1950 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1952 struct trace_event_call *call = file->event_call;
1953 struct trace_array *tr = file->tr;
1954 struct list_head *head;
1955 struct dentry *d_events;
1960 * If the trace point header did not define TRACE_SYSTEM
1961 * then the system would be called "TRACE_SYSTEM".
1963 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1964 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1970 name = trace_event_name(call);
1971 file->dir = tracefs_create_dir(name, d_events);
1973 pr_warn("Could not create tracefs '%s' directory\n", name);
1977 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1978 trace_create_file("enable", 0644, file->dir, file,
1979 &ftrace_enable_fops);
1981 #ifdef CONFIG_PERF_EVENTS
1982 if (call->event.type && call->class->reg)
1983 trace_create_file("id", 0444, file->dir,
1984 (void *)(long)call->event.type,
1985 &ftrace_event_id_fops);
1989 * Other events may have the same class. Only update
1990 * the fields if they are not already defined.
1992 head = trace_get_fields(call);
1993 if (list_empty(head)) {
1994 ret = call->class->define_fields(call);
1996 pr_warn("Could not initialize trace point events/%s\n",
2003 * Only event directories that can be enabled should have
2004 * triggers or filters.
2006 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2007 trace_create_file("filter", 0644, file->dir, file,
2008 &ftrace_event_filter_fops);
2010 trace_create_file("trigger", 0644, file->dir, file,
2011 &event_trigger_fops);
2014 #ifdef CONFIG_HIST_TRIGGERS
2015 trace_create_file("hist", 0444, file->dir, file,
2018 trace_create_file("format", 0444, file->dir, call,
2019 &ftrace_event_format_fops);
2024 static void remove_event_from_tracers(struct trace_event_call *call)
2026 struct trace_event_file *file;
2027 struct trace_array *tr;
2029 do_for_each_event_file_safe(tr, file) {
2030 if (file->event_call != call)
2033 remove_event_file_dir(file);
2035 * The do_for_each_event_file_safe() is
2036 * a double loop. After finding the call for this
2037 * trace_array, we use break to jump to the next
2041 } while_for_each_event_file();
2044 static void event_remove(struct trace_event_call *call)
2046 struct trace_array *tr;
2047 struct trace_event_file *file;
2049 do_for_each_event_file(tr, file) {
2050 if (file->event_call != call)
2053 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2054 tr->clear_trace = true;
2056 ftrace_event_enable_disable(file, 0);
2058 * The do_for_each_event_file() is
2059 * a double loop. After finding the call for this
2060 * trace_array, we use break to jump to the next
2064 } while_for_each_event_file();
2066 if (call->event.funcs)
2067 __unregister_trace_event(&call->event);
2068 remove_event_from_tracers(call);
2069 list_del(&call->list);
2072 static int event_init(struct trace_event_call *call)
2077 name = trace_event_name(call);
2081 if (call->class->raw_init) {
2082 ret = call->class->raw_init(call);
2083 if (ret < 0 && ret != -ENOSYS)
2084 pr_warn("Could not initialize trace events/%s\n", name);
2091 __register_event(struct trace_event_call *call, struct module *mod)
2095 ret = event_init(call);
2099 list_add(&call->list, &ftrace_events);
2105 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2110 /* Find the length of the eval value as a string */
2111 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2112 /* Make sure there's enough room to replace the string with the value */
2116 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2118 /* Get the rest of the string of ptr */
2119 rlen = strlen(ptr + len);
2120 memmove(ptr + elen, ptr + len, rlen);
2121 /* Make sure we end the new string */
2122 ptr[elen + rlen] = 0;
2127 static void update_event_printk(struct trace_event_call *call,
2128 struct trace_eval_map *map)
2132 int len = strlen(map->eval_string);
2134 for (ptr = call->print_fmt; *ptr; ptr++) {
2148 if (isdigit(*ptr)) {
2152 /* Check for alpha chars like ULL */
2153 } while (isalnum(*ptr));
2157 * A number must have some kind of delimiter after
2158 * it, and we can ignore that too.
2162 if (isalpha(*ptr) || *ptr == '_') {
2163 if (strncmp(map->eval_string, ptr, len) == 0 &&
2164 !isalnum(ptr[len]) && ptr[len] != '_') {
2165 ptr = eval_replace(ptr, map, len);
2166 /* enum/sizeof string smaller than value */
2167 if (WARN_ON_ONCE(!ptr))
2170 * No need to decrement here, as eval_replace()
2171 * returns the pointer to the character passed
2172 * the eval, and two evals can not be placed
2173 * back to back without something in between.
2174 * We can skip that something in between.
2181 } while (isalnum(*ptr) || *ptr == '_');
2185 * If what comes after this variable is a '.' or
2186 * '->' then we can continue to ignore that string.
2188 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2189 ptr += *ptr == '.' ? 1 : 2;
2195 * Once again, we can skip the delimiter that came
2203 void trace_event_eval_update(struct trace_eval_map **map, int len)
2205 struct trace_event_call *call, *p;
2206 const char *last_system = NULL;
2211 down_write(&trace_event_sem);
2212 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2213 /* events are usually grouped together with systems */
2214 if (!last_system || call->class->system != last_system) {
2217 last_system = call->class->system;
2221 * Since calls are grouped by systems, the likelyhood that the
2222 * next call in the iteration belongs to the same system as the
2223 * previous call is high. As an optimization, we skip seaching
2224 * for a map[] that matches the call's system if the last call
2225 * was from the same system. That's what last_i is for. If the
2226 * call has the same system as the previous call, then last_i
2227 * will be the index of the first map[] that has a matching
2230 for (i = last_i; i < len; i++) {
2231 if (call->class->system == map[i]->system) {
2232 /* Save the first system if need be */
2237 update_event_printk(call, map[i]);
2241 up_write(&trace_event_sem);
2244 static struct trace_event_file *
2245 trace_create_new_event(struct trace_event_call *call,
2246 struct trace_array *tr)
2248 struct trace_event_file *file;
2250 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2254 file->event_call = call;
2256 atomic_set(&file->sm_ref, 0);
2257 atomic_set(&file->tm_ref, 0);
2258 INIT_LIST_HEAD(&file->triggers);
2259 list_add(&file->list, &tr->events);
2264 /* Add an event to a trace directory */
2266 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2268 struct trace_event_file *file;
2270 file = trace_create_new_event(call, tr);
2274 return event_create_dir(tr->event_dir, file);
2278 * Just create a decriptor for early init. A descriptor is required
2279 * for enabling events at boot. We want to enable events before
2280 * the filesystem is initialized.
2283 __trace_early_add_new_event(struct trace_event_call *call,
2284 struct trace_array *tr)
2286 struct trace_event_file *file;
2288 file = trace_create_new_event(call, tr);
2295 struct ftrace_module_file_ops;
2296 static void __add_event_to_tracers(struct trace_event_call *call);
2298 /* Add an additional event_call dynamically */
2299 int trace_add_event_call(struct trace_event_call *call)
2302 lockdep_assert_held(&event_mutex);
2304 mutex_lock(&trace_types_lock);
2306 ret = __register_event(call, NULL);
2308 __add_event_to_tracers(call);
2310 mutex_unlock(&trace_types_lock);
2315 * Must be called under locking of trace_types_lock, event_mutex and
2318 static void __trace_remove_event_call(struct trace_event_call *call)
2321 trace_destroy_fields(call);
2322 free_event_filter(call->filter);
2323 call->filter = NULL;
2326 static int probe_remove_event_call(struct trace_event_call *call)
2328 struct trace_array *tr;
2329 struct trace_event_file *file;
2331 #ifdef CONFIG_PERF_EVENTS
2332 if (call->perf_refcount)
2335 do_for_each_event_file(tr, file) {
2336 if (file->event_call != call)
2339 * We can't rely on ftrace_event_enable_disable(enable => 0)
2340 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2341 * TRACE_REG_UNREGISTER.
2343 if (file->flags & EVENT_FILE_FL_ENABLED)
2346 * The do_for_each_event_file_safe() is
2347 * a double loop. After finding the call for this
2348 * trace_array, we use break to jump to the next
2352 } while_for_each_event_file();
2354 __trace_remove_event_call(call);
2359 /* Remove an event_call */
2360 int trace_remove_event_call(struct trace_event_call *call)
2364 lockdep_assert_held(&event_mutex);
2366 mutex_lock(&trace_types_lock);
2367 down_write(&trace_event_sem);
2368 ret = probe_remove_event_call(call);
2369 up_write(&trace_event_sem);
2370 mutex_unlock(&trace_types_lock);
2375 #define for_each_event(event, start, end) \
2376 for (event = start; \
2377 (unsigned long)event < (unsigned long)end; \
2380 #ifdef CONFIG_MODULES
2382 static void trace_module_add_events(struct module *mod)
2384 struct trace_event_call **call, **start, **end;
2386 if (!mod->num_trace_events)
2389 /* Don't add infrastructure for mods without tracepoints */
2390 if (trace_module_has_bad_taint(mod)) {
2391 pr_err("%s: module has bad taint, not creating trace events\n",
2396 start = mod->trace_events;
2397 end = mod->trace_events + mod->num_trace_events;
2399 for_each_event(call, start, end) {
2400 __register_event(*call, mod);
2401 __add_event_to_tracers(*call);
2405 static void trace_module_remove_events(struct module *mod)
2407 struct trace_event_call *call, *p;
2409 down_write(&trace_event_sem);
2410 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2411 if (call->mod == mod)
2412 __trace_remove_event_call(call);
2414 up_write(&trace_event_sem);
2417 * It is safest to reset the ring buffer if the module being unloaded
2418 * registered any events that were used. The only worry is if
2419 * a new module gets loaded, and takes on the same id as the events
2420 * of this module. When printing out the buffer, traced events left
2421 * over from this module may be passed to the new module events and
2422 * unexpected results may occur.
2424 tracing_reset_all_online_cpus();
2427 static int trace_module_notify(struct notifier_block *self,
2428 unsigned long val, void *data)
2430 struct module *mod = data;
2432 mutex_lock(&event_mutex);
2433 mutex_lock(&trace_types_lock);
2435 case MODULE_STATE_COMING:
2436 trace_module_add_events(mod);
2438 case MODULE_STATE_GOING:
2439 trace_module_remove_events(mod);
2442 mutex_unlock(&trace_types_lock);
2443 mutex_unlock(&event_mutex);
2448 static struct notifier_block trace_module_nb = {
2449 .notifier_call = trace_module_notify,
2450 .priority = 1, /* higher than trace.c module notify */
2452 #endif /* CONFIG_MODULES */
2454 /* Create a new event directory structure for a trace directory. */
2456 __trace_add_event_dirs(struct trace_array *tr)
2458 struct trace_event_call *call;
2461 list_for_each_entry(call, &ftrace_events, list) {
2462 ret = __trace_add_new_event(call, tr);
2464 pr_warn("Could not create directory for event %s\n",
2465 trace_event_name(call));
2469 /* Returns any file that matches the system and event */
2470 struct trace_event_file *
2471 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2473 struct trace_event_file *file;
2474 struct trace_event_call *call;
2477 list_for_each_entry(file, &tr->events, list) {
2479 call = file->event_call;
2480 name = trace_event_name(call);
2482 if (!name || !call->class)
2485 if (strcmp(event, name) == 0 &&
2486 strcmp(system, call->class->system) == 0)
2492 /* Returns valid trace event files that match system and event */
2493 struct trace_event_file *
2494 find_event_file(struct trace_array *tr, const char *system, const char *event)
2496 struct trace_event_file *file;
2498 file = __find_event_file(tr, system, event);
2499 if (!file || !file->event_call->class->reg ||
2500 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2506 #ifdef CONFIG_DYNAMIC_FTRACE
2509 #define ENABLE_EVENT_STR "enable_event"
2510 #define DISABLE_EVENT_STR "disable_event"
2512 struct event_probe_data {
2513 struct trace_event_file *file;
2514 unsigned long count;
2519 static void update_event_probe(struct event_probe_data *data)
2522 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2524 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2528 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2529 struct trace_array *tr, struct ftrace_probe_ops *ops,
2532 struct ftrace_func_mapper *mapper = data;
2533 struct event_probe_data *edata;
2536 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2537 if (!pdata || !*pdata)
2541 update_event_probe(edata);
2545 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2546 struct trace_array *tr, struct ftrace_probe_ops *ops,
2549 struct ftrace_func_mapper *mapper = data;
2550 struct event_probe_data *edata;
2553 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2554 if (!pdata || !*pdata)
2562 /* Skip if the event is in a state we want to switch to */
2563 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2566 if (edata->count != -1)
2569 update_event_probe(edata);
2573 event_enable_print(struct seq_file *m, unsigned long ip,
2574 struct ftrace_probe_ops *ops, void *data)
2576 struct ftrace_func_mapper *mapper = data;
2577 struct event_probe_data *edata;
2580 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2582 if (WARN_ON_ONCE(!pdata || !*pdata))
2587 seq_printf(m, "%ps:", (void *)ip);
2589 seq_printf(m, "%s:%s:%s",
2590 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2591 edata->file->event_call->class->system,
2592 trace_event_name(edata->file->event_call));
2594 if (edata->count == -1)
2595 seq_puts(m, ":unlimited\n");
2597 seq_printf(m, ":count=%ld\n", edata->count);
2603 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2604 unsigned long ip, void *init_data, void **data)
2606 struct ftrace_func_mapper *mapper = *data;
2607 struct event_probe_data *edata = init_data;
2611 mapper = allocate_ftrace_func_mapper();
2617 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2626 static int free_probe_data(void *data)
2628 struct event_probe_data *edata = data;
2632 /* Remove the SOFT_MODE flag */
2633 __ftrace_event_enable_disable(edata->file, 0, 1);
2634 module_put(edata->file->event_call->mod);
2641 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2642 unsigned long ip, void *data)
2644 struct ftrace_func_mapper *mapper = data;
2645 struct event_probe_data *edata;
2650 free_ftrace_func_mapper(mapper, free_probe_data);
2654 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2656 if (WARN_ON_ONCE(!edata))
2659 if (WARN_ON_ONCE(edata->ref <= 0))
2662 free_probe_data(edata);
2665 static struct ftrace_probe_ops event_enable_probe_ops = {
2666 .func = event_enable_probe,
2667 .print = event_enable_print,
2668 .init = event_enable_init,
2669 .free = event_enable_free,
2672 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2673 .func = event_enable_count_probe,
2674 .print = event_enable_print,
2675 .init = event_enable_init,
2676 .free = event_enable_free,
2679 static struct ftrace_probe_ops event_disable_probe_ops = {
2680 .func = event_enable_probe,
2681 .print = event_enable_print,
2682 .init = event_enable_init,
2683 .free = event_enable_free,
2686 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2687 .func = event_enable_count_probe,
2688 .print = event_enable_print,
2689 .init = event_enable_init,
2690 .free = event_enable_free,
2694 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
2695 char *glob, char *cmd, char *param, int enabled)
2697 struct trace_event_file *file;
2698 struct ftrace_probe_ops *ops;
2699 struct event_probe_data *data;
2709 /* hash funcs only work with set_ftrace_filter */
2710 if (!enabled || !param)
2713 system = strsep(¶m, ":");
2717 event = strsep(¶m, ":");
2719 mutex_lock(&event_mutex);
2722 file = find_event_file(tr, system, event);
2726 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2729 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2731 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2733 if (glob[0] == '!') {
2734 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
2740 data = kzalloc(sizeof(*data), GFP_KERNEL);
2744 data->enable = enable;
2751 number = strsep(¶m, ":");
2754 if (!strlen(number))
2758 * We use the callback data field (which is a pointer)
2761 ret = kstrtoul(number, 0, &data->count);
2766 /* Don't let event modules unload while probe registered */
2767 ret = try_module_get(file->event_call->mod);
2773 ret = __ftrace_event_enable_disable(file, 1, 1);
2777 ret = register_ftrace_function_probe(glob, tr, ops, data);
2779 * The above returns on success the # of functions enabled,
2780 * but if it didn't find any functions it returns zero.
2781 * Consider no functions a failure too.
2788 /* Just return zero, not the number of enabled functions */
2791 mutex_unlock(&event_mutex);
2795 __ftrace_event_enable_disable(file, 0, 1);
2797 module_put(file->event_call->mod);
2803 static struct ftrace_func_command event_enable_cmd = {
2804 .name = ENABLE_EVENT_STR,
2805 .func = event_enable_func,
2808 static struct ftrace_func_command event_disable_cmd = {
2809 .name = DISABLE_EVENT_STR,
2810 .func = event_enable_func,
2813 static __init int register_event_cmds(void)
2817 ret = register_ftrace_command(&event_enable_cmd);
2818 if (WARN_ON(ret < 0))
2820 ret = register_ftrace_command(&event_disable_cmd);
2821 if (WARN_ON(ret < 0))
2822 unregister_ftrace_command(&event_enable_cmd);
2826 static inline int register_event_cmds(void) { return 0; }
2827 #endif /* CONFIG_DYNAMIC_FTRACE */
2830 * The top level array has already had its trace_event_file
2831 * descriptors created in order to allow for early events to
2832 * be recorded. This function is called after the tracefs has been
2833 * initialized, and we now have to create the files associated
2837 __trace_early_add_event_dirs(struct trace_array *tr)
2839 struct trace_event_file *file;
2843 list_for_each_entry(file, &tr->events, list) {
2844 ret = event_create_dir(tr->event_dir, file);
2846 pr_warn("Could not create directory for event %s\n",
2847 trace_event_name(file->event_call));
2852 * For early boot up, the top trace array requires to have
2853 * a list of events that can be enabled. This must be done before
2854 * the filesystem is set up in order to allow events to be traced
2858 __trace_early_add_events(struct trace_array *tr)
2860 struct trace_event_call *call;
2863 list_for_each_entry(call, &ftrace_events, list) {
2864 /* Early boot up should not have any modules loaded */
2865 if (WARN_ON_ONCE(call->mod))
2868 ret = __trace_early_add_new_event(call, tr);
2870 pr_warn("Could not create early event %s\n",
2871 trace_event_name(call));
2875 /* Remove the event directory structure for a trace directory. */
2877 __trace_remove_event_dirs(struct trace_array *tr)
2879 struct trace_event_file *file, *next;
2881 list_for_each_entry_safe(file, next, &tr->events, list)
2882 remove_event_file_dir(file);
2885 static void __add_event_to_tracers(struct trace_event_call *call)
2887 struct trace_array *tr;
2889 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2890 __trace_add_new_event(call, tr);
2893 extern struct trace_event_call *__start_ftrace_events[];
2894 extern struct trace_event_call *__stop_ftrace_events[];
2896 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2898 static __init int setup_trace_event(char *str)
2900 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2901 ring_buffer_expanded = true;
2902 tracing_selftest_disabled = true;
2906 __setup("trace_event=", setup_trace_event);
2908 /* Expects to have event_mutex held when called */
2910 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2912 struct dentry *d_events;
2913 struct dentry *entry;
2915 entry = tracefs_create_file("set_event", 0644, parent,
2916 tr, &ftrace_set_event_fops);
2918 pr_warn("Could not create tracefs 'set_event' entry\n");
2922 d_events = tracefs_create_dir("events", parent);
2924 pr_warn("Could not create tracefs 'events' directory\n");
2928 entry = trace_create_file("enable", 0644, d_events,
2929 tr, &ftrace_tr_enable_fops);
2931 pr_warn("Could not create tracefs 'enable' entry\n");
2935 /* There are not as crucial, just warn if they are not created */
2937 entry = tracefs_create_file("set_event_pid", 0644, parent,
2938 tr, &ftrace_set_event_pid_fops);
2940 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
2942 /* ring buffer internal formats */
2943 entry = trace_create_file("header_page", 0444, d_events,
2944 ring_buffer_print_page_header,
2945 &ftrace_show_header_fops);
2947 pr_warn("Could not create tracefs 'header_page' entry\n");
2949 entry = trace_create_file("header_event", 0444, d_events,
2950 ring_buffer_print_entry_header,
2951 &ftrace_show_header_fops);
2953 pr_warn("Could not create tracefs 'header_event' entry\n");
2955 tr->event_dir = d_events;
2961 * event_trace_add_tracer - add a instance of a trace_array to events
2962 * @parent: The parent dentry to place the files/directories for events in
2963 * @tr: The trace array associated with these events
2965 * When a new instance is created, it needs to set up its events
2966 * directory, as well as other files associated with events. It also
2967 * creates the event hierachry in the @parent/events directory.
2969 * Returns 0 on success.
2971 * Must be called with event_mutex held.
2973 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2977 lockdep_assert_held(&event_mutex);
2979 ret = create_event_toplevel_files(parent, tr);
2983 down_write(&trace_event_sem);
2984 __trace_add_event_dirs(tr);
2985 up_write(&trace_event_sem);
2992 * The top trace array already had its file descriptors created.
2993 * Now the files themselves need to be created.
2996 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3000 mutex_lock(&event_mutex);
3002 ret = create_event_toplevel_files(parent, tr);
3006 down_write(&trace_event_sem);
3007 __trace_early_add_event_dirs(tr);
3008 up_write(&trace_event_sem);
3011 mutex_unlock(&event_mutex);
3016 /* Must be called with event_mutex held */
3017 int event_trace_del_tracer(struct trace_array *tr)
3019 lockdep_assert_held(&event_mutex);
3021 /* Disable any event triggers and associated soft-disabled events */
3022 clear_event_triggers(tr);
3024 /* Clear the pid list */
3025 __ftrace_clear_event_pids(tr);
3027 /* Disable any running events */
3028 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3030 /* Make sure no more events are being executed */
3031 tracepoint_synchronize_unregister();
3033 down_write(&trace_event_sem);
3034 __trace_remove_event_dirs(tr);
3035 tracefs_remove_recursive(tr->event_dir);
3036 up_write(&trace_event_sem);
3038 tr->event_dir = NULL;
3043 static __init int event_trace_memsetup(void)
3045 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3046 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3051 early_enable_events(struct trace_array *tr, bool disable_first)
3053 char *buf = bootup_event_buf;
3058 token = strsep(&buf, ",");
3064 /* Restarting syscalls requires that we stop them first */
3066 ftrace_set_clr_event(tr, token, 0);
3068 ret = ftrace_set_clr_event(tr, token, 1);
3070 pr_warn("Failed to enable trace event: %s\n", token);
3073 /* Put back the comma to allow this to be called again */
3079 static __init int event_trace_enable(void)
3081 struct trace_array *tr = top_trace_array();
3082 struct trace_event_call **iter, *call;
3088 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3091 ret = event_init(call);
3093 list_add(&call->list, &ftrace_events);
3097 * We need the top trace array to have a working set of trace
3098 * points at early init, before the debug files and directories
3099 * are created. Create the file entries now, and attach them
3100 * to the actual file dentries later.
3102 __trace_early_add_events(tr);
3104 early_enable_events(tr, false);
3106 trace_printk_start_comm();
3108 register_event_cmds();
3110 register_trigger_cmds();
3116 * event_trace_enable() is called from trace_event_init() first to
3117 * initialize events and perhaps start any events that are on the
3118 * command line. Unfortunately, there are some events that will not
3119 * start this early, like the system call tracepoints that need
3120 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3121 * is called before pid 1 starts, and this flag is never set, making
3122 * the syscall tracepoint never get reached, but the event is enabled
3123 * regardless (and not doing anything).
3125 static __init int event_trace_enable_again(void)
3127 struct trace_array *tr;
3129 tr = top_trace_array();
3133 early_enable_events(tr, true);
3138 early_initcall(event_trace_enable_again);
3140 __init int event_trace_init(void)
3142 struct trace_array *tr;
3143 struct dentry *d_tracer;
3144 struct dentry *entry;
3147 tr = top_trace_array();
3151 d_tracer = tracing_init_dentry();
3152 if (IS_ERR(d_tracer))
3155 entry = tracefs_create_file("available_events", 0444, d_tracer,
3156 tr, &ftrace_avail_fops);
3158 pr_warn("Could not create tracefs 'available_events' entry\n");
3160 if (trace_define_generic_fields())
3161 pr_warn("tracing: Failed to allocated generic fields");
3163 if (trace_define_common_fields())
3164 pr_warn("tracing: Failed to allocate common fields");
3166 ret = early_event_add_tracer(d_tracer, tr);
3170 #ifdef CONFIG_MODULES
3171 ret = register_module_notifier(&trace_module_nb);
3173 pr_warn("Failed to register trace events module notifier\n");
3178 void __init trace_event_init(void)
3180 event_trace_memsetup();
3181 init_ftrace_syscalls();
3182 event_trace_enable();
3185 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3187 static DEFINE_SPINLOCK(test_spinlock);
3188 static DEFINE_SPINLOCK(test_spinlock_irq);
3189 static DEFINE_MUTEX(test_mutex);
3191 static __init void test_work(struct work_struct *dummy)
3193 spin_lock(&test_spinlock);
3194 spin_lock_irq(&test_spinlock_irq);
3196 spin_unlock_irq(&test_spinlock_irq);
3197 spin_unlock(&test_spinlock);
3199 mutex_lock(&test_mutex);
3201 mutex_unlock(&test_mutex);
3204 static __init int event_test_thread(void *unused)
3208 test_malloc = kmalloc(1234, GFP_KERNEL);
3210 pr_info("failed to kmalloc\n");
3212 schedule_on_each_cpu(test_work);
3216 set_current_state(TASK_INTERRUPTIBLE);
3217 while (!kthread_should_stop()) {
3219 set_current_state(TASK_INTERRUPTIBLE);
3221 __set_current_state(TASK_RUNNING);
3227 * Do various things that may trigger events.
3229 static __init void event_test_stuff(void)
3231 struct task_struct *test_thread;
3233 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3235 kthread_stop(test_thread);
3239 * For every trace event defined, we will test each trace point separately,
3240 * and then by groups, and finally all trace points.
3242 static __init void event_trace_self_tests(void)
3244 struct trace_subsystem_dir *dir;
3245 struct trace_event_file *file;
3246 struct trace_event_call *call;
3247 struct event_subsystem *system;
3248 struct trace_array *tr;
3251 tr = top_trace_array();
3255 pr_info("Running tests on trace events:\n");
3257 list_for_each_entry(file, &tr->events, list) {
3259 call = file->event_call;
3261 /* Only test those that have a probe */
3262 if (!call->class || !call->class->probe)
3266 * Testing syscall events here is pretty useless, but
3267 * we still do it if configured. But this is time consuming.
3268 * What we really need is a user thread to perform the
3269 * syscalls as we test.
3271 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3272 if (call->class->system &&
3273 strcmp(call->class->system, "syscalls") == 0)
3277 pr_info("Testing event %s: ", trace_event_name(call));
3280 * If an event is already enabled, someone is using
3281 * it and the self test should not be on.
3283 if (file->flags & EVENT_FILE_FL_ENABLED) {
3284 pr_warn("Enabled event during self test!\n");
3289 ftrace_event_enable_disable(file, 1);
3291 ftrace_event_enable_disable(file, 0);
3296 /* Now test at the sub system level */
3298 pr_info("Running tests on trace event systems:\n");
3300 list_for_each_entry(dir, &tr->systems, list) {
3302 system = dir->subsystem;
3304 /* the ftrace system is special, skip it */
3305 if (strcmp(system->name, "ftrace") == 0)
3308 pr_info("Testing event system %s: ", system->name);
3310 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3311 if (WARN_ON_ONCE(ret)) {
3312 pr_warn("error enabling system %s\n",
3319 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3320 if (WARN_ON_ONCE(ret)) {
3321 pr_warn("error disabling system %s\n",
3329 /* Test with all events enabled */
3331 pr_info("Running tests on all trace events:\n");
3332 pr_info("Testing all events: ");
3334 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3335 if (WARN_ON_ONCE(ret)) {
3336 pr_warn("error enabling all events\n");
3343 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3344 if (WARN_ON_ONCE(ret)) {
3345 pr_warn("error disabling all events\n");
3352 #ifdef CONFIG_FUNCTION_TRACER
3354 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3356 static struct trace_event_file event_trace_file __initdata;
3359 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3360 struct ftrace_ops *op, struct pt_regs *pt_regs)
3362 struct ring_buffer_event *event;
3363 struct ring_buffer *buffer;
3364 struct ftrace_entry *entry;
3365 unsigned long flags;
3370 pc = preempt_count();
3371 preempt_disable_notrace();
3372 cpu = raw_smp_processor_id();
3373 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3378 local_save_flags(flags);
3380 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3381 TRACE_FN, sizeof(*entry),
3385 entry = ring_buffer_event_data(event);
3387 entry->parent_ip = parent_ip;
3389 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3392 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3393 preempt_enable_notrace();
3396 static struct ftrace_ops trace_ops __initdata =
3398 .func = function_test_events_call,
3399 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3402 static __init void event_trace_self_test_with_function(void)
3406 event_trace_file.tr = top_trace_array();
3407 if (WARN_ON(!event_trace_file.tr))
3410 ret = register_ftrace_function(&trace_ops);
3411 if (WARN_ON(ret < 0)) {
3412 pr_info("Failed to enable function tracer for event tests\n");
3415 pr_info("Running tests again, along with the function tracer\n");
3416 event_trace_self_tests();
3417 unregister_ftrace_function(&trace_ops);
3420 static __init void event_trace_self_test_with_function(void)
3425 static __init int event_trace_self_tests_init(void)
3427 if (!tracing_selftest_disabled) {
3428 event_trace_self_tests();
3429 event_trace_self_test_with_function();
3435 late_initcall(event_trace_self_tests_init);