4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
8 #include <linux/debugfs.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/ctype.h>
15 #define TRACE_SYSTEM "TRACE_SYSTEM"
17 #define events_for_each(event) \
18 for (event = __start_ftrace_events; \
19 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
22 void event_trace_printk(unsigned long ip, const char *fmt, ...)
27 tracing_record_cmdline(current);
28 trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
32 static void ftrace_clear_events(void)
34 struct ftrace_event_call *call = (void *)__start_ftrace_events;
37 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
47 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
57 if (call->raw_enabled) {
58 call->raw_enabled = 0;
64 (call->type & TRACE_EVENT_TYPE_PRINTF)) {
68 if (!call->raw_enabled &&
69 (call->type & TRACE_EVENT_TYPE_RAW)) {
70 call->raw_enabled = 1;
77 static int ftrace_set_clr_event(char *buf, int set)
79 struct ftrace_event_call *call = __start_ftrace_events;
80 char *event = NULL, *sub = NULL, *match;
84 * The buf format can be <subsystem>:<event-name>
85 * *:<event-name> means any event by that name.
86 * :<event-name> is the same.
88 * <subsystem>:* means all events in that subsystem
89 * <subsystem>: means the same.
91 * <name> (no ':') means all events in a subsystem with
92 * the name <name> or any event that matches <name>
95 match = strsep(&buf, ":");
101 if (!strlen(sub) || strcmp(sub, "*") == 0)
103 if (!strlen(event) || strcmp(event, "*") == 0)
107 events_for_each(call) {
113 strcmp(match, call->name) != 0 &&
114 strcmp(match, call->system) != 0)
117 if (sub && strcmp(sub, call->system) != 0)
120 if (event && strcmp(event, call->name) != 0)
123 ftrace_event_enable_disable(call, set);
130 /* 128 should be much more than enough */
131 #define EVENT_BUF_SIZE 127
134 ftrace_event_write(struct file *file, const char __user *ubuf,
135 size_t cnt, loff_t *ppos)
146 ret = get_user(ch, ubuf++);
152 /* skip white space */
153 while (cnt && isspace(ch)) {
154 ret = get_user(ch, ubuf++);
161 /* Only white space found? */
168 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
172 if (cnt > EVENT_BUF_SIZE)
173 cnt = EVENT_BUF_SIZE;
176 while (cnt && !isspace(ch)) {
182 ret = get_user(ch, ubuf++);
192 ret = ftrace_set_clr_event(buf, set);
205 t_next(struct seq_file *m, void *v, loff_t *pos)
207 struct ftrace_event_call *call = m->private;
208 struct ftrace_event_call *next = call;
212 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
220 static void *t_start(struct seq_file *m, loff_t *pos)
222 return t_next(m, NULL, pos);
226 s_next(struct seq_file *m, void *v, loff_t *pos)
228 struct ftrace_event_call *call = m->private;
229 struct ftrace_event_call *next;
234 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
237 if (!call->enabled) {
248 static void *s_start(struct seq_file *m, loff_t *pos)
250 return s_next(m, NULL, pos);
253 static int t_show(struct seq_file *m, void *v)
255 struct ftrace_event_call *call = v;
257 if (strcmp(call->system, TRACE_SYSTEM) != 0)
258 seq_printf(m, "%s:", call->system);
259 seq_printf(m, "%s\n", call->name);
264 static void t_stop(struct seq_file *m, void *p)
269 ftrace_event_seq_open(struct inode *inode, struct file *file)
272 const struct seq_operations *seq_ops;
274 if ((file->f_mode & FMODE_WRITE) &&
275 !(file->f_flags & O_APPEND))
276 ftrace_clear_events();
278 seq_ops = inode->i_private;
279 ret = seq_open(file, seq_ops);
281 struct seq_file *m = file->private_data;
283 m->private = __start_ftrace_events;
289 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
292 struct ftrace_event_call *call = filp->private_data;
295 if (call->enabled || call->raw_enabled)
300 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
304 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
307 struct ftrace_event_call *call = filp->private_data;
312 if (cnt >= sizeof(buf))
315 if (copy_from_user(&buf, ubuf, cnt))
320 ret = strict_strtoul(buf, 10, &val);
327 ftrace_event_enable_disable(call, val);
340 event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
343 struct ftrace_event_call *call = filp->private_data;
347 if (call->type & TRACE_EVENT_TYPE_PRINTF)
348 r += sprintf(buf, "printf\n");
350 if (call->type & TRACE_EVENT_TYPE_RAW)
351 r += sprintf(buf+r, "raw\n");
353 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
357 event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
360 struct ftrace_event_call *call = filp->private_data;
364 * If there's only one type, we can't change it.
365 * And currently we always have printf type, and we
366 * may or may not have raw type.
368 * This is a redundant check, the file should be read
369 * only if this is the case anyway.
375 if (cnt >= sizeof(buf))
378 if (copy_from_user(&buf, ubuf, cnt))
383 if (!strncmp(buf, "printf", 6) &&
384 (!buf[6] || isspace(buf[6]))) {
386 call->type = TRACE_EVENT_TYPE_PRINTF;
389 * If raw enabled, the disable it and enable
392 if (call->raw_enabled) {
393 call->raw_enabled = 0;
400 } else if (!strncmp(buf, "raw", 3) &&
401 (!buf[3] || isspace(buf[3]))) {
403 call->type = TRACE_EVENT_TYPE_RAW;
406 * If printf enabled, the disable it and enable
413 call->raw_enabled = 1;
425 event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
428 struct ftrace_event_call *call = filp->private_data;
432 r += sprintf(buf, "printf\n");
435 r += sprintf(buf+r, "raw\n");
437 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
440 static const struct seq_operations show_event_seq_ops = {
447 static const struct seq_operations show_set_event_seq_ops = {
454 static const struct file_operations ftrace_avail_fops = {
455 .open = ftrace_event_seq_open,
458 .release = seq_release,
461 static const struct file_operations ftrace_set_event_fops = {
462 .open = ftrace_event_seq_open,
464 .write = ftrace_event_write,
466 .release = seq_release,
469 static const struct file_operations ftrace_enable_fops = {
470 .open = tracing_open_generic,
471 .read = event_enable_read,
472 .write = event_enable_write,
475 static const struct file_operations ftrace_type_fops = {
476 .open = tracing_open_generic,
477 .read = event_type_read,
478 .write = event_type_write,
481 static const struct file_operations ftrace_available_types_fops = {
482 .open = tracing_open_generic,
483 .read = event_available_types_read,
486 static struct dentry *event_trace_events_dir(void)
488 static struct dentry *d_tracer;
489 static struct dentry *d_events;
494 d_tracer = tracing_init_dentry();
498 d_events = debugfs_create_dir("events", d_tracer);
500 pr_warning("Could not create debugfs "
501 "'events' directory\n");
506 struct event_subsystem {
507 struct list_head list;
509 struct dentry *entry;
512 static LIST_HEAD(event_subsystems);
514 static struct dentry *
515 event_subsystem_dir(const char *name, struct dentry *d_events)
517 struct event_subsystem *system;
519 /* First see if we did not already create this dir */
520 list_for_each_entry(system, &event_subsystems, list) {
521 if (strcmp(system->name, name) == 0)
522 return system->entry;
525 /* need to create new entry */
526 system = kmalloc(sizeof(*system), GFP_KERNEL);
528 pr_warning("No memory to create event subsystem %s\n",
533 system->entry = debugfs_create_dir(name, d_events);
534 if (!system->entry) {
535 pr_warning("Could not create event subsystem %s\n",
542 list_add(&system->list, &event_subsystems);
544 return system->entry;
548 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
550 struct dentry *entry;
554 * If the trace point header did not define TRACE_SYSTEM
555 * then the system would be called "TRACE_SYSTEM".
557 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
558 d_events = event_subsystem_dir(call->system, d_events);
560 if (call->raw_init) {
561 ret = call->raw_init();
563 pr_warning("Could not initialize trace point"
564 " events/%s\n", call->name);
569 /* default the output to printf */
570 call->type = TRACE_EVENT_TYPE_PRINTF;
572 call->dir = debugfs_create_dir(call->name, d_events);
574 pr_warning("Could not create debugfs "
575 "'%s' directory\n", call->name);
579 entry = debugfs_create_file("enable", 0644, call->dir, call,
580 &ftrace_enable_fops);
582 pr_warning("Could not create debugfs "
583 "'%s/enable' entry\n", call->name);
585 /* Only let type be writable, if we can change it */
586 entry = debugfs_create_file("type",
587 call->raw_init ? 0644 : 0444,
591 pr_warning("Could not create debugfs "
592 "'%s/type' entry\n", call->name);
594 entry = debugfs_create_file("available_types", 0444, call->dir, call,
595 &ftrace_available_types_fops);
597 pr_warning("Could not create debugfs "
598 "'%s/type' available_types\n", call->name);
603 static __init int event_trace_init(void)
605 struct ftrace_event_call *call = __start_ftrace_events;
606 struct dentry *d_tracer;
607 struct dentry *entry;
608 struct dentry *d_events;
610 d_tracer = tracing_init_dentry();
614 entry = debugfs_create_file("available_events", 0444, d_tracer,
615 (void *)&show_event_seq_ops,
618 pr_warning("Could not create debugfs "
619 "'available_events' entry\n");
621 entry = debugfs_create_file("set_event", 0644, d_tracer,
622 (void *)&show_set_event_seq_ops,
623 &ftrace_set_event_fops);
625 pr_warning("Could not create debugfs "
626 "'set_event' entry\n");
628 d_events = event_trace_events_dir();
632 events_for_each(call) {
633 /* The linker may leave blanks */
636 event_create_dir(call, d_events);
641 fs_initcall(event_trace_init);