4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
31 static LIST_HEAD(ftrace_common_fields);
33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
35 static struct kmem_cache *field_cachep;
36 static struct kmem_cache *file_cachep;
38 #define SYSTEM_FL_FREE_NAME (1 << 31)
40 static inline int system_refcount(struct event_subsystem *system)
42 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
45 static int system_refcount_inc(struct event_subsystem *system)
47 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
50 static int system_refcount_dec(struct event_subsystem *system)
52 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
55 /* Double loops, do not use break, only goto's work */
56 #define do_for_each_event_file(tr, file) \
57 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 list_for_each_entry(file, &tr->events, list)
60 #define do_for_each_event_file_safe(tr, file) \
61 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 struct ftrace_event_file *___n; \
63 list_for_each_entry_safe(file, ___n, &tr->events, list)
65 #define while_for_each_event_file() \
68 static struct list_head *
69 trace_get_fields(struct ftrace_event_call *event_call)
71 if (!event_call->class->get_fields)
72 return &event_call->class->fields;
73 return event_call->class->get_fields(event_call);
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
79 struct ftrace_event_field *field;
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
89 struct ftrace_event_field *
90 trace_find_event_field(struct ftrace_event_call *call, char *name)
92 struct ftrace_event_field *field;
93 struct list_head *head;
95 field = __find_event_field(&ftrace_common_fields, name);
99 head = trace_get_fields(call);
100 return __find_event_field(head, name);
103 static int __trace_define_field(struct list_head *head, const char *type,
104 const char *name, int offset, int size,
105 int is_signed, int filter_type)
107 struct ftrace_event_field *field;
109 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
116 if (filter_type == FILTER_OTHER)
117 field->filter_type = filter_assign_type(type);
119 field->filter_type = filter_type;
121 field->offset = offset;
123 field->is_signed = is_signed;
125 list_add(&field->link, head);
130 int trace_define_field(struct ftrace_event_call *call, const char *type,
131 const char *name, int offset, int size, int is_signed,
134 struct list_head *head;
136 if (WARN_ON(!call->class))
139 head = trace_get_fields(call);
140 return __trace_define_field(head, type, name, offset, size,
141 is_signed, filter_type);
143 EXPORT_SYMBOL_GPL(trace_define_field);
145 #define __common_field(type, item) \
146 ret = __trace_define_field(&ftrace_common_fields, #type, \
148 offsetof(typeof(ent), item), \
150 is_signed_type(type), FILTER_OTHER); \
154 static int trace_define_common_fields(void)
157 struct trace_entry ent;
159 __common_field(unsigned short, type);
160 __common_field(unsigned char, flags);
161 __common_field(unsigned char, preempt_count);
162 __common_field(int, pid);
167 static void trace_destroy_fields(struct ftrace_event_call *call)
169 struct ftrace_event_field *field, *next;
170 struct list_head *head;
172 head = trace_get_fields(call);
173 list_for_each_entry_safe(field, next, head, link) {
174 list_del(&field->link);
175 kmem_cache_free(field_cachep, field);
179 int trace_event_raw_init(struct ftrace_event_call *call)
183 id = register_ftrace_event(&call->event);
189 EXPORT_SYMBOL_GPL(trace_event_raw_init);
191 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer *fbuffer,
192 struct ftrace_event_file *ftrace_file,
195 struct ftrace_event_call *event_call = ftrace_file->event_call;
197 local_save_flags(fbuffer->flags);
198 fbuffer->pc = preempt_count();
199 fbuffer->ftrace_file = ftrace_file;
202 trace_event_buffer_lock_reserve(&fbuffer->buffer, ftrace_file,
203 event_call->event.type, len,
204 fbuffer->flags, fbuffer->pc);
208 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
209 return fbuffer->entry;
211 EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve);
213 void ftrace_event_buffer_commit(struct ftrace_event_buffer *fbuffer)
215 event_trigger_unlock_commit(fbuffer->ftrace_file, fbuffer->buffer,
216 fbuffer->event, fbuffer->entry,
217 fbuffer->flags, fbuffer->pc);
219 EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit);
221 int ftrace_event_reg(struct ftrace_event_call *call,
222 enum trace_reg type, void *data)
224 struct ftrace_event_file *file = data;
227 case TRACE_REG_REGISTER:
228 return tracepoint_probe_register(call->name,
231 case TRACE_REG_UNREGISTER:
232 tracepoint_probe_unregister(call->name,
237 #ifdef CONFIG_PERF_EVENTS
238 case TRACE_REG_PERF_REGISTER:
239 return tracepoint_probe_register(call->name,
240 call->class->perf_probe,
242 case TRACE_REG_PERF_UNREGISTER:
243 tracepoint_probe_unregister(call->name,
244 call->class->perf_probe,
247 case TRACE_REG_PERF_OPEN:
248 case TRACE_REG_PERF_CLOSE:
249 case TRACE_REG_PERF_ADD:
250 case TRACE_REG_PERF_DEL:
256 EXPORT_SYMBOL_GPL(ftrace_event_reg);
258 void trace_event_enable_cmd_record(bool enable)
260 struct ftrace_event_file *file;
261 struct trace_array *tr;
263 mutex_lock(&event_mutex);
264 do_for_each_event_file(tr, file) {
266 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
270 tracing_start_cmdline_record();
271 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
273 tracing_stop_cmdline_record();
274 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
276 } while_for_each_event_file();
277 mutex_unlock(&event_mutex);
280 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
281 int enable, int soft_disable)
283 struct ftrace_event_call *call = file->event_call;
290 * When soft_disable is set and enable is cleared, the sm_ref
291 * reference counter is decremented. If it reaches 0, we want
292 * to clear the SOFT_DISABLED flag but leave the event in the
293 * state that it was. That is, if the event was enabled and
294 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
295 * is set we do not want the event to be enabled before we
298 * When soft_disable is not set but the SOFT_MODE flag is,
299 * we do nothing. Do not disable the tracepoint, otherwise
300 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
303 if (atomic_dec_return(&file->sm_ref) > 0)
305 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
306 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
308 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
310 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
311 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
312 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
313 tracing_stop_cmdline_record();
314 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
316 call->class->reg(call, TRACE_REG_UNREGISTER, file);
318 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
319 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
320 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
322 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
326 * When soft_disable is set and enable is set, we want to
327 * register the tracepoint for the event, but leave the event
328 * as is. That means, if the event was already enabled, we do
329 * nothing (but set SOFT_MODE). If the event is disabled, we
330 * set SOFT_DISABLED before enabling the event tracepoint, so
331 * it still seems to be disabled.
334 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
336 if (atomic_inc_return(&file->sm_ref) > 1)
338 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
341 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
343 /* Keep the event disabled, when going to SOFT_MODE. */
345 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
347 if (trace_flags & TRACE_ITER_RECORD_CMD) {
348 tracing_start_cmdline_record();
349 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
351 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
353 tracing_stop_cmdline_record();
354 pr_info("event trace: Could not enable event "
358 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
360 /* WAS_ENABLED gets set but never cleared. */
361 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
369 int trace_event_enable_disable(struct ftrace_event_file *file,
370 int enable, int soft_disable)
372 return __ftrace_event_enable_disable(file, enable, soft_disable);
375 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
378 return __ftrace_event_enable_disable(file, enable, 0);
381 static void ftrace_clear_events(struct trace_array *tr)
383 struct ftrace_event_file *file;
385 mutex_lock(&event_mutex);
386 list_for_each_entry(file, &tr->events, list) {
387 ftrace_event_enable_disable(file, 0);
389 mutex_unlock(&event_mutex);
392 static void __put_system(struct event_subsystem *system)
394 struct event_filter *filter = system->filter;
396 WARN_ON_ONCE(system_refcount(system) == 0);
397 if (system_refcount_dec(system))
400 list_del(&system->list);
403 kfree(filter->filter_string);
406 if (system->ref_count & SYSTEM_FL_FREE_NAME)
411 static void __get_system(struct event_subsystem *system)
413 WARN_ON_ONCE(system_refcount(system) == 0);
414 system_refcount_inc(system);
417 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
419 WARN_ON_ONCE(dir->ref_count == 0);
421 __get_system(dir->subsystem);
424 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
426 WARN_ON_ONCE(dir->ref_count == 0);
427 /* If the subsystem is about to be freed, the dir must be too */
428 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
430 __put_system(dir->subsystem);
431 if (!--dir->ref_count)
435 static void put_system(struct ftrace_subsystem_dir *dir)
437 mutex_lock(&event_mutex);
438 __put_system_dir(dir);
439 mutex_unlock(&event_mutex);
442 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
447 if (!--dir->nr_events) {
448 debugfs_remove_recursive(dir->entry);
449 list_del(&dir->list);
450 __put_system_dir(dir);
454 static void remove_event_file_dir(struct ftrace_event_file *file)
456 struct dentry *dir = file->dir;
457 struct dentry *child;
460 spin_lock(&dir->d_lock); /* probably unneeded */
461 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
462 if (child->d_inode) /* probably unneeded */
463 child->d_inode->i_private = NULL;
465 spin_unlock(&dir->d_lock);
467 debugfs_remove_recursive(dir);
470 list_del(&file->list);
471 remove_subsystem(file->system);
472 kmem_cache_free(file_cachep, file);
476 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
479 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
480 const char *sub, const char *event, int set)
482 struct ftrace_event_file *file;
483 struct ftrace_event_call *call;
486 list_for_each_entry(file, &tr->events, list) {
488 call = file->event_call;
490 if (!call->name || !call->class || !call->class->reg)
493 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
497 strcmp(match, call->name) != 0 &&
498 strcmp(match, call->class->system) != 0)
501 if (sub && strcmp(sub, call->class->system) != 0)
504 if (event && strcmp(event, call->name) != 0)
507 ftrace_event_enable_disable(file, set);
515 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
516 const char *sub, const char *event, int set)
520 mutex_lock(&event_mutex);
521 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
522 mutex_unlock(&event_mutex);
527 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
529 char *event = NULL, *sub = NULL, *match;
532 * The buf format can be <subsystem>:<event-name>
533 * *:<event-name> means any event by that name.
534 * :<event-name> is the same.
536 * <subsystem>:* means all events in that subsystem
537 * <subsystem>: means the same.
539 * <name> (no ':') means all events in a subsystem with
540 * the name <name> or any event that matches <name>
543 match = strsep(&buf, ":");
549 if (!strlen(sub) || strcmp(sub, "*") == 0)
551 if (!strlen(event) || strcmp(event, "*") == 0)
555 return __ftrace_set_clr_event(tr, match, sub, event, set);
559 * trace_set_clr_event - enable or disable an event
560 * @system: system name to match (NULL for any system)
561 * @event: event name to match (NULL for all events, within system)
562 * @set: 1 to enable, 0 to disable
564 * This is a way for other parts of the kernel to enable or disable
567 * Returns 0 on success, -EINVAL if the parameters do not match any
570 int trace_set_clr_event(const char *system, const char *event, int set)
572 struct trace_array *tr = top_trace_array();
574 return __ftrace_set_clr_event(tr, NULL, system, event, set);
576 EXPORT_SYMBOL_GPL(trace_set_clr_event);
578 /* 128 should be much more than enough */
579 #define EVENT_BUF_SIZE 127
582 ftrace_event_write(struct file *file, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
585 struct trace_parser parser;
586 struct seq_file *m = file->private_data;
587 struct trace_array *tr = m->private;
593 ret = tracing_update_buffers();
597 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
600 read = trace_get_user(&parser, ubuf, cnt, ppos);
602 if (read >= 0 && trace_parser_loaded((&parser))) {
605 if (*parser.buffer == '!')
608 parser.buffer[parser.idx] = 0;
610 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
618 trace_parser_put(&parser);
624 t_next(struct seq_file *m, void *v, loff_t *pos)
626 struct ftrace_event_file *file = v;
627 struct ftrace_event_call *call;
628 struct trace_array *tr = m->private;
632 list_for_each_entry_continue(file, &tr->events, list) {
633 call = file->event_call;
635 * The ftrace subsystem is for showing formats only.
636 * They can not be enabled or disabled via the event files.
638 if (call->class && call->class->reg)
645 static void *t_start(struct seq_file *m, loff_t *pos)
647 struct ftrace_event_file *file;
648 struct trace_array *tr = m->private;
651 mutex_lock(&event_mutex);
653 file = list_entry(&tr->events, struct ftrace_event_file, list);
654 for (l = 0; l <= *pos; ) {
655 file = t_next(m, file, &l);
663 s_next(struct seq_file *m, void *v, loff_t *pos)
665 struct ftrace_event_file *file = v;
666 struct trace_array *tr = m->private;
670 list_for_each_entry_continue(file, &tr->events, list) {
671 if (file->flags & FTRACE_EVENT_FL_ENABLED)
678 static void *s_start(struct seq_file *m, loff_t *pos)
680 struct ftrace_event_file *file;
681 struct trace_array *tr = m->private;
684 mutex_lock(&event_mutex);
686 file = list_entry(&tr->events, struct ftrace_event_file, list);
687 for (l = 0; l <= *pos; ) {
688 file = s_next(m, file, &l);
695 static int t_show(struct seq_file *m, void *v)
697 struct ftrace_event_file *file = v;
698 struct ftrace_event_call *call = file->event_call;
700 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
701 seq_printf(m, "%s:", call->class->system);
702 seq_printf(m, "%s\n", call->name);
707 static void t_stop(struct seq_file *m, void *p)
709 mutex_unlock(&event_mutex);
713 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
716 struct ftrace_event_file *file;
720 mutex_lock(&event_mutex);
721 file = event_file_data(filp);
724 mutex_unlock(&event_mutex);
729 if (flags & FTRACE_EVENT_FL_ENABLED &&
730 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
733 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
734 flags & FTRACE_EVENT_FL_SOFT_MODE)
739 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
743 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
746 struct ftrace_event_file *file;
750 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
754 ret = tracing_update_buffers();
762 mutex_lock(&event_mutex);
763 file = event_file_data(filp);
765 ret = ftrace_event_enable_disable(file, val);
766 mutex_unlock(&event_mutex);
775 return ret ? ret : cnt;
779 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
782 const char set_to_char[4] = { '?', '0', '1', 'X' };
783 struct ftrace_subsystem_dir *dir = filp->private_data;
784 struct event_subsystem *system = dir->subsystem;
785 struct ftrace_event_call *call;
786 struct ftrace_event_file *file;
787 struct trace_array *tr = dir->tr;
792 mutex_lock(&event_mutex);
793 list_for_each_entry(file, &tr->events, list) {
794 call = file->event_call;
795 if (!call->name || !call->class || !call->class->reg)
798 if (system && strcmp(call->class->system, system->name) != 0)
802 * We need to find out if all the events are set
803 * or if all events or cleared, or if we have
806 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
809 * If we have a mixture, no need to look further.
814 mutex_unlock(&event_mutex);
816 buf[0] = set_to_char[set];
819 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
825 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
828 struct ftrace_subsystem_dir *dir = filp->private_data;
829 struct event_subsystem *system = dir->subsystem;
830 const char *name = NULL;
834 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
838 ret = tracing_update_buffers();
842 if (val != 0 && val != 1)
846 * Opening of "enable" adds a ref count to system,
847 * so the name is safe to use.
852 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
866 FORMAT_FIELD_SEPERATOR = 2,
870 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
872 struct ftrace_event_call *call = event_file_data(m->private);
873 struct list_head *common_head = &ftrace_common_fields;
874 struct list_head *head = trace_get_fields(call);
875 struct list_head *node = v;
879 switch ((unsigned long)v) {
884 case FORMAT_FIELD_SEPERATOR:
888 case FORMAT_PRINTFMT:
894 if (node == common_head)
895 return (void *)FORMAT_FIELD_SEPERATOR;
896 else if (node == head)
897 return (void *)FORMAT_PRINTFMT;
902 static int f_show(struct seq_file *m, void *v)
904 struct ftrace_event_call *call = event_file_data(m->private);
905 struct ftrace_event_field *field;
906 const char *array_descriptor;
908 switch ((unsigned long)v) {
910 seq_printf(m, "name: %s\n", call->name);
911 seq_printf(m, "ID: %d\n", call->event.type);
912 seq_printf(m, "format:\n");
915 case FORMAT_FIELD_SEPERATOR:
919 case FORMAT_PRINTFMT:
920 seq_printf(m, "\nprint fmt: %s\n",
925 field = list_entry(v, struct ftrace_event_field, link);
927 * Smartly shows the array type(except dynamic array).
930 * If TYPE := TYPE[LEN], it is shown:
931 * field:TYPE VAR[LEN]
933 array_descriptor = strchr(field->type, '[');
935 if (!strncmp(field->type, "__data_loc", 10))
936 array_descriptor = NULL;
938 if (!array_descriptor)
939 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
940 field->type, field->name, field->offset,
941 field->size, !!field->is_signed);
943 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
944 (int)(array_descriptor - field->type),
945 field->type, field->name,
946 array_descriptor, field->offset,
947 field->size, !!field->is_signed);
952 static void *f_start(struct seq_file *m, loff_t *pos)
954 void *p = (void *)FORMAT_HEADER;
957 /* ->stop() is called even if ->start() fails */
958 mutex_lock(&event_mutex);
959 if (!event_file_data(m->private))
960 return ERR_PTR(-ENODEV);
962 while (l < *pos && p)
963 p = f_next(m, p, &l);
968 static void f_stop(struct seq_file *m, void *p)
970 mutex_unlock(&event_mutex);
973 static const struct seq_operations trace_format_seq_ops = {
980 static int trace_format_open(struct inode *inode, struct file *file)
985 ret = seq_open(file, &trace_format_seq_ops);
989 m = file->private_data;
996 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
998 int id = (long)event_file_data(filp);
1008 len = sprintf(buf, "%d\n", id);
1010 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1014 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1017 struct ftrace_event_file *file;
1018 struct trace_seq *s;
1024 s = kmalloc(sizeof(*s), GFP_KERNEL);
1031 mutex_lock(&event_mutex);
1032 file = event_file_data(filp);
1034 print_event_filter(file, s);
1035 mutex_unlock(&event_mutex);
1038 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1046 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1049 struct ftrace_event_file *file;
1053 if (cnt >= PAGE_SIZE)
1056 buf = (char *)__get_free_page(GFP_TEMPORARY);
1060 if (copy_from_user(buf, ubuf, cnt)) {
1061 free_page((unsigned long) buf);
1066 mutex_lock(&event_mutex);
1067 file = event_file_data(filp);
1069 err = apply_event_filter(file, buf);
1070 mutex_unlock(&event_mutex);
1072 free_page((unsigned long) buf);
1081 static LIST_HEAD(event_subsystems);
1083 static int subsystem_open(struct inode *inode, struct file *filp)
1085 struct event_subsystem *system = NULL;
1086 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1087 struct trace_array *tr;
1090 if (tracing_is_disabled())
1093 /* Make sure the system still exists */
1094 mutex_lock(&trace_types_lock);
1095 mutex_lock(&event_mutex);
1096 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1097 list_for_each_entry(dir, &tr->systems, list) {
1098 if (dir == inode->i_private) {
1099 /* Don't open systems with no events */
1100 if (dir->nr_events) {
1101 __get_system_dir(dir);
1102 system = dir->subsystem;
1109 mutex_unlock(&event_mutex);
1110 mutex_unlock(&trace_types_lock);
1115 /* Some versions of gcc think dir can be uninitialized here */
1118 /* Still need to increment the ref count of the system */
1119 if (trace_array_get(tr) < 0) {
1124 ret = tracing_open_generic(inode, filp);
1126 trace_array_put(tr);
1133 static int system_tr_open(struct inode *inode, struct file *filp)
1135 struct ftrace_subsystem_dir *dir;
1136 struct trace_array *tr = inode->i_private;
1139 if (tracing_is_disabled())
1142 if (trace_array_get(tr) < 0)
1145 /* Make a temporary dir that has no system but points to tr */
1146 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1148 trace_array_put(tr);
1154 ret = tracing_open_generic(inode, filp);
1156 trace_array_put(tr);
1161 filp->private_data = dir;
1166 static int subsystem_release(struct inode *inode, struct file *file)
1168 struct ftrace_subsystem_dir *dir = file->private_data;
1170 trace_array_put(dir->tr);
1173 * If dir->subsystem is NULL, then this is a temporary
1174 * descriptor that was made for a trace_array to enable
1186 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1189 struct ftrace_subsystem_dir *dir = filp->private_data;
1190 struct event_subsystem *system = dir->subsystem;
1191 struct trace_seq *s;
1197 s = kmalloc(sizeof(*s), GFP_KERNEL);
1203 print_subsystem_event_filter(system, s);
1204 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1212 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1215 struct ftrace_subsystem_dir *dir = filp->private_data;
1219 if (cnt >= PAGE_SIZE)
1222 buf = (char *)__get_free_page(GFP_TEMPORARY);
1226 if (copy_from_user(buf, ubuf, cnt)) {
1227 free_page((unsigned long) buf);
1232 err = apply_subsystem_event_filter(dir, buf);
1233 free_page((unsigned long) buf);
1243 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1245 int (*func)(struct trace_seq *s) = filp->private_data;
1246 struct trace_seq *s;
1252 s = kmalloc(sizeof(*s), GFP_KERNEL);
1259 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1266 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1267 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1268 static int ftrace_event_release(struct inode *inode, struct file *file);
1270 static const struct seq_operations show_event_seq_ops = {
1277 static const struct seq_operations show_set_event_seq_ops = {
1284 static const struct file_operations ftrace_avail_fops = {
1285 .open = ftrace_event_avail_open,
1287 .llseek = seq_lseek,
1288 .release = seq_release,
1291 static const struct file_operations ftrace_set_event_fops = {
1292 .open = ftrace_event_set_open,
1294 .write = ftrace_event_write,
1295 .llseek = seq_lseek,
1296 .release = ftrace_event_release,
1299 static const struct file_operations ftrace_enable_fops = {
1300 .open = tracing_open_generic,
1301 .read = event_enable_read,
1302 .write = event_enable_write,
1303 .llseek = default_llseek,
1306 static const struct file_operations ftrace_event_format_fops = {
1307 .open = trace_format_open,
1309 .llseek = seq_lseek,
1310 .release = seq_release,
1313 static const struct file_operations ftrace_event_id_fops = {
1314 .read = event_id_read,
1315 .llseek = default_llseek,
1318 static const struct file_operations ftrace_event_filter_fops = {
1319 .open = tracing_open_generic,
1320 .read = event_filter_read,
1321 .write = event_filter_write,
1322 .llseek = default_llseek,
1325 static const struct file_operations ftrace_subsystem_filter_fops = {
1326 .open = subsystem_open,
1327 .read = subsystem_filter_read,
1328 .write = subsystem_filter_write,
1329 .llseek = default_llseek,
1330 .release = subsystem_release,
1333 static const struct file_operations ftrace_system_enable_fops = {
1334 .open = subsystem_open,
1335 .read = system_enable_read,
1336 .write = system_enable_write,
1337 .llseek = default_llseek,
1338 .release = subsystem_release,
1341 static const struct file_operations ftrace_tr_enable_fops = {
1342 .open = system_tr_open,
1343 .read = system_enable_read,
1344 .write = system_enable_write,
1345 .llseek = default_llseek,
1346 .release = subsystem_release,
1349 static const struct file_operations ftrace_show_header_fops = {
1350 .open = tracing_open_generic,
1351 .read = show_header,
1352 .llseek = default_llseek,
1356 ftrace_event_open(struct inode *inode, struct file *file,
1357 const struct seq_operations *seq_ops)
1362 ret = seq_open(file, seq_ops);
1365 m = file->private_data;
1366 /* copy tr over to seq ops */
1367 m->private = inode->i_private;
1372 static int ftrace_event_release(struct inode *inode, struct file *file)
1374 struct trace_array *tr = inode->i_private;
1376 trace_array_put(tr);
1378 return seq_release(inode, file);
1382 ftrace_event_avail_open(struct inode *inode, struct file *file)
1384 const struct seq_operations *seq_ops = &show_event_seq_ops;
1386 return ftrace_event_open(inode, file, seq_ops);
1390 ftrace_event_set_open(struct inode *inode, struct file *file)
1392 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1393 struct trace_array *tr = inode->i_private;
1396 if (trace_array_get(tr) < 0)
1399 if ((file->f_mode & FMODE_WRITE) &&
1400 (file->f_flags & O_TRUNC))
1401 ftrace_clear_events(tr);
1403 ret = ftrace_event_open(inode, file, seq_ops);
1405 trace_array_put(tr);
1409 static struct event_subsystem *
1410 create_new_subsystem(const char *name)
1412 struct event_subsystem *system;
1414 /* need to create new entry */
1415 system = kmalloc(sizeof(*system), GFP_KERNEL);
1419 system->ref_count = 1;
1421 /* Only allocate if dynamic (kprobes and modules) */
1422 if (!core_kernel_data((unsigned long)name)) {
1423 system->ref_count |= SYSTEM_FL_FREE_NAME;
1424 system->name = kstrdup(name, GFP_KERNEL);
1428 system->name = name;
1430 system->filter = NULL;
1432 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1433 if (!system->filter)
1436 list_add(&system->list, &event_subsystems);
1441 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1442 kfree(system->name);
1447 static struct dentry *
1448 event_subsystem_dir(struct trace_array *tr, const char *name,
1449 struct ftrace_event_file *file, struct dentry *parent)
1451 struct ftrace_subsystem_dir *dir;
1452 struct event_subsystem *system;
1453 struct dentry *entry;
1455 /* First see if we did not already create this dir */
1456 list_for_each_entry(dir, &tr->systems, list) {
1457 system = dir->subsystem;
1458 if (strcmp(system->name, name) == 0) {
1465 /* Now see if the system itself exists. */
1466 list_for_each_entry(system, &event_subsystems, list) {
1467 if (strcmp(system->name, name) == 0)
1470 /* Reset system variable when not found */
1471 if (&system->list == &event_subsystems)
1474 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1479 system = create_new_subsystem(name);
1483 __get_system(system);
1485 dir->entry = debugfs_create_dir(name, parent);
1487 pr_warning("Failed to create system directory %s\n", name);
1488 __put_system(system);
1495 dir->subsystem = system;
1498 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1499 &ftrace_subsystem_filter_fops);
1501 kfree(system->filter);
1502 system->filter = NULL;
1503 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1506 trace_create_file("enable", 0644, dir->entry, dir,
1507 &ftrace_system_enable_fops);
1509 list_add(&dir->list, &tr->systems);
1516 /* Only print this message if failed on memory allocation */
1517 if (!dir || !system)
1518 pr_warning("No memory to create event subsystem %s\n",
1524 event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1526 struct ftrace_event_call *call = file->event_call;
1527 struct trace_array *tr = file->tr;
1528 struct list_head *head;
1529 struct dentry *d_events;
1533 * If the trace point header did not define TRACE_SYSTEM
1534 * then the system would be called "TRACE_SYSTEM".
1536 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1537 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1543 file->dir = debugfs_create_dir(call->name, d_events);
1545 pr_warning("Could not create debugfs '%s' directory\n",
1550 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1551 trace_create_file("enable", 0644, file->dir, file,
1552 &ftrace_enable_fops);
1554 #ifdef CONFIG_PERF_EVENTS
1555 if (call->event.type && call->class->reg)
1556 trace_create_file("id", 0444, file->dir,
1557 (void *)(long)call->event.type,
1558 &ftrace_event_id_fops);
1562 * Other events may have the same class. Only update
1563 * the fields if they are not already defined.
1565 head = trace_get_fields(call);
1566 if (list_empty(head)) {
1567 ret = call->class->define_fields(call);
1569 pr_warning("Could not initialize trace point"
1570 " events/%s\n", call->name);
1574 trace_create_file("filter", 0644, file->dir, file,
1575 &ftrace_event_filter_fops);
1577 trace_create_file("trigger", 0644, file->dir, file,
1578 &event_trigger_fops);
1580 trace_create_file("format", 0444, file->dir, call,
1581 &ftrace_event_format_fops);
1586 static void remove_event_from_tracers(struct ftrace_event_call *call)
1588 struct ftrace_event_file *file;
1589 struct trace_array *tr;
1591 do_for_each_event_file_safe(tr, file) {
1592 if (file->event_call != call)
1595 remove_event_file_dir(file);
1597 * The do_for_each_event_file_safe() is
1598 * a double loop. After finding the call for this
1599 * trace_array, we use break to jump to the next
1603 } while_for_each_event_file();
1606 static void event_remove(struct ftrace_event_call *call)
1608 struct trace_array *tr;
1609 struct ftrace_event_file *file;
1611 do_for_each_event_file(tr, file) {
1612 if (file->event_call != call)
1614 ftrace_event_enable_disable(file, 0);
1615 destroy_preds(file);
1617 * The do_for_each_event_file() is
1618 * a double loop. After finding the call for this
1619 * trace_array, we use break to jump to the next
1623 } while_for_each_event_file();
1625 if (call->event.funcs)
1626 __unregister_ftrace_event(&call->event);
1627 remove_event_from_tracers(call);
1628 list_del(&call->list);
1631 static int event_init(struct ftrace_event_call *call)
1635 if (WARN_ON(!call->name))
1638 if (call->class->raw_init) {
1639 ret = call->class->raw_init(call);
1640 if (ret < 0 && ret != -ENOSYS)
1641 pr_warn("Could not initialize trace events/%s\n",
1649 __register_event(struct ftrace_event_call *call, struct module *mod)
1653 ret = event_init(call);
1657 list_add(&call->list, &ftrace_events);
1663 static struct ftrace_event_file *
1664 trace_create_new_event(struct ftrace_event_call *call,
1665 struct trace_array *tr)
1667 struct ftrace_event_file *file;
1669 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1673 file->event_call = call;
1675 atomic_set(&file->sm_ref, 0);
1676 atomic_set(&file->tm_ref, 0);
1677 INIT_LIST_HEAD(&file->triggers);
1678 list_add(&file->list, &tr->events);
1683 /* Add an event to a trace directory */
1685 __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1687 struct ftrace_event_file *file;
1689 file = trace_create_new_event(call, tr);
1693 return event_create_dir(tr->event_dir, file);
1697 * Just create a decriptor for early init. A descriptor is required
1698 * for enabling events at boot. We want to enable events before
1699 * the filesystem is initialized.
1702 __trace_early_add_new_event(struct ftrace_event_call *call,
1703 struct trace_array *tr)
1705 struct ftrace_event_file *file;
1707 file = trace_create_new_event(call, tr);
1714 struct ftrace_module_file_ops;
1715 static void __add_event_to_tracers(struct ftrace_event_call *call);
1717 /* Add an additional event_call dynamically */
1718 int trace_add_event_call(struct ftrace_event_call *call)
1721 mutex_lock(&trace_types_lock);
1722 mutex_lock(&event_mutex);
1724 ret = __register_event(call, NULL);
1726 __add_event_to_tracers(call);
1728 mutex_unlock(&event_mutex);
1729 mutex_unlock(&trace_types_lock);
1734 * Must be called under locking of trace_types_lock, event_mutex and
1737 static void __trace_remove_event_call(struct ftrace_event_call *call)
1740 trace_destroy_fields(call);
1741 destroy_call_preds(call);
1744 static int probe_remove_event_call(struct ftrace_event_call *call)
1746 struct trace_array *tr;
1747 struct ftrace_event_file *file;
1749 #ifdef CONFIG_PERF_EVENTS
1750 if (call->perf_refcount)
1753 do_for_each_event_file(tr, file) {
1754 if (file->event_call != call)
1757 * We can't rely on ftrace_event_enable_disable(enable => 0)
1758 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1759 * TRACE_REG_UNREGISTER.
1761 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1764 * The do_for_each_event_file_safe() is
1765 * a double loop. After finding the call for this
1766 * trace_array, we use break to jump to the next
1770 } while_for_each_event_file();
1772 __trace_remove_event_call(call);
1777 /* Remove an event_call */
1778 int trace_remove_event_call(struct ftrace_event_call *call)
1782 mutex_lock(&trace_types_lock);
1783 mutex_lock(&event_mutex);
1784 down_write(&trace_event_sem);
1785 ret = probe_remove_event_call(call);
1786 up_write(&trace_event_sem);
1787 mutex_unlock(&event_mutex);
1788 mutex_unlock(&trace_types_lock);
1793 #define for_each_event(event, start, end) \
1794 for (event = start; \
1795 (unsigned long)event < (unsigned long)end; \
1798 #ifdef CONFIG_MODULES
1800 static void trace_module_add_events(struct module *mod)
1802 struct ftrace_event_call **call, **start, **end;
1804 if (!mod->num_trace_events)
1807 /* Don't add infrastructure for mods without tracepoints */
1808 if (trace_module_has_bad_taint(mod)) {
1809 pr_err("%s: module has bad taint, not creating trace events\n",
1814 start = mod->trace_events;
1815 end = mod->trace_events + mod->num_trace_events;
1817 for_each_event(call, start, end) {
1818 __register_event(*call, mod);
1819 __add_event_to_tracers(*call);
1823 static void trace_module_remove_events(struct module *mod)
1825 struct ftrace_event_call *call, *p;
1826 bool clear_trace = false;
1828 down_write(&trace_event_sem);
1829 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1830 if (call->mod == mod) {
1831 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1833 __trace_remove_event_call(call);
1836 up_write(&trace_event_sem);
1839 * It is safest to reset the ring buffer if the module being unloaded
1840 * registered any events that were used. The only worry is if
1841 * a new module gets loaded, and takes on the same id as the events
1842 * of this module. When printing out the buffer, traced events left
1843 * over from this module may be passed to the new module events and
1844 * unexpected results may occur.
1847 tracing_reset_all_online_cpus();
1850 static int trace_module_notify(struct notifier_block *self,
1851 unsigned long val, void *data)
1853 struct module *mod = data;
1855 mutex_lock(&trace_types_lock);
1856 mutex_lock(&event_mutex);
1858 case MODULE_STATE_COMING:
1859 trace_module_add_events(mod);
1861 case MODULE_STATE_GOING:
1862 trace_module_remove_events(mod);
1865 mutex_unlock(&event_mutex);
1866 mutex_unlock(&trace_types_lock);
1871 static struct notifier_block trace_module_nb = {
1872 .notifier_call = trace_module_notify,
1875 #endif /* CONFIG_MODULES */
1877 /* Create a new event directory structure for a trace directory. */
1879 __trace_add_event_dirs(struct trace_array *tr)
1881 struct ftrace_event_call *call;
1884 list_for_each_entry(call, &ftrace_events, list) {
1885 ret = __trace_add_new_event(call, tr);
1887 pr_warning("Could not create directory for event %s\n",
1892 struct ftrace_event_file *
1893 find_event_file(struct trace_array *tr, const char *system, const char *event)
1895 struct ftrace_event_file *file;
1896 struct ftrace_event_call *call;
1898 list_for_each_entry(file, &tr->events, list) {
1900 call = file->event_call;
1902 if (!call->name || !call->class || !call->class->reg)
1905 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1908 if (strcmp(event, call->name) == 0 &&
1909 strcmp(system, call->class->system) == 0)
1915 #ifdef CONFIG_DYNAMIC_FTRACE
1918 #define ENABLE_EVENT_STR "enable_event"
1919 #define DISABLE_EVENT_STR "disable_event"
1921 struct event_probe_data {
1922 struct ftrace_event_file *file;
1923 unsigned long count;
1929 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1931 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1932 struct event_probe_data *data = *pdata;
1938 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1940 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1944 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1946 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1947 struct event_probe_data *data = *pdata;
1955 /* Skip if the event is in a state we want to switch to */
1956 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1959 if (data->count != -1)
1962 event_enable_probe(ip, parent_ip, _data);
1966 event_enable_print(struct seq_file *m, unsigned long ip,
1967 struct ftrace_probe_ops *ops, void *_data)
1969 struct event_probe_data *data = _data;
1971 seq_printf(m, "%ps:", (void *)ip);
1973 seq_printf(m, "%s:%s:%s",
1974 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1975 data->file->event_call->class->system,
1976 data->file->event_call->name);
1978 if (data->count == -1)
1979 seq_printf(m, ":unlimited\n");
1981 seq_printf(m, ":count=%ld\n", data->count);
1987 event_enable_init(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;
1998 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2001 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2002 struct event_probe_data *data = *pdata;
2004 if (WARN_ON_ONCE(data->ref <= 0))
2009 /* Remove the SOFT_MODE flag */
2010 __ftrace_event_enable_disable(data->file, 0, 1);
2011 module_put(data->file->event_call->mod);
2017 static struct ftrace_probe_ops event_enable_probe_ops = {
2018 .func = event_enable_probe,
2019 .print = event_enable_print,
2020 .init = event_enable_init,
2021 .free = event_enable_free,
2024 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2025 .func = event_enable_count_probe,
2026 .print = event_enable_print,
2027 .init = event_enable_init,
2028 .free = event_enable_free,
2031 static struct ftrace_probe_ops event_disable_probe_ops = {
2032 .func = event_enable_probe,
2033 .print = event_enable_print,
2034 .init = event_enable_init,
2035 .free = event_enable_free,
2038 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2039 .func = event_enable_count_probe,
2040 .print = event_enable_print,
2041 .init = event_enable_init,
2042 .free = event_enable_free,
2046 event_enable_func(struct ftrace_hash *hash,
2047 char *glob, char *cmd, char *param, int enabled)
2049 struct trace_array *tr = top_trace_array();
2050 struct ftrace_event_file *file;
2051 struct ftrace_probe_ops *ops;
2052 struct event_probe_data *data;
2059 /* hash funcs only work with set_ftrace_filter */
2060 if (!enabled || !param)
2063 system = strsep(¶m, ":");
2067 event = strsep(¶m, ":");
2069 mutex_lock(&event_mutex);
2072 file = find_event_file(tr, system, event);
2076 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2079 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2081 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2083 if (glob[0] == '!') {
2084 unregister_ftrace_function_probe_func(glob+1, ops);
2090 data = kzalloc(sizeof(*data), GFP_KERNEL);
2094 data->enable = enable;
2101 number = strsep(¶m, ":");
2104 if (!strlen(number))
2108 * We use the callback data field (which is a pointer)
2111 ret = kstrtoul(number, 0, &data->count);
2116 /* Don't let event modules unload while probe registered */
2117 ret = try_module_get(file->event_call->mod);
2123 ret = __ftrace_event_enable_disable(file, 1, 1);
2126 ret = register_ftrace_function_probe(glob, ops, data);
2128 * The above returns on success the # of functions enabled,
2129 * but if it didn't find any functions it returns zero.
2130 * Consider no functions a failure too.
2137 /* Just return zero, not the number of enabled functions */
2140 mutex_unlock(&event_mutex);
2144 __ftrace_event_enable_disable(file, 0, 1);
2146 module_put(file->event_call->mod);
2152 static struct ftrace_func_command event_enable_cmd = {
2153 .name = ENABLE_EVENT_STR,
2154 .func = event_enable_func,
2157 static struct ftrace_func_command event_disable_cmd = {
2158 .name = DISABLE_EVENT_STR,
2159 .func = event_enable_func,
2162 static __init int register_event_cmds(void)
2166 ret = register_ftrace_command(&event_enable_cmd);
2167 if (WARN_ON(ret < 0))
2169 ret = register_ftrace_command(&event_disable_cmd);
2170 if (WARN_ON(ret < 0))
2171 unregister_ftrace_command(&event_enable_cmd);
2175 static inline int register_event_cmds(void) { return 0; }
2176 #endif /* CONFIG_DYNAMIC_FTRACE */
2179 * The top level array has already had its ftrace_event_file
2180 * descriptors created in order to allow for early events to
2181 * be recorded. This function is called after the debugfs has been
2182 * initialized, and we now have to create the files associated
2186 __trace_early_add_event_dirs(struct trace_array *tr)
2188 struct ftrace_event_file *file;
2192 list_for_each_entry(file, &tr->events, list) {
2193 ret = event_create_dir(tr->event_dir, file);
2195 pr_warning("Could not create directory for event %s\n",
2196 file->event_call->name);
2201 * For early boot up, the top trace array requires to have
2202 * a list of events that can be enabled. This must be done before
2203 * the filesystem is set up in order to allow events to be traced
2207 __trace_early_add_events(struct trace_array *tr)
2209 struct ftrace_event_call *call;
2212 list_for_each_entry(call, &ftrace_events, list) {
2213 /* Early boot up should not have any modules loaded */
2214 if (WARN_ON_ONCE(call->mod))
2217 ret = __trace_early_add_new_event(call, tr);
2219 pr_warning("Could not create early event %s\n",
2224 /* Remove the event directory structure for a trace directory. */
2226 __trace_remove_event_dirs(struct trace_array *tr)
2228 struct ftrace_event_file *file, *next;
2230 list_for_each_entry_safe(file, next, &tr->events, list)
2231 remove_event_file_dir(file);
2234 static void __add_event_to_tracers(struct ftrace_event_call *call)
2236 struct trace_array *tr;
2238 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2239 __trace_add_new_event(call, tr);
2242 extern struct ftrace_event_call *__start_ftrace_events[];
2243 extern struct ftrace_event_call *__stop_ftrace_events[];
2245 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2247 static __init int setup_trace_event(char *str)
2249 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2250 ring_buffer_expanded = true;
2251 tracing_selftest_disabled = true;
2255 __setup("trace_event=", setup_trace_event);
2257 /* Expects to have event_mutex held when called */
2259 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2261 struct dentry *d_events;
2262 struct dentry *entry;
2264 entry = debugfs_create_file("set_event", 0644, parent,
2265 tr, &ftrace_set_event_fops);
2267 pr_warning("Could not create debugfs 'set_event' entry\n");
2271 d_events = debugfs_create_dir("events", parent);
2273 pr_warning("Could not create debugfs 'events' directory\n");
2277 /* ring buffer internal formats */
2278 trace_create_file("header_page", 0444, d_events,
2279 ring_buffer_print_page_header,
2280 &ftrace_show_header_fops);
2282 trace_create_file("header_event", 0444, d_events,
2283 ring_buffer_print_entry_header,
2284 &ftrace_show_header_fops);
2286 trace_create_file("enable", 0644, d_events,
2287 tr, &ftrace_tr_enable_fops);
2289 tr->event_dir = d_events;
2295 * event_trace_add_tracer - add a instance of a trace_array to events
2296 * @parent: The parent dentry to place the files/directories for events in
2297 * @tr: The trace array associated with these events
2299 * When a new instance is created, it needs to set up its events
2300 * directory, as well as other files associated with events. It also
2301 * creates the event hierachry in the @parent/events directory.
2303 * Returns 0 on success.
2305 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2309 mutex_lock(&event_mutex);
2311 ret = create_event_toplevel_files(parent, tr);
2315 down_write(&trace_event_sem);
2316 __trace_add_event_dirs(tr);
2317 up_write(&trace_event_sem);
2320 mutex_unlock(&event_mutex);
2326 * The top trace array already had its file descriptors created.
2327 * Now the files themselves need to be created.
2330 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2334 mutex_lock(&event_mutex);
2336 ret = create_event_toplevel_files(parent, tr);
2340 down_write(&trace_event_sem);
2341 __trace_early_add_event_dirs(tr);
2342 up_write(&trace_event_sem);
2345 mutex_unlock(&event_mutex);
2350 int event_trace_del_tracer(struct trace_array *tr)
2352 mutex_lock(&event_mutex);
2354 /* Disable any event triggers and associated soft-disabled events */
2355 clear_event_triggers(tr);
2357 /* Disable any running events */
2358 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2360 /* Access to events are within rcu_read_lock_sched() */
2361 synchronize_sched();
2363 down_write(&trace_event_sem);
2364 __trace_remove_event_dirs(tr);
2365 debugfs_remove_recursive(tr->event_dir);
2366 up_write(&trace_event_sem);
2368 tr->event_dir = NULL;
2370 mutex_unlock(&event_mutex);
2375 static __init int event_trace_memsetup(void)
2377 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2378 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2382 static __init int event_trace_enable(void)
2384 struct trace_array *tr = top_trace_array();
2385 struct ftrace_event_call **iter, *call;
2386 char *buf = bootup_event_buf;
2390 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2393 ret = event_init(call);
2395 list_add(&call->list, &ftrace_events);
2399 * We need the top trace array to have a working set of trace
2400 * points at early init, before the debug files and directories
2401 * are created. Create the file entries now, and attach them
2402 * to the actual file dentries later.
2404 __trace_early_add_events(tr);
2407 token = strsep(&buf, ",");
2414 ret = ftrace_set_clr_event(tr, token, 1);
2416 pr_warn("Failed to enable trace event: %s\n", token);
2419 trace_printk_start_comm();
2421 register_event_cmds();
2423 register_trigger_cmds();
2428 static __init int event_trace_init(void)
2430 struct trace_array *tr;
2431 struct dentry *d_tracer;
2432 struct dentry *entry;
2435 tr = top_trace_array();
2437 d_tracer = tracing_init_dentry();
2441 entry = debugfs_create_file("available_events", 0444, d_tracer,
2442 tr, &ftrace_avail_fops);
2444 pr_warning("Could not create debugfs "
2445 "'available_events' entry\n");
2447 if (trace_define_common_fields())
2448 pr_warning("tracing: Failed to allocate common fields");
2450 ret = early_event_add_tracer(d_tracer, tr);
2454 #ifdef CONFIG_MODULES
2455 ret = register_module_notifier(&trace_module_nb);
2457 pr_warning("Failed to register trace events module notifier\n");
2461 early_initcall(event_trace_memsetup);
2462 core_initcall(event_trace_enable);
2463 fs_initcall(event_trace_init);
2465 #ifdef CONFIG_FTRACE_STARTUP_TEST
2467 static DEFINE_SPINLOCK(test_spinlock);
2468 static DEFINE_SPINLOCK(test_spinlock_irq);
2469 static DEFINE_MUTEX(test_mutex);
2471 static __init void test_work(struct work_struct *dummy)
2473 spin_lock(&test_spinlock);
2474 spin_lock_irq(&test_spinlock_irq);
2476 spin_unlock_irq(&test_spinlock_irq);
2477 spin_unlock(&test_spinlock);
2479 mutex_lock(&test_mutex);
2481 mutex_unlock(&test_mutex);
2484 static __init int event_test_thread(void *unused)
2488 test_malloc = kmalloc(1234, GFP_KERNEL);
2490 pr_info("failed to kmalloc\n");
2492 schedule_on_each_cpu(test_work);
2496 set_current_state(TASK_INTERRUPTIBLE);
2497 while (!kthread_should_stop())
2504 * Do various things that may trigger events.
2506 static __init void event_test_stuff(void)
2508 struct task_struct *test_thread;
2510 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2512 kthread_stop(test_thread);
2516 * For every trace event defined, we will test each trace point separately,
2517 * and then by groups, and finally all trace points.
2519 static __init void event_trace_self_tests(void)
2521 struct ftrace_subsystem_dir *dir;
2522 struct ftrace_event_file *file;
2523 struct ftrace_event_call *call;
2524 struct event_subsystem *system;
2525 struct trace_array *tr;
2528 tr = top_trace_array();
2530 pr_info("Running tests on trace events:\n");
2532 list_for_each_entry(file, &tr->events, list) {
2534 call = file->event_call;
2536 /* Only test those that have a probe */
2537 if (!call->class || !call->class->probe)
2541 * Testing syscall events here is pretty useless, but
2542 * we still do it if configured. But this is time consuming.
2543 * What we really need is a user thread to perform the
2544 * syscalls as we test.
2546 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2547 if (call->class->system &&
2548 strcmp(call->class->system, "syscalls") == 0)
2552 pr_info("Testing event %s: ", call->name);
2555 * If an event is already enabled, someone is using
2556 * it and the self test should not be on.
2558 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2559 pr_warning("Enabled event during self test!\n");
2564 ftrace_event_enable_disable(file, 1);
2566 ftrace_event_enable_disable(file, 0);
2571 /* Now test at the sub system level */
2573 pr_info("Running tests on trace event systems:\n");
2575 list_for_each_entry(dir, &tr->systems, list) {
2577 system = dir->subsystem;
2579 /* the ftrace system is special, skip it */
2580 if (strcmp(system->name, "ftrace") == 0)
2583 pr_info("Testing event system %s: ", system->name);
2585 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2586 if (WARN_ON_ONCE(ret)) {
2587 pr_warning("error enabling system %s\n",
2594 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2595 if (WARN_ON_ONCE(ret)) {
2596 pr_warning("error disabling system %s\n",
2604 /* Test with all events enabled */
2606 pr_info("Running tests on all trace events:\n");
2607 pr_info("Testing all events: ");
2609 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2610 if (WARN_ON_ONCE(ret)) {
2611 pr_warning("error enabling all events\n");
2618 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2619 if (WARN_ON_ONCE(ret)) {
2620 pr_warning("error disabling all events\n");
2627 #ifdef CONFIG_FUNCTION_TRACER
2629 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2632 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2633 struct ftrace_ops *op, struct pt_regs *pt_regs)
2635 struct ring_buffer_event *event;
2636 struct ring_buffer *buffer;
2637 struct ftrace_entry *entry;
2638 unsigned long flags;
2643 pc = preempt_count();
2644 preempt_disable_notrace();
2645 cpu = raw_smp_processor_id();
2646 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2651 local_save_flags(flags);
2653 event = trace_current_buffer_lock_reserve(&buffer,
2654 TRACE_FN, sizeof(*entry),
2658 entry = ring_buffer_event_data(event);
2660 entry->parent_ip = parent_ip;
2662 trace_buffer_unlock_commit(buffer, event, flags, pc);
2665 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2666 preempt_enable_notrace();
2669 static struct ftrace_ops trace_ops __initdata =
2671 .func = function_test_events_call,
2672 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2675 static __init void event_trace_self_test_with_function(void)
2678 ret = register_ftrace_function(&trace_ops);
2679 if (WARN_ON(ret < 0)) {
2680 pr_info("Failed to enable function tracer for event tests\n");
2683 pr_info("Running tests again, along with the function tracer\n");
2684 event_trace_self_tests();
2685 unregister_ftrace_function(&trace_ops);
2688 static __init void event_trace_self_test_with_function(void)
2693 static __init int event_trace_self_tests_init(void)
2695 if (!tracing_selftest_disabled) {
2696 event_trace_self_tests();
2697 event_trace_self_test_with_function();
2703 late_initcall(event_trace_self_tests_init);