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 static DEFINE_MUTEX(event_mutex);
19 #define events_for_each(event) \
20 for (event = __start_ftrace_events; \
21 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
24 void event_trace_printk(unsigned long ip, const char *fmt, ...)
29 tracing_record_cmdline(current);
30 trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
34 static void ftrace_clear_events(void)
36 struct ftrace_event_call *call = (void *)__start_ftrace_events;
39 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
49 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
59 if (call->raw_enabled) {
60 call->raw_enabled = 0;
66 (call->type & TRACE_EVENT_TYPE_PRINTF)) {
70 if (!call->raw_enabled &&
71 (call->type & TRACE_EVENT_TYPE_RAW)) {
72 call->raw_enabled = 1;
79 static int ftrace_set_clr_event(char *buf, int set)
81 struct ftrace_event_call *call = __start_ftrace_events;
82 char *event = NULL, *sub = NULL, *match;
86 * The buf format can be <subsystem>:<event-name>
87 * *:<event-name> means any event by that name.
88 * :<event-name> is the same.
90 * <subsystem>:* means all events in that subsystem
91 * <subsystem>: means the same.
93 * <name> (no ':') means all events in a subsystem with
94 * the name <name> or any event that matches <name>
97 match = strsep(&buf, ":");
103 if (!strlen(sub) || strcmp(sub, "*") == 0)
105 if (!strlen(event) || strcmp(event, "*") == 0)
109 mutex_lock(&event_mutex);
110 events_for_each(call) {
116 strcmp(match, call->name) != 0 &&
117 strcmp(match, call->system) != 0)
120 if (sub && strcmp(sub, call->system) != 0)
123 if (event && strcmp(event, call->name) != 0)
126 ftrace_event_enable_disable(call, set);
130 mutex_unlock(&event_mutex);
135 /* 128 should be much more than enough */
136 #define EVENT_BUF_SIZE 127
139 ftrace_event_write(struct file *file, const char __user *ubuf,
140 size_t cnt, loff_t *ppos)
151 ret = get_user(ch, ubuf++);
157 /* skip white space */
158 while (cnt && isspace(ch)) {
159 ret = get_user(ch, ubuf++);
166 /* Only white space found? */
173 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
177 if (cnt > EVENT_BUF_SIZE)
178 cnt = EVENT_BUF_SIZE;
181 while (cnt && !isspace(ch)) {
187 ret = get_user(ch, ubuf++);
197 ret = ftrace_set_clr_event(buf, set);
210 t_next(struct seq_file *m, void *v, loff_t *pos)
212 struct ftrace_event_call *call = m->private;
213 struct ftrace_event_call *next = call;
217 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
225 static void *t_start(struct seq_file *m, loff_t *pos)
227 return t_next(m, NULL, pos);
231 s_next(struct seq_file *m, void *v, loff_t *pos)
233 struct ftrace_event_call *call = m->private;
234 struct ftrace_event_call *next;
239 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
242 if (!call->enabled) {
253 static void *s_start(struct seq_file *m, loff_t *pos)
255 return s_next(m, NULL, pos);
258 static int t_show(struct seq_file *m, void *v)
260 struct ftrace_event_call *call = v;
262 if (strcmp(call->system, TRACE_SYSTEM) != 0)
263 seq_printf(m, "%s:", call->system);
264 seq_printf(m, "%s\n", call->name);
269 static void t_stop(struct seq_file *m, void *p)
274 ftrace_event_seq_open(struct inode *inode, struct file *file)
277 const struct seq_operations *seq_ops;
279 if ((file->f_mode & FMODE_WRITE) &&
280 !(file->f_flags & O_APPEND))
281 ftrace_clear_events();
283 seq_ops = inode->i_private;
284 ret = seq_open(file, seq_ops);
286 struct seq_file *m = file->private_data;
288 m->private = __start_ftrace_events;
294 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
297 struct ftrace_event_call *call = filp->private_data;
300 if (call->enabled || call->raw_enabled)
305 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
309 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
312 struct ftrace_event_call *call = filp->private_data;
317 if (cnt >= sizeof(buf))
320 if (copy_from_user(&buf, ubuf, cnt))
325 ret = strict_strtoul(buf, 10, &val);
332 mutex_lock(&event_mutex);
333 ftrace_event_enable_disable(call, val);
334 mutex_unlock(&event_mutex);
347 event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
350 struct ftrace_event_call *call = filp->private_data;
354 if (call->type & TRACE_EVENT_TYPE_PRINTF)
355 r += sprintf(buf, "printf\n");
357 if (call->type & TRACE_EVENT_TYPE_RAW)
358 r += sprintf(buf+r, "raw\n");
360 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
364 event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
367 struct ftrace_event_call *call = filp->private_data;
371 * If there's only one type, we can't change it.
372 * And currently we always have printf type, and we
373 * may or may not have raw type.
375 * This is a redundant check, the file should be read
376 * only if this is the case anyway.
382 if (cnt >= sizeof(buf))
385 if (copy_from_user(&buf, ubuf, cnt))
390 if (!strncmp(buf, "printf", 6) &&
391 (!buf[6] || isspace(buf[6]))) {
393 call->type = TRACE_EVENT_TYPE_PRINTF;
396 * If raw enabled, the disable it and enable
399 if (call->raw_enabled) {
400 call->raw_enabled = 0;
407 } else if (!strncmp(buf, "raw", 3) &&
408 (!buf[3] || isspace(buf[3]))) {
410 call->type = TRACE_EVENT_TYPE_RAW;
413 * If printf enabled, the disable it and enable
420 call->raw_enabled = 1;
432 event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
435 struct ftrace_event_call *call = filp->private_data;
439 r += sprintf(buf, "printf\n");
442 r += sprintf(buf+r, "raw\n");
444 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
447 static const struct seq_operations show_event_seq_ops = {
454 static const struct seq_operations show_set_event_seq_ops = {
461 static const struct file_operations ftrace_avail_fops = {
462 .open = ftrace_event_seq_open,
465 .release = seq_release,
468 static const struct file_operations ftrace_set_event_fops = {
469 .open = ftrace_event_seq_open,
471 .write = ftrace_event_write,
473 .release = seq_release,
476 static const struct file_operations ftrace_enable_fops = {
477 .open = tracing_open_generic,
478 .read = event_enable_read,
479 .write = event_enable_write,
482 static const struct file_operations ftrace_type_fops = {
483 .open = tracing_open_generic,
484 .read = event_type_read,
485 .write = event_type_write,
488 static const struct file_operations ftrace_available_types_fops = {
489 .open = tracing_open_generic,
490 .read = event_available_types_read,
493 static struct dentry *event_trace_events_dir(void)
495 static struct dentry *d_tracer;
496 static struct dentry *d_events;
501 d_tracer = tracing_init_dentry();
505 d_events = debugfs_create_dir("events", d_tracer);
507 pr_warning("Could not create debugfs "
508 "'events' directory\n");
513 struct event_subsystem {
514 struct list_head list;
516 struct dentry *entry;
519 static LIST_HEAD(event_subsystems);
521 static struct dentry *
522 event_subsystem_dir(const char *name, struct dentry *d_events)
524 struct event_subsystem *system;
526 /* First see if we did not already create this dir */
527 list_for_each_entry(system, &event_subsystems, list) {
528 if (strcmp(system->name, name) == 0)
529 return system->entry;
532 /* need to create new entry */
533 system = kmalloc(sizeof(*system), GFP_KERNEL);
535 pr_warning("No memory to create event subsystem %s\n",
540 system->entry = debugfs_create_dir(name, d_events);
541 if (!system->entry) {
542 pr_warning("Could not create event subsystem %s\n",
549 list_add(&system->list, &event_subsystems);
551 return system->entry;
555 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
557 struct dentry *entry;
561 * If the trace point header did not define TRACE_SYSTEM
562 * then the system would be called "TRACE_SYSTEM".
564 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
565 d_events = event_subsystem_dir(call->system, d_events);
567 if (call->raw_init) {
568 ret = call->raw_init();
570 pr_warning("Could not initialize trace point"
571 " events/%s\n", call->name);
576 /* default the output to printf */
577 call->type = TRACE_EVENT_TYPE_PRINTF;
579 call->dir = debugfs_create_dir(call->name, d_events);
581 pr_warning("Could not create debugfs "
582 "'%s' directory\n", call->name);
586 entry = debugfs_create_file("enable", 0644, call->dir, call,
587 &ftrace_enable_fops);
589 pr_warning("Could not create debugfs "
590 "'%s/enable' entry\n", call->name);
592 /* Only let type be writable, if we can change it */
593 entry = debugfs_create_file("type",
594 call->raw_init ? 0644 : 0444,
598 pr_warning("Could not create debugfs "
599 "'%s/type' entry\n", call->name);
601 entry = debugfs_create_file("available_types", 0444, call->dir, call,
602 &ftrace_available_types_fops);
604 pr_warning("Could not create debugfs "
605 "'%s/type' available_types\n", call->name);
610 static __init int event_trace_init(void)
612 struct ftrace_event_call *call = __start_ftrace_events;
613 struct dentry *d_tracer;
614 struct dentry *entry;
615 struct dentry *d_events;
617 d_tracer = tracing_init_dentry();
621 entry = debugfs_create_file("available_events", 0444, d_tracer,
622 (void *)&show_event_seq_ops,
625 pr_warning("Could not create debugfs "
626 "'available_events' entry\n");
628 entry = debugfs_create_file("set_event", 0644, d_tracer,
629 (void *)&show_set_event_seq_ops,
630 &ftrace_set_event_fops);
632 pr_warning("Could not create debugfs "
633 "'set_event' entry\n");
635 d_events = event_trace_events_dir();
639 events_for_each(call) {
640 /* The linker may leave blanks */
643 event_create_dir(call, d_events);
648 fs_initcall(event_trace_init);