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 DEFINE_MUTEX(event_storage_mutex);
31 EXPORT_SYMBOL_GPL(event_storage_mutex);
33 char event_storage[EVENT_STORAGE_SIZE];
34 EXPORT_SYMBOL_GPL(event_storage);
36 LIST_HEAD(ftrace_events);
37 static LIST_HEAD(ftrace_common_fields);
39 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41 static struct kmem_cache *field_cachep;
42 static struct kmem_cache *file_cachep;
44 /* Double loops, do not use break, only goto's work */
45 #define do_for_each_event_file(tr, file) \
46 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
47 list_for_each_entry(file, &tr->events, list)
49 #define do_for_each_event_file_safe(tr, file) \
50 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
51 struct ftrace_event_file *___n; \
52 list_for_each_entry_safe(file, ___n, &tr->events, list)
54 #define while_for_each_event_file() \
57 static struct list_head *
58 trace_get_fields(struct ftrace_event_call *event_call)
60 if (!event_call->class->get_fields)
61 return &event_call->class->fields;
62 return event_call->class->get_fields(event_call);
65 static struct ftrace_event_field *
66 __find_event_field(struct list_head *head, char *name)
68 struct ftrace_event_field *field;
70 list_for_each_entry(field, head, link) {
71 if (!strcmp(field->name, name))
78 struct ftrace_event_field *
79 trace_find_event_field(struct ftrace_event_call *call, char *name)
81 struct ftrace_event_field *field;
82 struct list_head *head;
84 field = __find_event_field(&ftrace_common_fields, name);
88 head = trace_get_fields(call);
89 return __find_event_field(head, name);
92 static int __trace_define_field(struct list_head *head, const char *type,
93 const char *name, int offset, int size,
94 int is_signed, int filter_type)
96 struct ftrace_event_field *field;
98 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
105 if (filter_type == FILTER_OTHER)
106 field->filter_type = filter_assign_type(type);
108 field->filter_type = filter_type;
110 field->offset = offset;
112 field->is_signed = is_signed;
114 list_add(&field->link, head);
119 kmem_cache_free(field_cachep, field);
124 int trace_define_field(struct ftrace_event_call *call, const char *type,
125 const char *name, int offset, int size, int is_signed,
128 struct list_head *head;
130 if (WARN_ON(!call->class))
133 head = trace_get_fields(call);
134 return __trace_define_field(head, type, name, offset, size,
135 is_signed, filter_type);
137 EXPORT_SYMBOL_GPL(trace_define_field);
139 #define __common_field(type, item) \
140 ret = __trace_define_field(&ftrace_common_fields, #type, \
142 offsetof(typeof(ent), item), \
144 is_signed_type(type), FILTER_OTHER); \
148 static int trace_define_common_fields(void)
151 struct trace_entry ent;
153 __common_field(unsigned short, type);
154 __common_field(unsigned char, flags);
155 __common_field(unsigned char, preempt_count);
156 __common_field(int, pid);
161 static void trace_destroy_fields(struct ftrace_event_call *call)
163 struct ftrace_event_field *field, *next;
164 struct list_head *head;
166 head = trace_get_fields(call);
167 list_for_each_entry_safe(field, next, head, link) {
168 list_del(&field->link);
169 kmem_cache_free(field_cachep, field);
173 int trace_event_raw_init(struct ftrace_event_call *call)
177 id = register_ftrace_event(&call->event);
183 EXPORT_SYMBOL_GPL(trace_event_raw_init);
185 int ftrace_event_reg(struct ftrace_event_call *call,
186 enum trace_reg type, void *data)
188 struct ftrace_event_file *file = data;
191 case TRACE_REG_REGISTER:
192 return tracepoint_probe_register(call->name,
195 case TRACE_REG_UNREGISTER:
196 tracepoint_probe_unregister(call->name,
201 #ifdef CONFIG_PERF_EVENTS
202 case TRACE_REG_PERF_REGISTER:
203 return tracepoint_probe_register(call->name,
204 call->class->perf_probe,
206 case TRACE_REG_PERF_UNREGISTER:
207 tracepoint_probe_unregister(call->name,
208 call->class->perf_probe,
211 case TRACE_REG_PERF_OPEN:
212 case TRACE_REG_PERF_CLOSE:
213 case TRACE_REG_PERF_ADD:
214 case TRACE_REG_PERF_DEL:
220 EXPORT_SYMBOL_GPL(ftrace_event_reg);
222 void trace_event_enable_cmd_record(bool enable)
224 struct ftrace_event_file *file;
225 struct trace_array *tr;
227 mutex_lock(&event_mutex);
228 do_for_each_event_file(tr, file) {
230 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
234 tracing_start_cmdline_record();
235 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
237 tracing_stop_cmdline_record();
238 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
240 } while_for_each_event_file();
241 mutex_unlock(&event_mutex);
244 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
245 int enable, int soft_disable)
247 struct ftrace_event_call *call = file->event_call;
254 * When soft_disable is set and enable is cleared, we want
255 * to clear the SOFT_DISABLED flag but leave the event in the
256 * state that it was. That is, if the event was enabled and
257 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
258 * is set we do not want the event to be enabled before we
261 * When soft_disable is not set but the SOFT_MODE flag is,
262 * we do nothing. Do not disable the tracepoint, otherwise
263 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
266 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
267 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
269 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
271 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
272 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
273 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
274 tracing_stop_cmdline_record();
275 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
277 call->class->reg(call, TRACE_REG_UNREGISTER, file);
279 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
280 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
281 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
285 * When soft_disable is set and enable is set, we want to
286 * register the tracepoint for the event, but leave the event
287 * as is. That means, if the event was already enabled, we do
288 * nothing (but set SOFT_MODE). If the event is disabled, we
289 * set SOFT_DISABLED before enabling the event tracepoint, so
290 * it still seems to be disabled.
293 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
295 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
297 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
299 /* Keep the event disabled, when going to SOFT_MODE. */
301 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
303 if (trace_flags & TRACE_ITER_RECORD_CMD) {
304 tracing_start_cmdline_record();
305 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
307 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
309 tracing_stop_cmdline_record();
310 pr_info("event trace: Could not enable event "
314 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
316 /* WAS_ENABLED gets set but never cleared. */
317 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
325 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
328 return __ftrace_event_enable_disable(file, enable, 0);
331 static void ftrace_clear_events(struct trace_array *tr)
333 struct ftrace_event_file *file;
335 mutex_lock(&event_mutex);
336 list_for_each_entry(file, &tr->events, list) {
337 ftrace_event_enable_disable(file, 0);
339 mutex_unlock(&event_mutex);
342 static void __put_system(struct event_subsystem *system)
344 struct event_filter *filter = system->filter;
346 WARN_ON_ONCE(system->ref_count == 0);
347 if (--system->ref_count)
350 list_del(&system->list);
353 kfree(filter->filter_string);
359 static void __get_system(struct event_subsystem *system)
361 WARN_ON_ONCE(system->ref_count == 0);
365 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
367 WARN_ON_ONCE(dir->ref_count == 0);
369 __get_system(dir->subsystem);
372 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
374 WARN_ON_ONCE(dir->ref_count == 0);
375 /* If the subsystem is about to be freed, the dir must be too */
376 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
378 __put_system(dir->subsystem);
379 if (!--dir->ref_count)
383 static void put_system(struct ftrace_subsystem_dir *dir)
385 mutex_lock(&event_mutex);
386 __put_system_dir(dir);
387 mutex_unlock(&event_mutex);
391 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
393 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
394 const char *sub, const char *event, int set)
396 struct ftrace_event_file *file;
397 struct ftrace_event_call *call;
400 mutex_lock(&event_mutex);
401 list_for_each_entry(file, &tr->events, list) {
403 call = file->event_call;
405 if (!call->name || !call->class || !call->class->reg)
408 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
412 strcmp(match, call->name) != 0 &&
413 strcmp(match, call->class->system) != 0)
416 if (sub && strcmp(sub, call->class->system) != 0)
419 if (event && strcmp(event, call->name) != 0)
422 ftrace_event_enable_disable(file, set);
426 mutex_unlock(&event_mutex);
431 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
433 char *event = NULL, *sub = NULL, *match;
436 * The buf format can be <subsystem>:<event-name>
437 * *:<event-name> means any event by that name.
438 * :<event-name> is the same.
440 * <subsystem>:* means all events in that subsystem
441 * <subsystem>: means the same.
443 * <name> (no ':') means all events in a subsystem with
444 * the name <name> or any event that matches <name>
447 match = strsep(&buf, ":");
453 if (!strlen(sub) || strcmp(sub, "*") == 0)
455 if (!strlen(event) || strcmp(event, "*") == 0)
459 return __ftrace_set_clr_event(tr, match, sub, event, set);
463 * trace_set_clr_event - enable or disable an event
464 * @system: system name to match (NULL for any system)
465 * @event: event name to match (NULL for all events, within system)
466 * @set: 1 to enable, 0 to disable
468 * This is a way for other parts of the kernel to enable or disable
471 * Returns 0 on success, -EINVAL if the parameters do not match any
474 int trace_set_clr_event(const char *system, const char *event, int set)
476 struct trace_array *tr = top_trace_array();
478 return __ftrace_set_clr_event(tr, NULL, system, event, set);
480 EXPORT_SYMBOL_GPL(trace_set_clr_event);
482 /* 128 should be much more than enough */
483 #define EVENT_BUF_SIZE 127
486 ftrace_event_write(struct file *file, const char __user *ubuf,
487 size_t cnt, loff_t *ppos)
489 struct trace_parser parser;
490 struct seq_file *m = file->private_data;
491 struct trace_array *tr = m->private;
497 ret = tracing_update_buffers();
501 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
504 read = trace_get_user(&parser, ubuf, cnt, ppos);
506 if (read >= 0 && trace_parser_loaded((&parser))) {
509 if (*parser.buffer == '!')
512 parser.buffer[parser.idx] = 0;
514 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
522 trace_parser_put(&parser);
528 t_next(struct seq_file *m, void *v, loff_t *pos)
530 struct ftrace_event_file *file = v;
531 struct ftrace_event_call *call;
532 struct trace_array *tr = m->private;
536 list_for_each_entry_continue(file, &tr->events, list) {
537 call = file->event_call;
539 * The ftrace subsystem is for showing formats only.
540 * They can not be enabled or disabled via the event files.
542 if (call->class && call->class->reg)
549 static void *t_start(struct seq_file *m, loff_t *pos)
551 struct ftrace_event_file *file;
552 struct trace_array *tr = m->private;
555 mutex_lock(&event_mutex);
557 file = list_entry(&tr->events, struct ftrace_event_file, list);
558 for (l = 0; l <= *pos; ) {
559 file = t_next(m, file, &l);
567 s_next(struct seq_file *m, void *v, loff_t *pos)
569 struct ftrace_event_file *file = v;
570 struct trace_array *tr = m->private;
574 list_for_each_entry_continue(file, &tr->events, list) {
575 if (file->flags & FTRACE_EVENT_FL_ENABLED)
582 static void *s_start(struct seq_file *m, loff_t *pos)
584 struct ftrace_event_file *file;
585 struct trace_array *tr = m->private;
588 mutex_lock(&event_mutex);
590 file = list_entry(&tr->events, struct ftrace_event_file, list);
591 for (l = 0; l <= *pos; ) {
592 file = s_next(m, file, &l);
599 static int t_show(struct seq_file *m, void *v)
601 struct ftrace_event_file *file = v;
602 struct ftrace_event_call *call = file->event_call;
604 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
605 seq_printf(m, "%s:", call->class->system);
606 seq_printf(m, "%s\n", call->name);
611 static void t_stop(struct seq_file *m, void *p)
613 mutex_unlock(&event_mutex);
617 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
620 struct ftrace_event_file *file = filp->private_data;
623 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
624 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)
626 else if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
633 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
637 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
640 struct ftrace_event_file *file = filp->private_data;
647 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
651 ret = tracing_update_buffers();
658 mutex_lock(&event_mutex);
659 ret = ftrace_event_enable_disable(file, val);
660 mutex_unlock(&event_mutex);
669 return ret ? ret : cnt;
673 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
676 const char set_to_char[4] = { '?', '0', '1', 'X' };
677 struct ftrace_subsystem_dir *dir = filp->private_data;
678 struct event_subsystem *system = dir->subsystem;
679 struct ftrace_event_call *call;
680 struct ftrace_event_file *file;
681 struct trace_array *tr = dir->tr;
686 mutex_lock(&event_mutex);
687 list_for_each_entry(file, &tr->events, list) {
688 call = file->event_call;
689 if (!call->name || !call->class || !call->class->reg)
692 if (system && strcmp(call->class->system, system->name) != 0)
696 * We need to find out if all the events are set
697 * or if all events or cleared, or if we have
700 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
703 * If we have a mixture, no need to look further.
708 mutex_unlock(&event_mutex);
710 buf[0] = set_to_char[set];
713 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
719 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
722 struct ftrace_subsystem_dir *dir = filp->private_data;
723 struct event_subsystem *system = dir->subsystem;
724 const char *name = NULL;
728 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
732 ret = tracing_update_buffers();
736 if (val != 0 && val != 1)
740 * Opening of "enable" adds a ref count to system,
741 * so the name is safe to use.
746 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
760 FORMAT_FIELD_SEPERATOR = 2,
764 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
766 struct ftrace_event_call *call = m->private;
767 struct ftrace_event_field *field;
768 struct list_head *common_head = &ftrace_common_fields;
769 struct list_head *head = trace_get_fields(call);
773 switch ((unsigned long)v) {
775 if (unlikely(list_empty(common_head)))
778 field = list_entry(common_head->prev,
779 struct ftrace_event_field, link);
782 case FORMAT_FIELD_SEPERATOR:
783 if (unlikely(list_empty(head)))
786 field = list_entry(head->prev, struct ftrace_event_field, link);
789 case FORMAT_PRINTFMT:
795 if (field->link.prev == common_head)
796 return (void *)FORMAT_FIELD_SEPERATOR;
797 else if (field->link.prev == head)
798 return (void *)FORMAT_PRINTFMT;
800 field = list_entry(field->link.prev, struct ftrace_event_field, link);
805 static void *f_start(struct seq_file *m, loff_t *pos)
810 /* Start by showing the header */
812 return (void *)FORMAT_HEADER;
814 p = (void *)FORMAT_HEADER;
816 p = f_next(m, p, &l);
817 } while (p && l < *pos);
822 static int f_show(struct seq_file *m, void *v)
824 struct ftrace_event_call *call = m->private;
825 struct ftrace_event_field *field;
826 const char *array_descriptor;
828 switch ((unsigned long)v) {
830 seq_printf(m, "name: %s\n", call->name);
831 seq_printf(m, "ID: %d\n", call->event.type);
832 seq_printf(m, "format:\n");
835 case FORMAT_FIELD_SEPERATOR:
839 case FORMAT_PRINTFMT:
840 seq_printf(m, "\nprint fmt: %s\n",
848 * Smartly shows the array type(except dynamic array).
851 * If TYPE := TYPE[LEN], it is shown:
852 * field:TYPE VAR[LEN]
854 array_descriptor = strchr(field->type, '[');
856 if (!strncmp(field->type, "__data_loc", 10))
857 array_descriptor = NULL;
859 if (!array_descriptor)
860 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
861 field->type, field->name, field->offset,
862 field->size, !!field->is_signed);
864 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
865 (int)(array_descriptor - field->type),
866 field->type, field->name,
867 array_descriptor, field->offset,
868 field->size, !!field->is_signed);
873 static void f_stop(struct seq_file *m, void *p)
877 static const struct seq_operations trace_format_seq_ops = {
884 static int trace_format_open(struct inode *inode, struct file *file)
886 struct ftrace_event_call *call = inode->i_private;
890 ret = seq_open(file, &trace_format_seq_ops);
894 m = file->private_data;
901 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
903 struct ftrace_event_call *call = filp->private_data;
910 s = kmalloc(sizeof(*s), GFP_KERNEL);
915 trace_seq_printf(s, "%d\n", call->event.type);
917 r = simple_read_from_buffer(ubuf, cnt, ppos,
924 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
927 struct ftrace_event_call *call = filp->private_data;
934 s = kmalloc(sizeof(*s), GFP_KERNEL);
940 print_event_filter(call, s);
941 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
949 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
952 struct ftrace_event_call *call = filp->private_data;
956 if (cnt >= PAGE_SIZE)
959 buf = (char *)__get_free_page(GFP_TEMPORARY);
963 if (copy_from_user(buf, ubuf, cnt)) {
964 free_page((unsigned long) buf);
969 err = apply_event_filter(call, buf);
970 free_page((unsigned long) buf);
979 static LIST_HEAD(event_subsystems);
981 static int subsystem_open(struct inode *inode, struct file *filp)
983 struct event_subsystem *system = NULL;
984 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
985 struct trace_array *tr;
988 /* Make sure the system still exists */
989 mutex_lock(&event_mutex);
990 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
991 list_for_each_entry(dir, &tr->systems, list) {
992 if (dir == inode->i_private) {
993 /* Don't open systems with no events */
994 if (dir->nr_events) {
995 __get_system_dir(dir);
996 system = dir->subsystem;
1003 mutex_unlock(&event_mutex);
1008 /* Some versions of gcc think dir can be uninitialized here */
1011 ret = tracing_open_generic(inode, filp);
1018 static int system_tr_open(struct inode *inode, struct file *filp)
1020 struct ftrace_subsystem_dir *dir;
1021 struct trace_array *tr = inode->i_private;
1024 /* Make a temporary dir that has no system but points to tr */
1025 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1031 ret = tracing_open_generic(inode, filp);
1035 filp->private_data = dir;
1040 static int subsystem_release(struct inode *inode, struct file *file)
1042 struct ftrace_subsystem_dir *dir = file->private_data;
1045 * If dir->subsystem is NULL, then this is a temporary
1046 * descriptor that was made for a trace_array to enable
1058 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1061 struct ftrace_subsystem_dir *dir = filp->private_data;
1062 struct event_subsystem *system = dir->subsystem;
1063 struct trace_seq *s;
1069 s = kmalloc(sizeof(*s), GFP_KERNEL);
1075 print_subsystem_event_filter(system, s);
1076 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1084 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1087 struct ftrace_subsystem_dir *dir = filp->private_data;
1091 if (cnt >= PAGE_SIZE)
1094 buf = (char *)__get_free_page(GFP_TEMPORARY);
1098 if (copy_from_user(buf, ubuf, cnt)) {
1099 free_page((unsigned long) buf);
1104 err = apply_subsystem_event_filter(dir, buf);
1105 free_page((unsigned long) buf);
1115 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1117 int (*func)(struct trace_seq *s) = filp->private_data;
1118 struct trace_seq *s;
1124 s = kmalloc(sizeof(*s), GFP_KERNEL);
1131 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1138 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1139 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1141 static const struct seq_operations show_event_seq_ops = {
1148 static const struct seq_operations show_set_event_seq_ops = {
1155 static const struct file_operations ftrace_avail_fops = {
1156 .open = ftrace_event_avail_open,
1158 .llseek = seq_lseek,
1159 .release = seq_release,
1162 static const struct file_operations ftrace_set_event_fops = {
1163 .open = ftrace_event_set_open,
1165 .write = ftrace_event_write,
1166 .llseek = seq_lseek,
1167 .release = seq_release,
1170 static const struct file_operations ftrace_enable_fops = {
1171 .open = tracing_open_generic,
1172 .read = event_enable_read,
1173 .write = event_enable_write,
1174 .llseek = default_llseek,
1177 static const struct file_operations ftrace_event_format_fops = {
1178 .open = trace_format_open,
1180 .llseek = seq_lseek,
1181 .release = seq_release,
1184 static const struct file_operations ftrace_event_id_fops = {
1185 .open = tracing_open_generic,
1186 .read = event_id_read,
1187 .llseek = default_llseek,
1190 static const struct file_operations ftrace_event_filter_fops = {
1191 .open = tracing_open_generic,
1192 .read = event_filter_read,
1193 .write = event_filter_write,
1194 .llseek = default_llseek,
1197 static const struct file_operations ftrace_subsystem_filter_fops = {
1198 .open = subsystem_open,
1199 .read = subsystem_filter_read,
1200 .write = subsystem_filter_write,
1201 .llseek = default_llseek,
1202 .release = subsystem_release,
1205 static const struct file_operations ftrace_system_enable_fops = {
1206 .open = subsystem_open,
1207 .read = system_enable_read,
1208 .write = system_enable_write,
1209 .llseek = default_llseek,
1210 .release = subsystem_release,
1213 static const struct file_operations ftrace_tr_enable_fops = {
1214 .open = system_tr_open,
1215 .read = system_enable_read,
1216 .write = system_enable_write,
1217 .llseek = default_llseek,
1218 .release = subsystem_release,
1221 static const struct file_operations ftrace_show_header_fops = {
1222 .open = tracing_open_generic,
1223 .read = show_header,
1224 .llseek = default_llseek,
1228 ftrace_event_open(struct inode *inode, struct file *file,
1229 const struct seq_operations *seq_ops)
1234 ret = seq_open(file, seq_ops);
1237 m = file->private_data;
1238 /* copy tr over to seq ops */
1239 m->private = inode->i_private;
1245 ftrace_event_avail_open(struct inode *inode, struct file *file)
1247 const struct seq_operations *seq_ops = &show_event_seq_ops;
1249 return ftrace_event_open(inode, file, seq_ops);
1253 ftrace_event_set_open(struct inode *inode, struct file *file)
1255 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1256 struct trace_array *tr = inode->i_private;
1258 if ((file->f_mode & FMODE_WRITE) &&
1259 (file->f_flags & O_TRUNC))
1260 ftrace_clear_events(tr);
1262 return ftrace_event_open(inode, file, seq_ops);
1265 static struct event_subsystem *
1266 create_new_subsystem(const char *name)
1268 struct event_subsystem *system;
1270 /* need to create new entry */
1271 system = kmalloc(sizeof(*system), GFP_KERNEL);
1275 system->ref_count = 1;
1276 system->name = name;
1278 system->filter = NULL;
1280 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1281 if (!system->filter)
1284 list_add(&system->list, &event_subsystems);
1293 static struct dentry *
1294 event_subsystem_dir(struct trace_array *tr, const char *name,
1295 struct ftrace_event_file *file, struct dentry *parent)
1297 struct ftrace_subsystem_dir *dir;
1298 struct event_subsystem *system;
1299 struct dentry *entry;
1301 /* First see if we did not already create this dir */
1302 list_for_each_entry(dir, &tr->systems, list) {
1303 system = dir->subsystem;
1304 if (strcmp(system->name, name) == 0) {
1311 /* Now see if the system itself exists. */
1312 list_for_each_entry(system, &event_subsystems, list) {
1313 if (strcmp(system->name, name) == 0)
1316 /* Reset system variable when not found */
1317 if (&system->list == &event_subsystems)
1320 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1325 system = create_new_subsystem(name);
1329 __get_system(system);
1331 dir->entry = debugfs_create_dir(name, parent);
1333 pr_warning("Failed to create system directory %s\n", name);
1334 __put_system(system);
1341 dir->subsystem = system;
1344 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1345 &ftrace_subsystem_filter_fops);
1347 kfree(system->filter);
1348 system->filter = NULL;
1349 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1352 trace_create_file("enable", 0644, dir->entry, dir,
1353 &ftrace_system_enable_fops);
1355 list_add(&dir->list, &tr->systems);
1362 /* Only print this message if failed on memory allocation */
1363 if (!dir || !system)
1364 pr_warning("No memory to create event subsystem %s\n",
1370 event_create_dir(struct dentry *parent,
1371 struct ftrace_event_file *file,
1372 const struct file_operations *id,
1373 const struct file_operations *enable,
1374 const struct file_operations *filter,
1375 const struct file_operations *format)
1377 struct ftrace_event_call *call = file->event_call;
1378 struct trace_array *tr = file->tr;
1379 struct list_head *head;
1380 struct dentry *d_events;
1384 * If the trace point header did not define TRACE_SYSTEM
1385 * then the system would be called "TRACE_SYSTEM".
1387 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1388 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1394 file->dir = debugfs_create_dir(call->name, d_events);
1396 pr_warning("Could not create debugfs '%s' directory\n",
1401 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1402 trace_create_file("enable", 0644, file->dir, file,
1405 #ifdef CONFIG_PERF_EVENTS
1406 if (call->event.type && call->class->reg)
1407 trace_create_file("id", 0444, file->dir, call,
1412 * Other events may have the same class. Only update
1413 * the fields if they are not already defined.
1415 head = trace_get_fields(call);
1416 if (list_empty(head)) {
1417 ret = call->class->define_fields(call);
1419 pr_warning("Could not initialize trace point"
1420 " events/%s\n", call->name);
1424 trace_create_file("filter", 0644, file->dir, call,
1427 trace_create_file("format", 0444, file->dir, call,
1433 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1438 if (!--dir->nr_events) {
1439 debugfs_remove_recursive(dir->entry);
1440 list_del(&dir->list);
1441 __put_system_dir(dir);
1445 static void remove_event_from_tracers(struct ftrace_event_call *call)
1447 struct ftrace_event_file *file;
1448 struct trace_array *tr;
1450 do_for_each_event_file_safe(tr, file) {
1452 if (file->event_call != call)
1455 list_del(&file->list);
1456 debugfs_remove_recursive(file->dir);
1457 remove_subsystem(file->system);
1458 kmem_cache_free(file_cachep, file);
1461 * The do_for_each_event_file_safe() is
1462 * a double loop. After finding the call for this
1463 * trace_array, we use break to jump to the next
1467 } while_for_each_event_file();
1470 static void event_remove(struct ftrace_event_call *call)
1472 struct trace_array *tr;
1473 struct ftrace_event_file *file;
1475 do_for_each_event_file(tr, file) {
1476 if (file->event_call != call)
1478 ftrace_event_enable_disable(file, 0);
1480 * The do_for_each_event_file() is
1481 * a double loop. After finding the call for this
1482 * trace_array, we use break to jump to the next
1486 } while_for_each_event_file();
1488 if (call->event.funcs)
1489 __unregister_ftrace_event(&call->event);
1490 remove_event_from_tracers(call);
1491 list_del(&call->list);
1494 static int event_init(struct ftrace_event_call *call)
1498 if (WARN_ON(!call->name))
1501 if (call->class->raw_init) {
1502 ret = call->class->raw_init(call);
1503 if (ret < 0 && ret != -ENOSYS)
1504 pr_warn("Could not initialize trace events/%s\n",
1512 __register_event(struct ftrace_event_call *call, struct module *mod)
1516 ret = event_init(call);
1520 list_add(&call->list, &ftrace_events);
1526 /* Add an event to a trace directory */
1528 __trace_add_new_event(struct ftrace_event_call *call,
1529 struct trace_array *tr,
1530 const struct file_operations *id,
1531 const struct file_operations *enable,
1532 const struct file_operations *filter,
1533 const struct file_operations *format)
1535 struct ftrace_event_file *file;
1537 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1541 file->event_call = call;
1543 list_add(&file->list, &tr->events);
1545 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1549 * Just create a decriptor for early init. A descriptor is required
1550 * for enabling events at boot. We want to enable events before
1551 * the filesystem is initialized.
1554 __trace_early_add_new_event(struct ftrace_event_call *call,
1555 struct trace_array *tr)
1557 struct ftrace_event_file *file;
1559 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1563 file->event_call = call;
1565 list_add(&file->list, &tr->events);
1570 struct ftrace_module_file_ops;
1571 static void __add_event_to_tracers(struct ftrace_event_call *call,
1572 struct ftrace_module_file_ops *file_ops);
1574 /* Add an additional event_call dynamically */
1575 int trace_add_event_call(struct ftrace_event_call *call)
1578 mutex_lock(&event_mutex);
1580 ret = __register_event(call, NULL);
1582 __add_event_to_tracers(call, NULL);
1584 mutex_unlock(&event_mutex);
1589 * Must be called under locking both of event_mutex and trace_event_sem.
1591 static void __trace_remove_event_call(struct ftrace_event_call *call)
1594 trace_destroy_fields(call);
1595 destroy_preds(call);
1598 /* Remove an event_call */
1599 void trace_remove_event_call(struct ftrace_event_call *call)
1601 mutex_lock(&event_mutex);
1602 down_write(&trace_event_sem);
1603 __trace_remove_event_call(call);
1604 up_write(&trace_event_sem);
1605 mutex_unlock(&event_mutex);
1608 #define for_each_event(event, start, end) \
1609 for (event = start; \
1610 (unsigned long)event < (unsigned long)end; \
1613 #ifdef CONFIG_MODULES
1615 static LIST_HEAD(ftrace_module_file_list);
1618 * Modules must own their file_operations to keep up with
1619 * reference counting.
1621 struct ftrace_module_file_ops {
1622 struct list_head list;
1624 struct file_operations id;
1625 struct file_operations enable;
1626 struct file_operations format;
1627 struct file_operations filter;
1630 static struct ftrace_module_file_ops *
1631 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1634 * As event_calls are added in groups by module,
1635 * when we find one file_ops, we don't need to search for
1636 * each call in that module, as the rest should be the
1637 * same. Only search for a new one if the last one did
1640 if (file_ops && mod == file_ops->mod)
1643 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1644 if (file_ops->mod == mod)
1650 static struct ftrace_module_file_ops *
1651 trace_create_file_ops(struct module *mod)
1653 struct ftrace_module_file_ops *file_ops;
1656 * This is a bit of a PITA. To allow for correct reference
1657 * counting, modules must "own" their file_operations.
1658 * To do this, we allocate the file operations that will be
1659 * used in the event directory.
1662 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1666 file_ops->mod = mod;
1668 file_ops->id = ftrace_event_id_fops;
1669 file_ops->id.owner = mod;
1671 file_ops->enable = ftrace_enable_fops;
1672 file_ops->enable.owner = mod;
1674 file_ops->filter = ftrace_event_filter_fops;
1675 file_ops->filter.owner = mod;
1677 file_ops->format = ftrace_event_format_fops;
1678 file_ops->format.owner = mod;
1680 list_add(&file_ops->list, &ftrace_module_file_list);
1685 static void trace_module_add_events(struct module *mod)
1687 struct ftrace_module_file_ops *file_ops = NULL;
1688 struct ftrace_event_call **call, **start, **end;
1690 start = mod->trace_events;
1691 end = mod->trace_events + mod->num_trace_events;
1696 file_ops = trace_create_file_ops(mod);
1700 for_each_event(call, start, end) {
1701 __register_event(*call, mod);
1702 __add_event_to_tracers(*call, file_ops);
1706 static void trace_module_remove_events(struct module *mod)
1708 struct ftrace_module_file_ops *file_ops;
1709 struct ftrace_event_call *call, *p;
1710 bool clear_trace = false;
1712 down_write(&trace_event_sem);
1713 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1714 if (call->mod == mod) {
1715 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1717 __trace_remove_event_call(call);
1721 /* Now free the file_operations */
1722 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1723 if (file_ops->mod == mod)
1726 if (&file_ops->list != &ftrace_module_file_list) {
1727 list_del(&file_ops->list);
1730 up_write(&trace_event_sem);
1733 * It is safest to reset the ring buffer if the module being unloaded
1734 * registered any events that were used. The only worry is if
1735 * a new module gets loaded, and takes on the same id as the events
1736 * of this module. When printing out the buffer, traced events left
1737 * over from this module may be passed to the new module events and
1738 * unexpected results may occur.
1741 tracing_reset_all_online_cpus();
1744 static int trace_module_notify(struct notifier_block *self,
1745 unsigned long val, void *data)
1747 struct module *mod = data;
1749 mutex_lock(&event_mutex);
1751 case MODULE_STATE_COMING:
1752 trace_module_add_events(mod);
1754 case MODULE_STATE_GOING:
1755 trace_module_remove_events(mod);
1758 mutex_unlock(&event_mutex);
1764 __trace_add_new_mod_event(struct ftrace_event_call *call,
1765 struct trace_array *tr,
1766 struct ftrace_module_file_ops *file_ops)
1768 return __trace_add_new_event(call, tr,
1769 &file_ops->id, &file_ops->enable,
1770 &file_ops->filter, &file_ops->format);
1774 static inline struct ftrace_module_file_ops *
1775 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1779 static inline int trace_module_notify(struct notifier_block *self,
1780 unsigned long val, void *data)
1785 __trace_add_new_mod_event(struct ftrace_event_call *call,
1786 struct trace_array *tr,
1787 struct ftrace_module_file_ops *file_ops)
1791 #endif /* CONFIG_MODULES */
1793 /* Create a new event directory structure for a trace directory. */
1795 __trace_add_event_dirs(struct trace_array *tr)
1797 struct ftrace_module_file_ops *file_ops = NULL;
1798 struct ftrace_event_call *call;
1801 list_for_each_entry(call, &ftrace_events, list) {
1804 * Directories for events by modules need to
1805 * keep module ref counts when opened (as we don't
1806 * want the module to disappear when reading one
1807 * of these files). The file_ops keep account of
1808 * the module ref count.
1810 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1812 continue; /* Warn? */
1813 ret = __trace_add_new_mod_event(call, tr, file_ops);
1815 pr_warning("Could not create directory for event %s\n",
1819 ret = __trace_add_new_event(call, tr,
1820 &ftrace_event_id_fops,
1821 &ftrace_enable_fops,
1822 &ftrace_event_filter_fops,
1823 &ftrace_event_format_fops);
1825 pr_warning("Could not create directory for event %s\n",
1830 #ifdef CONFIG_DYNAMIC_FTRACE
1833 #define ENABLE_EVENT_STR "enable_event"
1834 #define DISABLE_EVENT_STR "disable_event"
1836 struct event_probe_data {
1837 struct ftrace_event_file *file;
1838 unsigned long count;
1843 static struct ftrace_event_file *
1844 find_event_file(struct trace_array *tr, const char *system, const char *event)
1846 struct ftrace_event_file *file;
1847 struct ftrace_event_call *call;
1849 list_for_each_entry(file, &tr->events, list) {
1851 call = file->event_call;
1853 if (!call->name || !call->class || !call->class->reg)
1856 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1859 if (strcmp(event, call->name) == 0 &&
1860 strcmp(system, call->class->system) == 0)
1867 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1869 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1870 struct event_probe_data *data = *pdata;
1876 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1878 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1882 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1884 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1885 struct event_probe_data *data = *pdata;
1893 /* Skip if the event is in a state we want to switch to */
1894 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1897 if (data->count != -1)
1900 event_enable_probe(ip, parent_ip, _data);
1904 event_enable_print(struct seq_file *m, unsigned long ip,
1905 struct ftrace_probe_ops *ops, void *_data)
1907 struct event_probe_data *data = _data;
1909 seq_printf(m, "%ps:", (void *)ip);
1911 seq_printf(m, "%s:%s:%s",
1912 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1913 data->file->event_call->class->system,
1914 data->file->event_call->name);
1916 if (data->count == -1)
1917 seq_printf(m, ":unlimited\n");
1919 seq_printf(m, ":count=%ld\n", data->count);
1925 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1928 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1929 struct event_probe_data *data = *pdata;
1936 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1939 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1940 struct event_probe_data *data = *pdata;
1942 if (WARN_ON_ONCE(data->ref <= 0))
1947 /* Remove the SOFT_MODE flag */
1948 __ftrace_event_enable_disable(data->file, 0, 1);
1949 module_put(data->file->event_call->mod);
1955 static struct ftrace_probe_ops event_enable_probe_ops = {
1956 .func = event_enable_probe,
1957 .print = event_enable_print,
1958 .init = event_enable_init,
1959 .free = event_enable_free,
1962 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1963 .func = event_enable_count_probe,
1964 .print = event_enable_print,
1965 .init = event_enable_init,
1966 .free = event_enable_free,
1969 static struct ftrace_probe_ops event_disable_probe_ops = {
1970 .func = event_enable_probe,
1971 .print = event_enable_print,
1972 .init = event_enable_init,
1973 .free = event_enable_free,
1976 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1977 .func = event_enable_count_probe,
1978 .print = event_enable_print,
1979 .init = event_enable_init,
1980 .free = event_enable_free,
1984 event_enable_func(struct ftrace_hash *hash,
1985 char *glob, char *cmd, char *param, int enabled)
1987 struct trace_array *tr = top_trace_array();
1988 struct ftrace_event_file *file;
1989 struct ftrace_probe_ops *ops;
1990 struct event_probe_data *data;
1997 /* hash funcs only work with set_ftrace_filter */
2004 system = strsep(¶m, ":");
2008 event = strsep(¶m, ":");
2010 mutex_lock(&event_mutex);
2013 file = find_event_file(tr, system, event);
2017 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2020 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2022 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2024 if (glob[0] == '!') {
2025 unregister_ftrace_function_probe_func(glob+1, ops);
2031 data = kzalloc(sizeof(*data), GFP_KERNEL);
2035 data->enable = enable;
2042 number = strsep(¶m, ":");
2045 if (!strlen(number))
2049 * We use the callback data field (which is a pointer)
2052 ret = kstrtoul(number, 0, &data->count);
2057 /* Don't let event modules unload while probe registered */
2058 ret = try_module_get(file->event_call->mod);
2062 ret = __ftrace_event_enable_disable(file, 1, 1);
2065 ret = register_ftrace_function_probe(glob, ops, data);
2067 * The above returns on success the # of functions enabled,
2068 * but if it didn't find any functions it returns zero.
2069 * Consider no functions a failure too.
2076 /* Just return zero, not the number of enabled functions */
2079 mutex_unlock(&event_mutex);
2083 __ftrace_event_enable_disable(file, 0, 1);
2085 module_put(file->event_call->mod);
2091 static struct ftrace_func_command event_enable_cmd = {
2092 .name = ENABLE_EVENT_STR,
2093 .func = event_enable_func,
2096 static struct ftrace_func_command event_disable_cmd = {
2097 .name = DISABLE_EVENT_STR,
2098 .func = event_enable_func,
2101 static __init int register_event_cmds(void)
2105 ret = register_ftrace_command(&event_enable_cmd);
2106 if (WARN_ON(ret < 0))
2108 ret = register_ftrace_command(&event_disable_cmd);
2109 if (WARN_ON(ret < 0))
2110 unregister_ftrace_command(&event_enable_cmd);
2114 static inline int register_event_cmds(void) { return 0; }
2115 #endif /* CONFIG_DYNAMIC_FTRACE */
2118 * The top level array has already had its ftrace_event_file
2119 * descriptors created in order to allow for early events to
2120 * be recorded. This function is called after the debugfs has been
2121 * initialized, and we now have to create the files associated
2125 __trace_early_add_event_dirs(struct trace_array *tr)
2127 struct ftrace_event_file *file;
2131 list_for_each_entry(file, &tr->events, list) {
2132 ret = event_create_dir(tr->event_dir, file,
2133 &ftrace_event_id_fops,
2134 &ftrace_enable_fops,
2135 &ftrace_event_filter_fops,
2136 &ftrace_event_format_fops);
2138 pr_warning("Could not create directory for event %s\n",
2139 file->event_call->name);
2144 * For early boot up, the top trace array requires to have
2145 * a list of events that can be enabled. This must be done before
2146 * the filesystem is set up in order to allow events to be traced
2150 __trace_early_add_events(struct trace_array *tr)
2152 struct ftrace_event_call *call;
2155 list_for_each_entry(call, &ftrace_events, list) {
2156 /* Early boot up should not have any modules loaded */
2157 if (WARN_ON_ONCE(call->mod))
2160 ret = __trace_early_add_new_event(call, tr);
2162 pr_warning("Could not create early event %s\n",
2167 /* Remove the event directory structure for a trace directory. */
2169 __trace_remove_event_dirs(struct trace_array *tr)
2171 struct ftrace_event_file *file, *next;
2173 list_for_each_entry_safe(file, next, &tr->events, list) {
2174 list_del(&file->list);
2175 debugfs_remove_recursive(file->dir);
2176 remove_subsystem(file->system);
2177 kmem_cache_free(file_cachep, file);
2182 __add_event_to_tracers(struct ftrace_event_call *call,
2183 struct ftrace_module_file_ops *file_ops)
2185 struct trace_array *tr;
2187 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2189 __trace_add_new_mod_event(call, tr, file_ops);
2191 __trace_add_new_event(call, tr,
2192 &ftrace_event_id_fops,
2193 &ftrace_enable_fops,
2194 &ftrace_event_filter_fops,
2195 &ftrace_event_format_fops);
2199 static struct notifier_block trace_module_nb = {
2200 .notifier_call = trace_module_notify,
2204 extern struct ftrace_event_call *__start_ftrace_events[];
2205 extern struct ftrace_event_call *__stop_ftrace_events[];
2207 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2209 static __init int setup_trace_event(char *str)
2211 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2212 ring_buffer_expanded = true;
2213 tracing_selftest_disabled = true;
2217 __setup("trace_event=", setup_trace_event);
2219 /* Expects to have event_mutex held when called */
2221 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2223 struct dentry *d_events;
2224 struct dentry *entry;
2226 entry = debugfs_create_file("set_event", 0644, parent,
2227 tr, &ftrace_set_event_fops);
2229 pr_warning("Could not create debugfs 'set_event' entry\n");
2233 d_events = debugfs_create_dir("events", parent);
2235 pr_warning("Could not create debugfs 'events' directory\n");
2239 /* ring buffer internal formats */
2240 trace_create_file("header_page", 0444, d_events,
2241 ring_buffer_print_page_header,
2242 &ftrace_show_header_fops);
2244 trace_create_file("header_event", 0444, d_events,
2245 ring_buffer_print_entry_header,
2246 &ftrace_show_header_fops);
2248 trace_create_file("enable", 0644, d_events,
2249 tr, &ftrace_tr_enable_fops);
2251 tr->event_dir = d_events;
2257 * event_trace_add_tracer - add a instance of a trace_array to events
2258 * @parent: The parent dentry to place the files/directories for events in
2259 * @tr: The trace array associated with these events
2261 * When a new instance is created, it needs to set up its events
2262 * directory, as well as other files associated with events. It also
2263 * creates the event hierachry in the @parent/events directory.
2265 * Returns 0 on success.
2267 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2271 mutex_lock(&event_mutex);
2273 ret = create_event_toplevel_files(parent, tr);
2277 down_write(&trace_event_sem);
2278 __trace_add_event_dirs(tr);
2279 up_write(&trace_event_sem);
2282 mutex_unlock(&event_mutex);
2288 * The top trace array already had its file descriptors created.
2289 * Now the files themselves need to be created.
2292 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2296 mutex_lock(&event_mutex);
2298 ret = create_event_toplevel_files(parent, tr);
2302 down_write(&trace_event_sem);
2303 __trace_early_add_event_dirs(tr);
2304 up_write(&trace_event_sem);
2307 mutex_unlock(&event_mutex);
2312 int event_trace_del_tracer(struct trace_array *tr)
2314 /* Disable any running events */
2315 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2317 mutex_lock(&event_mutex);
2319 down_write(&trace_event_sem);
2320 __trace_remove_event_dirs(tr);
2321 debugfs_remove_recursive(tr->event_dir);
2322 up_write(&trace_event_sem);
2324 tr->event_dir = NULL;
2326 mutex_unlock(&event_mutex);
2331 static __init int event_trace_memsetup(void)
2333 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2334 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2338 static __init int event_trace_enable(void)
2340 struct trace_array *tr = top_trace_array();
2341 struct ftrace_event_call **iter, *call;
2342 char *buf = bootup_event_buf;
2346 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2349 ret = event_init(call);
2351 list_add(&call->list, &ftrace_events);
2355 * We need the top trace array to have a working set of trace
2356 * points at early init, before the debug files and directories
2357 * are created. Create the file entries now, and attach them
2358 * to the actual file dentries later.
2360 __trace_early_add_events(tr);
2363 token = strsep(&buf, ",");
2370 ret = ftrace_set_clr_event(tr, token, 1);
2372 pr_warn("Failed to enable trace event: %s\n", token);
2375 trace_printk_start_comm();
2377 register_event_cmds();
2382 static __init int event_trace_init(void)
2384 struct trace_array *tr;
2385 struct dentry *d_tracer;
2386 struct dentry *entry;
2389 tr = top_trace_array();
2391 d_tracer = tracing_init_dentry();
2395 entry = debugfs_create_file("available_events", 0444, d_tracer,
2396 tr, &ftrace_avail_fops);
2398 pr_warning("Could not create debugfs "
2399 "'available_events' entry\n");
2401 if (trace_define_common_fields())
2402 pr_warning("tracing: Failed to allocate common fields");
2404 ret = early_event_add_tracer(d_tracer, tr);
2408 ret = register_module_notifier(&trace_module_nb);
2410 pr_warning("Failed to register trace events module notifier\n");
2414 early_initcall(event_trace_memsetup);
2415 core_initcall(event_trace_enable);
2416 fs_initcall(event_trace_init);
2418 #ifdef CONFIG_FTRACE_STARTUP_TEST
2420 static DEFINE_SPINLOCK(test_spinlock);
2421 static DEFINE_SPINLOCK(test_spinlock_irq);
2422 static DEFINE_MUTEX(test_mutex);
2424 static __init void test_work(struct work_struct *dummy)
2426 spin_lock(&test_spinlock);
2427 spin_lock_irq(&test_spinlock_irq);
2429 spin_unlock_irq(&test_spinlock_irq);
2430 spin_unlock(&test_spinlock);
2432 mutex_lock(&test_mutex);
2434 mutex_unlock(&test_mutex);
2437 static __init int event_test_thread(void *unused)
2441 test_malloc = kmalloc(1234, GFP_KERNEL);
2443 pr_info("failed to kmalloc\n");
2445 schedule_on_each_cpu(test_work);
2449 set_current_state(TASK_INTERRUPTIBLE);
2450 while (!kthread_should_stop())
2457 * Do various things that may trigger events.
2459 static __init void event_test_stuff(void)
2461 struct task_struct *test_thread;
2463 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2465 kthread_stop(test_thread);
2469 * For every trace event defined, we will test each trace point separately,
2470 * and then by groups, and finally all trace points.
2472 static __init void event_trace_self_tests(void)
2474 struct ftrace_subsystem_dir *dir;
2475 struct ftrace_event_file *file;
2476 struct ftrace_event_call *call;
2477 struct event_subsystem *system;
2478 struct trace_array *tr;
2481 tr = top_trace_array();
2483 pr_info("Running tests on trace events:\n");
2485 list_for_each_entry(file, &tr->events, list) {
2487 call = file->event_call;
2489 /* Only test those that have a probe */
2490 if (!call->class || !call->class->probe)
2494 * Testing syscall events here is pretty useless, but
2495 * we still do it if configured. But this is time consuming.
2496 * What we really need is a user thread to perform the
2497 * syscalls as we test.
2499 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2500 if (call->class->system &&
2501 strcmp(call->class->system, "syscalls") == 0)
2505 pr_info("Testing event %s: ", call->name);
2508 * If an event is already enabled, someone is using
2509 * it and the self test should not be on.
2511 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2512 pr_warning("Enabled event during self test!\n");
2517 ftrace_event_enable_disable(file, 1);
2519 ftrace_event_enable_disable(file, 0);
2524 /* Now test at the sub system level */
2526 pr_info("Running tests on trace event systems:\n");
2528 list_for_each_entry(dir, &tr->systems, list) {
2530 system = dir->subsystem;
2532 /* the ftrace system is special, skip it */
2533 if (strcmp(system->name, "ftrace") == 0)
2536 pr_info("Testing event system %s: ", system->name);
2538 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2539 if (WARN_ON_ONCE(ret)) {
2540 pr_warning("error enabling system %s\n",
2547 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2548 if (WARN_ON_ONCE(ret)) {
2549 pr_warning("error disabling system %s\n",
2557 /* Test with all events enabled */
2559 pr_info("Running tests on all trace events:\n");
2560 pr_info("Testing all events: ");
2562 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2563 if (WARN_ON_ONCE(ret)) {
2564 pr_warning("error enabling all events\n");
2571 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2572 if (WARN_ON_ONCE(ret)) {
2573 pr_warning("error disabling all events\n");
2580 #ifdef CONFIG_FUNCTION_TRACER
2582 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2585 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2586 struct ftrace_ops *op, struct pt_regs *pt_regs)
2588 struct ring_buffer_event *event;
2589 struct ring_buffer *buffer;
2590 struct ftrace_entry *entry;
2591 unsigned long flags;
2596 pc = preempt_count();
2597 preempt_disable_notrace();
2598 cpu = raw_smp_processor_id();
2599 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2604 local_save_flags(flags);
2606 event = trace_current_buffer_lock_reserve(&buffer,
2607 TRACE_FN, sizeof(*entry),
2611 entry = ring_buffer_event_data(event);
2613 entry->parent_ip = parent_ip;
2615 trace_buffer_unlock_commit(buffer, event, flags, pc);
2618 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2619 preempt_enable_notrace();
2622 static struct ftrace_ops trace_ops __initdata =
2624 .func = function_test_events_call,
2625 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2628 static __init void event_trace_self_test_with_function(void)
2631 ret = register_ftrace_function(&trace_ops);
2632 if (WARN_ON(ret < 0)) {
2633 pr_info("Failed to enable function tracer for event tests\n");
2636 pr_info("Running tests again, along with the function tracer\n");
2637 event_trace_self_tests();
2638 unregister_ftrace_function(&trace_ops);
2641 static __init void event_trace_self_test_with_function(void)
2646 static __init int event_trace_self_tests_init(void)
2648 if (!tracing_selftest_disabled) {
2649 event_trace_self_tests();
2650 event_trace_self_test_with_function();
2656 late_initcall(event_trace_self_tests_init);