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);
412 static void *event_file_data(struct file *filp)
414 return ACCESS_ONCE(file_inode(filp)->i_private);
418 * Open and update trace_array ref count.
419 * Must have the current trace_array passed to it.
421 static int tracing_open_generic_file(struct inode *inode, struct file *filp)
423 struct ftrace_event_file *file = inode->i_private;
424 struct trace_array *tr = file->tr;
427 if (trace_array_get(tr) < 0)
430 ret = tracing_open_generic(inode, filp);
436 static int tracing_release_generic_file(struct inode *inode, struct file *filp)
438 struct ftrace_event_file *file = inode->i_private;
439 struct trace_array *tr = file->tr;
447 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
450 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
451 const char *sub, const char *event, int set)
453 struct ftrace_event_file *file;
454 struct ftrace_event_call *call;
457 list_for_each_entry(file, &tr->events, list) {
459 call = file->event_call;
461 if (!call->name || !call->class || !call->class->reg)
464 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
468 strcmp(match, call->name) != 0 &&
469 strcmp(match, call->class->system) != 0)
472 if (sub && strcmp(sub, call->class->system) != 0)
475 if (event && strcmp(event, call->name) != 0)
478 ftrace_event_enable_disable(file, set);
486 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
487 const char *sub, const char *event, int set)
491 mutex_lock(&event_mutex);
492 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
493 mutex_unlock(&event_mutex);
498 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
500 char *event = NULL, *sub = NULL, *match;
503 * The buf format can be <subsystem>:<event-name>
504 * *:<event-name> means any event by that name.
505 * :<event-name> is the same.
507 * <subsystem>:* means all events in that subsystem
508 * <subsystem>: means the same.
510 * <name> (no ':') means all events in a subsystem with
511 * the name <name> or any event that matches <name>
514 match = strsep(&buf, ":");
520 if (!strlen(sub) || strcmp(sub, "*") == 0)
522 if (!strlen(event) || strcmp(event, "*") == 0)
526 return __ftrace_set_clr_event(tr, match, sub, event, set);
530 * trace_set_clr_event - enable or disable an event
531 * @system: system name to match (NULL for any system)
532 * @event: event name to match (NULL for all events, within system)
533 * @set: 1 to enable, 0 to disable
535 * This is a way for other parts of the kernel to enable or disable
538 * Returns 0 on success, -EINVAL if the parameters do not match any
541 int trace_set_clr_event(const char *system, const char *event, int set)
543 struct trace_array *tr = top_trace_array();
545 return __ftrace_set_clr_event(tr, NULL, system, event, set);
547 EXPORT_SYMBOL_GPL(trace_set_clr_event);
549 /* 128 should be much more than enough */
550 #define EVENT_BUF_SIZE 127
553 ftrace_event_write(struct file *file, const char __user *ubuf,
554 size_t cnt, loff_t *ppos)
556 struct trace_parser parser;
557 struct seq_file *m = file->private_data;
558 struct trace_array *tr = m->private;
564 ret = tracing_update_buffers();
568 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
571 read = trace_get_user(&parser, ubuf, cnt, ppos);
573 if (read >= 0 && trace_parser_loaded((&parser))) {
576 if (*parser.buffer == '!')
579 parser.buffer[parser.idx] = 0;
581 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
589 trace_parser_put(&parser);
595 t_next(struct seq_file *m, void *v, loff_t *pos)
597 struct ftrace_event_file *file = v;
598 struct ftrace_event_call *call;
599 struct trace_array *tr = m->private;
603 list_for_each_entry_continue(file, &tr->events, list) {
604 call = file->event_call;
606 * The ftrace subsystem is for showing formats only.
607 * They can not be enabled or disabled via the event files.
609 if (call->class && call->class->reg)
616 static void *t_start(struct seq_file *m, loff_t *pos)
618 struct ftrace_event_file *file;
619 struct trace_array *tr = m->private;
622 mutex_lock(&event_mutex);
624 file = list_entry(&tr->events, struct ftrace_event_file, list);
625 for (l = 0; l <= *pos; ) {
626 file = t_next(m, file, &l);
634 s_next(struct seq_file *m, void *v, loff_t *pos)
636 struct ftrace_event_file *file = v;
637 struct trace_array *tr = m->private;
641 list_for_each_entry_continue(file, &tr->events, list) {
642 if (file->flags & FTRACE_EVENT_FL_ENABLED)
649 static void *s_start(struct seq_file *m, loff_t *pos)
651 struct ftrace_event_file *file;
652 struct trace_array *tr = m->private;
655 mutex_lock(&event_mutex);
657 file = list_entry(&tr->events, struct ftrace_event_file, list);
658 for (l = 0; l <= *pos; ) {
659 file = s_next(m, file, &l);
666 static int t_show(struct seq_file *m, void *v)
668 struct ftrace_event_file *file = v;
669 struct ftrace_event_call *call = file->event_call;
671 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
672 seq_printf(m, "%s:", call->class->system);
673 seq_printf(m, "%s\n", call->name);
678 static void t_stop(struct seq_file *m, void *p)
680 mutex_unlock(&event_mutex);
684 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
687 struct ftrace_event_file *file;
691 mutex_lock(&event_mutex);
692 file = event_file_data(filp);
695 mutex_unlock(&event_mutex);
700 if (flags & FTRACE_EVENT_FL_ENABLED &&
701 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
704 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
705 flags & FTRACE_EVENT_FL_SOFT_MODE)
710 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
714 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
717 struct ftrace_event_file *file;
721 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
725 ret = tracing_update_buffers();
733 mutex_lock(&event_mutex);
734 file = event_file_data(filp);
736 ret = ftrace_event_enable_disable(file, val);
737 mutex_unlock(&event_mutex);
746 return ret ? ret : cnt;
750 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
753 const char set_to_char[4] = { '?', '0', '1', 'X' };
754 struct ftrace_subsystem_dir *dir = filp->private_data;
755 struct event_subsystem *system = dir->subsystem;
756 struct ftrace_event_call *call;
757 struct ftrace_event_file *file;
758 struct trace_array *tr = dir->tr;
763 mutex_lock(&event_mutex);
764 list_for_each_entry(file, &tr->events, list) {
765 call = file->event_call;
766 if (!call->name || !call->class || !call->class->reg)
769 if (system && strcmp(call->class->system, system->name) != 0)
773 * We need to find out if all the events are set
774 * or if all events or cleared, or if we have
777 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
780 * If we have a mixture, no need to look further.
785 mutex_unlock(&event_mutex);
787 buf[0] = set_to_char[set];
790 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
796 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
799 struct ftrace_subsystem_dir *dir = filp->private_data;
800 struct event_subsystem *system = dir->subsystem;
801 const char *name = NULL;
805 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
809 ret = tracing_update_buffers();
813 if (val != 0 && val != 1)
817 * Opening of "enable" adds a ref count to system,
818 * so the name is safe to use.
823 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
837 FORMAT_FIELD_SEPERATOR = 2,
841 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
843 struct ftrace_event_call *call = event_file_data(m->private);
844 struct list_head *common_head = &ftrace_common_fields;
845 struct list_head *head = trace_get_fields(call);
846 struct list_head *node = v;
850 switch ((unsigned long)v) {
855 case FORMAT_FIELD_SEPERATOR:
859 case FORMAT_PRINTFMT:
865 if (node == common_head)
866 return (void *)FORMAT_FIELD_SEPERATOR;
867 else if (node == head)
868 return (void *)FORMAT_PRINTFMT;
873 static int f_show(struct seq_file *m, void *v)
875 struct ftrace_event_call *call = event_file_data(m->private);
876 struct ftrace_event_field *field;
877 const char *array_descriptor;
879 switch ((unsigned long)v) {
881 seq_printf(m, "name: %s\n", call->name);
882 seq_printf(m, "ID: %d\n", call->event.type);
883 seq_printf(m, "format:\n");
886 case FORMAT_FIELD_SEPERATOR:
890 case FORMAT_PRINTFMT:
891 seq_printf(m, "\nprint fmt: %s\n",
896 field = list_entry(v, struct ftrace_event_field, link);
898 * Smartly shows the array type(except dynamic array).
901 * If TYPE := TYPE[LEN], it is shown:
902 * field:TYPE VAR[LEN]
904 array_descriptor = strchr(field->type, '[');
906 if (!strncmp(field->type, "__data_loc", 10))
907 array_descriptor = NULL;
909 if (!array_descriptor)
910 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
911 field->type, field->name, field->offset,
912 field->size, !!field->is_signed);
914 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
915 (int)(array_descriptor - field->type),
916 field->type, field->name,
917 array_descriptor, field->offset,
918 field->size, !!field->is_signed);
923 static void *f_start(struct seq_file *m, loff_t *pos)
925 void *p = (void *)FORMAT_HEADER;
928 /* ->stop() is called even if ->start() fails */
929 mutex_lock(&event_mutex);
930 if (!event_file_data(m->private))
931 return ERR_PTR(-ENODEV);
933 while (l < *pos && p)
934 p = f_next(m, p, &l);
939 static void f_stop(struct seq_file *m, void *p)
941 mutex_unlock(&event_mutex);
944 static const struct seq_operations trace_format_seq_ops = {
951 static int trace_format_open(struct inode *inode, struct file *file)
956 ret = seq_open(file, &trace_format_seq_ops);
960 m = file->private_data;
967 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
969 int id = (long)event_file_data(filp);
979 len = sprintf(buf, "%d\n", id);
981 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
985 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
988 struct ftrace_event_call *call;
995 s = kmalloc(sizeof(*s), GFP_KERNEL);
1002 mutex_lock(&event_mutex);
1003 call = event_file_data(filp);
1005 print_event_filter(call, s);
1006 mutex_unlock(&event_mutex);
1009 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1017 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1020 struct ftrace_event_call *call;
1024 if (cnt >= PAGE_SIZE)
1027 buf = (char *)__get_free_page(GFP_TEMPORARY);
1031 if (copy_from_user(buf, ubuf, cnt)) {
1032 free_page((unsigned long) buf);
1037 mutex_lock(&event_mutex);
1038 call = event_file_data(filp);
1040 err = apply_event_filter(call, buf);
1041 mutex_unlock(&event_mutex);
1043 free_page((unsigned long) buf);
1052 static LIST_HEAD(event_subsystems);
1054 static int subsystem_open(struct inode *inode, struct file *filp)
1056 struct event_subsystem *system = NULL;
1057 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1058 struct trace_array *tr;
1061 /* Make sure the system still exists */
1062 mutex_lock(&trace_types_lock);
1063 mutex_lock(&event_mutex);
1064 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1065 list_for_each_entry(dir, &tr->systems, list) {
1066 if (dir == inode->i_private) {
1067 /* Don't open systems with no events */
1068 if (dir->nr_events) {
1069 __get_system_dir(dir);
1070 system = dir->subsystem;
1077 mutex_unlock(&event_mutex);
1078 mutex_unlock(&trace_types_lock);
1083 /* Some versions of gcc think dir can be uninitialized here */
1086 /* Still need to increment the ref count of the system */
1087 if (trace_array_get(tr) < 0) {
1092 ret = tracing_open_generic(inode, filp);
1094 trace_array_put(tr);
1101 static int system_tr_open(struct inode *inode, struct file *filp)
1103 struct ftrace_subsystem_dir *dir;
1104 struct trace_array *tr = inode->i_private;
1107 if (trace_array_get(tr) < 0)
1110 /* Make a temporary dir that has no system but points to tr */
1111 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1113 trace_array_put(tr);
1119 ret = tracing_open_generic(inode, filp);
1121 trace_array_put(tr);
1125 filp->private_data = dir;
1130 static int subsystem_release(struct inode *inode, struct file *file)
1132 struct ftrace_subsystem_dir *dir = file->private_data;
1134 trace_array_put(dir->tr);
1137 * If dir->subsystem is NULL, then this is a temporary
1138 * descriptor that was made for a trace_array to enable
1150 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1153 struct ftrace_subsystem_dir *dir = filp->private_data;
1154 struct event_subsystem *system = dir->subsystem;
1155 struct trace_seq *s;
1161 s = kmalloc(sizeof(*s), GFP_KERNEL);
1167 print_subsystem_event_filter(system, s);
1168 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1176 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1179 struct ftrace_subsystem_dir *dir = filp->private_data;
1183 if (cnt >= PAGE_SIZE)
1186 buf = (char *)__get_free_page(GFP_TEMPORARY);
1190 if (copy_from_user(buf, ubuf, cnt)) {
1191 free_page((unsigned long) buf);
1196 err = apply_subsystem_event_filter(dir, buf);
1197 free_page((unsigned long) buf);
1207 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1209 int (*func)(struct trace_seq *s) = filp->private_data;
1210 struct trace_seq *s;
1216 s = kmalloc(sizeof(*s), GFP_KERNEL);
1223 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1230 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1231 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1232 static int ftrace_event_release(struct inode *inode, struct file *file);
1234 static const struct seq_operations show_event_seq_ops = {
1241 static const struct seq_operations show_set_event_seq_ops = {
1248 static const struct file_operations ftrace_avail_fops = {
1249 .open = ftrace_event_avail_open,
1251 .llseek = seq_lseek,
1252 .release = seq_release,
1255 static const struct file_operations ftrace_set_event_fops = {
1256 .open = ftrace_event_set_open,
1258 .write = ftrace_event_write,
1259 .llseek = seq_lseek,
1260 .release = ftrace_event_release,
1263 static const struct file_operations ftrace_enable_fops = {
1264 .open = tracing_open_generic_file,
1265 .read = event_enable_read,
1266 .write = event_enable_write,
1267 .release = tracing_release_generic_file,
1268 .llseek = default_llseek,
1271 static const struct file_operations ftrace_event_format_fops = {
1272 .open = trace_format_open,
1274 .llseek = seq_lseek,
1275 .release = seq_release,
1278 static const struct file_operations ftrace_event_id_fops = {
1279 .read = event_id_read,
1280 .llseek = default_llseek,
1283 static const struct file_operations ftrace_event_filter_fops = {
1284 .open = tracing_open_generic,
1285 .read = event_filter_read,
1286 .write = event_filter_write,
1287 .llseek = default_llseek,
1290 static const struct file_operations ftrace_subsystem_filter_fops = {
1291 .open = subsystem_open,
1292 .read = subsystem_filter_read,
1293 .write = subsystem_filter_write,
1294 .llseek = default_llseek,
1295 .release = subsystem_release,
1298 static const struct file_operations ftrace_system_enable_fops = {
1299 .open = subsystem_open,
1300 .read = system_enable_read,
1301 .write = system_enable_write,
1302 .llseek = default_llseek,
1303 .release = subsystem_release,
1306 static const struct file_operations ftrace_tr_enable_fops = {
1307 .open = system_tr_open,
1308 .read = system_enable_read,
1309 .write = system_enable_write,
1310 .llseek = default_llseek,
1311 .release = subsystem_release,
1314 static const struct file_operations ftrace_show_header_fops = {
1315 .open = tracing_open_generic,
1316 .read = show_header,
1317 .llseek = default_llseek,
1321 ftrace_event_open(struct inode *inode, struct file *file,
1322 const struct seq_operations *seq_ops)
1327 ret = seq_open(file, seq_ops);
1330 m = file->private_data;
1331 /* copy tr over to seq ops */
1332 m->private = inode->i_private;
1337 static int ftrace_event_release(struct inode *inode, struct file *file)
1339 struct trace_array *tr = inode->i_private;
1341 trace_array_put(tr);
1343 return seq_release(inode, file);
1347 ftrace_event_avail_open(struct inode *inode, struct file *file)
1349 const struct seq_operations *seq_ops = &show_event_seq_ops;
1351 return ftrace_event_open(inode, file, seq_ops);
1355 ftrace_event_set_open(struct inode *inode, struct file *file)
1357 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1358 struct trace_array *tr = inode->i_private;
1361 if (trace_array_get(tr) < 0)
1364 if ((file->f_mode & FMODE_WRITE) &&
1365 (file->f_flags & O_TRUNC))
1366 ftrace_clear_events(tr);
1368 ret = ftrace_event_open(inode, file, seq_ops);
1370 trace_array_put(tr);
1374 static struct event_subsystem *
1375 create_new_subsystem(const char *name)
1377 struct event_subsystem *system;
1379 /* need to create new entry */
1380 system = kmalloc(sizeof(*system), GFP_KERNEL);
1384 system->ref_count = 1;
1386 /* Only allocate if dynamic (kprobes and modules) */
1387 if (!core_kernel_data((unsigned long)name)) {
1388 system->ref_count |= SYSTEM_FL_FREE_NAME;
1389 system->name = kstrdup(name, GFP_KERNEL);
1393 system->name = name;
1395 system->filter = NULL;
1397 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1398 if (!system->filter)
1401 list_add(&system->list, &event_subsystems);
1406 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1407 kfree(system->name);
1412 static struct dentry *
1413 event_subsystem_dir(struct trace_array *tr, const char *name,
1414 struct ftrace_event_file *file, struct dentry *parent)
1416 struct ftrace_subsystem_dir *dir;
1417 struct event_subsystem *system;
1418 struct dentry *entry;
1420 /* First see if we did not already create this dir */
1421 list_for_each_entry(dir, &tr->systems, list) {
1422 system = dir->subsystem;
1423 if (strcmp(system->name, name) == 0) {
1430 /* Now see if the system itself exists. */
1431 list_for_each_entry(system, &event_subsystems, list) {
1432 if (strcmp(system->name, name) == 0)
1435 /* Reset system variable when not found */
1436 if (&system->list == &event_subsystems)
1439 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1444 system = create_new_subsystem(name);
1448 __get_system(system);
1450 dir->entry = debugfs_create_dir(name, parent);
1452 pr_warning("Failed to create system directory %s\n", name);
1453 __put_system(system);
1460 dir->subsystem = system;
1463 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1464 &ftrace_subsystem_filter_fops);
1466 kfree(system->filter);
1467 system->filter = NULL;
1468 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1471 trace_create_file("enable", 0644, dir->entry, dir,
1472 &ftrace_system_enable_fops);
1474 list_add(&dir->list, &tr->systems);
1481 /* Only print this message if failed on memory allocation */
1482 if (!dir || !system)
1483 pr_warning("No memory to create event subsystem %s\n",
1489 event_create_dir(struct dentry *parent,
1490 struct ftrace_event_file *file,
1491 const struct file_operations *id,
1492 const struct file_operations *enable,
1493 const struct file_operations *filter,
1494 const struct file_operations *format)
1496 struct ftrace_event_call *call = file->event_call;
1497 struct trace_array *tr = file->tr;
1498 struct list_head *head;
1499 struct dentry *d_events;
1503 * If the trace point header did not define TRACE_SYSTEM
1504 * then the system would be called "TRACE_SYSTEM".
1506 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1507 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1513 file->dir = debugfs_create_dir(call->name, d_events);
1515 pr_warning("Could not create debugfs '%s' directory\n",
1520 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1521 trace_create_file("enable", 0644, file->dir, file,
1524 #ifdef CONFIG_PERF_EVENTS
1525 if (call->event.type && call->class->reg)
1526 trace_create_file("id", 0444, file->dir,
1527 (void *)(long)call->event.type, id);
1531 * Other events may have the same class. Only update
1532 * the fields if they are not already defined.
1534 head = trace_get_fields(call);
1535 if (list_empty(head)) {
1536 ret = call->class->define_fields(call);
1538 pr_warning("Could not initialize trace point"
1539 " events/%s\n", call->name);
1543 trace_create_file("filter", 0644, file->dir, call,
1546 trace_create_file("format", 0444, file->dir, call,
1552 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1557 if (!--dir->nr_events) {
1558 debugfs_remove_recursive(dir->entry);
1559 list_del(&dir->list);
1560 __put_system_dir(dir);
1564 static void remove_event_from_tracers(struct ftrace_event_call *call)
1566 struct ftrace_event_file *file;
1567 struct trace_array *tr;
1569 do_for_each_event_file_safe(tr, file) {
1571 if (file->event_call != call)
1574 list_del(&file->list);
1575 debugfs_remove_recursive(file->dir);
1576 remove_subsystem(file->system);
1577 kmem_cache_free(file_cachep, file);
1580 * The do_for_each_event_file_safe() is
1581 * a double loop. After finding the call for this
1582 * trace_array, we use break to jump to the next
1586 } while_for_each_event_file();
1589 static void event_remove(struct ftrace_event_call *call)
1591 struct trace_array *tr;
1592 struct ftrace_event_file *file;
1594 do_for_each_event_file(tr, file) {
1595 if (file->event_call != call)
1597 ftrace_event_enable_disable(file, 0);
1599 * The do_for_each_event_file() is
1600 * a double loop. After finding the call for this
1601 * trace_array, we use break to jump to the next
1605 } while_for_each_event_file();
1607 if (call->event.funcs)
1608 __unregister_ftrace_event(&call->event);
1609 remove_event_from_tracers(call);
1610 list_del(&call->list);
1613 static int event_init(struct ftrace_event_call *call)
1617 if (WARN_ON(!call->name))
1620 if (call->class->raw_init) {
1621 ret = call->class->raw_init(call);
1622 if (ret < 0 && ret != -ENOSYS)
1623 pr_warn("Could not initialize trace events/%s\n",
1631 __register_event(struct ftrace_event_call *call, struct module *mod)
1635 ret = event_init(call);
1639 list_add(&call->list, &ftrace_events);
1645 static struct ftrace_event_file *
1646 trace_create_new_event(struct ftrace_event_call *call,
1647 struct trace_array *tr)
1649 struct ftrace_event_file *file;
1651 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1655 file->event_call = call;
1657 atomic_set(&file->sm_ref, 0);
1658 list_add(&file->list, &tr->events);
1663 /* Add an event to a trace directory */
1665 __trace_add_new_event(struct ftrace_event_call *call,
1666 struct trace_array *tr,
1667 const struct file_operations *id,
1668 const struct file_operations *enable,
1669 const struct file_operations *filter,
1670 const struct file_operations *format)
1672 struct ftrace_event_file *file;
1674 file = trace_create_new_event(call, tr);
1678 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1682 * Just create a decriptor for early init. A descriptor is required
1683 * for enabling events at boot. We want to enable events before
1684 * the filesystem is initialized.
1687 __trace_early_add_new_event(struct ftrace_event_call *call,
1688 struct trace_array *tr)
1690 struct ftrace_event_file *file;
1692 file = trace_create_new_event(call, tr);
1699 struct ftrace_module_file_ops;
1700 static void __add_event_to_tracers(struct ftrace_event_call *call,
1701 struct ftrace_module_file_ops *file_ops);
1703 /* Add an additional event_call dynamically */
1704 int trace_add_event_call(struct ftrace_event_call *call)
1707 mutex_lock(&trace_types_lock);
1708 mutex_lock(&event_mutex);
1710 ret = __register_event(call, NULL);
1712 __add_event_to_tracers(call, NULL);
1714 mutex_unlock(&event_mutex);
1715 mutex_unlock(&trace_types_lock);
1720 * Must be called under locking of trace_types_lock, event_mutex and
1723 static void __trace_remove_event_call(struct ftrace_event_call *call)
1726 trace_destroy_fields(call);
1727 destroy_preds(call);
1730 /* Remove an event_call */
1731 void trace_remove_event_call(struct ftrace_event_call *call)
1733 mutex_lock(&trace_types_lock);
1734 mutex_lock(&event_mutex);
1735 down_write(&trace_event_sem);
1736 __trace_remove_event_call(call);
1737 up_write(&trace_event_sem);
1738 mutex_unlock(&event_mutex);
1739 mutex_unlock(&trace_types_lock);
1742 #define for_each_event(event, start, end) \
1743 for (event = start; \
1744 (unsigned long)event < (unsigned long)end; \
1747 #ifdef CONFIG_MODULES
1749 static LIST_HEAD(ftrace_module_file_list);
1752 * Modules must own their file_operations to keep up with
1753 * reference counting.
1755 struct ftrace_module_file_ops {
1756 struct list_head list;
1758 struct file_operations id;
1759 struct file_operations enable;
1760 struct file_operations format;
1761 struct file_operations filter;
1764 static struct ftrace_module_file_ops *
1765 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1768 * As event_calls are added in groups by module,
1769 * when we find one file_ops, we don't need to search for
1770 * each call in that module, as the rest should be the
1771 * same. Only search for a new one if the last one did
1774 if (file_ops && mod == file_ops->mod)
1777 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1778 if (file_ops->mod == mod)
1784 static struct ftrace_module_file_ops *
1785 trace_create_file_ops(struct module *mod)
1787 struct ftrace_module_file_ops *file_ops;
1790 * This is a bit of a PITA. To allow for correct reference
1791 * counting, modules must "own" their file_operations.
1792 * To do this, we allocate the file operations that will be
1793 * used in the event directory.
1796 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1800 file_ops->mod = mod;
1802 file_ops->id = ftrace_event_id_fops;
1803 file_ops->id.owner = mod;
1805 file_ops->enable = ftrace_enable_fops;
1806 file_ops->enable.owner = mod;
1808 file_ops->filter = ftrace_event_filter_fops;
1809 file_ops->filter.owner = mod;
1811 file_ops->format = ftrace_event_format_fops;
1812 file_ops->format.owner = mod;
1814 list_add(&file_ops->list, &ftrace_module_file_list);
1819 static void trace_module_add_events(struct module *mod)
1821 struct ftrace_module_file_ops *file_ops = NULL;
1822 struct ftrace_event_call **call, **start, **end;
1824 start = mod->trace_events;
1825 end = mod->trace_events + mod->num_trace_events;
1830 file_ops = trace_create_file_ops(mod);
1834 for_each_event(call, start, end) {
1835 __register_event(*call, mod);
1836 __add_event_to_tracers(*call, file_ops);
1840 static void trace_module_remove_events(struct module *mod)
1842 struct ftrace_module_file_ops *file_ops;
1843 struct ftrace_event_call *call, *p;
1844 bool clear_trace = false;
1846 down_write(&trace_event_sem);
1847 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1848 if (call->mod == mod) {
1849 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1851 __trace_remove_event_call(call);
1855 /* Now free the file_operations */
1856 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1857 if (file_ops->mod == mod)
1860 if (&file_ops->list != &ftrace_module_file_list) {
1861 list_del(&file_ops->list);
1864 up_write(&trace_event_sem);
1867 * It is safest to reset the ring buffer if the module being unloaded
1868 * registered any events that were used. The only worry is if
1869 * a new module gets loaded, and takes on the same id as the events
1870 * of this module. When printing out the buffer, traced events left
1871 * over from this module may be passed to the new module events and
1872 * unexpected results may occur.
1875 tracing_reset_all_online_cpus();
1878 static int trace_module_notify(struct notifier_block *self,
1879 unsigned long val, void *data)
1881 struct module *mod = data;
1883 mutex_lock(&trace_types_lock);
1884 mutex_lock(&event_mutex);
1886 case MODULE_STATE_COMING:
1887 trace_module_add_events(mod);
1889 case MODULE_STATE_GOING:
1890 trace_module_remove_events(mod);
1893 mutex_unlock(&event_mutex);
1894 mutex_unlock(&trace_types_lock);
1900 __trace_add_new_mod_event(struct ftrace_event_call *call,
1901 struct trace_array *tr,
1902 struct ftrace_module_file_ops *file_ops)
1904 return __trace_add_new_event(call, tr,
1905 &file_ops->id, &file_ops->enable,
1906 &file_ops->filter, &file_ops->format);
1910 static inline struct ftrace_module_file_ops *
1911 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1915 static inline int trace_module_notify(struct notifier_block *self,
1916 unsigned long val, void *data)
1921 __trace_add_new_mod_event(struct ftrace_event_call *call,
1922 struct trace_array *tr,
1923 struct ftrace_module_file_ops *file_ops)
1927 #endif /* CONFIG_MODULES */
1929 /* Create a new event directory structure for a trace directory. */
1931 __trace_add_event_dirs(struct trace_array *tr)
1933 struct ftrace_module_file_ops *file_ops = NULL;
1934 struct ftrace_event_call *call;
1937 list_for_each_entry(call, &ftrace_events, list) {
1940 * Directories for events by modules need to
1941 * keep module ref counts when opened (as we don't
1942 * want the module to disappear when reading one
1943 * of these files). The file_ops keep account of
1944 * the module ref count.
1946 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1948 continue; /* Warn? */
1949 ret = __trace_add_new_mod_event(call, tr, file_ops);
1951 pr_warning("Could not create directory for event %s\n",
1955 ret = __trace_add_new_event(call, tr,
1956 &ftrace_event_id_fops,
1957 &ftrace_enable_fops,
1958 &ftrace_event_filter_fops,
1959 &ftrace_event_format_fops);
1961 pr_warning("Could not create directory for event %s\n",
1966 #ifdef CONFIG_DYNAMIC_FTRACE
1969 #define ENABLE_EVENT_STR "enable_event"
1970 #define DISABLE_EVENT_STR "disable_event"
1972 struct event_probe_data {
1973 struct ftrace_event_file *file;
1974 unsigned long count;
1979 static struct ftrace_event_file *
1980 find_event_file(struct trace_array *tr, const char *system, const char *event)
1982 struct ftrace_event_file *file;
1983 struct ftrace_event_call *call;
1985 list_for_each_entry(file, &tr->events, list) {
1987 call = file->event_call;
1989 if (!call->name || !call->class || !call->class->reg)
1992 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1995 if (strcmp(event, call->name) == 0 &&
1996 strcmp(system, call->class->system) == 0)
2003 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2005 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2006 struct event_probe_data *data = *pdata;
2012 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2014 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2018 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2020 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2021 struct event_probe_data *data = *pdata;
2029 /* Skip if the event is in a state we want to switch to */
2030 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2033 if (data->count != -1)
2036 event_enable_probe(ip, parent_ip, _data);
2040 event_enable_print(struct seq_file *m, unsigned long ip,
2041 struct ftrace_probe_ops *ops, void *_data)
2043 struct event_probe_data *data = _data;
2045 seq_printf(m, "%ps:", (void *)ip);
2047 seq_printf(m, "%s:%s:%s",
2048 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2049 data->file->event_call->class->system,
2050 data->file->event_call->name);
2052 if (data->count == -1)
2053 seq_printf(m, ":unlimited\n");
2055 seq_printf(m, ":count=%ld\n", data->count);
2061 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2064 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2065 struct event_probe_data *data = *pdata;
2072 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2075 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2076 struct event_probe_data *data = *pdata;
2078 if (WARN_ON_ONCE(data->ref <= 0))
2083 /* Remove the SOFT_MODE flag */
2084 __ftrace_event_enable_disable(data->file, 0, 1);
2085 module_put(data->file->event_call->mod);
2091 static struct ftrace_probe_ops event_enable_probe_ops = {
2092 .func = event_enable_probe,
2093 .print = event_enable_print,
2094 .init = event_enable_init,
2095 .free = event_enable_free,
2098 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2099 .func = event_enable_count_probe,
2100 .print = event_enable_print,
2101 .init = event_enable_init,
2102 .free = event_enable_free,
2105 static struct ftrace_probe_ops event_disable_probe_ops = {
2106 .func = event_enable_probe,
2107 .print = event_enable_print,
2108 .init = event_enable_init,
2109 .free = event_enable_free,
2112 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2113 .func = event_enable_count_probe,
2114 .print = event_enable_print,
2115 .init = event_enable_init,
2116 .free = event_enable_free,
2120 event_enable_func(struct ftrace_hash *hash,
2121 char *glob, char *cmd, char *param, int enabled)
2123 struct trace_array *tr = top_trace_array();
2124 struct ftrace_event_file *file;
2125 struct ftrace_probe_ops *ops;
2126 struct event_probe_data *data;
2133 /* hash funcs only work with set_ftrace_filter */
2134 if (!enabled || !param)
2137 system = strsep(¶m, ":");
2141 event = strsep(¶m, ":");
2143 mutex_lock(&event_mutex);
2146 file = find_event_file(tr, system, event);
2150 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2153 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2155 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2157 if (glob[0] == '!') {
2158 unregister_ftrace_function_probe_func(glob+1, ops);
2164 data = kzalloc(sizeof(*data), GFP_KERNEL);
2168 data->enable = enable;
2175 number = strsep(¶m, ":");
2178 if (!strlen(number))
2182 * We use the callback data field (which is a pointer)
2185 ret = kstrtoul(number, 0, &data->count);
2190 /* Don't let event modules unload while probe registered */
2191 ret = try_module_get(file->event_call->mod);
2197 ret = __ftrace_event_enable_disable(file, 1, 1);
2200 ret = register_ftrace_function_probe(glob, ops, data);
2202 * The above returns on success the # of functions enabled,
2203 * but if it didn't find any functions it returns zero.
2204 * Consider no functions a failure too.
2211 /* Just return zero, not the number of enabled functions */
2214 mutex_unlock(&event_mutex);
2218 __ftrace_event_enable_disable(file, 0, 1);
2220 module_put(file->event_call->mod);
2226 static struct ftrace_func_command event_enable_cmd = {
2227 .name = ENABLE_EVENT_STR,
2228 .func = event_enable_func,
2231 static struct ftrace_func_command event_disable_cmd = {
2232 .name = DISABLE_EVENT_STR,
2233 .func = event_enable_func,
2236 static __init int register_event_cmds(void)
2240 ret = register_ftrace_command(&event_enable_cmd);
2241 if (WARN_ON(ret < 0))
2243 ret = register_ftrace_command(&event_disable_cmd);
2244 if (WARN_ON(ret < 0))
2245 unregister_ftrace_command(&event_enable_cmd);
2249 static inline int register_event_cmds(void) { return 0; }
2250 #endif /* CONFIG_DYNAMIC_FTRACE */
2253 * The top level array has already had its ftrace_event_file
2254 * descriptors created in order to allow for early events to
2255 * be recorded. This function is called after the debugfs has been
2256 * initialized, and we now have to create the files associated
2260 __trace_early_add_event_dirs(struct trace_array *tr)
2262 struct ftrace_event_file *file;
2266 list_for_each_entry(file, &tr->events, list) {
2267 ret = event_create_dir(tr->event_dir, file,
2268 &ftrace_event_id_fops,
2269 &ftrace_enable_fops,
2270 &ftrace_event_filter_fops,
2271 &ftrace_event_format_fops);
2273 pr_warning("Could not create directory for event %s\n",
2274 file->event_call->name);
2279 * For early boot up, the top trace array requires to have
2280 * a list of events that can be enabled. This must be done before
2281 * the filesystem is set up in order to allow events to be traced
2285 __trace_early_add_events(struct trace_array *tr)
2287 struct ftrace_event_call *call;
2290 list_for_each_entry(call, &ftrace_events, list) {
2291 /* Early boot up should not have any modules loaded */
2292 if (WARN_ON_ONCE(call->mod))
2295 ret = __trace_early_add_new_event(call, tr);
2297 pr_warning("Could not create early event %s\n",
2302 /* Remove the event directory structure for a trace directory. */
2304 __trace_remove_event_dirs(struct trace_array *tr)
2306 struct ftrace_event_file *file, *next;
2308 list_for_each_entry_safe(file, next, &tr->events, list) {
2309 list_del(&file->list);
2310 debugfs_remove_recursive(file->dir);
2311 remove_subsystem(file->system);
2312 kmem_cache_free(file_cachep, file);
2317 __add_event_to_tracers(struct ftrace_event_call *call,
2318 struct ftrace_module_file_ops *file_ops)
2320 struct trace_array *tr;
2322 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2324 __trace_add_new_mod_event(call, tr, file_ops);
2326 __trace_add_new_event(call, tr,
2327 &ftrace_event_id_fops,
2328 &ftrace_enable_fops,
2329 &ftrace_event_filter_fops,
2330 &ftrace_event_format_fops);
2334 static struct notifier_block trace_module_nb = {
2335 .notifier_call = trace_module_notify,
2339 extern struct ftrace_event_call *__start_ftrace_events[];
2340 extern struct ftrace_event_call *__stop_ftrace_events[];
2342 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2344 static __init int setup_trace_event(char *str)
2346 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2347 ring_buffer_expanded = true;
2348 tracing_selftest_disabled = true;
2352 __setup("trace_event=", setup_trace_event);
2354 /* Expects to have event_mutex held when called */
2356 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2358 struct dentry *d_events;
2359 struct dentry *entry;
2361 entry = debugfs_create_file("set_event", 0644, parent,
2362 tr, &ftrace_set_event_fops);
2364 pr_warning("Could not create debugfs 'set_event' entry\n");
2368 d_events = debugfs_create_dir("events", parent);
2370 pr_warning("Could not create debugfs 'events' directory\n");
2374 /* ring buffer internal formats */
2375 trace_create_file("header_page", 0444, d_events,
2376 ring_buffer_print_page_header,
2377 &ftrace_show_header_fops);
2379 trace_create_file("header_event", 0444, d_events,
2380 ring_buffer_print_entry_header,
2381 &ftrace_show_header_fops);
2383 trace_create_file("enable", 0644, d_events,
2384 tr, &ftrace_tr_enable_fops);
2386 tr->event_dir = d_events;
2392 * event_trace_add_tracer - add a instance of a trace_array to events
2393 * @parent: The parent dentry to place the files/directories for events in
2394 * @tr: The trace array associated with these events
2396 * When a new instance is created, it needs to set up its events
2397 * directory, as well as other files associated with events. It also
2398 * creates the event hierachry in the @parent/events directory.
2400 * Returns 0 on success.
2402 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2406 mutex_lock(&event_mutex);
2408 ret = create_event_toplevel_files(parent, tr);
2412 down_write(&trace_event_sem);
2413 __trace_add_event_dirs(tr);
2414 up_write(&trace_event_sem);
2417 mutex_unlock(&event_mutex);
2423 * The top trace array already had its file descriptors created.
2424 * Now the files themselves need to be created.
2427 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2431 mutex_lock(&event_mutex);
2433 ret = create_event_toplevel_files(parent, tr);
2437 down_write(&trace_event_sem);
2438 __trace_early_add_event_dirs(tr);
2439 up_write(&trace_event_sem);
2442 mutex_unlock(&event_mutex);
2447 int event_trace_del_tracer(struct trace_array *tr)
2449 mutex_lock(&event_mutex);
2451 /* Disable any running events */
2452 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2454 down_write(&trace_event_sem);
2455 __trace_remove_event_dirs(tr);
2456 debugfs_remove_recursive(tr->event_dir);
2457 up_write(&trace_event_sem);
2459 tr->event_dir = NULL;
2461 mutex_unlock(&event_mutex);
2466 static __init int event_trace_memsetup(void)
2468 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2469 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2473 static __init int event_trace_enable(void)
2475 struct trace_array *tr = top_trace_array();
2476 struct ftrace_event_call **iter, *call;
2477 char *buf = bootup_event_buf;
2481 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2484 ret = event_init(call);
2486 list_add(&call->list, &ftrace_events);
2490 * We need the top trace array to have a working set of trace
2491 * points at early init, before the debug files and directories
2492 * are created. Create the file entries now, and attach them
2493 * to the actual file dentries later.
2495 __trace_early_add_events(tr);
2498 token = strsep(&buf, ",");
2505 ret = ftrace_set_clr_event(tr, token, 1);
2507 pr_warn("Failed to enable trace event: %s\n", token);
2510 trace_printk_start_comm();
2512 register_event_cmds();
2517 static __init int event_trace_init(void)
2519 struct trace_array *tr;
2520 struct dentry *d_tracer;
2521 struct dentry *entry;
2524 tr = top_trace_array();
2526 d_tracer = tracing_init_dentry();
2530 entry = debugfs_create_file("available_events", 0444, d_tracer,
2531 tr, &ftrace_avail_fops);
2533 pr_warning("Could not create debugfs "
2534 "'available_events' entry\n");
2536 if (trace_define_common_fields())
2537 pr_warning("tracing: Failed to allocate common fields");
2539 ret = early_event_add_tracer(d_tracer, tr);
2543 ret = register_module_notifier(&trace_module_nb);
2545 pr_warning("Failed to register trace events module notifier\n");
2549 early_initcall(event_trace_memsetup);
2550 core_initcall(event_trace_enable);
2551 fs_initcall(event_trace_init);
2553 #ifdef CONFIG_FTRACE_STARTUP_TEST
2555 static DEFINE_SPINLOCK(test_spinlock);
2556 static DEFINE_SPINLOCK(test_spinlock_irq);
2557 static DEFINE_MUTEX(test_mutex);
2559 static __init void test_work(struct work_struct *dummy)
2561 spin_lock(&test_spinlock);
2562 spin_lock_irq(&test_spinlock_irq);
2564 spin_unlock_irq(&test_spinlock_irq);
2565 spin_unlock(&test_spinlock);
2567 mutex_lock(&test_mutex);
2569 mutex_unlock(&test_mutex);
2572 static __init int event_test_thread(void *unused)
2576 test_malloc = kmalloc(1234, GFP_KERNEL);
2578 pr_info("failed to kmalloc\n");
2580 schedule_on_each_cpu(test_work);
2584 set_current_state(TASK_INTERRUPTIBLE);
2585 while (!kthread_should_stop())
2592 * Do various things that may trigger events.
2594 static __init void event_test_stuff(void)
2596 struct task_struct *test_thread;
2598 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2600 kthread_stop(test_thread);
2604 * For every trace event defined, we will test each trace point separately,
2605 * and then by groups, and finally all trace points.
2607 static __init void event_trace_self_tests(void)
2609 struct ftrace_subsystem_dir *dir;
2610 struct ftrace_event_file *file;
2611 struct ftrace_event_call *call;
2612 struct event_subsystem *system;
2613 struct trace_array *tr;
2616 tr = top_trace_array();
2618 pr_info("Running tests on trace events:\n");
2620 list_for_each_entry(file, &tr->events, list) {
2622 call = file->event_call;
2624 /* Only test those that have a probe */
2625 if (!call->class || !call->class->probe)
2629 * Testing syscall events here is pretty useless, but
2630 * we still do it if configured. But this is time consuming.
2631 * What we really need is a user thread to perform the
2632 * syscalls as we test.
2634 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2635 if (call->class->system &&
2636 strcmp(call->class->system, "syscalls") == 0)
2640 pr_info("Testing event %s: ", call->name);
2643 * If an event is already enabled, someone is using
2644 * it and the self test should not be on.
2646 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2647 pr_warning("Enabled event during self test!\n");
2652 ftrace_event_enable_disable(file, 1);
2654 ftrace_event_enable_disable(file, 0);
2659 /* Now test at the sub system level */
2661 pr_info("Running tests on trace event systems:\n");
2663 list_for_each_entry(dir, &tr->systems, list) {
2665 system = dir->subsystem;
2667 /* the ftrace system is special, skip it */
2668 if (strcmp(system->name, "ftrace") == 0)
2671 pr_info("Testing event system %s: ", system->name);
2673 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2674 if (WARN_ON_ONCE(ret)) {
2675 pr_warning("error enabling system %s\n",
2682 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2683 if (WARN_ON_ONCE(ret)) {
2684 pr_warning("error disabling system %s\n",
2692 /* Test with all events enabled */
2694 pr_info("Running tests on all trace events:\n");
2695 pr_info("Testing all events: ");
2697 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2698 if (WARN_ON_ONCE(ret)) {
2699 pr_warning("error enabling all events\n");
2706 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2707 if (WARN_ON_ONCE(ret)) {
2708 pr_warning("error disabling all events\n");
2715 #ifdef CONFIG_FUNCTION_TRACER
2717 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2720 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2721 struct ftrace_ops *op, struct pt_regs *pt_regs)
2723 struct ring_buffer_event *event;
2724 struct ring_buffer *buffer;
2725 struct ftrace_entry *entry;
2726 unsigned long flags;
2731 pc = preempt_count();
2732 preempt_disable_notrace();
2733 cpu = raw_smp_processor_id();
2734 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2739 local_save_flags(flags);
2741 event = trace_current_buffer_lock_reserve(&buffer,
2742 TRACE_FN, sizeof(*entry),
2746 entry = ring_buffer_event_data(event);
2748 entry->parent_ip = parent_ip;
2750 trace_buffer_unlock_commit(buffer, event, flags, pc);
2753 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2754 preempt_enable_notrace();
2757 static struct ftrace_ops trace_ops __initdata =
2759 .func = function_test_events_call,
2760 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2763 static __init void event_trace_self_test_with_function(void)
2766 ret = register_ftrace_function(&trace_ops);
2767 if (WARN_ON(ret < 0)) {
2768 pr_info("Failed to enable function tracer for event tests\n");
2771 pr_info("Running tests again, along with the function tracer\n");
2772 event_trace_self_tests();
2773 unregister_ftrace_function(&trace_ops);
2776 static __init void event_trace_self_test_with_function(void)
2781 static __init int event_trace_self_tests_init(void)
2783 if (!tracing_selftest_disabled) {
2784 event_trace_self_tests();
2785 event_trace_self_test_with_function();
2791 late_initcall(event_trace_self_tests_init);