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 int trace_define_field(struct ftrace_event_call *call, const char *type,
137 const char *name, int offset, int size, int is_signed,
140 struct list_head *head;
142 if (WARN_ON(!call->class))
145 head = trace_get_fields(call);
146 return __trace_define_field(head, type, name, offset, size,
147 is_signed, filter_type);
149 EXPORT_SYMBOL_GPL(trace_define_field);
151 #define __common_field(type, item) \
152 ret = __trace_define_field(&ftrace_common_fields, #type, \
154 offsetof(typeof(ent), item), \
156 is_signed_type(type), FILTER_OTHER); \
160 static int trace_define_common_fields(void)
163 struct trace_entry ent;
165 __common_field(unsigned short, type);
166 __common_field(unsigned char, flags);
167 __common_field(unsigned char, preempt_count);
168 __common_field(int, pid);
173 static void trace_destroy_fields(struct ftrace_event_call *call)
175 struct ftrace_event_field *field, *next;
176 struct list_head *head;
178 head = trace_get_fields(call);
179 list_for_each_entry_safe(field, next, head, link) {
180 list_del(&field->link);
181 kmem_cache_free(field_cachep, field);
185 int trace_event_raw_init(struct ftrace_event_call *call)
189 id = register_ftrace_event(&call->event);
195 EXPORT_SYMBOL_GPL(trace_event_raw_init);
197 int ftrace_event_reg(struct ftrace_event_call *call,
198 enum trace_reg type, void *data)
200 struct ftrace_event_file *file = data;
203 case TRACE_REG_REGISTER:
204 return tracepoint_probe_register(call->name,
207 case TRACE_REG_UNREGISTER:
208 tracepoint_probe_unregister(call->name,
213 #ifdef CONFIG_PERF_EVENTS
214 case TRACE_REG_PERF_REGISTER:
215 return tracepoint_probe_register(call->name,
216 call->class->perf_probe,
218 case TRACE_REG_PERF_UNREGISTER:
219 tracepoint_probe_unregister(call->name,
220 call->class->perf_probe,
223 case TRACE_REG_PERF_OPEN:
224 case TRACE_REG_PERF_CLOSE:
225 case TRACE_REG_PERF_ADD:
226 case TRACE_REG_PERF_DEL:
232 EXPORT_SYMBOL_GPL(ftrace_event_reg);
234 void trace_event_enable_cmd_record(bool enable)
236 struct ftrace_event_file *file;
237 struct trace_array *tr;
239 mutex_lock(&event_mutex);
240 do_for_each_event_file(tr, file) {
242 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
246 tracing_start_cmdline_record();
247 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
249 tracing_stop_cmdline_record();
250 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
252 } while_for_each_event_file();
253 mutex_unlock(&event_mutex);
256 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
257 int enable, int soft_disable)
259 struct ftrace_event_call *call = file->event_call;
266 * When soft_disable is set and enable is cleared, the sm_ref
267 * reference counter is decremented. If it reaches 0, we want
268 * to clear the SOFT_DISABLED flag but leave the event in the
269 * state that it was. That is, if the event was enabled and
270 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271 * is set we do not want the event to be enabled before we
274 * When soft_disable is not set but the SOFT_MODE flag is,
275 * we do nothing. Do not disable the tracepoint, otherwise
276 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
279 if (atomic_dec_return(&file->sm_ref) > 0)
281 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
282 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
284 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
286 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
287 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
288 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
289 tracing_stop_cmdline_record();
290 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
292 call->class->reg(call, TRACE_REG_UNREGISTER, file);
294 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
295 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
296 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
298 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
302 * When soft_disable is set and enable is set, we want to
303 * register the tracepoint for the event, but leave the event
304 * as is. That means, if the event was already enabled, we do
305 * nothing (but set SOFT_MODE). If the event is disabled, we
306 * set SOFT_DISABLED before enabling the event tracepoint, so
307 * it still seems to be disabled.
310 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
312 if (atomic_inc_return(&file->sm_ref) > 1)
314 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
317 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
319 /* Keep the event disabled, when going to SOFT_MODE. */
321 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
323 if (trace_flags & TRACE_ITER_RECORD_CMD) {
324 tracing_start_cmdline_record();
325 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
327 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
329 tracing_stop_cmdline_record();
330 pr_info("event trace: Could not enable event "
334 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
336 /* WAS_ENABLED gets set but never cleared. */
337 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
345 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
348 return __ftrace_event_enable_disable(file, enable, 0);
351 static void ftrace_clear_events(struct trace_array *tr)
353 struct ftrace_event_file *file;
355 mutex_lock(&event_mutex);
356 list_for_each_entry(file, &tr->events, list) {
357 ftrace_event_enable_disable(file, 0);
359 mutex_unlock(&event_mutex);
362 static void __put_system(struct event_subsystem *system)
364 struct event_filter *filter = system->filter;
366 WARN_ON_ONCE(system_refcount(system) == 0);
367 if (system_refcount_dec(system))
370 list_del(&system->list);
373 kfree(filter->filter_string);
376 if (system->ref_count & SYSTEM_FL_FREE_NAME)
381 static void __get_system(struct event_subsystem *system)
383 WARN_ON_ONCE(system_refcount(system) == 0);
384 system_refcount_inc(system);
387 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
389 WARN_ON_ONCE(dir->ref_count == 0);
391 __get_system(dir->subsystem);
394 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
396 WARN_ON_ONCE(dir->ref_count == 0);
397 /* If the subsystem is about to be freed, the dir must be too */
398 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
400 __put_system(dir->subsystem);
401 if (!--dir->ref_count)
405 static void put_system(struct ftrace_subsystem_dir *dir)
407 mutex_lock(&event_mutex);
408 __put_system_dir(dir);
409 mutex_unlock(&event_mutex);
413 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
415 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
416 const char *sub, const char *event, int set)
418 struct ftrace_event_file *file;
419 struct ftrace_event_call *call;
422 mutex_lock(&event_mutex);
423 list_for_each_entry(file, &tr->events, list) {
425 call = file->event_call;
427 if (!call->name || !call->class || !call->class->reg)
430 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
434 strcmp(match, call->name) != 0 &&
435 strcmp(match, call->class->system) != 0)
438 if (sub && strcmp(sub, call->class->system) != 0)
441 if (event && strcmp(event, call->name) != 0)
444 ftrace_event_enable_disable(file, set);
448 mutex_unlock(&event_mutex);
453 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
455 char *event = NULL, *sub = NULL, *match;
458 * The buf format can be <subsystem>:<event-name>
459 * *:<event-name> means any event by that name.
460 * :<event-name> is the same.
462 * <subsystem>:* means all events in that subsystem
463 * <subsystem>: means the same.
465 * <name> (no ':') means all events in a subsystem with
466 * the name <name> or any event that matches <name>
469 match = strsep(&buf, ":");
475 if (!strlen(sub) || strcmp(sub, "*") == 0)
477 if (!strlen(event) || strcmp(event, "*") == 0)
481 return __ftrace_set_clr_event(tr, match, sub, event, set);
485 * trace_set_clr_event - enable or disable an event
486 * @system: system name to match (NULL for any system)
487 * @event: event name to match (NULL for all events, within system)
488 * @set: 1 to enable, 0 to disable
490 * This is a way for other parts of the kernel to enable or disable
493 * Returns 0 on success, -EINVAL if the parameters do not match any
496 int trace_set_clr_event(const char *system, const char *event, int set)
498 struct trace_array *tr = top_trace_array();
500 return __ftrace_set_clr_event(tr, NULL, system, event, set);
502 EXPORT_SYMBOL_GPL(trace_set_clr_event);
504 /* 128 should be much more than enough */
505 #define EVENT_BUF_SIZE 127
508 ftrace_event_write(struct file *file, const char __user *ubuf,
509 size_t cnt, loff_t *ppos)
511 struct trace_parser parser;
512 struct seq_file *m = file->private_data;
513 struct trace_array *tr = m->private;
519 ret = tracing_update_buffers();
523 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
526 read = trace_get_user(&parser, ubuf, cnt, ppos);
528 if (read >= 0 && trace_parser_loaded((&parser))) {
531 if (*parser.buffer == '!')
534 parser.buffer[parser.idx] = 0;
536 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
544 trace_parser_put(&parser);
550 t_next(struct seq_file *m, void *v, loff_t *pos)
552 struct ftrace_event_file *file = v;
553 struct ftrace_event_call *call;
554 struct trace_array *tr = m->private;
558 list_for_each_entry_continue(file, &tr->events, list) {
559 call = file->event_call;
561 * The ftrace subsystem is for showing formats only.
562 * They can not be enabled or disabled via the event files.
564 if (call->class && call->class->reg)
571 static void *t_start(struct seq_file *m, loff_t *pos)
573 struct ftrace_event_file *file;
574 struct trace_array *tr = m->private;
577 mutex_lock(&event_mutex);
579 file = list_entry(&tr->events, struct ftrace_event_file, list);
580 for (l = 0; l <= *pos; ) {
581 file = t_next(m, file, &l);
589 s_next(struct seq_file *m, void *v, loff_t *pos)
591 struct ftrace_event_file *file = v;
592 struct trace_array *tr = m->private;
596 list_for_each_entry_continue(file, &tr->events, list) {
597 if (file->flags & FTRACE_EVENT_FL_ENABLED)
604 static void *s_start(struct seq_file *m, loff_t *pos)
606 struct ftrace_event_file *file;
607 struct trace_array *tr = m->private;
610 mutex_lock(&event_mutex);
612 file = list_entry(&tr->events, struct ftrace_event_file, list);
613 for (l = 0; l <= *pos; ) {
614 file = s_next(m, file, &l);
621 static int t_show(struct seq_file *m, void *v)
623 struct ftrace_event_file *file = v;
624 struct ftrace_event_call *call = file->event_call;
626 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
627 seq_printf(m, "%s:", call->class->system);
628 seq_printf(m, "%s\n", call->name);
633 static void t_stop(struct seq_file *m, void *p)
635 mutex_unlock(&event_mutex);
639 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
642 struct ftrace_event_file *file = filp->private_data;
645 if (file->flags & FTRACE_EVENT_FL_ENABLED &&
646 !(file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
649 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
650 file->flags & FTRACE_EVENT_FL_SOFT_MODE)
655 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
659 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
662 struct ftrace_event_file *file = filp->private_data;
669 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
673 ret = tracing_update_buffers();
680 mutex_lock(&event_mutex);
681 ret = ftrace_event_enable_disable(file, val);
682 mutex_unlock(&event_mutex);
691 return ret ? ret : cnt;
695 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
698 const char set_to_char[4] = { '?', '0', '1', 'X' };
699 struct ftrace_subsystem_dir *dir = filp->private_data;
700 struct event_subsystem *system = dir->subsystem;
701 struct ftrace_event_call *call;
702 struct ftrace_event_file *file;
703 struct trace_array *tr = dir->tr;
708 mutex_lock(&event_mutex);
709 list_for_each_entry(file, &tr->events, list) {
710 call = file->event_call;
711 if (!call->name || !call->class || !call->class->reg)
714 if (system && strcmp(call->class->system, system->name) != 0)
718 * We need to find out if all the events are set
719 * or if all events or cleared, or if we have
722 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
725 * If we have a mixture, no need to look further.
730 mutex_unlock(&event_mutex);
732 buf[0] = set_to_char[set];
735 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
741 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
744 struct ftrace_subsystem_dir *dir = filp->private_data;
745 struct event_subsystem *system = dir->subsystem;
746 const char *name = NULL;
750 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
754 ret = tracing_update_buffers();
758 if (val != 0 && val != 1)
762 * Opening of "enable" adds a ref count to system,
763 * so the name is safe to use.
768 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
782 FORMAT_FIELD_SEPERATOR = 2,
786 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
788 struct ftrace_event_call *call = m->private;
789 struct ftrace_event_field *field;
790 struct list_head *common_head = &ftrace_common_fields;
791 struct list_head *head = trace_get_fields(call);
795 switch ((unsigned long)v) {
797 if (unlikely(list_empty(common_head)))
800 field = list_entry(common_head->prev,
801 struct ftrace_event_field, link);
804 case FORMAT_FIELD_SEPERATOR:
805 if (unlikely(list_empty(head)))
808 field = list_entry(head->prev, struct ftrace_event_field, link);
811 case FORMAT_PRINTFMT:
817 if (field->link.prev == common_head)
818 return (void *)FORMAT_FIELD_SEPERATOR;
819 else if (field->link.prev == head)
820 return (void *)FORMAT_PRINTFMT;
822 field = list_entry(field->link.prev, struct ftrace_event_field, link);
827 static void *f_start(struct seq_file *m, loff_t *pos)
832 /* Start by showing the header */
834 return (void *)FORMAT_HEADER;
836 p = (void *)FORMAT_HEADER;
838 p = f_next(m, p, &l);
839 } while (p && l < *pos);
844 static int f_show(struct seq_file *m, void *v)
846 struct ftrace_event_call *call = m->private;
847 struct ftrace_event_field *field;
848 const char *array_descriptor;
850 switch ((unsigned long)v) {
852 seq_printf(m, "name: %s\n", call->name);
853 seq_printf(m, "ID: %d\n", call->event.type);
854 seq_printf(m, "format:\n");
857 case FORMAT_FIELD_SEPERATOR:
861 case FORMAT_PRINTFMT:
862 seq_printf(m, "\nprint fmt: %s\n",
870 * Smartly shows the array type(except dynamic array).
873 * If TYPE := TYPE[LEN], it is shown:
874 * field:TYPE VAR[LEN]
876 array_descriptor = strchr(field->type, '[');
878 if (!strncmp(field->type, "__data_loc", 10))
879 array_descriptor = NULL;
881 if (!array_descriptor)
882 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
883 field->type, field->name, field->offset,
884 field->size, !!field->is_signed);
886 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
887 (int)(array_descriptor - field->type),
888 field->type, field->name,
889 array_descriptor, field->offset,
890 field->size, !!field->is_signed);
895 static void f_stop(struct seq_file *m, void *p)
899 static const struct seq_operations trace_format_seq_ops = {
906 static int trace_format_open(struct inode *inode, struct file *file)
908 struct ftrace_event_call *call = inode->i_private;
912 ret = seq_open(file, &trace_format_seq_ops);
916 m = file->private_data;
923 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
925 struct ftrace_event_call *call = filp->private_data;
932 s = kmalloc(sizeof(*s), GFP_KERNEL);
937 trace_seq_printf(s, "%d\n", call->event.type);
939 r = simple_read_from_buffer(ubuf, cnt, ppos,
946 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
949 struct ftrace_event_call *call = filp->private_data;
956 s = kmalloc(sizeof(*s), GFP_KERNEL);
962 print_event_filter(call, s);
963 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
971 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
974 struct ftrace_event_call *call = filp->private_data;
978 if (cnt >= PAGE_SIZE)
981 buf = (char *)__get_free_page(GFP_TEMPORARY);
985 if (copy_from_user(buf, ubuf, cnt)) {
986 free_page((unsigned long) buf);
991 err = apply_event_filter(call, buf);
992 free_page((unsigned long) buf);
1001 static LIST_HEAD(event_subsystems);
1003 static int subsystem_open(struct inode *inode, struct file *filp)
1005 struct event_subsystem *system = NULL;
1006 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1007 struct trace_array *tr;
1010 /* Make sure the system still exists */
1011 mutex_lock(&trace_types_lock);
1012 mutex_lock(&event_mutex);
1013 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1014 list_for_each_entry(dir, &tr->systems, list) {
1015 if (dir == inode->i_private) {
1016 /* Don't open systems with no events */
1017 if (dir->nr_events) {
1018 __get_system_dir(dir);
1019 system = dir->subsystem;
1026 mutex_unlock(&event_mutex);
1027 mutex_unlock(&trace_types_lock);
1032 /* Some versions of gcc think dir can be uninitialized here */
1035 ret = tracing_open_generic(inode, filp);
1042 static int system_tr_open(struct inode *inode, struct file *filp)
1044 struct ftrace_subsystem_dir *dir;
1045 struct trace_array *tr = inode->i_private;
1048 /* Make a temporary dir that has no system but points to tr */
1049 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1055 ret = tracing_open_generic(inode, filp);
1059 filp->private_data = dir;
1064 static int subsystem_release(struct inode *inode, struct file *file)
1066 struct ftrace_subsystem_dir *dir = file->private_data;
1069 * If dir->subsystem is NULL, then this is a temporary
1070 * descriptor that was made for a trace_array to enable
1082 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1085 struct ftrace_subsystem_dir *dir = filp->private_data;
1086 struct event_subsystem *system = dir->subsystem;
1087 struct trace_seq *s;
1093 s = kmalloc(sizeof(*s), GFP_KERNEL);
1099 print_subsystem_event_filter(system, s);
1100 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1108 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1111 struct ftrace_subsystem_dir *dir = filp->private_data;
1115 if (cnt >= PAGE_SIZE)
1118 buf = (char *)__get_free_page(GFP_TEMPORARY);
1122 if (copy_from_user(buf, ubuf, cnt)) {
1123 free_page((unsigned long) buf);
1128 err = apply_subsystem_event_filter(dir, buf);
1129 free_page((unsigned long) buf);
1139 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1141 int (*func)(struct trace_seq *s) = filp->private_data;
1142 struct trace_seq *s;
1148 s = kmalloc(sizeof(*s), GFP_KERNEL);
1155 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1162 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1163 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1165 static const struct seq_operations show_event_seq_ops = {
1172 static const struct seq_operations show_set_event_seq_ops = {
1179 static const struct file_operations ftrace_avail_fops = {
1180 .open = ftrace_event_avail_open,
1182 .llseek = seq_lseek,
1183 .release = seq_release,
1186 static const struct file_operations ftrace_set_event_fops = {
1187 .open = ftrace_event_set_open,
1189 .write = ftrace_event_write,
1190 .llseek = seq_lseek,
1191 .release = seq_release,
1194 static const struct file_operations ftrace_enable_fops = {
1195 .open = tracing_open_generic,
1196 .read = event_enable_read,
1197 .write = event_enable_write,
1198 .llseek = default_llseek,
1201 static const struct file_operations ftrace_event_format_fops = {
1202 .open = trace_format_open,
1204 .llseek = seq_lseek,
1205 .release = seq_release,
1208 static const struct file_operations ftrace_event_id_fops = {
1209 .open = tracing_open_generic,
1210 .read = event_id_read,
1211 .llseek = default_llseek,
1214 static const struct file_operations ftrace_event_filter_fops = {
1215 .open = tracing_open_generic,
1216 .read = event_filter_read,
1217 .write = event_filter_write,
1218 .llseek = default_llseek,
1221 static const struct file_operations ftrace_subsystem_filter_fops = {
1222 .open = subsystem_open,
1223 .read = subsystem_filter_read,
1224 .write = subsystem_filter_write,
1225 .llseek = default_llseek,
1226 .release = subsystem_release,
1229 static const struct file_operations ftrace_system_enable_fops = {
1230 .open = subsystem_open,
1231 .read = system_enable_read,
1232 .write = system_enable_write,
1233 .llseek = default_llseek,
1234 .release = subsystem_release,
1237 static const struct file_operations ftrace_tr_enable_fops = {
1238 .open = system_tr_open,
1239 .read = system_enable_read,
1240 .write = system_enable_write,
1241 .llseek = default_llseek,
1242 .release = subsystem_release,
1245 static const struct file_operations ftrace_show_header_fops = {
1246 .open = tracing_open_generic,
1247 .read = show_header,
1248 .llseek = default_llseek,
1252 ftrace_event_open(struct inode *inode, struct file *file,
1253 const struct seq_operations *seq_ops)
1258 ret = seq_open(file, seq_ops);
1261 m = file->private_data;
1262 /* copy tr over to seq ops */
1263 m->private = inode->i_private;
1269 ftrace_event_avail_open(struct inode *inode, struct file *file)
1271 const struct seq_operations *seq_ops = &show_event_seq_ops;
1273 return ftrace_event_open(inode, file, seq_ops);
1277 ftrace_event_set_open(struct inode *inode, struct file *file)
1279 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1280 struct trace_array *tr = inode->i_private;
1282 if ((file->f_mode & FMODE_WRITE) &&
1283 (file->f_flags & O_TRUNC))
1284 ftrace_clear_events(tr);
1286 return ftrace_event_open(inode, file, seq_ops);
1289 static struct event_subsystem *
1290 create_new_subsystem(const char *name)
1292 struct event_subsystem *system;
1294 /* need to create new entry */
1295 system = kmalloc(sizeof(*system), GFP_KERNEL);
1299 system->ref_count = 1;
1301 /* Only allocate if dynamic (kprobes and modules) */
1302 if (!core_kernel_data((unsigned long)name)) {
1303 system->ref_count |= SYSTEM_FL_FREE_NAME;
1304 system->name = kstrdup(name, GFP_KERNEL);
1308 system->name = name;
1310 system->filter = NULL;
1312 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1313 if (!system->filter)
1316 list_add(&system->list, &event_subsystems);
1321 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1322 kfree(system->name);
1327 static struct dentry *
1328 event_subsystem_dir(struct trace_array *tr, const char *name,
1329 struct ftrace_event_file *file, struct dentry *parent)
1331 struct ftrace_subsystem_dir *dir;
1332 struct event_subsystem *system;
1333 struct dentry *entry;
1335 /* First see if we did not already create this dir */
1336 list_for_each_entry(dir, &tr->systems, list) {
1337 system = dir->subsystem;
1338 if (strcmp(system->name, name) == 0) {
1345 /* Now see if the system itself exists. */
1346 list_for_each_entry(system, &event_subsystems, list) {
1347 if (strcmp(system->name, name) == 0)
1350 /* Reset system variable when not found */
1351 if (&system->list == &event_subsystems)
1354 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1359 system = create_new_subsystem(name);
1363 __get_system(system);
1365 dir->entry = debugfs_create_dir(name, parent);
1367 pr_warning("Failed to create system directory %s\n", name);
1368 __put_system(system);
1375 dir->subsystem = system;
1378 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1379 &ftrace_subsystem_filter_fops);
1381 kfree(system->filter);
1382 system->filter = NULL;
1383 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1386 trace_create_file("enable", 0644, dir->entry, dir,
1387 &ftrace_system_enable_fops);
1389 list_add(&dir->list, &tr->systems);
1396 /* Only print this message if failed on memory allocation */
1397 if (!dir || !system)
1398 pr_warning("No memory to create event subsystem %s\n",
1404 event_create_dir(struct dentry *parent,
1405 struct ftrace_event_file *file,
1406 const struct file_operations *id,
1407 const struct file_operations *enable,
1408 const struct file_operations *filter,
1409 const struct file_operations *format)
1411 struct ftrace_event_call *call = file->event_call;
1412 struct trace_array *tr = file->tr;
1413 struct list_head *head;
1414 struct dentry *d_events;
1418 * If the trace point header did not define TRACE_SYSTEM
1419 * then the system would be called "TRACE_SYSTEM".
1421 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1422 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1428 file->dir = debugfs_create_dir(call->name, d_events);
1430 pr_warning("Could not create debugfs '%s' directory\n",
1435 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1436 trace_create_file("enable", 0644, file->dir, file,
1439 #ifdef CONFIG_PERF_EVENTS
1440 if (call->event.type && call->class->reg)
1441 trace_create_file("id", 0444, file->dir, call,
1446 * Other events may have the same class. Only update
1447 * the fields if they are not already defined.
1449 head = trace_get_fields(call);
1450 if (list_empty(head)) {
1451 ret = call->class->define_fields(call);
1453 pr_warning("Could not initialize trace point"
1454 " events/%s\n", call->name);
1458 trace_create_file("filter", 0644, file->dir, call,
1461 trace_create_file("format", 0444, file->dir, call,
1467 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1472 if (!--dir->nr_events) {
1473 debugfs_remove_recursive(dir->entry);
1474 list_del(&dir->list);
1475 __put_system_dir(dir);
1479 static void remove_event_from_tracers(struct ftrace_event_call *call)
1481 struct ftrace_event_file *file;
1482 struct trace_array *tr;
1484 do_for_each_event_file_safe(tr, file) {
1486 if (file->event_call != call)
1489 list_del(&file->list);
1490 debugfs_remove_recursive(file->dir);
1491 remove_subsystem(file->system);
1492 kmem_cache_free(file_cachep, file);
1495 * The do_for_each_event_file_safe() is
1496 * a double loop. After finding the call for this
1497 * trace_array, we use break to jump to the next
1501 } while_for_each_event_file();
1504 static void event_remove(struct ftrace_event_call *call)
1506 struct trace_array *tr;
1507 struct ftrace_event_file *file;
1509 do_for_each_event_file(tr, file) {
1510 if (file->event_call != call)
1512 ftrace_event_enable_disable(file, 0);
1514 * The do_for_each_event_file() is
1515 * a double loop. After finding the call for this
1516 * trace_array, we use break to jump to the next
1520 } while_for_each_event_file();
1522 if (call->event.funcs)
1523 __unregister_ftrace_event(&call->event);
1524 remove_event_from_tracers(call);
1525 list_del(&call->list);
1528 static int event_init(struct ftrace_event_call *call)
1532 if (WARN_ON(!call->name))
1535 if (call->class->raw_init) {
1536 ret = call->class->raw_init(call);
1537 if (ret < 0 && ret != -ENOSYS)
1538 pr_warn("Could not initialize trace events/%s\n",
1546 __register_event(struct ftrace_event_call *call, struct module *mod)
1550 ret = event_init(call);
1554 list_add(&call->list, &ftrace_events);
1560 static struct ftrace_event_file *
1561 trace_create_new_event(struct ftrace_event_call *call,
1562 struct trace_array *tr)
1564 struct ftrace_event_file *file;
1566 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1570 file->event_call = call;
1572 atomic_set(&file->sm_ref, 0);
1573 list_add(&file->list, &tr->events);
1578 /* Add an event to a trace directory */
1580 __trace_add_new_event(struct ftrace_event_call *call,
1581 struct trace_array *tr,
1582 const struct file_operations *id,
1583 const struct file_operations *enable,
1584 const struct file_operations *filter,
1585 const struct file_operations *format)
1587 struct ftrace_event_file *file;
1589 file = trace_create_new_event(call, tr);
1593 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1597 * Just create a decriptor for early init. A descriptor is required
1598 * for enabling events at boot. We want to enable events before
1599 * the filesystem is initialized.
1602 __trace_early_add_new_event(struct ftrace_event_call *call,
1603 struct trace_array *tr)
1605 struct ftrace_event_file *file;
1607 file = trace_create_new_event(call, tr);
1614 struct ftrace_module_file_ops;
1615 static void __add_event_to_tracers(struct ftrace_event_call *call,
1616 struct ftrace_module_file_ops *file_ops);
1618 /* Add an additional event_call dynamically */
1619 int trace_add_event_call(struct ftrace_event_call *call)
1622 mutex_lock(&trace_types_lock);
1623 mutex_lock(&event_mutex);
1625 ret = __register_event(call, NULL);
1627 __add_event_to_tracers(call, NULL);
1629 mutex_unlock(&event_mutex);
1630 mutex_unlock(&trace_types_lock);
1635 * Must be called under locking of trace_types_lock, event_mutex and
1638 static void __trace_remove_event_call(struct ftrace_event_call *call)
1641 trace_destroy_fields(call);
1642 destroy_preds(call);
1645 /* Remove an event_call */
1646 void trace_remove_event_call(struct ftrace_event_call *call)
1648 mutex_lock(&trace_types_lock);
1649 mutex_lock(&event_mutex);
1650 down_write(&trace_event_sem);
1651 __trace_remove_event_call(call);
1652 up_write(&trace_event_sem);
1653 mutex_unlock(&event_mutex);
1654 mutex_unlock(&trace_types_lock);
1657 #define for_each_event(event, start, end) \
1658 for (event = start; \
1659 (unsigned long)event < (unsigned long)end; \
1662 #ifdef CONFIG_MODULES
1664 static LIST_HEAD(ftrace_module_file_list);
1667 * Modules must own their file_operations to keep up with
1668 * reference counting.
1670 struct ftrace_module_file_ops {
1671 struct list_head list;
1673 struct file_operations id;
1674 struct file_operations enable;
1675 struct file_operations format;
1676 struct file_operations filter;
1679 static struct ftrace_module_file_ops *
1680 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1683 * As event_calls are added in groups by module,
1684 * when we find one file_ops, we don't need to search for
1685 * each call in that module, as the rest should be the
1686 * same. Only search for a new one if the last one did
1689 if (file_ops && mod == file_ops->mod)
1692 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1693 if (file_ops->mod == mod)
1699 static struct ftrace_module_file_ops *
1700 trace_create_file_ops(struct module *mod)
1702 struct ftrace_module_file_ops *file_ops;
1705 * This is a bit of a PITA. To allow for correct reference
1706 * counting, modules must "own" their file_operations.
1707 * To do this, we allocate the file operations that will be
1708 * used in the event directory.
1711 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1715 file_ops->mod = mod;
1717 file_ops->id = ftrace_event_id_fops;
1718 file_ops->id.owner = mod;
1720 file_ops->enable = ftrace_enable_fops;
1721 file_ops->enable.owner = mod;
1723 file_ops->filter = ftrace_event_filter_fops;
1724 file_ops->filter.owner = mod;
1726 file_ops->format = ftrace_event_format_fops;
1727 file_ops->format.owner = mod;
1729 list_add(&file_ops->list, &ftrace_module_file_list);
1734 static void trace_module_add_events(struct module *mod)
1736 struct ftrace_module_file_ops *file_ops = NULL;
1737 struct ftrace_event_call **call, **start, **end;
1739 start = mod->trace_events;
1740 end = mod->trace_events + mod->num_trace_events;
1745 file_ops = trace_create_file_ops(mod);
1749 for_each_event(call, start, end) {
1750 __register_event(*call, mod);
1751 __add_event_to_tracers(*call, file_ops);
1755 static void trace_module_remove_events(struct module *mod)
1757 struct ftrace_module_file_ops *file_ops;
1758 struct ftrace_event_call *call, *p;
1759 bool clear_trace = false;
1761 down_write(&trace_event_sem);
1762 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1763 if (call->mod == mod) {
1764 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1766 __trace_remove_event_call(call);
1770 /* Now free the file_operations */
1771 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1772 if (file_ops->mod == mod)
1775 if (&file_ops->list != &ftrace_module_file_list) {
1776 list_del(&file_ops->list);
1779 up_write(&trace_event_sem);
1782 * It is safest to reset the ring buffer if the module being unloaded
1783 * registered any events that were used. The only worry is if
1784 * a new module gets loaded, and takes on the same id as the events
1785 * of this module. When printing out the buffer, traced events left
1786 * over from this module may be passed to the new module events and
1787 * unexpected results may occur.
1790 tracing_reset_all_online_cpus();
1793 static int trace_module_notify(struct notifier_block *self,
1794 unsigned long val, void *data)
1796 struct module *mod = data;
1798 mutex_lock(&trace_types_lock);
1799 mutex_lock(&event_mutex);
1801 case MODULE_STATE_COMING:
1802 trace_module_add_events(mod);
1804 case MODULE_STATE_GOING:
1805 trace_module_remove_events(mod);
1808 mutex_unlock(&event_mutex);
1809 mutex_unlock(&trace_types_lock);
1815 __trace_add_new_mod_event(struct ftrace_event_call *call,
1816 struct trace_array *tr,
1817 struct ftrace_module_file_ops *file_ops)
1819 return __trace_add_new_event(call, tr,
1820 &file_ops->id, &file_ops->enable,
1821 &file_ops->filter, &file_ops->format);
1825 static inline struct ftrace_module_file_ops *
1826 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1830 static inline int trace_module_notify(struct notifier_block *self,
1831 unsigned long val, void *data)
1836 __trace_add_new_mod_event(struct ftrace_event_call *call,
1837 struct trace_array *tr,
1838 struct ftrace_module_file_ops *file_ops)
1842 #endif /* CONFIG_MODULES */
1844 /* Create a new event directory structure for a trace directory. */
1846 __trace_add_event_dirs(struct trace_array *tr)
1848 struct ftrace_module_file_ops *file_ops = NULL;
1849 struct ftrace_event_call *call;
1852 list_for_each_entry(call, &ftrace_events, list) {
1855 * Directories for events by modules need to
1856 * keep module ref counts when opened (as we don't
1857 * want the module to disappear when reading one
1858 * of these files). The file_ops keep account of
1859 * the module ref count.
1861 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1863 continue; /* Warn? */
1864 ret = __trace_add_new_mod_event(call, tr, file_ops);
1866 pr_warning("Could not create directory for event %s\n",
1870 ret = __trace_add_new_event(call, tr,
1871 &ftrace_event_id_fops,
1872 &ftrace_enable_fops,
1873 &ftrace_event_filter_fops,
1874 &ftrace_event_format_fops);
1876 pr_warning("Could not create directory for event %s\n",
1881 #ifdef CONFIG_DYNAMIC_FTRACE
1884 #define ENABLE_EVENT_STR "enable_event"
1885 #define DISABLE_EVENT_STR "disable_event"
1887 struct event_probe_data {
1888 struct ftrace_event_file *file;
1889 unsigned long count;
1894 static struct ftrace_event_file *
1895 find_event_file(struct trace_array *tr, const char *system, const char *event)
1897 struct ftrace_event_file *file;
1898 struct ftrace_event_call *call;
1900 list_for_each_entry(file, &tr->events, list) {
1902 call = file->event_call;
1904 if (!call->name || !call->class || !call->class->reg)
1907 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1910 if (strcmp(event, call->name) == 0 &&
1911 strcmp(system, call->class->system) == 0)
1918 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1920 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1921 struct event_probe_data *data = *pdata;
1927 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1929 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1933 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1935 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1936 struct event_probe_data *data = *pdata;
1944 /* Skip if the event is in a state we want to switch to */
1945 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1948 if (data->count != -1)
1951 event_enable_probe(ip, parent_ip, _data);
1955 event_enable_print(struct seq_file *m, unsigned long ip,
1956 struct ftrace_probe_ops *ops, void *_data)
1958 struct event_probe_data *data = _data;
1960 seq_printf(m, "%ps:", (void *)ip);
1962 seq_printf(m, "%s:%s:%s",
1963 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1964 data->file->event_call->class->system,
1965 data->file->event_call->name);
1967 if (data->count == -1)
1968 seq_printf(m, ":unlimited\n");
1970 seq_printf(m, ":count=%ld\n", data->count);
1976 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1979 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1980 struct event_probe_data *data = *pdata;
1987 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1990 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1991 struct event_probe_data *data = *pdata;
1993 if (WARN_ON_ONCE(data->ref <= 0))
1998 /* Remove the SOFT_MODE flag */
1999 __ftrace_event_enable_disable(data->file, 0, 1);
2000 module_put(data->file->event_call->mod);
2006 static struct ftrace_probe_ops event_enable_probe_ops = {
2007 .func = event_enable_probe,
2008 .print = event_enable_print,
2009 .init = event_enable_init,
2010 .free = event_enable_free,
2013 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2014 .func = event_enable_count_probe,
2015 .print = event_enable_print,
2016 .init = event_enable_init,
2017 .free = event_enable_free,
2020 static struct ftrace_probe_ops event_disable_probe_ops = {
2021 .func = event_enable_probe,
2022 .print = event_enable_print,
2023 .init = event_enable_init,
2024 .free = event_enable_free,
2027 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2028 .func = event_enable_count_probe,
2029 .print = event_enable_print,
2030 .init = event_enable_init,
2031 .free = event_enable_free,
2035 event_enable_func(struct ftrace_hash *hash,
2036 char *glob, char *cmd, char *param, int enabled)
2038 struct trace_array *tr = top_trace_array();
2039 struct ftrace_event_file *file;
2040 struct ftrace_probe_ops *ops;
2041 struct event_probe_data *data;
2048 /* hash funcs only work with set_ftrace_filter */
2049 if (!enabled || !param)
2052 system = strsep(¶m, ":");
2056 event = strsep(¶m, ":");
2058 mutex_lock(&event_mutex);
2061 file = find_event_file(tr, system, event);
2065 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2068 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2070 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2072 if (glob[0] == '!') {
2073 unregister_ftrace_function_probe_func(glob+1, ops);
2079 data = kzalloc(sizeof(*data), GFP_KERNEL);
2083 data->enable = enable;
2090 number = strsep(¶m, ":");
2093 if (!strlen(number))
2097 * We use the callback data field (which is a pointer)
2100 ret = kstrtoul(number, 0, &data->count);
2105 /* Don't let event modules unload while probe registered */
2106 ret = try_module_get(file->event_call->mod);
2112 ret = __ftrace_event_enable_disable(file, 1, 1);
2115 ret = register_ftrace_function_probe(glob, ops, data);
2117 * The above returns on success the # of functions enabled,
2118 * but if it didn't find any functions it returns zero.
2119 * Consider no functions a failure too.
2126 /* Just return zero, not the number of enabled functions */
2129 mutex_unlock(&event_mutex);
2133 __ftrace_event_enable_disable(file, 0, 1);
2135 module_put(file->event_call->mod);
2141 static struct ftrace_func_command event_enable_cmd = {
2142 .name = ENABLE_EVENT_STR,
2143 .func = event_enable_func,
2146 static struct ftrace_func_command event_disable_cmd = {
2147 .name = DISABLE_EVENT_STR,
2148 .func = event_enable_func,
2151 static __init int register_event_cmds(void)
2155 ret = register_ftrace_command(&event_enable_cmd);
2156 if (WARN_ON(ret < 0))
2158 ret = register_ftrace_command(&event_disable_cmd);
2159 if (WARN_ON(ret < 0))
2160 unregister_ftrace_command(&event_enable_cmd);
2164 static inline int register_event_cmds(void) { return 0; }
2165 #endif /* CONFIG_DYNAMIC_FTRACE */
2168 * The top level array has already had its ftrace_event_file
2169 * descriptors created in order to allow for early events to
2170 * be recorded. This function is called after the debugfs has been
2171 * initialized, and we now have to create the files associated
2175 __trace_early_add_event_dirs(struct trace_array *tr)
2177 struct ftrace_event_file *file;
2181 list_for_each_entry(file, &tr->events, list) {
2182 ret = event_create_dir(tr->event_dir, file,
2183 &ftrace_event_id_fops,
2184 &ftrace_enable_fops,
2185 &ftrace_event_filter_fops,
2186 &ftrace_event_format_fops);
2188 pr_warning("Could not create directory for event %s\n",
2189 file->event_call->name);
2194 * For early boot up, the top trace array requires to have
2195 * a list of events that can be enabled. This must be done before
2196 * the filesystem is set up in order to allow events to be traced
2200 __trace_early_add_events(struct trace_array *tr)
2202 struct ftrace_event_call *call;
2205 list_for_each_entry(call, &ftrace_events, list) {
2206 /* Early boot up should not have any modules loaded */
2207 if (WARN_ON_ONCE(call->mod))
2210 ret = __trace_early_add_new_event(call, tr);
2212 pr_warning("Could not create early event %s\n",
2217 /* Remove the event directory structure for a trace directory. */
2219 __trace_remove_event_dirs(struct trace_array *tr)
2221 struct ftrace_event_file *file, *next;
2223 list_for_each_entry_safe(file, next, &tr->events, list) {
2224 list_del(&file->list);
2225 debugfs_remove_recursive(file->dir);
2226 remove_subsystem(file->system);
2227 kmem_cache_free(file_cachep, file);
2232 __add_event_to_tracers(struct ftrace_event_call *call,
2233 struct ftrace_module_file_ops *file_ops)
2235 struct trace_array *tr;
2237 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2239 __trace_add_new_mod_event(call, tr, file_ops);
2241 __trace_add_new_event(call, tr,
2242 &ftrace_event_id_fops,
2243 &ftrace_enable_fops,
2244 &ftrace_event_filter_fops,
2245 &ftrace_event_format_fops);
2249 static struct notifier_block trace_module_nb = {
2250 .notifier_call = trace_module_notify,
2254 extern struct ftrace_event_call *__start_ftrace_events[];
2255 extern struct ftrace_event_call *__stop_ftrace_events[];
2257 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2259 static __init int setup_trace_event(char *str)
2261 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2262 ring_buffer_expanded = true;
2263 tracing_selftest_disabled = true;
2267 __setup("trace_event=", setup_trace_event);
2269 /* Expects to have event_mutex held when called */
2271 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2273 struct dentry *d_events;
2274 struct dentry *entry;
2276 entry = debugfs_create_file("set_event", 0644, parent,
2277 tr, &ftrace_set_event_fops);
2279 pr_warning("Could not create debugfs 'set_event' entry\n");
2283 d_events = debugfs_create_dir("events", parent);
2285 pr_warning("Could not create debugfs 'events' directory\n");
2289 /* ring buffer internal formats */
2290 trace_create_file("header_page", 0444, d_events,
2291 ring_buffer_print_page_header,
2292 &ftrace_show_header_fops);
2294 trace_create_file("header_event", 0444, d_events,
2295 ring_buffer_print_entry_header,
2296 &ftrace_show_header_fops);
2298 trace_create_file("enable", 0644, d_events,
2299 tr, &ftrace_tr_enable_fops);
2301 tr->event_dir = d_events;
2307 * event_trace_add_tracer - add a instance of a trace_array to events
2308 * @parent: The parent dentry to place the files/directories for events in
2309 * @tr: The trace array associated with these events
2311 * When a new instance is created, it needs to set up its events
2312 * directory, as well as other files associated with events. It also
2313 * creates the event hierachry in the @parent/events directory.
2315 * Returns 0 on success.
2317 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2321 mutex_lock(&event_mutex);
2323 ret = create_event_toplevel_files(parent, tr);
2327 down_write(&trace_event_sem);
2328 __trace_add_event_dirs(tr);
2329 up_write(&trace_event_sem);
2332 mutex_unlock(&event_mutex);
2338 * The top trace array already had its file descriptors created.
2339 * Now the files themselves need to be created.
2342 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2346 mutex_lock(&event_mutex);
2348 ret = create_event_toplevel_files(parent, tr);
2352 down_write(&trace_event_sem);
2353 __trace_early_add_event_dirs(tr);
2354 up_write(&trace_event_sem);
2357 mutex_unlock(&event_mutex);
2362 int event_trace_del_tracer(struct trace_array *tr)
2364 /* Disable any running events */
2365 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2367 mutex_lock(&event_mutex);
2369 down_write(&trace_event_sem);
2370 __trace_remove_event_dirs(tr);
2371 debugfs_remove_recursive(tr->event_dir);
2372 up_write(&trace_event_sem);
2374 tr->event_dir = NULL;
2376 mutex_unlock(&event_mutex);
2381 static __init int event_trace_memsetup(void)
2383 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2384 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2388 static __init int event_trace_enable(void)
2390 struct trace_array *tr = top_trace_array();
2391 struct ftrace_event_call **iter, *call;
2392 char *buf = bootup_event_buf;
2396 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2399 ret = event_init(call);
2401 list_add(&call->list, &ftrace_events);
2405 * We need the top trace array to have a working set of trace
2406 * points at early init, before the debug files and directories
2407 * are created. Create the file entries now, and attach them
2408 * to the actual file dentries later.
2410 __trace_early_add_events(tr);
2413 token = strsep(&buf, ",");
2420 ret = ftrace_set_clr_event(tr, token, 1);
2422 pr_warn("Failed to enable trace event: %s\n", token);
2425 trace_printk_start_comm();
2427 register_event_cmds();
2432 static __init int event_trace_init(void)
2434 struct trace_array *tr;
2435 struct dentry *d_tracer;
2436 struct dentry *entry;
2439 tr = top_trace_array();
2441 d_tracer = tracing_init_dentry();
2445 entry = debugfs_create_file("available_events", 0444, d_tracer,
2446 tr, &ftrace_avail_fops);
2448 pr_warning("Could not create debugfs "
2449 "'available_events' entry\n");
2451 if (trace_define_common_fields())
2452 pr_warning("tracing: Failed to allocate common fields");
2454 ret = early_event_add_tracer(d_tracer, tr);
2458 ret = register_module_notifier(&trace_module_nb);
2460 pr_warning("Failed to register trace events module notifier\n");
2464 early_initcall(event_trace_memsetup);
2465 core_initcall(event_trace_enable);
2466 fs_initcall(event_trace_init);
2468 #ifdef CONFIG_FTRACE_STARTUP_TEST
2470 static DEFINE_SPINLOCK(test_spinlock);
2471 static DEFINE_SPINLOCK(test_spinlock_irq);
2472 static DEFINE_MUTEX(test_mutex);
2474 static __init void test_work(struct work_struct *dummy)
2476 spin_lock(&test_spinlock);
2477 spin_lock_irq(&test_spinlock_irq);
2479 spin_unlock_irq(&test_spinlock_irq);
2480 spin_unlock(&test_spinlock);
2482 mutex_lock(&test_mutex);
2484 mutex_unlock(&test_mutex);
2487 static __init int event_test_thread(void *unused)
2491 test_malloc = kmalloc(1234, GFP_KERNEL);
2493 pr_info("failed to kmalloc\n");
2495 schedule_on_each_cpu(test_work);
2499 set_current_state(TASK_INTERRUPTIBLE);
2500 while (!kthread_should_stop())
2507 * Do various things that may trigger events.
2509 static __init void event_test_stuff(void)
2511 struct task_struct *test_thread;
2513 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2515 kthread_stop(test_thread);
2519 * For every trace event defined, we will test each trace point separately,
2520 * and then by groups, and finally all trace points.
2522 static __init void event_trace_self_tests(void)
2524 struct ftrace_subsystem_dir *dir;
2525 struct ftrace_event_file *file;
2526 struct ftrace_event_call *call;
2527 struct event_subsystem *system;
2528 struct trace_array *tr;
2531 tr = top_trace_array();
2533 pr_info("Running tests on trace events:\n");
2535 list_for_each_entry(file, &tr->events, list) {
2537 call = file->event_call;
2539 /* Only test those that have a probe */
2540 if (!call->class || !call->class->probe)
2544 * Testing syscall events here is pretty useless, but
2545 * we still do it if configured. But this is time consuming.
2546 * What we really need is a user thread to perform the
2547 * syscalls as we test.
2549 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2550 if (call->class->system &&
2551 strcmp(call->class->system, "syscalls") == 0)
2555 pr_info("Testing event %s: ", call->name);
2558 * If an event is already enabled, someone is using
2559 * it and the self test should not be on.
2561 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2562 pr_warning("Enabled event during self test!\n");
2567 ftrace_event_enable_disable(file, 1);
2569 ftrace_event_enable_disable(file, 0);
2574 /* Now test at the sub system level */
2576 pr_info("Running tests on trace event systems:\n");
2578 list_for_each_entry(dir, &tr->systems, list) {
2580 system = dir->subsystem;
2582 /* the ftrace system is special, skip it */
2583 if (strcmp(system->name, "ftrace") == 0)
2586 pr_info("Testing event system %s: ", system->name);
2588 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2589 if (WARN_ON_ONCE(ret)) {
2590 pr_warning("error enabling system %s\n",
2597 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2598 if (WARN_ON_ONCE(ret)) {
2599 pr_warning("error disabling system %s\n",
2607 /* Test with all events enabled */
2609 pr_info("Running tests on all trace events:\n");
2610 pr_info("Testing all events: ");
2612 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2613 if (WARN_ON_ONCE(ret)) {
2614 pr_warning("error enabling all events\n");
2621 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2622 if (WARN_ON_ONCE(ret)) {
2623 pr_warning("error disabling all events\n");
2630 #ifdef CONFIG_FUNCTION_TRACER
2632 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2635 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2636 struct ftrace_ops *op, struct pt_regs *pt_regs)
2638 struct ring_buffer_event *event;
2639 struct ring_buffer *buffer;
2640 struct ftrace_entry *entry;
2641 unsigned long flags;
2646 pc = preempt_count();
2647 preempt_disable_notrace();
2648 cpu = raw_smp_processor_id();
2649 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2654 local_save_flags(flags);
2656 event = trace_current_buffer_lock_reserve(&buffer,
2657 TRACE_FN, sizeof(*entry),
2661 entry = ring_buffer_event_data(event);
2663 entry->parent_ip = parent_ip;
2665 trace_buffer_unlock_commit(buffer, event, flags, pc);
2668 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2669 preempt_enable_notrace();
2672 static struct ftrace_ops trace_ops __initdata =
2674 .func = function_test_events_call,
2675 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2678 static __init void event_trace_self_test_with_function(void)
2681 ret = register_ftrace_function(&trace_ops);
2682 if (WARN_ON(ret < 0)) {
2683 pr_info("Failed to enable function tracer for event tests\n");
2686 pr_info("Running tests again, along with the function tracer\n");
2687 event_trace_self_tests();
2688 unregister_ftrace_function(&trace_ops);
2691 static __init void event_trace_self_test_with_function(void)
2696 static __init int event_trace_self_tests_init(void)
2698 if (!tracing_selftest_disabled) {
2699 event_trace_self_tests();
2700 event_trace_self_test_with_function();
2706 late_initcall(event_trace_self_tests_init);