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 LIST_HEAD(ftrace_common_fields);
39 /* Double loops, do not use break, only goto's work */
40 #define do_for_each_event_file(tr, file) \
41 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
42 list_for_each_entry(file, &tr->events, list)
44 #define do_for_each_event_file_safe(tr, file) \
45 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
46 struct ftrace_event_file *___n; \
47 list_for_each_entry_safe(file, ___n, &tr->events, list)
49 #define while_for_each_event_file() \
53 trace_get_fields(struct ftrace_event_call *event_call)
55 if (!event_call->class->get_fields)
56 return &event_call->class->fields;
57 return event_call->class->get_fields(event_call);
60 static int __trace_define_field(struct list_head *head, const char *type,
61 const char *name, int offset, int size,
62 int is_signed, int filter_type)
64 struct ftrace_event_field *field;
66 field = kzalloc(sizeof(*field), GFP_KERNEL);
70 field->name = kstrdup(name, GFP_KERNEL);
74 field->type = kstrdup(type, GFP_KERNEL);
78 if (filter_type == FILTER_OTHER)
79 field->filter_type = filter_assign_type(type);
81 field->filter_type = filter_type;
83 field->offset = offset;
85 field->is_signed = is_signed;
87 list_add(&field->link, head);
99 int trace_define_field(struct ftrace_event_call *call, const char *type,
100 const char *name, int offset, int size, int is_signed,
103 struct list_head *head;
105 if (WARN_ON(!call->class))
108 head = trace_get_fields(call);
109 return __trace_define_field(head, type, name, offset, size,
110 is_signed, filter_type);
112 EXPORT_SYMBOL_GPL(trace_define_field);
114 #define __common_field(type, item) \
115 ret = __trace_define_field(&ftrace_common_fields, #type, \
117 offsetof(typeof(ent), item), \
119 is_signed_type(type), FILTER_OTHER); \
123 static int trace_define_common_fields(void)
126 struct trace_entry ent;
128 __common_field(unsigned short, type);
129 __common_field(unsigned char, flags);
130 __common_field(unsigned char, preempt_count);
131 __common_field(int, pid);
136 void trace_destroy_fields(struct ftrace_event_call *call)
138 struct ftrace_event_field *field, *next;
139 struct list_head *head;
141 head = trace_get_fields(call);
142 list_for_each_entry_safe(field, next, head, link) {
143 list_del(&field->link);
150 int trace_event_raw_init(struct ftrace_event_call *call)
154 id = register_ftrace_event(&call->event);
160 EXPORT_SYMBOL_GPL(trace_event_raw_init);
162 int ftrace_event_reg(struct ftrace_event_call *call,
163 enum trace_reg type, void *data)
165 struct ftrace_event_file *file = data;
168 case TRACE_REG_REGISTER:
169 return tracepoint_probe_register(call->name,
172 case TRACE_REG_UNREGISTER:
173 tracepoint_probe_unregister(call->name,
178 #ifdef CONFIG_PERF_EVENTS
179 case TRACE_REG_PERF_REGISTER:
180 return tracepoint_probe_register(call->name,
181 call->class->perf_probe,
183 case TRACE_REG_PERF_UNREGISTER:
184 tracepoint_probe_unregister(call->name,
185 call->class->perf_probe,
188 case TRACE_REG_PERF_OPEN:
189 case TRACE_REG_PERF_CLOSE:
190 case TRACE_REG_PERF_ADD:
191 case TRACE_REG_PERF_DEL:
197 EXPORT_SYMBOL_GPL(ftrace_event_reg);
199 void trace_event_enable_cmd_record(bool enable)
201 struct ftrace_event_file *file;
202 struct trace_array *tr;
204 mutex_lock(&event_mutex);
205 do_for_each_event_file(tr, file) {
207 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
211 tracing_start_cmdline_record();
212 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
214 tracing_stop_cmdline_record();
215 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
217 } while_for_each_event_file();
218 mutex_unlock(&event_mutex);
221 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
224 struct ftrace_event_call *call = file->event_call;
229 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
230 file->flags &= ~FTRACE_EVENT_FL_ENABLED;
231 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
232 tracing_stop_cmdline_record();
233 file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
235 call->class->reg(call, TRACE_REG_UNREGISTER, file);
239 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
240 if (trace_flags & TRACE_ITER_RECORD_CMD) {
241 tracing_start_cmdline_record();
242 file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
244 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
246 tracing_stop_cmdline_record();
247 pr_info("event trace: Could not enable event "
251 file->flags |= FTRACE_EVENT_FL_ENABLED;
259 static void ftrace_clear_events(struct trace_array *tr)
261 struct ftrace_event_file *file;
263 mutex_lock(&event_mutex);
264 list_for_each_entry(file, &tr->events, list) {
265 ftrace_event_enable_disable(file, 0);
267 mutex_unlock(&event_mutex);
270 static void __put_system(struct event_subsystem *system)
272 struct event_filter *filter = system->filter;
274 WARN_ON_ONCE(system->ref_count == 0);
275 if (--system->ref_count)
278 list_del(&system->list);
281 kfree(filter->filter_string);
288 static void __get_system(struct event_subsystem *system)
290 WARN_ON_ONCE(system->ref_count == 0);
294 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
296 WARN_ON_ONCE(dir->ref_count == 0);
298 __get_system(dir->subsystem);
301 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
303 WARN_ON_ONCE(dir->ref_count == 0);
304 /* If the subsystem is about to be freed, the dir must be too */
305 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
307 __put_system(dir->subsystem);
308 if (!--dir->ref_count)
312 static void put_system(struct ftrace_subsystem_dir *dir)
314 mutex_lock(&event_mutex);
315 __put_system_dir(dir);
316 mutex_unlock(&event_mutex);
320 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
322 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
323 const char *sub, const char *event, int set)
325 struct ftrace_event_file *file;
326 struct ftrace_event_call *call;
329 mutex_lock(&event_mutex);
330 list_for_each_entry(file, &tr->events, list) {
332 call = file->event_call;
334 if (!call->name || !call->class || !call->class->reg)
337 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
341 strcmp(match, call->name) != 0 &&
342 strcmp(match, call->class->system) != 0)
345 if (sub && strcmp(sub, call->class->system) != 0)
348 if (event && strcmp(event, call->name) != 0)
351 ftrace_event_enable_disable(file, set);
355 mutex_unlock(&event_mutex);
360 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
362 char *event = NULL, *sub = NULL, *match;
365 * The buf format can be <subsystem>:<event-name>
366 * *:<event-name> means any event by that name.
367 * :<event-name> is the same.
369 * <subsystem>:* means all events in that subsystem
370 * <subsystem>: means the same.
372 * <name> (no ':') means all events in a subsystem with
373 * the name <name> or any event that matches <name>
376 match = strsep(&buf, ":");
382 if (!strlen(sub) || strcmp(sub, "*") == 0)
384 if (!strlen(event) || strcmp(event, "*") == 0)
388 return __ftrace_set_clr_event(tr, match, sub, event, set);
392 * trace_set_clr_event - enable or disable an event
393 * @system: system name to match (NULL for any system)
394 * @event: event name to match (NULL for all events, within system)
395 * @set: 1 to enable, 0 to disable
397 * This is a way for other parts of the kernel to enable or disable
400 * Returns 0 on success, -EINVAL if the parameters do not match any
403 int trace_set_clr_event(const char *system, const char *event, int set)
405 struct trace_array *tr = top_trace_array();
407 return __ftrace_set_clr_event(tr, NULL, system, event, set);
409 EXPORT_SYMBOL_GPL(trace_set_clr_event);
411 /* 128 should be much more than enough */
412 #define EVENT_BUF_SIZE 127
415 ftrace_event_write(struct file *file, const char __user *ubuf,
416 size_t cnt, loff_t *ppos)
418 struct trace_parser parser;
419 struct seq_file *m = file->private_data;
420 struct trace_array *tr = m->private;
426 ret = tracing_update_buffers();
430 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
433 read = trace_get_user(&parser, ubuf, cnt, ppos);
435 if (read >= 0 && trace_parser_loaded((&parser))) {
438 if (*parser.buffer == '!')
441 parser.buffer[parser.idx] = 0;
443 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
451 trace_parser_put(&parser);
457 t_next(struct seq_file *m, void *v, loff_t *pos)
459 struct ftrace_event_file *file = v;
460 struct ftrace_event_call *call;
461 struct trace_array *tr = m->private;
465 list_for_each_entry_continue(file, &tr->events, list) {
466 call = file->event_call;
468 * The ftrace subsystem is for showing formats only.
469 * They can not be enabled or disabled via the event files.
471 if (call->class && call->class->reg)
478 static void *t_start(struct seq_file *m, loff_t *pos)
480 struct ftrace_event_file *file;
481 struct trace_array *tr = m->private;
484 mutex_lock(&event_mutex);
486 file = list_entry(&tr->events, struct ftrace_event_file, list);
487 for (l = 0; l <= *pos; ) {
488 file = t_next(m, file, &l);
496 s_next(struct seq_file *m, void *v, loff_t *pos)
498 struct ftrace_event_file *file = v;
499 struct trace_array *tr = m->private;
503 list_for_each_entry_continue(file, &tr->events, list) {
504 if (file->flags & FTRACE_EVENT_FL_ENABLED)
511 static void *s_start(struct seq_file *m, loff_t *pos)
513 struct ftrace_event_file *file;
514 struct trace_array *tr = m->private;
517 mutex_lock(&event_mutex);
519 file = list_entry(&tr->events, struct ftrace_event_file, list);
520 for (l = 0; l <= *pos; ) {
521 file = s_next(m, file, &l);
528 static int t_show(struct seq_file *m, void *v)
530 struct ftrace_event_file *file = v;
531 struct ftrace_event_call *call = file->event_call;
533 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
534 seq_printf(m, "%s:", call->class->system);
535 seq_printf(m, "%s\n", call->name);
540 static void t_stop(struct seq_file *m, void *p)
542 mutex_unlock(&event_mutex);
546 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
549 struct ftrace_event_file *file = filp->private_data;
552 if (file->flags & FTRACE_EVENT_FL_ENABLED)
557 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
561 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
564 struct ftrace_event_file *file = filp->private_data;
571 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
575 ret = tracing_update_buffers();
582 mutex_lock(&event_mutex);
583 ret = ftrace_event_enable_disable(file, val);
584 mutex_unlock(&event_mutex);
593 return ret ? ret : cnt;
597 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
600 const char set_to_char[4] = { '?', '0', '1', 'X' };
601 struct ftrace_subsystem_dir *dir = filp->private_data;
602 struct event_subsystem *system = dir->subsystem;
603 struct ftrace_event_call *call;
604 struct ftrace_event_file *file;
605 struct trace_array *tr = dir->tr;
610 mutex_lock(&event_mutex);
611 list_for_each_entry(file, &tr->events, list) {
612 call = file->event_call;
613 if (!call->name || !call->class || !call->class->reg)
616 if (system && strcmp(call->class->system, system->name) != 0)
620 * We need to find out if all the events are set
621 * or if all events or cleared, or if we have
624 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
627 * If we have a mixture, no need to look further.
632 mutex_unlock(&event_mutex);
634 buf[0] = set_to_char[set];
637 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
643 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
646 struct ftrace_subsystem_dir *dir = filp->private_data;
647 struct event_subsystem *system = dir->subsystem;
648 const char *name = NULL;
652 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
656 ret = tracing_update_buffers();
660 if (val != 0 && val != 1)
664 * Opening of "enable" adds a ref count to system,
665 * so the name is safe to use.
670 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
684 FORMAT_FIELD_SEPERATOR = 2,
688 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
690 struct ftrace_event_call *call = m->private;
691 struct ftrace_event_field *field;
692 struct list_head *common_head = &ftrace_common_fields;
693 struct list_head *head = trace_get_fields(call);
697 switch ((unsigned long)v) {
699 if (unlikely(list_empty(common_head)))
702 field = list_entry(common_head->prev,
703 struct ftrace_event_field, link);
706 case FORMAT_FIELD_SEPERATOR:
707 if (unlikely(list_empty(head)))
710 field = list_entry(head->prev, struct ftrace_event_field, link);
713 case FORMAT_PRINTFMT:
719 if (field->link.prev == common_head)
720 return (void *)FORMAT_FIELD_SEPERATOR;
721 else if (field->link.prev == head)
722 return (void *)FORMAT_PRINTFMT;
724 field = list_entry(field->link.prev, struct ftrace_event_field, link);
729 static void *f_start(struct seq_file *m, loff_t *pos)
734 /* Start by showing the header */
736 return (void *)FORMAT_HEADER;
738 p = (void *)FORMAT_HEADER;
740 p = f_next(m, p, &l);
741 } while (p && l < *pos);
746 static int f_show(struct seq_file *m, void *v)
748 struct ftrace_event_call *call = m->private;
749 struct ftrace_event_field *field;
750 const char *array_descriptor;
752 switch ((unsigned long)v) {
754 seq_printf(m, "name: %s\n", call->name);
755 seq_printf(m, "ID: %d\n", call->event.type);
756 seq_printf(m, "format:\n");
759 case FORMAT_FIELD_SEPERATOR:
763 case FORMAT_PRINTFMT:
764 seq_printf(m, "\nprint fmt: %s\n",
772 * Smartly shows the array type(except dynamic array).
775 * If TYPE := TYPE[LEN], it is shown:
776 * field:TYPE VAR[LEN]
778 array_descriptor = strchr(field->type, '[');
780 if (!strncmp(field->type, "__data_loc", 10))
781 array_descriptor = NULL;
783 if (!array_descriptor)
784 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
785 field->type, field->name, field->offset,
786 field->size, !!field->is_signed);
788 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
789 (int)(array_descriptor - field->type),
790 field->type, field->name,
791 array_descriptor, field->offset,
792 field->size, !!field->is_signed);
797 static void f_stop(struct seq_file *m, void *p)
801 static const struct seq_operations trace_format_seq_ops = {
808 static int trace_format_open(struct inode *inode, struct file *file)
810 struct ftrace_event_call *call = inode->i_private;
814 ret = seq_open(file, &trace_format_seq_ops);
818 m = file->private_data;
825 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
827 struct ftrace_event_call *call = filp->private_data;
834 s = kmalloc(sizeof(*s), GFP_KERNEL);
839 trace_seq_printf(s, "%d\n", call->event.type);
841 r = simple_read_from_buffer(ubuf, cnt, ppos,
848 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
851 struct ftrace_event_call *call = filp->private_data;
858 s = kmalloc(sizeof(*s), GFP_KERNEL);
864 print_event_filter(call, s);
865 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
873 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
876 struct ftrace_event_call *call = filp->private_data;
880 if (cnt >= PAGE_SIZE)
883 buf = (char *)__get_free_page(GFP_TEMPORARY);
887 if (copy_from_user(buf, ubuf, cnt)) {
888 free_page((unsigned long) buf);
893 err = apply_event_filter(call, buf);
894 free_page((unsigned long) buf);
903 static LIST_HEAD(event_subsystems);
905 static int subsystem_open(struct inode *inode, struct file *filp)
907 struct event_subsystem *system = NULL;
908 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
909 struct trace_array *tr;
912 /* Make sure the system still exists */
913 mutex_lock(&event_mutex);
914 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
915 list_for_each_entry(dir, &tr->systems, list) {
916 if (dir == inode->i_private) {
917 /* Don't open systems with no events */
918 if (dir->nr_events) {
919 __get_system_dir(dir);
920 system = dir->subsystem;
927 mutex_unlock(&event_mutex);
932 /* Some versions of gcc think dir can be uninitialized here */
935 ret = tracing_open_generic(inode, filp);
942 static int system_tr_open(struct inode *inode, struct file *filp)
944 struct ftrace_subsystem_dir *dir;
945 struct trace_array *tr = inode->i_private;
948 /* Make a temporary dir that has no system but points to tr */
949 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
955 ret = tracing_open_generic(inode, filp);
959 filp->private_data = dir;
964 static int subsystem_release(struct inode *inode, struct file *file)
966 struct ftrace_subsystem_dir *dir = file->private_data;
969 * If dir->subsystem is NULL, then this is a temporary
970 * descriptor that was made for a trace_array to enable
982 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
985 struct ftrace_subsystem_dir *dir = filp->private_data;
986 struct event_subsystem *system = dir->subsystem;
993 s = kmalloc(sizeof(*s), GFP_KERNEL);
999 print_subsystem_event_filter(system, s);
1000 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1008 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1011 struct ftrace_subsystem_dir *dir = filp->private_data;
1015 if (cnt >= PAGE_SIZE)
1018 buf = (char *)__get_free_page(GFP_TEMPORARY);
1022 if (copy_from_user(buf, ubuf, cnt)) {
1023 free_page((unsigned long) buf);
1028 err = apply_subsystem_event_filter(dir, buf);
1029 free_page((unsigned long) buf);
1039 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1041 int (*func)(struct trace_seq *s) = filp->private_data;
1042 struct trace_seq *s;
1048 s = kmalloc(sizeof(*s), GFP_KERNEL);
1055 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1062 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1063 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1065 static const struct seq_operations show_event_seq_ops = {
1072 static const struct seq_operations show_set_event_seq_ops = {
1079 static const struct file_operations ftrace_avail_fops = {
1080 .open = ftrace_event_avail_open,
1082 .llseek = seq_lseek,
1083 .release = seq_release,
1086 static const struct file_operations ftrace_set_event_fops = {
1087 .open = ftrace_event_set_open,
1089 .write = ftrace_event_write,
1090 .llseek = seq_lseek,
1091 .release = seq_release,
1094 static const struct file_operations ftrace_enable_fops = {
1095 .open = tracing_open_generic,
1096 .read = event_enable_read,
1097 .write = event_enable_write,
1098 .llseek = default_llseek,
1101 static const struct file_operations ftrace_event_format_fops = {
1102 .open = trace_format_open,
1104 .llseek = seq_lseek,
1105 .release = seq_release,
1108 static const struct file_operations ftrace_event_id_fops = {
1109 .open = tracing_open_generic,
1110 .read = event_id_read,
1111 .llseek = default_llseek,
1114 static const struct file_operations ftrace_event_filter_fops = {
1115 .open = tracing_open_generic,
1116 .read = event_filter_read,
1117 .write = event_filter_write,
1118 .llseek = default_llseek,
1121 static const struct file_operations ftrace_subsystem_filter_fops = {
1122 .open = subsystem_open,
1123 .read = subsystem_filter_read,
1124 .write = subsystem_filter_write,
1125 .llseek = default_llseek,
1126 .release = subsystem_release,
1129 static const struct file_operations ftrace_system_enable_fops = {
1130 .open = subsystem_open,
1131 .read = system_enable_read,
1132 .write = system_enable_write,
1133 .llseek = default_llseek,
1134 .release = subsystem_release,
1137 static const struct file_operations ftrace_tr_enable_fops = {
1138 .open = system_tr_open,
1139 .read = system_enable_read,
1140 .write = system_enable_write,
1141 .llseek = default_llseek,
1142 .release = subsystem_release,
1145 static const struct file_operations ftrace_show_header_fops = {
1146 .open = tracing_open_generic,
1147 .read = show_header,
1148 .llseek = default_llseek,
1152 ftrace_event_open(struct inode *inode, struct file *file,
1153 const struct seq_operations *seq_ops)
1158 ret = seq_open(file, seq_ops);
1161 m = file->private_data;
1162 /* copy tr over to seq ops */
1163 m->private = inode->i_private;
1169 ftrace_event_avail_open(struct inode *inode, struct file *file)
1171 const struct seq_operations *seq_ops = &show_event_seq_ops;
1173 return ftrace_event_open(inode, file, seq_ops);
1177 ftrace_event_set_open(struct inode *inode, struct file *file)
1179 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1180 struct trace_array *tr = inode->i_private;
1182 if ((file->f_mode & FMODE_WRITE) &&
1183 (file->f_flags & O_TRUNC))
1184 ftrace_clear_events(tr);
1186 return ftrace_event_open(inode, file, seq_ops);
1189 static struct event_subsystem *
1190 create_new_subsystem(const char *name)
1192 struct event_subsystem *system;
1194 /* need to create new entry */
1195 system = kmalloc(sizeof(*system), GFP_KERNEL);
1199 system->ref_count = 1;
1200 system->name = kstrdup(name, GFP_KERNEL);
1205 system->filter = NULL;
1207 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1208 if (!system->filter)
1211 list_add(&system->list, &event_subsystems);
1216 kfree(system->name);
1221 static struct dentry *
1222 event_subsystem_dir(struct trace_array *tr, const char *name,
1223 struct ftrace_event_file *file, struct dentry *parent)
1225 struct ftrace_subsystem_dir *dir;
1226 struct event_subsystem *system;
1227 struct dentry *entry;
1229 /* First see if we did not already create this dir */
1230 list_for_each_entry(dir, &tr->systems, list) {
1231 system = dir->subsystem;
1232 if (strcmp(system->name, name) == 0) {
1239 /* Now see if the system itself exists. */
1240 list_for_each_entry(system, &event_subsystems, list) {
1241 if (strcmp(system->name, name) == 0)
1244 /* Reset system variable when not found */
1245 if (&system->list == &event_subsystems)
1248 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1253 system = create_new_subsystem(name);
1257 __get_system(system);
1259 dir->entry = debugfs_create_dir(name, parent);
1261 pr_warning("Failed to create system directory %s\n", name);
1262 __put_system(system);
1269 dir->subsystem = system;
1272 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1273 &ftrace_subsystem_filter_fops);
1275 kfree(system->filter);
1276 system->filter = NULL;
1277 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1280 trace_create_file("enable", 0644, dir->entry, dir,
1281 &ftrace_system_enable_fops);
1283 list_add(&dir->list, &tr->systems);
1290 /* Only print this message if failed on memory allocation */
1291 if (!dir || !system)
1292 pr_warning("No memory to create event subsystem %s\n",
1298 event_create_dir(struct dentry *parent,
1299 struct ftrace_event_file *file,
1300 const struct file_operations *id,
1301 const struct file_operations *enable,
1302 const struct file_operations *filter,
1303 const struct file_operations *format)
1305 struct ftrace_event_call *call = file->event_call;
1306 struct trace_array *tr = file->tr;
1307 struct list_head *head;
1308 struct dentry *d_events;
1312 * If the trace point header did not define TRACE_SYSTEM
1313 * then the system would be called "TRACE_SYSTEM".
1315 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1316 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1322 file->dir = debugfs_create_dir(call->name, d_events);
1324 pr_warning("Could not create debugfs '%s' directory\n",
1329 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1330 trace_create_file("enable", 0644, file->dir, file,
1333 #ifdef CONFIG_PERF_EVENTS
1334 if (call->event.type && call->class->reg)
1335 trace_create_file("id", 0444, file->dir, call,
1340 * Other events may have the same class. Only update
1341 * the fields if they are not already defined.
1343 head = trace_get_fields(call);
1344 if (list_empty(head)) {
1345 ret = call->class->define_fields(call);
1347 pr_warning("Could not initialize trace point"
1348 " events/%s\n", call->name);
1352 trace_create_file("filter", 0644, file->dir, call,
1355 trace_create_file("format", 0444, file->dir, call,
1361 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1366 if (!--dir->nr_events) {
1367 debugfs_remove_recursive(dir->entry);
1368 list_del(&dir->list);
1369 __put_system_dir(dir);
1373 static void remove_event_from_tracers(struct ftrace_event_call *call)
1375 struct ftrace_event_file *file;
1376 struct trace_array *tr;
1378 do_for_each_event_file_safe(tr, file) {
1380 if (file->event_call != call)
1383 list_del(&file->list);
1384 debugfs_remove_recursive(file->dir);
1385 remove_subsystem(file->system);
1389 * The do_for_each_event_file_safe() is
1390 * a double loop. After finding the call for this
1391 * trace_array, we use break to jump to the next
1395 } while_for_each_event_file();
1398 static void event_remove(struct ftrace_event_call *call)
1400 struct trace_array *tr;
1401 struct ftrace_event_file *file;
1403 do_for_each_event_file(tr, file) {
1404 if (file->event_call != call)
1406 ftrace_event_enable_disable(file, 0);
1408 * The do_for_each_event_file() is
1409 * a double loop. After finding the call for this
1410 * trace_array, we use break to jump to the next
1414 } while_for_each_event_file();
1416 if (call->event.funcs)
1417 __unregister_ftrace_event(&call->event);
1418 remove_event_from_tracers(call);
1419 list_del(&call->list);
1422 static int event_init(struct ftrace_event_call *call)
1426 if (WARN_ON(!call->name))
1429 if (call->class->raw_init) {
1430 ret = call->class->raw_init(call);
1431 if (ret < 0 && ret != -ENOSYS)
1432 pr_warn("Could not initialize trace events/%s\n",
1440 __register_event(struct ftrace_event_call *call, struct module *mod)
1444 ret = event_init(call);
1448 list_add(&call->list, &ftrace_events);
1454 /* Add an event to a trace directory */
1456 __trace_add_new_event(struct ftrace_event_call *call,
1457 struct trace_array *tr,
1458 const struct file_operations *id,
1459 const struct file_operations *enable,
1460 const struct file_operations *filter,
1461 const struct file_operations *format)
1463 struct ftrace_event_file *file;
1465 file = kzalloc(sizeof(*file), GFP_KERNEL);
1469 file->event_call = call;
1471 list_add(&file->list, &tr->events);
1473 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1476 struct ftrace_module_file_ops;
1477 static void __add_event_to_tracers(struct ftrace_event_call *call,
1478 struct ftrace_module_file_ops *file_ops);
1480 /* Add an additional event_call dynamically */
1481 int trace_add_event_call(struct ftrace_event_call *call)
1484 mutex_lock(&event_mutex);
1486 ret = __register_event(call, NULL);
1488 __add_event_to_tracers(call, NULL);
1490 mutex_unlock(&event_mutex);
1495 * Must be called under locking both of event_mutex and trace_event_mutex.
1497 static void __trace_remove_event_call(struct ftrace_event_call *call)
1500 trace_destroy_fields(call);
1501 destroy_preds(call);
1504 /* Remove an event_call */
1505 void trace_remove_event_call(struct ftrace_event_call *call)
1507 mutex_lock(&event_mutex);
1508 down_write(&trace_event_mutex);
1509 __trace_remove_event_call(call);
1510 up_write(&trace_event_mutex);
1511 mutex_unlock(&event_mutex);
1514 #define for_each_event(event, start, end) \
1515 for (event = start; \
1516 (unsigned long)event < (unsigned long)end; \
1519 #ifdef CONFIG_MODULES
1521 static LIST_HEAD(ftrace_module_file_list);
1524 * Modules must own their file_operations to keep up with
1525 * reference counting.
1527 struct ftrace_module_file_ops {
1528 struct list_head list;
1530 struct file_operations id;
1531 struct file_operations enable;
1532 struct file_operations format;
1533 struct file_operations filter;
1536 static struct ftrace_module_file_ops *find_ftrace_file_ops(struct module *mod)
1538 struct ftrace_module_file_ops *file_ops;
1540 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1541 if (file_ops->mod == mod)
1547 static struct ftrace_module_file_ops *
1548 trace_create_file_ops(struct module *mod)
1550 struct ftrace_module_file_ops *file_ops;
1553 * This is a bit of a PITA. To allow for correct reference
1554 * counting, modules must "own" their file_operations.
1555 * To do this, we allocate the file operations that will be
1556 * used in the event directory.
1559 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1563 file_ops->mod = mod;
1565 file_ops->id = ftrace_event_id_fops;
1566 file_ops->id.owner = mod;
1568 file_ops->enable = ftrace_enable_fops;
1569 file_ops->enable.owner = mod;
1571 file_ops->filter = ftrace_event_filter_fops;
1572 file_ops->filter.owner = mod;
1574 file_ops->format = ftrace_event_format_fops;
1575 file_ops->format.owner = mod;
1577 list_add(&file_ops->list, &ftrace_module_file_list);
1582 static void trace_module_add_events(struct module *mod)
1584 struct ftrace_module_file_ops *file_ops = NULL;
1585 struct ftrace_event_call **call, **start, **end;
1587 start = mod->trace_events;
1588 end = mod->trace_events + mod->num_trace_events;
1593 file_ops = trace_create_file_ops(mod);
1597 for_each_event(call, start, end) {
1598 __register_event(*call, mod);
1599 __add_event_to_tracers(*call, file_ops);
1603 static void trace_module_remove_events(struct module *mod)
1605 struct ftrace_module_file_ops *file_ops;
1606 struct ftrace_event_call *call, *p;
1609 down_write(&trace_event_mutex);
1610 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1611 if (call->mod == mod) {
1613 __trace_remove_event_call(call);
1617 /* Now free the file_operations */
1618 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1619 if (file_ops->mod == mod)
1622 if (&file_ops->list != &ftrace_module_file_list) {
1623 list_del(&file_ops->list);
1628 * It is safest to reset the ring buffer if the module being unloaded
1629 * registered any events.
1632 tracing_reset_current_online_cpus();
1633 up_write(&trace_event_mutex);
1636 static int trace_module_notify(struct notifier_block *self,
1637 unsigned long val, void *data)
1639 struct module *mod = data;
1641 mutex_lock(&event_mutex);
1643 case MODULE_STATE_COMING:
1644 trace_module_add_events(mod);
1646 case MODULE_STATE_GOING:
1647 trace_module_remove_events(mod);
1650 mutex_unlock(&event_mutex);
1655 static struct ftrace_module_file_ops *find_ftrace_file_ops(struct module *mod)
1659 static int trace_module_notify(struct notifier_block *self,
1660 unsigned long val, void *data)
1664 #endif /* CONFIG_MODULES */
1666 /* Create a new event directory structure for a trace directory. */
1668 __trace_add_event_dirs(struct trace_array *tr)
1670 struct ftrace_module_file_ops *file_ops = NULL;
1671 struct ftrace_event_call *call;
1674 list_for_each_entry(call, &ftrace_events, list) {
1677 * Directories for events by modules need to
1678 * keep module ref counts when opened (as we don't
1679 * want the module to disappear when reading one
1680 * of these files). The file_ops keep account of
1681 * the module ref count.
1683 * As event_calls are added in groups by module,
1684 * when we find one file_ops, we don't need to search for
1685 * each call in that module, as the rest should be the
1686 * same. Only search for a new one if the last one did
1689 if (!file_ops || call->mod != file_ops->mod)
1690 file_ops = find_ftrace_file_ops(call->mod);
1692 continue; /* Warn? */
1693 ret = __trace_add_new_event(call, tr,
1694 &file_ops->id, &file_ops->enable,
1695 &file_ops->filter, &file_ops->format);
1697 pr_warning("Could not create directory for event %s\n",
1701 ret = __trace_add_new_event(call, tr,
1702 &ftrace_event_id_fops,
1703 &ftrace_enable_fops,
1704 &ftrace_event_filter_fops,
1705 &ftrace_event_format_fops);
1707 pr_warning("Could not create directory for event %s\n",
1713 __add_event_to_tracers(struct ftrace_event_call *call,
1714 struct ftrace_module_file_ops *file_ops)
1716 struct trace_array *tr;
1718 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1720 __trace_add_new_event(call, tr,
1721 &file_ops->id, &file_ops->enable,
1722 &file_ops->filter, &file_ops->format);
1724 __trace_add_new_event(call, tr,
1725 &ftrace_event_id_fops,
1726 &ftrace_enable_fops,
1727 &ftrace_event_filter_fops,
1728 &ftrace_event_format_fops);
1732 static struct notifier_block trace_module_nb = {
1733 .notifier_call = trace_module_notify,
1737 extern struct ftrace_event_call *__start_ftrace_events[];
1738 extern struct ftrace_event_call *__stop_ftrace_events[];
1740 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1742 static __init int setup_trace_event(char *str)
1744 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1745 ring_buffer_expanded = 1;
1746 tracing_selftest_disabled = 1;
1750 __setup("trace_event=", setup_trace_event);
1752 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
1754 struct dentry *d_events;
1755 struct dentry *entry;
1757 mutex_lock(&event_mutex);
1759 entry = debugfs_create_file("set_event", 0644, parent,
1760 tr, &ftrace_set_event_fops);
1762 pr_warning("Could not create debugfs 'set_event' entry\n");
1763 mutex_unlock(&event_mutex);
1767 d_events = debugfs_create_dir("events", parent);
1769 pr_warning("Could not create debugfs 'events' directory\n");
1770 mutex_unlock(&event_mutex);
1774 /* ring buffer internal formats */
1775 trace_create_file("header_page", 0444, d_events,
1776 ring_buffer_print_page_header,
1777 &ftrace_show_header_fops);
1779 trace_create_file("header_event", 0444, d_events,
1780 ring_buffer_print_entry_header,
1781 &ftrace_show_header_fops);
1783 trace_create_file("enable", 0644, d_events,
1784 tr, &ftrace_tr_enable_fops);
1786 tr->event_dir = d_events;
1787 down_write(&trace_event_mutex);
1788 __trace_add_event_dirs(tr);
1789 up_write(&trace_event_mutex);
1791 mutex_unlock(&event_mutex);
1796 static __init int event_trace_enable(void)
1798 struct trace_array *tr = top_trace_array();
1799 struct ftrace_event_call **iter, *call;
1800 char *buf = bootup_event_buf;
1804 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
1807 ret = event_init(call);
1809 list_add(&call->list, &ftrace_events);
1813 token = strsep(&buf, ",");
1820 ret = ftrace_set_clr_event(tr, token, 1);
1822 pr_warn("Failed to enable trace event: %s\n", token);
1825 trace_printk_start_comm();
1830 static __init int event_trace_init(void)
1832 struct trace_array *tr;
1833 struct dentry *d_tracer;
1834 struct dentry *entry;
1837 tr = top_trace_array();
1839 d_tracer = tracing_init_dentry();
1843 entry = debugfs_create_file("available_events", 0444, d_tracer,
1844 tr, &ftrace_avail_fops);
1846 pr_warning("Could not create debugfs "
1847 "'available_events' entry\n");
1849 if (trace_define_common_fields())
1850 pr_warning("tracing: Failed to allocate common fields");
1852 ret = event_trace_add_tracer(d_tracer, tr);
1856 ret = register_module_notifier(&trace_module_nb);
1858 pr_warning("Failed to register trace events module notifier\n");
1862 core_initcall(event_trace_enable);
1863 fs_initcall(event_trace_init);
1865 #ifdef CONFIG_FTRACE_STARTUP_TEST
1867 static DEFINE_SPINLOCK(test_spinlock);
1868 static DEFINE_SPINLOCK(test_spinlock_irq);
1869 static DEFINE_MUTEX(test_mutex);
1871 static __init void test_work(struct work_struct *dummy)
1873 spin_lock(&test_spinlock);
1874 spin_lock_irq(&test_spinlock_irq);
1876 spin_unlock_irq(&test_spinlock_irq);
1877 spin_unlock(&test_spinlock);
1879 mutex_lock(&test_mutex);
1881 mutex_unlock(&test_mutex);
1884 static __init int event_test_thread(void *unused)
1888 test_malloc = kmalloc(1234, GFP_KERNEL);
1890 pr_info("failed to kmalloc\n");
1892 schedule_on_each_cpu(test_work);
1896 set_current_state(TASK_INTERRUPTIBLE);
1897 while (!kthread_should_stop())
1904 * Do various things that may trigger events.
1906 static __init void event_test_stuff(void)
1908 struct task_struct *test_thread;
1910 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1912 kthread_stop(test_thread);
1916 * For every trace event defined, we will test each trace point separately,
1917 * and then by groups, and finally all trace points.
1919 static __init void event_trace_self_tests(void)
1921 struct ftrace_subsystem_dir *dir;
1922 struct ftrace_event_file *file;
1923 struct ftrace_event_call *call;
1924 struct event_subsystem *system;
1925 struct trace_array *tr;
1928 tr = top_trace_array();
1930 pr_info("Running tests on trace events:\n");
1932 list_for_each_entry(file, &tr->events, list) {
1934 call = file->event_call;
1936 /* Only test those that have a probe */
1937 if (!call->class || !call->class->probe)
1941 * Testing syscall events here is pretty useless, but
1942 * we still do it if configured. But this is time consuming.
1943 * What we really need is a user thread to perform the
1944 * syscalls as we test.
1946 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1947 if (call->class->system &&
1948 strcmp(call->class->system, "syscalls") == 0)
1952 pr_info("Testing event %s: ", call->name);
1955 * If an event is already enabled, someone is using
1956 * it and the self test should not be on.
1958 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
1959 pr_warning("Enabled event during self test!\n");
1964 ftrace_event_enable_disable(file, 1);
1966 ftrace_event_enable_disable(file, 0);
1971 /* Now test at the sub system level */
1973 pr_info("Running tests on trace event systems:\n");
1975 list_for_each_entry(dir, &tr->systems, list) {
1977 system = dir->subsystem;
1979 /* the ftrace system is special, skip it */
1980 if (strcmp(system->name, "ftrace") == 0)
1983 pr_info("Testing event system %s: ", system->name);
1985 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
1986 if (WARN_ON_ONCE(ret)) {
1987 pr_warning("error enabling system %s\n",
1994 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
1995 if (WARN_ON_ONCE(ret)) {
1996 pr_warning("error disabling system %s\n",
2004 /* Test with all events enabled */
2006 pr_info("Running tests on all trace events:\n");
2007 pr_info("Testing all events: ");
2009 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2010 if (WARN_ON_ONCE(ret)) {
2011 pr_warning("error enabling all events\n");
2018 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2019 if (WARN_ON_ONCE(ret)) {
2020 pr_warning("error disabling all events\n");
2027 #ifdef CONFIG_FUNCTION_TRACER
2029 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2032 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2033 struct ftrace_ops *op, struct pt_regs *pt_regs)
2035 struct ring_buffer_event *event;
2036 struct ring_buffer *buffer;
2037 struct ftrace_entry *entry;
2038 unsigned long flags;
2043 pc = preempt_count();
2044 preempt_disable_notrace();
2045 cpu = raw_smp_processor_id();
2046 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2051 local_save_flags(flags);
2053 event = trace_current_buffer_lock_reserve(&buffer,
2054 TRACE_FN, sizeof(*entry),
2058 entry = ring_buffer_event_data(event);
2060 entry->parent_ip = parent_ip;
2062 trace_buffer_unlock_commit(buffer, event, flags, pc);
2065 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2066 preempt_enable_notrace();
2069 static struct ftrace_ops trace_ops __initdata =
2071 .func = function_test_events_call,
2072 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2075 static __init void event_trace_self_test_with_function(void)
2078 ret = register_ftrace_function(&trace_ops);
2079 if (WARN_ON(ret < 0)) {
2080 pr_info("Failed to enable function tracer for event tests\n");
2083 pr_info("Running tests again, along with the function tracer\n");
2084 event_trace_self_tests();
2085 unregister_ftrace_function(&trace_ops);
2088 static __init void event_trace_self_test_with_function(void)
2093 static __init int event_trace_self_tests_init(void)
2095 if (!tracing_selftest_disabled) {
2096 event_trace_self_tests();
2097 event_trace_self_test_with_function();
2103 late_initcall(event_trace_self_tests_init);