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)
631 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
635 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
638 struct ftrace_event_file *file = filp->private_data;
645 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
649 ret = tracing_update_buffers();
656 mutex_lock(&event_mutex);
657 ret = ftrace_event_enable_disable(file, val);
658 mutex_unlock(&event_mutex);
667 return ret ? ret : cnt;
671 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
674 const char set_to_char[4] = { '?', '0', '1', 'X' };
675 struct ftrace_subsystem_dir *dir = filp->private_data;
676 struct event_subsystem *system = dir->subsystem;
677 struct ftrace_event_call *call;
678 struct ftrace_event_file *file;
679 struct trace_array *tr = dir->tr;
684 mutex_lock(&event_mutex);
685 list_for_each_entry(file, &tr->events, list) {
686 call = file->event_call;
687 if (!call->name || !call->class || !call->class->reg)
690 if (system && strcmp(call->class->system, system->name) != 0)
694 * We need to find out if all the events are set
695 * or if all events or cleared, or if we have
698 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
701 * If we have a mixture, no need to look further.
706 mutex_unlock(&event_mutex);
708 buf[0] = set_to_char[set];
711 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
717 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
720 struct ftrace_subsystem_dir *dir = filp->private_data;
721 struct event_subsystem *system = dir->subsystem;
722 const char *name = NULL;
726 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
730 ret = tracing_update_buffers();
734 if (val != 0 && val != 1)
738 * Opening of "enable" adds a ref count to system,
739 * so the name is safe to use.
744 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
758 FORMAT_FIELD_SEPERATOR = 2,
762 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
764 struct ftrace_event_call *call = m->private;
765 struct ftrace_event_field *field;
766 struct list_head *common_head = &ftrace_common_fields;
767 struct list_head *head = trace_get_fields(call);
771 switch ((unsigned long)v) {
773 if (unlikely(list_empty(common_head)))
776 field = list_entry(common_head->prev,
777 struct ftrace_event_field, link);
780 case FORMAT_FIELD_SEPERATOR:
781 if (unlikely(list_empty(head)))
784 field = list_entry(head->prev, struct ftrace_event_field, link);
787 case FORMAT_PRINTFMT:
793 if (field->link.prev == common_head)
794 return (void *)FORMAT_FIELD_SEPERATOR;
795 else if (field->link.prev == head)
796 return (void *)FORMAT_PRINTFMT;
798 field = list_entry(field->link.prev, struct ftrace_event_field, link);
803 static void *f_start(struct seq_file *m, loff_t *pos)
808 /* Start by showing the header */
810 return (void *)FORMAT_HEADER;
812 p = (void *)FORMAT_HEADER;
814 p = f_next(m, p, &l);
815 } while (p && l < *pos);
820 static int f_show(struct seq_file *m, void *v)
822 struct ftrace_event_call *call = m->private;
823 struct ftrace_event_field *field;
824 const char *array_descriptor;
826 switch ((unsigned long)v) {
828 seq_printf(m, "name: %s\n", call->name);
829 seq_printf(m, "ID: %d\n", call->event.type);
830 seq_printf(m, "format:\n");
833 case FORMAT_FIELD_SEPERATOR:
837 case FORMAT_PRINTFMT:
838 seq_printf(m, "\nprint fmt: %s\n",
846 * Smartly shows the array type(except dynamic array).
849 * If TYPE := TYPE[LEN], it is shown:
850 * field:TYPE VAR[LEN]
852 array_descriptor = strchr(field->type, '[');
854 if (!strncmp(field->type, "__data_loc", 10))
855 array_descriptor = NULL;
857 if (!array_descriptor)
858 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
859 field->type, field->name, field->offset,
860 field->size, !!field->is_signed);
862 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
863 (int)(array_descriptor - field->type),
864 field->type, field->name,
865 array_descriptor, field->offset,
866 field->size, !!field->is_signed);
871 static void f_stop(struct seq_file *m, void *p)
875 static const struct seq_operations trace_format_seq_ops = {
882 static int trace_format_open(struct inode *inode, struct file *file)
884 struct ftrace_event_call *call = inode->i_private;
888 ret = seq_open(file, &trace_format_seq_ops);
892 m = file->private_data;
899 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
901 struct ftrace_event_call *call = filp->private_data;
908 s = kmalloc(sizeof(*s), GFP_KERNEL);
913 trace_seq_printf(s, "%d\n", call->event.type);
915 r = simple_read_from_buffer(ubuf, cnt, ppos,
922 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
925 struct ftrace_event_call *call = filp->private_data;
932 s = kmalloc(sizeof(*s), GFP_KERNEL);
938 print_event_filter(call, s);
939 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
947 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
950 struct ftrace_event_call *call = filp->private_data;
954 if (cnt >= PAGE_SIZE)
957 buf = (char *)__get_free_page(GFP_TEMPORARY);
961 if (copy_from_user(buf, ubuf, cnt)) {
962 free_page((unsigned long) buf);
967 err = apply_event_filter(call, buf);
968 free_page((unsigned long) buf);
977 static LIST_HEAD(event_subsystems);
979 static int subsystem_open(struct inode *inode, struct file *filp)
981 struct event_subsystem *system = NULL;
982 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
983 struct trace_array *tr;
986 /* Make sure the system still exists */
987 mutex_lock(&event_mutex);
988 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
989 list_for_each_entry(dir, &tr->systems, list) {
990 if (dir == inode->i_private) {
991 /* Don't open systems with no events */
992 if (dir->nr_events) {
993 __get_system_dir(dir);
994 system = dir->subsystem;
1001 mutex_unlock(&event_mutex);
1006 /* Some versions of gcc think dir can be uninitialized here */
1009 ret = tracing_open_generic(inode, filp);
1016 static int system_tr_open(struct inode *inode, struct file *filp)
1018 struct ftrace_subsystem_dir *dir;
1019 struct trace_array *tr = inode->i_private;
1022 /* Make a temporary dir that has no system but points to tr */
1023 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1029 ret = tracing_open_generic(inode, filp);
1033 filp->private_data = dir;
1038 static int subsystem_release(struct inode *inode, struct file *file)
1040 struct ftrace_subsystem_dir *dir = file->private_data;
1043 * If dir->subsystem is NULL, then this is a temporary
1044 * descriptor that was made for a trace_array to enable
1056 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1059 struct ftrace_subsystem_dir *dir = filp->private_data;
1060 struct event_subsystem *system = dir->subsystem;
1061 struct trace_seq *s;
1067 s = kmalloc(sizeof(*s), GFP_KERNEL);
1073 print_subsystem_event_filter(system, s);
1074 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1082 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1085 struct ftrace_subsystem_dir *dir = filp->private_data;
1089 if (cnt >= PAGE_SIZE)
1092 buf = (char *)__get_free_page(GFP_TEMPORARY);
1096 if (copy_from_user(buf, ubuf, cnt)) {
1097 free_page((unsigned long) buf);
1102 err = apply_subsystem_event_filter(dir, buf);
1103 free_page((unsigned long) buf);
1113 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1115 int (*func)(struct trace_seq *s) = filp->private_data;
1116 struct trace_seq *s;
1122 s = kmalloc(sizeof(*s), GFP_KERNEL);
1129 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1136 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1137 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1139 static const struct seq_operations show_event_seq_ops = {
1146 static const struct seq_operations show_set_event_seq_ops = {
1153 static const struct file_operations ftrace_avail_fops = {
1154 .open = ftrace_event_avail_open,
1156 .llseek = seq_lseek,
1157 .release = seq_release,
1160 static const struct file_operations ftrace_set_event_fops = {
1161 .open = ftrace_event_set_open,
1163 .write = ftrace_event_write,
1164 .llseek = seq_lseek,
1165 .release = seq_release,
1168 static const struct file_operations ftrace_enable_fops = {
1169 .open = tracing_open_generic,
1170 .read = event_enable_read,
1171 .write = event_enable_write,
1172 .llseek = default_llseek,
1175 static const struct file_operations ftrace_event_format_fops = {
1176 .open = trace_format_open,
1178 .llseek = seq_lseek,
1179 .release = seq_release,
1182 static const struct file_operations ftrace_event_id_fops = {
1183 .open = tracing_open_generic,
1184 .read = event_id_read,
1185 .llseek = default_llseek,
1188 static const struct file_operations ftrace_event_filter_fops = {
1189 .open = tracing_open_generic,
1190 .read = event_filter_read,
1191 .write = event_filter_write,
1192 .llseek = default_llseek,
1195 static const struct file_operations ftrace_subsystem_filter_fops = {
1196 .open = subsystem_open,
1197 .read = subsystem_filter_read,
1198 .write = subsystem_filter_write,
1199 .llseek = default_llseek,
1200 .release = subsystem_release,
1203 static const struct file_operations ftrace_system_enable_fops = {
1204 .open = subsystem_open,
1205 .read = system_enable_read,
1206 .write = system_enable_write,
1207 .llseek = default_llseek,
1208 .release = subsystem_release,
1211 static const struct file_operations ftrace_tr_enable_fops = {
1212 .open = system_tr_open,
1213 .read = system_enable_read,
1214 .write = system_enable_write,
1215 .llseek = default_llseek,
1216 .release = subsystem_release,
1219 static const struct file_operations ftrace_show_header_fops = {
1220 .open = tracing_open_generic,
1221 .read = show_header,
1222 .llseek = default_llseek,
1226 ftrace_event_open(struct inode *inode, struct file *file,
1227 const struct seq_operations *seq_ops)
1232 ret = seq_open(file, seq_ops);
1235 m = file->private_data;
1236 /* copy tr over to seq ops */
1237 m->private = inode->i_private;
1243 ftrace_event_avail_open(struct inode *inode, struct file *file)
1245 const struct seq_operations *seq_ops = &show_event_seq_ops;
1247 return ftrace_event_open(inode, file, seq_ops);
1251 ftrace_event_set_open(struct inode *inode, struct file *file)
1253 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1254 struct trace_array *tr = inode->i_private;
1256 if ((file->f_mode & FMODE_WRITE) &&
1257 (file->f_flags & O_TRUNC))
1258 ftrace_clear_events(tr);
1260 return ftrace_event_open(inode, file, seq_ops);
1263 static struct event_subsystem *
1264 create_new_subsystem(const char *name)
1266 struct event_subsystem *system;
1268 /* need to create new entry */
1269 system = kmalloc(sizeof(*system), GFP_KERNEL);
1273 system->ref_count = 1;
1274 system->name = name;
1276 system->filter = NULL;
1278 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1279 if (!system->filter)
1282 list_add(&system->list, &event_subsystems);
1291 static struct dentry *
1292 event_subsystem_dir(struct trace_array *tr, const char *name,
1293 struct ftrace_event_file *file, struct dentry *parent)
1295 struct ftrace_subsystem_dir *dir;
1296 struct event_subsystem *system;
1297 struct dentry *entry;
1299 /* First see if we did not already create this dir */
1300 list_for_each_entry(dir, &tr->systems, list) {
1301 system = dir->subsystem;
1302 if (strcmp(system->name, name) == 0) {
1309 /* Now see if the system itself exists. */
1310 list_for_each_entry(system, &event_subsystems, list) {
1311 if (strcmp(system->name, name) == 0)
1314 /* Reset system variable when not found */
1315 if (&system->list == &event_subsystems)
1318 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1323 system = create_new_subsystem(name);
1327 __get_system(system);
1329 dir->entry = debugfs_create_dir(name, parent);
1331 pr_warning("Failed to create system directory %s\n", name);
1332 __put_system(system);
1339 dir->subsystem = system;
1342 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1343 &ftrace_subsystem_filter_fops);
1345 kfree(system->filter);
1346 system->filter = NULL;
1347 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1350 trace_create_file("enable", 0644, dir->entry, dir,
1351 &ftrace_system_enable_fops);
1353 list_add(&dir->list, &tr->systems);
1360 /* Only print this message if failed on memory allocation */
1361 if (!dir || !system)
1362 pr_warning("No memory to create event subsystem %s\n",
1368 event_create_dir(struct dentry *parent,
1369 struct ftrace_event_file *file,
1370 const struct file_operations *id,
1371 const struct file_operations *enable,
1372 const struct file_operations *filter,
1373 const struct file_operations *format)
1375 struct ftrace_event_call *call = file->event_call;
1376 struct trace_array *tr = file->tr;
1377 struct list_head *head;
1378 struct dentry *d_events;
1382 * If the trace point header did not define TRACE_SYSTEM
1383 * then the system would be called "TRACE_SYSTEM".
1385 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1386 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1392 file->dir = debugfs_create_dir(call->name, d_events);
1394 pr_warning("Could not create debugfs '%s' directory\n",
1399 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1400 trace_create_file("enable", 0644, file->dir, file,
1403 #ifdef CONFIG_PERF_EVENTS
1404 if (call->event.type && call->class->reg)
1405 trace_create_file("id", 0444, file->dir, call,
1410 * Other events may have the same class. Only update
1411 * the fields if they are not already defined.
1413 head = trace_get_fields(call);
1414 if (list_empty(head)) {
1415 ret = call->class->define_fields(call);
1417 pr_warning("Could not initialize trace point"
1418 " events/%s\n", call->name);
1422 trace_create_file("filter", 0644, file->dir, call,
1425 trace_create_file("format", 0444, file->dir, call,
1431 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1436 if (!--dir->nr_events) {
1437 debugfs_remove_recursive(dir->entry);
1438 list_del(&dir->list);
1439 __put_system_dir(dir);
1443 static void remove_event_from_tracers(struct ftrace_event_call *call)
1445 struct ftrace_event_file *file;
1446 struct trace_array *tr;
1448 do_for_each_event_file_safe(tr, file) {
1450 if (file->event_call != call)
1453 list_del(&file->list);
1454 debugfs_remove_recursive(file->dir);
1455 remove_subsystem(file->system);
1456 kmem_cache_free(file_cachep, file);
1459 * The do_for_each_event_file_safe() is
1460 * a double loop. After finding the call for this
1461 * trace_array, we use break to jump to the next
1465 } while_for_each_event_file();
1468 static void event_remove(struct ftrace_event_call *call)
1470 struct trace_array *tr;
1471 struct ftrace_event_file *file;
1473 do_for_each_event_file(tr, file) {
1474 if (file->event_call != call)
1476 ftrace_event_enable_disable(file, 0);
1478 * The do_for_each_event_file() is
1479 * a double loop. After finding the call for this
1480 * trace_array, we use break to jump to the next
1484 } while_for_each_event_file();
1486 if (call->event.funcs)
1487 __unregister_ftrace_event(&call->event);
1488 remove_event_from_tracers(call);
1489 list_del(&call->list);
1492 static int event_init(struct ftrace_event_call *call)
1496 if (WARN_ON(!call->name))
1499 if (call->class->raw_init) {
1500 ret = call->class->raw_init(call);
1501 if (ret < 0 && ret != -ENOSYS)
1502 pr_warn("Could not initialize trace events/%s\n",
1510 __register_event(struct ftrace_event_call *call, struct module *mod)
1514 ret = event_init(call);
1518 list_add(&call->list, &ftrace_events);
1524 /* Add an event to a trace directory */
1526 __trace_add_new_event(struct ftrace_event_call *call,
1527 struct trace_array *tr,
1528 const struct file_operations *id,
1529 const struct file_operations *enable,
1530 const struct file_operations *filter,
1531 const struct file_operations *format)
1533 struct ftrace_event_file *file;
1535 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1539 file->event_call = call;
1541 list_add(&file->list, &tr->events);
1543 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1547 * Just create a decriptor for early init. A descriptor is required
1548 * for enabling events at boot. We want to enable events before
1549 * the filesystem is initialized.
1552 __trace_early_add_new_event(struct ftrace_event_call *call,
1553 struct trace_array *tr)
1555 struct ftrace_event_file *file;
1557 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1561 file->event_call = call;
1563 list_add(&file->list, &tr->events);
1568 struct ftrace_module_file_ops;
1569 static void __add_event_to_tracers(struct ftrace_event_call *call,
1570 struct ftrace_module_file_ops *file_ops);
1572 /* Add an additional event_call dynamically */
1573 int trace_add_event_call(struct ftrace_event_call *call)
1576 mutex_lock(&event_mutex);
1578 ret = __register_event(call, NULL);
1580 __add_event_to_tracers(call, NULL);
1582 mutex_unlock(&event_mutex);
1587 * Must be called under locking both of event_mutex and trace_event_sem.
1589 static void __trace_remove_event_call(struct ftrace_event_call *call)
1592 trace_destroy_fields(call);
1593 destroy_preds(call);
1596 /* Remove an event_call */
1597 void trace_remove_event_call(struct ftrace_event_call *call)
1599 mutex_lock(&event_mutex);
1600 down_write(&trace_event_sem);
1601 __trace_remove_event_call(call);
1602 up_write(&trace_event_sem);
1603 mutex_unlock(&event_mutex);
1606 #define for_each_event(event, start, end) \
1607 for (event = start; \
1608 (unsigned long)event < (unsigned long)end; \
1611 #ifdef CONFIG_MODULES
1613 static LIST_HEAD(ftrace_module_file_list);
1616 * Modules must own their file_operations to keep up with
1617 * reference counting.
1619 struct ftrace_module_file_ops {
1620 struct list_head list;
1622 struct file_operations id;
1623 struct file_operations enable;
1624 struct file_operations format;
1625 struct file_operations filter;
1628 static struct ftrace_module_file_ops *
1629 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1632 * As event_calls are added in groups by module,
1633 * when we find one file_ops, we don't need to search for
1634 * each call in that module, as the rest should be the
1635 * same. Only search for a new one if the last one did
1638 if (file_ops && mod == file_ops->mod)
1641 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1642 if (file_ops->mod == mod)
1648 static struct ftrace_module_file_ops *
1649 trace_create_file_ops(struct module *mod)
1651 struct ftrace_module_file_ops *file_ops;
1654 * This is a bit of a PITA. To allow for correct reference
1655 * counting, modules must "own" their file_operations.
1656 * To do this, we allocate the file operations that will be
1657 * used in the event directory.
1660 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1664 file_ops->mod = mod;
1666 file_ops->id = ftrace_event_id_fops;
1667 file_ops->id.owner = mod;
1669 file_ops->enable = ftrace_enable_fops;
1670 file_ops->enable.owner = mod;
1672 file_ops->filter = ftrace_event_filter_fops;
1673 file_ops->filter.owner = mod;
1675 file_ops->format = ftrace_event_format_fops;
1676 file_ops->format.owner = mod;
1678 list_add(&file_ops->list, &ftrace_module_file_list);
1683 static void trace_module_add_events(struct module *mod)
1685 struct ftrace_module_file_ops *file_ops = NULL;
1686 struct ftrace_event_call **call, **start, **end;
1688 start = mod->trace_events;
1689 end = mod->trace_events + mod->num_trace_events;
1694 file_ops = trace_create_file_ops(mod);
1698 for_each_event(call, start, end) {
1699 __register_event(*call, mod);
1700 __add_event_to_tracers(*call, file_ops);
1704 static void trace_module_remove_events(struct module *mod)
1706 struct ftrace_module_file_ops *file_ops;
1707 struct ftrace_event_call *call, *p;
1708 bool clear_trace = false;
1710 down_write(&trace_event_sem);
1711 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1712 if (call->mod == mod) {
1713 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1715 __trace_remove_event_call(call);
1719 /* Now free the file_operations */
1720 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1721 if (file_ops->mod == mod)
1724 if (&file_ops->list != &ftrace_module_file_list) {
1725 list_del(&file_ops->list);
1728 up_write(&trace_event_sem);
1731 * It is safest to reset the ring buffer if the module being unloaded
1732 * registered any events that were used. The only worry is if
1733 * a new module gets loaded, and takes on the same id as the events
1734 * of this module. When printing out the buffer, traced events left
1735 * over from this module may be passed to the new module events and
1736 * unexpected results may occur.
1739 tracing_reset_all_online_cpus();
1742 static int trace_module_notify(struct notifier_block *self,
1743 unsigned long val, void *data)
1745 struct module *mod = data;
1747 mutex_lock(&event_mutex);
1749 case MODULE_STATE_COMING:
1750 trace_module_add_events(mod);
1752 case MODULE_STATE_GOING:
1753 trace_module_remove_events(mod);
1756 mutex_unlock(&event_mutex);
1762 __trace_add_new_mod_event(struct ftrace_event_call *call,
1763 struct trace_array *tr,
1764 struct ftrace_module_file_ops *file_ops)
1766 return __trace_add_new_event(call, tr,
1767 &file_ops->id, &file_ops->enable,
1768 &file_ops->filter, &file_ops->format);
1772 static inline struct ftrace_module_file_ops *
1773 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1777 static inline int trace_module_notify(struct notifier_block *self,
1778 unsigned long val, void *data)
1783 __trace_add_new_mod_event(struct ftrace_event_call *call,
1784 struct trace_array *tr,
1785 struct ftrace_module_file_ops *file_ops)
1789 #endif /* CONFIG_MODULES */
1791 /* Create a new event directory structure for a trace directory. */
1793 __trace_add_event_dirs(struct trace_array *tr)
1795 struct ftrace_module_file_ops *file_ops = NULL;
1796 struct ftrace_event_call *call;
1799 list_for_each_entry(call, &ftrace_events, list) {
1802 * Directories for events by modules need to
1803 * keep module ref counts when opened (as we don't
1804 * want the module to disappear when reading one
1805 * of these files). The file_ops keep account of
1806 * the module ref count.
1808 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1810 continue; /* Warn? */
1811 ret = __trace_add_new_mod_event(call, tr, file_ops);
1813 pr_warning("Could not create directory for event %s\n",
1817 ret = __trace_add_new_event(call, tr,
1818 &ftrace_event_id_fops,
1819 &ftrace_enable_fops,
1820 &ftrace_event_filter_fops,
1821 &ftrace_event_format_fops);
1823 pr_warning("Could not create directory for event %s\n",
1828 #ifdef CONFIG_DYNAMIC_FTRACE
1831 #define ENABLE_EVENT_STR "enable_event"
1832 #define DISABLE_EVENT_STR "disable_event"
1834 struct event_probe_data {
1835 struct ftrace_event_file *file;
1836 unsigned long count;
1841 static struct ftrace_event_file *
1842 find_event_file(struct trace_array *tr, const char *system, const char *event)
1844 struct ftrace_event_file *file;
1845 struct ftrace_event_call *call;
1847 list_for_each_entry(file, &tr->events, list) {
1849 call = file->event_call;
1851 if (!call->name || !call->class || !call->class->reg)
1854 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1857 if (strcmp(event, call->name) == 0 &&
1858 strcmp(system, call->class->system) == 0)
1865 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1867 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1868 struct event_probe_data *data = *pdata;
1874 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1876 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1880 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1882 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1883 struct event_probe_data *data = *pdata;
1891 /* Skip if the event is in a state we want to switch to */
1892 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1895 if (data->count != -1)
1898 event_enable_probe(ip, parent_ip, _data);
1902 event_enable_print(struct seq_file *m, unsigned long ip,
1903 struct ftrace_probe_ops *ops, void *_data)
1905 struct event_probe_data *data = _data;
1907 seq_printf(m, "%ps:", (void *)ip);
1909 seq_printf(m, "%s:%s:%s",
1910 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1911 data->file->event_call->class->system,
1912 data->file->event_call->name);
1914 if (data->count == -1)
1915 seq_printf(m, ":unlimited\n");
1917 seq_printf(m, ":count=%ld\n", data->count);
1923 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1926 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1927 struct event_probe_data *data = *pdata;
1934 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1937 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1938 struct event_probe_data *data = *pdata;
1940 if (WARN_ON_ONCE(data->ref <= 0))
1945 /* Remove the SOFT_MODE flag */
1946 __ftrace_event_enable_disable(data->file, 0, 1);
1947 module_put(data->file->event_call->mod);
1953 static struct ftrace_probe_ops event_enable_probe_ops = {
1954 .func = event_enable_probe,
1955 .print = event_enable_print,
1956 .init = event_enable_init,
1957 .free = event_enable_free,
1960 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1961 .func = event_enable_count_probe,
1962 .print = event_enable_print,
1963 .init = event_enable_init,
1964 .free = event_enable_free,
1967 static struct ftrace_probe_ops event_disable_probe_ops = {
1968 .func = event_enable_probe,
1969 .print = event_enable_print,
1970 .init = event_enable_init,
1971 .free = event_enable_free,
1974 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1975 .func = event_enable_count_probe,
1976 .print = event_enable_print,
1977 .init = event_enable_init,
1978 .free = event_enable_free,
1982 event_enable_func(struct ftrace_hash *hash,
1983 char *glob, char *cmd, char *param, int enabled)
1985 struct trace_array *tr = top_trace_array();
1986 struct ftrace_event_file *file;
1987 struct ftrace_probe_ops *ops;
1988 struct event_probe_data *data;
1995 /* hash funcs only work with set_ftrace_filter */
2002 system = strsep(¶m, ":");
2006 event = strsep(¶m, ":");
2008 mutex_lock(&event_mutex);
2011 file = find_event_file(tr, system, event);
2015 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2018 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2020 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2022 if (glob[0] == '!') {
2023 unregister_ftrace_function_probe_func(glob+1, ops);
2029 data = kzalloc(sizeof(*data), GFP_KERNEL);
2033 data->enable = enable;
2040 number = strsep(¶m, ":");
2043 if (!strlen(number))
2047 * We use the callback data field (which is a pointer)
2050 ret = kstrtoul(number, 0, &data->count);
2055 /* Don't let event modules unload while probe registered */
2056 ret = try_module_get(file->event_call->mod);
2060 ret = __ftrace_event_enable_disable(file, 1, 1);
2063 ret = register_ftrace_function_probe(glob, ops, data);
2065 * The above returns on success the # of functions enabled,
2066 * but if it didn't find any functions it returns zero.
2067 * Consider no functions a failure too.
2074 /* Just return zero, not the number of enabled functions */
2077 mutex_unlock(&event_mutex);
2081 __ftrace_event_enable_disable(file, 0, 1);
2083 module_put(file->event_call->mod);
2089 static struct ftrace_func_command event_enable_cmd = {
2090 .name = ENABLE_EVENT_STR,
2091 .func = event_enable_func,
2094 static struct ftrace_func_command event_disable_cmd = {
2095 .name = DISABLE_EVENT_STR,
2096 .func = event_enable_func,
2099 static __init int register_event_cmds(void)
2103 ret = register_ftrace_command(&event_enable_cmd);
2104 if (WARN_ON(ret < 0))
2106 ret = register_ftrace_command(&event_disable_cmd);
2107 if (WARN_ON(ret < 0))
2108 unregister_ftrace_command(&event_enable_cmd);
2112 static inline int register_event_cmds(void) { return 0; }
2113 #endif /* CONFIG_DYNAMIC_FTRACE */
2116 * The top level array has already had its ftrace_event_file
2117 * descriptors created in order to allow for early events to
2118 * be recorded. This function is called after the debugfs has been
2119 * initialized, and we now have to create the files associated
2123 __trace_early_add_event_dirs(struct trace_array *tr)
2125 struct ftrace_event_file *file;
2129 list_for_each_entry(file, &tr->events, list) {
2130 ret = event_create_dir(tr->event_dir, file,
2131 &ftrace_event_id_fops,
2132 &ftrace_enable_fops,
2133 &ftrace_event_filter_fops,
2134 &ftrace_event_format_fops);
2136 pr_warning("Could not create directory for event %s\n",
2137 file->event_call->name);
2142 * For early boot up, the top trace array requires to have
2143 * a list of events that can be enabled. This must be done before
2144 * the filesystem is set up in order to allow events to be traced
2148 __trace_early_add_events(struct trace_array *tr)
2150 struct ftrace_event_call *call;
2153 list_for_each_entry(call, &ftrace_events, list) {
2154 /* Early boot up should not have any modules loaded */
2155 if (WARN_ON_ONCE(call->mod))
2158 ret = __trace_early_add_new_event(call, tr);
2160 pr_warning("Could not create early event %s\n",
2165 /* Remove the event directory structure for a trace directory. */
2167 __trace_remove_event_dirs(struct trace_array *tr)
2169 struct ftrace_event_file *file, *next;
2171 list_for_each_entry_safe(file, next, &tr->events, list) {
2172 list_del(&file->list);
2173 debugfs_remove_recursive(file->dir);
2174 remove_subsystem(file->system);
2175 kmem_cache_free(file_cachep, file);
2180 __add_event_to_tracers(struct ftrace_event_call *call,
2181 struct ftrace_module_file_ops *file_ops)
2183 struct trace_array *tr;
2185 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2187 __trace_add_new_mod_event(call, tr, file_ops);
2189 __trace_add_new_event(call, tr,
2190 &ftrace_event_id_fops,
2191 &ftrace_enable_fops,
2192 &ftrace_event_filter_fops,
2193 &ftrace_event_format_fops);
2197 static struct notifier_block trace_module_nb = {
2198 .notifier_call = trace_module_notify,
2202 extern struct ftrace_event_call *__start_ftrace_events[];
2203 extern struct ftrace_event_call *__stop_ftrace_events[];
2205 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2207 static __init int setup_trace_event(char *str)
2209 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2210 ring_buffer_expanded = true;
2211 tracing_selftest_disabled = true;
2215 __setup("trace_event=", setup_trace_event);
2217 /* Expects to have event_mutex held when called */
2219 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2221 struct dentry *d_events;
2222 struct dentry *entry;
2224 entry = debugfs_create_file("set_event", 0644, parent,
2225 tr, &ftrace_set_event_fops);
2227 pr_warning("Could not create debugfs 'set_event' entry\n");
2231 d_events = debugfs_create_dir("events", parent);
2233 pr_warning("Could not create debugfs 'events' directory\n");
2237 /* ring buffer internal formats */
2238 trace_create_file("header_page", 0444, d_events,
2239 ring_buffer_print_page_header,
2240 &ftrace_show_header_fops);
2242 trace_create_file("header_event", 0444, d_events,
2243 ring_buffer_print_entry_header,
2244 &ftrace_show_header_fops);
2246 trace_create_file("enable", 0644, d_events,
2247 tr, &ftrace_tr_enable_fops);
2249 tr->event_dir = d_events;
2255 * event_trace_add_tracer - add a instance of a trace_array to events
2256 * @parent: The parent dentry to place the files/directories for events in
2257 * @tr: The trace array associated with these events
2259 * When a new instance is created, it needs to set up its events
2260 * directory, as well as other files associated with events. It also
2261 * creates the event hierachry in the @parent/events directory.
2263 * Returns 0 on success.
2265 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2269 mutex_lock(&event_mutex);
2271 ret = create_event_toplevel_files(parent, tr);
2275 down_write(&trace_event_sem);
2276 __trace_add_event_dirs(tr);
2277 up_write(&trace_event_sem);
2280 mutex_unlock(&event_mutex);
2286 * The top trace array already had its file descriptors created.
2287 * Now the files themselves need to be created.
2290 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2294 mutex_lock(&event_mutex);
2296 ret = create_event_toplevel_files(parent, tr);
2300 down_write(&trace_event_sem);
2301 __trace_early_add_event_dirs(tr);
2302 up_write(&trace_event_sem);
2305 mutex_unlock(&event_mutex);
2310 int event_trace_del_tracer(struct trace_array *tr)
2312 /* Disable any running events */
2313 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2315 mutex_lock(&event_mutex);
2317 down_write(&trace_event_sem);
2318 __trace_remove_event_dirs(tr);
2319 debugfs_remove_recursive(tr->event_dir);
2320 up_write(&trace_event_sem);
2322 tr->event_dir = NULL;
2324 mutex_unlock(&event_mutex);
2329 static __init int event_trace_memsetup(void)
2331 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2332 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2336 static __init int event_trace_enable(void)
2338 struct trace_array *tr = top_trace_array();
2339 struct ftrace_event_call **iter, *call;
2340 char *buf = bootup_event_buf;
2344 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2347 ret = event_init(call);
2349 list_add(&call->list, &ftrace_events);
2353 * We need the top trace array to have a working set of trace
2354 * points at early init, before the debug files and directories
2355 * are created. Create the file entries now, and attach them
2356 * to the actual file dentries later.
2358 __trace_early_add_events(tr);
2361 token = strsep(&buf, ",");
2368 ret = ftrace_set_clr_event(tr, token, 1);
2370 pr_warn("Failed to enable trace event: %s\n", token);
2373 trace_printk_start_comm();
2375 register_event_cmds();
2380 static __init int event_trace_init(void)
2382 struct trace_array *tr;
2383 struct dentry *d_tracer;
2384 struct dentry *entry;
2387 tr = top_trace_array();
2389 d_tracer = tracing_init_dentry();
2393 entry = debugfs_create_file("available_events", 0444, d_tracer,
2394 tr, &ftrace_avail_fops);
2396 pr_warning("Could not create debugfs "
2397 "'available_events' entry\n");
2399 if (trace_define_common_fields())
2400 pr_warning("tracing: Failed to allocate common fields");
2402 ret = early_event_add_tracer(d_tracer, tr);
2406 ret = register_module_notifier(&trace_module_nb);
2408 pr_warning("Failed to register trace events module notifier\n");
2412 early_initcall(event_trace_memsetup);
2413 core_initcall(event_trace_enable);
2414 fs_initcall(event_trace_init);
2416 #ifdef CONFIG_FTRACE_STARTUP_TEST
2418 static DEFINE_SPINLOCK(test_spinlock);
2419 static DEFINE_SPINLOCK(test_spinlock_irq);
2420 static DEFINE_MUTEX(test_mutex);
2422 static __init void test_work(struct work_struct *dummy)
2424 spin_lock(&test_spinlock);
2425 spin_lock_irq(&test_spinlock_irq);
2427 spin_unlock_irq(&test_spinlock_irq);
2428 spin_unlock(&test_spinlock);
2430 mutex_lock(&test_mutex);
2432 mutex_unlock(&test_mutex);
2435 static __init int event_test_thread(void *unused)
2439 test_malloc = kmalloc(1234, GFP_KERNEL);
2441 pr_info("failed to kmalloc\n");
2443 schedule_on_each_cpu(test_work);
2447 set_current_state(TASK_INTERRUPTIBLE);
2448 while (!kthread_should_stop())
2455 * Do various things that may trigger events.
2457 static __init void event_test_stuff(void)
2459 struct task_struct *test_thread;
2461 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2463 kthread_stop(test_thread);
2467 * For every trace event defined, we will test each trace point separately,
2468 * and then by groups, and finally all trace points.
2470 static __init void event_trace_self_tests(void)
2472 struct ftrace_subsystem_dir *dir;
2473 struct ftrace_event_file *file;
2474 struct ftrace_event_call *call;
2475 struct event_subsystem *system;
2476 struct trace_array *tr;
2479 tr = top_trace_array();
2481 pr_info("Running tests on trace events:\n");
2483 list_for_each_entry(file, &tr->events, list) {
2485 call = file->event_call;
2487 /* Only test those that have a probe */
2488 if (!call->class || !call->class->probe)
2492 * Testing syscall events here is pretty useless, but
2493 * we still do it if configured. But this is time consuming.
2494 * What we really need is a user thread to perform the
2495 * syscalls as we test.
2497 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2498 if (call->class->system &&
2499 strcmp(call->class->system, "syscalls") == 0)
2503 pr_info("Testing event %s: ", call->name);
2506 * If an event is already enabled, someone is using
2507 * it and the self test should not be on.
2509 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2510 pr_warning("Enabled event during self test!\n");
2515 ftrace_event_enable_disable(file, 1);
2517 ftrace_event_enable_disable(file, 0);
2522 /* Now test at the sub system level */
2524 pr_info("Running tests on trace event systems:\n");
2526 list_for_each_entry(dir, &tr->systems, list) {
2528 system = dir->subsystem;
2530 /* the ftrace system is special, skip it */
2531 if (strcmp(system->name, "ftrace") == 0)
2534 pr_info("Testing event system %s: ", system->name);
2536 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2537 if (WARN_ON_ONCE(ret)) {
2538 pr_warning("error enabling system %s\n",
2545 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2546 if (WARN_ON_ONCE(ret)) {
2547 pr_warning("error disabling system %s\n",
2555 /* Test with all events enabled */
2557 pr_info("Running tests on all trace events:\n");
2558 pr_info("Testing all events: ");
2560 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2561 if (WARN_ON_ONCE(ret)) {
2562 pr_warning("error enabling all events\n");
2569 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2570 if (WARN_ON_ONCE(ret)) {
2571 pr_warning("error disabling all events\n");
2578 #ifdef CONFIG_FUNCTION_TRACER
2580 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2583 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2584 struct ftrace_ops *op, struct pt_regs *pt_regs)
2586 struct ring_buffer_event *event;
2587 struct ring_buffer *buffer;
2588 struct ftrace_entry *entry;
2589 unsigned long flags;
2594 pc = preempt_count();
2595 preempt_disable_notrace();
2596 cpu = raw_smp_processor_id();
2597 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2602 local_save_flags(flags);
2604 event = trace_current_buffer_lock_reserve(&buffer,
2605 TRACE_FN, sizeof(*entry),
2609 entry = ring_buffer_event_data(event);
2611 entry->parent_ip = parent_ip;
2613 trace_buffer_unlock_commit(buffer, event, flags, pc);
2616 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2617 preempt_enable_notrace();
2620 static struct ftrace_ops trace_ops __initdata =
2622 .func = function_test_events_call,
2623 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2626 static __init void event_trace_self_test_with_function(void)
2629 ret = register_ftrace_function(&trace_ops);
2630 if (WARN_ON(ret < 0)) {
2631 pr_info("Failed to enable function tracer for event tests\n");
2634 pr_info("Running tests again, along with the function tracer\n");
2635 event_trace_self_tests();
2636 unregister_ftrace_function(&trace_ops);
2639 static __init void event_trace_self_test_with_function(void)
2644 static __init int event_trace_self_tests_init(void)
2646 if (!tracing_selftest_disabled) {
2647 event_trace_self_tests();
2648 event_trace_self_test_with_function();
2654 late_initcall(event_trace_self_tests_init);