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 #define SYSTEM_FL_FREE_NAME (1 << 31)
46 static inline int system_refcount(struct event_subsystem *system)
48 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
51 static int system_refcount_inc(struct event_subsystem *system)
53 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
56 static int system_refcount_dec(struct event_subsystem *system)
58 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
61 /* Double loops, do not use break, only goto's work */
62 #define do_for_each_event_file(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 list_for_each_entry(file, &tr->events, list)
66 #define do_for_each_event_file_safe(tr, file) \
67 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
68 struct ftrace_event_file *___n; \
69 list_for_each_entry_safe(file, ___n, &tr->events, list)
71 #define while_for_each_event_file() \
74 static struct list_head *
75 trace_get_fields(struct ftrace_event_call *event_call)
77 if (!event_call->class->get_fields)
78 return &event_call->class->fields;
79 return event_call->class->get_fields(event_call);
82 static struct ftrace_event_field *
83 __find_event_field(struct list_head *head, char *name)
85 struct ftrace_event_field *field;
87 list_for_each_entry(field, head, link) {
88 if (!strcmp(field->name, name))
95 struct ftrace_event_field *
96 trace_find_event_field(struct ftrace_event_call *call, char *name)
98 struct ftrace_event_field *field;
99 struct list_head *head;
101 field = __find_event_field(&ftrace_common_fields, name);
105 head = trace_get_fields(call);
106 return __find_event_field(head, name);
109 static int __trace_define_field(struct list_head *head, const char *type,
110 const char *name, int offset, int size,
111 int is_signed, int filter_type)
113 struct ftrace_event_field *field;
115 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
122 if (filter_type == FILTER_OTHER)
123 field->filter_type = filter_assign_type(type);
125 field->filter_type = filter_type;
127 field->offset = offset;
129 field->is_signed = is_signed;
131 list_add(&field->link, head);
136 kmem_cache_free(field_cachep, field);
141 int trace_define_field(struct ftrace_event_call *call, const char *type,
142 const char *name, int offset, int size, int is_signed,
145 struct list_head *head;
147 if (WARN_ON(!call->class))
150 head = trace_get_fields(call);
151 return __trace_define_field(head, type, name, offset, size,
152 is_signed, filter_type);
154 EXPORT_SYMBOL_GPL(trace_define_field);
156 #define __common_field(type, item) \
157 ret = __trace_define_field(&ftrace_common_fields, #type, \
159 offsetof(typeof(ent), item), \
161 is_signed_type(type), FILTER_OTHER); \
165 static int trace_define_common_fields(void)
168 struct trace_entry ent;
170 __common_field(unsigned short, type);
171 __common_field(unsigned char, flags);
172 __common_field(unsigned char, preempt_count);
173 __common_field(int, pid);
178 static void trace_destroy_fields(struct ftrace_event_call *call)
180 struct ftrace_event_field *field, *next;
181 struct list_head *head;
183 head = trace_get_fields(call);
184 list_for_each_entry_safe(field, next, head, link) {
185 list_del(&field->link);
186 kmem_cache_free(field_cachep, field);
190 int trace_event_raw_init(struct ftrace_event_call *call)
194 id = register_ftrace_event(&call->event);
200 EXPORT_SYMBOL_GPL(trace_event_raw_init);
202 int ftrace_event_reg(struct ftrace_event_call *call,
203 enum trace_reg type, void *data)
205 struct ftrace_event_file *file = data;
208 case TRACE_REG_REGISTER:
209 return tracepoint_probe_register(call->name,
212 case TRACE_REG_UNREGISTER:
213 tracepoint_probe_unregister(call->name,
218 #ifdef CONFIG_PERF_EVENTS
219 case TRACE_REG_PERF_REGISTER:
220 return tracepoint_probe_register(call->name,
221 call->class->perf_probe,
223 case TRACE_REG_PERF_UNREGISTER:
224 tracepoint_probe_unregister(call->name,
225 call->class->perf_probe,
228 case TRACE_REG_PERF_OPEN:
229 case TRACE_REG_PERF_CLOSE:
230 case TRACE_REG_PERF_ADD:
231 case TRACE_REG_PERF_DEL:
237 EXPORT_SYMBOL_GPL(ftrace_event_reg);
239 void trace_event_enable_cmd_record(bool enable)
241 struct ftrace_event_file *file;
242 struct trace_array *tr;
244 mutex_lock(&event_mutex);
245 do_for_each_event_file(tr, file) {
247 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
251 tracing_start_cmdline_record();
252 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
254 tracing_stop_cmdline_record();
255 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
257 } while_for_each_event_file();
258 mutex_unlock(&event_mutex);
261 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
262 int enable, int soft_disable)
264 struct ftrace_event_call *call = file->event_call;
271 * When soft_disable is set and enable is cleared, the sm_ref
272 * reference counter is decremented. If it reaches 0, we want
273 * to clear the SOFT_DISABLED flag but leave the event in the
274 * state that it was. That is, if the event was enabled and
275 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
276 * is set we do not want the event to be enabled before we
279 * When soft_disable is not set but the SOFT_MODE flag is,
280 * we do nothing. Do not disable the tracepoint, otherwise
281 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
284 if (atomic_dec_return(&file->sm_ref) > 0)
286 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
287 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
289 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
291 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
292 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
293 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
294 tracing_stop_cmdline_record();
295 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
297 call->class->reg(call, TRACE_REG_UNREGISTER, file);
299 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
300 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
301 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
305 * When soft_disable is set and enable is set, we want to
306 * register the tracepoint for the event, but leave the event
307 * as is. That means, if the event was already enabled, we do
308 * nothing (but set SOFT_MODE). If the event is disabled, we
309 * set SOFT_DISABLED before enabling the event tracepoint, so
310 * it still seems to be disabled.
313 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
315 if (atomic_inc_return(&file->sm_ref) > 1)
317 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
320 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
322 /* Keep the event disabled, when going to SOFT_MODE. */
324 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
326 if (trace_flags & TRACE_ITER_RECORD_CMD) {
327 tracing_start_cmdline_record();
328 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
330 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
332 tracing_stop_cmdline_record();
333 pr_info("event trace: Could not enable event "
337 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
339 /* WAS_ENABLED gets set but never cleared. */
340 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
348 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
351 return __ftrace_event_enable_disable(file, enable, 0);
354 static void ftrace_clear_events(struct trace_array *tr)
356 struct ftrace_event_file *file;
358 mutex_lock(&event_mutex);
359 list_for_each_entry(file, &tr->events, list) {
360 ftrace_event_enable_disable(file, 0);
362 mutex_unlock(&event_mutex);
365 static void __put_system(struct event_subsystem *system)
367 struct event_filter *filter = system->filter;
369 WARN_ON_ONCE(system_refcount(system) == 0);
370 if (system_refcount_dec(system))
373 list_del(&system->list);
376 kfree(filter->filter_string);
379 if (system->ref_count & SYSTEM_FL_FREE_NAME)
384 static void __get_system(struct event_subsystem *system)
386 WARN_ON_ONCE(system_refcount(system) == 0);
387 system_refcount_inc(system);
390 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
392 WARN_ON_ONCE(dir->ref_count == 0);
394 __get_system(dir->subsystem);
397 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
399 WARN_ON_ONCE(dir->ref_count == 0);
400 /* If the subsystem is about to be freed, the dir must be too */
401 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
403 __put_system(dir->subsystem);
404 if (!--dir->ref_count)
408 static void put_system(struct ftrace_subsystem_dir *dir)
410 mutex_lock(&event_mutex);
411 __put_system_dir(dir);
412 mutex_unlock(&event_mutex);
416 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
418 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
419 const char *sub, const char *event, int set)
421 struct ftrace_event_file *file;
422 struct ftrace_event_call *call;
425 mutex_lock(&event_mutex);
426 list_for_each_entry(file, &tr->events, list) {
428 call = file->event_call;
430 if (!call->name || !call->class || !call->class->reg)
433 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
437 strcmp(match, call->name) != 0 &&
438 strcmp(match, call->class->system) != 0)
441 if (sub && strcmp(sub, call->class->system) != 0)
444 if (event && strcmp(event, call->name) != 0)
447 ftrace_event_enable_disable(file, set);
451 mutex_unlock(&event_mutex);
456 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
458 char *event = NULL, *sub = NULL, *match;
461 * The buf format can be <subsystem>:<event-name>
462 * *:<event-name> means any event by that name.
463 * :<event-name> is the same.
465 * <subsystem>:* means all events in that subsystem
466 * <subsystem>: means the same.
468 * <name> (no ':') means all events in a subsystem with
469 * the name <name> or any event that matches <name>
472 match = strsep(&buf, ":");
478 if (!strlen(sub) || strcmp(sub, "*") == 0)
480 if (!strlen(event) || strcmp(event, "*") == 0)
484 return __ftrace_set_clr_event(tr, match, sub, event, set);
488 * trace_set_clr_event - enable or disable an event
489 * @system: system name to match (NULL for any system)
490 * @event: event name to match (NULL for all events, within system)
491 * @set: 1 to enable, 0 to disable
493 * This is a way for other parts of the kernel to enable or disable
496 * Returns 0 on success, -EINVAL if the parameters do not match any
499 int trace_set_clr_event(const char *system, const char *event, int set)
501 struct trace_array *tr = top_trace_array();
503 return __ftrace_set_clr_event(tr, NULL, system, event, set);
505 EXPORT_SYMBOL_GPL(trace_set_clr_event);
507 /* 128 should be much more than enough */
508 #define EVENT_BUF_SIZE 127
511 ftrace_event_write(struct file *file, const char __user *ubuf,
512 size_t cnt, loff_t *ppos)
514 struct trace_parser parser;
515 struct seq_file *m = file->private_data;
516 struct trace_array *tr = m->private;
522 ret = tracing_update_buffers();
526 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
529 read = trace_get_user(&parser, ubuf, cnt, ppos);
531 if (read >= 0 && trace_parser_loaded((&parser))) {
534 if (*parser.buffer == '!')
537 parser.buffer[parser.idx] = 0;
539 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
547 trace_parser_put(&parser);
553 t_next(struct seq_file *m, void *v, loff_t *pos)
555 struct ftrace_event_file *file = v;
556 struct ftrace_event_call *call;
557 struct trace_array *tr = m->private;
561 list_for_each_entry_continue(file, &tr->events, list) {
562 call = file->event_call;
564 * The ftrace subsystem is for showing formats only.
565 * They can not be enabled or disabled via the event files.
567 if (call->class && call->class->reg)
574 static void *t_start(struct seq_file *m, loff_t *pos)
576 struct ftrace_event_file *file;
577 struct trace_array *tr = m->private;
580 mutex_lock(&event_mutex);
582 file = list_entry(&tr->events, struct ftrace_event_file, list);
583 for (l = 0; l <= *pos; ) {
584 file = t_next(m, file, &l);
592 s_next(struct seq_file *m, void *v, loff_t *pos)
594 struct ftrace_event_file *file = v;
595 struct trace_array *tr = m->private;
599 list_for_each_entry_continue(file, &tr->events, list) {
600 if (file->flags & FTRACE_EVENT_FL_ENABLED)
607 static void *s_start(struct seq_file *m, loff_t *pos)
609 struct ftrace_event_file *file;
610 struct trace_array *tr = m->private;
613 mutex_lock(&event_mutex);
615 file = list_entry(&tr->events, struct ftrace_event_file, list);
616 for (l = 0; l <= *pos; ) {
617 file = s_next(m, file, &l);
624 static int t_show(struct seq_file *m, void *v)
626 struct ftrace_event_file *file = v;
627 struct ftrace_event_call *call = file->event_call;
629 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
630 seq_printf(m, "%s:", call->class->system);
631 seq_printf(m, "%s\n", call->name);
636 static void t_stop(struct seq_file *m, void *p)
638 mutex_unlock(&event_mutex);
642 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
645 struct ftrace_event_file *file = filp->private_data;
648 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
649 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)
651 else if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
658 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
662 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
665 struct ftrace_event_file *file = filp->private_data;
672 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
676 ret = tracing_update_buffers();
683 mutex_lock(&event_mutex);
684 ret = ftrace_event_enable_disable(file, val);
685 mutex_unlock(&event_mutex);
694 return ret ? ret : cnt;
698 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
701 const char set_to_char[4] = { '?', '0', '1', 'X' };
702 struct ftrace_subsystem_dir *dir = filp->private_data;
703 struct event_subsystem *system = dir->subsystem;
704 struct ftrace_event_call *call;
705 struct ftrace_event_file *file;
706 struct trace_array *tr = dir->tr;
711 mutex_lock(&event_mutex);
712 list_for_each_entry(file, &tr->events, list) {
713 call = file->event_call;
714 if (!call->name || !call->class || !call->class->reg)
717 if (system && strcmp(call->class->system, system->name) != 0)
721 * We need to find out if all the events are set
722 * or if all events or cleared, or if we have
725 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
728 * If we have a mixture, no need to look further.
733 mutex_unlock(&event_mutex);
735 buf[0] = set_to_char[set];
738 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
744 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
747 struct ftrace_subsystem_dir *dir = filp->private_data;
748 struct event_subsystem *system = dir->subsystem;
749 const char *name = NULL;
753 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
757 ret = tracing_update_buffers();
761 if (val != 0 && val != 1)
765 * Opening of "enable" adds a ref count to system,
766 * so the name is safe to use.
771 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
785 FORMAT_FIELD_SEPERATOR = 2,
789 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
791 struct ftrace_event_call *call = m->private;
792 struct ftrace_event_field *field;
793 struct list_head *common_head = &ftrace_common_fields;
794 struct list_head *head = trace_get_fields(call);
798 switch ((unsigned long)v) {
800 if (unlikely(list_empty(common_head)))
803 field = list_entry(common_head->prev,
804 struct ftrace_event_field, link);
807 case FORMAT_FIELD_SEPERATOR:
808 if (unlikely(list_empty(head)))
811 field = list_entry(head->prev, struct ftrace_event_field, link);
814 case FORMAT_PRINTFMT:
820 if (field->link.prev == common_head)
821 return (void *)FORMAT_FIELD_SEPERATOR;
822 else if (field->link.prev == head)
823 return (void *)FORMAT_PRINTFMT;
825 field = list_entry(field->link.prev, struct ftrace_event_field, link);
830 static void *f_start(struct seq_file *m, loff_t *pos)
835 /* Start by showing the header */
837 return (void *)FORMAT_HEADER;
839 p = (void *)FORMAT_HEADER;
841 p = f_next(m, p, &l);
842 } while (p && l < *pos);
847 static int f_show(struct seq_file *m, void *v)
849 struct ftrace_event_call *call = m->private;
850 struct ftrace_event_field *field;
851 const char *array_descriptor;
853 switch ((unsigned long)v) {
855 seq_printf(m, "name: %s\n", call->name);
856 seq_printf(m, "ID: %d\n", call->event.type);
857 seq_printf(m, "format:\n");
860 case FORMAT_FIELD_SEPERATOR:
864 case FORMAT_PRINTFMT:
865 seq_printf(m, "\nprint fmt: %s\n",
873 * Smartly shows the array type(except dynamic array).
876 * If TYPE := TYPE[LEN], it is shown:
877 * field:TYPE VAR[LEN]
879 array_descriptor = strchr(field->type, '[');
881 if (!strncmp(field->type, "__data_loc", 10))
882 array_descriptor = NULL;
884 if (!array_descriptor)
885 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
886 field->type, field->name, field->offset,
887 field->size, !!field->is_signed);
889 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
890 (int)(array_descriptor - field->type),
891 field->type, field->name,
892 array_descriptor, field->offset,
893 field->size, !!field->is_signed);
898 static void f_stop(struct seq_file *m, void *p)
902 static const struct seq_operations trace_format_seq_ops = {
909 static int trace_format_open(struct inode *inode, struct file *file)
911 struct ftrace_event_call *call = inode->i_private;
915 ret = seq_open(file, &trace_format_seq_ops);
919 m = file->private_data;
926 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
928 struct ftrace_event_call *call = filp->private_data;
935 s = kmalloc(sizeof(*s), GFP_KERNEL);
940 trace_seq_printf(s, "%d\n", call->event.type);
942 r = simple_read_from_buffer(ubuf, cnt, ppos,
949 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
952 struct ftrace_event_call *call = filp->private_data;
959 s = kmalloc(sizeof(*s), GFP_KERNEL);
965 print_event_filter(call, s);
966 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
974 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
977 struct ftrace_event_call *call = filp->private_data;
981 if (cnt >= PAGE_SIZE)
984 buf = (char *)__get_free_page(GFP_TEMPORARY);
988 if (copy_from_user(buf, ubuf, cnt)) {
989 free_page((unsigned long) buf);
994 err = apply_event_filter(call, buf);
995 free_page((unsigned long) buf);
1004 static LIST_HEAD(event_subsystems);
1006 static int subsystem_open(struct inode *inode, struct file *filp)
1008 struct event_subsystem *system = NULL;
1009 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1010 struct trace_array *tr;
1013 /* Make sure the system still exists */
1014 mutex_lock(&trace_types_lock);
1015 mutex_lock(&event_mutex);
1016 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1017 list_for_each_entry(dir, &tr->systems, list) {
1018 if (dir == inode->i_private) {
1019 /* Don't open systems with no events */
1020 if (dir->nr_events) {
1021 __get_system_dir(dir);
1022 system = dir->subsystem;
1029 mutex_unlock(&event_mutex);
1030 mutex_unlock(&trace_types_lock);
1035 /* Some versions of gcc think dir can be uninitialized here */
1038 ret = tracing_open_generic(inode, filp);
1045 static int system_tr_open(struct inode *inode, struct file *filp)
1047 struct ftrace_subsystem_dir *dir;
1048 struct trace_array *tr = inode->i_private;
1051 /* Make a temporary dir that has no system but points to tr */
1052 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1058 ret = tracing_open_generic(inode, filp);
1062 filp->private_data = dir;
1067 static int subsystem_release(struct inode *inode, struct file *file)
1069 struct ftrace_subsystem_dir *dir = file->private_data;
1072 * If dir->subsystem is NULL, then this is a temporary
1073 * descriptor that was made for a trace_array to enable
1085 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1088 struct ftrace_subsystem_dir *dir = filp->private_data;
1089 struct event_subsystem *system = dir->subsystem;
1090 struct trace_seq *s;
1096 s = kmalloc(sizeof(*s), GFP_KERNEL);
1102 print_subsystem_event_filter(system, s);
1103 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1111 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1114 struct ftrace_subsystem_dir *dir = filp->private_data;
1118 if (cnt >= PAGE_SIZE)
1121 buf = (char *)__get_free_page(GFP_TEMPORARY);
1125 if (copy_from_user(buf, ubuf, cnt)) {
1126 free_page((unsigned long) buf);
1131 err = apply_subsystem_event_filter(dir, buf);
1132 free_page((unsigned long) buf);
1142 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1144 int (*func)(struct trace_seq *s) = filp->private_data;
1145 struct trace_seq *s;
1151 s = kmalloc(sizeof(*s), GFP_KERNEL);
1158 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1165 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1166 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1168 static const struct seq_operations show_event_seq_ops = {
1175 static const struct seq_operations show_set_event_seq_ops = {
1182 static const struct file_operations ftrace_avail_fops = {
1183 .open = ftrace_event_avail_open,
1185 .llseek = seq_lseek,
1186 .release = seq_release,
1189 static const struct file_operations ftrace_set_event_fops = {
1190 .open = ftrace_event_set_open,
1192 .write = ftrace_event_write,
1193 .llseek = seq_lseek,
1194 .release = seq_release,
1197 static const struct file_operations ftrace_enable_fops = {
1198 .open = tracing_open_generic,
1199 .read = event_enable_read,
1200 .write = event_enable_write,
1201 .llseek = default_llseek,
1204 static const struct file_operations ftrace_event_format_fops = {
1205 .open = trace_format_open,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1211 static const struct file_operations ftrace_event_id_fops = {
1212 .open = tracing_open_generic,
1213 .read = event_id_read,
1214 .llseek = default_llseek,
1217 static const struct file_operations ftrace_event_filter_fops = {
1218 .open = tracing_open_generic,
1219 .read = event_filter_read,
1220 .write = event_filter_write,
1221 .llseek = default_llseek,
1224 static const struct file_operations ftrace_subsystem_filter_fops = {
1225 .open = subsystem_open,
1226 .read = subsystem_filter_read,
1227 .write = subsystem_filter_write,
1228 .llseek = default_llseek,
1229 .release = subsystem_release,
1232 static const struct file_operations ftrace_system_enable_fops = {
1233 .open = subsystem_open,
1234 .read = system_enable_read,
1235 .write = system_enable_write,
1236 .llseek = default_llseek,
1237 .release = subsystem_release,
1240 static const struct file_operations ftrace_tr_enable_fops = {
1241 .open = system_tr_open,
1242 .read = system_enable_read,
1243 .write = system_enable_write,
1244 .llseek = default_llseek,
1245 .release = subsystem_release,
1248 static const struct file_operations ftrace_show_header_fops = {
1249 .open = tracing_open_generic,
1250 .read = show_header,
1251 .llseek = default_llseek,
1255 ftrace_event_open(struct inode *inode, struct file *file,
1256 const struct seq_operations *seq_ops)
1261 ret = seq_open(file, seq_ops);
1264 m = file->private_data;
1265 /* copy tr over to seq ops */
1266 m->private = inode->i_private;
1272 ftrace_event_avail_open(struct inode *inode, struct file *file)
1274 const struct seq_operations *seq_ops = &show_event_seq_ops;
1276 return ftrace_event_open(inode, file, seq_ops);
1280 ftrace_event_set_open(struct inode *inode, struct file *file)
1282 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1283 struct trace_array *tr = inode->i_private;
1285 if ((file->f_mode & FMODE_WRITE) &&
1286 (file->f_flags & O_TRUNC))
1287 ftrace_clear_events(tr);
1289 return ftrace_event_open(inode, file, seq_ops);
1292 static struct event_subsystem *
1293 create_new_subsystem(const char *name)
1295 struct event_subsystem *system;
1297 /* need to create new entry */
1298 system = kmalloc(sizeof(*system), GFP_KERNEL);
1302 system->ref_count = 1;
1304 /* Only allocate if dynamic (kprobes and modules) */
1305 if (!core_kernel_data((unsigned long)name)) {
1306 system->ref_count |= SYSTEM_FL_FREE_NAME;
1307 system->name = kstrdup(name, GFP_KERNEL);
1311 system->name = name;
1313 system->filter = NULL;
1315 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1316 if (!system->filter)
1319 list_add(&system->list, &event_subsystems);
1324 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1325 kfree(system->name);
1330 static struct dentry *
1331 event_subsystem_dir(struct trace_array *tr, const char *name,
1332 struct ftrace_event_file *file, struct dentry *parent)
1334 struct ftrace_subsystem_dir *dir;
1335 struct event_subsystem *system;
1336 struct dentry *entry;
1338 /* First see if we did not already create this dir */
1339 list_for_each_entry(dir, &tr->systems, list) {
1340 system = dir->subsystem;
1341 if (strcmp(system->name, name) == 0) {
1348 /* Now see if the system itself exists. */
1349 list_for_each_entry(system, &event_subsystems, list) {
1350 if (strcmp(system->name, name) == 0)
1353 /* Reset system variable when not found */
1354 if (&system->list == &event_subsystems)
1357 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1362 system = create_new_subsystem(name);
1366 __get_system(system);
1368 dir->entry = debugfs_create_dir(name, parent);
1370 pr_warning("Failed to create system directory %s\n", name);
1371 __put_system(system);
1378 dir->subsystem = system;
1381 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1382 &ftrace_subsystem_filter_fops);
1384 kfree(system->filter);
1385 system->filter = NULL;
1386 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1389 trace_create_file("enable", 0644, dir->entry, dir,
1390 &ftrace_system_enable_fops);
1392 list_add(&dir->list, &tr->systems);
1399 /* Only print this message if failed on memory allocation */
1400 if (!dir || !system)
1401 pr_warning("No memory to create event subsystem %s\n",
1407 event_create_dir(struct dentry *parent,
1408 struct ftrace_event_file *file,
1409 const struct file_operations *id,
1410 const struct file_operations *enable,
1411 const struct file_operations *filter,
1412 const struct file_operations *format)
1414 struct ftrace_event_call *call = file->event_call;
1415 struct trace_array *tr = file->tr;
1416 struct list_head *head;
1417 struct dentry *d_events;
1421 * If the trace point header did not define TRACE_SYSTEM
1422 * then the system would be called "TRACE_SYSTEM".
1424 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1425 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1431 file->dir = debugfs_create_dir(call->name, d_events);
1433 pr_warning("Could not create debugfs '%s' directory\n",
1438 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1439 trace_create_file("enable", 0644, file->dir, file,
1442 #ifdef CONFIG_PERF_EVENTS
1443 if (call->event.type && call->class->reg)
1444 trace_create_file("id", 0444, file->dir, call,
1449 * Other events may have the same class. Only update
1450 * the fields if they are not already defined.
1452 head = trace_get_fields(call);
1453 if (list_empty(head)) {
1454 ret = call->class->define_fields(call);
1456 pr_warning("Could not initialize trace point"
1457 " events/%s\n", call->name);
1461 trace_create_file("filter", 0644, file->dir, call,
1464 trace_create_file("format", 0444, file->dir, call,
1470 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1475 if (!--dir->nr_events) {
1476 debugfs_remove_recursive(dir->entry);
1477 list_del(&dir->list);
1478 __put_system_dir(dir);
1482 static void remove_event_from_tracers(struct ftrace_event_call *call)
1484 struct ftrace_event_file *file;
1485 struct trace_array *tr;
1487 do_for_each_event_file_safe(tr, file) {
1489 if (file->event_call != call)
1492 list_del(&file->list);
1493 debugfs_remove_recursive(file->dir);
1494 remove_subsystem(file->system);
1495 kmem_cache_free(file_cachep, file);
1498 * The do_for_each_event_file_safe() is
1499 * a double loop. After finding the call for this
1500 * trace_array, we use break to jump to the next
1504 } while_for_each_event_file();
1507 static void event_remove(struct ftrace_event_call *call)
1509 struct trace_array *tr;
1510 struct ftrace_event_file *file;
1512 do_for_each_event_file(tr, file) {
1513 if (file->event_call != call)
1515 ftrace_event_enable_disable(file, 0);
1517 * The do_for_each_event_file() is
1518 * a double loop. After finding the call for this
1519 * trace_array, we use break to jump to the next
1523 } while_for_each_event_file();
1525 if (call->event.funcs)
1526 __unregister_ftrace_event(&call->event);
1527 remove_event_from_tracers(call);
1528 list_del(&call->list);
1531 static int event_init(struct ftrace_event_call *call)
1535 if (WARN_ON(!call->name))
1538 if (call->class->raw_init) {
1539 ret = call->class->raw_init(call);
1540 if (ret < 0 && ret != -ENOSYS)
1541 pr_warn("Could not initialize trace events/%s\n",
1549 __register_event(struct ftrace_event_call *call, struct module *mod)
1553 ret = event_init(call);
1557 list_add(&call->list, &ftrace_events);
1563 static struct ftrace_event_file *
1564 trace_create_new_event(struct ftrace_event_call *call,
1565 struct trace_array *tr)
1567 struct ftrace_event_file *file;
1569 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1573 file->event_call = call;
1575 atomic_set(&file->sm_ref, 0);
1576 list_add(&file->list, &tr->events);
1581 /* Add an event to a trace directory */
1583 __trace_add_new_event(struct ftrace_event_call *call,
1584 struct trace_array *tr,
1585 const struct file_operations *id,
1586 const struct file_operations *enable,
1587 const struct file_operations *filter,
1588 const struct file_operations *format)
1590 struct ftrace_event_file *file;
1592 file = trace_create_new_event(call, tr);
1596 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1600 * Just create a decriptor for early init. A descriptor is required
1601 * for enabling events at boot. We want to enable events before
1602 * the filesystem is initialized.
1605 __trace_early_add_new_event(struct ftrace_event_call *call,
1606 struct trace_array *tr)
1608 struct ftrace_event_file *file;
1610 file = trace_create_new_event(call, tr);
1617 struct ftrace_module_file_ops;
1618 static void __add_event_to_tracers(struct ftrace_event_call *call,
1619 struct ftrace_module_file_ops *file_ops);
1621 /* Add an additional event_call dynamically */
1622 int trace_add_event_call(struct ftrace_event_call *call)
1625 mutex_lock(&trace_types_lock);
1626 mutex_lock(&event_mutex);
1628 ret = __register_event(call, NULL);
1630 __add_event_to_tracers(call, NULL);
1632 mutex_unlock(&event_mutex);
1633 mutex_unlock(&trace_types_lock);
1638 * Must be called under locking of trace_types_lock, event_mutex and
1641 static void __trace_remove_event_call(struct ftrace_event_call *call)
1644 trace_destroy_fields(call);
1645 destroy_preds(call);
1648 /* Remove an event_call */
1649 void trace_remove_event_call(struct ftrace_event_call *call)
1651 mutex_lock(&trace_types_lock);
1652 mutex_lock(&event_mutex);
1653 down_write(&trace_event_sem);
1654 __trace_remove_event_call(call);
1655 up_write(&trace_event_sem);
1656 mutex_unlock(&event_mutex);
1657 mutex_unlock(&trace_types_lock);
1660 #define for_each_event(event, start, end) \
1661 for (event = start; \
1662 (unsigned long)event < (unsigned long)end; \
1665 #ifdef CONFIG_MODULES
1667 static LIST_HEAD(ftrace_module_file_list);
1670 * Modules must own their file_operations to keep up with
1671 * reference counting.
1673 struct ftrace_module_file_ops {
1674 struct list_head list;
1676 struct file_operations id;
1677 struct file_operations enable;
1678 struct file_operations format;
1679 struct file_operations filter;
1682 static struct ftrace_module_file_ops *
1683 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1686 * As event_calls are added in groups by module,
1687 * when we find one file_ops, we don't need to search for
1688 * each call in that module, as the rest should be the
1689 * same. Only search for a new one if the last one did
1692 if (file_ops && mod == file_ops->mod)
1695 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1696 if (file_ops->mod == mod)
1702 static struct ftrace_module_file_ops *
1703 trace_create_file_ops(struct module *mod)
1705 struct ftrace_module_file_ops *file_ops;
1708 * This is a bit of a PITA. To allow for correct reference
1709 * counting, modules must "own" their file_operations.
1710 * To do this, we allocate the file operations that will be
1711 * used in the event directory.
1714 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1718 file_ops->mod = mod;
1720 file_ops->id = ftrace_event_id_fops;
1721 file_ops->id.owner = mod;
1723 file_ops->enable = ftrace_enable_fops;
1724 file_ops->enable.owner = mod;
1726 file_ops->filter = ftrace_event_filter_fops;
1727 file_ops->filter.owner = mod;
1729 file_ops->format = ftrace_event_format_fops;
1730 file_ops->format.owner = mod;
1732 list_add(&file_ops->list, &ftrace_module_file_list);
1737 static void trace_module_add_events(struct module *mod)
1739 struct ftrace_module_file_ops *file_ops = NULL;
1740 struct ftrace_event_call **call, **start, **end;
1742 start = mod->trace_events;
1743 end = mod->trace_events + mod->num_trace_events;
1748 file_ops = trace_create_file_ops(mod);
1752 for_each_event(call, start, end) {
1753 __register_event(*call, mod);
1754 __add_event_to_tracers(*call, file_ops);
1758 static void trace_module_remove_events(struct module *mod)
1760 struct ftrace_module_file_ops *file_ops;
1761 struct ftrace_event_call *call, *p;
1762 bool clear_trace = false;
1764 down_write(&trace_event_sem);
1765 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1766 if (call->mod == mod) {
1767 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1769 __trace_remove_event_call(call);
1773 /* Now free the file_operations */
1774 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1775 if (file_ops->mod == mod)
1778 if (&file_ops->list != &ftrace_module_file_list) {
1779 list_del(&file_ops->list);
1782 up_write(&trace_event_sem);
1785 * It is safest to reset the ring buffer if the module being unloaded
1786 * registered any events that were used. The only worry is if
1787 * a new module gets loaded, and takes on the same id as the events
1788 * of this module. When printing out the buffer, traced events left
1789 * over from this module may be passed to the new module events and
1790 * unexpected results may occur.
1793 tracing_reset_all_online_cpus();
1796 static int trace_module_notify(struct notifier_block *self,
1797 unsigned long val, void *data)
1799 struct module *mod = data;
1801 mutex_lock(&trace_types_lock);
1802 mutex_lock(&event_mutex);
1804 case MODULE_STATE_COMING:
1805 trace_module_add_events(mod);
1807 case MODULE_STATE_GOING:
1808 trace_module_remove_events(mod);
1811 mutex_unlock(&event_mutex);
1812 mutex_unlock(&trace_types_lock);
1818 __trace_add_new_mod_event(struct ftrace_event_call *call,
1819 struct trace_array *tr,
1820 struct ftrace_module_file_ops *file_ops)
1822 return __trace_add_new_event(call, tr,
1823 &file_ops->id, &file_ops->enable,
1824 &file_ops->filter, &file_ops->format);
1828 static inline struct ftrace_module_file_ops *
1829 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1833 static inline int trace_module_notify(struct notifier_block *self,
1834 unsigned long val, void *data)
1839 __trace_add_new_mod_event(struct ftrace_event_call *call,
1840 struct trace_array *tr,
1841 struct ftrace_module_file_ops *file_ops)
1845 #endif /* CONFIG_MODULES */
1847 /* Create a new event directory structure for a trace directory. */
1849 __trace_add_event_dirs(struct trace_array *tr)
1851 struct ftrace_module_file_ops *file_ops = NULL;
1852 struct ftrace_event_call *call;
1855 list_for_each_entry(call, &ftrace_events, list) {
1858 * Directories for events by modules need to
1859 * keep module ref counts when opened (as we don't
1860 * want the module to disappear when reading one
1861 * of these files). The file_ops keep account of
1862 * the module ref count.
1864 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1866 continue; /* Warn? */
1867 ret = __trace_add_new_mod_event(call, tr, file_ops);
1869 pr_warning("Could not create directory for event %s\n",
1873 ret = __trace_add_new_event(call, tr,
1874 &ftrace_event_id_fops,
1875 &ftrace_enable_fops,
1876 &ftrace_event_filter_fops,
1877 &ftrace_event_format_fops);
1879 pr_warning("Could not create directory for event %s\n",
1884 #ifdef CONFIG_DYNAMIC_FTRACE
1887 #define ENABLE_EVENT_STR "enable_event"
1888 #define DISABLE_EVENT_STR "disable_event"
1890 struct event_probe_data {
1891 struct ftrace_event_file *file;
1892 unsigned long count;
1897 static struct ftrace_event_file *
1898 find_event_file(struct trace_array *tr, const char *system, const char *event)
1900 struct ftrace_event_file *file;
1901 struct ftrace_event_call *call;
1903 list_for_each_entry(file, &tr->events, list) {
1905 call = file->event_call;
1907 if (!call->name || !call->class || !call->class->reg)
1910 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1913 if (strcmp(event, call->name) == 0 &&
1914 strcmp(system, call->class->system) == 0)
1921 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1923 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1924 struct event_probe_data *data = *pdata;
1930 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1932 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1936 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1938 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1939 struct event_probe_data *data = *pdata;
1947 /* Skip if the event is in a state we want to switch to */
1948 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1951 if (data->count != -1)
1954 event_enable_probe(ip, parent_ip, _data);
1958 event_enable_print(struct seq_file *m, unsigned long ip,
1959 struct ftrace_probe_ops *ops, void *_data)
1961 struct event_probe_data *data = _data;
1963 seq_printf(m, "%ps:", (void *)ip);
1965 seq_printf(m, "%s:%s:%s",
1966 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1967 data->file->event_call->class->system,
1968 data->file->event_call->name);
1970 if (data->count == -1)
1971 seq_printf(m, ":unlimited\n");
1973 seq_printf(m, ":count=%ld\n", data->count);
1979 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1982 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1983 struct event_probe_data *data = *pdata;
1990 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1993 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1994 struct event_probe_data *data = *pdata;
1996 if (WARN_ON_ONCE(data->ref <= 0))
2001 /* Remove the SOFT_MODE flag */
2002 __ftrace_event_enable_disable(data->file, 0, 1);
2003 module_put(data->file->event_call->mod);
2009 static struct ftrace_probe_ops event_enable_probe_ops = {
2010 .func = event_enable_probe,
2011 .print = event_enable_print,
2012 .init = event_enable_init,
2013 .free = event_enable_free,
2016 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2017 .func = event_enable_count_probe,
2018 .print = event_enable_print,
2019 .init = event_enable_init,
2020 .free = event_enable_free,
2023 static struct ftrace_probe_ops event_disable_probe_ops = {
2024 .func = event_enable_probe,
2025 .print = event_enable_print,
2026 .init = event_enable_init,
2027 .free = event_enable_free,
2030 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2031 .func = event_enable_count_probe,
2032 .print = event_enable_print,
2033 .init = event_enable_init,
2034 .free = event_enable_free,
2038 event_enable_func(struct ftrace_hash *hash,
2039 char *glob, char *cmd, char *param, int enabled)
2041 struct trace_array *tr = top_trace_array();
2042 struct ftrace_event_file *file;
2043 struct ftrace_probe_ops *ops;
2044 struct event_probe_data *data;
2051 /* hash funcs only work with set_ftrace_filter */
2058 system = strsep(¶m, ":");
2062 event = strsep(¶m, ":");
2064 mutex_lock(&event_mutex);
2067 file = find_event_file(tr, system, event);
2071 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2074 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2076 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2078 if (glob[0] == '!') {
2079 unregister_ftrace_function_probe_func(glob+1, ops);
2085 data = kzalloc(sizeof(*data), GFP_KERNEL);
2089 data->enable = enable;
2096 number = strsep(¶m, ":");
2099 if (!strlen(number))
2103 * We use the callback data field (which is a pointer)
2106 ret = kstrtoul(number, 0, &data->count);
2111 /* Don't let event modules unload while probe registered */
2112 ret = try_module_get(file->event_call->mod);
2118 ret = __ftrace_event_enable_disable(file, 1, 1);
2121 ret = register_ftrace_function_probe(glob, ops, data);
2123 * The above returns on success the # of functions enabled,
2124 * but if it didn't find any functions it returns zero.
2125 * Consider no functions a failure too.
2132 /* Just return zero, not the number of enabled functions */
2135 mutex_unlock(&event_mutex);
2139 __ftrace_event_enable_disable(file, 0, 1);
2141 module_put(file->event_call->mod);
2147 static struct ftrace_func_command event_enable_cmd = {
2148 .name = ENABLE_EVENT_STR,
2149 .func = event_enable_func,
2152 static struct ftrace_func_command event_disable_cmd = {
2153 .name = DISABLE_EVENT_STR,
2154 .func = event_enable_func,
2157 static __init int register_event_cmds(void)
2161 ret = register_ftrace_command(&event_enable_cmd);
2162 if (WARN_ON(ret < 0))
2164 ret = register_ftrace_command(&event_disable_cmd);
2165 if (WARN_ON(ret < 0))
2166 unregister_ftrace_command(&event_enable_cmd);
2170 static inline int register_event_cmds(void) { return 0; }
2171 #endif /* CONFIG_DYNAMIC_FTRACE */
2174 * The top level array has already had its ftrace_event_file
2175 * descriptors created in order to allow for early events to
2176 * be recorded. This function is called after the debugfs has been
2177 * initialized, and we now have to create the files associated
2181 __trace_early_add_event_dirs(struct trace_array *tr)
2183 struct ftrace_event_file *file;
2187 list_for_each_entry(file, &tr->events, list) {
2188 ret = event_create_dir(tr->event_dir, file,
2189 &ftrace_event_id_fops,
2190 &ftrace_enable_fops,
2191 &ftrace_event_filter_fops,
2192 &ftrace_event_format_fops);
2194 pr_warning("Could not create directory for event %s\n",
2195 file->event_call->name);
2200 * For early boot up, the top trace array requires to have
2201 * a list of events that can be enabled. This must be done before
2202 * the filesystem is set up in order to allow events to be traced
2206 __trace_early_add_events(struct trace_array *tr)
2208 struct ftrace_event_call *call;
2211 list_for_each_entry(call, &ftrace_events, list) {
2212 /* Early boot up should not have any modules loaded */
2213 if (WARN_ON_ONCE(call->mod))
2216 ret = __trace_early_add_new_event(call, tr);
2218 pr_warning("Could not create early event %s\n",
2223 /* Remove the event directory structure for a trace directory. */
2225 __trace_remove_event_dirs(struct trace_array *tr)
2227 struct ftrace_event_file *file, *next;
2229 list_for_each_entry_safe(file, next, &tr->events, list) {
2230 list_del(&file->list);
2231 debugfs_remove_recursive(file->dir);
2232 remove_subsystem(file->system);
2233 kmem_cache_free(file_cachep, file);
2238 __add_event_to_tracers(struct ftrace_event_call *call,
2239 struct ftrace_module_file_ops *file_ops)
2241 struct trace_array *tr;
2243 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2245 __trace_add_new_mod_event(call, tr, file_ops);
2247 __trace_add_new_event(call, tr,
2248 &ftrace_event_id_fops,
2249 &ftrace_enable_fops,
2250 &ftrace_event_filter_fops,
2251 &ftrace_event_format_fops);
2255 static struct notifier_block trace_module_nb = {
2256 .notifier_call = trace_module_notify,
2260 extern struct ftrace_event_call *__start_ftrace_events[];
2261 extern struct ftrace_event_call *__stop_ftrace_events[];
2263 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2265 static __init int setup_trace_event(char *str)
2267 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2268 ring_buffer_expanded = true;
2269 tracing_selftest_disabled = true;
2273 __setup("trace_event=", setup_trace_event);
2275 /* Expects to have event_mutex held when called */
2277 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2279 struct dentry *d_events;
2280 struct dentry *entry;
2282 entry = debugfs_create_file("set_event", 0644, parent,
2283 tr, &ftrace_set_event_fops);
2285 pr_warning("Could not create debugfs 'set_event' entry\n");
2289 d_events = debugfs_create_dir("events", parent);
2291 pr_warning("Could not create debugfs 'events' directory\n");
2295 /* ring buffer internal formats */
2296 trace_create_file("header_page", 0444, d_events,
2297 ring_buffer_print_page_header,
2298 &ftrace_show_header_fops);
2300 trace_create_file("header_event", 0444, d_events,
2301 ring_buffer_print_entry_header,
2302 &ftrace_show_header_fops);
2304 trace_create_file("enable", 0644, d_events,
2305 tr, &ftrace_tr_enable_fops);
2307 tr->event_dir = d_events;
2313 * event_trace_add_tracer - add a instance of a trace_array to events
2314 * @parent: The parent dentry to place the files/directories for events in
2315 * @tr: The trace array associated with these events
2317 * When a new instance is created, it needs to set up its events
2318 * directory, as well as other files associated with events. It also
2319 * creates the event hierachry in the @parent/events directory.
2321 * Returns 0 on success.
2323 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2327 mutex_lock(&event_mutex);
2329 ret = create_event_toplevel_files(parent, tr);
2333 down_write(&trace_event_sem);
2334 __trace_add_event_dirs(tr);
2335 up_write(&trace_event_sem);
2338 mutex_unlock(&event_mutex);
2344 * The top trace array already had its file descriptors created.
2345 * Now the files themselves need to be created.
2348 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2352 mutex_lock(&event_mutex);
2354 ret = create_event_toplevel_files(parent, tr);
2358 down_write(&trace_event_sem);
2359 __trace_early_add_event_dirs(tr);
2360 up_write(&trace_event_sem);
2363 mutex_unlock(&event_mutex);
2368 int event_trace_del_tracer(struct trace_array *tr)
2370 /* Disable any running events */
2371 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2373 mutex_lock(&event_mutex);
2375 down_write(&trace_event_sem);
2376 __trace_remove_event_dirs(tr);
2377 debugfs_remove_recursive(tr->event_dir);
2378 up_write(&trace_event_sem);
2380 tr->event_dir = NULL;
2382 mutex_unlock(&event_mutex);
2387 static __init int event_trace_memsetup(void)
2389 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2390 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2394 static __init int event_trace_enable(void)
2396 struct trace_array *tr = top_trace_array();
2397 struct ftrace_event_call **iter, *call;
2398 char *buf = bootup_event_buf;
2402 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2405 ret = event_init(call);
2407 list_add(&call->list, &ftrace_events);
2411 * We need the top trace array to have a working set of trace
2412 * points at early init, before the debug files and directories
2413 * are created. Create the file entries now, and attach them
2414 * to the actual file dentries later.
2416 __trace_early_add_events(tr);
2419 token = strsep(&buf, ",");
2426 ret = ftrace_set_clr_event(tr, token, 1);
2428 pr_warn("Failed to enable trace event: %s\n", token);
2431 trace_printk_start_comm();
2433 register_event_cmds();
2438 static __init int event_trace_init(void)
2440 struct trace_array *tr;
2441 struct dentry *d_tracer;
2442 struct dentry *entry;
2445 tr = top_trace_array();
2447 d_tracer = tracing_init_dentry();
2451 entry = debugfs_create_file("available_events", 0444, d_tracer,
2452 tr, &ftrace_avail_fops);
2454 pr_warning("Could not create debugfs "
2455 "'available_events' entry\n");
2457 if (trace_define_common_fields())
2458 pr_warning("tracing: Failed to allocate common fields");
2460 ret = early_event_add_tracer(d_tracer, tr);
2464 ret = register_module_notifier(&trace_module_nb);
2466 pr_warning("Failed to register trace events module notifier\n");
2470 early_initcall(event_trace_memsetup);
2471 core_initcall(event_trace_enable);
2472 fs_initcall(event_trace_init);
2474 #ifdef CONFIG_FTRACE_STARTUP_TEST
2476 static DEFINE_SPINLOCK(test_spinlock);
2477 static DEFINE_SPINLOCK(test_spinlock_irq);
2478 static DEFINE_MUTEX(test_mutex);
2480 static __init void test_work(struct work_struct *dummy)
2482 spin_lock(&test_spinlock);
2483 spin_lock_irq(&test_spinlock_irq);
2485 spin_unlock_irq(&test_spinlock_irq);
2486 spin_unlock(&test_spinlock);
2488 mutex_lock(&test_mutex);
2490 mutex_unlock(&test_mutex);
2493 static __init int event_test_thread(void *unused)
2497 test_malloc = kmalloc(1234, GFP_KERNEL);
2499 pr_info("failed to kmalloc\n");
2501 schedule_on_each_cpu(test_work);
2505 set_current_state(TASK_INTERRUPTIBLE);
2506 while (!kthread_should_stop())
2513 * Do various things that may trigger events.
2515 static __init void event_test_stuff(void)
2517 struct task_struct *test_thread;
2519 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2521 kthread_stop(test_thread);
2525 * For every trace event defined, we will test each trace point separately,
2526 * and then by groups, and finally all trace points.
2528 static __init void event_trace_self_tests(void)
2530 struct ftrace_subsystem_dir *dir;
2531 struct ftrace_event_file *file;
2532 struct ftrace_event_call *call;
2533 struct event_subsystem *system;
2534 struct trace_array *tr;
2537 tr = top_trace_array();
2539 pr_info("Running tests on trace events:\n");
2541 list_for_each_entry(file, &tr->events, list) {
2543 call = file->event_call;
2545 /* Only test those that have a probe */
2546 if (!call->class || !call->class->probe)
2550 * Testing syscall events here is pretty useless, but
2551 * we still do it if configured. But this is time consuming.
2552 * What we really need is a user thread to perform the
2553 * syscalls as we test.
2555 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2556 if (call->class->system &&
2557 strcmp(call->class->system, "syscalls") == 0)
2561 pr_info("Testing event %s: ", call->name);
2564 * If an event is already enabled, someone is using
2565 * it and the self test should not be on.
2567 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2568 pr_warning("Enabled event during self test!\n");
2573 ftrace_event_enable_disable(file, 1);
2575 ftrace_event_enable_disable(file, 0);
2580 /* Now test at the sub system level */
2582 pr_info("Running tests on trace event systems:\n");
2584 list_for_each_entry(dir, &tr->systems, list) {
2586 system = dir->subsystem;
2588 /* the ftrace system is special, skip it */
2589 if (strcmp(system->name, "ftrace") == 0)
2592 pr_info("Testing event system %s: ", system->name);
2594 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2595 if (WARN_ON_ONCE(ret)) {
2596 pr_warning("error enabling system %s\n",
2603 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2604 if (WARN_ON_ONCE(ret)) {
2605 pr_warning("error disabling system %s\n",
2613 /* Test with all events enabled */
2615 pr_info("Running tests on all trace events:\n");
2616 pr_info("Testing all events: ");
2618 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2619 if (WARN_ON_ONCE(ret)) {
2620 pr_warning("error enabling all events\n");
2627 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2628 if (WARN_ON_ONCE(ret)) {
2629 pr_warning("error disabling all events\n");
2636 #ifdef CONFIG_FUNCTION_TRACER
2638 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2641 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2642 struct ftrace_ops *op, struct pt_regs *pt_regs)
2644 struct ring_buffer_event *event;
2645 struct ring_buffer *buffer;
2646 struct ftrace_entry *entry;
2647 unsigned long flags;
2652 pc = preempt_count();
2653 preempt_disable_notrace();
2654 cpu = raw_smp_processor_id();
2655 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2660 local_save_flags(flags);
2662 event = trace_current_buffer_lock_reserve(&buffer,
2663 TRACE_FN, sizeof(*entry),
2667 entry = ring_buffer_event_data(event);
2669 entry->parent_ip = parent_ip;
2671 trace_buffer_unlock_commit(buffer, event, flags, pc);
2674 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2675 preempt_enable_notrace();
2678 static struct ftrace_ops trace_ops __initdata =
2680 .func = function_test_events_call,
2681 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2684 static __init void event_trace_self_test_with_function(void)
2687 ret = register_ftrace_function(&trace_ops);
2688 if (WARN_ON(ret < 0)) {
2689 pr_info("Failed to enable function tracer for event tests\n");
2692 pr_info("Running tests again, along with the function tracer\n");
2693 event_trace_self_tests();
2694 unregister_ftrace_function(&trace_ops);
2697 static __init void event_trace_self_test_with_function(void)
2702 static __init int event_trace_self_tests_init(void)
2704 if (!tracing_selftest_disabled) {
2705 event_trace_self_tests();
2706 event_trace_self_test_with_function();
2712 late_initcall(event_trace_self_tests_init);