tracing: fix stack tracer header
[linux-2.6/verdex.git] / kernel / trace / trace_events.c
blob238ea95a41159ddee14b8fa0fb3ae1f765147925
1 /*
2 * event tracer
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>.
9 */
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; \
25 event++)
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) {
34 if (call->enabled) {
35 call->enabled = 0;
36 call->unregfunc();
38 call++;
42 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
43 int enable)
46 switch (enable) {
47 case 0:
48 if (call->enabled) {
49 call->enabled = 0;
50 call->unregfunc();
52 break;
53 case 1:
54 if (!call->enabled) {
55 call->enabled = 1;
56 call->regfunc();
58 break;
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;
66 int ret = -EINVAL;
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, ":");
81 if (buf) {
82 sub = match;
83 event = buf;
84 match = NULL;
86 if (!strlen(sub) || strcmp(sub, "*") == 0)
87 sub = NULL;
88 if (!strlen(event) || strcmp(event, "*") == 0)
89 event = NULL;
92 mutex_lock(&event_mutex);
93 events_for_each(call) {
95 if (!call->name || !call->regfunc)
96 continue;
98 if (match &&
99 strcmp(match, call->name) != 0 &&
100 strcmp(match, call->system) != 0)
101 continue;
103 if (sub && strcmp(sub, call->system) != 0)
104 continue;
106 if (event && strcmp(event, call->name) != 0)
107 continue;
109 ftrace_event_enable_disable(call, set);
111 ret = 0;
113 mutex_unlock(&event_mutex);
115 return ret;
118 /* 128 should be much more than enough */
119 #define EVENT_BUF_SIZE 127
121 static ssize_t
122 ftrace_event_write(struct file *file, const char __user *ubuf,
123 size_t cnt, loff_t *ppos)
125 size_t read = 0;
126 int i, set = 1;
127 ssize_t ret;
128 char *buf;
129 char ch;
131 if (!cnt || cnt < 0)
132 return 0;
134 ret = tracing_update_buffers();
135 if (ret < 0)
136 return ret;
138 ret = get_user(ch, ubuf++);
139 if (ret)
140 return ret;
141 read++;
142 cnt--;
144 /* skip white space */
145 while (cnt && isspace(ch)) {
146 ret = get_user(ch, ubuf++);
147 if (ret)
148 return ret;
149 read++;
150 cnt--;
153 /* Only white space found? */
154 if (isspace(ch)) {
155 file->f_pos += read;
156 ret = read;
157 return ret;
160 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
161 if (!buf)
162 return -ENOMEM;
164 if (cnt > EVENT_BUF_SIZE)
165 cnt = EVENT_BUF_SIZE;
167 i = 0;
168 while (cnt && !isspace(ch)) {
169 if (!i && ch == '!')
170 set = 0;
171 else
172 buf[i++] = ch;
174 ret = get_user(ch, ubuf++);
175 if (ret)
176 goto out_free;
177 read++;
178 cnt--;
180 buf[i] = 0;
182 file->f_pos += read;
184 ret = ftrace_set_clr_event(buf, set);
185 if (ret)
186 goto out_free;
188 ret = read;
190 out_free:
191 kfree(buf);
193 return ret;
196 static void *
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;
202 (*pos)++;
204 for (;;) {
205 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
206 return NULL;
209 * The ftrace subsystem is for showing formats only.
210 * They can not be enabled or disabled via the event files.
212 if (call->regfunc)
213 break;
215 call++;
216 next = call;
219 m->private = ++next;
221 return call;
224 static void *t_start(struct seq_file *m, loff_t *pos)
226 return t_next(m, NULL, pos);
229 static void *
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;
235 (*pos)++;
237 retry:
238 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
239 return NULL;
241 if (!call->enabled) {
242 call++;
243 goto retry;
246 next = call;
247 m->private = ++next;
249 return call;
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);
265 return 0;
268 static void t_stop(struct seq_file *m, void *p)
272 static int
273 ftrace_event_seq_open(struct inode *inode, struct file *file)
275 int ret;
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);
284 if (!ret) {
285 struct seq_file *m = file->private_data;
287 m->private = __start_ftrace_events;
289 return ret;
292 static ssize_t
293 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
294 loff_t *ppos)
296 struct ftrace_event_call *call = filp->private_data;
297 char *buf;
299 if (call->enabled)
300 buf = "1\n";
301 else
302 buf = "0\n";
304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
307 static ssize_t
308 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
309 loff_t *ppos)
311 struct ftrace_event_call *call = filp->private_data;
312 char buf[64];
313 unsigned long val;
314 int ret;
316 if (cnt >= sizeof(buf))
317 return -EINVAL;
319 if (copy_from_user(&buf, ubuf, cnt))
320 return -EFAULT;
322 buf[cnt] = 0;
324 ret = strict_strtoul(buf, 10, &val);
325 if (ret < 0)
326 return ret;
328 ret = tracing_update_buffers();
329 if (ret < 0)
330 return ret;
332 switch (val) {
333 case 0:
334 case 1:
335 mutex_lock(&event_mutex);
336 ftrace_event_enable_disable(call, val);
337 mutex_unlock(&event_mutex);
338 break;
340 default:
341 return -EINVAL;
344 *ppos += cnt;
346 return cnt;
349 #undef FIELD
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"
364 "\n",
365 FIELD(unsigned char, type),
366 FIELD(unsigned char, flags),
367 FIELD(unsigned char, preempt_count),
368 FIELD(int, pid),
369 FIELD(int, tgid));
372 static ssize_t
373 event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
374 loff_t *ppos)
376 struct ftrace_event_call *call = filp->private_data;
377 struct trace_seq *s;
378 char *buf;
379 int r;
381 s = kmalloc(sizeof(*s), GFP_KERNEL);
382 if (!s)
383 return -ENOMEM;
385 trace_seq_init(s);
387 if (*ppos)
388 return 0;
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);
398 if (!r) {
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,
404 buf, strlen(buf));
405 goto out;
408 r = simple_read_from_buffer(ubuf, cnt, ppos,
409 s->buffer, s->len);
410 out:
411 kfree(s);
412 return r;
415 static const struct seq_operations show_event_seq_ops = {
416 .start = t_start,
417 .next = t_next,
418 .show = t_show,
419 .stop = t_stop,
422 static const struct seq_operations show_set_event_seq_ops = {
423 .start = s_start,
424 .next = s_next,
425 .show = t_show,
426 .stop = t_stop,
429 static const struct file_operations ftrace_avail_fops = {
430 .open = ftrace_event_seq_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = seq_release,
436 static const struct file_operations ftrace_set_event_fops = {
437 .open = ftrace_event_seq_open,
438 .read = seq_read,
439 .write = ftrace_event_write,
440 .llseek = seq_lseek,
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;
460 if (d_events)
461 return d_events;
463 d_tracer = tracing_init_dentry();
464 if (!d_tracer)
465 return NULL;
467 d_events = debugfs_create_dir("events", d_tracer);
468 if (!d_events)
469 pr_warning("Could not create debugfs "
470 "'events' directory\n");
472 return d_events;
475 struct event_subsystem {
476 struct list_head list;
477 const char *name;
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);
496 if (!system) {
497 pr_warning("No memory to create event subsystem %s\n",
498 name);
499 return d_events;
502 system->entry = debugfs_create_dir(name, d_events);
503 if (!system->entry) {
504 pr_warning("Could not create event subsystem %s\n",
505 name);
506 kfree(system);
507 return d_events;
510 system->name = name;
511 list_add(&system->list, &event_subsystems);
513 return system->entry;
516 static int
517 event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
519 struct dentry *entry;
520 int ret;
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();
531 if (ret < 0) {
532 pr_warning("Could not initialize trace point"
533 " events/%s\n", call->name);
534 return ret;
538 call->dir = debugfs_create_dir(call->name, d_events);
539 if (!call->dir) {
540 pr_warning("Could not create debugfs "
541 "'%s' directory\n", call->name);
542 return -1;
545 if (call->regfunc) {
546 entry = debugfs_create_file("enable", 0644, call->dir, call,
547 &ftrace_enable_fops);
548 if (!entry)
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)
555 return 0;
557 entry = debugfs_create_file("format", 0444, call->dir, call,
558 &ftrace_event_format_fops);
559 if (!entry)
560 pr_warning("Could not create debugfs "
561 "'%s/format' entry\n", call->name);
563 return 0;
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();
574 if (!d_tracer)
575 return 0;
577 entry = debugfs_create_file("available_events", 0444, d_tracer,
578 (void *)&show_event_seq_ops,
579 &ftrace_avail_fops);
580 if (!entry)
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);
587 if (!entry)
588 pr_warning("Could not create debugfs "
589 "'set_event' entry\n");
591 d_events = event_trace_events_dir();
592 if (!d_events)
593 return 0;
595 events_for_each(call) {
596 /* The linker may leave blanks */
597 if (!call->name)
598 continue;
599 event_create_dir(call, d_events);
602 return 0;
604 fs_initcall(event_trace_init);