blob: b92081588088dbe4451c25fa94866c7af8cd9904 [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
Steven Rostedte6187002009-04-15 13:36:40 -040011#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050014#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
Steven Rostedte6187002009-04-15 13:36:40 -040018#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050019
Steven Rostedt91729ef92009-03-02 15:03:01 -050020#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050021
Steven Rostedtb628b3e2009-02-27 23:32:58 -050022#define TRACE_SYSTEM "TRACE_SYSTEM"
23
Steven Rostedt11a241a2009-03-02 11:49:04 -050024static DEFINE_MUTEX(event_mutex);
25
Steven Rostedta59fd602009-04-10 13:52:20 -040026LIST_HEAD(ftrace_events);
27
Tom Zanussicf027f62009-03-22 03:30:39 -050028int trace_define_field(struct ftrace_event_call *call, char *type,
29 char *name, int offset, int size)
30{
31 struct ftrace_event_field *field;
32
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010033 field = kzalloc(sizeof(*field), GFP_KERNEL);
Tom Zanussicf027f62009-03-22 03:30:39 -050034 if (!field)
35 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010036
Tom Zanussicf027f62009-03-22 03:30:39 -050037 field->name = kstrdup(name, GFP_KERNEL);
38 if (!field->name)
39 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010040
Tom Zanussicf027f62009-03-22 03:30:39 -050041 field->type = kstrdup(type, GFP_KERNEL);
42 if (!field->type)
43 goto err;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010044
Tom Zanussicf027f62009-03-22 03:30:39 -050045 field->offset = offset;
46 field->size = size;
47 list_add(&field->link, &call->fields);
48
49 return 0;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010050
Tom Zanussicf027f62009-03-22 03:30:39 -050051err:
52 if (field) {
53 kfree(field->name);
54 kfree(field->type);
55 }
56 kfree(field);
Ingo Molnarfe9f57f22009-03-22 18:41:59 +010057
Tom Zanussicf027f62009-03-22 03:30:39 -050058 return -ENOMEM;
59}
Steven Rostedt17c873e2009-04-10 18:12:50 -040060EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050061
Steven Rostedtb77e38a2009-02-24 10:21:36 -050062static void ftrace_clear_events(void)
63{
Steven Rostedta59fd602009-04-10 13:52:20 -040064 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -050065
Steven Rostedta59fd602009-04-10 13:52:20 -040066 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -050067
68 if (call->enabled) {
69 call->enabled = 0;
70 call->unregfunc();
71 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -050072 }
73}
74
Steven Rostedtfd994982009-02-28 02:41:25 -050075static void ftrace_event_enable_disable(struct ftrace_event_call *call,
76 int enable)
77{
78
79 switch (enable) {
80 case 0:
81 if (call->enabled) {
82 call->enabled = 0;
83 call->unregfunc();
84 }
Steven Rostedtfd994982009-02-28 02:41:25 -050085 break;
86 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -040087 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -050088 call->enabled = 1;
89 call->regfunc();
90 }
Steven Rostedtfd994982009-02-28 02:41:25 -050091 break;
92 }
93}
94
Steven Rostedtb77e38a2009-02-24 10:21:36 -050095static int ftrace_set_clr_event(char *buf, int set)
96{
Steven Rostedta59fd602009-04-10 13:52:20 -040097 struct ftrace_event_call *call;
Steven Rostedtb628b3e2009-02-27 23:32:58 -050098 char *event = NULL, *sub = NULL, *match;
99 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500100
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500101 /*
102 * The buf format can be <subsystem>:<event-name>
103 * *:<event-name> means any event by that name.
104 * :<event-name> is the same.
105 *
106 * <subsystem>:* means all events in that subsystem
107 * <subsystem>: means the same.
108 *
109 * <name> (no ':') means all events in a subsystem with
110 * the name <name> or any event that matches <name>
111 */
112
113 match = strsep(&buf, ":");
114 if (buf) {
115 sub = match;
116 event = buf;
117 match = NULL;
118
119 if (!strlen(sub) || strcmp(sub, "*") == 0)
120 sub = NULL;
121 if (!strlen(event) || strcmp(event, "*") == 0)
122 event = NULL;
123 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500124
Steven Rostedt11a241a2009-03-02 11:49:04 -0500125 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400126 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500127
Steven Rostedt40e26812009-03-10 11:32:40 -0400128 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500129 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500130
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500131 if (match &&
132 strcmp(match, call->name) != 0 &&
133 strcmp(match, call->system) != 0)
134 continue;
135
136 if (sub && strcmp(sub, call->system) != 0)
137 continue;
138
139 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500140 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500141
Steven Rostedtfd994982009-02-28 02:41:25 -0500142 ftrace_event_enable_disable(call, set);
143
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500144 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500145 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500146 mutex_unlock(&event_mutex);
147
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500148 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500149}
150
151/* 128 should be much more than enough */
152#define EVENT_BUF_SIZE 127
153
154static ssize_t
155ftrace_event_write(struct file *file, const char __user *ubuf,
156 size_t cnt, loff_t *ppos)
157{
158 size_t read = 0;
159 int i, set = 1;
160 ssize_t ret;
161 char *buf;
162 char ch;
163
164 if (!cnt || cnt < 0)
165 return 0;
166
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400167 ret = tracing_update_buffers();
168 if (ret < 0)
169 return ret;
170
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500171 ret = get_user(ch, ubuf++);
172 if (ret)
173 return ret;
174 read++;
175 cnt--;
176
177 /* skip white space */
178 while (cnt && isspace(ch)) {
179 ret = get_user(ch, ubuf++);
180 if (ret)
181 return ret;
182 read++;
183 cnt--;
184 }
185
186 /* Only white space found? */
187 if (isspace(ch)) {
188 file->f_pos += read;
189 ret = read;
190 return ret;
191 }
192
193 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
196
197 if (cnt > EVENT_BUF_SIZE)
198 cnt = EVENT_BUF_SIZE;
199
200 i = 0;
201 while (cnt && !isspace(ch)) {
202 if (!i && ch == '!')
203 set = 0;
204 else
205 buf[i++] = ch;
206
207 ret = get_user(ch, ubuf++);
208 if (ret)
209 goto out_free;
210 read++;
211 cnt--;
212 }
213 buf[i] = 0;
214
215 file->f_pos += read;
216
217 ret = ftrace_set_clr_event(buf, set);
218 if (ret)
219 goto out_free;
220
221 ret = read;
222
223 out_free:
224 kfree(buf);
225
226 return ret;
227}
228
229static void *
230t_next(struct seq_file *m, void *v, loff_t *pos)
231{
Steven Rostedta59fd602009-04-10 13:52:20 -0400232 struct list_head *list = m->private;
233 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500234
235 (*pos)++;
236
Steven Rostedt40e26812009-03-10 11:32:40 -0400237 for (;;) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400238 if (list == &ftrace_events)
Steven Rostedt40e26812009-03-10 11:32:40 -0400239 return NULL;
240
Steven Rostedta59fd602009-04-10 13:52:20 -0400241 call = list_entry(list, struct ftrace_event_call, list);
242
Steven Rostedt40e26812009-03-10 11:32:40 -0400243 /*
244 * The ftrace subsystem is for showing formats only.
245 * They can not be enabled or disabled via the event files.
246 */
247 if (call->regfunc)
248 break;
249
Steven Rostedta59fd602009-04-10 13:52:20 -0400250 list = list->next;
Steven Rostedt40e26812009-03-10 11:32:40 -0400251 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500252
Steven Rostedta59fd602009-04-10 13:52:20 -0400253 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500254
255 return call;
256}
257
258static void *t_start(struct seq_file *m, loff_t *pos)
259{
260 return t_next(m, NULL, pos);
261}
262
263static void *
264s_next(struct seq_file *m, void *v, loff_t *pos)
265{
Steven Rostedta59fd602009-04-10 13:52:20 -0400266 struct list_head *list = m->private;
267 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500268
269 (*pos)++;
270
271 retry:
Steven Rostedta59fd602009-04-10 13:52:20 -0400272 if (list == &ftrace_events)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500273 return NULL;
274
Steven Rostedta59fd602009-04-10 13:52:20 -0400275 call = list_entry(list, struct ftrace_event_call, list);
276
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500277 if (!call->enabled) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400278 list = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500279 goto retry;
280 }
281
Steven Rostedta59fd602009-04-10 13:52:20 -0400282 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500283
284 return call;
285}
286
287static void *s_start(struct seq_file *m, loff_t *pos)
288{
289 return s_next(m, NULL, pos);
290}
291
292static int t_show(struct seq_file *m, void *v)
293{
294 struct ftrace_event_call *call = v;
295
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500296 if (strcmp(call->system, TRACE_SYSTEM) != 0)
297 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500298 seq_printf(m, "%s\n", call->name);
299
300 return 0;
301}
302
303static void t_stop(struct seq_file *m, void *p)
304{
305}
306
307static int
308ftrace_event_seq_open(struct inode *inode, struct file *file)
309{
310 int ret;
311 const struct seq_operations *seq_ops;
312
313 if ((file->f_mode & FMODE_WRITE) &&
314 !(file->f_flags & O_APPEND))
315 ftrace_clear_events();
316
317 seq_ops = inode->i_private;
318 ret = seq_open(file, seq_ops);
319 if (!ret) {
320 struct seq_file *m = file->private_data;
321
Steven Rostedta59fd602009-04-10 13:52:20 -0400322 m->private = ftrace_events.next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500323 }
324 return ret;
325}
326
Steven Rostedt1473e442009-02-24 14:15:08 -0500327static ssize_t
328event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
329 loff_t *ppos)
330{
331 struct ftrace_event_call *call = filp->private_data;
332 char *buf;
333
Steven Rostedtda4d0302009-03-09 17:14:30 -0400334 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500335 buf = "1\n";
336 else
337 buf = "0\n";
338
339 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
340}
341
342static ssize_t
343event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
344 loff_t *ppos)
345{
346 struct ftrace_event_call *call = filp->private_data;
347 char buf[64];
348 unsigned long val;
349 int ret;
350
351 if (cnt >= sizeof(buf))
352 return -EINVAL;
353
354 if (copy_from_user(&buf, ubuf, cnt))
355 return -EFAULT;
356
357 buf[cnt] = 0;
358
359 ret = strict_strtoul(buf, 10, &val);
360 if (ret < 0)
361 return ret;
362
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400363 ret = tracing_update_buffers();
364 if (ret < 0)
365 return ret;
366
Steven Rostedt1473e442009-02-24 14:15:08 -0500367 switch (val) {
368 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500369 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500370 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500371 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500372 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500373 break;
374
375 default:
376 return -EINVAL;
377 }
378
379 *ppos += cnt;
380
381 return cnt;
382}
383
Steven Rostedt75db37d2009-03-26 11:43:36 -0400384extern char *__bad_type_size(void);
385
Steven Rostedt91729ef92009-03-02 15:03:01 -0500386#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500387#define FIELD(type, name) \
Steven Rostedt75db37d2009-03-26 11:43:36 -0400388 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
Tom Zanussicf027f62009-03-22 03:30:39 -0500389 #type, "common_" #name, offsetof(typeof(field), name), \
390 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500391
392static int trace_write_header(struct trace_seq *s)
393{
394 struct trace_entry field;
395
396 /* struct trace_entry */
397 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400398 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
399 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
400 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
401 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
402 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500403 "\n",
Steven Rostedt89ec0de2009-03-26 11:03:29 -0400404 FIELD(unsigned short, type),
Steven Rostedt91729ef92009-03-02 15:03:01 -0500405 FIELD(unsigned char, flags),
406 FIELD(unsigned char, preempt_count),
407 FIELD(int, pid),
408 FIELD(int, tgid));
409}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400410
Steven Rostedt981d0812009-03-02 13:53:59 -0500411static ssize_t
412event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
413 loff_t *ppos)
414{
415 struct ftrace_event_call *call = filp->private_data;
416 struct trace_seq *s;
417 char *buf;
418 int r;
419
Tom Zanussic269fc82009-03-17 01:20:59 -0500420 if (*ppos)
421 return 0;
422
Steven Rostedt981d0812009-03-02 13:53:59 -0500423 s = kmalloc(sizeof(*s), GFP_KERNEL);
424 if (!s)
425 return -ENOMEM;
426
427 trace_seq_init(s);
428
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500429 /* If any of the first writes fail, so will the show_format. */
430
431 trace_seq_printf(s, "name: %s\n", call->name);
432 trace_seq_printf(s, "ID: %d\n", call->id);
433 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500434 trace_write_header(s);
435
Steven Rostedt981d0812009-03-02 13:53:59 -0500436 r = call->show_format(s);
437 if (!r) {
438 /*
439 * ug! The format output is bigger than a PAGE!!
440 */
441 buf = "FORMAT TOO BIG\n";
442 r = simple_read_from_buffer(ubuf, cnt, ppos,
443 buf, strlen(buf));
444 goto out;
445 }
446
447 r = simple_read_from_buffer(ubuf, cnt, ppos,
448 s->buffer, s->len);
449 out:
450 kfree(s);
451 return r;
452}
453
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100454static ssize_t
455event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
456{
457 struct ftrace_event_call *call = filp->private_data;
458 struct trace_seq *s;
459 int r;
460
461 if (*ppos)
462 return 0;
463
464 s = kmalloc(sizeof(*s), GFP_KERNEL);
465 if (!s)
466 return -ENOMEM;
467
468 trace_seq_init(s);
469 trace_seq_printf(s, "%d\n", call->id);
470
471 r = simple_read_from_buffer(ubuf, cnt, ppos,
472 s->buffer, s->len);
473 kfree(s);
474 return r;
475}
476
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500477static ssize_t
478event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
479 loff_t *ppos)
480{
481 struct ftrace_event_call *call = filp->private_data;
482 struct trace_seq *s;
483 int r;
484
485 if (*ppos)
486 return 0;
487
488 s = kmalloc(sizeof(*s), GFP_KERNEL);
489 if (!s)
490 return -ENOMEM;
491
492 trace_seq_init(s);
493
Tom Zanussiac1adc52009-04-17 00:27:08 -0500494 filter_print_preds(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500495 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500496
497 kfree(s);
498
499 return r;
500}
501
502static ssize_t
503event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
504 loff_t *ppos)
505{
506 struct ftrace_event_call *call = filp->private_data;
507 char buf[64], *pbuf = buf;
508 struct filter_pred *pred;
509 int err;
510
511 if (cnt >= sizeof(buf))
512 return -EINVAL;
513
514 if (copy_from_user(&buf, ubuf, cnt))
515 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800516 buf[cnt] = '\0';
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500517
518 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
519 if (!pred)
520 return -ENOMEM;
521
522 err = filter_parse(&pbuf, pred);
523 if (err < 0) {
524 filter_free_pred(pred);
525 return err;
526 }
527
528 if (pred->clear) {
Tom Zanussi0a19e532009-04-13 03:17:50 -0500529 filter_disable_preds(call);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500530 filter_free_pred(pred);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500531 return cnt;
532 }
533
Li Zefan44e9c8b2009-04-11 15:55:28 +0800534 err = filter_add_pred(call, pred);
535 if (err < 0) {
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500536 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800537 return err;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500538 }
539
Tom Zanussi0a19e532009-04-13 03:17:50 -0500540 filter_free_pred(pred);
541
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500542 *ppos += cnt;
543
544 return cnt;
545}
546
Tom Zanussicfb180f2009-03-22 03:31:17 -0500547static ssize_t
548subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
549 loff_t *ppos)
550{
551 struct event_subsystem *system = filp->private_data;
552 struct trace_seq *s;
553 int r;
554
555 if (*ppos)
556 return 0;
557
558 s = kmalloc(sizeof(*s), GFP_KERNEL);
559 if (!s)
560 return -ENOMEM;
561
562 trace_seq_init(s);
563
Tom Zanussiac1adc52009-04-17 00:27:08 -0500564 filter_print_subsystem_preds(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500565 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500566
567 kfree(s);
568
569 return r;
570}
571
572static ssize_t
573subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
574 loff_t *ppos)
575{
576 struct event_subsystem *system = filp->private_data;
577 char buf[64], *pbuf = buf;
578 struct filter_pred *pred;
579 int err;
580
581 if (cnt >= sizeof(buf))
582 return -EINVAL;
583
584 if (copy_from_user(&buf, ubuf, cnt))
585 return -EFAULT;
Li Zefan8433a402009-04-11 15:52:18 +0800586 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500587
588 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
589 if (!pred)
590 return -ENOMEM;
591
592 err = filter_parse(&pbuf, pred);
593 if (err < 0) {
594 filter_free_pred(pred);
595 return err;
596 }
597
598 if (pred->clear) {
599 filter_free_subsystem_preds(system);
Tom Zanussi09f1f242009-03-24 02:14:11 -0500600 filter_free_pred(pred);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500601 return cnt;
602 }
603
Li Zefan44e9c8b2009-04-11 15:55:28 +0800604 err = filter_add_subsystem_pred(system, pred);
605 if (err < 0) {
Tom Zanussicfb180f2009-03-22 03:31:17 -0500606 filter_free_pred(pred);
Li Zefan44e9c8b2009-04-11 15:55:28 +0800607 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500608 }
609
610 *ppos += cnt;
611
612 return cnt;
613}
614
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400615static ssize_t
616show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
617{
618 int (*func)(struct trace_seq *s) = filp->private_data;
619 struct trace_seq *s;
620 int r;
621
622 if (*ppos)
623 return 0;
624
625 s = kmalloc(sizeof(*s), GFP_KERNEL);
626 if (!s)
627 return -ENOMEM;
628
629 trace_seq_init(s);
630
631 func(s);
632 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
633
634 kfree(s);
635
636 return r;
637}
638
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500639static const struct seq_operations show_event_seq_ops = {
640 .start = t_start,
641 .next = t_next,
642 .show = t_show,
643 .stop = t_stop,
644};
645
646static const struct seq_operations show_set_event_seq_ops = {
647 .start = s_start,
648 .next = s_next,
649 .show = t_show,
650 .stop = t_stop,
651};
652
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400653static const struct file_operations ftrace_avail_fops = {
654 .open = ftrace_event_seq_open,
655 .read = seq_read,
656 .llseek = seq_lseek,
657 .release = seq_release,
658};
659
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500660static const struct file_operations ftrace_set_event_fops = {
661 .open = ftrace_event_seq_open,
662 .read = seq_read,
663 .write = ftrace_event_write,
664 .llseek = seq_lseek,
665 .release = seq_release,
666};
667
Steven Rostedt1473e442009-02-24 14:15:08 -0500668static const struct file_operations ftrace_enable_fops = {
669 .open = tracing_open_generic,
670 .read = event_enable_read,
671 .write = event_enable_write,
672};
673
Steven Rostedt981d0812009-03-02 13:53:59 -0500674static const struct file_operations ftrace_event_format_fops = {
675 .open = tracing_open_generic,
676 .read = event_format_read,
677};
678
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100679static const struct file_operations ftrace_event_id_fops = {
680 .open = tracing_open_generic,
681 .read = event_id_read,
682};
683
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500684static const struct file_operations ftrace_event_filter_fops = {
685 .open = tracing_open_generic,
686 .read = event_filter_read,
687 .write = event_filter_write,
688};
689
Tom Zanussicfb180f2009-03-22 03:31:17 -0500690static const struct file_operations ftrace_subsystem_filter_fops = {
691 .open = tracing_open_generic,
692 .read = subsystem_filter_read,
693 .write = subsystem_filter_write,
694};
695
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400696static const struct file_operations ftrace_show_header_fops = {
697 .open = tracing_open_generic,
698 .read = show_header,
699};
700
Steven Rostedt1473e442009-02-24 14:15:08 -0500701static struct dentry *event_trace_events_dir(void)
702{
703 static struct dentry *d_tracer;
704 static struct dentry *d_events;
705
706 if (d_events)
707 return d_events;
708
709 d_tracer = tracing_init_dentry();
710 if (!d_tracer)
711 return NULL;
712
713 d_events = debugfs_create_dir("events", d_tracer);
714 if (!d_events)
715 pr_warning("Could not create debugfs "
716 "'events' directory\n");
717
718 return d_events;
719}
720
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500721static LIST_HEAD(event_subsystems);
722
723static struct dentry *
724event_subsystem_dir(const char *name, struct dentry *d_events)
725{
726 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500727 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500728
729 /* First see if we did not already create this dir */
730 list_for_each_entry(system, &event_subsystems, list) {
731 if (strcmp(system->name, name) == 0)
732 return system->entry;
733 }
734
735 /* need to create new entry */
736 system = kmalloc(sizeof(*system), GFP_KERNEL);
737 if (!system) {
738 pr_warning("No memory to create event subsystem %s\n",
739 name);
740 return d_events;
741 }
742
743 system->entry = debugfs_create_dir(name, d_events);
744 if (!system->entry) {
745 pr_warning("Could not create event subsystem %s\n",
746 name);
747 kfree(system);
748 return d_events;
749 }
750
Steven Rostedt6d723732009-04-10 14:53:50 -0400751 system->name = kstrdup(name, GFP_KERNEL);
752 if (!system->name) {
753 debugfs_remove(system->entry);
754 kfree(system);
755 return d_events;
756 }
757
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500758 list_add(&system->list, &event_subsystems);
759
Tom Zanussicfb180f2009-03-22 03:31:17 -0500760 system->preds = NULL;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500761 system->n_preds = 0;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500762
Tom Zanussie1112b42009-03-31 00:48:49 -0500763 entry = debugfs_create_file("filter", 0644, system->entry, system,
764 &ftrace_subsystem_filter_fops);
765 if (!entry)
766 pr_warning("Could not create debugfs "
767 "'%s/filter' entry\n", name);
768
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500769 return system->entry;
770}
771
Steven Rostedt1473e442009-02-24 14:15:08 -0500772static int
773event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
774{
775 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500776 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500777
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500778 /*
779 * If the trace point header did not define TRACE_SYSTEM
780 * then the system would be called "TRACE_SYSTEM".
781 */
Steven Rostedt6d723732009-04-10 14:53:50 -0400782 if (strcmp(call->system, TRACE_SYSTEM) != 0)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500783 d_events = event_subsystem_dir(call->system, d_events);
784
Steven Rostedtfd994982009-02-28 02:41:25 -0500785 if (call->raw_init) {
786 ret = call->raw_init();
787 if (ret < 0) {
788 pr_warning("Could not initialize trace point"
789 " events/%s\n", call->name);
790 return ret;
791 }
792 }
793
Steven Rostedt1473e442009-02-24 14:15:08 -0500794 call->dir = debugfs_create_dir(call->name, d_events);
795 if (!call->dir) {
796 pr_warning("Could not create debugfs "
797 "'%s' directory\n", call->name);
798 return -1;
799 }
800
Steven Rostedt6d723732009-04-10 14:53:50 -0400801 if (call->regfunc)
802 entry = trace_create_file("enable", 0644, call->dir, call,
803 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -0500804
Steven Rostedt6d723732009-04-10 14:53:50 -0400805 if (call->id)
806 entry = trace_create_file("id", 0444, call->dir, call,
807 &ftrace_event_id_fops);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100808
Tom Zanussicf027f62009-03-22 03:30:39 -0500809 if (call->define_fields) {
810 ret = call->define_fields();
811 if (ret < 0) {
812 pr_warning("Could not initialize trace point"
813 " events/%s\n", call->name);
814 return ret;
815 }
Steven Rostedt6d723732009-04-10 14:53:50 -0400816 entry = trace_create_file("filter", 0644, call->dir, call,
817 &ftrace_event_filter_fops);
Tom Zanussicf027f62009-03-22 03:30:39 -0500818 }
819
Steven Rostedt981d0812009-03-02 13:53:59 -0500820 /* A trace may not want to export its format */
821 if (!call->show_format)
822 return 0;
823
Steven Rostedt6d723732009-04-10 14:53:50 -0400824 entry = trace_create_file("format", 0444, call->dir, call,
825 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -0500826
Steven Rostedt1473e442009-02-24 14:15:08 -0500827 return 0;
828}
829
Steven Rostedt6d723732009-04-10 14:53:50 -0400830#define for_each_event(event, start, end) \
831 for (event = start; \
832 (unsigned long)event < (unsigned long)end; \
833 event++)
834
Steven Rostedt61f919a2009-04-14 18:22:32 -0400835#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -0400836static void trace_module_add_events(struct module *mod)
837{
838 struct ftrace_event_call *call, *start, *end;
839 struct dentry *d_events;
840
841 start = mod->trace_events;
842 end = mod->trace_events + mod->num_trace_events;
843
844 if (start == end)
845 return;
846
847 d_events = event_trace_events_dir();
848 if (!d_events)
849 return;
850
851 for_each_event(call, start, end) {
852 /* The linker may leave blanks */
853 if (!call->name)
854 continue;
855 call->mod = mod;
856 list_add(&call->list, &ftrace_events);
857 event_create_dir(call, d_events);
858 }
859}
860
861static void trace_module_remove_events(struct module *mod)
862{
863 struct ftrace_event_call *call, *p;
864
865 list_for_each_entry_safe(call, p, &ftrace_events, list) {
866 if (call->mod == mod) {
867 if (call->enabled) {
868 call->enabled = 0;
869 call->unregfunc();
870 }
871 if (call->event)
872 unregister_ftrace_event(call->event);
873 debugfs_remove_recursive(call->dir);
874 list_del(&call->list);
875 }
876 }
877}
878
Steven Rostedt61f919a2009-04-14 18:22:32 -0400879static int trace_module_notify(struct notifier_block *self,
880 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -0400881{
882 struct module *mod = data;
883
884 mutex_lock(&event_mutex);
885 switch (val) {
886 case MODULE_STATE_COMING:
887 trace_module_add_events(mod);
888 break;
889 case MODULE_STATE_GOING:
890 trace_module_remove_events(mod);
891 break;
892 }
893 mutex_unlock(&event_mutex);
894
895 return 0;
896}
Steven Rostedt61f919a2009-04-14 18:22:32 -0400897#else
898static int trace_module_notify(struct notifier_block *self,
899 unsigned long val, void *data)
900{
901 return 0;
902}
903#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -0400904
905struct notifier_block trace_module_nb = {
906 .notifier_call = trace_module_notify,
907 .priority = 0,
908};
909
Steven Rostedta59fd602009-04-10 13:52:20 -0400910extern struct ftrace_event_call __start_ftrace_events[];
911extern struct ftrace_event_call __stop_ftrace_events[];
912
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500913static __init int event_trace_init(void)
914{
Steven Rostedta59fd602009-04-10 13:52:20 -0400915 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500916 struct dentry *d_tracer;
917 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -0500918 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -0400919 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500920
921 d_tracer = tracing_init_dentry();
922 if (!d_tracer)
923 return 0;
924
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400925 entry = debugfs_create_file("available_events", 0444, d_tracer,
926 (void *)&show_event_seq_ops,
927 &ftrace_avail_fops);
928 if (!entry)
929 pr_warning("Could not create debugfs "
930 "'available_events' entry\n");
931
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500932 entry = debugfs_create_file("set_event", 0644, d_tracer,
933 (void *)&show_set_event_seq_ops,
934 &ftrace_set_event_fops);
935 if (!entry)
936 pr_warning("Could not create debugfs "
937 "'set_event' entry\n");
938
Steven Rostedt1473e442009-02-24 14:15:08 -0500939 d_events = event_trace_events_dir();
940 if (!d_events)
941 return 0;
942
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400943 /* ring buffer internal formats */
944 trace_create_file("header_page", 0444, d_events,
945 ring_buffer_print_page_header,
946 &ftrace_show_header_fops);
947
948 trace_create_file("header_event", 0444, d_events,
949 ring_buffer_print_entry_header,
950 &ftrace_show_header_fops);
951
Steven Rostedt6d723732009-04-10 14:53:50 -0400952 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -0500953 /* The linker may leave blanks */
954 if (!call->name)
955 continue;
Steven Rostedta59fd602009-04-10 13:52:20 -0400956 list_add(&call->list, &ftrace_events);
Steven Rostedt1473e442009-02-24 14:15:08 -0500957 event_create_dir(call, d_events);
958 }
959
Steven Rostedt6d723732009-04-10 14:53:50 -0400960 ret = register_module_notifier(&trace_module_nb);
961 if (!ret)
962 pr_warning("Failed to register trace events module notifier\n");
963
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500964 return 0;
965}
966fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -0400967
968#ifdef CONFIG_FTRACE_STARTUP_TEST
969
970static DEFINE_SPINLOCK(test_spinlock);
971static DEFINE_SPINLOCK(test_spinlock_irq);
972static DEFINE_MUTEX(test_mutex);
973
974static __init void test_work(struct work_struct *dummy)
975{
976 spin_lock(&test_spinlock);
977 spin_lock_irq(&test_spinlock_irq);
978 udelay(1);
979 spin_unlock_irq(&test_spinlock_irq);
980 spin_unlock(&test_spinlock);
981
982 mutex_lock(&test_mutex);
983 msleep(1);
984 mutex_unlock(&test_mutex);
985}
986
987static __init int event_test_thread(void *unused)
988{
989 void *test_malloc;
990
991 test_malloc = kmalloc(1234, GFP_KERNEL);
992 if (!test_malloc)
993 pr_info("failed to kmalloc\n");
994
995 schedule_on_each_cpu(test_work);
996
997 kfree(test_malloc);
998
999 set_current_state(TASK_INTERRUPTIBLE);
1000 while (!kthread_should_stop())
1001 schedule();
1002
1003 return 0;
1004}
1005
1006/*
1007 * Do various things that may trigger events.
1008 */
1009static __init void event_test_stuff(void)
1010{
1011 struct task_struct *test_thread;
1012
1013 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1014 msleep(1);
1015 kthread_stop(test_thread);
1016}
1017
1018/*
1019 * For every trace event defined, we will test each trace point separately,
1020 * and then by groups, and finally all trace points.
1021 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001022static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04001023{
1024 struct ftrace_event_call *call;
1025 struct event_subsystem *system;
1026 char *sysname;
1027 int ret;
1028
1029 pr_info("Running tests on trace events:\n");
1030
1031 list_for_each_entry(call, &ftrace_events, list) {
1032
1033 /* Only test those that have a regfunc */
1034 if (!call->regfunc)
1035 continue;
1036
1037 pr_info("Testing event %s: ", call->name);
1038
1039 /*
1040 * If an event is already enabled, someone is using
1041 * it and the self test should not be on.
1042 */
1043 if (call->enabled) {
1044 pr_warning("Enabled event during self test!\n");
1045 WARN_ON_ONCE(1);
1046 continue;
1047 }
1048
1049 call->enabled = 1;
1050 call->regfunc();
1051
1052 event_test_stuff();
1053
1054 call->unregfunc();
1055 call->enabled = 0;
1056
1057 pr_cont("OK\n");
1058 }
1059
1060 /* Now test at the sub system level */
1061
1062 pr_info("Running tests on trace event systems:\n");
1063
1064 list_for_each_entry(system, &event_subsystems, list) {
1065
1066 /* the ftrace system is special, skip it */
1067 if (strcmp(system->name, "ftrace") == 0)
1068 continue;
1069
1070 pr_info("Testing event system %s: ", system->name);
1071
1072 /* ftrace_set_clr_event can modify the name passed in. */
1073 sysname = kstrdup(system->name, GFP_KERNEL);
1074 if (WARN_ON(!sysname)) {
1075 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001076 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001077 }
1078 ret = ftrace_set_clr_event(sysname, 1);
1079 kfree(sysname);
1080 if (WARN_ON_ONCE(ret)) {
1081 pr_warning("error enabling system %s\n",
1082 system->name);
1083 continue;
1084 }
1085
1086 event_test_stuff();
1087
1088 sysname = kstrdup(system->name, GFP_KERNEL);
1089 if (WARN_ON(!sysname)) {
1090 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001091 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001092 }
1093 ret = ftrace_set_clr_event(sysname, 0);
1094 kfree(sysname);
1095
1096 if (WARN_ON_ONCE(ret))
1097 pr_warning("error disabling system %s\n",
1098 system->name);
1099
1100 pr_cont("OK\n");
1101 }
1102
1103 /* Test with all events enabled */
1104
1105 pr_info("Running tests on all trace events:\n");
1106 pr_info("Testing all events: ");
1107
1108 sysname = kmalloc(4, GFP_KERNEL);
1109 if (WARN_ON(!sysname)) {
1110 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001111 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001112 }
1113 memcpy(sysname, "*:*", 4);
1114 ret = ftrace_set_clr_event(sysname, 1);
1115 if (WARN_ON_ONCE(ret)) {
1116 kfree(sysname);
1117 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001118 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001119 }
1120
1121 event_test_stuff();
1122
1123 /* reset sysname */
1124 memcpy(sysname, "*:*", 4);
1125 ret = ftrace_set_clr_event(sysname, 0);
1126 kfree(sysname);
1127
1128 if (WARN_ON_ONCE(ret)) {
1129 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001130 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001131 }
1132
1133 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001134}
1135
1136#ifdef CONFIG_FUNCTION_TRACER
1137
1138static DEFINE_PER_CPU(atomic_t, test_event_disable);
1139
1140static void
1141function_test_events_call(unsigned long ip, unsigned long parent_ip)
1142{
1143 struct ring_buffer_event *event;
1144 struct ftrace_entry *entry;
1145 unsigned long flags;
1146 long disabled;
1147 int resched;
1148 int cpu;
1149 int pc;
1150
1151 pc = preempt_count();
1152 resched = ftrace_preempt_disable();
1153 cpu = raw_smp_processor_id();
1154 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1155
1156 if (disabled != 1)
1157 goto out;
1158
1159 local_save_flags(flags);
1160
1161 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1162 flags, pc);
1163 if (!event)
1164 goto out;
1165 entry = ring_buffer_event_data(event);
1166 entry->ip = ip;
1167 entry->parent_ip = parent_ip;
1168
Steven Rostedtcb4764a2009-04-20 18:16:44 -04001169 trace_nowake_buffer_unlock_commit(event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001170
1171 out:
1172 atomic_dec(&per_cpu(test_event_disable, cpu));
1173 ftrace_preempt_enable(resched);
1174}
1175
1176static struct ftrace_ops trace_ops __initdata =
1177{
1178 .func = function_test_events_call,
1179};
1180
1181static __init void event_trace_self_test_with_function(void)
1182{
1183 register_ftrace_function(&trace_ops);
1184 pr_info("Running tests again, along with the function tracer\n");
1185 event_trace_self_tests();
1186 unregister_ftrace_function(&trace_ops);
1187}
1188#else
1189static __init void event_trace_self_test_with_function(void)
1190{
1191}
1192#endif
1193
1194static __init int event_trace_self_tests_init(void)
1195{
1196
1197 event_trace_self_tests();
1198
1199 event_trace_self_test_with_function();
Steven Rostedte6187002009-04-15 13:36:40 -04001200
1201 return 0;
1202}
1203
Steven Rostedt28d20e22009-04-20 12:12:44 -04001204late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001205
1206#endif