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/security.h>
16 #include <linux/spinlock.h>
17 #include <linux/kthread.h>
18 #include <linux/tracefs.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/ctype.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
29 #include <asm/setup.h>
31 #include "trace_output.h"
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
36 DEFINE_MUTEX(event_mutex);
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
42 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
44 static struct kmem_cache *field_cachep;
45 static struct kmem_cache *file_cachep;
47 static inline int system_refcount(struct event_subsystem *system)
49 return system->ref_count;
52 static int system_refcount_inc(struct event_subsystem *system)
54 return system->ref_count++;
57 static int system_refcount_dec(struct event_subsystem *system)
59 return --system->ref_count;
62 /* Double loops, do not use break, only goto's work */
63 #define do_for_each_event_file(tr, file) \
64 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
65 list_for_each_entry(file, &tr->events, list)
67 #define do_for_each_event_file_safe(tr, file) \
68 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
69 struct trace_event_file *___n; \
70 list_for_each_entry_safe(file, ___n, &tr->events, list)
72 #define while_for_each_event_file() \
75 static struct ftrace_event_field *
76 __find_event_field(struct list_head *head, char *name)
78 struct ftrace_event_field *field;
80 list_for_each_entry(field, head, link) {
81 if (!strcmp(field->name, name))
88 struct ftrace_event_field *
89 trace_find_event_field(struct trace_event_call *call, char *name)
91 struct ftrace_event_field *field;
92 struct list_head *head;
94 head = trace_get_fields(call);
95 field = __find_event_field(head, name);
99 field = __find_event_field(&ftrace_generic_fields, name);
103 return __find_event_field(&ftrace_common_fields, name);
106 static int __trace_define_field(struct list_head *head, const char *type,
107 const char *name, int offset, int size,
108 int is_signed, int filter_type)
110 struct ftrace_event_field *field;
112 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
119 if (filter_type == FILTER_OTHER)
120 field->filter_type = filter_assign_type(type);
122 field->filter_type = filter_type;
124 field->offset = offset;
126 field->is_signed = is_signed;
128 list_add(&field->link, head);
133 int trace_define_field(struct trace_event_call *call, const char *type,
134 const char *name, int offset, int size, int is_signed,
137 struct list_head *head;
139 if (WARN_ON(!call->class))
142 head = trace_get_fields(call);
143 return __trace_define_field(head, type, name, offset, size,
144 is_signed, filter_type);
146 EXPORT_SYMBOL_GPL(trace_define_field);
148 #define __generic_field(type, item, filter_type) \
149 ret = __trace_define_field(&ftrace_generic_fields, #type, \
150 #item, 0, 0, is_signed_type(type), \
155 #define __common_field(type, item) \
156 ret = __trace_define_field(&ftrace_common_fields, #type, \
158 offsetof(typeof(ent), item), \
160 is_signed_type(type), FILTER_OTHER); \
164 static int trace_define_generic_fields(void)
168 __generic_field(int, CPU, FILTER_CPU);
169 __generic_field(int, cpu, FILTER_CPU);
170 __generic_field(char *, COMM, FILTER_COMM);
171 __generic_field(char *, comm, FILTER_COMM);
176 static int trace_define_common_fields(void)
179 struct trace_entry ent;
181 __common_field(unsigned short, type);
182 __common_field(unsigned char, flags);
183 __common_field(unsigned char, preempt_count);
184 __common_field(int, pid);
189 static void trace_destroy_fields(struct trace_event_call *call)
191 struct ftrace_event_field *field, *next;
192 struct list_head *head;
194 head = trace_get_fields(call);
195 list_for_each_entry_safe(field, next, head, link) {
196 list_del(&field->link);
197 kmem_cache_free(field_cachep, field);
202 * run-time version of trace_event_get_offsets_<call>() that returns the last
203 * accessible offset of trace fields excluding __dynamic_array bytes
205 int trace_event_get_offsets(struct trace_event_call *call)
207 struct ftrace_event_field *tail;
208 struct list_head *head;
210 head = trace_get_fields(call);
212 * head->next points to the last field with the largest offset,
213 * since it was added last by trace_define_field()
215 tail = list_first_entry(head, struct ftrace_event_field, link);
216 return tail->offset + tail->size;
219 int trace_event_raw_init(struct trace_event_call *call)
223 id = register_trace_event(&call->event);
229 EXPORT_SYMBOL_GPL(trace_event_raw_init);
231 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
233 struct trace_array *tr = trace_file->tr;
234 struct trace_array_cpu *data;
235 struct trace_pid_list *pid_list;
237 pid_list = rcu_dereference_raw(tr->filtered_pids);
241 data = this_cpu_ptr(tr->trace_buffer.data);
243 return data->ignore_pid;
245 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
247 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
248 struct trace_event_file *trace_file,
251 struct trace_event_call *event_call = trace_file->event_call;
253 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
254 trace_event_ignore_this_pid(trace_file))
257 local_save_flags(fbuffer->flags);
258 fbuffer->pc = preempt_count();
260 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
261 * preemption (adding one to the preempt_count). Since we are
262 * interested in the preempt_count at the time the tracepoint was
263 * hit, we need to subtract one to offset the increment.
265 if (IS_ENABLED(CONFIG_PREEMPTION))
267 fbuffer->trace_file = trace_file;
270 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
271 event_call->event.type, len,
272 fbuffer->flags, fbuffer->pc);
276 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
277 return fbuffer->entry;
279 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
281 int trace_event_reg(struct trace_event_call *call,
282 enum trace_reg type, void *data)
284 struct trace_event_file *file = data;
286 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
288 case TRACE_REG_REGISTER:
289 return tracepoint_probe_register(call->tp,
292 case TRACE_REG_UNREGISTER:
293 tracepoint_probe_unregister(call->tp,
298 #ifdef CONFIG_PERF_EVENTS
299 case TRACE_REG_PERF_REGISTER:
300 return tracepoint_probe_register(call->tp,
301 call->class->perf_probe,
303 case TRACE_REG_PERF_UNREGISTER:
304 tracepoint_probe_unregister(call->tp,
305 call->class->perf_probe,
308 case TRACE_REG_PERF_OPEN:
309 case TRACE_REG_PERF_CLOSE:
310 case TRACE_REG_PERF_ADD:
311 case TRACE_REG_PERF_DEL:
317 EXPORT_SYMBOL_GPL(trace_event_reg);
319 void trace_event_enable_cmd_record(bool enable)
321 struct trace_event_file *file;
322 struct trace_array *tr;
324 mutex_lock(&event_mutex);
325 do_for_each_event_file(tr, file) {
327 if (!(file->flags & EVENT_FILE_FL_ENABLED))
331 tracing_start_cmdline_record();
332 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
334 tracing_stop_cmdline_record();
335 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
337 } while_for_each_event_file();
338 mutex_unlock(&event_mutex);
341 void trace_event_enable_tgid_record(bool enable)
343 struct trace_event_file *file;
344 struct trace_array *tr;
346 mutex_lock(&event_mutex);
347 do_for_each_event_file(tr, file) {
348 if (!(file->flags & EVENT_FILE_FL_ENABLED))
352 tracing_start_tgid_record();
353 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
355 tracing_stop_tgid_record();
356 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
359 } while_for_each_event_file();
360 mutex_unlock(&event_mutex);
363 static int __ftrace_event_enable_disable(struct trace_event_file *file,
364 int enable, int soft_disable)
366 struct trace_event_call *call = file->event_call;
367 struct trace_array *tr = file->tr;
368 unsigned long file_flags = file->flags;
375 * When soft_disable is set and enable is cleared, the sm_ref
376 * reference counter is decremented. If it reaches 0, we want
377 * to clear the SOFT_DISABLED flag but leave the event in the
378 * state that it was. That is, if the event was enabled and
379 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
380 * is set we do not want the event to be enabled before we
383 * When soft_disable is not set but the SOFT_MODE flag is,
384 * we do nothing. Do not disable the tracepoint, otherwise
385 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
388 if (atomic_dec_return(&file->sm_ref) > 0)
390 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
391 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
393 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
395 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
396 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
397 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
398 tracing_stop_cmdline_record();
399 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
402 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
403 tracing_stop_tgid_record();
404 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
407 call->class->reg(call, TRACE_REG_UNREGISTER, file);
409 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
410 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
411 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
413 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
417 * When soft_disable is set and enable is set, we want to
418 * register the tracepoint for the event, but leave the event
419 * as is. That means, if the event was already enabled, we do
420 * nothing (but set SOFT_MODE). If the event is disabled, we
421 * set SOFT_DISABLED before enabling the event tracepoint, so
422 * it still seems to be disabled.
425 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
427 if (atomic_inc_return(&file->sm_ref) > 1)
429 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
432 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
433 bool cmd = false, tgid = false;
435 /* Keep the event disabled, when going to SOFT_MODE. */
437 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
439 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
441 tracing_start_cmdline_record();
442 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
445 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
447 tracing_start_tgid_record();
448 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
451 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
454 tracing_stop_cmdline_record();
456 tracing_stop_tgid_record();
457 pr_info("event trace: Could not enable event "
458 "%s\n", trace_event_name(call));
461 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
463 /* WAS_ENABLED gets set but never cleared. */
464 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
469 /* Enable or disable use of trace_buffered_event */
470 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
471 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
472 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
473 trace_buffered_event_enable();
475 trace_buffered_event_disable();
481 int trace_event_enable_disable(struct trace_event_file *file,
482 int enable, int soft_disable)
484 return __ftrace_event_enable_disable(file, enable, soft_disable);
487 static int ftrace_event_enable_disable(struct trace_event_file *file,
490 return __ftrace_event_enable_disable(file, enable, 0);
493 static void ftrace_clear_events(struct trace_array *tr)
495 struct trace_event_file *file;
497 mutex_lock(&event_mutex);
498 list_for_each_entry(file, &tr->events, list) {
499 ftrace_event_enable_disable(file, 0);
501 mutex_unlock(&event_mutex);
505 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
507 struct trace_pid_list *pid_list;
508 struct trace_array *tr = data;
510 pid_list = rcu_dereference_raw(tr->filtered_pids);
511 trace_filter_add_remove_task(pid_list, NULL, task);
515 event_filter_pid_sched_process_fork(void *data,
516 struct task_struct *self,
517 struct task_struct *task)
519 struct trace_pid_list *pid_list;
520 struct trace_array *tr = data;
522 pid_list = rcu_dereference_sched(tr->filtered_pids);
523 trace_filter_add_remove_task(pid_list, self, task);
526 void trace_event_follow_fork(struct trace_array *tr, bool enable)
529 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
531 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
534 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
536 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
542 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
543 struct task_struct *prev, struct task_struct *next)
545 struct trace_array *tr = data;
546 struct trace_pid_list *pid_list;
548 pid_list = rcu_dereference_sched(tr->filtered_pids);
550 this_cpu_write(tr->trace_buffer.data->ignore_pid,
551 trace_ignore_this_task(pid_list, prev) &&
552 trace_ignore_this_task(pid_list, next));
556 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
557 struct task_struct *prev, struct task_struct *next)
559 struct trace_array *tr = data;
560 struct trace_pid_list *pid_list;
562 pid_list = rcu_dereference_sched(tr->filtered_pids);
564 this_cpu_write(tr->trace_buffer.data->ignore_pid,
565 trace_ignore_this_task(pid_list, next));
569 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
571 struct trace_array *tr = data;
572 struct trace_pid_list *pid_list;
574 /* Nothing to do if we are already tracing */
575 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
578 pid_list = rcu_dereference_sched(tr->filtered_pids);
580 this_cpu_write(tr->trace_buffer.data->ignore_pid,
581 trace_ignore_this_task(pid_list, task));
585 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
587 struct trace_array *tr = data;
588 struct trace_pid_list *pid_list;
590 /* Nothing to do if we are not tracing */
591 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
594 pid_list = rcu_dereference_sched(tr->filtered_pids);
596 /* Set tracing if current is enabled */
597 this_cpu_write(tr->trace_buffer.data->ignore_pid,
598 trace_ignore_this_task(pid_list, current));
601 static void __ftrace_clear_event_pids(struct trace_array *tr)
603 struct trace_pid_list *pid_list;
604 struct trace_event_file *file;
607 pid_list = rcu_dereference_protected(tr->filtered_pids,
608 lockdep_is_held(&event_mutex));
612 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
613 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
615 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
616 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
618 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
619 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
621 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
622 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
624 list_for_each_entry(file, &tr->events, list) {
625 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
628 for_each_possible_cpu(cpu)
629 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
631 rcu_assign_pointer(tr->filtered_pids, NULL);
633 /* Wait till all users are no longer using pid filtering */
634 tracepoint_synchronize_unregister();
636 trace_free_pid_list(pid_list);
639 static void ftrace_clear_event_pids(struct trace_array *tr)
641 mutex_lock(&event_mutex);
642 __ftrace_clear_event_pids(tr);
643 mutex_unlock(&event_mutex);
646 static void __put_system(struct event_subsystem *system)
648 struct event_filter *filter = system->filter;
650 WARN_ON_ONCE(system_refcount(system) == 0);
651 if (system_refcount_dec(system))
654 list_del(&system->list);
657 kfree(filter->filter_string);
660 kfree_const(system->name);
664 static void __get_system(struct event_subsystem *system)
666 WARN_ON_ONCE(system_refcount(system) == 0);
667 system_refcount_inc(system);
670 static void __get_system_dir(struct trace_subsystem_dir *dir)
672 WARN_ON_ONCE(dir->ref_count == 0);
674 __get_system(dir->subsystem);
677 static void __put_system_dir(struct trace_subsystem_dir *dir)
679 WARN_ON_ONCE(dir->ref_count == 0);
680 /* If the subsystem is about to be freed, the dir must be too */
681 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
683 __put_system(dir->subsystem);
684 if (!--dir->ref_count)
688 static void put_system(struct trace_subsystem_dir *dir)
690 mutex_lock(&event_mutex);
691 __put_system_dir(dir);
692 mutex_unlock(&event_mutex);
695 static void remove_subsystem(struct trace_subsystem_dir *dir)
700 if (!--dir->nr_events) {
701 tracefs_remove_recursive(dir->entry);
702 list_del(&dir->list);
703 __put_system_dir(dir);
707 static void remove_event_file_dir(struct trace_event_file *file)
709 struct dentry *dir = file->dir;
710 struct dentry *child;
713 spin_lock(&dir->d_lock); /* probably unneeded */
714 list_for_each_entry(child, &dir->d_subdirs, d_child) {
715 if (d_really_is_positive(child)) /* probably unneeded */
716 d_inode(child)->i_private = NULL;
718 spin_unlock(&dir->d_lock);
720 tracefs_remove_recursive(dir);
723 list_del(&file->list);
724 remove_subsystem(file->system);
725 free_event_filter(file->filter);
726 kmem_cache_free(file_cachep, file);
730 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
733 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
734 const char *sub, const char *event, int set)
736 struct trace_event_file *file;
737 struct trace_event_call *call;
742 list_for_each_entry(file, &tr->events, list) {
744 call = file->event_call;
745 name = trace_event_name(call);
747 if (!name || !call->class || !call->class->reg)
750 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
754 strcmp(match, name) != 0 &&
755 strcmp(match, call->class->system) != 0)
758 if (sub && strcmp(sub, call->class->system) != 0)
761 if (event && strcmp(event, name) != 0)
764 ret = ftrace_event_enable_disable(file, set);
767 * Save the first error and return that. Some events
768 * may still have been enabled, but let the user
769 * know that something went wrong.
780 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
781 const char *sub, const char *event, int set)
785 mutex_lock(&event_mutex);
786 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
787 mutex_unlock(&event_mutex);
792 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
794 char *event = NULL, *sub = NULL, *match;
800 * The buf format can be <subsystem>:<event-name>
801 * *:<event-name> means any event by that name.
802 * :<event-name> is the same.
804 * <subsystem>:* means all events in that subsystem
805 * <subsystem>: means the same.
807 * <name> (no ':') means all events in a subsystem with
808 * the name <name> or any event that matches <name>
811 match = strsep(&buf, ":");
817 if (!strlen(sub) || strcmp(sub, "*") == 0)
819 if (!strlen(event) || strcmp(event, "*") == 0)
823 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
825 /* Put back the colon to allow this to be called again */
833 * trace_set_clr_event - enable or disable an event
834 * @system: system name to match (NULL for any system)
835 * @event: event name to match (NULL for all events, within system)
836 * @set: 1 to enable, 0 to disable
838 * This is a way for other parts of the kernel to enable or disable
841 * Returns 0 on success, -EINVAL if the parameters do not match any
844 int trace_set_clr_event(const char *system, const char *event, int set)
846 struct trace_array *tr = top_trace_array();
851 return __ftrace_set_clr_event(tr, NULL, system, event, set);
853 EXPORT_SYMBOL_GPL(trace_set_clr_event);
856 * trace_array_set_clr_event - enable or disable an event for a trace array.
857 * @tr: concerned trace array.
858 * @system: system name to match (NULL for any system)
859 * @event: event name to match (NULL for all events, within system)
860 * @enable: true to enable, false 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_array_set_clr_event(struct trace_array *tr, const char *system,
869 const char *event, bool enable)
876 set = (enable == true) ? 1 : 0;
877 return __ftrace_set_clr_event(tr, NULL, system, event, set);
879 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
881 /* 128 should be much more than enough */
882 #define EVENT_BUF_SIZE 127
885 ftrace_event_write(struct file *file, const char __user *ubuf,
886 size_t cnt, loff_t *ppos)
888 struct trace_parser parser;
889 struct seq_file *m = file->private_data;
890 struct trace_array *tr = m->private;
896 ret = tracing_update_buffers();
900 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
903 read = trace_get_user(&parser, ubuf, cnt, ppos);
905 if (read >= 0 && trace_parser_loaded((&parser))) {
908 if (*parser.buffer == '!')
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);
1020 return trace_pid_next(pid_list, v, pos);
1023 static void *p_start(struct seq_file *m, loff_t *pos)
1026 struct trace_pid_list *pid_list;
1027 struct trace_array *tr = m->private;
1030 * Grab the mutex, to keep calls to p_next() having the same
1031 * tr->filtered_pids as p_start() has.
1032 * If we just passed the tr->filtered_pids around, then RCU would
1033 * have been enough, but doing that makes things more complex.
1035 mutex_lock(&event_mutex);
1036 rcu_read_lock_sched();
1038 pid_list = rcu_dereference_sched(tr->filtered_pids);
1043 return trace_pid_start(pid_list, pos);
1046 static void p_stop(struct seq_file *m, void *p)
1049 rcu_read_unlock_sched();
1050 mutex_unlock(&event_mutex);
1054 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1057 struct trace_event_file *file;
1058 unsigned long flags;
1061 mutex_lock(&event_mutex);
1062 file = event_file_data(filp);
1064 flags = file->flags;
1065 mutex_unlock(&event_mutex);
1070 if (flags & EVENT_FILE_FL_ENABLED &&
1071 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1074 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1075 flags & EVENT_FILE_FL_SOFT_MODE)
1080 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1084 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1087 struct trace_event_file *file;
1091 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1095 ret = tracing_update_buffers();
1103 mutex_lock(&event_mutex);
1104 file = event_file_data(filp);
1106 ret = ftrace_event_enable_disable(file, val);
1107 mutex_unlock(&event_mutex);
1116 return ret ? ret : cnt;
1120 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1123 const char set_to_char[4] = { '?', '0', '1', 'X' };
1124 struct trace_subsystem_dir *dir = filp->private_data;
1125 struct event_subsystem *system = dir->subsystem;
1126 struct trace_event_call *call;
1127 struct trace_event_file *file;
1128 struct trace_array *tr = dir->tr;
1133 mutex_lock(&event_mutex);
1134 list_for_each_entry(file, &tr->events, list) {
1135 call = file->event_call;
1136 if (!trace_event_name(call) || !call->class || !call->class->reg)
1139 if (system && strcmp(call->class->system, system->name) != 0)
1143 * We need to find out if all the events are set
1144 * or if all events or cleared, or if we have
1147 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1150 * If we have a mixture, no need to look further.
1155 mutex_unlock(&event_mutex);
1157 buf[0] = set_to_char[set];
1160 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1166 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1169 struct trace_subsystem_dir *dir = filp->private_data;
1170 struct event_subsystem *system = dir->subsystem;
1171 const char *name = NULL;
1175 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1179 ret = tracing_update_buffers();
1183 if (val != 0 && val != 1)
1187 * Opening of "enable" adds a ref count to system,
1188 * so the name is safe to use.
1191 name = system->name;
1193 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1207 FORMAT_FIELD_SEPERATOR = 2,
1208 FORMAT_PRINTFMT = 3,
1211 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1213 struct trace_event_call *call = event_file_data(m->private);
1214 struct list_head *common_head = &ftrace_common_fields;
1215 struct list_head *head = trace_get_fields(call);
1216 struct list_head *node = v;
1220 switch ((unsigned long)v) {
1225 case FORMAT_FIELD_SEPERATOR:
1229 case FORMAT_PRINTFMT:
1235 if (node == common_head)
1236 return (void *)FORMAT_FIELD_SEPERATOR;
1237 else if (node == head)
1238 return (void *)FORMAT_PRINTFMT;
1243 static int f_show(struct seq_file *m, void *v)
1245 struct trace_event_call *call = event_file_data(m->private);
1246 struct ftrace_event_field *field;
1247 const char *array_descriptor;
1249 switch ((unsigned long)v) {
1251 seq_printf(m, "name: %s\n", trace_event_name(call));
1252 seq_printf(m, "ID: %d\n", call->event.type);
1253 seq_puts(m, "format:\n");
1256 case FORMAT_FIELD_SEPERATOR:
1260 case FORMAT_PRINTFMT:
1261 seq_printf(m, "\nprint fmt: %s\n",
1266 field = list_entry(v, struct ftrace_event_field, link);
1268 * Smartly shows the array type(except dynamic array).
1271 * If TYPE := TYPE[LEN], it is shown:
1272 * field:TYPE VAR[LEN]
1274 array_descriptor = strchr(field->type, '[');
1276 if (str_has_prefix(field->type, "__data_loc"))
1277 array_descriptor = NULL;
1279 if (!array_descriptor)
1280 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1281 field->type, field->name, field->offset,
1282 field->size, !!field->is_signed);
1284 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1285 (int)(array_descriptor - field->type),
1286 field->type, field->name,
1287 array_descriptor, field->offset,
1288 field->size, !!field->is_signed);
1293 static void *f_start(struct seq_file *m, loff_t *pos)
1295 void *p = (void *)FORMAT_HEADER;
1298 /* ->stop() is called even if ->start() fails */
1299 mutex_lock(&event_mutex);
1300 if (!event_file_data(m->private))
1301 return ERR_PTR(-ENODEV);
1303 while (l < *pos && p)
1304 p = f_next(m, p, &l);
1309 static void f_stop(struct seq_file *m, void *p)
1311 mutex_unlock(&event_mutex);
1314 static const struct seq_operations trace_format_seq_ops = {
1321 static int trace_format_open(struct inode *inode, struct file *file)
1326 /* Do we want to hide event format files on tracefs lockdown? */
1328 ret = seq_open(file, &trace_format_seq_ops);
1332 m = file->private_data;
1339 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1341 int id = (long)event_file_data(filp);
1348 len = sprintf(buf, "%d\n", id);
1350 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1354 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1357 struct trace_event_file *file;
1358 struct trace_seq *s;
1364 s = kmalloc(sizeof(*s), GFP_KERNEL);
1371 mutex_lock(&event_mutex);
1372 file = event_file_data(filp);
1374 print_event_filter(file, s);
1375 mutex_unlock(&event_mutex);
1378 r = simple_read_from_buffer(ubuf, cnt, ppos,
1379 s->buffer, trace_seq_used(s));
1387 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1390 struct trace_event_file *file;
1394 if (cnt >= PAGE_SIZE)
1397 buf = memdup_user_nul(ubuf, cnt);
1399 return PTR_ERR(buf);
1401 mutex_lock(&event_mutex);
1402 file = event_file_data(filp);
1404 err = apply_event_filter(file, buf);
1405 mutex_unlock(&event_mutex);
1416 static LIST_HEAD(event_subsystems);
1418 static int subsystem_open(struct inode *inode, struct file *filp)
1420 struct event_subsystem *system = NULL;
1421 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1422 struct trace_array *tr;
1425 if (tracing_is_disabled())
1428 /* Make sure the system still exists */
1429 mutex_lock(&event_mutex);
1430 mutex_lock(&trace_types_lock);
1431 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1432 list_for_each_entry(dir, &tr->systems, list) {
1433 if (dir == inode->i_private) {
1434 /* Don't open systems with no events */
1435 if (dir->nr_events) {
1436 __get_system_dir(dir);
1437 system = dir->subsystem;
1444 mutex_unlock(&trace_types_lock);
1445 mutex_unlock(&event_mutex);
1450 /* Some versions of gcc think dir can be uninitialized here */
1453 /* Still need to increment the ref count of the system */
1454 if (trace_array_get(tr) < 0) {
1459 ret = tracing_open_generic(inode, filp);
1461 trace_array_put(tr);
1468 static int system_tr_open(struct inode *inode, struct file *filp)
1470 struct trace_subsystem_dir *dir;
1471 struct trace_array *tr = inode->i_private;
1474 /* Make a temporary dir that has no system but points to tr */
1475 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1479 ret = tracing_open_generic_tr(inode, filp);
1485 filp->private_data = dir;
1490 static int subsystem_release(struct inode *inode, struct file *file)
1492 struct trace_subsystem_dir *dir = file->private_data;
1494 trace_array_put(dir->tr);
1497 * If dir->subsystem is NULL, then this is a temporary
1498 * descriptor that was made for a trace_array to enable
1510 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1513 struct trace_subsystem_dir *dir = filp->private_data;
1514 struct event_subsystem *system = dir->subsystem;
1515 struct trace_seq *s;
1521 s = kmalloc(sizeof(*s), GFP_KERNEL);
1527 print_subsystem_event_filter(system, s);
1528 r = simple_read_from_buffer(ubuf, cnt, ppos,
1529 s->buffer, trace_seq_used(s));
1537 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1540 struct trace_subsystem_dir *dir = filp->private_data;
1544 if (cnt >= PAGE_SIZE)
1547 buf = memdup_user_nul(ubuf, cnt);
1549 return PTR_ERR(buf);
1551 err = apply_subsystem_event_filter(dir, buf);
1562 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1564 int (*func)(struct trace_seq *s) = filp->private_data;
1565 struct trace_seq *s;
1571 s = kmalloc(sizeof(*s), GFP_KERNEL);
1578 r = simple_read_from_buffer(ubuf, cnt, ppos,
1579 s->buffer, trace_seq_used(s));
1586 static void ignore_task_cpu(void *data)
1588 struct trace_array *tr = data;
1589 struct trace_pid_list *pid_list;
1592 * This function is called by on_each_cpu() while the
1593 * event_mutex is held.
1595 pid_list = rcu_dereference_protected(tr->filtered_pids,
1596 mutex_is_locked(&event_mutex));
1598 this_cpu_write(tr->trace_buffer.data->ignore_pid,
1599 trace_ignore_this_task(pid_list, current));
1603 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1604 size_t cnt, loff_t *ppos)
1606 struct seq_file *m = filp->private_data;
1607 struct trace_array *tr = m->private;
1608 struct trace_pid_list *filtered_pids = NULL;
1609 struct trace_pid_list *pid_list;
1610 struct trace_event_file *file;
1616 ret = tracing_update_buffers();
1620 mutex_lock(&event_mutex);
1622 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1623 lockdep_is_held(&event_mutex));
1625 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1629 rcu_assign_pointer(tr->filtered_pids, pid_list);
1631 list_for_each_entry(file, &tr->events, list) {
1632 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1635 if (filtered_pids) {
1636 tracepoint_synchronize_unregister();
1637 trace_free_pid_list(filtered_pids);
1638 } else if (pid_list) {
1640 * Register a probe that is called before all other probes
1641 * to set ignore_pid if next or prev do not match.
1642 * Register a probe this is called after all other probes
1643 * to only keep ignore_pid set if next pid matches.
1645 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1647 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1650 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1652 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1655 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1657 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1660 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1662 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1667 * Ignoring of pids is done at task switch. But we have to
1668 * check for those tasks that are currently running.
1669 * Always do this in case a pid was appended or removed.
1671 on_each_cpu(ignore_task_cpu, tr, 1);
1674 mutex_unlock(&event_mutex);
1682 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1683 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1684 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1685 static int ftrace_event_release(struct inode *inode, struct file *file);
1687 static const struct seq_operations show_event_seq_ops = {
1694 static const struct seq_operations show_set_event_seq_ops = {
1701 static const struct seq_operations show_set_pid_seq_ops = {
1704 .show = trace_pid_show,
1708 static const struct file_operations ftrace_avail_fops = {
1709 .open = ftrace_event_avail_open,
1711 .llseek = seq_lseek,
1712 .release = seq_release,
1715 static const struct file_operations ftrace_set_event_fops = {
1716 .open = ftrace_event_set_open,
1718 .write = ftrace_event_write,
1719 .llseek = seq_lseek,
1720 .release = ftrace_event_release,
1723 static const struct file_operations ftrace_set_event_pid_fops = {
1724 .open = ftrace_event_set_pid_open,
1726 .write = ftrace_event_pid_write,
1727 .llseek = seq_lseek,
1728 .release = ftrace_event_release,
1731 static const struct file_operations ftrace_enable_fops = {
1732 .open = tracing_open_generic,
1733 .read = event_enable_read,
1734 .write = event_enable_write,
1735 .llseek = default_llseek,
1738 static const struct file_operations ftrace_event_format_fops = {
1739 .open = trace_format_open,
1741 .llseek = seq_lseek,
1742 .release = seq_release,
1745 static const struct file_operations ftrace_event_id_fops = {
1746 .read = event_id_read,
1747 .llseek = default_llseek,
1750 static const struct file_operations ftrace_event_filter_fops = {
1751 .open = tracing_open_generic,
1752 .read = event_filter_read,
1753 .write = event_filter_write,
1754 .llseek = default_llseek,
1757 static const struct file_operations ftrace_subsystem_filter_fops = {
1758 .open = subsystem_open,
1759 .read = subsystem_filter_read,
1760 .write = subsystem_filter_write,
1761 .llseek = default_llseek,
1762 .release = subsystem_release,
1765 static const struct file_operations ftrace_system_enable_fops = {
1766 .open = subsystem_open,
1767 .read = system_enable_read,
1768 .write = system_enable_write,
1769 .llseek = default_llseek,
1770 .release = subsystem_release,
1773 static const struct file_operations ftrace_tr_enable_fops = {
1774 .open = system_tr_open,
1775 .read = system_enable_read,
1776 .write = system_enable_write,
1777 .llseek = default_llseek,
1778 .release = subsystem_release,
1781 static const struct file_operations ftrace_show_header_fops = {
1782 .open = tracing_open_generic,
1783 .read = show_header,
1784 .llseek = default_llseek,
1788 ftrace_event_open(struct inode *inode, struct file *file,
1789 const struct seq_operations *seq_ops)
1794 ret = security_locked_down(LOCKDOWN_TRACEFS);
1798 ret = seq_open(file, seq_ops);
1801 m = file->private_data;
1802 /* copy tr over to seq ops */
1803 m->private = inode->i_private;
1808 static int ftrace_event_release(struct inode *inode, struct file *file)
1810 struct trace_array *tr = inode->i_private;
1812 trace_array_put(tr);
1814 return seq_release(inode, file);
1818 ftrace_event_avail_open(struct inode *inode, struct file *file)
1820 const struct seq_operations *seq_ops = &show_event_seq_ops;
1822 /* Checks for tracefs lockdown */
1823 return ftrace_event_open(inode, file, seq_ops);
1827 ftrace_event_set_open(struct inode *inode, struct file *file)
1829 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1830 struct trace_array *tr = inode->i_private;
1833 ret = tracing_check_open_get_tr(tr);
1837 if ((file->f_mode & FMODE_WRITE) &&
1838 (file->f_flags & O_TRUNC))
1839 ftrace_clear_events(tr);
1841 ret = ftrace_event_open(inode, file, seq_ops);
1843 trace_array_put(tr);
1848 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1850 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1851 struct trace_array *tr = inode->i_private;
1854 ret = tracing_check_open_get_tr(tr);
1858 if ((file->f_mode & FMODE_WRITE) &&
1859 (file->f_flags & O_TRUNC))
1860 ftrace_clear_event_pids(tr);
1862 ret = ftrace_event_open(inode, file, seq_ops);
1864 trace_array_put(tr);
1868 static struct event_subsystem *
1869 create_new_subsystem(const char *name)
1871 struct event_subsystem *system;
1873 /* need to create new entry */
1874 system = kmalloc(sizeof(*system), GFP_KERNEL);
1878 system->ref_count = 1;
1880 /* Only allocate if dynamic (kprobes and modules) */
1881 system->name = kstrdup_const(name, GFP_KERNEL);
1885 system->filter = NULL;
1887 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1888 if (!system->filter)
1891 list_add(&system->list, &event_subsystems);
1896 kfree_const(system->name);
1901 static struct dentry *
1902 event_subsystem_dir(struct trace_array *tr, const char *name,
1903 struct trace_event_file *file, struct dentry *parent)
1905 struct trace_subsystem_dir *dir;
1906 struct event_subsystem *system;
1907 struct dentry *entry;
1909 /* First see if we did not already create this dir */
1910 list_for_each_entry(dir, &tr->systems, list) {
1911 system = dir->subsystem;
1912 if (strcmp(system->name, name) == 0) {
1919 /* Now see if the system itself exists. */
1920 list_for_each_entry(system, &event_subsystems, list) {
1921 if (strcmp(system->name, name) == 0)
1924 /* Reset system variable when not found */
1925 if (&system->list == &event_subsystems)
1928 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1933 system = create_new_subsystem(name);
1937 __get_system(system);
1939 dir->entry = tracefs_create_dir(name, parent);
1941 pr_warn("Failed to create system directory %s\n", name);
1942 __put_system(system);
1949 dir->subsystem = system;
1952 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1953 &ftrace_subsystem_filter_fops);
1955 kfree(system->filter);
1956 system->filter = NULL;
1957 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1960 trace_create_file("enable", 0644, dir->entry, dir,
1961 &ftrace_system_enable_fops);
1963 list_add(&dir->list, &tr->systems);
1970 /* Only print this message if failed on memory allocation */
1971 if (!dir || !system)
1972 pr_warn("No memory to create event subsystem %s\n", name);
1977 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1979 struct trace_event_call *call = file->event_call;
1980 struct trace_array *tr = file->tr;
1981 struct list_head *head;
1982 struct dentry *d_events;
1987 * If the trace point header did not define TRACE_SYSTEM
1988 * then the system would be called "TRACE_SYSTEM".
1990 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1991 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1997 name = trace_event_name(call);
1998 file->dir = tracefs_create_dir(name, d_events);
2000 pr_warn("Could not create tracefs '%s' directory\n", name);
2004 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2005 trace_create_file("enable", 0644, file->dir, file,
2006 &ftrace_enable_fops);
2008 #ifdef CONFIG_PERF_EVENTS
2009 if (call->event.type && call->class->reg)
2010 trace_create_file("id", 0444, file->dir,
2011 (void *)(long)call->event.type,
2012 &ftrace_event_id_fops);
2016 * Other events may have the same class. Only update
2017 * the fields if they are not already defined.
2019 head = trace_get_fields(call);
2020 if (list_empty(head)) {
2021 struct trace_event_fields *field = call->class->fields_array;
2022 unsigned int offset = sizeof(struct trace_entry);
2024 for (; field->type; field++) {
2025 if (field->type == TRACE_FUNCTION_TYPE) {
2026 ret = field->define_fields(call);
2030 offset = ALIGN(offset, field->align);
2031 ret = trace_define_field(call, field->type, field->name,
2032 offset, field->size,
2033 field->is_signed, field->filter_type);
2037 offset += field->size;
2040 pr_warn("Could not initialize trace point events/%s\n",
2047 * Only event directories that can be enabled should have
2048 * triggers or filters.
2050 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2051 trace_create_file("filter", 0644, file->dir, file,
2052 &ftrace_event_filter_fops);
2054 trace_create_file("trigger", 0644, file->dir, file,
2055 &event_trigger_fops);
2058 #ifdef CONFIG_HIST_TRIGGERS
2059 trace_create_file("hist", 0444, file->dir, file,
2062 trace_create_file("format", 0444, file->dir, call,
2063 &ftrace_event_format_fops);
2065 #ifdef CONFIG_TRACE_EVENT_INJECT
2066 if (call->event.type && call->class->reg)
2067 trace_create_file("inject", 0200, file->dir, file,
2068 &event_inject_fops);
2074 static void remove_event_from_tracers(struct trace_event_call *call)
2076 struct trace_event_file *file;
2077 struct trace_array *tr;
2079 do_for_each_event_file_safe(tr, file) {
2080 if (file->event_call != call)
2083 remove_event_file_dir(file);
2085 * The do_for_each_event_file_safe() is
2086 * a double loop. After finding the call for this
2087 * trace_array, we use break to jump to the next
2091 } while_for_each_event_file();
2094 static void event_remove(struct trace_event_call *call)
2096 struct trace_array *tr;
2097 struct trace_event_file *file;
2099 do_for_each_event_file(tr, file) {
2100 if (file->event_call != call)
2103 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2104 tr->clear_trace = true;
2106 ftrace_event_enable_disable(file, 0);
2108 * The do_for_each_event_file() is
2109 * a double loop. After finding the call for this
2110 * trace_array, we use break to jump to the next
2114 } while_for_each_event_file();
2116 if (call->event.funcs)
2117 __unregister_trace_event(&call->event);
2118 remove_event_from_tracers(call);
2119 list_del(&call->list);
2122 static int event_init(struct trace_event_call *call)
2127 name = trace_event_name(call);
2131 if (call->class->raw_init) {
2132 ret = call->class->raw_init(call);
2133 if (ret < 0 && ret != -ENOSYS)
2134 pr_warn("Could not initialize trace events/%s\n", name);
2141 __register_event(struct trace_event_call *call, struct module *mod)
2145 ret = event_init(call);
2149 list_add(&call->list, &ftrace_events);
2155 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2160 /* Find the length of the eval value as a string */
2161 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2162 /* Make sure there's enough room to replace the string with the value */
2166 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2168 /* Get the rest of the string of ptr */
2169 rlen = strlen(ptr + len);
2170 memmove(ptr + elen, ptr + len, rlen);
2171 /* Make sure we end the new string */
2172 ptr[elen + rlen] = 0;
2177 static void update_event_printk(struct trace_event_call *call,
2178 struct trace_eval_map *map)
2182 int len = strlen(map->eval_string);
2184 for (ptr = call->print_fmt; *ptr; ptr++) {
2198 if (isdigit(*ptr)) {
2202 /* Check for alpha chars like ULL */
2203 } while (isalnum(*ptr));
2207 * A number must have some kind of delimiter after
2208 * it, and we can ignore that too.
2212 if (isalpha(*ptr) || *ptr == '_') {
2213 if (strncmp(map->eval_string, ptr, len) == 0 &&
2214 !isalnum(ptr[len]) && ptr[len] != '_') {
2215 ptr = eval_replace(ptr, map, len);
2216 /* enum/sizeof string smaller than value */
2217 if (WARN_ON_ONCE(!ptr))
2220 * No need to decrement here, as eval_replace()
2221 * returns the pointer to the character passed
2222 * the eval, and two evals can not be placed
2223 * back to back without something in between.
2224 * We can skip that something in between.
2231 } while (isalnum(*ptr) || *ptr == '_');
2235 * If what comes after this variable is a '.' or
2236 * '->' then we can continue to ignore that string.
2238 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2239 ptr += *ptr == '.' ? 1 : 2;
2245 * Once again, we can skip the delimiter that came
2253 void trace_event_eval_update(struct trace_eval_map **map, int len)
2255 struct trace_event_call *call, *p;
2256 const char *last_system = NULL;
2261 down_write(&trace_event_sem);
2262 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2263 /* events are usually grouped together with systems */
2264 if (!last_system || call->class->system != last_system) {
2267 last_system = call->class->system;
2271 * Since calls are grouped by systems, the likelyhood that the
2272 * next call in the iteration belongs to the same system as the
2273 * previous call is high. As an optimization, we skip seaching
2274 * for a map[] that matches the call's system if the last call
2275 * was from the same system. That's what last_i is for. If the
2276 * call has the same system as the previous call, then last_i
2277 * will be the index of the first map[] that has a matching
2280 for (i = last_i; i < len; i++) {
2281 if (call->class->system == map[i]->system) {
2282 /* Save the first system if need be */
2287 update_event_printk(call, map[i]);
2291 up_write(&trace_event_sem);
2294 static struct trace_event_file *
2295 trace_create_new_event(struct trace_event_call *call,
2296 struct trace_array *tr)
2298 struct trace_event_file *file;
2300 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2304 file->event_call = call;
2306 atomic_set(&file->sm_ref, 0);
2307 atomic_set(&file->tm_ref, 0);
2308 INIT_LIST_HEAD(&file->triggers);
2309 list_add(&file->list, &tr->events);
2314 /* Add an event to a trace directory */
2316 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2318 struct trace_event_file *file;
2320 file = trace_create_new_event(call, tr);
2324 return event_create_dir(tr->event_dir, file);
2328 * Just create a decriptor for early init. A descriptor is required
2329 * for enabling events at boot. We want to enable events before
2330 * the filesystem is initialized.
2333 __trace_early_add_new_event(struct trace_event_call *call,
2334 struct trace_array *tr)
2336 struct trace_event_file *file;
2338 file = trace_create_new_event(call, tr);
2345 struct ftrace_module_file_ops;
2346 static void __add_event_to_tracers(struct trace_event_call *call);
2348 /* Add an additional event_call dynamically */
2349 int trace_add_event_call(struct trace_event_call *call)
2352 lockdep_assert_held(&event_mutex);
2354 mutex_lock(&trace_types_lock);
2356 ret = __register_event(call, NULL);
2358 __add_event_to_tracers(call);
2360 mutex_unlock(&trace_types_lock);
2365 * Must be called under locking of trace_types_lock, event_mutex and
2368 static void __trace_remove_event_call(struct trace_event_call *call)
2371 trace_destroy_fields(call);
2372 free_event_filter(call->filter);
2373 call->filter = NULL;
2376 static int probe_remove_event_call(struct trace_event_call *call)
2378 struct trace_array *tr;
2379 struct trace_event_file *file;
2381 #ifdef CONFIG_PERF_EVENTS
2382 if (call->perf_refcount)
2385 do_for_each_event_file(tr, file) {
2386 if (file->event_call != call)
2389 * We can't rely on ftrace_event_enable_disable(enable => 0)
2390 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2391 * TRACE_REG_UNREGISTER.
2393 if (file->flags & EVENT_FILE_FL_ENABLED)
2396 * The do_for_each_event_file_safe() is
2397 * a double loop. After finding the call for this
2398 * trace_array, we use break to jump to the next
2402 } while_for_each_event_file();
2404 __trace_remove_event_call(call);
2409 /* Remove an event_call */
2410 int trace_remove_event_call(struct trace_event_call *call)
2414 lockdep_assert_held(&event_mutex);
2416 mutex_lock(&trace_types_lock);
2417 down_write(&trace_event_sem);
2418 ret = probe_remove_event_call(call);
2419 up_write(&trace_event_sem);
2420 mutex_unlock(&trace_types_lock);
2425 #define for_each_event(event, start, end) \
2426 for (event = start; \
2427 (unsigned long)event < (unsigned long)end; \
2430 #ifdef CONFIG_MODULES
2432 static void trace_module_add_events(struct module *mod)
2434 struct trace_event_call **call, **start, **end;
2436 if (!mod->num_trace_events)
2439 /* Don't add infrastructure for mods without tracepoints */
2440 if (trace_module_has_bad_taint(mod)) {
2441 pr_err("%s: module has bad taint, not creating trace events\n",
2446 start = mod->trace_events;
2447 end = mod->trace_events + mod->num_trace_events;
2449 for_each_event(call, start, end) {
2450 __register_event(*call, mod);
2451 __add_event_to_tracers(*call);
2455 static void trace_module_remove_events(struct module *mod)
2457 struct trace_event_call *call, *p;
2459 down_write(&trace_event_sem);
2460 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2461 if (call->mod == mod)
2462 __trace_remove_event_call(call);
2464 up_write(&trace_event_sem);
2467 * It is safest to reset the ring buffer if the module being unloaded
2468 * registered any events that were used. The only worry is if
2469 * a new module gets loaded, and takes on the same id as the events
2470 * of this module. When printing out the buffer, traced events left
2471 * over from this module may be passed to the new module events and
2472 * unexpected results may occur.
2474 tracing_reset_all_online_cpus();
2477 static int trace_module_notify(struct notifier_block *self,
2478 unsigned long val, void *data)
2480 struct module *mod = data;
2482 mutex_lock(&event_mutex);
2483 mutex_lock(&trace_types_lock);
2485 case MODULE_STATE_COMING:
2486 trace_module_add_events(mod);
2488 case MODULE_STATE_GOING:
2489 trace_module_remove_events(mod);
2492 mutex_unlock(&trace_types_lock);
2493 mutex_unlock(&event_mutex);
2498 static struct notifier_block trace_module_nb = {
2499 .notifier_call = trace_module_notify,
2500 .priority = 1, /* higher than trace.c module notify */
2502 #endif /* CONFIG_MODULES */
2504 /* Create a new event directory structure for a trace directory. */
2506 __trace_add_event_dirs(struct trace_array *tr)
2508 struct trace_event_call *call;
2511 list_for_each_entry(call, &ftrace_events, list) {
2512 ret = __trace_add_new_event(call, tr);
2514 pr_warn("Could not create directory for event %s\n",
2515 trace_event_name(call));
2519 /* Returns any file that matches the system and event */
2520 struct trace_event_file *
2521 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2523 struct trace_event_file *file;
2524 struct trace_event_call *call;
2527 list_for_each_entry(file, &tr->events, list) {
2529 call = file->event_call;
2530 name = trace_event_name(call);
2532 if (!name || !call->class)
2535 if (strcmp(event, name) == 0 &&
2536 strcmp(system, call->class->system) == 0)
2542 /* Returns valid trace event files that match system and event */
2543 struct trace_event_file *
2544 find_event_file(struct trace_array *tr, const char *system, const char *event)
2546 struct trace_event_file *file;
2548 file = __find_event_file(tr, system, event);
2549 if (!file || !file->event_call->class->reg ||
2550 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2556 #ifdef CONFIG_DYNAMIC_FTRACE
2559 #define ENABLE_EVENT_STR "enable_event"
2560 #define DISABLE_EVENT_STR "disable_event"
2562 struct event_probe_data {
2563 struct trace_event_file *file;
2564 unsigned long count;
2569 static void update_event_probe(struct event_probe_data *data)
2572 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2574 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2578 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2579 struct trace_array *tr, struct ftrace_probe_ops *ops,
2582 struct ftrace_func_mapper *mapper = data;
2583 struct event_probe_data *edata;
2586 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2587 if (!pdata || !*pdata)
2591 update_event_probe(edata);
2595 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2596 struct trace_array *tr, struct ftrace_probe_ops *ops,
2599 struct ftrace_func_mapper *mapper = data;
2600 struct event_probe_data *edata;
2603 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2604 if (!pdata || !*pdata)
2612 /* Skip if the event is in a state we want to switch to */
2613 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2616 if (edata->count != -1)
2619 update_event_probe(edata);
2623 event_enable_print(struct seq_file *m, unsigned long ip,
2624 struct ftrace_probe_ops *ops, void *data)
2626 struct ftrace_func_mapper *mapper = data;
2627 struct event_probe_data *edata;
2630 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2632 if (WARN_ON_ONCE(!pdata || !*pdata))
2637 seq_printf(m, "%ps:", (void *)ip);
2639 seq_printf(m, "%s:%s:%s",
2640 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2641 edata->file->event_call->class->system,
2642 trace_event_name(edata->file->event_call));
2644 if (edata->count == -1)
2645 seq_puts(m, ":unlimited\n");
2647 seq_printf(m, ":count=%ld\n", edata->count);
2653 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2654 unsigned long ip, void *init_data, void **data)
2656 struct ftrace_func_mapper *mapper = *data;
2657 struct event_probe_data *edata = init_data;
2661 mapper = allocate_ftrace_func_mapper();
2667 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2676 static int free_probe_data(void *data)
2678 struct event_probe_data *edata = data;
2682 /* Remove the SOFT_MODE flag */
2683 __ftrace_event_enable_disable(edata->file, 0, 1);
2684 module_put(edata->file->event_call->mod);
2691 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2692 unsigned long ip, void *data)
2694 struct ftrace_func_mapper *mapper = data;
2695 struct event_probe_data *edata;
2700 free_ftrace_func_mapper(mapper, free_probe_data);
2704 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2706 if (WARN_ON_ONCE(!edata))
2709 if (WARN_ON_ONCE(edata->ref <= 0))
2712 free_probe_data(edata);
2715 static struct ftrace_probe_ops event_enable_probe_ops = {
2716 .func = event_enable_probe,
2717 .print = event_enable_print,
2718 .init = event_enable_init,
2719 .free = event_enable_free,
2722 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2723 .func = event_enable_count_probe,
2724 .print = event_enable_print,
2725 .init = event_enable_init,
2726 .free = event_enable_free,
2729 static struct ftrace_probe_ops event_disable_probe_ops = {
2730 .func = event_enable_probe,
2731 .print = event_enable_print,
2732 .init = event_enable_init,
2733 .free = event_enable_free,
2736 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2737 .func = event_enable_count_probe,
2738 .print = event_enable_print,
2739 .init = event_enable_init,
2740 .free = event_enable_free,
2744 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
2745 char *glob, char *cmd, char *param, int enabled)
2747 struct trace_event_file *file;
2748 struct ftrace_probe_ops *ops;
2749 struct event_probe_data *data;
2759 /* hash funcs only work with set_ftrace_filter */
2760 if (!enabled || !param)
2763 system = strsep(¶m, ":");
2767 event = strsep(¶m, ":");
2769 mutex_lock(&event_mutex);
2772 file = find_event_file(tr, system, event);
2776 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2779 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2781 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2783 if (glob[0] == '!') {
2784 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
2790 data = kzalloc(sizeof(*data), GFP_KERNEL);
2794 data->enable = enable;
2801 number = strsep(¶m, ":");
2804 if (!strlen(number))
2808 * We use the callback data field (which is a pointer)
2811 ret = kstrtoul(number, 0, &data->count);
2816 /* Don't let event modules unload while probe registered */
2817 ret = try_module_get(file->event_call->mod);
2823 ret = __ftrace_event_enable_disable(file, 1, 1);
2827 ret = register_ftrace_function_probe(glob, tr, ops, data);
2829 * The above returns on success the # of functions enabled,
2830 * but if it didn't find any functions it returns zero.
2831 * Consider no functions a failure too.
2838 /* Just return zero, not the number of enabled functions */
2841 mutex_unlock(&event_mutex);
2845 __ftrace_event_enable_disable(file, 0, 1);
2847 module_put(file->event_call->mod);
2853 static struct ftrace_func_command event_enable_cmd = {
2854 .name = ENABLE_EVENT_STR,
2855 .func = event_enable_func,
2858 static struct ftrace_func_command event_disable_cmd = {
2859 .name = DISABLE_EVENT_STR,
2860 .func = event_enable_func,
2863 static __init int register_event_cmds(void)
2867 ret = register_ftrace_command(&event_enable_cmd);
2868 if (WARN_ON(ret < 0))
2870 ret = register_ftrace_command(&event_disable_cmd);
2871 if (WARN_ON(ret < 0))
2872 unregister_ftrace_command(&event_enable_cmd);
2876 static inline int register_event_cmds(void) { return 0; }
2877 #endif /* CONFIG_DYNAMIC_FTRACE */
2880 * The top level array has already had its trace_event_file
2881 * descriptors created in order to allow for early events to
2882 * be recorded. This function is called after the tracefs has been
2883 * initialized, and we now have to create the files associated
2887 __trace_early_add_event_dirs(struct trace_array *tr)
2889 struct trace_event_file *file;
2893 list_for_each_entry(file, &tr->events, list) {
2894 ret = event_create_dir(tr->event_dir, file);
2896 pr_warn("Could not create directory for event %s\n",
2897 trace_event_name(file->event_call));
2902 * For early boot up, the top trace array requires to have
2903 * a list of events that can be enabled. This must be done before
2904 * the filesystem is set up in order to allow events to be traced
2908 __trace_early_add_events(struct trace_array *tr)
2910 struct trace_event_call *call;
2913 list_for_each_entry(call, &ftrace_events, list) {
2914 /* Early boot up should not have any modules loaded */
2915 if (WARN_ON_ONCE(call->mod))
2918 ret = __trace_early_add_new_event(call, tr);
2920 pr_warn("Could not create early event %s\n",
2921 trace_event_name(call));
2925 /* Remove the event directory structure for a trace directory. */
2927 __trace_remove_event_dirs(struct trace_array *tr)
2929 struct trace_event_file *file, *next;
2931 list_for_each_entry_safe(file, next, &tr->events, list)
2932 remove_event_file_dir(file);
2935 static void __add_event_to_tracers(struct trace_event_call *call)
2937 struct trace_array *tr;
2939 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2940 __trace_add_new_event(call, tr);
2943 extern struct trace_event_call *__start_ftrace_events[];
2944 extern struct trace_event_call *__stop_ftrace_events[];
2946 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2948 static __init int setup_trace_event(char *str)
2950 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2951 ring_buffer_expanded = true;
2952 tracing_selftest_disabled = true;
2956 __setup("trace_event=", setup_trace_event);
2958 /* Expects to have event_mutex held when called */
2960 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2962 struct dentry *d_events;
2963 struct dentry *entry;
2965 entry = tracefs_create_file("set_event", 0644, parent,
2966 tr, &ftrace_set_event_fops);
2968 pr_warn("Could not create tracefs 'set_event' entry\n");
2972 d_events = tracefs_create_dir("events", parent);
2974 pr_warn("Could not create tracefs 'events' directory\n");
2978 entry = trace_create_file("enable", 0644, d_events,
2979 tr, &ftrace_tr_enable_fops);
2981 pr_warn("Could not create tracefs 'enable' entry\n");
2985 /* There are not as crucial, just warn if they are not created */
2987 entry = tracefs_create_file("set_event_pid", 0644, parent,
2988 tr, &ftrace_set_event_pid_fops);
2990 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
2992 /* ring buffer internal formats */
2993 entry = trace_create_file("header_page", 0444, d_events,
2994 ring_buffer_print_page_header,
2995 &ftrace_show_header_fops);
2997 pr_warn("Could not create tracefs 'header_page' entry\n");
2999 entry = trace_create_file("header_event", 0444, d_events,
3000 ring_buffer_print_entry_header,
3001 &ftrace_show_header_fops);
3003 pr_warn("Could not create tracefs 'header_event' entry\n");
3005 tr->event_dir = d_events;
3011 * event_trace_add_tracer - add a instance of a trace_array to events
3012 * @parent: The parent dentry to place the files/directories for events in
3013 * @tr: The trace array associated with these events
3015 * When a new instance is created, it needs to set up its events
3016 * directory, as well as other files associated with events. It also
3017 * creates the event hierachry in the @parent/events directory.
3019 * Returns 0 on success.
3021 * Must be called with event_mutex held.
3023 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3027 lockdep_assert_held(&event_mutex);
3029 ret = create_event_toplevel_files(parent, tr);
3033 down_write(&trace_event_sem);
3034 __trace_add_event_dirs(tr);
3035 up_write(&trace_event_sem);
3042 * The top trace array already had its file descriptors created.
3043 * Now the files themselves need to be created.
3046 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3050 mutex_lock(&event_mutex);
3052 ret = create_event_toplevel_files(parent, tr);
3056 down_write(&trace_event_sem);
3057 __trace_early_add_event_dirs(tr);
3058 up_write(&trace_event_sem);
3061 mutex_unlock(&event_mutex);
3066 /* Must be called with event_mutex held */
3067 int event_trace_del_tracer(struct trace_array *tr)
3069 lockdep_assert_held(&event_mutex);
3071 /* Disable any event triggers and associated soft-disabled events */
3072 clear_event_triggers(tr);
3074 /* Clear the pid list */
3075 __ftrace_clear_event_pids(tr);
3077 /* Disable any running events */
3078 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3080 /* Make sure no more events are being executed */
3081 tracepoint_synchronize_unregister();
3083 down_write(&trace_event_sem);
3084 __trace_remove_event_dirs(tr);
3085 tracefs_remove_recursive(tr->event_dir);
3086 up_write(&trace_event_sem);
3088 tr->event_dir = NULL;
3093 static __init int event_trace_memsetup(void)
3095 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3096 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3101 early_enable_events(struct trace_array *tr, bool disable_first)
3103 char *buf = bootup_event_buf;
3108 token = strsep(&buf, ",");
3114 /* Restarting syscalls requires that we stop them first */
3116 ftrace_set_clr_event(tr, token, 0);
3118 ret = ftrace_set_clr_event(tr, token, 1);
3120 pr_warn("Failed to enable trace event: %s\n", token);
3123 /* Put back the comma to allow this to be called again */
3129 static __init int event_trace_enable(void)
3131 struct trace_array *tr = top_trace_array();
3132 struct trace_event_call **iter, *call;
3138 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3141 ret = event_init(call);
3143 list_add(&call->list, &ftrace_events);
3147 * We need the top trace array to have a working set of trace
3148 * points at early init, before the debug files and directories
3149 * are created. Create the file entries now, and attach them
3150 * to the actual file dentries later.
3152 __trace_early_add_events(tr);
3154 early_enable_events(tr, false);
3156 trace_printk_start_comm();
3158 register_event_cmds();
3160 register_trigger_cmds();
3166 * event_trace_enable() is called from trace_event_init() first to
3167 * initialize events and perhaps start any events that are on the
3168 * command line. Unfortunately, there are some events that will not
3169 * start this early, like the system call tracepoints that need
3170 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3171 * is called before pid 1 starts, and this flag is never set, making
3172 * the syscall tracepoint never get reached, but the event is enabled
3173 * regardless (and not doing anything).
3175 static __init int event_trace_enable_again(void)
3177 struct trace_array *tr;
3179 tr = top_trace_array();
3183 early_enable_events(tr, true);
3188 early_initcall(event_trace_enable_again);
3190 __init int event_trace_init(void)
3192 struct trace_array *tr;
3193 struct dentry *d_tracer;
3194 struct dentry *entry;
3197 tr = top_trace_array();
3201 d_tracer = tracing_init_dentry();
3202 if (IS_ERR(d_tracer))
3205 entry = tracefs_create_file("available_events", 0444, d_tracer,
3206 tr, &ftrace_avail_fops);
3208 pr_warn("Could not create tracefs 'available_events' entry\n");
3210 if (trace_define_generic_fields())
3211 pr_warn("tracing: Failed to allocated generic fields");
3213 if (trace_define_common_fields())
3214 pr_warn("tracing: Failed to allocate common fields");
3216 ret = early_event_add_tracer(d_tracer, tr);
3220 #ifdef CONFIG_MODULES
3221 ret = register_module_notifier(&trace_module_nb);
3223 pr_warn("Failed to register trace events module notifier\n");
3228 void __init trace_event_init(void)
3230 event_trace_memsetup();
3231 init_ftrace_syscalls();
3232 event_trace_enable();
3235 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3237 static DEFINE_SPINLOCK(test_spinlock);
3238 static DEFINE_SPINLOCK(test_spinlock_irq);
3239 static DEFINE_MUTEX(test_mutex);
3241 static __init void test_work(struct work_struct *dummy)
3243 spin_lock(&test_spinlock);
3244 spin_lock_irq(&test_spinlock_irq);
3246 spin_unlock_irq(&test_spinlock_irq);
3247 spin_unlock(&test_spinlock);
3249 mutex_lock(&test_mutex);
3251 mutex_unlock(&test_mutex);
3254 static __init int event_test_thread(void *unused)
3258 test_malloc = kmalloc(1234, GFP_KERNEL);
3260 pr_info("failed to kmalloc\n");
3262 schedule_on_each_cpu(test_work);
3266 set_current_state(TASK_INTERRUPTIBLE);
3267 while (!kthread_should_stop()) {
3269 set_current_state(TASK_INTERRUPTIBLE);
3271 __set_current_state(TASK_RUNNING);
3277 * Do various things that may trigger events.
3279 static __init void event_test_stuff(void)
3281 struct task_struct *test_thread;
3283 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3285 kthread_stop(test_thread);
3289 * For every trace event defined, we will test each trace point separately,
3290 * and then by groups, and finally all trace points.
3292 static __init void event_trace_self_tests(void)
3294 struct trace_subsystem_dir *dir;
3295 struct trace_event_file *file;
3296 struct trace_event_call *call;
3297 struct event_subsystem *system;
3298 struct trace_array *tr;
3301 tr = top_trace_array();
3305 pr_info("Running tests on trace events:\n");
3307 list_for_each_entry(file, &tr->events, list) {
3309 call = file->event_call;
3311 /* Only test those that have a probe */
3312 if (!call->class || !call->class->probe)
3316 * Testing syscall events here is pretty useless, but
3317 * we still do it if configured. But this is time consuming.
3318 * What we really need is a user thread to perform the
3319 * syscalls as we test.
3321 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3322 if (call->class->system &&
3323 strcmp(call->class->system, "syscalls") == 0)
3327 pr_info("Testing event %s: ", trace_event_name(call));
3330 * If an event is already enabled, someone is using
3331 * it and the self test should not be on.
3333 if (file->flags & EVENT_FILE_FL_ENABLED) {
3334 pr_warn("Enabled event during self test!\n");
3339 ftrace_event_enable_disable(file, 1);
3341 ftrace_event_enable_disable(file, 0);
3346 /* Now test at the sub system level */
3348 pr_info("Running tests on trace event systems:\n");
3350 list_for_each_entry(dir, &tr->systems, list) {
3352 system = dir->subsystem;
3354 /* the ftrace system is special, skip it */
3355 if (strcmp(system->name, "ftrace") == 0)
3358 pr_info("Testing event system %s: ", system->name);
3360 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3361 if (WARN_ON_ONCE(ret)) {
3362 pr_warn("error enabling system %s\n",
3369 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3370 if (WARN_ON_ONCE(ret)) {
3371 pr_warn("error disabling system %s\n",
3379 /* Test with all events enabled */
3381 pr_info("Running tests on all trace events:\n");
3382 pr_info("Testing all events: ");
3384 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3385 if (WARN_ON_ONCE(ret)) {
3386 pr_warn("error enabling all events\n");
3393 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3394 if (WARN_ON_ONCE(ret)) {
3395 pr_warn("error disabling all events\n");
3402 #ifdef CONFIG_FUNCTION_TRACER
3404 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3406 static struct trace_event_file event_trace_file __initdata;
3409 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3410 struct ftrace_ops *op, struct pt_regs *pt_regs)
3412 struct ring_buffer_event *event;
3413 struct ring_buffer *buffer;
3414 struct ftrace_entry *entry;
3415 unsigned long flags;
3420 pc = preempt_count();
3421 preempt_disable_notrace();
3422 cpu = raw_smp_processor_id();
3423 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3428 local_save_flags(flags);
3430 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3431 TRACE_FN, sizeof(*entry),
3435 entry = ring_buffer_event_data(event);
3437 entry->parent_ip = parent_ip;
3439 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3442 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3443 preempt_enable_notrace();
3446 static struct ftrace_ops trace_ops __initdata =
3448 .func = function_test_events_call,
3449 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3452 static __init void event_trace_self_test_with_function(void)
3456 event_trace_file.tr = top_trace_array();
3457 if (WARN_ON(!event_trace_file.tr))
3460 ret = register_ftrace_function(&trace_ops);
3461 if (WARN_ON(ret < 0)) {
3462 pr_info("Failed to enable function tracer for event tests\n");
3465 pr_info("Running tests again, along with the function tracer\n");
3466 event_trace_self_tests();
3467 unregister_ftrace_function(&trace_ops);
3470 static __init void event_trace_self_test_with_function(void)
3475 static __init int event_trace_self_tests_init(void)
3477 if (!tracing_selftest_disabled) {
3478 event_trace_self_tests();
3479 event_trace_self_test_with_function();
3485 late_initcall(event_trace_self_tests_init);