drm/panfrost: Remove set but not used variable 'bo'
[linux/fpc-iii.git] / kernel / trace / trace_events.c
blobf38234ecea185ec0f29b393f166dccd7e9d5b046
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * event tracer
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"
33 #undef TRACE_SYSTEM
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
36 DEFINE_MUTEX(event_mutex);
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
42 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
44 static struct kmem_cache *field_cachep;
45 static struct kmem_cache *file_cachep;
47 static inline int system_refcount(struct event_subsystem *system)
49 return system->ref_count;
52 static int system_refcount_inc(struct event_subsystem *system)
54 return system->ref_count++;
57 static int system_refcount_dec(struct event_subsystem *system)
59 return --system->ref_count;
62 /* Double loops, do not use break, only goto's work */
63 #define do_for_each_event_file(tr, file) \
64 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
65 list_for_each_entry(file, &tr->events, list)
67 #define do_for_each_event_file_safe(tr, file) \
68 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
69 struct trace_event_file *___n; \
70 list_for_each_entry_safe(file, ___n, &tr->events, list)
72 #define while_for_each_event_file() \
75 static struct ftrace_event_field *
76 __find_event_field(struct list_head *head, char *name)
78 struct ftrace_event_field *field;
80 list_for_each_entry(field, head, link) {
81 if (!strcmp(field->name, name))
82 return field;
85 return NULL;
88 struct ftrace_event_field *
89 trace_find_event_field(struct trace_event_call *call, char *name)
91 struct ftrace_event_field *field;
92 struct list_head *head;
94 head = trace_get_fields(call);
95 field = __find_event_field(head, name);
96 if (field)
97 return field;
99 field = __find_event_field(&ftrace_generic_fields, name);
100 if (field)
101 return field;
103 return __find_event_field(&ftrace_common_fields, name);
106 static int __trace_define_field(struct list_head *head, const char *type,
107 const char *name, int offset, int size,
108 int is_signed, int filter_type)
110 struct ftrace_event_field *field;
112 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
113 if (!field)
114 return -ENOMEM;
116 field->name = name;
117 field->type = type;
119 if (filter_type == FILTER_OTHER)
120 field->filter_type = filter_assign_type(type);
121 else
122 field->filter_type = filter_type;
124 field->offset = offset;
125 field->size = size;
126 field->is_signed = is_signed;
128 list_add(&field->link, head);
130 return 0;
133 int trace_define_field(struct trace_event_call *call, const char *type,
134 const char *name, int offset, int size, int is_signed,
135 int filter_type)
137 struct list_head *head;
139 if (WARN_ON(!call->class))
140 return 0;
142 head = trace_get_fields(call);
143 return __trace_define_field(head, type, name, offset, size,
144 is_signed, filter_type);
146 EXPORT_SYMBOL_GPL(trace_define_field);
148 #define __generic_field(type, item, filter_type) \
149 ret = __trace_define_field(&ftrace_generic_fields, #type, \
150 #item, 0, 0, is_signed_type(type), \
151 filter_type); \
152 if (ret) \
153 return ret;
155 #define __common_field(type, item) \
156 ret = __trace_define_field(&ftrace_common_fields, #type, \
157 "common_" #item, \
158 offsetof(typeof(ent), item), \
159 sizeof(ent.item), \
160 is_signed_type(type), FILTER_OTHER); \
161 if (ret) \
162 return ret;
164 static int trace_define_generic_fields(void)
166 int ret;
168 __generic_field(int, CPU, FILTER_CPU);
169 __generic_field(int, cpu, FILTER_CPU);
170 __generic_field(char *, COMM, FILTER_COMM);
171 __generic_field(char *, comm, FILTER_COMM);
173 return ret;
176 static int trace_define_common_fields(void)
178 int ret;
179 struct trace_entry ent;
181 __common_field(unsigned short, type);
182 __common_field(unsigned char, flags);
183 __common_field(unsigned char, preempt_count);
184 __common_field(int, pid);
186 return ret;
189 static void trace_destroy_fields(struct trace_event_call *call)
191 struct ftrace_event_field *field, *next;
192 struct list_head *head;
194 head = trace_get_fields(call);
195 list_for_each_entry_safe(field, next, head, link) {
196 list_del(&field->link);
197 kmem_cache_free(field_cachep, field);
202 * run-time version of trace_event_get_offsets_<call>() that returns the last
203 * accessible offset of trace fields excluding __dynamic_array bytes
205 int trace_event_get_offsets(struct trace_event_call *call)
207 struct ftrace_event_field *tail;
208 struct list_head *head;
210 head = trace_get_fields(call);
212 * head->next points to the last field with the largest offset,
213 * since it was added last by trace_define_field()
215 tail = list_first_entry(head, struct ftrace_event_field, link);
216 return tail->offset + tail->size;
219 int trace_event_raw_init(struct trace_event_call *call)
221 int id;
223 id = register_trace_event(&call->event);
224 if (!id)
225 return -ENODEV;
227 return 0;
229 EXPORT_SYMBOL_GPL(trace_event_raw_init);
231 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
233 struct trace_array *tr = trace_file->tr;
234 struct trace_array_cpu *data;
235 struct trace_pid_list *pid_list;
237 pid_list = rcu_dereference_raw(tr->filtered_pids);
238 if (!pid_list)
239 return false;
241 data = this_cpu_ptr(tr->array_buffer.data);
243 return data->ignore_pid;
245 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
247 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
248 struct trace_event_file *trace_file,
249 unsigned long len)
251 struct trace_event_call *event_call = trace_file->event_call;
253 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
254 trace_event_ignore_this_pid(trace_file))
255 return NULL;
257 local_save_flags(fbuffer->flags);
258 fbuffer->pc = preempt_count();
260 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
261 * preemption (adding one to the preempt_count). Since we are
262 * interested in the preempt_count at the time the tracepoint was
263 * hit, we need to subtract one to offset the increment.
265 if (IS_ENABLED(CONFIG_PREEMPTION))
266 fbuffer->pc--;
267 fbuffer->trace_file = trace_file;
269 fbuffer->event =
270 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
271 event_call->event.type, len,
272 fbuffer->flags, fbuffer->pc);
273 if (!fbuffer->event)
274 return NULL;
276 fbuffer->regs = NULL;
277 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
278 return fbuffer->entry;
280 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
282 int trace_event_reg(struct trace_event_call *call,
283 enum trace_reg type, void *data)
285 struct trace_event_file *file = data;
287 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
288 switch (type) {
289 case TRACE_REG_REGISTER:
290 return tracepoint_probe_register(call->tp,
291 call->class->probe,
292 file);
293 case TRACE_REG_UNREGISTER:
294 tracepoint_probe_unregister(call->tp,
295 call->class->probe,
296 file);
297 return 0;
299 #ifdef CONFIG_PERF_EVENTS
300 case TRACE_REG_PERF_REGISTER:
301 return tracepoint_probe_register(call->tp,
302 call->class->perf_probe,
303 call);
304 case TRACE_REG_PERF_UNREGISTER:
305 tracepoint_probe_unregister(call->tp,
306 call->class->perf_probe,
307 call);
308 return 0;
309 case TRACE_REG_PERF_OPEN:
310 case TRACE_REG_PERF_CLOSE:
311 case TRACE_REG_PERF_ADD:
312 case TRACE_REG_PERF_DEL:
313 return 0;
314 #endif
316 return 0;
318 EXPORT_SYMBOL_GPL(trace_event_reg);
320 void trace_event_enable_cmd_record(bool enable)
322 struct trace_event_file *file;
323 struct trace_array *tr;
325 lockdep_assert_held(&event_mutex);
327 do_for_each_event_file(tr, file) {
329 if (!(file->flags & EVENT_FILE_FL_ENABLED))
330 continue;
332 if (enable) {
333 tracing_start_cmdline_record();
334 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
335 } else {
336 tracing_stop_cmdline_record();
337 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
339 } while_for_each_event_file();
342 void trace_event_enable_tgid_record(bool enable)
344 struct trace_event_file *file;
345 struct trace_array *tr;
347 lockdep_assert_held(&event_mutex);
349 do_for_each_event_file(tr, file) {
350 if (!(file->flags & EVENT_FILE_FL_ENABLED))
351 continue;
353 if (enable) {
354 tracing_start_tgid_record();
355 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
356 } else {
357 tracing_stop_tgid_record();
358 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
359 &file->flags);
361 } while_for_each_event_file();
364 static int __ftrace_event_enable_disable(struct trace_event_file *file,
365 int enable, int soft_disable)
367 struct trace_event_call *call = file->event_call;
368 struct trace_array *tr = file->tr;
369 unsigned long file_flags = file->flags;
370 int ret = 0;
371 int disable;
373 switch (enable) {
374 case 0:
376 * When soft_disable is set and enable is cleared, the sm_ref
377 * reference counter is decremented. If it reaches 0, we want
378 * to clear the SOFT_DISABLED flag but leave the event in the
379 * state that it was. That is, if the event was enabled and
380 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
381 * is set we do not want the event to be enabled before we
382 * clear the bit.
384 * When soft_disable is not set but the SOFT_MODE flag is,
385 * we do nothing. Do not disable the tracepoint, otherwise
386 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
388 if (soft_disable) {
389 if (atomic_dec_return(&file->sm_ref) > 0)
390 break;
391 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
392 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
393 } else
394 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
396 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
397 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
398 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
399 tracing_stop_cmdline_record();
400 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
403 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
404 tracing_stop_tgid_record();
405 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
408 call->class->reg(call, TRACE_REG_UNREGISTER, file);
410 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
411 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
412 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
413 else
414 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
415 break;
416 case 1:
418 * When soft_disable is set and enable is set, we want to
419 * register the tracepoint for the event, but leave the event
420 * as is. That means, if the event was already enabled, we do
421 * nothing (but set SOFT_MODE). If the event is disabled, we
422 * set SOFT_DISABLED before enabling the event tracepoint, so
423 * it still seems to be disabled.
425 if (!soft_disable)
426 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
427 else {
428 if (atomic_inc_return(&file->sm_ref) > 1)
429 break;
430 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
433 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
434 bool cmd = false, tgid = false;
436 /* Keep the event disabled, when going to SOFT_MODE. */
437 if (soft_disable)
438 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
440 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
441 cmd = true;
442 tracing_start_cmdline_record();
443 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
446 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
447 tgid = true;
448 tracing_start_tgid_record();
449 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
452 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
453 if (ret) {
454 if (cmd)
455 tracing_stop_cmdline_record();
456 if (tgid)
457 tracing_stop_tgid_record();
458 pr_info("event trace: Could not enable event "
459 "%s\n", trace_event_name(call));
460 break;
462 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
464 /* WAS_ENABLED gets set but never cleared. */
465 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
467 break;
470 /* Enable or disable use of trace_buffered_event */
471 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
472 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
473 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
474 trace_buffered_event_enable();
475 else
476 trace_buffered_event_disable();
479 return ret;
482 int trace_event_enable_disable(struct trace_event_file *file,
483 int enable, int soft_disable)
485 return __ftrace_event_enable_disable(file, enable, soft_disable);
488 static int ftrace_event_enable_disable(struct trace_event_file *file,
489 int enable)
491 return __ftrace_event_enable_disable(file, enable, 0);
494 static void ftrace_clear_events(struct trace_array *tr)
496 struct trace_event_file *file;
498 mutex_lock(&event_mutex);
499 list_for_each_entry(file, &tr->events, list) {
500 ftrace_event_enable_disable(file, 0);
502 mutex_unlock(&event_mutex);
505 static void
506 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
508 struct trace_pid_list *pid_list;
509 struct trace_array *tr = data;
511 pid_list = rcu_dereference_raw(tr->filtered_pids);
512 trace_filter_add_remove_task(pid_list, NULL, task);
515 static void
516 event_filter_pid_sched_process_fork(void *data,
517 struct task_struct *self,
518 struct task_struct *task)
520 struct trace_pid_list *pid_list;
521 struct trace_array *tr = data;
523 pid_list = rcu_dereference_sched(tr->filtered_pids);
524 trace_filter_add_remove_task(pid_list, self, task);
527 void trace_event_follow_fork(struct trace_array *tr, bool enable)
529 if (enable) {
530 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
531 tr, INT_MIN);
532 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
533 tr, INT_MAX);
534 } else {
535 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
536 tr);
537 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
538 tr);
542 static void
543 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
544 struct task_struct *prev, struct task_struct *next)
546 struct trace_array *tr = data;
547 struct trace_pid_list *pid_list;
549 pid_list = rcu_dereference_sched(tr->filtered_pids);
551 this_cpu_write(tr->array_buffer.data->ignore_pid,
552 trace_ignore_this_task(pid_list, prev) &&
553 trace_ignore_this_task(pid_list, next));
556 static void
557 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
558 struct task_struct *prev, struct task_struct *next)
560 struct trace_array *tr = data;
561 struct trace_pid_list *pid_list;
563 pid_list = rcu_dereference_sched(tr->filtered_pids);
565 this_cpu_write(tr->array_buffer.data->ignore_pid,
566 trace_ignore_this_task(pid_list, next));
569 static void
570 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
572 struct trace_array *tr = data;
573 struct trace_pid_list *pid_list;
575 /* Nothing to do if we are already tracing */
576 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
577 return;
579 pid_list = rcu_dereference_sched(tr->filtered_pids);
581 this_cpu_write(tr->array_buffer.data->ignore_pid,
582 trace_ignore_this_task(pid_list, task));
585 static void
586 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
588 struct trace_array *tr = data;
589 struct trace_pid_list *pid_list;
591 /* Nothing to do if we are not tracing */
592 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
593 return;
595 pid_list = rcu_dereference_sched(tr->filtered_pids);
597 /* Set tracing if current is enabled */
598 this_cpu_write(tr->array_buffer.data->ignore_pid,
599 trace_ignore_this_task(pid_list, current));
602 static void __ftrace_clear_event_pids(struct trace_array *tr)
604 struct trace_pid_list *pid_list;
605 struct trace_event_file *file;
606 int cpu;
608 pid_list = rcu_dereference_protected(tr->filtered_pids,
609 lockdep_is_held(&event_mutex));
610 if (!pid_list)
611 return;
613 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
614 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
616 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
617 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
619 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
620 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
622 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
623 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
625 list_for_each_entry(file, &tr->events, list) {
626 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
629 for_each_possible_cpu(cpu)
630 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
632 rcu_assign_pointer(tr->filtered_pids, NULL);
634 /* Wait till all users are no longer using pid filtering */
635 tracepoint_synchronize_unregister();
637 trace_free_pid_list(pid_list);
640 static void ftrace_clear_event_pids(struct trace_array *tr)
642 mutex_lock(&event_mutex);
643 __ftrace_clear_event_pids(tr);
644 mutex_unlock(&event_mutex);
647 static void __put_system(struct event_subsystem *system)
649 struct event_filter *filter = system->filter;
651 WARN_ON_ONCE(system_refcount(system) == 0);
652 if (system_refcount_dec(system))
653 return;
655 list_del(&system->list);
657 if (filter) {
658 kfree(filter->filter_string);
659 kfree(filter);
661 kfree_const(system->name);
662 kfree(system);
665 static void __get_system(struct event_subsystem *system)
667 WARN_ON_ONCE(system_refcount(system) == 0);
668 system_refcount_inc(system);
671 static void __get_system_dir(struct trace_subsystem_dir *dir)
673 WARN_ON_ONCE(dir->ref_count == 0);
674 dir->ref_count++;
675 __get_system(dir->subsystem);
678 static void __put_system_dir(struct trace_subsystem_dir *dir)
680 WARN_ON_ONCE(dir->ref_count == 0);
681 /* If the subsystem is about to be freed, the dir must be too */
682 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
684 __put_system(dir->subsystem);
685 if (!--dir->ref_count)
686 kfree(dir);
689 static void put_system(struct trace_subsystem_dir *dir)
691 mutex_lock(&event_mutex);
692 __put_system_dir(dir);
693 mutex_unlock(&event_mutex);
696 static void remove_subsystem(struct trace_subsystem_dir *dir)
698 if (!dir)
699 return;
701 if (!--dir->nr_events) {
702 tracefs_remove(dir->entry);
703 list_del(&dir->list);
704 __put_system_dir(dir);
708 static void remove_event_file_dir(struct trace_event_file *file)
710 struct dentry *dir = file->dir;
711 struct dentry *child;
713 if (dir) {
714 spin_lock(&dir->d_lock); /* probably unneeded */
715 list_for_each_entry(child, &dir->d_subdirs, d_child) {
716 if (d_really_is_positive(child)) /* probably unneeded */
717 d_inode(child)->i_private = NULL;
719 spin_unlock(&dir->d_lock);
721 tracefs_remove(dir);
724 list_del(&file->list);
725 remove_subsystem(file->system);
726 free_event_filter(file->filter);
727 kmem_cache_free(file_cachep, file);
731 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
733 static int
734 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
735 const char *sub, const char *event, int set)
737 struct trace_event_file *file;
738 struct trace_event_call *call;
739 const char *name;
740 int ret = -EINVAL;
741 int eret = 0;
743 list_for_each_entry(file, &tr->events, list) {
745 call = file->event_call;
746 name = trace_event_name(call);
748 if (!name || !call->class || !call->class->reg)
749 continue;
751 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
752 continue;
754 if (match &&
755 strcmp(match, name) != 0 &&
756 strcmp(match, call->class->system) != 0)
757 continue;
759 if (sub && strcmp(sub, call->class->system) != 0)
760 continue;
762 if (event && strcmp(event, name) != 0)
763 continue;
765 ret = ftrace_event_enable_disable(file, set);
768 * Save the first error and return that. Some events
769 * may still have been enabled, but let the user
770 * know that something went wrong.
772 if (ret && !eret)
773 eret = ret;
775 ret = eret;
778 return ret;
781 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
782 const char *sub, const char *event, int set)
784 int ret;
786 mutex_lock(&event_mutex);
787 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
788 mutex_unlock(&event_mutex);
790 return ret;
793 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
795 char *event = NULL, *sub = NULL, *match;
796 int ret;
798 if (!tr)
799 return -ENOENT;
801 * The buf format can be <subsystem>:<event-name>
802 * *:<event-name> means any event by that name.
803 * :<event-name> is the same.
805 * <subsystem>:* means all events in that subsystem
806 * <subsystem>: means the same.
808 * <name> (no ':') means all events in a subsystem with
809 * the name <name> or any event that matches <name>
812 match = strsep(&buf, ":");
813 if (buf) {
814 sub = match;
815 event = buf;
816 match = NULL;
818 if (!strlen(sub) || strcmp(sub, "*") == 0)
819 sub = NULL;
820 if (!strlen(event) || strcmp(event, "*") == 0)
821 event = NULL;
824 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
826 /* Put back the colon to allow this to be called again */
827 if (buf)
828 *(buf - 1) = ':';
830 return ret;
834 * trace_set_clr_event - enable or disable an event
835 * @system: system name to match (NULL for any system)
836 * @event: event name to match (NULL for all events, within system)
837 * @set: 1 to enable, 0 to disable
839 * This is a way for other parts of the kernel to enable or disable
840 * event recording.
842 * Returns 0 on success, -EINVAL if the parameters do not match any
843 * registered events.
845 int trace_set_clr_event(const char *system, const char *event, int set)
847 struct trace_array *tr = top_trace_array();
849 if (!tr)
850 return -ENODEV;
852 return __ftrace_set_clr_event(tr, NULL, system, event, set);
854 EXPORT_SYMBOL_GPL(trace_set_clr_event);
857 * trace_array_set_clr_event - enable or disable an event for a trace array.
858 * @tr: concerned trace array.
859 * @system: system name to match (NULL for any system)
860 * @event: event name to match (NULL for all events, within system)
861 * @enable: true to enable, false to disable
863 * This is a way for other parts of the kernel to enable or disable
864 * event recording.
866 * Returns 0 on success, -EINVAL if the parameters do not match any
867 * registered events.
869 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
870 const char *event, bool enable)
872 int set;
874 if (!tr)
875 return -ENOENT;
877 set = (enable == true) ? 1 : 0;
878 return __ftrace_set_clr_event(tr, NULL, system, event, set);
880 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
882 /* 128 should be much more than enough */
883 #define EVENT_BUF_SIZE 127
885 static ssize_t
886 ftrace_event_write(struct file *file, const char __user *ubuf,
887 size_t cnt, loff_t *ppos)
889 struct trace_parser parser;
890 struct seq_file *m = file->private_data;
891 struct trace_array *tr = m->private;
892 ssize_t read, ret;
894 if (!cnt)
895 return 0;
897 ret = tracing_update_buffers();
898 if (ret < 0)
899 return ret;
901 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
902 return -ENOMEM;
904 read = trace_get_user(&parser, ubuf, cnt, ppos);
906 if (read >= 0 && trace_parser_loaded((&parser))) {
907 int set = 1;
909 if (*parser.buffer == '!')
910 set = 0;
912 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
913 if (ret)
914 goto out_put;
917 ret = read;
919 out_put:
920 trace_parser_put(&parser);
922 return ret;
925 static void *
926 t_next(struct seq_file *m, void *v, loff_t *pos)
928 struct trace_event_file *file = v;
929 struct trace_event_call *call;
930 struct trace_array *tr = m->private;
932 (*pos)++;
934 list_for_each_entry_continue(file, &tr->events, list) {
935 call = file->event_call;
937 * The ftrace subsystem is for showing formats only.
938 * They can not be enabled or disabled via the event files.
940 if (call->class && call->class->reg &&
941 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
942 return file;
945 return NULL;
948 static void *t_start(struct seq_file *m, loff_t *pos)
950 struct trace_event_file *file;
951 struct trace_array *tr = m->private;
952 loff_t l;
954 mutex_lock(&event_mutex);
956 file = list_entry(&tr->events, struct trace_event_file, list);
957 for (l = 0; l <= *pos; ) {
958 file = t_next(m, file, &l);
959 if (!file)
960 break;
962 return file;
965 static void *
966 s_next(struct seq_file *m, void *v, loff_t *pos)
968 struct trace_event_file *file = v;
969 struct trace_array *tr = m->private;
971 (*pos)++;
973 list_for_each_entry_continue(file, &tr->events, list) {
974 if (file->flags & EVENT_FILE_FL_ENABLED)
975 return file;
978 return NULL;
981 static void *s_start(struct seq_file *m, loff_t *pos)
983 struct trace_event_file *file;
984 struct trace_array *tr = m->private;
985 loff_t l;
987 mutex_lock(&event_mutex);
989 file = list_entry(&tr->events, struct trace_event_file, list);
990 for (l = 0; l <= *pos; ) {
991 file = s_next(m, file, &l);
992 if (!file)
993 break;
995 return file;
998 static int t_show(struct seq_file *m, void *v)
1000 struct trace_event_file *file = v;
1001 struct trace_event_call *call = file->event_call;
1003 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1004 seq_printf(m, "%s:", call->class->system);
1005 seq_printf(m, "%s\n", trace_event_name(call));
1007 return 0;
1010 static void t_stop(struct seq_file *m, void *p)
1012 mutex_unlock(&event_mutex);
1015 static void *
1016 p_next(struct seq_file *m, void *v, loff_t *pos)
1018 struct trace_array *tr = m->private;
1019 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
1021 return trace_pid_next(pid_list, v, pos);
1024 static void *p_start(struct seq_file *m, loff_t *pos)
1025 __acquires(RCU)
1027 struct trace_pid_list *pid_list;
1028 struct trace_array *tr = m->private;
1031 * Grab the mutex, to keep calls to p_next() having the same
1032 * tr->filtered_pids as p_start() has.
1033 * If we just passed the tr->filtered_pids around, then RCU would
1034 * have been enough, but doing that makes things more complex.
1036 mutex_lock(&event_mutex);
1037 rcu_read_lock_sched();
1039 pid_list = rcu_dereference_sched(tr->filtered_pids);
1041 if (!pid_list)
1042 return NULL;
1044 return trace_pid_start(pid_list, pos);
1047 static void p_stop(struct seq_file *m, void *p)
1048 __releases(RCU)
1050 rcu_read_unlock_sched();
1051 mutex_unlock(&event_mutex);
1054 static ssize_t
1055 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1056 loff_t *ppos)
1058 struct trace_event_file *file;
1059 unsigned long flags;
1060 char buf[4] = "0";
1062 mutex_lock(&event_mutex);
1063 file = event_file_data(filp);
1064 if (likely(file))
1065 flags = file->flags;
1066 mutex_unlock(&event_mutex);
1068 if (!file)
1069 return -ENODEV;
1071 if (flags & EVENT_FILE_FL_ENABLED &&
1072 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1073 strcpy(buf, "1");
1075 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1076 flags & EVENT_FILE_FL_SOFT_MODE)
1077 strcat(buf, "*");
1079 strcat(buf, "\n");
1081 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1084 static ssize_t
1085 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1086 loff_t *ppos)
1088 struct trace_event_file *file;
1089 unsigned long val;
1090 int ret;
1092 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1093 if (ret)
1094 return ret;
1096 ret = tracing_update_buffers();
1097 if (ret < 0)
1098 return ret;
1100 switch (val) {
1101 case 0:
1102 case 1:
1103 ret = -ENODEV;
1104 mutex_lock(&event_mutex);
1105 file = event_file_data(filp);
1106 if (likely(file))
1107 ret = ftrace_event_enable_disable(file, val);
1108 mutex_unlock(&event_mutex);
1109 break;
1111 default:
1112 return -EINVAL;
1115 *ppos += cnt;
1117 return ret ? ret : cnt;
1120 static ssize_t
1121 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1122 loff_t *ppos)
1124 const char set_to_char[4] = { '?', '0', '1', 'X' };
1125 struct trace_subsystem_dir *dir = filp->private_data;
1126 struct event_subsystem *system = dir->subsystem;
1127 struct trace_event_call *call;
1128 struct trace_event_file *file;
1129 struct trace_array *tr = dir->tr;
1130 char buf[2];
1131 int set = 0;
1132 int ret;
1134 mutex_lock(&event_mutex);
1135 list_for_each_entry(file, &tr->events, list) {
1136 call = file->event_call;
1137 if (!trace_event_name(call) || !call->class || !call->class->reg)
1138 continue;
1140 if (system && strcmp(call->class->system, system->name) != 0)
1141 continue;
1144 * We need to find out if all the events are set
1145 * or if all events or cleared, or if we have
1146 * a mixture.
1148 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1151 * If we have a mixture, no need to look further.
1153 if (set == 3)
1154 break;
1156 mutex_unlock(&event_mutex);
1158 buf[0] = set_to_char[set];
1159 buf[1] = '\n';
1161 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1163 return ret;
1166 static ssize_t
1167 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1168 loff_t *ppos)
1170 struct trace_subsystem_dir *dir = filp->private_data;
1171 struct event_subsystem *system = dir->subsystem;
1172 const char *name = NULL;
1173 unsigned long val;
1174 ssize_t ret;
1176 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1177 if (ret)
1178 return ret;
1180 ret = tracing_update_buffers();
1181 if (ret < 0)
1182 return ret;
1184 if (val != 0 && val != 1)
1185 return -EINVAL;
1188 * Opening of "enable" adds a ref count to system,
1189 * so the name is safe to use.
1191 if (system)
1192 name = system->name;
1194 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1195 if (ret)
1196 goto out;
1198 ret = cnt;
1200 out:
1201 *ppos += cnt;
1203 return ret;
1206 enum {
1207 FORMAT_HEADER = 1,
1208 FORMAT_FIELD_SEPERATOR = 2,
1209 FORMAT_PRINTFMT = 3,
1212 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1214 struct trace_event_call *call = event_file_data(m->private);
1215 struct list_head *common_head = &ftrace_common_fields;
1216 struct list_head *head = trace_get_fields(call);
1217 struct list_head *node = v;
1219 (*pos)++;
1221 switch ((unsigned long)v) {
1222 case FORMAT_HEADER:
1223 node = common_head;
1224 break;
1226 case FORMAT_FIELD_SEPERATOR:
1227 node = head;
1228 break;
1230 case FORMAT_PRINTFMT:
1231 /* all done */
1232 return NULL;
1235 node = node->prev;
1236 if (node == common_head)
1237 return (void *)FORMAT_FIELD_SEPERATOR;
1238 else if (node == head)
1239 return (void *)FORMAT_PRINTFMT;
1240 else
1241 return node;
1244 static int f_show(struct seq_file *m, void *v)
1246 struct trace_event_call *call = event_file_data(m->private);
1247 struct ftrace_event_field *field;
1248 const char *array_descriptor;
1250 switch ((unsigned long)v) {
1251 case FORMAT_HEADER:
1252 seq_printf(m, "name: %s\n", trace_event_name(call));
1253 seq_printf(m, "ID: %d\n", call->event.type);
1254 seq_puts(m, "format:\n");
1255 return 0;
1257 case FORMAT_FIELD_SEPERATOR:
1258 seq_putc(m, '\n');
1259 return 0;
1261 case FORMAT_PRINTFMT:
1262 seq_printf(m, "\nprint fmt: %s\n",
1263 call->print_fmt);
1264 return 0;
1267 field = list_entry(v, struct ftrace_event_field, link);
1269 * Smartly shows the array type(except dynamic array).
1270 * Normal:
1271 * field:TYPE VAR
1272 * If TYPE := TYPE[LEN], it is shown:
1273 * field:TYPE VAR[LEN]
1275 array_descriptor = strchr(field->type, '[');
1277 if (str_has_prefix(field->type, "__data_loc"))
1278 array_descriptor = NULL;
1280 if (!array_descriptor)
1281 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1282 field->type, field->name, field->offset,
1283 field->size, !!field->is_signed);
1284 else
1285 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1286 (int)(array_descriptor - field->type),
1287 field->type, field->name,
1288 array_descriptor, field->offset,
1289 field->size, !!field->is_signed);
1291 return 0;
1294 static void *f_start(struct seq_file *m, loff_t *pos)
1296 void *p = (void *)FORMAT_HEADER;
1297 loff_t l = 0;
1299 /* ->stop() is called even if ->start() fails */
1300 mutex_lock(&event_mutex);
1301 if (!event_file_data(m->private))
1302 return ERR_PTR(-ENODEV);
1304 while (l < *pos && p)
1305 p = f_next(m, p, &l);
1307 return p;
1310 static void f_stop(struct seq_file *m, void *p)
1312 mutex_unlock(&event_mutex);
1315 static const struct seq_operations trace_format_seq_ops = {
1316 .start = f_start,
1317 .next = f_next,
1318 .stop = f_stop,
1319 .show = f_show,
1322 static int trace_format_open(struct inode *inode, struct file *file)
1324 struct seq_file *m;
1325 int ret;
1327 /* Do we want to hide event format files on tracefs lockdown? */
1329 ret = seq_open(file, &trace_format_seq_ops);
1330 if (ret < 0)
1331 return ret;
1333 m = file->private_data;
1334 m->private = file;
1336 return 0;
1339 static ssize_t
1340 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1342 int id = (long)event_file_data(filp);
1343 char buf[32];
1344 int len;
1346 if (unlikely(!id))
1347 return -ENODEV;
1349 len = sprintf(buf, "%d\n", id);
1351 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1354 static ssize_t
1355 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1356 loff_t *ppos)
1358 struct trace_event_file *file;
1359 struct trace_seq *s;
1360 int r = -ENODEV;
1362 if (*ppos)
1363 return 0;
1365 s = kmalloc(sizeof(*s), GFP_KERNEL);
1367 if (!s)
1368 return -ENOMEM;
1370 trace_seq_init(s);
1372 mutex_lock(&event_mutex);
1373 file = event_file_data(filp);
1374 if (file)
1375 print_event_filter(file, s);
1376 mutex_unlock(&event_mutex);
1378 if (file)
1379 r = simple_read_from_buffer(ubuf, cnt, ppos,
1380 s->buffer, trace_seq_used(s));
1382 kfree(s);
1384 return r;
1387 static ssize_t
1388 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1389 loff_t *ppos)
1391 struct trace_event_file *file;
1392 char *buf;
1393 int err = -ENODEV;
1395 if (cnt >= PAGE_SIZE)
1396 return -EINVAL;
1398 buf = memdup_user_nul(ubuf, cnt);
1399 if (IS_ERR(buf))
1400 return PTR_ERR(buf);
1402 mutex_lock(&event_mutex);
1403 file = event_file_data(filp);
1404 if (file)
1405 err = apply_event_filter(file, buf);
1406 mutex_unlock(&event_mutex);
1408 kfree(buf);
1409 if (err < 0)
1410 return err;
1412 *ppos += cnt;
1414 return cnt;
1417 static LIST_HEAD(event_subsystems);
1419 static int subsystem_open(struct inode *inode, struct file *filp)
1421 struct event_subsystem *system = NULL;
1422 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1423 struct trace_array *tr;
1424 int ret;
1426 if (tracing_is_disabled())
1427 return -ENODEV;
1429 /* Make sure the system still exists */
1430 mutex_lock(&event_mutex);
1431 mutex_lock(&trace_types_lock);
1432 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1433 list_for_each_entry(dir, &tr->systems, list) {
1434 if (dir == inode->i_private) {
1435 /* Don't open systems with no events */
1436 if (dir->nr_events) {
1437 __get_system_dir(dir);
1438 system = dir->subsystem;
1440 goto exit_loop;
1444 exit_loop:
1445 mutex_unlock(&trace_types_lock);
1446 mutex_unlock(&event_mutex);
1448 if (!system)
1449 return -ENODEV;
1451 /* Some versions of gcc think dir can be uninitialized here */
1452 WARN_ON(!dir);
1454 /* Still need to increment the ref count of the system */
1455 if (trace_array_get(tr) < 0) {
1456 put_system(dir);
1457 return -ENODEV;
1460 ret = tracing_open_generic(inode, filp);
1461 if (ret < 0) {
1462 trace_array_put(tr);
1463 put_system(dir);
1466 return ret;
1469 static int system_tr_open(struct inode *inode, struct file *filp)
1471 struct trace_subsystem_dir *dir;
1472 struct trace_array *tr = inode->i_private;
1473 int ret;
1475 /* Make a temporary dir that has no system but points to tr */
1476 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1477 if (!dir)
1478 return -ENOMEM;
1480 ret = tracing_open_generic_tr(inode, filp);
1481 if (ret < 0) {
1482 kfree(dir);
1483 return ret;
1485 dir->tr = tr;
1486 filp->private_data = dir;
1488 return 0;
1491 static int subsystem_release(struct inode *inode, struct file *file)
1493 struct trace_subsystem_dir *dir = file->private_data;
1495 trace_array_put(dir->tr);
1498 * If dir->subsystem is NULL, then this is a temporary
1499 * descriptor that was made for a trace_array to enable
1500 * all subsystems.
1502 if (dir->subsystem)
1503 put_system(dir);
1504 else
1505 kfree(dir);
1507 return 0;
1510 static ssize_t
1511 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1512 loff_t *ppos)
1514 struct trace_subsystem_dir *dir = filp->private_data;
1515 struct event_subsystem *system = dir->subsystem;
1516 struct trace_seq *s;
1517 int r;
1519 if (*ppos)
1520 return 0;
1522 s = kmalloc(sizeof(*s), GFP_KERNEL);
1523 if (!s)
1524 return -ENOMEM;
1526 trace_seq_init(s);
1528 print_subsystem_event_filter(system, s);
1529 r = simple_read_from_buffer(ubuf, cnt, ppos,
1530 s->buffer, trace_seq_used(s));
1532 kfree(s);
1534 return r;
1537 static ssize_t
1538 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1539 loff_t *ppos)
1541 struct trace_subsystem_dir *dir = filp->private_data;
1542 char *buf;
1543 int err;
1545 if (cnt >= PAGE_SIZE)
1546 return -EINVAL;
1548 buf = memdup_user_nul(ubuf, cnt);
1549 if (IS_ERR(buf))
1550 return PTR_ERR(buf);
1552 err = apply_subsystem_event_filter(dir, buf);
1553 kfree(buf);
1554 if (err < 0)
1555 return err;
1557 *ppos += cnt;
1559 return cnt;
1562 static ssize_t
1563 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1565 int (*func)(struct trace_seq *s) = filp->private_data;
1566 struct trace_seq *s;
1567 int r;
1569 if (*ppos)
1570 return 0;
1572 s = kmalloc(sizeof(*s), GFP_KERNEL);
1573 if (!s)
1574 return -ENOMEM;
1576 trace_seq_init(s);
1578 func(s);
1579 r = simple_read_from_buffer(ubuf, cnt, ppos,
1580 s->buffer, trace_seq_used(s));
1582 kfree(s);
1584 return r;
1587 static void ignore_task_cpu(void *data)
1589 struct trace_array *tr = data;
1590 struct trace_pid_list *pid_list;
1593 * This function is called by on_each_cpu() while the
1594 * event_mutex is held.
1596 pid_list = rcu_dereference_protected(tr->filtered_pids,
1597 mutex_is_locked(&event_mutex));
1599 this_cpu_write(tr->array_buffer.data->ignore_pid,
1600 trace_ignore_this_task(pid_list, current));
1603 static ssize_t
1604 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1605 size_t cnt, loff_t *ppos)
1607 struct seq_file *m = filp->private_data;
1608 struct trace_array *tr = m->private;
1609 struct trace_pid_list *filtered_pids = NULL;
1610 struct trace_pid_list *pid_list;
1611 struct trace_event_file *file;
1612 ssize_t ret;
1614 if (!cnt)
1615 return 0;
1617 ret = tracing_update_buffers();
1618 if (ret < 0)
1619 return ret;
1621 mutex_lock(&event_mutex);
1623 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1624 lockdep_is_held(&event_mutex));
1626 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1627 if (ret < 0)
1628 goto out;
1630 rcu_assign_pointer(tr->filtered_pids, pid_list);
1632 list_for_each_entry(file, &tr->events, list) {
1633 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1636 if (filtered_pids) {
1637 tracepoint_synchronize_unregister();
1638 trace_free_pid_list(filtered_pids);
1639 } else if (pid_list) {
1641 * Register a probe that is called before all other probes
1642 * to set ignore_pid if next or prev do not match.
1643 * Register a probe this is called after all other probes
1644 * to only keep ignore_pid set if next pid matches.
1646 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1647 tr, INT_MAX);
1648 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1649 tr, 0);
1651 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1652 tr, INT_MAX);
1653 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1654 tr, 0);
1656 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1657 tr, INT_MAX);
1658 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1659 tr, 0);
1661 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1662 tr, INT_MAX);
1663 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1664 tr, 0);
1668 * Ignoring of pids is done at task switch. But we have to
1669 * check for those tasks that are currently running.
1670 * Always do this in case a pid was appended or removed.
1672 on_each_cpu(ignore_task_cpu, tr, 1);
1674 out:
1675 mutex_unlock(&event_mutex);
1677 if (ret > 0)
1678 *ppos += ret;
1680 return ret;
1683 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1684 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1685 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1686 static int ftrace_event_release(struct inode *inode, struct file *file);
1688 static const struct seq_operations show_event_seq_ops = {
1689 .start = t_start,
1690 .next = t_next,
1691 .show = t_show,
1692 .stop = t_stop,
1695 static const struct seq_operations show_set_event_seq_ops = {
1696 .start = s_start,
1697 .next = s_next,
1698 .show = t_show,
1699 .stop = t_stop,
1702 static const struct seq_operations show_set_pid_seq_ops = {
1703 .start = p_start,
1704 .next = p_next,
1705 .show = trace_pid_show,
1706 .stop = p_stop,
1709 static const struct file_operations ftrace_avail_fops = {
1710 .open = ftrace_event_avail_open,
1711 .read = seq_read,
1712 .llseek = seq_lseek,
1713 .release = seq_release,
1716 static const struct file_operations ftrace_set_event_fops = {
1717 .open = ftrace_event_set_open,
1718 .read = seq_read,
1719 .write = ftrace_event_write,
1720 .llseek = seq_lseek,
1721 .release = ftrace_event_release,
1724 static const struct file_operations ftrace_set_event_pid_fops = {
1725 .open = ftrace_event_set_pid_open,
1726 .read = seq_read,
1727 .write = ftrace_event_pid_write,
1728 .llseek = seq_lseek,
1729 .release = ftrace_event_release,
1732 static const struct file_operations ftrace_enable_fops = {
1733 .open = tracing_open_generic,
1734 .read = event_enable_read,
1735 .write = event_enable_write,
1736 .llseek = default_llseek,
1739 static const struct file_operations ftrace_event_format_fops = {
1740 .open = trace_format_open,
1741 .read = seq_read,
1742 .llseek = seq_lseek,
1743 .release = seq_release,
1746 static const struct file_operations ftrace_event_id_fops = {
1747 .read = event_id_read,
1748 .llseek = default_llseek,
1751 static const struct file_operations ftrace_event_filter_fops = {
1752 .open = tracing_open_generic,
1753 .read = event_filter_read,
1754 .write = event_filter_write,
1755 .llseek = default_llseek,
1758 static const struct file_operations ftrace_subsystem_filter_fops = {
1759 .open = subsystem_open,
1760 .read = subsystem_filter_read,
1761 .write = subsystem_filter_write,
1762 .llseek = default_llseek,
1763 .release = subsystem_release,
1766 static const struct file_operations ftrace_system_enable_fops = {
1767 .open = subsystem_open,
1768 .read = system_enable_read,
1769 .write = system_enable_write,
1770 .llseek = default_llseek,
1771 .release = subsystem_release,
1774 static const struct file_operations ftrace_tr_enable_fops = {
1775 .open = system_tr_open,
1776 .read = system_enable_read,
1777 .write = system_enable_write,
1778 .llseek = default_llseek,
1779 .release = subsystem_release,
1782 static const struct file_operations ftrace_show_header_fops = {
1783 .open = tracing_open_generic,
1784 .read = show_header,
1785 .llseek = default_llseek,
1788 static int
1789 ftrace_event_open(struct inode *inode, struct file *file,
1790 const struct seq_operations *seq_ops)
1792 struct seq_file *m;
1793 int ret;
1795 ret = security_locked_down(LOCKDOWN_TRACEFS);
1796 if (ret)
1797 return ret;
1799 ret = seq_open(file, seq_ops);
1800 if (ret < 0)
1801 return ret;
1802 m = file->private_data;
1803 /* copy tr over to seq ops */
1804 m->private = inode->i_private;
1806 return ret;
1809 static int ftrace_event_release(struct inode *inode, struct file *file)
1811 struct trace_array *tr = inode->i_private;
1813 trace_array_put(tr);
1815 return seq_release(inode, file);
1818 static int
1819 ftrace_event_avail_open(struct inode *inode, struct file *file)
1821 const struct seq_operations *seq_ops = &show_event_seq_ops;
1823 /* Checks for tracefs lockdown */
1824 return ftrace_event_open(inode, file, seq_ops);
1827 static int
1828 ftrace_event_set_open(struct inode *inode, struct file *file)
1830 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1831 struct trace_array *tr = inode->i_private;
1832 int ret;
1834 ret = tracing_check_open_get_tr(tr);
1835 if (ret)
1836 return ret;
1838 if ((file->f_mode & FMODE_WRITE) &&
1839 (file->f_flags & O_TRUNC))
1840 ftrace_clear_events(tr);
1842 ret = ftrace_event_open(inode, file, seq_ops);
1843 if (ret < 0)
1844 trace_array_put(tr);
1845 return ret;
1848 static int
1849 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1851 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1852 struct trace_array *tr = inode->i_private;
1853 int ret;
1855 ret = tracing_check_open_get_tr(tr);
1856 if (ret)
1857 return ret;
1859 if ((file->f_mode & FMODE_WRITE) &&
1860 (file->f_flags & O_TRUNC))
1861 ftrace_clear_event_pids(tr);
1863 ret = ftrace_event_open(inode, file, seq_ops);
1864 if (ret < 0)
1865 trace_array_put(tr);
1866 return ret;
1869 static struct event_subsystem *
1870 create_new_subsystem(const char *name)
1872 struct event_subsystem *system;
1874 /* need to create new entry */
1875 system = kmalloc(sizeof(*system), GFP_KERNEL);
1876 if (!system)
1877 return NULL;
1879 system->ref_count = 1;
1881 /* Only allocate if dynamic (kprobes and modules) */
1882 system->name = kstrdup_const(name, GFP_KERNEL);
1883 if (!system->name)
1884 goto out_free;
1886 system->filter = NULL;
1888 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1889 if (!system->filter)
1890 goto out_free;
1892 list_add(&system->list, &event_subsystems);
1894 return system;
1896 out_free:
1897 kfree_const(system->name);
1898 kfree(system);
1899 return NULL;
1902 static struct dentry *
1903 event_subsystem_dir(struct trace_array *tr, const char *name,
1904 struct trace_event_file *file, struct dentry *parent)
1906 struct trace_subsystem_dir *dir;
1907 struct event_subsystem *system;
1908 struct dentry *entry;
1910 /* First see if we did not already create this dir */
1911 list_for_each_entry(dir, &tr->systems, list) {
1912 system = dir->subsystem;
1913 if (strcmp(system->name, name) == 0) {
1914 dir->nr_events++;
1915 file->system = dir;
1916 return dir->entry;
1920 /* Now see if the system itself exists. */
1921 list_for_each_entry(system, &event_subsystems, list) {
1922 if (strcmp(system->name, name) == 0)
1923 break;
1925 /* Reset system variable when not found */
1926 if (&system->list == &event_subsystems)
1927 system = NULL;
1929 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1930 if (!dir)
1931 goto out_fail;
1933 if (!system) {
1934 system = create_new_subsystem(name);
1935 if (!system)
1936 goto out_free;
1937 } else
1938 __get_system(system);
1940 dir->entry = tracefs_create_dir(name, parent);
1941 if (!dir->entry) {
1942 pr_warn("Failed to create system directory %s\n", name);
1943 __put_system(system);
1944 goto out_free;
1947 dir->tr = tr;
1948 dir->ref_count = 1;
1949 dir->nr_events = 1;
1950 dir->subsystem = system;
1951 file->system = dir;
1953 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1954 &ftrace_subsystem_filter_fops);
1955 if (!entry) {
1956 kfree(system->filter);
1957 system->filter = NULL;
1958 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1961 trace_create_file("enable", 0644, dir->entry, dir,
1962 &ftrace_system_enable_fops);
1964 list_add(&dir->list, &tr->systems);
1966 return dir->entry;
1968 out_free:
1969 kfree(dir);
1970 out_fail:
1971 /* Only print this message if failed on memory allocation */
1972 if (!dir || !system)
1973 pr_warn("No memory to create event subsystem %s\n", name);
1974 return NULL;
1977 static int
1978 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1980 struct trace_event_call *call = file->event_call;
1981 struct trace_array *tr = file->tr;
1982 struct list_head *head;
1983 struct dentry *d_events;
1984 const char *name;
1985 int ret;
1988 * If the trace point header did not define TRACE_SYSTEM
1989 * then the system would be called "TRACE_SYSTEM".
1991 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1992 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1993 if (!d_events)
1994 return -ENOMEM;
1995 } else
1996 d_events = parent;
1998 name = trace_event_name(call);
1999 file->dir = tracefs_create_dir(name, d_events);
2000 if (!file->dir) {
2001 pr_warn("Could not create tracefs '%s' directory\n", name);
2002 return -1;
2005 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2006 trace_create_file("enable", 0644, file->dir, file,
2007 &ftrace_enable_fops);
2009 #ifdef CONFIG_PERF_EVENTS
2010 if (call->event.type && call->class->reg)
2011 trace_create_file("id", 0444, file->dir,
2012 (void *)(long)call->event.type,
2013 &ftrace_event_id_fops);
2014 #endif
2017 * Other events may have the same class. Only update
2018 * the fields if they are not already defined.
2020 head = trace_get_fields(call);
2021 if (list_empty(head)) {
2022 struct trace_event_fields *field = call->class->fields_array;
2023 unsigned int offset = sizeof(struct trace_entry);
2025 for (; field->type; field++) {
2026 if (field->type == TRACE_FUNCTION_TYPE) {
2027 ret = field->define_fields(call);
2028 break;
2031 offset = ALIGN(offset, field->align);
2032 ret = trace_define_field(call, field->type, field->name,
2033 offset, field->size,
2034 field->is_signed, field->filter_type);
2035 if (ret)
2036 break;
2038 offset += field->size;
2040 if (ret < 0) {
2041 pr_warn("Could not initialize trace point events/%s\n",
2042 name);
2043 return -1;
2048 * Only event directories that can be enabled should have
2049 * triggers or filters.
2051 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2052 trace_create_file("filter", 0644, file->dir, file,
2053 &ftrace_event_filter_fops);
2055 trace_create_file("trigger", 0644, file->dir, file,
2056 &event_trigger_fops);
2059 #ifdef CONFIG_HIST_TRIGGERS
2060 trace_create_file("hist", 0444, file->dir, file,
2061 &event_hist_fops);
2062 #endif
2063 trace_create_file("format", 0444, file->dir, call,
2064 &ftrace_event_format_fops);
2066 #ifdef CONFIG_TRACE_EVENT_INJECT
2067 if (call->event.type && call->class->reg)
2068 trace_create_file("inject", 0200, file->dir, file,
2069 &event_inject_fops);
2070 #endif
2072 return 0;
2075 static void remove_event_from_tracers(struct trace_event_call *call)
2077 struct trace_event_file *file;
2078 struct trace_array *tr;
2080 do_for_each_event_file_safe(tr, file) {
2081 if (file->event_call != call)
2082 continue;
2084 remove_event_file_dir(file);
2086 * The do_for_each_event_file_safe() is
2087 * a double loop. After finding the call for this
2088 * trace_array, we use break to jump to the next
2089 * trace_array.
2091 break;
2092 } while_for_each_event_file();
2095 static void event_remove(struct trace_event_call *call)
2097 struct trace_array *tr;
2098 struct trace_event_file *file;
2100 do_for_each_event_file(tr, file) {
2101 if (file->event_call != call)
2102 continue;
2104 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2105 tr->clear_trace = true;
2107 ftrace_event_enable_disable(file, 0);
2109 * The do_for_each_event_file() is
2110 * a double loop. After finding the call for this
2111 * trace_array, we use break to jump to the next
2112 * trace_array.
2114 break;
2115 } while_for_each_event_file();
2117 if (call->event.funcs)
2118 __unregister_trace_event(&call->event);
2119 remove_event_from_tracers(call);
2120 list_del(&call->list);
2123 static int event_init(struct trace_event_call *call)
2125 int ret = 0;
2126 const char *name;
2128 name = trace_event_name(call);
2129 if (WARN_ON(!name))
2130 return -EINVAL;
2132 if (call->class->raw_init) {
2133 ret = call->class->raw_init(call);
2134 if (ret < 0 && ret != -ENOSYS)
2135 pr_warn("Could not initialize trace events/%s\n", name);
2138 return ret;
2141 static int
2142 __register_event(struct trace_event_call *call, struct module *mod)
2144 int ret;
2146 ret = event_init(call);
2147 if (ret < 0)
2148 return ret;
2150 list_add(&call->list, &ftrace_events);
2151 call->mod = mod;
2153 return 0;
2156 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2158 int rlen;
2159 int elen;
2161 /* Find the length of the eval value as a string */
2162 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2163 /* Make sure there's enough room to replace the string with the value */
2164 if (len < elen)
2165 return NULL;
2167 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2169 /* Get the rest of the string of ptr */
2170 rlen = strlen(ptr + len);
2171 memmove(ptr + elen, ptr + len, rlen);
2172 /* Make sure we end the new string */
2173 ptr[elen + rlen] = 0;
2175 return ptr + elen;
2178 static void update_event_printk(struct trace_event_call *call,
2179 struct trace_eval_map *map)
2181 char *ptr;
2182 int quote = 0;
2183 int len = strlen(map->eval_string);
2185 for (ptr = call->print_fmt; *ptr; ptr++) {
2186 if (*ptr == '\\') {
2187 ptr++;
2188 /* paranoid */
2189 if (!*ptr)
2190 break;
2191 continue;
2193 if (*ptr == '"') {
2194 quote ^= 1;
2195 continue;
2197 if (quote)
2198 continue;
2199 if (isdigit(*ptr)) {
2200 /* skip numbers */
2201 do {
2202 ptr++;
2203 /* Check for alpha chars like ULL */
2204 } while (isalnum(*ptr));
2205 if (!*ptr)
2206 break;
2208 * A number must have some kind of delimiter after
2209 * it, and we can ignore that too.
2211 continue;
2213 if (isalpha(*ptr) || *ptr == '_') {
2214 if (strncmp(map->eval_string, ptr, len) == 0 &&
2215 !isalnum(ptr[len]) && ptr[len] != '_') {
2216 ptr = eval_replace(ptr, map, len);
2217 /* enum/sizeof string smaller than value */
2218 if (WARN_ON_ONCE(!ptr))
2219 return;
2221 * No need to decrement here, as eval_replace()
2222 * returns the pointer to the character passed
2223 * the eval, and two evals can not be placed
2224 * back to back without something in between.
2225 * We can skip that something in between.
2227 continue;
2229 skip_more:
2230 do {
2231 ptr++;
2232 } while (isalnum(*ptr) || *ptr == '_');
2233 if (!*ptr)
2234 break;
2236 * If what comes after this variable is a '.' or
2237 * '->' then we can continue to ignore that string.
2239 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2240 ptr += *ptr == '.' ? 1 : 2;
2241 if (!*ptr)
2242 break;
2243 goto skip_more;
2246 * Once again, we can skip the delimiter that came
2247 * after the string.
2249 continue;
2254 void trace_event_eval_update(struct trace_eval_map **map, int len)
2256 struct trace_event_call *call, *p;
2257 const char *last_system = NULL;
2258 bool first = false;
2259 int last_i;
2260 int i;
2262 down_write(&trace_event_sem);
2263 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2264 /* events are usually grouped together with systems */
2265 if (!last_system || call->class->system != last_system) {
2266 first = true;
2267 last_i = 0;
2268 last_system = call->class->system;
2272 * Since calls are grouped by systems, the likelyhood that the
2273 * next call in the iteration belongs to the same system as the
2274 * previous call is high. As an optimization, we skip seaching
2275 * for a map[] that matches the call's system if the last call
2276 * was from the same system. That's what last_i is for. If the
2277 * call has the same system as the previous call, then last_i
2278 * will be the index of the first map[] that has a matching
2279 * system.
2281 for (i = last_i; i < len; i++) {
2282 if (call->class->system == map[i]->system) {
2283 /* Save the first system if need be */
2284 if (first) {
2285 last_i = i;
2286 first = false;
2288 update_event_printk(call, map[i]);
2292 up_write(&trace_event_sem);
2295 static struct trace_event_file *
2296 trace_create_new_event(struct trace_event_call *call,
2297 struct trace_array *tr)
2299 struct trace_event_file *file;
2301 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2302 if (!file)
2303 return NULL;
2305 file->event_call = call;
2306 file->tr = tr;
2307 atomic_set(&file->sm_ref, 0);
2308 atomic_set(&file->tm_ref, 0);
2309 INIT_LIST_HEAD(&file->triggers);
2310 list_add(&file->list, &tr->events);
2312 return file;
2315 /* Add an event to a trace directory */
2316 static int
2317 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2319 struct trace_event_file *file;
2321 file = trace_create_new_event(call, tr);
2322 if (!file)
2323 return -ENOMEM;
2325 return event_create_dir(tr->event_dir, file);
2329 * Just create a decriptor for early init. A descriptor is required
2330 * for enabling events at boot. We want to enable events before
2331 * the filesystem is initialized.
2333 static __init int
2334 __trace_early_add_new_event(struct trace_event_call *call,
2335 struct trace_array *tr)
2337 struct trace_event_file *file;
2339 file = trace_create_new_event(call, tr);
2340 if (!file)
2341 return -ENOMEM;
2343 return 0;
2346 struct ftrace_module_file_ops;
2347 static void __add_event_to_tracers(struct trace_event_call *call);
2349 /* Add an additional event_call dynamically */
2350 int trace_add_event_call(struct trace_event_call *call)
2352 int ret;
2353 lockdep_assert_held(&event_mutex);
2355 mutex_lock(&trace_types_lock);
2357 ret = __register_event(call, NULL);
2358 if (ret >= 0)
2359 __add_event_to_tracers(call);
2361 mutex_unlock(&trace_types_lock);
2362 return ret;
2366 * Must be called under locking of trace_types_lock, event_mutex and
2367 * trace_event_sem.
2369 static void __trace_remove_event_call(struct trace_event_call *call)
2371 event_remove(call);
2372 trace_destroy_fields(call);
2373 free_event_filter(call->filter);
2374 call->filter = NULL;
2377 static int probe_remove_event_call(struct trace_event_call *call)
2379 struct trace_array *tr;
2380 struct trace_event_file *file;
2382 #ifdef CONFIG_PERF_EVENTS
2383 if (call->perf_refcount)
2384 return -EBUSY;
2385 #endif
2386 do_for_each_event_file(tr, file) {
2387 if (file->event_call != call)
2388 continue;
2390 * We can't rely on ftrace_event_enable_disable(enable => 0)
2391 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2392 * TRACE_REG_UNREGISTER.
2394 if (file->flags & EVENT_FILE_FL_ENABLED)
2395 return -EBUSY;
2397 * The do_for_each_event_file_safe() is
2398 * a double loop. After finding the call for this
2399 * trace_array, we use break to jump to the next
2400 * trace_array.
2402 break;
2403 } while_for_each_event_file();
2405 __trace_remove_event_call(call);
2407 return 0;
2410 /* Remove an event_call */
2411 int trace_remove_event_call(struct trace_event_call *call)
2413 int ret;
2415 lockdep_assert_held(&event_mutex);
2417 mutex_lock(&trace_types_lock);
2418 down_write(&trace_event_sem);
2419 ret = probe_remove_event_call(call);
2420 up_write(&trace_event_sem);
2421 mutex_unlock(&trace_types_lock);
2423 return ret;
2426 #define for_each_event(event, start, end) \
2427 for (event = start; \
2428 (unsigned long)event < (unsigned long)end; \
2429 event++)
2431 #ifdef CONFIG_MODULES
2433 static void trace_module_add_events(struct module *mod)
2435 struct trace_event_call **call, **start, **end;
2437 if (!mod->num_trace_events)
2438 return;
2440 /* Don't add infrastructure for mods without tracepoints */
2441 if (trace_module_has_bad_taint(mod)) {
2442 pr_err("%s: module has bad taint, not creating trace events\n",
2443 mod->name);
2444 return;
2447 start = mod->trace_events;
2448 end = mod->trace_events + mod->num_trace_events;
2450 for_each_event(call, start, end) {
2451 __register_event(*call, mod);
2452 __add_event_to_tracers(*call);
2456 static void trace_module_remove_events(struct module *mod)
2458 struct trace_event_call *call, *p;
2460 down_write(&trace_event_sem);
2461 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2462 if (call->mod == mod)
2463 __trace_remove_event_call(call);
2465 up_write(&trace_event_sem);
2468 * It is safest to reset the ring buffer if the module being unloaded
2469 * registered any events that were used. The only worry is if
2470 * a new module gets loaded, and takes on the same id as the events
2471 * of this module. When printing out the buffer, traced events left
2472 * over from this module may be passed to the new module events and
2473 * unexpected results may occur.
2475 tracing_reset_all_online_cpus();
2478 static int trace_module_notify(struct notifier_block *self,
2479 unsigned long val, void *data)
2481 struct module *mod = data;
2483 mutex_lock(&event_mutex);
2484 mutex_lock(&trace_types_lock);
2485 switch (val) {
2486 case MODULE_STATE_COMING:
2487 trace_module_add_events(mod);
2488 break;
2489 case MODULE_STATE_GOING:
2490 trace_module_remove_events(mod);
2491 break;
2493 mutex_unlock(&trace_types_lock);
2494 mutex_unlock(&event_mutex);
2496 return 0;
2499 static struct notifier_block trace_module_nb = {
2500 .notifier_call = trace_module_notify,
2501 .priority = 1, /* higher than trace.c module notify */
2503 #endif /* CONFIG_MODULES */
2505 /* Create a new event directory structure for a trace directory. */
2506 static void
2507 __trace_add_event_dirs(struct trace_array *tr)
2509 struct trace_event_call *call;
2510 int ret;
2512 list_for_each_entry(call, &ftrace_events, list) {
2513 ret = __trace_add_new_event(call, tr);
2514 if (ret < 0)
2515 pr_warn("Could not create directory for event %s\n",
2516 trace_event_name(call));
2520 /* Returns any file that matches the system and event */
2521 struct trace_event_file *
2522 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2524 struct trace_event_file *file;
2525 struct trace_event_call *call;
2526 const char *name;
2528 list_for_each_entry(file, &tr->events, list) {
2530 call = file->event_call;
2531 name = trace_event_name(call);
2533 if (!name || !call->class)
2534 continue;
2536 if (strcmp(event, name) == 0 &&
2537 strcmp(system, call->class->system) == 0)
2538 return file;
2540 return NULL;
2543 /* Returns valid trace event files that match system and event */
2544 struct trace_event_file *
2545 find_event_file(struct trace_array *tr, const char *system, const char *event)
2547 struct trace_event_file *file;
2549 file = __find_event_file(tr, system, event);
2550 if (!file || !file->event_call->class->reg ||
2551 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2552 return NULL;
2554 return file;
2558 * trace_get_event_file - Find and return a trace event file
2559 * @instance: The name of the trace instance containing the event
2560 * @system: The name of the system containing the event
2561 * @event: The name of the event
2563 * Return a trace event file given the trace instance name, trace
2564 * system, and trace event name. If the instance name is NULL, it
2565 * refers to the top-level trace array.
2567 * This function will look it up and return it if found, after calling
2568 * trace_array_get() to prevent the instance from going away, and
2569 * increment the event's module refcount to prevent it from being
2570 * removed.
2572 * To release the file, call trace_put_event_file(), which will call
2573 * trace_array_put() and decrement the event's module refcount.
2575 * Return: The trace event on success, ERR_PTR otherwise.
2577 struct trace_event_file *trace_get_event_file(const char *instance,
2578 const char *system,
2579 const char *event)
2581 struct trace_array *tr = top_trace_array();
2582 struct trace_event_file *file = NULL;
2583 int ret = -EINVAL;
2585 if (instance) {
2586 tr = trace_array_find_get(instance);
2587 if (!tr)
2588 return ERR_PTR(-ENOENT);
2589 } else {
2590 ret = trace_array_get(tr);
2591 if (ret)
2592 return ERR_PTR(ret);
2595 mutex_lock(&event_mutex);
2597 file = find_event_file(tr, system, event);
2598 if (!file) {
2599 trace_array_put(tr);
2600 ret = -EINVAL;
2601 goto out;
2604 /* Don't let event modules unload while in use */
2605 ret = try_module_get(file->event_call->mod);
2606 if (!ret) {
2607 trace_array_put(tr);
2608 ret = -EBUSY;
2609 goto out;
2612 ret = 0;
2613 out:
2614 mutex_unlock(&event_mutex);
2616 if (ret)
2617 file = ERR_PTR(ret);
2619 return file;
2621 EXPORT_SYMBOL_GPL(trace_get_event_file);
2624 * trace_put_event_file - Release a file from trace_get_event_file()
2625 * @file: The trace event file
2627 * If a file was retrieved using trace_get_event_file(), this should
2628 * be called when it's no longer needed. It will cancel the previous
2629 * trace_array_get() called by that function, and decrement the
2630 * event's module refcount.
2632 void trace_put_event_file(struct trace_event_file *file)
2634 mutex_lock(&event_mutex);
2635 module_put(file->event_call->mod);
2636 mutex_unlock(&event_mutex);
2638 trace_array_put(file->tr);
2640 EXPORT_SYMBOL_GPL(trace_put_event_file);
2642 #ifdef CONFIG_DYNAMIC_FTRACE
2644 /* Avoid typos */
2645 #define ENABLE_EVENT_STR "enable_event"
2646 #define DISABLE_EVENT_STR "disable_event"
2648 struct event_probe_data {
2649 struct trace_event_file *file;
2650 unsigned long count;
2651 int ref;
2652 bool enable;
2655 static void update_event_probe(struct event_probe_data *data)
2657 if (data->enable)
2658 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2659 else
2660 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2663 static void
2664 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2665 struct trace_array *tr, struct ftrace_probe_ops *ops,
2666 void *data)
2668 struct ftrace_func_mapper *mapper = data;
2669 struct event_probe_data *edata;
2670 void **pdata;
2672 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2673 if (!pdata || !*pdata)
2674 return;
2676 edata = *pdata;
2677 update_event_probe(edata);
2680 static void
2681 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2682 struct trace_array *tr, struct ftrace_probe_ops *ops,
2683 void *data)
2685 struct ftrace_func_mapper *mapper = data;
2686 struct event_probe_data *edata;
2687 void **pdata;
2689 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2690 if (!pdata || !*pdata)
2691 return;
2693 edata = *pdata;
2695 if (!edata->count)
2696 return;
2698 /* Skip if the event is in a state we want to switch to */
2699 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2700 return;
2702 if (edata->count != -1)
2703 (edata->count)--;
2705 update_event_probe(edata);
2708 static int
2709 event_enable_print(struct seq_file *m, unsigned long ip,
2710 struct ftrace_probe_ops *ops, void *data)
2712 struct ftrace_func_mapper *mapper = data;
2713 struct event_probe_data *edata;
2714 void **pdata;
2716 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2718 if (WARN_ON_ONCE(!pdata || !*pdata))
2719 return 0;
2721 edata = *pdata;
2723 seq_printf(m, "%ps:", (void *)ip);
2725 seq_printf(m, "%s:%s:%s",
2726 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2727 edata->file->event_call->class->system,
2728 trace_event_name(edata->file->event_call));
2730 if (edata->count == -1)
2731 seq_puts(m, ":unlimited\n");
2732 else
2733 seq_printf(m, ":count=%ld\n", edata->count);
2735 return 0;
2738 static int
2739 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2740 unsigned long ip, void *init_data, void **data)
2742 struct ftrace_func_mapper *mapper = *data;
2743 struct event_probe_data *edata = init_data;
2744 int ret;
2746 if (!mapper) {
2747 mapper = allocate_ftrace_func_mapper();
2748 if (!mapper)
2749 return -ENODEV;
2750 *data = mapper;
2753 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2754 if (ret < 0)
2755 return ret;
2757 edata->ref++;
2759 return 0;
2762 static int free_probe_data(void *data)
2764 struct event_probe_data *edata = data;
2766 edata->ref--;
2767 if (!edata->ref) {
2768 /* Remove the SOFT_MODE flag */
2769 __ftrace_event_enable_disable(edata->file, 0, 1);
2770 module_put(edata->file->event_call->mod);
2771 kfree(edata);
2773 return 0;
2776 static void
2777 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2778 unsigned long ip, void *data)
2780 struct ftrace_func_mapper *mapper = data;
2781 struct event_probe_data *edata;
2783 if (!ip) {
2784 if (!mapper)
2785 return;
2786 free_ftrace_func_mapper(mapper, free_probe_data);
2787 return;
2790 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2792 if (WARN_ON_ONCE(!edata))
2793 return;
2795 if (WARN_ON_ONCE(edata->ref <= 0))
2796 return;
2798 free_probe_data(edata);
2801 static struct ftrace_probe_ops event_enable_probe_ops = {
2802 .func = event_enable_probe,
2803 .print = event_enable_print,
2804 .init = event_enable_init,
2805 .free = event_enable_free,
2808 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2809 .func = event_enable_count_probe,
2810 .print = event_enable_print,
2811 .init = event_enable_init,
2812 .free = event_enable_free,
2815 static struct ftrace_probe_ops event_disable_probe_ops = {
2816 .func = event_enable_probe,
2817 .print = event_enable_print,
2818 .init = event_enable_init,
2819 .free = event_enable_free,
2822 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2823 .func = event_enable_count_probe,
2824 .print = event_enable_print,
2825 .init = event_enable_init,
2826 .free = event_enable_free,
2829 static int
2830 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
2831 char *glob, char *cmd, char *param, int enabled)
2833 struct trace_event_file *file;
2834 struct ftrace_probe_ops *ops;
2835 struct event_probe_data *data;
2836 const char *system;
2837 const char *event;
2838 char *number;
2839 bool enable;
2840 int ret;
2842 if (!tr)
2843 return -ENODEV;
2845 /* hash funcs only work with set_ftrace_filter */
2846 if (!enabled || !param)
2847 return -EINVAL;
2849 system = strsep(&param, ":");
2850 if (!param)
2851 return -EINVAL;
2853 event = strsep(&param, ":");
2855 mutex_lock(&event_mutex);
2857 ret = -EINVAL;
2858 file = find_event_file(tr, system, event);
2859 if (!file)
2860 goto out;
2862 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2864 if (enable)
2865 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2866 else
2867 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2869 if (glob[0] == '!') {
2870 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
2871 goto out;
2874 ret = -ENOMEM;
2876 data = kzalloc(sizeof(*data), GFP_KERNEL);
2877 if (!data)
2878 goto out;
2880 data->enable = enable;
2881 data->count = -1;
2882 data->file = file;
2884 if (!param)
2885 goto out_reg;
2887 number = strsep(&param, ":");
2889 ret = -EINVAL;
2890 if (!strlen(number))
2891 goto out_free;
2894 * We use the callback data field (which is a pointer)
2895 * as our counter.
2897 ret = kstrtoul(number, 0, &data->count);
2898 if (ret)
2899 goto out_free;
2901 out_reg:
2902 /* Don't let event modules unload while probe registered */
2903 ret = try_module_get(file->event_call->mod);
2904 if (!ret) {
2905 ret = -EBUSY;
2906 goto out_free;
2909 ret = __ftrace_event_enable_disable(file, 1, 1);
2910 if (ret < 0)
2911 goto out_put;
2913 ret = register_ftrace_function_probe(glob, tr, ops, data);
2915 * The above returns on success the # of functions enabled,
2916 * but if it didn't find any functions it returns zero.
2917 * Consider no functions a failure too.
2919 if (!ret) {
2920 ret = -ENOENT;
2921 goto out_disable;
2922 } else if (ret < 0)
2923 goto out_disable;
2924 /* Just return zero, not the number of enabled functions */
2925 ret = 0;
2926 out:
2927 mutex_unlock(&event_mutex);
2928 return ret;
2930 out_disable:
2931 __ftrace_event_enable_disable(file, 0, 1);
2932 out_put:
2933 module_put(file->event_call->mod);
2934 out_free:
2935 kfree(data);
2936 goto out;
2939 static struct ftrace_func_command event_enable_cmd = {
2940 .name = ENABLE_EVENT_STR,
2941 .func = event_enable_func,
2944 static struct ftrace_func_command event_disable_cmd = {
2945 .name = DISABLE_EVENT_STR,
2946 .func = event_enable_func,
2949 static __init int register_event_cmds(void)
2951 int ret;
2953 ret = register_ftrace_command(&event_enable_cmd);
2954 if (WARN_ON(ret < 0))
2955 return ret;
2956 ret = register_ftrace_command(&event_disable_cmd);
2957 if (WARN_ON(ret < 0))
2958 unregister_ftrace_command(&event_enable_cmd);
2959 return ret;
2961 #else
2962 static inline int register_event_cmds(void) { return 0; }
2963 #endif /* CONFIG_DYNAMIC_FTRACE */
2966 * The top level array has already had its trace_event_file
2967 * descriptors created in order to allow for early events to
2968 * be recorded. This function is called after the tracefs has been
2969 * initialized, and we now have to create the files associated
2970 * to the events.
2972 static __init void
2973 __trace_early_add_event_dirs(struct trace_array *tr)
2975 struct trace_event_file *file;
2976 int ret;
2979 list_for_each_entry(file, &tr->events, list) {
2980 ret = event_create_dir(tr->event_dir, file);
2981 if (ret < 0)
2982 pr_warn("Could not create directory for event %s\n",
2983 trace_event_name(file->event_call));
2988 * For early boot up, the top trace array requires to have
2989 * a list of events that can be enabled. This must be done before
2990 * the filesystem is set up in order to allow events to be traced
2991 * early.
2993 static __init void
2994 __trace_early_add_events(struct trace_array *tr)
2996 struct trace_event_call *call;
2997 int ret;
2999 list_for_each_entry(call, &ftrace_events, list) {
3000 /* Early boot up should not have any modules loaded */
3001 if (WARN_ON_ONCE(call->mod))
3002 continue;
3004 ret = __trace_early_add_new_event(call, tr);
3005 if (ret < 0)
3006 pr_warn("Could not create early event %s\n",
3007 trace_event_name(call));
3011 /* Remove the event directory structure for a trace directory. */
3012 static void
3013 __trace_remove_event_dirs(struct trace_array *tr)
3015 struct trace_event_file *file, *next;
3017 list_for_each_entry_safe(file, next, &tr->events, list)
3018 remove_event_file_dir(file);
3021 static void __add_event_to_tracers(struct trace_event_call *call)
3023 struct trace_array *tr;
3025 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3026 __trace_add_new_event(call, tr);
3029 extern struct trace_event_call *__start_ftrace_events[];
3030 extern struct trace_event_call *__stop_ftrace_events[];
3032 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3034 static __init int setup_trace_event(char *str)
3036 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3037 ring_buffer_expanded = true;
3038 tracing_selftest_disabled = true;
3040 return 1;
3042 __setup("trace_event=", setup_trace_event);
3044 /* Expects to have event_mutex held when called */
3045 static int
3046 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3048 struct dentry *d_events;
3049 struct dentry *entry;
3051 entry = tracefs_create_file("set_event", 0644, parent,
3052 tr, &ftrace_set_event_fops);
3053 if (!entry) {
3054 pr_warn("Could not create tracefs 'set_event' entry\n");
3055 return -ENOMEM;
3058 d_events = tracefs_create_dir("events", parent);
3059 if (!d_events) {
3060 pr_warn("Could not create tracefs 'events' directory\n");
3061 return -ENOMEM;
3064 entry = trace_create_file("enable", 0644, d_events,
3065 tr, &ftrace_tr_enable_fops);
3066 if (!entry) {
3067 pr_warn("Could not create tracefs 'enable' entry\n");
3068 return -ENOMEM;
3071 /* There are not as crucial, just warn if they are not created */
3073 entry = tracefs_create_file("set_event_pid", 0644, parent,
3074 tr, &ftrace_set_event_pid_fops);
3075 if (!entry)
3076 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3078 /* ring buffer internal formats */
3079 entry = trace_create_file("header_page", 0444, d_events,
3080 ring_buffer_print_page_header,
3081 &ftrace_show_header_fops);
3082 if (!entry)
3083 pr_warn("Could not create tracefs 'header_page' entry\n");
3085 entry = trace_create_file("header_event", 0444, d_events,
3086 ring_buffer_print_entry_header,
3087 &ftrace_show_header_fops);
3088 if (!entry)
3089 pr_warn("Could not create tracefs 'header_event' entry\n");
3091 tr->event_dir = d_events;
3093 return 0;
3097 * event_trace_add_tracer - add a instance of a trace_array to events
3098 * @parent: The parent dentry to place the files/directories for events in
3099 * @tr: The trace array associated with these events
3101 * When a new instance is created, it needs to set up its events
3102 * directory, as well as other files associated with events. It also
3103 * creates the event hierachry in the @parent/events directory.
3105 * Returns 0 on success.
3107 * Must be called with event_mutex held.
3109 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3111 int ret;
3113 lockdep_assert_held(&event_mutex);
3115 ret = create_event_toplevel_files(parent, tr);
3116 if (ret)
3117 goto out;
3119 down_write(&trace_event_sem);
3120 __trace_add_event_dirs(tr);
3121 up_write(&trace_event_sem);
3123 out:
3124 return ret;
3128 * The top trace array already had its file descriptors created.
3129 * Now the files themselves need to be created.
3131 static __init int
3132 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3134 int ret;
3136 mutex_lock(&event_mutex);
3138 ret = create_event_toplevel_files(parent, tr);
3139 if (ret)
3140 goto out_unlock;
3142 down_write(&trace_event_sem);
3143 __trace_early_add_event_dirs(tr);
3144 up_write(&trace_event_sem);
3146 out_unlock:
3147 mutex_unlock(&event_mutex);
3149 return ret;
3152 /* Must be called with event_mutex held */
3153 int event_trace_del_tracer(struct trace_array *tr)
3155 lockdep_assert_held(&event_mutex);
3157 /* Disable any event triggers and associated soft-disabled events */
3158 clear_event_triggers(tr);
3160 /* Clear the pid list */
3161 __ftrace_clear_event_pids(tr);
3163 /* Disable any running events */
3164 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3166 /* Make sure no more events are being executed */
3167 tracepoint_synchronize_unregister();
3169 down_write(&trace_event_sem);
3170 __trace_remove_event_dirs(tr);
3171 tracefs_remove(tr->event_dir);
3172 up_write(&trace_event_sem);
3174 tr->event_dir = NULL;
3176 return 0;
3179 static __init int event_trace_memsetup(void)
3181 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3182 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3183 return 0;
3186 static __init void
3187 early_enable_events(struct trace_array *tr, bool disable_first)
3189 char *buf = bootup_event_buf;
3190 char *token;
3191 int ret;
3193 while (true) {
3194 token = strsep(&buf, ",");
3196 if (!token)
3197 break;
3199 if (*token) {
3200 /* Restarting syscalls requires that we stop them first */
3201 if (disable_first)
3202 ftrace_set_clr_event(tr, token, 0);
3204 ret = ftrace_set_clr_event(tr, token, 1);
3205 if (ret)
3206 pr_warn("Failed to enable trace event: %s\n", token);
3209 /* Put back the comma to allow this to be called again */
3210 if (buf)
3211 *(buf - 1) = ',';
3215 static __init int event_trace_enable(void)
3217 struct trace_array *tr = top_trace_array();
3218 struct trace_event_call **iter, *call;
3219 int ret;
3221 if (!tr)
3222 return -ENODEV;
3224 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3226 call = *iter;
3227 ret = event_init(call);
3228 if (!ret)
3229 list_add(&call->list, &ftrace_events);
3233 * We need the top trace array to have a working set of trace
3234 * points at early init, before the debug files and directories
3235 * are created. Create the file entries now, and attach them
3236 * to the actual file dentries later.
3238 __trace_early_add_events(tr);
3240 early_enable_events(tr, false);
3242 trace_printk_start_comm();
3244 register_event_cmds();
3246 register_trigger_cmds();
3248 return 0;
3252 * event_trace_enable() is called from trace_event_init() first to
3253 * initialize events and perhaps start any events that are on the
3254 * command line. Unfortunately, there are some events that will not
3255 * start this early, like the system call tracepoints that need
3256 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3257 * is called before pid 1 starts, and this flag is never set, making
3258 * the syscall tracepoint never get reached, but the event is enabled
3259 * regardless (and not doing anything).
3261 static __init int event_trace_enable_again(void)
3263 struct trace_array *tr;
3265 tr = top_trace_array();
3266 if (!tr)
3267 return -ENODEV;
3269 early_enable_events(tr, true);
3271 return 0;
3274 early_initcall(event_trace_enable_again);
3276 __init int event_trace_init(void)
3278 struct trace_array *tr;
3279 struct dentry *d_tracer;
3280 struct dentry *entry;
3281 int ret;
3283 tr = top_trace_array();
3284 if (!tr)
3285 return -ENODEV;
3287 d_tracer = tracing_init_dentry();
3288 if (IS_ERR(d_tracer))
3289 return 0;
3291 entry = tracefs_create_file("available_events", 0444, d_tracer,
3292 tr, &ftrace_avail_fops);
3293 if (!entry)
3294 pr_warn("Could not create tracefs 'available_events' entry\n");
3296 if (trace_define_generic_fields())
3297 pr_warn("tracing: Failed to allocated generic fields");
3299 if (trace_define_common_fields())
3300 pr_warn("tracing: Failed to allocate common fields");
3302 ret = early_event_add_tracer(d_tracer, tr);
3303 if (ret)
3304 return ret;
3306 #ifdef CONFIG_MODULES
3307 ret = register_module_notifier(&trace_module_nb);
3308 if (ret)
3309 pr_warn("Failed to register trace events module notifier\n");
3310 #endif
3311 return 0;
3314 void __init trace_event_init(void)
3316 event_trace_memsetup();
3317 init_ftrace_syscalls();
3318 event_trace_enable();
3321 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3323 static DEFINE_SPINLOCK(test_spinlock);
3324 static DEFINE_SPINLOCK(test_spinlock_irq);
3325 static DEFINE_MUTEX(test_mutex);
3327 static __init void test_work(struct work_struct *dummy)
3329 spin_lock(&test_spinlock);
3330 spin_lock_irq(&test_spinlock_irq);
3331 udelay(1);
3332 spin_unlock_irq(&test_spinlock_irq);
3333 spin_unlock(&test_spinlock);
3335 mutex_lock(&test_mutex);
3336 msleep(1);
3337 mutex_unlock(&test_mutex);
3340 static __init int event_test_thread(void *unused)
3342 void *test_malloc;
3344 test_malloc = kmalloc(1234, GFP_KERNEL);
3345 if (!test_malloc)
3346 pr_info("failed to kmalloc\n");
3348 schedule_on_each_cpu(test_work);
3350 kfree(test_malloc);
3352 set_current_state(TASK_INTERRUPTIBLE);
3353 while (!kthread_should_stop()) {
3354 schedule();
3355 set_current_state(TASK_INTERRUPTIBLE);
3357 __set_current_state(TASK_RUNNING);
3359 return 0;
3363 * Do various things that may trigger events.
3365 static __init void event_test_stuff(void)
3367 struct task_struct *test_thread;
3369 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3370 msleep(1);
3371 kthread_stop(test_thread);
3375 * For every trace event defined, we will test each trace point separately,
3376 * and then by groups, and finally all trace points.
3378 static __init void event_trace_self_tests(void)
3380 struct trace_subsystem_dir *dir;
3381 struct trace_event_file *file;
3382 struct trace_event_call *call;
3383 struct event_subsystem *system;
3384 struct trace_array *tr;
3385 int ret;
3387 tr = top_trace_array();
3388 if (!tr)
3389 return;
3391 pr_info("Running tests on trace events:\n");
3393 list_for_each_entry(file, &tr->events, list) {
3395 call = file->event_call;
3397 /* Only test those that have a probe */
3398 if (!call->class || !call->class->probe)
3399 continue;
3402 * Testing syscall events here is pretty useless, but
3403 * we still do it if configured. But this is time consuming.
3404 * What we really need is a user thread to perform the
3405 * syscalls as we test.
3407 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3408 if (call->class->system &&
3409 strcmp(call->class->system, "syscalls") == 0)
3410 continue;
3411 #endif
3413 pr_info("Testing event %s: ", trace_event_name(call));
3416 * If an event is already enabled, someone is using
3417 * it and the self test should not be on.
3419 if (file->flags & EVENT_FILE_FL_ENABLED) {
3420 pr_warn("Enabled event during self test!\n");
3421 WARN_ON_ONCE(1);
3422 continue;
3425 ftrace_event_enable_disable(file, 1);
3426 event_test_stuff();
3427 ftrace_event_enable_disable(file, 0);
3429 pr_cont("OK\n");
3432 /* Now test at the sub system level */
3434 pr_info("Running tests on trace event systems:\n");
3436 list_for_each_entry(dir, &tr->systems, list) {
3438 system = dir->subsystem;
3440 /* the ftrace system is special, skip it */
3441 if (strcmp(system->name, "ftrace") == 0)
3442 continue;
3444 pr_info("Testing event system %s: ", system->name);
3446 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3447 if (WARN_ON_ONCE(ret)) {
3448 pr_warn("error enabling system %s\n",
3449 system->name);
3450 continue;
3453 event_test_stuff();
3455 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3456 if (WARN_ON_ONCE(ret)) {
3457 pr_warn("error disabling system %s\n",
3458 system->name);
3459 continue;
3462 pr_cont("OK\n");
3465 /* Test with all events enabled */
3467 pr_info("Running tests on all trace events:\n");
3468 pr_info("Testing all events: ");
3470 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3471 if (WARN_ON_ONCE(ret)) {
3472 pr_warn("error enabling all events\n");
3473 return;
3476 event_test_stuff();
3478 /* reset sysname */
3479 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3480 if (WARN_ON_ONCE(ret)) {
3481 pr_warn("error disabling all events\n");
3482 return;
3485 pr_cont("OK\n");
3488 #ifdef CONFIG_FUNCTION_TRACER
3490 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3492 static struct trace_event_file event_trace_file __initdata;
3494 static void __init
3495 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3496 struct ftrace_ops *op, struct pt_regs *pt_regs)
3498 struct trace_buffer *buffer;
3499 struct ring_buffer_event *event;
3500 struct ftrace_entry *entry;
3501 unsigned long flags;
3502 long disabled;
3503 int cpu;
3504 int pc;
3506 pc = preempt_count();
3507 preempt_disable_notrace();
3508 cpu = raw_smp_processor_id();
3509 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3511 if (disabled != 1)
3512 goto out;
3514 local_save_flags(flags);
3516 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3517 TRACE_FN, sizeof(*entry),
3518 flags, pc);
3519 if (!event)
3520 goto out;
3521 entry = ring_buffer_event_data(event);
3522 entry->ip = ip;
3523 entry->parent_ip = parent_ip;
3525 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3526 entry, flags, pc);
3527 out:
3528 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3529 preempt_enable_notrace();
3532 static struct ftrace_ops trace_ops __initdata =
3534 .func = function_test_events_call,
3535 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3538 static __init void event_trace_self_test_with_function(void)
3540 int ret;
3542 event_trace_file.tr = top_trace_array();
3543 if (WARN_ON(!event_trace_file.tr))
3544 return;
3546 ret = register_ftrace_function(&trace_ops);
3547 if (WARN_ON(ret < 0)) {
3548 pr_info("Failed to enable function tracer for event tests\n");
3549 return;
3551 pr_info("Running tests again, along with the function tracer\n");
3552 event_trace_self_tests();
3553 unregister_ftrace_function(&trace_ops);
3555 #else
3556 static __init void event_trace_self_test_with_function(void)
3559 #endif
3561 static __init int event_trace_self_tests_init(void)
3563 if (!tracing_selftest_disabled) {
3564 event_trace_self_tests();
3565 event_trace_self_test_with_function();
3568 return 0;
3571 late_initcall(event_trace_self_tests_init);
3573 #endif