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;
347 * When soft_disable is set and enable is cleared, the sm_ref
348 * reference counter is decremented. If it reaches 0, we want
349 * to clear the SOFT_DISABLED flag but leave the event in the
350 * state that it was. That is, if the event was enabled and
351 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
352 * is set we do not want the event to be enabled before we
355 * When soft_disable is not set but the SOFT_MODE flag is,
356 * we do nothing. Do not disable the tracepoint, otherwise
357 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
360 if (atomic_dec_return(&file->sm_ref) > 0)
362 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
363 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
365 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
367 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
368 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
369 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
370 tracing_stop_cmdline_record();
371 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
373 call->class->reg(call, TRACE_REG_UNREGISTER, file);
375 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
376 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
377 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
379 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
383 * When soft_disable is set and enable is set, we want to
384 * register the tracepoint for the event, but leave the event
385 * as is. That means, if the event was already enabled, we do
386 * nothing (but set SOFT_MODE). If the event is disabled, we
387 * set SOFT_DISABLED before enabling the event tracepoint, so
388 * it still seems to be disabled.
391 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
393 if (atomic_inc_return(&file->sm_ref) > 1)
395 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
398 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
400 /* Keep the event disabled, when going to SOFT_MODE. */
402 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
404 if (trace_flags & TRACE_ITER_RECORD_CMD) {
405 tracing_start_cmdline_record();
406 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
408 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
410 tracing_stop_cmdline_record();
411 pr_info("event trace: Could not enable event "
412 "%s\n", trace_event_name(call));
415 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
417 /* WAS_ENABLED gets set but never cleared. */
418 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
426 int trace_event_enable_disable(struct trace_event_file *file,
427 int enable, int soft_disable)
429 return __ftrace_event_enable_disable(file, enable, soft_disable);
432 static int ftrace_event_enable_disable(struct trace_event_file *file,
435 return __ftrace_event_enable_disable(file, enable, 0);
438 static void ftrace_clear_events(struct trace_array *tr)
440 struct trace_event_file *file;
442 mutex_lock(&event_mutex);
443 list_for_each_entry(file, &tr->events, list) {
444 ftrace_event_enable_disable(file, 0);
446 mutex_unlock(&event_mutex);
449 static void __put_system(struct event_subsystem *system)
451 struct event_filter *filter = system->filter;
453 WARN_ON_ONCE(system_refcount(system) == 0);
454 if (system_refcount_dec(system))
457 list_del(&system->list);
460 kfree(filter->filter_string);
463 if (system->ref_count & SYSTEM_FL_FREE_NAME)
468 static void __get_system(struct event_subsystem *system)
470 WARN_ON_ONCE(system_refcount(system) == 0);
471 system_refcount_inc(system);
474 static void __get_system_dir(struct trace_subsystem_dir *dir)
476 WARN_ON_ONCE(dir->ref_count == 0);
478 __get_system(dir->subsystem);
481 static void __put_system_dir(struct trace_subsystem_dir *dir)
483 WARN_ON_ONCE(dir->ref_count == 0);
484 /* If the subsystem is about to be freed, the dir must be too */
485 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
487 __put_system(dir->subsystem);
488 if (!--dir->ref_count)
492 static void put_system(struct trace_subsystem_dir *dir)
494 mutex_lock(&event_mutex);
495 __put_system_dir(dir);
496 mutex_unlock(&event_mutex);
499 static void remove_subsystem(struct trace_subsystem_dir *dir)
504 if (!--dir->nr_events) {
505 tracefs_remove_recursive(dir->entry);
506 list_del(&dir->list);
507 __put_system_dir(dir);
511 static void remove_event_file_dir(struct trace_event_file *file)
513 struct dentry *dir = file->dir;
514 struct dentry *child;
517 spin_lock(&dir->d_lock); /* probably unneeded */
518 list_for_each_entry(child, &dir->d_subdirs, d_child) {
519 if (d_really_is_positive(child)) /* probably unneeded */
520 d_inode(child)->i_private = NULL;
522 spin_unlock(&dir->d_lock);
524 tracefs_remove_recursive(dir);
527 list_del(&file->list);
528 remove_subsystem(file->system);
529 free_event_filter(file->filter);
530 kmem_cache_free(file_cachep, file);
534 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
537 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
538 const char *sub, const char *event, int set)
540 struct trace_event_file *file;
541 struct trace_event_call *call;
545 list_for_each_entry(file, &tr->events, list) {
547 call = file->event_call;
548 name = trace_event_name(call);
550 if (!name || !call->class || !call->class->reg)
553 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
557 strcmp(match, name) != 0 &&
558 strcmp(match, call->class->system) != 0)
561 if (sub && strcmp(sub, call->class->system) != 0)
564 if (event && strcmp(event, name) != 0)
567 ftrace_event_enable_disable(file, set);
575 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
576 const char *sub, const char *event, int set)
580 mutex_lock(&event_mutex);
581 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
582 mutex_unlock(&event_mutex);
587 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
589 char *event = NULL, *sub = NULL, *match;
593 * The buf format can be <subsystem>:<event-name>
594 * *:<event-name> means any event by that name.
595 * :<event-name> is the same.
597 * <subsystem>:* means all events in that subsystem
598 * <subsystem>: means the same.
600 * <name> (no ':') means all events in a subsystem with
601 * the name <name> or any event that matches <name>
604 match = strsep(&buf, ":");
610 if (!strlen(sub) || strcmp(sub, "*") == 0)
612 if (!strlen(event) || strcmp(event, "*") == 0)
616 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
618 /* Put back the colon to allow this to be called again */
626 * trace_set_clr_event - enable or disable an event
627 * @system: system name to match (NULL for any system)
628 * @event: event name to match (NULL for all events, within system)
629 * @set: 1 to enable, 0 to disable
631 * This is a way for other parts of the kernel to enable or disable
634 * Returns 0 on success, -EINVAL if the parameters do not match any
637 int trace_set_clr_event(const char *system, const char *event, int set)
639 struct trace_array *tr = top_trace_array();
644 return __ftrace_set_clr_event(tr, NULL, system, event, set);
646 EXPORT_SYMBOL_GPL(trace_set_clr_event);
648 /* 128 should be much more than enough */
649 #define EVENT_BUF_SIZE 127
652 ftrace_event_write(struct file *file, const char __user *ubuf,
653 size_t cnt, loff_t *ppos)
655 struct trace_parser parser;
656 struct seq_file *m = file->private_data;
657 struct trace_array *tr = m->private;
663 ret = tracing_update_buffers();
667 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
670 read = trace_get_user(&parser, ubuf, cnt, ppos);
672 if (read >= 0 && trace_parser_loaded((&parser))) {
675 if (*parser.buffer == '!')
678 parser.buffer[parser.idx] = 0;
680 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
688 trace_parser_put(&parser);
694 t_next(struct seq_file *m, void *v, loff_t *pos)
696 struct trace_event_file *file = v;
697 struct trace_event_call *call;
698 struct trace_array *tr = m->private;
702 list_for_each_entry_continue(file, &tr->events, list) {
703 call = file->event_call;
705 * The ftrace subsystem is for showing formats only.
706 * They can not be enabled or disabled via the event files.
708 if (call->class && call->class->reg)
715 static void *t_start(struct seq_file *m, loff_t *pos)
717 struct trace_event_file *file;
718 struct trace_array *tr = m->private;
721 mutex_lock(&event_mutex);
723 file = list_entry(&tr->events, struct trace_event_file, list);
724 for (l = 0; l <= *pos; ) {
725 file = t_next(m, file, &l);
733 s_next(struct seq_file *m, void *v, loff_t *pos)
735 struct trace_event_file *file = v;
736 struct trace_array *tr = m->private;
740 list_for_each_entry_continue(file, &tr->events, list) {
741 if (file->flags & EVENT_FILE_FL_ENABLED)
748 static void *s_start(struct seq_file *m, loff_t *pos)
750 struct trace_event_file *file;
751 struct trace_array *tr = m->private;
754 mutex_lock(&event_mutex);
756 file = list_entry(&tr->events, struct trace_event_file, list);
757 for (l = 0; l <= *pos; ) {
758 file = s_next(m, file, &l);
765 static int t_show(struct seq_file *m, void *v)
767 struct trace_event_file *file = v;
768 struct trace_event_call *call = file->event_call;
770 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
771 seq_printf(m, "%s:", call->class->system);
772 seq_printf(m, "%s\n", trace_event_name(call));
777 static void t_stop(struct seq_file *m, void *p)
779 mutex_unlock(&event_mutex);
783 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
786 struct trace_event_file *file;
790 mutex_lock(&event_mutex);
791 file = event_file_data(filp);
794 mutex_unlock(&event_mutex);
799 if (flags & EVENT_FILE_FL_ENABLED &&
800 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
803 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
804 flags & EVENT_FILE_FL_SOFT_MODE)
809 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
813 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
816 struct trace_event_file *file;
820 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
824 ret = tracing_update_buffers();
832 mutex_lock(&event_mutex);
833 file = event_file_data(filp);
835 ret = ftrace_event_enable_disable(file, val);
836 mutex_unlock(&event_mutex);
845 return ret ? ret : cnt;
849 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
852 const char set_to_char[4] = { '?', '0', '1', 'X' };
853 struct trace_subsystem_dir *dir = filp->private_data;
854 struct event_subsystem *system = dir->subsystem;
855 struct trace_event_call *call;
856 struct trace_event_file *file;
857 struct trace_array *tr = dir->tr;
862 mutex_lock(&event_mutex);
863 list_for_each_entry(file, &tr->events, list) {
864 call = file->event_call;
865 if (!trace_event_name(call) || !call->class || !call->class->reg)
868 if (system && strcmp(call->class->system, system->name) != 0)
872 * We need to find out if all the events are set
873 * or if all events or cleared, or if we have
876 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
879 * If we have a mixture, no need to look further.
884 mutex_unlock(&event_mutex);
886 buf[0] = set_to_char[set];
889 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
895 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
898 struct trace_subsystem_dir *dir = filp->private_data;
899 struct event_subsystem *system = dir->subsystem;
900 const char *name = NULL;
904 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
908 ret = tracing_update_buffers();
912 if (val != 0 && val != 1)
916 * Opening of "enable" adds a ref count to system,
917 * so the name is safe to use.
922 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
936 FORMAT_FIELD_SEPERATOR = 2,
940 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
942 struct trace_event_call *call = event_file_data(m->private);
943 struct list_head *common_head = &ftrace_common_fields;
944 struct list_head *head = trace_get_fields(call);
945 struct list_head *node = v;
949 switch ((unsigned long)v) {
954 case FORMAT_FIELD_SEPERATOR:
958 case FORMAT_PRINTFMT:
964 if (node == common_head)
965 return (void *)FORMAT_FIELD_SEPERATOR;
966 else if (node == head)
967 return (void *)FORMAT_PRINTFMT;
972 static int f_show(struct seq_file *m, void *v)
974 struct trace_event_call *call = event_file_data(m->private);
975 struct ftrace_event_field *field;
976 const char *array_descriptor;
978 switch ((unsigned long)v) {
980 seq_printf(m, "name: %s\n", trace_event_name(call));
981 seq_printf(m, "ID: %d\n", call->event.type);
982 seq_puts(m, "format:\n");
985 case FORMAT_FIELD_SEPERATOR:
989 case FORMAT_PRINTFMT:
990 seq_printf(m, "\nprint fmt: %s\n",
995 field = list_entry(v, struct ftrace_event_field, link);
997 * Smartly shows the array type(except dynamic array).
1000 * If TYPE := TYPE[LEN], it is shown:
1001 * field:TYPE VAR[LEN]
1003 array_descriptor = strchr(field->type, '[');
1005 if (!strncmp(field->type, "__data_loc", 10))
1006 array_descriptor = NULL;
1008 if (!array_descriptor)
1009 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1010 field->type, field->name, field->offset,
1011 field->size, !!field->is_signed);
1013 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1014 (int)(array_descriptor - field->type),
1015 field->type, field->name,
1016 array_descriptor, field->offset,
1017 field->size, !!field->is_signed);
1022 static void *f_start(struct seq_file *m, loff_t *pos)
1024 void *p = (void *)FORMAT_HEADER;
1027 /* ->stop() is called even if ->start() fails */
1028 mutex_lock(&event_mutex);
1029 if (!event_file_data(m->private))
1030 return ERR_PTR(-ENODEV);
1032 while (l < *pos && p)
1033 p = f_next(m, p, &l);
1038 static void f_stop(struct seq_file *m, void *p)
1040 mutex_unlock(&event_mutex);
1043 static const struct seq_operations trace_format_seq_ops = {
1050 static int trace_format_open(struct inode *inode, struct file *file)
1055 ret = seq_open(file, &trace_format_seq_ops);
1059 m = file->private_data;
1066 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1068 int id = (long)event_file_data(filp);
1078 len = sprintf(buf, "%d\n", id);
1080 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1084 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1087 struct trace_event_file *file;
1088 struct trace_seq *s;
1094 s = kmalloc(sizeof(*s), GFP_KERNEL);
1101 mutex_lock(&event_mutex);
1102 file = event_file_data(filp);
1104 print_event_filter(file, s);
1105 mutex_unlock(&event_mutex);
1108 r = simple_read_from_buffer(ubuf, cnt, ppos,
1109 s->buffer, trace_seq_used(s));
1117 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1120 struct trace_event_file *file;
1124 if (cnt >= PAGE_SIZE)
1127 buf = (char *)__get_free_page(GFP_TEMPORARY);
1131 if (copy_from_user(buf, ubuf, cnt)) {
1132 free_page((unsigned long) buf);
1137 mutex_lock(&event_mutex);
1138 file = event_file_data(filp);
1140 err = apply_event_filter(file, buf);
1141 mutex_unlock(&event_mutex);
1143 free_page((unsigned long) buf);
1152 static LIST_HEAD(event_subsystems);
1154 static int subsystem_open(struct inode *inode, struct file *filp)
1156 struct event_subsystem *system = NULL;
1157 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1158 struct trace_array *tr;
1161 if (tracing_is_disabled())
1164 /* Make sure the system still exists */
1165 mutex_lock(&trace_types_lock);
1166 mutex_lock(&event_mutex);
1167 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1168 list_for_each_entry(dir, &tr->systems, list) {
1169 if (dir == inode->i_private) {
1170 /* Don't open systems with no events */
1171 if (dir->nr_events) {
1172 __get_system_dir(dir);
1173 system = dir->subsystem;
1180 mutex_unlock(&event_mutex);
1181 mutex_unlock(&trace_types_lock);
1186 /* Some versions of gcc think dir can be uninitialized here */
1189 /* Still need to increment the ref count of the system */
1190 if (trace_array_get(tr) < 0) {
1195 ret = tracing_open_generic(inode, filp);
1197 trace_array_put(tr);
1204 static int system_tr_open(struct inode *inode, struct file *filp)
1206 struct trace_subsystem_dir *dir;
1207 struct trace_array *tr = inode->i_private;
1210 if (tracing_is_disabled())
1213 if (trace_array_get(tr) < 0)
1216 /* Make a temporary dir that has no system but points to tr */
1217 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1219 trace_array_put(tr);
1225 ret = tracing_open_generic(inode, filp);
1227 trace_array_put(tr);
1232 filp->private_data = dir;
1237 static int subsystem_release(struct inode *inode, struct file *file)
1239 struct trace_subsystem_dir *dir = file->private_data;
1241 trace_array_put(dir->tr);
1244 * If dir->subsystem is NULL, then this is a temporary
1245 * descriptor that was made for a trace_array to enable
1257 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1260 struct trace_subsystem_dir *dir = filp->private_data;
1261 struct event_subsystem *system = dir->subsystem;
1262 struct trace_seq *s;
1268 s = kmalloc(sizeof(*s), GFP_KERNEL);
1274 print_subsystem_event_filter(system, s);
1275 r = simple_read_from_buffer(ubuf, cnt, ppos,
1276 s->buffer, trace_seq_used(s));
1284 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1287 struct trace_subsystem_dir *dir = filp->private_data;
1291 if (cnt >= PAGE_SIZE)
1294 buf = (char *)__get_free_page(GFP_TEMPORARY);
1298 if (copy_from_user(buf, ubuf, cnt)) {
1299 free_page((unsigned long) buf);
1304 err = apply_subsystem_event_filter(dir, buf);
1305 free_page((unsigned long) buf);
1315 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1317 int (*func)(struct trace_seq *s) = filp->private_data;
1318 struct trace_seq *s;
1324 s = kmalloc(sizeof(*s), GFP_KERNEL);
1331 r = simple_read_from_buffer(ubuf, cnt, ppos,
1332 s->buffer, trace_seq_used(s));
1339 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1340 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1341 static int ftrace_event_release(struct inode *inode, struct file *file);
1343 static const struct seq_operations show_event_seq_ops = {
1350 static const struct seq_operations show_set_event_seq_ops = {
1357 static const struct file_operations ftrace_avail_fops = {
1358 .open = ftrace_event_avail_open,
1360 .llseek = seq_lseek,
1361 .release = seq_release,
1364 static const struct file_operations ftrace_set_event_fops = {
1365 .open = ftrace_event_set_open,
1367 .write = ftrace_event_write,
1368 .llseek = seq_lseek,
1369 .release = ftrace_event_release,
1372 static const struct file_operations ftrace_enable_fops = {
1373 .open = tracing_open_generic,
1374 .read = event_enable_read,
1375 .write = event_enable_write,
1376 .llseek = default_llseek,
1379 static const struct file_operations ftrace_event_format_fops = {
1380 .open = trace_format_open,
1382 .llseek = seq_lseek,
1383 .release = seq_release,
1386 static const struct file_operations ftrace_event_id_fops = {
1387 .read = event_id_read,
1388 .llseek = default_llseek,
1391 static const struct file_operations ftrace_event_filter_fops = {
1392 .open = tracing_open_generic,
1393 .read = event_filter_read,
1394 .write = event_filter_write,
1395 .llseek = default_llseek,
1398 static const struct file_operations ftrace_subsystem_filter_fops = {
1399 .open = subsystem_open,
1400 .read = subsystem_filter_read,
1401 .write = subsystem_filter_write,
1402 .llseek = default_llseek,
1403 .release = subsystem_release,
1406 static const struct file_operations ftrace_system_enable_fops = {
1407 .open = subsystem_open,
1408 .read = system_enable_read,
1409 .write = system_enable_write,
1410 .llseek = default_llseek,
1411 .release = subsystem_release,
1414 static const struct file_operations ftrace_tr_enable_fops = {
1415 .open = system_tr_open,
1416 .read = system_enable_read,
1417 .write = system_enable_write,
1418 .llseek = default_llseek,
1419 .release = subsystem_release,
1422 static const struct file_operations ftrace_show_header_fops = {
1423 .open = tracing_open_generic,
1424 .read = show_header,
1425 .llseek = default_llseek,
1429 ftrace_event_open(struct inode *inode, struct file *file,
1430 const struct seq_operations *seq_ops)
1435 ret = seq_open(file, seq_ops);
1438 m = file->private_data;
1439 /* copy tr over to seq ops */
1440 m->private = inode->i_private;
1445 static int ftrace_event_release(struct inode *inode, struct file *file)
1447 struct trace_array *tr = inode->i_private;
1449 trace_array_put(tr);
1451 return seq_release(inode, file);
1455 ftrace_event_avail_open(struct inode *inode, struct file *file)
1457 const struct seq_operations *seq_ops = &show_event_seq_ops;
1459 return ftrace_event_open(inode, file, seq_ops);
1463 ftrace_event_set_open(struct inode *inode, struct file *file)
1465 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1466 struct trace_array *tr = inode->i_private;
1469 if (trace_array_get(tr) < 0)
1472 if ((file->f_mode & FMODE_WRITE) &&
1473 (file->f_flags & O_TRUNC))
1474 ftrace_clear_events(tr);
1476 ret = ftrace_event_open(inode, file, seq_ops);
1478 trace_array_put(tr);
1482 static struct event_subsystem *
1483 create_new_subsystem(const char *name)
1485 struct event_subsystem *system;
1487 /* need to create new entry */
1488 system = kmalloc(sizeof(*system), GFP_KERNEL);
1492 system->ref_count = 1;
1494 /* Only allocate if dynamic (kprobes and modules) */
1495 if (!core_kernel_data((unsigned long)name)) {
1496 system->ref_count |= SYSTEM_FL_FREE_NAME;
1497 system->name = kstrdup(name, GFP_KERNEL);
1501 system->name = name;
1503 system->filter = NULL;
1505 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1506 if (!system->filter)
1509 list_add(&system->list, &event_subsystems);
1514 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1515 kfree(system->name);
1520 static struct dentry *
1521 event_subsystem_dir(struct trace_array *tr, const char *name,
1522 struct trace_event_file *file, struct dentry *parent)
1524 struct trace_subsystem_dir *dir;
1525 struct event_subsystem *system;
1526 struct dentry *entry;
1528 /* First see if we did not already create this dir */
1529 list_for_each_entry(dir, &tr->systems, list) {
1530 system = dir->subsystem;
1531 if (strcmp(system->name, name) == 0) {
1538 /* Now see if the system itself exists. */
1539 list_for_each_entry(system, &event_subsystems, list) {
1540 if (strcmp(system->name, name) == 0)
1543 /* Reset system variable when not found */
1544 if (&system->list == &event_subsystems)
1547 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1552 system = create_new_subsystem(name);
1556 __get_system(system);
1558 dir->entry = tracefs_create_dir(name, parent);
1560 pr_warn("Failed to create system directory %s\n", name);
1561 __put_system(system);
1568 dir->subsystem = system;
1571 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
1572 &ftrace_subsystem_filter_fops);
1574 kfree(system->filter);
1575 system->filter = NULL;
1576 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
1579 trace_create_file("enable", 0644, dir->entry, dir,
1580 &ftrace_system_enable_fops);
1582 list_add(&dir->list, &tr->systems);
1589 /* Only print this message if failed on memory allocation */
1590 if (!dir || !system)
1591 pr_warn("No memory to create event subsystem %s\n", name);
1596 event_create_dir(struct dentry *parent, struct trace_event_file *file)
1598 struct trace_event_call *call = file->event_call;
1599 struct trace_array *tr = file->tr;
1600 struct list_head *head;
1601 struct dentry *d_events;
1606 * If the trace point header did not define TRACE_SYSTEM
1607 * then the system would be called "TRACE_SYSTEM".
1609 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1610 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1616 name = trace_event_name(call);
1617 file->dir = tracefs_create_dir(name, d_events);
1619 pr_warn("Could not create tracefs '%s' directory\n", name);
1623 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1624 trace_create_file("enable", 0644, file->dir, file,
1625 &ftrace_enable_fops);
1627 #ifdef CONFIG_PERF_EVENTS
1628 if (call->event.type && call->class->reg)
1629 trace_create_file("id", 0444, file->dir,
1630 (void *)(long)call->event.type,
1631 &ftrace_event_id_fops);
1635 * Other events may have the same class. Only update
1636 * the fields if they are not already defined.
1638 head = trace_get_fields(call);
1639 if (list_empty(head)) {
1640 ret = call->class->define_fields(call);
1642 pr_warn("Could not initialize trace point events/%s\n",
1647 trace_create_file("filter", 0644, file->dir, file,
1648 &ftrace_event_filter_fops);
1650 trace_create_file("trigger", 0644, file->dir, file,
1651 &event_trigger_fops);
1653 trace_create_file("format", 0444, file->dir, call,
1654 &ftrace_event_format_fops);
1659 static void remove_event_from_tracers(struct trace_event_call *call)
1661 struct trace_event_file *file;
1662 struct trace_array *tr;
1664 do_for_each_event_file_safe(tr, file) {
1665 if (file->event_call != call)
1668 remove_event_file_dir(file);
1670 * The do_for_each_event_file_safe() is
1671 * a double loop. After finding the call for this
1672 * trace_array, we use break to jump to the next
1676 } while_for_each_event_file();
1679 static void event_remove(struct trace_event_call *call)
1681 struct trace_array *tr;
1682 struct trace_event_file *file;
1684 do_for_each_event_file(tr, file) {
1685 if (file->event_call != call)
1687 ftrace_event_enable_disable(file, 0);
1689 * The do_for_each_event_file() is
1690 * a double loop. After finding the call for this
1691 * trace_array, we use break to jump to the next
1695 } while_for_each_event_file();
1697 if (call->event.funcs)
1698 __unregister_trace_event(&call->event);
1699 remove_event_from_tracers(call);
1700 list_del(&call->list);
1703 static int event_init(struct trace_event_call *call)
1708 name = trace_event_name(call);
1712 if (call->class->raw_init) {
1713 ret = call->class->raw_init(call);
1714 if (ret < 0 && ret != -ENOSYS)
1715 pr_warn("Could not initialize trace events/%s\n", name);
1722 __register_event(struct trace_event_call *call, struct module *mod)
1726 ret = event_init(call);
1730 list_add(&call->list, &ftrace_events);
1736 static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
1741 /* Find the length of the enum value as a string */
1742 elen = snprintf(ptr, 0, "%ld", map->enum_value);
1743 /* Make sure there's enough room to replace the string with the value */
1747 snprintf(ptr, elen + 1, "%ld", map->enum_value);
1749 /* Get the rest of the string of ptr */
1750 rlen = strlen(ptr + len);
1751 memmove(ptr + elen, ptr + len, rlen);
1752 /* Make sure we end the new string */
1753 ptr[elen + rlen] = 0;
1758 static void update_event_printk(struct trace_event_call *call,
1759 struct trace_enum_map *map)
1763 int len = strlen(map->enum_string);
1765 for (ptr = call->print_fmt; *ptr; ptr++) {
1779 if (isdigit(*ptr)) {
1783 /* Check for alpha chars like ULL */
1784 } while (isalnum(*ptr));
1788 * A number must have some kind of delimiter after
1789 * it, and we can ignore that too.
1793 if (isalpha(*ptr) || *ptr == '_') {
1794 if (strncmp(map->enum_string, ptr, len) == 0 &&
1795 !isalnum(ptr[len]) && ptr[len] != '_') {
1796 ptr = enum_replace(ptr, map, len);
1797 /* Hmm, enum string smaller than value */
1798 if (WARN_ON_ONCE(!ptr))
1801 * No need to decrement here, as enum_replace()
1802 * returns the pointer to the character passed
1803 * the enum, and two enums can not be placed
1804 * back to back without something in between.
1805 * We can skip that something in between.
1812 } while (isalnum(*ptr) || *ptr == '_');
1816 * If what comes after this variable is a '.' or
1817 * '->' then we can continue to ignore that string.
1819 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
1820 ptr += *ptr == '.' ? 1 : 2;
1826 * Once again, we can skip the delimiter that came
1834 void trace_event_enum_update(struct trace_enum_map **map, int len)
1836 struct trace_event_call *call, *p;
1837 const char *last_system = NULL;
1841 down_write(&trace_event_sem);
1842 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1843 /* events are usually grouped together with systems */
1844 if (!last_system || call->class->system != last_system) {
1846 last_system = call->class->system;
1849 for (i = last_i; i < len; i++) {
1850 if (call->class->system == map[i]->system) {
1851 /* Save the first system if need be */
1854 update_event_printk(call, map[i]);
1858 up_write(&trace_event_sem);
1861 static struct trace_event_file *
1862 trace_create_new_event(struct trace_event_call *call,
1863 struct trace_array *tr)
1865 struct trace_event_file *file;
1867 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1871 file->event_call = call;
1873 atomic_set(&file->sm_ref, 0);
1874 atomic_set(&file->tm_ref, 0);
1875 INIT_LIST_HEAD(&file->triggers);
1876 list_add(&file->list, &tr->events);
1881 /* Add an event to a trace directory */
1883 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
1885 struct trace_event_file *file;
1887 file = trace_create_new_event(call, tr);
1891 return event_create_dir(tr->event_dir, file);
1895 * Just create a decriptor for early init. A descriptor is required
1896 * for enabling events at boot. We want to enable events before
1897 * the filesystem is initialized.
1900 __trace_early_add_new_event(struct trace_event_call *call,
1901 struct trace_array *tr)
1903 struct trace_event_file *file;
1905 file = trace_create_new_event(call, tr);
1912 struct ftrace_module_file_ops;
1913 static void __add_event_to_tracers(struct trace_event_call *call);
1915 /* Add an additional event_call dynamically */
1916 int trace_add_event_call(struct trace_event_call *call)
1919 mutex_lock(&trace_types_lock);
1920 mutex_lock(&event_mutex);
1922 ret = __register_event(call, NULL);
1924 __add_event_to_tracers(call);
1926 mutex_unlock(&event_mutex);
1927 mutex_unlock(&trace_types_lock);
1932 * Must be called under locking of trace_types_lock, event_mutex and
1935 static void __trace_remove_event_call(struct trace_event_call *call)
1938 trace_destroy_fields(call);
1939 free_event_filter(call->filter);
1940 call->filter = NULL;
1943 static int probe_remove_event_call(struct trace_event_call *call)
1945 struct trace_array *tr;
1946 struct trace_event_file *file;
1948 #ifdef CONFIG_PERF_EVENTS
1949 if (call->perf_refcount)
1952 do_for_each_event_file(tr, file) {
1953 if (file->event_call != call)
1956 * We can't rely on ftrace_event_enable_disable(enable => 0)
1957 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
1958 * TRACE_REG_UNREGISTER.
1960 if (file->flags & EVENT_FILE_FL_ENABLED)
1963 * The do_for_each_event_file_safe() is
1964 * a double loop. After finding the call for this
1965 * trace_array, we use break to jump to the next
1969 } while_for_each_event_file();
1971 __trace_remove_event_call(call);
1976 /* Remove an event_call */
1977 int trace_remove_event_call(struct trace_event_call *call)
1981 mutex_lock(&trace_types_lock);
1982 mutex_lock(&event_mutex);
1983 down_write(&trace_event_sem);
1984 ret = probe_remove_event_call(call);
1985 up_write(&trace_event_sem);
1986 mutex_unlock(&event_mutex);
1987 mutex_unlock(&trace_types_lock);
1992 #define for_each_event(event, start, end) \
1993 for (event = start; \
1994 (unsigned long)event < (unsigned long)end; \
1997 #ifdef CONFIG_MODULES
1999 static void trace_module_add_events(struct module *mod)
2001 struct trace_event_call **call, **start, **end;
2003 if (!mod->num_trace_events)
2006 /* Don't add infrastructure for mods without tracepoints */
2007 if (trace_module_has_bad_taint(mod)) {
2008 pr_err("%s: module has bad taint, not creating trace events\n",
2013 start = mod->trace_events;
2014 end = mod->trace_events + mod->num_trace_events;
2016 for_each_event(call, start, end) {
2017 __register_event(*call, mod);
2018 __add_event_to_tracers(*call);
2022 static void trace_module_remove_events(struct module *mod)
2024 struct trace_event_call *call, *p;
2025 bool clear_trace = false;
2027 down_write(&trace_event_sem);
2028 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2029 if (call->mod == mod) {
2030 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
2032 __trace_remove_event_call(call);
2035 up_write(&trace_event_sem);
2038 * It is safest to reset the ring buffer if the module being unloaded
2039 * registered any events that were used. The only worry is if
2040 * a new module gets loaded, and takes on the same id as the events
2041 * of this module. When printing out the buffer, traced events left
2042 * over from this module may be passed to the new module events and
2043 * unexpected results may occur.
2046 tracing_reset_all_online_cpus();
2049 static int trace_module_notify(struct notifier_block *self,
2050 unsigned long val, void *data)
2052 struct module *mod = data;
2054 mutex_lock(&trace_types_lock);
2055 mutex_lock(&event_mutex);
2057 case MODULE_STATE_COMING:
2058 trace_module_add_events(mod);
2060 case MODULE_STATE_GOING:
2061 trace_module_remove_events(mod);
2064 mutex_unlock(&event_mutex);
2065 mutex_unlock(&trace_types_lock);
2070 static struct notifier_block trace_module_nb = {
2071 .notifier_call = trace_module_notify,
2072 .priority = 1, /* higher than trace.c module notify */
2074 #endif /* CONFIG_MODULES */
2076 /* Create a new event directory structure for a trace directory. */
2078 __trace_add_event_dirs(struct trace_array *tr)
2080 struct trace_event_call *call;
2083 list_for_each_entry(call, &ftrace_events, list) {
2084 ret = __trace_add_new_event(call, tr);
2086 pr_warn("Could not create directory for event %s\n",
2087 trace_event_name(call));
2091 struct trace_event_file *
2092 find_event_file(struct trace_array *tr, const char *system, const char *event)
2094 struct trace_event_file *file;
2095 struct trace_event_call *call;
2098 list_for_each_entry(file, &tr->events, list) {
2100 call = file->event_call;
2101 name = trace_event_name(call);
2103 if (!name || !call->class || !call->class->reg)
2106 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2109 if (strcmp(event, name) == 0 &&
2110 strcmp(system, call->class->system) == 0)
2116 #ifdef CONFIG_DYNAMIC_FTRACE
2119 #define ENABLE_EVENT_STR "enable_event"
2120 #define DISABLE_EVENT_STR "disable_event"
2122 struct event_probe_data {
2123 struct trace_event_file *file;
2124 unsigned long count;
2130 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2132 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2133 struct event_probe_data *data = *pdata;
2139 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2141 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2145 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2147 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2148 struct event_probe_data *data = *pdata;
2156 /* Skip if the event is in a state we want to switch to */
2157 if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2160 if (data->count != -1)
2163 event_enable_probe(ip, parent_ip, _data);
2167 event_enable_print(struct seq_file *m, unsigned long ip,
2168 struct ftrace_probe_ops *ops, void *_data)
2170 struct event_probe_data *data = _data;
2172 seq_printf(m, "%ps:", (void *)ip);
2174 seq_printf(m, "%s:%s:%s",
2175 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2176 data->file->event_call->class->system,
2177 trace_event_name(data->file->event_call));
2179 if (data->count == -1)
2180 seq_puts(m, ":unlimited\n");
2182 seq_printf(m, ":count=%ld\n", data->count);
2188 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2191 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2192 struct event_probe_data *data = *pdata;
2199 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2202 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2203 struct event_probe_data *data = *pdata;
2205 if (WARN_ON_ONCE(data->ref <= 0))
2210 /* Remove the SOFT_MODE flag */
2211 __ftrace_event_enable_disable(data->file, 0, 1);
2212 module_put(data->file->event_call->mod);
2218 static struct ftrace_probe_ops event_enable_probe_ops = {
2219 .func = event_enable_probe,
2220 .print = event_enable_print,
2221 .init = event_enable_init,
2222 .free = event_enable_free,
2225 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2226 .func = event_enable_count_probe,
2227 .print = event_enable_print,
2228 .init = event_enable_init,
2229 .free = event_enable_free,
2232 static struct ftrace_probe_ops event_disable_probe_ops = {
2233 .func = event_enable_probe,
2234 .print = event_enable_print,
2235 .init = event_enable_init,
2236 .free = event_enable_free,
2239 static struct ftrace_probe_ops event_disable_count_probe_ops = {
2240 .func = event_enable_count_probe,
2241 .print = event_enable_print,
2242 .init = event_enable_init,
2243 .free = event_enable_free,
2247 event_enable_func(struct ftrace_hash *hash,
2248 char *glob, char *cmd, char *param, int enabled)
2250 struct trace_array *tr = top_trace_array();
2251 struct trace_event_file *file;
2252 struct ftrace_probe_ops *ops;
2253 struct event_probe_data *data;
2263 /* hash funcs only work with set_ftrace_filter */
2264 if (!enabled || !param)
2267 system = strsep(¶m, ":");
2271 event = strsep(¶m, ":");
2273 mutex_lock(&event_mutex);
2276 file = find_event_file(tr, system, event);
2280 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2283 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2285 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2287 if (glob[0] == '!') {
2288 unregister_ftrace_function_probe_func(glob+1, ops);
2294 data = kzalloc(sizeof(*data), GFP_KERNEL);
2298 data->enable = enable;
2305 number = strsep(¶m, ":");
2308 if (!strlen(number))
2312 * We use the callback data field (which is a pointer)
2315 ret = kstrtoul(number, 0, &data->count);
2320 /* Don't let event modules unload while probe registered */
2321 ret = try_module_get(file->event_call->mod);
2327 ret = __ftrace_event_enable_disable(file, 1, 1);
2330 ret = register_ftrace_function_probe(glob, ops, data);
2332 * The above returns on success the # of functions enabled,
2333 * but if it didn't find any functions it returns zero.
2334 * Consider no functions a failure too.
2341 /* Just return zero, not the number of enabled functions */
2344 mutex_unlock(&event_mutex);
2348 __ftrace_event_enable_disable(file, 0, 1);
2350 module_put(file->event_call->mod);
2356 static struct ftrace_func_command event_enable_cmd = {
2357 .name = ENABLE_EVENT_STR,
2358 .func = event_enable_func,
2361 static struct ftrace_func_command event_disable_cmd = {
2362 .name = DISABLE_EVENT_STR,
2363 .func = event_enable_func,
2366 static __init int register_event_cmds(void)
2370 ret = register_ftrace_command(&event_enable_cmd);
2371 if (WARN_ON(ret < 0))
2373 ret = register_ftrace_command(&event_disable_cmd);
2374 if (WARN_ON(ret < 0))
2375 unregister_ftrace_command(&event_enable_cmd);
2379 static inline int register_event_cmds(void) { return 0; }
2380 #endif /* CONFIG_DYNAMIC_FTRACE */
2383 * The top level array has already had its trace_event_file
2384 * descriptors created in order to allow for early events to
2385 * be recorded. This function is called after the tracefs has been
2386 * initialized, and we now have to create the files associated
2390 __trace_early_add_event_dirs(struct trace_array *tr)
2392 struct trace_event_file *file;
2396 list_for_each_entry(file, &tr->events, list) {
2397 ret = event_create_dir(tr->event_dir, file);
2399 pr_warn("Could not create directory for event %s\n",
2400 trace_event_name(file->event_call));
2405 * For early boot up, the top trace array requires to have
2406 * a list of events that can be enabled. This must be done before
2407 * the filesystem is set up in order to allow events to be traced
2411 __trace_early_add_events(struct trace_array *tr)
2413 struct trace_event_call *call;
2416 list_for_each_entry(call, &ftrace_events, list) {
2417 /* Early boot up should not have any modules loaded */
2418 if (WARN_ON_ONCE(call->mod))
2421 ret = __trace_early_add_new_event(call, tr);
2423 pr_warn("Could not create early event %s\n",
2424 trace_event_name(call));
2428 /* Remove the event directory structure for a trace directory. */
2430 __trace_remove_event_dirs(struct trace_array *tr)
2432 struct trace_event_file *file, *next;
2434 list_for_each_entry_safe(file, next, &tr->events, list)
2435 remove_event_file_dir(file);
2438 static void __add_event_to_tracers(struct trace_event_call *call)
2440 struct trace_array *tr;
2442 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2443 __trace_add_new_event(call, tr);
2446 extern struct trace_event_call *__start_ftrace_events[];
2447 extern struct trace_event_call *__stop_ftrace_events[];
2449 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2451 static __init int setup_trace_event(char *str)
2453 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2454 ring_buffer_expanded = true;
2455 tracing_selftest_disabled = true;
2459 __setup("trace_event=", setup_trace_event);
2461 /* Expects to have event_mutex held when called */
2463 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2465 struct dentry *d_events;
2466 struct dentry *entry;
2468 entry = tracefs_create_file("set_event", 0644, parent,
2469 tr, &ftrace_set_event_fops);
2471 pr_warn("Could not create tracefs 'set_event' entry\n");
2475 d_events = tracefs_create_dir("events", parent);
2477 pr_warn("Could not create tracefs 'events' directory\n");
2481 /* ring buffer internal formats */
2482 trace_create_file("header_page", 0444, d_events,
2483 ring_buffer_print_page_header,
2484 &ftrace_show_header_fops);
2486 trace_create_file("header_event", 0444, d_events,
2487 ring_buffer_print_entry_header,
2488 &ftrace_show_header_fops);
2490 trace_create_file("enable", 0644, d_events,
2491 tr, &ftrace_tr_enable_fops);
2493 tr->event_dir = d_events;
2499 * event_trace_add_tracer - add a instance of a trace_array to events
2500 * @parent: The parent dentry to place the files/directories for events in
2501 * @tr: The trace array associated with these events
2503 * When a new instance is created, it needs to set up its events
2504 * directory, as well as other files associated with events. It also
2505 * creates the event hierachry in the @parent/events directory.
2507 * Returns 0 on success.
2509 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2513 mutex_lock(&event_mutex);
2515 ret = create_event_toplevel_files(parent, tr);
2519 down_write(&trace_event_sem);
2520 __trace_add_event_dirs(tr);
2521 up_write(&trace_event_sem);
2524 mutex_unlock(&event_mutex);
2530 * The top trace array already had its file descriptors created.
2531 * Now the files themselves need to be created.
2534 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2538 mutex_lock(&event_mutex);
2540 ret = create_event_toplevel_files(parent, tr);
2544 down_write(&trace_event_sem);
2545 __trace_early_add_event_dirs(tr);
2546 up_write(&trace_event_sem);
2549 mutex_unlock(&event_mutex);
2554 int event_trace_del_tracer(struct trace_array *tr)
2556 mutex_lock(&event_mutex);
2558 /* Disable any event triggers and associated soft-disabled events */
2559 clear_event_triggers(tr);
2561 /* Disable any running events */
2562 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2564 /* Access to events are within rcu_read_lock_sched() */
2565 synchronize_sched();
2567 down_write(&trace_event_sem);
2568 __trace_remove_event_dirs(tr);
2569 tracefs_remove_recursive(tr->event_dir);
2570 up_write(&trace_event_sem);
2572 tr->event_dir = NULL;
2574 mutex_unlock(&event_mutex);
2579 static __init int event_trace_memsetup(void)
2581 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2582 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
2587 early_enable_events(struct trace_array *tr, bool disable_first)
2589 char *buf = bootup_event_buf;
2594 token = strsep(&buf, ",");
2601 /* Restarting syscalls requires that we stop them first */
2603 ftrace_set_clr_event(tr, token, 0);
2605 ret = ftrace_set_clr_event(tr, token, 1);
2607 pr_warn("Failed to enable trace event: %s\n", token);
2609 /* Put back the comma to allow this to be called again */
2615 static __init int event_trace_enable(void)
2617 struct trace_array *tr = top_trace_array();
2618 struct trace_event_call **iter, *call;
2624 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2627 ret = event_init(call);
2629 list_add(&call->list, &ftrace_events);
2633 * We need the top trace array to have a working set of trace
2634 * points at early init, before the debug files and directories
2635 * are created. Create the file entries now, and attach them
2636 * to the actual file dentries later.
2638 __trace_early_add_events(tr);
2640 early_enable_events(tr, false);
2642 trace_printk_start_comm();
2644 register_event_cmds();
2646 register_trigger_cmds();
2652 * event_trace_enable() is called from trace_event_init() first to
2653 * initialize events and perhaps start any events that are on the
2654 * command line. Unfortunately, there are some events that will not
2655 * start this early, like the system call tracepoints that need
2656 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
2657 * is called before pid 1 starts, and this flag is never set, making
2658 * the syscall tracepoint never get reached, but the event is enabled
2659 * regardless (and not doing anything).
2661 static __init int event_trace_enable_again(void)
2663 struct trace_array *tr;
2665 tr = top_trace_array();
2669 early_enable_events(tr, true);
2674 early_initcall(event_trace_enable_again);
2676 static __init int event_trace_init(void)
2678 struct trace_array *tr;
2679 struct dentry *d_tracer;
2680 struct dentry *entry;
2683 tr = top_trace_array();
2687 d_tracer = tracing_init_dentry();
2688 if (IS_ERR(d_tracer))
2691 entry = tracefs_create_file("available_events", 0444, d_tracer,
2692 tr, &ftrace_avail_fops);
2694 pr_warn("Could not create tracefs 'available_events' entry\n");
2696 if (trace_define_generic_fields())
2697 pr_warn("tracing: Failed to allocated generic fields");
2699 if (trace_define_common_fields())
2700 pr_warn("tracing: Failed to allocate common fields");
2702 ret = early_event_add_tracer(d_tracer, tr);
2706 #ifdef CONFIG_MODULES
2707 ret = register_module_notifier(&trace_module_nb);
2709 pr_warn("Failed to register trace events module notifier\n");
2714 void __init trace_event_init(void)
2716 event_trace_memsetup();
2717 init_ftrace_syscalls();
2718 event_trace_enable();
2721 fs_initcall(event_trace_init);
2723 #ifdef CONFIG_FTRACE_STARTUP_TEST
2725 static DEFINE_SPINLOCK(test_spinlock);
2726 static DEFINE_SPINLOCK(test_spinlock_irq);
2727 static DEFINE_MUTEX(test_mutex);
2729 static __init void test_work(struct work_struct *dummy)
2731 spin_lock(&test_spinlock);
2732 spin_lock_irq(&test_spinlock_irq);
2734 spin_unlock_irq(&test_spinlock_irq);
2735 spin_unlock(&test_spinlock);
2737 mutex_lock(&test_mutex);
2739 mutex_unlock(&test_mutex);
2742 static __init int event_test_thread(void *unused)
2746 test_malloc = kmalloc(1234, GFP_KERNEL);
2748 pr_info("failed to kmalloc\n");
2750 schedule_on_each_cpu(test_work);
2754 set_current_state(TASK_INTERRUPTIBLE);
2755 while (!kthread_should_stop()) {
2757 set_current_state(TASK_INTERRUPTIBLE);
2759 __set_current_state(TASK_RUNNING);
2765 * Do various things that may trigger events.
2767 static __init void event_test_stuff(void)
2769 struct task_struct *test_thread;
2771 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2773 kthread_stop(test_thread);
2777 * For every trace event defined, we will test each trace point separately,
2778 * and then by groups, and finally all trace points.
2780 static __init void event_trace_self_tests(void)
2782 struct trace_subsystem_dir *dir;
2783 struct trace_event_file *file;
2784 struct trace_event_call *call;
2785 struct event_subsystem *system;
2786 struct trace_array *tr;
2789 tr = top_trace_array();
2793 pr_info("Running tests on trace events:\n");
2795 list_for_each_entry(file, &tr->events, list) {
2797 call = file->event_call;
2799 /* Only test those that have a probe */
2800 if (!call->class || !call->class->probe)
2804 * Testing syscall events here is pretty useless, but
2805 * we still do it if configured. But this is time consuming.
2806 * What we really need is a user thread to perform the
2807 * syscalls as we test.
2809 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2810 if (call->class->system &&
2811 strcmp(call->class->system, "syscalls") == 0)
2815 pr_info("Testing event %s: ", trace_event_name(call));
2818 * If an event is already enabled, someone is using
2819 * it and the self test should not be on.
2821 if (file->flags & EVENT_FILE_FL_ENABLED) {
2822 pr_warn("Enabled event during self test!\n");
2827 ftrace_event_enable_disable(file, 1);
2829 ftrace_event_enable_disable(file, 0);
2834 /* Now test at the sub system level */
2836 pr_info("Running tests on trace event systems:\n");
2838 list_for_each_entry(dir, &tr->systems, list) {
2840 system = dir->subsystem;
2842 /* the ftrace system is special, skip it */
2843 if (strcmp(system->name, "ftrace") == 0)
2846 pr_info("Testing event system %s: ", system->name);
2848 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2849 if (WARN_ON_ONCE(ret)) {
2850 pr_warn("error enabling system %s\n",
2857 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2858 if (WARN_ON_ONCE(ret)) {
2859 pr_warn("error disabling system %s\n",
2867 /* Test with all events enabled */
2869 pr_info("Running tests on all trace events:\n");
2870 pr_info("Testing all events: ");
2872 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2873 if (WARN_ON_ONCE(ret)) {
2874 pr_warn("error enabling all events\n");
2881 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2882 if (WARN_ON_ONCE(ret)) {
2883 pr_warn("error disabling all events\n");
2890 #ifdef CONFIG_FUNCTION_TRACER
2892 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2895 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2896 struct ftrace_ops *op, struct pt_regs *pt_regs)
2898 struct ring_buffer_event *event;
2899 struct ring_buffer *buffer;
2900 struct ftrace_entry *entry;
2901 unsigned long flags;
2906 pc = preempt_count();
2907 preempt_disable_notrace();
2908 cpu = raw_smp_processor_id();
2909 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2914 local_save_flags(flags);
2916 event = trace_current_buffer_lock_reserve(&buffer,
2917 TRACE_FN, sizeof(*entry),
2921 entry = ring_buffer_event_data(event);
2923 entry->parent_ip = parent_ip;
2925 trace_buffer_unlock_commit(buffer, event, flags, pc);
2928 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2929 preempt_enable_notrace();
2932 static struct ftrace_ops trace_ops __initdata =
2934 .func = function_test_events_call,
2935 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2938 static __init void event_trace_self_test_with_function(void)
2941 ret = register_ftrace_function(&trace_ops);
2942 if (WARN_ON(ret < 0)) {
2943 pr_info("Failed to enable function tracer for event tests\n");
2946 pr_info("Running tests again, along with the function tracer\n");
2947 event_trace_self_tests();
2948 unregister_ftrace_function(&trace_ops);
2951 static __init void event_trace_self_test_with_function(void)
2956 static __init int event_trace_self_tests_init(void)
2958 if (!tracing_selftest_disabled) {
2959 event_trace_self_tests();
2960 event_trace_self_test_with_function();
2966 late_initcall(event_trace_self_tests_init);