blob: a4b177720a6ce20b4b5d062a4f8bfc071f0637c4 [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
Steven Rostedta59fd602009-04-10 13:52:20 -040022LIST_HEAD(ftrace_events);
23
Tom Zanussicf027f62009-03-22 03:30:39 -050024int trace_define_field(struct ftrace_event_call *call, char *type,
25 char *name, int offset, int size)
26{
27 struct ftrace_event_field *field;
28
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010029 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050030 if (!field)
31 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010032
Tom Zanussicf027f62009-03-22 03:30:39 -050033 field->name = kstrdup(name, GFP_KERNEL);
34 if (!field->name)
35 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010036
Tom Zanussicf027f62009-03-22 03:30:39 -050037 field->type = kstrdup(type, GFP_KERNEL);
38 if (!field->type)
39 goto err;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010040
Tom Zanussicf027f62009-03-22 03:30:39 -050041 field->offset = offset;
42 field->size = size;
43 list_add(&field->link, &call->fields);
44
45 return 0;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010046
Tom Zanussicf027f62009-03-22 03:30:39 -050047err:
48 if (field) {
49 kfree(field->name);
50 kfree(field->type);
51 }
52 kfree(field);
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010053
Tom Zanussicf027f62009-03-22 03:30:39 -050054 return -ENOMEM;
55}
Steven Rostedt17c873e2009-04-10 18:12:50 -040056EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050057
Steven Rostedtb77e38a2009-02-24 10:21:36 -050058static void ftrace_clear_events(void)
59{
Steven Rostedta59fd602009-04-10 13:52:20 -040060 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -050061
Steven Rostedta59fd602009-04-10 13:52:20 -040062 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -050063
64 if (call->enabled) {
65 call->enabled = 0;
66 call->unregfunc();
67 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -050068 }
69}
70
Steven Rostedtfd994982009-02-28 02:41:25 -050071static void ftrace_event_enable_disable(struct ftrace_event_call *call,
72 int enable)
73{
74
75 switch (enable) {
76 case 0:
77 if (call->enabled) {
78 call->enabled = 0;
79 call->unregfunc();
80 }
Steven Rostedtfd994982009-02-28 02:41:25 -050081 break;
82 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -040083 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -050084 call->enabled = 1;
85 call->regfunc();
86 }
Steven Rostedtfd994982009-02-28 02:41:25 -050087 break;
88 }
89}
90
Steven Rostedtb77e38a2009-02-24 10:21:36 -050091static int ftrace_set_clr_event(char *buf, int set)
92{
Steven Rostedta59fd602009-04-10 13:52:20 -040093 struct ftrace_event_call *call;
Steven Rostedtb628b3e2009-02-27 23:32:58 -050094 char *event = NULL, *sub = NULL, *match;
95 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -050096
Steven Rostedtb628b3e2009-02-27 23:32:58 -050097 /*
98 * The buf format can be <subsystem>:<event-name>
99 * *:<event-name> means any event by that name.
100 * :<event-name> is the same.
101 *
102 * <subsystem>:* means all events in that subsystem
103 * <subsystem>: means the same.
104 *
105 * <name> (no ':') means all events in a subsystem with
106 * the name <name> or any event that matches <name>
107 */
108
109 match = strsep(&buf, ":");
110 if (buf) {
111 sub = match;
112 event = buf;
113 match = NULL;
114
115 if (!strlen(sub) || strcmp(sub, "*") == 0)
116 sub = NULL;
117 if (!strlen(event) || strcmp(event, "*") == 0)
118 event = NULL;
119 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500120
Steven Rostedt11a241a2009-03-02 11:49:04 -0500121 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400122 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500123
Steven Rostedt40e26812009-03-10 11:32:40 -0400124 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500125 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500126
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500127 if (match &&
128 strcmp(match, call->name) != 0 &&
129 strcmp(match, call->system) != 0)
130 continue;
131
132 if (sub && strcmp(sub, call->system) != 0)
133 continue;
134
135 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500136 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500137
Steven Rostedtfd994982009-02-28 02:41:25 -0500138 ftrace_event_enable_disable(call, set);
139
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500140 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500141 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500142 mutex_unlock(&event_mutex);
143
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500144 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500145}
146
147/* 128 should be much more than enough */
148#define EVENT_BUF_SIZE 127
149
150static ssize_t
151ftrace_event_write(struct file *file, const char __user *ubuf,
152 size_t cnt, loff_t *ppos)
153{
154 size_t read = 0;
155 int i, set = 1;
156 ssize_t ret;
157 char *buf;
158 char ch;
159
160 if (!cnt || cnt < 0)
161 return 0;
162
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400163 ret = tracing_update_buffers();
164 if (ret < 0)
165 return ret;
166
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500167 ret = get_user(ch, ubuf++);
168 if (ret)
169 return ret;
170 read++;
171 cnt--;
172
173 /* skip white space */
174 while (cnt && isspace(ch)) {
175 ret = get_user(ch, ubuf++);
176 if (ret)
177 return ret;
178 read++;
179 cnt--;
180 }
181
182 /* Only white space found? */
183 if (isspace(ch)) {
184 file->f_pos += read;
185 ret = read;
186 return ret;
187 }
188
189 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
190 if (!buf)
191 return -ENOMEM;
192
193 if (cnt > EVENT_BUF_SIZE)
194 cnt = EVENT_BUF_SIZE;
195
196 i = 0;
197 while (cnt && !isspace(ch)) {
198 if (!i && ch == '!')
199 set = 0;
200 else
201 buf[i++] = ch;
202
203 ret = get_user(ch, ubuf++);
204 if (ret)
205 goto out_free;
206 read++;
207 cnt--;
208 }
209 buf[i] = 0;
210
211 file->f_pos += read;
212
213 ret = ftrace_set_clr_event(buf, set);
214 if (ret)
215 goto out_free;
216
217 ret = read;
218
219 out_free:
220 kfree(buf);
221
222 return ret;
223}
224
225static void *
226t_next(struct seq_file *m, void *v, loff_t *pos)
227{
Steven Rostedta59fd602009-04-10 13:52:20 -0400228 struct list_head *list = m->private;
229 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500230
231 (*pos)++;
232
Steven Rostedt40e26812009-03-10 11:32:40 -0400233 for (;;) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400234 if (list == &ftrace_events)
Steven Rostedt40e26812009-03-10 11:32:40 -0400235 return NULL;
236
Steven Rostedta59fd602009-04-10 13:52:20 -0400237 call = list_entry(list, struct ftrace_event_call, list);
238
Steven Rostedt40e26812009-03-10 11:32:40 -0400239 /*
240 * The ftrace subsystem is for showing formats only.
241 * They can not be enabled or disabled via the event files.
242 */
243 if (call->regfunc)
244 break;
245
Steven Rostedta59fd602009-04-10 13:52:20 -0400246 list = list->next;
Steven Rostedt40e26812009-03-10 11:32:40 -0400247 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500248
Steven Rostedta59fd602009-04-10 13:52:20 -0400249 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500250
251 return call;
252}
253
254static void *t_start(struct seq_file *m, loff_t *pos)
255{
256 return t_next(m, NULL, pos);
257}
258
259static void *
260s_next(struct seq_file *m, void *v, loff_t *pos)
261{
Steven Rostedta59fd602009-04-10 13:52:20 -0400262 struct list_head *list = m->private;
263 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500264
265 (*pos)++;
266
267 retry:
Steven Rostedta59fd602009-04-10 13:52:20 -0400268 if (list == &ftrace_events)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500269 return NULL;
270
Steven Rostedta59fd602009-04-10 13:52:20 -0400271 call = list_entry(list, struct ftrace_event_call, list);
272
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500273 if (!call->enabled) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400274 list = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500275 goto retry;
276 }
277
Steven Rostedta59fd602009-04-10 13:52:20 -0400278 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500279
280 return call;
281}
282
283static void *s_start(struct seq_file *m, loff_t *pos)
284{
285 return s_next(m, NULL, pos);
286}
287
288static int t_show(struct seq_file *m, void *v)
289{
290 struct ftrace_event_call *call = v;
291
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500292 if (strcmp(call->system, TRACE_SYSTEM) != 0)
293 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500294 seq_printf(m, "%s\n", call->name);
295
296 return 0;
297}
298
299static void t_stop(struct seq_file *m, void *p)
300{
301}
302
303static int
304ftrace_event_seq_open(struct inode *inode, struct file *file)
305{
306 int ret;
307 const struct seq_operations *seq_ops;
308
309 if ((file->f_mode & FMODE_WRITE) &&
310 !(file->f_flags & O_APPEND))
311 ftrace_clear_events();
312
313 seq_ops = inode->i_private;
314 ret = seq_open(file, seq_ops);
315 if (!ret) {
316 struct seq_file *m = file->private_data;
317
Steven Rostedta59fd602009-04-10 13:52:20 -0400318 m->private = ftrace_events.next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500319 }
320 return ret;
321}
322
Steven Rostedt1473e442009-02-24 14:15:08 -0500323static ssize_t
324event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
325 loff_t *ppos)
326{
327 struct ftrace_event_call *call = filp->private_data;
328 char *buf;
329
Steven Rostedtda4d0302009-03-09 17:14:30 -0400330 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500331 buf = "1\n";
332 else
333 buf = "0\n";
334
335 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
336}
337
338static ssize_t
339event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
340 loff_t *ppos)
341{
342 struct ftrace_event_call *call = filp->private_data;
343 char buf[64];
344 unsigned long val;
345 int ret;
346
347 if (cnt >= sizeof(buf))
348 return -EINVAL;
349
350 if (copy_from_user(&buf, ubuf, cnt))
351 return -EFAULT;
352
353 buf[cnt] = 0;
354
355 ret = strict_strtoul(buf, 10, &val);
356 if (ret < 0)
357 return ret;
358
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400359 ret = tracing_update_buffers();
360 if (ret < 0)
361 return ret;
362
Steven Rostedt1473e442009-02-24 14:15:08 -0500363 switch (val) {
364 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500365 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500366 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500367 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500368 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500369 break;
370
371 default:
372 return -EINVAL;
373 }
374
375 *ppos += cnt;
376
377 return cnt;
378}
379
Steven Rostedt91729ef92009-03-02 15:03:01 -0500380#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500381#define FIELD(type, name) \
Tom Zanussicf027f62009-03-22 03:30:39 -0500382 #type, "common_" #name, offsetof(typeof(field), name), \
383 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500384
385static int trace_write_header(struct trace_seq *s)
386{
387 struct trace_entry field;
388
389 /* struct trace_entry */
390 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400391 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
392 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
393 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
394 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
395 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500396 "\n",
397 FIELD(unsigned char, type),
398 FIELD(unsigned char, flags),
399 FIELD(unsigned char, preempt_count),
400 FIELD(int, pid),
401 FIELD(int, tgid));
402}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400403
Steven Rostedt981d0812009-03-02 13:53:59 -0500404static ssize_t
405event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
406 loff_t *ppos)
407{
408 struct ftrace_event_call *call = filp->private_data;
409 struct trace_seq *s;
410 char *buf;
411 int r;
412
Tom Zanussic269fc82009-03-17 01:20:59 -0500413 if (*ppos)
414 return 0;
415
Steven Rostedt981d0812009-03-02 13:53:59 -0500416 s = kmalloc(sizeof(*s), GFP_KERNEL);
417 if (!s)
418 return -ENOMEM;
419
420 trace_seq_init(s);
421
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500422 /* If any of the first writes fail, so will the show_format. */
423
424 trace_seq_printf(s, "name: %s\n", call->name);
425 trace_seq_printf(s, "ID: %d\n", call->id);
426 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500427 trace_write_header(s);
428
Steven Rostedt981d0812009-03-02 13:53:59 -0500429 r = call->show_format(s);
430 if (!r) {
431 /*
432 * ug! The format output is bigger than a PAGE!!
433 */
434 buf = "FORMAT TOO BIG\n";
435 r = simple_read_from_buffer(ubuf, cnt, ppos,
436 buf, strlen(buf));
437 goto out;
438 }
439
440 r = simple_read_from_buffer(ubuf, cnt, ppos,
441 s->buffer, s->len);
442 out:
443 kfree(s);
444 return r;
445}
446
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100447static ssize_t
448event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
449{
450 struct ftrace_event_call *call = filp->private_data;
451 struct trace_seq *s;
452 int r;
453
454 if (*ppos)
455 return 0;
456
457 s = kmalloc(sizeof(*s), GFP_KERNEL);
458 if (!s)
459 return -ENOMEM;
460
461 trace_seq_init(s);
462 trace_seq_printf(s, "%d\n", call->id);
463
464 r = simple_read_from_buffer(ubuf, cnt, ppos,
465 s->buffer, s->len);
466 kfree(s);
467 return r;
468}
469
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500470static ssize_t
471event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
472 loff_t *ppos)
473{
474 struct ftrace_event_call *call = filp->private_data;
475 struct trace_seq *s;
476 int r;
477
478 if (*ppos)
479 return 0;
480
481 s = kmalloc(sizeof(*s), GFP_KERNEL);
482 if (!s)
483 return -ENOMEM;
484
485 trace_seq_init(s);
486
Tom Zanussi0a19e532009-04-13 03:17:50 -0500487 filter_print_preds(call->preds, call->n_preds, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500488 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500489
490 kfree(s);
491
492 return r;
493}
494
495static ssize_t
496event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
497 loff_t *ppos)
498{
499 struct ftrace_event_call *call = filp->private_data;
500 char buf[64], *pbuf = buf;
501 struct filter_pred *pred;
502 int err;
503
504 if (cnt >= sizeof(buf))
505 return -EINVAL;
506
507 if (copy_from_user(&buf, ubuf, cnt))
508 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800509 buf[cnt] = '\0';
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500510
511 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
512 if (!pred)
513 return -ENOMEM;
514
515 err = filter_parse(&pbuf, pred);
516 if (err < 0) {
517 filter_free_pred(pred);
518 return err;
519 }
520
521 if (pred->clear) {
Tom Zanussi0a19e532009-04-13 03:17:50 -0500522 filter_disable_preds(call);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500523 filter_free_pred(pred);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500524 return cnt;
525 }
526
Li Zefan44e9c8b2009-04-11 15:55:28 +0800527 err = filter_add_pred(call, pred);
528 if (err < 0) {
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500529 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800530 return err;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500531 }
532
Tom Zanussi0a19e532009-04-13 03:17:50 -0500533 filter_free_pred(pred);
534
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500535 *ppos += cnt;
536
537 return cnt;
538}
539
Tom Zanussicfb180f2009-03-22 03:31:17 -0500540static ssize_t
541subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
542 loff_t *ppos)
543{
544 struct event_subsystem *system = filp->private_data;
545 struct trace_seq *s;
546 int r;
547
548 if (*ppos)
549 return 0;
550
551 s = kmalloc(sizeof(*s), GFP_KERNEL);
552 if (!s)
553 return -ENOMEM;
554
555 trace_seq_init(s);
556
Tom Zanussi0a19e532009-04-13 03:17:50 -0500557 filter_print_preds(system->preds, system->n_preds, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500558 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500559
560 kfree(s);
561
562 return r;
563}
564
565static ssize_t
566subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
567 loff_t *ppos)
568{
569 struct event_subsystem *system = filp->private_data;
570 char buf[64], *pbuf = buf;
571 struct filter_pred *pred;
572 int err;
573
574 if (cnt >= sizeof(buf))
575 return -EINVAL;
576
577 if (copy_from_user(&buf, ubuf, cnt))
578 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800579 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500580
581 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
582 if (!pred)
583 return -ENOMEM;
584
585 err = filter_parse(&pbuf, pred);
586 if (err < 0) {
587 filter_free_pred(pred);
588 return err;
589 }
590
591 if (pred->clear) {
592 filter_free_subsystem_preds(system);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500593 filter_free_pred(pred);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500594 return cnt;
595 }
596
Li Zefan44e9c8b2009-04-11 15:55:28 +0800597 err = filter_add_subsystem_pred(system, pred);
598 if (err < 0) {
Tom Zanussic4cff062009-03-23 03:26:48 -0500599 filter_free_subsystem_preds(system);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500600 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800601 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500602 }
603
604 *ppos += cnt;
605
606 return cnt;
607}
608
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500609static const struct seq_operations show_event_seq_ops = {
610 .start = t_start,
611 .next = t_next,
612 .show = t_show,
613 .stop = t_stop,
614};
615
616static const struct seq_operations show_set_event_seq_ops = {
617 .start = s_start,
618 .next = s_next,
619 .show = t_show,
620 .stop = t_stop,
621};
622
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400623static const struct file_operations ftrace_avail_fops = {
624 .open = ftrace_event_seq_open,
625 .read = seq_read,
626 .llseek = seq_lseek,
627 .release = seq_release,
628};
629
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500630static const struct file_operations ftrace_set_event_fops = {
631 .open = ftrace_event_seq_open,
632 .read = seq_read,
633 .write = ftrace_event_write,
634 .llseek = seq_lseek,
635 .release = seq_release,
636};
637
Steven Rostedt1473e442009-02-24 14:15:08 -0500638static const struct file_operations ftrace_enable_fops = {
639 .open = tracing_open_generic,
640 .read = event_enable_read,
641 .write = event_enable_write,
642};
643
Steven Rostedt981d0812009-03-02 13:53:59 -0500644static const struct file_operations ftrace_event_format_fops = {
645 .open = tracing_open_generic,
646 .read = event_format_read,
647};
648
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100649static const struct file_operations ftrace_event_id_fops = {
650 .open = tracing_open_generic,
651 .read = event_id_read,
652};
653
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500654static const struct file_operations ftrace_event_filter_fops = {
655 .open = tracing_open_generic,
656 .read = event_filter_read,
657 .write = event_filter_write,
658};
659
Tom Zanussicfb180f2009-03-22 03:31:17 -0500660static const struct file_operations ftrace_subsystem_filter_fops = {
661 .open = tracing_open_generic,
662 .read = subsystem_filter_read,
663 .write = subsystem_filter_write,
664};
665
Steven Rostedt1473e442009-02-24 14:15:08 -0500666static struct dentry *event_trace_events_dir(void)
667{
668 static struct dentry *d_tracer;
669 static struct dentry *d_events;
670
671 if (d_events)
672 return d_events;
673
674 d_tracer = tracing_init_dentry();
675 if (!d_tracer)
676 return NULL;
677
678 d_events = debugfs_create_dir("events", d_tracer);
679 if (!d_events)
680 pr_warning("Could not create debugfs "
681 "'events' directory\n");
682
683 return d_events;
684}
685
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500686static LIST_HEAD(event_subsystems);
687
688static struct dentry *
689event_subsystem_dir(const char *name, struct dentry *d_events)
690{
691 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500692 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500693
694 /* First see if we did not already create this dir */
695 list_for_each_entry(system, &event_subsystems, list) {
696 if (strcmp(system->name, name) == 0)
697 return system->entry;
698 }
699
700 /* need to create new entry */
701 system = kmalloc(sizeof(*system), GFP_KERNEL);
702 if (!system) {
703 pr_warning("No memory to create event subsystem %s\n",
704 name);
705 return d_events;
706 }
707
708 system->entry = debugfs_create_dir(name, d_events);
709 if (!system->entry) {
710 pr_warning("Could not create event subsystem %s\n",
711 name);
712 kfree(system);
713 return d_events;
714 }
715
Steven Rostedt6d723732009-04-10 14:53:50 -0400716 system->name = kstrdup(name, GFP_KERNEL);
717 if (!system->name) {
718 debugfs_remove(system->entry);
719 kfree(system);
720 return d_events;
721 }
722
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500723 list_add(&system->list, &event_subsystems);
724
Tom Zanussicfb180f2009-03-22 03:31:17 -0500725 system->preds = NULL;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500726 system->n_preds = 0;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500727
Tom Zanussie1112b42009-03-31 00:48:49 -0500728 entry = debugfs_create_file("filter", 0644, system->entry, system,
729 &ftrace_subsystem_filter_fops);
730 if (!entry)
731 pr_warning("Could not create debugfs "
732 "'%s/filter' entry\n", name);
733
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500734 return system->entry;
735}
736
Steven Rostedt1473e442009-02-24 14:15:08 -0500737static int
738event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
739{
740 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500741 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500742
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500743 /*
744 * If the trace point header did not define TRACE_SYSTEM
745 * then the system would be called "TRACE_SYSTEM".
746 */
Steven Rostedt6d723732009-04-10 14:53:50 -0400747 if (strcmp(call->system, TRACE_SYSTEM) != 0)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500748 d_events = event_subsystem_dir(call->system, d_events);
749
Steven Rostedtfd994982009-02-28 02:41:25 -0500750 if (call->raw_init) {
751 ret = call->raw_init();
752 if (ret < 0) {
753 pr_warning("Could not initialize trace point"
754 " events/%s\n", call->name);
755 return ret;
756 }
757 }
758
Steven Rostedt1473e442009-02-24 14:15:08 -0500759 call->dir = debugfs_create_dir(call->name, d_events);
760 if (!call->dir) {
761 pr_warning("Could not create debugfs "
762 "'%s' directory\n", call->name);
763 return -1;
764 }
765
Steven Rostedt6d723732009-04-10 14:53:50 -0400766 if (call->regfunc)
767 entry = trace_create_file("enable", 0644, call->dir, call,
768 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -0500769
Steven Rostedt6d723732009-04-10 14:53:50 -0400770 if (call->id)
771 entry = trace_create_file("id", 0444, call->dir, call,
772 &ftrace_event_id_fops);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100773
Tom Zanussicf027f62009-03-22 03:30:39 -0500774 if (call->define_fields) {
775 ret = call->define_fields();
776 if (ret < 0) {
777 pr_warning("Could not initialize trace point"
778 " events/%s\n", call->name);
779 return ret;
780 }
Steven Rostedt6d723732009-04-10 14:53:50 -0400781 entry = trace_create_file("filter", 0644, call->dir, call,
782 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -0500783 }
784
Steven Rostedt981d0812009-03-02 13:53:59 -0500785 /* A trace may not want to export its format */
786 if (!call->show_format)
787 return 0;
788
Steven Rostedt6d723732009-04-10 14:53:50 -0400789 entry = trace_create_file("format", 0444, call->dir, call,
790 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -0500791
Steven Rostedt1473e442009-02-24 14:15:08 -0500792 return 0;
793}
794
Steven Rostedt6d723732009-04-10 14:53:50 -0400795#define for_each_event(event, start, end) \
796 for (event = start; \
797 (unsigned long)event < (unsigned long)end; \
798 event++)
799
800static void trace_module_add_events(struct module *mod)
801{
802 struct ftrace_event_call *call, *start, *end;
803 struct dentry *d_events;
804
805 start = mod->trace_events;
806 end = mod->trace_events + mod->num_trace_events;
807
808 if (start == end)
809 return;
810
811 d_events = event_trace_events_dir();
812 if (!d_events)
813 return;
814
815 for_each_event(call, start, end) {
816 /* The linker may leave blanks */
817 if (!call->name)
818 continue;
819 call->mod = mod;
820 list_add(&call->list, &ftrace_events);
821 event_create_dir(call, d_events);
822 }
823}
824
825static void trace_module_remove_events(struct module *mod)
826{
827 struct ftrace_event_call *call, *p;
828
829 list_for_each_entry_safe(call, p, &ftrace_events, list) {
830 if (call->mod == mod) {
831 if (call->enabled) {
832 call->enabled = 0;
833 call->unregfunc();
834 }
835 if (call->event)
836 unregister_ftrace_event(call->event);
837 debugfs_remove_recursive(call->dir);
838 list_del(&call->list);
839 }
840 }
841}
842
843int trace_module_notify(struct notifier_block *self,
844 unsigned long val, void *data)
845{
846 struct module *mod = data;
847
848 mutex_lock(&event_mutex);
849 switch (val) {
850 case MODULE_STATE_COMING:
851 trace_module_add_events(mod);
852 break;
853 case MODULE_STATE_GOING:
854 trace_module_remove_events(mod);
855 break;
856 }
857 mutex_unlock(&event_mutex);
858
859 return 0;
860}
861
862struct notifier_block trace_module_nb = {
863 .notifier_call = trace_module_notify,
864 .priority = 0,
865};
866
Steven Rostedta59fd602009-04-10 13:52:20 -0400867extern struct ftrace_event_call __start_ftrace_events[];
868extern struct ftrace_event_call __stop_ftrace_events[];
869
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500870static __init int event_trace_init(void)
871{
Steven Rostedta59fd602009-04-10 13:52:20 -0400872 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500873 struct dentry *d_tracer;
874 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -0500875 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -0400876 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500877
878 d_tracer = tracing_init_dentry();
879 if (!d_tracer)
880 return 0;
881
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400882 entry = debugfs_create_file("available_events", 0444, d_tracer,
883 (void *)&show_event_seq_ops,
884 &ftrace_avail_fops);
885 if (!entry)
886 pr_warning("Could not create debugfs "
887 "'available_events' entry\n");
888
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500889 entry = debugfs_create_file("set_event", 0644, d_tracer,
890 (void *)&show_set_event_seq_ops,
891 &ftrace_set_event_fops);
892 if (!entry)
893 pr_warning("Could not create debugfs "
894 "'set_event' entry\n");
895
Steven Rostedt1473e442009-02-24 14:15:08 -0500896 d_events = event_trace_events_dir();
897 if (!d_events)
898 return 0;
899
Steven Rostedt6d723732009-04-10 14:53:50 -0400900 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -0500901 /* The linker may leave blanks */
902 if (!call->name)
903 continue;
Steven Rostedta59fd602009-04-10 13:52:20 -0400904 list_add(&call->list, &ftrace_events);
Steven Rostedt1473e442009-02-24 14:15:08 -0500905 event_create_dir(call, d_events);
906 }
907
Steven Rostedt6d723732009-04-10 14:53:50 -0400908 ret = register_module_notifier(&trace_module_nb);
909 if (!ret)
910 pr_warning("Failed to register trace events module notifier\n");
911
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500912 return 0;
913}
914fs_initcall(event_trace_init);