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 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
45 static struct kmem_cache *field_cachep;
46 static struct kmem_cache *file_cachep;
48 static inline int system_refcount(struct event_subsystem *system)
50 return system->ref_count;
53 static int system_refcount_inc(struct event_subsystem *system)
55 return system->ref_count++;
58 static int system_refcount_dec(struct event_subsystem *system)
60 return --system->ref_count;
63 /* Double loops, do not use break, only goto's work */
64 #define do_for_each_event_file(tr, file) \
65 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
66 list_for_each_entry(file, &tr->events, list)
68 #define do_for_each_event_file_safe(tr, file) \
69 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
70 struct trace_event_file *___n; \
71 list_for_each_entry_safe(file, ___n, &tr->events, list)
73 #define while_for_each_event_file() \
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
79 struct ftrace_event_field *field;
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
89 struct ftrace_event_field *
90 trace_find_event_field(struct trace_event_call *call, char *name)
92 struct ftrace_event_field *field;
93 struct list_head *head;
95 head = trace_get_fields(call);
96 field = __find_event_field(head, name);
100 field = __find_event_field(&ftrace_generic_fields, name);
104 return __find_event_field(&ftrace_common_fields, name);
107 static int __trace_define_field(struct list_head *head, const char *type,
108 const char *name, int offset, int size,
109 int is_signed, int filter_type)
111 struct ftrace_event_field *field;
113 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
120 if (filter_type == FILTER_OTHER)
121 field->filter_type = filter_assign_type(type);
123 field->filter_type = filter_type;
125 field->offset = offset;
127 field->is_signed = is_signed;
129 list_add(&field->link, head);
134 int trace_define_field(struct trace_event_call *call, const char *type,
135 const char *name, int offset, int size, int is_signed,
138 struct list_head *head;
140 if (WARN_ON(!call->class))
143 head = trace_get_fields(call);
144 return __trace_define_field(head, type, name, offset, size,
145 is_signed, filter_type);
147 EXPORT_SYMBOL_GPL(trace_define_field);
149 #define __generic_field(type, item, filter_type) \
150 ret = __trace_define_field(&ftrace_generic_fields, #type, \
151 #item, 0, 0, is_signed_type(type), \
156 #define __common_field(type, item) \
157 ret = __trace_define_field(&ftrace_common_fields, #type, \
159 offsetof(typeof(ent), item), \
161 is_signed_type(type), FILTER_OTHER); \
165 static int trace_define_generic_fields(void)
169 __generic_field(int, CPU, FILTER_CPU);
170 __generic_field(int, cpu, FILTER_CPU);
171 __generic_field(int, common_cpu, FILTER_CPU);
172 __generic_field(char *, COMM, FILTER_COMM);
173 __generic_field(char *, comm, FILTER_COMM);
178 static int trace_define_common_fields(void)
181 struct trace_entry ent;
183 __common_field(unsigned short, type);
184 __common_field(unsigned char, flags);
185 __common_field(unsigned char, preempt_count);
186 __common_field(int, pid);
191 static void trace_destroy_fields(struct trace_event_call *call)
193 struct ftrace_event_field *field, *next;
194 struct list_head *head;
196 head = trace_get_fields(call);
197 list_for_each_entry_safe(field, next, head, link) {
198 list_del(&field->link);
199 kmem_cache_free(field_cachep, field);
204 * run-time version of trace_event_get_offsets_<call>() that returns the last
205 * accessible offset of trace fields excluding __dynamic_array bytes
207 int trace_event_get_offsets(struct trace_event_call *call)
209 struct ftrace_event_field *tail;
210 struct list_head *head;
212 head = trace_get_fields(call);
214 * head->next points to the last field with the largest offset,
215 * since it was added last by trace_define_field()
217 tail = list_first_entry(head, struct ftrace_event_field, link);
218 return tail->offset + tail->size;
221 int trace_event_raw_init(struct trace_event_call *call)
225 id = register_trace_event(&call->event);
231 EXPORT_SYMBOL_GPL(trace_event_raw_init);
233 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
235 struct trace_array *tr = trace_file->tr;
236 struct trace_array_cpu *data;
237 struct trace_pid_list *no_pid_list;
238 struct trace_pid_list *pid_list;
240 pid_list = rcu_dereference_raw(tr->filtered_pids);
241 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
243 if (!pid_list && !no_pid_list)
246 data = this_cpu_ptr(tr->array_buffer.data);
248 return data->ignore_pid;
250 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
252 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
253 struct trace_event_file *trace_file,
256 struct trace_event_call *event_call = trace_file->event_call;
258 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
259 trace_event_ignore_this_pid(trace_file))
262 local_save_flags(fbuffer->flags);
263 fbuffer->pc = preempt_count();
265 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
266 * preemption (adding one to the preempt_count). Since we are
267 * interested in the preempt_count at the time the tracepoint was
268 * hit, we need to subtract one to offset the increment.
270 if (IS_ENABLED(CONFIG_PREEMPTION))
272 fbuffer->trace_file = trace_file;
275 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
276 event_call->event.type, len,
277 fbuffer->flags, fbuffer->pc);
281 fbuffer->regs = NULL;
282 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
283 return fbuffer->entry;
285 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
287 int trace_event_reg(struct trace_event_call *call,
288 enum trace_reg type, void *data)
290 struct trace_event_file *file = data;
292 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
294 case TRACE_REG_REGISTER:
295 return tracepoint_probe_register(call->tp,
298 case TRACE_REG_UNREGISTER:
299 tracepoint_probe_unregister(call->tp,
304 #ifdef CONFIG_PERF_EVENTS
305 case TRACE_REG_PERF_REGISTER:
306 return tracepoint_probe_register(call->tp,
307 call->class->perf_probe,
309 case TRACE_REG_PERF_UNREGISTER:
310 tracepoint_probe_unregister(call->tp,
311 call->class->perf_probe,
314 case TRACE_REG_PERF_OPEN:
315 case TRACE_REG_PERF_CLOSE:
316 case TRACE_REG_PERF_ADD:
317 case TRACE_REG_PERF_DEL:
323 EXPORT_SYMBOL_GPL(trace_event_reg);
325 void trace_event_enable_cmd_record(bool enable)
327 struct trace_event_file *file;
328 struct trace_array *tr;
330 lockdep_assert_held(&event_mutex);
332 do_for_each_event_file(tr, file) {
334 if (!(file->flags & EVENT_FILE_FL_ENABLED))
338 tracing_start_cmdline_record();
339 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
341 tracing_stop_cmdline_record();
342 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
344 } while_for_each_event_file();
347 void trace_event_enable_tgid_record(bool enable)
349 struct trace_event_file *file;
350 struct trace_array *tr;
352 lockdep_assert_held(&event_mutex);
354 do_for_each_event_file(tr, file) {
355 if (!(file->flags & EVENT_FILE_FL_ENABLED))
359 tracing_start_tgid_record();
360 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
362 tracing_stop_tgid_record();
363 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
366 } while_for_each_event_file();
369 static int __ftrace_event_enable_disable(struct trace_event_file *file,
370 int enable, int soft_disable)
372 struct trace_event_call *call = file->event_call;
373 struct trace_array *tr = file->tr;
380 * When soft_disable is set and enable is cleared, the sm_ref
381 * reference counter is decremented. If it reaches 0, we want
382 * to clear the SOFT_DISABLED flag but leave the event in the
383 * state that it was. That is, if the event was enabled and
384 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
385 * is set we do not want the event to be enabled before we
388 * When soft_disable is not set but the SOFT_MODE flag is,
389 * we do nothing. Do not disable the tracepoint, otherwise
390 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
393 if (atomic_dec_return(&file->sm_ref) > 0)
395 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
396 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
397 /* Disable use of trace_buffered_event */
398 trace_buffered_event_disable();
400 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
402 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
403 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
404 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
405 tracing_stop_cmdline_record();
406 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
409 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
410 tracing_stop_tgid_record();
411 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
414 call->class->reg(call, TRACE_REG_UNREGISTER, file);
416 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
417 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
418 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
420 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
424 * When soft_disable is set and enable is set, we want to
425 * register the tracepoint for the event, but leave the event
426 * as is. That means, if the event was already enabled, we do
427 * nothing (but set SOFT_MODE). If the event is disabled, we
428 * set SOFT_DISABLED before enabling the event tracepoint, so
429 * it still seems to be disabled.
432 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
434 if (atomic_inc_return(&file->sm_ref) > 1)
436 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
437 /* Enable use of trace_buffered_event */
438 trace_buffered_event_enable();
441 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
442 bool cmd = false, tgid = false;
444 /* Keep the event disabled, when going to SOFT_MODE. */
446 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
448 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
450 tracing_start_cmdline_record();
451 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
454 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
456 tracing_start_tgid_record();
457 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
460 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
463 tracing_stop_cmdline_record();
465 tracing_stop_tgid_record();
466 pr_info("event trace: Could not enable event "
467 "%s\n", trace_event_name(call));
470 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
472 /* WAS_ENABLED gets set but never cleared. */
473 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
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);
513 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
514 trace_filter_add_remove_task(pid_list, NULL, task);
518 event_filter_pid_sched_process_fork(void *data,
519 struct task_struct *self,
520 struct task_struct *task)
522 struct trace_pid_list *pid_list;
523 struct trace_array *tr = data;
525 pid_list = rcu_dereference_sched(tr->filtered_pids);
526 trace_filter_add_remove_task(pid_list, self, task);
528 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
529 trace_filter_add_remove_task(pid_list, self, task);
532 void trace_event_follow_fork(struct trace_array *tr, bool enable)
535 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
537 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
540 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
542 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
548 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
549 struct task_struct *prev, struct task_struct *next)
551 struct trace_array *tr = data;
552 struct trace_pid_list *no_pid_list;
553 struct trace_pid_list *pid_list;
556 pid_list = rcu_dereference_sched(tr->filtered_pids);
557 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
560 * Sched switch is funny, as we only want to ignore it
561 * in the notrace case if both prev and next should be ignored.
563 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
564 trace_ignore_this_task(NULL, no_pid_list, next);
566 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
567 (trace_ignore_this_task(pid_list, NULL, prev) &&
568 trace_ignore_this_task(pid_list, NULL, next)));
572 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
573 struct task_struct *prev, struct task_struct *next)
575 struct trace_array *tr = data;
576 struct trace_pid_list *no_pid_list;
577 struct trace_pid_list *pid_list;
579 pid_list = rcu_dereference_sched(tr->filtered_pids);
580 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
582 this_cpu_write(tr->array_buffer.data->ignore_pid,
583 trace_ignore_this_task(pid_list, no_pid_list, next));
587 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
589 struct trace_array *tr = data;
590 struct trace_pid_list *no_pid_list;
591 struct trace_pid_list *pid_list;
593 /* Nothing to do if we are already tracing */
594 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
597 pid_list = rcu_dereference_sched(tr->filtered_pids);
598 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
600 this_cpu_write(tr->array_buffer.data->ignore_pid,
601 trace_ignore_this_task(pid_list, no_pid_list, task));
605 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
607 struct trace_array *tr = data;
608 struct trace_pid_list *no_pid_list;
609 struct trace_pid_list *pid_list;
611 /* Nothing to do if we are not tracing */
612 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
615 pid_list = rcu_dereference_sched(tr->filtered_pids);
616 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
618 /* Set tracing if current is enabled */
619 this_cpu_write(tr->array_buffer.data->ignore_pid,
620 trace_ignore_this_task(pid_list, no_pid_list, current));
623 static void unregister_pid_events(struct trace_array *tr)
625 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
626 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
628 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
629 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
631 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
632 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
634 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
635 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
638 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
640 struct trace_pid_list *pid_list;
641 struct trace_pid_list *no_pid_list;
642 struct trace_event_file *file;
645 pid_list = rcu_dereference_protected(tr->filtered_pids,
646 lockdep_is_held(&event_mutex));
647 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
648 lockdep_is_held(&event_mutex));
650 /* Make sure there's something to do */
651 if (!pid_type_enabled(type, pid_list, no_pid_list))
654 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
655 unregister_pid_events(tr);
657 list_for_each_entry(file, &tr->events, list) {
658 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
661 for_each_possible_cpu(cpu)
662 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
665 if (type & TRACE_PIDS)
666 rcu_assign_pointer(tr->filtered_pids, NULL);
668 if (type & TRACE_NO_PIDS)
669 rcu_assign_pointer(tr->filtered_no_pids, NULL);
671 /* Wait till all users are no longer using pid filtering */
672 tracepoint_synchronize_unregister();
674 if ((type & TRACE_PIDS) && pid_list)
675 trace_free_pid_list(pid_list);
677 if ((type & TRACE_NO_PIDS) && no_pid_list)
678 trace_free_pid_list(no_pid_list);
681 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
683 mutex_lock(&event_mutex);
684 __ftrace_clear_event_pids(tr, type);
685 mutex_unlock(&event_mutex);
688 static void __put_system(struct event_subsystem *system)
690 struct event_filter *filter = system->filter;
692 WARN_ON_ONCE(system_refcount(system) == 0);
693 if (system_refcount_dec(system))
696 list_del(&system->list);
699 kfree(filter->filter_string);
702 kfree_const(system->name);
706 static void __get_system(struct event_subsystem *system)
708 WARN_ON_ONCE(system_refcount(system) == 0);
709 system_refcount_inc(system);
712 static void __get_system_dir(struct trace_subsystem_dir *dir)
714 WARN_ON_ONCE(dir->ref_count == 0);
716 __get_system(dir->subsystem);
719 static void __put_system_dir(struct trace_subsystem_dir *dir)
721 WARN_ON_ONCE(dir->ref_count == 0);
722 /* If the subsystem is about to be freed, the dir must be too */
723 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
725 __put_system(dir->subsystem);
726 if (!--dir->ref_count)
730 static void put_system(struct trace_subsystem_dir *dir)
732 mutex_lock(&event_mutex);
733 __put_system_dir(dir);
734 mutex_unlock(&event_mutex);
737 static void remove_subsystem(struct trace_subsystem_dir *dir)
742 if (!--dir->nr_events) {
743 tracefs_remove(dir->entry);
744 list_del(&dir->list);
745 __put_system_dir(dir);
749 void event_file_get(struct trace_event_file *file)
751 atomic_inc(&file->ref);
754 void event_file_put(struct trace_event_file *file)
756 if (WARN_ON_ONCE(!atomic_read(&file->ref))) {
757 if (file->flags & EVENT_FILE_FL_FREED)
758 kmem_cache_free(file_cachep, file);
762 if (atomic_dec_and_test(&file->ref)) {
763 /* Count should only go to zero when it is freed */
764 if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
766 kmem_cache_free(file_cachep, file);
770 static void remove_event_file_dir(struct trace_event_file *file)
772 struct dentry *dir = file->dir;
776 list_del(&file->list);
777 remove_subsystem(file->system);
778 free_event_filter(file->filter);
779 file->flags |= EVENT_FILE_FL_FREED;
780 event_file_put(file);
784 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
787 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
788 const char *sub, const char *event, int set)
790 struct trace_event_file *file;
791 struct trace_event_call *call;
796 list_for_each_entry(file, &tr->events, list) {
798 call = file->event_call;
799 name = trace_event_name(call);
801 if (!name || !call->class || !call->class->reg)
804 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
808 strcmp(match, name) != 0 &&
809 strcmp(match, call->class->system) != 0)
812 if (sub && strcmp(sub, call->class->system) != 0)
815 if (event && strcmp(event, name) != 0)
818 ret = ftrace_event_enable_disable(file, set);
821 * Save the first error and return that. Some events
822 * may still have been enabled, but let the user
823 * know that something went wrong.
834 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
835 const char *sub, const char *event, int set)
839 mutex_lock(&event_mutex);
840 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
841 mutex_unlock(&event_mutex);
846 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
848 char *event = NULL, *sub = NULL, *match;
854 * The buf format can be <subsystem>:<event-name>
855 * *:<event-name> means any event by that name.
856 * :<event-name> is the same.
858 * <subsystem>:* means all events in that subsystem
859 * <subsystem>: means the same.
861 * <name> (no ':') means all events in a subsystem with
862 * the name <name> or any event that matches <name>
865 match = strsep(&buf, ":");
871 if (!strlen(sub) || strcmp(sub, "*") == 0)
873 if (!strlen(event) || strcmp(event, "*") == 0)
877 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
879 /* Put back the colon to allow this to be called again */
887 * trace_set_clr_event - enable or disable an event
888 * @system: system name to match (NULL for any system)
889 * @event: event name to match (NULL for all events, within system)
890 * @set: 1 to enable, 0 to disable
892 * This is a way for other parts of the kernel to enable or disable
895 * Returns 0 on success, -EINVAL if the parameters do not match any
898 int trace_set_clr_event(const char *system, const char *event, int set)
900 struct trace_array *tr = top_trace_array();
905 return __ftrace_set_clr_event(tr, NULL, system, event, set);
907 EXPORT_SYMBOL_GPL(trace_set_clr_event);
910 * trace_array_set_clr_event - enable or disable an event for a trace array.
911 * @tr: concerned trace array.
912 * @system: system name to match (NULL for any system)
913 * @event: event name to match (NULL for all events, within system)
914 * @enable: true to enable, false to disable
916 * This is a way for other parts of the kernel to enable or disable
919 * Returns 0 on success, -EINVAL if the parameters do not match any
922 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
923 const char *event, bool enable)
930 set = (enable == true) ? 1 : 0;
931 return __ftrace_set_clr_event(tr, NULL, system, event, set);
933 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
935 /* 128 should be much more than enough */
936 #define EVENT_BUF_SIZE 127
939 ftrace_event_write(struct file *file, const char __user *ubuf,
940 size_t cnt, loff_t *ppos)
942 struct trace_parser parser;
943 struct seq_file *m = file->private_data;
944 struct trace_array *tr = m->private;
950 ret = tracing_update_buffers();
954 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
957 read = trace_get_user(&parser, ubuf, cnt, ppos);
959 if (read >= 0 && trace_parser_loaded((&parser))) {
962 if (*parser.buffer == '!')
965 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
973 trace_parser_put(&parser);
979 t_next(struct seq_file *m, void *v, loff_t *pos)
981 struct trace_event_file *file = v;
982 struct trace_event_call *call;
983 struct trace_array *tr = m->private;
987 list_for_each_entry_continue(file, &tr->events, list) {
988 call = file->event_call;
990 * The ftrace subsystem is for showing formats only.
991 * They can not be enabled or disabled via the event files.
993 if (call->class && call->class->reg &&
994 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1001 static void *t_start(struct seq_file *m, loff_t *pos)
1003 struct trace_event_file *file;
1004 struct trace_array *tr = m->private;
1007 mutex_lock(&event_mutex);
1009 file = list_entry(&tr->events, struct trace_event_file, list);
1010 for (l = 0; l <= *pos; ) {
1011 file = t_next(m, file, &l);
1019 s_next(struct seq_file *m, void *v, loff_t *pos)
1021 struct trace_event_file *file = v;
1022 struct trace_array *tr = m->private;
1026 list_for_each_entry_continue(file, &tr->events, list) {
1027 if (file->flags & EVENT_FILE_FL_ENABLED)
1034 static void *s_start(struct seq_file *m, loff_t *pos)
1036 struct trace_event_file *file;
1037 struct trace_array *tr = m->private;
1040 mutex_lock(&event_mutex);
1042 file = list_entry(&tr->events, struct trace_event_file, list);
1043 for (l = 0; l <= *pos; ) {
1044 file = s_next(m, file, &l);
1051 static int t_show(struct seq_file *m, void *v)
1053 struct trace_event_file *file = v;
1054 struct trace_event_call *call = file->event_call;
1056 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1057 seq_printf(m, "%s:", call->class->system);
1058 seq_printf(m, "%s\n", trace_event_name(call));
1063 static void t_stop(struct seq_file *m, void *p)
1065 mutex_unlock(&event_mutex);
1069 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1071 struct trace_array *tr = m->private;
1072 struct trace_pid_list *pid_list;
1074 if (type == TRACE_PIDS)
1075 pid_list = rcu_dereference_sched(tr->filtered_pids);
1077 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1079 return trace_pid_next(pid_list, v, pos);
1083 p_next(struct seq_file *m, void *v, loff_t *pos)
1085 return __next(m, v, pos, TRACE_PIDS);
1089 np_next(struct seq_file *m, void *v, loff_t *pos)
1091 return __next(m, v, pos, TRACE_NO_PIDS);
1094 static void *__start(struct seq_file *m, loff_t *pos, int type)
1097 struct trace_pid_list *pid_list;
1098 struct trace_array *tr = m->private;
1101 * Grab the mutex, to keep calls to p_next() having the same
1102 * tr->filtered_pids as p_start() has.
1103 * If we just passed the tr->filtered_pids around, then RCU would
1104 * have been enough, but doing that makes things more complex.
1106 mutex_lock(&event_mutex);
1107 rcu_read_lock_sched();
1109 if (type == TRACE_PIDS)
1110 pid_list = rcu_dereference_sched(tr->filtered_pids);
1112 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1117 return trace_pid_start(pid_list, pos);
1120 static void *p_start(struct seq_file *m, loff_t *pos)
1123 return __start(m, pos, TRACE_PIDS);
1126 static void *np_start(struct seq_file *m, loff_t *pos)
1129 return __start(m, pos, TRACE_NO_PIDS);
1132 static void p_stop(struct seq_file *m, void *p)
1135 rcu_read_unlock_sched();
1136 mutex_unlock(&event_mutex);
1140 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1143 struct trace_event_file *file;
1144 unsigned long flags;
1147 mutex_lock(&event_mutex);
1148 file = event_file_data(filp);
1150 flags = file->flags;
1151 mutex_unlock(&event_mutex);
1153 if (!file || flags & EVENT_FILE_FL_FREED)
1156 if (flags & EVENT_FILE_FL_ENABLED &&
1157 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1160 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1161 flags & EVENT_FILE_FL_SOFT_MODE)
1166 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1170 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1173 struct trace_event_file *file;
1177 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1181 ret = tracing_update_buffers();
1189 mutex_lock(&event_mutex);
1190 file = event_file_data(filp);
1191 if (likely(file && !(file->flags & EVENT_FILE_FL_FREED)))
1192 ret = ftrace_event_enable_disable(file, val);
1193 mutex_unlock(&event_mutex);
1202 return ret ? ret : cnt;
1206 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1209 const char set_to_char[4] = { '?', '0', '1', 'X' };
1210 struct trace_subsystem_dir *dir = filp->private_data;
1211 struct event_subsystem *system = dir->subsystem;
1212 struct trace_event_call *call;
1213 struct trace_event_file *file;
1214 struct trace_array *tr = dir->tr;
1219 mutex_lock(&event_mutex);
1220 list_for_each_entry(file, &tr->events, list) {
1221 call = file->event_call;
1222 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1223 !trace_event_name(call) || !call->class || !call->class->reg)
1226 if (system && strcmp(call->class->system, system->name) != 0)
1230 * We need to find out if all the events are set
1231 * or if all events or cleared, or if we have
1234 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1237 * If we have a mixture, no need to look further.
1242 mutex_unlock(&event_mutex);
1244 buf[0] = set_to_char[set];
1247 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1253 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1256 struct trace_subsystem_dir *dir = filp->private_data;
1257 struct event_subsystem *system = dir->subsystem;
1258 const char *name = NULL;
1262 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1266 ret = tracing_update_buffers();
1270 if (val != 0 && val != 1)
1274 * Opening of "enable" adds a ref count to system,
1275 * so the name is safe to use.
1278 name = system->name;
1280 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1294 FORMAT_FIELD_SEPERATOR = 2,
1295 FORMAT_PRINTFMT = 3,
1298 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1300 struct trace_event_call *call = event_file_data(m->private);
1301 struct list_head *common_head = &ftrace_common_fields;
1302 struct list_head *head = trace_get_fields(call);
1303 struct list_head *node = v;
1307 switch ((unsigned long)v) {
1312 case FORMAT_FIELD_SEPERATOR:
1316 case FORMAT_PRINTFMT:
1322 if (node == common_head)
1323 return (void *)FORMAT_FIELD_SEPERATOR;
1324 else if (node == head)
1325 return (void *)FORMAT_PRINTFMT;
1330 static int f_show(struct seq_file *m, void *v)
1332 struct trace_event_call *call = event_file_data(m->private);
1333 struct ftrace_event_field *field;
1334 const char *array_descriptor;
1336 switch ((unsigned long)v) {
1338 seq_printf(m, "name: %s\n", trace_event_name(call));
1339 seq_printf(m, "ID: %d\n", call->event.type);
1340 seq_puts(m, "format:\n");
1343 case FORMAT_FIELD_SEPERATOR:
1347 case FORMAT_PRINTFMT:
1348 seq_printf(m, "\nprint fmt: %s\n",
1353 field = list_entry(v, struct ftrace_event_field, link);
1355 * Smartly shows the array type(except dynamic array).
1358 * If TYPE := TYPE[LEN], it is shown:
1359 * field:TYPE VAR[LEN]
1361 array_descriptor = strchr(field->type, '[');
1363 if (str_has_prefix(field->type, "__data_loc"))
1364 array_descriptor = NULL;
1366 if (!array_descriptor)
1367 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1368 field->type, field->name, field->offset,
1369 field->size, !!field->is_signed);
1371 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1372 (int)(array_descriptor - field->type),
1373 field->type, field->name,
1374 array_descriptor, field->offset,
1375 field->size, !!field->is_signed);
1380 static void *f_start(struct seq_file *m, loff_t *pos)
1382 void *p = (void *)FORMAT_HEADER;
1385 /* ->stop() is called even if ->start() fails */
1386 mutex_lock(&event_mutex);
1387 if (!event_file_data(m->private))
1388 return ERR_PTR(-ENODEV);
1390 while (l < *pos && p)
1391 p = f_next(m, p, &l);
1396 static void f_stop(struct seq_file *m, void *p)
1398 mutex_unlock(&event_mutex);
1401 static const struct seq_operations trace_format_seq_ops = {
1408 static int trace_format_open(struct inode *inode, struct file *file)
1413 /* Do we want to hide event format files on tracefs lockdown? */
1415 ret = seq_open(file, &trace_format_seq_ops);
1419 m = file->private_data;
1426 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1428 int id = (long)event_file_data(filp);
1435 len = sprintf(buf, "%d\n", id);
1437 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1441 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1444 struct trace_event_file *file;
1445 struct trace_seq *s;
1451 s = kmalloc(sizeof(*s), GFP_KERNEL);
1458 mutex_lock(&event_mutex);
1459 file = event_file_data(filp);
1460 if (file && !(file->flags & EVENT_FILE_FL_FREED))
1461 print_event_filter(file, s);
1462 mutex_unlock(&event_mutex);
1465 r = simple_read_from_buffer(ubuf, cnt, ppos,
1466 s->buffer, trace_seq_used(s));
1474 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1477 struct trace_event_file *file;
1481 if (cnt >= PAGE_SIZE)
1484 buf = memdup_user_nul(ubuf, cnt);
1486 return PTR_ERR(buf);
1488 mutex_lock(&event_mutex);
1489 file = event_file_data(filp);
1491 err = apply_event_filter(file, buf);
1492 mutex_unlock(&event_mutex);
1503 static LIST_HEAD(event_subsystems);
1505 static int subsystem_open(struct inode *inode, struct file *filp)
1507 struct event_subsystem *system = NULL;
1508 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1509 struct trace_array *tr;
1512 if (tracing_is_disabled())
1515 /* Make sure the system still exists */
1516 mutex_lock(&event_mutex);
1517 mutex_lock(&trace_types_lock);
1518 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1519 list_for_each_entry(dir, &tr->systems, list) {
1520 if (dir == inode->i_private) {
1521 /* Don't open systems with no events */
1522 if (dir->nr_events) {
1523 __get_system_dir(dir);
1524 system = dir->subsystem;
1531 mutex_unlock(&trace_types_lock);
1532 mutex_unlock(&event_mutex);
1537 /* Some versions of gcc think dir can be uninitialized here */
1540 /* Still need to increment the ref count of the system */
1541 if (trace_array_get(tr) < 0) {
1546 ret = tracing_open_generic(inode, filp);
1548 trace_array_put(tr);
1555 static int system_tr_open(struct inode *inode, struct file *filp)
1557 struct trace_subsystem_dir *dir;
1558 struct trace_array *tr = inode->i_private;
1561 /* Make a temporary dir that has no system but points to tr */
1562 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1566 ret = tracing_open_generic_tr(inode, filp);
1572 filp->private_data = dir;
1577 static int subsystem_release(struct inode *inode, struct file *file)
1579 struct trace_subsystem_dir *dir = file->private_data;
1581 trace_array_put(dir->tr);
1584 * If dir->subsystem is NULL, then this is a temporary
1585 * descriptor that was made for a trace_array to enable
1597 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1600 struct trace_subsystem_dir *dir = filp->private_data;
1601 struct event_subsystem *system = dir->subsystem;
1602 struct trace_seq *s;
1608 s = kmalloc(sizeof(*s), GFP_KERNEL);
1614 print_subsystem_event_filter(system, s);
1615 r = simple_read_from_buffer(ubuf, cnt, ppos,
1616 s->buffer, trace_seq_used(s));
1624 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1627 struct trace_subsystem_dir *dir = filp->private_data;
1631 if (cnt >= PAGE_SIZE)
1634 buf = memdup_user_nul(ubuf, cnt);
1636 return PTR_ERR(buf);
1638 err = apply_subsystem_event_filter(dir, buf);
1649 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1651 int (*func)(struct trace_seq *s) = filp->private_data;
1652 struct trace_seq *s;
1658 s = kmalloc(sizeof(*s), GFP_KERNEL);
1665 r = simple_read_from_buffer(ubuf, cnt, ppos,
1666 s->buffer, trace_seq_used(s));
1673 static void ignore_task_cpu(void *data)
1675 struct trace_array *tr = data;
1676 struct trace_pid_list *pid_list;
1677 struct trace_pid_list *no_pid_list;
1680 * This function is called by on_each_cpu() while the
1681 * event_mutex is held.
1683 pid_list = rcu_dereference_protected(tr->filtered_pids,
1684 mutex_is_locked(&event_mutex));
1685 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1686 mutex_is_locked(&event_mutex));
1688 this_cpu_write(tr->array_buffer.data->ignore_pid,
1689 trace_ignore_this_task(pid_list, no_pid_list, current));
1692 static void register_pid_events(struct trace_array *tr)
1695 * Register a probe that is called before all other probes
1696 * to set ignore_pid if next or prev do not match.
1697 * Register a probe this is called after all other probes
1698 * to only keep ignore_pid set if next pid matches.
1700 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1702 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1705 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1707 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1710 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1712 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1715 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1717 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1722 event_pid_write(struct file *filp, const char __user *ubuf,
1723 size_t cnt, loff_t *ppos, int type)
1725 struct seq_file *m = filp->private_data;
1726 struct trace_array *tr = m->private;
1727 struct trace_pid_list *filtered_pids = NULL;
1728 struct trace_pid_list *other_pids = NULL;
1729 struct trace_pid_list *pid_list;
1730 struct trace_event_file *file;
1736 ret = tracing_update_buffers();
1740 mutex_lock(&event_mutex);
1742 if (type == TRACE_PIDS) {
1743 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1744 lockdep_is_held(&event_mutex));
1745 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1746 lockdep_is_held(&event_mutex));
1748 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1749 lockdep_is_held(&event_mutex));
1750 other_pids = rcu_dereference_protected(tr->filtered_pids,
1751 lockdep_is_held(&event_mutex));
1754 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1758 if (type == TRACE_PIDS)
1759 rcu_assign_pointer(tr->filtered_pids, pid_list);
1761 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1763 list_for_each_entry(file, &tr->events, list) {
1764 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1767 if (filtered_pids) {
1768 tracepoint_synchronize_unregister();
1769 trace_free_pid_list(filtered_pids);
1770 } else if (pid_list && !other_pids) {
1771 register_pid_events(tr);
1775 * Ignoring of pids is done at task switch. But we have to
1776 * check for those tasks that are currently running.
1777 * Always do this in case a pid was appended or removed.
1779 on_each_cpu(ignore_task_cpu, tr, 1);
1782 mutex_unlock(&event_mutex);
1791 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1792 size_t cnt, loff_t *ppos)
1794 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
1798 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
1799 size_t cnt, loff_t *ppos)
1801 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
1804 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1805 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1806 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1807 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
1808 static int ftrace_event_release(struct inode *inode, struct file *file);
1810 static const struct seq_operations show_event_seq_ops = {
1817 static const struct seq_operations show_set_event_seq_ops = {
1824 static const struct seq_operations show_set_pid_seq_ops = {
1827 .show = trace_pid_show,
1831 static const struct seq_operations show_set_no_pid_seq_ops = {
1834 .show = trace_pid_show,
1838 static const struct file_operations ftrace_avail_fops = {
1839 .open = ftrace_event_avail_open,
1841 .llseek = seq_lseek,
1842 .release = seq_release,
1845 static const struct file_operations ftrace_set_event_fops = {
1846 .open = ftrace_event_set_open,
1848 .write = ftrace_event_write,
1849 .llseek = seq_lseek,
1850 .release = ftrace_event_release,
1853 static const struct file_operations ftrace_set_event_pid_fops = {
1854 .open = ftrace_event_set_pid_open,
1856 .write = ftrace_event_pid_write,
1857 .llseek = seq_lseek,
1858 .release = ftrace_event_release,
1861 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
1862 .open = ftrace_event_set_npid_open,
1864 .write = ftrace_event_npid_write,
1865 .llseek = seq_lseek,
1866 .release = ftrace_event_release,
1869 static const struct file_operations ftrace_enable_fops = {
1870 .open = tracing_open_file_tr,
1871 .read = event_enable_read,
1872 .write = event_enable_write,
1873 .release = tracing_release_file_tr,
1874 .llseek = default_llseek,
1877 static const struct file_operations ftrace_event_format_fops = {
1878 .open = trace_format_open,
1880 .llseek = seq_lseek,
1881 .release = seq_release,
1884 static const struct file_operations ftrace_event_id_fops = {
1885 .read = event_id_read,
1886 .llseek = default_llseek,
1889 static const struct file_operations ftrace_event_filter_fops = {
1890 .open = tracing_open_file_tr,
1891 .read = event_filter_read,
1892 .write = event_filter_write,
1893 .release = tracing_release_file_tr,
1894 .llseek = default_llseek,
1897 static const struct file_operations ftrace_subsystem_filter_fops = {
1898 .open = subsystem_open,
1899 .read = subsystem_filter_read,
1900 .write = subsystem_filter_write,
1901 .llseek = default_llseek,
1902 .release = subsystem_release,
1905 static const struct file_operations ftrace_system_enable_fops = {
1906 .open = subsystem_open,
1907 .read = system_enable_read,
1908 .write = system_enable_write,
1909 .llseek = default_llseek,
1910 .release = subsystem_release,
1913 static const struct file_operations ftrace_tr_enable_fops = {
1914 .open = system_tr_open,
1915 .read = system_enable_read,
1916 .write = system_enable_write,
1917 .llseek = default_llseek,
1918 .release = subsystem_release,
1921 static const struct file_operations ftrace_show_header_fops = {
1922 .open = tracing_open_generic,
1923 .read = show_header,
1924 .llseek = default_llseek,
1928 ftrace_event_open(struct inode *inode, struct file *file,
1929 const struct seq_operations *seq_ops)
1934 ret = security_locked_down(LOCKDOWN_TRACEFS);
1938 ret = seq_open(file, seq_ops);
1941 m = file->private_data;
1942 /* copy tr over to seq ops */
1943 m->private = inode->i_private;
1948 static int ftrace_event_release(struct inode *inode, struct file *file)
1950 struct trace_array *tr = inode->i_private;
1952 trace_array_put(tr);
1954 return seq_release(inode, file);
1958 ftrace_event_avail_open(struct inode *inode, struct file *file)
1960 const struct seq_operations *seq_ops = &show_event_seq_ops;
1962 /* Checks for tracefs lockdown */
1963 return ftrace_event_open(inode, file, seq_ops);
1967 ftrace_event_set_open(struct inode *inode, struct file *file)
1969 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1970 struct trace_array *tr = inode->i_private;
1973 ret = tracing_check_open_get_tr(tr);
1977 if ((file->f_mode & FMODE_WRITE) &&
1978 (file->f_flags & O_TRUNC))
1979 ftrace_clear_events(tr);
1981 ret = ftrace_event_open(inode, file, seq_ops);
1983 trace_array_put(tr);
1988 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1990 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1991 struct trace_array *tr = inode->i_private;
1994 ret = tracing_check_open_get_tr(tr);
1998 if ((file->f_mode & FMODE_WRITE) &&
1999 (file->f_flags & O_TRUNC))
2000 ftrace_clear_event_pids(tr, TRACE_PIDS);
2002 ret = ftrace_event_open(inode, file, seq_ops);
2004 trace_array_put(tr);
2009 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2011 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2012 struct trace_array *tr = inode->i_private;
2015 ret = tracing_check_open_get_tr(tr);
2019 if ((file->f_mode & FMODE_WRITE) &&
2020 (file->f_flags & O_TRUNC))
2021 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2023 ret = ftrace_event_open(inode, file, seq_ops);
2025 trace_array_put(tr);
2029 static struct event_subsystem *
2030 create_new_subsystem(const char *name)
2032 struct event_subsystem *system;
2034 /* need to create new entry */
2035 system = kmalloc(sizeof(*system), GFP_KERNEL);
2039 system->ref_count = 1;
2041 /* Only allocate if dynamic (kprobes and modules) */
2042 system->name = kstrdup_const(name, GFP_KERNEL);
2046 system->filter = NULL;
2048 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2049 if (!system->filter)
2052 list_add(&system->list, &event_subsystems);
2057 kfree_const(system->name);
2062 static struct dentry *
2063 event_subsystem_dir(struct trace_array *tr, const char *name,
2064 struct trace_event_file *file, struct dentry *parent)
2066 struct trace_subsystem_dir *dir;
2067 struct event_subsystem *system;
2068 struct dentry *entry;
2070 /* First see if we did not already create this dir */
2071 list_for_each_entry(dir, &tr->systems, list) {
2072 system = dir->subsystem;
2073 if (strcmp(system->name, name) == 0) {
2080 /* Now see if the system itself exists. */
2081 list_for_each_entry(system, &event_subsystems, list) {
2082 if (strcmp(system->name, name) == 0)
2085 /* Reset system variable when not found */
2086 if (&system->list == &event_subsystems)
2089 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2094 system = create_new_subsystem(name);
2098 __get_system(system);
2100 dir->entry = tracefs_create_dir(name, parent);
2102 pr_warn("Failed to create system directory %s\n", name);
2103 __put_system(system);
2110 dir->subsystem = system;
2113 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2114 &ftrace_subsystem_filter_fops);
2116 kfree(system->filter);
2117 system->filter = NULL;
2118 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2121 trace_create_file("enable", 0644, dir->entry, dir,
2122 &ftrace_system_enable_fops);
2124 list_add(&dir->list, &tr->systems);
2131 /* Only print this message if failed on memory allocation */
2132 if (!dir || !system)
2133 pr_warn("No memory to create event subsystem %s\n", name);
2138 event_define_fields(struct trace_event_call *call)
2140 struct list_head *head;
2144 * Other events may have the same class. Only update
2145 * the fields if they are not already defined.
2147 head = trace_get_fields(call);
2148 if (list_empty(head)) {
2149 struct trace_event_fields *field = call->class->fields_array;
2150 unsigned int offset = sizeof(struct trace_entry);
2152 for (; field->type; field++) {
2153 if (field->type == TRACE_FUNCTION_TYPE) {
2154 field->define_fields(call);
2158 offset = ALIGN(offset, field->align);
2159 ret = trace_define_field(call, field->type, field->name,
2160 offset, field->size,
2161 field->is_signed, field->filter_type);
2162 if (WARN_ON_ONCE(ret)) {
2163 pr_err("error code is %d\n", ret);
2167 offset += field->size;
2175 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2177 struct trace_event_call *call = file->event_call;
2178 struct trace_array *tr = file->tr;
2179 struct dentry *d_events;
2184 * If the trace point header did not define TRACE_SYSTEM
2185 * then the system would be called "TRACE_SYSTEM".
2187 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2188 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2194 name = trace_event_name(call);
2195 file->dir = tracefs_create_dir(name, d_events);
2197 pr_warn("Could not create tracefs '%s' directory\n", name);
2201 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2202 trace_create_file("enable", 0644, file->dir, file,
2203 &ftrace_enable_fops);
2205 #ifdef CONFIG_PERF_EVENTS
2206 if (call->event.type && call->class->reg)
2207 trace_create_file("id", 0444, file->dir,
2208 (void *)(long)call->event.type,
2209 &ftrace_event_id_fops);
2212 ret = event_define_fields(call);
2214 pr_warn("Could not initialize trace point events/%s\n", name);
2219 * Only event directories that can be enabled should have
2220 * triggers or filters.
2222 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2223 trace_create_file("filter", 0644, file->dir, file,
2224 &ftrace_event_filter_fops);
2226 trace_create_file("trigger", 0644, file->dir, file,
2227 &event_trigger_fops);
2230 #ifdef CONFIG_HIST_TRIGGERS
2231 trace_create_file("hist", 0444, file->dir, file,
2234 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2235 trace_create_file("hist_debug", 0444, file->dir, file,
2236 &event_hist_debug_fops);
2238 trace_create_file("format", 0444, file->dir, call,
2239 &ftrace_event_format_fops);
2241 #ifdef CONFIG_TRACE_EVENT_INJECT
2242 if (call->event.type && call->class->reg)
2243 trace_create_file("inject", 0200, file->dir, file,
2244 &event_inject_fops);
2250 static void remove_event_from_tracers(struct trace_event_call *call)
2252 struct trace_event_file *file;
2253 struct trace_array *tr;
2255 do_for_each_event_file_safe(tr, file) {
2256 if (file->event_call != call)
2259 remove_event_file_dir(file);
2261 * The do_for_each_event_file_safe() is
2262 * a double loop. After finding the call for this
2263 * trace_array, we use break to jump to the next
2267 } while_for_each_event_file();
2270 static void event_remove(struct trace_event_call *call)
2272 struct trace_array *tr;
2273 struct trace_event_file *file;
2275 do_for_each_event_file(tr, file) {
2276 if (file->event_call != call)
2279 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2280 tr->clear_trace = true;
2282 ftrace_event_enable_disable(file, 0);
2284 * The do_for_each_event_file() is
2285 * a double loop. After finding the call for this
2286 * trace_array, we use break to jump to the next
2290 } while_for_each_event_file();
2292 if (call->event.funcs)
2293 __unregister_trace_event(&call->event);
2294 remove_event_from_tracers(call);
2295 list_del(&call->list);
2298 static int event_init(struct trace_event_call *call)
2303 name = trace_event_name(call);
2307 if (call->class->raw_init) {
2308 ret = call->class->raw_init(call);
2309 if (ret < 0 && ret != -ENOSYS)
2310 pr_warn("Could not initialize trace events/%s\n", name);
2317 __register_event(struct trace_event_call *call, struct module *mod)
2321 ret = event_init(call);
2325 list_add(&call->list, &ftrace_events);
2331 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2336 /* Find the length of the eval value as a string */
2337 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2338 /* Make sure there's enough room to replace the string with the value */
2342 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2344 /* Get the rest of the string of ptr */
2345 rlen = strlen(ptr + len);
2346 memmove(ptr + elen, ptr + len, rlen);
2347 /* Make sure we end the new string */
2348 ptr[elen + rlen] = 0;
2353 static void update_event_printk(struct trace_event_call *call,
2354 struct trace_eval_map *map)
2358 int len = strlen(map->eval_string);
2360 for (ptr = call->print_fmt; *ptr; ptr++) {
2374 if (isdigit(*ptr)) {
2378 /* Check for alpha chars like ULL */
2379 } while (isalnum(*ptr));
2383 * A number must have some kind of delimiter after
2384 * it, and we can ignore that too.
2388 if (isalpha(*ptr) || *ptr == '_') {
2389 if (strncmp(map->eval_string, ptr, len) == 0 &&
2390 !isalnum(ptr[len]) && ptr[len] != '_') {
2391 ptr = eval_replace(ptr, map, len);
2392 /* enum/sizeof string smaller than value */
2393 if (WARN_ON_ONCE(!ptr))
2396 * No need to decrement here, as eval_replace()
2397 * returns the pointer to the character passed
2398 * the eval, and two evals can not be placed
2399 * back to back without something in between.
2400 * We can skip that something in between.
2407 } while (isalnum(*ptr) || *ptr == '_');
2411 * If what comes after this variable is a '.' or
2412 * '->' then we can continue to ignore that string.
2414 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2415 ptr += *ptr == '.' ? 1 : 2;
2421 * Once again, we can skip the delimiter that came
2429 void trace_event_eval_update(struct trace_eval_map **map, int len)
2431 struct trace_event_call *call, *p;
2432 const char *last_system = NULL;
2437 down_write(&trace_event_sem);
2438 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2439 /* events are usually grouped together with systems */
2440 if (!last_system || call->class->system != last_system) {
2443 last_system = call->class->system;
2447 * Since calls are grouped by systems, the likelyhood that the
2448 * next call in the iteration belongs to the same system as the
2449 * previous call is high. As an optimization, we skip seaching
2450 * for a map[] that matches the call's system if the last call
2451 * was from the same system. That's what last_i is for. If the
2452 * call has the same system as the previous call, then last_i
2453 * will be the index of the first map[] that has a matching
2456 for (i = last_i; i < len; i++) {
2457 if (call->class->system == map[i]->system) {
2458 /* Save the first system if need be */
2463 update_event_printk(call, map[i]);
2468 up_write(&trace_event_sem);
2471 static struct trace_event_file *
2472 trace_create_new_event(struct trace_event_call *call,
2473 struct trace_array *tr)
2475 struct trace_pid_list *no_pid_list;
2476 struct trace_pid_list *pid_list;
2477 struct trace_event_file *file;
2479 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2483 pid_list = rcu_dereference_protected(tr->filtered_pids,
2484 lockdep_is_held(&event_mutex));
2485 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2486 lockdep_is_held(&event_mutex));
2488 if (pid_list || no_pid_list)
2489 file->flags |= EVENT_FILE_FL_PID_FILTER;
2491 file->event_call = call;
2493 atomic_set(&file->sm_ref, 0);
2494 atomic_set(&file->tm_ref, 0);
2495 INIT_LIST_HEAD(&file->triggers);
2496 list_add(&file->list, &tr->events);
2497 event_file_get(file);
2502 /* Add an event to a trace directory */
2504 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2506 struct trace_event_file *file;
2508 file = trace_create_new_event(call, tr);
2512 if (eventdir_initialized)
2513 return event_create_dir(tr->event_dir, file);
2515 return event_define_fields(call);
2519 * Just create a decriptor for early init. A descriptor is required
2520 * for enabling events at boot. We want to enable events before
2521 * the filesystem is initialized.
2524 __trace_early_add_new_event(struct trace_event_call *call,
2525 struct trace_array *tr)
2527 struct trace_event_file *file;
2529 file = trace_create_new_event(call, tr);
2533 return event_define_fields(call);
2536 struct ftrace_module_file_ops;
2537 static void __add_event_to_tracers(struct trace_event_call *call);
2539 /* Add an additional event_call dynamically */
2540 int trace_add_event_call(struct trace_event_call *call)
2543 lockdep_assert_held(&event_mutex);
2545 mutex_lock(&trace_types_lock);
2547 ret = __register_event(call, NULL);
2549 __add_event_to_tracers(call);
2551 mutex_unlock(&trace_types_lock);
2556 * Must be called under locking of trace_types_lock, event_mutex and
2559 static void __trace_remove_event_call(struct trace_event_call *call)
2562 trace_destroy_fields(call);
2563 free_event_filter(call->filter);
2564 call->filter = NULL;
2567 static int probe_remove_event_call(struct trace_event_call *call)
2569 struct trace_array *tr;
2570 struct trace_event_file *file;
2572 #ifdef CONFIG_PERF_EVENTS
2573 if (call->perf_refcount)
2576 do_for_each_event_file(tr, file) {
2577 if (file->event_call != call)
2580 * We can't rely on ftrace_event_enable_disable(enable => 0)
2581 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2582 * TRACE_REG_UNREGISTER.
2584 if (file->flags & EVENT_FILE_FL_ENABLED)
2587 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2588 tr->clear_trace = true;
2590 * The do_for_each_event_file_safe() is
2591 * a double loop. After finding the call for this
2592 * trace_array, we use break to jump to the next
2596 } while_for_each_event_file();
2598 __trace_remove_event_call(call);
2602 /* No need to clear the trace now */
2603 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2604 tr->clear_trace = false;
2609 /* Remove an event_call */
2610 int trace_remove_event_call(struct trace_event_call *call)
2614 lockdep_assert_held(&event_mutex);
2616 mutex_lock(&trace_types_lock);
2617 down_write(&trace_event_sem);
2618 ret = probe_remove_event_call(call);
2619 up_write(&trace_event_sem);
2620 mutex_unlock(&trace_types_lock);
2625 #define for_each_event(event, start, end) \
2626 for (event = start; \
2627 (unsigned long)event < (unsigned long)end; \
2630 #ifdef CONFIG_MODULES
2632 static void trace_module_add_events(struct module *mod)
2634 struct trace_event_call **call, **start, **end;
2636 if (!mod->num_trace_events)
2639 /* Don't add infrastructure for mods without tracepoints */
2640 if (trace_module_has_bad_taint(mod)) {
2641 pr_err("%s: module has bad taint, not creating trace events\n",
2646 start = mod->trace_events;
2647 end = mod->trace_events + mod->num_trace_events;
2649 for_each_event(call, start, end) {
2650 __register_event(*call, mod);
2651 __add_event_to_tracers(*call);
2655 static void trace_module_remove_events(struct module *mod)
2657 struct trace_event_call *call, *p;
2659 down_write(&trace_event_sem);
2660 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2661 if (call->mod == mod)
2662 __trace_remove_event_call(call);
2664 up_write(&trace_event_sem);
2667 * It is safest to reset the ring buffer if the module being unloaded
2668 * registered any events that were used. The only worry is if
2669 * a new module gets loaded, and takes on the same id as the events
2670 * of this module. When printing out the buffer, traced events left
2671 * over from this module may be passed to the new module events and
2672 * unexpected results may occur.
2674 tracing_reset_all_online_cpus_unlocked();
2677 static int trace_module_notify(struct notifier_block *self,
2678 unsigned long val, void *data)
2680 struct module *mod = data;
2682 mutex_lock(&event_mutex);
2683 mutex_lock(&trace_types_lock);
2685 case MODULE_STATE_COMING:
2686 trace_module_add_events(mod);
2688 case MODULE_STATE_GOING:
2689 trace_module_remove_events(mod);
2692 mutex_unlock(&trace_types_lock);
2693 mutex_unlock(&event_mutex);
2698 static struct notifier_block trace_module_nb = {
2699 .notifier_call = trace_module_notify,
2700 .priority = 1, /* higher than trace.c module notify */
2702 #endif /* CONFIG_MODULES */
2704 /* Create a new event directory structure for a trace directory. */
2706 __trace_add_event_dirs(struct trace_array *tr)
2708 struct trace_event_call *call;
2711 list_for_each_entry(call, &ftrace_events, list) {
2712 ret = __trace_add_new_event(call, tr);
2714 pr_warn("Could not create directory for event %s\n",
2715 trace_event_name(call));
2719 /* Returns any file that matches the system and event */
2720 struct trace_event_file *
2721 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2723 struct trace_event_file *file;
2724 struct trace_event_call *call;
2727 list_for_each_entry(file, &tr->events, list) {
2729 call = file->event_call;
2730 name = trace_event_name(call);
2732 if (!name || !call->class)
2735 if (strcmp(event, name) == 0 &&
2736 strcmp(system, call->class->system) == 0)
2742 /* Returns valid trace event files that match system and event */
2743 struct trace_event_file *
2744 find_event_file(struct trace_array *tr, const char *system, const char *event)
2746 struct trace_event_file *file;
2748 file = __find_event_file(tr, system, event);
2749 if (!file || !file->event_call->class->reg ||
2750 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2757 * trace_get_event_file - Find and return a trace event file
2758 * @instance: The name of the trace instance containing the event
2759 * @system: The name of the system containing the event
2760 * @event: The name of the event
2762 * Return a trace event file given the trace instance name, trace
2763 * system, and trace event name. If the instance name is NULL, it
2764 * refers to the top-level trace array.
2766 * This function will look it up and return it if found, after calling
2767 * trace_array_get() to prevent the instance from going away, and
2768 * increment the event's module refcount to prevent it from being
2771 * To release the file, call trace_put_event_file(), which will call
2772 * trace_array_put() and decrement the event's module refcount.
2774 * Return: The trace event on success, ERR_PTR otherwise.
2776 struct trace_event_file *trace_get_event_file(const char *instance,
2780 struct trace_array *tr = top_trace_array();
2781 struct trace_event_file *file = NULL;
2785 tr = trace_array_find_get(instance);
2787 return ERR_PTR(-ENOENT);
2789 ret = trace_array_get(tr);
2791 return ERR_PTR(ret);
2794 mutex_lock(&event_mutex);
2796 file = find_event_file(tr, system, event);
2798 trace_array_put(tr);
2803 /* Don't let event modules unload while in use */
2804 ret = try_module_get(file->event_call->mod);
2806 trace_array_put(tr);
2813 mutex_unlock(&event_mutex);
2816 file = ERR_PTR(ret);
2820 EXPORT_SYMBOL_GPL(trace_get_event_file);
2823 * trace_put_event_file - Release a file from trace_get_event_file()
2824 * @file: The trace event file
2826 * If a file was retrieved using trace_get_event_file(), this should
2827 * be called when it's no longer needed. It will cancel the previous
2828 * trace_array_get() called by that function, and decrement the
2829 * event's module refcount.
2831 void trace_put_event_file(struct trace_event_file *file)
2833 mutex_lock(&event_mutex);
2834 module_put(file->event_call->mod);
2835 mutex_unlock(&event_mutex);
2837 trace_array_put(file->tr);
2839 EXPORT_SYMBOL_GPL(trace_put_event_file);
2841 #ifdef CONFIG_DYNAMIC_FTRACE
2844 #define ENABLE_EVENT_STR "enable_event"
2845 #define DISABLE_EVENT_STR "disable_event"
2847 struct event_probe_data {
2848 struct trace_event_file *file;
2849 unsigned long count;
2854 static void update_event_probe(struct event_probe_data *data)
2857 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2859 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2863 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2864 struct trace_array *tr, struct ftrace_probe_ops *ops,
2867 struct ftrace_func_mapper *mapper = data;
2868 struct event_probe_data *edata;
2871 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2872 if (!pdata || !*pdata)
2876 update_event_probe(edata);
2880 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2881 struct trace_array *tr, struct ftrace_probe_ops *ops,
2884 struct ftrace_func_mapper *mapper = data;
2885 struct event_probe_data *edata;
2888 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2889 if (!pdata || !*pdata)
2897 /* Skip if the event is in a state we want to switch to */
2898 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2901 if (edata->count != -1)
2904 update_event_probe(edata);
2908 event_enable_print(struct seq_file *m, unsigned long ip,
2909 struct ftrace_probe_ops *ops, void *data)
2911 struct ftrace_func_mapper *mapper = data;
2912 struct event_probe_data *edata;
2915 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2917 if (WARN_ON_ONCE(!pdata || !*pdata))
2922 seq_printf(m, "%ps:", (void *)ip);
2924 seq_printf(m, "%s:%s:%s",
2925 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2926 edata->file->event_call->class->system,
2927 trace_event_name(edata->file->event_call));
2929 if (edata->count == -1)
2930 seq_puts(m, ":unlimited\n");
2932 seq_printf(m, ":count=%ld\n", edata->count);
2938 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2939 unsigned long ip, void *init_data, void **data)
2941 struct ftrace_func_mapper *mapper = *data;
2942 struct event_probe_data *edata = init_data;
2946 mapper = allocate_ftrace_func_mapper();
2952 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2961 static int free_probe_data(void *data)
2963 struct event_probe_data *edata = data;
2967 /* Remove the SOFT_MODE flag */
2968 __ftrace_event_enable_disable(edata->file, 0, 1);
2969 module_put(edata->file->event_call->mod);
2976 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2977 unsigned long ip, void *data)
2979 struct ftrace_func_mapper *mapper = data;
2980 struct event_probe_data *edata;
2985 free_ftrace_func_mapper(mapper, free_probe_data);
2989 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2991 if (WARN_ON_ONCE(!edata))
2994 if (WARN_ON_ONCE(edata->ref <= 0))
2997 free_probe_data(edata);
3000 static struct ftrace_probe_ops event_enable_probe_ops = {
3001 .func = event_enable_probe,
3002 .print = event_enable_print,
3003 .init = event_enable_init,
3004 .free = event_enable_free,
3007 static struct ftrace_probe_ops event_enable_count_probe_ops = {
3008 .func = event_enable_count_probe,
3009 .print = event_enable_print,
3010 .init = event_enable_init,
3011 .free = event_enable_free,
3014 static struct ftrace_probe_ops event_disable_probe_ops = {
3015 .func = event_enable_probe,
3016 .print = event_enable_print,
3017 .init = event_enable_init,
3018 .free = event_enable_free,
3021 static struct ftrace_probe_ops event_disable_count_probe_ops = {
3022 .func = event_enable_count_probe,
3023 .print = event_enable_print,
3024 .init = event_enable_init,
3025 .free = event_enable_free,
3029 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3030 char *glob, char *cmd, char *param, int enabled)
3032 struct trace_event_file *file;
3033 struct ftrace_probe_ops *ops;
3034 struct event_probe_data *data;
3044 /* hash funcs only work with set_ftrace_filter */
3045 if (!enabled || !param)
3048 system = strsep(¶m, ":");
3052 event = strsep(¶m, ":");
3054 mutex_lock(&event_mutex);
3057 file = find_event_file(tr, system, event);
3061 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3064 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3066 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3068 if (glob[0] == '!') {
3069 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3075 data = kzalloc(sizeof(*data), GFP_KERNEL);
3079 data->enable = enable;
3086 number = strsep(¶m, ":");
3089 if (!strlen(number))
3093 * We use the callback data field (which is a pointer)
3096 ret = kstrtoul(number, 0, &data->count);
3101 /* Don't let event modules unload while probe registered */
3102 ret = try_module_get(file->event_call->mod);
3108 ret = __ftrace_event_enable_disable(file, 1, 1);
3112 ret = register_ftrace_function_probe(glob, tr, ops, data);
3114 * The above returns on success the # of functions enabled,
3115 * but if it didn't find any functions it returns zero.
3116 * Consider no functions a failure too.
3123 /* Just return zero, not the number of enabled functions */
3126 mutex_unlock(&event_mutex);
3130 __ftrace_event_enable_disable(file, 0, 1);
3132 module_put(file->event_call->mod);
3138 static struct ftrace_func_command event_enable_cmd = {
3139 .name = ENABLE_EVENT_STR,
3140 .func = event_enable_func,
3143 static struct ftrace_func_command event_disable_cmd = {
3144 .name = DISABLE_EVENT_STR,
3145 .func = event_enable_func,
3148 static __init int register_event_cmds(void)
3152 ret = register_ftrace_command(&event_enable_cmd);
3153 if (WARN_ON(ret < 0))
3155 ret = register_ftrace_command(&event_disable_cmd);
3156 if (WARN_ON(ret < 0))
3157 unregister_ftrace_command(&event_enable_cmd);
3161 static inline int register_event_cmds(void) { return 0; }
3162 #endif /* CONFIG_DYNAMIC_FTRACE */
3165 * The top level array and trace arrays created by boot-time tracing
3166 * have already had its trace_event_file descriptors created in order
3167 * to allow for early events to be recorded.
3168 * This function is called after the tracefs has been initialized,
3169 * and we now have to create the files associated to the events.
3171 static void __trace_early_add_event_dirs(struct trace_array *tr)
3173 struct trace_event_file *file;
3177 list_for_each_entry(file, &tr->events, list) {
3178 ret = event_create_dir(tr->event_dir, file);
3180 pr_warn("Could not create directory for event %s\n",
3181 trace_event_name(file->event_call));
3186 * For early boot up, the top trace array and the trace arrays created
3187 * by boot-time tracing require to have a list of events that can be
3188 * enabled. This must be done before the filesystem is set up in order
3189 * to allow events to be traced early.
3191 void __trace_early_add_events(struct trace_array *tr)
3193 struct trace_event_call *call;
3196 list_for_each_entry(call, &ftrace_events, list) {
3197 /* Early boot up should not have any modules loaded */
3198 if (WARN_ON_ONCE(call->mod))
3201 ret = __trace_early_add_new_event(call, tr);
3203 pr_warn("Could not create early event %s\n",
3204 trace_event_name(call));
3208 /* Remove the event directory structure for a trace directory. */
3210 __trace_remove_event_dirs(struct trace_array *tr)
3212 struct trace_event_file *file, *next;
3214 list_for_each_entry_safe(file, next, &tr->events, list)
3215 remove_event_file_dir(file);
3218 static void __add_event_to_tracers(struct trace_event_call *call)
3220 struct trace_array *tr;
3222 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3223 __trace_add_new_event(call, tr);
3226 extern struct trace_event_call *__start_ftrace_events[];
3227 extern struct trace_event_call *__stop_ftrace_events[];
3229 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3231 static __init int setup_trace_event(char *str)
3233 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3234 ring_buffer_expanded = true;
3235 disable_tracing_selftest("running event tracing");
3239 __setup("trace_event=", setup_trace_event);
3241 /* Expects to have event_mutex held when called */
3243 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3245 struct dentry *d_events;
3246 struct dentry *entry;
3248 entry = tracefs_create_file("set_event", 0644, parent,
3249 tr, &ftrace_set_event_fops);
3251 pr_warn("Could not create tracefs 'set_event' entry\n");
3255 d_events = tracefs_create_dir("events", parent);
3257 pr_warn("Could not create tracefs 'events' directory\n");
3261 entry = trace_create_file("enable", 0644, d_events,
3262 tr, &ftrace_tr_enable_fops);
3264 pr_warn("Could not create tracefs 'enable' entry\n");
3268 /* There are not as crucial, just warn if they are not created */
3270 entry = tracefs_create_file("set_event_pid", 0644, parent,
3271 tr, &ftrace_set_event_pid_fops);
3273 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3275 entry = tracefs_create_file("set_event_notrace_pid", 0644, parent,
3276 tr, &ftrace_set_event_notrace_pid_fops);
3278 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
3280 /* ring buffer internal formats */
3281 entry = trace_create_file("header_page", 0444, d_events,
3282 ring_buffer_print_page_header,
3283 &ftrace_show_header_fops);
3285 pr_warn("Could not create tracefs 'header_page' entry\n");
3287 entry = trace_create_file("header_event", 0444, d_events,
3288 ring_buffer_print_entry_header,
3289 &ftrace_show_header_fops);
3291 pr_warn("Could not create tracefs 'header_event' entry\n");
3293 tr->event_dir = d_events;
3299 * event_trace_add_tracer - add a instance of a trace_array to events
3300 * @parent: The parent dentry to place the files/directories for events in
3301 * @tr: The trace array associated with these events
3303 * When a new instance is created, it needs to set up its events
3304 * directory, as well as other files associated with events. It also
3305 * creates the event hierachry in the @parent/events directory.
3307 * Returns 0 on success.
3309 * Must be called with event_mutex held.
3311 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3315 lockdep_assert_held(&event_mutex);
3317 ret = create_event_toplevel_files(parent, tr);
3321 down_write(&trace_event_sem);
3322 /* If tr already has the event list, it is initialized in early boot. */
3323 if (unlikely(!list_empty(&tr->events)))
3324 __trace_early_add_event_dirs(tr);
3326 __trace_add_event_dirs(tr);
3327 up_write(&trace_event_sem);
3334 * The top trace array already had its file descriptors created.
3335 * Now the files themselves need to be created.
3338 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3342 mutex_lock(&event_mutex);
3344 ret = create_event_toplevel_files(parent, tr);
3348 down_write(&trace_event_sem);
3349 __trace_early_add_event_dirs(tr);
3350 up_write(&trace_event_sem);
3353 mutex_unlock(&event_mutex);
3358 /* Must be called with event_mutex held */
3359 int event_trace_del_tracer(struct trace_array *tr)
3361 lockdep_assert_held(&event_mutex);
3363 /* Disable any event triggers and associated soft-disabled events */
3364 clear_event_triggers(tr);
3366 /* Clear the pid list */
3367 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3369 /* Disable any running events */
3370 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3372 /* Make sure no more events are being executed */
3373 tracepoint_synchronize_unregister();
3375 down_write(&trace_event_sem);
3376 __trace_remove_event_dirs(tr);
3377 tracefs_remove(tr->event_dir);
3378 up_write(&trace_event_sem);
3380 tr->event_dir = NULL;
3385 static __init int event_trace_memsetup(void)
3387 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3388 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3393 early_enable_events(struct trace_array *tr, bool disable_first)
3395 char *buf = bootup_event_buf;
3400 token = strsep(&buf, ",");
3406 /* Restarting syscalls requires that we stop them first */
3408 ftrace_set_clr_event(tr, token, 0);
3410 ret = ftrace_set_clr_event(tr, token, 1);
3412 pr_warn("Failed to enable trace event: %s\n", token);
3415 /* Put back the comma to allow this to be called again */
3421 static __init int event_trace_enable(void)
3423 struct trace_array *tr = top_trace_array();
3424 struct trace_event_call **iter, *call;
3430 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3433 ret = event_init(call);
3435 list_add(&call->list, &ftrace_events);
3439 * We need the top trace array to have a working set of trace
3440 * points at early init, before the debug files and directories
3441 * are created. Create the file entries now, and attach them
3442 * to the actual file dentries later.
3444 __trace_early_add_events(tr);
3446 early_enable_events(tr, false);
3448 trace_printk_start_comm();
3450 register_event_cmds();
3452 register_trigger_cmds();
3458 * event_trace_enable() is called from trace_event_init() first to
3459 * initialize events and perhaps start any events that are on the
3460 * command line. Unfortunately, there are some events that will not
3461 * start this early, like the system call tracepoints that need
3462 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3463 * is called before pid 1 starts, and this flag is never set, making
3464 * the syscall tracepoint never get reached, but the event is enabled
3465 * regardless (and not doing anything).
3467 static __init int event_trace_enable_again(void)
3469 struct trace_array *tr;
3471 tr = top_trace_array();
3475 early_enable_events(tr, true);
3480 early_initcall(event_trace_enable_again);
3482 /* Init fields which doesn't related to the tracefs */
3483 static __init int event_trace_init_fields(void)
3485 if (trace_define_generic_fields())
3486 pr_warn("tracing: Failed to allocated generic fields");
3488 if (trace_define_common_fields())
3489 pr_warn("tracing: Failed to allocate common fields");
3494 __init int event_trace_init(void)
3496 struct trace_array *tr;
3497 struct dentry *entry;
3500 tr = top_trace_array();
3504 entry = tracefs_create_file("available_events", 0444, NULL,
3505 tr, &ftrace_avail_fops);
3507 pr_warn("Could not create tracefs 'available_events' entry\n");
3509 ret = early_event_add_tracer(NULL, tr);
3513 #ifdef CONFIG_MODULES
3514 ret = register_module_notifier(&trace_module_nb);
3516 pr_warn("Failed to register trace events module notifier\n");
3519 eventdir_initialized = true;
3524 void __init trace_event_init(void)
3526 event_trace_memsetup();
3527 init_ftrace_syscalls();
3528 event_trace_enable();
3529 event_trace_init_fields();
3532 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3534 static DEFINE_SPINLOCK(test_spinlock);
3535 static DEFINE_SPINLOCK(test_spinlock_irq);
3536 static DEFINE_MUTEX(test_mutex);
3538 static __init void test_work(struct work_struct *dummy)
3540 spin_lock(&test_spinlock);
3541 spin_lock_irq(&test_spinlock_irq);
3543 spin_unlock_irq(&test_spinlock_irq);
3544 spin_unlock(&test_spinlock);
3546 mutex_lock(&test_mutex);
3548 mutex_unlock(&test_mutex);
3551 static __init int event_test_thread(void *unused)
3555 test_malloc = kmalloc(1234, GFP_KERNEL);
3557 pr_info("failed to kmalloc\n");
3559 schedule_on_each_cpu(test_work);
3563 set_current_state(TASK_INTERRUPTIBLE);
3564 while (!kthread_should_stop()) {
3566 set_current_state(TASK_INTERRUPTIBLE);
3568 __set_current_state(TASK_RUNNING);
3574 * Do various things that may trigger events.
3576 static __init void event_test_stuff(void)
3578 struct task_struct *test_thread;
3580 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3582 kthread_stop(test_thread);
3586 * For every trace event defined, we will test each trace point separately,
3587 * and then by groups, and finally all trace points.
3589 static __init void event_trace_self_tests(void)
3591 struct trace_subsystem_dir *dir;
3592 struct trace_event_file *file;
3593 struct trace_event_call *call;
3594 struct event_subsystem *system;
3595 struct trace_array *tr;
3598 tr = top_trace_array();
3602 pr_info("Running tests on trace events:\n");
3604 list_for_each_entry(file, &tr->events, list) {
3606 call = file->event_call;
3608 /* Only test those that have a probe */
3609 if (!call->class || !call->class->probe)
3613 * Testing syscall events here is pretty useless, but
3614 * we still do it if configured. But this is time consuming.
3615 * What we really need is a user thread to perform the
3616 * syscalls as we test.
3618 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3619 if (call->class->system &&
3620 strcmp(call->class->system, "syscalls") == 0)
3624 pr_info("Testing event %s: ", trace_event_name(call));
3627 * If an event is already enabled, someone is using
3628 * it and the self test should not be on.
3630 if (file->flags & EVENT_FILE_FL_ENABLED) {
3631 pr_warn("Enabled event during self test!\n");
3636 ftrace_event_enable_disable(file, 1);
3638 ftrace_event_enable_disable(file, 0);
3643 /* Now test at the sub system level */
3645 pr_info("Running tests on trace event systems:\n");
3647 list_for_each_entry(dir, &tr->systems, list) {
3649 system = dir->subsystem;
3651 /* the ftrace system is special, skip it */
3652 if (strcmp(system->name, "ftrace") == 0)
3655 pr_info("Testing event system %s: ", system->name);
3657 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3658 if (WARN_ON_ONCE(ret)) {
3659 pr_warn("error enabling system %s\n",
3666 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3667 if (WARN_ON_ONCE(ret)) {
3668 pr_warn("error disabling system %s\n",
3676 /* Test with all events enabled */
3678 pr_info("Running tests on all trace events:\n");
3679 pr_info("Testing all events: ");
3681 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3682 if (WARN_ON_ONCE(ret)) {
3683 pr_warn("error enabling all events\n");
3690 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3691 if (WARN_ON_ONCE(ret)) {
3692 pr_warn("error disabling all events\n");
3699 #ifdef CONFIG_FUNCTION_TRACER
3701 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3703 static struct trace_event_file event_trace_file __initdata;
3706 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3707 struct ftrace_ops *op, struct pt_regs *pt_regs)
3709 struct trace_buffer *buffer;
3710 struct ring_buffer_event *event;
3711 struct ftrace_entry *entry;
3712 unsigned long flags;
3717 pc = preempt_count();
3718 preempt_disable_notrace();
3719 cpu = raw_smp_processor_id();
3720 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3725 local_save_flags(flags);
3727 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3728 TRACE_FN, sizeof(*entry),
3732 entry = ring_buffer_event_data(event);
3734 entry->parent_ip = parent_ip;
3736 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3739 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3740 preempt_enable_notrace();
3743 static struct ftrace_ops trace_ops __initdata =
3745 .func = function_test_events_call,
3746 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3749 static __init void event_trace_self_test_with_function(void)
3753 event_trace_file.tr = top_trace_array();
3754 if (WARN_ON(!event_trace_file.tr))
3757 ret = register_ftrace_function(&trace_ops);
3758 if (WARN_ON(ret < 0)) {
3759 pr_info("Failed to enable function tracer for event tests\n");
3762 pr_info("Running tests again, along with the function tracer\n");
3763 event_trace_self_tests();
3764 unregister_ftrace_function(&trace_ops);
3767 static __init void event_trace_self_test_with_function(void)
3772 static __init int event_trace_self_tests_init(void)
3774 if (!tracing_selftest_disabled) {
3775 event_trace_self_tests();
3776 event_trace_self_test_with_function();
3782 late_initcall(event_trace_self_tests_init);