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);
41 static bool eventdir_initialized;
43 static LIST_HEAD(module_strings);
45 struct module_string {
46 struct list_head next;
47 struct module *module;
51 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
53 static struct kmem_cache *field_cachep;
54 static struct kmem_cache *file_cachep;
56 static inline int system_refcount(struct event_subsystem *system)
58 return system->ref_count;
61 static int system_refcount_inc(struct event_subsystem *system)
63 return system->ref_count++;
66 static int system_refcount_dec(struct event_subsystem *system)
68 return --system->ref_count;
71 /* Double loops, do not use break, only goto's work */
72 #define do_for_each_event_file(tr, file) \
73 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
74 list_for_each_entry(file, &tr->events, list)
76 #define do_for_each_event_file_safe(tr, file) \
77 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
78 struct trace_event_file *___n; \
79 list_for_each_entry_safe(file, ___n, &tr->events, list)
81 #define while_for_each_event_file() \
84 static struct ftrace_event_field *
85 __find_event_field(struct list_head *head, char *name)
87 struct ftrace_event_field *field;
89 list_for_each_entry(field, head, link) {
90 if (!strcmp(field->name, name))
97 struct ftrace_event_field *
98 trace_find_event_field(struct trace_event_call *call, char *name)
100 struct ftrace_event_field *field;
101 struct list_head *head;
103 head = trace_get_fields(call);
104 field = __find_event_field(head, name);
108 field = __find_event_field(&ftrace_generic_fields, name);
112 return __find_event_field(&ftrace_common_fields, name);
115 static int __trace_define_field(struct list_head *head, const char *type,
116 const char *name, int offset, int size,
117 int is_signed, int filter_type)
119 struct ftrace_event_field *field;
121 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
128 if (filter_type == FILTER_OTHER)
129 field->filter_type = filter_assign_type(type);
131 field->filter_type = filter_type;
133 field->offset = offset;
135 field->is_signed = is_signed;
137 list_add(&field->link, head);
142 int trace_define_field(struct trace_event_call *call, const char *type,
143 const char *name, int offset, int size, int is_signed,
146 struct list_head *head;
148 if (WARN_ON(!call->class))
151 head = trace_get_fields(call);
152 return __trace_define_field(head, type, name, offset, size,
153 is_signed, filter_type);
155 EXPORT_SYMBOL_GPL(trace_define_field);
157 #define __generic_field(type, item, filter_type) \
158 ret = __trace_define_field(&ftrace_generic_fields, #type, \
159 #item, 0, 0, is_signed_type(type), \
164 #define __common_field(type, item) \
165 ret = __trace_define_field(&ftrace_common_fields, #type, \
167 offsetof(typeof(ent), item), \
169 is_signed_type(type), FILTER_OTHER); \
173 static int trace_define_generic_fields(void)
177 __generic_field(int, CPU, FILTER_CPU);
178 __generic_field(int, cpu, FILTER_CPU);
179 __generic_field(int, common_cpu, FILTER_CPU);
180 __generic_field(char *, COMM, FILTER_COMM);
181 __generic_field(char *, comm, FILTER_COMM);
186 static int trace_define_common_fields(void)
189 struct trace_entry ent;
191 __common_field(unsigned short, type);
192 __common_field(unsigned char, flags);
193 /* Holds both preempt_count and migrate_disable */
194 __common_field(unsigned char, preempt_count);
195 __common_field(int, pid);
200 static void trace_destroy_fields(struct trace_event_call *call)
202 struct ftrace_event_field *field, *next;
203 struct list_head *head;
205 head = trace_get_fields(call);
206 list_for_each_entry_safe(field, next, head, link) {
207 list_del(&field->link);
208 kmem_cache_free(field_cachep, field);
213 * run-time version of trace_event_get_offsets_<call>() that returns the last
214 * accessible offset of trace fields excluding __dynamic_array bytes
216 int trace_event_get_offsets(struct trace_event_call *call)
218 struct ftrace_event_field *tail;
219 struct list_head *head;
221 head = trace_get_fields(call);
223 * head->next points to the last field with the largest offset,
224 * since it was added last by trace_define_field()
226 tail = list_first_entry(head, struct ftrace_event_field, link);
227 return tail->offset + tail->size;
231 * Check if the referenced field is an array and return true,
232 * as arrays are OK to dereference.
234 static bool test_field(const char *fmt, struct trace_event_call *call)
236 struct trace_event_fields *field = call->class->fields_array;
237 const char *array_descriptor;
241 if (!(len = str_has_prefix(fmt, "REC->")))
244 for (p = fmt; *p; p++) {
245 if (!isalnum(*p) && *p != '_')
250 for (; field->type; field++) {
251 if (strncmp(field->name, fmt, len) ||
254 array_descriptor = strchr(field->type, '[');
255 /* This is an array and is OK to dereference. */
256 return array_descriptor != NULL;
262 * Examine the print fmt of the event looking for unsafe dereference
263 * pointers using %p* that could be recorded in the trace event and
264 * much later referenced after the pointer was freed. Dereferencing
265 * pointers are OK, if it is dereferenced into the event itself.
267 static void test_event_printk(struct trace_event_call *call)
269 u64 dereference_flags = 0;
271 const char *fmt, *c, *r, *a;
278 fmt = call->print_fmt;
283 for (i = 0; fmt[i]; i++) {
293 * The print fmt starts with a string that
294 * is processed first to find %p* usage,
295 * then after the first string, the print fmt
296 * contains arguments that are used to check
297 * if the dereferenced %p* usage is safe.
306 * If there was no %p* uses
309 if (!dereference_flags)
314 if (in_quote == fmt[i])
321 if (!first || !in_quote)
330 /* Find dereferencing fields */
331 switch (fmt[i + 1]) {
332 case 'B': case 'R': case 'r':
333 case 'b': case 'M': case 'm':
334 case 'I': case 'i': case 'E':
335 case 'U': case 'V': case 'N':
336 case 'a': case 'd': case 'D':
337 case 'g': case 't': case 'C':
339 if (WARN_ONCE(arg == 63,
340 "Too many args for event: %s",
341 trace_event_name(call)))
343 dereference_flags |= 1ULL << arg;
351 /* Increment arg if %*s exists. */
352 for (j = 0; fmt[i + j]; j++) {
353 if (isdigit(fmt[i + j]) ||
356 if (fmt[i + j] == '*') {
360 if ((fmt[i + j] == 's') && star)
379 if (WARN_ONCE(parens < 0,
380 "Paren mismatch for event: %s\narg='%s'\n%*s",
381 trace_event_name(call),
383 (i - start_arg) + 5, "^"))
387 if (in_quote || parens)
390 while (isspace(fmt[i]))
393 if (!(dereference_flags & (1ULL << arg)))
396 /* Find the REC-> in the argument */
397 c = strchr(fmt + i, ',');
398 r = strstr(fmt + i, "REC->");
399 if (r && (!c || r < c)) {
401 * Addresses of events on the buffer,
402 * or an array on the buffer is
404 * There's ways to fool this, but
405 * this is to catch common mistakes,
406 * not malicious code.
408 a = strchr(fmt + i, '&');
409 if ((a && (a < r)) || test_field(r, call))
410 dereference_flags &= ~(1ULL << arg);
411 } else if ((r = strstr(fmt + i, "__get_dynamic_array(")) &&
413 dereference_flags &= ~(1ULL << arg);
414 } else if ((r = strstr(fmt + i, "__get_sockaddr(")) &&
416 dereference_flags &= ~(1ULL << arg);
426 * If you triggered the below warning, the trace event reported
427 * uses an unsafe dereference pointer %p*. As the data stored
428 * at the trace event time may no longer exist when the trace
429 * event is printed, dereferencing to the original source is
430 * unsafe. The source of the dereference must be copied into the
431 * event itself, and the dereference must access the copy instead.
433 if (WARN_ON_ONCE(dereference_flags)) {
435 while (!(dereference_flags & 1)) {
436 dereference_flags >>= 1;
439 pr_warn("event %s has unsafe dereference of argument %d\n",
440 trace_event_name(call), arg);
441 pr_warn("print_fmt: %s\n", fmt);
445 int trace_event_raw_init(struct trace_event_call *call)
449 id = register_trace_event(&call->event);
453 test_event_printk(call);
457 EXPORT_SYMBOL_GPL(trace_event_raw_init);
459 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
461 struct trace_array *tr = trace_file->tr;
462 struct trace_array_cpu *data;
463 struct trace_pid_list *no_pid_list;
464 struct trace_pid_list *pid_list;
466 pid_list = rcu_dereference_raw(tr->filtered_pids);
467 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
469 if (!pid_list && !no_pid_list)
472 data = this_cpu_ptr(tr->array_buffer.data);
474 return data->ignore_pid;
476 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
478 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
479 struct trace_event_file *trace_file,
482 struct trace_event_call *event_call = trace_file->event_call;
484 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
485 trace_event_ignore_this_pid(trace_file))
489 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
490 * preemption (adding one to the preempt_count). Since we are
491 * interested in the preempt_count at the time the tracepoint was
492 * hit, we need to subtract one to offset the increment.
494 fbuffer->trace_ctx = tracing_gen_ctx_dec();
495 fbuffer->trace_file = trace_file;
498 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
499 event_call->event.type, len,
504 fbuffer->regs = NULL;
505 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
506 return fbuffer->entry;
508 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
510 int trace_event_reg(struct trace_event_call *call,
511 enum trace_reg type, void *data)
513 struct trace_event_file *file = data;
515 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
517 case TRACE_REG_REGISTER:
518 return tracepoint_probe_register(call->tp,
521 case TRACE_REG_UNREGISTER:
522 tracepoint_probe_unregister(call->tp,
527 #ifdef CONFIG_PERF_EVENTS
528 case TRACE_REG_PERF_REGISTER:
529 return tracepoint_probe_register(call->tp,
530 call->class->perf_probe,
532 case TRACE_REG_PERF_UNREGISTER:
533 tracepoint_probe_unregister(call->tp,
534 call->class->perf_probe,
537 case TRACE_REG_PERF_OPEN:
538 case TRACE_REG_PERF_CLOSE:
539 case TRACE_REG_PERF_ADD:
540 case TRACE_REG_PERF_DEL:
546 EXPORT_SYMBOL_GPL(trace_event_reg);
548 void trace_event_enable_cmd_record(bool enable)
550 struct trace_event_file *file;
551 struct trace_array *tr;
553 lockdep_assert_held(&event_mutex);
555 do_for_each_event_file(tr, file) {
557 if (!(file->flags & EVENT_FILE_FL_ENABLED))
561 tracing_start_cmdline_record();
562 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
564 tracing_stop_cmdline_record();
565 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
567 } while_for_each_event_file();
570 void trace_event_enable_tgid_record(bool enable)
572 struct trace_event_file *file;
573 struct trace_array *tr;
575 lockdep_assert_held(&event_mutex);
577 do_for_each_event_file(tr, file) {
578 if (!(file->flags & EVENT_FILE_FL_ENABLED))
582 tracing_start_tgid_record();
583 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
585 tracing_stop_tgid_record();
586 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
589 } while_for_each_event_file();
592 static int __ftrace_event_enable_disable(struct trace_event_file *file,
593 int enable, int soft_disable)
595 struct trace_event_call *call = file->event_call;
596 struct trace_array *tr = file->tr;
603 * When soft_disable is set and enable is cleared, the sm_ref
604 * reference counter is decremented. If it reaches 0, we want
605 * to clear the SOFT_DISABLED flag but leave the event in the
606 * state that it was. That is, if the event was enabled and
607 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
608 * is set we do not want the event to be enabled before we
611 * When soft_disable is not set but the SOFT_MODE flag is,
612 * we do nothing. Do not disable the tracepoint, otherwise
613 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
616 if (atomic_dec_return(&file->sm_ref) > 0)
618 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
619 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
620 /* Disable use of trace_buffered_event */
621 trace_buffered_event_disable();
623 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
625 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
626 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
627 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
628 tracing_stop_cmdline_record();
629 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
632 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
633 tracing_stop_tgid_record();
634 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
637 call->class->reg(call, TRACE_REG_UNREGISTER, file);
639 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
640 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
641 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
643 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
647 * When soft_disable is set and enable is set, we want to
648 * register the tracepoint for the event, but leave the event
649 * as is. That means, if the event was already enabled, we do
650 * nothing (but set SOFT_MODE). If the event is disabled, we
651 * set SOFT_DISABLED before enabling the event tracepoint, so
652 * it still seems to be disabled.
655 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
657 if (atomic_inc_return(&file->sm_ref) > 1)
659 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
660 /* Enable use of trace_buffered_event */
661 trace_buffered_event_enable();
664 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
665 bool cmd = false, tgid = false;
667 /* Keep the event disabled, when going to SOFT_MODE. */
669 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
671 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
673 tracing_start_cmdline_record();
674 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
677 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
679 tracing_start_tgid_record();
680 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
683 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
686 tracing_stop_cmdline_record();
688 tracing_stop_tgid_record();
689 pr_info("event trace: Could not enable event "
690 "%s\n", trace_event_name(call));
693 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
695 /* WAS_ENABLED gets set but never cleared. */
696 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
704 int trace_event_enable_disable(struct trace_event_file *file,
705 int enable, int soft_disable)
707 return __ftrace_event_enable_disable(file, enable, soft_disable);
710 static int ftrace_event_enable_disable(struct trace_event_file *file,
713 return __ftrace_event_enable_disable(file, enable, 0);
716 static void ftrace_clear_events(struct trace_array *tr)
718 struct trace_event_file *file;
720 mutex_lock(&event_mutex);
721 list_for_each_entry(file, &tr->events, list) {
722 ftrace_event_enable_disable(file, 0);
724 mutex_unlock(&event_mutex);
728 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
730 struct trace_pid_list *pid_list;
731 struct trace_array *tr = data;
733 pid_list = rcu_dereference_raw(tr->filtered_pids);
734 trace_filter_add_remove_task(pid_list, NULL, task);
736 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
737 trace_filter_add_remove_task(pid_list, NULL, task);
741 event_filter_pid_sched_process_fork(void *data,
742 struct task_struct *self,
743 struct task_struct *task)
745 struct trace_pid_list *pid_list;
746 struct trace_array *tr = data;
748 pid_list = rcu_dereference_sched(tr->filtered_pids);
749 trace_filter_add_remove_task(pid_list, self, task);
751 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
752 trace_filter_add_remove_task(pid_list, self, task);
755 void trace_event_follow_fork(struct trace_array *tr, bool enable)
758 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
760 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
763 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
765 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
771 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
772 struct task_struct *prev, struct task_struct *next)
774 struct trace_array *tr = data;
775 struct trace_pid_list *no_pid_list;
776 struct trace_pid_list *pid_list;
779 pid_list = rcu_dereference_sched(tr->filtered_pids);
780 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
783 * Sched switch is funny, as we only want to ignore it
784 * in the notrace case if both prev and next should be ignored.
786 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
787 trace_ignore_this_task(NULL, no_pid_list, next);
789 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
790 (trace_ignore_this_task(pid_list, NULL, prev) &&
791 trace_ignore_this_task(pid_list, NULL, next)));
795 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
796 struct task_struct *prev, struct task_struct *next)
798 struct trace_array *tr = data;
799 struct trace_pid_list *no_pid_list;
800 struct trace_pid_list *pid_list;
802 pid_list = rcu_dereference_sched(tr->filtered_pids);
803 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
805 this_cpu_write(tr->array_buffer.data->ignore_pid,
806 trace_ignore_this_task(pid_list, no_pid_list, next));
810 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
812 struct trace_array *tr = data;
813 struct trace_pid_list *no_pid_list;
814 struct trace_pid_list *pid_list;
816 /* Nothing to do if we are already tracing */
817 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
820 pid_list = rcu_dereference_sched(tr->filtered_pids);
821 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
823 this_cpu_write(tr->array_buffer.data->ignore_pid,
824 trace_ignore_this_task(pid_list, no_pid_list, task));
828 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
830 struct trace_array *tr = data;
831 struct trace_pid_list *no_pid_list;
832 struct trace_pid_list *pid_list;
834 /* Nothing to do if we are not tracing */
835 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
838 pid_list = rcu_dereference_sched(tr->filtered_pids);
839 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
841 /* Set tracing if current is enabled */
842 this_cpu_write(tr->array_buffer.data->ignore_pid,
843 trace_ignore_this_task(pid_list, no_pid_list, current));
846 static void unregister_pid_events(struct trace_array *tr)
848 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
849 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
851 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
852 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
854 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
855 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
857 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
858 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
861 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
863 struct trace_pid_list *pid_list;
864 struct trace_pid_list *no_pid_list;
865 struct trace_event_file *file;
868 pid_list = rcu_dereference_protected(tr->filtered_pids,
869 lockdep_is_held(&event_mutex));
870 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
871 lockdep_is_held(&event_mutex));
873 /* Make sure there's something to do */
874 if (!pid_type_enabled(type, pid_list, no_pid_list))
877 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
878 unregister_pid_events(tr);
880 list_for_each_entry(file, &tr->events, list) {
881 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
884 for_each_possible_cpu(cpu)
885 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
888 if (type & TRACE_PIDS)
889 rcu_assign_pointer(tr->filtered_pids, NULL);
891 if (type & TRACE_NO_PIDS)
892 rcu_assign_pointer(tr->filtered_no_pids, NULL);
894 /* Wait till all users are no longer using pid filtering */
895 tracepoint_synchronize_unregister();
897 if ((type & TRACE_PIDS) && pid_list)
898 trace_pid_list_free(pid_list);
900 if ((type & TRACE_NO_PIDS) && no_pid_list)
901 trace_pid_list_free(no_pid_list);
904 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
906 mutex_lock(&event_mutex);
907 __ftrace_clear_event_pids(tr, type);
908 mutex_unlock(&event_mutex);
911 static void __put_system(struct event_subsystem *system)
913 struct event_filter *filter = system->filter;
915 WARN_ON_ONCE(system_refcount(system) == 0);
916 if (system_refcount_dec(system))
919 list_del(&system->list);
922 kfree(filter->filter_string);
925 kfree_const(system->name);
929 static void __get_system(struct event_subsystem *system)
931 WARN_ON_ONCE(system_refcount(system) == 0);
932 system_refcount_inc(system);
935 static void __get_system_dir(struct trace_subsystem_dir *dir)
937 WARN_ON_ONCE(dir->ref_count == 0);
939 __get_system(dir->subsystem);
942 static void __put_system_dir(struct trace_subsystem_dir *dir)
944 WARN_ON_ONCE(dir->ref_count == 0);
945 /* If the subsystem is about to be freed, the dir must be too */
946 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
948 __put_system(dir->subsystem);
949 if (!--dir->ref_count)
953 static void put_system(struct trace_subsystem_dir *dir)
955 mutex_lock(&event_mutex);
956 __put_system_dir(dir);
957 mutex_unlock(&event_mutex);
960 static void remove_subsystem(struct trace_subsystem_dir *dir)
965 if (!--dir->nr_events) {
966 tracefs_remove(dir->entry);
967 list_del(&dir->list);
968 __put_system_dir(dir);
972 static void remove_event_file_dir(struct trace_event_file *file)
974 struct dentry *dir = file->dir;
975 struct dentry *child;
978 spin_lock(&dir->d_lock); /* probably unneeded */
979 list_for_each_entry(child, &dir->d_subdirs, d_child) {
980 if (d_really_is_positive(child)) /* probably unneeded */
981 d_inode(child)->i_private = NULL;
983 spin_unlock(&dir->d_lock);
988 list_del(&file->list);
989 remove_subsystem(file->system);
990 free_event_filter(file->filter);
991 kmem_cache_free(file_cachep, file);
995 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
998 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
999 const char *sub, const char *event, int set)
1001 struct trace_event_file *file;
1002 struct trace_event_call *call;
1007 list_for_each_entry(file, &tr->events, list) {
1009 call = file->event_call;
1010 name = trace_event_name(call);
1012 if (!name || !call->class || !call->class->reg)
1015 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1019 strcmp(match, name) != 0 &&
1020 strcmp(match, call->class->system) != 0)
1023 if (sub && strcmp(sub, call->class->system) != 0)
1026 if (event && strcmp(event, name) != 0)
1029 ret = ftrace_event_enable_disable(file, set);
1032 * Save the first error and return that. Some events
1033 * may still have been enabled, but let the user
1034 * know that something went wrong.
1045 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1046 const char *sub, const char *event, int set)
1050 mutex_lock(&event_mutex);
1051 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
1052 mutex_unlock(&event_mutex);
1057 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
1059 char *event = NULL, *sub = NULL, *match;
1065 * The buf format can be <subsystem>:<event-name>
1066 * *:<event-name> means any event by that name.
1067 * :<event-name> is the same.
1069 * <subsystem>:* means all events in that subsystem
1070 * <subsystem>: means the same.
1072 * <name> (no ':') means all events in a subsystem with
1073 * the name <name> or any event that matches <name>
1076 match = strsep(&buf, ":");
1082 if (!strlen(sub) || strcmp(sub, "*") == 0)
1084 if (!strlen(event) || strcmp(event, "*") == 0)
1088 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
1090 /* Put back the colon to allow this to be called again */
1098 * trace_set_clr_event - enable or disable an event
1099 * @system: system name to match (NULL for any system)
1100 * @event: event name to match (NULL for all events, within system)
1101 * @set: 1 to enable, 0 to disable
1103 * This is a way for other parts of the kernel to enable or disable
1106 * Returns 0 on success, -EINVAL if the parameters do not match any
1107 * registered events.
1109 int trace_set_clr_event(const char *system, const char *event, int set)
1111 struct trace_array *tr = top_trace_array();
1116 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1118 EXPORT_SYMBOL_GPL(trace_set_clr_event);
1121 * trace_array_set_clr_event - enable or disable an event for a trace array.
1122 * @tr: concerned trace array.
1123 * @system: system name to match (NULL for any system)
1124 * @event: event name to match (NULL for all events, within system)
1125 * @enable: true to enable, false to disable
1127 * This is a way for other parts of the kernel to enable or disable
1130 * Returns 0 on success, -EINVAL if the parameters do not match any
1131 * registered events.
1133 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
1134 const char *event, bool enable)
1141 set = (enable == true) ? 1 : 0;
1142 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1144 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
1146 /* 128 should be much more than enough */
1147 #define EVENT_BUF_SIZE 127
1150 ftrace_event_write(struct file *file, const char __user *ubuf,
1151 size_t cnt, loff_t *ppos)
1153 struct trace_parser parser;
1154 struct seq_file *m = file->private_data;
1155 struct trace_array *tr = m->private;
1161 ret = tracing_update_buffers();
1165 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1168 read = trace_get_user(&parser, ubuf, cnt, ppos);
1170 if (read >= 0 && trace_parser_loaded((&parser))) {
1173 if (*parser.buffer == '!')
1176 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
1184 trace_parser_put(&parser);
1190 t_next(struct seq_file *m, void *v, loff_t *pos)
1192 struct trace_event_file *file = v;
1193 struct trace_event_call *call;
1194 struct trace_array *tr = m->private;
1198 list_for_each_entry_continue(file, &tr->events, list) {
1199 call = file->event_call;
1201 * The ftrace subsystem is for showing formats only.
1202 * They can not be enabled or disabled via the event files.
1204 if (call->class && call->class->reg &&
1205 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1212 static void *t_start(struct seq_file *m, loff_t *pos)
1214 struct trace_event_file *file;
1215 struct trace_array *tr = m->private;
1218 mutex_lock(&event_mutex);
1220 file = list_entry(&tr->events, struct trace_event_file, list);
1221 for (l = 0; l <= *pos; ) {
1222 file = t_next(m, file, &l);
1230 s_next(struct seq_file *m, void *v, loff_t *pos)
1232 struct trace_event_file *file = v;
1233 struct trace_array *tr = m->private;
1237 list_for_each_entry_continue(file, &tr->events, list) {
1238 if (file->flags & EVENT_FILE_FL_ENABLED)
1245 static void *s_start(struct seq_file *m, loff_t *pos)
1247 struct trace_event_file *file;
1248 struct trace_array *tr = m->private;
1251 mutex_lock(&event_mutex);
1253 file = list_entry(&tr->events, struct trace_event_file, list);
1254 for (l = 0; l <= *pos; ) {
1255 file = s_next(m, file, &l);
1262 static int t_show(struct seq_file *m, void *v)
1264 struct trace_event_file *file = v;
1265 struct trace_event_call *call = file->event_call;
1267 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1268 seq_printf(m, "%s:", call->class->system);
1269 seq_printf(m, "%s\n", trace_event_name(call));
1274 static void t_stop(struct seq_file *m, void *p)
1276 mutex_unlock(&event_mutex);
1280 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1282 struct trace_array *tr = m->private;
1283 struct trace_pid_list *pid_list;
1285 if (type == TRACE_PIDS)
1286 pid_list = rcu_dereference_sched(tr->filtered_pids);
1288 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1290 return trace_pid_next(pid_list, v, pos);
1294 p_next(struct seq_file *m, void *v, loff_t *pos)
1296 return __next(m, v, pos, TRACE_PIDS);
1300 np_next(struct seq_file *m, void *v, loff_t *pos)
1302 return __next(m, v, pos, TRACE_NO_PIDS);
1305 static void *__start(struct seq_file *m, loff_t *pos, int type)
1308 struct trace_pid_list *pid_list;
1309 struct trace_array *tr = m->private;
1312 * Grab the mutex, to keep calls to p_next() having the same
1313 * tr->filtered_pids as p_start() has.
1314 * If we just passed the tr->filtered_pids around, then RCU would
1315 * have been enough, but doing that makes things more complex.
1317 mutex_lock(&event_mutex);
1318 rcu_read_lock_sched();
1320 if (type == TRACE_PIDS)
1321 pid_list = rcu_dereference_sched(tr->filtered_pids);
1323 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1328 return trace_pid_start(pid_list, pos);
1331 static void *p_start(struct seq_file *m, loff_t *pos)
1334 return __start(m, pos, TRACE_PIDS);
1337 static void *np_start(struct seq_file *m, loff_t *pos)
1340 return __start(m, pos, TRACE_NO_PIDS);
1343 static void p_stop(struct seq_file *m, void *p)
1346 rcu_read_unlock_sched();
1347 mutex_unlock(&event_mutex);
1351 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1354 struct trace_event_file *file;
1355 unsigned long flags;
1358 mutex_lock(&event_mutex);
1359 file = event_file_data(filp);
1361 flags = file->flags;
1362 mutex_unlock(&event_mutex);
1367 if (flags & EVENT_FILE_FL_ENABLED &&
1368 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1371 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1372 flags & EVENT_FILE_FL_SOFT_MODE)
1377 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1381 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1384 struct trace_event_file *file;
1388 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1392 ret = tracing_update_buffers();
1400 mutex_lock(&event_mutex);
1401 file = event_file_data(filp);
1403 ret = ftrace_event_enable_disable(file, val);
1404 mutex_unlock(&event_mutex);
1413 return ret ? ret : cnt;
1417 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1420 const char set_to_char[4] = { '?', '0', '1', 'X' };
1421 struct trace_subsystem_dir *dir = filp->private_data;
1422 struct event_subsystem *system = dir->subsystem;
1423 struct trace_event_call *call;
1424 struct trace_event_file *file;
1425 struct trace_array *tr = dir->tr;
1430 mutex_lock(&event_mutex);
1431 list_for_each_entry(file, &tr->events, list) {
1432 call = file->event_call;
1433 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1434 !trace_event_name(call) || !call->class || !call->class->reg)
1437 if (system && strcmp(call->class->system, system->name) != 0)
1441 * We need to find out if all the events are set
1442 * or if all events or cleared, or if we have
1445 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1448 * If we have a mixture, no need to look further.
1453 mutex_unlock(&event_mutex);
1455 buf[0] = set_to_char[set];
1458 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1464 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1467 struct trace_subsystem_dir *dir = filp->private_data;
1468 struct event_subsystem *system = dir->subsystem;
1469 const char *name = NULL;
1473 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1477 ret = tracing_update_buffers();
1481 if (val != 0 && val != 1)
1485 * Opening of "enable" adds a ref count to system,
1486 * so the name is safe to use.
1489 name = system->name;
1491 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1505 FORMAT_FIELD_SEPERATOR = 2,
1506 FORMAT_PRINTFMT = 3,
1509 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1511 struct trace_event_call *call = event_file_data(m->private);
1512 struct list_head *common_head = &ftrace_common_fields;
1513 struct list_head *head = trace_get_fields(call);
1514 struct list_head *node = v;
1518 switch ((unsigned long)v) {
1523 case FORMAT_FIELD_SEPERATOR:
1527 case FORMAT_PRINTFMT:
1533 if (node == common_head)
1534 return (void *)FORMAT_FIELD_SEPERATOR;
1535 else if (node == head)
1536 return (void *)FORMAT_PRINTFMT;
1541 static int f_show(struct seq_file *m, void *v)
1543 struct trace_event_call *call = event_file_data(m->private);
1544 struct ftrace_event_field *field;
1545 const char *array_descriptor;
1547 switch ((unsigned long)v) {
1549 seq_printf(m, "name: %s\n", trace_event_name(call));
1550 seq_printf(m, "ID: %d\n", call->event.type);
1551 seq_puts(m, "format:\n");
1554 case FORMAT_FIELD_SEPERATOR:
1558 case FORMAT_PRINTFMT:
1559 seq_printf(m, "\nprint fmt: %s\n",
1564 field = list_entry(v, struct ftrace_event_field, link);
1566 * Smartly shows the array type(except dynamic array).
1569 * If TYPE := TYPE[LEN], it is shown:
1570 * field:TYPE VAR[LEN]
1572 array_descriptor = strchr(field->type, '[');
1574 if (str_has_prefix(field->type, "__data_loc"))
1575 array_descriptor = NULL;
1577 if (!array_descriptor)
1578 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1579 field->type, field->name, field->offset,
1580 field->size, !!field->is_signed);
1582 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1583 (int)(array_descriptor - field->type),
1584 field->type, field->name,
1585 array_descriptor, field->offset,
1586 field->size, !!field->is_signed);
1591 static void *f_start(struct seq_file *m, loff_t *pos)
1593 void *p = (void *)FORMAT_HEADER;
1596 /* ->stop() is called even if ->start() fails */
1597 mutex_lock(&event_mutex);
1598 if (!event_file_data(m->private))
1599 return ERR_PTR(-ENODEV);
1601 while (l < *pos && p)
1602 p = f_next(m, p, &l);
1607 static void f_stop(struct seq_file *m, void *p)
1609 mutex_unlock(&event_mutex);
1612 static const struct seq_operations trace_format_seq_ops = {
1619 static int trace_format_open(struct inode *inode, struct file *file)
1624 /* Do we want to hide event format files on tracefs lockdown? */
1626 ret = seq_open(file, &trace_format_seq_ops);
1630 m = file->private_data;
1637 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1639 int id = (long)event_file_data(filp);
1646 len = sprintf(buf, "%d\n", id);
1648 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1652 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1655 struct trace_event_file *file;
1656 struct trace_seq *s;
1662 s = kmalloc(sizeof(*s), GFP_KERNEL);
1669 mutex_lock(&event_mutex);
1670 file = event_file_data(filp);
1672 print_event_filter(file, s);
1673 mutex_unlock(&event_mutex);
1676 r = simple_read_from_buffer(ubuf, cnt, ppos,
1677 s->buffer, trace_seq_used(s));
1685 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1688 struct trace_event_file *file;
1692 if (cnt >= PAGE_SIZE)
1695 buf = memdup_user_nul(ubuf, cnt);
1697 return PTR_ERR(buf);
1699 mutex_lock(&event_mutex);
1700 file = event_file_data(filp);
1702 err = apply_event_filter(file, buf);
1703 mutex_unlock(&event_mutex);
1714 static LIST_HEAD(event_subsystems);
1716 static int subsystem_open(struct inode *inode, struct file *filp)
1718 struct event_subsystem *system = NULL;
1719 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1720 struct trace_array *tr;
1723 if (tracing_is_disabled())
1726 /* Make sure the system still exists */
1727 mutex_lock(&event_mutex);
1728 mutex_lock(&trace_types_lock);
1729 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1730 list_for_each_entry(dir, &tr->systems, list) {
1731 if (dir == inode->i_private) {
1732 /* Don't open systems with no events */
1733 if (dir->nr_events) {
1734 __get_system_dir(dir);
1735 system = dir->subsystem;
1742 mutex_unlock(&trace_types_lock);
1743 mutex_unlock(&event_mutex);
1748 /* Some versions of gcc think dir can be uninitialized here */
1751 /* Still need to increment the ref count of the system */
1752 if (trace_array_get(tr) < 0) {
1757 ret = tracing_open_generic(inode, filp);
1759 trace_array_put(tr);
1766 static int system_tr_open(struct inode *inode, struct file *filp)
1768 struct trace_subsystem_dir *dir;
1769 struct trace_array *tr = inode->i_private;
1772 /* Make a temporary dir that has no system but points to tr */
1773 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1777 ret = tracing_open_generic_tr(inode, filp);
1783 filp->private_data = dir;
1788 static int subsystem_release(struct inode *inode, struct file *file)
1790 struct trace_subsystem_dir *dir = file->private_data;
1792 trace_array_put(dir->tr);
1795 * If dir->subsystem is NULL, then this is a temporary
1796 * descriptor that was made for a trace_array to enable
1808 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1811 struct trace_subsystem_dir *dir = filp->private_data;
1812 struct event_subsystem *system = dir->subsystem;
1813 struct trace_seq *s;
1819 s = kmalloc(sizeof(*s), GFP_KERNEL);
1825 print_subsystem_event_filter(system, s);
1826 r = simple_read_from_buffer(ubuf, cnt, ppos,
1827 s->buffer, trace_seq_used(s));
1835 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1838 struct trace_subsystem_dir *dir = filp->private_data;
1842 if (cnt >= PAGE_SIZE)
1845 buf = memdup_user_nul(ubuf, cnt);
1847 return PTR_ERR(buf);
1849 err = apply_subsystem_event_filter(dir, buf);
1860 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1862 int (*func)(struct trace_seq *s) = filp->private_data;
1863 struct trace_seq *s;
1869 s = kmalloc(sizeof(*s), GFP_KERNEL);
1876 r = simple_read_from_buffer(ubuf, cnt, ppos,
1877 s->buffer, trace_seq_used(s));
1884 static void ignore_task_cpu(void *data)
1886 struct trace_array *tr = data;
1887 struct trace_pid_list *pid_list;
1888 struct trace_pid_list *no_pid_list;
1891 * This function is called by on_each_cpu() while the
1892 * event_mutex is held.
1894 pid_list = rcu_dereference_protected(tr->filtered_pids,
1895 mutex_is_locked(&event_mutex));
1896 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1897 mutex_is_locked(&event_mutex));
1899 this_cpu_write(tr->array_buffer.data->ignore_pid,
1900 trace_ignore_this_task(pid_list, no_pid_list, current));
1903 static void register_pid_events(struct trace_array *tr)
1906 * Register a probe that is called before all other probes
1907 * to set ignore_pid if next or prev do not match.
1908 * Register a probe this is called after all other probes
1909 * to only keep ignore_pid set if next pid matches.
1911 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1913 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1916 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1918 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1921 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1923 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1926 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1928 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1933 event_pid_write(struct file *filp, const char __user *ubuf,
1934 size_t cnt, loff_t *ppos, int type)
1936 struct seq_file *m = filp->private_data;
1937 struct trace_array *tr = m->private;
1938 struct trace_pid_list *filtered_pids = NULL;
1939 struct trace_pid_list *other_pids = NULL;
1940 struct trace_pid_list *pid_list;
1941 struct trace_event_file *file;
1947 ret = tracing_update_buffers();
1951 mutex_lock(&event_mutex);
1953 if (type == TRACE_PIDS) {
1954 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1955 lockdep_is_held(&event_mutex));
1956 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1957 lockdep_is_held(&event_mutex));
1959 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1960 lockdep_is_held(&event_mutex));
1961 other_pids = rcu_dereference_protected(tr->filtered_pids,
1962 lockdep_is_held(&event_mutex));
1965 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1969 if (type == TRACE_PIDS)
1970 rcu_assign_pointer(tr->filtered_pids, pid_list);
1972 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1974 list_for_each_entry(file, &tr->events, list) {
1975 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1978 if (filtered_pids) {
1979 tracepoint_synchronize_unregister();
1980 trace_pid_list_free(filtered_pids);
1981 } else if (pid_list && !other_pids) {
1982 register_pid_events(tr);
1986 * Ignoring of pids is done at task switch. But we have to
1987 * check for those tasks that are currently running.
1988 * Always do this in case a pid was appended or removed.
1990 on_each_cpu(ignore_task_cpu, tr, 1);
1993 mutex_unlock(&event_mutex);
2002 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2003 size_t cnt, loff_t *ppos)
2005 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2009 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2010 size_t cnt, loff_t *ppos)
2012 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2015 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2016 static int ftrace_event_set_open(struct inode *inode, struct file *file);
2017 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
2018 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
2019 static int ftrace_event_release(struct inode *inode, struct file *file);
2021 static const struct seq_operations show_event_seq_ops = {
2028 static const struct seq_operations show_set_event_seq_ops = {
2035 static const struct seq_operations show_set_pid_seq_ops = {
2038 .show = trace_pid_show,
2042 static const struct seq_operations show_set_no_pid_seq_ops = {
2045 .show = trace_pid_show,
2049 static const struct file_operations ftrace_avail_fops = {
2050 .open = ftrace_event_avail_open,
2052 .llseek = seq_lseek,
2053 .release = seq_release,
2056 static const struct file_operations ftrace_set_event_fops = {
2057 .open = ftrace_event_set_open,
2059 .write = ftrace_event_write,
2060 .llseek = seq_lseek,
2061 .release = ftrace_event_release,
2064 static const struct file_operations ftrace_set_event_pid_fops = {
2065 .open = ftrace_event_set_pid_open,
2067 .write = ftrace_event_pid_write,
2068 .llseek = seq_lseek,
2069 .release = ftrace_event_release,
2072 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2073 .open = ftrace_event_set_npid_open,
2075 .write = ftrace_event_npid_write,
2076 .llseek = seq_lseek,
2077 .release = ftrace_event_release,
2080 static const struct file_operations ftrace_enable_fops = {
2081 .open = tracing_open_file_tr,
2082 .read = event_enable_read,
2083 .write = event_enable_write,
2084 .release = tracing_release_file_tr,
2085 .llseek = default_llseek,
2088 static const struct file_operations ftrace_event_format_fops = {
2089 .open = trace_format_open,
2091 .llseek = seq_lseek,
2092 .release = seq_release,
2095 static const struct file_operations ftrace_event_id_fops = {
2096 .read = event_id_read,
2097 .llseek = default_llseek,
2100 static const struct file_operations ftrace_event_filter_fops = {
2101 .open = tracing_open_file_tr,
2102 .read = event_filter_read,
2103 .write = event_filter_write,
2104 .release = tracing_release_file_tr,
2105 .llseek = default_llseek,
2108 static const struct file_operations ftrace_subsystem_filter_fops = {
2109 .open = subsystem_open,
2110 .read = subsystem_filter_read,
2111 .write = subsystem_filter_write,
2112 .llseek = default_llseek,
2113 .release = subsystem_release,
2116 static const struct file_operations ftrace_system_enable_fops = {
2117 .open = subsystem_open,
2118 .read = system_enable_read,
2119 .write = system_enable_write,
2120 .llseek = default_llseek,
2121 .release = subsystem_release,
2124 static const struct file_operations ftrace_tr_enable_fops = {
2125 .open = system_tr_open,
2126 .read = system_enable_read,
2127 .write = system_enable_write,
2128 .llseek = default_llseek,
2129 .release = subsystem_release,
2132 static const struct file_operations ftrace_show_header_fops = {
2133 .open = tracing_open_generic,
2134 .read = show_header,
2135 .llseek = default_llseek,
2139 ftrace_event_open(struct inode *inode, struct file *file,
2140 const struct seq_operations *seq_ops)
2145 ret = security_locked_down(LOCKDOWN_TRACEFS);
2149 ret = seq_open(file, seq_ops);
2152 m = file->private_data;
2153 /* copy tr over to seq ops */
2154 m->private = inode->i_private;
2159 static int ftrace_event_release(struct inode *inode, struct file *file)
2161 struct trace_array *tr = inode->i_private;
2163 trace_array_put(tr);
2165 return seq_release(inode, file);
2169 ftrace_event_avail_open(struct inode *inode, struct file *file)
2171 const struct seq_operations *seq_ops = &show_event_seq_ops;
2173 /* Checks for tracefs lockdown */
2174 return ftrace_event_open(inode, file, seq_ops);
2178 ftrace_event_set_open(struct inode *inode, struct file *file)
2180 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
2181 struct trace_array *tr = inode->i_private;
2184 ret = tracing_check_open_get_tr(tr);
2188 if ((file->f_mode & FMODE_WRITE) &&
2189 (file->f_flags & O_TRUNC))
2190 ftrace_clear_events(tr);
2192 ret = ftrace_event_open(inode, file, seq_ops);
2194 trace_array_put(tr);
2199 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2201 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2202 struct trace_array *tr = inode->i_private;
2205 ret = tracing_check_open_get_tr(tr);
2209 if ((file->f_mode & FMODE_WRITE) &&
2210 (file->f_flags & O_TRUNC))
2211 ftrace_clear_event_pids(tr, TRACE_PIDS);
2213 ret = ftrace_event_open(inode, file, seq_ops);
2215 trace_array_put(tr);
2220 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2222 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2223 struct trace_array *tr = inode->i_private;
2226 ret = tracing_check_open_get_tr(tr);
2230 if ((file->f_mode & FMODE_WRITE) &&
2231 (file->f_flags & O_TRUNC))
2232 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2234 ret = ftrace_event_open(inode, file, seq_ops);
2236 trace_array_put(tr);
2240 static struct event_subsystem *
2241 create_new_subsystem(const char *name)
2243 struct event_subsystem *system;
2245 /* need to create new entry */
2246 system = kmalloc(sizeof(*system), GFP_KERNEL);
2250 system->ref_count = 1;
2252 /* Only allocate if dynamic (kprobes and modules) */
2253 system->name = kstrdup_const(name, GFP_KERNEL);
2257 system->filter = NULL;
2259 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2260 if (!system->filter)
2263 list_add(&system->list, &event_subsystems);
2268 kfree_const(system->name);
2273 static struct dentry *
2274 event_subsystem_dir(struct trace_array *tr, const char *name,
2275 struct trace_event_file *file, struct dentry *parent)
2277 struct trace_subsystem_dir *dir;
2278 struct event_subsystem *system;
2279 struct dentry *entry;
2281 /* First see if we did not already create this dir */
2282 list_for_each_entry(dir, &tr->systems, list) {
2283 system = dir->subsystem;
2284 if (strcmp(system->name, name) == 0) {
2291 /* Now see if the system itself exists. */
2292 list_for_each_entry(system, &event_subsystems, list) {
2293 if (strcmp(system->name, name) == 0)
2296 /* Reset system variable when not found */
2297 if (&system->list == &event_subsystems)
2300 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2305 system = create_new_subsystem(name);
2309 __get_system(system);
2311 dir->entry = tracefs_create_dir(name, parent);
2313 pr_warn("Failed to create system directory %s\n", name);
2314 __put_system(system);
2321 dir->subsystem = system;
2324 /* the ftrace system is special, do not create enable or filter files */
2325 if (strcmp(name, "ftrace") != 0) {
2327 entry = tracefs_create_file("filter", TRACE_MODE_WRITE,
2329 &ftrace_subsystem_filter_fops);
2331 kfree(system->filter);
2332 system->filter = NULL;
2333 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2336 trace_create_file("enable", TRACE_MODE_WRITE, dir->entry, dir,
2337 &ftrace_system_enable_fops);
2340 list_add(&dir->list, &tr->systems);
2347 /* Only print this message if failed on memory allocation */
2348 if (!dir || !system)
2349 pr_warn("No memory to create event subsystem %s\n", name);
2354 event_define_fields(struct trace_event_call *call)
2356 struct list_head *head;
2360 * Other events may have the same class. Only update
2361 * the fields if they are not already defined.
2363 head = trace_get_fields(call);
2364 if (list_empty(head)) {
2365 struct trace_event_fields *field = call->class->fields_array;
2366 unsigned int offset = sizeof(struct trace_entry);
2368 for (; field->type; field++) {
2369 if (field->type == TRACE_FUNCTION_TYPE) {
2370 field->define_fields(call);
2374 offset = ALIGN(offset, field->align);
2375 ret = trace_define_field(call, field->type, field->name,
2376 offset, field->size,
2377 field->is_signed, field->filter_type);
2378 if (WARN_ON_ONCE(ret)) {
2379 pr_err("error code is %d\n", ret);
2383 offset += field->size;
2391 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2393 struct trace_event_call *call = file->event_call;
2394 struct trace_array *tr = file->tr;
2395 struct dentry *d_events;
2400 * If the trace point header did not define TRACE_SYSTEM
2401 * then the system would be called "TRACE_SYSTEM".
2403 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2404 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2410 name = trace_event_name(call);
2411 file->dir = tracefs_create_dir(name, d_events);
2413 pr_warn("Could not create tracefs '%s' directory\n", name);
2417 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2418 trace_create_file("enable", TRACE_MODE_WRITE, file->dir, file,
2419 &ftrace_enable_fops);
2421 #ifdef CONFIG_PERF_EVENTS
2422 if (call->event.type && call->class->reg)
2423 trace_create_file("id", TRACE_MODE_READ, file->dir,
2424 (void *)(long)call->event.type,
2425 &ftrace_event_id_fops);
2428 ret = event_define_fields(call);
2430 pr_warn("Could not initialize trace point events/%s\n", name);
2435 * Only event directories that can be enabled should have
2436 * triggers or filters.
2438 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2439 trace_create_file("filter", TRACE_MODE_WRITE, file->dir,
2440 file, &ftrace_event_filter_fops);
2442 trace_create_file("trigger", TRACE_MODE_WRITE, file->dir,
2443 file, &event_trigger_fops);
2446 #ifdef CONFIG_HIST_TRIGGERS
2447 trace_create_file("hist", TRACE_MODE_READ, file->dir, file,
2450 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2451 trace_create_file("hist_debug", TRACE_MODE_READ, file->dir, file,
2452 &event_hist_debug_fops);
2454 trace_create_file("format", TRACE_MODE_READ, file->dir, call,
2455 &ftrace_event_format_fops);
2457 #ifdef CONFIG_TRACE_EVENT_INJECT
2458 if (call->event.type && call->class->reg)
2459 trace_create_file("inject", 0200, file->dir, file,
2460 &event_inject_fops);
2466 static void remove_event_from_tracers(struct trace_event_call *call)
2468 struct trace_event_file *file;
2469 struct trace_array *tr;
2471 do_for_each_event_file_safe(tr, file) {
2472 if (file->event_call != call)
2475 remove_event_file_dir(file);
2477 * The do_for_each_event_file_safe() is
2478 * a double loop. After finding the call for this
2479 * trace_array, we use break to jump to the next
2483 } while_for_each_event_file();
2486 static void event_remove(struct trace_event_call *call)
2488 struct trace_array *tr;
2489 struct trace_event_file *file;
2491 do_for_each_event_file(tr, file) {
2492 if (file->event_call != call)
2495 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2496 tr->clear_trace = true;
2498 ftrace_event_enable_disable(file, 0);
2500 * The do_for_each_event_file() is
2501 * a double loop. After finding the call for this
2502 * trace_array, we use break to jump to the next
2506 } while_for_each_event_file();
2508 if (call->event.funcs)
2509 __unregister_trace_event(&call->event);
2510 remove_event_from_tracers(call);
2511 list_del(&call->list);
2514 static int event_init(struct trace_event_call *call)
2519 name = trace_event_name(call);
2523 if (call->class->raw_init) {
2524 ret = call->class->raw_init(call);
2525 if (ret < 0 && ret != -ENOSYS)
2526 pr_warn("Could not initialize trace events/%s\n", name);
2533 __register_event(struct trace_event_call *call, struct module *mod)
2537 ret = event_init(call);
2541 list_add(&call->list, &ftrace_events);
2542 if (call->flags & TRACE_EVENT_FL_DYNAMIC)
2543 atomic_set(&call->refcnt, 0);
2550 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2555 /* Find the length of the eval value as a string */
2556 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2557 /* Make sure there's enough room to replace the string with the value */
2561 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2563 /* Get the rest of the string of ptr */
2564 rlen = strlen(ptr + len);
2565 memmove(ptr + elen, ptr + len, rlen);
2566 /* Make sure we end the new string */
2567 ptr[elen + rlen] = 0;
2572 static void update_event_printk(struct trace_event_call *call,
2573 struct trace_eval_map *map)
2577 int len = strlen(map->eval_string);
2579 for (ptr = call->print_fmt; *ptr; ptr++) {
2593 if (isdigit(*ptr)) {
2597 /* Check for alpha chars like ULL */
2598 } while (isalnum(*ptr));
2602 * A number must have some kind of delimiter after
2603 * it, and we can ignore that too.
2607 if (isalpha(*ptr) || *ptr == '_') {
2608 if (strncmp(map->eval_string, ptr, len) == 0 &&
2609 !isalnum(ptr[len]) && ptr[len] != '_') {
2610 ptr = eval_replace(ptr, map, len);
2611 /* enum/sizeof string smaller than value */
2612 if (WARN_ON_ONCE(!ptr))
2615 * No need to decrement here, as eval_replace()
2616 * returns the pointer to the character passed
2617 * the eval, and two evals can not be placed
2618 * back to back without something in between.
2619 * We can skip that something in between.
2626 } while (isalnum(*ptr) || *ptr == '_');
2630 * If what comes after this variable is a '.' or
2631 * '->' then we can continue to ignore that string.
2633 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2634 ptr += *ptr == '.' ? 1 : 2;
2640 * Once again, we can skip the delimiter that came
2648 static void add_str_to_module(struct module *module, char *str)
2650 struct module_string *modstr;
2652 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
2655 * If we failed to allocate memory here, then we'll just
2656 * let the str memory leak when the module is removed.
2657 * If this fails to allocate, there's worse problems than
2658 * a leaked string on module removal.
2660 if (WARN_ON_ONCE(!modstr))
2663 modstr->module = module;
2666 list_add(&modstr->next, &module_strings);
2669 static void update_event_fields(struct trace_event_call *call,
2670 struct trace_eval_map *map)
2672 struct ftrace_event_field *field;
2673 struct list_head *head;
2676 int len = strlen(map->eval_string);
2678 /* Dynamic events should never have field maps */
2679 if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
2682 head = trace_get_fields(call);
2683 list_for_each_entry(field, head, link) {
2684 ptr = strchr(field->type, '[');
2689 if (!isalpha(*ptr) && *ptr != '_')
2692 if (strncmp(map->eval_string, ptr, len) != 0)
2695 str = kstrdup(field->type, GFP_KERNEL);
2696 if (WARN_ON_ONCE(!str))
2698 ptr = str + (ptr - field->type);
2699 ptr = eval_replace(ptr, map, len);
2700 /* enum/sizeof string smaller than value */
2701 if (WARN_ON_ONCE(!ptr)) {
2707 * If the event is part of a module, then we need to free the string
2708 * when the module is removed. Otherwise, it will stay allocated
2712 add_str_to_module(call->module, str);
2718 void trace_event_eval_update(struct trace_eval_map **map, int len)
2720 struct trace_event_call *call, *p;
2721 const char *last_system = NULL;
2726 down_write(&trace_event_sem);
2727 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2728 /* events are usually grouped together with systems */
2729 if (!last_system || call->class->system != last_system) {
2732 last_system = call->class->system;
2736 * Since calls are grouped by systems, the likelihood that the
2737 * next call in the iteration belongs to the same system as the
2738 * previous call is high. As an optimization, we skip searching
2739 * for a map[] that matches the call's system if the last call
2740 * was from the same system. That's what last_i is for. If the
2741 * call has the same system as the previous call, then last_i
2742 * will be the index of the first map[] that has a matching
2745 for (i = last_i; i < len; i++) {
2746 if (call->class->system == map[i]->system) {
2747 /* Save the first system if need be */
2752 update_event_printk(call, map[i]);
2753 update_event_fields(call, map[i]);
2758 up_write(&trace_event_sem);
2761 static struct trace_event_file *
2762 trace_create_new_event(struct trace_event_call *call,
2763 struct trace_array *tr)
2765 struct trace_pid_list *no_pid_list;
2766 struct trace_pid_list *pid_list;
2767 struct trace_event_file *file;
2769 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2773 pid_list = rcu_dereference_protected(tr->filtered_pids,
2774 lockdep_is_held(&event_mutex));
2775 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2776 lockdep_is_held(&event_mutex));
2778 if (pid_list || no_pid_list)
2779 file->flags |= EVENT_FILE_FL_PID_FILTER;
2781 file->event_call = call;
2783 atomic_set(&file->sm_ref, 0);
2784 atomic_set(&file->tm_ref, 0);
2785 INIT_LIST_HEAD(&file->triggers);
2786 list_add(&file->list, &tr->events);
2791 /* Add an event to a trace directory */
2793 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2795 struct trace_event_file *file;
2797 file = trace_create_new_event(call, tr);
2801 if (eventdir_initialized)
2802 return event_create_dir(tr->event_dir, file);
2804 return event_define_fields(call);
2808 * Just create a descriptor for early init. A descriptor is required
2809 * for enabling events at boot. We want to enable events before
2810 * the filesystem is initialized.
2813 __trace_early_add_new_event(struct trace_event_call *call,
2814 struct trace_array *tr)
2816 struct trace_event_file *file;
2818 file = trace_create_new_event(call, tr);
2822 return event_define_fields(call);
2825 struct ftrace_module_file_ops;
2826 static void __add_event_to_tracers(struct trace_event_call *call);
2828 /* Add an additional event_call dynamically */
2829 int trace_add_event_call(struct trace_event_call *call)
2832 lockdep_assert_held(&event_mutex);
2834 mutex_lock(&trace_types_lock);
2836 ret = __register_event(call, NULL);
2838 __add_event_to_tracers(call);
2840 mutex_unlock(&trace_types_lock);
2845 * Must be called under locking of trace_types_lock, event_mutex and
2848 static void __trace_remove_event_call(struct trace_event_call *call)
2851 trace_destroy_fields(call);
2852 free_event_filter(call->filter);
2853 call->filter = NULL;
2856 static int probe_remove_event_call(struct trace_event_call *call)
2858 struct trace_array *tr;
2859 struct trace_event_file *file;
2861 #ifdef CONFIG_PERF_EVENTS
2862 if (call->perf_refcount)
2865 do_for_each_event_file(tr, file) {
2866 if (file->event_call != call)
2869 * We can't rely on ftrace_event_enable_disable(enable => 0)
2870 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2871 * TRACE_REG_UNREGISTER.
2873 if (file->flags & EVENT_FILE_FL_ENABLED)
2876 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2877 tr->clear_trace = true;
2879 * The do_for_each_event_file_safe() is
2880 * a double loop. After finding the call for this
2881 * trace_array, we use break to jump to the next
2885 } while_for_each_event_file();
2887 __trace_remove_event_call(call);
2891 /* No need to clear the trace now */
2892 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2893 tr->clear_trace = false;
2898 /* Remove an event_call */
2899 int trace_remove_event_call(struct trace_event_call *call)
2903 lockdep_assert_held(&event_mutex);
2905 mutex_lock(&trace_types_lock);
2906 down_write(&trace_event_sem);
2907 ret = probe_remove_event_call(call);
2908 up_write(&trace_event_sem);
2909 mutex_unlock(&trace_types_lock);
2914 #define for_each_event(event, start, end) \
2915 for (event = start; \
2916 (unsigned long)event < (unsigned long)end; \
2919 #ifdef CONFIG_MODULES
2921 static void trace_module_add_events(struct module *mod)
2923 struct trace_event_call **call, **start, **end;
2925 if (!mod->num_trace_events)
2928 /* Don't add infrastructure for mods without tracepoints */
2929 if (trace_module_has_bad_taint(mod)) {
2930 pr_err("%s: module has bad taint, not creating trace events\n",
2935 start = mod->trace_events;
2936 end = mod->trace_events + mod->num_trace_events;
2938 for_each_event(call, start, end) {
2939 __register_event(*call, mod);
2940 __add_event_to_tracers(*call);
2944 static void trace_module_remove_events(struct module *mod)
2946 struct trace_event_call *call, *p;
2947 struct module_string *modstr, *m;
2949 down_write(&trace_event_sem);
2950 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2951 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
2953 if (call->module == mod)
2954 __trace_remove_event_call(call);
2956 /* Check for any strings allocade for this module */
2957 list_for_each_entry_safe(modstr, m, &module_strings, next) {
2958 if (modstr->module != mod)
2960 list_del(&modstr->next);
2964 up_write(&trace_event_sem);
2967 * It is safest to reset the ring buffer if the module being unloaded
2968 * registered any events that were used. The only worry is if
2969 * a new module gets loaded, and takes on the same id as the events
2970 * of this module. When printing out the buffer, traced events left
2971 * over from this module may be passed to the new module events and
2972 * unexpected results may occur.
2974 tracing_reset_all_online_cpus_unlocked();
2977 static int trace_module_notify(struct notifier_block *self,
2978 unsigned long val, void *data)
2980 struct module *mod = data;
2982 mutex_lock(&event_mutex);
2983 mutex_lock(&trace_types_lock);
2985 case MODULE_STATE_COMING:
2986 trace_module_add_events(mod);
2988 case MODULE_STATE_GOING:
2989 trace_module_remove_events(mod);
2992 mutex_unlock(&trace_types_lock);
2993 mutex_unlock(&event_mutex);
2998 static struct notifier_block trace_module_nb = {
2999 .notifier_call = trace_module_notify,
3000 .priority = 1, /* higher than trace.c module notify */
3002 #endif /* CONFIG_MODULES */
3004 /* Create a new event directory structure for a trace directory. */
3006 __trace_add_event_dirs(struct trace_array *tr)
3008 struct trace_event_call *call;
3011 list_for_each_entry(call, &ftrace_events, list) {
3012 ret = __trace_add_new_event(call, tr);
3014 pr_warn("Could not create directory for event %s\n",
3015 trace_event_name(call));
3019 /* Returns any file that matches the system and event */
3020 struct trace_event_file *
3021 __find_event_file(struct trace_array *tr, const char *system, const char *event)
3023 struct trace_event_file *file;
3024 struct trace_event_call *call;
3027 list_for_each_entry(file, &tr->events, list) {
3029 call = file->event_call;
3030 name = trace_event_name(call);
3032 if (!name || !call->class)
3035 if (strcmp(event, name) == 0 &&
3036 strcmp(system, call->class->system) == 0)
3042 /* Returns valid trace event files that match system and event */
3043 struct trace_event_file *
3044 find_event_file(struct trace_array *tr, const char *system, const char *event)
3046 struct trace_event_file *file;
3048 file = __find_event_file(tr, system, event);
3049 if (!file || !file->event_call->class->reg ||
3050 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
3057 * trace_get_event_file - Find and return a trace event file
3058 * @instance: The name of the trace instance containing the event
3059 * @system: The name of the system containing the event
3060 * @event: The name of the event
3062 * Return a trace event file given the trace instance name, trace
3063 * system, and trace event name. If the instance name is NULL, it
3064 * refers to the top-level trace array.
3066 * This function will look it up and return it if found, after calling
3067 * trace_array_get() to prevent the instance from going away, and
3068 * increment the event's module refcount to prevent it from being
3071 * To release the file, call trace_put_event_file(), which will call
3072 * trace_array_put() and decrement the event's module refcount.
3074 * Return: The trace event on success, ERR_PTR otherwise.
3076 struct trace_event_file *trace_get_event_file(const char *instance,
3080 struct trace_array *tr = top_trace_array();
3081 struct trace_event_file *file = NULL;
3085 tr = trace_array_find_get(instance);
3087 return ERR_PTR(-ENOENT);
3089 ret = trace_array_get(tr);
3091 return ERR_PTR(ret);
3094 mutex_lock(&event_mutex);
3096 file = find_event_file(tr, system, event);
3098 trace_array_put(tr);
3103 /* Don't let event modules unload while in use */
3104 ret = trace_event_try_get_ref(file->event_call);
3106 trace_array_put(tr);
3113 mutex_unlock(&event_mutex);
3116 file = ERR_PTR(ret);
3120 EXPORT_SYMBOL_GPL(trace_get_event_file);
3123 * trace_put_event_file - Release a file from trace_get_event_file()
3124 * @file: The trace event file
3126 * If a file was retrieved using trace_get_event_file(), this should
3127 * be called when it's no longer needed. It will cancel the previous
3128 * trace_array_get() called by that function, and decrement the
3129 * event's module refcount.
3131 void trace_put_event_file(struct trace_event_file *file)
3133 mutex_lock(&event_mutex);
3134 trace_event_put_ref(file->event_call);
3135 mutex_unlock(&event_mutex);
3137 trace_array_put(file->tr);
3139 EXPORT_SYMBOL_GPL(trace_put_event_file);
3141 #ifdef CONFIG_DYNAMIC_FTRACE
3144 #define ENABLE_EVENT_STR "enable_event"
3145 #define DISABLE_EVENT_STR "disable_event"
3147 struct event_probe_data {
3148 struct trace_event_file *file;
3149 unsigned long count;
3154 static void update_event_probe(struct event_probe_data *data)
3157 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3159 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3163 event_enable_probe(unsigned long ip, unsigned long parent_ip,
3164 struct trace_array *tr, struct ftrace_probe_ops *ops,
3167 struct ftrace_func_mapper *mapper = data;
3168 struct event_probe_data *edata;
3171 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3172 if (!pdata || !*pdata)
3176 update_event_probe(edata);
3180 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
3181 struct trace_array *tr, struct ftrace_probe_ops *ops,
3184 struct ftrace_func_mapper *mapper = data;
3185 struct event_probe_data *edata;
3188 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3189 if (!pdata || !*pdata)
3197 /* Skip if the event is in a state we want to switch to */
3198 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3201 if (edata->count != -1)
3204 update_event_probe(edata);
3208 event_enable_print(struct seq_file *m, unsigned long ip,
3209 struct ftrace_probe_ops *ops, void *data)
3211 struct ftrace_func_mapper *mapper = data;
3212 struct event_probe_data *edata;
3215 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3217 if (WARN_ON_ONCE(!pdata || !*pdata))
3222 seq_printf(m, "%ps:", (void *)ip);
3224 seq_printf(m, "%s:%s:%s",
3225 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
3226 edata->file->event_call->class->system,
3227 trace_event_name(edata->file->event_call));
3229 if (edata->count == -1)
3230 seq_puts(m, ":unlimited\n");
3232 seq_printf(m, ":count=%ld\n", edata->count);
3238 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
3239 unsigned long ip, void *init_data, void **data)
3241 struct ftrace_func_mapper *mapper = *data;
3242 struct event_probe_data *edata = init_data;
3246 mapper = allocate_ftrace_func_mapper();
3252 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
3261 static int free_probe_data(void *data)
3263 struct event_probe_data *edata = data;
3267 /* Remove the SOFT_MODE flag */
3268 __ftrace_event_enable_disable(edata->file, 0, 1);
3269 trace_event_put_ref(edata->file->event_call);
3276 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
3277 unsigned long ip, void *data)
3279 struct ftrace_func_mapper *mapper = data;
3280 struct event_probe_data *edata;
3285 free_ftrace_func_mapper(mapper, free_probe_data);
3289 edata = ftrace_func_mapper_remove_ip(mapper, ip);
3291 if (WARN_ON_ONCE(!edata))
3294 if (WARN_ON_ONCE(edata->ref <= 0))
3297 free_probe_data(edata);
3300 static struct ftrace_probe_ops event_enable_probe_ops = {
3301 .func = event_enable_probe,
3302 .print = event_enable_print,
3303 .init = event_enable_init,
3304 .free = event_enable_free,
3307 static struct ftrace_probe_ops event_enable_count_probe_ops = {
3308 .func = event_enable_count_probe,
3309 .print = event_enable_print,
3310 .init = event_enable_init,
3311 .free = event_enable_free,
3314 static struct ftrace_probe_ops event_disable_probe_ops = {
3315 .func = event_enable_probe,
3316 .print = event_enable_print,
3317 .init = event_enable_init,
3318 .free = event_enable_free,
3321 static struct ftrace_probe_ops event_disable_count_probe_ops = {
3322 .func = event_enable_count_probe,
3323 .print = event_enable_print,
3324 .init = event_enable_init,
3325 .free = event_enable_free,
3329 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3330 char *glob, char *cmd, char *param, int enabled)
3332 struct trace_event_file *file;
3333 struct ftrace_probe_ops *ops;
3334 struct event_probe_data *data;
3344 /* hash funcs only work with set_ftrace_filter */
3345 if (!enabled || !param)
3348 system = strsep(¶m, ":");
3352 event = strsep(¶m, ":");
3354 mutex_lock(&event_mutex);
3357 file = find_event_file(tr, system, event);
3361 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3364 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3366 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3368 if (glob[0] == '!') {
3369 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3375 data = kzalloc(sizeof(*data), GFP_KERNEL);
3379 data->enable = enable;
3386 number = strsep(¶m, ":");
3389 if (!strlen(number))
3393 * We use the callback data field (which is a pointer)
3396 ret = kstrtoul(number, 0, &data->count);
3401 /* Don't let event modules unload while probe registered */
3402 ret = trace_event_try_get_ref(file->event_call);
3408 ret = __ftrace_event_enable_disable(file, 1, 1);
3412 ret = register_ftrace_function_probe(glob, tr, ops, data);
3414 * The above returns on success the # of functions enabled,
3415 * but if it didn't find any functions it returns zero.
3416 * Consider no functions a failure too.
3423 /* Just return zero, not the number of enabled functions */
3426 mutex_unlock(&event_mutex);
3430 __ftrace_event_enable_disable(file, 0, 1);
3432 trace_event_put_ref(file->event_call);
3438 static struct ftrace_func_command event_enable_cmd = {
3439 .name = ENABLE_EVENT_STR,
3440 .func = event_enable_func,
3443 static struct ftrace_func_command event_disable_cmd = {
3444 .name = DISABLE_EVENT_STR,
3445 .func = event_enable_func,
3448 static __init int register_event_cmds(void)
3452 ret = register_ftrace_command(&event_enable_cmd);
3453 if (WARN_ON(ret < 0))
3455 ret = register_ftrace_command(&event_disable_cmd);
3456 if (WARN_ON(ret < 0))
3457 unregister_ftrace_command(&event_enable_cmd);
3461 static inline int register_event_cmds(void) { return 0; }
3462 #endif /* CONFIG_DYNAMIC_FTRACE */
3465 * The top level array and trace arrays created by boot-time tracing
3466 * have already had its trace_event_file descriptors created in order
3467 * to allow for early events to be recorded.
3468 * This function is called after the tracefs has been initialized,
3469 * and we now have to create the files associated to the events.
3471 static void __trace_early_add_event_dirs(struct trace_array *tr)
3473 struct trace_event_file *file;
3477 list_for_each_entry(file, &tr->events, list) {
3478 ret = event_create_dir(tr->event_dir, file);
3480 pr_warn("Could not create directory for event %s\n",
3481 trace_event_name(file->event_call));
3486 * For early boot up, the top trace array and the trace arrays created
3487 * by boot-time tracing require to have a list of events that can be
3488 * enabled. This must be done before the filesystem is set up in order
3489 * to allow events to be traced early.
3491 void __trace_early_add_events(struct trace_array *tr)
3493 struct trace_event_call *call;
3496 list_for_each_entry(call, &ftrace_events, list) {
3497 /* Early boot up should not have any modules loaded */
3498 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
3499 WARN_ON_ONCE(call->module))
3502 ret = __trace_early_add_new_event(call, tr);
3504 pr_warn("Could not create early event %s\n",
3505 trace_event_name(call));
3509 /* Remove the event directory structure for a trace directory. */
3511 __trace_remove_event_dirs(struct trace_array *tr)
3513 struct trace_event_file *file, *next;
3515 list_for_each_entry_safe(file, next, &tr->events, list)
3516 remove_event_file_dir(file);
3519 static void __add_event_to_tracers(struct trace_event_call *call)
3521 struct trace_array *tr;
3523 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3524 __trace_add_new_event(call, tr);
3527 extern struct trace_event_call *__start_ftrace_events[];
3528 extern struct trace_event_call *__stop_ftrace_events[];
3530 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3532 static __init int setup_trace_event(char *str)
3534 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3535 ring_buffer_expanded = true;
3536 disable_tracing_selftest("running event tracing");
3540 __setup("trace_event=", setup_trace_event);
3542 /* Expects to have event_mutex held when called */
3544 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3546 struct dentry *d_events;
3547 struct dentry *entry;
3549 entry = tracefs_create_file("set_event", TRACE_MODE_WRITE, parent,
3550 tr, &ftrace_set_event_fops);
3552 pr_warn("Could not create tracefs 'set_event' entry\n");
3556 d_events = tracefs_create_dir("events", parent);
3558 pr_warn("Could not create tracefs 'events' directory\n");
3562 entry = trace_create_file("enable", TRACE_MODE_WRITE, d_events,
3563 tr, &ftrace_tr_enable_fops);
3565 pr_warn("Could not create tracefs 'enable' entry\n");
3569 /* There are not as crucial, just warn if they are not created */
3571 entry = tracefs_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
3572 tr, &ftrace_set_event_pid_fops);
3574 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3576 entry = tracefs_create_file("set_event_notrace_pid",
3577 TRACE_MODE_WRITE, parent, tr,
3578 &ftrace_set_event_notrace_pid_fops);
3580 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
3582 /* ring buffer internal formats */
3583 entry = trace_create_file("header_page", TRACE_MODE_READ, d_events,
3584 ring_buffer_print_page_header,
3585 &ftrace_show_header_fops);
3587 pr_warn("Could not create tracefs 'header_page' entry\n");
3589 entry = trace_create_file("header_event", TRACE_MODE_READ, d_events,
3590 ring_buffer_print_entry_header,
3591 &ftrace_show_header_fops);
3593 pr_warn("Could not create tracefs 'header_event' entry\n");
3595 tr->event_dir = d_events;
3601 * event_trace_add_tracer - add a instance of a trace_array to events
3602 * @parent: The parent dentry to place the files/directories for events in
3603 * @tr: The trace array associated with these events
3605 * When a new instance is created, it needs to set up its events
3606 * directory, as well as other files associated with events. It also
3607 * creates the event hierarchy in the @parent/events directory.
3609 * Returns 0 on success.
3611 * Must be called with event_mutex held.
3613 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3617 lockdep_assert_held(&event_mutex);
3619 ret = create_event_toplevel_files(parent, tr);
3623 down_write(&trace_event_sem);
3624 /* If tr already has the event list, it is initialized in early boot. */
3625 if (unlikely(!list_empty(&tr->events)))
3626 __trace_early_add_event_dirs(tr);
3628 __trace_add_event_dirs(tr);
3629 up_write(&trace_event_sem);
3636 * The top trace array already had its file descriptors created.
3637 * Now the files themselves need to be created.
3640 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3644 mutex_lock(&event_mutex);
3646 ret = create_event_toplevel_files(parent, tr);
3650 down_write(&trace_event_sem);
3651 __trace_early_add_event_dirs(tr);
3652 up_write(&trace_event_sem);
3655 mutex_unlock(&event_mutex);
3660 /* Must be called with event_mutex held */
3661 int event_trace_del_tracer(struct trace_array *tr)
3663 lockdep_assert_held(&event_mutex);
3665 /* Disable any event triggers and associated soft-disabled events */
3666 clear_event_triggers(tr);
3668 /* Clear the pid list */
3669 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3671 /* Disable any running events */
3672 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3674 /* Make sure no more events are being executed */
3675 tracepoint_synchronize_unregister();
3677 down_write(&trace_event_sem);
3678 __trace_remove_event_dirs(tr);
3679 tracefs_remove(tr->event_dir);
3680 up_write(&trace_event_sem);
3682 tr->event_dir = NULL;
3687 static __init int event_trace_memsetup(void)
3689 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3690 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3695 early_enable_events(struct trace_array *tr, bool disable_first)
3697 char *buf = bootup_event_buf;
3702 token = strsep(&buf, ",");
3708 /* Restarting syscalls requires that we stop them first */
3710 ftrace_set_clr_event(tr, token, 0);
3712 ret = ftrace_set_clr_event(tr, token, 1);
3714 pr_warn("Failed to enable trace event: %s\n", token);
3717 /* Put back the comma to allow this to be called again */
3723 static __init int event_trace_enable(void)
3725 struct trace_array *tr = top_trace_array();
3726 struct trace_event_call **iter, *call;
3732 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3735 ret = event_init(call);
3737 list_add(&call->list, &ftrace_events);
3741 * We need the top trace array to have a working set of trace
3742 * points at early init, before the debug files and directories
3743 * are created. Create the file entries now, and attach them
3744 * to the actual file dentries later.
3746 __trace_early_add_events(tr);
3748 early_enable_events(tr, false);
3750 trace_printk_start_comm();
3752 register_event_cmds();
3754 register_trigger_cmds();
3760 * event_trace_enable() is called from trace_event_init() first to
3761 * initialize events and perhaps start any events that are on the
3762 * command line. Unfortunately, there are some events that will not
3763 * start this early, like the system call tracepoints that need
3764 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
3765 * event_trace_enable() is called before pid 1 starts, and this flag
3766 * is never set, making the syscall tracepoint never get reached, but
3767 * the event is enabled regardless (and not doing anything).
3769 static __init int event_trace_enable_again(void)
3771 struct trace_array *tr;
3773 tr = top_trace_array();
3777 early_enable_events(tr, true);
3782 early_initcall(event_trace_enable_again);
3784 /* Init fields which doesn't related to the tracefs */
3785 static __init int event_trace_init_fields(void)
3787 if (trace_define_generic_fields())
3788 pr_warn("tracing: Failed to allocated generic fields");
3790 if (trace_define_common_fields())
3791 pr_warn("tracing: Failed to allocate common fields");
3796 __init int event_trace_init(void)
3798 struct trace_array *tr;
3799 struct dentry *entry;
3802 tr = top_trace_array();
3806 entry = tracefs_create_file("available_events", TRACE_MODE_READ,
3807 NULL, tr, &ftrace_avail_fops);
3809 pr_warn("Could not create tracefs 'available_events' entry\n");
3811 ret = early_event_add_tracer(NULL, tr);
3815 #ifdef CONFIG_MODULES
3816 ret = register_module_notifier(&trace_module_nb);
3818 pr_warn("Failed to register trace events module notifier\n");
3821 eventdir_initialized = true;
3826 void __init trace_event_init(void)
3828 event_trace_memsetup();
3829 init_ftrace_syscalls();
3830 event_trace_enable();
3831 event_trace_init_fields();
3834 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3836 static DEFINE_SPINLOCK(test_spinlock);
3837 static DEFINE_SPINLOCK(test_spinlock_irq);
3838 static DEFINE_MUTEX(test_mutex);
3840 static __init void test_work(struct work_struct *dummy)
3842 spin_lock(&test_spinlock);
3843 spin_lock_irq(&test_spinlock_irq);
3845 spin_unlock_irq(&test_spinlock_irq);
3846 spin_unlock(&test_spinlock);
3848 mutex_lock(&test_mutex);
3850 mutex_unlock(&test_mutex);
3853 static __init int event_test_thread(void *unused)
3857 test_malloc = kmalloc(1234, GFP_KERNEL);
3859 pr_info("failed to kmalloc\n");
3861 schedule_on_each_cpu(test_work);
3865 set_current_state(TASK_INTERRUPTIBLE);
3866 while (!kthread_should_stop()) {
3868 set_current_state(TASK_INTERRUPTIBLE);
3870 __set_current_state(TASK_RUNNING);
3876 * Do various things that may trigger events.
3878 static __init void event_test_stuff(void)
3880 struct task_struct *test_thread;
3882 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3884 kthread_stop(test_thread);
3888 * For every trace event defined, we will test each trace point separately,
3889 * and then by groups, and finally all trace points.
3891 static __init void event_trace_self_tests(void)
3893 struct trace_subsystem_dir *dir;
3894 struct trace_event_file *file;
3895 struct trace_event_call *call;
3896 struct event_subsystem *system;
3897 struct trace_array *tr;
3900 tr = top_trace_array();
3904 pr_info("Running tests on trace events:\n");
3906 list_for_each_entry(file, &tr->events, list) {
3908 call = file->event_call;
3910 /* Only test those that have a probe */
3911 if (!call->class || !call->class->probe)
3915 * Testing syscall events here is pretty useless, but
3916 * we still do it if configured. But this is time consuming.
3917 * What we really need is a user thread to perform the
3918 * syscalls as we test.
3920 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3921 if (call->class->system &&
3922 strcmp(call->class->system, "syscalls") == 0)
3926 pr_info("Testing event %s: ", trace_event_name(call));
3929 * If an event is already enabled, someone is using
3930 * it and the self test should not be on.
3932 if (file->flags & EVENT_FILE_FL_ENABLED) {
3933 pr_warn("Enabled event during self test!\n");
3938 ftrace_event_enable_disable(file, 1);
3940 ftrace_event_enable_disable(file, 0);
3945 /* Now test at the sub system level */
3947 pr_info("Running tests on trace event systems:\n");
3949 list_for_each_entry(dir, &tr->systems, list) {
3951 system = dir->subsystem;
3953 /* the ftrace system is special, skip it */
3954 if (strcmp(system->name, "ftrace") == 0)
3957 pr_info("Testing event system %s: ", system->name);
3959 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3960 if (WARN_ON_ONCE(ret)) {
3961 pr_warn("error enabling system %s\n",
3968 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3969 if (WARN_ON_ONCE(ret)) {
3970 pr_warn("error disabling system %s\n",
3978 /* Test with all events enabled */
3980 pr_info("Running tests on all trace events:\n");
3981 pr_info("Testing all events: ");
3983 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3984 if (WARN_ON_ONCE(ret)) {
3985 pr_warn("error enabling all events\n");
3992 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3993 if (WARN_ON_ONCE(ret)) {
3994 pr_warn("error disabling all events\n");
4001 #ifdef CONFIG_FUNCTION_TRACER
4003 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
4005 static struct trace_event_file event_trace_file __initdata;
4008 function_test_events_call(unsigned long ip, unsigned long parent_ip,
4009 struct ftrace_ops *op, struct ftrace_regs *regs)
4011 struct trace_buffer *buffer;
4012 struct ring_buffer_event *event;
4013 struct ftrace_entry *entry;
4014 unsigned int trace_ctx;
4018 trace_ctx = tracing_gen_ctx();
4019 preempt_disable_notrace();
4020 cpu = raw_smp_processor_id();
4021 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
4026 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
4027 TRACE_FN, sizeof(*entry),
4031 entry = ring_buffer_event_data(event);
4033 entry->parent_ip = parent_ip;
4035 event_trigger_unlock_commit(&event_trace_file, buffer, event,
4038 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
4039 preempt_enable_notrace();
4042 static struct ftrace_ops trace_ops __initdata =
4044 .func = function_test_events_call,
4047 static __init void event_trace_self_test_with_function(void)
4051 event_trace_file.tr = top_trace_array();
4052 if (WARN_ON(!event_trace_file.tr))
4055 ret = register_ftrace_function(&trace_ops);
4056 if (WARN_ON(ret < 0)) {
4057 pr_info("Failed to enable function tracer for event tests\n");
4060 pr_info("Running tests again, along with the function tracer\n");
4061 event_trace_self_tests();
4062 unregister_ftrace_function(&trace_ops);
4065 static __init void event_trace_self_test_with_function(void)
4070 static __init int event_trace_self_tests_init(void)
4072 if (!tracing_selftest_disabled) {
4073 event_trace_self_tests();
4074 event_trace_self_test_with_function();
4080 late_initcall(event_trace_self_tests_init);