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,
415 static const struct seq_operations show_event_seq_ops = {
422 static const struct seq_operations show_set_event_seq_ops = {
429 static const struct file_operations ftrace_avail_fops = {
430 .open = ftrace_event_seq_open,
433 .release = seq_release,
436 static const struct file_operations ftrace_set_event_fops = {
437 .open = ftrace_event_seq_open,
439 .write = ftrace_event_write,
441 .release = seq_release,
444 static const struct file_operations ftrace_enable_fops = {
445 .open = tracing_open_generic,
446 .read = event_enable_read,
447 .write = event_enable_write,
450 static const struct file_operations ftrace_event_format_fops = {
451 .open = tracing_open_generic,
452 .read = event_format_read,
455 static struct dentry *event_trace_events_dir(void)
457 static struct dentry *d_tracer;
458 static struct dentry *d_events;
463 d_tracer = tracing_init_dentry();
467 d_events = debugfs_create_dir("events", d_tracer);
469 pr_warning("Could not create debugfs "
470 "'events' directory\n");
475 struct event_subsystem {
476 struct list_head list;
478 struct dentry *entry;
481 static LIST_HEAD(event_subsystems);
483 static struct dentry *
484 event_subsystem_dir(const char *name, struct dentry *d_events)
486 struct event_subsystem *system;
488 /* First see if we did not already create this dir */
489 list_for_each_entry(system, &event_subsystems, list) {
490 if (strcmp(system->name, name) == 0)
491 return system->entry;
494 /* need to create new entry */
495 system = kmalloc(sizeof(*system), GFP_KERNEL);
497 pr_warning("No memory to create event subsystem %s\n",
502 system->entry = debugfs_create_dir(name, d_events);
503 if (!system->entry) {
504 pr_warning("Could not create event subsystem %s\n",
511 list_add(&system->list, &event_subsystems);
513 return system->entry;
517 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
519 struct dentry *entry;
523 * If the trace point header did not define TRACE_SYSTEM
524 * then the system would be called "TRACE_SYSTEM".
526 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
527 d_events = event_subsystem_dir(call->system, d_events);
529 if (call->raw_init) {
530 ret = call->raw_init();
532 pr_warning("Could not initialize trace point"
533 " events/%s\n", call->name);
538 call->dir = debugfs_create_dir(call->name, d_events);
540 pr_warning("Could not create debugfs "
541 "'%s' directory\n", call->name);
546 entry = debugfs_create_file("enable", 0644, call->dir, call,
547 &ftrace_enable_fops);
549 pr_warning("Could not create debugfs "
550 "'%s/enable' entry\n", call->name);
553 /* A trace may not want to export its format */
554 if (!call->show_format)
557 entry = debugfs_create_file("format", 0444, call->dir, call,
558 &ftrace_event_format_fops);
560 pr_warning("Could not create debugfs "
561 "'%s/format' entry\n", call->name);
566 static __init int event_trace_init(void)
568 struct ftrace_event_call *call = __start_ftrace_events;
569 struct dentry *d_tracer;
570 struct dentry *entry;
571 struct dentry *d_events;
573 d_tracer = tracing_init_dentry();
577 entry = debugfs_create_file("available_events", 0444, d_tracer,
578 (void *)&show_event_seq_ops,
581 pr_warning("Could not create debugfs "
582 "'available_events' entry\n");
584 entry = debugfs_create_file("set_event", 0644, d_tracer,
585 (void *)&show_set_event_seq_ops,
586 &ftrace_set_event_fops);
588 pr_warning("Could not create debugfs "
589 "'set_event' entry\n");
591 d_events = event_trace_events_dir();
595 events_for_each(call) {
596 /* The linker may leave blanks */
599 event_create_dir(call, d_events);
604 fs_initcall(event_trace_init);