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>
13 #include "trace_events.h"
15 #define events_for_each(event) \
16 for (event = __start_ftrace_events; \
17 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
20 void event_trace_printk(unsigned long ip, const char *fmt, ...)
25 tracing_record_cmdline(current);
26 trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
30 static void ftrace_clear_events(void)
32 struct ftrace_event_call *call = (void *)__start_ftrace_events;
35 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
45 static int ftrace_set_clr_event(char *buf, int set)
47 struct ftrace_event_call *call = __start_ftrace_events;
50 events_for_each(call) {
55 if (strcmp(buf, call->name) != 0)
65 /* Already cleared? */
76 /* 128 should be much more than enough */
77 #define EVENT_BUF_SIZE 127
80 ftrace_event_write(struct file *file, const char __user *ubuf,
81 size_t cnt, loff_t *ppos)
92 ret = get_user(ch, ubuf++);
98 /* skip white space */
99 while (cnt && isspace(ch)) {
100 ret = get_user(ch, ubuf++);
107 /* Only white space found? */
114 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
118 if (cnt > EVENT_BUF_SIZE)
119 cnt = EVENT_BUF_SIZE;
122 while (cnt && !isspace(ch)) {
128 ret = get_user(ch, ubuf++);
138 ret = ftrace_set_clr_event(buf, set);
151 t_next(struct seq_file *m, void *v, loff_t *pos)
153 struct ftrace_event_call *call = m->private;
154 struct ftrace_event_call *next = call;
158 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
166 static void *t_start(struct seq_file *m, loff_t *pos)
168 return t_next(m, NULL, pos);
172 s_next(struct seq_file *m, void *v, loff_t *pos)
174 struct ftrace_event_call *call = m->private;
175 struct ftrace_event_call *next;
180 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
183 if (!call->enabled) {
194 static void *s_start(struct seq_file *m, loff_t *pos)
196 return s_next(m, NULL, pos);
199 static int t_show(struct seq_file *m, void *v)
201 struct ftrace_event_call *call = v;
203 seq_printf(m, "%s\n", call->name);
208 static void t_stop(struct seq_file *m, void *p)
213 ftrace_event_seq_open(struct inode *inode, struct file *file)
216 const struct seq_operations *seq_ops;
218 if ((file->f_mode & FMODE_WRITE) &&
219 !(file->f_flags & O_APPEND))
220 ftrace_clear_events();
222 seq_ops = inode->i_private;
223 ret = seq_open(file, seq_ops);
225 struct seq_file *m = file->private_data;
227 m->private = __start_ftrace_events;
233 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
236 struct ftrace_event_call *call = filp->private_data;
244 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
248 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
251 struct ftrace_event_call *call = filp->private_data;
256 if (cnt >= sizeof(buf))
259 if (copy_from_user(&buf, ubuf, cnt))
264 ret = strict_strtoul(buf, 10, &val);
293 static const struct seq_operations show_event_seq_ops = {
300 static const struct seq_operations show_set_event_seq_ops = {
307 static const struct file_operations ftrace_avail_fops = {
308 .open = ftrace_event_seq_open,
311 .release = seq_release,
314 static const struct file_operations ftrace_set_event_fops = {
315 .open = ftrace_event_seq_open,
317 .write = ftrace_event_write,
319 .release = seq_release,
322 static const struct file_operations ftrace_enable_fops = {
323 .open = tracing_open_generic,
324 .read = event_enable_read,
325 .write = event_enable_write,
328 static struct dentry *event_trace_events_dir(void)
330 static struct dentry *d_tracer;
331 static struct dentry *d_events;
336 d_tracer = tracing_init_dentry();
340 d_events = debugfs_create_dir("events", d_tracer);
342 pr_warning("Could not create debugfs "
343 "'events' directory\n");
348 struct event_subsystem {
349 struct list_head list;
351 struct dentry *entry;
354 static LIST_HEAD(event_subsystems);
356 static struct dentry *
357 event_subsystem_dir(const char *name, struct dentry *d_events)
359 struct event_subsystem *system;
361 /* First see if we did not already create this dir */
362 list_for_each_entry(system, &event_subsystems, list) {
363 if (strcmp(system->name, name) == 0)
364 return system->entry;
367 /* need to create new entry */
368 system = kmalloc(sizeof(*system), GFP_KERNEL);
370 pr_warning("No memory to create event subsystem %s\n",
375 system->entry = debugfs_create_dir(name, d_events);
376 if (!system->entry) {
377 pr_warning("Could not create event subsystem %s\n",
384 list_add(&system->list, &event_subsystems);
386 return system->entry;
390 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
392 struct dentry *entry;
395 * If the trace point header did not define TRACE_SYSTEM
396 * then the system would be called "TRACE_SYSTEM".
398 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
399 d_events = event_subsystem_dir(call->system, d_events);
401 call->dir = debugfs_create_dir(call->name, d_events);
403 pr_warning("Could not create debugfs "
404 "'%s' directory\n", call->name);
408 entry = debugfs_create_file("enable", 0644, call->dir, call,
409 &ftrace_enable_fops);
411 pr_warning("Could not create debugfs "
412 "'%s/enable' entry\n", call->name);
417 static __init int event_trace_init(void)
419 struct ftrace_event_call *call = __start_ftrace_events;
420 struct dentry *d_tracer;
421 struct dentry *entry;
422 struct dentry *d_events;
424 d_tracer = tracing_init_dentry();
428 entry = debugfs_create_file("available_events", 0444, d_tracer,
429 (void *)&show_event_seq_ops,
432 pr_warning("Could not create debugfs "
433 "'available_events' entry\n");
435 entry = debugfs_create_file("set_event", 0644, d_tracer,
436 (void *)&show_set_event_seq_ops,
437 &ftrace_set_event_fops);
439 pr_warning("Could not create debugfs "
440 "'set_event' entry\n");
442 d_events = event_trace_events_dir();
446 events_for_each(call) {
447 /* The linker may leave blanks */
450 event_create_dir(call, d_events);
455 fs_initcall(event_trace_init);