blob: ead68ac9919138f41a0c34438d469eff5379fb77 [file] [log] [blame]
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
Steven Rostedt981d0812009-03-02 13:53:59 -05006 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
Steven Rostedtb77e38a2009-02-24 10:21:36 -05009 */
10
11#include <linux/debugfs.h>
12#include <linux/uaccess.h>
13#include <linux/module.h>
14#include <linux/ctype.h>
15
Steven Rostedt91729ef92009-03-02 15:03:01 -050016#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050017
Steven Rostedtb628b3e2009-02-27 23:32:58 -050018#define TRACE_SYSTEM "TRACE_SYSTEM"
19
Steven Rostedt11a241a2009-03-02 11:49:04 -050020static DEFINE_MUTEX(event_mutex);
21
Tom Zanussicf027f62009-03-22 03:30:39 -050022int trace_define_field(struct ftrace_event_call *call, char *type,
23 char *name, int offset, int size)
24{
25 struct ftrace_event_field *field;
26
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010027 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050028 if (!field)
29 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010030
Tom Zanussicf027f62009-03-22 03:30:39 -050031 field->name = kstrdup(name, GFP_KERNEL);
32 if (!field->name)
33 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010034
Tom Zanussicf027f62009-03-22 03:30:39 -050035 field->type = kstrdup(type, GFP_KERNEL);
36 if (!field->type)
37 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010038
Tom Zanussicf027f62009-03-22 03:30:39 -050039 field->offset = offset;
40 field->size = size;
41 list_add(&field->link, &call->fields);
42
43 return 0;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010044
Tom Zanussicf027f62009-03-22 03:30:39 -050045err:
46 if (field) {
47 kfree(field->name);
48 kfree(field->type);
49 }
50 kfree(field);
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010051
Tom Zanussicf027f62009-03-22 03:30:39 -050052 return -ENOMEM;
53}
54
Steven Rostedtb77e38a2009-02-24 10:21:36 -050055static void ftrace_clear_events(void)
56{
57 struct ftrace_event_call *call = (void *)__start_ftrace_events;
58
59
60 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
61
62 if (call->enabled) {
63 call->enabled = 0;
64 call->unregfunc();
65 }
66 call++;
67 }
68}
69
Steven Rostedtfd994982009-02-28 02:41:25 -050070static void ftrace_event_enable_disable(struct ftrace_event_call *call,
71 int enable)
72{
73
74 switch (enable) {
75 case 0:
76 if (call->enabled) {
77 call->enabled = 0;
78 call->unregfunc();
79 }
Steven Rostedtfd994982009-02-28 02:41:25 -050080 break;
81 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -040082 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -050083 call->enabled = 1;
84 call->regfunc();
85 }
Steven Rostedtfd994982009-02-28 02:41:25 -050086 break;
87 }
88}
89
Steven Rostedtb77e38a2009-02-24 10:21:36 -050090static int ftrace_set_clr_event(char *buf, int set)
91{
Steven Rostedt1473e442009-02-24 14:15:08 -050092 struct ftrace_event_call *call = __start_ftrace_events;
Steven Rostedtb628b3e2009-02-27 23:32:58 -050093 char *event = NULL, *sub = NULL, *match;
94 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -050095
Steven Rostedtb628b3e2009-02-27 23:32:58 -050096 /*
97 * The buf format can be <subsystem>:<event-name>
98 * *:<event-name> means any event by that name.
99 * :<event-name> is the same.
100 *
101 * <subsystem>:* means all events in that subsystem
102 * <subsystem>: means the same.
103 *
104 * <name> (no ':') means all events in a subsystem with
105 * the name <name> or any event that matches <name>
106 */
107
108 match = strsep(&buf, ":");
109 if (buf) {
110 sub = match;
111 event = buf;
112 match = NULL;
113
114 if (!strlen(sub) || strcmp(sub, "*") == 0)
115 sub = NULL;
116 if (!strlen(event) || strcmp(event, "*") == 0)
117 event = NULL;
118 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500119
Steven Rostedt11a241a2009-03-02 11:49:04 -0500120 mutex_lock(&event_mutex);
Peter Zijlstraac199db2009-03-19 20:26:15 +0100121 for_each_event(call) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500122
Steven Rostedt40e26812009-03-10 11:32:40 -0400123 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500124 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500125
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500126 if (match &&
127 strcmp(match, call->name) != 0 &&
128 strcmp(match, call->system) != 0)
129 continue;
130
131 if (sub && strcmp(sub, call->system) != 0)
132 continue;
133
134 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500135 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500136
Steven Rostedtfd994982009-02-28 02:41:25 -0500137 ftrace_event_enable_disable(call, set);
138
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500139 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500140 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500141 mutex_unlock(&event_mutex);
142
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500143 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500144}
145
146/* 128 should be much more than enough */
147#define EVENT_BUF_SIZE 127
148
149static ssize_t
150ftrace_event_write(struct file *file, const char __user *ubuf,
151 size_t cnt, loff_t *ppos)
152{
153 size_t read = 0;
154 int i, set = 1;
155 ssize_t ret;
156 char *buf;
157 char ch;
158
159 if (!cnt || cnt < 0)
160 return 0;
161
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400162 ret = tracing_update_buffers();
163 if (ret < 0)
164 return ret;
165
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500166 ret = get_user(ch, ubuf++);
167 if (ret)
168 return ret;
169 read++;
170 cnt--;
171
172 /* skip white space */
173 while (cnt && isspace(ch)) {
174 ret = get_user(ch, ubuf++);
175 if (ret)
176 return ret;
177 read++;
178 cnt--;
179 }
180
181 /* Only white space found? */
182 if (isspace(ch)) {
183 file->f_pos += read;
184 ret = read;
185 return ret;
186 }
187
188 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
189 if (!buf)
190 return -ENOMEM;
191
192 if (cnt > EVENT_BUF_SIZE)
193 cnt = EVENT_BUF_SIZE;
194
195 i = 0;
196 while (cnt && !isspace(ch)) {
197 if (!i && ch == '!')
198 set = 0;
199 else
200 buf[i++] = ch;
201
202 ret = get_user(ch, ubuf++);
203 if (ret)
204 goto out_free;
205 read++;
206 cnt--;
207 }
208 buf[i] = 0;
209
210 file->f_pos += read;
211
212 ret = ftrace_set_clr_event(buf, set);
213 if (ret)
214 goto out_free;
215
216 ret = read;
217
218 out_free:
219 kfree(buf);
220
221 return ret;
222}
223
224static void *
225t_next(struct seq_file *m, void *v, loff_t *pos)
226{
227 struct ftrace_event_call *call = m->private;
228 struct ftrace_event_call *next = call;
229
230 (*pos)++;
231
Steven Rostedt40e26812009-03-10 11:32:40 -0400232 for (;;) {
233 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
234 return NULL;
235
236 /*
237 * The ftrace subsystem is for showing formats only.
238 * They can not be enabled or disabled via the event files.
239 */
240 if (call->regfunc)
241 break;
242
243 call++;
244 next = call;
245 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500246
247 m->private = ++next;
248
249 return call;
250}
251
252static void *t_start(struct seq_file *m, loff_t *pos)
253{
254 return t_next(m, NULL, pos);
255}
256
257static void *
258s_next(struct seq_file *m, void *v, loff_t *pos)
259{
260 struct ftrace_event_call *call = m->private;
261 struct ftrace_event_call *next;
262
263 (*pos)++;
264
265 retry:
266 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
267 return NULL;
268
269 if (!call->enabled) {
270 call++;
271 goto retry;
272 }
273
274 next = call;
275 m->private = ++next;
276
277 return call;
278}
279
280static void *s_start(struct seq_file *m, loff_t *pos)
281{
282 return s_next(m, NULL, pos);
283}
284
285static int t_show(struct seq_file *m, void *v)
286{
287 struct ftrace_event_call *call = v;
288
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500289 if (strcmp(call->system, TRACE_SYSTEM) != 0)
290 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500291 seq_printf(m, "%s\n", call->name);
292
293 return 0;
294}
295
296static void t_stop(struct seq_file *m, void *p)
297{
298}
299
300static int
301ftrace_event_seq_open(struct inode *inode, struct file *file)
302{
303 int ret;
304 const struct seq_operations *seq_ops;
305
306 if ((file->f_mode & FMODE_WRITE) &&
307 !(file->f_flags & O_APPEND))
308 ftrace_clear_events();
309
310 seq_ops = inode->i_private;
311 ret = seq_open(file, seq_ops);
312 if (!ret) {
313 struct seq_file *m = file->private_data;
314
315 m->private = __start_ftrace_events;
316 }
317 return ret;
318}
319
Steven Rostedt1473e442009-02-24 14:15:08 -0500320static ssize_t
321event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
322 loff_t *ppos)
323{
324 struct ftrace_event_call *call = filp->private_data;
325 char *buf;
326
Steven Rostedtda4d0302009-03-09 17:14:30 -0400327 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500328 buf = "1\n";
329 else
330 buf = "0\n";
331
332 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
333}
334
335static ssize_t
336event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
337 loff_t *ppos)
338{
339 struct ftrace_event_call *call = filp->private_data;
340 char buf[64];
341 unsigned long val;
342 int ret;
343
344 if (cnt >= sizeof(buf))
345 return -EINVAL;
346
347 if (copy_from_user(&buf, ubuf, cnt))
348 return -EFAULT;
349
350 buf[cnt] = 0;
351
352 ret = strict_strtoul(buf, 10, &val);
353 if (ret < 0)
354 return ret;
355
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400356 ret = tracing_update_buffers();
357 if (ret < 0)
358 return ret;
359
Steven Rostedt1473e442009-02-24 14:15:08 -0500360 switch (val) {
361 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500362 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500363 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500364 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500365 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500366 break;
367
368 default:
369 return -EINVAL;
370 }
371
372 *ppos += cnt;
373
374 return cnt;
375}
376
Steven Rostedt91729ef92009-03-02 15:03:01 -0500377#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500378#define FIELD(type, name) \
Tom Zanussicf027f62009-03-22 03:30:39 -0500379 #type, "common_" #name, offsetof(typeof(field), name), \
380 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500381
382static int trace_write_header(struct trace_seq *s)
383{
384 struct trace_entry field;
385
386 /* struct trace_entry */
387 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400388 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
389 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
390 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
391 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500393 "\n",
394 FIELD(unsigned char, type),
395 FIELD(unsigned char, flags),
396 FIELD(unsigned char, preempt_count),
397 FIELD(int, pid),
398 FIELD(int, tgid));
399}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400400
Steven Rostedt981d0812009-03-02 13:53:59 -0500401static ssize_t
402event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
403 loff_t *ppos)
404{
405 struct ftrace_event_call *call = filp->private_data;
406 struct trace_seq *s;
407 char *buf;
408 int r;
409
Tom Zanussic269fc82009-03-17 01:20:59 -0500410 if (*ppos)
411 return 0;
412
Steven Rostedt981d0812009-03-02 13:53:59 -0500413 s = kmalloc(sizeof(*s), GFP_KERNEL);
414 if (!s)
415 return -ENOMEM;
416
417 trace_seq_init(s);
418
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500419 /* If any of the first writes fail, so will the show_format. */
420
421 trace_seq_printf(s, "name: %s\n", call->name);
422 trace_seq_printf(s, "ID: %d\n", call->id);
423 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500424 trace_write_header(s);
425
Steven Rostedt981d0812009-03-02 13:53:59 -0500426 r = call->show_format(s);
427 if (!r) {
428 /*
429 * ug! The format output is bigger than a PAGE!!
430 */
431 buf = "FORMAT TOO BIG\n";
432 r = simple_read_from_buffer(ubuf, cnt, ppos,
433 buf, strlen(buf));
434 goto out;
435 }
436
437 r = simple_read_from_buffer(ubuf, cnt, ppos,
438 s->buffer, s->len);
439 out:
440 kfree(s);
441 return r;
442}
443
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100444static ssize_t
445event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
446{
447 struct ftrace_event_call *call = filp->private_data;
448 struct trace_seq *s;
449 int r;
450
451 if (*ppos)
452 return 0;
453
454 s = kmalloc(sizeof(*s), GFP_KERNEL);
455 if (!s)
456 return -ENOMEM;
457
458 trace_seq_init(s);
459 trace_seq_printf(s, "%d\n", call->id);
460
461 r = simple_read_from_buffer(ubuf, cnt, ppos,
462 s->buffer, s->len);
463 kfree(s);
464 return r;
465}
466
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500467static ssize_t
468event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
469 loff_t *ppos)
470{
471 struct ftrace_event_call *call = filp->private_data;
472 struct trace_seq *s;
473 int r;
474
475 if (*ppos)
476 return 0;
477
478 s = kmalloc(sizeof(*s), GFP_KERNEL);
479 if (!s)
480 return -ENOMEM;
481
482 trace_seq_init(s);
483
Tom Zanussi0a19e532009-04-13 03:17:50 -0500484 filter_print_preds(call->preds, call->n_preds, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500485 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500486
487 kfree(s);
488
489 return r;
490}
491
492static ssize_t
493event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
494 loff_t *ppos)
495{
496 struct ftrace_event_call *call = filp->private_data;
497 char buf[64], *pbuf = buf;
498 struct filter_pred *pred;
499 int err;
500
501 if (cnt >= sizeof(buf))
502 return -EINVAL;
503
504 if (copy_from_user(&buf, ubuf, cnt))
505 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800506 buf[cnt] = '\0';
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500507
508 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
509 if (!pred)
510 return -ENOMEM;
511
512 err = filter_parse(&pbuf, pred);
513 if (err < 0) {
514 filter_free_pred(pred);
515 return err;
516 }
517
518 if (pred->clear) {
Tom Zanussi0a19e532009-04-13 03:17:50 -0500519 filter_disable_preds(call);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500520 filter_free_pred(pred);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500521 return cnt;
522 }
523
Li Zefan44e9c8b2009-04-11 15:55:28 +0800524 err = filter_add_pred(call, pred);
525 if (err < 0) {
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500526 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800527 return err;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500528 }
529
Tom Zanussi0a19e532009-04-13 03:17:50 -0500530 filter_free_pred(pred);
531
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500532 *ppos += cnt;
533
534 return cnt;
535}
536
Tom Zanussicfb180f2009-03-22 03:31:17 -0500537static ssize_t
538subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
539 loff_t *ppos)
540{
541 struct event_subsystem *system = filp->private_data;
542 struct trace_seq *s;
543 int r;
544
545 if (*ppos)
546 return 0;
547
548 s = kmalloc(sizeof(*s), GFP_KERNEL);
549 if (!s)
550 return -ENOMEM;
551
552 trace_seq_init(s);
553
Tom Zanussi0a19e532009-04-13 03:17:50 -0500554 filter_print_preds(system->preds, system->n_preds, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500555 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500556
557 kfree(s);
558
559 return r;
560}
561
562static ssize_t
563subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
564 loff_t *ppos)
565{
566 struct event_subsystem *system = filp->private_data;
567 char buf[64], *pbuf = buf;
568 struct filter_pred *pred;
569 int err;
570
571 if (cnt >= sizeof(buf))
572 return -EINVAL;
573
574 if (copy_from_user(&buf, ubuf, cnt))
575 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800576 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500577
578 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
579 if (!pred)
580 return -ENOMEM;
581
582 err = filter_parse(&pbuf, pred);
583 if (err < 0) {
584 filter_free_pred(pred);
585 return err;
586 }
587
588 if (pred->clear) {
589 filter_free_subsystem_preds(system);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500590 filter_free_pred(pred);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500591 return cnt;
592 }
593
Li Zefan44e9c8b2009-04-11 15:55:28 +0800594 err = filter_add_subsystem_pred(system, pred);
595 if (err < 0) {
Tom Zanussic4cff062009-03-23 03:26:48 -0500596 filter_free_subsystem_preds(system);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500597 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800598 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500599 }
600
601 *ppos += cnt;
602
603 return cnt;
604}
605
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500606static const struct seq_operations show_event_seq_ops = {
607 .start = t_start,
608 .next = t_next,
609 .show = t_show,
610 .stop = t_stop,
611};
612
613static const struct seq_operations show_set_event_seq_ops = {
614 .start = s_start,
615 .next = s_next,
616 .show = t_show,
617 .stop = t_stop,
618};
619
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400620static const struct file_operations ftrace_avail_fops = {
621 .open = ftrace_event_seq_open,
622 .read = seq_read,
623 .llseek = seq_lseek,
624 .release = seq_release,
625};
626
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500627static const struct file_operations ftrace_set_event_fops = {
628 .open = ftrace_event_seq_open,
629 .read = seq_read,
630 .write = ftrace_event_write,
631 .llseek = seq_lseek,
632 .release = seq_release,
633};
634
Steven Rostedt1473e442009-02-24 14:15:08 -0500635static const struct file_operations ftrace_enable_fops = {
636 .open = tracing_open_generic,
637 .read = event_enable_read,
638 .write = event_enable_write,
639};
640
Steven Rostedt981d0812009-03-02 13:53:59 -0500641static const struct file_operations ftrace_event_format_fops = {
642 .open = tracing_open_generic,
643 .read = event_format_read,
644};
645
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100646static const struct file_operations ftrace_event_id_fops = {
647 .open = tracing_open_generic,
648 .read = event_id_read,
649};
650
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500651static const struct file_operations ftrace_event_filter_fops = {
652 .open = tracing_open_generic,
653 .read = event_filter_read,
654 .write = event_filter_write,
655};
656
Tom Zanussicfb180f2009-03-22 03:31:17 -0500657static const struct file_operations ftrace_subsystem_filter_fops = {
658 .open = tracing_open_generic,
659 .read = subsystem_filter_read,
660 .write = subsystem_filter_write,
661};
662
Steven Rostedt1473e442009-02-24 14:15:08 -0500663static struct dentry *event_trace_events_dir(void)
664{
665 static struct dentry *d_tracer;
666 static struct dentry *d_events;
667
668 if (d_events)
669 return d_events;
670
671 d_tracer = tracing_init_dentry();
672 if (!d_tracer)
673 return NULL;
674
675 d_events = debugfs_create_dir("events", d_tracer);
676 if (!d_events)
677 pr_warning("Could not create debugfs "
678 "'events' directory\n");
679
680 return d_events;
681}
682
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500683static LIST_HEAD(event_subsystems);
684
685static struct dentry *
686event_subsystem_dir(const char *name, struct dentry *d_events)
687{
688 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500689 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500690
691 /* First see if we did not already create this dir */
692 list_for_each_entry(system, &event_subsystems, list) {
693 if (strcmp(system->name, name) == 0)
694 return system->entry;
695 }
696
697 /* need to create new entry */
698 system = kmalloc(sizeof(*system), GFP_KERNEL);
699 if (!system) {
700 pr_warning("No memory to create event subsystem %s\n",
701 name);
702 return d_events;
703 }
704
705 system->entry = debugfs_create_dir(name, d_events);
706 if (!system->entry) {
707 pr_warning("Could not create event subsystem %s\n",
708 name);
709 kfree(system);
710 return d_events;
711 }
712
713 system->name = name;
714 list_add(&system->list, &event_subsystems);
715
Tom Zanussicfb180f2009-03-22 03:31:17 -0500716 system->preds = NULL;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500717 system->n_preds = 0;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500718
Tom Zanussie1112b42009-03-31 00:48:49 -0500719 entry = debugfs_create_file("filter", 0644, system->entry, system,
720 &ftrace_subsystem_filter_fops);
721 if (!entry)
722 pr_warning("Could not create debugfs "
723 "'%s/filter' entry\n", name);
724
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500725 return system->entry;
726}
727
Steven Rostedt1473e442009-02-24 14:15:08 -0500728static int
729event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
730{
731 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500732 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500733
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500734 /*
735 * If the trace point header did not define TRACE_SYSTEM
736 * then the system would be called "TRACE_SYSTEM".
737 */
738 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
739 d_events = event_subsystem_dir(call->system, d_events);
740
Steven Rostedtfd994982009-02-28 02:41:25 -0500741 if (call->raw_init) {
742 ret = call->raw_init();
743 if (ret < 0) {
744 pr_warning("Could not initialize trace point"
745 " events/%s\n", call->name);
746 return ret;
747 }
748 }
749
Steven Rostedt1473e442009-02-24 14:15:08 -0500750 call->dir = debugfs_create_dir(call->name, d_events);
751 if (!call->dir) {
752 pr_warning("Could not create debugfs "
753 "'%s' directory\n", call->name);
754 return -1;
755 }
756
Steven Rostedt770cb242009-03-05 21:35:29 -0500757 if (call->regfunc) {
758 entry = debugfs_create_file("enable", 0644, call->dir, call,
759 &ftrace_enable_fops);
760 if (!entry)
761 pr_warning("Could not create debugfs "
762 "'%s/enable' entry\n", call->name);
763 }
Steven Rostedt1473e442009-02-24 14:15:08 -0500764
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100765 if (call->id) {
766 entry = debugfs_create_file("id", 0444, call->dir, call,
767 &ftrace_event_id_fops);
768 if (!entry)
769 pr_warning("Could not create debugfs '%s/id' entry\n",
770 call->name);
771 }
772
Tom Zanussicf027f62009-03-22 03:30:39 -0500773 if (call->define_fields) {
774 ret = call->define_fields();
775 if (ret < 0) {
776 pr_warning("Could not initialize trace point"
777 " events/%s\n", call->name);
778 return ret;
779 }
Tom Zanussi9a8118b2009-03-26 01:24:34 -0500780 entry = debugfs_create_file("filter", 0644, call->dir, call,
781 &ftrace_event_filter_fops);
782 if (!entry)
783 pr_warning("Could not create debugfs "
784 "'%s/filter' entry\n", call->name);
Tom Zanussicf027f62009-03-22 03:30:39 -0500785 }
786
Steven Rostedt981d0812009-03-02 13:53:59 -0500787 /* A trace may not want to export its format */
788 if (!call->show_format)
789 return 0;
790
791 entry = debugfs_create_file("format", 0444, call->dir, call,
792 &ftrace_event_format_fops);
793 if (!entry)
794 pr_warning("Could not create debugfs "
795 "'%s/format' entry\n", call->name);
Steven Rostedtfd994982009-02-28 02:41:25 -0500796
Steven Rostedt1473e442009-02-24 14:15:08 -0500797 return 0;
798}
799
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500800static __init int event_trace_init(void)
801{
Steven Rostedt1473e442009-02-24 14:15:08 -0500802 struct ftrace_event_call *call = __start_ftrace_events;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500803 struct dentry *d_tracer;
804 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -0500805 struct dentry *d_events;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500806
807 d_tracer = tracing_init_dentry();
808 if (!d_tracer)
809 return 0;
810
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400811 entry = debugfs_create_file("available_events", 0444, d_tracer,
812 (void *)&show_event_seq_ops,
813 &ftrace_avail_fops);
814 if (!entry)
815 pr_warning("Could not create debugfs "
816 "'available_events' entry\n");
817
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500818 entry = debugfs_create_file("set_event", 0644, d_tracer,
819 (void *)&show_set_event_seq_ops,
820 &ftrace_set_event_fops);
821 if (!entry)
822 pr_warning("Could not create debugfs "
823 "'set_event' entry\n");
824
Steven Rostedt1473e442009-02-24 14:15:08 -0500825 d_events = event_trace_events_dir();
826 if (!d_events)
827 return 0;
828
Peter Zijlstraac199db2009-03-19 20:26:15 +0100829 for_each_event(call) {
Steven Rostedt1473e442009-02-24 14:15:08 -0500830 /* The linker may leave blanks */
831 if (!call->name)
832 continue;
833 event_create_dir(call, d_events);
834 }
835
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500836 return 0;
837}
838fs_initcall(event_trace_init);