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 #define SYSTEM_FL_FREE_NAME (1 << 31)
43 static inline int system_refcount(struct event_subsystem *system)
45 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
48 static int system_refcount_inc(struct event_subsystem *system)
50 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
53 static int system_refcount_dec(struct event_subsystem *system)
55 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
58 /* Double loops, do not use break, only goto's work */
59 #define do_for_each_event_file(tr, file) \
60 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
61 list_for_each_entry(file, &tr->events, list)
63 #define do_for_each_event_file_safe(tr, file) \
64 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
65 struct trace_event_file *___n; \
66 list_for_each_entry_safe(file, ___n, &tr->events, list)
68 #define while_for_each_event_file() \
71 static struct list_head *
72 trace_get_fields(struct trace_event_call *event_call)
74 if (!event_call->class->get_fields)
75 return &event_call->class->fields;
76 return event_call->class->get_fields(event_call);
79 static struct ftrace_event_field *
80 __find_event_field(struct list_head *head, char *name)
82 struct ftrace_event_field *field;
84 list_for_each_entry(field, head, link) {
85 if (!strcmp(field->name, name))
92 struct ftrace_event_field *
93 trace_find_event_field(struct trace_event_call *call, char *name)
95 struct ftrace_event_field *field;
96 struct list_head *head;
98 field = __find_event_field(&ftrace_generic_fields, name);
102 field = __find_event_field(&ftrace_common_fields, name);
106 head = trace_get_fields(call);
107 return __find_event_field(head, name);
110 static int __trace_define_field(struct list_head *head, const char *type,
111 const char *name, int offset, int size,
112 int is_signed, int filter_type)
114 struct ftrace_event_field *field;
116 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
123 if (filter_type == FILTER_OTHER)
124 field->filter_type = filter_assign_type(type);
126 field->filter_type = filter_type;
128 field->offset = offset;
130 field->is_signed = is_signed;
132 list_add(&field->link, head);
137 int trace_define_field(struct trace_event_call *call, const char *type,
138 const char *name, int offset, int size, int is_signed,
141 struct list_head *head;
143 if (WARN_ON(!call->class))
146 head = trace_get_fields(call);
147 return __trace_define_field(head, type, name, offset, size,
148 is_signed, filter_type);
150 EXPORT_SYMBOL_GPL(trace_define_field);
152 #define __generic_field(type, item, filter_type) \
153 ret = __trace_define_field(&ftrace_generic_fields, #type, \
154 #item, 0, 0, is_signed_type(type), \
159 #define __common_field(type, item) \
160 ret = __trace_define_field(&ftrace_common_fields, #type, \
162 offsetof(typeof(ent), item), \
164 is_signed_type(type), FILTER_OTHER); \
168 static int trace_define_generic_fields(void)
172 __generic_field(int, cpu, FILTER_OTHER);
173 __generic_field(char *, comm, FILTER_PTR_STRING);
178 static int trace_define_common_fields(void)
181 struct trace_entry ent;
183 __common_field(unsigned short, type);
184 __common_field(unsigned char, flags);
185 __common_field(unsigned char, preempt_count);
186 __common_field(int, pid);
191 static void trace_destroy_fields(struct trace_event_call *call)
193 struct ftrace_event_field *field, *next;
194 struct list_head *head;
196 head = trace_get_fields(call);
197 list_for_each_entry_safe(field, next, head, link) {
198 list_del(&field->link);
199 kmem_cache_free(field_cachep, field);
203 int trace_event_raw_init(struct trace_event_call *call)
207 id = register_trace_event(&call->event);
213 EXPORT_SYMBOL_GPL(trace_event_raw_init);
215 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
216 struct trace_event_file *trace_file,
219 struct trace_event_call *event_call = trace_file->event_call;
221 local_save_flags(fbuffer->flags);
222 fbuffer->pc = preempt_count();
223 fbuffer->trace_file = trace_file;
226 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
227 event_call->event.type, len,
228 fbuffer->flags, fbuffer->pc);
232 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
233 return fbuffer->entry;
235 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
237 static DEFINE_SPINLOCK(tracepoint_iter_lock);
239 static void output_printk(struct trace_event_buffer *fbuffer)
241 struct trace_event_call *event_call;
242 struct trace_event *event;
244 struct trace_iterator *iter = tracepoint_print_iter;
249 event_call = fbuffer->trace_file->event_call;
250 if (!event_call || !event_call->event.funcs ||
251 !event_call->event.funcs->trace)
254 event = &fbuffer->trace_file->event_call->event;
256 spin_lock_irqsave(&tracepoint_iter_lock, flags);
257 trace_seq_init(&iter->seq);
258 iter->ent = fbuffer->entry;
259 event_call->event.funcs->trace(iter, 0, event);
260 trace_seq_putc(&iter->seq, 0);
261 printk("%s", iter->seq.buffer);
263 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
266 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
268 if (tracepoint_printk)
269 output_printk(fbuffer);
271 event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
272 fbuffer->event, fbuffer->entry,
273 fbuffer->flags, fbuffer->pc);
275 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
277 int trace_event_reg(struct trace_event_call *call,
278 enum trace_reg type, void *data)
280 struct trace_event_file *file = data;
282 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
284 case TRACE_REG_REGISTER:
285 return tracepoint_probe_register(call->tp,
288 case TRACE_REG_UNREGISTER:
289 tracepoint_probe_unregister(call->tp,
294 #ifdef CONFIG_PERF_EVENTS
295 case TRACE_REG_PERF_REGISTER:
296 return tracepoint_probe_register(call->tp,
297 call->class->perf_probe,
299 case TRACE_REG_PERF_UNREGISTER:
300 tracepoint_probe_unregister(call->tp,
301 call->class->perf_probe,
304 case TRACE_REG_PERF_OPEN:
305 case TRACE_REG_PERF_CLOSE:
306 case TRACE_REG_PERF_ADD:
307 case TRACE_REG_PERF_DEL:
313 EXPORT_SYMBOL_GPL(trace_event_reg);
315 void trace_event_enable_cmd_record(bool enable)
317 struct trace_event_file *file;
318 struct trace_array *tr;
320 mutex_lock(&event_mutex);
321 do_for_each_event_file(tr, file) {
323 if (!(file->flags & EVENT_FILE_FL_ENABLED))
327 tracing_start_cmdline_record();
328 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
330 tracing_stop_cmdline_record();
331 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
333 } while_for_each_event_file();
334 mutex_unlock(&event_mutex);
337 static int __ftrace_event_enable_disable(struct trace_event_file *file,
338 int enable, int soft_disable)
340 struct trace_event_call *call = file->event_call;
341 struct trace_array *tr = file->tr;
348 * When soft_disable is set and enable is cleared, the sm_ref
349 * reference counter is decremented. If it reaches 0, we want
350 * to clear the SOFT_DISABLED flag but leave the event in the
351 * state that it was. That is, if the event was enabled and
352 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
353 * is set we do not want the event to be enabled before we
356 * When soft_disable is not set but the SOFT_MODE flag is,
357 * we do nothing. Do not disable the tracepoint, otherwise
358 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
361 if (atomic_dec_return(&file->sm_ref) > 0)
363 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
364 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
366 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
368 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
369 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
370 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
371 tracing_stop_cmdline_record();
372 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
374 call->class->reg(call, TRACE_REG_UNREGISTER, file);
376 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
377 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
378 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
380 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
384 * When soft_disable is set and enable is set, we want to
385 * register the tracepoint for the event, but leave the event
386 * as is. That means, if the event was already enabled, we do
387 * nothing (but set SOFT_MODE). If the event is disabled, we
388 * set SOFT_DISABLED before enabling the event tracepoint, so
389 * it still seems to be disabled.
392 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
394 if (atomic_inc_return(&file->sm_ref) > 1)
396 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
399 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
401 /* Keep the event disabled, when going to SOFT_MODE. */
403 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
405 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
406 tracing_start_cmdline_record();
407 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
409 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
411 tracing_stop_cmdline_record();
412 pr_info("event trace: Could not enable event "
413 "%s\n", trace_event_name(call));
416 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
418 /* WAS_ENABLED gets set but never cleared. */
419 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
427 int trace_event_enable_disable(struct trace_event_file *file,
428 int enable, int soft_disable)
430 return __ftrace_event_enable_disable(file, enable, soft_disable);
433 static int ftrace_event_enable_disable(struct trace_event_file *file,
436 return __ftrace_event_enable_disable(file, enable, 0);
439 static void ftrace_clear_events(struct trace_array *tr)
441 struct trace_event_file *file;
443 mutex_lock(&event_mutex);
444 list_for_each_entry(file, &tr->events, list) {
445 ftrace_event_enable_disable(file, 0);
447 mutex_unlock(&event_mutex);
450 static void __put_system(struct event_subsystem *system)
452 struct event_filter *filter = system->filter;
454 WARN_ON_ONCE(system_refcount(system) == 0);
455 if (system_refcount_dec(system))
458 list_del(&system->list);
461 kfree(filter->filter_string);
464 if (system->ref_count & SYSTEM_FL_FREE_NAME)
469 static void __get_system(struct event_subsystem *system)
471 WARN_ON_ONCE(system_refcount(system) == 0);
472 system_refcount_inc(system);
475 static void __get_system_dir(struct trace_subsystem_dir *dir)
477 WARN_ON_ONCE(dir->ref_count == 0);
479 __get_system(dir->subsystem);
482 static void __put_system_dir(struct trace_subsystem_dir *dir)
484 WARN_ON_ONCE(dir->ref_count == 0);
485 /* If the subsystem is about to be freed, the dir must be too */
486 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
488 __put_system(dir->subsystem);
489 if (!--dir->ref_count)
493 static void put_system(struct trace_subsystem_dir *dir)
495 mutex_lock(&event_mutex);
496 __put_system_dir(dir);
497 mutex_unlock(&event_mutex);
500 static void remove_subsystem(struct trace_subsystem_dir *dir)
505 if (!--dir->nr_events) {
506 tracefs_remove_recursive(dir->entry);
507 list_del(&dir->list);
508 __put_system_dir(dir);
512 static void remove_event_file_dir(struct trace_event_file *file)
514 struct dentry *dir = file->dir;
515 struct dentry *child;
518 spin_lock(&dir->d_lock); /* probably unneeded */
519 list_for_each_entry(child, &dir->d_subdirs, d_child) {
520 if (d_really_is_positive(child)) /* probably unneeded */
521 d_inode(child)->i_private = NULL;
523 spin_unlock(&dir->d_lock);
525 tracefs_remove_recursive(dir);
528 list_del(&file->list);
529 remove_subsystem(file->system);
530 free_event_filter(file->filter);
531 kmem_cache_free(file_cachep, file);
535 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
538 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
539 const char *sub, const char *event, int set)
541 struct trace_event_file *file;
542 struct trace_event_call *call;
546 list_for_each_entry(file, &tr->events, list) {
548 call = file->event_call;
549 name = trace_event_name(call);
551 if (!name || !call->class || !call->class->reg)
554 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
558 strcmp(match, name) != 0 &&
559 strcmp(match, call->class->system) != 0)
562 if (sub && strcmp(sub, call->class->system) != 0)
565 if (event && strcmp(event, name) != 0)
568 ftrace_event_enable_disable(file, set);
576 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
577 const char *sub, const char *event, int set)
581 mutex_lock(&event_mutex);
582 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
583 mutex_unlock(&event_mutex);
588 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
590 char *event = NULL, *sub = NULL, *match;
594 * The buf format can be <subsystem>:<event-name>
595 * *:<event-name> means any event by that name.
596 * :<event-name> is the same.
598 * <subsystem>:* means all events in that subsystem
599 * <subsystem>: means the same.
601 * <name> (no ':') means all events in a subsystem with
602 * the name <name> or any event that matches <name>
605 match = strsep(&buf, ":");
611 if (!strlen(sub) || strcmp(sub, "*") == 0)
613 if (!strlen(event) || strcmp(event, "*") == 0)
617 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
619 /* Put back the colon to allow this to be called again */
627 * trace_set_clr_event - enable or disable an event
628 * @system: system name to match (NULL for any system)
629 * @event: event name to match (NULL for all events, within system)
630 * @set: 1 to enable, 0 to disable
632 * This is a way for other parts of the kernel to enable or disable
635 * Returns 0 on success, -EINVAL if the parameters do not match any
638 int trace_set_clr_event(const char *system, const char *event, int set)
640 struct trace_array *tr = top_trace_array();
645 return __ftrace_set_clr_event(tr, NULL, system, event, set);
647 EXPORT_SYMBOL_GPL(trace_set_clr_event);
649 /* 128 should be much more than enough */
650 #define EVENT_BUF_SIZE 127
653 ftrace_event_write(struct file *file, const char __user *ubuf,
654 size_t cnt, loff_t *ppos)
656 struct trace_parser parser;
657 struct seq_file *m = file->private_data;
658 struct trace_array *tr = m->private;
664 ret = tracing_update_buffers();
668 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
671 read = trace_get_user(&parser, ubuf, cnt, ppos);
673 if (read >= 0 && trace_parser_loaded((&parser))) {
676 if (*parser.buffer == '!')
679 parser.buffer[parser.idx] = 0;
681 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
689 trace_parser_put(&parser);
695 t_next(struct seq_file *m, void *v, loff_t *pos)
697 struct trace_event_file *file = v;
698 struct trace_event_call *call;
699 struct trace_array *tr = m->private;
703 list_for_each_entry_continue(file, &tr->events, list) {
704 call = file->event_call;
706 * The ftrace subsystem is for showing formats only.
707 * They can not be enabled or disabled via the event files.
709 if (call->class && call->class->reg)
716 static void *t_start(struct seq_file *m, loff_t *pos)
718 struct trace_event_file *file;
719 struct trace_array *tr = m->private;
722 mutex_lock(&event_mutex);
724 file = list_entry(&tr->events, struct trace_event_file, list);
725 for (l = 0; l <= *pos; ) {
726 file = t_next(m, file, &l);
734 s_next(struct seq_file *m, void *v, loff_t *pos)
736 struct trace_event_file *file = v;
737 struct trace_array *tr = m->private;
741 list_for_each_entry_continue(file, &tr->events, list) {
742 if (file->flags & EVENT_FILE_FL_ENABLED)
749 static void *s_start(struct seq_file *m, loff_t *pos)
751 struct trace_event_file *file;
752 struct trace_array *tr = m->private;
755 mutex_lock(&event_mutex);
757 file = list_entry(&tr->events, struct trace_event_file, list);
758 for (l = 0; l <= *pos; ) {
759 file = s_next(m, file, &l);
766 static int t_show(struct seq_file *m, void *v)
768 struct trace_event_file *file = v;
769 struct trace_event_call *call = file->event_call;
771 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
772 seq_printf(m, "%s:", call->class->system);
773 seq_printf(m, "%s\n", trace_event_name(call));
778 static void t_stop(struct seq_file *m, void *p)
780 mutex_unlock(&event_mutex);
784 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
787 struct trace_event_file *file;
791 mutex_lock(&event_mutex);
792 file = event_file_data(filp);
795 mutex_unlock(&event_mutex);
800 if (flags & EVENT_FILE_FL_ENABLED &&
801 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
804 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
805 flags & EVENT_FILE_FL_SOFT_MODE)
810 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
814 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
817 struct trace_event_file *file;
821 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
825 ret = tracing_update_buffers();
833 mutex_lock(&event_mutex);
834 file = event_file_data(filp);
836 ret = ftrace_event_enable_disable(file, val);
837 mutex_unlock(&event_mutex);
846 return ret ? ret : cnt;
850 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
853 const char set_to_char[4] = { '?', '0', '1', 'X' };
854 struct trace_subsystem_dir *dir = filp->private_data;
855 struct event_subsystem *system = dir->subsystem;
856 struct trace_event_call *call;
857 struct trace_event_file *file;
858 struct trace_array *tr = dir->tr;
863 mutex_lock(&event_mutex);
864 list_for_each_entry(file, &tr->events, list) {
865 call = file->event_call;
866 if (!trace_event_name(call) || !call->class || !call->class->reg)
869 if (system && strcmp(call->class->system, system->name) != 0)
873 * We need to find out if all the events are set
874 * or if all events or cleared, or if we have
877 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
880 * If we have a mixture, no need to look further.
885 mutex_unlock(&event_mutex);
887 buf[0] = set_to_char[set];
890 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
896 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
899 struct trace_subsystem_dir *dir = filp->private_data;
900 struct event_subsystem *system = dir->subsystem;
901 const char *name = NULL;
905 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
909 ret = tracing_update_buffers();
913 if (val != 0 && val != 1)
917 * Opening of "enable" adds a ref count to system,
918 * so the name is safe to use.
923 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
937 FORMAT_FIELD_SEPERATOR = 2,
941 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
943 struct trace_event_call *call = event_file_data(m->private);
944 struct list_head *common_head = &ftrace_common_fields;
945 struct list_head *head = trace_get_fields(call);
946 struct list_head *node = v;
950 switch ((unsigned long)v) {
955 case FORMAT_FIELD_SEPERATOR:
959 case FORMAT_PRINTFMT:
965 if (node == common_head)
966 return (void *)FORMAT_FIELD_SEPERATOR;
967 else if (node == head)
968 return (void *)FORMAT_PRINTFMT;
973 static int f_show(struct seq_file *m, void *v)
975 struct trace_event_call *call = event_file_data(m->private);
976 struct ftrace_event_field *field;
977 const char *array_descriptor;
979 switch ((unsigned long)v) {
981 seq_printf(m, "name: %s\n", trace_event_name(call));
982 seq_printf(m, "ID: %d\n", call->event.type);
983 seq_puts(m, "format:\n");
986 case FORMAT_FIELD_SEPERATOR:
990 case FORMAT_PRINTFMT:
991 seq_printf(m, "\nprint fmt: %s\n",
996 field = list_entry(v, struct ftrace_event_field, link);
998 * Smartly shows the array type(except dynamic array).
1001 * If TYPE := TYPE[LEN], it is shown:
1002 * field:TYPE VAR[LEN]
1004 array_descriptor = strchr(field->type, '[');
1006 if (!strncmp(field->type, "__data_loc", 10))
1007 array_descriptor = NULL;
1009 if (!array_descriptor)
1010 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1011 field->type, field->name, field->offset,
1012 field->size, !!field->is_signed);
1014 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1015 (int)(array_descriptor - field->type),
1016 field->type, field->name,
1017 array_descriptor, field->offset,
1018 field->size, !!field->is_signed);
1023 static void *f_start(struct seq_file *m, loff_t *pos)
1025 void *p = (void *)FORMAT_HEADER;
1028 /* ->stop() is called even if ->start() fails */
1029 mutex_lock(&event_mutex);
1030 if (!event_file_data(m->private))
1031 return ERR_PTR(-ENODEV);
1033 while (l < *pos && p)
1034 p = f_next(m, p, &l);
1039 static void f_stop(struct seq_file *m, void *p)
1041 mutex_unlock(&event_mutex);
1044 static const struct seq_operations trace_format_seq_ops = {
1051 static int trace_format_open(struct inode *inode, struct file *file)
1056 ret = seq_open(file, &trace_format_seq_ops);
1060 m = file->private_data;
1067 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1069 int id = (long)event_file_data(filp);
1079 len = sprintf(buf, "%d\n", id);
1081 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1085 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1088 struct trace_event_file *file;
1089 struct trace_seq *s;
1095 s = kmalloc(sizeof(*s), GFP_KERNEL);
1102 mutex_lock(&event_mutex);
1103 file = event_file_data(filp);
1105 print_event_filter(file, s);
1106 mutex_unlock(&event_mutex);
1109 r = simple_read_from_buffer(ubuf, cnt, ppos,
1110 s->buffer, trace_seq_used(s));
1118 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1121 struct trace_event_file *file;
1125 if (cnt >= PAGE_SIZE)
1128 buf = (char *)__get_free_page(GFP_TEMPORARY);
1132 if (copy_from_user(buf, ubuf, cnt)) {
1133 free_page((unsigned long) buf);
1138 mutex_lock(&event_mutex);
1139 file = event_file_data(filp);
1141 err = apply_event_filter(file, buf);
1142 mutex_unlock(&event_mutex);
1144 free_page((unsigned long) buf);
1153 static LIST_HEAD(event_subsystems);
1155 static int subsystem_open(struct inode *inode, struct file *filp)
1157 struct event_subsystem *system = NULL;
1158 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1159 struct trace_array *tr;
1162 if (tracing_is_disabled())
1165 /* Make sure the system still exists */
1166 mutex_lock(&trace_types_lock);
1167 mutex_lock(&event_mutex);
1168 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1169 list_for_each_entry(dir, &tr->systems, list) {
1170 if (dir == inode->i_private) {
1171 /* Don't open systems with no events */
1172 if (dir->nr_events) {
1173 __get_system_dir(dir);
1174 system = dir->subsystem;
1181 mutex_unlock(&event_mutex);
1182 mutex_unlock(&trace_types_lock);
1187 /* Some versions of gcc think dir can be uninitialized here */
1190 /* Still need to increment the ref count of the system */
1191 if (trace_array_get(tr) < 0) {
1196 ret = tracing_open_generic(inode, filp);
1198 trace_array_put(tr);
1205 static int system_tr_open(struct inode *inode, struct file *filp)
1207 struct trace_subsystem_dir *dir;
1208 struct trace_array *tr = inode->i_private;
1211 if (tracing_is_disabled())
1214 if (trace_array_get(tr) < 0)
1217 /* Make a temporary dir that has no system but points to tr */
1218 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1220 trace_array_put(tr);
1226 ret = tracing_open_generic(inode, filp);
1228 trace_array_put(tr);
1233 filp->private_data = dir;
1238 static int subsystem_release(struct inode *inode, struct file *file)
1240 struct trace_subsystem_dir *dir = file->private_data;
1242 trace_array_put(dir->tr);
1245 * If dir->subsystem is NULL, then this is a temporary
1246 * descriptor that was made for a trace_array to enable
1258 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1261 struct trace_subsystem_dir *dir = filp->private_data;
1262 struct event_subsystem *system = dir->subsystem;
1263 struct trace_seq *s;
1269 s = kmalloc(sizeof(*s), GFP_KERNEL);
1275 print_subsystem_event_filter(system, s);
1276 r = simple_read_from_buffer(ubuf, cnt, ppos,
1277 s->buffer, trace_seq_used(s));
1285 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1288 struct trace_subsystem_dir *dir = filp->private_data;
1292 if (cnt >= PAGE_SIZE)
1295 buf = (char *)__get_free_page(GFP_TEMPORARY);
1299 if (copy_from_user(buf, ubuf, cnt)) {
1300 free_page((unsigned long) buf);
1305 err = apply_subsystem_event_filter(dir, buf);
1306 free_page((unsigned long) buf);
1316 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1318 int (*func)(struct trace_seq *s) = filp->private_data;
1319 struct trace_seq *s;
1325 s = kmalloc(sizeof(*s), GFP_KERNEL);
1332 r = simple_read_from_buffer(ubuf, cnt, ppos,
1333 s->buffer, trace_seq_used(s));
1340 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1341 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1342 static int ftrace_event_release(struct inode *inode, struct file *file);
1344 static const struct seq_operations show_event_seq_ops = {
1351 static const struct seq_operations show_set_event_seq_ops = {
1358 static const struct file_operations ftrace_avail_fops = {
1359 .open = ftrace_event_avail_open,
1361 .llseek = seq_lseek,
1362 .release = seq_release,
1365 static const struct file_operations ftrace_set_event_fops = {
1366 .open = ftrace_event_set_open,
1368 .write = ftrace_event_write,
1369 .llseek = seq_lseek,
1370 .release = ftrace_event_release,
1373 static const struct file_operations ftrace_enable_fops = {
1374 .open = tracing_open_generic,
1375 .read = event_enable_read,
1376 .write = event_enable_write,
1377 .llseek = default_llseek,
1380 static const struct file_operations ftrace_event_format_fops = {
1381 .open = trace_format_open,
1383 .llseek = seq_lseek,
1384 .release = seq_release,
1387 static const struct file_operations ftrace_event_id_fops = {
1388 .read = event_id_read,
1389 .llseek = default_llseek,
1392 static const struct file_operations ftrace_event_filter_fops = {
1393 .open = tracing_open_generic,
1394 .read = event_filter_read,
1395 .write = event_filter_write,
1396 .llseek = default_llseek,
1399 static const struct file_operations ftrace_subsystem_filter_fops = {
1400 .open = subsystem_open,
1401 .read = subsystem_filter_read,
1402 .write = subsystem_filter_write,
1403 .llseek = default_llseek,
1404 .release = subsystem_release,
1407 static const struct file_operations ftrace_system_enable_fops = {
1408 .open = subsystem_open,
1409 .read = system_enable_read,
1410 .write = system_enable_write,
1411 .llseek = default_llseek,
1412 .release = subsystem_release,
1415 static const struct file_operations ftrace_tr_enable_fops = {
1416 .open = system_tr_open,
1417 .read = system_enable_read,
1418 .write = system_enable_write,
1419 .llseek = default_llseek,
1420 .release = subsystem_release,
1423 static const struct file_operations ftrace_show_header_fops = {
1424 .open = tracing_open_generic,
1425 .read = show_header,
1426 .llseek = default_llseek,
1430 ftrace_event_open(struct inode *inode, struct file *file,
1431 const struct seq_operations *seq_ops)
1436 ret = seq_open(file, seq_ops);
1439 m = file->private_data;
1440 /* copy tr over to seq ops */
1441 m->private = inode->i_private;
1446 static int ftrace_event_release(struct inode *inode, struct file *file)
1448 struct trace_array *tr = inode->i_private;
1450 trace_array_put(tr);
1452 return seq_release(inode, file);
1456 ftrace_event_avail_open(struct inode *inode, struct file *file)
1458 const struct seq_operations *seq_ops = &show_event_seq_ops;
1460 return ftrace_event_open(inode, file, seq_ops);
1464 ftrace_event_set_open(struct inode *inode, struct file *file)
1466 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1467 struct trace_array *tr = inode->i_private;
1470 if (trace_array_get(tr) < 0)
1473 if ((file->f_mode & FMODE_WRITE) &&
1474 (file->f_flags & O_TRUNC))
1475 ftrace_clear_events(tr);
1477 ret = ftrace_event_open(inode, file, seq_ops);
1479 trace_array_put(tr);
1483 static struct event_subsystem *
1484 create_new_subsystem(const char *name)
1486 struct event_subsystem *system;
1488 /* need to create new entry */
1489 system = kmalloc(sizeof(*system), GFP_KERNEL);
1493 system->ref_count = 1;
1495 /* Only allocate if dynamic (kprobes and modules) */
1496 if (!core_kernel_data((unsigned long)name)) {
1497 system->ref_count |= SYSTEM_FL_FREE_NAME;
1498 system->name = kstrdup(name, GFP_KERNEL);
1502 system->name = name;
1504 system->filter = NULL;
1506 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1507 if (!system->filter)
1510 list_add(&system->list, &event_subsystems);
1515 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1516 kfree(system->name);
1521 static struct dentry *
1522 event_subsystem_dir(struct trace_array *tr, const char *name,
1523 struct trace_event_file *file, struct dentry *parent)
1525 struct trace_subsystem_dir *dir;
1526 struct event_subsystem *system;
1527 struct dentry *entry;
1529 /* First see if we did not already create this dir */
1530 list_for_each_entry(dir, &tr->systems, list) {
1531 system = dir->subsystem;
1532 if (strcmp(system->name, name) == 0) {
1539 /* Now see if the system itself exists. */
1540 list_for_each_entry(system, &event_subsystems, list) {
1541 if (strcmp(system->name, name) == 0)
1544 /* Reset system variable when not found */
1545 if (&system->list == &event_subsystems)
1548 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1553 system = create_new_subsystem(name);
1557 __get_system(system);
1559 dir->entry = tracefs_create_dir(name, parent);
1561 pr_warn("Failed to create system directory %s\n", name);
1562 __put_system(system);
1569 dir->subsystem = system;
1572 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1573 &ftrace_subsystem_filter_fops);
1575 kfree(system->filter);
1576 system->filter = NULL;
1577 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1580 trace_create_file("enable", 0644, dir->entry, dir,
1581 &ftrace_system_enable_fops);
1583 list_add(&dir->list, &tr->systems);
1590 /* Only print this message if failed on memory allocation */
1591 if (!dir || !system)
1592 pr_warn("No memory to create event subsystem %s\n", name);
1597 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1599 struct trace_event_call *call = file->event_call;
1600 struct trace_array *tr = file->tr;
1601 struct list_head *head;
1602 struct dentry *d_events;
1607 * If the trace point header did not define TRACE_SYSTEM
1608 * then the system would be called "TRACE_SYSTEM".
1610 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1611 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1617 name = trace_event_name(call);
1618 file->dir = tracefs_create_dir(name, d_events);
1620 pr_warn("Could not create tracefs '%s' directory\n", name);
1624 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1625 trace_create_file("enable", 0644, file->dir, file,
1626 &ftrace_enable_fops);
1628 #ifdef CONFIG_PERF_EVENTS
1629 if (call->event.type && call->class->reg)
1630 trace_create_file("id", 0444, file->dir,
1631 (void *)(long)call->event.type,
1632 &ftrace_event_id_fops);
1636 * Other events may have the same class. Only update
1637 * the fields if they are not already defined.
1639 head = trace_get_fields(call);
1640 if (list_empty(head)) {
1641 ret = call->class->define_fields(call);
1643 pr_warn("Could not initialize trace point events/%s\n",
1648 trace_create_file("filter", 0644, file->dir, file,
1649 &ftrace_event_filter_fops);
1651 trace_create_file("trigger", 0644, file->dir, file,
1652 &event_trigger_fops);
1654 trace_create_file("format", 0444, file->dir, call,
1655 &ftrace_event_format_fops);
1660 static void remove_event_from_tracers(struct trace_event_call *call)
1662 struct trace_event_file *file;
1663 struct trace_array *tr;
1665 do_for_each_event_file_safe(tr, file) {
1666 if (file->event_call != call)
1669 remove_event_file_dir(file);
1671 * The do_for_each_event_file_safe() is
1672 * a double loop. After finding the call for this
1673 * trace_array, we use break to jump to the next
1677 } while_for_each_event_file();
1680 static void event_remove(struct trace_event_call *call)
1682 struct trace_array *tr;
1683 struct trace_event_file *file;
1685 do_for_each_event_file(tr, file) {
1686 if (file->event_call != call)
1688 ftrace_event_enable_disable(file, 0);
1690 * The do_for_each_event_file() is
1691 * a double loop. After finding the call for this
1692 * trace_array, we use break to jump to the next
1696 } while_for_each_event_file();
1698 if (call->event.funcs)
1699 __unregister_trace_event(&call->event);
1700 remove_event_from_tracers(call);
1701 list_del(&call->list);
1704 static int event_init(struct trace_event_call *call)
1709 name = trace_event_name(call);
1713 if (call->class->raw_init) {
1714 ret = call->class->raw_init(call);
1715 if (ret < 0 && ret != -ENOSYS)
1716 pr_warn("Could not initialize trace events/%s\n", name);
1723 __register_event(struct trace_event_call *call, struct module *mod)
1727 ret = event_init(call);
1731 list_add(&call->list, &ftrace_events);
1737 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1742 /* Find the length of the enum value as a string */
1743 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1744 /* Make sure there's enough room to replace the string with the value */
1748 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1750 /* Get the rest of the string of ptr */
1751 rlen = strlen(ptr + len);
1752 memmove(ptr + elen, ptr + len, rlen);
1753 /* Make sure we end the new string */
1754 ptr[elen + rlen] = 0;
1759 static void update_event_printk(struct trace_event_call *call,
1760 struct trace_enum_map *map)
1764 int len = strlen(map->enum_string);
1766 for (ptr = call->print_fmt; *ptr; ptr++) {
1780 if (isdigit(*ptr)) {
1784 /* Check for alpha chars like ULL */
1785 } while (isalnum(*ptr));
1789 * A number must have some kind of delimiter after
1790 * it, and we can ignore that too.
1794 if (isalpha(*ptr) || *ptr == '_') {
1795 if (strncmp(map->enum_string, ptr, len) == 0 &&
1796 !isalnum(ptr[len]) && ptr[len] != '_') {
1797 ptr = enum_replace(ptr, map, len);
1798 /* Hmm, enum string smaller than value */
1799 if (WARN_ON_ONCE(!ptr))
1802 * No need to decrement here, as enum_replace()
1803 * returns the pointer to the character passed
1804 * the enum, and two enums can not be placed
1805 * back to back without something in between.
1806 * We can skip that something in between.
1813 } while (isalnum(*ptr) || *ptr == '_');
1817 * If what comes after this variable is a '.' or
1818 * '->' then we can continue to ignore that string.
1820 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1821 ptr += *ptr == '.' ? 1 : 2;
1827 * Once again, we can skip the delimiter that came
1835 void trace_event_enum_update(struct trace_enum_map **map, int len)
1837 struct trace_event_call *call, *p;
1838 const char *last_system = NULL;
1842 down_write(&trace_event_sem);
1843 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1844 /* events are usually grouped together with systems */
1845 if (!last_system || call->class->system != last_system) {
1847 last_system = call->class->system;
1850 for (i = last_i; i < len; i++) {
1851 if (call->class->system == map[i]->system) {
1852 /* Save the first system if need be */
1855 update_event_printk(call, map[i]);
1859 up_write(&trace_event_sem);
1862 static struct trace_event_file *
1863 trace_create_new_event(struct trace_event_call *call,
1864 struct trace_array *tr)
1866 struct trace_event_file *file;
1868 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1872 file->event_call = call;
1874 atomic_set(&file->sm_ref, 0);
1875 atomic_set(&file->tm_ref, 0);
1876 INIT_LIST_HEAD(&file->triggers);
1877 list_add(&file->list, &tr->events);
1882 /* Add an event to a trace directory */
1884 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
1886 struct trace_event_file *file;
1888 file = trace_create_new_event(call, tr);
1892 return event_create_dir(tr->event_dir, file);
1896 * Just create a decriptor for early init. A descriptor is required
1897 * for enabling events at boot. We want to enable events before
1898 * the filesystem is initialized.
1901 __trace_early_add_new_event(struct trace_event_call *call,
1902 struct trace_array *tr)
1904 struct trace_event_file *file;
1906 file = trace_create_new_event(call, tr);
1913 struct ftrace_module_file_ops;
1914 static void __add_event_to_tracers(struct trace_event_call *call);
1916 /* Add an additional event_call dynamically */
1917 int trace_add_event_call(struct trace_event_call *call)
1920 mutex_lock(&trace_types_lock);
1921 mutex_lock(&event_mutex);
1923 ret = __register_event(call, NULL);
1925 __add_event_to_tracers(call);
1927 mutex_unlock(&event_mutex);
1928 mutex_unlock(&trace_types_lock);
1933 * Must be called under locking of trace_types_lock, event_mutex and
1936 static void __trace_remove_event_call(struct trace_event_call *call)
1939 trace_destroy_fields(call);
1940 free_event_filter(call->filter);
1941 call->filter = NULL;
1944 static int probe_remove_event_call(struct trace_event_call *call)
1946 struct trace_array *tr;
1947 struct trace_event_file *file;
1949 #ifdef CONFIG_PERF_EVENTS
1950 if (call->perf_refcount)
1953 do_for_each_event_file(tr, file) {
1954 if (file->event_call != call)
1957 * We can't rely on ftrace_event_enable_disable(enable => 0)
1958 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
1959 * TRACE_REG_UNREGISTER.
1961 if (file->flags & EVENT_FILE_FL_ENABLED)
1964 * The do_for_each_event_file_safe() is
1965 * a double loop. After finding the call for this
1966 * trace_array, we use break to jump to the next
1970 } while_for_each_event_file();
1972 __trace_remove_event_call(call);
1977 /* Remove an event_call */
1978 int trace_remove_event_call(struct trace_event_call *call)
1982 mutex_lock(&trace_types_lock);
1983 mutex_lock(&event_mutex);
1984 down_write(&trace_event_sem);
1985 ret = probe_remove_event_call(call);
1986 up_write(&trace_event_sem);
1987 mutex_unlock(&event_mutex);
1988 mutex_unlock(&trace_types_lock);
1993 #define for_each_event(event, start, end) \
1994 for (event = start; \
1995 (unsigned long)event < (unsigned long)end; \
1998 #ifdef CONFIG_MODULES
2000 static void trace_module_add_events(struct module *mod)
2002 struct trace_event_call **call, **start, **end;
2004 if (!mod->num_trace_events)
2007 /* Don't add infrastructure for mods without tracepoints */
2008 if (trace_module_has_bad_taint(mod)) {
2009 pr_err("%s: module has bad taint, not creating trace events\n",
2014 start = mod->trace_events;
2015 end = mod->trace_events + mod->num_trace_events;
2017 for_each_event(call, start, end) {
2018 __register_event(*call, mod);
2019 __add_event_to_tracers(*call);
2023 static void trace_module_remove_events(struct module *mod)
2025 struct trace_event_call *call, *p;
2026 bool clear_trace = false;
2028 down_write(&trace_event_sem);
2029 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2030 if (call->mod == mod) {
2031 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2033 __trace_remove_event_call(call);
2036 up_write(&trace_event_sem);
2039 * It is safest to reset the ring buffer if the module being unloaded
2040 * registered any events that were used. The only worry is if
2041 * a new module gets loaded, and takes on the same id as the events
2042 * of this module. When printing out the buffer, traced events left
2043 * over from this module may be passed to the new module events and
2044 * unexpected results may occur.
2047 tracing_reset_all_online_cpus();
2050 static int trace_module_notify(struct notifier_block *self,
2051 unsigned long val, void *data)
2053 struct module *mod = data;
2055 mutex_lock(&trace_types_lock);
2056 mutex_lock(&event_mutex);
2058 case MODULE_STATE_COMING:
2059 trace_module_add_events(mod);
2061 case MODULE_STATE_GOING:
2062 trace_module_remove_events(mod);
2065 mutex_unlock(&event_mutex);
2066 mutex_unlock(&trace_types_lock);
2071 static struct notifier_block trace_module_nb = {
2072 .notifier_call = trace_module_notify,
2073 .priority = 1, /* higher than trace.c module notify */
2075 #endif /* CONFIG_MODULES */
2077 /* Create a new event directory structure for a trace directory. */
2079 __trace_add_event_dirs(struct trace_array *tr)
2081 struct trace_event_call *call;
2084 list_for_each_entry(call, &ftrace_events, list) {
2085 ret = __trace_add_new_event(call, tr);
2087 pr_warn("Could not create directory for event %s\n",
2088 trace_event_name(call));
2092 struct trace_event_file *
2093 find_event_file(struct trace_array *tr, const char *system, const char *event)
2095 struct trace_event_file *file;
2096 struct trace_event_call *call;
2099 list_for_each_entry(file, &tr->events, list) {
2101 call = file->event_call;
2102 name = trace_event_name(call);
2104 if (!name || !call->class || !call->class->reg)
2107 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2110 if (strcmp(event, name) == 0 &&
2111 strcmp(system, call->class->system) == 0)
2117 #ifdef CONFIG_DYNAMIC_FTRACE
2120 #define ENABLE_EVENT_STR "enable_event"
2121 #define DISABLE_EVENT_STR "disable_event"
2123 struct event_probe_data {
2124 struct trace_event_file *file;
2125 unsigned long count;
2131 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2133 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2134 struct event_probe_data *data = *pdata;
2140 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2142 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2146 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2148 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2149 struct event_probe_data *data = *pdata;
2157 /* Skip if the event is in a state we want to switch to */
2158 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2161 if (data->count != -1)
2164 event_enable_probe(ip, parent_ip, _data);
2168 event_enable_print(struct seq_file *m, unsigned long ip,
2169 struct ftrace_probe_ops *ops, void *_data)
2171 struct event_probe_data *data = _data;
2173 seq_printf(m, "%ps:", (void *)ip);
2175 seq_printf(m, "%s:%s:%s",
2176 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2177 data->file->event_call->class->system,
2178 trace_event_name(data->file->event_call));
2180 if (data->count == -1)
2181 seq_puts(m, ":unlimited\n");
2183 seq_printf(m, ":count=%ld\n", data->count);
2189 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2192 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2193 struct event_probe_data *data = *pdata;
2200 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2203 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2204 struct event_probe_data *data = *pdata;
2206 if (WARN_ON_ONCE(data->ref <= 0))
2211 /* Remove the SOFT_MODE flag */
2212 __ftrace_event_enable_disable(data->file, 0, 1);
2213 module_put(data->file->event_call->mod);
2219 static struct ftrace_probe_ops event_enable_probe_ops = {
2220 .func = event_enable_probe,
2221 .print = event_enable_print,
2222 .init = event_enable_init,
2223 .free = event_enable_free,
2226 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2227 .func = event_enable_count_probe,
2228 .print = event_enable_print,
2229 .init = event_enable_init,
2230 .free = event_enable_free,
2233 static struct ftrace_probe_ops event_disable_probe_ops = {
2234 .func = event_enable_probe,
2235 .print = event_enable_print,
2236 .init = event_enable_init,
2237 .free = event_enable_free,
2240 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2241 .func = event_enable_count_probe,
2242 .print = event_enable_print,
2243 .init = event_enable_init,
2244 .free = event_enable_free,
2248 event_enable_func(struct ftrace_hash *hash,
2249 char *glob, char *cmd, char *param, int enabled)
2251 struct trace_array *tr = top_trace_array();
2252 struct trace_event_file *file;
2253 struct ftrace_probe_ops *ops;
2254 struct event_probe_data *data;
2264 /* hash funcs only work with set_ftrace_filter */
2265 if (!enabled || !param)
2268 system = strsep(¶m, ":");
2272 event = strsep(¶m, ":");
2274 mutex_lock(&event_mutex);
2277 file = find_event_file(tr, system, event);
2281 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2284 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2286 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2288 if (glob[0] == '!') {
2289 unregister_ftrace_function_probe_func(glob+1, ops);
2295 data = kzalloc(sizeof(*data), GFP_KERNEL);
2299 data->enable = enable;
2306 number = strsep(¶m, ":");
2309 if (!strlen(number))
2313 * We use the callback data field (which is a pointer)
2316 ret = kstrtoul(number, 0, &data->count);
2321 /* Don't let event modules unload while probe registered */
2322 ret = try_module_get(file->event_call->mod);
2328 ret = __ftrace_event_enable_disable(file, 1, 1);
2331 ret = register_ftrace_function_probe(glob, ops, data);
2333 * The above returns on success the # of functions enabled,
2334 * but if it didn't find any functions it returns zero.
2335 * Consider no functions a failure too.
2342 /* Just return zero, not the number of enabled functions */
2345 mutex_unlock(&event_mutex);
2349 __ftrace_event_enable_disable(file, 0, 1);
2351 module_put(file->event_call->mod);
2357 static struct ftrace_func_command event_enable_cmd = {
2358 .name = ENABLE_EVENT_STR,
2359 .func = event_enable_func,
2362 static struct ftrace_func_command event_disable_cmd = {
2363 .name = DISABLE_EVENT_STR,
2364 .func = event_enable_func,
2367 static __init int register_event_cmds(void)
2371 ret = register_ftrace_command(&event_enable_cmd);
2372 if (WARN_ON(ret < 0))
2374 ret = register_ftrace_command(&event_disable_cmd);
2375 if (WARN_ON(ret < 0))
2376 unregister_ftrace_command(&event_enable_cmd);
2380 static inline int register_event_cmds(void) { return 0; }
2381 #endif /* CONFIG_DYNAMIC_FTRACE */
2384 * The top level array has already had its trace_event_file
2385 * descriptors created in order to allow for early events to
2386 * be recorded. This function is called after the tracefs has been
2387 * initialized, and we now have to create the files associated
2391 __trace_early_add_event_dirs(struct trace_array *tr)
2393 struct trace_event_file *file;
2397 list_for_each_entry(file, &tr->events, list) {
2398 ret = event_create_dir(tr->event_dir, file);
2400 pr_warn("Could not create directory for event %s\n",
2401 trace_event_name(file->event_call));
2406 * For early boot up, the top trace array requires to have
2407 * a list of events that can be enabled. This must be done before
2408 * the filesystem is set up in order to allow events to be traced
2412 __trace_early_add_events(struct trace_array *tr)
2414 struct trace_event_call *call;
2417 list_for_each_entry(call, &ftrace_events, list) {
2418 /* Early boot up should not have any modules loaded */
2419 if (WARN_ON_ONCE(call->mod))
2422 ret = __trace_early_add_new_event(call, tr);
2424 pr_warn("Could not create early event %s\n",
2425 trace_event_name(call));
2429 /* Remove the event directory structure for a trace directory. */
2431 __trace_remove_event_dirs(struct trace_array *tr)
2433 struct trace_event_file *file, *next;
2435 list_for_each_entry_safe(file, next, &tr->events, list)
2436 remove_event_file_dir(file);
2439 static void __add_event_to_tracers(struct trace_event_call *call)
2441 struct trace_array *tr;
2443 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2444 __trace_add_new_event(call, tr);
2447 extern struct trace_event_call *__start_ftrace_events[];
2448 extern struct trace_event_call *__stop_ftrace_events[];
2450 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2452 static __init int setup_trace_event(char *str)
2454 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2455 ring_buffer_expanded = true;
2456 tracing_selftest_disabled = true;
2460 __setup("trace_event=", setup_trace_event);
2462 /* Expects to have event_mutex held when called */
2464 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2466 struct dentry *d_events;
2467 struct dentry *entry;
2469 entry = tracefs_create_file("set_event", 0644, parent,
2470 tr, &ftrace_set_event_fops);
2472 pr_warn("Could not create tracefs 'set_event' entry\n");
2476 d_events = tracefs_create_dir("events", parent);
2478 pr_warn("Could not create tracefs 'events' directory\n");
2482 /* ring buffer internal formats */
2483 trace_create_file("header_page", 0444, d_events,
2484 ring_buffer_print_page_header,
2485 &ftrace_show_header_fops);
2487 trace_create_file("header_event", 0444, d_events,
2488 ring_buffer_print_entry_header,
2489 &ftrace_show_header_fops);
2491 trace_create_file("enable", 0644, d_events,
2492 tr, &ftrace_tr_enable_fops);
2494 tr->event_dir = d_events;
2500 * event_trace_add_tracer - add a instance of a trace_array to events
2501 * @parent: The parent dentry to place the files/directories for events in
2502 * @tr: The trace array associated with these events
2504 * When a new instance is created, it needs to set up its events
2505 * directory, as well as other files associated with events. It also
2506 * creates the event hierachry in the @parent/events directory.
2508 * Returns 0 on success.
2510 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2514 mutex_lock(&event_mutex);
2516 ret = create_event_toplevel_files(parent, tr);
2520 down_write(&trace_event_sem);
2521 __trace_add_event_dirs(tr);
2522 up_write(&trace_event_sem);
2525 mutex_unlock(&event_mutex);
2531 * The top trace array already had its file descriptors created.
2532 * Now the files themselves need to be created.
2535 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2539 mutex_lock(&event_mutex);
2541 ret = create_event_toplevel_files(parent, tr);
2545 down_write(&trace_event_sem);
2546 __trace_early_add_event_dirs(tr);
2547 up_write(&trace_event_sem);
2550 mutex_unlock(&event_mutex);
2555 int event_trace_del_tracer(struct trace_array *tr)
2557 mutex_lock(&event_mutex);
2559 /* Disable any event triggers and associated soft-disabled events */
2560 clear_event_triggers(tr);
2562 /* Disable any running events */
2563 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2565 /* Access to events are within rcu_read_lock_sched() */
2566 synchronize_sched();
2568 down_write(&trace_event_sem);
2569 __trace_remove_event_dirs(tr);
2570 tracefs_remove_recursive(tr->event_dir);
2571 up_write(&trace_event_sem);
2573 tr->event_dir = NULL;
2575 mutex_unlock(&event_mutex);
2580 static __init int event_trace_memsetup(void)
2582 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2583 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
2588 early_enable_events(struct trace_array *tr, bool disable_first)
2590 char *buf = bootup_event_buf;
2595 token = strsep(&buf, ",");
2602 /* Restarting syscalls requires that we stop them first */
2604 ftrace_set_clr_event(tr, token, 0);
2606 ret = ftrace_set_clr_event(tr, token, 1);
2608 pr_warn("Failed to enable trace event: %s\n", token);
2610 /* Put back the comma to allow this to be called again */
2616 static __init int event_trace_enable(void)
2618 struct trace_array *tr = top_trace_array();
2619 struct trace_event_call **iter, *call;
2625 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2628 ret = event_init(call);
2630 list_add(&call->list, &ftrace_events);
2634 * We need the top trace array to have a working set of trace
2635 * points at early init, before the debug files and directories
2636 * are created. Create the file entries now, and attach them
2637 * to the actual file dentries later.
2639 __trace_early_add_events(tr);
2641 early_enable_events(tr, false);
2643 trace_printk_start_comm();
2645 register_event_cmds();
2647 register_trigger_cmds();
2653 * event_trace_enable() is called from trace_event_init() first to
2654 * initialize events and perhaps start any events that are on the
2655 * command line. Unfortunately, there are some events that will not
2656 * start this early, like the system call tracepoints that need
2657 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2658 * is called before pid 1 starts, and this flag is never set, making
2659 * the syscall tracepoint never get reached, but the event is enabled
2660 * regardless (and not doing anything).
2662 static __init int event_trace_enable_again(void)
2664 struct trace_array *tr;
2666 tr = top_trace_array();
2670 early_enable_events(tr, true);
2675 early_initcall(event_trace_enable_again);
2677 static __init int event_trace_init(void)
2679 struct trace_array *tr;
2680 struct dentry *d_tracer;
2681 struct dentry *entry;
2684 tr = top_trace_array();
2688 d_tracer = tracing_init_dentry();
2689 if (IS_ERR(d_tracer))
2692 entry = tracefs_create_file("available_events", 0444, d_tracer,
2693 tr, &ftrace_avail_fops);
2695 pr_warn("Could not create tracefs 'available_events' entry\n");
2697 if (trace_define_generic_fields())
2698 pr_warn("tracing: Failed to allocated generic fields");
2700 if (trace_define_common_fields())
2701 pr_warn("tracing: Failed to allocate common fields");
2703 ret = early_event_add_tracer(d_tracer, tr);
2707 #ifdef CONFIG_MODULES
2708 ret = register_module_notifier(&trace_module_nb);
2710 pr_warn("Failed to register trace events module notifier\n");
2715 void __init trace_event_init(void)
2717 event_trace_memsetup();
2718 init_ftrace_syscalls();
2719 event_trace_enable();
2722 fs_initcall(event_trace_init);
2724 #ifdef CONFIG_FTRACE_STARTUP_TEST
2726 static DEFINE_SPINLOCK(test_spinlock);
2727 static DEFINE_SPINLOCK(test_spinlock_irq);
2728 static DEFINE_MUTEX(test_mutex);
2730 static __init void test_work(struct work_struct *dummy)
2732 spin_lock(&test_spinlock);
2733 spin_lock_irq(&test_spinlock_irq);
2735 spin_unlock_irq(&test_spinlock_irq);
2736 spin_unlock(&test_spinlock);
2738 mutex_lock(&test_mutex);
2740 mutex_unlock(&test_mutex);
2743 static __init int event_test_thread(void *unused)
2747 test_malloc = kmalloc(1234, GFP_KERNEL);
2749 pr_info("failed to kmalloc\n");
2751 schedule_on_each_cpu(test_work);
2755 set_current_state(TASK_INTERRUPTIBLE);
2756 while (!kthread_should_stop()) {
2758 set_current_state(TASK_INTERRUPTIBLE);
2760 __set_current_state(TASK_RUNNING);
2766 * Do various things that may trigger events.
2768 static __init void event_test_stuff(void)
2770 struct task_struct *test_thread;
2772 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2774 kthread_stop(test_thread);
2778 * For every trace event defined, we will test each trace point separately,
2779 * and then by groups, and finally all trace points.
2781 static __init void event_trace_self_tests(void)
2783 struct trace_subsystem_dir *dir;
2784 struct trace_event_file *file;
2785 struct trace_event_call *call;
2786 struct event_subsystem *system;
2787 struct trace_array *tr;
2790 tr = top_trace_array();
2794 pr_info("Running tests on trace events:\n");
2796 list_for_each_entry(file, &tr->events, list) {
2798 call = file->event_call;
2800 /* Only test those that have a probe */
2801 if (!call->class || !call->class->probe)
2805 * Testing syscall events here is pretty useless, but
2806 * we still do it if configured. But this is time consuming.
2807 * What we really need is a user thread to perform the
2808 * syscalls as we test.
2810 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2811 if (call->class->system &&
2812 strcmp(call->class->system, "syscalls") == 0)
2816 pr_info("Testing event %s: ", trace_event_name(call));
2819 * If an event is already enabled, someone is using
2820 * it and the self test should not be on.
2822 if (file->flags & EVENT_FILE_FL_ENABLED) {
2823 pr_warn("Enabled event during self test!\n");
2828 ftrace_event_enable_disable(file, 1);
2830 ftrace_event_enable_disable(file, 0);
2835 /* Now test at the sub system level */
2837 pr_info("Running tests on trace event systems:\n");
2839 list_for_each_entry(dir, &tr->systems, list) {
2841 system = dir->subsystem;
2843 /* the ftrace system is special, skip it */
2844 if (strcmp(system->name, "ftrace") == 0)
2847 pr_info("Testing event system %s: ", system->name);
2849 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2850 if (WARN_ON_ONCE(ret)) {
2851 pr_warn("error enabling system %s\n",
2858 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2859 if (WARN_ON_ONCE(ret)) {
2860 pr_warn("error disabling system %s\n",
2868 /* Test with all events enabled */
2870 pr_info("Running tests on all trace events:\n");
2871 pr_info("Testing all events: ");
2873 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2874 if (WARN_ON_ONCE(ret)) {
2875 pr_warn("error enabling all events\n");
2882 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2883 if (WARN_ON_ONCE(ret)) {
2884 pr_warn("error disabling all events\n");
2891 #ifdef CONFIG_FUNCTION_TRACER
2893 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2895 static struct trace_array *event_tr;
2898 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2899 struct ftrace_ops *op, struct pt_regs *pt_regs)
2901 struct ring_buffer_event *event;
2902 struct ring_buffer *buffer;
2903 struct ftrace_entry *entry;
2904 unsigned long flags;
2909 pc = preempt_count();
2910 preempt_disable_notrace();
2911 cpu = raw_smp_processor_id();
2912 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2917 local_save_flags(flags);
2919 event = trace_current_buffer_lock_reserve(&buffer,
2920 TRACE_FN, sizeof(*entry),
2924 entry = ring_buffer_event_data(event);
2926 entry->parent_ip = parent_ip;
2928 trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
2931 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2932 preempt_enable_notrace();
2935 static struct ftrace_ops trace_ops __initdata =
2937 .func = function_test_events_call,
2938 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2941 static __init void event_trace_self_test_with_function(void)
2944 ret = register_ftrace_function(&trace_ops);
2945 if (WARN_ON(ret < 0)) {
2946 pr_info("Failed to enable function tracer for event tests\n");
2949 pr_info("Running tests again, along with the function tracer\n");
2950 event_tr = top_trace_array();
2951 if (WARN_ON(!event_tr))
2953 event_trace_self_tests();
2954 unregister_ftrace_function(&trace_ops);
2957 static __init void event_trace_self_test_with_function(void)
2962 static __init int event_trace_self_tests_init(void)
2964 if (!tracing_selftest_disabled) {
2965 event_trace_self_tests();
2966 event_trace_self_test_with_function();
2972 late_initcall(event_trace_self_tests_init);