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 = kmalloc(sizeof(*field), GFP_KERNEL);
30 field->name = kstrdup(name, GFP_KERNEL);
33 field->type = kstrdup(type, GFP_KERNEL);
36 field->offset = offset;
38 list_add(&field->link, &call->fields);
50 static void ftrace_clear_events(void)
52 struct ftrace_event_call *call = (void *)__start_ftrace_events;
55 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
65 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
85 static int ftrace_set_clr_event(char *buf, int set)
87 struct ftrace_event_call *call = __start_ftrace_events;
88 char *event = NULL, *sub = NULL, *match;
92 * The buf format can be <subsystem>:<event-name>
93 * *:<event-name> means any event by that name.
94 * :<event-name> is the same.
96 * <subsystem>:* means all events in that subsystem
97 * <subsystem>: means the same.
99 * <name> (no ':') means all events in a subsystem with
100 * the name <name> or any event that matches <name>
103 match = strsep(&buf, ":");
109 if (!strlen(sub) || strcmp(sub, "*") == 0)
111 if (!strlen(event) || strcmp(event, "*") == 0)
115 mutex_lock(&event_mutex);
116 for_each_event(call) {
118 if (!call->name || !call->regfunc)
122 strcmp(match, call->name) != 0 &&
123 strcmp(match, call->system) != 0)
126 if (sub && strcmp(sub, call->system) != 0)
129 if (event && strcmp(event, call->name) != 0)
132 ftrace_event_enable_disable(call, set);
136 mutex_unlock(&event_mutex);
141 /* 128 should be much more than enough */
142 #define EVENT_BUF_SIZE 127
145 ftrace_event_write(struct file *file, const char __user *ubuf,
146 size_t cnt, loff_t *ppos)
157 ret = tracing_update_buffers();
161 ret = get_user(ch, ubuf++);
167 /* skip white space */
168 while (cnt && isspace(ch)) {
169 ret = get_user(ch, ubuf++);
176 /* Only white space found? */
183 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
187 if (cnt > EVENT_BUF_SIZE)
188 cnt = EVENT_BUF_SIZE;
191 while (cnt && !isspace(ch)) {
197 ret = get_user(ch, ubuf++);
207 ret = ftrace_set_clr_event(buf, set);
220 t_next(struct seq_file *m, void *v, loff_t *pos)
222 struct ftrace_event_call *call = m->private;
223 struct ftrace_event_call *next = call;
228 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
232 * The ftrace subsystem is for showing formats only.
233 * They can not be enabled or disabled via the event files.
247 static void *t_start(struct seq_file *m, loff_t *pos)
249 return t_next(m, NULL, pos);
253 s_next(struct seq_file *m, void *v, loff_t *pos)
255 struct ftrace_event_call *call = m->private;
256 struct ftrace_event_call *next;
261 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
264 if (!call->enabled) {
275 static void *s_start(struct seq_file *m, loff_t *pos)
277 return s_next(m, NULL, pos);
280 static int t_show(struct seq_file *m, void *v)
282 struct ftrace_event_call *call = v;
284 if (strcmp(call->system, TRACE_SYSTEM) != 0)
285 seq_printf(m, "%s:", call->system);
286 seq_printf(m, "%s\n", call->name);
291 static void t_stop(struct seq_file *m, void *p)
296 ftrace_event_seq_open(struct inode *inode, struct file *file)
299 const struct seq_operations *seq_ops;
301 if ((file->f_mode & FMODE_WRITE) &&
302 !(file->f_flags & O_APPEND))
303 ftrace_clear_events();
305 seq_ops = inode->i_private;
306 ret = seq_open(file, seq_ops);
308 struct seq_file *m = file->private_data;
310 m->private = __start_ftrace_events;
316 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
319 struct ftrace_event_call *call = filp->private_data;
327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
331 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
334 struct ftrace_event_call *call = filp->private_data;
339 if (cnt >= sizeof(buf))
342 if (copy_from_user(&buf, ubuf, cnt))
347 ret = strict_strtoul(buf, 10, &val);
351 ret = tracing_update_buffers();
358 mutex_lock(&event_mutex);
359 ftrace_event_enable_disable(call, val);
360 mutex_unlock(&event_mutex);
373 #define FIELD(type, name) \
374 #type, "common_" #name, offsetof(typeof(field), name), \
377 static int trace_write_header(struct trace_seq *s)
379 struct trace_entry field;
381 /* struct trace_entry */
382 return trace_seq_printf(s,
383 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
384 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
385 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
386 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
387 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
389 FIELD(unsigned char, type),
390 FIELD(unsigned char, flags),
391 FIELD(unsigned char, preempt_count),
397 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
400 struct ftrace_event_call *call = filp->private_data;
408 s = kmalloc(sizeof(*s), GFP_KERNEL);
414 /* If any of the first writes fail, so will the show_format. */
416 trace_seq_printf(s, "name: %s\n", call->name);
417 trace_seq_printf(s, "ID: %d\n", call->id);
418 trace_seq_printf(s, "format:\n");
419 trace_write_header(s);
421 r = call->show_format(s);
424 * ug! The format output is bigger than a PAGE!!
426 buf = "FORMAT TOO BIG\n";
427 r = simple_read_from_buffer(ubuf, cnt, ppos,
432 r = simple_read_from_buffer(ubuf, cnt, ppos,
440 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
442 struct ftrace_event_call *call = filp->private_data;
449 s = kmalloc(sizeof(*s), GFP_KERNEL);
454 trace_seq_printf(s, "%d\n", call->id);
456 r = simple_read_from_buffer(ubuf, cnt, ppos,
463 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
466 struct ftrace_event_call *call = filp->private_data;
473 s = kmalloc(sizeof(*s), GFP_KERNEL);
479 r = filter_print_preds(call->preds, s->buffer);
480 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, r);
488 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
491 struct ftrace_event_call *call = filp->private_data;
492 char buf[64], *pbuf = buf;
493 struct filter_pred *pred;
496 if (cnt >= sizeof(buf))
499 if (copy_from_user(&buf, ubuf, cnt))
502 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
506 err = filter_parse(&pbuf, pred);
508 filter_free_pred(pred);
513 filter_free_preds(call);
517 if (filter_add_pred(call, pred)) {
518 filter_free_pred(pred);
527 static const struct seq_operations show_event_seq_ops = {
534 static const struct seq_operations show_set_event_seq_ops = {
541 static const struct file_operations ftrace_avail_fops = {
542 .open = ftrace_event_seq_open,
545 .release = seq_release,
548 static const struct file_operations ftrace_set_event_fops = {
549 .open = ftrace_event_seq_open,
551 .write = ftrace_event_write,
553 .release = seq_release,
556 static const struct file_operations ftrace_enable_fops = {
557 .open = tracing_open_generic,
558 .read = event_enable_read,
559 .write = event_enable_write,
562 static const struct file_operations ftrace_event_format_fops = {
563 .open = tracing_open_generic,
564 .read = event_format_read,
567 static const struct file_operations ftrace_event_id_fops = {
568 .open = tracing_open_generic,
569 .read = event_id_read,
572 static const struct file_operations ftrace_event_filter_fops = {
573 .open = tracing_open_generic,
574 .read = event_filter_read,
575 .write = event_filter_write,
578 static struct dentry *event_trace_events_dir(void)
580 static struct dentry *d_tracer;
581 static struct dentry *d_events;
586 d_tracer = tracing_init_dentry();
590 d_events = debugfs_create_dir("events", d_tracer);
592 pr_warning("Could not create debugfs "
593 "'events' directory\n");
598 struct event_subsystem {
599 struct list_head list;
601 struct dentry *entry;
604 static LIST_HEAD(event_subsystems);
606 static struct dentry *
607 event_subsystem_dir(const char *name, struct dentry *d_events)
609 struct event_subsystem *system;
611 /* First see if we did not already create this dir */
612 list_for_each_entry(system, &event_subsystems, list) {
613 if (strcmp(system->name, name) == 0)
614 return system->entry;
617 /* need to create new entry */
618 system = kmalloc(sizeof(*system), GFP_KERNEL);
620 pr_warning("No memory to create event subsystem %s\n",
625 system->entry = debugfs_create_dir(name, d_events);
626 if (!system->entry) {
627 pr_warning("Could not create event subsystem %s\n",
634 list_add(&system->list, &event_subsystems);
636 return system->entry;
640 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
642 struct dentry *entry;
646 * If the trace point header did not define TRACE_SYSTEM
647 * then the system would be called "TRACE_SYSTEM".
649 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
650 d_events = event_subsystem_dir(call->system, d_events);
652 if (call->raw_init) {
653 ret = call->raw_init();
655 pr_warning("Could not initialize trace point"
656 " events/%s\n", call->name);
661 call->dir = debugfs_create_dir(call->name, d_events);
663 pr_warning("Could not create debugfs "
664 "'%s' directory\n", call->name);
669 entry = debugfs_create_file("enable", 0644, call->dir, call,
670 &ftrace_enable_fops);
672 pr_warning("Could not create debugfs "
673 "'%s/enable' entry\n", call->name);
677 entry = debugfs_create_file("id", 0444, call->dir, call,
678 &ftrace_event_id_fops);
680 pr_warning("Could not create debugfs '%s/id' entry\n",
684 if (call->define_fields) {
685 ret = call->define_fields();
687 pr_warning("Could not initialize trace point"
688 " events/%s\n", call->name);
693 entry = debugfs_create_file("filter", 0444, call->dir, call,
694 &ftrace_event_filter_fops);
696 pr_warning("Could not create debugfs "
697 "'%s/filter' entry\n", call->name);
699 /* A trace may not want to export its format */
700 if (!call->show_format)
703 entry = debugfs_create_file("format", 0444, call->dir, call,
704 &ftrace_event_format_fops);
706 pr_warning("Could not create debugfs "
707 "'%s/format' entry\n", call->name);
712 static __init int event_trace_init(void)
714 struct ftrace_event_call *call = __start_ftrace_events;
715 struct dentry *d_tracer;
716 struct dentry *entry;
717 struct dentry *d_events;
719 d_tracer = tracing_init_dentry();
723 entry = debugfs_create_file("available_events", 0444, d_tracer,
724 (void *)&show_event_seq_ops,
727 pr_warning("Could not create debugfs "
728 "'available_events' entry\n");
730 entry = debugfs_create_file("set_event", 0644, d_tracer,
731 (void *)&show_set_event_seq_ops,
732 &ftrace_set_event_fops);
734 pr_warning("Could not create debugfs "
735 "'set_event' entry\n");
737 d_events = event_trace_events_dir();
741 for_each_event(call) {
742 /* The linker may leave blanks */
745 event_create_dir(call, d_events);
750 fs_initcall(event_trace_init);