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 #define events_for_each(event) \
23 for (event = __start_ftrace_events; \
24 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
27 void event_trace_printk(unsigned long ip, const char *fmt, ...)
32 tracing_record_cmdline(current);
33 trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
37 static void ftrace_clear_events(void)
39 struct ftrace_event_call *call = (void *)__start_ftrace_events;
42 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
52 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
62 if (call->raw_enabled) {
63 call->raw_enabled = 0;
69 (call->type & TRACE_EVENT_TYPE_PRINTF)) {
73 if (!call->raw_enabled &&
74 (call->type & TRACE_EVENT_TYPE_RAW)) {
75 call->raw_enabled = 1;
82 static int ftrace_set_clr_event(char *buf, int set)
84 struct ftrace_event_call *call = __start_ftrace_events;
85 char *event = NULL, *sub = NULL, *match;
89 * The buf format can be <subsystem>:<event-name>
90 * *:<event-name> means any event by that name.
91 * :<event-name> is the same.
93 * <subsystem>:* means all events in that subsystem
94 * <subsystem>: means the same.
96 * <name> (no ':') means all events in a subsystem with
97 * the name <name> or any event that matches <name>
100 match = strsep(&buf, ":");
106 if (!strlen(sub) || strcmp(sub, "*") == 0)
108 if (!strlen(event) || strcmp(event, "*") == 0)
112 mutex_lock(&event_mutex);
113 events_for_each(call) {
119 strcmp(match, call->name) != 0 &&
120 strcmp(match, call->system) != 0)
123 if (sub && strcmp(sub, call->system) != 0)
126 if (event && strcmp(event, call->name) != 0)
129 ftrace_event_enable_disable(call, set);
133 mutex_unlock(&event_mutex);
138 /* 128 should be much more than enough */
139 #define EVENT_BUF_SIZE 127
142 ftrace_event_write(struct file *file, const char __user *ubuf,
143 size_t cnt, loff_t *ppos)
154 ret = get_user(ch, ubuf++);
160 /* skip white space */
161 while (cnt && isspace(ch)) {
162 ret = get_user(ch, ubuf++);
169 /* Only white space found? */
176 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
180 if (cnt > EVENT_BUF_SIZE)
181 cnt = EVENT_BUF_SIZE;
184 while (cnt && !isspace(ch)) {
190 ret = get_user(ch, ubuf++);
200 ret = ftrace_set_clr_event(buf, set);
213 t_next(struct seq_file *m, void *v, loff_t *pos)
215 struct ftrace_event_call *call = m->private;
216 struct ftrace_event_call *next = call;
220 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
228 static void *t_start(struct seq_file *m, loff_t *pos)
230 return t_next(m, NULL, pos);
234 s_next(struct seq_file *m, void *v, loff_t *pos)
236 struct ftrace_event_call *call = m->private;
237 struct ftrace_event_call *next;
242 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
245 if (!call->enabled) {
256 static void *s_start(struct seq_file *m, loff_t *pos)
258 return s_next(m, NULL, pos);
261 static int t_show(struct seq_file *m, void *v)
263 struct ftrace_event_call *call = v;
265 if (strcmp(call->system, TRACE_SYSTEM) != 0)
266 seq_printf(m, "%s:", call->system);
267 seq_printf(m, "%s\n", call->name);
272 static void t_stop(struct seq_file *m, void *p)
277 ftrace_event_seq_open(struct inode *inode, struct file *file)
280 const struct seq_operations *seq_ops;
282 if ((file->f_mode & FMODE_WRITE) &&
283 !(file->f_flags & O_APPEND))
284 ftrace_clear_events();
286 seq_ops = inode->i_private;
287 ret = seq_open(file, seq_ops);
289 struct seq_file *m = file->private_data;
291 m->private = __start_ftrace_events;
297 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
300 struct ftrace_event_call *call = filp->private_data;
303 if (call->enabled || call->raw_enabled)
308 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
312 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
315 struct ftrace_event_call *call = filp->private_data;
320 if (cnt >= sizeof(buf))
323 if (copy_from_user(&buf, ubuf, cnt))
328 ret = strict_strtoul(buf, 10, &val);
335 mutex_lock(&event_mutex);
336 ftrace_event_enable_disable(call, val);
337 mutex_unlock(&event_mutex);
350 event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
353 struct ftrace_event_call *call = filp->private_data;
357 if (call->type & TRACE_EVENT_TYPE_PRINTF)
358 r += sprintf(buf, "printf\n");
360 if (call->type & TRACE_EVENT_TYPE_RAW)
361 r += sprintf(buf+r, "raw\n");
363 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
367 event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
370 struct ftrace_event_call *call = filp->private_data;
374 * If there's only one type, we can't change it.
375 * And currently we always have printf type, and we
376 * may or may not have raw type.
378 * This is a redundant check, the file should be read
379 * only if this is the case anyway.
385 if (cnt >= sizeof(buf))
388 if (copy_from_user(&buf, ubuf, cnt))
393 if (!strncmp(buf, "printf", 6) &&
394 (!buf[6] || isspace(buf[6]))) {
396 call->type = TRACE_EVENT_TYPE_PRINTF;
399 * If raw enabled, the disable it and enable
402 if (call->raw_enabled) {
403 call->raw_enabled = 0;
410 } else if (!strncmp(buf, "raw", 3) &&
411 (!buf[3] || isspace(buf[3]))) {
413 call->type = TRACE_EVENT_TYPE_RAW;
416 * If printf enabled, the disable it and enable
423 call->raw_enabled = 1;
435 event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
438 struct ftrace_event_call *call = filp->private_data;
442 r += sprintf(buf, "printf\n");
445 r += sprintf(buf+r, "raw\n");
447 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
451 #define FIELD(type, name) \
452 #type, #name, offsetof(typeof(field), name), sizeof(field.name)
454 static int trace_write_header(struct trace_seq *s)
456 struct trace_entry field;
458 /* struct trace_entry */
459 return trace_seq_printf(s,
460 "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
461 "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
462 "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
463 "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
464 "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
466 FIELD(unsigned char, type),
467 FIELD(unsigned char, flags),
468 FIELD(unsigned char, preempt_count),
473 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
476 struct ftrace_event_call *call = filp->private_data;
481 s = kmalloc(sizeof(*s), GFP_KERNEL);
490 /* If any of the first writes fail, so will the show_format. */
492 trace_seq_printf(s, "name: %s\n", call->name);
493 trace_seq_printf(s, "ID: %d\n", call->id);
494 trace_seq_printf(s, "format:\n");
495 trace_write_header(s);
497 r = call->show_format(s);
500 * ug! The format output is bigger than a PAGE!!
502 buf = "FORMAT TOO BIG\n";
503 r = simple_read_from_buffer(ubuf, cnt, ppos,
508 r = simple_read_from_buffer(ubuf, cnt, ppos,
515 static const struct seq_operations show_event_seq_ops = {
522 static const struct seq_operations show_set_event_seq_ops = {
529 static const struct file_operations ftrace_avail_fops = {
530 .open = ftrace_event_seq_open,
533 .release = seq_release,
536 static const struct file_operations ftrace_set_event_fops = {
537 .open = ftrace_event_seq_open,
539 .write = ftrace_event_write,
541 .release = seq_release,
544 static const struct file_operations ftrace_enable_fops = {
545 .open = tracing_open_generic,
546 .read = event_enable_read,
547 .write = event_enable_write,
550 static const struct file_operations ftrace_type_fops = {
551 .open = tracing_open_generic,
552 .read = event_type_read,
553 .write = event_type_write,
556 static const struct file_operations ftrace_available_types_fops = {
557 .open = tracing_open_generic,
558 .read = event_available_types_read,
561 static const struct file_operations ftrace_event_format_fops = {
562 .open = tracing_open_generic,
563 .read = event_format_read,
566 static struct dentry *event_trace_events_dir(void)
568 static struct dentry *d_tracer;
569 static struct dentry *d_events;
574 d_tracer = tracing_init_dentry();
578 d_events = debugfs_create_dir("events", d_tracer);
580 pr_warning("Could not create debugfs "
581 "'events' directory\n");
586 struct event_subsystem {
587 struct list_head list;
589 struct dentry *entry;
592 static LIST_HEAD(event_subsystems);
594 static struct dentry *
595 event_subsystem_dir(const char *name, struct dentry *d_events)
597 struct event_subsystem *system;
599 /* First see if we did not already create this dir */
600 list_for_each_entry(system, &event_subsystems, list) {
601 if (strcmp(system->name, name) == 0)
602 return system->entry;
605 /* need to create new entry */
606 system = kmalloc(sizeof(*system), GFP_KERNEL);
608 pr_warning("No memory to create event subsystem %s\n",
613 system->entry = debugfs_create_dir(name, d_events);
614 if (!system->entry) {
615 pr_warning("Could not create event subsystem %s\n",
622 list_add(&system->list, &event_subsystems);
624 return system->entry;
628 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
630 struct dentry *entry;
634 * If the trace point header did not define TRACE_SYSTEM
635 * then the system would be called "TRACE_SYSTEM".
637 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
638 d_events = event_subsystem_dir(call->system, d_events);
640 if (call->raw_init) {
641 ret = call->raw_init();
643 pr_warning("Could not initialize trace point"
644 " events/%s\n", call->name);
649 /* default the output to printf */
650 call->type = TRACE_EVENT_TYPE_PRINTF;
652 call->dir = debugfs_create_dir(call->name, d_events);
654 pr_warning("Could not create debugfs "
655 "'%s' directory\n", call->name);
659 entry = debugfs_create_file("enable", 0644, call->dir, call,
660 &ftrace_enable_fops);
662 pr_warning("Could not create debugfs "
663 "'%s/enable' entry\n", call->name);
665 /* Only let type be writable, if we can change it */
666 entry = debugfs_create_file("type",
667 call->raw_init ? 0644 : 0444,
671 pr_warning("Could not create debugfs "
672 "'%s/type' entry\n", call->name);
674 entry = debugfs_create_file("available_types", 0444, call->dir, call,
675 &ftrace_available_types_fops);
677 pr_warning("Could not create debugfs "
678 "'%s/available_types' entry\n", call->name);
680 /* A trace may not want to export its format */
681 if (!call->show_format)
684 entry = debugfs_create_file("format", 0444, call->dir, call,
685 &ftrace_event_format_fops);
687 pr_warning("Could not create debugfs "
688 "'%s/format' entry\n", call->name);
693 static __init int event_trace_init(void)
695 struct ftrace_event_call *call = __start_ftrace_events;
696 struct dentry *d_tracer;
697 struct dentry *entry;
698 struct dentry *d_events;
700 d_tracer = tracing_init_dentry();
704 entry = debugfs_create_file("available_events", 0444, d_tracer,
705 (void *)&show_event_seq_ops,
708 pr_warning("Could not create debugfs "
709 "'available_events' entry\n");
711 entry = debugfs_create_file("set_event", 0644, d_tracer,
712 (void *)&show_set_event_seq_ops,
713 &ftrace_set_event_fops);
715 pr_warning("Could not create debugfs "
716 "'set_event' entry\n");
718 d_events = event_trace_events_dir();
722 events_for_each(call) {
723 /* The linker may leave blanks */
726 event_create_dir(call, d_events);
731 fs_initcall(event_trace_init);