4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
31 static LIST_HEAD(ftrace_common_fields);
33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
35 static struct kmem_cache *field_cachep;
36 static struct kmem_cache *file_cachep;
38 #define SYSTEM_FL_FREE_NAME (1 << 31)
40 static inline int system_refcount(struct event_subsystem *system)
42 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
45 static int system_refcount_inc(struct event_subsystem *system)
47 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
50 static int system_refcount_dec(struct event_subsystem *system)
52 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
55 /* Double loops, do not use break, only goto's work */
56 #define do_for_each_event_file(tr, file) \
57 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 list_for_each_entry(file, &tr->events, list)
60 #define do_for_each_event_file_safe(tr, file) \
61 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 struct ftrace_event_file *___n; \
63 list_for_each_entry_safe(file, ___n, &tr->events, list)
65 #define while_for_each_event_file() \
68 static struct list_head *
69 trace_get_fields(struct ftrace_event_call *event_call)
71 if (!event_call->class->get_fields)
72 return &event_call->class->fields;
73 return event_call->class->get_fields(event_call);
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
79 struct ftrace_event_field *field;
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
89 struct ftrace_event_field *
90 trace_find_event_field(struct ftrace_event_call *call, char *name)
92 struct ftrace_event_field *field;
93 struct list_head *head;
95 field = __find_event_field(&ftrace_common_fields, name);
99 head = trace_get_fields(call);
100 return __find_event_field(head, name);
103 static int __trace_define_field(struct list_head *head, const char *type,
104 const char *name, int offset, int size,
105 int is_signed, int filter_type)
107 struct ftrace_event_field *field;
109 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
116 if (filter_type == FILTER_OTHER)
117 field->filter_type = filter_assign_type(type);
119 field->filter_type = filter_type;
121 field->offset = offset;
123 field->is_signed = is_signed;
125 list_add(&field->link, head);
130 int trace_define_field(struct ftrace_event_call *call, const char *type,
131 const char *name, int offset, int size, int is_signed,
134 struct list_head *head;
136 if (WARN_ON(!call->class))
139 head = trace_get_fields(call);
140 return __trace_define_field(head, type, name, offset, size,
141 is_signed, filter_type);
143 EXPORT_SYMBOL_GPL(trace_define_field);
145 #define __common_field(type, item) \
146 ret = __trace_define_field(&ftrace_common_fields, #type, \
148 offsetof(typeof(ent), item), \
150 is_signed_type(type), FILTER_OTHER); \
154 static int trace_define_common_fields(void)
157 struct trace_entry ent;
159 __common_field(unsigned short, type);
160 __common_field(unsigned char, flags);
161 __common_field(unsigned char, preempt_count);
162 __common_field(int, pid);
167 static void trace_destroy_fields(struct ftrace_event_call *call)
169 struct ftrace_event_field *field, *next;
170 struct list_head *head;
172 head = trace_get_fields(call);
173 list_for_each_entry_safe(field, next, head, link) {
174 list_del(&field->link);
175 kmem_cache_free(field_cachep, field);
179 int trace_event_raw_init(struct ftrace_event_call *call)
183 id = register_ftrace_event(&call->event);
189 EXPORT_SYMBOL_GPL(trace_event_raw_init);
191 int ftrace_event_reg(struct ftrace_event_call *call,
192 enum trace_reg type, void *data)
194 struct ftrace_event_file *file = data;
197 case TRACE_REG_REGISTER:
198 return tracepoint_probe_register(call->name,
201 case TRACE_REG_UNREGISTER:
202 tracepoint_probe_unregister(call->name,
207 #ifdef CONFIG_PERF_EVENTS
208 case TRACE_REG_PERF_REGISTER:
209 return tracepoint_probe_register(call->name,
210 call->class->perf_probe,
212 case TRACE_REG_PERF_UNREGISTER:
213 tracepoint_probe_unregister(call->name,
214 call->class->perf_probe,
217 case TRACE_REG_PERF_OPEN:
218 case TRACE_REG_PERF_CLOSE:
219 case TRACE_REG_PERF_ADD:
220 case TRACE_REG_PERF_DEL:
226 EXPORT_SYMBOL_GPL(ftrace_event_reg);
228 void trace_event_enable_cmd_record(bool enable)
230 struct ftrace_event_file *file;
231 struct trace_array *tr;
233 mutex_lock(&event_mutex);
234 do_for_each_event_file(tr, file) {
236 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
240 tracing_start_cmdline_record();
241 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
243 tracing_stop_cmdline_record();
244 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
246 } while_for_each_event_file();
247 mutex_unlock(&event_mutex);
250 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
251 int enable, int soft_disable)
253 struct ftrace_event_call *call = file->event_call;
260 * When soft_disable is set and enable is cleared, the sm_ref
261 * reference counter is decremented. If it reaches 0, we want
262 * to clear the SOFT_DISABLED flag but leave the event in the
263 * state that it was. That is, if the event was enabled and
264 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265 * is set we do not want the event to be enabled before we
268 * When soft_disable is not set but the SOFT_MODE flag is,
269 * we do nothing. Do not disable the tracepoint, otherwise
270 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
273 if (atomic_dec_return(&file->sm_ref) > 0)
275 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
276 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
278 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
280 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
281 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
282 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
283 tracing_stop_cmdline_record();
284 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
286 call->class->reg(call, TRACE_REG_UNREGISTER, file);
288 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
289 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
290 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
294 * When soft_disable is set and enable is set, we want to
295 * register the tracepoint for the event, but leave the event
296 * as is. That means, if the event was already enabled, we do
297 * nothing (but set SOFT_MODE). If the event is disabled, we
298 * set SOFT_DISABLED before enabling the event tracepoint, so
299 * it still seems to be disabled.
302 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
304 if (atomic_inc_return(&file->sm_ref) > 1)
306 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
309 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
311 /* Keep the event disabled, when going to SOFT_MODE. */
313 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
315 if (trace_flags & TRACE_ITER_RECORD_CMD) {
316 tracing_start_cmdline_record();
317 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
319 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
321 tracing_stop_cmdline_record();
322 pr_info("event trace: Could not enable event "
326 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
328 /* WAS_ENABLED gets set but never cleared. */
329 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
337 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
340 return __ftrace_event_enable_disable(file, enable, 0);
343 static void ftrace_clear_events(struct trace_array *tr)
345 struct ftrace_event_file *file;
347 mutex_lock(&event_mutex);
348 list_for_each_entry(file, &tr->events, list) {
349 ftrace_event_enable_disable(file, 0);
351 mutex_unlock(&event_mutex);
354 static void __put_system(struct event_subsystem *system)
356 struct event_filter *filter = system->filter;
358 WARN_ON_ONCE(system_refcount(system) == 0);
359 if (system_refcount_dec(system))
362 list_del(&system->list);
365 kfree(filter->filter_string);
368 if (system->ref_count & SYSTEM_FL_FREE_NAME)
373 static void __get_system(struct event_subsystem *system)
375 WARN_ON_ONCE(system_refcount(system) == 0);
376 system_refcount_inc(system);
379 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
381 WARN_ON_ONCE(dir->ref_count == 0);
383 __get_system(dir->subsystem);
386 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
388 WARN_ON_ONCE(dir->ref_count == 0);
389 /* If the subsystem is about to be freed, the dir must be too */
390 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
392 __put_system(dir->subsystem);
393 if (!--dir->ref_count)
397 static void put_system(struct ftrace_subsystem_dir *dir)
399 mutex_lock(&event_mutex);
400 __put_system_dir(dir);
401 mutex_unlock(&event_mutex);
404 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
409 if (!--dir->nr_events) {
410 debugfs_remove_recursive(dir->entry);
411 list_del(&dir->list);
412 __put_system_dir(dir);
416 static void *event_file_data(struct file *filp)
418 return ACCESS_ONCE(file_inode(filp)->i_private);
421 static void remove_event_file_dir(struct ftrace_event_file *file)
423 struct dentry *dir = file->dir;
424 struct dentry *child;
427 spin_lock(&dir->d_lock); /* probably unneeded */
428 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
429 if (child->d_inode) /* probably unneeded */
430 child->d_inode->i_private = NULL;
432 spin_unlock(&dir->d_lock);
434 debugfs_remove_recursive(dir);
437 list_del(&file->list);
438 remove_subsystem(file->system);
439 kmem_cache_free(file_cachep, file);
443 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
446 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
447 const char *sub, const char *event, int set)
449 struct ftrace_event_file *file;
450 struct ftrace_event_call *call;
453 list_for_each_entry(file, &tr->events, list) {
455 call = file->event_call;
457 if (!call->name || !call->class || !call->class->reg)
460 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
464 strcmp(match, call->name) != 0 &&
465 strcmp(match, call->class->system) != 0)
468 if (sub && strcmp(sub, call->class->system) != 0)
471 if (event && strcmp(event, call->name) != 0)
474 ftrace_event_enable_disable(file, set);
482 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
483 const char *sub, const char *event, int set)
487 mutex_lock(&event_mutex);
488 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
489 mutex_unlock(&event_mutex);
494 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
496 char *event = NULL, *sub = NULL, *match;
499 * The buf format can be <subsystem>:<event-name>
500 * *:<event-name> means any event by that name.
501 * :<event-name> is the same.
503 * <subsystem>:* means all events in that subsystem
504 * <subsystem>: means the same.
506 * <name> (no ':') means all events in a subsystem with
507 * the name <name> or any event that matches <name>
510 match = strsep(&buf, ":");
516 if (!strlen(sub) || strcmp(sub, "*") == 0)
518 if (!strlen(event) || strcmp(event, "*") == 0)
522 return __ftrace_set_clr_event(tr, match, sub, event, set);
526 * trace_set_clr_event - enable or disable an event
527 * @system: system name to match (NULL for any system)
528 * @event: event name to match (NULL for all events, within system)
529 * @set: 1 to enable, 0 to disable
531 * This is a way for other parts of the kernel to enable or disable
534 * Returns 0 on success, -EINVAL if the parameters do not match any
537 int trace_set_clr_event(const char *system, const char *event, int set)
539 struct trace_array *tr = top_trace_array();
541 return __ftrace_set_clr_event(tr, NULL, system, event, set);
543 EXPORT_SYMBOL_GPL(trace_set_clr_event);
545 /* 128 should be much more than enough */
546 #define EVENT_BUF_SIZE 127
549 ftrace_event_write(struct file *file, const char __user *ubuf,
550 size_t cnt, loff_t *ppos)
552 struct trace_parser parser;
553 struct seq_file *m = file->private_data;
554 struct trace_array *tr = m->private;
560 ret = tracing_update_buffers();
564 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
567 read = trace_get_user(&parser, ubuf, cnt, ppos);
569 if (read >= 0 && trace_parser_loaded((&parser))) {
572 if (*parser.buffer == '!')
575 parser.buffer[parser.idx] = 0;
577 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
585 trace_parser_put(&parser);
591 t_next(struct seq_file *m, void *v, loff_t *pos)
593 struct ftrace_event_file *file = v;
594 struct ftrace_event_call *call;
595 struct trace_array *tr = m->private;
599 list_for_each_entry_continue(file, &tr->events, list) {
600 call = file->event_call;
602 * The ftrace subsystem is for showing formats only.
603 * They can not be enabled or disabled via the event files.
605 if (call->class && call->class->reg)
612 static void *t_start(struct seq_file *m, loff_t *pos)
614 struct ftrace_event_file *file;
615 struct trace_array *tr = m->private;
618 mutex_lock(&event_mutex);
620 file = list_entry(&tr->events, struct ftrace_event_file, list);
621 for (l = 0; l <= *pos; ) {
622 file = t_next(m, file, &l);
630 s_next(struct seq_file *m, void *v, loff_t *pos)
632 struct ftrace_event_file *file = v;
633 struct trace_array *tr = m->private;
637 list_for_each_entry_continue(file, &tr->events, list) {
638 if (file->flags & FTRACE_EVENT_FL_ENABLED)
645 static void *s_start(struct seq_file *m, loff_t *pos)
647 struct ftrace_event_file *file;
648 struct trace_array *tr = m->private;
651 mutex_lock(&event_mutex);
653 file = list_entry(&tr->events, struct ftrace_event_file, list);
654 for (l = 0; l <= *pos; ) {
655 file = s_next(m, file, &l);
662 static int t_show(struct seq_file *m, void *v)
664 struct ftrace_event_file *file = v;
665 struct ftrace_event_call *call = file->event_call;
667 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
668 seq_printf(m, "%s:", call->class->system);
669 seq_printf(m, "%s\n", call->name);
674 static void t_stop(struct seq_file *m, void *p)
676 mutex_unlock(&event_mutex);
680 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
683 struct ftrace_event_file *file;
687 mutex_lock(&event_mutex);
688 file = event_file_data(filp);
691 mutex_unlock(&event_mutex);
696 if (flags & FTRACE_EVENT_FL_ENABLED) {
697 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED)
699 else if (flags & FTRACE_EVENT_FL_SOFT_MODE)
706 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
710 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
713 struct ftrace_event_file *file;
717 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
721 ret = tracing_update_buffers();
729 mutex_lock(&event_mutex);
730 file = event_file_data(filp);
732 ret = ftrace_event_enable_disable(file, val);
733 mutex_unlock(&event_mutex);
742 return ret ? ret : cnt;
746 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
749 const char set_to_char[4] = { '?', '0', '1', 'X' };
750 struct ftrace_subsystem_dir *dir = filp->private_data;
751 struct event_subsystem *system = dir->subsystem;
752 struct ftrace_event_call *call;
753 struct ftrace_event_file *file;
754 struct trace_array *tr = dir->tr;
759 mutex_lock(&event_mutex);
760 list_for_each_entry(file, &tr->events, list) {
761 call = file->event_call;
762 if (!call->name || !call->class || !call->class->reg)
765 if (system && strcmp(call->class->system, system->name) != 0)
769 * We need to find out if all the events are set
770 * or if all events or cleared, or if we have
773 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
776 * If we have a mixture, no need to look further.
781 mutex_unlock(&event_mutex);
783 buf[0] = set_to_char[set];
786 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
792 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
795 struct ftrace_subsystem_dir *dir = filp->private_data;
796 struct event_subsystem *system = dir->subsystem;
797 const char *name = NULL;
801 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
805 ret = tracing_update_buffers();
809 if (val != 0 && val != 1)
813 * Opening of "enable" adds a ref count to system,
814 * so the name is safe to use.
819 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
833 FORMAT_FIELD_SEPERATOR = 2,
837 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
839 struct ftrace_event_call *call = event_file_data(m->private);
840 struct ftrace_event_field *field;
841 struct list_head *common_head = &ftrace_common_fields;
842 struct list_head *head = trace_get_fields(call);
846 switch ((unsigned long)v) {
848 if (unlikely(list_empty(common_head)))
851 field = list_entry(common_head->prev,
852 struct ftrace_event_field, link);
855 case FORMAT_FIELD_SEPERATOR:
856 if (unlikely(list_empty(head)))
859 field = list_entry(head->prev, struct ftrace_event_field, link);
862 case FORMAT_PRINTFMT:
868 if (field->link.prev == common_head)
869 return (void *)FORMAT_FIELD_SEPERATOR;
870 else if (field->link.prev == head)
871 return (void *)FORMAT_PRINTFMT;
873 field = list_entry(field->link.prev, struct ftrace_event_field, link);
878 static void *f_start(struct seq_file *m, loff_t *pos)
883 /* ->stop() is called even if ->start() fails */
884 mutex_lock(&event_mutex);
885 if (!event_file_data(m->private))
886 return ERR_PTR(-ENODEV);
888 /* Start by showing the header */
890 return (void *)FORMAT_HEADER;
892 p = (void *)FORMAT_HEADER;
894 p = f_next(m, p, &l);
895 } while (p && l < *pos);
900 static int f_show(struct seq_file *m, void *v)
902 struct ftrace_event_call *call = event_file_data(m->private);
903 struct ftrace_event_field *field;
904 const char *array_descriptor;
906 switch ((unsigned long)v) {
908 seq_printf(m, "name: %s\n", call->name);
909 seq_printf(m, "ID: %d\n", call->event.type);
910 seq_printf(m, "format:\n");
913 case FORMAT_FIELD_SEPERATOR:
917 case FORMAT_PRINTFMT:
918 seq_printf(m, "\nprint fmt: %s\n",
926 * Smartly shows the array type(except dynamic array).
929 * If TYPE := TYPE[LEN], it is shown:
930 * field:TYPE VAR[LEN]
932 array_descriptor = strchr(field->type, '[');
934 if (!strncmp(field->type, "__data_loc", 10))
935 array_descriptor = NULL;
937 if (!array_descriptor)
938 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
939 field->type, field->name, field->offset,
940 field->size, !!field->is_signed);
942 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
943 (int)(array_descriptor - field->type),
944 field->type, field->name,
945 array_descriptor, field->offset,
946 field->size, !!field->is_signed);
951 static void f_stop(struct seq_file *m, void *p)
953 mutex_unlock(&event_mutex);
956 static const struct seq_operations trace_format_seq_ops = {
963 static int trace_format_open(struct inode *inode, struct file *file)
968 ret = seq_open(file, &trace_format_seq_ops);
972 m = file->private_data;
979 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
981 int id = (long)event_file_data(filp);
991 s = kmalloc(sizeof(*s), GFP_KERNEL);
996 trace_seq_printf(s, "%d\n", id);
998 r = simple_read_from_buffer(ubuf, cnt, ppos,
1005 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1008 struct ftrace_event_call *call;
1009 struct trace_seq *s;
1015 s = kmalloc(sizeof(*s), GFP_KERNEL);
1022 mutex_lock(&event_mutex);
1023 call = event_file_data(filp);
1025 print_event_filter(call, s);
1026 mutex_unlock(&event_mutex);
1029 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1037 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1040 struct ftrace_event_call *call;
1044 if (cnt >= PAGE_SIZE)
1047 buf = (char *)__get_free_page(GFP_TEMPORARY);
1051 if (copy_from_user(buf, ubuf, cnt)) {
1052 free_page((unsigned long) buf);
1057 mutex_lock(&event_mutex);
1058 call = event_file_data(filp);
1060 err = apply_event_filter(call, buf);
1061 mutex_unlock(&event_mutex);
1063 free_page((unsigned long) buf);
1072 static LIST_HEAD(event_subsystems);
1074 static int subsystem_open(struct inode *inode, struct file *filp)
1076 struct event_subsystem *system = NULL;
1077 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1078 struct trace_array *tr;
1081 /* Make sure the system still exists */
1082 mutex_lock(&trace_types_lock);
1083 mutex_lock(&event_mutex);
1084 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1085 list_for_each_entry(dir, &tr->systems, list) {
1086 if (dir == inode->i_private) {
1087 /* Don't open systems with no events */
1088 if (dir->nr_events) {
1089 __get_system_dir(dir);
1090 system = dir->subsystem;
1097 mutex_unlock(&event_mutex);
1098 mutex_unlock(&trace_types_lock);
1103 /* Some versions of gcc think dir can be uninitialized here */
1106 /* Still need to increment the ref count of the system */
1107 if (trace_array_get(tr) < 0) {
1112 ret = tracing_open_generic(inode, filp);
1114 trace_array_put(tr);
1121 static int system_tr_open(struct inode *inode, struct file *filp)
1123 struct ftrace_subsystem_dir *dir;
1124 struct trace_array *tr = inode->i_private;
1127 if (trace_array_get(tr) < 0)
1130 /* Make a temporary dir that has no system but points to tr */
1131 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1133 trace_array_put(tr);
1139 ret = tracing_open_generic(inode, filp);
1141 trace_array_put(tr);
1145 filp->private_data = dir;
1150 static int subsystem_release(struct inode *inode, struct file *file)
1152 struct ftrace_subsystem_dir *dir = file->private_data;
1154 trace_array_put(dir->tr);
1157 * If dir->subsystem is NULL, then this is a temporary
1158 * descriptor that was made for a trace_array to enable
1170 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1173 struct ftrace_subsystem_dir *dir = filp->private_data;
1174 struct event_subsystem *system = dir->subsystem;
1175 struct trace_seq *s;
1181 s = kmalloc(sizeof(*s), GFP_KERNEL);
1187 print_subsystem_event_filter(system, s);
1188 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1196 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1199 struct ftrace_subsystem_dir *dir = filp->private_data;
1203 if (cnt >= PAGE_SIZE)
1206 buf = (char *)__get_free_page(GFP_TEMPORARY);
1210 if (copy_from_user(buf, ubuf, cnt)) {
1211 free_page((unsigned long) buf);
1216 err = apply_subsystem_event_filter(dir, buf);
1217 free_page((unsigned long) buf);
1227 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1229 int (*func)(struct trace_seq *s) = filp->private_data;
1230 struct trace_seq *s;
1236 s = kmalloc(sizeof(*s), GFP_KERNEL);
1243 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1250 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1251 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1252 static int ftrace_event_release(struct inode *inode, struct file *file);
1254 static const struct seq_operations show_event_seq_ops = {
1261 static const struct seq_operations show_set_event_seq_ops = {
1268 static const struct file_operations ftrace_avail_fops = {
1269 .open = ftrace_event_avail_open,
1271 .llseek = seq_lseek,
1272 .release = seq_release,
1275 static const struct file_operations ftrace_set_event_fops = {
1276 .open = ftrace_event_set_open,
1278 .write = ftrace_event_write,
1279 .llseek = seq_lseek,
1280 .release = ftrace_event_release,
1283 static const struct file_operations ftrace_enable_fops = {
1284 .open = tracing_open_generic,
1285 .read = event_enable_read,
1286 .write = event_enable_write,
1287 .llseek = default_llseek,
1290 static const struct file_operations ftrace_event_format_fops = {
1291 .open = trace_format_open,
1293 .llseek = seq_lseek,
1294 .release = seq_release,
1297 static const struct file_operations ftrace_event_id_fops = {
1298 .read = event_id_read,
1299 .llseek = default_llseek,
1302 static const struct file_operations ftrace_event_filter_fops = {
1303 .open = tracing_open_generic,
1304 .read = event_filter_read,
1305 .write = event_filter_write,
1306 .llseek = default_llseek,
1309 static const struct file_operations ftrace_subsystem_filter_fops = {
1310 .open = subsystem_open,
1311 .read = subsystem_filter_read,
1312 .write = subsystem_filter_write,
1313 .llseek = default_llseek,
1314 .release = subsystem_release,
1317 static const struct file_operations ftrace_system_enable_fops = {
1318 .open = subsystem_open,
1319 .read = system_enable_read,
1320 .write = system_enable_write,
1321 .llseek = default_llseek,
1322 .release = subsystem_release,
1325 static const struct file_operations ftrace_tr_enable_fops = {
1326 .open = system_tr_open,
1327 .read = system_enable_read,
1328 .write = system_enable_write,
1329 .llseek = default_llseek,
1330 .release = subsystem_release,
1333 static const struct file_operations ftrace_show_header_fops = {
1334 .open = tracing_open_generic,
1335 .read = show_header,
1336 .llseek = default_llseek,
1340 ftrace_event_open(struct inode *inode, struct file *file,
1341 const struct seq_operations *seq_ops)
1346 ret = seq_open(file, seq_ops);
1349 m = file->private_data;
1350 /* copy tr over to seq ops */
1351 m->private = inode->i_private;
1356 static int ftrace_event_release(struct inode *inode, struct file *file)
1358 struct trace_array *tr = inode->i_private;
1360 trace_array_put(tr);
1362 return seq_release(inode, file);
1366 ftrace_event_avail_open(struct inode *inode, struct file *file)
1368 const struct seq_operations *seq_ops = &show_event_seq_ops;
1370 return ftrace_event_open(inode, file, seq_ops);
1374 ftrace_event_set_open(struct inode *inode, struct file *file)
1376 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1377 struct trace_array *tr = inode->i_private;
1380 if (trace_array_get(tr) < 0)
1383 if ((file->f_mode & FMODE_WRITE) &&
1384 (file->f_flags & O_TRUNC))
1385 ftrace_clear_events(tr);
1387 ret = ftrace_event_open(inode, file, seq_ops);
1389 trace_array_put(tr);
1393 static struct event_subsystem *
1394 create_new_subsystem(const char *name)
1396 struct event_subsystem *system;
1398 /* need to create new entry */
1399 system = kmalloc(sizeof(*system), GFP_KERNEL);
1403 system->ref_count = 1;
1405 /* Only allocate if dynamic (kprobes and modules) */
1406 if (!core_kernel_data((unsigned long)name)) {
1407 system->ref_count |= SYSTEM_FL_FREE_NAME;
1408 system->name = kstrdup(name, GFP_KERNEL);
1412 system->name = name;
1414 system->filter = NULL;
1416 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1417 if (!system->filter)
1420 list_add(&system->list, &event_subsystems);
1425 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1426 kfree(system->name);
1431 static struct dentry *
1432 event_subsystem_dir(struct trace_array *tr, const char *name,
1433 struct ftrace_event_file *file, struct dentry *parent)
1435 struct ftrace_subsystem_dir *dir;
1436 struct event_subsystem *system;
1437 struct dentry *entry;
1439 /* First see if we did not already create this dir */
1440 list_for_each_entry(dir, &tr->systems, list) {
1441 system = dir->subsystem;
1442 if (strcmp(system->name, name) == 0) {
1449 /* Now see if the system itself exists. */
1450 list_for_each_entry(system, &event_subsystems, list) {
1451 if (strcmp(system->name, name) == 0)
1454 /* Reset system variable when not found */
1455 if (&system->list == &event_subsystems)
1458 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1463 system = create_new_subsystem(name);
1467 __get_system(system);
1469 dir->entry = debugfs_create_dir(name, parent);
1471 pr_warning("Failed to create system directory %s\n", name);
1472 __put_system(system);
1479 dir->subsystem = system;
1482 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1483 &ftrace_subsystem_filter_fops);
1485 kfree(system->filter);
1486 system->filter = NULL;
1487 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1490 trace_create_file("enable", 0644, dir->entry, dir,
1491 &ftrace_system_enable_fops);
1493 list_add(&dir->list, &tr->systems);
1500 /* Only print this message if failed on memory allocation */
1501 if (!dir || !system)
1502 pr_warning("No memory to create event subsystem %s\n",
1508 event_create_dir(struct dentry *parent,
1509 struct ftrace_event_file *file,
1510 const struct file_operations *id,
1511 const struct file_operations *enable,
1512 const struct file_operations *filter,
1513 const struct file_operations *format)
1515 struct ftrace_event_call *call = file->event_call;
1516 struct trace_array *tr = file->tr;
1517 struct list_head *head;
1518 struct dentry *d_events;
1522 * If the trace point header did not define TRACE_SYSTEM
1523 * then the system would be called "TRACE_SYSTEM".
1525 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1526 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1532 file->dir = debugfs_create_dir(call->name, d_events);
1534 pr_warning("Could not create debugfs '%s' directory\n",
1539 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1540 trace_create_file("enable", 0644, file->dir, file,
1543 #ifdef CONFIG_PERF_EVENTS
1544 if (call->event.type && call->class->reg)
1545 trace_create_file("id", 0444, file->dir,
1546 (void *)(long)call->event.type, id);
1550 * Other events may have the same class. Only update
1551 * the fields if they are not already defined.
1553 head = trace_get_fields(call);
1554 if (list_empty(head)) {
1555 ret = call->class->define_fields(call);
1557 pr_warning("Could not initialize trace point"
1558 " events/%s\n", call->name);
1562 trace_create_file("filter", 0644, file->dir, call,
1565 trace_create_file("format", 0444, file->dir, call,
1571 static void remove_event_from_tracers(struct ftrace_event_call *call)
1573 struct ftrace_event_file *file;
1574 struct trace_array *tr;
1576 do_for_each_event_file_safe(tr, file) {
1577 if (file->event_call != call)
1580 remove_event_file_dir(file);
1582 * The do_for_each_event_file_safe() is
1583 * a double loop. After finding the call for this
1584 * trace_array, we use break to jump to the next
1588 } while_for_each_event_file();
1591 static void event_remove(struct ftrace_event_call *call)
1593 struct trace_array *tr;
1594 struct ftrace_event_file *file;
1596 do_for_each_event_file(tr, file) {
1597 if (file->event_call != call)
1599 ftrace_event_enable_disable(file, 0);
1601 * The do_for_each_event_file() is
1602 * a double loop. After finding the call for this
1603 * trace_array, we use break to jump to the next
1607 } while_for_each_event_file();
1609 if (call->event.funcs)
1610 __unregister_ftrace_event(&call->event);
1611 remove_event_from_tracers(call);
1612 list_del(&call->list);
1615 static int event_init(struct ftrace_event_call *call)
1619 if (WARN_ON(!call->name))
1622 if (call->class->raw_init) {
1623 ret = call->class->raw_init(call);
1624 if (ret < 0 && ret != -ENOSYS)
1625 pr_warn("Could not initialize trace events/%s\n",
1633 __register_event(struct ftrace_event_call *call, struct module *mod)
1637 ret = event_init(call);
1641 list_add(&call->list, &ftrace_events);
1647 static struct ftrace_event_file *
1648 trace_create_new_event(struct ftrace_event_call *call,
1649 struct trace_array *tr)
1651 struct ftrace_event_file *file;
1653 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1657 file->event_call = call;
1659 atomic_set(&file->sm_ref, 0);
1660 list_add(&file->list, &tr->events);
1665 /* Add an event to a trace directory */
1667 __trace_add_new_event(struct ftrace_event_call *call,
1668 struct trace_array *tr,
1669 const struct file_operations *id,
1670 const struct file_operations *enable,
1671 const struct file_operations *filter,
1672 const struct file_operations *format)
1674 struct ftrace_event_file *file;
1676 file = trace_create_new_event(call, tr);
1680 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1684 * Just create a decriptor for early init. A descriptor is required
1685 * for enabling events at boot. We want to enable events before
1686 * the filesystem is initialized.
1689 __trace_early_add_new_event(struct ftrace_event_call *call,
1690 struct trace_array *tr)
1692 struct ftrace_event_file *file;
1694 file = trace_create_new_event(call, tr);
1701 struct ftrace_module_file_ops;
1702 static void __add_event_to_tracers(struct ftrace_event_call *call,
1703 struct ftrace_module_file_ops *file_ops);
1705 /* Add an additional event_call dynamically */
1706 int trace_add_event_call(struct ftrace_event_call *call)
1709 mutex_lock(&trace_types_lock);
1710 mutex_lock(&event_mutex);
1712 ret = __register_event(call, NULL);
1714 __add_event_to_tracers(call, NULL);
1716 mutex_unlock(&event_mutex);
1717 mutex_unlock(&trace_types_lock);
1722 * Must be called under locking of trace_types_lock, event_mutex and
1725 static void __trace_remove_event_call(struct ftrace_event_call *call)
1728 trace_destroy_fields(call);
1729 destroy_preds(call);
1732 static int probe_remove_event_call(struct ftrace_event_call *call)
1734 struct trace_array *tr;
1735 struct ftrace_event_file *file;
1737 #ifdef CONFIG_PERF_EVENTS
1738 if (call->perf_refcount)
1741 do_for_each_event_file(tr, file) {
1742 if (file->event_call != call)
1745 * We can't rely on ftrace_event_enable_disable(enable => 0)
1746 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1747 * TRACE_REG_UNREGISTER.
1749 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1752 } while_for_each_event_file();
1754 __trace_remove_event_call(call);
1759 /* Remove an event_call */
1760 int trace_remove_event_call(struct ftrace_event_call *call)
1764 mutex_lock(&trace_types_lock);
1765 mutex_lock(&event_mutex);
1766 down_write(&trace_event_sem);
1767 ret = probe_remove_event_call(call);
1768 up_write(&trace_event_sem);
1769 mutex_unlock(&event_mutex);
1770 mutex_unlock(&trace_types_lock);
1775 #define for_each_event(event, start, end) \
1776 for (event = start; \
1777 (unsigned long)event < (unsigned long)end; \
1780 #ifdef CONFIG_MODULES
1782 static LIST_HEAD(ftrace_module_file_list);
1785 * Modules must own their file_operations to keep up with
1786 * reference counting.
1788 struct ftrace_module_file_ops {
1789 struct list_head list;
1791 struct file_operations id;
1792 struct file_operations enable;
1793 struct file_operations format;
1794 struct file_operations filter;
1797 static struct ftrace_module_file_ops *
1798 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1801 * As event_calls are added in groups by module,
1802 * when we find one file_ops, we don't need to search for
1803 * each call in that module, as the rest should be the
1804 * same. Only search for a new one if the last one did
1807 if (file_ops && mod == file_ops->mod)
1810 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1811 if (file_ops->mod == mod)
1817 static struct ftrace_module_file_ops *
1818 trace_create_file_ops(struct module *mod)
1820 struct ftrace_module_file_ops *file_ops;
1823 * This is a bit of a PITA. To allow for correct reference
1824 * counting, modules must "own" their file_operations.
1825 * To do this, we allocate the file operations that will be
1826 * used in the event directory.
1829 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1833 file_ops->mod = mod;
1835 file_ops->id = ftrace_event_id_fops;
1836 file_ops->id.owner = mod;
1838 file_ops->enable = ftrace_enable_fops;
1839 file_ops->enable.owner = mod;
1841 file_ops->filter = ftrace_event_filter_fops;
1842 file_ops->filter.owner = mod;
1844 file_ops->format = ftrace_event_format_fops;
1845 file_ops->format.owner = mod;
1847 list_add(&file_ops->list, &ftrace_module_file_list);
1852 static void trace_module_add_events(struct module *mod)
1854 struct ftrace_module_file_ops *file_ops = NULL;
1855 struct ftrace_event_call **call, **start, **end;
1857 if (!mod->num_trace_events)
1860 /* Don't add infrastructure for mods without tracepoints */
1861 if (trace_module_has_bad_taint(mod)) {
1862 pr_err("%s: module has bad taint, not creating trace events\n",
1867 start = mod->trace_events;
1868 end = mod->trace_events + mod->num_trace_events;
1873 file_ops = trace_create_file_ops(mod);
1877 for_each_event(call, start, end) {
1878 __register_event(*call, mod);
1879 __add_event_to_tracers(*call, file_ops);
1883 static void trace_module_remove_events(struct module *mod)
1885 struct ftrace_module_file_ops *file_ops;
1886 struct ftrace_event_call *call, *p;
1887 bool clear_trace = false;
1889 down_write(&trace_event_sem);
1890 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1891 if (call->mod == mod) {
1892 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1894 __trace_remove_event_call(call);
1898 /* Now free the file_operations */
1899 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1900 if (file_ops->mod == mod)
1903 if (&file_ops->list != &ftrace_module_file_list) {
1904 list_del(&file_ops->list);
1907 up_write(&trace_event_sem);
1910 * It is safest to reset the ring buffer if the module being unloaded
1911 * registered any events that were used. The only worry is if
1912 * a new module gets loaded, and takes on the same id as the events
1913 * of this module. When printing out the buffer, traced events left
1914 * over from this module may be passed to the new module events and
1915 * unexpected results may occur.
1918 tracing_reset_all_online_cpus();
1921 static int trace_module_notify(struct notifier_block *self,
1922 unsigned long val, void *data)
1924 struct module *mod = data;
1926 mutex_lock(&trace_types_lock);
1927 mutex_lock(&event_mutex);
1929 case MODULE_STATE_COMING:
1930 trace_module_add_events(mod);
1932 case MODULE_STATE_GOING:
1933 trace_module_remove_events(mod);
1936 mutex_unlock(&event_mutex);
1937 mutex_unlock(&trace_types_lock);
1943 __trace_add_new_mod_event(struct ftrace_event_call *call,
1944 struct trace_array *tr,
1945 struct ftrace_module_file_ops *file_ops)
1947 return __trace_add_new_event(call, tr,
1948 &file_ops->id, &file_ops->enable,
1949 &file_ops->filter, &file_ops->format);
1953 static inline struct ftrace_module_file_ops *
1954 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1958 static inline int trace_module_notify(struct notifier_block *self,
1959 unsigned long val, void *data)
1964 __trace_add_new_mod_event(struct ftrace_event_call *call,
1965 struct trace_array *tr,
1966 struct ftrace_module_file_ops *file_ops)
1970 #endif /* CONFIG_MODULES */
1972 /* Create a new event directory structure for a trace directory. */
1974 __trace_add_event_dirs(struct trace_array *tr)
1976 struct ftrace_module_file_ops *file_ops = NULL;
1977 struct ftrace_event_call *call;
1980 list_for_each_entry(call, &ftrace_events, list) {
1983 * Directories for events by modules need to
1984 * keep module ref counts when opened (as we don't
1985 * want the module to disappear when reading one
1986 * of these files). The file_ops keep account of
1987 * the module ref count.
1989 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1991 continue; /* Warn? */
1992 ret = __trace_add_new_mod_event(call, tr, file_ops);
1994 pr_warning("Could not create directory for event %s\n",
1998 ret = __trace_add_new_event(call, tr,
1999 &ftrace_event_id_fops,
2000 &ftrace_enable_fops,
2001 &ftrace_event_filter_fops,
2002 &ftrace_event_format_fops);
2004 pr_warning("Could not create directory for event %s\n",
2009 #ifdef CONFIG_DYNAMIC_FTRACE
2012 #define ENABLE_EVENT_STR "enable_event"
2013 #define DISABLE_EVENT_STR "disable_event"
2015 struct event_probe_data {
2016 struct ftrace_event_file *file;
2017 unsigned long count;
2022 static struct ftrace_event_file *
2023 find_event_file(struct trace_array *tr, const char *system, const char *event)
2025 struct ftrace_event_file *file;
2026 struct ftrace_event_call *call;
2028 list_for_each_entry(file, &tr->events, list) {
2030 call = file->event_call;
2032 if (!call->name || !call->class || !call->class->reg)
2035 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2038 if (strcmp(event, call->name) == 0 &&
2039 strcmp(system, call->class->system) == 0)
2046 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2048 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2049 struct event_probe_data *data = *pdata;
2055 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2057 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2061 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2063 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2064 struct event_probe_data *data = *pdata;
2072 /* Skip if the event is in a state we want to switch to */
2073 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2076 if (data->count != -1)
2079 event_enable_probe(ip, parent_ip, _data);
2083 event_enable_print(struct seq_file *m, unsigned long ip,
2084 struct ftrace_probe_ops *ops, void *_data)
2086 struct event_probe_data *data = _data;
2088 seq_printf(m, "%ps:", (void *)ip);
2090 seq_printf(m, "%s:%s:%s",
2091 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2092 data->file->event_call->class->system,
2093 data->file->event_call->name);
2095 if (data->count == -1)
2096 seq_printf(m, ":unlimited\n");
2098 seq_printf(m, ":count=%ld\n", data->count);
2104 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2107 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2108 struct event_probe_data *data = *pdata;
2115 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2118 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2119 struct event_probe_data *data = *pdata;
2121 if (WARN_ON_ONCE(data->ref <= 0))
2126 /* Remove the SOFT_MODE flag */
2127 __ftrace_event_enable_disable(data->file, 0, 1);
2128 module_put(data->file->event_call->mod);
2134 static struct ftrace_probe_ops event_enable_probe_ops = {
2135 .func = event_enable_probe,
2136 .print = event_enable_print,
2137 .init = event_enable_init,
2138 .free = event_enable_free,
2141 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2142 .func = event_enable_count_probe,
2143 .print = event_enable_print,
2144 .init = event_enable_init,
2145 .free = event_enable_free,
2148 static struct ftrace_probe_ops event_disable_probe_ops = {
2149 .func = event_enable_probe,
2150 .print = event_enable_print,
2151 .init = event_enable_init,
2152 .free = event_enable_free,
2155 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2156 .func = event_enable_count_probe,
2157 .print = event_enable_print,
2158 .init = event_enable_init,
2159 .free = event_enable_free,
2163 event_enable_func(struct ftrace_hash *hash,
2164 char *glob, char *cmd, char *param, int enabled)
2166 struct trace_array *tr = top_trace_array();
2167 struct ftrace_event_file *file;
2168 struct ftrace_probe_ops *ops;
2169 struct event_probe_data *data;
2176 /* hash funcs only work with set_ftrace_filter */
2183 system = strsep(¶m, ":");
2187 event = strsep(¶m, ":");
2189 mutex_lock(&event_mutex);
2192 file = find_event_file(tr, system, event);
2196 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2199 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2201 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2203 if (glob[0] == '!') {
2204 unregister_ftrace_function_probe_func(glob+1, ops);
2210 data = kzalloc(sizeof(*data), GFP_KERNEL);
2214 data->enable = enable;
2221 number = strsep(¶m, ":");
2224 if (!strlen(number))
2228 * We use the callback data field (which is a pointer)
2231 ret = kstrtoul(number, 0, &data->count);
2236 /* Don't let event modules unload while probe registered */
2237 ret = try_module_get(file->event_call->mod);
2243 ret = __ftrace_event_enable_disable(file, 1, 1);
2246 ret = register_ftrace_function_probe(glob, ops, data);
2248 * The above returns on success the # of functions enabled,
2249 * but if it didn't find any functions it returns zero.
2250 * Consider no functions a failure too.
2257 /* Just return zero, not the number of enabled functions */
2260 mutex_unlock(&event_mutex);
2264 __ftrace_event_enable_disable(file, 0, 1);
2266 module_put(file->event_call->mod);
2272 static struct ftrace_func_command event_enable_cmd = {
2273 .name = ENABLE_EVENT_STR,
2274 .func = event_enable_func,
2277 static struct ftrace_func_command event_disable_cmd = {
2278 .name = DISABLE_EVENT_STR,
2279 .func = event_enable_func,
2282 static __init int register_event_cmds(void)
2286 ret = register_ftrace_command(&event_enable_cmd);
2287 if (WARN_ON(ret < 0))
2289 ret = register_ftrace_command(&event_disable_cmd);
2290 if (WARN_ON(ret < 0))
2291 unregister_ftrace_command(&event_enable_cmd);
2295 static inline int register_event_cmds(void) { return 0; }
2296 #endif /* CONFIG_DYNAMIC_FTRACE */
2299 * The top level array has already had its ftrace_event_file
2300 * descriptors created in order to allow for early events to
2301 * be recorded. This function is called after the debugfs has been
2302 * initialized, and we now have to create the files associated
2306 __trace_early_add_event_dirs(struct trace_array *tr)
2308 struct ftrace_event_file *file;
2312 list_for_each_entry(file, &tr->events, list) {
2313 ret = event_create_dir(tr->event_dir, file,
2314 &ftrace_event_id_fops,
2315 &ftrace_enable_fops,
2316 &ftrace_event_filter_fops,
2317 &ftrace_event_format_fops);
2319 pr_warning("Could not create directory for event %s\n",
2320 file->event_call->name);
2325 * For early boot up, the top trace array requires to have
2326 * a list of events that can be enabled. This must be done before
2327 * the filesystem is set up in order to allow events to be traced
2331 __trace_early_add_events(struct trace_array *tr)
2333 struct ftrace_event_call *call;
2336 list_for_each_entry(call, &ftrace_events, list) {
2337 /* Early boot up should not have any modules loaded */
2338 if (WARN_ON_ONCE(call->mod))
2341 ret = __trace_early_add_new_event(call, tr);
2343 pr_warning("Could not create early event %s\n",
2348 /* Remove the event directory structure for a trace directory. */
2350 __trace_remove_event_dirs(struct trace_array *tr)
2352 struct ftrace_event_file *file, *next;
2354 list_for_each_entry_safe(file, next, &tr->events, list)
2355 remove_event_file_dir(file);
2359 __add_event_to_tracers(struct ftrace_event_call *call,
2360 struct ftrace_module_file_ops *file_ops)
2362 struct trace_array *tr;
2364 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2366 __trace_add_new_mod_event(call, tr, file_ops);
2368 __trace_add_new_event(call, tr,
2369 &ftrace_event_id_fops,
2370 &ftrace_enable_fops,
2371 &ftrace_event_filter_fops,
2372 &ftrace_event_format_fops);
2376 static struct notifier_block trace_module_nb = {
2377 .notifier_call = trace_module_notify,
2381 extern struct ftrace_event_call *__start_ftrace_events[];
2382 extern struct ftrace_event_call *__stop_ftrace_events[];
2384 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2386 static __init int setup_trace_event(char *str)
2388 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2389 ring_buffer_expanded = true;
2390 tracing_selftest_disabled = true;
2394 __setup("trace_event=", setup_trace_event);
2396 /* Expects to have event_mutex held when called */
2398 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2400 struct dentry *d_events;
2401 struct dentry *entry;
2403 entry = debugfs_create_file("set_event", 0644, parent,
2404 tr, &ftrace_set_event_fops);
2406 pr_warning("Could not create debugfs 'set_event' entry\n");
2410 d_events = debugfs_create_dir("events", parent);
2412 pr_warning("Could not create debugfs 'events' directory\n");
2416 /* ring buffer internal formats */
2417 trace_create_file("header_page", 0444, d_events,
2418 ring_buffer_print_page_header,
2419 &ftrace_show_header_fops);
2421 trace_create_file("header_event", 0444, d_events,
2422 ring_buffer_print_entry_header,
2423 &ftrace_show_header_fops);
2425 trace_create_file("enable", 0644, d_events,
2426 tr, &ftrace_tr_enable_fops);
2428 tr->event_dir = d_events;
2434 * event_trace_add_tracer - add a instance of a trace_array to events
2435 * @parent: The parent dentry to place the files/directories for events in
2436 * @tr: The trace array associated with these events
2438 * When a new instance is created, it needs to set up its events
2439 * directory, as well as other files associated with events. It also
2440 * creates the event hierachry in the @parent/events directory.
2442 * Returns 0 on success.
2444 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2448 mutex_lock(&event_mutex);
2450 ret = create_event_toplevel_files(parent, tr);
2454 down_write(&trace_event_sem);
2455 __trace_add_event_dirs(tr);
2456 up_write(&trace_event_sem);
2459 mutex_unlock(&event_mutex);
2465 * The top trace array already had its file descriptors created.
2466 * Now the files themselves need to be created.
2469 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2473 mutex_lock(&event_mutex);
2475 ret = create_event_toplevel_files(parent, tr);
2479 down_write(&trace_event_sem);
2480 __trace_early_add_event_dirs(tr);
2481 up_write(&trace_event_sem);
2484 mutex_unlock(&event_mutex);
2489 int event_trace_del_tracer(struct trace_array *tr)
2491 mutex_lock(&event_mutex);
2493 /* Disable any running events */
2494 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2496 down_write(&trace_event_sem);
2497 __trace_remove_event_dirs(tr);
2498 debugfs_remove_recursive(tr->event_dir);
2499 up_write(&trace_event_sem);
2501 tr->event_dir = NULL;
2503 mutex_unlock(&event_mutex);
2508 static __init int event_trace_memsetup(void)
2510 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2511 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2515 static __init int event_trace_enable(void)
2517 struct trace_array *tr = top_trace_array();
2518 struct ftrace_event_call **iter, *call;
2519 char *buf = bootup_event_buf;
2523 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2526 ret = event_init(call);
2528 list_add(&call->list, &ftrace_events);
2532 * We need the top trace array to have a working set of trace
2533 * points at early init, before the debug files and directories
2534 * are created. Create the file entries now, and attach them
2535 * to the actual file dentries later.
2537 __trace_early_add_events(tr);
2540 token = strsep(&buf, ",");
2547 ret = ftrace_set_clr_event(tr, token, 1);
2549 pr_warn("Failed to enable trace event: %s\n", token);
2552 trace_printk_start_comm();
2554 register_event_cmds();
2559 static __init int event_trace_init(void)
2561 struct trace_array *tr;
2562 struct dentry *d_tracer;
2563 struct dentry *entry;
2566 tr = top_trace_array();
2568 d_tracer = tracing_init_dentry();
2572 entry = debugfs_create_file("available_events", 0444, d_tracer,
2573 tr, &ftrace_avail_fops);
2575 pr_warning("Could not create debugfs "
2576 "'available_events' entry\n");
2578 if (trace_define_common_fields())
2579 pr_warning("tracing: Failed to allocate common fields");
2581 ret = early_event_add_tracer(d_tracer, tr);
2585 ret = register_module_notifier(&trace_module_nb);
2587 pr_warning("Failed to register trace events module notifier\n");
2591 early_initcall(event_trace_memsetup);
2592 core_initcall(event_trace_enable);
2593 fs_initcall(event_trace_init);
2595 #ifdef CONFIG_FTRACE_STARTUP_TEST
2597 static DEFINE_SPINLOCK(test_spinlock);
2598 static DEFINE_SPINLOCK(test_spinlock_irq);
2599 static DEFINE_MUTEX(test_mutex);
2601 static __init void test_work(struct work_struct *dummy)
2603 spin_lock(&test_spinlock);
2604 spin_lock_irq(&test_spinlock_irq);
2606 spin_unlock_irq(&test_spinlock_irq);
2607 spin_unlock(&test_spinlock);
2609 mutex_lock(&test_mutex);
2611 mutex_unlock(&test_mutex);
2614 static __init int event_test_thread(void *unused)
2618 test_malloc = kmalloc(1234, GFP_KERNEL);
2620 pr_info("failed to kmalloc\n");
2622 schedule_on_each_cpu(test_work);
2626 set_current_state(TASK_INTERRUPTIBLE);
2627 while (!kthread_should_stop())
2634 * Do various things that may trigger events.
2636 static __init void event_test_stuff(void)
2638 struct task_struct *test_thread;
2640 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2642 kthread_stop(test_thread);
2646 * For every trace event defined, we will test each trace point separately,
2647 * and then by groups, and finally all trace points.
2649 static __init void event_trace_self_tests(void)
2651 struct ftrace_subsystem_dir *dir;
2652 struct ftrace_event_file *file;
2653 struct ftrace_event_call *call;
2654 struct event_subsystem *system;
2655 struct trace_array *tr;
2658 tr = top_trace_array();
2660 pr_info("Running tests on trace events:\n");
2662 list_for_each_entry(file, &tr->events, list) {
2664 call = file->event_call;
2666 /* Only test those that have a probe */
2667 if (!call->class || !call->class->probe)
2671 * Testing syscall events here is pretty useless, but
2672 * we still do it if configured. But this is time consuming.
2673 * What we really need is a user thread to perform the
2674 * syscalls as we test.
2676 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2677 if (call->class->system &&
2678 strcmp(call->class->system, "syscalls") == 0)
2682 pr_info("Testing event %s: ", call->name);
2685 * If an event is already enabled, someone is using
2686 * it and the self test should not be on.
2688 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2689 pr_warning("Enabled event during self test!\n");
2694 ftrace_event_enable_disable(file, 1);
2696 ftrace_event_enable_disable(file, 0);
2701 /* Now test at the sub system level */
2703 pr_info("Running tests on trace event systems:\n");
2705 list_for_each_entry(dir, &tr->systems, list) {
2707 system = dir->subsystem;
2709 /* the ftrace system is special, skip it */
2710 if (strcmp(system->name, "ftrace") == 0)
2713 pr_info("Testing event system %s: ", system->name);
2715 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2716 if (WARN_ON_ONCE(ret)) {
2717 pr_warning("error enabling system %s\n",
2724 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2725 if (WARN_ON_ONCE(ret)) {
2726 pr_warning("error disabling system %s\n",
2734 /* Test with all events enabled */
2736 pr_info("Running tests on all trace events:\n");
2737 pr_info("Testing all events: ");
2739 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2740 if (WARN_ON_ONCE(ret)) {
2741 pr_warning("error enabling all events\n");
2748 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2749 if (WARN_ON_ONCE(ret)) {
2750 pr_warning("error disabling all events\n");
2757 #ifdef CONFIG_FUNCTION_TRACER
2759 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2762 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2763 struct ftrace_ops *op, struct pt_regs *pt_regs)
2765 struct ring_buffer_event *event;
2766 struct ring_buffer *buffer;
2767 struct ftrace_entry *entry;
2768 unsigned long flags;
2773 pc = preempt_count();
2774 preempt_disable_notrace();
2775 cpu = raw_smp_processor_id();
2776 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2781 local_save_flags(flags);
2783 event = trace_current_buffer_lock_reserve(&buffer,
2784 TRACE_FN, sizeof(*entry),
2788 entry = ring_buffer_event_data(event);
2790 entry->parent_ip = parent_ip;
2792 trace_buffer_unlock_commit(buffer, event, flags, pc);
2795 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2796 preempt_enable_notrace();
2799 static struct ftrace_ops trace_ops __initdata =
2801 .func = function_test_events_call,
2802 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2805 static __init void event_trace_self_test_with_function(void)
2808 ret = register_ftrace_function(&trace_ops);
2809 if (WARN_ON(ret < 0)) {
2810 pr_info("Failed to enable function tracer for event tests\n");
2813 pr_info("Running tests again, along with the function tracer\n");
2814 event_trace_self_tests();
2815 unregister_ftrace_function(&trace_ops);
2818 static __init void event_trace_self_test_with_function(void)
2823 static __init int event_trace_self_tests_init(void)
2825 if (!tracing_selftest_disabled) {
2826 event_trace_self_tests();
2827 event_trace_self_test_with_function();
2833 late_initcall(event_trace_self_tests_init);