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 #define pr_fmt(fmt) fmt
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/kthread.h>
16 #include <linux/tracefs.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
23 #include <asm/setup.h>
25 #include "trace_output.h"
28 #define TRACE_SYSTEM "TRACE_SYSTEM"
30 DEFINE_MUTEX(event_mutex);
32 LIST_HEAD(ftrace_events);
33 static LIST_HEAD(ftrace_generic_fields);
34 static LIST_HEAD(ftrace_common_fields);
36 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
38 static struct kmem_cache *field_cachep;
39 static struct kmem_cache *file_cachep;
41 static inline int system_refcount(struct event_subsystem *system)
43 return system->ref_count;
46 static int system_refcount_inc(struct event_subsystem *system)
48 return system->ref_count++;
51 static int system_refcount_dec(struct event_subsystem *system)
53 return --system->ref_count;
56 /* Double loops, do not use break, only goto's work */
57 #define do_for_each_event_file(tr, file) \
58 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
59 list_for_each_entry(file, &tr->events, list)
61 #define do_for_each_event_file_safe(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 struct trace_event_file *___n; \
64 list_for_each_entry_safe(file, ___n, &tr->events, list)
66 #define while_for_each_event_file() \
69 static struct list_head *
70 trace_get_fields(struct trace_event_call *event_call)
72 if (!event_call->class->get_fields)
73 return &event_call->class->fields;
74 return event_call->class->get_fields(event_call);
77 static struct ftrace_event_field *
78 __find_event_field(struct list_head *head, char *name)
80 struct ftrace_event_field *field;
82 list_for_each_entry(field, head, link) {
83 if (!strcmp(field->name, name))
90 struct ftrace_event_field *
91 trace_find_event_field(struct trace_event_call *call, char *name)
93 struct ftrace_event_field *field;
94 struct list_head *head;
96 field = __find_event_field(&ftrace_generic_fields, name);
100 field = __find_event_field(&ftrace_common_fields, name);
104 head = trace_get_fields(call);
105 return __find_event_field(head, name);
108 static int __trace_define_field(struct list_head *head, const char *type,
109 const char *name, int offset, int size,
110 int is_signed, int filter_type)
112 struct ftrace_event_field *field;
114 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
121 if (filter_type == FILTER_OTHER)
122 field->filter_type = filter_assign_type(type);
124 field->filter_type = filter_type;
126 field->offset = offset;
128 field->is_signed = is_signed;
130 list_add(&field->link, head);
135 int trace_define_field(struct trace_event_call *call, const char *type,
136 const char *name, int offset, int size, int is_signed,
139 struct list_head *head;
141 if (WARN_ON(!call->class))
144 head = trace_get_fields(call);
145 return __trace_define_field(head, type, name, offset, size,
146 is_signed, filter_type);
148 EXPORT_SYMBOL_GPL(trace_define_field);
150 #define __generic_field(type, item, filter_type) \
151 ret = __trace_define_field(&ftrace_generic_fields, #type, \
152 #item, 0, 0, is_signed_type(type), \
157 #define __common_field(type, item) \
158 ret = __trace_define_field(&ftrace_common_fields, #type, \
160 offsetof(typeof(ent), item), \
162 is_signed_type(type), FILTER_OTHER); \
166 static int trace_define_generic_fields(void)
170 __generic_field(int, cpu, FILTER_OTHER);
171 __generic_field(char *, comm, FILTER_PTR_STRING);
176 static int trace_define_common_fields(void)
179 struct trace_entry ent;
181 __common_field(unsigned short, type);
182 __common_field(unsigned char, flags);
183 __common_field(unsigned char, preempt_count);
184 __common_field(int, pid);
189 static void trace_destroy_fields(struct trace_event_call *call)
191 struct ftrace_event_field *field, *next;
192 struct list_head *head;
194 head = trace_get_fields(call);
195 list_for_each_entry_safe(field, next, head, link) {
196 list_del(&field->link);
197 kmem_cache_free(field_cachep, field);
201 int trace_event_raw_init(struct trace_event_call *call)
205 id = register_trace_event(&call->event);
211 EXPORT_SYMBOL_GPL(trace_event_raw_init);
213 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
214 struct trace_event_file *trace_file,
217 struct trace_event_call *event_call = trace_file->event_call;
219 local_save_flags(fbuffer->flags);
220 fbuffer->pc = preempt_count();
221 fbuffer->trace_file = trace_file;
224 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
225 event_call->event.type, len,
226 fbuffer->flags, fbuffer->pc);
230 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
231 return fbuffer->entry;
233 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
235 static DEFINE_SPINLOCK(tracepoint_iter_lock);
237 static void output_printk(struct trace_event_buffer *fbuffer)
239 struct trace_event_call *event_call;
240 struct trace_event *event;
242 struct trace_iterator *iter = tracepoint_print_iter;
247 event_call = fbuffer->trace_file->event_call;
248 if (!event_call || !event_call->event.funcs ||
249 !event_call->event.funcs->trace)
252 event = &fbuffer->trace_file->event_call->event;
254 spin_lock_irqsave(&tracepoint_iter_lock, flags);
255 trace_seq_init(&iter->seq);
256 iter->ent = fbuffer->entry;
257 event_call->event.funcs->trace(iter, 0, event);
258 trace_seq_putc(&iter->seq, 0);
259 printk("%s", iter->seq.buffer);
261 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
264 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
266 if (tracepoint_printk)
267 output_printk(fbuffer);
269 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
270 fbuffer->event, fbuffer->entry,
271 fbuffer->flags, fbuffer->pc);
273 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
275 int trace_event_reg(struct trace_event_call *call,
276 enum trace_reg type, void *data)
278 struct trace_event_file *file = data;
280 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
282 case TRACE_REG_REGISTER:
283 return tracepoint_probe_register(call->tp,
286 case TRACE_REG_UNREGISTER:
287 tracepoint_probe_unregister(call->tp,
292 #ifdef CONFIG_PERF_EVENTS
293 case TRACE_REG_PERF_REGISTER:
294 return tracepoint_probe_register(call->tp,
295 call->class->perf_probe,
297 case TRACE_REG_PERF_UNREGISTER:
298 tracepoint_probe_unregister(call->tp,
299 call->class->perf_probe,
302 case TRACE_REG_PERF_OPEN:
303 case TRACE_REG_PERF_CLOSE:
304 case TRACE_REG_PERF_ADD:
305 case TRACE_REG_PERF_DEL:
311 EXPORT_SYMBOL_GPL(trace_event_reg);
313 void trace_event_enable_cmd_record(bool enable)
315 struct trace_event_file *file;
316 struct trace_array *tr;
318 mutex_lock(&event_mutex);
319 do_for_each_event_file(tr, file) {
321 if (!(file->flags & EVENT_FILE_FL_ENABLED))
325 tracing_start_cmdline_record();
326 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
328 tracing_stop_cmdline_record();
329 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
331 } while_for_each_event_file();
332 mutex_unlock(&event_mutex);
335 static int __ftrace_event_enable_disable(struct trace_event_file *file,
336 int enable, int soft_disable)
338 struct trace_event_call *call = file->event_call;
339 struct trace_array *tr = file->tr;
346 * When soft_disable is set and enable is cleared, the sm_ref
347 * reference counter is decremented. If it reaches 0, we want
348 * to clear the SOFT_DISABLED flag but leave the event in the
349 * state that it was. That is, if the event was enabled and
350 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
351 * is set we do not want the event to be enabled before we
354 * When soft_disable is not set but the SOFT_MODE flag is,
355 * we do nothing. Do not disable the tracepoint, otherwise
356 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
359 if (atomic_dec_return(&file->sm_ref) > 0)
361 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
362 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
364 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
366 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
367 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
368 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
369 tracing_stop_cmdline_record();
370 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
372 call->class->reg(call, TRACE_REG_UNREGISTER, file);
374 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
375 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
376 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
378 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
382 * When soft_disable is set and enable is set, we want to
383 * register the tracepoint for the event, but leave the event
384 * as is. That means, if the event was already enabled, we do
385 * nothing (but set SOFT_MODE). If the event is disabled, we
386 * set SOFT_DISABLED before enabling the event tracepoint, so
387 * it still seems to be disabled.
390 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
392 if (atomic_inc_return(&file->sm_ref) > 1)
394 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
397 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
399 /* Keep the event disabled, when going to SOFT_MODE. */
401 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
403 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
404 tracing_start_cmdline_record();
405 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
407 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
409 tracing_stop_cmdline_record();
410 pr_info("event trace: Could not enable event "
411 "%s\n", trace_event_name(call));
414 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
416 /* WAS_ENABLED gets set but never cleared. */
417 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
425 int trace_event_enable_disable(struct trace_event_file *file,
426 int enable, int soft_disable)
428 return __ftrace_event_enable_disable(file, enable, soft_disable);
431 static int ftrace_event_enable_disable(struct trace_event_file *file,
434 return __ftrace_event_enable_disable(file, enable, 0);
437 static void ftrace_clear_events(struct trace_array *tr)
439 struct trace_event_file *file;
441 mutex_lock(&event_mutex);
442 list_for_each_entry(file, &tr->events, list) {
443 ftrace_event_enable_disable(file, 0);
445 mutex_unlock(&event_mutex);
448 static void __put_system(struct event_subsystem *system)
450 struct event_filter *filter = system->filter;
452 WARN_ON_ONCE(system_refcount(system) == 0);
453 if (system_refcount_dec(system))
456 list_del(&system->list);
459 kfree(filter->filter_string);
462 kfree_const(system->name);
466 static void __get_system(struct event_subsystem *system)
468 WARN_ON_ONCE(system_refcount(system) == 0);
469 system_refcount_inc(system);
472 static void __get_system_dir(struct trace_subsystem_dir *dir)
474 WARN_ON_ONCE(dir->ref_count == 0);
476 __get_system(dir->subsystem);
479 static void __put_system_dir(struct trace_subsystem_dir *dir)
481 WARN_ON_ONCE(dir->ref_count == 0);
482 /* If the subsystem is about to be freed, the dir must be too */
483 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
485 __put_system(dir->subsystem);
486 if (!--dir->ref_count)
490 static void put_system(struct trace_subsystem_dir *dir)
492 mutex_lock(&event_mutex);
493 __put_system_dir(dir);
494 mutex_unlock(&event_mutex);
497 static void remove_subsystem(struct trace_subsystem_dir *dir)
502 if (!--dir->nr_events) {
503 tracefs_remove_recursive(dir->entry);
504 list_del(&dir->list);
505 __put_system_dir(dir);
509 static void remove_event_file_dir(struct trace_event_file *file)
511 struct dentry *dir = file->dir;
512 struct dentry *child;
515 spin_lock(&dir->d_lock); /* probably unneeded */
516 list_for_each_entry(child, &dir->d_subdirs, d_child) {
517 if (d_really_is_positive(child)) /* probably unneeded */
518 d_inode(child)->i_private = NULL;
520 spin_unlock(&dir->d_lock);
522 tracefs_remove_recursive(dir);
525 list_del(&file->list);
526 remove_subsystem(file->system);
527 free_event_filter(file->filter);
528 kmem_cache_free(file_cachep, file);
532 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
535 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
536 const char *sub, const char *event, int set)
538 struct trace_event_file *file;
539 struct trace_event_call *call;
543 list_for_each_entry(file, &tr->events, list) {
545 call = file->event_call;
546 name = trace_event_name(call);
548 if (!name || !call->class || !call->class->reg)
551 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
555 strcmp(match, name) != 0 &&
556 strcmp(match, call->class->system) != 0)
559 if (sub && strcmp(sub, call->class->system) != 0)
562 if (event && strcmp(event, name) != 0)
565 ftrace_event_enable_disable(file, set);
573 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
574 const char *sub, const char *event, int set)
578 mutex_lock(&event_mutex);
579 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
580 mutex_unlock(&event_mutex);
585 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
587 char *event = NULL, *sub = NULL, *match;
591 * The buf format can be <subsystem>:<event-name>
592 * *:<event-name> means any event by that name.
593 * :<event-name> is the same.
595 * <subsystem>:* means all events in that subsystem
596 * <subsystem>: means the same.
598 * <name> (no ':') means all events in a subsystem with
599 * the name <name> or any event that matches <name>
602 match = strsep(&buf, ":");
608 if (!strlen(sub) || strcmp(sub, "*") == 0)
610 if (!strlen(event) || strcmp(event, "*") == 0)
614 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
616 /* Put back the colon to allow this to be called again */
624 * trace_set_clr_event - enable or disable an event
625 * @system: system name to match (NULL for any system)
626 * @event: event name to match (NULL for all events, within system)
627 * @set: 1 to enable, 0 to disable
629 * This is a way for other parts of the kernel to enable or disable
632 * Returns 0 on success, -EINVAL if the parameters do not match any
635 int trace_set_clr_event(const char *system, const char *event, int set)
637 struct trace_array *tr = top_trace_array();
642 return __ftrace_set_clr_event(tr, NULL, system, event, set);
644 EXPORT_SYMBOL_GPL(trace_set_clr_event);
646 /* 128 should be much more than enough */
647 #define EVENT_BUF_SIZE 127
650 ftrace_event_write(struct file *file, const char __user *ubuf,
651 size_t cnt, loff_t *ppos)
653 struct trace_parser parser;
654 struct seq_file *m = file->private_data;
655 struct trace_array *tr = m->private;
661 ret = tracing_update_buffers();
665 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
668 read = trace_get_user(&parser, ubuf, cnt, ppos);
670 if (read >= 0 && trace_parser_loaded((&parser))) {
673 if (*parser.buffer == '!')
676 parser.buffer[parser.idx] = 0;
678 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
686 trace_parser_put(&parser);
692 t_next(struct seq_file *m, void *v, loff_t *pos)
694 struct trace_event_file *file = v;
695 struct trace_event_call *call;
696 struct trace_array *tr = m->private;
700 list_for_each_entry_continue(file, &tr->events, list) {
701 call = file->event_call;
703 * The ftrace subsystem is for showing formats only.
704 * They can not be enabled or disabled via the event files.
706 if (call->class && call->class->reg)
713 static void *t_start(struct seq_file *m, loff_t *pos)
715 struct trace_event_file *file;
716 struct trace_array *tr = m->private;
719 mutex_lock(&event_mutex);
721 file = list_entry(&tr->events, struct trace_event_file, list);
722 for (l = 0; l <= *pos; ) {
723 file = t_next(m, file, &l);
731 s_next(struct seq_file *m, void *v, loff_t *pos)
733 struct trace_event_file *file = v;
734 struct trace_array *tr = m->private;
738 list_for_each_entry_continue(file, &tr->events, list) {
739 if (file->flags & EVENT_FILE_FL_ENABLED)
746 static void *s_start(struct seq_file *m, loff_t *pos)
748 struct trace_event_file *file;
749 struct trace_array *tr = m->private;
752 mutex_lock(&event_mutex);
754 file = list_entry(&tr->events, struct trace_event_file, list);
755 for (l = 0; l <= *pos; ) {
756 file = s_next(m, file, &l);
763 static int t_show(struct seq_file *m, void *v)
765 struct trace_event_file *file = v;
766 struct trace_event_call *call = file->event_call;
768 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
769 seq_printf(m, "%s:", call->class->system);
770 seq_printf(m, "%s\n", trace_event_name(call));
775 static void t_stop(struct seq_file *m, void *p)
777 mutex_unlock(&event_mutex);
781 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
784 struct trace_event_file *file;
788 mutex_lock(&event_mutex);
789 file = event_file_data(filp);
792 mutex_unlock(&event_mutex);
797 if (flags & EVENT_FILE_FL_ENABLED &&
798 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
801 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
802 flags & EVENT_FILE_FL_SOFT_MODE)
807 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
811 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
814 struct trace_event_file *file;
818 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
822 ret = tracing_update_buffers();
830 mutex_lock(&event_mutex);
831 file = event_file_data(filp);
833 ret = ftrace_event_enable_disable(file, val);
834 mutex_unlock(&event_mutex);
843 return ret ? ret : cnt;
847 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
850 const char set_to_char[4] = { '?', '0', '1', 'X' };
851 struct trace_subsystem_dir *dir = filp->private_data;
852 struct event_subsystem *system = dir->subsystem;
853 struct trace_event_call *call;
854 struct trace_event_file *file;
855 struct trace_array *tr = dir->tr;
860 mutex_lock(&event_mutex);
861 list_for_each_entry(file, &tr->events, list) {
862 call = file->event_call;
863 if (!trace_event_name(call) || !call->class || !call->class->reg)
866 if (system && strcmp(call->class->system, system->name) != 0)
870 * We need to find out if all the events are set
871 * or if all events or cleared, or if we have
874 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
877 * If we have a mixture, no need to look further.
882 mutex_unlock(&event_mutex);
884 buf[0] = set_to_char[set];
887 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
893 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
896 struct trace_subsystem_dir *dir = filp->private_data;
897 struct event_subsystem *system = dir->subsystem;
898 const char *name = NULL;
902 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
906 ret = tracing_update_buffers();
910 if (val != 0 && val != 1)
914 * Opening of "enable" adds a ref count to system,
915 * so the name is safe to use.
920 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
934 FORMAT_FIELD_SEPERATOR = 2,
938 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
940 struct trace_event_call *call = event_file_data(m->private);
941 struct list_head *common_head = &ftrace_common_fields;
942 struct list_head *head = trace_get_fields(call);
943 struct list_head *node = v;
947 switch ((unsigned long)v) {
952 case FORMAT_FIELD_SEPERATOR:
956 case FORMAT_PRINTFMT:
962 if (node == common_head)
963 return (void *)FORMAT_FIELD_SEPERATOR;
964 else if (node == head)
965 return (void *)FORMAT_PRINTFMT;
970 static int f_show(struct seq_file *m, void *v)
972 struct trace_event_call *call = event_file_data(m->private);
973 struct ftrace_event_field *field;
974 const char *array_descriptor;
976 switch ((unsigned long)v) {
978 seq_printf(m, "name: %s\n", trace_event_name(call));
979 seq_printf(m, "ID: %d\n", call->event.type);
980 seq_puts(m, "format:\n");
983 case FORMAT_FIELD_SEPERATOR:
987 case FORMAT_PRINTFMT:
988 seq_printf(m, "\nprint fmt: %s\n",
993 field = list_entry(v, struct ftrace_event_field, link);
995 * Smartly shows the array type(except dynamic array).
998 * If TYPE := TYPE[LEN], it is shown:
999 * field:TYPE VAR[LEN]
1001 array_descriptor = strchr(field->type, '[');
1003 if (!strncmp(field->type, "__data_loc", 10))
1004 array_descriptor = NULL;
1006 if (!array_descriptor)
1007 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1008 field->type, field->name, field->offset,
1009 field->size, !!field->is_signed);
1011 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1012 (int)(array_descriptor - field->type),
1013 field->type, field->name,
1014 array_descriptor, field->offset,
1015 field->size, !!field->is_signed);
1020 static void *f_start(struct seq_file *m, loff_t *pos)
1022 void *p = (void *)FORMAT_HEADER;
1025 /* ->stop() is called even if ->start() fails */
1026 mutex_lock(&event_mutex);
1027 if (!event_file_data(m->private))
1028 return ERR_PTR(-ENODEV);
1030 while (l < *pos && p)
1031 p = f_next(m, p, &l);
1036 static void f_stop(struct seq_file *m, void *p)
1038 mutex_unlock(&event_mutex);
1041 static const struct seq_operations trace_format_seq_ops = {
1048 static int trace_format_open(struct inode *inode, struct file *file)
1053 ret = seq_open(file, &trace_format_seq_ops);
1057 m = file->private_data;
1064 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1066 int id = (long)event_file_data(filp);
1076 len = sprintf(buf, "%d\n", id);
1078 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1082 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1085 struct trace_event_file *file;
1086 struct trace_seq *s;
1092 s = kmalloc(sizeof(*s), GFP_KERNEL);
1099 mutex_lock(&event_mutex);
1100 file = event_file_data(filp);
1102 print_event_filter(file, s);
1103 mutex_unlock(&event_mutex);
1106 r = simple_read_from_buffer(ubuf, cnt, ppos,
1107 s->buffer, trace_seq_used(s));
1115 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1118 struct trace_event_file *file;
1122 if (cnt >= PAGE_SIZE)
1125 buf = (char *)__get_free_page(GFP_TEMPORARY);
1129 if (copy_from_user(buf, ubuf, cnt)) {
1130 free_page((unsigned long) buf);
1135 mutex_lock(&event_mutex);
1136 file = event_file_data(filp);
1138 err = apply_event_filter(file, buf);
1139 mutex_unlock(&event_mutex);
1141 free_page((unsigned long) buf);
1150 static LIST_HEAD(event_subsystems);
1152 static int subsystem_open(struct inode *inode, struct file *filp)
1154 struct event_subsystem *system = NULL;
1155 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1156 struct trace_array *tr;
1159 if (tracing_is_disabled())
1162 /* Make sure the system still exists */
1163 mutex_lock(&trace_types_lock);
1164 mutex_lock(&event_mutex);
1165 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1166 list_for_each_entry(dir, &tr->systems, list) {
1167 if (dir == inode->i_private) {
1168 /* Don't open systems with no events */
1169 if (dir->nr_events) {
1170 __get_system_dir(dir);
1171 system = dir->subsystem;
1178 mutex_unlock(&event_mutex);
1179 mutex_unlock(&trace_types_lock);
1184 /* Some versions of gcc think dir can be uninitialized here */
1187 /* Still need to increment the ref count of the system */
1188 if (trace_array_get(tr) < 0) {
1193 ret = tracing_open_generic(inode, filp);
1195 trace_array_put(tr);
1202 static int system_tr_open(struct inode *inode, struct file *filp)
1204 struct trace_subsystem_dir *dir;
1205 struct trace_array *tr = inode->i_private;
1208 if (tracing_is_disabled())
1211 if (trace_array_get(tr) < 0)
1214 /* Make a temporary dir that has no system but points to tr */
1215 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1217 trace_array_put(tr);
1223 ret = tracing_open_generic(inode, filp);
1225 trace_array_put(tr);
1230 filp->private_data = dir;
1235 static int subsystem_release(struct inode *inode, struct file *file)
1237 struct trace_subsystem_dir *dir = file->private_data;
1239 trace_array_put(dir->tr);
1242 * If dir->subsystem is NULL, then this is a temporary
1243 * descriptor that was made for a trace_array to enable
1255 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1258 struct trace_subsystem_dir *dir = filp->private_data;
1259 struct event_subsystem *system = dir->subsystem;
1260 struct trace_seq *s;
1266 s = kmalloc(sizeof(*s), GFP_KERNEL);
1272 print_subsystem_event_filter(system, s);
1273 r = simple_read_from_buffer(ubuf, cnt, ppos,
1274 s->buffer, trace_seq_used(s));
1282 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1285 struct trace_subsystem_dir *dir = filp->private_data;
1289 if (cnt >= PAGE_SIZE)
1292 buf = (char *)__get_free_page(GFP_TEMPORARY);
1296 if (copy_from_user(buf, ubuf, cnt)) {
1297 free_page((unsigned long) buf);
1302 err = apply_subsystem_event_filter(dir, buf);
1303 free_page((unsigned long) buf);
1313 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1315 int (*func)(struct trace_seq *s) = filp->private_data;
1316 struct trace_seq *s;
1322 s = kmalloc(sizeof(*s), GFP_KERNEL);
1329 r = simple_read_from_buffer(ubuf, cnt, ppos,
1330 s->buffer, trace_seq_used(s));
1337 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1338 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1339 static int ftrace_event_release(struct inode *inode, struct file *file);
1341 static const struct seq_operations show_event_seq_ops = {
1348 static const struct seq_operations show_set_event_seq_ops = {
1355 static const struct file_operations ftrace_avail_fops = {
1356 .open = ftrace_event_avail_open,
1358 .llseek = seq_lseek,
1359 .release = seq_release,
1362 static const struct file_operations ftrace_set_event_fops = {
1363 .open = ftrace_event_set_open,
1365 .write = ftrace_event_write,
1366 .llseek = seq_lseek,
1367 .release = ftrace_event_release,
1370 static const struct file_operations ftrace_enable_fops = {
1371 .open = tracing_open_generic,
1372 .read = event_enable_read,
1373 .write = event_enable_write,
1374 .llseek = default_llseek,
1377 static const struct file_operations ftrace_event_format_fops = {
1378 .open = trace_format_open,
1380 .llseek = seq_lseek,
1381 .release = seq_release,
1384 static const struct file_operations ftrace_event_id_fops = {
1385 .read = event_id_read,
1386 .llseek = default_llseek,
1389 static const struct file_operations ftrace_event_filter_fops = {
1390 .open = tracing_open_generic,
1391 .read = event_filter_read,
1392 .write = event_filter_write,
1393 .llseek = default_llseek,
1396 static const struct file_operations ftrace_subsystem_filter_fops = {
1397 .open = subsystem_open,
1398 .read = subsystem_filter_read,
1399 .write = subsystem_filter_write,
1400 .llseek = default_llseek,
1401 .release = subsystem_release,
1404 static const struct file_operations ftrace_system_enable_fops = {
1405 .open = subsystem_open,
1406 .read = system_enable_read,
1407 .write = system_enable_write,
1408 .llseek = default_llseek,
1409 .release = subsystem_release,
1412 static const struct file_operations ftrace_tr_enable_fops = {
1413 .open = system_tr_open,
1414 .read = system_enable_read,
1415 .write = system_enable_write,
1416 .llseek = default_llseek,
1417 .release = subsystem_release,
1420 static const struct file_operations ftrace_show_header_fops = {
1421 .open = tracing_open_generic,
1422 .read = show_header,
1423 .llseek = default_llseek,
1427 ftrace_event_open(struct inode *inode, struct file *file,
1428 const struct seq_operations *seq_ops)
1433 ret = seq_open(file, seq_ops);
1436 m = file->private_data;
1437 /* copy tr over to seq ops */
1438 m->private = inode->i_private;
1443 static int ftrace_event_release(struct inode *inode, struct file *file)
1445 struct trace_array *tr = inode->i_private;
1447 trace_array_put(tr);
1449 return seq_release(inode, file);
1453 ftrace_event_avail_open(struct inode *inode, struct file *file)
1455 const struct seq_operations *seq_ops = &show_event_seq_ops;
1457 return ftrace_event_open(inode, file, seq_ops);
1461 ftrace_event_set_open(struct inode *inode, struct file *file)
1463 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1464 struct trace_array *tr = inode->i_private;
1467 if (trace_array_get(tr) < 0)
1470 if ((file->f_mode & FMODE_WRITE) &&
1471 (file->f_flags & O_TRUNC))
1472 ftrace_clear_events(tr);
1474 ret = ftrace_event_open(inode, file, seq_ops);
1476 trace_array_put(tr);
1480 static struct event_subsystem *
1481 create_new_subsystem(const char *name)
1483 struct event_subsystem *system;
1485 /* need to create new entry */
1486 system = kmalloc(sizeof(*system), GFP_KERNEL);
1490 system->ref_count = 1;
1492 /* Only allocate if dynamic (kprobes and modules) */
1493 system->name = kstrdup_const(name, GFP_KERNEL);
1497 system->filter = NULL;
1499 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1500 if (!system->filter)
1503 list_add(&system->list, &event_subsystems);
1508 kfree_const(system->name);
1513 static struct dentry *
1514 event_subsystem_dir(struct trace_array *tr, const char *name,
1515 struct trace_event_file *file, struct dentry *parent)
1517 struct trace_subsystem_dir *dir;
1518 struct event_subsystem *system;
1519 struct dentry *entry;
1521 /* First see if we did not already create this dir */
1522 list_for_each_entry(dir, &tr->systems, list) {
1523 system = dir->subsystem;
1524 if (strcmp(system->name, name) == 0) {
1531 /* Now see if the system itself exists. */
1532 list_for_each_entry(system, &event_subsystems, list) {
1533 if (strcmp(system->name, name) == 0)
1536 /* Reset system variable when not found */
1537 if (&system->list == &event_subsystems)
1540 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1545 system = create_new_subsystem(name);
1549 __get_system(system);
1551 dir->entry = tracefs_create_dir(name, parent);
1553 pr_warn("Failed to create system directory %s\n", name);
1554 __put_system(system);
1561 dir->subsystem = system;
1564 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1565 &ftrace_subsystem_filter_fops);
1567 kfree(system->filter);
1568 system->filter = NULL;
1569 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1572 trace_create_file("enable", 0644, dir->entry, dir,
1573 &ftrace_system_enable_fops);
1575 list_add(&dir->list, &tr->systems);
1582 /* Only print this message if failed on memory allocation */
1583 if (!dir || !system)
1584 pr_warn("No memory to create event subsystem %s\n", name);
1589 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1591 struct trace_event_call *call = file->event_call;
1592 struct trace_array *tr = file->tr;
1593 struct list_head *head;
1594 struct dentry *d_events;
1599 * If the trace point header did not define TRACE_SYSTEM
1600 * then the system would be called "TRACE_SYSTEM".
1602 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1603 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1609 name = trace_event_name(call);
1610 file->dir = tracefs_create_dir(name, d_events);
1612 pr_warn("Could not create tracefs '%s' directory\n", name);
1616 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1617 trace_create_file("enable", 0644, file->dir, file,
1618 &ftrace_enable_fops);
1620 #ifdef CONFIG_PERF_EVENTS
1621 if (call->event.type && call->class->reg)
1622 trace_create_file("id", 0444, file->dir,
1623 (void *)(long)call->event.type,
1624 &ftrace_event_id_fops);
1628 * Other events may have the same class. Only update
1629 * the fields if they are not already defined.
1631 head = trace_get_fields(call);
1632 if (list_empty(head)) {
1633 ret = call->class->define_fields(call);
1635 pr_warn("Could not initialize trace point events/%s\n",
1640 trace_create_file("filter", 0644, file->dir, file,
1641 &ftrace_event_filter_fops);
1643 trace_create_file("trigger", 0644, file->dir, file,
1644 &event_trigger_fops);
1646 trace_create_file("format", 0444, file->dir, call,
1647 &ftrace_event_format_fops);
1652 static void remove_event_from_tracers(struct trace_event_call *call)
1654 struct trace_event_file *file;
1655 struct trace_array *tr;
1657 do_for_each_event_file_safe(tr, file) {
1658 if (file->event_call != call)
1661 remove_event_file_dir(file);
1663 * The do_for_each_event_file_safe() is
1664 * a double loop. After finding the call for this
1665 * trace_array, we use break to jump to the next
1669 } while_for_each_event_file();
1672 static void event_remove(struct trace_event_call *call)
1674 struct trace_array *tr;
1675 struct trace_event_file *file;
1677 do_for_each_event_file(tr, file) {
1678 if (file->event_call != call)
1680 ftrace_event_enable_disable(file, 0);
1682 * The do_for_each_event_file() is
1683 * a double loop. After finding the call for this
1684 * trace_array, we use break to jump to the next
1688 } while_for_each_event_file();
1690 if (call->event.funcs)
1691 __unregister_trace_event(&call->event);
1692 remove_event_from_tracers(call);
1693 list_del(&call->list);
1696 static int event_init(struct trace_event_call *call)
1701 name = trace_event_name(call);
1705 if (call->class->raw_init) {
1706 ret = call->class->raw_init(call);
1707 if (ret < 0 && ret != -ENOSYS)
1708 pr_warn("Could not initialize trace events/%s\n", name);
1715 __register_event(struct trace_event_call *call, struct module *mod)
1719 ret = event_init(call);
1723 list_add(&call->list, &ftrace_events);
1729 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1734 /* Find the length of the enum value as a string */
1735 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1736 /* Make sure there's enough room to replace the string with the value */
1740 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1742 /* Get the rest of the string of ptr */
1743 rlen = strlen(ptr + len);
1744 memmove(ptr + elen, ptr + len, rlen);
1745 /* Make sure we end the new string */
1746 ptr[elen + rlen] = 0;
1751 static void update_event_printk(struct trace_event_call *call,
1752 struct trace_enum_map *map)
1756 int len = strlen(map->enum_string);
1758 for (ptr = call->print_fmt; *ptr; ptr++) {
1772 if (isdigit(*ptr)) {
1776 /* Check for alpha chars like ULL */
1777 } while (isalnum(*ptr));
1781 * A number must have some kind of delimiter after
1782 * it, and we can ignore that too.
1786 if (isalpha(*ptr) || *ptr == '_') {
1787 if (strncmp(map->enum_string, ptr, len) == 0 &&
1788 !isalnum(ptr[len]) && ptr[len] != '_') {
1789 ptr = enum_replace(ptr, map, len);
1790 /* Hmm, enum string smaller than value */
1791 if (WARN_ON_ONCE(!ptr))
1794 * No need to decrement here, as enum_replace()
1795 * returns the pointer to the character passed
1796 * the enum, and two enums can not be placed
1797 * back to back without something in between.
1798 * We can skip that something in between.
1805 } while (isalnum(*ptr) || *ptr == '_');
1809 * If what comes after this variable is a '.' or
1810 * '->' then we can continue to ignore that string.
1812 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1813 ptr += *ptr == '.' ? 1 : 2;
1819 * Once again, we can skip the delimiter that came
1827 void trace_event_enum_update(struct trace_enum_map **map, int len)
1829 struct trace_event_call *call, *p;
1830 const char *last_system = NULL;
1834 down_write(&trace_event_sem);
1835 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1836 /* events are usually grouped together with systems */
1837 if (!last_system || call->class->system != last_system) {
1839 last_system = call->class->system;
1842 for (i = last_i; i < len; i++) {
1843 if (call->class->system == map[i]->system) {
1844 /* Save the first system if need be */
1847 update_event_printk(call, map[i]);
1851 up_write(&trace_event_sem);
1854 static struct trace_event_file *
1855 trace_create_new_event(struct trace_event_call *call,
1856 struct trace_array *tr)
1858 struct trace_event_file *file;
1860 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1864 file->event_call = call;
1866 atomic_set(&file->sm_ref, 0);
1867 atomic_set(&file->tm_ref, 0);
1868 INIT_LIST_HEAD(&file->triggers);
1869 list_add(&file->list, &tr->events);
1874 /* Add an event to a trace directory */
1876 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
1878 struct trace_event_file *file;
1880 file = trace_create_new_event(call, tr);
1884 return event_create_dir(tr->event_dir, file);
1888 * Just create a decriptor for early init. A descriptor is required
1889 * for enabling events at boot. We want to enable events before
1890 * the filesystem is initialized.
1893 __trace_early_add_new_event(struct trace_event_call *call,
1894 struct trace_array *tr)
1896 struct trace_event_file *file;
1898 file = trace_create_new_event(call, tr);
1905 struct ftrace_module_file_ops;
1906 static void __add_event_to_tracers(struct trace_event_call *call);
1908 /* Add an additional event_call dynamically */
1909 int trace_add_event_call(struct trace_event_call *call)
1912 mutex_lock(&trace_types_lock);
1913 mutex_lock(&event_mutex);
1915 ret = __register_event(call, NULL);
1917 __add_event_to_tracers(call);
1919 mutex_unlock(&event_mutex);
1920 mutex_unlock(&trace_types_lock);
1925 * Must be called under locking of trace_types_lock, event_mutex and
1928 static void __trace_remove_event_call(struct trace_event_call *call)
1931 trace_destroy_fields(call);
1932 free_event_filter(call->filter);
1933 call->filter = NULL;
1936 static int probe_remove_event_call(struct trace_event_call *call)
1938 struct trace_array *tr;
1939 struct trace_event_file *file;
1941 #ifdef CONFIG_PERF_EVENTS
1942 if (call->perf_refcount)
1945 do_for_each_event_file(tr, file) {
1946 if (file->event_call != call)
1949 * We can't rely on ftrace_event_enable_disable(enable => 0)
1950 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
1951 * TRACE_REG_UNREGISTER.
1953 if (file->flags & EVENT_FILE_FL_ENABLED)
1956 * The do_for_each_event_file_safe() is
1957 * a double loop. After finding the call for this
1958 * trace_array, we use break to jump to the next
1962 } while_for_each_event_file();
1964 __trace_remove_event_call(call);
1969 /* Remove an event_call */
1970 int trace_remove_event_call(struct trace_event_call *call)
1974 mutex_lock(&trace_types_lock);
1975 mutex_lock(&event_mutex);
1976 down_write(&trace_event_sem);
1977 ret = probe_remove_event_call(call);
1978 up_write(&trace_event_sem);
1979 mutex_unlock(&event_mutex);
1980 mutex_unlock(&trace_types_lock);
1985 #define for_each_event(event, start, end) \
1986 for (event = start; \
1987 (unsigned long)event < (unsigned long)end; \
1990 #ifdef CONFIG_MODULES
1992 static void trace_module_add_events(struct module *mod)
1994 struct trace_event_call **call, **start, **end;
1996 if (!mod->num_trace_events)
1999 /* Don't add infrastructure for mods without tracepoints */
2000 if (trace_module_has_bad_taint(mod)) {
2001 pr_err("%s: module has bad taint, not creating trace events\n",
2006 start = mod->trace_events;
2007 end = mod->trace_events + mod->num_trace_events;
2009 for_each_event(call, start, end) {
2010 __register_event(*call, mod);
2011 __add_event_to_tracers(*call);
2015 static void trace_module_remove_events(struct module *mod)
2017 struct trace_event_call *call, *p;
2018 bool clear_trace = false;
2020 down_write(&trace_event_sem);
2021 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2022 if (call->mod == mod) {
2023 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2025 __trace_remove_event_call(call);
2028 up_write(&trace_event_sem);
2031 * It is safest to reset the ring buffer if the module being unloaded
2032 * registered any events that were used. The only worry is if
2033 * a new module gets loaded, and takes on the same id as the events
2034 * of this module. When printing out the buffer, traced events left
2035 * over from this module may be passed to the new module events and
2036 * unexpected results may occur.
2039 tracing_reset_all_online_cpus();
2042 static int trace_module_notify(struct notifier_block *self,
2043 unsigned long val, void *data)
2045 struct module *mod = data;
2047 mutex_lock(&trace_types_lock);
2048 mutex_lock(&event_mutex);
2050 case MODULE_STATE_COMING:
2051 trace_module_add_events(mod);
2053 case MODULE_STATE_GOING:
2054 trace_module_remove_events(mod);
2057 mutex_unlock(&event_mutex);
2058 mutex_unlock(&trace_types_lock);
2063 static struct notifier_block trace_module_nb = {
2064 .notifier_call = trace_module_notify,
2065 .priority = 1, /* higher than trace.c module notify */
2067 #endif /* CONFIG_MODULES */
2069 /* Create a new event directory structure for a trace directory. */
2071 __trace_add_event_dirs(struct trace_array *tr)
2073 struct trace_event_call *call;
2076 list_for_each_entry(call, &ftrace_events, list) {
2077 ret = __trace_add_new_event(call, tr);
2079 pr_warn("Could not create directory for event %s\n",
2080 trace_event_name(call));
2084 struct trace_event_file *
2085 find_event_file(struct trace_array *tr, const char *system, const char *event)
2087 struct trace_event_file *file;
2088 struct trace_event_call *call;
2091 list_for_each_entry(file, &tr->events, list) {
2093 call = file->event_call;
2094 name = trace_event_name(call);
2096 if (!name || !call->class || !call->class->reg)
2099 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2102 if (strcmp(event, name) == 0 &&
2103 strcmp(system, call->class->system) == 0)
2109 #ifdef CONFIG_DYNAMIC_FTRACE
2112 #define ENABLE_EVENT_STR "enable_event"
2113 #define DISABLE_EVENT_STR "disable_event"
2115 struct event_probe_data {
2116 struct trace_event_file *file;
2117 unsigned long count;
2123 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2125 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2126 struct event_probe_data *data = *pdata;
2132 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2134 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2138 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2140 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2141 struct event_probe_data *data = *pdata;
2149 /* Skip if the event is in a state we want to switch to */
2150 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2153 if (data->count != -1)
2156 event_enable_probe(ip, parent_ip, _data);
2160 event_enable_print(struct seq_file *m, unsigned long ip,
2161 struct ftrace_probe_ops *ops, void *_data)
2163 struct event_probe_data *data = _data;
2165 seq_printf(m, "%ps:", (void *)ip);
2167 seq_printf(m, "%s:%s:%s",
2168 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2169 data->file->event_call->class->system,
2170 trace_event_name(data->file->event_call));
2172 if (data->count == -1)
2173 seq_puts(m, ":unlimited\n");
2175 seq_printf(m, ":count=%ld\n", data->count);
2181 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2184 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2185 struct event_probe_data *data = *pdata;
2192 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2195 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2196 struct event_probe_data *data = *pdata;
2198 if (WARN_ON_ONCE(data->ref <= 0))
2203 /* Remove the SOFT_MODE flag */
2204 __ftrace_event_enable_disable(data->file, 0, 1);
2205 module_put(data->file->event_call->mod);
2211 static struct ftrace_probe_ops event_enable_probe_ops = {
2212 .func = event_enable_probe,
2213 .print = event_enable_print,
2214 .init = event_enable_init,
2215 .free = event_enable_free,
2218 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2219 .func = event_enable_count_probe,
2220 .print = event_enable_print,
2221 .init = event_enable_init,
2222 .free = event_enable_free,
2225 static struct ftrace_probe_ops event_disable_probe_ops = {
2226 .func = event_enable_probe,
2227 .print = event_enable_print,
2228 .init = event_enable_init,
2229 .free = event_enable_free,
2232 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2233 .func = event_enable_count_probe,
2234 .print = event_enable_print,
2235 .init = event_enable_init,
2236 .free = event_enable_free,
2240 event_enable_func(struct ftrace_hash *hash,
2241 char *glob, char *cmd, char *param, int enabled)
2243 struct trace_array *tr = top_trace_array();
2244 struct trace_event_file *file;
2245 struct ftrace_probe_ops *ops;
2246 struct event_probe_data *data;
2256 /* hash funcs only work with set_ftrace_filter */
2257 if (!enabled || !param)
2260 system = strsep(¶m, ":");
2264 event = strsep(¶m, ":");
2266 mutex_lock(&event_mutex);
2269 file = find_event_file(tr, system, event);
2273 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2276 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2278 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2280 if (glob[0] == '!') {
2281 unregister_ftrace_function_probe_func(glob+1, ops);
2287 data = kzalloc(sizeof(*data), GFP_KERNEL);
2291 data->enable = enable;
2298 number = strsep(¶m, ":");
2301 if (!strlen(number))
2305 * We use the callback data field (which is a pointer)
2308 ret = kstrtoul(number, 0, &data->count);
2313 /* Don't let event modules unload while probe registered */
2314 ret = try_module_get(file->event_call->mod);
2320 ret = __ftrace_event_enable_disable(file, 1, 1);
2323 ret = register_ftrace_function_probe(glob, ops, data);
2325 * The above returns on success the # of functions enabled,
2326 * but if it didn't find any functions it returns zero.
2327 * Consider no functions a failure too.
2334 /* Just return zero, not the number of enabled functions */
2337 mutex_unlock(&event_mutex);
2341 __ftrace_event_enable_disable(file, 0, 1);
2343 module_put(file->event_call->mod);
2349 static struct ftrace_func_command event_enable_cmd = {
2350 .name = ENABLE_EVENT_STR,
2351 .func = event_enable_func,
2354 static struct ftrace_func_command event_disable_cmd = {
2355 .name = DISABLE_EVENT_STR,
2356 .func = event_enable_func,
2359 static __init int register_event_cmds(void)
2363 ret = register_ftrace_command(&event_enable_cmd);
2364 if (WARN_ON(ret < 0))
2366 ret = register_ftrace_command(&event_disable_cmd);
2367 if (WARN_ON(ret < 0))
2368 unregister_ftrace_command(&event_enable_cmd);
2372 static inline int register_event_cmds(void) { return 0; }
2373 #endif /* CONFIG_DYNAMIC_FTRACE */
2376 * The top level array has already had its trace_event_file
2377 * descriptors created in order to allow for early events to
2378 * be recorded. This function is called after the tracefs has been
2379 * initialized, and we now have to create the files associated
2383 __trace_early_add_event_dirs(struct trace_array *tr)
2385 struct trace_event_file *file;
2389 list_for_each_entry(file, &tr->events, list) {
2390 ret = event_create_dir(tr->event_dir, file);
2392 pr_warn("Could not create directory for event %s\n",
2393 trace_event_name(file->event_call));
2398 * For early boot up, the top trace array requires to have
2399 * a list of events that can be enabled. This must be done before
2400 * the filesystem is set up in order to allow events to be traced
2404 __trace_early_add_events(struct trace_array *tr)
2406 struct trace_event_call *call;
2409 list_for_each_entry(call, &ftrace_events, list) {
2410 /* Early boot up should not have any modules loaded */
2411 if (WARN_ON_ONCE(call->mod))
2414 ret = __trace_early_add_new_event(call, tr);
2416 pr_warn("Could not create early event %s\n",
2417 trace_event_name(call));
2421 /* Remove the event directory structure for a trace directory. */
2423 __trace_remove_event_dirs(struct trace_array *tr)
2425 struct trace_event_file *file, *next;
2427 list_for_each_entry_safe(file, next, &tr->events, list)
2428 remove_event_file_dir(file);
2431 static void __add_event_to_tracers(struct trace_event_call *call)
2433 struct trace_array *tr;
2435 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2436 __trace_add_new_event(call, tr);
2439 extern struct trace_event_call *__start_ftrace_events[];
2440 extern struct trace_event_call *__stop_ftrace_events[];
2442 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2444 static __init int setup_trace_event(char *str)
2446 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2447 ring_buffer_expanded = true;
2448 tracing_selftest_disabled = true;
2452 __setup("trace_event=", setup_trace_event);
2454 /* Expects to have event_mutex held when called */
2456 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2458 struct dentry *d_events;
2459 struct dentry *entry;
2461 entry = tracefs_create_file("set_event", 0644, parent,
2462 tr, &ftrace_set_event_fops);
2464 pr_warn("Could not create tracefs 'set_event' entry\n");
2468 d_events = tracefs_create_dir("events", parent);
2470 pr_warn("Could not create tracefs 'events' directory\n");
2474 /* ring buffer internal formats */
2475 trace_create_file("header_page", 0444, d_events,
2476 ring_buffer_print_page_header,
2477 &ftrace_show_header_fops);
2479 trace_create_file("header_event", 0444, d_events,
2480 ring_buffer_print_entry_header,
2481 &ftrace_show_header_fops);
2483 trace_create_file("enable", 0644, d_events,
2484 tr, &ftrace_tr_enable_fops);
2486 tr->event_dir = d_events;
2492 * event_trace_add_tracer - add a instance of a trace_array to events
2493 * @parent: The parent dentry to place the files/directories for events in
2494 * @tr: The trace array associated with these events
2496 * When a new instance is created, it needs to set up its events
2497 * directory, as well as other files associated with events. It also
2498 * creates the event hierachry in the @parent/events directory.
2500 * Returns 0 on success.
2502 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2506 mutex_lock(&event_mutex);
2508 ret = create_event_toplevel_files(parent, tr);
2512 down_write(&trace_event_sem);
2513 __trace_add_event_dirs(tr);
2514 up_write(&trace_event_sem);
2517 mutex_unlock(&event_mutex);
2523 * The top trace array already had its file descriptors created.
2524 * Now the files themselves need to be created.
2527 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2531 mutex_lock(&event_mutex);
2533 ret = create_event_toplevel_files(parent, tr);
2537 down_write(&trace_event_sem);
2538 __trace_early_add_event_dirs(tr);
2539 up_write(&trace_event_sem);
2542 mutex_unlock(&event_mutex);
2547 int event_trace_del_tracer(struct trace_array *tr)
2549 mutex_lock(&event_mutex);
2551 /* Disable any event triggers and associated soft-disabled events */
2552 clear_event_triggers(tr);
2554 /* Disable any running events */
2555 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2557 /* Access to events are within rcu_read_lock_sched() */
2558 synchronize_sched();
2560 down_write(&trace_event_sem);
2561 __trace_remove_event_dirs(tr);
2562 tracefs_remove_recursive(tr->event_dir);
2563 up_write(&trace_event_sem);
2565 tr->event_dir = NULL;
2567 mutex_unlock(&event_mutex);
2572 static __init int event_trace_memsetup(void)
2574 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2575 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
2580 early_enable_events(struct trace_array *tr, bool disable_first)
2582 char *buf = bootup_event_buf;
2587 token = strsep(&buf, ",");
2594 /* Restarting syscalls requires that we stop them first */
2596 ftrace_set_clr_event(tr, token, 0);
2598 ret = ftrace_set_clr_event(tr, token, 1);
2600 pr_warn("Failed to enable trace event: %s\n", token);
2602 /* Put back the comma to allow this to be called again */
2608 static __init int event_trace_enable(void)
2610 struct trace_array *tr = top_trace_array();
2611 struct trace_event_call **iter, *call;
2617 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2620 ret = event_init(call);
2622 list_add(&call->list, &ftrace_events);
2626 * We need the top trace array to have a working set of trace
2627 * points at early init, before the debug files and directories
2628 * are created. Create the file entries now, and attach them
2629 * to the actual file dentries later.
2631 __trace_early_add_events(tr);
2633 early_enable_events(tr, false);
2635 trace_printk_start_comm();
2637 register_event_cmds();
2639 register_trigger_cmds();
2645 * event_trace_enable() is called from trace_event_init() first to
2646 * initialize events and perhaps start any events that are on the
2647 * command line. Unfortunately, there are some events that will not
2648 * start this early, like the system call tracepoints that need
2649 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2650 * is called before pid 1 starts, and this flag is never set, making
2651 * the syscall tracepoint never get reached, but the event is enabled
2652 * regardless (and not doing anything).
2654 static __init int event_trace_enable_again(void)
2656 struct trace_array *tr;
2658 tr = top_trace_array();
2662 early_enable_events(tr, true);
2667 early_initcall(event_trace_enable_again);
2669 static __init int event_trace_init(void)
2671 struct trace_array *tr;
2672 struct dentry *d_tracer;
2673 struct dentry *entry;
2676 tr = top_trace_array();
2680 d_tracer = tracing_init_dentry();
2681 if (IS_ERR(d_tracer))
2684 entry = tracefs_create_file("available_events", 0444, d_tracer,
2685 tr, &ftrace_avail_fops);
2687 pr_warn("Could not create tracefs 'available_events' entry\n");
2689 if (trace_define_generic_fields())
2690 pr_warn("tracing: Failed to allocated generic fields");
2692 if (trace_define_common_fields())
2693 pr_warn("tracing: Failed to allocate common fields");
2695 ret = early_event_add_tracer(d_tracer, tr);
2699 #ifdef CONFIG_MODULES
2700 ret = register_module_notifier(&trace_module_nb);
2702 pr_warn("Failed to register trace events module notifier\n");
2707 void __init trace_event_init(void)
2709 event_trace_memsetup();
2710 init_ftrace_syscalls();
2711 event_trace_enable();
2714 fs_initcall(event_trace_init);
2716 #ifdef CONFIG_FTRACE_STARTUP_TEST
2718 static DEFINE_SPINLOCK(test_spinlock);
2719 static DEFINE_SPINLOCK(test_spinlock_irq);
2720 static DEFINE_MUTEX(test_mutex);
2722 static __init void test_work(struct work_struct *dummy)
2724 spin_lock(&test_spinlock);
2725 spin_lock_irq(&test_spinlock_irq);
2727 spin_unlock_irq(&test_spinlock_irq);
2728 spin_unlock(&test_spinlock);
2730 mutex_lock(&test_mutex);
2732 mutex_unlock(&test_mutex);
2735 static __init int event_test_thread(void *unused)
2739 test_malloc = kmalloc(1234, GFP_KERNEL);
2741 pr_info("failed to kmalloc\n");
2743 schedule_on_each_cpu(test_work);
2747 set_current_state(TASK_INTERRUPTIBLE);
2748 while (!kthread_should_stop()) {
2750 set_current_state(TASK_INTERRUPTIBLE);
2752 __set_current_state(TASK_RUNNING);
2758 * Do various things that may trigger events.
2760 static __init void event_test_stuff(void)
2762 struct task_struct *test_thread;
2764 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2766 kthread_stop(test_thread);
2770 * For every trace event defined, we will test each trace point separately,
2771 * and then by groups, and finally all trace points.
2773 static __init void event_trace_self_tests(void)
2775 struct trace_subsystem_dir *dir;
2776 struct trace_event_file *file;
2777 struct trace_event_call *call;
2778 struct event_subsystem *system;
2779 struct trace_array *tr;
2782 tr = top_trace_array();
2786 pr_info("Running tests on trace events:\n");
2788 list_for_each_entry(file, &tr->events, list) {
2790 call = file->event_call;
2792 /* Only test those that have a probe */
2793 if (!call->class || !call->class->probe)
2797 * Testing syscall events here is pretty useless, but
2798 * we still do it if configured. But this is time consuming.
2799 * What we really need is a user thread to perform the
2800 * syscalls as we test.
2802 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2803 if (call->class->system &&
2804 strcmp(call->class->system, "syscalls") == 0)
2808 pr_info("Testing event %s: ", trace_event_name(call));
2811 * If an event is already enabled, someone is using
2812 * it and the self test should not be on.
2814 if (file->flags & EVENT_FILE_FL_ENABLED) {
2815 pr_warn("Enabled event during self test!\n");
2820 ftrace_event_enable_disable(file, 1);
2822 ftrace_event_enable_disable(file, 0);
2827 /* Now test at the sub system level */
2829 pr_info("Running tests on trace event systems:\n");
2831 list_for_each_entry(dir, &tr->systems, list) {
2833 system = dir->subsystem;
2835 /* the ftrace system is special, skip it */
2836 if (strcmp(system->name, "ftrace") == 0)
2839 pr_info("Testing event system %s: ", system->name);
2841 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2842 if (WARN_ON_ONCE(ret)) {
2843 pr_warn("error enabling system %s\n",
2850 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2851 if (WARN_ON_ONCE(ret)) {
2852 pr_warn("error disabling system %s\n",
2860 /* Test with all events enabled */
2862 pr_info("Running tests on all trace events:\n");
2863 pr_info("Testing all events: ");
2865 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2866 if (WARN_ON_ONCE(ret)) {
2867 pr_warn("error enabling all events\n");
2874 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2875 if (WARN_ON_ONCE(ret)) {
2876 pr_warn("error disabling all events\n");
2883 #ifdef CONFIG_FUNCTION_TRACER
2885 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2887 static struct trace_array *event_tr;
2890 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2891 struct ftrace_ops *op, struct pt_regs *pt_regs)
2893 struct ring_buffer_event *event;
2894 struct ring_buffer *buffer;
2895 struct ftrace_entry *entry;
2896 unsigned long flags;
2901 pc = preempt_count();
2902 preempt_disable_notrace();
2903 cpu = raw_smp_processor_id();
2904 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2909 local_save_flags(flags);
2911 event = trace_current_buffer_lock_reserve(&buffer,
2912 TRACE_FN, sizeof(*entry),
2916 entry = ring_buffer_event_data(event);
2918 entry->parent_ip = parent_ip;
2920 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
2923 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2924 preempt_enable_notrace();
2927 static struct ftrace_ops trace_ops __initdata =
2929 .func = function_test_events_call,
2930 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2933 static __init void event_trace_self_test_with_function(void)
2936 event_tr = top_trace_array();
2937 if (WARN_ON(!event_tr))
2939 ret = register_ftrace_function(&trace_ops);
2940 if (WARN_ON(ret < 0)) {
2941 pr_info("Failed to enable function tracer for event tests\n");
2944 pr_info("Running tests again, along with the function tracer\n");
2945 event_trace_self_tests();
2946 unregister_ftrace_function(&trace_ops);
2949 static __init void event_trace_self_test_with_function(void)
2954 static __init int event_trace_self_tests_init(void)
2956 if (!tracing_selftest_disabled) {
2957 event_trace_self_tests();
2958 event_trace_self_test_with_function();
2964 late_initcall(event_trace_self_tests_init);