fs/affs/amigaffs.c: remove unneeded initialization
[linux-2.6/btrfs-unstable.git] / kernel / trace / trace_events.c
blob404a372ad85a94545638d043bf86d113dfe9216f
1 /*
2 * event tracer
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9 */
11 #define pr_fmt(fmt) fmt
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/tracefs.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
23 #include <asm/setup.h>
25 #include "trace_output.h"
27 #undef TRACE_SYSTEM
28 #define TRACE_SYSTEM "TRACE_SYSTEM"
30 DEFINE_MUTEX(event_mutex);
32 LIST_HEAD(ftrace_events);
33 static LIST_HEAD(ftrace_common_fields);
35 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37 static struct kmem_cache *field_cachep;
38 static struct kmem_cache *file_cachep;
40 #define SYSTEM_FL_FREE_NAME (1 << 31)
42 static inline int system_refcount(struct event_subsystem *system)
44 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
47 static int system_refcount_inc(struct event_subsystem *system)
49 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
52 static int system_refcount_dec(struct event_subsystem *system)
54 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
57 /* Double loops, do not use break, only goto's work */
58 #define do_for_each_event_file(tr, file) \
59 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
60 list_for_each_entry(file, &tr->events, list)
62 #define do_for_each_event_file_safe(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 struct trace_event_file *___n; \
65 list_for_each_entry_safe(file, ___n, &tr->events, list)
67 #define while_for_each_event_file() \
70 static struct list_head *
71 trace_get_fields(struct trace_event_call *event_call)
73 if (!event_call->class->get_fields)
74 return &event_call->class->fields;
75 return event_call->class->get_fields(event_call);
78 static struct ftrace_event_field *
79 __find_event_field(struct list_head *head, char *name)
81 struct ftrace_event_field *field;
83 list_for_each_entry(field, head, link) {
84 if (!strcmp(field->name, name))
85 return field;
88 return NULL;
91 struct ftrace_event_field *
92 trace_find_event_field(struct trace_event_call *call, char *name)
94 struct ftrace_event_field *field;
95 struct list_head *head;
97 field = __find_event_field(&ftrace_common_fields, name);
98 if (field)
99 return field;
101 head = trace_get_fields(call);
102 return __find_event_field(head, name);
105 static int __trace_define_field(struct list_head *head, const char *type,
106 const char *name, int offset, int size,
107 int is_signed, int filter_type)
109 struct ftrace_event_field *field;
111 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
112 if (!field)
113 return -ENOMEM;
115 field->name = name;
116 field->type = type;
118 if (filter_type == FILTER_OTHER)
119 field->filter_type = filter_assign_type(type);
120 else
121 field->filter_type = filter_type;
123 field->offset = offset;
124 field->size = size;
125 field->is_signed = is_signed;
127 list_add(&field->link, head);
129 return 0;
132 int trace_define_field(struct trace_event_call *call, const char *type,
133 const char *name, int offset, int size, int is_signed,
134 int filter_type)
136 struct list_head *head;
138 if (WARN_ON(!call->class))
139 return 0;
141 head = trace_get_fields(call);
142 return __trace_define_field(head, type, name, offset, size,
143 is_signed, filter_type);
145 EXPORT_SYMBOL_GPL(trace_define_field);
147 #define __common_field(type, item) \
148 ret = __trace_define_field(&ftrace_common_fields, #type, \
149 "common_" #item, \
150 offsetof(typeof(ent), item), \
151 sizeof(ent.item), \
152 is_signed_type(type), FILTER_OTHER); \
153 if (ret) \
154 return ret;
156 static int trace_define_common_fields(void)
158 int ret;
159 struct trace_entry ent;
161 __common_field(unsigned short, type);
162 __common_field(unsigned char, flags);
163 __common_field(unsigned char, preempt_count);
164 __common_field(int, pid);
166 return ret;
169 static void trace_destroy_fields(struct trace_event_call *call)
171 struct ftrace_event_field *field, *next;
172 struct list_head *head;
174 head = trace_get_fields(call);
175 list_for_each_entry_safe(field, next, head, link) {
176 list_del(&field->link);
177 kmem_cache_free(field_cachep, field);
181 int trace_event_raw_init(struct trace_event_call *call)
183 int id;
185 id = register_trace_event(&call->event);
186 if (!id)
187 return -ENODEV;
189 return 0;
191 EXPORT_SYMBOL_GPL(trace_event_raw_init);
193 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
194 struct trace_event_file *trace_file,
195 unsigned long len)
197 struct trace_event_call *event_call = trace_file->event_call;
199 local_save_flags(fbuffer->flags);
200 fbuffer->pc = preempt_count();
201 fbuffer->trace_file = trace_file;
203 fbuffer->event =
204 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
205 event_call->event.type, len,
206 fbuffer->flags, fbuffer->pc);
207 if (!fbuffer->event)
208 return NULL;
210 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
211 return fbuffer->entry;
213 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
215 static DEFINE_SPINLOCK(tracepoint_iter_lock);
217 static void output_printk(struct trace_event_buffer *fbuffer)
219 struct trace_event_call *event_call;
220 struct trace_event *event;
221 unsigned long flags;
222 struct trace_iterator *iter = tracepoint_print_iter;
224 if (!iter)
225 return;
227 event_call = fbuffer->trace_file->event_call;
228 if (!event_call || !event_call->event.funcs ||
229 !event_call->event.funcs->trace)
230 return;
232 event = &fbuffer->trace_file->event_call->event;
234 spin_lock_irqsave(&tracepoint_iter_lock, flags);
235 trace_seq_init(&iter->seq);
236 iter->ent = fbuffer->entry;
237 event_call->event.funcs->trace(iter, 0, event);
238 trace_seq_putc(&iter->seq, 0);
239 printk("%s", iter->seq.buffer);
241 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
244 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
246 if (tracepoint_printk)
247 output_printk(fbuffer);
249 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
250 fbuffer->event, fbuffer->entry,
251 fbuffer->flags, fbuffer->pc);
253 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
255 int trace_event_reg(struct trace_event_call *call,
256 enum trace_reg type, void *data)
258 struct trace_event_file *file = data;
260 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
261 switch (type) {
262 case TRACE_REG_REGISTER:
263 return tracepoint_probe_register(call->tp,
264 call->class->probe,
265 file);
266 case TRACE_REG_UNREGISTER:
267 tracepoint_probe_unregister(call->tp,
268 call->class->probe,
269 file);
270 return 0;
272 #ifdef CONFIG_PERF_EVENTS
273 case TRACE_REG_PERF_REGISTER:
274 return tracepoint_probe_register(call->tp,
275 call->class->perf_probe,
276 call);
277 case TRACE_REG_PERF_UNREGISTER:
278 tracepoint_probe_unregister(call->tp,
279 call->class->perf_probe,
280 call);
281 return 0;
282 case TRACE_REG_PERF_OPEN:
283 case TRACE_REG_PERF_CLOSE:
284 case TRACE_REG_PERF_ADD:
285 case TRACE_REG_PERF_DEL:
286 return 0;
287 #endif
289 return 0;
291 EXPORT_SYMBOL_GPL(trace_event_reg);
293 void trace_event_enable_cmd_record(bool enable)
295 struct trace_event_file *file;
296 struct trace_array *tr;
298 mutex_lock(&event_mutex);
299 do_for_each_event_file(tr, file) {
301 if (!(file->flags & EVENT_FILE_FL_ENABLED))
302 continue;
304 if (enable) {
305 tracing_start_cmdline_record();
306 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
307 } else {
308 tracing_stop_cmdline_record();
309 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
311 } while_for_each_event_file();
312 mutex_unlock(&event_mutex);
315 static int __ftrace_event_enable_disable(struct trace_event_file *file,
316 int enable, int soft_disable)
318 struct trace_event_call *call = file->event_call;
319 int ret = 0;
320 int disable;
322 switch (enable) {
323 case 0:
325 * When soft_disable is set and enable is cleared, the sm_ref
326 * reference counter is decremented. If it reaches 0, we want
327 * to clear the SOFT_DISABLED flag but leave the event in the
328 * state that it was. That is, if the event was enabled and
329 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
330 * is set we do not want the event to be enabled before we
331 * clear the bit.
333 * When soft_disable is not set but the SOFT_MODE flag is,
334 * we do nothing. Do not disable the tracepoint, otherwise
335 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
337 if (soft_disable) {
338 if (atomic_dec_return(&file->sm_ref) > 0)
339 break;
340 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
341 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
342 } else
343 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
345 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
346 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
347 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
348 tracing_stop_cmdline_record();
349 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
351 call->class->reg(call, TRACE_REG_UNREGISTER, file);
353 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
354 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
355 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
356 else
357 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
358 break;
359 case 1:
361 * When soft_disable is set and enable is set, we want to
362 * register the tracepoint for the event, but leave the event
363 * as is. That means, if the event was already enabled, we do
364 * nothing (but set SOFT_MODE). If the event is disabled, we
365 * set SOFT_DISABLED before enabling the event tracepoint, so
366 * it still seems to be disabled.
368 if (!soft_disable)
369 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
370 else {
371 if (atomic_inc_return(&file->sm_ref) > 1)
372 break;
373 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
376 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
378 /* Keep the event disabled, when going to SOFT_MODE. */
379 if (soft_disable)
380 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
382 if (trace_flags & TRACE_ITER_RECORD_CMD) {
383 tracing_start_cmdline_record();
384 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
386 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
387 if (ret) {
388 tracing_stop_cmdline_record();
389 pr_info("event trace: Could not enable event "
390 "%s\n", trace_event_name(call));
391 break;
393 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
395 /* WAS_ENABLED gets set but never cleared. */
396 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
398 break;
401 return ret;
404 int trace_event_enable_disable(struct trace_event_file *file,
405 int enable, int soft_disable)
407 return __ftrace_event_enable_disable(file, enable, soft_disable);
410 static int ftrace_event_enable_disable(struct trace_event_file *file,
411 int enable)
413 return __ftrace_event_enable_disable(file, enable, 0);
416 static void ftrace_clear_events(struct trace_array *tr)
418 struct trace_event_file *file;
420 mutex_lock(&event_mutex);
421 list_for_each_entry(file, &tr->events, list) {
422 ftrace_event_enable_disable(file, 0);
424 mutex_unlock(&event_mutex);
427 static void __put_system(struct event_subsystem *system)
429 struct event_filter *filter = system->filter;
431 WARN_ON_ONCE(system_refcount(system) == 0);
432 if (system_refcount_dec(system))
433 return;
435 list_del(&system->list);
437 if (filter) {
438 kfree(filter->filter_string);
439 kfree(filter);
441 if (system->ref_count & SYSTEM_FL_FREE_NAME)
442 kfree(system->name);
443 kfree(system);
446 static void __get_system(struct event_subsystem *system)
448 WARN_ON_ONCE(system_refcount(system) == 0);
449 system_refcount_inc(system);
452 static void __get_system_dir(struct trace_subsystem_dir *dir)
454 WARN_ON_ONCE(dir->ref_count == 0);
455 dir->ref_count++;
456 __get_system(dir->subsystem);
459 static void __put_system_dir(struct trace_subsystem_dir *dir)
461 WARN_ON_ONCE(dir->ref_count == 0);
462 /* If the subsystem is about to be freed, the dir must be too */
463 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
465 __put_system(dir->subsystem);
466 if (!--dir->ref_count)
467 kfree(dir);
470 static void put_system(struct trace_subsystem_dir *dir)
472 mutex_lock(&event_mutex);
473 __put_system_dir(dir);
474 mutex_unlock(&event_mutex);
477 static void remove_subsystem(struct trace_subsystem_dir *dir)
479 if (!dir)
480 return;
482 if (!--dir->nr_events) {
483 tracefs_remove_recursive(dir->entry);
484 list_del(&dir->list);
485 __put_system_dir(dir);
489 static void remove_event_file_dir(struct trace_event_file *file)
491 struct dentry *dir = file->dir;
492 struct dentry *child;
494 if (dir) {
495 spin_lock(&dir->d_lock); /* probably unneeded */
496 list_for_each_entry(child, &dir->d_subdirs, d_child) {
497 if (d_really_is_positive(child)) /* probably unneeded */
498 d_inode(child)->i_private = NULL;
500 spin_unlock(&dir->d_lock);
502 tracefs_remove_recursive(dir);
505 list_del(&file->list);
506 remove_subsystem(file->system);
507 free_event_filter(file->filter);
508 kmem_cache_free(file_cachep, file);
512 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
514 static int
515 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
516 const char *sub, const char *event, int set)
518 struct trace_event_file *file;
519 struct trace_event_call *call;
520 const char *name;
521 int ret = -EINVAL;
523 list_for_each_entry(file, &tr->events, list) {
525 call = file->event_call;
526 name = trace_event_name(call);
528 if (!name || !call->class || !call->class->reg)
529 continue;
531 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
532 continue;
534 if (match &&
535 strcmp(match, name) != 0 &&
536 strcmp(match, call->class->system) != 0)
537 continue;
539 if (sub && strcmp(sub, call->class->system) != 0)
540 continue;
542 if (event && strcmp(event, name) != 0)
543 continue;
545 ftrace_event_enable_disable(file, set);
547 ret = 0;
550 return ret;
553 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
554 const char *sub, const char *event, int set)
556 int ret;
558 mutex_lock(&event_mutex);
559 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
560 mutex_unlock(&event_mutex);
562 return ret;
565 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
567 char *event = NULL, *sub = NULL, *match;
568 int ret;
571 * The buf format can be <subsystem>:<event-name>
572 * *:<event-name> means any event by that name.
573 * :<event-name> is the same.
575 * <subsystem>:* means all events in that subsystem
576 * <subsystem>: means the same.
578 * <name> (no ':') means all events in a subsystem with
579 * the name <name> or any event that matches <name>
582 match = strsep(&buf, ":");
583 if (buf) {
584 sub = match;
585 event = buf;
586 match = NULL;
588 if (!strlen(sub) || strcmp(sub, "*") == 0)
589 sub = NULL;
590 if (!strlen(event) || strcmp(event, "*") == 0)
591 event = NULL;
594 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
596 /* Put back the colon to allow this to be called again */
597 if (buf)
598 *(buf - 1) = ':';
600 return ret;
604 * trace_set_clr_event - enable or disable an event
605 * @system: system name to match (NULL for any system)
606 * @event: event name to match (NULL for all events, within system)
607 * @set: 1 to enable, 0 to disable
609 * This is a way for other parts of the kernel to enable or disable
610 * event recording.
612 * Returns 0 on success, -EINVAL if the parameters do not match any
613 * registered events.
615 int trace_set_clr_event(const char *system, const char *event, int set)
617 struct trace_array *tr = top_trace_array();
619 if (!tr)
620 return -ENODEV;
622 return __ftrace_set_clr_event(tr, NULL, system, event, set);
624 EXPORT_SYMBOL_GPL(trace_set_clr_event);
626 /* 128 should be much more than enough */
627 #define EVENT_BUF_SIZE 127
629 static ssize_t
630 ftrace_event_write(struct file *file, const char __user *ubuf,
631 size_t cnt, loff_t *ppos)
633 struct trace_parser parser;
634 struct seq_file *m = file->private_data;
635 struct trace_array *tr = m->private;
636 ssize_t read, ret;
638 if (!cnt)
639 return 0;
641 ret = tracing_update_buffers();
642 if (ret < 0)
643 return ret;
645 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
646 return -ENOMEM;
648 read = trace_get_user(&parser, ubuf, cnt, ppos);
650 if (read >= 0 && trace_parser_loaded((&parser))) {
651 int set = 1;
653 if (*parser.buffer == '!')
654 set = 0;
656 parser.buffer[parser.idx] = 0;
658 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
659 if (ret)
660 goto out_put;
663 ret = read;
665 out_put:
666 trace_parser_put(&parser);
668 return ret;
671 static void *
672 t_next(struct seq_file *m, void *v, loff_t *pos)
674 struct trace_event_file *file = v;
675 struct trace_event_call *call;
676 struct trace_array *tr = m->private;
678 (*pos)++;
680 list_for_each_entry_continue(file, &tr->events, list) {
681 call = file->event_call;
683 * The ftrace subsystem is for showing formats only.
684 * They can not be enabled or disabled via the event files.
686 if (call->class && call->class->reg)
687 return file;
690 return NULL;
693 static void *t_start(struct seq_file *m, loff_t *pos)
695 struct trace_event_file *file;
696 struct trace_array *tr = m->private;
697 loff_t l;
699 mutex_lock(&event_mutex);
701 file = list_entry(&tr->events, struct trace_event_file, list);
702 for (l = 0; l <= *pos; ) {
703 file = t_next(m, file, &l);
704 if (!file)
705 break;
707 return file;
710 static void *
711 s_next(struct seq_file *m, void *v, loff_t *pos)
713 struct trace_event_file *file = v;
714 struct trace_array *tr = m->private;
716 (*pos)++;
718 list_for_each_entry_continue(file, &tr->events, list) {
719 if (file->flags & EVENT_FILE_FL_ENABLED)
720 return file;
723 return NULL;
726 static void *s_start(struct seq_file *m, loff_t *pos)
728 struct trace_event_file *file;
729 struct trace_array *tr = m->private;
730 loff_t l;
732 mutex_lock(&event_mutex);
734 file = list_entry(&tr->events, struct trace_event_file, list);
735 for (l = 0; l <= *pos; ) {
736 file = s_next(m, file, &l);
737 if (!file)
738 break;
740 return file;
743 static int t_show(struct seq_file *m, void *v)
745 struct trace_event_file *file = v;
746 struct trace_event_call *call = file->event_call;
748 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
749 seq_printf(m, "%s:", call->class->system);
750 seq_printf(m, "%s\n", trace_event_name(call));
752 return 0;
755 static void t_stop(struct seq_file *m, void *p)
757 mutex_unlock(&event_mutex);
760 static ssize_t
761 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
762 loff_t *ppos)
764 struct trace_event_file *file;
765 unsigned long flags;
766 char buf[4] = "0";
768 mutex_lock(&event_mutex);
769 file = event_file_data(filp);
770 if (likely(file))
771 flags = file->flags;
772 mutex_unlock(&event_mutex);
774 if (!file)
775 return -ENODEV;
777 if (flags & EVENT_FILE_FL_ENABLED &&
778 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
779 strcpy(buf, "1");
781 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
782 flags & EVENT_FILE_FL_SOFT_MODE)
783 strcat(buf, "*");
785 strcat(buf, "\n");
787 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
790 static ssize_t
791 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
792 loff_t *ppos)
794 struct trace_event_file *file;
795 unsigned long val;
796 int ret;
798 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
799 if (ret)
800 return ret;
802 ret = tracing_update_buffers();
803 if (ret < 0)
804 return ret;
806 switch (val) {
807 case 0:
808 case 1:
809 ret = -ENODEV;
810 mutex_lock(&event_mutex);
811 file = event_file_data(filp);
812 if (likely(file))
813 ret = ftrace_event_enable_disable(file, val);
814 mutex_unlock(&event_mutex);
815 break;
817 default:
818 return -EINVAL;
821 *ppos += cnt;
823 return ret ? ret : cnt;
826 static ssize_t
827 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
828 loff_t *ppos)
830 const char set_to_char[4] = { '?', '0', '1', 'X' };
831 struct trace_subsystem_dir *dir = filp->private_data;
832 struct event_subsystem *system = dir->subsystem;
833 struct trace_event_call *call;
834 struct trace_event_file *file;
835 struct trace_array *tr = dir->tr;
836 char buf[2];
837 int set = 0;
838 int ret;
840 mutex_lock(&event_mutex);
841 list_for_each_entry(file, &tr->events, list) {
842 call = file->event_call;
843 if (!trace_event_name(call) || !call->class || !call->class->reg)
844 continue;
846 if (system && strcmp(call->class->system, system->name) != 0)
847 continue;
850 * We need to find out if all the events are set
851 * or if all events or cleared, or if we have
852 * a mixture.
854 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
857 * If we have a mixture, no need to look further.
859 if (set == 3)
860 break;
862 mutex_unlock(&event_mutex);
864 buf[0] = set_to_char[set];
865 buf[1] = '\n';
867 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
869 return ret;
872 static ssize_t
873 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
874 loff_t *ppos)
876 struct trace_subsystem_dir *dir = filp->private_data;
877 struct event_subsystem *system = dir->subsystem;
878 const char *name = NULL;
879 unsigned long val;
880 ssize_t ret;
882 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
883 if (ret)
884 return ret;
886 ret = tracing_update_buffers();
887 if (ret < 0)
888 return ret;
890 if (val != 0 && val != 1)
891 return -EINVAL;
894 * Opening of "enable" adds a ref count to system,
895 * so the name is safe to use.
897 if (system)
898 name = system->name;
900 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
901 if (ret)
902 goto out;
904 ret = cnt;
906 out:
907 *ppos += cnt;
909 return ret;
912 enum {
913 FORMAT_HEADER = 1,
914 FORMAT_FIELD_SEPERATOR = 2,
915 FORMAT_PRINTFMT = 3,
918 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
920 struct trace_event_call *call = event_file_data(m->private);
921 struct list_head *common_head = &ftrace_common_fields;
922 struct list_head *head = trace_get_fields(call);
923 struct list_head *node = v;
925 (*pos)++;
927 switch ((unsigned long)v) {
928 case FORMAT_HEADER:
929 node = common_head;
930 break;
932 case FORMAT_FIELD_SEPERATOR:
933 node = head;
934 break;
936 case FORMAT_PRINTFMT:
937 /* all done */
938 return NULL;
941 node = node->prev;
942 if (node == common_head)
943 return (void *)FORMAT_FIELD_SEPERATOR;
944 else if (node == head)
945 return (void *)FORMAT_PRINTFMT;
946 else
947 return node;
950 static int f_show(struct seq_file *m, void *v)
952 struct trace_event_call *call = event_file_data(m->private);
953 struct ftrace_event_field *field;
954 const char *array_descriptor;
956 switch ((unsigned long)v) {
957 case FORMAT_HEADER:
958 seq_printf(m, "name: %s\n", trace_event_name(call));
959 seq_printf(m, "ID: %d\n", call->event.type);
960 seq_puts(m, "format:\n");
961 return 0;
963 case FORMAT_FIELD_SEPERATOR:
964 seq_putc(m, '\n');
965 return 0;
967 case FORMAT_PRINTFMT:
968 seq_printf(m, "\nprint fmt: %s\n",
969 call->print_fmt);
970 return 0;
973 field = list_entry(v, struct ftrace_event_field, link);
975 * Smartly shows the array type(except dynamic array).
976 * Normal:
977 * field:TYPE VAR
978 * If TYPE := TYPE[LEN], it is shown:
979 * field:TYPE VAR[LEN]
981 array_descriptor = strchr(field->type, '[');
983 if (!strncmp(field->type, "__data_loc", 10))
984 array_descriptor = NULL;
986 if (!array_descriptor)
987 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
988 field->type, field->name, field->offset,
989 field->size, !!field->is_signed);
990 else
991 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
992 (int)(array_descriptor - field->type),
993 field->type, field->name,
994 array_descriptor, field->offset,
995 field->size, !!field->is_signed);
997 return 0;
1000 static void *f_start(struct seq_file *m, loff_t *pos)
1002 void *p = (void *)FORMAT_HEADER;
1003 loff_t l = 0;
1005 /* ->stop() is called even if ->start() fails */
1006 mutex_lock(&event_mutex);
1007 if (!event_file_data(m->private))
1008 return ERR_PTR(-ENODEV);
1010 while (l < *pos && p)
1011 p = f_next(m, p, &l);
1013 return p;
1016 static void f_stop(struct seq_file *m, void *p)
1018 mutex_unlock(&event_mutex);
1021 static const struct seq_operations trace_format_seq_ops = {
1022 .start = f_start,
1023 .next = f_next,
1024 .stop = f_stop,
1025 .show = f_show,
1028 static int trace_format_open(struct inode *inode, struct file *file)
1030 struct seq_file *m;
1031 int ret;
1033 ret = seq_open(file, &trace_format_seq_ops);
1034 if (ret < 0)
1035 return ret;
1037 m = file->private_data;
1038 m->private = file;
1040 return 0;
1043 static ssize_t
1044 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1046 int id = (long)event_file_data(filp);
1047 char buf[32];
1048 int len;
1050 if (*ppos)
1051 return 0;
1053 if (unlikely(!id))
1054 return -ENODEV;
1056 len = sprintf(buf, "%d\n", id);
1058 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1061 static ssize_t
1062 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1063 loff_t *ppos)
1065 struct trace_event_file *file;
1066 struct trace_seq *s;
1067 int r = -ENODEV;
1069 if (*ppos)
1070 return 0;
1072 s = kmalloc(sizeof(*s), GFP_KERNEL);
1074 if (!s)
1075 return -ENOMEM;
1077 trace_seq_init(s);
1079 mutex_lock(&event_mutex);
1080 file = event_file_data(filp);
1081 if (file)
1082 print_event_filter(file, s);
1083 mutex_unlock(&event_mutex);
1085 if (file)
1086 r = simple_read_from_buffer(ubuf, cnt, ppos,
1087 s->buffer, trace_seq_used(s));
1089 kfree(s);
1091 return r;
1094 static ssize_t
1095 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1096 loff_t *ppos)
1098 struct trace_event_file *file;
1099 char *buf;
1100 int err = -ENODEV;
1102 if (cnt >= PAGE_SIZE)
1103 return -EINVAL;
1105 buf = (char *)__get_free_page(GFP_TEMPORARY);
1106 if (!buf)
1107 return -ENOMEM;
1109 if (copy_from_user(buf, ubuf, cnt)) {
1110 free_page((unsigned long) buf);
1111 return -EFAULT;
1113 buf[cnt] = '\0';
1115 mutex_lock(&event_mutex);
1116 file = event_file_data(filp);
1117 if (file)
1118 err = apply_event_filter(file, buf);
1119 mutex_unlock(&event_mutex);
1121 free_page((unsigned long) buf);
1122 if (err < 0)
1123 return err;
1125 *ppos += cnt;
1127 return cnt;
1130 static LIST_HEAD(event_subsystems);
1132 static int subsystem_open(struct inode *inode, struct file *filp)
1134 struct event_subsystem *system = NULL;
1135 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1136 struct trace_array *tr;
1137 int ret;
1139 if (tracing_is_disabled())
1140 return -ENODEV;
1142 /* Make sure the system still exists */
1143 mutex_lock(&trace_types_lock);
1144 mutex_lock(&event_mutex);
1145 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1146 list_for_each_entry(dir, &tr->systems, list) {
1147 if (dir == inode->i_private) {
1148 /* Don't open systems with no events */
1149 if (dir->nr_events) {
1150 __get_system_dir(dir);
1151 system = dir->subsystem;
1153 goto exit_loop;
1157 exit_loop:
1158 mutex_unlock(&event_mutex);
1159 mutex_unlock(&trace_types_lock);
1161 if (!system)
1162 return -ENODEV;
1164 /* Some versions of gcc think dir can be uninitialized here */
1165 WARN_ON(!dir);
1167 /* Still need to increment the ref count of the system */
1168 if (trace_array_get(tr) < 0) {
1169 put_system(dir);
1170 return -ENODEV;
1173 ret = tracing_open_generic(inode, filp);
1174 if (ret < 0) {
1175 trace_array_put(tr);
1176 put_system(dir);
1179 return ret;
1182 static int system_tr_open(struct inode *inode, struct file *filp)
1184 struct trace_subsystem_dir *dir;
1185 struct trace_array *tr = inode->i_private;
1186 int ret;
1188 if (tracing_is_disabled())
1189 return -ENODEV;
1191 if (trace_array_get(tr) < 0)
1192 return -ENODEV;
1194 /* Make a temporary dir that has no system but points to tr */
1195 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1196 if (!dir) {
1197 trace_array_put(tr);
1198 return -ENOMEM;
1201 dir->tr = tr;
1203 ret = tracing_open_generic(inode, filp);
1204 if (ret < 0) {
1205 trace_array_put(tr);
1206 kfree(dir);
1207 return ret;
1210 filp->private_data = dir;
1212 return 0;
1215 static int subsystem_release(struct inode *inode, struct file *file)
1217 struct trace_subsystem_dir *dir = file->private_data;
1219 trace_array_put(dir->tr);
1222 * If dir->subsystem is NULL, then this is a temporary
1223 * descriptor that was made for a trace_array to enable
1224 * all subsystems.
1226 if (dir->subsystem)
1227 put_system(dir);
1228 else
1229 kfree(dir);
1231 return 0;
1234 static ssize_t
1235 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1236 loff_t *ppos)
1238 struct trace_subsystem_dir *dir = filp->private_data;
1239 struct event_subsystem *system = dir->subsystem;
1240 struct trace_seq *s;
1241 int r;
1243 if (*ppos)
1244 return 0;
1246 s = kmalloc(sizeof(*s), GFP_KERNEL);
1247 if (!s)
1248 return -ENOMEM;
1250 trace_seq_init(s);
1252 print_subsystem_event_filter(system, s);
1253 r = simple_read_from_buffer(ubuf, cnt, ppos,
1254 s->buffer, trace_seq_used(s));
1256 kfree(s);
1258 return r;
1261 static ssize_t
1262 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1263 loff_t *ppos)
1265 struct trace_subsystem_dir *dir = filp->private_data;
1266 char *buf;
1267 int err;
1269 if (cnt >= PAGE_SIZE)
1270 return -EINVAL;
1272 buf = (char *)__get_free_page(GFP_TEMPORARY);
1273 if (!buf)
1274 return -ENOMEM;
1276 if (copy_from_user(buf, ubuf, cnt)) {
1277 free_page((unsigned long) buf);
1278 return -EFAULT;
1280 buf[cnt] = '\0';
1282 err = apply_subsystem_event_filter(dir, buf);
1283 free_page((unsigned long) buf);
1284 if (err < 0)
1285 return err;
1287 *ppos += cnt;
1289 return cnt;
1292 static ssize_t
1293 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1295 int (*func)(struct trace_seq *s) = filp->private_data;
1296 struct trace_seq *s;
1297 int r;
1299 if (*ppos)
1300 return 0;
1302 s = kmalloc(sizeof(*s), GFP_KERNEL);
1303 if (!s)
1304 return -ENOMEM;
1306 trace_seq_init(s);
1308 func(s);
1309 r = simple_read_from_buffer(ubuf, cnt, ppos,
1310 s->buffer, trace_seq_used(s));
1312 kfree(s);
1314 return r;
1317 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1318 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1319 static int ftrace_event_release(struct inode *inode, struct file *file);
1321 static const struct seq_operations show_event_seq_ops = {
1322 .start = t_start,
1323 .next = t_next,
1324 .show = t_show,
1325 .stop = t_stop,
1328 static const struct seq_operations show_set_event_seq_ops = {
1329 .start = s_start,
1330 .next = s_next,
1331 .show = t_show,
1332 .stop = t_stop,
1335 static const struct file_operations ftrace_avail_fops = {
1336 .open = ftrace_event_avail_open,
1337 .read = seq_read,
1338 .llseek = seq_lseek,
1339 .release = seq_release,
1342 static const struct file_operations ftrace_set_event_fops = {
1343 .open = ftrace_event_set_open,
1344 .read = seq_read,
1345 .write = ftrace_event_write,
1346 .llseek = seq_lseek,
1347 .release = ftrace_event_release,
1350 static const struct file_operations ftrace_enable_fops = {
1351 .open = tracing_open_generic,
1352 .read = event_enable_read,
1353 .write = event_enable_write,
1354 .llseek = default_llseek,
1357 static const struct file_operations ftrace_event_format_fops = {
1358 .open = trace_format_open,
1359 .read = seq_read,
1360 .llseek = seq_lseek,
1361 .release = seq_release,
1364 static const struct file_operations ftrace_event_id_fops = {
1365 .read = event_id_read,
1366 .llseek = default_llseek,
1369 static const struct file_operations ftrace_event_filter_fops = {
1370 .open = tracing_open_generic,
1371 .read = event_filter_read,
1372 .write = event_filter_write,
1373 .llseek = default_llseek,
1376 static const struct file_operations ftrace_subsystem_filter_fops = {
1377 .open = subsystem_open,
1378 .read = subsystem_filter_read,
1379 .write = subsystem_filter_write,
1380 .llseek = default_llseek,
1381 .release = subsystem_release,
1384 static const struct file_operations ftrace_system_enable_fops = {
1385 .open = subsystem_open,
1386 .read = system_enable_read,
1387 .write = system_enable_write,
1388 .llseek = default_llseek,
1389 .release = subsystem_release,
1392 static const struct file_operations ftrace_tr_enable_fops = {
1393 .open = system_tr_open,
1394 .read = system_enable_read,
1395 .write = system_enable_write,
1396 .llseek = default_llseek,
1397 .release = subsystem_release,
1400 static const struct file_operations ftrace_show_header_fops = {
1401 .open = tracing_open_generic,
1402 .read = show_header,
1403 .llseek = default_llseek,
1406 static int
1407 ftrace_event_open(struct inode *inode, struct file *file,
1408 const struct seq_operations *seq_ops)
1410 struct seq_file *m;
1411 int ret;
1413 ret = seq_open(file, seq_ops);
1414 if (ret < 0)
1415 return ret;
1416 m = file->private_data;
1417 /* copy tr over to seq ops */
1418 m->private = inode->i_private;
1420 return ret;
1423 static int ftrace_event_release(struct inode *inode, struct file *file)
1425 struct trace_array *tr = inode->i_private;
1427 trace_array_put(tr);
1429 return seq_release(inode, file);
1432 static int
1433 ftrace_event_avail_open(struct inode *inode, struct file *file)
1435 const struct seq_operations *seq_ops = &show_event_seq_ops;
1437 return ftrace_event_open(inode, file, seq_ops);
1440 static int
1441 ftrace_event_set_open(struct inode *inode, struct file *file)
1443 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1444 struct trace_array *tr = inode->i_private;
1445 int ret;
1447 if (trace_array_get(tr) < 0)
1448 return -ENODEV;
1450 if ((file->f_mode & FMODE_WRITE) &&
1451 (file->f_flags & O_TRUNC))
1452 ftrace_clear_events(tr);
1454 ret = ftrace_event_open(inode, file, seq_ops);
1455 if (ret < 0)
1456 trace_array_put(tr);
1457 return ret;
1460 static struct event_subsystem *
1461 create_new_subsystem(const char *name)
1463 struct event_subsystem *system;
1465 /* need to create new entry */
1466 system = kmalloc(sizeof(*system), GFP_KERNEL);
1467 if (!system)
1468 return NULL;
1470 system->ref_count = 1;
1472 /* Only allocate if dynamic (kprobes and modules) */
1473 if (!core_kernel_data((unsigned long)name)) {
1474 system->ref_count |= SYSTEM_FL_FREE_NAME;
1475 system->name = kstrdup(name, GFP_KERNEL);
1476 if (!system->name)
1477 goto out_free;
1478 } else
1479 system->name = name;
1481 system->filter = NULL;
1483 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1484 if (!system->filter)
1485 goto out_free;
1487 list_add(&system->list, &event_subsystems);
1489 return system;
1491 out_free:
1492 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1493 kfree(system->name);
1494 kfree(system);
1495 return NULL;
1498 static struct dentry *
1499 event_subsystem_dir(struct trace_array *tr, const char *name,
1500 struct trace_event_file *file, struct dentry *parent)
1502 struct trace_subsystem_dir *dir;
1503 struct event_subsystem *system;
1504 struct dentry *entry;
1506 /* First see if we did not already create this dir */
1507 list_for_each_entry(dir, &tr->systems, list) {
1508 system = dir->subsystem;
1509 if (strcmp(system->name, name) == 0) {
1510 dir->nr_events++;
1511 file->system = dir;
1512 return dir->entry;
1516 /* Now see if the system itself exists. */
1517 list_for_each_entry(system, &event_subsystems, list) {
1518 if (strcmp(system->name, name) == 0)
1519 break;
1521 /* Reset system variable when not found */
1522 if (&system->list == &event_subsystems)
1523 system = NULL;
1525 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1526 if (!dir)
1527 goto out_fail;
1529 if (!system) {
1530 system = create_new_subsystem(name);
1531 if (!system)
1532 goto out_free;
1533 } else
1534 __get_system(system);
1536 dir->entry = tracefs_create_dir(name, parent);
1537 if (!dir->entry) {
1538 pr_warn("Failed to create system directory %s\n", name);
1539 __put_system(system);
1540 goto out_free;
1543 dir->tr = tr;
1544 dir->ref_count = 1;
1545 dir->nr_events = 1;
1546 dir->subsystem = system;
1547 file->system = dir;
1549 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1550 &ftrace_subsystem_filter_fops);
1551 if (!entry) {
1552 kfree(system->filter);
1553 system->filter = NULL;
1554 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1557 trace_create_file("enable", 0644, dir->entry, dir,
1558 &ftrace_system_enable_fops);
1560 list_add(&dir->list, &tr->systems);
1562 return dir->entry;
1564 out_free:
1565 kfree(dir);
1566 out_fail:
1567 /* Only print this message if failed on memory allocation */
1568 if (!dir || !system)
1569 pr_warn("No memory to create event subsystem %s\n", name);
1570 return NULL;
1573 static int
1574 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1576 struct trace_event_call *call = file->event_call;
1577 struct trace_array *tr = file->tr;
1578 struct list_head *head;
1579 struct dentry *d_events;
1580 const char *name;
1581 int ret;
1584 * If the trace point header did not define TRACE_SYSTEM
1585 * then the system would be called "TRACE_SYSTEM".
1587 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1588 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1589 if (!d_events)
1590 return -ENOMEM;
1591 } else
1592 d_events = parent;
1594 name = trace_event_name(call);
1595 file->dir = tracefs_create_dir(name, d_events);
1596 if (!file->dir) {
1597 pr_warn("Could not create tracefs '%s' directory\n", name);
1598 return -1;
1601 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1602 trace_create_file("enable", 0644, file->dir, file,
1603 &ftrace_enable_fops);
1605 #ifdef CONFIG_PERF_EVENTS
1606 if (call->event.type && call->class->reg)
1607 trace_create_file("id", 0444, file->dir,
1608 (void *)(long)call->event.type,
1609 &ftrace_event_id_fops);
1610 #endif
1613 * Other events may have the same class. Only update
1614 * the fields if they are not already defined.
1616 head = trace_get_fields(call);
1617 if (list_empty(head)) {
1618 ret = call->class->define_fields(call);
1619 if (ret < 0) {
1620 pr_warn("Could not initialize trace point events/%s\n",
1621 name);
1622 return -1;
1625 trace_create_file("filter", 0644, file->dir, file,
1626 &ftrace_event_filter_fops);
1628 trace_create_file("trigger", 0644, file->dir, file,
1629 &event_trigger_fops);
1631 trace_create_file("format", 0444, file->dir, call,
1632 &ftrace_event_format_fops);
1634 return 0;
1637 static void remove_event_from_tracers(struct trace_event_call *call)
1639 struct trace_event_file *file;
1640 struct trace_array *tr;
1642 do_for_each_event_file_safe(tr, file) {
1643 if (file->event_call != call)
1644 continue;
1646 remove_event_file_dir(file);
1648 * The do_for_each_event_file_safe() is
1649 * a double loop. After finding the call for this
1650 * trace_array, we use break to jump to the next
1651 * trace_array.
1653 break;
1654 } while_for_each_event_file();
1657 static void event_remove(struct trace_event_call *call)
1659 struct trace_array *tr;
1660 struct trace_event_file *file;
1662 do_for_each_event_file(tr, file) {
1663 if (file->event_call != call)
1664 continue;
1665 ftrace_event_enable_disable(file, 0);
1667 * The do_for_each_event_file() is
1668 * a double loop. After finding the call for this
1669 * trace_array, we use break to jump to the next
1670 * trace_array.
1672 break;
1673 } while_for_each_event_file();
1675 if (call->event.funcs)
1676 __unregister_trace_event(&call->event);
1677 remove_event_from_tracers(call);
1678 list_del(&call->list);
1681 static int event_init(struct trace_event_call *call)
1683 int ret = 0;
1684 const char *name;
1686 name = trace_event_name(call);
1687 if (WARN_ON(!name))
1688 return -EINVAL;
1690 if (call->class->raw_init) {
1691 ret = call->class->raw_init(call);
1692 if (ret < 0 && ret != -ENOSYS)
1693 pr_warn("Could not initialize trace events/%s\n", name);
1696 return ret;
1699 static int
1700 __register_event(struct trace_event_call *call, struct module *mod)
1702 int ret;
1704 ret = event_init(call);
1705 if (ret < 0)
1706 return ret;
1708 list_add(&call->list, &ftrace_events);
1709 call->mod = mod;
1711 return 0;
1714 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1716 int rlen;
1717 int elen;
1719 /* Find the length of the enum value as a string */
1720 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1721 /* Make sure there's enough room to replace the string with the value */
1722 if (len < elen)
1723 return NULL;
1725 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1727 /* Get the rest of the string of ptr */
1728 rlen = strlen(ptr + len);
1729 memmove(ptr + elen, ptr + len, rlen);
1730 /* Make sure we end the new string */
1731 ptr[elen + rlen] = 0;
1733 return ptr + elen;
1736 static void update_event_printk(struct trace_event_call *call,
1737 struct trace_enum_map *map)
1739 char *ptr;
1740 int quote = 0;
1741 int len = strlen(map->enum_string);
1743 for (ptr = call->print_fmt; *ptr; ptr++) {
1744 if (*ptr == '\\') {
1745 ptr++;
1746 /* paranoid */
1747 if (!*ptr)
1748 break;
1749 continue;
1751 if (*ptr == '"') {
1752 quote ^= 1;
1753 continue;
1755 if (quote)
1756 continue;
1757 if (isdigit(*ptr)) {
1758 /* skip numbers */
1759 do {
1760 ptr++;
1761 /* Check for alpha chars like ULL */
1762 } while (isalnum(*ptr));
1763 if (!*ptr)
1764 break;
1766 * A number must have some kind of delimiter after
1767 * it, and we can ignore that too.
1769 continue;
1771 if (isalpha(*ptr) || *ptr == '_') {
1772 if (strncmp(map->enum_string, ptr, len) == 0 &&
1773 !isalnum(ptr[len]) && ptr[len] != '_') {
1774 ptr = enum_replace(ptr, map, len);
1775 /* Hmm, enum string smaller than value */
1776 if (WARN_ON_ONCE(!ptr))
1777 return;
1779 * No need to decrement here, as enum_replace()
1780 * returns the pointer to the character passed
1781 * the enum, and two enums can not be placed
1782 * back to back without something in between.
1783 * We can skip that something in between.
1785 continue;
1787 skip_more:
1788 do {
1789 ptr++;
1790 } while (isalnum(*ptr) || *ptr == '_');
1791 if (!*ptr)
1792 break;
1794 * If what comes after this variable is a '.' or
1795 * '->' then we can continue to ignore that string.
1797 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1798 ptr += *ptr == '.' ? 1 : 2;
1799 if (!*ptr)
1800 break;
1801 goto skip_more;
1804 * Once again, we can skip the delimiter that came
1805 * after the string.
1807 continue;
1812 void trace_event_enum_update(struct trace_enum_map **map, int len)
1814 struct trace_event_call *call, *p;
1815 const char *last_system = NULL;
1816 int last_i;
1817 int i;
1819 down_write(&trace_event_sem);
1820 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1821 /* events are usually grouped together with systems */
1822 if (!last_system || call->class->system != last_system) {
1823 last_i = 0;
1824 last_system = call->class->system;
1827 for (i = last_i; i < len; i++) {
1828 if (call->class->system == map[i]->system) {
1829 /* Save the first system if need be */
1830 if (!last_i)
1831 last_i = i;
1832 update_event_printk(call, map[i]);
1836 up_write(&trace_event_sem);
1839 static struct trace_event_file *
1840 trace_create_new_event(struct trace_event_call *call,
1841 struct trace_array *tr)
1843 struct trace_event_file *file;
1845 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1846 if (!file)
1847 return NULL;
1849 file->event_call = call;
1850 file->tr = tr;
1851 atomic_set(&file->sm_ref, 0);
1852 atomic_set(&file->tm_ref, 0);
1853 INIT_LIST_HEAD(&file->triggers);
1854 list_add(&file->list, &tr->events);
1856 return file;
1859 /* Add an event to a trace directory */
1860 static int
1861 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
1863 struct trace_event_file *file;
1865 file = trace_create_new_event(call, tr);
1866 if (!file)
1867 return -ENOMEM;
1869 return event_create_dir(tr->event_dir, file);
1873 * Just create a decriptor for early init. A descriptor is required
1874 * for enabling events at boot. We want to enable events before
1875 * the filesystem is initialized.
1877 static __init int
1878 __trace_early_add_new_event(struct trace_event_call *call,
1879 struct trace_array *tr)
1881 struct trace_event_file *file;
1883 file = trace_create_new_event(call, tr);
1884 if (!file)
1885 return -ENOMEM;
1887 return 0;
1890 struct ftrace_module_file_ops;
1891 static void __add_event_to_tracers(struct trace_event_call *call);
1893 /* Add an additional event_call dynamically */
1894 int trace_add_event_call(struct trace_event_call *call)
1896 int ret;
1897 mutex_lock(&trace_types_lock);
1898 mutex_lock(&event_mutex);
1900 ret = __register_event(call, NULL);
1901 if (ret >= 0)
1902 __add_event_to_tracers(call);
1904 mutex_unlock(&event_mutex);
1905 mutex_unlock(&trace_types_lock);
1906 return ret;
1910 * Must be called under locking of trace_types_lock, event_mutex and
1911 * trace_event_sem.
1913 static void __trace_remove_event_call(struct trace_event_call *call)
1915 event_remove(call);
1916 trace_destroy_fields(call);
1917 free_event_filter(call->filter);
1918 call->filter = NULL;
1921 static int probe_remove_event_call(struct trace_event_call *call)
1923 struct trace_array *tr;
1924 struct trace_event_file *file;
1926 #ifdef CONFIG_PERF_EVENTS
1927 if (call->perf_refcount)
1928 return -EBUSY;
1929 #endif
1930 do_for_each_event_file(tr, file) {
1931 if (file->event_call != call)
1932 continue;
1934 * We can't rely on ftrace_event_enable_disable(enable => 0)
1935 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
1936 * TRACE_REG_UNREGISTER.
1938 if (file->flags & EVENT_FILE_FL_ENABLED)
1939 return -EBUSY;
1941 * The do_for_each_event_file_safe() is
1942 * a double loop. After finding the call for this
1943 * trace_array, we use break to jump to the next
1944 * trace_array.
1946 break;
1947 } while_for_each_event_file();
1949 __trace_remove_event_call(call);
1951 return 0;
1954 /* Remove an event_call */
1955 int trace_remove_event_call(struct trace_event_call *call)
1957 int ret;
1959 mutex_lock(&trace_types_lock);
1960 mutex_lock(&event_mutex);
1961 down_write(&trace_event_sem);
1962 ret = probe_remove_event_call(call);
1963 up_write(&trace_event_sem);
1964 mutex_unlock(&event_mutex);
1965 mutex_unlock(&trace_types_lock);
1967 return ret;
1970 #define for_each_event(event, start, end) \
1971 for (event = start; \
1972 (unsigned long)event < (unsigned long)end; \
1973 event++)
1975 #ifdef CONFIG_MODULES
1977 static void trace_module_add_events(struct module *mod)
1979 struct trace_event_call **call, **start, **end;
1981 if (!mod->num_trace_events)
1982 return;
1984 /* Don't add infrastructure for mods without tracepoints */
1985 if (trace_module_has_bad_taint(mod)) {
1986 pr_err("%s: module has bad taint, not creating trace events\n",
1987 mod->name);
1988 return;
1991 start = mod->trace_events;
1992 end = mod->trace_events + mod->num_trace_events;
1994 for_each_event(call, start, end) {
1995 __register_event(*call, mod);
1996 __add_event_to_tracers(*call);
2000 static void trace_module_remove_events(struct module *mod)
2002 struct trace_event_call *call, *p;
2003 bool clear_trace = false;
2005 down_write(&trace_event_sem);
2006 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2007 if (call->mod == mod) {
2008 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2009 clear_trace = true;
2010 __trace_remove_event_call(call);
2013 up_write(&trace_event_sem);
2016 * It is safest to reset the ring buffer if the module being unloaded
2017 * registered any events that were used. The only worry is if
2018 * a new module gets loaded, and takes on the same id as the events
2019 * of this module. When printing out the buffer, traced events left
2020 * over from this module may be passed to the new module events and
2021 * unexpected results may occur.
2023 if (clear_trace)
2024 tracing_reset_all_online_cpus();
2027 static int trace_module_notify(struct notifier_block *self,
2028 unsigned long val, void *data)
2030 struct module *mod = data;
2032 mutex_lock(&trace_types_lock);
2033 mutex_lock(&event_mutex);
2034 switch (val) {
2035 case MODULE_STATE_COMING:
2036 trace_module_add_events(mod);
2037 break;
2038 case MODULE_STATE_GOING:
2039 trace_module_remove_events(mod);
2040 break;
2042 mutex_unlock(&event_mutex);
2043 mutex_unlock(&trace_types_lock);
2045 return 0;
2048 static struct notifier_block trace_module_nb = {
2049 .notifier_call = trace_module_notify,
2050 .priority = 1, /* higher than trace.c module notify */
2052 #endif /* CONFIG_MODULES */
2054 /* Create a new event directory structure for a trace directory. */
2055 static void
2056 __trace_add_event_dirs(struct trace_array *tr)
2058 struct trace_event_call *call;
2059 int ret;
2061 list_for_each_entry(call, &ftrace_events, list) {
2062 ret = __trace_add_new_event(call, tr);
2063 if (ret < 0)
2064 pr_warn("Could not create directory for event %s\n",
2065 trace_event_name(call));
2069 struct trace_event_file *
2070 find_event_file(struct trace_array *tr, const char *system, const char *event)
2072 struct trace_event_file *file;
2073 struct trace_event_call *call;
2074 const char *name;
2076 list_for_each_entry(file, &tr->events, list) {
2078 call = file->event_call;
2079 name = trace_event_name(call);
2081 if (!name || !call->class || !call->class->reg)
2082 continue;
2084 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2085 continue;
2087 if (strcmp(event, name) == 0 &&
2088 strcmp(system, call->class->system) == 0)
2089 return file;
2091 return NULL;
2094 #ifdef CONFIG_DYNAMIC_FTRACE
2096 /* Avoid typos */
2097 #define ENABLE_EVENT_STR "enable_event"
2098 #define DISABLE_EVENT_STR "disable_event"
2100 struct event_probe_data {
2101 struct trace_event_file *file;
2102 unsigned long count;
2103 int ref;
2104 bool enable;
2107 static void
2108 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2110 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2111 struct event_probe_data *data = *pdata;
2113 if (!data)
2114 return;
2116 if (data->enable)
2117 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2118 else
2119 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2122 static void
2123 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2125 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2126 struct event_probe_data *data = *pdata;
2128 if (!data)
2129 return;
2131 if (!data->count)
2132 return;
2134 /* Skip if the event is in a state we want to switch to */
2135 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2136 return;
2138 if (data->count != -1)
2139 (data->count)--;
2141 event_enable_probe(ip, parent_ip, _data);
2144 static int
2145 event_enable_print(struct seq_file *m, unsigned long ip,
2146 struct ftrace_probe_ops *ops, void *_data)
2148 struct event_probe_data *data = _data;
2150 seq_printf(m, "%ps:", (void *)ip);
2152 seq_printf(m, "%s:%s:%s",
2153 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2154 data->file->event_call->class->system,
2155 trace_event_name(data->file->event_call));
2157 if (data->count == -1)
2158 seq_puts(m, ":unlimited\n");
2159 else
2160 seq_printf(m, ":count=%ld\n", data->count);
2162 return 0;
2165 static int
2166 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2167 void **_data)
2169 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2170 struct event_probe_data *data = *pdata;
2172 data->ref++;
2173 return 0;
2176 static void
2177 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2178 void **_data)
2180 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2181 struct event_probe_data *data = *pdata;
2183 if (WARN_ON_ONCE(data->ref <= 0))
2184 return;
2186 data->ref--;
2187 if (!data->ref) {
2188 /* Remove the SOFT_MODE flag */
2189 __ftrace_event_enable_disable(data->file, 0, 1);
2190 module_put(data->file->event_call->mod);
2191 kfree(data);
2193 *pdata = NULL;
2196 static struct ftrace_probe_ops event_enable_probe_ops = {
2197 .func = event_enable_probe,
2198 .print = event_enable_print,
2199 .init = event_enable_init,
2200 .free = event_enable_free,
2203 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2204 .func = event_enable_count_probe,
2205 .print = event_enable_print,
2206 .init = event_enable_init,
2207 .free = event_enable_free,
2210 static struct ftrace_probe_ops event_disable_probe_ops = {
2211 .func = event_enable_probe,
2212 .print = event_enable_print,
2213 .init = event_enable_init,
2214 .free = event_enable_free,
2217 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2218 .func = event_enable_count_probe,
2219 .print = event_enable_print,
2220 .init = event_enable_init,
2221 .free = event_enable_free,
2224 static int
2225 event_enable_func(struct ftrace_hash *hash,
2226 char *glob, char *cmd, char *param, int enabled)
2228 struct trace_array *tr = top_trace_array();
2229 struct trace_event_file *file;
2230 struct ftrace_probe_ops *ops;
2231 struct event_probe_data *data;
2232 const char *system;
2233 const char *event;
2234 char *number;
2235 bool enable;
2236 int ret;
2238 if (!tr)
2239 return -ENODEV;
2241 /* hash funcs only work with set_ftrace_filter */
2242 if (!enabled || !param)
2243 return -EINVAL;
2245 system = strsep(&param, ":");
2246 if (!param)
2247 return -EINVAL;
2249 event = strsep(&param, ":");
2251 mutex_lock(&event_mutex);
2253 ret = -EINVAL;
2254 file = find_event_file(tr, system, event);
2255 if (!file)
2256 goto out;
2258 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2260 if (enable)
2261 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2262 else
2263 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2265 if (glob[0] == '!') {
2266 unregister_ftrace_function_probe_func(glob+1, ops);
2267 ret = 0;
2268 goto out;
2271 ret = -ENOMEM;
2272 data = kzalloc(sizeof(*data), GFP_KERNEL);
2273 if (!data)
2274 goto out;
2276 data->enable = enable;
2277 data->count = -1;
2278 data->file = file;
2280 if (!param)
2281 goto out_reg;
2283 number = strsep(&param, ":");
2285 ret = -EINVAL;
2286 if (!strlen(number))
2287 goto out_free;
2290 * We use the callback data field (which is a pointer)
2291 * as our counter.
2293 ret = kstrtoul(number, 0, &data->count);
2294 if (ret)
2295 goto out_free;
2297 out_reg:
2298 /* Don't let event modules unload while probe registered */
2299 ret = try_module_get(file->event_call->mod);
2300 if (!ret) {
2301 ret = -EBUSY;
2302 goto out_free;
2305 ret = __ftrace_event_enable_disable(file, 1, 1);
2306 if (ret < 0)
2307 goto out_put;
2308 ret = register_ftrace_function_probe(glob, ops, data);
2310 * The above returns on success the # of functions enabled,
2311 * but if it didn't find any functions it returns zero.
2312 * Consider no functions a failure too.
2314 if (!ret) {
2315 ret = -ENOENT;
2316 goto out_disable;
2317 } else if (ret < 0)
2318 goto out_disable;
2319 /* Just return zero, not the number of enabled functions */
2320 ret = 0;
2321 out:
2322 mutex_unlock(&event_mutex);
2323 return ret;
2325 out_disable:
2326 __ftrace_event_enable_disable(file, 0, 1);
2327 out_put:
2328 module_put(file->event_call->mod);
2329 out_free:
2330 kfree(data);
2331 goto out;
2334 static struct ftrace_func_command event_enable_cmd = {
2335 .name = ENABLE_EVENT_STR,
2336 .func = event_enable_func,
2339 static struct ftrace_func_command event_disable_cmd = {
2340 .name = DISABLE_EVENT_STR,
2341 .func = event_enable_func,
2344 static __init int register_event_cmds(void)
2346 int ret;
2348 ret = register_ftrace_command(&event_enable_cmd);
2349 if (WARN_ON(ret < 0))
2350 return ret;
2351 ret = register_ftrace_command(&event_disable_cmd);
2352 if (WARN_ON(ret < 0))
2353 unregister_ftrace_command(&event_enable_cmd);
2354 return ret;
2356 #else
2357 static inline int register_event_cmds(void) { return 0; }
2358 #endif /* CONFIG_DYNAMIC_FTRACE */
2361 * The top level array has already had its trace_event_file
2362 * descriptors created in order to allow for early events to
2363 * be recorded. This function is called after the tracefs has been
2364 * initialized, and we now have to create the files associated
2365 * to the events.
2367 static __init void
2368 __trace_early_add_event_dirs(struct trace_array *tr)
2370 struct trace_event_file *file;
2371 int ret;
2374 list_for_each_entry(file, &tr->events, list) {
2375 ret = event_create_dir(tr->event_dir, file);
2376 if (ret < 0)
2377 pr_warn("Could not create directory for event %s\n",
2378 trace_event_name(file->event_call));
2383 * For early boot up, the top trace array requires to have
2384 * a list of events that can be enabled. This must be done before
2385 * the filesystem is set up in order to allow events to be traced
2386 * early.
2388 static __init void
2389 __trace_early_add_events(struct trace_array *tr)
2391 struct trace_event_call *call;
2392 int ret;
2394 list_for_each_entry(call, &ftrace_events, list) {
2395 /* Early boot up should not have any modules loaded */
2396 if (WARN_ON_ONCE(call->mod))
2397 continue;
2399 ret = __trace_early_add_new_event(call, tr);
2400 if (ret < 0)
2401 pr_warn("Could not create early event %s\n",
2402 trace_event_name(call));
2406 /* Remove the event directory structure for a trace directory. */
2407 static void
2408 __trace_remove_event_dirs(struct trace_array *tr)
2410 struct trace_event_file *file, *next;
2412 list_for_each_entry_safe(file, next, &tr->events, list)
2413 remove_event_file_dir(file);
2416 static void __add_event_to_tracers(struct trace_event_call *call)
2418 struct trace_array *tr;
2420 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2421 __trace_add_new_event(call, tr);
2424 extern struct trace_event_call *__start_ftrace_events[];
2425 extern struct trace_event_call *__stop_ftrace_events[];
2427 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2429 static __init int setup_trace_event(char *str)
2431 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2432 ring_buffer_expanded = true;
2433 tracing_selftest_disabled = true;
2435 return 1;
2437 __setup("trace_event=", setup_trace_event);
2439 /* Expects to have event_mutex held when called */
2440 static int
2441 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2443 struct dentry *d_events;
2444 struct dentry *entry;
2446 entry = tracefs_create_file("set_event", 0644, parent,
2447 tr, &ftrace_set_event_fops);
2448 if (!entry) {
2449 pr_warn("Could not create tracefs 'set_event' entry\n");
2450 return -ENOMEM;
2453 d_events = tracefs_create_dir("events", parent);
2454 if (!d_events) {
2455 pr_warn("Could not create tracefs 'events' directory\n");
2456 return -ENOMEM;
2459 /* ring buffer internal formats */
2460 trace_create_file("header_page", 0444, d_events,
2461 ring_buffer_print_page_header,
2462 &ftrace_show_header_fops);
2464 trace_create_file("header_event", 0444, d_events,
2465 ring_buffer_print_entry_header,
2466 &ftrace_show_header_fops);
2468 trace_create_file("enable", 0644, d_events,
2469 tr, &ftrace_tr_enable_fops);
2471 tr->event_dir = d_events;
2473 return 0;
2477 * event_trace_add_tracer - add a instance of a trace_array to events
2478 * @parent: The parent dentry to place the files/directories for events in
2479 * @tr: The trace array associated with these events
2481 * When a new instance is created, it needs to set up its events
2482 * directory, as well as other files associated with events. It also
2483 * creates the event hierachry in the @parent/events directory.
2485 * Returns 0 on success.
2487 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2489 int ret;
2491 mutex_lock(&event_mutex);
2493 ret = create_event_toplevel_files(parent, tr);
2494 if (ret)
2495 goto out_unlock;
2497 down_write(&trace_event_sem);
2498 __trace_add_event_dirs(tr);
2499 up_write(&trace_event_sem);
2501 out_unlock:
2502 mutex_unlock(&event_mutex);
2504 return ret;
2508 * The top trace array already had its file descriptors created.
2509 * Now the files themselves need to be created.
2511 static __init int
2512 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2514 int ret;
2516 mutex_lock(&event_mutex);
2518 ret = create_event_toplevel_files(parent, tr);
2519 if (ret)
2520 goto out_unlock;
2522 down_write(&trace_event_sem);
2523 __trace_early_add_event_dirs(tr);
2524 up_write(&trace_event_sem);
2526 out_unlock:
2527 mutex_unlock(&event_mutex);
2529 return ret;
2532 int event_trace_del_tracer(struct trace_array *tr)
2534 mutex_lock(&event_mutex);
2536 /* Disable any event triggers and associated soft-disabled events */
2537 clear_event_triggers(tr);
2539 /* Disable any running events */
2540 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2542 /* Access to events are within rcu_read_lock_sched() */
2543 synchronize_sched();
2545 down_write(&trace_event_sem);
2546 __trace_remove_event_dirs(tr);
2547 tracefs_remove_recursive(tr->event_dir);
2548 up_write(&trace_event_sem);
2550 tr->event_dir = NULL;
2552 mutex_unlock(&event_mutex);
2554 return 0;
2557 static __init int event_trace_memsetup(void)
2559 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2560 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
2561 return 0;
2564 static __init void
2565 early_enable_events(struct trace_array *tr, bool disable_first)
2567 char *buf = bootup_event_buf;
2568 char *token;
2569 int ret;
2571 while (true) {
2572 token = strsep(&buf, ",");
2574 if (!token)
2575 break;
2576 if (!*token)
2577 continue;
2579 /* Restarting syscalls requires that we stop them first */
2580 if (disable_first)
2581 ftrace_set_clr_event(tr, token, 0);
2583 ret = ftrace_set_clr_event(tr, token, 1);
2584 if (ret)
2585 pr_warn("Failed to enable trace event: %s\n", token);
2587 /* Put back the comma to allow this to be called again */
2588 if (buf)
2589 *(buf - 1) = ',';
2593 static __init int event_trace_enable(void)
2595 struct trace_array *tr = top_trace_array();
2596 struct trace_event_call **iter, *call;
2597 int ret;
2599 if (!tr)
2600 return -ENODEV;
2602 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2604 call = *iter;
2605 ret = event_init(call);
2606 if (!ret)
2607 list_add(&call->list, &ftrace_events);
2611 * We need the top trace array to have a working set of trace
2612 * points at early init, before the debug files and directories
2613 * are created. Create the file entries now, and attach them
2614 * to the actual file dentries later.
2616 __trace_early_add_events(tr);
2618 early_enable_events(tr, false);
2620 trace_printk_start_comm();
2622 register_event_cmds();
2624 register_trigger_cmds();
2626 return 0;
2630 * event_trace_enable() is called from trace_event_init() first to
2631 * initialize events and perhaps start any events that are on the
2632 * command line. Unfortunately, there are some events that will not
2633 * start this early, like the system call tracepoints that need
2634 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2635 * is called before pid 1 starts, and this flag is never set, making
2636 * the syscall tracepoint never get reached, but the event is enabled
2637 * regardless (and not doing anything).
2639 static __init int event_trace_enable_again(void)
2641 struct trace_array *tr;
2643 tr = top_trace_array();
2644 if (!tr)
2645 return -ENODEV;
2647 early_enable_events(tr, true);
2649 return 0;
2652 early_initcall(event_trace_enable_again);
2654 static __init int event_trace_init(void)
2656 struct trace_array *tr;
2657 struct dentry *d_tracer;
2658 struct dentry *entry;
2659 int ret;
2661 tr = top_trace_array();
2662 if (!tr)
2663 return -ENODEV;
2665 d_tracer = tracing_init_dentry();
2666 if (IS_ERR(d_tracer))
2667 return 0;
2669 entry = tracefs_create_file("available_events", 0444, d_tracer,
2670 tr, &ftrace_avail_fops);
2671 if (!entry)
2672 pr_warn("Could not create tracefs 'available_events' entry\n");
2674 if (trace_define_common_fields())
2675 pr_warn("tracing: Failed to allocate common fields");
2677 ret = early_event_add_tracer(d_tracer, tr);
2678 if (ret)
2679 return ret;
2681 #ifdef CONFIG_MODULES
2682 ret = register_module_notifier(&trace_module_nb);
2683 if (ret)
2684 pr_warn("Failed to register trace events module notifier\n");
2685 #endif
2686 return 0;
2689 void __init trace_event_init(void)
2691 event_trace_memsetup();
2692 init_ftrace_syscalls();
2693 event_trace_enable();
2696 fs_initcall(event_trace_init);
2698 #ifdef CONFIG_FTRACE_STARTUP_TEST
2700 static DEFINE_SPINLOCK(test_spinlock);
2701 static DEFINE_SPINLOCK(test_spinlock_irq);
2702 static DEFINE_MUTEX(test_mutex);
2704 static __init void test_work(struct work_struct *dummy)
2706 spin_lock(&test_spinlock);
2707 spin_lock_irq(&test_spinlock_irq);
2708 udelay(1);
2709 spin_unlock_irq(&test_spinlock_irq);
2710 spin_unlock(&test_spinlock);
2712 mutex_lock(&test_mutex);
2713 msleep(1);
2714 mutex_unlock(&test_mutex);
2717 static __init int event_test_thread(void *unused)
2719 void *test_malloc;
2721 test_malloc = kmalloc(1234, GFP_KERNEL);
2722 if (!test_malloc)
2723 pr_info("failed to kmalloc\n");
2725 schedule_on_each_cpu(test_work);
2727 kfree(test_malloc);
2729 set_current_state(TASK_INTERRUPTIBLE);
2730 while (!kthread_should_stop()) {
2731 schedule();
2732 set_current_state(TASK_INTERRUPTIBLE);
2734 __set_current_state(TASK_RUNNING);
2736 return 0;
2740 * Do various things that may trigger events.
2742 static __init void event_test_stuff(void)
2744 struct task_struct *test_thread;
2746 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2747 msleep(1);
2748 kthread_stop(test_thread);
2752 * For every trace event defined, we will test each trace point separately,
2753 * and then by groups, and finally all trace points.
2755 static __init void event_trace_self_tests(void)
2757 struct trace_subsystem_dir *dir;
2758 struct trace_event_file *file;
2759 struct trace_event_call *call;
2760 struct event_subsystem *system;
2761 struct trace_array *tr;
2762 int ret;
2764 tr = top_trace_array();
2765 if (!tr)
2766 return;
2768 pr_info("Running tests on trace events:\n");
2770 list_for_each_entry(file, &tr->events, list) {
2772 call = file->event_call;
2774 /* Only test those that have a probe */
2775 if (!call->class || !call->class->probe)
2776 continue;
2779 * Testing syscall events here is pretty useless, but
2780 * we still do it if configured. But this is time consuming.
2781 * What we really need is a user thread to perform the
2782 * syscalls as we test.
2784 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2785 if (call->class->system &&
2786 strcmp(call->class->system, "syscalls") == 0)
2787 continue;
2788 #endif
2790 pr_info("Testing event %s: ", trace_event_name(call));
2793 * If an event is already enabled, someone is using
2794 * it and the self test should not be on.
2796 if (file->flags & EVENT_FILE_FL_ENABLED) {
2797 pr_warn("Enabled event during self test!\n");
2798 WARN_ON_ONCE(1);
2799 continue;
2802 ftrace_event_enable_disable(file, 1);
2803 event_test_stuff();
2804 ftrace_event_enable_disable(file, 0);
2806 pr_cont("OK\n");
2809 /* Now test at the sub system level */
2811 pr_info("Running tests on trace event systems:\n");
2813 list_for_each_entry(dir, &tr->systems, list) {
2815 system = dir->subsystem;
2817 /* the ftrace system is special, skip it */
2818 if (strcmp(system->name, "ftrace") == 0)
2819 continue;
2821 pr_info("Testing event system %s: ", system->name);
2823 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2824 if (WARN_ON_ONCE(ret)) {
2825 pr_warn("error enabling system %s\n",
2826 system->name);
2827 continue;
2830 event_test_stuff();
2832 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2833 if (WARN_ON_ONCE(ret)) {
2834 pr_warn("error disabling system %s\n",
2835 system->name);
2836 continue;
2839 pr_cont("OK\n");
2842 /* Test with all events enabled */
2844 pr_info("Running tests on all trace events:\n");
2845 pr_info("Testing all events: ");
2847 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2848 if (WARN_ON_ONCE(ret)) {
2849 pr_warn("error enabling all events\n");
2850 return;
2853 event_test_stuff();
2855 /* reset sysname */
2856 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2857 if (WARN_ON_ONCE(ret)) {
2858 pr_warn("error disabling all events\n");
2859 return;
2862 pr_cont("OK\n");
2865 #ifdef CONFIG_FUNCTION_TRACER
2867 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2869 static void
2870 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2871 struct ftrace_ops *op, struct pt_regs *pt_regs)
2873 struct ring_buffer_event *event;
2874 struct ring_buffer *buffer;
2875 struct ftrace_entry *entry;
2876 unsigned long flags;
2877 long disabled;
2878 int cpu;
2879 int pc;
2881 pc = preempt_count();
2882 preempt_disable_notrace();
2883 cpu = raw_smp_processor_id();
2884 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2886 if (disabled != 1)
2887 goto out;
2889 local_save_flags(flags);
2891 event = trace_current_buffer_lock_reserve(&buffer,
2892 TRACE_FN, sizeof(*entry),
2893 flags, pc);
2894 if (!event)
2895 goto out;
2896 entry = ring_buffer_event_data(event);
2897 entry->ip = ip;
2898 entry->parent_ip = parent_ip;
2900 trace_buffer_unlock_commit(buffer, event, flags, pc);
2902 out:
2903 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2904 preempt_enable_notrace();
2907 static struct ftrace_ops trace_ops __initdata =
2909 .func = function_test_events_call,
2910 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2913 static __init void event_trace_self_test_with_function(void)
2915 int ret;
2916 ret = register_ftrace_function(&trace_ops);
2917 if (WARN_ON(ret < 0)) {
2918 pr_info("Failed to enable function tracer for event tests\n");
2919 return;
2921 pr_info("Running tests again, along with the function tracer\n");
2922 event_trace_self_tests();
2923 unregister_ftrace_function(&trace_ops);
2925 #else
2926 static __init void event_trace_self_test_with_function(void)
2929 #endif
2931 static __init int event_trace_self_tests_init(void)
2933 if (!tracing_selftest_disabled) {
2934 event_trace_self_tests();
2935 event_trace_self_test_with_function();
2938 return 0;
2941 late_initcall(event_trace_self_tests_init);
2943 #endif