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(char *, COMM, FILTER_COMM);
172 __generic_field(char *, comm, FILTER_COMM);
177 static int trace_define_common_fields(void)
180 struct trace_entry ent;
182 __common_field(unsigned short, type);
183 __common_field(unsigned char, flags);
184 __common_field(unsigned char, preempt_count);
185 __common_field(int, pid);
190 static void trace_destroy_fields(struct trace_event_call *call)
192 struct ftrace_event_field *field, *next;
193 struct list_head *head;
195 head = trace_get_fields(call);
196 list_for_each_entry_safe(field, next, head, link) {
197 list_del(&field->link);
198 kmem_cache_free(field_cachep, field);
203 * run-time version of trace_event_get_offsets_<call>() that returns the last
204 * accessible offset of trace fields excluding __dynamic_array bytes
206 int trace_event_get_offsets(struct trace_event_call *call)
208 struct ftrace_event_field *tail;
209 struct list_head *head;
211 head = trace_get_fields(call);
213 * head->next points to the last field with the largest offset,
214 * since it was added last by trace_define_field()
216 tail = list_first_entry(head, struct ftrace_event_field, link);
217 return tail->offset + tail->size;
220 int trace_event_raw_init(struct trace_event_call *call)
224 id = register_trace_event(&call->event);
230 EXPORT_SYMBOL_GPL(trace_event_raw_init);
232 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
234 struct trace_array *tr = trace_file->tr;
235 struct trace_array_cpu *data;
236 struct trace_pid_list *no_pid_list;
237 struct trace_pid_list *pid_list;
239 pid_list = rcu_dereference_raw(tr->filtered_pids);
240 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
242 if (!pid_list && !no_pid_list)
245 data = this_cpu_ptr(tr->array_buffer.data);
247 return data->ignore_pid;
249 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
251 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
252 struct trace_event_file *trace_file,
255 struct trace_event_call *event_call = trace_file->event_call;
257 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
258 trace_event_ignore_this_pid(trace_file))
262 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
263 * preemption (adding one to the preempt_count). Since we are
264 * interested in the preempt_count at the time the tracepoint was
265 * hit, we need to subtract one to offset the increment.
267 fbuffer->trace_ctx = tracing_gen_ctx_dec();
268 fbuffer->trace_file = trace_file;
271 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
272 event_call->event.type, len,
277 fbuffer->regs = NULL;
278 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
279 return fbuffer->entry;
281 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
283 int trace_event_reg(struct trace_event_call *call,
284 enum trace_reg type, void *data)
286 struct trace_event_file *file = data;
288 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
290 case TRACE_REG_REGISTER:
291 return tracepoint_probe_register(call->tp,
294 case TRACE_REG_UNREGISTER:
295 tracepoint_probe_unregister(call->tp,
300 #ifdef CONFIG_PERF_EVENTS
301 case TRACE_REG_PERF_REGISTER:
302 return tracepoint_probe_register(call->tp,
303 call->class->perf_probe,
305 case TRACE_REG_PERF_UNREGISTER:
306 tracepoint_probe_unregister(call->tp,
307 call->class->perf_probe,
310 case TRACE_REG_PERF_OPEN:
311 case TRACE_REG_PERF_CLOSE:
312 case TRACE_REG_PERF_ADD:
313 case TRACE_REG_PERF_DEL:
319 EXPORT_SYMBOL_GPL(trace_event_reg);
321 void trace_event_enable_cmd_record(bool enable)
323 struct trace_event_file *file;
324 struct trace_array *tr;
326 lockdep_assert_held(&event_mutex);
328 do_for_each_event_file(tr, file) {
330 if (!(file->flags & EVENT_FILE_FL_ENABLED))
334 tracing_start_cmdline_record();
335 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
337 tracing_stop_cmdline_record();
338 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
340 } while_for_each_event_file();
343 void trace_event_enable_tgid_record(bool enable)
345 struct trace_event_file *file;
346 struct trace_array *tr;
348 lockdep_assert_held(&event_mutex);
350 do_for_each_event_file(tr, file) {
351 if (!(file->flags & EVENT_FILE_FL_ENABLED))
355 tracing_start_tgid_record();
356 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
358 tracing_stop_tgid_record();
359 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
362 } while_for_each_event_file();
365 static int __ftrace_event_enable_disable(struct trace_event_file *file,
366 int enable, int soft_disable)
368 struct trace_event_call *call = file->event_call;
369 struct trace_array *tr = file->tr;
370 unsigned long file_flags = file->flags;
377 * When soft_disable is set and enable is cleared, the sm_ref
378 * reference counter is decremented. If it reaches 0, we want
379 * to clear the SOFT_DISABLED flag but leave the event in the
380 * state that it was. That is, if the event was enabled and
381 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
382 * is set we do not want the event to be enabled before we
385 * When soft_disable is not set but the SOFT_MODE flag is,
386 * we do nothing. Do not disable the tracepoint, otherwise
387 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
390 if (atomic_dec_return(&file->sm_ref) > 0)
392 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
393 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
395 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
397 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
398 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
399 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
400 tracing_stop_cmdline_record();
401 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
404 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
405 tracing_stop_tgid_record();
406 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
409 call->class->reg(call, TRACE_REG_UNREGISTER, file);
411 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
412 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
413 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
415 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
419 * When soft_disable is set and enable is set, we want to
420 * register the tracepoint for the event, but leave the event
421 * as is. That means, if the event was already enabled, we do
422 * nothing (but set SOFT_MODE). If the event is disabled, we
423 * set SOFT_DISABLED before enabling the event tracepoint, so
424 * it still seems to be disabled.
427 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
429 if (atomic_inc_return(&file->sm_ref) > 1)
431 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
434 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
435 bool cmd = false, tgid = false;
437 /* Keep the event disabled, when going to SOFT_MODE. */
439 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
441 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
443 tracing_start_cmdline_record();
444 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
447 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
449 tracing_start_tgid_record();
450 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
453 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
456 tracing_stop_cmdline_record();
458 tracing_stop_tgid_record();
459 pr_info("event trace: Could not enable event "
460 "%s\n", trace_event_name(call));
463 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
465 /* WAS_ENABLED gets set but never cleared. */
466 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
471 /* Enable or disable use of trace_buffered_event */
472 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
473 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
474 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
475 trace_buffered_event_enable();
477 trace_buffered_event_disable();
483 int trace_event_enable_disable(struct trace_event_file *file,
484 int enable, int soft_disable)
486 return __ftrace_event_enable_disable(file, enable, soft_disable);
489 static int ftrace_event_enable_disable(struct trace_event_file *file,
492 return __ftrace_event_enable_disable(file, enable, 0);
495 static void ftrace_clear_events(struct trace_array *tr)
497 struct trace_event_file *file;
499 mutex_lock(&event_mutex);
500 list_for_each_entry(file, &tr->events, list) {
501 ftrace_event_enable_disable(file, 0);
503 mutex_unlock(&event_mutex);
507 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
509 struct trace_pid_list *pid_list;
510 struct trace_array *tr = data;
512 pid_list = rcu_dereference_raw(tr->filtered_pids);
513 trace_filter_add_remove_task(pid_list, NULL, task);
515 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
516 trace_filter_add_remove_task(pid_list, NULL, task);
520 event_filter_pid_sched_process_fork(void *data,
521 struct task_struct *self,
522 struct task_struct *task)
524 struct trace_pid_list *pid_list;
525 struct trace_array *tr = data;
527 pid_list = rcu_dereference_sched(tr->filtered_pids);
528 trace_filter_add_remove_task(pid_list, self, task);
530 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
531 trace_filter_add_remove_task(pid_list, self, task);
534 void trace_event_follow_fork(struct trace_array *tr, bool enable)
537 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
539 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
542 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
544 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
550 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
551 struct task_struct *prev, struct task_struct *next)
553 struct trace_array *tr = data;
554 struct trace_pid_list *no_pid_list;
555 struct trace_pid_list *pid_list;
558 pid_list = rcu_dereference_sched(tr->filtered_pids);
559 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
562 * Sched switch is funny, as we only want to ignore it
563 * in the notrace case if both prev and next should be ignored.
565 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
566 trace_ignore_this_task(NULL, no_pid_list, next);
568 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
569 (trace_ignore_this_task(pid_list, NULL, prev) &&
570 trace_ignore_this_task(pid_list, NULL, next)));
574 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
575 struct task_struct *prev, struct task_struct *next)
577 struct trace_array *tr = data;
578 struct trace_pid_list *no_pid_list;
579 struct trace_pid_list *pid_list;
581 pid_list = rcu_dereference_sched(tr->filtered_pids);
582 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
584 this_cpu_write(tr->array_buffer.data->ignore_pid,
585 trace_ignore_this_task(pid_list, no_pid_list, next));
589 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
591 struct trace_array *tr = data;
592 struct trace_pid_list *no_pid_list;
593 struct trace_pid_list *pid_list;
595 /* Nothing to do if we are already tracing */
596 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
599 pid_list = rcu_dereference_sched(tr->filtered_pids);
600 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
602 this_cpu_write(tr->array_buffer.data->ignore_pid,
603 trace_ignore_this_task(pid_list, no_pid_list, task));
607 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
609 struct trace_array *tr = data;
610 struct trace_pid_list *no_pid_list;
611 struct trace_pid_list *pid_list;
613 /* Nothing to do if we are not tracing */
614 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
617 pid_list = rcu_dereference_sched(tr->filtered_pids);
618 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
620 /* Set tracing if current is enabled */
621 this_cpu_write(tr->array_buffer.data->ignore_pid,
622 trace_ignore_this_task(pid_list, no_pid_list, current));
625 static void unregister_pid_events(struct trace_array *tr)
627 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
628 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
630 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
631 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
633 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
634 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
636 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
637 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
640 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
642 struct trace_pid_list *pid_list;
643 struct trace_pid_list *no_pid_list;
644 struct trace_event_file *file;
647 pid_list = rcu_dereference_protected(tr->filtered_pids,
648 lockdep_is_held(&event_mutex));
649 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
650 lockdep_is_held(&event_mutex));
652 /* Make sure there's something to do */
653 if (!pid_type_enabled(type, pid_list, no_pid_list))
656 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
657 unregister_pid_events(tr);
659 list_for_each_entry(file, &tr->events, list) {
660 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
663 for_each_possible_cpu(cpu)
664 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
667 if (type & TRACE_PIDS)
668 rcu_assign_pointer(tr->filtered_pids, NULL);
670 if (type & TRACE_NO_PIDS)
671 rcu_assign_pointer(tr->filtered_no_pids, NULL);
673 /* Wait till all users are no longer using pid filtering */
674 tracepoint_synchronize_unregister();
676 if ((type & TRACE_PIDS) && pid_list)
677 trace_free_pid_list(pid_list);
679 if ((type & TRACE_NO_PIDS) && no_pid_list)
680 trace_free_pid_list(no_pid_list);
683 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
685 mutex_lock(&event_mutex);
686 __ftrace_clear_event_pids(tr, type);
687 mutex_unlock(&event_mutex);
690 static void __put_system(struct event_subsystem *system)
692 struct event_filter *filter = system->filter;
694 WARN_ON_ONCE(system_refcount(system) == 0);
695 if (system_refcount_dec(system))
698 list_del(&system->list);
701 kfree(filter->filter_string);
704 kfree_const(system->name);
708 static void __get_system(struct event_subsystem *system)
710 WARN_ON_ONCE(system_refcount(system) == 0);
711 system_refcount_inc(system);
714 static void __get_system_dir(struct trace_subsystem_dir *dir)
716 WARN_ON_ONCE(dir->ref_count == 0);
718 __get_system(dir->subsystem);
721 static void __put_system_dir(struct trace_subsystem_dir *dir)
723 WARN_ON_ONCE(dir->ref_count == 0);
724 /* If the subsystem is about to be freed, the dir must be too */
725 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
727 __put_system(dir->subsystem);
728 if (!--dir->ref_count)
732 static void put_system(struct trace_subsystem_dir *dir)
734 mutex_lock(&event_mutex);
735 __put_system_dir(dir);
736 mutex_unlock(&event_mutex);
739 static void remove_subsystem(struct trace_subsystem_dir *dir)
744 if (!--dir->nr_events) {
745 tracefs_remove(dir->entry);
746 list_del(&dir->list);
747 __put_system_dir(dir);
751 static void remove_event_file_dir(struct trace_event_file *file)
753 struct dentry *dir = file->dir;
754 struct dentry *child;
757 spin_lock(&dir->d_lock); /* probably unneeded */
758 list_for_each_entry(child, &dir->d_subdirs, d_child) {
759 if (d_really_is_positive(child)) /* probably unneeded */
760 d_inode(child)->i_private = NULL;
762 spin_unlock(&dir->d_lock);
767 list_del(&file->list);
768 remove_subsystem(file->system);
769 free_event_filter(file->filter);
770 kmem_cache_free(file_cachep, file);
774 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
777 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
778 const char *sub, const char *event, int set)
780 struct trace_event_file *file;
781 struct trace_event_call *call;
786 list_for_each_entry(file, &tr->events, list) {
788 call = file->event_call;
789 name = trace_event_name(call);
791 if (!name || !call->class || !call->class->reg)
794 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
798 strcmp(match, name) != 0 &&
799 strcmp(match, call->class->system) != 0)
802 if (sub && strcmp(sub, call->class->system) != 0)
805 if (event && strcmp(event, name) != 0)
808 ret = ftrace_event_enable_disable(file, set);
811 * Save the first error and return that. Some events
812 * may still have been enabled, but let the user
813 * know that something went wrong.
824 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
825 const char *sub, const char *event, int set)
829 mutex_lock(&event_mutex);
830 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
831 mutex_unlock(&event_mutex);
836 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
838 char *event = NULL, *sub = NULL, *match;
844 * The buf format can be <subsystem>:<event-name>
845 * *:<event-name> means any event by that name.
846 * :<event-name> is the same.
848 * <subsystem>:* means all events in that subsystem
849 * <subsystem>: means the same.
851 * <name> (no ':') means all events in a subsystem with
852 * the name <name> or any event that matches <name>
855 match = strsep(&buf, ":");
861 if (!strlen(sub) || strcmp(sub, "*") == 0)
863 if (!strlen(event) || strcmp(event, "*") == 0)
867 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
869 /* Put back the colon to allow this to be called again */
877 * trace_set_clr_event - enable or disable an event
878 * @system: system name to match (NULL for any system)
879 * @event: event name to match (NULL for all events, within system)
880 * @set: 1 to enable, 0 to disable
882 * This is a way for other parts of the kernel to enable or disable
885 * Returns 0 on success, -EINVAL if the parameters do not match any
888 int trace_set_clr_event(const char *system, const char *event, int set)
890 struct trace_array *tr = top_trace_array();
895 return __ftrace_set_clr_event(tr, NULL, system, event, set);
897 EXPORT_SYMBOL_GPL(trace_set_clr_event);
900 * trace_array_set_clr_event - enable or disable an event for a trace array.
901 * @tr: concerned trace array.
902 * @system: system name to match (NULL for any system)
903 * @event: event name to match (NULL for all events, within system)
904 * @enable: true to enable, false to disable
906 * This is a way for other parts of the kernel to enable or disable
909 * Returns 0 on success, -EINVAL if the parameters do not match any
912 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
913 const char *event, bool enable)
920 set = (enable == true) ? 1 : 0;
921 return __ftrace_set_clr_event(tr, NULL, system, event, set);
923 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
925 /* 128 should be much more than enough */
926 #define EVENT_BUF_SIZE 127
929 ftrace_event_write(struct file *file, const char __user *ubuf,
930 size_t cnt, loff_t *ppos)
932 struct trace_parser parser;
933 struct seq_file *m = file->private_data;
934 struct trace_array *tr = m->private;
940 ret = tracing_update_buffers();
944 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
947 read = trace_get_user(&parser, ubuf, cnt, ppos);
949 if (read >= 0 && trace_parser_loaded((&parser))) {
952 if (*parser.buffer == '!')
955 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
963 trace_parser_put(&parser);
969 t_next(struct seq_file *m, void *v, loff_t *pos)
971 struct trace_event_file *file = v;
972 struct trace_event_call *call;
973 struct trace_array *tr = m->private;
977 list_for_each_entry_continue(file, &tr->events, list) {
978 call = file->event_call;
980 * The ftrace subsystem is for showing formats only.
981 * They can not be enabled or disabled via the event files.
983 if (call->class && call->class->reg &&
984 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
991 static void *t_start(struct seq_file *m, loff_t *pos)
993 struct trace_event_file *file;
994 struct trace_array *tr = m->private;
997 mutex_lock(&event_mutex);
999 file = list_entry(&tr->events, struct trace_event_file, list);
1000 for (l = 0; l <= *pos; ) {
1001 file = t_next(m, file, &l);
1009 s_next(struct seq_file *m, void *v, loff_t *pos)
1011 struct trace_event_file *file = v;
1012 struct trace_array *tr = m->private;
1016 list_for_each_entry_continue(file, &tr->events, list) {
1017 if (file->flags & EVENT_FILE_FL_ENABLED)
1024 static void *s_start(struct seq_file *m, loff_t *pos)
1026 struct trace_event_file *file;
1027 struct trace_array *tr = m->private;
1030 mutex_lock(&event_mutex);
1032 file = list_entry(&tr->events, struct trace_event_file, list);
1033 for (l = 0; l <= *pos; ) {
1034 file = s_next(m, file, &l);
1041 static int t_show(struct seq_file *m, void *v)
1043 struct trace_event_file *file = v;
1044 struct trace_event_call *call = file->event_call;
1046 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1047 seq_printf(m, "%s:", call->class->system);
1048 seq_printf(m, "%s\n", trace_event_name(call));
1053 static void t_stop(struct seq_file *m, void *p)
1055 mutex_unlock(&event_mutex);
1059 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1061 struct trace_array *tr = m->private;
1062 struct trace_pid_list *pid_list;
1064 if (type == TRACE_PIDS)
1065 pid_list = rcu_dereference_sched(tr->filtered_pids);
1067 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1069 return trace_pid_next(pid_list, v, pos);
1073 p_next(struct seq_file *m, void *v, loff_t *pos)
1075 return __next(m, v, pos, TRACE_PIDS);
1079 np_next(struct seq_file *m, void *v, loff_t *pos)
1081 return __next(m, v, pos, TRACE_NO_PIDS);
1084 static void *__start(struct seq_file *m, loff_t *pos, int type)
1087 struct trace_pid_list *pid_list;
1088 struct trace_array *tr = m->private;
1091 * Grab the mutex, to keep calls to p_next() having the same
1092 * tr->filtered_pids as p_start() has.
1093 * If we just passed the tr->filtered_pids around, then RCU would
1094 * have been enough, but doing that makes things more complex.
1096 mutex_lock(&event_mutex);
1097 rcu_read_lock_sched();
1099 if (type == TRACE_PIDS)
1100 pid_list = rcu_dereference_sched(tr->filtered_pids);
1102 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1107 return trace_pid_start(pid_list, pos);
1110 static void *p_start(struct seq_file *m, loff_t *pos)
1113 return __start(m, pos, TRACE_PIDS);
1116 static void *np_start(struct seq_file *m, loff_t *pos)
1119 return __start(m, pos, TRACE_NO_PIDS);
1122 static void p_stop(struct seq_file *m, void *p)
1125 rcu_read_unlock_sched();
1126 mutex_unlock(&event_mutex);
1130 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1133 struct trace_event_file *file;
1134 unsigned long flags;
1137 mutex_lock(&event_mutex);
1138 file = event_file_data(filp);
1140 flags = file->flags;
1141 mutex_unlock(&event_mutex);
1146 if (flags & EVENT_FILE_FL_ENABLED &&
1147 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1150 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1151 flags & EVENT_FILE_FL_SOFT_MODE)
1156 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1160 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1163 struct trace_event_file *file;
1167 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1171 ret = tracing_update_buffers();
1179 mutex_lock(&event_mutex);
1180 file = event_file_data(filp);
1182 ret = ftrace_event_enable_disable(file, val);
1183 mutex_unlock(&event_mutex);
1192 return ret ? ret : cnt;
1196 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1199 const char set_to_char[4] = { '?', '0', '1', 'X' };
1200 struct trace_subsystem_dir *dir = filp->private_data;
1201 struct event_subsystem *system = dir->subsystem;
1202 struct trace_event_call *call;
1203 struct trace_event_file *file;
1204 struct trace_array *tr = dir->tr;
1209 mutex_lock(&event_mutex);
1210 list_for_each_entry(file, &tr->events, list) {
1211 call = file->event_call;
1212 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1213 !trace_event_name(call) || !call->class || !call->class->reg)
1216 if (system && strcmp(call->class->system, system->name) != 0)
1220 * We need to find out if all the events are set
1221 * or if all events or cleared, or if we have
1224 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1227 * If we have a mixture, no need to look further.
1232 mutex_unlock(&event_mutex);
1234 buf[0] = set_to_char[set];
1237 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1243 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1246 struct trace_subsystem_dir *dir = filp->private_data;
1247 struct event_subsystem *system = dir->subsystem;
1248 const char *name = NULL;
1252 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1256 ret = tracing_update_buffers();
1260 if (val != 0 && val != 1)
1264 * Opening of "enable" adds a ref count to system,
1265 * so the name is safe to use.
1268 name = system->name;
1270 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1284 FORMAT_FIELD_SEPERATOR = 2,
1285 FORMAT_PRINTFMT = 3,
1288 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1290 struct trace_event_call *call = event_file_data(m->private);
1291 struct list_head *common_head = &ftrace_common_fields;
1292 struct list_head *head = trace_get_fields(call);
1293 struct list_head *node = v;
1297 switch ((unsigned long)v) {
1302 case FORMAT_FIELD_SEPERATOR:
1306 case FORMAT_PRINTFMT:
1312 if (node == common_head)
1313 return (void *)FORMAT_FIELD_SEPERATOR;
1314 else if (node == head)
1315 return (void *)FORMAT_PRINTFMT;
1320 static int f_show(struct seq_file *m, void *v)
1322 struct trace_event_call *call = event_file_data(m->private);
1323 struct ftrace_event_field *field;
1324 const char *array_descriptor;
1326 switch ((unsigned long)v) {
1328 seq_printf(m, "name: %s\n", trace_event_name(call));
1329 seq_printf(m, "ID: %d\n", call->event.type);
1330 seq_puts(m, "format:\n");
1333 case FORMAT_FIELD_SEPERATOR:
1337 case FORMAT_PRINTFMT:
1338 seq_printf(m, "\nprint fmt: %s\n",
1343 field = list_entry(v, struct ftrace_event_field, link);
1345 * Smartly shows the array type(except dynamic array).
1348 * If TYPE := TYPE[LEN], it is shown:
1349 * field:TYPE VAR[LEN]
1351 array_descriptor = strchr(field->type, '[');
1353 if (str_has_prefix(field->type, "__data_loc"))
1354 array_descriptor = NULL;
1356 if (!array_descriptor)
1357 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1358 field->type, field->name, field->offset,
1359 field->size, !!field->is_signed);
1361 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1362 (int)(array_descriptor - field->type),
1363 field->type, field->name,
1364 array_descriptor, field->offset,
1365 field->size, !!field->is_signed);
1370 static void *f_start(struct seq_file *m, loff_t *pos)
1372 void *p = (void *)FORMAT_HEADER;
1375 /* ->stop() is called even if ->start() fails */
1376 mutex_lock(&event_mutex);
1377 if (!event_file_data(m->private))
1378 return ERR_PTR(-ENODEV);
1380 while (l < *pos && p)
1381 p = f_next(m, p, &l);
1386 static void f_stop(struct seq_file *m, void *p)
1388 mutex_unlock(&event_mutex);
1391 static const struct seq_operations trace_format_seq_ops = {
1398 static int trace_format_open(struct inode *inode, struct file *file)
1403 /* Do we want to hide event format files on tracefs lockdown? */
1405 ret = seq_open(file, &trace_format_seq_ops);
1409 m = file->private_data;
1416 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1418 int id = (long)event_file_data(filp);
1425 len = sprintf(buf, "%d\n", id);
1427 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1431 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1434 struct trace_event_file *file;
1435 struct trace_seq *s;
1441 s = kmalloc(sizeof(*s), GFP_KERNEL);
1448 mutex_lock(&event_mutex);
1449 file = event_file_data(filp);
1451 print_event_filter(file, s);
1452 mutex_unlock(&event_mutex);
1455 r = simple_read_from_buffer(ubuf, cnt, ppos,
1456 s->buffer, trace_seq_used(s));
1464 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1467 struct trace_event_file *file;
1471 if (cnt >= PAGE_SIZE)
1474 buf = memdup_user_nul(ubuf, cnt);
1476 return PTR_ERR(buf);
1478 mutex_lock(&event_mutex);
1479 file = event_file_data(filp);
1481 err = apply_event_filter(file, buf);
1482 mutex_unlock(&event_mutex);
1493 static LIST_HEAD(event_subsystems);
1495 static int subsystem_open(struct inode *inode, struct file *filp)
1497 struct event_subsystem *system = NULL;
1498 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1499 struct trace_array *tr;
1502 if (tracing_is_disabled())
1505 /* Make sure the system still exists */
1506 mutex_lock(&event_mutex);
1507 mutex_lock(&trace_types_lock);
1508 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1509 list_for_each_entry(dir, &tr->systems, list) {
1510 if (dir == inode->i_private) {
1511 /* Don't open systems with no events */
1512 if (dir->nr_events) {
1513 __get_system_dir(dir);
1514 system = dir->subsystem;
1521 mutex_unlock(&trace_types_lock);
1522 mutex_unlock(&event_mutex);
1527 /* Some versions of gcc think dir can be uninitialized here */
1530 /* Still need to increment the ref count of the system */
1531 if (trace_array_get(tr) < 0) {
1536 ret = tracing_open_generic(inode, filp);
1538 trace_array_put(tr);
1545 static int system_tr_open(struct inode *inode, struct file *filp)
1547 struct trace_subsystem_dir *dir;
1548 struct trace_array *tr = inode->i_private;
1551 /* Make a temporary dir that has no system but points to tr */
1552 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1556 ret = tracing_open_generic_tr(inode, filp);
1562 filp->private_data = dir;
1567 static int subsystem_release(struct inode *inode, struct file *file)
1569 struct trace_subsystem_dir *dir = file->private_data;
1571 trace_array_put(dir->tr);
1574 * If dir->subsystem is NULL, then this is a temporary
1575 * descriptor that was made for a trace_array to enable
1587 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1590 struct trace_subsystem_dir *dir = filp->private_data;
1591 struct event_subsystem *system = dir->subsystem;
1592 struct trace_seq *s;
1598 s = kmalloc(sizeof(*s), GFP_KERNEL);
1604 print_subsystem_event_filter(system, s);
1605 r = simple_read_from_buffer(ubuf, cnt, ppos,
1606 s->buffer, trace_seq_used(s));
1614 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1617 struct trace_subsystem_dir *dir = filp->private_data;
1621 if (cnt >= PAGE_SIZE)
1624 buf = memdup_user_nul(ubuf, cnt);
1626 return PTR_ERR(buf);
1628 err = apply_subsystem_event_filter(dir, buf);
1639 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1641 int (*func)(struct trace_seq *s) = filp->private_data;
1642 struct trace_seq *s;
1648 s = kmalloc(sizeof(*s), GFP_KERNEL);
1655 r = simple_read_from_buffer(ubuf, cnt, ppos,
1656 s->buffer, trace_seq_used(s));
1663 static void ignore_task_cpu(void *data)
1665 struct trace_array *tr = data;
1666 struct trace_pid_list *pid_list;
1667 struct trace_pid_list *no_pid_list;
1670 * This function is called by on_each_cpu() while the
1671 * event_mutex is held.
1673 pid_list = rcu_dereference_protected(tr->filtered_pids,
1674 mutex_is_locked(&event_mutex));
1675 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1676 mutex_is_locked(&event_mutex));
1678 this_cpu_write(tr->array_buffer.data->ignore_pid,
1679 trace_ignore_this_task(pid_list, no_pid_list, current));
1682 static void register_pid_events(struct trace_array *tr)
1685 * Register a probe that is called before all other probes
1686 * to set ignore_pid if next or prev do not match.
1687 * Register a probe this is called after all other probes
1688 * to only keep ignore_pid set if next pid matches.
1690 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1692 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1695 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1697 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1700 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1702 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1705 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1707 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1712 event_pid_write(struct file *filp, const char __user *ubuf,
1713 size_t cnt, loff_t *ppos, int type)
1715 struct seq_file *m = filp->private_data;
1716 struct trace_array *tr = m->private;
1717 struct trace_pid_list *filtered_pids = NULL;
1718 struct trace_pid_list *other_pids = NULL;
1719 struct trace_pid_list *pid_list;
1720 struct trace_event_file *file;
1726 ret = tracing_update_buffers();
1730 mutex_lock(&event_mutex);
1732 if (type == TRACE_PIDS) {
1733 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1734 lockdep_is_held(&event_mutex));
1735 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1736 lockdep_is_held(&event_mutex));
1738 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1739 lockdep_is_held(&event_mutex));
1740 other_pids = rcu_dereference_protected(tr->filtered_pids,
1741 lockdep_is_held(&event_mutex));
1744 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1748 if (type == TRACE_PIDS)
1749 rcu_assign_pointer(tr->filtered_pids, pid_list);
1751 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1753 list_for_each_entry(file, &tr->events, list) {
1754 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1757 if (filtered_pids) {
1758 tracepoint_synchronize_unregister();
1759 trace_free_pid_list(filtered_pids);
1760 } else if (pid_list && !other_pids) {
1761 register_pid_events(tr);
1765 * Ignoring of pids is done at task switch. But we have to
1766 * check for those tasks that are currently running.
1767 * Always do this in case a pid was appended or removed.
1769 on_each_cpu(ignore_task_cpu, tr, 1);
1772 mutex_unlock(&event_mutex);
1781 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1782 size_t cnt, loff_t *ppos)
1784 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
1788 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
1789 size_t cnt, loff_t *ppos)
1791 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
1794 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1795 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1796 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1797 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
1798 static int ftrace_event_release(struct inode *inode, struct file *file);
1800 static const struct seq_operations show_event_seq_ops = {
1807 static const struct seq_operations show_set_event_seq_ops = {
1814 static const struct seq_operations show_set_pid_seq_ops = {
1817 .show = trace_pid_show,
1821 static const struct seq_operations show_set_no_pid_seq_ops = {
1824 .show = trace_pid_show,
1828 static const struct file_operations ftrace_avail_fops = {
1829 .open = ftrace_event_avail_open,
1831 .llseek = seq_lseek,
1832 .release = seq_release,
1835 static const struct file_operations ftrace_set_event_fops = {
1836 .open = ftrace_event_set_open,
1838 .write = ftrace_event_write,
1839 .llseek = seq_lseek,
1840 .release = ftrace_event_release,
1843 static const struct file_operations ftrace_set_event_pid_fops = {
1844 .open = ftrace_event_set_pid_open,
1846 .write = ftrace_event_pid_write,
1847 .llseek = seq_lseek,
1848 .release = ftrace_event_release,
1851 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
1852 .open = ftrace_event_set_npid_open,
1854 .write = ftrace_event_npid_write,
1855 .llseek = seq_lseek,
1856 .release = ftrace_event_release,
1859 static const struct file_operations ftrace_enable_fops = {
1860 .open = tracing_open_generic,
1861 .read = event_enable_read,
1862 .write = event_enable_write,
1863 .llseek = default_llseek,
1866 static const struct file_operations ftrace_event_format_fops = {
1867 .open = trace_format_open,
1869 .llseek = seq_lseek,
1870 .release = seq_release,
1873 static const struct file_operations ftrace_event_id_fops = {
1874 .read = event_id_read,
1875 .llseek = default_llseek,
1878 static const struct file_operations ftrace_event_filter_fops = {
1879 .open = tracing_open_generic,
1880 .read = event_filter_read,
1881 .write = event_filter_write,
1882 .llseek = default_llseek,
1885 static const struct file_operations ftrace_subsystem_filter_fops = {
1886 .open = subsystem_open,
1887 .read = subsystem_filter_read,
1888 .write = subsystem_filter_write,
1889 .llseek = default_llseek,
1890 .release = subsystem_release,
1893 static const struct file_operations ftrace_system_enable_fops = {
1894 .open = subsystem_open,
1895 .read = system_enable_read,
1896 .write = system_enable_write,
1897 .llseek = default_llseek,
1898 .release = subsystem_release,
1901 static const struct file_operations ftrace_tr_enable_fops = {
1902 .open = system_tr_open,
1903 .read = system_enable_read,
1904 .write = system_enable_write,
1905 .llseek = default_llseek,
1906 .release = subsystem_release,
1909 static const struct file_operations ftrace_show_header_fops = {
1910 .open = tracing_open_generic,
1911 .read = show_header,
1912 .llseek = default_llseek,
1916 ftrace_event_open(struct inode *inode, struct file *file,
1917 const struct seq_operations *seq_ops)
1922 ret = security_locked_down(LOCKDOWN_TRACEFS);
1926 ret = seq_open(file, seq_ops);
1929 m = file->private_data;
1930 /* copy tr over to seq ops */
1931 m->private = inode->i_private;
1936 static int ftrace_event_release(struct inode *inode, struct file *file)
1938 struct trace_array *tr = inode->i_private;
1940 trace_array_put(tr);
1942 return seq_release(inode, file);
1946 ftrace_event_avail_open(struct inode *inode, struct file *file)
1948 const struct seq_operations *seq_ops = &show_event_seq_ops;
1950 /* Checks for tracefs lockdown */
1951 return ftrace_event_open(inode, file, seq_ops);
1955 ftrace_event_set_open(struct inode *inode, struct file *file)
1957 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1958 struct trace_array *tr = inode->i_private;
1961 ret = tracing_check_open_get_tr(tr);
1965 if ((file->f_mode & FMODE_WRITE) &&
1966 (file->f_flags & O_TRUNC))
1967 ftrace_clear_events(tr);
1969 ret = ftrace_event_open(inode, file, seq_ops);
1971 trace_array_put(tr);
1976 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1978 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1979 struct trace_array *tr = inode->i_private;
1982 ret = tracing_check_open_get_tr(tr);
1986 if ((file->f_mode & FMODE_WRITE) &&
1987 (file->f_flags & O_TRUNC))
1988 ftrace_clear_event_pids(tr, TRACE_PIDS);
1990 ret = ftrace_event_open(inode, file, seq_ops);
1992 trace_array_put(tr);
1997 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
1999 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2000 struct trace_array *tr = inode->i_private;
2003 ret = tracing_check_open_get_tr(tr);
2007 if ((file->f_mode & FMODE_WRITE) &&
2008 (file->f_flags & O_TRUNC))
2009 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2011 ret = ftrace_event_open(inode, file, seq_ops);
2013 trace_array_put(tr);
2017 static struct event_subsystem *
2018 create_new_subsystem(const char *name)
2020 struct event_subsystem *system;
2022 /* need to create new entry */
2023 system = kmalloc(sizeof(*system), GFP_KERNEL);
2027 system->ref_count = 1;
2029 /* Only allocate if dynamic (kprobes and modules) */
2030 system->name = kstrdup_const(name, GFP_KERNEL);
2034 system->filter = NULL;
2036 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2037 if (!system->filter)
2040 list_add(&system->list, &event_subsystems);
2045 kfree_const(system->name);
2050 static struct dentry *
2051 event_subsystem_dir(struct trace_array *tr, const char *name,
2052 struct trace_event_file *file, struct dentry *parent)
2054 struct trace_subsystem_dir *dir;
2055 struct event_subsystem *system;
2056 struct dentry *entry;
2058 /* First see if we did not already create this dir */
2059 list_for_each_entry(dir, &tr->systems, list) {
2060 system = dir->subsystem;
2061 if (strcmp(system->name, name) == 0) {
2068 /* Now see if the system itself exists. */
2069 list_for_each_entry(system, &event_subsystems, list) {
2070 if (strcmp(system->name, name) == 0)
2073 /* Reset system variable when not found */
2074 if (&system->list == &event_subsystems)
2077 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2082 system = create_new_subsystem(name);
2086 __get_system(system);
2088 dir->entry = tracefs_create_dir(name, parent);
2090 pr_warn("Failed to create system directory %s\n", name);
2091 __put_system(system);
2098 dir->subsystem = system;
2101 /* the ftrace system is special, do not create enable or filter files */
2102 if (strcmp(name, "ftrace") != 0) {
2104 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2105 &ftrace_subsystem_filter_fops);
2107 kfree(system->filter);
2108 system->filter = NULL;
2109 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2112 trace_create_file("enable", 0644, dir->entry, dir,
2113 &ftrace_system_enable_fops);
2116 list_add(&dir->list, &tr->systems);
2123 /* Only print this message if failed on memory allocation */
2124 if (!dir || !system)
2125 pr_warn("No memory to create event subsystem %s\n", name);
2130 event_define_fields(struct trace_event_call *call)
2132 struct list_head *head;
2136 * Other events may have the same class. Only update
2137 * the fields if they are not already defined.
2139 head = trace_get_fields(call);
2140 if (list_empty(head)) {
2141 struct trace_event_fields *field = call->class->fields_array;
2142 unsigned int offset = sizeof(struct trace_entry);
2144 for (; field->type; field++) {
2145 if (field->type == TRACE_FUNCTION_TYPE) {
2146 field->define_fields(call);
2150 offset = ALIGN(offset, field->align);
2151 ret = trace_define_field(call, field->type, field->name,
2152 offset, field->size,
2153 field->is_signed, field->filter_type);
2154 if (WARN_ON_ONCE(ret)) {
2155 pr_err("error code is %d\n", ret);
2159 offset += field->size;
2167 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2169 struct trace_event_call *call = file->event_call;
2170 struct trace_array *tr = file->tr;
2171 struct dentry *d_events;
2176 * If the trace point header did not define TRACE_SYSTEM
2177 * then the system would be called "TRACE_SYSTEM".
2179 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2180 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2186 name = trace_event_name(call);
2187 file->dir = tracefs_create_dir(name, d_events);
2189 pr_warn("Could not create tracefs '%s' directory\n", name);
2193 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2194 trace_create_file("enable", 0644, file->dir, file,
2195 &ftrace_enable_fops);
2197 #ifdef CONFIG_PERF_EVENTS
2198 if (call->event.type && call->class->reg)
2199 trace_create_file("id", 0444, file->dir,
2200 (void *)(long)call->event.type,
2201 &ftrace_event_id_fops);
2204 ret = event_define_fields(call);
2206 pr_warn("Could not initialize trace point events/%s\n", name);
2211 * Only event directories that can be enabled should have
2212 * triggers or filters.
2214 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2215 trace_create_file("filter", 0644, file->dir, file,
2216 &ftrace_event_filter_fops);
2218 trace_create_file("trigger", 0644, file->dir, file,
2219 &event_trigger_fops);
2222 #ifdef CONFIG_HIST_TRIGGERS
2223 trace_create_file("hist", 0444, file->dir, file,
2226 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2227 trace_create_file("hist_debug", 0444, file->dir, file,
2228 &event_hist_debug_fops);
2230 trace_create_file("format", 0444, file->dir, call,
2231 &ftrace_event_format_fops);
2233 #ifdef CONFIG_TRACE_EVENT_INJECT
2234 if (call->event.type && call->class->reg)
2235 trace_create_file("inject", 0200, file->dir, file,
2236 &event_inject_fops);
2242 static void remove_event_from_tracers(struct trace_event_call *call)
2244 struct trace_event_file *file;
2245 struct trace_array *tr;
2247 do_for_each_event_file_safe(tr, file) {
2248 if (file->event_call != call)
2251 remove_event_file_dir(file);
2253 * The do_for_each_event_file_safe() is
2254 * a double loop. After finding the call for this
2255 * trace_array, we use break to jump to the next
2259 } while_for_each_event_file();
2262 static void event_remove(struct trace_event_call *call)
2264 struct trace_array *tr;
2265 struct trace_event_file *file;
2267 do_for_each_event_file(tr, file) {
2268 if (file->event_call != call)
2271 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2272 tr->clear_trace = true;
2274 ftrace_event_enable_disable(file, 0);
2276 * The do_for_each_event_file() is
2277 * a double loop. After finding the call for this
2278 * trace_array, we use break to jump to the next
2282 } while_for_each_event_file();
2284 if (call->event.funcs)
2285 __unregister_trace_event(&call->event);
2286 remove_event_from_tracers(call);
2287 list_del(&call->list);
2290 static int event_init(struct trace_event_call *call)
2295 name = trace_event_name(call);
2299 if (call->class->raw_init) {
2300 ret = call->class->raw_init(call);
2301 if (ret < 0 && ret != -ENOSYS)
2302 pr_warn("Could not initialize trace events/%s\n", name);
2309 __register_event(struct trace_event_call *call, struct module *mod)
2313 ret = event_init(call);
2317 list_add(&call->list, &ftrace_events);
2323 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2328 /* Find the length of the eval value as a string */
2329 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2330 /* Make sure there's enough room to replace the string with the value */
2334 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2336 /* Get the rest of the string of ptr */
2337 rlen = strlen(ptr + len);
2338 memmove(ptr + elen, ptr + len, rlen);
2339 /* Make sure we end the new string */
2340 ptr[elen + rlen] = 0;
2345 static void update_event_printk(struct trace_event_call *call,
2346 struct trace_eval_map *map)
2350 int len = strlen(map->eval_string);
2352 for (ptr = call->print_fmt; *ptr; ptr++) {
2366 if (isdigit(*ptr)) {
2370 /* Check for alpha chars like ULL */
2371 } while (isalnum(*ptr));
2375 * A number must have some kind of delimiter after
2376 * it, and we can ignore that too.
2380 if (isalpha(*ptr) || *ptr == '_') {
2381 if (strncmp(map->eval_string, ptr, len) == 0 &&
2382 !isalnum(ptr[len]) && ptr[len] != '_') {
2383 ptr = eval_replace(ptr, map, len);
2384 /* enum/sizeof string smaller than value */
2385 if (WARN_ON_ONCE(!ptr))
2388 * No need to decrement here, as eval_replace()
2389 * returns the pointer to the character passed
2390 * the eval, and two evals can not be placed
2391 * back to back without something in between.
2392 * We can skip that something in between.
2399 } while (isalnum(*ptr) || *ptr == '_');
2403 * If what comes after this variable is a '.' or
2404 * '->' then we can continue to ignore that string.
2406 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2407 ptr += *ptr == '.' ? 1 : 2;
2413 * Once again, we can skip the delimiter that came
2421 void trace_event_eval_update(struct trace_eval_map **map, int len)
2423 struct trace_event_call *call, *p;
2424 const char *last_system = NULL;
2429 down_write(&trace_event_sem);
2430 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2431 /* events are usually grouped together with systems */
2432 if (!last_system || call->class->system != last_system) {
2435 last_system = call->class->system;
2439 * Since calls are grouped by systems, the likelyhood that the
2440 * next call in the iteration belongs to the same system as the
2441 * previous call is high. As an optimization, we skip searching
2442 * for a map[] that matches the call's system if the last call
2443 * was from the same system. That's what last_i is for. If the
2444 * call has the same system as the previous call, then last_i
2445 * will be the index of the first map[] that has a matching
2448 for (i = last_i; i < len; i++) {
2449 if (call->class->system == map[i]->system) {
2450 /* Save the first system if need be */
2455 update_event_printk(call, map[i]);
2459 up_write(&trace_event_sem);
2462 static struct trace_event_file *
2463 trace_create_new_event(struct trace_event_call *call,
2464 struct trace_array *tr)
2466 struct trace_event_file *file;
2468 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2472 file->event_call = call;
2474 atomic_set(&file->sm_ref, 0);
2475 atomic_set(&file->tm_ref, 0);
2476 INIT_LIST_HEAD(&file->triggers);
2477 list_add(&file->list, &tr->events);
2482 /* Add an event to a trace directory */
2484 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2486 struct trace_event_file *file;
2488 file = trace_create_new_event(call, tr);
2492 if (eventdir_initialized)
2493 return event_create_dir(tr->event_dir, file);
2495 return event_define_fields(call);
2499 * Just create a decriptor for early init. A descriptor is required
2500 * for enabling events at boot. We want to enable events before
2501 * the filesystem is initialized.
2504 __trace_early_add_new_event(struct trace_event_call *call,
2505 struct trace_array *tr)
2507 struct trace_event_file *file;
2509 file = trace_create_new_event(call, tr);
2513 return event_define_fields(call);
2516 struct ftrace_module_file_ops;
2517 static void __add_event_to_tracers(struct trace_event_call *call);
2519 /* Add an additional event_call dynamically */
2520 int trace_add_event_call(struct trace_event_call *call)
2523 lockdep_assert_held(&event_mutex);
2525 mutex_lock(&trace_types_lock);
2527 ret = __register_event(call, NULL);
2529 __add_event_to_tracers(call);
2531 mutex_unlock(&trace_types_lock);
2536 * Must be called under locking of trace_types_lock, event_mutex and
2539 static void __trace_remove_event_call(struct trace_event_call *call)
2542 trace_destroy_fields(call);
2543 free_event_filter(call->filter);
2544 call->filter = NULL;
2547 static int probe_remove_event_call(struct trace_event_call *call)
2549 struct trace_array *tr;
2550 struct trace_event_file *file;
2552 #ifdef CONFIG_PERF_EVENTS
2553 if (call->perf_refcount)
2556 do_for_each_event_file(tr, file) {
2557 if (file->event_call != call)
2560 * We can't rely on ftrace_event_enable_disable(enable => 0)
2561 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2562 * TRACE_REG_UNREGISTER.
2564 if (file->flags & EVENT_FILE_FL_ENABLED)
2567 * The do_for_each_event_file_safe() is
2568 * a double loop. After finding the call for this
2569 * trace_array, we use break to jump to the next
2573 } while_for_each_event_file();
2575 __trace_remove_event_call(call);
2580 /* Remove an event_call */
2581 int trace_remove_event_call(struct trace_event_call *call)
2585 lockdep_assert_held(&event_mutex);
2587 mutex_lock(&trace_types_lock);
2588 down_write(&trace_event_sem);
2589 ret = probe_remove_event_call(call);
2590 up_write(&trace_event_sem);
2591 mutex_unlock(&trace_types_lock);
2596 #define for_each_event(event, start, end) \
2597 for (event = start; \
2598 (unsigned long)event < (unsigned long)end; \
2601 #ifdef CONFIG_MODULES
2603 static void trace_module_add_events(struct module *mod)
2605 struct trace_event_call **call, **start, **end;
2607 if (!mod->num_trace_events)
2610 /* Don't add infrastructure for mods without tracepoints */
2611 if (trace_module_has_bad_taint(mod)) {
2612 pr_err("%s: module has bad taint, not creating trace events\n",
2617 start = mod->trace_events;
2618 end = mod->trace_events + mod->num_trace_events;
2620 for_each_event(call, start, end) {
2621 __register_event(*call, mod);
2622 __add_event_to_tracers(*call);
2626 static void trace_module_remove_events(struct module *mod)
2628 struct trace_event_call *call, *p;
2630 down_write(&trace_event_sem);
2631 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2632 if (call->mod == mod)
2633 __trace_remove_event_call(call);
2635 up_write(&trace_event_sem);
2638 * It is safest to reset the ring buffer if the module being unloaded
2639 * registered any events that were used. The only worry is if
2640 * a new module gets loaded, and takes on the same id as the events
2641 * of this module. When printing out the buffer, traced events left
2642 * over from this module may be passed to the new module events and
2643 * unexpected results may occur.
2645 tracing_reset_all_online_cpus();
2648 static int trace_module_notify(struct notifier_block *self,
2649 unsigned long val, void *data)
2651 struct module *mod = data;
2653 mutex_lock(&event_mutex);
2654 mutex_lock(&trace_types_lock);
2656 case MODULE_STATE_COMING:
2657 trace_module_add_events(mod);
2659 case MODULE_STATE_GOING:
2660 trace_module_remove_events(mod);
2663 mutex_unlock(&trace_types_lock);
2664 mutex_unlock(&event_mutex);
2669 static struct notifier_block trace_module_nb = {
2670 .notifier_call = trace_module_notify,
2671 .priority = 1, /* higher than trace.c module notify */
2673 #endif /* CONFIG_MODULES */
2675 /* Create a new event directory structure for a trace directory. */
2677 __trace_add_event_dirs(struct trace_array *tr)
2679 struct trace_event_call *call;
2682 list_for_each_entry(call, &ftrace_events, list) {
2683 ret = __trace_add_new_event(call, tr);
2685 pr_warn("Could not create directory for event %s\n",
2686 trace_event_name(call));
2690 /* Returns any file that matches the system and event */
2691 struct trace_event_file *
2692 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2694 struct trace_event_file *file;
2695 struct trace_event_call *call;
2698 list_for_each_entry(file, &tr->events, list) {
2700 call = file->event_call;
2701 name = trace_event_name(call);
2703 if (!name || !call->class)
2706 if (strcmp(event, name) == 0 &&
2707 strcmp(system, call->class->system) == 0)
2713 /* Returns valid trace event files that match system and event */
2714 struct trace_event_file *
2715 find_event_file(struct trace_array *tr, const char *system, const char *event)
2717 struct trace_event_file *file;
2719 file = __find_event_file(tr, system, event);
2720 if (!file || !file->event_call->class->reg ||
2721 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2728 * trace_get_event_file - Find and return a trace event file
2729 * @instance: The name of the trace instance containing the event
2730 * @system: The name of the system containing the event
2731 * @event: The name of the event
2733 * Return a trace event file given the trace instance name, trace
2734 * system, and trace event name. If the instance name is NULL, it
2735 * refers to the top-level trace array.
2737 * This function will look it up and return it if found, after calling
2738 * trace_array_get() to prevent the instance from going away, and
2739 * increment the event's module refcount to prevent it from being
2742 * To release the file, call trace_put_event_file(), which will call
2743 * trace_array_put() and decrement the event's module refcount.
2745 * Return: The trace event on success, ERR_PTR otherwise.
2747 struct trace_event_file *trace_get_event_file(const char *instance,
2751 struct trace_array *tr = top_trace_array();
2752 struct trace_event_file *file = NULL;
2756 tr = trace_array_find_get(instance);
2758 return ERR_PTR(-ENOENT);
2760 ret = trace_array_get(tr);
2762 return ERR_PTR(ret);
2765 mutex_lock(&event_mutex);
2767 file = find_event_file(tr, system, event);
2769 trace_array_put(tr);
2774 /* Don't let event modules unload while in use */
2775 ret = try_module_get(file->event_call->mod);
2777 trace_array_put(tr);
2784 mutex_unlock(&event_mutex);
2787 file = ERR_PTR(ret);
2791 EXPORT_SYMBOL_GPL(trace_get_event_file);
2794 * trace_put_event_file - Release a file from trace_get_event_file()
2795 * @file: The trace event file
2797 * If a file was retrieved using trace_get_event_file(), this should
2798 * be called when it's no longer needed. It will cancel the previous
2799 * trace_array_get() called by that function, and decrement the
2800 * event's module refcount.
2802 void trace_put_event_file(struct trace_event_file *file)
2804 mutex_lock(&event_mutex);
2805 module_put(file->event_call->mod);
2806 mutex_unlock(&event_mutex);
2808 trace_array_put(file->tr);
2810 EXPORT_SYMBOL_GPL(trace_put_event_file);
2812 #ifdef CONFIG_DYNAMIC_FTRACE
2815 #define ENABLE_EVENT_STR "enable_event"
2816 #define DISABLE_EVENT_STR "disable_event"
2818 struct event_probe_data {
2819 struct trace_event_file *file;
2820 unsigned long count;
2825 static void update_event_probe(struct event_probe_data *data)
2828 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2830 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2834 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2835 struct trace_array *tr, struct ftrace_probe_ops *ops,
2838 struct ftrace_func_mapper *mapper = data;
2839 struct event_probe_data *edata;
2842 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2843 if (!pdata || !*pdata)
2847 update_event_probe(edata);
2851 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2852 struct trace_array *tr, struct ftrace_probe_ops *ops,
2855 struct ftrace_func_mapper *mapper = data;
2856 struct event_probe_data *edata;
2859 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2860 if (!pdata || !*pdata)
2868 /* Skip if the event is in a state we want to switch to */
2869 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2872 if (edata->count != -1)
2875 update_event_probe(edata);
2879 event_enable_print(struct seq_file *m, unsigned long ip,
2880 struct ftrace_probe_ops *ops, void *data)
2882 struct ftrace_func_mapper *mapper = data;
2883 struct event_probe_data *edata;
2886 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2888 if (WARN_ON_ONCE(!pdata || !*pdata))
2893 seq_printf(m, "%ps:", (void *)ip);
2895 seq_printf(m, "%s:%s:%s",
2896 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2897 edata->file->event_call->class->system,
2898 trace_event_name(edata->file->event_call));
2900 if (edata->count == -1)
2901 seq_puts(m, ":unlimited\n");
2903 seq_printf(m, ":count=%ld\n", edata->count);
2909 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2910 unsigned long ip, void *init_data, void **data)
2912 struct ftrace_func_mapper *mapper = *data;
2913 struct event_probe_data *edata = init_data;
2917 mapper = allocate_ftrace_func_mapper();
2923 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2932 static int free_probe_data(void *data)
2934 struct event_probe_data *edata = data;
2938 /* Remove the SOFT_MODE flag */
2939 __ftrace_event_enable_disable(edata->file, 0, 1);
2940 module_put(edata->file->event_call->mod);
2947 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2948 unsigned long ip, void *data)
2950 struct ftrace_func_mapper *mapper = data;
2951 struct event_probe_data *edata;
2956 free_ftrace_func_mapper(mapper, free_probe_data);
2960 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2962 if (WARN_ON_ONCE(!edata))
2965 if (WARN_ON_ONCE(edata->ref <= 0))
2968 free_probe_data(edata);
2971 static struct ftrace_probe_ops event_enable_probe_ops = {
2972 .func = event_enable_probe,
2973 .print = event_enable_print,
2974 .init = event_enable_init,
2975 .free = event_enable_free,
2978 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2979 .func = event_enable_count_probe,
2980 .print = event_enable_print,
2981 .init = event_enable_init,
2982 .free = event_enable_free,
2985 static struct ftrace_probe_ops event_disable_probe_ops = {
2986 .func = event_enable_probe,
2987 .print = event_enable_print,
2988 .init = event_enable_init,
2989 .free = event_enable_free,
2992 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2993 .func = event_enable_count_probe,
2994 .print = event_enable_print,
2995 .init = event_enable_init,
2996 .free = event_enable_free,
3000 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3001 char *glob, char *cmd, char *param, int enabled)
3003 struct trace_event_file *file;
3004 struct ftrace_probe_ops *ops;
3005 struct event_probe_data *data;
3015 /* hash funcs only work with set_ftrace_filter */
3016 if (!enabled || !param)
3019 system = strsep(¶m, ":");
3023 event = strsep(¶m, ":");
3025 mutex_lock(&event_mutex);
3028 file = find_event_file(tr, system, event);
3032 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3035 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3037 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3039 if (glob[0] == '!') {
3040 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3046 data = kzalloc(sizeof(*data), GFP_KERNEL);
3050 data->enable = enable;
3057 number = strsep(¶m, ":");
3060 if (!strlen(number))
3064 * We use the callback data field (which is a pointer)
3067 ret = kstrtoul(number, 0, &data->count);
3072 /* Don't let event modules unload while probe registered */
3073 ret = try_module_get(file->event_call->mod);
3079 ret = __ftrace_event_enable_disable(file, 1, 1);
3083 ret = register_ftrace_function_probe(glob, tr, ops, data);
3085 * The above returns on success the # of functions enabled,
3086 * but if it didn't find any functions it returns zero.
3087 * Consider no functions a failure too.
3094 /* Just return zero, not the number of enabled functions */
3097 mutex_unlock(&event_mutex);
3101 __ftrace_event_enable_disable(file, 0, 1);
3103 module_put(file->event_call->mod);
3109 static struct ftrace_func_command event_enable_cmd = {
3110 .name = ENABLE_EVENT_STR,
3111 .func = event_enable_func,
3114 static struct ftrace_func_command event_disable_cmd = {
3115 .name = DISABLE_EVENT_STR,
3116 .func = event_enable_func,
3119 static __init int register_event_cmds(void)
3123 ret = register_ftrace_command(&event_enable_cmd);
3124 if (WARN_ON(ret < 0))
3126 ret = register_ftrace_command(&event_disable_cmd);
3127 if (WARN_ON(ret < 0))
3128 unregister_ftrace_command(&event_enable_cmd);
3132 static inline int register_event_cmds(void) { return 0; }
3133 #endif /* CONFIG_DYNAMIC_FTRACE */
3136 * The top level array and trace arrays created by boot-time tracing
3137 * have already had its trace_event_file descriptors created in order
3138 * to allow for early events to be recorded.
3139 * This function is called after the tracefs has been initialized,
3140 * and we now have to create the files associated to the events.
3142 static void __trace_early_add_event_dirs(struct trace_array *tr)
3144 struct trace_event_file *file;
3148 list_for_each_entry(file, &tr->events, list) {
3149 ret = event_create_dir(tr->event_dir, file);
3151 pr_warn("Could not create directory for event %s\n",
3152 trace_event_name(file->event_call));
3157 * For early boot up, the top trace array and the trace arrays created
3158 * by boot-time tracing require to have a list of events that can be
3159 * enabled. This must be done before the filesystem is set up in order
3160 * to allow events to be traced early.
3162 void __trace_early_add_events(struct trace_array *tr)
3164 struct trace_event_call *call;
3167 list_for_each_entry(call, &ftrace_events, list) {
3168 /* Early boot up should not have any modules loaded */
3169 if (WARN_ON_ONCE(call->mod))
3172 ret = __trace_early_add_new_event(call, tr);
3174 pr_warn("Could not create early event %s\n",
3175 trace_event_name(call));
3179 /* Remove the event directory structure for a trace directory. */
3181 __trace_remove_event_dirs(struct trace_array *tr)
3183 struct trace_event_file *file, *next;
3185 list_for_each_entry_safe(file, next, &tr->events, list)
3186 remove_event_file_dir(file);
3189 static void __add_event_to_tracers(struct trace_event_call *call)
3191 struct trace_array *tr;
3193 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3194 __trace_add_new_event(call, tr);
3197 extern struct trace_event_call *__start_ftrace_events[];
3198 extern struct trace_event_call *__stop_ftrace_events[];
3200 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3202 static __init int setup_trace_event(char *str)
3204 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3205 ring_buffer_expanded = true;
3206 disable_tracing_selftest("running event tracing");
3210 __setup("trace_event=", setup_trace_event);
3212 /* Expects to have event_mutex held when called */
3214 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3216 struct dentry *d_events;
3217 struct dentry *entry;
3219 entry = tracefs_create_file("set_event", 0644, parent,
3220 tr, &ftrace_set_event_fops);
3222 pr_warn("Could not create tracefs 'set_event' entry\n");
3226 d_events = tracefs_create_dir("events", parent);
3228 pr_warn("Could not create tracefs 'events' directory\n");
3232 entry = trace_create_file("enable", 0644, d_events,
3233 tr, &ftrace_tr_enable_fops);
3235 pr_warn("Could not create tracefs 'enable' entry\n");
3239 /* There are not as crucial, just warn if they are not created */
3241 entry = tracefs_create_file("set_event_pid", 0644, parent,
3242 tr, &ftrace_set_event_pid_fops);
3244 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3246 entry = tracefs_create_file("set_event_notrace_pid", 0644, parent,
3247 tr, &ftrace_set_event_notrace_pid_fops);
3249 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
3251 /* ring buffer internal formats */
3252 entry = trace_create_file("header_page", 0444, d_events,
3253 ring_buffer_print_page_header,
3254 &ftrace_show_header_fops);
3256 pr_warn("Could not create tracefs 'header_page' entry\n");
3258 entry = trace_create_file("header_event", 0444, d_events,
3259 ring_buffer_print_entry_header,
3260 &ftrace_show_header_fops);
3262 pr_warn("Could not create tracefs 'header_event' entry\n");
3264 tr->event_dir = d_events;
3270 * event_trace_add_tracer - add a instance of a trace_array to events
3271 * @parent: The parent dentry to place the files/directories for events in
3272 * @tr: The trace array associated with these events
3274 * When a new instance is created, it needs to set up its events
3275 * directory, as well as other files associated with events. It also
3276 * creates the event hierarchy in the @parent/events directory.
3278 * Returns 0 on success.
3280 * Must be called with event_mutex held.
3282 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3286 lockdep_assert_held(&event_mutex);
3288 ret = create_event_toplevel_files(parent, tr);
3292 down_write(&trace_event_sem);
3293 /* If tr already has the event list, it is initialized in early boot. */
3294 if (unlikely(!list_empty(&tr->events)))
3295 __trace_early_add_event_dirs(tr);
3297 __trace_add_event_dirs(tr);
3298 up_write(&trace_event_sem);
3305 * The top trace array already had its file descriptors created.
3306 * Now the files themselves need to be created.
3309 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3313 mutex_lock(&event_mutex);
3315 ret = create_event_toplevel_files(parent, tr);
3319 down_write(&trace_event_sem);
3320 __trace_early_add_event_dirs(tr);
3321 up_write(&trace_event_sem);
3324 mutex_unlock(&event_mutex);
3329 /* Must be called with event_mutex held */
3330 int event_trace_del_tracer(struct trace_array *tr)
3332 lockdep_assert_held(&event_mutex);
3334 /* Disable any event triggers and associated soft-disabled events */
3335 clear_event_triggers(tr);
3337 /* Clear the pid list */
3338 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3340 /* Disable any running events */
3341 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3343 /* Make sure no more events are being executed */
3344 tracepoint_synchronize_unregister();
3346 down_write(&trace_event_sem);
3347 __trace_remove_event_dirs(tr);
3348 tracefs_remove(tr->event_dir);
3349 up_write(&trace_event_sem);
3351 tr->event_dir = NULL;
3356 static __init int event_trace_memsetup(void)
3358 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3359 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3364 early_enable_events(struct trace_array *tr, bool disable_first)
3366 char *buf = bootup_event_buf;
3371 token = strsep(&buf, ",");
3377 /* Restarting syscalls requires that we stop them first */
3379 ftrace_set_clr_event(tr, token, 0);
3381 ret = ftrace_set_clr_event(tr, token, 1);
3383 pr_warn("Failed to enable trace event: %s\n", token);
3386 /* Put back the comma to allow this to be called again */
3392 static __init int event_trace_enable(void)
3394 struct trace_array *tr = top_trace_array();
3395 struct trace_event_call **iter, *call;
3401 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3404 ret = event_init(call);
3406 list_add(&call->list, &ftrace_events);
3410 * We need the top trace array to have a working set of trace
3411 * points at early init, before the debug files and directories
3412 * are created. Create the file entries now, and attach them
3413 * to the actual file dentries later.
3415 __trace_early_add_events(tr);
3417 early_enable_events(tr, false);
3419 trace_printk_start_comm();
3421 register_event_cmds();
3423 register_trigger_cmds();
3429 * event_trace_enable() is called from trace_event_init() first to
3430 * initialize events and perhaps start any events that are on the
3431 * command line. Unfortunately, there are some events that will not
3432 * start this early, like the system call tracepoints that need
3433 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
3434 * event_trace_enable() is called before pid 1 starts, and this flag
3435 * is never set, making the syscall tracepoint never get reached, but
3436 * the event is enabled regardless (and not doing anything).
3438 static __init int event_trace_enable_again(void)
3440 struct trace_array *tr;
3442 tr = top_trace_array();
3446 early_enable_events(tr, true);
3451 early_initcall(event_trace_enable_again);
3453 /* Init fields which doesn't related to the tracefs */
3454 static __init int event_trace_init_fields(void)
3456 if (trace_define_generic_fields())
3457 pr_warn("tracing: Failed to allocated generic fields");
3459 if (trace_define_common_fields())
3460 pr_warn("tracing: Failed to allocate common fields");
3465 __init int event_trace_init(void)
3467 struct trace_array *tr;
3468 struct dentry *entry;
3471 tr = top_trace_array();
3475 entry = tracefs_create_file("available_events", 0444, NULL,
3476 tr, &ftrace_avail_fops);
3478 pr_warn("Could not create tracefs 'available_events' entry\n");
3480 ret = early_event_add_tracer(NULL, tr);
3484 #ifdef CONFIG_MODULES
3485 ret = register_module_notifier(&trace_module_nb);
3487 pr_warn("Failed to register trace events module notifier\n");
3490 eventdir_initialized = true;
3495 void __init trace_event_init(void)
3497 event_trace_memsetup();
3498 init_ftrace_syscalls();
3499 event_trace_enable();
3500 event_trace_init_fields();
3503 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3505 static DEFINE_SPINLOCK(test_spinlock);
3506 static DEFINE_SPINLOCK(test_spinlock_irq);
3507 static DEFINE_MUTEX(test_mutex);
3509 static __init void test_work(struct work_struct *dummy)
3511 spin_lock(&test_spinlock);
3512 spin_lock_irq(&test_spinlock_irq);
3514 spin_unlock_irq(&test_spinlock_irq);
3515 spin_unlock(&test_spinlock);
3517 mutex_lock(&test_mutex);
3519 mutex_unlock(&test_mutex);
3522 static __init int event_test_thread(void *unused)
3526 test_malloc = kmalloc(1234, GFP_KERNEL);
3528 pr_info("failed to kmalloc\n");
3530 schedule_on_each_cpu(test_work);
3534 set_current_state(TASK_INTERRUPTIBLE);
3535 while (!kthread_should_stop()) {
3537 set_current_state(TASK_INTERRUPTIBLE);
3539 __set_current_state(TASK_RUNNING);
3545 * Do various things that may trigger events.
3547 static __init void event_test_stuff(void)
3549 struct task_struct *test_thread;
3551 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3553 kthread_stop(test_thread);
3557 * For every trace event defined, we will test each trace point separately,
3558 * and then by groups, and finally all trace points.
3560 static __init void event_trace_self_tests(void)
3562 struct trace_subsystem_dir *dir;
3563 struct trace_event_file *file;
3564 struct trace_event_call *call;
3565 struct event_subsystem *system;
3566 struct trace_array *tr;
3569 tr = top_trace_array();
3573 pr_info("Running tests on trace events:\n");
3575 list_for_each_entry(file, &tr->events, list) {
3577 call = file->event_call;
3579 /* Only test those that have a probe */
3580 if (!call->class || !call->class->probe)
3584 * Testing syscall events here is pretty useless, but
3585 * we still do it if configured. But this is time consuming.
3586 * What we really need is a user thread to perform the
3587 * syscalls as we test.
3589 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3590 if (call->class->system &&
3591 strcmp(call->class->system, "syscalls") == 0)
3595 pr_info("Testing event %s: ", trace_event_name(call));
3598 * If an event is already enabled, someone is using
3599 * it and the self test should not be on.
3601 if (file->flags & EVENT_FILE_FL_ENABLED) {
3602 pr_warn("Enabled event during self test!\n");
3607 ftrace_event_enable_disable(file, 1);
3609 ftrace_event_enable_disable(file, 0);
3614 /* Now test at the sub system level */
3616 pr_info("Running tests on trace event systems:\n");
3618 list_for_each_entry(dir, &tr->systems, list) {
3620 system = dir->subsystem;
3622 /* the ftrace system is special, skip it */
3623 if (strcmp(system->name, "ftrace") == 0)
3626 pr_info("Testing event system %s: ", system->name);
3628 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3629 if (WARN_ON_ONCE(ret)) {
3630 pr_warn("error enabling system %s\n",
3637 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3638 if (WARN_ON_ONCE(ret)) {
3639 pr_warn("error disabling system %s\n",
3647 /* Test with all events enabled */
3649 pr_info("Running tests on all trace events:\n");
3650 pr_info("Testing all events: ");
3652 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3653 if (WARN_ON_ONCE(ret)) {
3654 pr_warn("error enabling all events\n");
3661 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3662 if (WARN_ON_ONCE(ret)) {
3663 pr_warn("error disabling all events\n");
3670 #ifdef CONFIG_FUNCTION_TRACER
3672 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3674 static struct trace_event_file event_trace_file __initdata;
3677 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3678 struct ftrace_ops *op, struct ftrace_regs *regs)
3680 struct trace_buffer *buffer;
3681 struct ring_buffer_event *event;
3682 struct ftrace_entry *entry;
3683 unsigned int trace_ctx;
3687 trace_ctx = tracing_gen_ctx();
3688 preempt_disable_notrace();
3689 cpu = raw_smp_processor_id();
3690 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3695 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3696 TRACE_FN, sizeof(*entry),
3700 entry = ring_buffer_event_data(event);
3702 entry->parent_ip = parent_ip;
3704 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3707 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3708 preempt_enable_notrace();
3711 static struct ftrace_ops trace_ops __initdata =
3713 .func = function_test_events_call,
3716 static __init void event_trace_self_test_with_function(void)
3720 event_trace_file.tr = top_trace_array();
3721 if (WARN_ON(!event_trace_file.tr))
3724 ret = register_ftrace_function(&trace_ops);
3725 if (WARN_ON(ret < 0)) {
3726 pr_info("Failed to enable function tracer for event tests\n");
3729 pr_info("Running tests again, along with the function tracer\n");
3730 event_trace_self_tests();
3731 unregister_ftrace_function(&trace_ops);
3734 static __init void event_trace_self_test_with_function(void)
3739 static __init int event_trace_self_tests_init(void)
3741 if (!tracing_selftest_disabled) {
3742 event_trace_self_tests();
3743 event_trace_self_test_with_function();
3749 late_initcall(event_trace_self_tests_init);