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>.
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
31 static LIST_HEAD(ftrace_common_fields);
33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
35 static struct kmem_cache *field_cachep;
36 static struct kmem_cache *file_cachep;
38 #define SYSTEM_FL_FREE_NAME (1 << 31)
40 static inline int system_refcount(struct event_subsystem *system)
42 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
45 static int system_refcount_inc(struct event_subsystem *system)
47 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
50 static int system_refcount_dec(struct event_subsystem *system)
52 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
55 /* Double loops, do not use break, only goto's work */
56 #define do_for_each_event_file(tr, file) \
57 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 list_for_each_entry(file, &tr->events, list)
60 #define do_for_each_event_file_safe(tr, file) \
61 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 struct ftrace_event_file *___n; \
63 list_for_each_entry_safe(file, ___n, &tr->events, list)
65 #define while_for_each_event_file() \
68 static struct list_head *
69 trace_get_fields(struct ftrace_event_call *event_call)
71 if (!event_call->class->get_fields)
72 return &event_call->class->fields;
73 return event_call->class->get_fields(event_call);
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
79 struct ftrace_event_field *field;
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
89 struct ftrace_event_field *
90 trace_find_event_field(struct ftrace_event_call *call, char *name)
92 struct ftrace_event_field *field;
93 struct list_head *head;
95 field = __find_event_field(&ftrace_common_fields, name);
99 head = trace_get_fields(call);
100 return __find_event_field(head, name);
103 static int __trace_define_field(struct list_head *head, const char *type,
104 const char *name, int offset, int size,
105 int is_signed, int filter_type)
107 struct ftrace_event_field *field;
109 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
116 if (filter_type == FILTER_OTHER)
117 field->filter_type = filter_assign_type(type);
119 field->filter_type = filter_type;
121 field->offset = offset;
123 field->is_signed = is_signed;
125 list_add(&field->link, head);
130 int trace_define_field(struct ftrace_event_call *call, const char *type,
131 const char *name, int offset, int size, int is_signed,
134 struct list_head *head;
136 if (WARN_ON(!call->class))
139 head = trace_get_fields(call);
140 return __trace_define_field(head, type, name, offset, size,
141 is_signed, filter_type);
143 EXPORT_SYMBOL_GPL(trace_define_field);
145 #define __common_field(type, item) \
146 ret = __trace_define_field(&ftrace_common_fields, #type, \
148 offsetof(typeof(ent), item), \
150 is_signed_type(type), FILTER_OTHER); \
154 static int trace_define_common_fields(void)
157 struct trace_entry ent;
159 __common_field(unsigned short, type);
160 __common_field(unsigned char, flags);
161 __common_field(unsigned char, preempt_count);
162 __common_field(int, pid);
167 static void trace_destroy_fields(struct ftrace_event_call *call)
169 struct ftrace_event_field *field, *next;
170 struct list_head *head;
172 head = trace_get_fields(call);
173 list_for_each_entry_safe(field, next, head, link) {
174 list_del(&field->link);
175 kmem_cache_free(field_cachep, field);
179 int trace_event_raw_init(struct ftrace_event_call *call)
183 id = register_ftrace_event(&call->event);
189 EXPORT_SYMBOL_GPL(trace_event_raw_init);
191 int ftrace_event_reg(struct ftrace_event_call *call,
192 enum trace_reg type, void *data)
194 struct ftrace_event_file *file = data;
197 case TRACE_REG_REGISTER:
198 return tracepoint_probe_register(call->name,
201 case TRACE_REG_UNREGISTER:
202 tracepoint_probe_unregister(call->name,
207 #ifdef CONFIG_PERF_EVENTS
208 case TRACE_REG_PERF_REGISTER:
209 return tracepoint_probe_register(call->name,
210 call->class->perf_probe,
212 case TRACE_REG_PERF_UNREGISTER:
213 tracepoint_probe_unregister(call->name,
214 call->class->perf_probe,
217 case TRACE_REG_PERF_OPEN:
218 case TRACE_REG_PERF_CLOSE:
219 case TRACE_REG_PERF_ADD:
220 case TRACE_REG_PERF_DEL:
226 EXPORT_SYMBOL_GPL(ftrace_event_reg);
228 void trace_event_enable_cmd_record(bool enable)
230 struct ftrace_event_file *file;
231 struct trace_array *tr;
233 mutex_lock(&event_mutex);
234 do_for_each_event_file(tr, file) {
236 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
240 tracing_start_cmdline_record();
241 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
243 tracing_stop_cmdline_record();
244 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
246 } while_for_each_event_file();
247 mutex_unlock(&event_mutex);
250 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
251 int enable, int soft_disable)
253 struct ftrace_event_call *call = file->event_call;
260 * When soft_disable is set and enable is cleared, the sm_ref
261 * reference counter is decremented. If it reaches 0, we want
262 * to clear the SOFT_DISABLED flag but leave the event in the
263 * state that it was. That is, if the event was enabled and
264 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265 * is set we do not want the event to be enabled before we
268 * When soft_disable is not set but the SOFT_MODE flag is,
269 * we do nothing. Do not disable the tracepoint, otherwise
270 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
273 if (atomic_dec_return(&file->sm_ref) > 0)
275 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
276 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
278 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
280 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
281 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
282 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
283 tracing_stop_cmdline_record();
284 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
286 call->class->reg(call, TRACE_REG_UNREGISTER, file);
288 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
289 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
290 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
292 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
296 * When soft_disable is set and enable is set, we want to
297 * register the tracepoint for the event, but leave the event
298 * as is. That means, if the event was already enabled, we do
299 * nothing (but set SOFT_MODE). If the event is disabled, we
300 * set SOFT_DISABLED before enabling the event tracepoint, so
301 * it still seems to be disabled.
304 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
306 if (atomic_inc_return(&file->sm_ref) > 1)
308 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
311 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
313 /* Keep the event disabled, when going to SOFT_MODE. */
315 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
317 if (trace_flags & TRACE_ITER_RECORD_CMD) {
318 tracing_start_cmdline_record();
319 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
321 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
323 tracing_stop_cmdline_record();
324 pr_info("event trace: Could not enable event "
328 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
330 /* WAS_ENABLED gets set but never cleared. */
331 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
339 int trace_event_enable_disable(struct ftrace_event_file *file,
340 int enable, int soft_disable)
342 return __ftrace_event_enable_disable(file, enable, soft_disable);
345 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
348 return __ftrace_event_enable_disable(file, enable, 0);
351 static void ftrace_clear_events(struct trace_array *tr)
353 struct ftrace_event_file *file;
355 mutex_lock(&event_mutex);
356 list_for_each_entry(file, &tr->events, list) {
357 ftrace_event_enable_disable(file, 0);
359 mutex_unlock(&event_mutex);
362 static void __put_system(struct event_subsystem *system)
364 struct event_filter *filter = system->filter;
366 WARN_ON_ONCE(system_refcount(system) == 0);
367 if (system_refcount_dec(system))
370 list_del(&system->list);
373 kfree(filter->filter_string);
376 if (system->ref_count & SYSTEM_FL_FREE_NAME)
381 static void __get_system(struct event_subsystem *system)
383 WARN_ON_ONCE(system_refcount(system) == 0);
384 system_refcount_inc(system);
387 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
389 WARN_ON_ONCE(dir->ref_count == 0);
391 __get_system(dir->subsystem);
394 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
396 WARN_ON_ONCE(dir->ref_count == 0);
397 /* If the subsystem is about to be freed, the dir must be too */
398 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
400 __put_system(dir->subsystem);
401 if (!--dir->ref_count)
405 static void put_system(struct ftrace_subsystem_dir *dir)
407 mutex_lock(&event_mutex);
408 __put_system_dir(dir);
409 mutex_unlock(&event_mutex);
412 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
417 if (!--dir->nr_events) {
418 debugfs_remove_recursive(dir->entry);
419 list_del(&dir->list);
420 __put_system_dir(dir);
424 static void remove_event_file_dir(struct ftrace_event_file *file)
426 struct dentry *dir = file->dir;
427 struct dentry *child;
430 spin_lock(&dir->d_lock); /* probably unneeded */
431 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
432 if (child->d_inode) /* probably unneeded */
433 child->d_inode->i_private = NULL;
435 spin_unlock(&dir->d_lock);
437 debugfs_remove_recursive(dir);
440 list_del(&file->list);
441 remove_subsystem(file->system);
442 kmem_cache_free(file_cachep, file);
446 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
449 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
450 const char *sub, const char *event, int set)
452 struct ftrace_event_file *file;
453 struct ftrace_event_call *call;
456 list_for_each_entry(file, &tr->events, list) {
458 call = file->event_call;
460 if (!call->name || !call->class || !call->class->reg)
463 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
467 strcmp(match, call->name) != 0 &&
468 strcmp(match, call->class->system) != 0)
471 if (sub && strcmp(sub, call->class->system) != 0)
474 if (event && strcmp(event, call->name) != 0)
477 ftrace_event_enable_disable(file, set);
485 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
486 const char *sub, const char *event, int set)
490 mutex_lock(&event_mutex);
491 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
492 mutex_unlock(&event_mutex);
497 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
499 char *event = NULL, *sub = NULL, *match;
502 * The buf format can be <subsystem>:<event-name>
503 * *:<event-name> means any event by that name.
504 * :<event-name> is the same.
506 * <subsystem>:* means all events in that subsystem
507 * <subsystem>: means the same.
509 * <name> (no ':') means all events in a subsystem with
510 * the name <name> or any event that matches <name>
513 match = strsep(&buf, ":");
519 if (!strlen(sub) || strcmp(sub, "*") == 0)
521 if (!strlen(event) || strcmp(event, "*") == 0)
525 return __ftrace_set_clr_event(tr, match, sub, event, set);
529 * trace_set_clr_event - enable or disable an event
530 * @system: system name to match (NULL for any system)
531 * @event: event name to match (NULL for all events, within system)
532 * @set: 1 to enable, 0 to disable
534 * This is a way for other parts of the kernel to enable or disable
537 * Returns 0 on success, -EINVAL if the parameters do not match any
540 int trace_set_clr_event(const char *system, const char *event, int set)
542 struct trace_array *tr = top_trace_array();
544 return __ftrace_set_clr_event(tr, NULL, system, event, set);
546 EXPORT_SYMBOL_GPL(trace_set_clr_event);
548 /* 128 should be much more than enough */
549 #define EVENT_BUF_SIZE 127
552 ftrace_event_write(struct file *file, const char __user *ubuf,
553 size_t cnt, loff_t *ppos)
555 struct trace_parser parser;
556 struct seq_file *m = file->private_data;
557 struct trace_array *tr = m->private;
563 ret = tracing_update_buffers();
567 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
570 read = trace_get_user(&parser, ubuf, cnt, ppos);
572 if (read >= 0 && trace_parser_loaded((&parser))) {
575 if (*parser.buffer == '!')
578 parser.buffer[parser.idx] = 0;
580 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
588 trace_parser_put(&parser);
594 t_next(struct seq_file *m, void *v, loff_t *pos)
596 struct ftrace_event_file *file = v;
597 struct ftrace_event_call *call;
598 struct trace_array *tr = m->private;
602 list_for_each_entry_continue(file, &tr->events, list) {
603 call = file->event_call;
605 * The ftrace subsystem is for showing formats only.
606 * They can not be enabled or disabled via the event files.
608 if (call->class && call->class->reg)
615 static void *t_start(struct seq_file *m, loff_t *pos)
617 struct ftrace_event_file *file;
618 struct trace_array *tr = m->private;
621 mutex_lock(&event_mutex);
623 file = list_entry(&tr->events, struct ftrace_event_file, list);
624 for (l = 0; l <= *pos; ) {
625 file = t_next(m, file, &l);
633 s_next(struct seq_file *m, void *v, loff_t *pos)
635 struct ftrace_event_file *file = v;
636 struct trace_array *tr = m->private;
640 list_for_each_entry_continue(file, &tr->events, list) {
641 if (file->flags & FTRACE_EVENT_FL_ENABLED)
648 static void *s_start(struct seq_file *m, loff_t *pos)
650 struct ftrace_event_file *file;
651 struct trace_array *tr = m->private;
654 mutex_lock(&event_mutex);
656 file = list_entry(&tr->events, struct ftrace_event_file, list);
657 for (l = 0; l <= *pos; ) {
658 file = s_next(m, file, &l);
665 static int t_show(struct seq_file *m, void *v)
667 struct ftrace_event_file *file = v;
668 struct ftrace_event_call *call = file->event_call;
670 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
671 seq_printf(m, "%s:", call->class->system);
672 seq_printf(m, "%s\n", call->name);
677 static void t_stop(struct seq_file *m, void *p)
679 mutex_unlock(&event_mutex);
683 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
686 struct ftrace_event_file *file;
690 mutex_lock(&event_mutex);
691 file = event_file_data(filp);
694 mutex_unlock(&event_mutex);
699 if (flags & FTRACE_EVENT_FL_ENABLED &&
700 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
703 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
704 flags & FTRACE_EVENT_FL_SOFT_MODE)
709 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
713 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
716 struct ftrace_event_file *file;
720 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
724 ret = tracing_update_buffers();
732 mutex_lock(&event_mutex);
733 file = event_file_data(filp);
735 ret = ftrace_event_enable_disable(file, val);
736 mutex_unlock(&event_mutex);
745 return ret ? ret : cnt;
749 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
752 const char set_to_char[4] = { '?', '0', '1', 'X' };
753 struct ftrace_subsystem_dir *dir = filp->private_data;
754 struct event_subsystem *system = dir->subsystem;
755 struct ftrace_event_call *call;
756 struct ftrace_event_file *file;
757 struct trace_array *tr = dir->tr;
762 mutex_lock(&event_mutex);
763 list_for_each_entry(file, &tr->events, list) {
764 call = file->event_call;
765 if (!call->name || !call->class || !call->class->reg)
768 if (system && strcmp(call->class->system, system->name) != 0)
772 * We need to find out if all the events are set
773 * or if all events or cleared, or if we have
776 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
779 * If we have a mixture, no need to look further.
784 mutex_unlock(&event_mutex);
786 buf[0] = set_to_char[set];
789 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
795 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
798 struct ftrace_subsystem_dir *dir = filp->private_data;
799 struct event_subsystem *system = dir->subsystem;
800 const char *name = NULL;
804 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
808 ret = tracing_update_buffers();
812 if (val != 0 && val != 1)
816 * Opening of "enable" adds a ref count to system,
817 * so the name is safe to use.
822 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
836 FORMAT_FIELD_SEPERATOR = 2,
840 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
842 struct ftrace_event_call *call = event_file_data(m->private);
843 struct list_head *common_head = &ftrace_common_fields;
844 struct list_head *head = trace_get_fields(call);
845 struct list_head *node = v;
849 switch ((unsigned long)v) {
854 case FORMAT_FIELD_SEPERATOR:
858 case FORMAT_PRINTFMT:
864 if (node == common_head)
865 return (void *)FORMAT_FIELD_SEPERATOR;
866 else if (node == head)
867 return (void *)FORMAT_PRINTFMT;
872 static int f_show(struct seq_file *m, void *v)
874 struct ftrace_event_call *call = event_file_data(m->private);
875 struct ftrace_event_field *field;
876 const char *array_descriptor;
878 switch ((unsigned long)v) {
880 seq_printf(m, "name: %s\n", call->name);
881 seq_printf(m, "ID: %d\n", call->event.type);
882 seq_printf(m, "format:\n");
885 case FORMAT_FIELD_SEPERATOR:
889 case FORMAT_PRINTFMT:
890 seq_printf(m, "\nprint fmt: %s\n",
895 field = list_entry(v, struct ftrace_event_field, link);
897 * Smartly shows the array type(except dynamic array).
900 * If TYPE := TYPE[LEN], it is shown:
901 * field:TYPE VAR[LEN]
903 array_descriptor = strchr(field->type, '[');
905 if (!strncmp(field->type, "__data_loc", 10))
906 array_descriptor = NULL;
908 if (!array_descriptor)
909 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
910 field->type, field->name, field->offset,
911 field->size, !!field->is_signed);
913 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
914 (int)(array_descriptor - field->type),
915 field->type, field->name,
916 array_descriptor, field->offset,
917 field->size, !!field->is_signed);
922 static void *f_start(struct seq_file *m, loff_t *pos)
924 void *p = (void *)FORMAT_HEADER;
927 /* ->stop() is called even if ->start() fails */
928 mutex_lock(&event_mutex);
929 if (!event_file_data(m->private))
930 return ERR_PTR(-ENODEV);
932 while (l < *pos && p)
933 p = f_next(m, p, &l);
938 static void f_stop(struct seq_file *m, void *p)
940 mutex_unlock(&event_mutex);
943 static const struct seq_operations trace_format_seq_ops = {
950 static int trace_format_open(struct inode *inode, struct file *file)
955 ret = seq_open(file, &trace_format_seq_ops);
959 m = file->private_data;
966 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
968 int id = (long)event_file_data(filp);
978 len = sprintf(buf, "%d\n", id);
980 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
984 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
987 struct ftrace_event_file *file;
994 s = kmalloc(sizeof(*s), GFP_KERNEL);
1001 mutex_lock(&event_mutex);
1002 file = event_file_data(filp);
1004 print_event_filter(file, s);
1005 mutex_unlock(&event_mutex);
1008 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1016 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1019 struct ftrace_event_file *file;
1023 if (cnt >= PAGE_SIZE)
1026 buf = (char *)__get_free_page(GFP_TEMPORARY);
1030 if (copy_from_user(buf, ubuf, cnt)) {
1031 free_page((unsigned long) buf);
1036 mutex_lock(&event_mutex);
1037 file = event_file_data(filp);
1039 err = apply_event_filter(file, buf);
1040 mutex_unlock(&event_mutex);
1042 free_page((unsigned long) buf);
1051 static LIST_HEAD(event_subsystems);
1053 static int subsystem_open(struct inode *inode, struct file *filp)
1055 struct event_subsystem *system = NULL;
1056 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1057 struct trace_array *tr;
1060 if (tracing_is_disabled())
1063 /* Make sure the system still exists */
1064 mutex_lock(&trace_types_lock);
1065 mutex_lock(&event_mutex);
1066 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1067 list_for_each_entry(dir, &tr->systems, list) {
1068 if (dir == inode->i_private) {
1069 /* Don't open systems with no events */
1070 if (dir->nr_events) {
1071 __get_system_dir(dir);
1072 system = dir->subsystem;
1079 mutex_unlock(&event_mutex);
1080 mutex_unlock(&trace_types_lock);
1085 /* Some versions of gcc think dir can be uninitialized here */
1088 /* Still need to increment the ref count of the system */
1089 if (trace_array_get(tr) < 0) {
1094 ret = tracing_open_generic(inode, filp);
1096 trace_array_put(tr);
1103 static int system_tr_open(struct inode *inode, struct file *filp)
1105 struct ftrace_subsystem_dir *dir;
1106 struct trace_array *tr = inode->i_private;
1109 if (tracing_is_disabled())
1112 if (trace_array_get(tr) < 0)
1115 /* Make a temporary dir that has no system but points to tr */
1116 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1118 trace_array_put(tr);
1124 ret = tracing_open_generic(inode, filp);
1126 trace_array_put(tr);
1131 filp->private_data = dir;
1136 static int subsystem_release(struct inode *inode, struct file *file)
1138 struct ftrace_subsystem_dir *dir = file->private_data;
1140 trace_array_put(dir->tr);
1143 * If dir->subsystem is NULL, then this is a temporary
1144 * descriptor that was made for a trace_array to enable
1156 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1159 struct ftrace_subsystem_dir *dir = filp->private_data;
1160 struct event_subsystem *system = dir->subsystem;
1161 struct trace_seq *s;
1167 s = kmalloc(sizeof(*s), GFP_KERNEL);
1173 print_subsystem_event_filter(system, s);
1174 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1182 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1185 struct ftrace_subsystem_dir *dir = filp->private_data;
1189 if (cnt >= PAGE_SIZE)
1192 buf = (char *)__get_free_page(GFP_TEMPORARY);
1196 if (copy_from_user(buf, ubuf, cnt)) {
1197 free_page((unsigned long) buf);
1202 err = apply_subsystem_event_filter(dir, buf);
1203 free_page((unsigned long) buf);
1213 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1215 int (*func)(struct trace_seq *s) = filp->private_data;
1216 struct trace_seq *s;
1222 s = kmalloc(sizeof(*s), GFP_KERNEL);
1229 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1236 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1237 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1238 static int ftrace_event_release(struct inode *inode, struct file *file);
1240 static const struct seq_operations show_event_seq_ops = {
1247 static const struct seq_operations show_set_event_seq_ops = {
1254 static const struct file_operations ftrace_avail_fops = {
1255 .open = ftrace_event_avail_open,
1257 .llseek = seq_lseek,
1258 .release = seq_release,
1261 static const struct file_operations ftrace_set_event_fops = {
1262 .open = ftrace_event_set_open,
1264 .write = ftrace_event_write,
1265 .llseek = seq_lseek,
1266 .release = ftrace_event_release,
1269 static const struct file_operations ftrace_enable_fops = {
1270 .open = tracing_open_generic,
1271 .read = event_enable_read,
1272 .write = event_enable_write,
1273 .llseek = default_llseek,
1276 static const struct file_operations ftrace_event_format_fops = {
1277 .open = trace_format_open,
1279 .llseek = seq_lseek,
1280 .release = seq_release,
1283 static const struct file_operations ftrace_event_id_fops = {
1284 .read = event_id_read,
1285 .llseek = default_llseek,
1288 static const struct file_operations ftrace_event_filter_fops = {
1289 .open = tracing_open_generic,
1290 .read = event_filter_read,
1291 .write = event_filter_write,
1292 .llseek = default_llseek,
1295 static const struct file_operations ftrace_subsystem_filter_fops = {
1296 .open = subsystem_open,
1297 .read = subsystem_filter_read,
1298 .write = subsystem_filter_write,
1299 .llseek = default_llseek,
1300 .release = subsystem_release,
1303 static const struct file_operations ftrace_system_enable_fops = {
1304 .open = subsystem_open,
1305 .read = system_enable_read,
1306 .write = system_enable_write,
1307 .llseek = default_llseek,
1308 .release = subsystem_release,
1311 static const struct file_operations ftrace_tr_enable_fops = {
1312 .open = system_tr_open,
1313 .read = system_enable_read,
1314 .write = system_enable_write,
1315 .llseek = default_llseek,
1316 .release = subsystem_release,
1319 static const struct file_operations ftrace_show_header_fops = {
1320 .open = tracing_open_generic,
1321 .read = show_header,
1322 .llseek = default_llseek,
1326 ftrace_event_open(struct inode *inode, struct file *file,
1327 const struct seq_operations *seq_ops)
1332 ret = seq_open(file, seq_ops);
1335 m = file->private_data;
1336 /* copy tr over to seq ops */
1337 m->private = inode->i_private;
1342 static int ftrace_event_release(struct inode *inode, struct file *file)
1344 struct trace_array *tr = inode->i_private;
1346 trace_array_put(tr);
1348 return seq_release(inode, file);
1352 ftrace_event_avail_open(struct inode *inode, struct file *file)
1354 const struct seq_operations *seq_ops = &show_event_seq_ops;
1356 return ftrace_event_open(inode, file, seq_ops);
1360 ftrace_event_set_open(struct inode *inode, struct file *file)
1362 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1363 struct trace_array *tr = inode->i_private;
1366 if (trace_array_get(tr) < 0)
1369 if ((file->f_mode & FMODE_WRITE) &&
1370 (file->f_flags & O_TRUNC))
1371 ftrace_clear_events(tr);
1373 ret = ftrace_event_open(inode, file, seq_ops);
1375 trace_array_put(tr);
1379 static struct event_subsystem *
1380 create_new_subsystem(const char *name)
1382 struct event_subsystem *system;
1384 /* need to create new entry */
1385 system = kmalloc(sizeof(*system), GFP_KERNEL);
1389 system->ref_count = 1;
1391 /* Only allocate if dynamic (kprobes and modules) */
1392 if (!core_kernel_data((unsigned long)name)) {
1393 system->ref_count |= SYSTEM_FL_FREE_NAME;
1394 system->name = kstrdup(name, GFP_KERNEL);
1398 system->name = name;
1400 system->filter = NULL;
1402 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1403 if (!system->filter)
1406 list_add(&system->list, &event_subsystems);
1411 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1412 kfree(system->name);
1417 static struct dentry *
1418 event_subsystem_dir(struct trace_array *tr, const char *name,
1419 struct ftrace_event_file *file, struct dentry *parent)
1421 struct ftrace_subsystem_dir *dir;
1422 struct event_subsystem *system;
1423 struct dentry *entry;
1425 /* First see if we did not already create this dir */
1426 list_for_each_entry(dir, &tr->systems, list) {
1427 system = dir->subsystem;
1428 if (strcmp(system->name, name) == 0) {
1435 /* Now see if the system itself exists. */
1436 list_for_each_entry(system, &event_subsystems, list) {
1437 if (strcmp(system->name, name) == 0)
1440 /* Reset system variable when not found */
1441 if (&system->list == &event_subsystems)
1444 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1449 system = create_new_subsystem(name);
1453 __get_system(system);
1455 dir->entry = debugfs_create_dir(name, parent);
1457 pr_warning("Failed to create system directory %s\n", name);
1458 __put_system(system);
1465 dir->subsystem = system;
1468 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1469 &ftrace_subsystem_filter_fops);
1471 kfree(system->filter);
1472 system->filter = NULL;
1473 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1476 trace_create_file("enable", 0644, dir->entry, dir,
1477 &ftrace_system_enable_fops);
1479 list_add(&dir->list, &tr->systems);
1486 /* Only print this message if failed on memory allocation */
1487 if (!dir || !system)
1488 pr_warning("No memory to create event subsystem %s\n",
1494 event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1496 struct ftrace_event_call *call = file->event_call;
1497 struct trace_array *tr = file->tr;
1498 struct list_head *head;
1499 struct dentry *d_events;
1503 * If the trace point header did not define TRACE_SYSTEM
1504 * then the system would be called "TRACE_SYSTEM".
1506 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1507 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1513 file->dir = debugfs_create_dir(call->name, d_events);
1515 pr_warning("Could not create debugfs '%s' directory\n",
1520 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1521 trace_create_file("enable", 0644, file->dir, file,
1522 &ftrace_enable_fops);
1524 #ifdef CONFIG_PERF_EVENTS
1525 if (call->event.type && call->class->reg)
1526 trace_create_file("id", 0444, file->dir,
1527 (void *)(long)call->event.type,
1528 &ftrace_event_id_fops);
1532 * Other events may have the same class. Only update
1533 * the fields if they are not already defined.
1535 head = trace_get_fields(call);
1536 if (list_empty(head)) {
1537 ret = call->class->define_fields(call);
1539 pr_warning("Could not initialize trace point"
1540 " events/%s\n", call->name);
1544 trace_create_file("filter", 0644, file->dir, file,
1545 &ftrace_event_filter_fops);
1547 trace_create_file("trigger", 0644, file->dir, file,
1548 &event_trigger_fops);
1550 trace_create_file("format", 0444, file->dir, call,
1551 &ftrace_event_format_fops);
1556 static void remove_event_from_tracers(struct ftrace_event_call *call)
1558 struct ftrace_event_file *file;
1559 struct trace_array *tr;
1561 do_for_each_event_file_safe(tr, file) {
1562 if (file->event_call != call)
1565 remove_event_file_dir(file);
1567 * The do_for_each_event_file_safe() is
1568 * a double loop. After finding the call for this
1569 * trace_array, we use break to jump to the next
1573 } while_for_each_event_file();
1576 static void event_remove(struct ftrace_event_call *call)
1578 struct trace_array *tr;
1579 struct ftrace_event_file *file;
1581 do_for_each_event_file(tr, file) {
1582 if (file->event_call != call)
1584 ftrace_event_enable_disable(file, 0);
1585 destroy_preds(file);
1587 * The do_for_each_event_file() is
1588 * a double loop. After finding the call for this
1589 * trace_array, we use break to jump to the next
1593 } while_for_each_event_file();
1595 if (call->event.funcs)
1596 __unregister_ftrace_event(&call->event);
1597 remove_event_from_tracers(call);
1598 list_del(&call->list);
1601 static int event_init(struct ftrace_event_call *call)
1605 if (WARN_ON(!call->name))
1608 if (call->class->raw_init) {
1609 ret = call->class->raw_init(call);
1610 if (ret < 0 && ret != -ENOSYS)
1611 pr_warn("Could not initialize trace events/%s\n",
1619 __register_event(struct ftrace_event_call *call, struct module *mod)
1623 ret = event_init(call);
1627 list_add(&call->list, &ftrace_events);
1633 static struct ftrace_event_file *
1634 trace_create_new_event(struct ftrace_event_call *call,
1635 struct trace_array *tr)
1637 struct ftrace_event_file *file;
1639 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1643 file->event_call = call;
1645 atomic_set(&file->sm_ref, 0);
1646 atomic_set(&file->tm_ref, 0);
1647 INIT_LIST_HEAD(&file->triggers);
1648 list_add(&file->list, &tr->events);
1653 /* Add an event to a trace directory */
1655 __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1657 struct ftrace_event_file *file;
1659 file = trace_create_new_event(call, tr);
1663 return event_create_dir(tr->event_dir, file);
1667 * Just create a decriptor for early init. A descriptor is required
1668 * for enabling events at boot. We want to enable events before
1669 * the filesystem is initialized.
1672 __trace_early_add_new_event(struct ftrace_event_call *call,
1673 struct trace_array *tr)
1675 struct ftrace_event_file *file;
1677 file = trace_create_new_event(call, tr);
1684 struct ftrace_module_file_ops;
1685 static void __add_event_to_tracers(struct ftrace_event_call *call);
1687 /* Add an additional event_call dynamically */
1688 int trace_add_event_call(struct ftrace_event_call *call)
1691 mutex_lock(&trace_types_lock);
1692 mutex_lock(&event_mutex);
1694 ret = __register_event(call, NULL);
1696 __add_event_to_tracers(call);
1698 mutex_unlock(&event_mutex);
1699 mutex_unlock(&trace_types_lock);
1704 * Must be called under locking of trace_types_lock, event_mutex and
1707 static void __trace_remove_event_call(struct ftrace_event_call *call)
1710 trace_destroy_fields(call);
1711 destroy_call_preds(call);
1714 static int probe_remove_event_call(struct ftrace_event_call *call)
1716 struct trace_array *tr;
1717 struct ftrace_event_file *file;
1719 #ifdef CONFIG_PERF_EVENTS
1720 if (call->perf_refcount)
1723 do_for_each_event_file(tr, file) {
1724 if (file->event_call != call)
1727 * We can't rely on ftrace_event_enable_disable(enable => 0)
1728 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1729 * TRACE_REG_UNREGISTER.
1731 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1734 * The do_for_each_event_file_safe() is
1735 * a double loop. After finding the call for this
1736 * trace_array, we use break to jump to the next
1740 } while_for_each_event_file();
1742 __trace_remove_event_call(call);
1747 /* Remove an event_call */
1748 int trace_remove_event_call(struct ftrace_event_call *call)
1752 mutex_lock(&trace_types_lock);
1753 mutex_lock(&event_mutex);
1754 down_write(&trace_event_sem);
1755 ret = probe_remove_event_call(call);
1756 up_write(&trace_event_sem);
1757 mutex_unlock(&event_mutex);
1758 mutex_unlock(&trace_types_lock);
1763 #define for_each_event(event, start, end) \
1764 for (event = start; \
1765 (unsigned long)event < (unsigned long)end; \
1768 #ifdef CONFIG_MODULES
1770 static void trace_module_add_events(struct module *mod)
1772 struct ftrace_event_call **call, **start, **end;
1774 if (!mod->num_trace_events)
1777 /* Don't add infrastructure for mods without tracepoints */
1778 if (trace_module_has_bad_taint(mod)) {
1779 pr_err("%s: module has bad taint, not creating trace events\n",
1784 start = mod->trace_events;
1785 end = mod->trace_events + mod->num_trace_events;
1787 for_each_event(call, start, end) {
1788 __register_event(*call, mod);
1789 __add_event_to_tracers(*call);
1793 static void trace_module_remove_events(struct module *mod)
1795 struct ftrace_event_call *call, *p;
1796 bool clear_trace = false;
1798 down_write(&trace_event_sem);
1799 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1800 if (call->mod == mod) {
1801 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1803 __trace_remove_event_call(call);
1806 up_write(&trace_event_sem);
1809 * It is safest to reset the ring buffer if the module being unloaded
1810 * registered any events that were used. The only worry is if
1811 * a new module gets loaded, and takes on the same id as the events
1812 * of this module. When printing out the buffer, traced events left
1813 * over from this module may be passed to the new module events and
1814 * unexpected results may occur.
1817 tracing_reset_all_online_cpus();
1820 static int trace_module_notify(struct notifier_block *self,
1821 unsigned long val, void *data)
1823 struct module *mod = data;
1825 mutex_lock(&trace_types_lock);
1826 mutex_lock(&event_mutex);
1828 case MODULE_STATE_COMING:
1829 trace_module_add_events(mod);
1831 case MODULE_STATE_GOING:
1832 trace_module_remove_events(mod);
1835 mutex_unlock(&event_mutex);
1836 mutex_unlock(&trace_types_lock);
1841 static struct notifier_block trace_module_nb = {
1842 .notifier_call = trace_module_notify,
1845 #endif /* CONFIG_MODULES */
1847 /* Create a new event directory structure for a trace directory. */
1849 __trace_add_event_dirs(struct trace_array *tr)
1851 struct ftrace_event_call *call;
1854 list_for_each_entry(call, &ftrace_events, list) {
1855 ret = __trace_add_new_event(call, tr);
1857 pr_warning("Could not create directory for event %s\n",
1862 struct ftrace_event_file *
1863 find_event_file(struct trace_array *tr, const char *system, const char *event)
1865 struct ftrace_event_file *file;
1866 struct ftrace_event_call *call;
1868 list_for_each_entry(file, &tr->events, list) {
1870 call = file->event_call;
1872 if (!call->name || !call->class || !call->class->reg)
1875 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1878 if (strcmp(event, call->name) == 0 &&
1879 strcmp(system, call->class->system) == 0)
1885 #ifdef CONFIG_DYNAMIC_FTRACE
1888 #define ENABLE_EVENT_STR "enable_event"
1889 #define DISABLE_EVENT_STR "disable_event"
1891 struct event_probe_data {
1892 struct ftrace_event_file *file;
1893 unsigned long count;
1899 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1901 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1902 struct event_probe_data *data = *pdata;
1908 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1910 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1914 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1916 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1917 struct event_probe_data *data = *pdata;
1925 /* Skip if the event is in a state we want to switch to */
1926 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1929 if (data->count != -1)
1932 event_enable_probe(ip, parent_ip, _data);
1936 event_enable_print(struct seq_file *m, unsigned long ip,
1937 struct ftrace_probe_ops *ops, void *_data)
1939 struct event_probe_data *data = _data;
1941 seq_printf(m, "%ps:", (void *)ip);
1943 seq_printf(m, "%s:%s:%s",
1944 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1945 data->file->event_call->class->system,
1946 data->file->event_call->name);
1948 if (data->count == -1)
1949 seq_printf(m, ":unlimited\n");
1951 seq_printf(m, ":count=%ld\n", data->count);
1957 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1960 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1961 struct event_probe_data *data = *pdata;
1968 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1971 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1972 struct event_probe_data *data = *pdata;
1974 if (WARN_ON_ONCE(data->ref <= 0))
1979 /* Remove the SOFT_MODE flag */
1980 __ftrace_event_enable_disable(data->file, 0, 1);
1981 module_put(data->file->event_call->mod);
1987 static struct ftrace_probe_ops event_enable_probe_ops = {
1988 .func = event_enable_probe,
1989 .print = event_enable_print,
1990 .init = event_enable_init,
1991 .free = event_enable_free,
1994 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1995 .func = event_enable_count_probe,
1996 .print = event_enable_print,
1997 .init = event_enable_init,
1998 .free = event_enable_free,
2001 static struct ftrace_probe_ops event_disable_probe_ops = {
2002 .func = event_enable_probe,
2003 .print = event_enable_print,
2004 .init = event_enable_init,
2005 .free = event_enable_free,
2008 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2009 .func = event_enable_count_probe,
2010 .print = event_enable_print,
2011 .init = event_enable_init,
2012 .free = event_enable_free,
2016 event_enable_func(struct ftrace_hash *hash,
2017 char *glob, char *cmd, char *param, int enabled)
2019 struct trace_array *tr = top_trace_array();
2020 struct ftrace_event_file *file;
2021 struct ftrace_probe_ops *ops;
2022 struct event_probe_data *data;
2029 /* hash funcs only work with set_ftrace_filter */
2030 if (!enabled || !param)
2033 system = strsep(¶m, ":");
2037 event = strsep(¶m, ":");
2039 mutex_lock(&event_mutex);
2042 file = find_event_file(tr, system, event);
2046 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2049 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2051 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2053 if (glob[0] == '!') {
2054 unregister_ftrace_function_probe_func(glob+1, ops);
2060 data = kzalloc(sizeof(*data), GFP_KERNEL);
2064 data->enable = enable;
2071 number = strsep(¶m, ":");
2074 if (!strlen(number))
2078 * We use the callback data field (which is a pointer)
2081 ret = kstrtoul(number, 0, &data->count);
2086 /* Don't let event modules unload while probe registered */
2087 ret = try_module_get(file->event_call->mod);
2093 ret = __ftrace_event_enable_disable(file, 1, 1);
2096 ret = register_ftrace_function_probe(glob, ops, data);
2098 * The above returns on success the # of functions enabled,
2099 * but if it didn't find any functions it returns zero.
2100 * Consider no functions a failure too.
2107 /* Just return zero, not the number of enabled functions */
2110 mutex_unlock(&event_mutex);
2114 __ftrace_event_enable_disable(file, 0, 1);
2116 module_put(file->event_call->mod);
2122 static struct ftrace_func_command event_enable_cmd = {
2123 .name = ENABLE_EVENT_STR,
2124 .func = event_enable_func,
2127 static struct ftrace_func_command event_disable_cmd = {
2128 .name = DISABLE_EVENT_STR,
2129 .func = event_enable_func,
2132 static __init int register_event_cmds(void)
2136 ret = register_ftrace_command(&event_enable_cmd);
2137 if (WARN_ON(ret < 0))
2139 ret = register_ftrace_command(&event_disable_cmd);
2140 if (WARN_ON(ret < 0))
2141 unregister_ftrace_command(&event_enable_cmd);
2145 static inline int register_event_cmds(void) { return 0; }
2146 #endif /* CONFIG_DYNAMIC_FTRACE */
2149 * The top level array has already had its ftrace_event_file
2150 * descriptors created in order to allow for early events to
2151 * be recorded. This function is called after the debugfs has been
2152 * initialized, and we now have to create the files associated
2156 __trace_early_add_event_dirs(struct trace_array *tr)
2158 struct ftrace_event_file *file;
2162 list_for_each_entry(file, &tr->events, list) {
2163 ret = event_create_dir(tr->event_dir, file);
2165 pr_warning("Could not create directory for event %s\n",
2166 file->event_call->name);
2171 * For early boot up, the top trace array requires to have
2172 * a list of events that can be enabled. This must be done before
2173 * the filesystem is set up in order to allow events to be traced
2177 __trace_early_add_events(struct trace_array *tr)
2179 struct ftrace_event_call *call;
2182 list_for_each_entry(call, &ftrace_events, list) {
2183 /* Early boot up should not have any modules loaded */
2184 if (WARN_ON_ONCE(call->mod))
2187 ret = __trace_early_add_new_event(call, tr);
2189 pr_warning("Could not create early event %s\n",
2194 /* Remove the event directory structure for a trace directory. */
2196 __trace_remove_event_dirs(struct trace_array *tr)
2198 struct ftrace_event_file *file, *next;
2200 list_for_each_entry_safe(file, next, &tr->events, list)
2201 remove_event_file_dir(file);
2204 static void __add_event_to_tracers(struct ftrace_event_call *call)
2206 struct trace_array *tr;
2208 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2209 __trace_add_new_event(call, tr);
2212 extern struct ftrace_event_call *__start_ftrace_events[];
2213 extern struct ftrace_event_call *__stop_ftrace_events[];
2215 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2217 static __init int setup_trace_event(char *str)
2219 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2220 ring_buffer_expanded = true;
2221 tracing_selftest_disabled = true;
2225 __setup("trace_event=", setup_trace_event);
2227 /* Expects to have event_mutex held when called */
2229 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2231 struct dentry *d_events;
2232 struct dentry *entry;
2234 entry = debugfs_create_file("set_event", 0644, parent,
2235 tr, &ftrace_set_event_fops);
2237 pr_warning("Could not create debugfs 'set_event' entry\n");
2241 d_events = debugfs_create_dir("events", parent);
2243 pr_warning("Could not create debugfs 'events' directory\n");
2247 /* ring buffer internal formats */
2248 trace_create_file("header_page", 0444, d_events,
2249 ring_buffer_print_page_header,
2250 &ftrace_show_header_fops);
2252 trace_create_file("header_event", 0444, d_events,
2253 ring_buffer_print_entry_header,
2254 &ftrace_show_header_fops);
2256 trace_create_file("enable", 0644, d_events,
2257 tr, &ftrace_tr_enable_fops);
2259 tr->event_dir = d_events;
2265 * event_trace_add_tracer - add a instance of a trace_array to events
2266 * @parent: The parent dentry to place the files/directories for events in
2267 * @tr: The trace array associated with these events
2269 * When a new instance is created, it needs to set up its events
2270 * directory, as well as other files associated with events. It also
2271 * creates the event hierachry in the @parent/events directory.
2273 * Returns 0 on success.
2275 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2279 mutex_lock(&event_mutex);
2281 ret = create_event_toplevel_files(parent, tr);
2285 down_write(&trace_event_sem);
2286 __trace_add_event_dirs(tr);
2287 up_write(&trace_event_sem);
2290 mutex_unlock(&event_mutex);
2296 * The top trace array already had its file descriptors created.
2297 * Now the files themselves need to be created.
2300 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2304 mutex_lock(&event_mutex);
2306 ret = create_event_toplevel_files(parent, tr);
2310 down_write(&trace_event_sem);
2311 __trace_early_add_event_dirs(tr);
2312 up_write(&trace_event_sem);
2315 mutex_unlock(&event_mutex);
2320 int event_trace_del_tracer(struct trace_array *tr)
2322 mutex_lock(&event_mutex);
2324 /* Disable any event triggers and associated soft-disabled events */
2325 clear_event_triggers(tr);
2327 /* Disable any running events */
2328 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2330 /* Access to events are within rcu_read_lock_sched() */
2331 synchronize_sched();
2333 down_write(&trace_event_sem);
2334 __trace_remove_event_dirs(tr);
2335 debugfs_remove_recursive(tr->event_dir);
2336 up_write(&trace_event_sem);
2338 tr->event_dir = NULL;
2340 mutex_unlock(&event_mutex);
2345 static __init int event_trace_memsetup(void)
2347 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2348 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2352 static __init int event_trace_enable(void)
2354 struct trace_array *tr = top_trace_array();
2355 struct ftrace_event_call **iter, *call;
2356 char *buf = bootup_event_buf;
2360 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2363 ret = event_init(call);
2365 list_add(&call->list, &ftrace_events);
2369 * We need the top trace array to have a working set of trace
2370 * points at early init, before the debug files and directories
2371 * are created. Create the file entries now, and attach them
2372 * to the actual file dentries later.
2374 __trace_early_add_events(tr);
2377 token = strsep(&buf, ",");
2384 ret = ftrace_set_clr_event(tr, token, 1);
2386 pr_warn("Failed to enable trace event: %s\n", token);
2389 trace_printk_start_comm();
2391 register_event_cmds();
2393 register_trigger_cmds();
2398 static __init int event_trace_init(void)
2400 struct trace_array *tr;
2401 struct dentry *d_tracer;
2402 struct dentry *entry;
2405 tr = top_trace_array();
2407 d_tracer = tracing_init_dentry();
2411 entry = debugfs_create_file("available_events", 0444, d_tracer,
2412 tr, &ftrace_avail_fops);
2414 pr_warning("Could not create debugfs "
2415 "'available_events' entry\n");
2417 if (trace_define_common_fields())
2418 pr_warning("tracing: Failed to allocate common fields");
2420 ret = early_event_add_tracer(d_tracer, tr);
2424 #ifdef CONFIG_MODULES
2425 ret = register_module_notifier(&trace_module_nb);
2427 pr_warning("Failed to register trace events module notifier\n");
2431 early_initcall(event_trace_memsetup);
2432 core_initcall(event_trace_enable);
2433 fs_initcall(event_trace_init);
2435 #ifdef CONFIG_FTRACE_STARTUP_TEST
2437 static DEFINE_SPINLOCK(test_spinlock);
2438 static DEFINE_SPINLOCK(test_spinlock_irq);
2439 static DEFINE_MUTEX(test_mutex);
2441 static __init void test_work(struct work_struct *dummy)
2443 spin_lock(&test_spinlock);
2444 spin_lock_irq(&test_spinlock_irq);
2446 spin_unlock_irq(&test_spinlock_irq);
2447 spin_unlock(&test_spinlock);
2449 mutex_lock(&test_mutex);
2451 mutex_unlock(&test_mutex);
2454 static __init int event_test_thread(void *unused)
2458 test_malloc = kmalloc(1234, GFP_KERNEL);
2460 pr_info("failed to kmalloc\n");
2462 schedule_on_each_cpu(test_work);
2466 set_current_state(TASK_INTERRUPTIBLE);
2467 while (!kthread_should_stop())
2474 * Do various things that may trigger events.
2476 static __init void event_test_stuff(void)
2478 struct task_struct *test_thread;
2480 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2482 kthread_stop(test_thread);
2486 * For every trace event defined, we will test each trace point separately,
2487 * and then by groups, and finally all trace points.
2489 static __init void event_trace_self_tests(void)
2491 struct ftrace_subsystem_dir *dir;
2492 struct ftrace_event_file *file;
2493 struct ftrace_event_call *call;
2494 struct event_subsystem *system;
2495 struct trace_array *tr;
2498 tr = top_trace_array();
2500 pr_info("Running tests on trace events:\n");
2502 list_for_each_entry(file, &tr->events, list) {
2504 call = file->event_call;
2506 /* Only test those that have a probe */
2507 if (!call->class || !call->class->probe)
2511 * Testing syscall events here is pretty useless, but
2512 * we still do it if configured. But this is time consuming.
2513 * What we really need is a user thread to perform the
2514 * syscalls as we test.
2516 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2517 if (call->class->system &&
2518 strcmp(call->class->system, "syscalls") == 0)
2522 pr_info("Testing event %s: ", call->name);
2525 * If an event is already enabled, someone is using
2526 * it and the self test should not be on.
2528 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2529 pr_warning("Enabled event during self test!\n");
2534 ftrace_event_enable_disable(file, 1);
2536 ftrace_event_enable_disable(file, 0);
2541 /* Now test at the sub system level */
2543 pr_info("Running tests on trace event systems:\n");
2545 list_for_each_entry(dir, &tr->systems, list) {
2547 system = dir->subsystem;
2549 /* the ftrace system is special, skip it */
2550 if (strcmp(system->name, "ftrace") == 0)
2553 pr_info("Testing event system %s: ", system->name);
2555 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2556 if (WARN_ON_ONCE(ret)) {
2557 pr_warning("error enabling system %s\n",
2564 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2565 if (WARN_ON_ONCE(ret)) {
2566 pr_warning("error disabling system %s\n",
2574 /* Test with all events enabled */
2576 pr_info("Running tests on all trace events:\n");
2577 pr_info("Testing all events: ");
2579 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2580 if (WARN_ON_ONCE(ret)) {
2581 pr_warning("error enabling all events\n");
2588 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2589 if (WARN_ON_ONCE(ret)) {
2590 pr_warning("error disabling all events\n");
2597 #ifdef CONFIG_FUNCTION_TRACER
2599 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2602 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2603 struct ftrace_ops *op, struct pt_regs *pt_regs)
2605 struct ring_buffer_event *event;
2606 struct ring_buffer *buffer;
2607 struct ftrace_entry *entry;
2608 unsigned long flags;
2613 pc = preempt_count();
2614 preempt_disable_notrace();
2615 cpu = raw_smp_processor_id();
2616 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2621 local_save_flags(flags);
2623 event = trace_current_buffer_lock_reserve(&buffer,
2624 TRACE_FN, sizeof(*entry),
2628 entry = ring_buffer_event_data(event);
2630 entry->parent_ip = parent_ip;
2632 trace_buffer_unlock_commit(buffer, event, flags, pc);
2635 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2636 preempt_enable_notrace();
2639 static struct ftrace_ops trace_ops __initdata =
2641 .func = function_test_events_call,
2642 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2645 static __init void event_trace_self_test_with_function(void)
2648 ret = register_ftrace_function(&trace_ops);
2649 if (WARN_ON(ret < 0)) {
2650 pr_info("Failed to enable function tracer for event tests\n");
2653 pr_info("Running tests again, along with the function tracer\n");
2654 event_trace_self_tests();
2655 unregister_ftrace_function(&trace_ops);
2658 static __init void event_trace_self_test_with_function(void)
2663 static __init int event_trace_self_tests_init(void)
2665 if (!tracing_selftest_disabled) {
2666 event_trace_self_tests();
2667 event_trace_self_test_with_function();
2673 late_initcall(event_trace_self_tests_init);