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,
72 static int ftrace_set_clr_event(char *buf, int set)
74 struct ftrace_event_call *call = __start_ftrace_events;
75 char *event = NULL, *sub = NULL, *match;
79 * The buf format can be <subsystem>:<event-name>
80 * *:<event-name> means any event by that name.
81 * :<event-name> is the same.
83 * <subsystem>:* means all events in that subsystem
84 * <subsystem>: means the same.
86 * <name> (no ':') means all events in a subsystem with
87 * the name <name> or any event that matches <name>
90 match = strsep(&buf, ":");
96 if (!strlen(sub) || strcmp(sub, "*") == 0)
98 if (!strlen(event) || strcmp(event, "*") == 0)
102 mutex_lock(&event_mutex);
103 events_for_each(call) {
105 if (!call->name || !call->regfunc)
109 strcmp(match, call->name) != 0 &&
110 strcmp(match, call->system) != 0)
113 if (sub && strcmp(sub, call->system) != 0)
116 if (event && strcmp(event, call->name) != 0)
119 ftrace_event_enable_disable(call, set);
123 mutex_unlock(&event_mutex);
128 /* 128 should be much more than enough */
129 #define EVENT_BUF_SIZE 127
132 ftrace_event_write(struct file *file, const char __user *ubuf,
133 size_t cnt, loff_t *ppos)
144 ret = get_user(ch, ubuf++);
150 /* skip white space */
151 while (cnt && isspace(ch)) {
152 ret = get_user(ch, ubuf++);
159 /* Only white space found? */
166 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
170 if (cnt > EVENT_BUF_SIZE)
171 cnt = EVENT_BUF_SIZE;
174 while (cnt && !isspace(ch)) {
180 ret = get_user(ch, ubuf++);
190 ret = ftrace_set_clr_event(buf, set);
203 t_next(struct seq_file *m, void *v, loff_t *pos)
205 struct ftrace_event_call *call = m->private;
206 struct ftrace_event_call *next = call;
211 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
215 * The ftrace subsystem is for showing formats only.
216 * They can not be enabled or disabled via the event files.
230 static void *t_start(struct seq_file *m, loff_t *pos)
232 return t_next(m, NULL, pos);
236 s_next(struct seq_file *m, void *v, loff_t *pos)
238 struct ftrace_event_call *call = m->private;
239 struct ftrace_event_call *next;
244 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
247 if (!call->enabled) {
258 static void *s_start(struct seq_file *m, loff_t *pos)
260 return s_next(m, NULL, pos);
263 static int t_show(struct seq_file *m, void *v)
265 struct ftrace_event_call *call = v;
267 if (strcmp(call->system, TRACE_SYSTEM) != 0)
268 seq_printf(m, "%s:", call->system);
269 seq_printf(m, "%s\n", call->name);
274 static void t_stop(struct seq_file *m, void *p)
279 ftrace_event_seq_open(struct inode *inode, struct file *file)
282 const struct seq_operations *seq_ops;
284 if ((file->f_mode & FMODE_WRITE) &&
285 !(file->f_flags & O_APPEND))
286 ftrace_clear_events();
288 seq_ops = inode->i_private;
289 ret = seq_open(file, seq_ops);
291 struct seq_file *m = file->private_data;
293 m->private = __start_ftrace_events;
299 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
302 struct ftrace_event_call *call = filp->private_data;
310 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
314 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
317 struct ftrace_event_call *call = filp->private_data;
322 if (cnt >= sizeof(buf))
325 if (copy_from_user(&buf, ubuf, cnt))
330 ret = strict_strtoul(buf, 10, &val);
337 mutex_lock(&event_mutex);
338 ftrace_event_enable_disable(call, val);
339 mutex_unlock(&event_mutex);
352 #define FIELD(type, name) \
353 #type, #name, offsetof(typeof(field), name), sizeof(field.name)
355 static int trace_write_header(struct trace_seq *s)
357 struct trace_entry field;
359 /* struct trace_entry */
360 return trace_seq_printf(s,
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"
364 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
365 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
367 FIELD(unsigned char, type),
368 FIELD(unsigned char, flags),
369 FIELD(unsigned char, preempt_count),
375 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
378 struct ftrace_event_call *call = filp->private_data;
383 s = kmalloc(sizeof(*s), GFP_KERNEL);
392 /* If any of the first writes fail, so will the show_format. */
394 trace_seq_printf(s, "name: %s\n", call->name);
395 trace_seq_printf(s, "ID: %d\n", call->id);
396 trace_seq_printf(s, "format:\n");
397 trace_write_header(s);
399 r = call->show_format(s);
402 * ug! The format output is bigger than a PAGE!!
404 buf = "FORMAT TOO BIG\n";
405 r = simple_read_from_buffer(ubuf, cnt, ppos,
410 r = simple_read_from_buffer(ubuf, cnt, ppos,
417 static const struct seq_operations show_event_seq_ops = {
424 static const struct seq_operations show_set_event_seq_ops = {
431 static const struct file_operations ftrace_avail_fops = {
432 .open = ftrace_event_seq_open,
435 .release = seq_release,
438 static const struct file_operations ftrace_set_event_fops = {
439 .open = ftrace_event_seq_open,
441 .write = ftrace_event_write,
443 .release = seq_release,
446 static const struct file_operations ftrace_enable_fops = {
447 .open = tracing_open_generic,
448 .read = event_enable_read,
449 .write = event_enable_write,
452 static const struct file_operations ftrace_event_format_fops = {
453 .open = tracing_open_generic,
454 .read = event_format_read,
457 static struct dentry *event_trace_events_dir(void)
459 static struct dentry *d_tracer;
460 static struct dentry *d_events;
465 d_tracer = tracing_init_dentry();
469 d_events = debugfs_create_dir("events", d_tracer);
471 pr_warning("Could not create debugfs "
472 "'events' directory\n");
477 struct event_subsystem {
478 struct list_head list;
480 struct dentry *entry;
483 static LIST_HEAD(event_subsystems);
485 static struct dentry *
486 event_subsystem_dir(const char *name, struct dentry *d_events)
488 struct event_subsystem *system;
490 /* First see if we did not already create this dir */
491 list_for_each_entry(system, &event_subsystems, list) {
492 if (strcmp(system->name, name) == 0)
493 return system->entry;
496 /* need to create new entry */
497 system = kmalloc(sizeof(*system), GFP_KERNEL);
499 pr_warning("No memory to create event subsystem %s\n",
504 system->entry = debugfs_create_dir(name, d_events);
505 if (!system->entry) {
506 pr_warning("Could not create event subsystem %s\n",
513 list_add(&system->list, &event_subsystems);
515 return system->entry;
519 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
521 struct dentry *entry;
525 * If the trace point header did not define TRACE_SYSTEM
526 * then the system would be called "TRACE_SYSTEM".
528 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
529 d_events = event_subsystem_dir(call->system, d_events);
531 if (call->raw_init) {
532 ret = call->raw_init();
534 pr_warning("Could not initialize trace point"
535 " events/%s\n", call->name);
540 call->dir = debugfs_create_dir(call->name, d_events);
542 pr_warning("Could not create debugfs "
543 "'%s' directory\n", call->name);
548 entry = debugfs_create_file("enable", 0644, call->dir, call,
549 &ftrace_enable_fops);
551 pr_warning("Could not create debugfs "
552 "'%s/enable' entry\n", call->name);
555 /* A trace may not want to export its format */
556 if (!call->show_format)
559 entry = debugfs_create_file("format", 0444, call->dir, call,
560 &ftrace_event_format_fops);
562 pr_warning("Could not create debugfs "
563 "'%s/format' entry\n", call->name);
568 static __init int event_trace_init(void)
570 struct ftrace_event_call *call = __start_ftrace_events;
571 struct dentry *d_tracer;
572 struct dentry *entry;
573 struct dentry *d_events;
575 d_tracer = tracing_init_dentry();
579 entry = debugfs_create_file("available_events", 0444, d_tracer,
580 (void *)&show_event_seq_ops,
583 pr_warning("Could not create debugfs "
584 "'available_events' entry\n");
586 entry = debugfs_create_file("set_event", 0644, d_tracer,
587 (void *)&show_set_event_seq_ops,
588 &ftrace_set_event_fops);
590 pr_warning("Could not create debugfs "
591 "'set_event' entry\n");
593 d_events = event_trace_events_dir();
597 events_for_each(call) {
598 /* The linker may leave blanks */
601 event_create_dir(call, d_events);
606 fs_initcall(event_trace_init);