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_common_fields);
35 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
37 static struct kmem_cache *field_cachep;
38 static struct kmem_cache *file_cachep;
40 #define SYSTEM_FL_FREE_NAME (1 << 31)
42 static inline int system_refcount(struct event_subsystem *system)
44 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
47 static int system_refcount_inc(struct event_subsystem *system)
49 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
52 static int system_refcount_dec(struct event_subsystem *system)
54 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
57 /* Double loops, do not use break, only goto's work */
58 #define do_for_each_event_file(tr, file) \
59 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
60 list_for_each_entry(file, &tr->events, list)
62 #define do_for_each_event_file_safe(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 struct ftrace_event_file *___n; \
65 list_for_each_entry_safe(file, ___n, &tr->events, list)
67 #define while_for_each_event_file() \
70 static struct list_head *
71 trace_get_fields(struct ftrace_event_call *event_call)
73 if (!event_call->class->get_fields)
74 return &event_call->class->fields;
75 return event_call->class->get_fields(event_call);
78 static struct ftrace_event_field *
79 __find_event_field(struct list_head *head, char *name)
81 struct ftrace_event_field *field;
83 list_for_each_entry(field, head, link) {
84 if (!strcmp(field->name, name))
91 struct ftrace_event_field *
92 trace_find_event_field(struct ftrace_event_call *call, char *name)
94 struct ftrace_event_field *field;
95 struct list_head *head;
97 field = __find_event_field(&ftrace_common_fields, name);
101 head = trace_get_fields(call);
102 return __find_event_field(head, name);
105 static int __trace_define_field(struct list_head *head, const char *type,
106 const char *name, int offset, int size,
107 int is_signed, int filter_type)
109 struct ftrace_event_field *field;
111 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
118 if (filter_type == FILTER_OTHER)
119 field->filter_type = filter_assign_type(type);
121 field->filter_type = filter_type;
123 field->offset = offset;
125 field->is_signed = is_signed;
127 list_add(&field->link, head);
132 int trace_define_field(struct ftrace_event_call *call, const char *type,
133 const char *name, int offset, int size, int is_signed,
136 struct list_head *head;
138 if (WARN_ON(!call->class))
141 head = trace_get_fields(call);
142 return __trace_define_field(head, type, name, offset, size,
143 is_signed, filter_type);
145 EXPORT_SYMBOL_GPL(trace_define_field);
147 #define __common_field(type, item) \
148 ret = __trace_define_field(&ftrace_common_fields, #type, \
150 offsetof(typeof(ent), item), \
152 is_signed_type(type), FILTER_OTHER); \
156 static int trace_define_common_fields(void)
159 struct trace_entry ent;
161 __common_field(unsigned short, type);
162 __common_field(unsigned char, flags);
163 __common_field(unsigned char, preempt_count);
164 __common_field(int, pid);
169 static void trace_destroy_fields(struct ftrace_event_call *call)
171 struct ftrace_event_field *field, *next;
172 struct list_head *head;
174 head = trace_get_fields(call);
175 list_for_each_entry_safe(field, next, head, link) {
176 list_del(&field->link);
177 kmem_cache_free(field_cachep, field);
181 int trace_event_raw_init(struct ftrace_event_call *call)
185 id = register_ftrace_event(&call->event);
191 EXPORT_SYMBOL_GPL(trace_event_raw_init);
193 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer *fbuffer,
194 struct ftrace_event_file *ftrace_file,
197 struct ftrace_event_call *event_call = ftrace_file->event_call;
199 local_save_flags(fbuffer->flags);
200 fbuffer->pc = preempt_count();
201 fbuffer->ftrace_file = ftrace_file;
204 trace_event_buffer_lock_reserve(&fbuffer->buffer, ftrace_file,
205 event_call->event.type, len,
206 fbuffer->flags, fbuffer->pc);
210 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
211 return fbuffer->entry;
213 EXPORT_SYMBOL_GPL(ftrace_event_buffer_reserve);
215 static DEFINE_SPINLOCK(tracepoint_iter_lock);
217 static void output_printk(struct ftrace_event_buffer *fbuffer)
219 struct ftrace_event_call *event_call;
220 struct trace_event *event;
222 struct trace_iterator *iter = tracepoint_print_iter;
227 event_call = fbuffer->ftrace_file->event_call;
228 if (!event_call || !event_call->event.funcs ||
229 !event_call->event.funcs->trace)
232 event = &fbuffer->ftrace_file->event_call->event;
234 spin_lock_irqsave(&tracepoint_iter_lock, flags);
235 trace_seq_init(&iter->seq);
236 iter->ent = fbuffer->entry;
237 event_call->event.funcs->trace(iter, 0, event);
238 trace_seq_putc(&iter->seq, 0);
239 printk("%s", iter->seq.buffer);
241 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
244 void ftrace_event_buffer_commit(struct ftrace_event_buffer *fbuffer)
246 if (tracepoint_printk)
247 output_printk(fbuffer);
249 event_trigger_unlock_commit(fbuffer->ftrace_file, fbuffer->buffer,
250 fbuffer->event, fbuffer->entry,
251 fbuffer->flags, fbuffer->pc);
253 EXPORT_SYMBOL_GPL(ftrace_event_buffer_commit);
255 int ftrace_event_reg(struct ftrace_event_call *call,
256 enum trace_reg type, void *data)
258 struct ftrace_event_file *file = data;
260 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
262 case TRACE_REG_REGISTER:
263 return tracepoint_probe_register(call->tp,
266 case TRACE_REG_UNREGISTER:
267 tracepoint_probe_unregister(call->tp,
272 #ifdef CONFIG_PERF_EVENTS
273 case TRACE_REG_PERF_REGISTER:
274 return tracepoint_probe_register(call->tp,
275 call->class->perf_probe,
277 case TRACE_REG_PERF_UNREGISTER:
278 tracepoint_probe_unregister(call->tp,
279 call->class->perf_probe,
282 case TRACE_REG_PERF_OPEN:
283 case TRACE_REG_PERF_CLOSE:
284 case TRACE_REG_PERF_ADD:
285 case TRACE_REG_PERF_DEL:
291 EXPORT_SYMBOL_GPL(ftrace_event_reg);
293 void trace_event_enable_cmd_record(bool enable)
295 struct ftrace_event_file *file;
296 struct trace_array *tr;
298 mutex_lock(&event_mutex);
299 do_for_each_event_file(tr, file) {
301 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
305 tracing_start_cmdline_record();
306 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
308 tracing_stop_cmdline_record();
309 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
311 } while_for_each_event_file();
312 mutex_unlock(&event_mutex);
315 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
316 int enable, int soft_disable)
318 struct ftrace_event_call *call = file->event_call;
325 * When soft_disable is set and enable is cleared, the sm_ref
326 * reference counter is decremented. If it reaches 0, we want
327 * to clear the SOFT_DISABLED flag but leave the event in the
328 * state that it was. That is, if the event was enabled and
329 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
330 * is set we do not want the event to be enabled before we
333 * When soft_disable is not set but the SOFT_MODE flag is,
334 * we do nothing. Do not disable the tracepoint, otherwise
335 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
338 if (atomic_dec_return(&file->sm_ref) > 0)
340 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
341 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
343 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
345 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
346 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
347 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
348 tracing_stop_cmdline_record();
349 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
351 call->class->reg(call, TRACE_REG_UNREGISTER, file);
353 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
354 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
355 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
357 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
361 * When soft_disable is set and enable is set, we want to
362 * register the tracepoint for the event, but leave the event
363 * as is. That means, if the event was already enabled, we do
364 * nothing (but set SOFT_MODE). If the event is disabled, we
365 * set SOFT_DISABLED before enabling the event tracepoint, so
366 * it still seems to be disabled.
369 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
371 if (atomic_inc_return(&file->sm_ref) > 1)
373 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
376 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
378 /* Keep the event disabled, when going to SOFT_MODE. */
380 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
382 if (trace_flags & TRACE_ITER_RECORD_CMD) {
383 tracing_start_cmdline_record();
384 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
386 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
388 tracing_stop_cmdline_record();
389 pr_info("event trace: Could not enable event "
390 "%s\n", ftrace_event_name(call));
393 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
395 /* WAS_ENABLED gets set but never cleared. */
396 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
404 int trace_event_enable_disable(struct ftrace_event_file *file,
405 int enable, int soft_disable)
407 return __ftrace_event_enable_disable(file, enable, soft_disable);
410 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
413 return __ftrace_event_enable_disable(file, enable, 0);
416 static void ftrace_clear_events(struct trace_array *tr)
418 struct ftrace_event_file *file;
420 mutex_lock(&event_mutex);
421 list_for_each_entry(file, &tr->events, list) {
422 ftrace_event_enable_disable(file, 0);
424 mutex_unlock(&event_mutex);
427 static void __put_system(struct event_subsystem *system)
429 struct event_filter *filter = system->filter;
431 WARN_ON_ONCE(system_refcount(system) == 0);
432 if (system_refcount_dec(system))
435 list_del(&system->list);
438 kfree(filter->filter_string);
441 if (system->ref_count & SYSTEM_FL_FREE_NAME)
446 static void __get_system(struct event_subsystem *system)
448 WARN_ON_ONCE(system_refcount(system) == 0);
449 system_refcount_inc(system);
452 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
454 WARN_ON_ONCE(dir->ref_count == 0);
456 __get_system(dir->subsystem);
459 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
461 WARN_ON_ONCE(dir->ref_count == 0);
462 /* If the subsystem is about to be freed, the dir must be too */
463 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
465 __put_system(dir->subsystem);
466 if (!--dir->ref_count)
470 static void put_system(struct ftrace_subsystem_dir *dir)
472 mutex_lock(&event_mutex);
473 __put_system_dir(dir);
474 mutex_unlock(&event_mutex);
477 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
482 if (!--dir->nr_events) {
483 tracefs_remove_recursive(dir->entry);
484 list_del(&dir->list);
485 __put_system_dir(dir);
489 static void remove_event_file_dir(struct ftrace_event_file *file)
491 struct dentry *dir = file->dir;
492 struct dentry *child;
495 spin_lock(&dir->d_lock); /* probably unneeded */
496 list_for_each_entry(child, &dir->d_subdirs, d_child) {
497 if (child->d_inode) /* probably unneeded */
498 child->d_inode->i_private = NULL;
500 spin_unlock(&dir->d_lock);
502 tracefs_remove_recursive(dir);
505 list_del(&file->list);
506 remove_subsystem(file->system);
507 free_event_filter(file->filter);
508 kmem_cache_free(file_cachep, file);
512 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
515 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
516 const char *sub, const char *event, int set)
518 struct ftrace_event_file *file;
519 struct ftrace_event_call *call;
523 list_for_each_entry(file, &tr->events, list) {
525 call = file->event_call;
526 name = ftrace_event_name(call);
528 if (!name || !call->class || !call->class->reg)
531 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
535 strcmp(match, name) != 0 &&
536 strcmp(match, call->class->system) != 0)
539 if (sub && strcmp(sub, call->class->system) != 0)
542 if (event && strcmp(event, name) != 0)
545 ftrace_event_enable_disable(file, set);
553 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
554 const char *sub, const char *event, int set)
558 mutex_lock(&event_mutex);
559 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
560 mutex_unlock(&event_mutex);
565 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
567 char *event = NULL, *sub = NULL, *match;
571 * The buf format can be <subsystem>:<event-name>
572 * *:<event-name> means any event by that name.
573 * :<event-name> is the same.
575 * <subsystem>:* means all events in that subsystem
576 * <subsystem>: means the same.
578 * <name> (no ':') means all events in a subsystem with
579 * the name <name> or any event that matches <name>
582 match = strsep(&buf, ":");
588 if (!strlen(sub) || strcmp(sub, "*") == 0)
590 if (!strlen(event) || strcmp(event, "*") == 0)
594 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
596 /* Put back the colon to allow this to be called again */
604 * trace_set_clr_event - enable or disable an event
605 * @system: system name to match (NULL for any system)
606 * @event: event name to match (NULL for all events, within system)
607 * @set: 1 to enable, 0 to disable
609 * This is a way for other parts of the kernel to enable or disable
612 * Returns 0 on success, -EINVAL if the parameters do not match any
615 int trace_set_clr_event(const char *system, const char *event, int set)
617 struct trace_array *tr = top_trace_array();
622 return __ftrace_set_clr_event(tr, NULL, system, event, set);
624 EXPORT_SYMBOL_GPL(trace_set_clr_event);
626 /* 128 should be much more than enough */
627 #define EVENT_BUF_SIZE 127
630 ftrace_event_write(struct file *file, const char __user *ubuf,
631 size_t cnt, loff_t *ppos)
633 struct trace_parser parser;
634 struct seq_file *m = file->private_data;
635 struct trace_array *tr = m->private;
641 ret = tracing_update_buffers();
645 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
648 read = trace_get_user(&parser, ubuf, cnt, ppos);
650 if (read >= 0 && trace_parser_loaded((&parser))) {
653 if (*parser.buffer == '!')
656 parser.buffer[parser.idx] = 0;
658 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
666 trace_parser_put(&parser);
672 t_next(struct seq_file *m, void *v, loff_t *pos)
674 struct ftrace_event_file *file = v;
675 struct ftrace_event_call *call;
676 struct trace_array *tr = m->private;
680 list_for_each_entry_continue(file, &tr->events, list) {
681 call = file->event_call;
683 * The ftrace subsystem is for showing formats only.
684 * They can not be enabled or disabled via the event files.
686 if (call->class && call->class->reg)
693 static void *t_start(struct seq_file *m, loff_t *pos)
695 struct ftrace_event_file *file;
696 struct trace_array *tr = m->private;
699 mutex_lock(&event_mutex);
701 file = list_entry(&tr->events, struct ftrace_event_file, list);
702 for (l = 0; l <= *pos; ) {
703 file = t_next(m, file, &l);
711 s_next(struct seq_file *m, void *v, loff_t *pos)
713 struct ftrace_event_file *file = v;
714 struct trace_array *tr = m->private;
718 list_for_each_entry_continue(file, &tr->events, list) {
719 if (file->flags & FTRACE_EVENT_FL_ENABLED)
726 static void *s_start(struct seq_file *m, loff_t *pos)
728 struct ftrace_event_file *file;
729 struct trace_array *tr = m->private;
732 mutex_lock(&event_mutex);
734 file = list_entry(&tr->events, struct ftrace_event_file, list);
735 for (l = 0; l <= *pos; ) {
736 file = s_next(m, file, &l);
743 static int t_show(struct seq_file *m, void *v)
745 struct ftrace_event_file *file = v;
746 struct ftrace_event_call *call = file->event_call;
748 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
749 seq_printf(m, "%s:", call->class->system);
750 seq_printf(m, "%s\n", ftrace_event_name(call));
755 static void t_stop(struct seq_file *m, void *p)
757 mutex_unlock(&event_mutex);
761 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
764 struct ftrace_event_file *file;
768 mutex_lock(&event_mutex);
769 file = event_file_data(filp);
772 mutex_unlock(&event_mutex);
777 if (flags & FTRACE_EVENT_FL_ENABLED &&
778 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
781 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
782 flags & FTRACE_EVENT_FL_SOFT_MODE)
787 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
791 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
794 struct ftrace_event_file *file;
798 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
802 ret = tracing_update_buffers();
810 mutex_lock(&event_mutex);
811 file = event_file_data(filp);
813 ret = ftrace_event_enable_disable(file, val);
814 mutex_unlock(&event_mutex);
823 return ret ? ret : cnt;
827 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
830 const char set_to_char[4] = { '?', '0', '1', 'X' };
831 struct ftrace_subsystem_dir *dir = filp->private_data;
832 struct event_subsystem *system = dir->subsystem;
833 struct ftrace_event_call *call;
834 struct ftrace_event_file *file;
835 struct trace_array *tr = dir->tr;
840 mutex_lock(&event_mutex);
841 list_for_each_entry(file, &tr->events, list) {
842 call = file->event_call;
843 if (!ftrace_event_name(call) || !call->class || !call->class->reg)
846 if (system && strcmp(call->class->system, system->name) != 0)
850 * We need to find out if all the events are set
851 * or if all events or cleared, or if we have
854 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
857 * If we have a mixture, no need to look further.
862 mutex_unlock(&event_mutex);
864 buf[0] = set_to_char[set];
867 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
873 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
876 struct ftrace_subsystem_dir *dir = filp->private_data;
877 struct event_subsystem *system = dir->subsystem;
878 const char *name = NULL;
882 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
886 ret = tracing_update_buffers();
890 if (val != 0 && val != 1)
894 * Opening of "enable" adds a ref count to system,
895 * so the name is safe to use.
900 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
914 FORMAT_FIELD_SEPERATOR = 2,
918 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
920 struct ftrace_event_call *call = event_file_data(m->private);
921 struct list_head *common_head = &ftrace_common_fields;
922 struct list_head *head = trace_get_fields(call);
923 struct list_head *node = v;
927 switch ((unsigned long)v) {
932 case FORMAT_FIELD_SEPERATOR:
936 case FORMAT_PRINTFMT:
942 if (node == common_head)
943 return (void *)FORMAT_FIELD_SEPERATOR;
944 else if (node == head)
945 return (void *)FORMAT_PRINTFMT;
950 static int f_show(struct seq_file *m, void *v)
952 struct ftrace_event_call *call = event_file_data(m->private);
953 struct ftrace_event_field *field;
954 const char *array_descriptor;
956 switch ((unsigned long)v) {
958 seq_printf(m, "name: %s\n", ftrace_event_name(call));
959 seq_printf(m, "ID: %d\n", call->event.type);
960 seq_puts(m, "format:\n");
963 case FORMAT_FIELD_SEPERATOR:
967 case FORMAT_PRINTFMT:
968 seq_printf(m, "\nprint fmt: %s\n",
973 field = list_entry(v, struct ftrace_event_field, link);
975 * Smartly shows the array type(except dynamic array).
978 * If TYPE := TYPE[LEN], it is shown:
979 * field:TYPE VAR[LEN]
981 array_descriptor = strchr(field->type, '[');
983 if (!strncmp(field->type, "__data_loc", 10))
984 array_descriptor = NULL;
986 if (!array_descriptor)
987 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
988 field->type, field->name, field->offset,
989 field->size, !!field->is_signed);
991 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
992 (int)(array_descriptor - field->type),
993 field->type, field->name,
994 array_descriptor, field->offset,
995 field->size, !!field->is_signed);
1000 static void *f_start(struct seq_file *m, loff_t *pos)
1002 void *p = (void *)FORMAT_HEADER;
1005 /* ->stop() is called even if ->start() fails */
1006 mutex_lock(&event_mutex);
1007 if (!event_file_data(m->private))
1008 return ERR_PTR(-ENODEV);
1010 while (l < *pos && p)
1011 p = f_next(m, p, &l);
1016 static void f_stop(struct seq_file *m, void *p)
1018 mutex_unlock(&event_mutex);
1021 static const struct seq_operations trace_format_seq_ops = {
1028 static int trace_format_open(struct inode *inode, struct file *file)
1033 ret = seq_open(file, &trace_format_seq_ops);
1037 m = file->private_data;
1044 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1046 int id = (long)event_file_data(filp);
1056 len = sprintf(buf, "%d\n", id);
1058 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1062 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1065 struct ftrace_event_file *file;
1066 struct trace_seq *s;
1072 s = kmalloc(sizeof(*s), GFP_KERNEL);
1079 mutex_lock(&event_mutex);
1080 file = event_file_data(filp);
1082 print_event_filter(file, s);
1083 mutex_unlock(&event_mutex);
1086 r = simple_read_from_buffer(ubuf, cnt, ppos,
1087 s->buffer, trace_seq_used(s));
1095 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1098 struct ftrace_event_file *file;
1102 if (cnt >= PAGE_SIZE)
1105 buf = (char *)__get_free_page(GFP_TEMPORARY);
1109 if (copy_from_user(buf, ubuf, cnt)) {
1110 free_page((unsigned long) buf);
1115 mutex_lock(&event_mutex);
1116 file = event_file_data(filp);
1118 err = apply_event_filter(file, buf);
1119 mutex_unlock(&event_mutex);
1121 free_page((unsigned long) buf);
1130 static LIST_HEAD(event_subsystems);
1132 static int subsystem_open(struct inode *inode, struct file *filp)
1134 struct event_subsystem *system = NULL;
1135 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1136 struct trace_array *tr;
1139 if (tracing_is_disabled())
1142 /* Make sure the system still exists */
1143 mutex_lock(&trace_types_lock);
1144 mutex_lock(&event_mutex);
1145 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1146 list_for_each_entry(dir, &tr->systems, list) {
1147 if (dir == inode->i_private) {
1148 /* Don't open systems with no events */
1149 if (dir->nr_events) {
1150 __get_system_dir(dir);
1151 system = dir->subsystem;
1158 mutex_unlock(&event_mutex);
1159 mutex_unlock(&trace_types_lock);
1164 /* Some versions of gcc think dir can be uninitialized here */
1167 /* Still need to increment the ref count of the system */
1168 if (trace_array_get(tr) < 0) {
1173 ret = tracing_open_generic(inode, filp);
1175 trace_array_put(tr);
1182 static int system_tr_open(struct inode *inode, struct file *filp)
1184 struct ftrace_subsystem_dir *dir;
1185 struct trace_array *tr = inode->i_private;
1188 if (tracing_is_disabled())
1191 if (trace_array_get(tr) < 0)
1194 /* Make a temporary dir that has no system but points to tr */
1195 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1197 trace_array_put(tr);
1203 ret = tracing_open_generic(inode, filp);
1205 trace_array_put(tr);
1210 filp->private_data = dir;
1215 static int subsystem_release(struct inode *inode, struct file *file)
1217 struct ftrace_subsystem_dir *dir = file->private_data;
1219 trace_array_put(dir->tr);
1222 * If dir->subsystem is NULL, then this is a temporary
1223 * descriptor that was made for a trace_array to enable
1235 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1238 struct ftrace_subsystem_dir *dir = filp->private_data;
1239 struct event_subsystem *system = dir->subsystem;
1240 struct trace_seq *s;
1246 s = kmalloc(sizeof(*s), GFP_KERNEL);
1252 print_subsystem_event_filter(system, s);
1253 r = simple_read_from_buffer(ubuf, cnt, ppos,
1254 s->buffer, trace_seq_used(s));
1262 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1265 struct ftrace_subsystem_dir *dir = filp->private_data;
1269 if (cnt >= PAGE_SIZE)
1272 buf = (char *)__get_free_page(GFP_TEMPORARY);
1276 if (copy_from_user(buf, ubuf, cnt)) {
1277 free_page((unsigned long) buf);
1282 err = apply_subsystem_event_filter(dir, buf);
1283 free_page((unsigned long) buf);
1293 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1295 int (*func)(struct trace_seq *s) = filp->private_data;
1296 struct trace_seq *s;
1302 s = kmalloc(sizeof(*s), GFP_KERNEL);
1309 r = simple_read_from_buffer(ubuf, cnt, ppos,
1310 s->buffer, trace_seq_used(s));
1317 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1318 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1319 static int ftrace_event_release(struct inode *inode, struct file *file);
1321 static const struct seq_operations show_event_seq_ops = {
1328 static const struct seq_operations show_set_event_seq_ops = {
1335 static const struct file_operations ftrace_avail_fops = {
1336 .open = ftrace_event_avail_open,
1338 .llseek = seq_lseek,
1339 .release = seq_release,
1342 static const struct file_operations ftrace_set_event_fops = {
1343 .open = ftrace_event_set_open,
1345 .write = ftrace_event_write,
1346 .llseek = seq_lseek,
1347 .release = ftrace_event_release,
1350 static const struct file_operations ftrace_enable_fops = {
1351 .open = tracing_open_generic,
1352 .read = event_enable_read,
1353 .write = event_enable_write,
1354 .llseek = default_llseek,
1357 static const struct file_operations ftrace_event_format_fops = {
1358 .open = trace_format_open,
1360 .llseek = seq_lseek,
1361 .release = seq_release,
1364 static const struct file_operations ftrace_event_id_fops = {
1365 .read = event_id_read,
1366 .llseek = default_llseek,
1369 static const struct file_operations ftrace_event_filter_fops = {
1370 .open = tracing_open_generic,
1371 .read = event_filter_read,
1372 .write = event_filter_write,
1373 .llseek = default_llseek,
1376 static const struct file_operations ftrace_subsystem_filter_fops = {
1377 .open = subsystem_open,
1378 .read = subsystem_filter_read,
1379 .write = subsystem_filter_write,
1380 .llseek = default_llseek,
1381 .release = subsystem_release,
1384 static const struct file_operations ftrace_system_enable_fops = {
1385 .open = subsystem_open,
1386 .read = system_enable_read,
1387 .write = system_enable_write,
1388 .llseek = default_llseek,
1389 .release = subsystem_release,
1392 static const struct file_operations ftrace_tr_enable_fops = {
1393 .open = system_tr_open,
1394 .read = system_enable_read,
1395 .write = system_enable_write,
1396 .llseek = default_llseek,
1397 .release = subsystem_release,
1400 static const struct file_operations ftrace_show_header_fops = {
1401 .open = tracing_open_generic,
1402 .read = show_header,
1403 .llseek = default_llseek,
1407 ftrace_event_open(struct inode *inode, struct file *file,
1408 const struct seq_operations *seq_ops)
1413 ret = seq_open(file, seq_ops);
1416 m = file->private_data;
1417 /* copy tr over to seq ops */
1418 m->private = inode->i_private;
1423 static int ftrace_event_release(struct inode *inode, struct file *file)
1425 struct trace_array *tr = inode->i_private;
1427 trace_array_put(tr);
1429 return seq_release(inode, file);
1433 ftrace_event_avail_open(struct inode *inode, struct file *file)
1435 const struct seq_operations *seq_ops = &show_event_seq_ops;
1437 return ftrace_event_open(inode, file, seq_ops);
1441 ftrace_event_set_open(struct inode *inode, struct file *file)
1443 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1444 struct trace_array *tr = inode->i_private;
1447 if (trace_array_get(tr) < 0)
1450 if ((file->f_mode & FMODE_WRITE) &&
1451 (file->f_flags & O_TRUNC))
1452 ftrace_clear_events(tr);
1454 ret = ftrace_event_open(inode, file, seq_ops);
1456 trace_array_put(tr);
1460 static struct event_subsystem *
1461 create_new_subsystem(const char *name)
1463 struct event_subsystem *system;
1465 /* need to create new entry */
1466 system = kmalloc(sizeof(*system), GFP_KERNEL);
1470 system->ref_count = 1;
1472 /* Only allocate if dynamic (kprobes and modules) */
1473 if (!core_kernel_data((unsigned long)name)) {
1474 system->ref_count |= SYSTEM_FL_FREE_NAME;
1475 system->name = kstrdup(name, GFP_KERNEL);
1479 system->name = name;
1481 system->filter = NULL;
1483 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1484 if (!system->filter)
1487 list_add(&system->list, &event_subsystems);
1492 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1493 kfree(system->name);
1498 static struct dentry *
1499 event_subsystem_dir(struct trace_array *tr, const char *name,
1500 struct ftrace_event_file *file, struct dentry *parent)
1502 struct ftrace_subsystem_dir *dir;
1503 struct event_subsystem *system;
1504 struct dentry *entry;
1506 /* First see if we did not already create this dir */
1507 list_for_each_entry(dir, &tr->systems, list) {
1508 system = dir->subsystem;
1509 if (strcmp(system->name, name) == 0) {
1516 /* Now see if the system itself exists. */
1517 list_for_each_entry(system, &event_subsystems, list) {
1518 if (strcmp(system->name, name) == 0)
1521 /* Reset system variable when not found */
1522 if (&system->list == &event_subsystems)
1525 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1530 system = create_new_subsystem(name);
1534 __get_system(system);
1536 dir->entry = tracefs_create_dir(name, parent);
1538 pr_warn("Failed to create system directory %s\n", name);
1539 __put_system(system);
1546 dir->subsystem = system;
1549 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1550 &ftrace_subsystem_filter_fops);
1552 kfree(system->filter);
1553 system->filter = NULL;
1554 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1557 trace_create_file("enable", 0644, dir->entry, dir,
1558 &ftrace_system_enable_fops);
1560 list_add(&dir->list, &tr->systems);
1567 /* Only print this message if failed on memory allocation */
1568 if (!dir || !system)
1569 pr_warn("No memory to create event subsystem %s\n", name);
1574 event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1576 struct ftrace_event_call *call = file->event_call;
1577 struct trace_array *tr = file->tr;
1578 struct list_head *head;
1579 struct dentry *d_events;
1584 * If the trace point header did not define TRACE_SYSTEM
1585 * then the system would be called "TRACE_SYSTEM".
1587 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1588 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1594 name = ftrace_event_name(call);
1595 file->dir = tracefs_create_dir(name, d_events);
1597 pr_warn("Could not create tracefs '%s' directory\n", name);
1601 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1602 trace_create_file("enable", 0644, file->dir, file,
1603 &ftrace_enable_fops);
1605 #ifdef CONFIG_PERF_EVENTS
1606 if (call->event.type && call->class->reg)
1607 trace_create_file("id", 0444, file->dir,
1608 (void *)(long)call->event.type,
1609 &ftrace_event_id_fops);
1613 * Other events may have the same class. Only update
1614 * the fields if they are not already defined.
1616 head = trace_get_fields(call);
1617 if (list_empty(head)) {
1618 ret = call->class->define_fields(call);
1620 pr_warn("Could not initialize trace point events/%s\n",
1625 trace_create_file("filter", 0644, file->dir, file,
1626 &ftrace_event_filter_fops);
1628 trace_create_file("trigger", 0644, file->dir, file,
1629 &event_trigger_fops);
1631 trace_create_file("format", 0444, file->dir, call,
1632 &ftrace_event_format_fops);
1637 static void remove_event_from_tracers(struct ftrace_event_call *call)
1639 struct ftrace_event_file *file;
1640 struct trace_array *tr;
1642 do_for_each_event_file_safe(tr, file) {
1643 if (file->event_call != call)
1646 remove_event_file_dir(file);
1648 * The do_for_each_event_file_safe() is
1649 * a double loop. After finding the call for this
1650 * trace_array, we use break to jump to the next
1654 } while_for_each_event_file();
1657 static void event_remove(struct ftrace_event_call *call)
1659 struct trace_array *tr;
1660 struct ftrace_event_file *file;
1662 do_for_each_event_file(tr, file) {
1663 if (file->event_call != call)
1665 ftrace_event_enable_disable(file, 0);
1667 * The do_for_each_event_file() is
1668 * a double loop. After finding the call for this
1669 * trace_array, we use break to jump to the next
1673 } while_for_each_event_file();
1675 if (call->event.funcs)
1676 __unregister_ftrace_event(&call->event);
1677 remove_event_from_tracers(call);
1678 list_del(&call->list);
1681 static int event_init(struct ftrace_event_call *call)
1686 name = ftrace_event_name(call);
1690 if (call->class->raw_init) {
1691 ret = call->class->raw_init(call);
1692 if (ret < 0 && ret != -ENOSYS)
1693 pr_warn("Could not initialize trace events/%s\n", name);
1700 __register_event(struct ftrace_event_call *call, struct module *mod)
1704 ret = event_init(call);
1708 list_add(&call->list, &ftrace_events);
1714 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1719 /* Find the length of the enum value as a string */
1720 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1721 /* Make sure there's enough room to replace the string with the value */
1725 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1727 /* Get the rest of the string of ptr */
1728 rlen = strlen(ptr + len);
1729 memmove(ptr + elen, ptr + len, rlen);
1730 /* Make sure we end the new string */
1731 ptr[elen + rlen] = 0;
1736 static void update_event_printk(struct ftrace_event_call *call,
1737 struct trace_enum_map *map)
1741 int len = strlen(map->enum_string);
1743 for (ptr = call->print_fmt; *ptr; ptr++) {
1757 if (isdigit(*ptr)) {
1761 /* Check for alpha chars like ULL */
1762 } while (isalnum(*ptr));
1766 * A number must have some kind of delimiter after
1767 * it, and we can ignore that too.
1771 if (isalpha(*ptr) || *ptr == '_') {
1772 if (strncmp(map->enum_string, ptr, len) == 0 &&
1773 !isalnum(ptr[len]) && ptr[len] != '_') {
1774 ptr = enum_replace(ptr, map, len);
1775 /* Hmm, enum string smaller than value */
1776 if (WARN_ON_ONCE(!ptr))
1779 * No need to decrement here, as enum_replace()
1780 * returns the pointer to the character passed
1781 * the enum, and two enums can not be placed
1782 * back to back without something in between.
1783 * We can skip that something in between.
1790 } while (isalnum(*ptr) || *ptr == '_');
1794 * If what comes after this variable is a '.' or
1795 * '->' then we can continue to ignore that string.
1797 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1798 ptr += *ptr == '.' ? 1 : 2;
1804 * Once again, we can skip the delimiter that came
1812 void trace_event_enum_update(struct trace_enum_map **map, int len)
1814 struct ftrace_event_call *call, *p;
1815 const char *last_system = NULL;
1819 down_write(&trace_event_sem);
1820 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1821 /* events are usually grouped together with systems */
1822 if (!last_system || call->class->system != last_system) {
1824 last_system = call->class->system;
1827 for (i = last_i; i < len; i++) {
1828 if (call->class->system == map[i]->system) {
1829 /* Save the first system if need be */
1832 update_event_printk(call, map[i]);
1836 up_write(&trace_event_sem);
1839 static struct ftrace_event_file *
1840 trace_create_new_event(struct ftrace_event_call *call,
1841 struct trace_array *tr)
1843 struct ftrace_event_file *file;
1845 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1849 file->event_call = call;
1851 atomic_set(&file->sm_ref, 0);
1852 atomic_set(&file->tm_ref, 0);
1853 INIT_LIST_HEAD(&file->triggers);
1854 list_add(&file->list, &tr->events);
1859 /* Add an event to a trace directory */
1861 __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1863 struct ftrace_event_file *file;
1865 file = trace_create_new_event(call, tr);
1869 return event_create_dir(tr->event_dir, file);
1873 * Just create a decriptor for early init. A descriptor is required
1874 * for enabling events at boot. We want to enable events before
1875 * the filesystem is initialized.
1878 __trace_early_add_new_event(struct ftrace_event_call *call,
1879 struct trace_array *tr)
1881 struct ftrace_event_file *file;
1883 file = trace_create_new_event(call, tr);
1890 struct ftrace_module_file_ops;
1891 static void __add_event_to_tracers(struct ftrace_event_call *call);
1893 /* Add an additional event_call dynamically */
1894 int trace_add_event_call(struct ftrace_event_call *call)
1897 mutex_lock(&trace_types_lock);
1898 mutex_lock(&event_mutex);
1900 ret = __register_event(call, NULL);
1902 __add_event_to_tracers(call);
1904 mutex_unlock(&event_mutex);
1905 mutex_unlock(&trace_types_lock);
1910 * Must be called under locking of trace_types_lock, event_mutex and
1913 static void __trace_remove_event_call(struct ftrace_event_call *call)
1916 trace_destroy_fields(call);
1917 free_event_filter(call->filter);
1918 call->filter = NULL;
1921 static int probe_remove_event_call(struct ftrace_event_call *call)
1923 struct trace_array *tr;
1924 struct ftrace_event_file *file;
1926 #ifdef CONFIG_PERF_EVENTS
1927 if (call->perf_refcount)
1930 do_for_each_event_file(tr, file) {
1931 if (file->event_call != call)
1934 * We can't rely on ftrace_event_enable_disable(enable => 0)
1935 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1936 * TRACE_REG_UNREGISTER.
1938 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1941 * The do_for_each_event_file_safe() is
1942 * a double loop. After finding the call for this
1943 * trace_array, we use break to jump to the next
1947 } while_for_each_event_file();
1949 __trace_remove_event_call(call);
1954 /* Remove an event_call */
1955 int trace_remove_event_call(struct ftrace_event_call *call)
1959 mutex_lock(&trace_types_lock);
1960 mutex_lock(&event_mutex);
1961 down_write(&trace_event_sem);
1962 ret = probe_remove_event_call(call);
1963 up_write(&trace_event_sem);
1964 mutex_unlock(&event_mutex);
1965 mutex_unlock(&trace_types_lock);
1970 #define for_each_event(event, start, end) \
1971 for (event = start; \
1972 (unsigned long)event < (unsigned long)end; \
1975 #ifdef CONFIG_MODULES
1977 static void trace_module_add_events(struct module *mod)
1979 struct ftrace_event_call **call, **start, **end;
1981 if (!mod->num_trace_events)
1984 /* Don't add infrastructure for mods without tracepoints */
1985 if (trace_module_has_bad_taint(mod)) {
1986 pr_err("%s: module has bad taint, not creating trace events\n",
1991 start = mod->trace_events;
1992 end = mod->trace_events + mod->num_trace_events;
1994 for_each_event(call, start, end) {
1995 __register_event(*call, mod);
1996 __add_event_to_tracers(*call);
2000 static void trace_module_remove_events(struct module *mod)
2002 struct ftrace_event_call *call, *p;
2003 bool clear_trace = false;
2005 down_write(&trace_event_sem);
2006 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2007 if (call->mod == mod) {
2008 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2010 __trace_remove_event_call(call);
2013 up_write(&trace_event_sem);
2016 * It is safest to reset the ring buffer if the module being unloaded
2017 * registered any events that were used. The only worry is if
2018 * a new module gets loaded, and takes on the same id as the events
2019 * of this module. When printing out the buffer, traced events left
2020 * over from this module may be passed to the new module events and
2021 * unexpected results may occur.
2024 tracing_reset_all_online_cpus();
2027 static int trace_module_notify(struct notifier_block *self,
2028 unsigned long val, void *data)
2030 struct module *mod = data;
2032 mutex_lock(&trace_types_lock);
2033 mutex_lock(&event_mutex);
2035 case MODULE_STATE_COMING:
2036 trace_module_add_events(mod);
2038 case MODULE_STATE_GOING:
2039 trace_module_remove_events(mod);
2042 mutex_unlock(&event_mutex);
2043 mutex_unlock(&trace_types_lock);
2048 static struct notifier_block trace_module_nb = {
2049 .notifier_call = trace_module_notify,
2050 .priority = 1, /* higher than trace.c module notify */
2052 #endif /* CONFIG_MODULES */
2054 /* Create a new event directory structure for a trace directory. */
2056 __trace_add_event_dirs(struct trace_array *tr)
2058 struct ftrace_event_call *call;
2061 list_for_each_entry(call, &ftrace_events, list) {
2062 ret = __trace_add_new_event(call, tr);
2064 pr_warn("Could not create directory for event %s\n",
2065 ftrace_event_name(call));
2069 struct ftrace_event_file *
2070 find_event_file(struct trace_array *tr, const char *system, const char *event)
2072 struct ftrace_event_file *file;
2073 struct ftrace_event_call *call;
2076 list_for_each_entry(file, &tr->events, list) {
2078 call = file->event_call;
2079 name = ftrace_event_name(call);
2081 if (!name || !call->class || !call->class->reg)
2084 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2087 if (strcmp(event, name) == 0 &&
2088 strcmp(system, call->class->system) == 0)
2094 #ifdef CONFIG_DYNAMIC_FTRACE
2097 #define ENABLE_EVENT_STR "enable_event"
2098 #define DISABLE_EVENT_STR "disable_event"
2100 struct event_probe_data {
2101 struct ftrace_event_file *file;
2102 unsigned long count;
2108 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2110 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2111 struct event_probe_data *data = *pdata;
2117 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2119 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2123 event_enable_count_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;
2134 /* Skip if the event is in a state we want to switch to */
2135 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2138 if (data->count != -1)
2141 event_enable_probe(ip, parent_ip, _data);
2145 event_enable_print(struct seq_file *m, unsigned long ip,
2146 struct ftrace_probe_ops *ops, void *_data)
2148 struct event_probe_data *data = _data;
2150 seq_printf(m, "%ps:", (void *)ip);
2152 seq_printf(m, "%s:%s:%s",
2153 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2154 data->file->event_call->class->system,
2155 ftrace_event_name(data->file->event_call));
2157 if (data->count == -1)
2158 seq_puts(m, ":unlimited\n");
2160 seq_printf(m, ":count=%ld\n", data->count);
2166 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2169 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2170 struct event_probe_data *data = *pdata;
2177 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2180 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2181 struct event_probe_data *data = *pdata;
2183 if (WARN_ON_ONCE(data->ref <= 0))
2188 /* Remove the SOFT_MODE flag */
2189 __ftrace_event_enable_disable(data->file, 0, 1);
2190 module_put(data->file->event_call->mod);
2196 static struct ftrace_probe_ops event_enable_probe_ops = {
2197 .func = event_enable_probe,
2198 .print = event_enable_print,
2199 .init = event_enable_init,
2200 .free = event_enable_free,
2203 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2204 .func = event_enable_count_probe,
2205 .print = event_enable_print,
2206 .init = event_enable_init,
2207 .free = event_enable_free,
2210 static struct ftrace_probe_ops event_disable_probe_ops = {
2211 .func = event_enable_probe,
2212 .print = event_enable_print,
2213 .init = event_enable_init,
2214 .free = event_enable_free,
2217 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2218 .func = event_enable_count_probe,
2219 .print = event_enable_print,
2220 .init = event_enable_init,
2221 .free = event_enable_free,
2225 event_enable_func(struct ftrace_hash *hash,
2226 char *glob, char *cmd, char *param, int enabled)
2228 struct trace_array *tr = top_trace_array();
2229 struct ftrace_event_file *file;
2230 struct ftrace_probe_ops *ops;
2231 struct event_probe_data *data;
2241 /* hash funcs only work with set_ftrace_filter */
2242 if (!enabled || !param)
2245 system = strsep(¶m, ":");
2249 event = strsep(¶m, ":");
2251 mutex_lock(&event_mutex);
2254 file = find_event_file(tr, system, event);
2258 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2261 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2263 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2265 if (glob[0] == '!') {
2266 unregister_ftrace_function_probe_func(glob+1, ops);
2272 data = kzalloc(sizeof(*data), GFP_KERNEL);
2276 data->enable = enable;
2283 number = strsep(¶m, ":");
2286 if (!strlen(number))
2290 * We use the callback data field (which is a pointer)
2293 ret = kstrtoul(number, 0, &data->count);
2298 /* Don't let event modules unload while probe registered */
2299 ret = try_module_get(file->event_call->mod);
2305 ret = __ftrace_event_enable_disable(file, 1, 1);
2308 ret = register_ftrace_function_probe(glob, ops, data);
2310 * The above returns on success the # of functions enabled,
2311 * but if it didn't find any functions it returns zero.
2312 * Consider no functions a failure too.
2319 /* Just return zero, not the number of enabled functions */
2322 mutex_unlock(&event_mutex);
2326 __ftrace_event_enable_disable(file, 0, 1);
2328 module_put(file->event_call->mod);
2334 static struct ftrace_func_command event_enable_cmd = {
2335 .name = ENABLE_EVENT_STR,
2336 .func = event_enable_func,
2339 static struct ftrace_func_command event_disable_cmd = {
2340 .name = DISABLE_EVENT_STR,
2341 .func = event_enable_func,
2344 static __init int register_event_cmds(void)
2348 ret = register_ftrace_command(&event_enable_cmd);
2349 if (WARN_ON(ret < 0))
2351 ret = register_ftrace_command(&event_disable_cmd);
2352 if (WARN_ON(ret < 0))
2353 unregister_ftrace_command(&event_enable_cmd);
2357 static inline int register_event_cmds(void) { return 0; }
2358 #endif /* CONFIG_DYNAMIC_FTRACE */
2361 * The top level array has already had its ftrace_event_file
2362 * descriptors created in order to allow for early events to
2363 * be recorded. This function is called after the tracefs has been
2364 * initialized, and we now have to create the files associated
2368 __trace_early_add_event_dirs(struct trace_array *tr)
2370 struct ftrace_event_file *file;
2374 list_for_each_entry(file, &tr->events, list) {
2375 ret = event_create_dir(tr->event_dir, file);
2377 pr_warn("Could not create directory for event %s\n",
2378 ftrace_event_name(file->event_call));
2383 * For early boot up, the top trace array requires to have
2384 * a list of events that can be enabled. This must be done before
2385 * the filesystem is set up in order to allow events to be traced
2389 __trace_early_add_events(struct trace_array *tr)
2391 struct ftrace_event_call *call;
2394 list_for_each_entry(call, &ftrace_events, list) {
2395 /* Early boot up should not have any modules loaded */
2396 if (WARN_ON_ONCE(call->mod))
2399 ret = __trace_early_add_new_event(call, tr);
2401 pr_warn("Could not create early event %s\n",
2402 ftrace_event_name(call));
2406 /* Remove the event directory structure for a trace directory. */
2408 __trace_remove_event_dirs(struct trace_array *tr)
2410 struct ftrace_event_file *file, *next;
2412 list_for_each_entry_safe(file, next, &tr->events, list)
2413 remove_event_file_dir(file);
2416 static void __add_event_to_tracers(struct ftrace_event_call *call)
2418 struct trace_array *tr;
2420 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2421 __trace_add_new_event(call, tr);
2424 extern struct ftrace_event_call *__start_ftrace_events[];
2425 extern struct ftrace_event_call *__stop_ftrace_events[];
2427 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2429 static __init int setup_trace_event(char *str)
2431 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2432 ring_buffer_expanded = true;
2433 tracing_selftest_disabled = true;
2437 __setup("trace_event=", setup_trace_event);
2439 /* Expects to have event_mutex held when called */
2441 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2443 struct dentry *d_events;
2444 struct dentry *entry;
2446 entry = tracefs_create_file("set_event", 0644, parent,
2447 tr, &ftrace_set_event_fops);
2449 pr_warn("Could not create tracefs 'set_event' entry\n");
2453 d_events = tracefs_create_dir("events", parent);
2455 pr_warn("Could not create tracefs 'events' directory\n");
2459 /* ring buffer internal formats */
2460 trace_create_file("header_page", 0444, d_events,
2461 ring_buffer_print_page_header,
2462 &ftrace_show_header_fops);
2464 trace_create_file("header_event", 0444, d_events,
2465 ring_buffer_print_entry_header,
2466 &ftrace_show_header_fops);
2468 trace_create_file("enable", 0644, d_events,
2469 tr, &ftrace_tr_enable_fops);
2471 tr->event_dir = d_events;
2477 * event_trace_add_tracer - add a instance of a trace_array to events
2478 * @parent: The parent dentry to place the files/directories for events in
2479 * @tr: The trace array associated with these events
2481 * When a new instance is created, it needs to set up its events
2482 * directory, as well as other files associated with events. It also
2483 * creates the event hierachry in the @parent/events directory.
2485 * Returns 0 on success.
2487 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2491 mutex_lock(&event_mutex);
2493 ret = create_event_toplevel_files(parent, tr);
2497 down_write(&trace_event_sem);
2498 __trace_add_event_dirs(tr);
2499 up_write(&trace_event_sem);
2502 mutex_unlock(&event_mutex);
2508 * The top trace array already had its file descriptors created.
2509 * Now the files themselves need to be created.
2512 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2516 mutex_lock(&event_mutex);
2518 ret = create_event_toplevel_files(parent, tr);
2522 down_write(&trace_event_sem);
2523 __trace_early_add_event_dirs(tr);
2524 up_write(&trace_event_sem);
2527 mutex_unlock(&event_mutex);
2532 int event_trace_del_tracer(struct trace_array *tr)
2534 mutex_lock(&event_mutex);
2536 /* Disable any event triggers and associated soft-disabled events */
2537 clear_event_triggers(tr);
2539 /* Disable any running events */
2540 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2542 /* Access to events are within rcu_read_lock_sched() */
2543 synchronize_sched();
2545 down_write(&trace_event_sem);
2546 __trace_remove_event_dirs(tr);
2547 tracefs_remove_recursive(tr->event_dir);
2548 up_write(&trace_event_sem);
2550 tr->event_dir = NULL;
2552 mutex_unlock(&event_mutex);
2557 static __init int event_trace_memsetup(void)
2559 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2560 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2565 early_enable_events(struct trace_array *tr, bool disable_first)
2567 char *buf = bootup_event_buf;
2572 token = strsep(&buf, ",");
2579 /* Restarting syscalls requires that we stop them first */
2581 ftrace_set_clr_event(tr, token, 0);
2583 ret = ftrace_set_clr_event(tr, token, 1);
2585 pr_warn("Failed to enable trace event: %s\n", token);
2587 /* Put back the comma to allow this to be called again */
2593 static __init int event_trace_enable(void)
2595 struct trace_array *tr = top_trace_array();
2596 struct ftrace_event_call **iter, *call;
2602 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2605 ret = event_init(call);
2607 list_add(&call->list, &ftrace_events);
2611 * We need the top trace array to have a working set of trace
2612 * points at early init, before the debug files and directories
2613 * are created. Create the file entries now, and attach them
2614 * to the actual file dentries later.
2616 __trace_early_add_events(tr);
2618 early_enable_events(tr, false);
2620 trace_printk_start_comm();
2622 register_event_cmds();
2624 register_trigger_cmds();
2630 * event_trace_enable() is called from trace_event_init() first to
2631 * initialize events and perhaps start any events that are on the
2632 * command line. Unfortunately, there are some events that will not
2633 * start this early, like the system call tracepoints that need
2634 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2635 * is called before pid 1 starts, and this flag is never set, making
2636 * the syscall tracepoint never get reached, but the event is enabled
2637 * regardless (and not doing anything).
2639 static __init int event_trace_enable_again(void)
2641 struct trace_array *tr;
2643 tr = top_trace_array();
2647 early_enable_events(tr, true);
2652 early_initcall(event_trace_enable_again);
2654 static __init int event_trace_init(void)
2656 struct trace_array *tr;
2657 struct dentry *d_tracer;
2658 struct dentry *entry;
2661 tr = top_trace_array();
2665 d_tracer = tracing_init_dentry();
2666 if (IS_ERR(d_tracer))
2669 entry = tracefs_create_file("available_events", 0444, d_tracer,
2670 tr, &ftrace_avail_fops);
2672 pr_warn("Could not create tracefs 'available_events' entry\n");
2674 if (trace_define_common_fields())
2675 pr_warn("tracing: Failed to allocate common fields");
2677 ret = early_event_add_tracer(d_tracer, tr);
2681 #ifdef CONFIG_MODULES
2682 ret = register_module_notifier(&trace_module_nb);
2684 pr_warn("Failed to register trace events module notifier\n");
2689 void __init trace_event_init(void)
2691 event_trace_memsetup();
2692 init_ftrace_syscalls();
2693 event_trace_enable();
2696 fs_initcall(event_trace_init);
2698 #ifdef CONFIG_FTRACE_STARTUP_TEST
2700 static DEFINE_SPINLOCK(test_spinlock);
2701 static DEFINE_SPINLOCK(test_spinlock_irq);
2702 static DEFINE_MUTEX(test_mutex);
2704 static __init void test_work(struct work_struct *dummy)
2706 spin_lock(&test_spinlock);
2707 spin_lock_irq(&test_spinlock_irq);
2709 spin_unlock_irq(&test_spinlock_irq);
2710 spin_unlock(&test_spinlock);
2712 mutex_lock(&test_mutex);
2714 mutex_unlock(&test_mutex);
2717 static __init int event_test_thread(void *unused)
2721 test_malloc = kmalloc(1234, GFP_KERNEL);
2723 pr_info("failed to kmalloc\n");
2725 schedule_on_each_cpu(test_work);
2729 set_current_state(TASK_INTERRUPTIBLE);
2730 while (!kthread_should_stop()) {
2732 set_current_state(TASK_INTERRUPTIBLE);
2734 __set_current_state(TASK_RUNNING);
2740 * Do various things that may trigger events.
2742 static __init void event_test_stuff(void)
2744 struct task_struct *test_thread;
2746 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2748 kthread_stop(test_thread);
2752 * For every trace event defined, we will test each trace point separately,
2753 * and then by groups, and finally all trace points.
2755 static __init void event_trace_self_tests(void)
2757 struct ftrace_subsystem_dir *dir;
2758 struct ftrace_event_file *file;
2759 struct ftrace_event_call *call;
2760 struct event_subsystem *system;
2761 struct trace_array *tr;
2764 tr = top_trace_array();
2768 pr_info("Running tests on trace events:\n");
2770 list_for_each_entry(file, &tr->events, list) {
2772 call = file->event_call;
2774 /* Only test those that have a probe */
2775 if (!call->class || !call->class->probe)
2779 * Testing syscall events here is pretty useless, but
2780 * we still do it if configured. But this is time consuming.
2781 * What we really need is a user thread to perform the
2782 * syscalls as we test.
2784 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2785 if (call->class->system &&
2786 strcmp(call->class->system, "syscalls") == 0)
2790 pr_info("Testing event %s: ", ftrace_event_name(call));
2793 * If an event is already enabled, someone is using
2794 * it and the self test should not be on.
2796 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2797 pr_warn("Enabled event during self test!\n");
2802 ftrace_event_enable_disable(file, 1);
2804 ftrace_event_enable_disable(file, 0);
2809 /* Now test at the sub system level */
2811 pr_info("Running tests on trace event systems:\n");
2813 list_for_each_entry(dir, &tr->systems, list) {
2815 system = dir->subsystem;
2817 /* the ftrace system is special, skip it */
2818 if (strcmp(system->name, "ftrace") == 0)
2821 pr_info("Testing event system %s: ", system->name);
2823 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2824 if (WARN_ON_ONCE(ret)) {
2825 pr_warn("error enabling system %s\n",
2832 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2833 if (WARN_ON_ONCE(ret)) {
2834 pr_warn("error disabling system %s\n",
2842 /* Test with all events enabled */
2844 pr_info("Running tests on all trace events:\n");
2845 pr_info("Testing all events: ");
2847 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2848 if (WARN_ON_ONCE(ret)) {
2849 pr_warn("error enabling all events\n");
2856 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2857 if (WARN_ON_ONCE(ret)) {
2858 pr_warn("error disabling all events\n");
2865 #ifdef CONFIG_FUNCTION_TRACER
2867 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2870 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2871 struct ftrace_ops *op, struct pt_regs *pt_regs)
2873 struct ring_buffer_event *event;
2874 struct ring_buffer *buffer;
2875 struct ftrace_entry *entry;
2876 unsigned long flags;
2881 pc = preempt_count();
2882 preempt_disable_notrace();
2883 cpu = raw_smp_processor_id();
2884 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2889 local_save_flags(flags);
2891 event = trace_current_buffer_lock_reserve(&buffer,
2892 TRACE_FN, sizeof(*entry),
2896 entry = ring_buffer_event_data(event);
2898 entry->parent_ip = parent_ip;
2900 trace_buffer_unlock_commit(buffer, event, flags, pc);
2903 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2904 preempt_enable_notrace();
2907 static struct ftrace_ops trace_ops __initdata =
2909 .func = function_test_events_call,
2910 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2913 static __init void event_trace_self_test_with_function(void)
2916 ret = register_ftrace_function(&trace_ops);
2917 if (WARN_ON(ret < 0)) {
2918 pr_info("Failed to enable function tracer for event tests\n");
2921 pr_info("Running tests again, along with the function tracer\n");
2922 event_trace_self_tests();
2923 unregister_ftrace_function(&trace_ops);
2926 static __init void event_trace_self_test_with_function(void)
2931 static __init int event_trace_self_tests_init(void)
2933 if (!tracing_selftest_disabled) {
2934 event_trace_self_tests();
2935 event_trace_self_test_with_function();
2941 late_initcall(event_trace_self_tests_init);