Linux 3.12.39
[linux/fpc-iii.git] / kernel / trace / trace_events.c
blobbe15da87b3908dd393fe843881284052c2fdf8ff
1 /*
2 * event tracer
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>.
9 */
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
21 #include <asm/setup.h>
23 #include "trace_output.h"
25 #undef TRACE_SYSTEM
26 #define TRACE_SYSTEM "TRACE_SYSTEM"
28 DEFINE_MUTEX(event_mutex);
30 LIST_HEAD(ftrace_events);
31 static LIST_HEAD(ftrace_common_fields);
33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
35 static struct kmem_cache *field_cachep;
36 static struct kmem_cache *file_cachep;
38 #define SYSTEM_FL_FREE_NAME (1 << 31)
40 static inline int system_refcount(struct event_subsystem *system)
42 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
45 static int system_refcount_inc(struct event_subsystem *system)
47 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
50 static int system_refcount_dec(struct event_subsystem *system)
52 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
55 /* Double loops, do not use break, only goto's work */
56 #define do_for_each_event_file(tr, file) \
57 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
58 list_for_each_entry(file, &tr->events, list)
60 #define do_for_each_event_file_safe(tr, file) \
61 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
62 struct ftrace_event_file *___n; \
63 list_for_each_entry_safe(file, ___n, &tr->events, list)
65 #define while_for_each_event_file() \
68 static struct list_head *
69 trace_get_fields(struct ftrace_event_call *event_call)
71 if (!event_call->class->get_fields)
72 return &event_call->class->fields;
73 return event_call->class->get_fields(event_call);
76 static struct ftrace_event_field *
77 __find_event_field(struct list_head *head, char *name)
79 struct ftrace_event_field *field;
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
83 return field;
86 return NULL;
89 struct ftrace_event_field *
90 trace_find_event_field(struct ftrace_event_call *call, char *name)
92 struct ftrace_event_field *field;
93 struct list_head *head;
95 field = __find_event_field(&ftrace_common_fields, name);
96 if (field)
97 return field;
99 head = trace_get_fields(call);
100 return __find_event_field(head, name);
103 static int __trace_define_field(struct list_head *head, const char *type,
104 const char *name, int offset, int size,
105 int is_signed, int filter_type)
107 struct ftrace_event_field *field;
109 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
110 if (!field)
111 return -ENOMEM;
113 field->name = name;
114 field->type = type;
116 if (filter_type == FILTER_OTHER)
117 field->filter_type = filter_assign_type(type);
118 else
119 field->filter_type = filter_type;
121 field->offset = offset;
122 field->size = size;
123 field->is_signed = is_signed;
125 list_add(&field->link, head);
127 return 0;
130 int trace_define_field(struct ftrace_event_call *call, const char *type,
131 const char *name, int offset, int size, int is_signed,
132 int filter_type)
134 struct list_head *head;
136 if (WARN_ON(!call->class))
137 return 0;
139 head = trace_get_fields(call);
140 return __trace_define_field(head, type, name, offset, size,
141 is_signed, filter_type);
143 EXPORT_SYMBOL_GPL(trace_define_field);
145 #define __common_field(type, item) \
146 ret = __trace_define_field(&ftrace_common_fields, #type, \
147 "common_" #item, \
148 offsetof(typeof(ent), item), \
149 sizeof(ent.item), \
150 is_signed_type(type), FILTER_OTHER); \
151 if (ret) \
152 return ret;
154 static int trace_define_common_fields(void)
156 int ret;
157 struct trace_entry ent;
159 __common_field(unsigned short, type);
160 __common_field(unsigned char, flags);
161 __common_field(unsigned char, preempt_count);
162 __common_field(int, pid);
164 return ret;
167 static void trace_destroy_fields(struct ftrace_event_call *call)
169 struct ftrace_event_field *field, *next;
170 struct list_head *head;
172 head = trace_get_fields(call);
173 list_for_each_entry_safe(field, next, head, link) {
174 list_del(&field->link);
175 kmem_cache_free(field_cachep, field);
179 int trace_event_raw_init(struct ftrace_event_call *call)
181 int id;
183 id = register_ftrace_event(&call->event);
184 if (!id)
185 return -ENODEV;
187 return 0;
189 EXPORT_SYMBOL_GPL(trace_event_raw_init);
191 int ftrace_event_reg(struct ftrace_event_call *call,
192 enum trace_reg type, void *data)
194 struct ftrace_event_file *file = data;
196 switch (type) {
197 case TRACE_REG_REGISTER:
198 return tracepoint_probe_register(call->name,
199 call->class->probe,
200 file);
201 case TRACE_REG_UNREGISTER:
202 tracepoint_probe_unregister(call->name,
203 call->class->probe,
204 file);
205 return 0;
207 #ifdef CONFIG_PERF_EVENTS
208 case TRACE_REG_PERF_REGISTER:
209 return tracepoint_probe_register(call->name,
210 call->class->perf_probe,
211 call);
212 case TRACE_REG_PERF_UNREGISTER:
213 tracepoint_probe_unregister(call->name,
214 call->class->perf_probe,
215 call);
216 return 0;
217 case TRACE_REG_PERF_OPEN:
218 case TRACE_REG_PERF_CLOSE:
219 case TRACE_REG_PERF_ADD:
220 case TRACE_REG_PERF_DEL:
221 return 0;
222 #endif
224 return 0;
226 EXPORT_SYMBOL_GPL(ftrace_event_reg);
228 void trace_event_enable_cmd_record(bool enable)
230 struct ftrace_event_file *file;
231 struct trace_array *tr;
233 mutex_lock(&event_mutex);
234 do_for_each_event_file(tr, file) {
236 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
237 continue;
239 if (enable) {
240 tracing_start_cmdline_record();
241 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
242 } else {
243 tracing_stop_cmdline_record();
244 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
246 } while_for_each_event_file();
247 mutex_unlock(&event_mutex);
250 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
251 int enable, int soft_disable)
253 struct ftrace_event_call *call = file->event_call;
254 int ret = 0;
255 int disable;
257 switch (enable) {
258 case 0:
260 * When soft_disable is set and enable is cleared, the sm_ref
261 * reference counter is decremented. If it reaches 0, we want
262 * to clear the SOFT_DISABLED flag but leave the event in the
263 * state that it was. That is, if the event was enabled and
264 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265 * is set we do not want the event to be enabled before we
266 * clear the bit.
268 * When soft_disable is not set but the SOFT_MODE flag is,
269 * we do nothing. Do not disable the tracepoint, otherwise
270 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
272 if (soft_disable) {
273 if (atomic_dec_return(&file->sm_ref) > 0)
274 break;
275 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
276 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
277 } else
278 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
280 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
281 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
282 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
283 tracing_stop_cmdline_record();
284 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
286 call->class->reg(call, TRACE_REG_UNREGISTER, file);
288 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
289 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
290 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
291 else
292 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
293 break;
294 case 1:
296 * When soft_disable is set and enable is set, we want to
297 * register the tracepoint for the event, but leave the event
298 * as is. That means, if the event was already enabled, we do
299 * nothing (but set SOFT_MODE). If the event is disabled, we
300 * set SOFT_DISABLED before enabling the event tracepoint, so
301 * it still seems to be disabled.
303 if (!soft_disable)
304 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
305 else {
306 if (atomic_inc_return(&file->sm_ref) > 1)
307 break;
308 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
311 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
313 /* Keep the event disabled, when going to SOFT_MODE. */
314 if (soft_disable)
315 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
317 if (trace_flags & TRACE_ITER_RECORD_CMD) {
318 tracing_start_cmdline_record();
319 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
321 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
322 if (ret) {
323 tracing_stop_cmdline_record();
324 pr_info("event trace: Could not enable event "
325 "%s\n", call->name);
326 break;
328 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
330 /* WAS_ENABLED gets set but never cleared. */
331 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
333 break;
336 return ret;
339 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
340 int enable)
342 return __ftrace_event_enable_disable(file, enable, 0);
345 static void ftrace_clear_events(struct trace_array *tr)
347 struct ftrace_event_file *file;
349 mutex_lock(&event_mutex);
350 list_for_each_entry(file, &tr->events, list) {
351 ftrace_event_enable_disable(file, 0);
353 mutex_unlock(&event_mutex);
356 static void __put_system(struct event_subsystem *system)
358 struct event_filter *filter = system->filter;
360 WARN_ON_ONCE(system_refcount(system) == 0);
361 if (system_refcount_dec(system))
362 return;
364 list_del(&system->list);
366 if (filter) {
367 kfree(filter->filter_string);
368 kfree(filter);
370 if (system->ref_count & SYSTEM_FL_FREE_NAME)
371 kfree(system->name);
372 kfree(system);
375 static void __get_system(struct event_subsystem *system)
377 WARN_ON_ONCE(system_refcount(system) == 0);
378 system_refcount_inc(system);
381 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
383 WARN_ON_ONCE(dir->ref_count == 0);
384 dir->ref_count++;
385 __get_system(dir->subsystem);
388 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
390 WARN_ON_ONCE(dir->ref_count == 0);
391 /* If the subsystem is about to be freed, the dir must be too */
392 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
394 __put_system(dir->subsystem);
395 if (!--dir->ref_count)
396 kfree(dir);
399 static void put_system(struct ftrace_subsystem_dir *dir)
401 mutex_lock(&event_mutex);
402 __put_system_dir(dir);
403 mutex_unlock(&event_mutex);
406 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
408 if (!dir)
409 return;
411 if (!--dir->nr_events) {
412 debugfs_remove_recursive(dir->entry);
413 list_del(&dir->list);
414 __put_system_dir(dir);
418 static void *event_file_data(struct file *filp)
420 return ACCESS_ONCE(file_inode(filp)->i_private);
423 static void remove_event_file_dir(struct ftrace_event_file *file)
425 struct dentry *dir = file->dir;
426 struct dentry *child;
428 if (dir) {
429 spin_lock(&dir->d_lock); /* probably unneeded */
430 list_for_each_entry(child, &dir->d_subdirs, d_child) {
431 if (child->d_inode) /* probably unneeded */
432 child->d_inode->i_private = NULL;
434 spin_unlock(&dir->d_lock);
436 debugfs_remove_recursive(dir);
439 list_del(&file->list);
440 remove_subsystem(file->system);
441 kmem_cache_free(file_cachep, file);
445 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
447 static int
448 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
449 const char *sub, const char *event, int set)
451 struct ftrace_event_file *file;
452 struct ftrace_event_call *call;
453 int ret = -EINVAL;
455 list_for_each_entry(file, &tr->events, list) {
457 call = file->event_call;
459 if (!call->name || !call->class || !call->class->reg)
460 continue;
462 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
463 continue;
465 if (match &&
466 strcmp(match, call->name) != 0 &&
467 strcmp(match, call->class->system) != 0)
468 continue;
470 if (sub && strcmp(sub, call->class->system) != 0)
471 continue;
473 if (event && strcmp(event, call->name) != 0)
474 continue;
476 ftrace_event_enable_disable(file, set);
478 ret = 0;
481 return ret;
484 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
485 const char *sub, const char *event, int set)
487 int ret;
489 mutex_lock(&event_mutex);
490 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
491 mutex_unlock(&event_mutex);
493 return ret;
496 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
498 char *event = NULL, *sub = NULL, *match;
501 * The buf format can be <subsystem>:<event-name>
502 * *:<event-name> means any event by that name.
503 * :<event-name> is the same.
505 * <subsystem>:* means all events in that subsystem
506 * <subsystem>: means the same.
508 * <name> (no ':') means all events in a subsystem with
509 * the name <name> or any event that matches <name>
512 match = strsep(&buf, ":");
513 if (buf) {
514 sub = match;
515 event = buf;
516 match = NULL;
518 if (!strlen(sub) || strcmp(sub, "*") == 0)
519 sub = NULL;
520 if (!strlen(event) || strcmp(event, "*") == 0)
521 event = NULL;
524 return __ftrace_set_clr_event(tr, match, sub, event, set);
528 * trace_set_clr_event - enable or disable an event
529 * @system: system name to match (NULL for any system)
530 * @event: event name to match (NULL for all events, within system)
531 * @set: 1 to enable, 0 to disable
533 * This is a way for other parts of the kernel to enable or disable
534 * event recording.
536 * Returns 0 on success, -EINVAL if the parameters do not match any
537 * registered events.
539 int trace_set_clr_event(const char *system, const char *event, int set)
541 struct trace_array *tr = top_trace_array();
543 return __ftrace_set_clr_event(tr, NULL, system, event, set);
545 EXPORT_SYMBOL_GPL(trace_set_clr_event);
547 /* 128 should be much more than enough */
548 #define EVENT_BUF_SIZE 127
550 static ssize_t
551 ftrace_event_write(struct file *file, const char __user *ubuf,
552 size_t cnt, loff_t *ppos)
554 struct trace_parser parser;
555 struct seq_file *m = file->private_data;
556 struct trace_array *tr = m->private;
557 ssize_t read, ret;
559 if (!cnt)
560 return 0;
562 ret = tracing_update_buffers();
563 if (ret < 0)
564 return ret;
566 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
567 return -ENOMEM;
569 read = trace_get_user(&parser, ubuf, cnt, ppos);
571 if (read >= 0 && trace_parser_loaded((&parser))) {
572 int set = 1;
574 if (*parser.buffer == '!')
575 set = 0;
577 parser.buffer[parser.idx] = 0;
579 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
580 if (ret)
581 goto out_put;
584 ret = read;
586 out_put:
587 trace_parser_put(&parser);
589 return ret;
592 static void *
593 t_next(struct seq_file *m, void *v, loff_t *pos)
595 struct ftrace_event_file *file = v;
596 struct ftrace_event_call *call;
597 struct trace_array *tr = m->private;
599 (*pos)++;
601 list_for_each_entry_continue(file, &tr->events, list) {
602 call = file->event_call;
604 * The ftrace subsystem is for showing formats only.
605 * They can not be enabled or disabled via the event files.
607 if (call->class && call->class->reg)
608 return file;
611 return NULL;
614 static void *t_start(struct seq_file *m, loff_t *pos)
616 struct ftrace_event_file *file;
617 struct trace_array *tr = m->private;
618 loff_t l;
620 mutex_lock(&event_mutex);
622 file = list_entry(&tr->events, struct ftrace_event_file, list);
623 for (l = 0; l <= *pos; ) {
624 file = t_next(m, file, &l);
625 if (!file)
626 break;
628 return file;
631 static void *
632 s_next(struct seq_file *m, void *v, loff_t *pos)
634 struct ftrace_event_file *file = v;
635 struct trace_array *tr = m->private;
637 (*pos)++;
639 list_for_each_entry_continue(file, &tr->events, list) {
640 if (file->flags & FTRACE_EVENT_FL_ENABLED)
641 return file;
644 return NULL;
647 static void *s_start(struct seq_file *m, loff_t *pos)
649 struct ftrace_event_file *file;
650 struct trace_array *tr = m->private;
651 loff_t l;
653 mutex_lock(&event_mutex);
655 file = list_entry(&tr->events, struct ftrace_event_file, list);
656 for (l = 0; l <= *pos; ) {
657 file = s_next(m, file, &l);
658 if (!file)
659 break;
661 return file;
664 static int t_show(struct seq_file *m, void *v)
666 struct ftrace_event_file *file = v;
667 struct ftrace_event_call *call = file->event_call;
669 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
670 seq_printf(m, "%s:", call->class->system);
671 seq_printf(m, "%s\n", call->name);
673 return 0;
676 static void t_stop(struct seq_file *m, void *p)
678 mutex_unlock(&event_mutex);
681 static ssize_t
682 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
683 loff_t *ppos)
685 struct ftrace_event_file *file;
686 unsigned long flags;
687 char buf[4] = "0";
689 mutex_lock(&event_mutex);
690 file = event_file_data(filp);
691 if (likely(file))
692 flags = file->flags;
693 mutex_unlock(&event_mutex);
695 if (!file)
696 return -ENODEV;
698 if (flags & FTRACE_EVENT_FL_ENABLED &&
699 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
700 strcpy(buf, "1");
702 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
703 flags & FTRACE_EVENT_FL_SOFT_MODE)
704 strcat(buf, "*");
706 strcat(buf, "\n");
708 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
711 static ssize_t
712 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
713 loff_t *ppos)
715 struct ftrace_event_file *file;
716 unsigned long val;
717 int ret;
719 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
720 if (ret)
721 return ret;
723 ret = tracing_update_buffers();
724 if (ret < 0)
725 return ret;
727 switch (val) {
728 case 0:
729 case 1:
730 ret = -ENODEV;
731 mutex_lock(&event_mutex);
732 file = event_file_data(filp);
733 if (likely(file))
734 ret = ftrace_event_enable_disable(file, val);
735 mutex_unlock(&event_mutex);
736 break;
738 default:
739 return -EINVAL;
742 *ppos += cnt;
744 return ret ? ret : cnt;
747 static ssize_t
748 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
749 loff_t *ppos)
751 const char set_to_char[4] = { '?', '0', '1', 'X' };
752 struct ftrace_subsystem_dir *dir = filp->private_data;
753 struct event_subsystem *system = dir->subsystem;
754 struct ftrace_event_call *call;
755 struct ftrace_event_file *file;
756 struct trace_array *tr = dir->tr;
757 char buf[2];
758 int set = 0;
759 int ret;
761 mutex_lock(&event_mutex);
762 list_for_each_entry(file, &tr->events, list) {
763 call = file->event_call;
764 if (!call->name || !call->class || !call->class->reg)
765 continue;
767 if (system && strcmp(call->class->system, system->name) != 0)
768 continue;
771 * We need to find out if all the events are set
772 * or if all events or cleared, or if we have
773 * a mixture.
775 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
778 * If we have a mixture, no need to look further.
780 if (set == 3)
781 break;
783 mutex_unlock(&event_mutex);
785 buf[0] = set_to_char[set];
786 buf[1] = '\n';
788 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
790 return ret;
793 static ssize_t
794 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
795 loff_t *ppos)
797 struct ftrace_subsystem_dir *dir = filp->private_data;
798 struct event_subsystem *system = dir->subsystem;
799 const char *name = NULL;
800 unsigned long val;
801 ssize_t ret;
803 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
804 if (ret)
805 return ret;
807 ret = tracing_update_buffers();
808 if (ret < 0)
809 return ret;
811 if (val != 0 && val != 1)
812 return -EINVAL;
815 * Opening of "enable" adds a ref count to system,
816 * so the name is safe to use.
818 if (system)
819 name = system->name;
821 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
822 if (ret)
823 goto out;
825 ret = cnt;
827 out:
828 *ppos += cnt;
830 return ret;
833 enum {
834 FORMAT_HEADER = 1,
835 FORMAT_FIELD_SEPERATOR = 2,
836 FORMAT_PRINTFMT = 3,
839 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
841 struct ftrace_event_call *call = event_file_data(m->private);
842 struct list_head *common_head = &ftrace_common_fields;
843 struct list_head *head = trace_get_fields(call);
844 struct list_head *node = v;
846 (*pos)++;
848 switch ((unsigned long)v) {
849 case FORMAT_HEADER:
850 node = common_head;
851 break;
853 case FORMAT_FIELD_SEPERATOR:
854 node = head;
855 break;
857 case FORMAT_PRINTFMT:
858 /* all done */
859 return NULL;
862 node = node->prev;
863 if (node == common_head)
864 return (void *)FORMAT_FIELD_SEPERATOR;
865 else if (node == head)
866 return (void *)FORMAT_PRINTFMT;
867 else
868 return node;
871 static int f_show(struct seq_file *m, void *v)
873 struct ftrace_event_call *call = event_file_data(m->private);
874 struct ftrace_event_field *field;
875 const char *array_descriptor;
877 switch ((unsigned long)v) {
878 case FORMAT_HEADER:
879 seq_printf(m, "name: %s\n", call->name);
880 seq_printf(m, "ID: %d\n", call->event.type);
881 seq_printf(m, "format:\n");
882 return 0;
884 case FORMAT_FIELD_SEPERATOR:
885 seq_putc(m, '\n');
886 return 0;
888 case FORMAT_PRINTFMT:
889 seq_printf(m, "\nprint fmt: %s\n",
890 call->print_fmt);
891 return 0;
894 field = list_entry(v, struct ftrace_event_field, link);
896 * Smartly shows the array type(except dynamic array).
897 * Normal:
898 * field:TYPE VAR
899 * If TYPE := TYPE[LEN], it is shown:
900 * field:TYPE VAR[LEN]
902 array_descriptor = strchr(field->type, '[');
904 if (!strncmp(field->type, "__data_loc", 10))
905 array_descriptor = NULL;
907 if (!array_descriptor)
908 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
909 field->type, field->name, field->offset,
910 field->size, !!field->is_signed);
911 else
912 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
913 (int)(array_descriptor - field->type),
914 field->type, field->name,
915 array_descriptor, field->offset,
916 field->size, !!field->is_signed);
918 return 0;
921 static void *f_start(struct seq_file *m, loff_t *pos)
923 void *p = (void *)FORMAT_HEADER;
924 loff_t l = 0;
926 /* ->stop() is called even if ->start() fails */
927 mutex_lock(&event_mutex);
928 if (!event_file_data(m->private))
929 return ERR_PTR(-ENODEV);
931 while (l < *pos && p)
932 p = f_next(m, p, &l);
934 return p;
937 static void f_stop(struct seq_file *m, void *p)
939 mutex_unlock(&event_mutex);
942 static const struct seq_operations trace_format_seq_ops = {
943 .start = f_start,
944 .next = f_next,
945 .stop = f_stop,
946 .show = f_show,
949 static int trace_format_open(struct inode *inode, struct file *file)
951 struct seq_file *m;
952 int ret;
954 ret = seq_open(file, &trace_format_seq_ops);
955 if (ret < 0)
956 return ret;
958 m = file->private_data;
959 m->private = file;
961 return 0;
964 static ssize_t
965 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
967 int id = (long)event_file_data(filp);
968 char buf[32];
969 int len;
971 if (*ppos)
972 return 0;
974 if (unlikely(!id))
975 return -ENODEV;
977 len = sprintf(buf, "%d\n", id);
979 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
982 static ssize_t
983 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
984 loff_t *ppos)
986 struct ftrace_event_call *call;
987 struct trace_seq *s;
988 int r = -ENODEV;
990 if (*ppos)
991 return 0;
993 s = kmalloc(sizeof(*s), GFP_KERNEL);
995 if (!s)
996 return -ENOMEM;
998 trace_seq_init(s);
1000 mutex_lock(&event_mutex);
1001 call = event_file_data(filp);
1002 if (call)
1003 print_event_filter(call, s);
1004 mutex_unlock(&event_mutex);
1006 if (call)
1007 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1009 kfree(s);
1011 return r;
1014 static ssize_t
1015 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1016 loff_t *ppos)
1018 struct ftrace_event_call *call;
1019 char *buf;
1020 int err = -ENODEV;
1022 if (cnt >= PAGE_SIZE)
1023 return -EINVAL;
1025 buf = (char *)__get_free_page(GFP_TEMPORARY);
1026 if (!buf)
1027 return -ENOMEM;
1029 if (copy_from_user(buf, ubuf, cnt)) {
1030 free_page((unsigned long) buf);
1031 return -EFAULT;
1033 buf[cnt] = '\0';
1035 mutex_lock(&event_mutex);
1036 call = event_file_data(filp);
1037 if (call)
1038 err = apply_event_filter(call, buf);
1039 mutex_unlock(&event_mutex);
1041 free_page((unsigned long) buf);
1042 if (err < 0)
1043 return err;
1045 *ppos += cnt;
1047 return cnt;
1050 static LIST_HEAD(event_subsystems);
1052 static int subsystem_open(struct inode *inode, struct file *filp)
1054 struct event_subsystem *system = NULL;
1055 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1056 struct trace_array *tr;
1057 int ret;
1059 /* Make sure the system still exists */
1060 mutex_lock(&trace_types_lock);
1061 mutex_lock(&event_mutex);
1062 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1063 list_for_each_entry(dir, &tr->systems, list) {
1064 if (dir == inode->i_private) {
1065 /* Don't open systems with no events */
1066 if (dir->nr_events) {
1067 __get_system_dir(dir);
1068 system = dir->subsystem;
1070 goto exit_loop;
1074 exit_loop:
1075 mutex_unlock(&event_mutex);
1076 mutex_unlock(&trace_types_lock);
1078 if (!system)
1079 return -ENODEV;
1081 /* Some versions of gcc think dir can be uninitialized here */
1082 WARN_ON(!dir);
1084 /* Still need to increment the ref count of the system */
1085 if (trace_array_get(tr) < 0) {
1086 put_system(dir);
1087 return -ENODEV;
1090 ret = tracing_open_generic(inode, filp);
1091 if (ret < 0) {
1092 trace_array_put(tr);
1093 put_system(dir);
1096 return ret;
1099 static int system_tr_open(struct inode *inode, struct file *filp)
1101 struct ftrace_subsystem_dir *dir;
1102 struct trace_array *tr = inode->i_private;
1103 int ret;
1105 if (trace_array_get(tr) < 0)
1106 return -ENODEV;
1108 /* Make a temporary dir that has no system but points to tr */
1109 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1110 if (!dir) {
1111 trace_array_put(tr);
1112 return -ENOMEM;
1115 dir->tr = tr;
1117 ret = tracing_open_generic(inode, filp);
1118 if (ret < 0) {
1119 trace_array_put(tr);
1120 kfree(dir);
1123 filp->private_data = dir;
1125 return ret;
1128 static int subsystem_release(struct inode *inode, struct file *file)
1130 struct ftrace_subsystem_dir *dir = file->private_data;
1132 trace_array_put(dir->tr);
1135 * If dir->subsystem is NULL, then this is a temporary
1136 * descriptor that was made for a trace_array to enable
1137 * all subsystems.
1139 if (dir->subsystem)
1140 put_system(dir);
1141 else
1142 kfree(dir);
1144 return 0;
1147 static ssize_t
1148 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1149 loff_t *ppos)
1151 struct ftrace_subsystem_dir *dir = filp->private_data;
1152 struct event_subsystem *system = dir->subsystem;
1153 struct trace_seq *s;
1154 int r;
1156 if (*ppos)
1157 return 0;
1159 s = kmalloc(sizeof(*s), GFP_KERNEL);
1160 if (!s)
1161 return -ENOMEM;
1163 trace_seq_init(s);
1165 print_subsystem_event_filter(system, s);
1166 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1168 kfree(s);
1170 return r;
1173 static ssize_t
1174 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1175 loff_t *ppos)
1177 struct ftrace_subsystem_dir *dir = filp->private_data;
1178 char *buf;
1179 int err;
1181 if (cnt >= PAGE_SIZE)
1182 return -EINVAL;
1184 buf = (char *)__get_free_page(GFP_TEMPORARY);
1185 if (!buf)
1186 return -ENOMEM;
1188 if (copy_from_user(buf, ubuf, cnt)) {
1189 free_page((unsigned long) buf);
1190 return -EFAULT;
1192 buf[cnt] = '\0';
1194 err = apply_subsystem_event_filter(dir, buf);
1195 free_page((unsigned long) buf);
1196 if (err < 0)
1197 return err;
1199 *ppos += cnt;
1201 return cnt;
1204 static ssize_t
1205 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1207 int (*func)(struct trace_seq *s) = filp->private_data;
1208 struct trace_seq *s;
1209 int r;
1211 if (*ppos)
1212 return 0;
1214 s = kmalloc(sizeof(*s), GFP_KERNEL);
1215 if (!s)
1216 return -ENOMEM;
1218 trace_seq_init(s);
1220 func(s);
1221 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1223 kfree(s);
1225 return r;
1228 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1229 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1230 static int ftrace_event_release(struct inode *inode, struct file *file);
1232 static const struct seq_operations show_event_seq_ops = {
1233 .start = t_start,
1234 .next = t_next,
1235 .show = t_show,
1236 .stop = t_stop,
1239 static const struct seq_operations show_set_event_seq_ops = {
1240 .start = s_start,
1241 .next = s_next,
1242 .show = t_show,
1243 .stop = t_stop,
1246 static const struct file_operations ftrace_avail_fops = {
1247 .open = ftrace_event_avail_open,
1248 .read = seq_read,
1249 .llseek = seq_lseek,
1250 .release = seq_release,
1253 static const struct file_operations ftrace_set_event_fops = {
1254 .open = ftrace_event_set_open,
1255 .read = seq_read,
1256 .write = ftrace_event_write,
1257 .llseek = seq_lseek,
1258 .release = ftrace_event_release,
1261 static const struct file_operations ftrace_enable_fops = {
1262 .open = tracing_open_generic,
1263 .read = event_enable_read,
1264 .write = event_enable_write,
1265 .llseek = default_llseek,
1268 static const struct file_operations ftrace_event_format_fops = {
1269 .open = trace_format_open,
1270 .read = seq_read,
1271 .llseek = seq_lseek,
1272 .release = seq_release,
1275 static const struct file_operations ftrace_event_id_fops = {
1276 .read = event_id_read,
1277 .llseek = default_llseek,
1280 static const struct file_operations ftrace_event_filter_fops = {
1281 .open = tracing_open_generic,
1282 .read = event_filter_read,
1283 .write = event_filter_write,
1284 .llseek = default_llseek,
1287 static const struct file_operations ftrace_subsystem_filter_fops = {
1288 .open = subsystem_open,
1289 .read = subsystem_filter_read,
1290 .write = subsystem_filter_write,
1291 .llseek = default_llseek,
1292 .release = subsystem_release,
1295 static const struct file_operations ftrace_system_enable_fops = {
1296 .open = subsystem_open,
1297 .read = system_enable_read,
1298 .write = system_enable_write,
1299 .llseek = default_llseek,
1300 .release = subsystem_release,
1303 static const struct file_operations ftrace_tr_enable_fops = {
1304 .open = system_tr_open,
1305 .read = system_enable_read,
1306 .write = system_enable_write,
1307 .llseek = default_llseek,
1308 .release = subsystem_release,
1311 static const struct file_operations ftrace_show_header_fops = {
1312 .open = tracing_open_generic,
1313 .read = show_header,
1314 .llseek = default_llseek,
1317 static int
1318 ftrace_event_open(struct inode *inode, struct file *file,
1319 const struct seq_operations *seq_ops)
1321 struct seq_file *m;
1322 int ret;
1324 ret = seq_open(file, seq_ops);
1325 if (ret < 0)
1326 return ret;
1327 m = file->private_data;
1328 /* copy tr over to seq ops */
1329 m->private = inode->i_private;
1331 return ret;
1334 static int ftrace_event_release(struct inode *inode, struct file *file)
1336 struct trace_array *tr = inode->i_private;
1338 trace_array_put(tr);
1340 return seq_release(inode, file);
1343 static int
1344 ftrace_event_avail_open(struct inode *inode, struct file *file)
1346 const struct seq_operations *seq_ops = &show_event_seq_ops;
1348 return ftrace_event_open(inode, file, seq_ops);
1351 static int
1352 ftrace_event_set_open(struct inode *inode, struct file *file)
1354 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1355 struct trace_array *tr = inode->i_private;
1356 int ret;
1358 if (trace_array_get(tr) < 0)
1359 return -ENODEV;
1361 if ((file->f_mode & FMODE_WRITE) &&
1362 (file->f_flags & O_TRUNC))
1363 ftrace_clear_events(tr);
1365 ret = ftrace_event_open(inode, file, seq_ops);
1366 if (ret < 0)
1367 trace_array_put(tr);
1368 return ret;
1371 static struct event_subsystem *
1372 create_new_subsystem(const char *name)
1374 struct event_subsystem *system;
1376 /* need to create new entry */
1377 system = kmalloc(sizeof(*system), GFP_KERNEL);
1378 if (!system)
1379 return NULL;
1381 system->ref_count = 1;
1383 /* Only allocate if dynamic (kprobes and modules) */
1384 if (!core_kernel_data((unsigned long)name)) {
1385 system->ref_count |= SYSTEM_FL_FREE_NAME;
1386 system->name = kstrdup(name, GFP_KERNEL);
1387 if (!system->name)
1388 goto out_free;
1389 } else
1390 system->name = name;
1392 system->filter = NULL;
1394 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1395 if (!system->filter)
1396 goto out_free;
1398 list_add(&system->list, &event_subsystems);
1400 return system;
1402 out_free:
1403 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1404 kfree(system->name);
1405 kfree(system);
1406 return NULL;
1409 static struct dentry *
1410 event_subsystem_dir(struct trace_array *tr, const char *name,
1411 struct ftrace_event_file *file, struct dentry *parent)
1413 struct ftrace_subsystem_dir *dir;
1414 struct event_subsystem *system;
1415 struct dentry *entry;
1417 /* First see if we did not already create this dir */
1418 list_for_each_entry(dir, &tr->systems, list) {
1419 system = dir->subsystem;
1420 if (strcmp(system->name, name) == 0) {
1421 dir->nr_events++;
1422 file->system = dir;
1423 return dir->entry;
1427 /* Now see if the system itself exists. */
1428 list_for_each_entry(system, &event_subsystems, list) {
1429 if (strcmp(system->name, name) == 0)
1430 break;
1432 /* Reset system variable when not found */
1433 if (&system->list == &event_subsystems)
1434 system = NULL;
1436 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1437 if (!dir)
1438 goto out_fail;
1440 if (!system) {
1441 system = create_new_subsystem(name);
1442 if (!system)
1443 goto out_free;
1444 } else
1445 __get_system(system);
1447 dir->entry = debugfs_create_dir(name, parent);
1448 if (!dir->entry) {
1449 pr_warning("Failed to create system directory %s\n", name);
1450 __put_system(system);
1451 goto out_free;
1454 dir->tr = tr;
1455 dir->ref_count = 1;
1456 dir->nr_events = 1;
1457 dir->subsystem = system;
1458 file->system = dir;
1460 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1461 &ftrace_subsystem_filter_fops);
1462 if (!entry) {
1463 kfree(system->filter);
1464 system->filter = NULL;
1465 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1468 trace_create_file("enable", 0644, dir->entry, dir,
1469 &ftrace_system_enable_fops);
1471 list_add(&dir->list, &tr->systems);
1473 return dir->entry;
1475 out_free:
1476 kfree(dir);
1477 out_fail:
1478 /* Only print this message if failed on memory allocation */
1479 if (!dir || !system)
1480 pr_warning("No memory to create event subsystem %s\n",
1481 name);
1482 return NULL;
1485 static int
1486 event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1488 struct ftrace_event_call *call = file->event_call;
1489 struct trace_array *tr = file->tr;
1490 struct list_head *head;
1491 struct dentry *d_events;
1492 int ret;
1495 * If the trace point header did not define TRACE_SYSTEM
1496 * then the system would be called "TRACE_SYSTEM".
1498 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1499 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1500 if (!d_events)
1501 return -ENOMEM;
1502 } else
1503 d_events = parent;
1505 file->dir = debugfs_create_dir(call->name, d_events);
1506 if (!file->dir) {
1507 pr_warning("Could not create debugfs '%s' directory\n",
1508 call->name);
1509 return -1;
1512 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1513 trace_create_file("enable", 0644, file->dir, file,
1514 &ftrace_enable_fops);
1516 #ifdef CONFIG_PERF_EVENTS
1517 if (call->event.type && call->class->reg)
1518 trace_create_file("id", 0444, file->dir,
1519 (void *)(long)call->event.type,
1520 &ftrace_event_id_fops);
1521 #endif
1524 * Other events may have the same class. Only update
1525 * the fields if they are not already defined.
1527 head = trace_get_fields(call);
1528 if (list_empty(head)) {
1529 ret = call->class->define_fields(call);
1530 if (ret < 0) {
1531 pr_warning("Could not initialize trace point"
1532 " events/%s\n", call->name);
1533 return -1;
1536 trace_create_file("filter", 0644, file->dir, call,
1537 &ftrace_event_filter_fops);
1539 trace_create_file("format", 0444, file->dir, call,
1540 &ftrace_event_format_fops);
1542 return 0;
1545 static void remove_event_from_tracers(struct ftrace_event_call *call)
1547 struct ftrace_event_file *file;
1548 struct trace_array *tr;
1550 do_for_each_event_file_safe(tr, file) {
1551 if (file->event_call != call)
1552 continue;
1554 remove_event_file_dir(file);
1556 * The do_for_each_event_file_safe() is
1557 * a double loop. After finding the call for this
1558 * trace_array, we use break to jump to the next
1559 * trace_array.
1561 break;
1562 } while_for_each_event_file();
1565 static void event_remove(struct ftrace_event_call *call)
1567 struct trace_array *tr;
1568 struct ftrace_event_file *file;
1570 do_for_each_event_file(tr, file) {
1571 if (file->event_call != call)
1572 continue;
1573 ftrace_event_enable_disable(file, 0);
1575 * The do_for_each_event_file() is
1576 * a double loop. After finding the call for this
1577 * trace_array, we use break to jump to the next
1578 * trace_array.
1580 break;
1581 } while_for_each_event_file();
1583 if (call->event.funcs)
1584 __unregister_ftrace_event(&call->event);
1585 remove_event_from_tracers(call);
1586 list_del(&call->list);
1589 static int event_init(struct ftrace_event_call *call)
1591 int ret = 0;
1593 if (WARN_ON(!call->name))
1594 return -EINVAL;
1596 if (call->class->raw_init) {
1597 ret = call->class->raw_init(call);
1598 if (ret < 0 && ret != -ENOSYS)
1599 pr_warn("Could not initialize trace events/%s\n",
1600 call->name);
1603 return ret;
1606 static int
1607 __register_event(struct ftrace_event_call *call, struct module *mod)
1609 int ret;
1611 ret = event_init(call);
1612 if (ret < 0)
1613 return ret;
1615 list_add(&call->list, &ftrace_events);
1616 call->mod = mod;
1618 return 0;
1621 static struct ftrace_event_file *
1622 trace_create_new_event(struct ftrace_event_call *call,
1623 struct trace_array *tr)
1625 struct ftrace_event_file *file;
1627 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1628 if (!file)
1629 return NULL;
1631 file->event_call = call;
1632 file->tr = tr;
1633 atomic_set(&file->sm_ref, 0);
1634 list_add(&file->list, &tr->events);
1636 return file;
1639 /* Add an event to a trace directory */
1640 static int
1641 __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1643 struct ftrace_event_file *file;
1645 file = trace_create_new_event(call, tr);
1646 if (!file)
1647 return -ENOMEM;
1649 return event_create_dir(tr->event_dir, file);
1653 * Just create a decriptor for early init. A descriptor is required
1654 * for enabling events at boot. We want to enable events before
1655 * the filesystem is initialized.
1657 static __init int
1658 __trace_early_add_new_event(struct ftrace_event_call *call,
1659 struct trace_array *tr)
1661 struct ftrace_event_file *file;
1663 file = trace_create_new_event(call, tr);
1664 if (!file)
1665 return -ENOMEM;
1667 return 0;
1670 struct ftrace_module_file_ops;
1671 static void __add_event_to_tracers(struct ftrace_event_call *call);
1673 /* Add an additional event_call dynamically */
1674 int trace_add_event_call(struct ftrace_event_call *call)
1676 int ret;
1677 mutex_lock(&trace_types_lock);
1678 mutex_lock(&event_mutex);
1680 ret = __register_event(call, NULL);
1681 if (ret >= 0)
1682 __add_event_to_tracers(call);
1684 mutex_unlock(&event_mutex);
1685 mutex_unlock(&trace_types_lock);
1686 return ret;
1690 * Must be called under locking of trace_types_lock, event_mutex and
1691 * trace_event_sem.
1693 static void __trace_remove_event_call(struct ftrace_event_call *call)
1695 event_remove(call);
1696 trace_destroy_fields(call);
1697 destroy_preds(call);
1700 static int probe_remove_event_call(struct ftrace_event_call *call)
1702 struct trace_array *tr;
1703 struct ftrace_event_file *file;
1705 #ifdef CONFIG_PERF_EVENTS
1706 if (call->perf_refcount)
1707 return -EBUSY;
1708 #endif
1709 do_for_each_event_file(tr, file) {
1710 if (file->event_call != call)
1711 continue;
1713 * We can't rely on ftrace_event_enable_disable(enable => 0)
1714 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1715 * TRACE_REG_UNREGISTER.
1717 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1718 return -EBUSY;
1720 * The do_for_each_event_file_safe() is
1721 * a double loop. After finding the call for this
1722 * trace_array, we use break to jump to the next
1723 * trace_array.
1725 break;
1726 } while_for_each_event_file();
1728 __trace_remove_event_call(call);
1730 return 0;
1733 /* Remove an event_call */
1734 int trace_remove_event_call(struct ftrace_event_call *call)
1736 int ret;
1738 mutex_lock(&trace_types_lock);
1739 mutex_lock(&event_mutex);
1740 down_write(&trace_event_sem);
1741 ret = probe_remove_event_call(call);
1742 up_write(&trace_event_sem);
1743 mutex_unlock(&event_mutex);
1744 mutex_unlock(&trace_types_lock);
1746 return ret;
1749 #define for_each_event(event, start, end) \
1750 for (event = start; \
1751 (unsigned long)event < (unsigned long)end; \
1752 event++)
1754 #ifdef CONFIG_MODULES
1756 static void trace_module_add_events(struct module *mod)
1758 struct ftrace_event_call **call, **start, **end;
1760 if (!mod->num_trace_events)
1761 return;
1763 /* Don't add infrastructure for mods without tracepoints */
1764 if (trace_module_has_bad_taint(mod)) {
1765 pr_err("%s: module has bad taint, not creating trace events\n",
1766 mod->name);
1767 return;
1770 start = mod->trace_events;
1771 end = mod->trace_events + mod->num_trace_events;
1773 for_each_event(call, start, end) {
1774 __register_event(*call, mod);
1775 __add_event_to_tracers(*call);
1779 static void trace_module_remove_events(struct module *mod)
1781 struct ftrace_event_call *call, *p;
1782 bool clear_trace = false;
1784 down_write(&trace_event_sem);
1785 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1786 if (call->mod == mod) {
1787 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1788 clear_trace = true;
1789 __trace_remove_event_call(call);
1792 up_write(&trace_event_sem);
1795 * It is safest to reset the ring buffer if the module being unloaded
1796 * registered any events that were used. The only worry is if
1797 * a new module gets loaded, and takes on the same id as the events
1798 * of this module. When printing out the buffer, traced events left
1799 * over from this module may be passed to the new module events and
1800 * unexpected results may occur.
1802 if (clear_trace)
1803 tracing_reset_all_online_cpus();
1806 static int trace_module_notify(struct notifier_block *self,
1807 unsigned long val, void *data)
1809 struct module *mod = data;
1811 mutex_lock(&trace_types_lock);
1812 mutex_lock(&event_mutex);
1813 switch (val) {
1814 case MODULE_STATE_COMING:
1815 trace_module_add_events(mod);
1816 break;
1817 case MODULE_STATE_GOING:
1818 trace_module_remove_events(mod);
1819 break;
1821 mutex_unlock(&event_mutex);
1822 mutex_unlock(&trace_types_lock);
1824 return 0;
1827 static struct notifier_block trace_module_nb = {
1828 .notifier_call = trace_module_notify,
1829 .priority = 0,
1831 #endif /* CONFIG_MODULES */
1833 /* Create a new event directory structure for a trace directory. */
1834 static void
1835 __trace_add_event_dirs(struct trace_array *tr)
1837 struct ftrace_event_call *call;
1838 int ret;
1840 list_for_each_entry(call, &ftrace_events, list) {
1841 ret = __trace_add_new_event(call, tr);
1842 if (ret < 0)
1843 pr_warning("Could not create directory for event %s\n",
1844 call->name);
1848 #ifdef CONFIG_DYNAMIC_FTRACE
1850 /* Avoid typos */
1851 #define ENABLE_EVENT_STR "enable_event"
1852 #define DISABLE_EVENT_STR "disable_event"
1854 struct event_probe_data {
1855 struct ftrace_event_file *file;
1856 unsigned long count;
1857 int ref;
1858 bool enable;
1861 static struct ftrace_event_file *
1862 find_event_file(struct trace_array *tr, const char *system, const char *event)
1864 struct ftrace_event_file *file;
1865 struct ftrace_event_call *call;
1867 list_for_each_entry(file, &tr->events, list) {
1869 call = file->event_call;
1871 if (!call->name || !call->class || !call->class->reg)
1872 continue;
1874 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1875 continue;
1877 if (strcmp(event, call->name) == 0 &&
1878 strcmp(system, call->class->system) == 0)
1879 return file;
1881 return NULL;
1884 static void
1885 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1887 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1888 struct event_probe_data *data = *pdata;
1890 if (!data)
1891 return;
1893 if (data->enable)
1894 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1895 else
1896 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1899 static void
1900 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1902 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1903 struct event_probe_data *data = *pdata;
1905 if (!data)
1906 return;
1908 if (!data->count)
1909 return;
1911 /* Skip if the event is in a state we want to switch to */
1912 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1913 return;
1915 if (data->count != -1)
1916 (data->count)--;
1918 event_enable_probe(ip, parent_ip, _data);
1921 static int
1922 event_enable_print(struct seq_file *m, unsigned long ip,
1923 struct ftrace_probe_ops *ops, void *_data)
1925 struct event_probe_data *data = _data;
1927 seq_printf(m, "%ps:", (void *)ip);
1929 seq_printf(m, "%s:%s:%s",
1930 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1931 data->file->event_call->class->system,
1932 data->file->event_call->name);
1934 if (data->count == -1)
1935 seq_printf(m, ":unlimited\n");
1936 else
1937 seq_printf(m, ":count=%ld\n", data->count);
1939 return 0;
1942 static int
1943 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1944 void **_data)
1946 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1947 struct event_probe_data *data = *pdata;
1949 data->ref++;
1950 return 0;
1953 static void
1954 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1955 void **_data)
1957 struct event_probe_data **pdata = (struct event_probe_data **)_data;
1958 struct event_probe_data *data = *pdata;
1960 if (WARN_ON_ONCE(data->ref <= 0))
1961 return;
1963 data->ref--;
1964 if (!data->ref) {
1965 /* Remove the SOFT_MODE flag */
1966 __ftrace_event_enable_disable(data->file, 0, 1);
1967 module_put(data->file->event_call->mod);
1968 kfree(data);
1970 *pdata = NULL;
1973 static struct ftrace_probe_ops event_enable_probe_ops = {
1974 .func = event_enable_probe,
1975 .print = event_enable_print,
1976 .init = event_enable_init,
1977 .free = event_enable_free,
1980 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1981 .func = event_enable_count_probe,
1982 .print = event_enable_print,
1983 .init = event_enable_init,
1984 .free = event_enable_free,
1987 static struct ftrace_probe_ops event_disable_probe_ops = {
1988 .func = event_enable_probe,
1989 .print = event_enable_print,
1990 .init = event_enable_init,
1991 .free = event_enable_free,
1994 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1995 .func = event_enable_count_probe,
1996 .print = event_enable_print,
1997 .init = event_enable_init,
1998 .free = event_enable_free,
2001 static int
2002 event_enable_func(struct ftrace_hash *hash,
2003 char *glob, char *cmd, char *param, int enabled)
2005 struct trace_array *tr = top_trace_array();
2006 struct ftrace_event_file *file;
2007 struct ftrace_probe_ops *ops;
2008 struct event_probe_data *data;
2009 const char *system;
2010 const char *event;
2011 char *number;
2012 bool enable;
2013 int ret;
2015 /* hash funcs only work with set_ftrace_filter */
2016 if (!enabled || !param)
2017 return -EINVAL;
2019 system = strsep(&param, ":");
2020 if (!param)
2021 return -EINVAL;
2023 event = strsep(&param, ":");
2025 mutex_lock(&event_mutex);
2027 ret = -EINVAL;
2028 file = find_event_file(tr, system, event);
2029 if (!file)
2030 goto out;
2032 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2034 if (enable)
2035 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2036 else
2037 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2039 if (glob[0] == '!') {
2040 unregister_ftrace_function_probe_func(glob+1, ops);
2041 ret = 0;
2042 goto out;
2045 ret = -ENOMEM;
2046 data = kzalloc(sizeof(*data), GFP_KERNEL);
2047 if (!data)
2048 goto out;
2050 data->enable = enable;
2051 data->count = -1;
2052 data->file = file;
2054 if (!param)
2055 goto out_reg;
2057 number = strsep(&param, ":");
2059 ret = -EINVAL;
2060 if (!strlen(number))
2061 goto out_free;
2064 * We use the callback data field (which is a pointer)
2065 * as our counter.
2067 ret = kstrtoul(number, 0, &data->count);
2068 if (ret)
2069 goto out_free;
2071 out_reg:
2072 /* Don't let event modules unload while probe registered */
2073 ret = try_module_get(file->event_call->mod);
2074 if (!ret) {
2075 ret = -EBUSY;
2076 goto out_free;
2079 ret = __ftrace_event_enable_disable(file, 1, 1);
2080 if (ret < 0)
2081 goto out_put;
2082 ret = register_ftrace_function_probe(glob, ops, data);
2084 * The above returns on success the # of functions enabled,
2085 * but if it didn't find any functions it returns zero.
2086 * Consider no functions a failure too.
2088 if (!ret) {
2089 ret = -ENOENT;
2090 goto out_disable;
2091 } else if (ret < 0)
2092 goto out_disable;
2093 /* Just return zero, not the number of enabled functions */
2094 ret = 0;
2095 out:
2096 mutex_unlock(&event_mutex);
2097 return ret;
2099 out_disable:
2100 __ftrace_event_enable_disable(file, 0, 1);
2101 out_put:
2102 module_put(file->event_call->mod);
2103 out_free:
2104 kfree(data);
2105 goto out;
2108 static struct ftrace_func_command event_enable_cmd = {
2109 .name = ENABLE_EVENT_STR,
2110 .func = event_enable_func,
2113 static struct ftrace_func_command event_disable_cmd = {
2114 .name = DISABLE_EVENT_STR,
2115 .func = event_enable_func,
2118 static __init int register_event_cmds(void)
2120 int ret;
2122 ret = register_ftrace_command(&event_enable_cmd);
2123 if (WARN_ON(ret < 0))
2124 return ret;
2125 ret = register_ftrace_command(&event_disable_cmd);
2126 if (WARN_ON(ret < 0))
2127 unregister_ftrace_command(&event_enable_cmd);
2128 return ret;
2130 #else
2131 static inline int register_event_cmds(void) { return 0; }
2132 #endif /* CONFIG_DYNAMIC_FTRACE */
2135 * The top level array has already had its ftrace_event_file
2136 * descriptors created in order to allow for early events to
2137 * be recorded. This function is called after the debugfs has been
2138 * initialized, and we now have to create the files associated
2139 * to the events.
2141 static __init void
2142 __trace_early_add_event_dirs(struct trace_array *tr)
2144 struct ftrace_event_file *file;
2145 int ret;
2148 list_for_each_entry(file, &tr->events, list) {
2149 ret = event_create_dir(tr->event_dir, file);
2150 if (ret < 0)
2151 pr_warning("Could not create directory for event %s\n",
2152 file->event_call->name);
2157 * For early boot up, the top trace array requires to have
2158 * a list of events that can be enabled. This must be done before
2159 * the filesystem is set up in order to allow events to be traced
2160 * early.
2162 static __init void
2163 __trace_early_add_events(struct trace_array *tr)
2165 struct ftrace_event_call *call;
2166 int ret;
2168 list_for_each_entry(call, &ftrace_events, list) {
2169 /* Early boot up should not have any modules loaded */
2170 if (WARN_ON_ONCE(call->mod))
2171 continue;
2173 ret = __trace_early_add_new_event(call, tr);
2174 if (ret < 0)
2175 pr_warning("Could not create early event %s\n",
2176 call->name);
2180 /* Remove the event directory structure for a trace directory. */
2181 static void
2182 __trace_remove_event_dirs(struct trace_array *tr)
2184 struct ftrace_event_file *file, *next;
2186 list_for_each_entry_safe(file, next, &tr->events, list)
2187 remove_event_file_dir(file);
2190 static void __add_event_to_tracers(struct ftrace_event_call *call)
2192 struct trace_array *tr;
2194 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2195 __trace_add_new_event(call, tr);
2198 extern struct ftrace_event_call *__start_ftrace_events[];
2199 extern struct ftrace_event_call *__stop_ftrace_events[];
2201 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2203 static __init int setup_trace_event(char *str)
2205 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2206 ring_buffer_expanded = true;
2207 tracing_selftest_disabled = true;
2209 return 1;
2211 __setup("trace_event=", setup_trace_event);
2213 /* Expects to have event_mutex held when called */
2214 static int
2215 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2217 struct dentry *d_events;
2218 struct dentry *entry;
2220 entry = debugfs_create_file("set_event", 0644, parent,
2221 tr, &ftrace_set_event_fops);
2222 if (!entry) {
2223 pr_warning("Could not create debugfs 'set_event' entry\n");
2224 return -ENOMEM;
2227 d_events = debugfs_create_dir("events", parent);
2228 if (!d_events) {
2229 pr_warning("Could not create debugfs 'events' directory\n");
2230 return -ENOMEM;
2233 /* ring buffer internal formats */
2234 trace_create_file("header_page", 0444, d_events,
2235 ring_buffer_print_page_header,
2236 &ftrace_show_header_fops);
2238 trace_create_file("header_event", 0444, d_events,
2239 ring_buffer_print_entry_header,
2240 &ftrace_show_header_fops);
2242 trace_create_file("enable", 0644, d_events,
2243 tr, &ftrace_tr_enable_fops);
2245 tr->event_dir = d_events;
2247 return 0;
2251 * event_trace_add_tracer - add a instance of a trace_array to events
2252 * @parent: The parent dentry to place the files/directories for events in
2253 * @tr: The trace array associated with these events
2255 * When a new instance is created, it needs to set up its events
2256 * directory, as well as other files associated with events. It also
2257 * creates the event hierachry in the @parent/events directory.
2259 * Returns 0 on success.
2261 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2263 int ret;
2265 mutex_lock(&event_mutex);
2267 ret = create_event_toplevel_files(parent, tr);
2268 if (ret)
2269 goto out_unlock;
2271 down_write(&trace_event_sem);
2272 __trace_add_event_dirs(tr);
2273 up_write(&trace_event_sem);
2275 out_unlock:
2276 mutex_unlock(&event_mutex);
2278 return ret;
2282 * The top trace array already had its file descriptors created.
2283 * Now the files themselves need to be created.
2285 static __init int
2286 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2288 int ret;
2290 mutex_lock(&event_mutex);
2292 ret = create_event_toplevel_files(parent, tr);
2293 if (ret)
2294 goto out_unlock;
2296 down_write(&trace_event_sem);
2297 __trace_early_add_event_dirs(tr);
2298 up_write(&trace_event_sem);
2300 out_unlock:
2301 mutex_unlock(&event_mutex);
2303 return ret;
2306 int event_trace_del_tracer(struct trace_array *tr)
2308 mutex_lock(&event_mutex);
2310 /* Disable any running events */
2311 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2313 down_write(&trace_event_sem);
2314 __trace_remove_event_dirs(tr);
2315 debugfs_remove_recursive(tr->event_dir);
2316 up_write(&trace_event_sem);
2318 tr->event_dir = NULL;
2320 mutex_unlock(&event_mutex);
2322 return 0;
2325 static __init int event_trace_memsetup(void)
2327 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2328 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2329 return 0;
2332 static __init int event_trace_enable(void)
2334 struct trace_array *tr = top_trace_array();
2335 struct ftrace_event_call **iter, *call;
2336 char *buf = bootup_event_buf;
2337 char *token;
2338 int ret;
2340 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2342 call = *iter;
2343 ret = event_init(call);
2344 if (!ret)
2345 list_add(&call->list, &ftrace_events);
2349 * We need the top trace array to have a working set of trace
2350 * points at early init, before the debug files and directories
2351 * are created. Create the file entries now, and attach them
2352 * to the actual file dentries later.
2354 __trace_early_add_events(tr);
2356 while (true) {
2357 token = strsep(&buf, ",");
2359 if (!token)
2360 break;
2361 if (!*token)
2362 continue;
2364 ret = ftrace_set_clr_event(tr, token, 1);
2365 if (ret)
2366 pr_warn("Failed to enable trace event: %s\n", token);
2369 trace_printk_start_comm();
2371 register_event_cmds();
2373 return 0;
2376 static __init int event_trace_init(void)
2378 struct trace_array *tr;
2379 struct dentry *d_tracer;
2380 struct dentry *entry;
2381 int ret;
2383 tr = top_trace_array();
2385 d_tracer = tracing_init_dentry();
2386 if (!d_tracer)
2387 return 0;
2389 entry = debugfs_create_file("available_events", 0444, d_tracer,
2390 tr, &ftrace_avail_fops);
2391 if (!entry)
2392 pr_warning("Could not create debugfs "
2393 "'available_events' entry\n");
2395 if (trace_define_common_fields())
2396 pr_warning("tracing: Failed to allocate common fields");
2398 ret = early_event_add_tracer(d_tracer, tr);
2399 if (ret)
2400 return ret;
2402 #ifdef CONFIG_MODULES
2403 ret = register_module_notifier(&trace_module_nb);
2404 if (ret)
2405 pr_warning("Failed to register trace events module notifier\n");
2406 #endif
2407 return 0;
2409 early_initcall(event_trace_memsetup);
2410 core_initcall(event_trace_enable);
2411 fs_initcall(event_trace_init);
2413 #ifdef CONFIG_FTRACE_STARTUP_TEST
2415 static DEFINE_SPINLOCK(test_spinlock);
2416 static DEFINE_SPINLOCK(test_spinlock_irq);
2417 static DEFINE_MUTEX(test_mutex);
2419 static __init void test_work(struct work_struct *dummy)
2421 spin_lock(&test_spinlock);
2422 spin_lock_irq(&test_spinlock_irq);
2423 udelay(1);
2424 spin_unlock_irq(&test_spinlock_irq);
2425 spin_unlock(&test_spinlock);
2427 mutex_lock(&test_mutex);
2428 msleep(1);
2429 mutex_unlock(&test_mutex);
2432 static __init int event_test_thread(void *unused)
2434 void *test_malloc;
2436 test_malloc = kmalloc(1234, GFP_KERNEL);
2437 if (!test_malloc)
2438 pr_info("failed to kmalloc\n");
2440 schedule_on_each_cpu(test_work);
2442 kfree(test_malloc);
2444 set_current_state(TASK_INTERRUPTIBLE);
2445 while (!kthread_should_stop())
2446 schedule();
2448 return 0;
2452 * Do various things that may trigger events.
2454 static __init void event_test_stuff(void)
2456 struct task_struct *test_thread;
2458 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2459 msleep(1);
2460 kthread_stop(test_thread);
2464 * For every trace event defined, we will test each trace point separately,
2465 * and then by groups, and finally all trace points.
2467 static __init void event_trace_self_tests(void)
2469 struct ftrace_subsystem_dir *dir;
2470 struct ftrace_event_file *file;
2471 struct ftrace_event_call *call;
2472 struct event_subsystem *system;
2473 struct trace_array *tr;
2474 int ret;
2476 tr = top_trace_array();
2478 pr_info("Running tests on trace events:\n");
2480 list_for_each_entry(file, &tr->events, list) {
2482 call = file->event_call;
2484 /* Only test those that have a probe */
2485 if (!call->class || !call->class->probe)
2486 continue;
2489 * Testing syscall events here is pretty useless, but
2490 * we still do it if configured. But this is time consuming.
2491 * What we really need is a user thread to perform the
2492 * syscalls as we test.
2494 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2495 if (call->class->system &&
2496 strcmp(call->class->system, "syscalls") == 0)
2497 continue;
2498 #endif
2500 pr_info("Testing event %s: ", call->name);
2503 * If an event is already enabled, someone is using
2504 * it and the self test should not be on.
2506 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2507 pr_warning("Enabled event during self test!\n");
2508 WARN_ON_ONCE(1);
2509 continue;
2512 ftrace_event_enable_disable(file, 1);
2513 event_test_stuff();
2514 ftrace_event_enable_disable(file, 0);
2516 pr_cont("OK\n");
2519 /* Now test at the sub system level */
2521 pr_info("Running tests on trace event systems:\n");
2523 list_for_each_entry(dir, &tr->systems, list) {
2525 system = dir->subsystem;
2527 /* the ftrace system is special, skip it */
2528 if (strcmp(system->name, "ftrace") == 0)
2529 continue;
2531 pr_info("Testing event system %s: ", system->name);
2533 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2534 if (WARN_ON_ONCE(ret)) {
2535 pr_warning("error enabling system %s\n",
2536 system->name);
2537 continue;
2540 event_test_stuff();
2542 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2543 if (WARN_ON_ONCE(ret)) {
2544 pr_warning("error disabling system %s\n",
2545 system->name);
2546 continue;
2549 pr_cont("OK\n");
2552 /* Test with all events enabled */
2554 pr_info("Running tests on all trace events:\n");
2555 pr_info("Testing all events: ");
2557 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2558 if (WARN_ON_ONCE(ret)) {
2559 pr_warning("error enabling all events\n");
2560 return;
2563 event_test_stuff();
2565 /* reset sysname */
2566 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2567 if (WARN_ON_ONCE(ret)) {
2568 pr_warning("error disabling all events\n");
2569 return;
2572 pr_cont("OK\n");
2575 #ifdef CONFIG_FUNCTION_TRACER
2577 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2579 static void
2580 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2581 struct ftrace_ops *op, struct pt_regs *pt_regs)
2583 struct ring_buffer_event *event;
2584 struct ring_buffer *buffer;
2585 struct ftrace_entry *entry;
2586 unsigned long flags;
2587 long disabled;
2588 int cpu;
2589 int pc;
2591 pc = preempt_count();
2592 preempt_disable_notrace();
2593 cpu = raw_smp_processor_id();
2594 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2596 if (disabled != 1)
2597 goto out;
2599 local_save_flags(flags);
2601 event = trace_current_buffer_lock_reserve(&buffer,
2602 TRACE_FN, sizeof(*entry),
2603 flags, pc);
2604 if (!event)
2605 goto out;
2606 entry = ring_buffer_event_data(event);
2607 entry->ip = ip;
2608 entry->parent_ip = parent_ip;
2610 trace_buffer_unlock_commit(buffer, event, flags, pc);
2612 out:
2613 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2614 preempt_enable_notrace();
2617 static struct ftrace_ops trace_ops __initdata =
2619 .func = function_test_events_call,
2620 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2623 static __init void event_trace_self_test_with_function(void)
2625 int ret;
2626 ret = register_ftrace_function(&trace_ops);
2627 if (WARN_ON(ret < 0)) {
2628 pr_info("Failed to enable function tracer for event tests\n");
2629 return;
2631 pr_info("Running tests again, along with the function tracer\n");
2632 event_trace_self_tests();
2633 unregister_ftrace_function(&trace_ops);
2635 #else
2636 static __init void event_trace_self_test_with_function(void)
2639 #endif
2641 static __init int event_trace_self_tests_init(void)
2643 if (!tracing_selftest_disabled) {
2644 event_trace_self_tests();
2645 event_trace_self_test_with_function();
2648 return 0;
2651 late_initcall(event_trace_self_tests_init);
2653 #endif