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 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/ctype.h>
16 #include "trace_output.h"
18 #define TRACE_SYSTEM "TRACE_SYSTEM"
20 static DEFINE_MUTEX(event_mutex
);
22 int trace_define_field(struct ftrace_event_call
*call
, char *type
,
23 char *name
, int offset
, int size
)
25 struct ftrace_event_field
*field
;
27 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
31 field
->name
= kstrdup(name
, GFP_KERNEL
);
35 field
->type
= kstrdup(type
, GFP_KERNEL
);
39 field
->offset
= offset
;
41 list_add(&field
->link
, &call
->fields
);
55 static void ftrace_clear_events(void)
57 struct ftrace_event_call
*call
= (void *)__start_ftrace_events
;
60 while ((unsigned long)call
< (unsigned long)__stop_ftrace_events
) {
70 static void ftrace_event_enable_disable(struct ftrace_event_call
*call
,
90 static int ftrace_set_clr_event(char *buf
, int set
)
92 struct ftrace_event_call
*call
= __start_ftrace_events
;
93 char *event
= NULL
, *sub
= NULL
, *match
;
97 * The buf format can be <subsystem>:<event-name>
98 * *:<event-name> means any event by that name.
99 * :<event-name> is the same.
101 * <subsystem>:* means all events in that subsystem
102 * <subsystem>: means the same.
104 * <name> (no ':') means all events in a subsystem with
105 * the name <name> or any event that matches <name>
108 match
= strsep(&buf
, ":");
114 if (!strlen(sub
) || strcmp(sub
, "*") == 0)
116 if (!strlen(event
) || strcmp(event
, "*") == 0)
120 mutex_lock(&event_mutex
);
121 for_each_event(call
) {
123 if (!call
->name
|| !call
->regfunc
)
127 strcmp(match
, call
->name
) != 0 &&
128 strcmp(match
, call
->system
) != 0)
131 if (sub
&& strcmp(sub
, call
->system
) != 0)
134 if (event
&& strcmp(event
, call
->name
) != 0)
137 ftrace_event_enable_disable(call
, set
);
141 mutex_unlock(&event_mutex
);
146 /* 128 should be much more than enough */
147 #define EVENT_BUF_SIZE 127
150 ftrace_event_write(struct file
*file
, const char __user
*ubuf
,
151 size_t cnt
, loff_t
*ppos
)
162 ret
= tracing_update_buffers();
166 ret
= get_user(ch
, ubuf
++);
172 /* skip white space */
173 while (cnt
&& isspace(ch
)) {
174 ret
= get_user(ch
, ubuf
++);
181 /* Only white space found? */
188 buf
= kmalloc(EVENT_BUF_SIZE
+1, GFP_KERNEL
);
192 if (cnt
> EVENT_BUF_SIZE
)
193 cnt
= EVENT_BUF_SIZE
;
196 while (cnt
&& !isspace(ch
)) {
202 ret
= get_user(ch
, ubuf
++);
212 ret
= ftrace_set_clr_event(buf
, set
);
225 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
227 struct ftrace_event_call
*call
= m
->private;
228 struct ftrace_event_call
*next
= call
;
233 if ((unsigned long)call
>= (unsigned long)__stop_ftrace_events
)
237 * The ftrace subsystem is for showing formats only.
238 * They can not be enabled or disabled via the event files.
252 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
254 return t_next(m
, NULL
, pos
);
258 s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
260 struct ftrace_event_call
*call
= m
->private;
261 struct ftrace_event_call
*next
;
266 if ((unsigned long)call
>= (unsigned long)__stop_ftrace_events
)
269 if (!call
->enabled
) {
280 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
282 return s_next(m
, NULL
, pos
);
285 static int t_show(struct seq_file
*m
, void *v
)
287 struct ftrace_event_call
*call
= v
;
289 if (strcmp(call
->system
, TRACE_SYSTEM
) != 0)
290 seq_printf(m
, "%s:", call
->system
);
291 seq_printf(m
, "%s\n", call
->name
);
296 static void t_stop(struct seq_file
*m
, void *p
)
301 ftrace_event_seq_open(struct inode
*inode
, struct file
*file
)
304 const struct seq_operations
*seq_ops
;
306 if ((file
->f_mode
& FMODE_WRITE
) &&
307 !(file
->f_flags
& O_APPEND
))
308 ftrace_clear_events();
310 seq_ops
= inode
->i_private
;
311 ret
= seq_open(file
, seq_ops
);
313 struct seq_file
*m
= file
->private_data
;
315 m
->private = __start_ftrace_events
;
321 event_enable_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
324 struct ftrace_event_call
*call
= filp
->private_data
;
332 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, 2);
336 event_enable_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
339 struct ftrace_event_call
*call
= filp
->private_data
;
344 if (cnt
>= sizeof(buf
))
347 if (copy_from_user(&buf
, ubuf
, cnt
))
352 ret
= strict_strtoul(buf
, 10, &val
);
356 ret
= tracing_update_buffers();
363 mutex_lock(&event_mutex
);
364 ftrace_event_enable_disable(call
, val
);
365 mutex_unlock(&event_mutex
);
378 #define FIELD(type, name) \
379 #type, "common_" #name, offsetof(typeof(field), name), \
382 static int trace_write_header(struct trace_seq
*s
)
384 struct trace_entry field
;
386 /* struct trace_entry */
387 return trace_seq_printf(s
,
388 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
389 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
390 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
391 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
394 FIELD(unsigned char, type
),
395 FIELD(unsigned char, flags
),
396 FIELD(unsigned char, preempt_count
),
402 event_format_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
405 struct ftrace_event_call
*call
= filp
->private_data
;
413 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
419 /* If any of the first writes fail, so will the show_format. */
421 trace_seq_printf(s
, "name: %s\n", call
->name
);
422 trace_seq_printf(s
, "ID: %d\n", call
->id
);
423 trace_seq_printf(s
, "format:\n");
424 trace_write_header(s
);
426 r
= call
->show_format(s
);
429 * ug! The format output is bigger than a PAGE!!
431 buf
= "FORMAT TOO BIG\n";
432 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
437 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
445 event_id_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
, loff_t
*ppos
)
447 struct ftrace_event_call
*call
= filp
->private_data
;
454 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
459 trace_seq_printf(s
, "%d\n", call
->id
);
461 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
,
468 event_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
471 struct ftrace_event_call
*call
= filp
->private_data
;
478 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
484 filter_print_preds(call
->preds
, s
);
485 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
493 event_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
496 struct ftrace_event_call
*call
= filp
->private_data
;
497 char buf
[64], *pbuf
= buf
;
498 struct filter_pred
*pred
;
501 if (cnt
>= sizeof(buf
))
504 if (copy_from_user(&buf
, ubuf
, cnt
))
507 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
511 err
= filter_parse(&pbuf
, pred
);
513 filter_free_pred(pred
);
518 filter_free_preds(call
);
519 filter_free_pred(pred
);
523 if (filter_add_pred(call
, pred
)) {
524 filter_free_pred(pred
);
534 subsystem_filter_read(struct file
*filp
, char __user
*ubuf
, size_t cnt
,
537 struct event_subsystem
*system
= filp
->private_data
;
544 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
550 filter_print_preds(system
->preds
, s
);
551 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, s
->buffer
, s
->len
);
559 subsystem_filter_write(struct file
*filp
, const char __user
*ubuf
, size_t cnt
,
562 struct event_subsystem
*system
= filp
->private_data
;
563 char buf
[64], *pbuf
= buf
;
564 struct filter_pred
*pred
;
567 if (cnt
>= sizeof(buf
))
570 if (copy_from_user(&buf
, ubuf
, cnt
))
573 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
577 err
= filter_parse(&pbuf
, pred
);
579 filter_free_pred(pred
);
584 filter_free_subsystem_preds(system
);
585 filter_free_pred(pred
);
589 if (filter_add_subsystem_pred(system
, pred
)) {
590 filter_free_subsystem_preds(system
);
591 filter_free_pred(pred
);
600 static const struct seq_operations show_event_seq_ops
= {
607 static const struct seq_operations show_set_event_seq_ops
= {
614 static const struct file_operations ftrace_avail_fops
= {
615 .open
= ftrace_event_seq_open
,
618 .release
= seq_release
,
621 static const struct file_operations ftrace_set_event_fops
= {
622 .open
= ftrace_event_seq_open
,
624 .write
= ftrace_event_write
,
626 .release
= seq_release
,
629 static const struct file_operations ftrace_enable_fops
= {
630 .open
= tracing_open_generic
,
631 .read
= event_enable_read
,
632 .write
= event_enable_write
,
635 static const struct file_operations ftrace_event_format_fops
= {
636 .open
= tracing_open_generic
,
637 .read
= event_format_read
,
640 static const struct file_operations ftrace_event_id_fops
= {
641 .open
= tracing_open_generic
,
642 .read
= event_id_read
,
645 static const struct file_operations ftrace_event_filter_fops
= {
646 .open
= tracing_open_generic
,
647 .read
= event_filter_read
,
648 .write
= event_filter_write
,
651 static const struct file_operations ftrace_subsystem_filter_fops
= {
652 .open
= tracing_open_generic
,
653 .read
= subsystem_filter_read
,
654 .write
= subsystem_filter_write
,
657 static struct dentry
*event_trace_events_dir(void)
659 static struct dentry
*d_tracer
;
660 static struct dentry
*d_events
;
665 d_tracer
= tracing_init_dentry();
669 d_events
= debugfs_create_dir("events", d_tracer
);
671 pr_warning("Could not create debugfs "
672 "'events' directory\n");
677 static LIST_HEAD(event_subsystems
);
679 static struct dentry
*
680 event_subsystem_dir(const char *name
, struct dentry
*d_events
)
682 struct event_subsystem
*system
;
683 struct dentry
*entry
;
685 /* First see if we did not already create this dir */
686 list_for_each_entry(system
, &event_subsystems
, list
) {
687 if (strcmp(system
->name
, name
) == 0)
688 return system
->entry
;
691 /* need to create new entry */
692 system
= kmalloc(sizeof(*system
), GFP_KERNEL
);
694 pr_warning("No memory to create event subsystem %s\n",
699 system
->entry
= debugfs_create_dir(name
, d_events
);
700 if (!system
->entry
) {
701 pr_warning("Could not create event subsystem %s\n",
708 list_add(&system
->list
, &event_subsystems
);
710 system
->preds
= NULL
;
712 entry
= debugfs_create_file("filter", 0644, system
->entry
, system
,
713 &ftrace_subsystem_filter_fops
);
715 pr_warning("Could not create debugfs "
716 "'%s/filter' entry\n", name
);
718 return system
->entry
;
722 event_create_dir(struct ftrace_event_call
*call
, struct dentry
*d_events
)
724 struct dentry
*entry
;
728 * If the trace point header did not define TRACE_SYSTEM
729 * then the system would be called "TRACE_SYSTEM".
731 if (strcmp(call
->system
, "TRACE_SYSTEM") != 0)
732 d_events
= event_subsystem_dir(call
->system
, d_events
);
734 if (call
->raw_init
) {
735 ret
= call
->raw_init();
737 pr_warning("Could not initialize trace point"
738 " events/%s\n", call
->name
);
743 call
->dir
= debugfs_create_dir(call
->name
, d_events
);
745 pr_warning("Could not create debugfs "
746 "'%s' directory\n", call
->name
);
751 entry
= debugfs_create_file("enable", 0644, call
->dir
, call
,
752 &ftrace_enable_fops
);
754 pr_warning("Could not create debugfs "
755 "'%s/enable' entry\n", call
->name
);
759 entry
= debugfs_create_file("id", 0444, call
->dir
, call
,
760 &ftrace_event_id_fops
);
762 pr_warning("Could not create debugfs '%s/id' entry\n",
766 if (call
->define_fields
) {
767 ret
= call
->define_fields();
769 pr_warning("Could not initialize trace point"
770 " events/%s\n", call
->name
);
775 entry
= debugfs_create_file("filter", 0644, call
->dir
, call
,
776 &ftrace_event_filter_fops
);
778 pr_warning("Could not create debugfs "
779 "'%s/filter' entry\n", call
->name
);
781 /* A trace may not want to export its format */
782 if (!call
->show_format
)
785 entry
= debugfs_create_file("format", 0444, call
->dir
, call
,
786 &ftrace_event_format_fops
);
788 pr_warning("Could not create debugfs "
789 "'%s/format' entry\n", call
->name
);
794 static __init
int event_trace_init(void)
796 struct ftrace_event_call
*call
= __start_ftrace_events
;
797 struct dentry
*d_tracer
;
798 struct dentry
*entry
;
799 struct dentry
*d_events
;
801 d_tracer
= tracing_init_dentry();
805 entry
= debugfs_create_file("available_events", 0444, d_tracer
,
806 (void *)&show_event_seq_ops
,
809 pr_warning("Could not create debugfs "
810 "'available_events' entry\n");
812 entry
= debugfs_create_file("set_event", 0644, d_tracer
,
813 (void *)&show_set_event_seq_ops
,
814 &ftrace_set_event_fops
);
816 pr_warning("Could not create debugfs "
817 "'set_event' entry\n");
819 d_events
= event_trace_events_dir();
823 for_each_event(call
) {
824 /* The linker may leave blanks */
827 event_create_dir(call
, d_events
);
832 fs_initcall(event_trace_init
);