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 /* Double loops, do not use break, only goto's work */
45 #define do_for_each_event_file(tr, file) \
46 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
47 list_for_each_entry(file, &tr->events, list)
49 #define do_for_each_event_file_safe(tr, file) \
50 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
51 struct ftrace_event_file *___n; \
52 list_for_each_entry_safe(file, ___n, &tr->events, list)
54 #define while_for_each_event_file() \
57 static struct list_head *
58 trace_get_fields(struct ftrace_event_call *event_call)
60 if (!event_call->class->get_fields)
61 return &event_call->class->fields;
62 return event_call->class->get_fields(event_call);
65 static struct ftrace_event_field *
66 __find_event_field(struct list_head *head, char *name)
68 struct ftrace_event_field *field;
70 list_for_each_entry(field, head, link) {
71 if (!strcmp(field->name, name))
78 struct ftrace_event_field *
79 trace_find_event_field(struct ftrace_event_call *call, char *name)
81 struct ftrace_event_field *field;
82 struct list_head *head;
84 field = __find_event_field(&ftrace_common_fields, name);
88 head = trace_get_fields(call);
89 return __find_event_field(head, name);
92 static int __trace_define_field(struct list_head *head, const char *type,
93 const char *name, int offset, int size,
94 int is_signed, int filter_type)
96 struct ftrace_event_field *field;
98 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
105 if (filter_type == FILTER_OTHER)
106 field->filter_type = filter_assign_type(type);
108 field->filter_type = filter_type;
110 field->offset = offset;
112 field->is_signed = is_signed;
114 list_add(&field->link, head);
119 int trace_define_field(struct ftrace_event_call *call, const char *type,
120 const char *name, int offset, int size, int is_signed,
123 struct list_head *head;
125 if (WARN_ON(!call->class))
128 head = trace_get_fields(call);
129 return __trace_define_field(head, type, name, offset, size,
130 is_signed, filter_type);
132 EXPORT_SYMBOL_GPL(trace_define_field);
134 #define __common_field(type, item) \
135 ret = __trace_define_field(&ftrace_common_fields, #type, \
137 offsetof(typeof(ent), item), \
139 is_signed_type(type), FILTER_OTHER); \
143 static int trace_define_common_fields(void)
146 struct trace_entry ent;
148 __common_field(unsigned short, type);
149 __common_field(unsigned char, flags);
150 __common_field(unsigned char, preempt_count);
151 __common_field(int, pid);
156 static void trace_destroy_fields(struct ftrace_event_call *call)
158 struct ftrace_event_field *field, *next;
159 struct list_head *head;
161 head = trace_get_fields(call);
162 list_for_each_entry_safe(field, next, head, link) {
163 list_del(&field->link);
164 kmem_cache_free(field_cachep, field);
168 int trace_event_raw_init(struct ftrace_event_call *call)
172 id = register_ftrace_event(&call->event);
178 EXPORT_SYMBOL_GPL(trace_event_raw_init);
180 int ftrace_event_reg(struct ftrace_event_call *call,
181 enum trace_reg type, void *data)
183 struct ftrace_event_file *file = data;
186 case TRACE_REG_REGISTER:
187 return tracepoint_probe_register(call->name,
190 case TRACE_REG_UNREGISTER:
191 tracepoint_probe_unregister(call->name,
196 #ifdef CONFIG_PERF_EVENTS
197 case TRACE_REG_PERF_REGISTER:
198 return tracepoint_probe_register(call->name,
199 call->class->perf_probe,
201 case TRACE_REG_PERF_UNREGISTER:
202 tracepoint_probe_unregister(call->name,
203 call->class->perf_probe,
206 case TRACE_REG_PERF_OPEN:
207 case TRACE_REG_PERF_CLOSE:
208 case TRACE_REG_PERF_ADD:
209 case TRACE_REG_PERF_DEL:
215 EXPORT_SYMBOL_GPL(ftrace_event_reg);
217 void trace_event_enable_cmd_record(bool enable)
219 struct ftrace_event_file *file;
220 struct trace_array *tr;
222 mutex_lock(&event_mutex);
223 do_for_each_event_file(tr, file) {
225 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
229 tracing_start_cmdline_record();
230 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
232 tracing_stop_cmdline_record();
233 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
235 } while_for_each_event_file();
236 mutex_unlock(&event_mutex);
239 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
240 int enable, int soft_disable)
242 struct ftrace_event_call *call = file->event_call;
249 * When soft_disable is set and enable is cleared, the sm_ref
250 * reference counter is decremented. If it reaches 0, we want
251 * to clear the SOFT_DISABLED flag but leave the event in the
252 * state that it was. That is, if the event was enabled and
253 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
254 * is set we do not want the event to be enabled before we
257 * When soft_disable is not set but the SOFT_MODE flag is,
258 * we do nothing. Do not disable the tracepoint, otherwise
259 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
262 if (atomic_dec_return(&file->sm_ref) > 0)
264 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
265 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
267 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
269 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
270 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
271 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
272 tracing_stop_cmdline_record();
273 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
275 call->class->reg(call, TRACE_REG_UNREGISTER, file);
277 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
278 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
279 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
283 * When soft_disable is set and enable is set, we want to
284 * register the tracepoint for the event, but leave the event
285 * as is. That means, if the event was already enabled, we do
286 * nothing (but set SOFT_MODE). If the event is disabled, we
287 * set SOFT_DISABLED before enabling the event tracepoint, so
288 * it still seems to be disabled.
291 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
293 if (atomic_inc_return(&file->sm_ref) > 1)
295 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
298 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
300 /* Keep the event disabled, when going to SOFT_MODE. */
302 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
304 if (trace_flags & TRACE_ITER_RECORD_CMD) {
305 tracing_start_cmdline_record();
306 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
308 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
310 tracing_stop_cmdline_record();
311 pr_info("event trace: Could not enable event "
315 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
317 /* WAS_ENABLED gets set but never cleared. */
318 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
326 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
329 return __ftrace_event_enable_disable(file, enable, 0);
332 static void ftrace_clear_events(struct trace_array *tr)
334 struct ftrace_event_file *file;
336 mutex_lock(&event_mutex);
337 list_for_each_entry(file, &tr->events, list) {
338 ftrace_event_enable_disable(file, 0);
340 mutex_unlock(&event_mutex);
343 static void __put_system(struct event_subsystem *system)
345 struct event_filter *filter = system->filter;
347 WARN_ON_ONCE(system->ref_count == 0);
348 if (--system->ref_count)
351 list_del(&system->list);
354 kfree(filter->filter_string);
360 static void __get_system(struct event_subsystem *system)
362 WARN_ON_ONCE(system->ref_count == 0);
366 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
368 WARN_ON_ONCE(dir->ref_count == 0);
370 __get_system(dir->subsystem);
373 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
375 WARN_ON_ONCE(dir->ref_count == 0);
376 /* If the subsystem is about to be freed, the dir must be too */
377 WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
379 __put_system(dir->subsystem);
380 if (!--dir->ref_count)
384 static void put_system(struct ftrace_subsystem_dir *dir)
386 mutex_lock(&event_mutex);
387 __put_system_dir(dir);
388 mutex_unlock(&event_mutex);
392 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
394 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
395 const char *sub, const char *event, int set)
397 struct ftrace_event_file *file;
398 struct ftrace_event_call *call;
401 mutex_lock(&event_mutex);
402 list_for_each_entry(file, &tr->events, list) {
404 call = file->event_call;
406 if (!call->name || !call->class || !call->class->reg)
409 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
413 strcmp(match, call->name) != 0 &&
414 strcmp(match, call->class->system) != 0)
417 if (sub && strcmp(sub, call->class->system) != 0)
420 if (event && strcmp(event, call->name) != 0)
423 ftrace_event_enable_disable(file, set);
427 mutex_unlock(&event_mutex);
432 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
434 char *event = NULL, *sub = NULL, *match;
437 * The buf format can be <subsystem>:<event-name>
438 * *:<event-name> means any event by that name.
439 * :<event-name> is the same.
441 * <subsystem>:* means all events in that subsystem
442 * <subsystem>: means the same.
444 * <name> (no ':') means all events in a subsystem with
445 * the name <name> or any event that matches <name>
448 match = strsep(&buf, ":");
454 if (!strlen(sub) || strcmp(sub, "*") == 0)
456 if (!strlen(event) || strcmp(event, "*") == 0)
460 return __ftrace_set_clr_event(tr, match, sub, event, set);
464 * trace_set_clr_event - enable or disable an event
465 * @system: system name to match (NULL for any system)
466 * @event: event name to match (NULL for all events, within system)
467 * @set: 1 to enable, 0 to disable
469 * This is a way for other parts of the kernel to enable or disable
472 * Returns 0 on success, -EINVAL if the parameters do not match any
475 int trace_set_clr_event(const char *system, const char *event, int set)
477 struct trace_array *tr = top_trace_array();
479 return __ftrace_set_clr_event(tr, NULL, system, event, set);
481 EXPORT_SYMBOL_GPL(trace_set_clr_event);
483 /* 128 should be much more than enough */
484 #define EVENT_BUF_SIZE 127
487 ftrace_event_write(struct file *file, const char __user *ubuf,
488 size_t cnt, loff_t *ppos)
490 struct trace_parser parser;
491 struct seq_file *m = file->private_data;
492 struct trace_array *tr = m->private;
498 ret = tracing_update_buffers();
502 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
505 read = trace_get_user(&parser, ubuf, cnt, ppos);
507 if (read >= 0 && trace_parser_loaded((&parser))) {
510 if (*parser.buffer == '!')
513 parser.buffer[parser.idx] = 0;
515 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
523 trace_parser_put(&parser);
529 t_next(struct seq_file *m, void *v, loff_t *pos)
531 struct ftrace_event_file *file = v;
532 struct ftrace_event_call *call;
533 struct trace_array *tr = m->private;
537 list_for_each_entry_continue(file, &tr->events, list) {
538 call = file->event_call;
540 * The ftrace subsystem is for showing formats only.
541 * They can not be enabled or disabled via the event files.
543 if (call->class && call->class->reg)
550 static void *t_start(struct seq_file *m, loff_t *pos)
552 struct ftrace_event_file *file;
553 struct trace_array *tr = m->private;
556 mutex_lock(&event_mutex);
558 file = list_entry(&tr->events, struct ftrace_event_file, list);
559 for (l = 0; l <= *pos; ) {
560 file = t_next(m, file, &l);
568 s_next(struct seq_file *m, void *v, loff_t *pos)
570 struct ftrace_event_file *file = v;
571 struct trace_array *tr = m->private;
575 list_for_each_entry_continue(file, &tr->events, list) {
576 if (file->flags & FTRACE_EVENT_FL_ENABLED)
583 static void *s_start(struct seq_file *m, loff_t *pos)
585 struct ftrace_event_file *file;
586 struct trace_array *tr = m->private;
589 mutex_lock(&event_mutex);
591 file = list_entry(&tr->events, struct ftrace_event_file, list);
592 for (l = 0; l <= *pos; ) {
593 file = s_next(m, file, &l);
600 static int t_show(struct seq_file *m, void *v)
602 struct ftrace_event_file *file = v;
603 struct ftrace_event_call *call = file->event_call;
605 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
606 seq_printf(m, "%s:", call->class->system);
607 seq_printf(m, "%s\n", call->name);
612 static void t_stop(struct seq_file *m, void *p)
614 mutex_unlock(&event_mutex);
618 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
621 struct ftrace_event_file *file = filp->private_data;
624 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
625 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)
627 else if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
634 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
638 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
641 struct ftrace_event_file *file = filp->private_data;
648 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
652 ret = tracing_update_buffers();
659 mutex_lock(&event_mutex);
660 ret = ftrace_event_enable_disable(file, val);
661 mutex_unlock(&event_mutex);
670 return ret ? ret : cnt;
674 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
677 const char set_to_char[4] = { '?', '0', '1', 'X' };
678 struct ftrace_subsystem_dir *dir = filp->private_data;
679 struct event_subsystem *system = dir->subsystem;
680 struct ftrace_event_call *call;
681 struct ftrace_event_file *file;
682 struct trace_array *tr = dir->tr;
687 mutex_lock(&event_mutex);
688 list_for_each_entry(file, &tr->events, list) {
689 call = file->event_call;
690 if (!call->name || !call->class || !call->class->reg)
693 if (system && strcmp(call->class->system, system->name) != 0)
697 * We need to find out if all the events are set
698 * or if all events or cleared, or if we have
701 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
704 * If we have a mixture, no need to look further.
709 mutex_unlock(&event_mutex);
711 buf[0] = set_to_char[set];
714 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
720 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
723 struct ftrace_subsystem_dir *dir = filp->private_data;
724 struct event_subsystem *system = dir->subsystem;
725 const char *name = NULL;
729 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
733 ret = tracing_update_buffers();
737 if (val != 0 && val != 1)
741 * Opening of "enable" adds a ref count to system,
742 * so the name is safe to use.
747 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
761 FORMAT_FIELD_SEPERATOR = 2,
765 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
767 struct ftrace_event_call *call = m->private;
768 struct ftrace_event_field *field;
769 struct list_head *common_head = &ftrace_common_fields;
770 struct list_head *head = trace_get_fields(call);
774 switch ((unsigned long)v) {
776 if (unlikely(list_empty(common_head)))
779 field = list_entry(common_head->prev,
780 struct ftrace_event_field, link);
783 case FORMAT_FIELD_SEPERATOR:
784 if (unlikely(list_empty(head)))
787 field = list_entry(head->prev, struct ftrace_event_field, link);
790 case FORMAT_PRINTFMT:
796 if (field->link.prev == common_head)
797 return (void *)FORMAT_FIELD_SEPERATOR;
798 else if (field->link.prev == head)
799 return (void *)FORMAT_PRINTFMT;
801 field = list_entry(field->link.prev, struct ftrace_event_field, link);
806 static void *f_start(struct seq_file *m, loff_t *pos)
811 /* Start by showing the header */
813 return (void *)FORMAT_HEADER;
815 p = (void *)FORMAT_HEADER;
817 p = f_next(m, p, &l);
818 } while (p && l < *pos);
823 static int f_show(struct seq_file *m, void *v)
825 struct ftrace_event_call *call = m->private;
826 struct ftrace_event_field *field;
827 const char *array_descriptor;
829 switch ((unsigned long)v) {
831 seq_printf(m, "name: %s\n", call->name);
832 seq_printf(m, "ID: %d\n", call->event.type);
833 seq_printf(m, "format:\n");
836 case FORMAT_FIELD_SEPERATOR:
840 case FORMAT_PRINTFMT:
841 seq_printf(m, "\nprint fmt: %s\n",
849 * Smartly shows the array type(except dynamic array).
852 * If TYPE := TYPE[LEN], it is shown:
853 * field:TYPE VAR[LEN]
855 array_descriptor = strchr(field->type, '[');
857 if (!strncmp(field->type, "__data_loc", 10))
858 array_descriptor = NULL;
860 if (!array_descriptor)
861 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
862 field->type, field->name, field->offset,
863 field->size, !!field->is_signed);
865 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
866 (int)(array_descriptor - field->type),
867 field->type, field->name,
868 array_descriptor, field->offset,
869 field->size, !!field->is_signed);
874 static void f_stop(struct seq_file *m, void *p)
878 static const struct seq_operations trace_format_seq_ops = {
885 static int trace_format_open(struct inode *inode, struct file *file)
887 struct ftrace_event_call *call = inode->i_private;
891 ret = seq_open(file, &trace_format_seq_ops);
895 m = file->private_data;
902 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
904 struct ftrace_event_call *call = filp->private_data;
911 s = kmalloc(sizeof(*s), GFP_KERNEL);
916 trace_seq_printf(s, "%d\n", call->event.type);
918 r = simple_read_from_buffer(ubuf, cnt, ppos,
925 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
928 struct ftrace_event_call *call = filp->private_data;
935 s = kmalloc(sizeof(*s), GFP_KERNEL);
941 print_event_filter(call, s);
942 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
950 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
953 struct ftrace_event_call *call = filp->private_data;
957 if (cnt >= PAGE_SIZE)
960 buf = (char *)__get_free_page(GFP_TEMPORARY);
964 if (copy_from_user(buf, ubuf, cnt)) {
965 free_page((unsigned long) buf);
970 err = apply_event_filter(call, buf);
971 free_page((unsigned long) buf);
980 static LIST_HEAD(event_subsystems);
982 static int subsystem_open(struct inode *inode, struct file *filp)
984 struct event_subsystem *system = NULL;
985 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
986 struct trace_array *tr;
989 /* Make sure the system still exists */
990 mutex_lock(&event_mutex);
991 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
992 list_for_each_entry(dir, &tr->systems, list) {
993 if (dir == inode->i_private) {
994 /* Don't open systems with no events */
995 if (dir->nr_events) {
996 __get_system_dir(dir);
997 system = dir->subsystem;
1004 mutex_unlock(&event_mutex);
1009 /* Some versions of gcc think dir can be uninitialized here */
1012 ret = tracing_open_generic(inode, filp);
1019 static int system_tr_open(struct inode *inode, struct file *filp)
1021 struct ftrace_subsystem_dir *dir;
1022 struct trace_array *tr = inode->i_private;
1025 /* Make a temporary dir that has no system but points to tr */
1026 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1032 ret = tracing_open_generic(inode, filp);
1036 filp->private_data = dir;
1041 static int subsystem_release(struct inode *inode, struct file *file)
1043 struct ftrace_subsystem_dir *dir = file->private_data;
1046 * If dir->subsystem is NULL, then this is a temporary
1047 * descriptor that was made for a trace_array to enable
1059 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1062 struct ftrace_subsystem_dir *dir = filp->private_data;
1063 struct event_subsystem *system = dir->subsystem;
1064 struct trace_seq *s;
1070 s = kmalloc(sizeof(*s), GFP_KERNEL);
1076 print_subsystem_event_filter(system, s);
1077 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1085 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1088 struct ftrace_subsystem_dir *dir = filp->private_data;
1092 if (cnt >= PAGE_SIZE)
1095 buf = (char *)__get_free_page(GFP_TEMPORARY);
1099 if (copy_from_user(buf, ubuf, cnt)) {
1100 free_page((unsigned long) buf);
1105 err = apply_subsystem_event_filter(dir, buf);
1106 free_page((unsigned long) buf);
1116 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1118 int (*func)(struct trace_seq *s) = filp->private_data;
1119 struct trace_seq *s;
1125 s = kmalloc(sizeof(*s), GFP_KERNEL);
1132 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1139 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1140 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1142 static const struct seq_operations show_event_seq_ops = {
1149 static const struct seq_operations show_set_event_seq_ops = {
1156 static const struct file_operations ftrace_avail_fops = {
1157 .open = ftrace_event_avail_open,
1159 .llseek = seq_lseek,
1160 .release = seq_release,
1163 static const struct file_operations ftrace_set_event_fops = {
1164 .open = ftrace_event_set_open,
1166 .write = ftrace_event_write,
1167 .llseek = seq_lseek,
1168 .release = seq_release,
1171 static const struct file_operations ftrace_enable_fops = {
1172 .open = tracing_open_generic,
1173 .read = event_enable_read,
1174 .write = event_enable_write,
1175 .llseek = default_llseek,
1178 static const struct file_operations ftrace_event_format_fops = {
1179 .open = trace_format_open,
1181 .llseek = seq_lseek,
1182 .release = seq_release,
1185 static const struct file_operations ftrace_event_id_fops = {
1186 .open = tracing_open_generic,
1187 .read = event_id_read,
1188 .llseek = default_llseek,
1191 static const struct file_operations ftrace_event_filter_fops = {
1192 .open = tracing_open_generic,
1193 .read = event_filter_read,
1194 .write = event_filter_write,
1195 .llseek = default_llseek,
1198 static const struct file_operations ftrace_subsystem_filter_fops = {
1199 .open = subsystem_open,
1200 .read = subsystem_filter_read,
1201 .write = subsystem_filter_write,
1202 .llseek = default_llseek,
1203 .release = subsystem_release,
1206 static const struct file_operations ftrace_system_enable_fops = {
1207 .open = subsystem_open,
1208 .read = system_enable_read,
1209 .write = system_enable_write,
1210 .llseek = default_llseek,
1211 .release = subsystem_release,
1214 static const struct file_operations ftrace_tr_enable_fops = {
1215 .open = system_tr_open,
1216 .read = system_enable_read,
1217 .write = system_enable_write,
1218 .llseek = default_llseek,
1219 .release = subsystem_release,
1222 static const struct file_operations ftrace_show_header_fops = {
1223 .open = tracing_open_generic,
1224 .read = show_header,
1225 .llseek = default_llseek,
1229 ftrace_event_open(struct inode *inode, struct file *file,
1230 const struct seq_operations *seq_ops)
1235 ret = seq_open(file, seq_ops);
1238 m = file->private_data;
1239 /* copy tr over to seq ops */
1240 m->private = inode->i_private;
1246 ftrace_event_avail_open(struct inode *inode, struct file *file)
1248 const struct seq_operations *seq_ops = &show_event_seq_ops;
1250 return ftrace_event_open(inode, file, seq_ops);
1254 ftrace_event_set_open(struct inode *inode, struct file *file)
1256 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1257 struct trace_array *tr = inode->i_private;
1259 if ((file->f_mode & FMODE_WRITE) &&
1260 (file->f_flags & O_TRUNC))
1261 ftrace_clear_events(tr);
1263 return ftrace_event_open(inode, file, seq_ops);
1266 static struct event_subsystem *
1267 create_new_subsystem(const char *name)
1269 struct event_subsystem *system;
1271 /* need to create new entry */
1272 system = kmalloc(sizeof(*system), GFP_KERNEL);
1276 system->ref_count = 1;
1277 system->name = name;
1279 system->filter = NULL;
1281 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1282 if (!system->filter)
1285 list_add(&system->list, &event_subsystems);
1294 static struct dentry *
1295 event_subsystem_dir(struct trace_array *tr, const char *name,
1296 struct ftrace_event_file *file, struct dentry *parent)
1298 struct ftrace_subsystem_dir *dir;
1299 struct event_subsystem *system;
1300 struct dentry *entry;
1302 /* First see if we did not already create this dir */
1303 list_for_each_entry(dir, &tr->systems, list) {
1304 system = dir->subsystem;
1305 if (strcmp(system->name, name) == 0) {
1312 /* Now see if the system itself exists. */
1313 list_for_each_entry(system, &event_subsystems, list) {
1314 if (strcmp(system->name, name) == 0)
1317 /* Reset system variable when not found */
1318 if (&system->list == &event_subsystems)
1321 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1326 system = create_new_subsystem(name);
1330 __get_system(system);
1332 dir->entry = debugfs_create_dir(name, parent);
1334 pr_warning("Failed to create system directory %s\n", name);
1335 __put_system(system);
1342 dir->subsystem = system;
1345 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1346 &ftrace_subsystem_filter_fops);
1348 kfree(system->filter);
1349 system->filter = NULL;
1350 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1353 trace_create_file("enable", 0644, dir->entry, dir,
1354 &ftrace_system_enable_fops);
1356 list_add(&dir->list, &tr->systems);
1363 /* Only print this message if failed on memory allocation */
1364 if (!dir || !system)
1365 pr_warning("No memory to create event subsystem %s\n",
1371 event_create_dir(struct dentry *parent,
1372 struct ftrace_event_file *file,
1373 const struct file_operations *id,
1374 const struct file_operations *enable,
1375 const struct file_operations *filter,
1376 const struct file_operations *format)
1378 struct ftrace_event_call *call = file->event_call;
1379 struct trace_array *tr = file->tr;
1380 struct list_head *head;
1381 struct dentry *d_events;
1385 * If the trace point header did not define TRACE_SYSTEM
1386 * then the system would be called "TRACE_SYSTEM".
1388 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1389 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1395 file->dir = debugfs_create_dir(call->name, d_events);
1397 pr_warning("Could not create debugfs '%s' directory\n",
1402 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1403 trace_create_file("enable", 0644, file->dir, file,
1406 #ifdef CONFIG_PERF_EVENTS
1407 if (call->event.type && call->class->reg)
1408 trace_create_file("id", 0444, file->dir, call,
1413 * Other events may have the same class. Only update
1414 * the fields if they are not already defined.
1416 head = trace_get_fields(call);
1417 if (list_empty(head)) {
1418 ret = call->class->define_fields(call);
1420 pr_warning("Could not initialize trace point"
1421 " events/%s\n", call->name);
1425 trace_create_file("filter", 0644, file->dir, call,
1428 trace_create_file("format", 0444, file->dir, call,
1434 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1439 if (!--dir->nr_events) {
1440 debugfs_remove_recursive(dir->entry);
1441 list_del(&dir->list);
1442 __put_system_dir(dir);
1446 static void remove_event_from_tracers(struct ftrace_event_call *call)
1448 struct ftrace_event_file *file;
1449 struct trace_array *tr;
1451 do_for_each_event_file_safe(tr, file) {
1453 if (file->event_call != call)
1456 list_del(&file->list);
1457 debugfs_remove_recursive(file->dir);
1458 remove_subsystem(file->system);
1459 kmem_cache_free(file_cachep, file);
1462 * The do_for_each_event_file_safe() is
1463 * a double loop. After finding the call for this
1464 * trace_array, we use break to jump to the next
1468 } while_for_each_event_file();
1471 static void event_remove(struct ftrace_event_call *call)
1473 struct trace_array *tr;
1474 struct ftrace_event_file *file;
1476 do_for_each_event_file(tr, file) {
1477 if (file->event_call != call)
1479 ftrace_event_enable_disable(file, 0);
1481 * The do_for_each_event_file() is
1482 * a double loop. After finding the call for this
1483 * trace_array, we use break to jump to the next
1487 } while_for_each_event_file();
1489 if (call->event.funcs)
1490 __unregister_ftrace_event(&call->event);
1491 remove_event_from_tracers(call);
1492 list_del(&call->list);
1495 static int event_init(struct ftrace_event_call *call)
1499 if (WARN_ON(!call->name))
1502 if (call->class->raw_init) {
1503 ret = call->class->raw_init(call);
1504 if (ret < 0 && ret != -ENOSYS)
1505 pr_warn("Could not initialize trace events/%s\n",
1513 __register_event(struct ftrace_event_call *call, struct module *mod)
1517 ret = event_init(call);
1521 list_add(&call->list, &ftrace_events);
1527 static struct ftrace_event_file *
1528 trace_create_new_event(struct ftrace_event_call *call,
1529 struct trace_array *tr)
1531 struct ftrace_event_file *file;
1533 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1537 file->event_call = call;
1539 atomic_set(&file->sm_ref, 0);
1540 list_add(&file->list, &tr->events);
1545 /* Add an event to a trace directory */
1547 __trace_add_new_event(struct ftrace_event_call *call,
1548 struct trace_array *tr,
1549 const struct file_operations *id,
1550 const struct file_operations *enable,
1551 const struct file_operations *filter,
1552 const struct file_operations *format)
1554 struct ftrace_event_file *file;
1556 file = trace_create_new_event(call, tr);
1560 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1564 * Just create a decriptor for early init. A descriptor is required
1565 * for enabling events at boot. We want to enable events before
1566 * the filesystem is initialized.
1569 __trace_early_add_new_event(struct ftrace_event_call *call,
1570 struct trace_array *tr)
1572 struct ftrace_event_file *file;
1574 file = trace_create_new_event(call, tr);
1581 struct ftrace_module_file_ops;
1582 static void __add_event_to_tracers(struct ftrace_event_call *call,
1583 struct ftrace_module_file_ops *file_ops);
1585 /* Add an additional event_call dynamically */
1586 int trace_add_event_call(struct ftrace_event_call *call)
1589 mutex_lock(&event_mutex);
1591 ret = __register_event(call, NULL);
1593 __add_event_to_tracers(call, NULL);
1595 mutex_unlock(&event_mutex);
1600 * Must be called under locking both of event_mutex and trace_event_sem.
1602 static void __trace_remove_event_call(struct ftrace_event_call *call)
1605 trace_destroy_fields(call);
1606 destroy_preds(call);
1609 /* Remove an event_call */
1610 void trace_remove_event_call(struct ftrace_event_call *call)
1612 mutex_lock(&event_mutex);
1613 down_write(&trace_event_sem);
1614 __trace_remove_event_call(call);
1615 up_write(&trace_event_sem);
1616 mutex_unlock(&event_mutex);
1619 #define for_each_event(event, start, end) \
1620 for (event = start; \
1621 (unsigned long)event < (unsigned long)end; \
1624 #ifdef CONFIG_MODULES
1626 static LIST_HEAD(ftrace_module_file_list);
1629 * Modules must own their file_operations to keep up with
1630 * reference counting.
1632 struct ftrace_module_file_ops {
1633 struct list_head list;
1635 struct file_operations id;
1636 struct file_operations enable;
1637 struct file_operations format;
1638 struct file_operations filter;
1641 static struct ftrace_module_file_ops *
1642 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1645 * As event_calls are added in groups by module,
1646 * when we find one file_ops, we don't need to search for
1647 * each call in that module, as the rest should be the
1648 * same. Only search for a new one if the last one did
1651 if (file_ops && mod == file_ops->mod)
1654 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1655 if (file_ops->mod == mod)
1661 static struct ftrace_module_file_ops *
1662 trace_create_file_ops(struct module *mod)
1664 struct ftrace_module_file_ops *file_ops;
1667 * This is a bit of a PITA. To allow for correct reference
1668 * counting, modules must "own" their file_operations.
1669 * To do this, we allocate the file operations that will be
1670 * used in the event directory.
1673 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1677 file_ops->mod = mod;
1679 file_ops->id = ftrace_event_id_fops;
1680 file_ops->id.owner = mod;
1682 file_ops->enable = ftrace_enable_fops;
1683 file_ops->enable.owner = mod;
1685 file_ops->filter = ftrace_event_filter_fops;
1686 file_ops->filter.owner = mod;
1688 file_ops->format = ftrace_event_format_fops;
1689 file_ops->format.owner = mod;
1691 list_add(&file_ops->list, &ftrace_module_file_list);
1696 static void trace_module_add_events(struct module *mod)
1698 struct ftrace_module_file_ops *file_ops = NULL;
1699 struct ftrace_event_call **call, **start, **end;
1701 start = mod->trace_events;
1702 end = mod->trace_events + mod->num_trace_events;
1707 file_ops = trace_create_file_ops(mod);
1711 for_each_event(call, start, end) {
1712 __register_event(*call, mod);
1713 __add_event_to_tracers(*call, file_ops);
1717 static void trace_module_remove_events(struct module *mod)
1719 struct ftrace_module_file_ops *file_ops;
1720 struct ftrace_event_call *call, *p;
1721 bool clear_trace = false;
1723 down_write(&trace_event_sem);
1724 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1725 if (call->mod == mod) {
1726 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1728 __trace_remove_event_call(call);
1732 /* Now free the file_operations */
1733 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1734 if (file_ops->mod == mod)
1737 if (&file_ops->list != &ftrace_module_file_list) {
1738 list_del(&file_ops->list);
1741 up_write(&trace_event_sem);
1744 * It is safest to reset the ring buffer if the module being unloaded
1745 * registered any events that were used. The only worry is if
1746 * a new module gets loaded, and takes on the same id as the events
1747 * of this module. When printing out the buffer, traced events left
1748 * over from this module may be passed to the new module events and
1749 * unexpected results may occur.
1752 tracing_reset_all_online_cpus();
1755 static int trace_module_notify(struct notifier_block *self,
1756 unsigned long val, void *data)
1758 struct module *mod = data;
1760 mutex_lock(&event_mutex);
1762 case MODULE_STATE_COMING:
1763 trace_module_add_events(mod);
1765 case MODULE_STATE_GOING:
1766 trace_module_remove_events(mod);
1769 mutex_unlock(&event_mutex);
1775 __trace_add_new_mod_event(struct ftrace_event_call *call,
1776 struct trace_array *tr,
1777 struct ftrace_module_file_ops *file_ops)
1779 return __trace_add_new_event(call, tr,
1780 &file_ops->id, &file_ops->enable,
1781 &file_ops->filter, &file_ops->format);
1785 static inline struct ftrace_module_file_ops *
1786 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1790 static inline int trace_module_notify(struct notifier_block *self,
1791 unsigned long val, void *data)
1796 __trace_add_new_mod_event(struct ftrace_event_call *call,
1797 struct trace_array *tr,
1798 struct ftrace_module_file_ops *file_ops)
1802 #endif /* CONFIG_MODULES */
1804 /* Create a new event directory structure for a trace directory. */
1806 __trace_add_event_dirs(struct trace_array *tr)
1808 struct ftrace_module_file_ops *file_ops = NULL;
1809 struct ftrace_event_call *call;
1812 list_for_each_entry(call, &ftrace_events, list) {
1815 * Directories for events by modules need to
1816 * keep module ref counts when opened (as we don't
1817 * want the module to disappear when reading one
1818 * of these files). The file_ops keep account of
1819 * the module ref count.
1821 file_ops = find_ftrace_file_ops(file_ops, call->mod);
1823 continue; /* Warn? */
1824 ret = __trace_add_new_mod_event(call, tr, file_ops);
1826 pr_warning("Could not create directory for event %s\n",
1830 ret = __trace_add_new_event(call, tr,
1831 &ftrace_event_id_fops,
1832 &ftrace_enable_fops,
1833 &ftrace_event_filter_fops,
1834 &ftrace_event_format_fops);
1836 pr_warning("Could not create directory for event %s\n",
1841 #ifdef CONFIG_DYNAMIC_FTRACE
1844 #define ENABLE_EVENT_STR "enable_event"
1845 #define DISABLE_EVENT_STR "disable_event"
1847 struct event_probe_data {
1848 struct ftrace_event_file *file;
1849 unsigned long count;
1854 static struct ftrace_event_file *
1855 find_event_file(struct trace_array *tr, const char *system, const char *event)
1857 struct ftrace_event_file *file;
1858 struct ftrace_event_call *call;
1860 list_for_each_entry(file, &tr->events, list) {
1862 call = file->event_call;
1864 if (!call->name || !call->class || !call->class->reg)
1867 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1870 if (strcmp(event, call->name) == 0 &&
1871 strcmp(system, call->class->system) == 0)
1878 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1880 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1881 struct event_probe_data *data = *pdata;
1887 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1889 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1893 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1895 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1896 struct event_probe_data *data = *pdata;
1904 /* Skip if the event is in a state we want to switch to */
1905 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1908 if (data->count != -1)
1911 event_enable_probe(ip, parent_ip, _data);
1915 event_enable_print(struct seq_file *m, unsigned long ip,
1916 struct ftrace_probe_ops *ops, void *_data)
1918 struct event_probe_data *data = _data;
1920 seq_printf(m, "%ps:", (void *)ip);
1922 seq_printf(m, "%s:%s:%s",
1923 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1924 data->file->event_call->class->system,
1925 data->file->event_call->name);
1927 if (data->count == -1)
1928 seq_printf(m, ":unlimited\n");
1930 seq_printf(m, ":count=%ld\n", data->count);
1936 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1939 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1940 struct event_probe_data *data = *pdata;
1947 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1950 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1951 struct event_probe_data *data = *pdata;
1953 if (WARN_ON_ONCE(data->ref <= 0))
1958 /* Remove the SOFT_MODE flag */
1959 __ftrace_event_enable_disable(data->file, 0, 1);
1960 module_put(data->file->event_call->mod);
1966 static struct ftrace_probe_ops event_enable_probe_ops = {
1967 .func = event_enable_probe,
1968 .print = event_enable_print,
1969 .init = event_enable_init,
1970 .free = event_enable_free,
1973 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1974 .func = event_enable_count_probe,
1975 .print = event_enable_print,
1976 .init = event_enable_init,
1977 .free = event_enable_free,
1980 static struct ftrace_probe_ops event_disable_probe_ops = {
1981 .func = event_enable_probe,
1982 .print = event_enable_print,
1983 .init = event_enable_init,
1984 .free = event_enable_free,
1987 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1988 .func = event_enable_count_probe,
1989 .print = event_enable_print,
1990 .init = event_enable_init,
1991 .free = event_enable_free,
1995 event_enable_func(struct ftrace_hash *hash,
1996 char *glob, char *cmd, char *param, int enabled)
1998 struct trace_array *tr = top_trace_array();
1999 struct ftrace_event_file *file;
2000 struct ftrace_probe_ops *ops;
2001 struct event_probe_data *data;
2008 /* hash funcs only work with set_ftrace_filter */
2009 if (!enabled || !param)
2012 system = strsep(¶m, ":");
2016 event = strsep(¶m, ":");
2018 mutex_lock(&event_mutex);
2021 file = find_event_file(tr, system, event);
2025 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2028 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2030 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2032 if (glob[0] == '!') {
2033 unregister_ftrace_function_probe_func(glob+1, ops);
2039 data = kzalloc(sizeof(*data), GFP_KERNEL);
2043 data->enable = enable;
2050 number = strsep(¶m, ":");
2053 if (!strlen(number))
2057 * We use the callback data field (which is a pointer)
2060 ret = kstrtoul(number, 0, &data->count);
2065 /* Don't let event modules unload while probe registered */
2066 ret = try_module_get(file->event_call->mod);
2072 ret = __ftrace_event_enable_disable(file, 1, 1);
2075 ret = register_ftrace_function_probe(glob, ops, data);
2077 * The above returns on success the # of functions enabled,
2078 * but if it didn't find any functions it returns zero.
2079 * Consider no functions a failure too.
2086 /* Just return zero, not the number of enabled functions */
2089 mutex_unlock(&event_mutex);
2093 __ftrace_event_enable_disable(file, 0, 1);
2095 module_put(file->event_call->mod);
2101 static struct ftrace_func_command event_enable_cmd = {
2102 .name = ENABLE_EVENT_STR,
2103 .func = event_enable_func,
2106 static struct ftrace_func_command event_disable_cmd = {
2107 .name = DISABLE_EVENT_STR,
2108 .func = event_enable_func,
2111 static __init int register_event_cmds(void)
2115 ret = register_ftrace_command(&event_enable_cmd);
2116 if (WARN_ON(ret < 0))
2118 ret = register_ftrace_command(&event_disable_cmd);
2119 if (WARN_ON(ret < 0))
2120 unregister_ftrace_command(&event_enable_cmd);
2124 static inline int register_event_cmds(void) { return 0; }
2125 #endif /* CONFIG_DYNAMIC_FTRACE */
2128 * The top level array has already had its ftrace_event_file
2129 * descriptors created in order to allow for early events to
2130 * be recorded. This function is called after the debugfs has been
2131 * initialized, and we now have to create the files associated
2135 __trace_early_add_event_dirs(struct trace_array *tr)
2137 struct ftrace_event_file *file;
2141 list_for_each_entry(file, &tr->events, list) {
2142 ret = event_create_dir(tr->event_dir, file,
2143 &ftrace_event_id_fops,
2144 &ftrace_enable_fops,
2145 &ftrace_event_filter_fops,
2146 &ftrace_event_format_fops);
2148 pr_warning("Could not create directory for event %s\n",
2149 file->event_call->name);
2154 * For early boot up, the top trace array requires to have
2155 * a list of events that can be enabled. This must be done before
2156 * the filesystem is set up in order to allow events to be traced
2160 __trace_early_add_events(struct trace_array *tr)
2162 struct ftrace_event_call *call;
2165 list_for_each_entry(call, &ftrace_events, list) {
2166 /* Early boot up should not have any modules loaded */
2167 if (WARN_ON_ONCE(call->mod))
2170 ret = __trace_early_add_new_event(call, tr);
2172 pr_warning("Could not create early event %s\n",
2177 /* Remove the event directory structure for a trace directory. */
2179 __trace_remove_event_dirs(struct trace_array *tr)
2181 struct ftrace_event_file *file, *next;
2183 list_for_each_entry_safe(file, next, &tr->events, list) {
2184 list_del(&file->list);
2185 debugfs_remove_recursive(file->dir);
2186 remove_subsystem(file->system);
2187 kmem_cache_free(file_cachep, file);
2192 __add_event_to_tracers(struct ftrace_event_call *call,
2193 struct ftrace_module_file_ops *file_ops)
2195 struct trace_array *tr;
2197 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2199 __trace_add_new_mod_event(call, tr, file_ops);
2201 __trace_add_new_event(call, tr,
2202 &ftrace_event_id_fops,
2203 &ftrace_enable_fops,
2204 &ftrace_event_filter_fops,
2205 &ftrace_event_format_fops);
2209 static struct notifier_block trace_module_nb = {
2210 .notifier_call = trace_module_notify,
2214 extern struct ftrace_event_call *__start_ftrace_events[];
2215 extern struct ftrace_event_call *__stop_ftrace_events[];
2217 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2219 static __init int setup_trace_event(char *str)
2221 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2222 ring_buffer_expanded = true;
2223 tracing_selftest_disabled = true;
2227 __setup("trace_event=", setup_trace_event);
2229 /* Expects to have event_mutex held when called */
2231 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2233 struct dentry *d_events;
2234 struct dentry *entry;
2236 entry = debugfs_create_file("set_event", 0644, parent,
2237 tr, &ftrace_set_event_fops);
2239 pr_warning("Could not create debugfs 'set_event' entry\n");
2243 d_events = debugfs_create_dir("events", parent);
2245 pr_warning("Could not create debugfs 'events' directory\n");
2249 /* ring buffer internal formats */
2250 trace_create_file("header_page", 0444, d_events,
2251 ring_buffer_print_page_header,
2252 &ftrace_show_header_fops);
2254 trace_create_file("header_event", 0444, d_events,
2255 ring_buffer_print_entry_header,
2256 &ftrace_show_header_fops);
2258 trace_create_file("enable", 0644, d_events,
2259 tr, &ftrace_tr_enable_fops);
2261 tr->event_dir = d_events;
2267 * event_trace_add_tracer - add a instance of a trace_array to events
2268 * @parent: The parent dentry to place the files/directories for events in
2269 * @tr: The trace array associated with these events
2271 * When a new instance is created, it needs to set up its events
2272 * directory, as well as other files associated with events. It also
2273 * creates the event hierachry in the @parent/events directory.
2275 * Returns 0 on success.
2277 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2281 mutex_lock(&event_mutex);
2283 ret = create_event_toplevel_files(parent, tr);
2287 down_write(&trace_event_sem);
2288 __trace_add_event_dirs(tr);
2289 up_write(&trace_event_sem);
2292 mutex_unlock(&event_mutex);
2298 * The top trace array already had its file descriptors created.
2299 * Now the files themselves need to be created.
2302 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2306 mutex_lock(&event_mutex);
2308 ret = create_event_toplevel_files(parent, tr);
2312 down_write(&trace_event_sem);
2313 __trace_early_add_event_dirs(tr);
2314 up_write(&trace_event_sem);
2317 mutex_unlock(&event_mutex);
2322 int event_trace_del_tracer(struct trace_array *tr)
2324 /* Disable any running events */
2325 __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2327 mutex_lock(&event_mutex);
2329 down_write(&trace_event_sem);
2330 __trace_remove_event_dirs(tr);
2331 debugfs_remove_recursive(tr->event_dir);
2332 up_write(&trace_event_sem);
2334 tr->event_dir = NULL;
2336 mutex_unlock(&event_mutex);
2341 static __init int event_trace_memsetup(void)
2343 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2344 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2348 static __init int event_trace_enable(void)
2350 struct trace_array *tr = top_trace_array();
2351 struct ftrace_event_call **iter, *call;
2352 char *buf = bootup_event_buf;
2356 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2359 ret = event_init(call);
2361 list_add(&call->list, &ftrace_events);
2365 * We need the top trace array to have a working set of trace
2366 * points at early init, before the debug files and directories
2367 * are created. Create the file entries now, and attach them
2368 * to the actual file dentries later.
2370 __trace_early_add_events(tr);
2373 token = strsep(&buf, ",");
2380 ret = ftrace_set_clr_event(tr, token, 1);
2382 pr_warn("Failed to enable trace event: %s\n", token);
2385 trace_printk_start_comm();
2387 register_event_cmds();
2392 static __init int event_trace_init(void)
2394 struct trace_array *tr;
2395 struct dentry *d_tracer;
2396 struct dentry *entry;
2399 tr = top_trace_array();
2401 d_tracer = tracing_init_dentry();
2405 entry = debugfs_create_file("available_events", 0444, d_tracer,
2406 tr, &ftrace_avail_fops);
2408 pr_warning("Could not create debugfs "
2409 "'available_events' entry\n");
2411 if (trace_define_common_fields())
2412 pr_warning("tracing: Failed to allocate common fields");
2414 ret = early_event_add_tracer(d_tracer, tr);
2418 ret = register_module_notifier(&trace_module_nb);
2420 pr_warning("Failed to register trace events module notifier\n");
2424 early_initcall(event_trace_memsetup);
2425 core_initcall(event_trace_enable);
2426 fs_initcall(event_trace_init);
2428 #ifdef CONFIG_FTRACE_STARTUP_TEST
2430 static DEFINE_SPINLOCK(test_spinlock);
2431 static DEFINE_SPINLOCK(test_spinlock_irq);
2432 static DEFINE_MUTEX(test_mutex);
2434 static __init void test_work(struct work_struct *dummy)
2436 spin_lock(&test_spinlock);
2437 spin_lock_irq(&test_spinlock_irq);
2439 spin_unlock_irq(&test_spinlock_irq);
2440 spin_unlock(&test_spinlock);
2442 mutex_lock(&test_mutex);
2444 mutex_unlock(&test_mutex);
2447 static __init int event_test_thread(void *unused)
2451 test_malloc = kmalloc(1234, GFP_KERNEL);
2453 pr_info("failed to kmalloc\n");
2455 schedule_on_each_cpu(test_work);
2459 set_current_state(TASK_INTERRUPTIBLE);
2460 while (!kthread_should_stop())
2467 * Do various things that may trigger events.
2469 static __init void event_test_stuff(void)
2471 struct task_struct *test_thread;
2473 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2475 kthread_stop(test_thread);
2479 * For every trace event defined, we will test each trace point separately,
2480 * and then by groups, and finally all trace points.
2482 static __init void event_trace_self_tests(void)
2484 struct ftrace_subsystem_dir *dir;
2485 struct ftrace_event_file *file;
2486 struct ftrace_event_call *call;
2487 struct event_subsystem *system;
2488 struct trace_array *tr;
2491 tr = top_trace_array();
2493 pr_info("Running tests on trace events:\n");
2495 list_for_each_entry(file, &tr->events, list) {
2497 call = file->event_call;
2499 /* Only test those that have a probe */
2500 if (!call->class || !call->class->probe)
2504 * Testing syscall events here is pretty useless, but
2505 * we still do it if configured. But this is time consuming.
2506 * What we really need is a user thread to perform the
2507 * syscalls as we test.
2509 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2510 if (call->class->system &&
2511 strcmp(call->class->system, "syscalls") == 0)
2515 pr_info("Testing event %s: ", call->name);
2518 * If an event is already enabled, someone is using
2519 * it and the self test should not be on.
2521 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2522 pr_warning("Enabled event during self test!\n");
2527 ftrace_event_enable_disable(file, 1);
2529 ftrace_event_enable_disable(file, 0);
2534 /* Now test at the sub system level */
2536 pr_info("Running tests on trace event systems:\n");
2538 list_for_each_entry(dir, &tr->systems, list) {
2540 system = dir->subsystem;
2542 /* the ftrace system is special, skip it */
2543 if (strcmp(system->name, "ftrace") == 0)
2546 pr_info("Testing event system %s: ", system->name);
2548 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2549 if (WARN_ON_ONCE(ret)) {
2550 pr_warning("error enabling system %s\n",
2557 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2558 if (WARN_ON_ONCE(ret)) {
2559 pr_warning("error disabling system %s\n",
2567 /* Test with all events enabled */
2569 pr_info("Running tests on all trace events:\n");
2570 pr_info("Testing all events: ");
2572 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2573 if (WARN_ON_ONCE(ret)) {
2574 pr_warning("error enabling all events\n");
2581 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2582 if (WARN_ON_ONCE(ret)) {
2583 pr_warning("error disabling all events\n");
2590 #ifdef CONFIG_FUNCTION_TRACER
2592 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2595 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2596 struct ftrace_ops *op, struct pt_regs *pt_regs)
2598 struct ring_buffer_event *event;
2599 struct ring_buffer *buffer;
2600 struct ftrace_entry *entry;
2601 unsigned long flags;
2606 pc = preempt_count();
2607 preempt_disable_notrace();
2608 cpu = raw_smp_processor_id();
2609 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2614 local_save_flags(flags);
2616 event = trace_current_buffer_lock_reserve(&buffer,
2617 TRACE_FN, sizeof(*entry),
2621 entry = ring_buffer_event_data(event);
2623 entry->parent_ip = parent_ip;
2625 trace_buffer_unlock_commit(buffer, event, flags, pc);
2628 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2629 preempt_enable_notrace();
2632 static struct ftrace_ops trace_ops __initdata =
2634 .func = function_test_events_call,
2635 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2638 static __init void event_trace_self_test_with_function(void)
2641 ret = register_ftrace_function(&trace_ops);
2642 if (WARN_ON(ret < 0)) {
2643 pr_info("Failed to enable function tracer for event tests\n");
2646 pr_info("Running tests again, along with the function tracer\n");
2647 event_trace_self_tests();
2648 unregister_ftrace_function(&trace_ops);
2651 static __init void event_trace_self_test_with_function(void)
2656 static __init int event_trace_self_tests_init(void)
2658 if (!tracing_selftest_disabled) {
2659 event_trace_self_tests();
2660 event_trace_self_test_with_function();
2666 late_initcall(event_trace_self_tests_init);