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 static void ftrace_clear_events(void)
29 struct ftrace_event_call *call = (void *)__start_ftrace_events;
32 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
42 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
62 static int ftrace_set_clr_event(char *buf, int set)
64 struct ftrace_event_call *call = __start_ftrace_events;
65 char *event = NULL, *sub = NULL, *match;
69 * The buf format can be <subsystem>:<event-name>
70 * *:<event-name> means any event by that name.
71 * :<event-name> is the same.
73 * <subsystem>:* means all events in that subsystem
74 * <subsystem>: means the same.
76 * <name> (no ':') means all events in a subsystem with
77 * the name <name> or any event that matches <name>
80 match = strsep(&buf, ":");
86 if (!strlen(sub) || strcmp(sub, "*") == 0)
88 if (!strlen(event) || strcmp(event, "*") == 0)
92 mutex_lock(&event_mutex);
93 events_for_each(call) {
95 if (!call->name || !call->regfunc)
99 strcmp(match, call->name) != 0 &&
100 strcmp(match, call->system) != 0)
103 if (sub && strcmp(sub, call->system) != 0)
106 if (event && strcmp(event, call->name) != 0)
109 ftrace_event_enable_disable(call, set);
113 mutex_unlock(&event_mutex);
118 /* 128 should be much more than enough */
119 #define EVENT_BUF_SIZE 127
122 ftrace_event_write(struct file *file, const char __user *ubuf,
123 size_t cnt, loff_t *ppos)
134 ret = tracing_update_buffers();
138 ret = get_user(ch, ubuf++);
144 /* skip white space */
145 while (cnt && isspace(ch)) {
146 ret = get_user(ch, ubuf++);
153 /* Only white space found? */
160 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
164 if (cnt > EVENT_BUF_SIZE)
165 cnt = EVENT_BUF_SIZE;
168 while (cnt && !isspace(ch)) {
174 ret = get_user(ch, ubuf++);
184 ret = ftrace_set_clr_event(buf, set);
197 t_next(struct seq_file *m, void *v, loff_t *pos)
199 struct ftrace_event_call *call = m->private;
200 struct ftrace_event_call *next = call;
205 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
209 * The ftrace subsystem is for showing formats only.
210 * They can not be enabled or disabled via the event files.
224 static void *t_start(struct seq_file *m, loff_t *pos)
226 return t_next(m, NULL, pos);
230 s_next(struct seq_file *m, void *v, loff_t *pos)
232 struct ftrace_event_call *call = m->private;
233 struct ftrace_event_call *next;
238 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
241 if (!call->enabled) {
252 static void *s_start(struct seq_file *m, loff_t *pos)
254 return s_next(m, NULL, pos);
257 static int t_show(struct seq_file *m, void *v)
259 struct ftrace_event_call *call = v;
261 if (strcmp(call->system, TRACE_SYSTEM) != 0)
262 seq_printf(m, "%s:", call->system);
263 seq_printf(m, "%s\n", call->name);
268 static void t_stop(struct seq_file *m, void *p)
273 ftrace_event_seq_open(struct inode *inode, struct file *file)
276 const struct seq_operations *seq_ops;
278 if ((file->f_mode & FMODE_WRITE) &&
279 !(file->f_flags & O_APPEND))
280 ftrace_clear_events();
282 seq_ops = inode->i_private;
283 ret = seq_open(file, seq_ops);
285 struct seq_file *m = file->private_data;
287 m->private = __start_ftrace_events;
293 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
296 struct ftrace_event_call *call = filp->private_data;
304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
308 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
311 struct ftrace_event_call *call = filp->private_data;
316 if (cnt >= sizeof(buf))
319 if (copy_from_user(&buf, ubuf, cnt))
324 ret = strict_strtoul(buf, 10, &val);
328 ret = tracing_update_buffers();
335 mutex_lock(&event_mutex);
336 ftrace_event_enable_disable(call, val);
337 mutex_unlock(&event_mutex);
350 #define FIELD(type, name) \
351 #type, #name, offsetof(typeof(field), name), sizeof(field.name)
353 static int trace_write_header(struct trace_seq *s)
355 struct trace_entry field;
357 /* struct trace_entry */
358 return trace_seq_printf(s,
359 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
360 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
361 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
362 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
363 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
365 FIELD(unsigned char, type),
366 FIELD(unsigned char, flags),
367 FIELD(unsigned char, preempt_count),
373 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
376 struct ftrace_event_call *call = filp->private_data;
384 s = kmalloc(sizeof(*s), GFP_KERNEL);
390 /* If any of the first writes fail, so will the show_format. */
392 trace_seq_printf(s, "name: %s\n", call->name);
393 trace_seq_printf(s, "ID: %d\n", call->id);
394 trace_seq_printf(s, "format:\n");
395 trace_write_header(s);
397 r = call->show_format(s);
400 * ug! The format output is bigger than a PAGE!!
402 buf = "FORMAT TOO BIG\n";
403 r = simple_read_from_buffer(ubuf, cnt, ppos,
408 r = simple_read_from_buffer(ubuf, cnt, ppos,
416 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
418 struct ftrace_event_call *call = filp->private_data;
425 s = kmalloc(sizeof(*s), GFP_KERNEL);
430 trace_seq_printf(s, "%d\n", call->id);
432 r = simple_read_from_buffer(ubuf, cnt, ppos,
438 static const struct seq_operations show_event_seq_ops = {
445 static const struct seq_operations show_set_event_seq_ops = {
452 static const struct file_operations ftrace_avail_fops = {
453 .open = ftrace_event_seq_open,
456 .release = seq_release,
459 static const struct file_operations ftrace_set_event_fops = {
460 .open = ftrace_event_seq_open,
462 .write = ftrace_event_write,
464 .release = seq_release,
467 static const struct file_operations ftrace_enable_fops = {
468 .open = tracing_open_generic,
469 .read = event_enable_read,
470 .write = event_enable_write,
473 static const struct file_operations ftrace_event_format_fops = {
474 .open = tracing_open_generic,
475 .read = event_format_read,
478 static const struct file_operations ftrace_event_id_fops = {
479 .open = tracing_open_generic,
480 .read = event_id_read,
483 static struct dentry *event_trace_events_dir(void)
485 static struct dentry *d_tracer;
486 static struct dentry *d_events;
491 d_tracer = tracing_init_dentry();
495 d_events = debugfs_create_dir("events", d_tracer);
497 pr_warning("Could not create debugfs "
498 "'events' directory\n");
503 struct event_subsystem {
504 struct list_head list;
506 struct dentry *entry;
509 static LIST_HEAD(event_subsystems);
511 static struct dentry *
512 event_subsystem_dir(const char *name, struct dentry *d_events)
514 struct event_subsystem *system;
516 /* First see if we did not already create this dir */
517 list_for_each_entry(system, &event_subsystems, list) {
518 if (strcmp(system->name, name) == 0)
519 return system->entry;
522 /* need to create new entry */
523 system = kmalloc(sizeof(*system), GFP_KERNEL);
525 pr_warning("No memory to create event subsystem %s\n",
530 system->entry = debugfs_create_dir(name, d_events);
531 if (!system->entry) {
532 pr_warning("Could not create event subsystem %s\n",
539 list_add(&system->list, &event_subsystems);
541 return system->entry;
545 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
547 struct dentry *entry;
551 * If the trace point header did not define TRACE_SYSTEM
552 * then the system would be called "TRACE_SYSTEM".
554 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
555 d_events = event_subsystem_dir(call->system, d_events);
557 if (call->raw_init) {
558 ret = call->raw_init();
560 pr_warning("Could not initialize trace point"
561 " events/%s\n", call->name);
566 call->dir = debugfs_create_dir(call->name, d_events);
568 pr_warning("Could not create debugfs "
569 "'%s' directory\n", call->name);
574 entry = debugfs_create_file("enable", 0644, call->dir, call,
575 &ftrace_enable_fops);
577 pr_warning("Could not create debugfs "
578 "'%s/enable' entry\n", call->name);
582 entry = debugfs_create_file("id", 0444, call->dir, call,
583 &ftrace_event_id_fops);
585 pr_warning("Could not create debugfs '%s/id' entry\n",
589 /* A trace may not want to export its format */
590 if (!call->show_format)
593 entry = debugfs_create_file("format", 0444, call->dir, call,
594 &ftrace_event_format_fops);
596 pr_warning("Could not create debugfs "
597 "'%s/format' entry\n", call->name);
602 static __init int event_trace_init(void)
604 struct ftrace_event_call *call = __start_ftrace_events;
605 struct dentry *d_tracer;
606 struct dentry *entry;
607 struct dentry *d_events;
609 d_tracer = tracing_init_dentry();
613 entry = debugfs_create_file("available_events", 0444, d_tracer,
614 (void *)&show_event_seq_ops,
617 pr_warning("Could not create debugfs "
618 "'available_events' entry\n");
620 entry = debugfs_create_file("set_event", 0644, d_tracer,
621 (void *)&show_set_event_seq_ops,
622 &ftrace_set_event_fops);
624 pr_warning("Could not create debugfs "
625 "'set_event' entry\n");
627 d_events = event_trace_events_dir();
631 events_for_each(call) {
632 /* The linker may leave blanks */
635 event_create_dir(call, d_events);
640 fs_initcall(event_trace_init);