blob: 87feb0117ce28a5c1a51b75e6857e21281e0ed7e [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
Li Zefan20c89282009-05-06 10:33:45 +080024DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050025
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,
Tom Zanussia118e4d2009-04-28 03:04:53 -050029 char *name, int offset, int size, int is_signed)
Tom Zanussicf027f62009-03-22 03:30:39 -050030{
31 struct ftrace_event_field *field;
32
Ingo Molnarfe9f57f2009-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 Molnarfe9f57f2009-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 Molnarfe9f57f2009-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 Molnarfe9f57f2009-03-22 18:41:59 +010044
Tom Zanussicf027f62009-03-22 03:30:39 -050045 field->offset = offset;
46 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -050047 field->is_signed = is_signed;
Tom Zanussicf027f62009-03-22 03:30:39 -050048 list_add(&field->link, &call->fields);
49
50 return 0;
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010051
Tom Zanussicf027f62009-03-22 03:30:39 -050052err:
53 if (field) {
54 kfree(field->name);
55 kfree(field->type);
56 }
57 kfree(field);
Ingo Molnarfe9f57f2009-03-22 18:41:59 +010058
Tom Zanussicf027f62009-03-22 03:30:39 -050059 return -ENOMEM;
60}
Steven Rostedt17c873e2009-04-10 18:12:50 -040061EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -050062
Li Zefan2df75e42009-05-06 10:33:04 +080063#ifdef CONFIG_MODULES
64
65static void trace_destroy_fields(struct ftrace_event_call *call)
66{
67 struct ftrace_event_field *field, *next;
68
69 list_for_each_entry_safe(field, next, &call->fields, link) {
70 list_del(&field->link);
71 kfree(field->type);
72 kfree(field->name);
73 kfree(field);
74 }
75}
76
77#endif /* CONFIG_MODULES */
78
Steven Rostedtb77e38a2009-02-24 10:21:36 -050079static void ftrace_clear_events(void)
80{
Steven Rostedta59fd602009-04-10 13:52:20 -040081 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -050082
Li Zefan20c89282009-05-06 10:33:45 +080083 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -040084 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -050085
86 if (call->enabled) {
87 call->enabled = 0;
88 call->unregfunc();
89 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -050090 }
Li Zefan20c89282009-05-06 10:33:45 +080091 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -050092}
93
Steven Rostedtfd994982009-02-28 02:41:25 -050094static void ftrace_event_enable_disable(struct ftrace_event_call *call,
95 int enable)
96{
97
98 switch (enable) {
99 case 0:
100 if (call->enabled) {
101 call->enabled = 0;
102 call->unregfunc();
103 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500104 break;
105 case 1:
Steven Rostedtda4d0302009-03-09 17:14:30 -0400106 if (!call->enabled) {
Steven Rostedtfd994982009-02-28 02:41:25 -0500107 call->enabled = 1;
108 call->regfunc();
109 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500110 break;
111 }
112}
113
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500114static int ftrace_set_clr_event(char *buf, int set)
115{
Steven Rostedta59fd602009-04-10 13:52:20 -0400116 struct ftrace_event_call *call;
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500117 char *event = NULL, *sub = NULL, *match;
118 int ret = -EINVAL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500119
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500120 /*
121 * The buf format can be <subsystem>:<event-name>
122 * *:<event-name> means any event by that name.
123 * :<event-name> is the same.
124 *
125 * <subsystem>:* means all events in that subsystem
126 * <subsystem>: means the same.
127 *
128 * <name> (no ':') means all events in a subsystem with
129 * the name <name> or any event that matches <name>
130 */
131
132 match = strsep(&buf, ":");
133 if (buf) {
134 sub = match;
135 event = buf;
136 match = NULL;
137
138 if (!strlen(sub) || strcmp(sub, "*") == 0)
139 sub = NULL;
140 if (!strlen(event) || strcmp(event, "*") == 0)
141 event = NULL;
142 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500143
Steven Rostedt11a241a2009-03-02 11:49:04 -0500144 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400145 list_for_each_entry(call, &ftrace_events, list) {
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500146
Steven Rostedt40e26812009-03-10 11:32:40 -0400147 if (!call->name || !call->regfunc)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500148 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500149
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500150 if (match &&
151 strcmp(match, call->name) != 0 &&
152 strcmp(match, call->system) != 0)
153 continue;
154
155 if (sub && strcmp(sub, call->system) != 0)
156 continue;
157
158 if (event && strcmp(event, call->name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500159 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500160
Steven Rostedtfd994982009-02-28 02:41:25 -0500161 ftrace_event_enable_disable(call, set);
162
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500163 ret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500164 }
Steven Rostedt11a241a2009-03-02 11:49:04 -0500165 mutex_unlock(&event_mutex);
166
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500167 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500168}
169
170/* 128 should be much more than enough */
171#define EVENT_BUF_SIZE 127
172
173static ssize_t
174ftrace_event_write(struct file *file, const char __user *ubuf,
175 size_t cnt, loff_t *ppos)
176{
177 size_t read = 0;
178 int i, set = 1;
179 ssize_t ret;
180 char *buf;
181 char ch;
182
183 if (!cnt || cnt < 0)
184 return 0;
185
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400186 ret = tracing_update_buffers();
187 if (ret < 0)
188 return ret;
189
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500190 ret = get_user(ch, ubuf++);
191 if (ret)
192 return ret;
193 read++;
194 cnt--;
195
196 /* skip white space */
197 while (cnt && isspace(ch)) {
198 ret = get_user(ch, ubuf++);
199 if (ret)
200 return ret;
201 read++;
202 cnt--;
203 }
204
205 /* Only white space found? */
206 if (isspace(ch)) {
207 file->f_pos += read;
208 ret = read;
209 return ret;
210 }
211
212 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
213 if (!buf)
214 return -ENOMEM;
215
216 if (cnt > EVENT_BUF_SIZE)
217 cnt = EVENT_BUF_SIZE;
218
219 i = 0;
220 while (cnt && !isspace(ch)) {
221 if (!i && ch == '!')
222 set = 0;
223 else
224 buf[i++] = ch;
225
226 ret = get_user(ch, ubuf++);
227 if (ret)
228 goto out_free;
229 read++;
230 cnt--;
231 }
232 buf[i] = 0;
233
234 file->f_pos += read;
235
236 ret = ftrace_set_clr_event(buf, set);
237 if (ret)
238 goto out_free;
239
240 ret = read;
241
242 out_free:
243 kfree(buf);
244
245 return ret;
246}
247
248static void *
249t_next(struct seq_file *m, void *v, loff_t *pos)
250{
Steven Rostedta59fd602009-04-10 13:52:20 -0400251 struct list_head *list = m->private;
252 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500253
254 (*pos)++;
255
Steven Rostedt40e26812009-03-10 11:32:40 -0400256 for (;;) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400257 if (list == &ftrace_events)
Steven Rostedt40e26812009-03-10 11:32:40 -0400258 return NULL;
259
Steven Rostedta59fd602009-04-10 13:52:20 -0400260 call = list_entry(list, struct ftrace_event_call, list);
261
Steven Rostedt40e26812009-03-10 11:32:40 -0400262 /*
263 * The ftrace subsystem is for showing formats only.
264 * They can not be enabled or disabled via the event files.
265 */
266 if (call->regfunc)
267 break;
268
Steven Rostedta59fd602009-04-10 13:52:20 -0400269 list = list->next;
Steven Rostedt40e26812009-03-10 11:32:40 -0400270 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500271
Steven Rostedta59fd602009-04-10 13:52:20 -0400272 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500273
274 return call;
275}
276
277static void *t_start(struct seq_file *m, loff_t *pos)
278{
Li Zefan20c89282009-05-06 10:33:45 +0800279 mutex_lock(&event_mutex);
280 if (*pos == 0)
281 m->private = ftrace_events.next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500282 return t_next(m, NULL, pos);
283}
284
285static void *
286s_next(struct seq_file *m, void *v, loff_t *pos)
287{
Steven Rostedta59fd602009-04-10 13:52:20 -0400288 struct list_head *list = m->private;
289 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500290
291 (*pos)++;
292
293 retry:
Steven Rostedta59fd602009-04-10 13:52:20 -0400294 if (list == &ftrace_events)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500295 return NULL;
296
Steven Rostedta59fd602009-04-10 13:52:20 -0400297 call = list_entry(list, struct ftrace_event_call, list);
298
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500299 if (!call->enabled) {
Steven Rostedta59fd602009-04-10 13:52:20 -0400300 list = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500301 goto retry;
302 }
303
Steven Rostedta59fd602009-04-10 13:52:20 -0400304 m->private = list->next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500305
306 return call;
307}
308
309static void *s_start(struct seq_file *m, loff_t *pos)
310{
Li Zefan20c89282009-05-06 10:33:45 +0800311 mutex_lock(&event_mutex);
312 if (*pos == 0)
313 m->private = ftrace_events.next;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500314 return s_next(m, NULL, pos);
315}
316
317static int t_show(struct seq_file *m, void *v)
318{
319 struct ftrace_event_call *call = v;
320
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500321 if (strcmp(call->system, TRACE_SYSTEM) != 0)
322 seq_printf(m, "%s:", call->system);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500323 seq_printf(m, "%s\n", call->name);
324
325 return 0;
326}
327
328static void t_stop(struct seq_file *m, void *p)
329{
Li Zefan20c89282009-05-06 10:33:45 +0800330 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500331}
332
333static int
334ftrace_event_seq_open(struct inode *inode, struct file *file)
335{
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500336 const struct seq_operations *seq_ops;
337
338 if ((file->f_mode & FMODE_WRITE) &&
339 !(file->f_flags & O_APPEND))
340 ftrace_clear_events();
341
342 seq_ops = inode->i_private;
Li Zefan20c89282009-05-06 10:33:45 +0800343 return seq_open(file, seq_ops);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500344}
345
Steven Rostedt1473e442009-02-24 14:15:08 -0500346static ssize_t
347event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
348 loff_t *ppos)
349{
350 struct ftrace_event_call *call = filp->private_data;
351 char *buf;
352
Steven Rostedtda4d0302009-03-09 17:14:30 -0400353 if (call->enabled)
Steven Rostedt1473e442009-02-24 14:15:08 -0500354 buf = "1\n";
355 else
356 buf = "0\n";
357
358 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
359}
360
361static ssize_t
362event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
363 loff_t *ppos)
364{
365 struct ftrace_event_call *call = filp->private_data;
366 char buf[64];
367 unsigned long val;
368 int ret;
369
370 if (cnt >= sizeof(buf))
371 return -EINVAL;
372
373 if (copy_from_user(&buf, ubuf, cnt))
374 return -EFAULT;
375
376 buf[cnt] = 0;
377
378 ret = strict_strtoul(buf, 10, &val);
379 if (ret < 0)
380 return ret;
381
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400382 ret = tracing_update_buffers();
383 if (ret < 0)
384 return ret;
385
Steven Rostedt1473e442009-02-24 14:15:08 -0500386 switch (val) {
387 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -0500388 case 1:
Steven Rostedt11a241a2009-03-02 11:49:04 -0500389 mutex_lock(&event_mutex);
Steven Rostedtfd994982009-02-28 02:41:25 -0500390 ftrace_event_enable_disable(call, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500391 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -0500392 break;
393
394 default:
395 return -EINVAL;
396 }
397
398 *ppos += cnt;
399
400 return cnt;
401}
402
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400403static ssize_t
404system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
405 loff_t *ppos)
406{
407 const char *system = filp->private_data;
408 struct ftrace_event_call *call;
409 char buf[2];
410 int set = -1;
411 int all = 0;
412 int ret;
413
414 if (system[0] == '*')
415 all = 1;
416
417 mutex_lock(&event_mutex);
418 list_for_each_entry(call, &ftrace_events, list) {
419 if (!call->name || !call->regfunc)
420 continue;
421
422 if (!all && strcmp(call->system, system) != 0)
423 continue;
424
425 /*
426 * We need to find out if all the events are set
427 * or if all events or cleared, or if we have
428 * a mixture.
429 */
430 if (call->enabled) {
431 switch (set) {
432 case -1:
433 set = 1;
434 break;
435 case 0:
436 set = 2;
437 break;
438 }
439 } else {
440 switch (set) {
441 case -1:
442 set = 0;
443 break;
444 case 1:
445 set = 2;
446 break;
447 }
448 }
449 /*
450 * If we have a mixture, no need to look further.
451 */
452 if (set == 2)
453 break;
454 }
455 mutex_unlock(&event_mutex);
456
457 buf[1] = '\n';
458 switch (set) {
459 case 0:
460 buf[0] = '0';
461 break;
462 case 1:
463 buf[0] = '1';
464 break;
465 case 2:
466 buf[0] = 'X';
467 break;
468 default:
469 buf[0] = '?';
470 }
471
472 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
473
474 return ret;
475}
476
477static ssize_t
478system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
479 loff_t *ppos)
480{
481 const char *system = filp->private_data;
482 unsigned long val;
483 char *command;
484 char buf[64];
485 ssize_t ret;
486
487 if (cnt >= sizeof(buf))
488 return -EINVAL;
489
490 if (copy_from_user(&buf, ubuf, cnt))
491 return -EFAULT;
492
493 buf[cnt] = 0;
494
495 ret = strict_strtoul(buf, 10, &val);
496 if (ret < 0)
497 return ret;
498
499 ret = tracing_update_buffers();
500 if (ret < 0)
501 return ret;
502
503 switch (val) {
504 case 0:
505 case 1:
506 break;
507
508 default:
509 return -EINVAL;
510 }
511
512 command = kstrdup(system, GFP_KERNEL);
513 if (!command)
514 return -ENOMEM;
515
516 ret = ftrace_set_clr_event(command, val);
517 if (ret)
518 goto out_free;
519
520 ret = cnt;
521
522 out_free:
523 kfree(command);
524
525 *ppos += cnt;
526
527 return ret;
528}
529
Steven Rostedt75db37d2009-03-26 11:43:36 -0400530extern char *__bad_type_size(void);
531
Steven Rostedt91729ef92009-03-02 15:03:01 -0500532#undef FIELD
Steven Rostedt156b5f12009-03-06 10:50:53 -0500533#define FIELD(type, name) \
Steven Rostedt75db37d2009-03-26 11:43:36 -0400534 sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
Tom Zanussicf027f62009-03-22 03:30:39 -0500535 #type, "common_" #name, offsetof(typeof(field), name), \
536 sizeof(field.name)
Steven Rostedt91729ef92009-03-02 15:03:01 -0500537
538static int trace_write_header(struct trace_seq *s)
539{
540 struct trace_entry field;
541
542 /* struct trace_entry */
543 return trace_seq_printf(s,
Steven Rostedtce8eb2b2009-03-10 10:14:35 -0400544 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
545 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
546 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
547 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
548 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
Steven Rostedt91729ef92009-03-02 15:03:01 -0500549 "\n",
Steven Rostedt89ec0de2009-03-26 11:03:29 -0400550 FIELD(unsigned short, type),
Steven Rostedt91729ef92009-03-02 15:03:01 -0500551 FIELD(unsigned char, flags),
552 FIELD(unsigned char, preempt_count),
553 FIELD(int, pid),
554 FIELD(int, tgid));
555}
Steven Rostedtda4d0302009-03-09 17:14:30 -0400556
Steven Rostedt981d0812009-03-02 13:53:59 -0500557static ssize_t
558event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
559 loff_t *ppos)
560{
561 struct ftrace_event_call *call = filp->private_data;
562 struct trace_seq *s;
563 char *buf;
564 int r;
565
Tom Zanussic269fc82009-03-17 01:20:59 -0500566 if (*ppos)
567 return 0;
568
Steven Rostedt981d0812009-03-02 13:53:59 -0500569 s = kmalloc(sizeof(*s), GFP_KERNEL);
570 if (!s)
571 return -ENOMEM;
572
573 trace_seq_init(s);
574
Steven Rostedtc5e4e192009-03-02 15:10:02 -0500575 /* If any of the first writes fail, so will the show_format. */
576
577 trace_seq_printf(s, "name: %s\n", call->name);
578 trace_seq_printf(s, "ID: %d\n", call->id);
579 trace_seq_printf(s, "format:\n");
Steven Rostedt91729ef92009-03-02 15:03:01 -0500580 trace_write_header(s);
581
Steven Rostedt981d0812009-03-02 13:53:59 -0500582 r = call->show_format(s);
583 if (!r) {
584 /*
585 * ug! The format output is bigger than a PAGE!!
586 */
587 buf = "FORMAT TOO BIG\n";
588 r = simple_read_from_buffer(ubuf, cnt, ppos,
589 buf, strlen(buf));
590 goto out;
591 }
592
593 r = simple_read_from_buffer(ubuf, cnt, ppos,
594 s->buffer, s->len);
595 out:
596 kfree(s);
597 return r;
598}
599
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100600static ssize_t
601event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
602{
603 struct ftrace_event_call *call = filp->private_data;
604 struct trace_seq *s;
605 int r;
606
607 if (*ppos)
608 return 0;
609
610 s = kmalloc(sizeof(*s), GFP_KERNEL);
611 if (!s)
612 return -ENOMEM;
613
614 trace_seq_init(s);
615 trace_seq_printf(s, "%d\n", call->id);
616
617 r = simple_read_from_buffer(ubuf, cnt, ppos,
618 s->buffer, s->len);
619 kfree(s);
620 return r;
621}
622
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500623static ssize_t
624event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
625 loff_t *ppos)
626{
627 struct ftrace_event_call *call = filp->private_data;
628 struct trace_seq *s;
629 int r;
630
631 if (*ppos)
632 return 0;
633
634 s = kmalloc(sizeof(*s), GFP_KERNEL);
635 if (!s)
636 return -ENOMEM;
637
638 trace_seq_init(s);
639
Tom Zanussi8b372562009-04-28 03:04:59 -0500640 print_event_filter(call, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500641 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500642
643 kfree(s);
644
645 return r;
646}
647
648static ssize_t
649event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
650 loff_t *ppos)
651{
652 struct ftrace_event_call *call = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500653 char *buf;
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500654 int err;
655
Tom Zanussi8b372562009-04-28 03:04:59 -0500656 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500657 return -EINVAL;
658
Tom Zanussi8b372562009-04-28 03:04:59 -0500659 buf = (char *)__get_free_page(GFP_TEMPORARY);
660 if (!buf)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500661 return -ENOMEM;
662
Tom Zanussi8b372562009-04-28 03:04:59 -0500663 if (copy_from_user(buf, ubuf, cnt)) {
664 free_page((unsigned long) buf);
665 return -EFAULT;
666 }
667 buf[cnt] = '\0';
668
669 err = apply_event_filter(call, buf);
670 free_page((unsigned long) buf);
671 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500672 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -0500673
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500674 *ppos += cnt;
675
676 return cnt;
677}
678
Tom Zanussicfb180f2009-03-22 03:31:17 -0500679static ssize_t
680subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
681 loff_t *ppos)
682{
683 struct event_subsystem *system = filp->private_data;
684 struct trace_seq *s;
685 int r;
686
687 if (*ppos)
688 return 0;
689
690 s = kmalloc(sizeof(*s), GFP_KERNEL);
691 if (!s)
692 return -ENOMEM;
693
694 trace_seq_init(s);
695
Tom Zanussi8b372562009-04-28 03:04:59 -0500696 print_subsystem_event_filter(system, s);
Tom Zanussi4bda2d52009-03-24 02:14:31 -0500697 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
Tom Zanussicfb180f2009-03-22 03:31:17 -0500698
699 kfree(s);
700
701 return r;
702}
703
704static ssize_t
705subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
706 loff_t *ppos)
707{
708 struct event_subsystem *system = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -0500709 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500710 int err;
711
Tom Zanussi8b372562009-04-28 03:04:59 -0500712 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500713 return -EINVAL;
714
Tom Zanussi8b372562009-04-28 03:04:59 -0500715 buf = (char *)__get_free_page(GFP_TEMPORARY);
716 if (!buf)
Tom Zanussicfb180f2009-03-22 03:31:17 -0500717 return -ENOMEM;
718
Tom Zanussi8b372562009-04-28 03:04:59 -0500719 if (copy_from_user(buf, ubuf, cnt)) {
720 free_page((unsigned long) buf);
721 return -EFAULT;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500722 }
Tom Zanussi8b372562009-04-28 03:04:59 -0500723 buf[cnt] = '\0';
Tom Zanussicfb180f2009-03-22 03:31:17 -0500724
Tom Zanussi8b372562009-04-28 03:04:59 -0500725 err = apply_subsystem_event_filter(system, buf);
726 free_page((unsigned long) buf);
727 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +0800728 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500729
730 *ppos += cnt;
731
732 return cnt;
733}
734
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400735static ssize_t
736show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
737{
738 int (*func)(struct trace_seq *s) = filp->private_data;
739 struct trace_seq *s;
740 int r;
741
742 if (*ppos)
743 return 0;
744
745 s = kmalloc(sizeof(*s), GFP_KERNEL);
746 if (!s)
747 return -ENOMEM;
748
749 trace_seq_init(s);
750
751 func(s);
752 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
753
754 kfree(s);
755
756 return r;
757}
758
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500759static const struct seq_operations show_event_seq_ops = {
760 .start = t_start,
761 .next = t_next,
762 .show = t_show,
763 .stop = t_stop,
764};
765
766static const struct seq_operations show_set_event_seq_ops = {
767 .start = s_start,
768 .next = s_next,
769 .show = t_show,
770 .stop = t_stop,
771};
772
Steven Rostedt2314c4a2009-03-10 12:04:02 -0400773static const struct file_operations ftrace_avail_fops = {
774 .open = ftrace_event_seq_open,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = seq_release,
778};
779
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500780static const struct file_operations ftrace_set_event_fops = {
781 .open = ftrace_event_seq_open,
782 .read = seq_read,
783 .write = ftrace_event_write,
784 .llseek = seq_lseek,
785 .release = seq_release,
786};
787
Steven Rostedt1473e442009-02-24 14:15:08 -0500788static const struct file_operations ftrace_enable_fops = {
789 .open = tracing_open_generic,
790 .read = event_enable_read,
791 .write = event_enable_write,
792};
793
Steven Rostedt981d0812009-03-02 13:53:59 -0500794static const struct file_operations ftrace_event_format_fops = {
795 .open = tracing_open_generic,
796 .read = event_format_read,
797};
798
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100799static const struct file_operations ftrace_event_id_fops = {
800 .open = tracing_open_generic,
801 .read = event_id_read,
802};
803
Tom Zanussi7ce7e422009-03-22 03:31:04 -0500804static const struct file_operations ftrace_event_filter_fops = {
805 .open = tracing_open_generic,
806 .read = event_filter_read,
807 .write = event_filter_write,
808};
809
Tom Zanussicfb180f2009-03-22 03:31:17 -0500810static const struct file_operations ftrace_subsystem_filter_fops = {
811 .open = tracing_open_generic,
812 .read = subsystem_filter_read,
813 .write = subsystem_filter_write,
814};
815
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400816static const struct file_operations ftrace_system_enable_fops = {
817 .open = tracing_open_generic,
818 .read = system_enable_read,
819 .write = system_enable_write,
820};
821
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400822static const struct file_operations ftrace_show_header_fops = {
823 .open = tracing_open_generic,
824 .read = show_header,
825};
826
Steven Rostedt1473e442009-02-24 14:15:08 -0500827static struct dentry *event_trace_events_dir(void)
828{
829 static struct dentry *d_tracer;
830 static struct dentry *d_events;
831
832 if (d_events)
833 return d_events;
834
835 d_tracer = tracing_init_dentry();
836 if (!d_tracer)
837 return NULL;
838
839 d_events = debugfs_create_dir("events", d_tracer);
840 if (!d_events)
841 pr_warning("Could not create debugfs "
842 "'events' directory\n");
843
844 return d_events;
845}
846
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500847static LIST_HEAD(event_subsystems);
848
849static struct dentry *
850event_subsystem_dir(const char *name, struct dentry *d_events)
851{
852 struct event_subsystem *system;
Tom Zanussie1112b42009-03-31 00:48:49 -0500853 struct dentry *entry;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500854
855 /* First see if we did not already create this dir */
856 list_for_each_entry(system, &event_subsystems, list) {
857 if (strcmp(system->name, name) == 0)
858 return system->entry;
859 }
860
861 /* need to create new entry */
862 system = kmalloc(sizeof(*system), GFP_KERNEL);
863 if (!system) {
864 pr_warning("No memory to create event subsystem %s\n",
865 name);
866 return d_events;
867 }
868
869 system->entry = debugfs_create_dir(name, d_events);
870 if (!system->entry) {
871 pr_warning("Could not create event subsystem %s\n",
872 name);
873 kfree(system);
874 return d_events;
875 }
876
Steven Rostedt6d723732009-04-10 14:53:50 -0400877 system->name = kstrdup(name, GFP_KERNEL);
878 if (!system->name) {
879 debugfs_remove(system->entry);
880 kfree(system);
881 return d_events;
882 }
883
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500884 list_add(&system->list, &event_subsystems);
885
Tom Zanussi30e673b2009-04-28 03:04:47 -0500886 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -0500887
Tom Zanussi8b372562009-04-28 03:04:59 -0500888 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
889 if (!system->filter) {
890 pr_warning("Could not allocate filter for subsystem "
891 "'%s'\n", name);
892 return system->entry;
893 }
894
Tom Zanussie1112b42009-03-31 00:48:49 -0500895 entry = debugfs_create_file("filter", 0644, system->entry, system,
896 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -0500897 if (!entry) {
898 kfree(system->filter);
899 system->filter = NULL;
Tom Zanussie1112b42009-03-31 00:48:49 -0500900 pr_warning("Could not create debugfs "
901 "'%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -0500902 }
Tom Zanussie1112b42009-03-31 00:48:49 -0500903
Steven Rostedt8ae79a12009-05-06 22:52:15 -0400904 entry = trace_create_file("enable", 0644, system->entry,
905 (void *)system->name,
906 &ftrace_system_enable_fops);
907
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500908 return system->entry;
909}
910
Steven Rostedt1473e442009-02-24 14:15:08 -0500911static int
Steven Rostedt701970b2009-04-24 23:11:22 -0400912event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
913 const struct file_operations *id,
914 const struct file_operations *enable,
915 const struct file_operations *filter,
916 const struct file_operations *format)
Steven Rostedt1473e442009-02-24 14:15:08 -0500917{
918 struct dentry *entry;
Steven Rostedtfd994982009-02-28 02:41:25 -0500919 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -0500920
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500921 /*
922 * If the trace point header did not define TRACE_SYSTEM
923 * then the system would be called "TRACE_SYSTEM".
924 */
Steven Rostedt6d723732009-04-10 14:53:50 -0400925 if (strcmp(call->system, TRACE_SYSTEM) != 0)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -0500926 d_events = event_subsystem_dir(call->system, d_events);
927
Steven Rostedtfd994982009-02-28 02:41:25 -0500928 if (call->raw_init) {
929 ret = call->raw_init();
930 if (ret < 0) {
931 pr_warning("Could not initialize trace point"
932 " events/%s\n", call->name);
933 return ret;
934 }
935 }
936
Steven Rostedt1473e442009-02-24 14:15:08 -0500937 call->dir = debugfs_create_dir(call->name, d_events);
938 if (!call->dir) {
939 pr_warning("Could not create debugfs "
940 "'%s' directory\n", call->name);
941 return -1;
942 }
943
Steven Rostedt6d723732009-04-10 14:53:50 -0400944 if (call->regfunc)
945 entry = trace_create_file("enable", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400946 enable);
Steven Rostedt1473e442009-02-24 14:15:08 -0500947
Steven Rostedt6d723732009-04-10 14:53:50 -0400948 if (call->id)
949 entry = trace_create_file("id", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400950 id);
Peter Zijlstra23725ae2009-03-19 20:26:13 +0100951
Tom Zanussicf027f62009-03-22 03:30:39 -0500952 if (call->define_fields) {
953 ret = call->define_fields();
954 if (ret < 0) {
955 pr_warning("Could not initialize trace point"
956 " events/%s\n", call->name);
957 return ret;
958 }
Steven Rostedt6d723732009-04-10 14:53:50 -0400959 entry = trace_create_file("filter", 0644, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400960 filter);
Tom Zanussicf027f62009-03-22 03:30:39 -0500961 }
962
Steven Rostedt981d0812009-03-02 13:53:59 -0500963 /* A trace may not want to export its format */
964 if (!call->show_format)
965 return 0;
966
Steven Rostedt6d723732009-04-10 14:53:50 -0400967 entry = trace_create_file("format", 0444, call->dir, call,
Steven Rostedt701970b2009-04-24 23:11:22 -0400968 format);
Steven Rostedtfd994982009-02-28 02:41:25 -0500969
Steven Rostedt1473e442009-02-24 14:15:08 -0500970 return 0;
971}
972
Steven Rostedt6d723732009-04-10 14:53:50 -0400973#define for_each_event(event, start, end) \
974 for (event = start; \
975 (unsigned long)event < (unsigned long)end; \
976 event++)
977
Steven Rostedt61f919a2009-04-14 18:22:32 -0400978#ifdef CONFIG_MODULES
Steven Rostedt701970b2009-04-24 23:11:22 -0400979
980static LIST_HEAD(ftrace_module_file_list);
981
982/*
983 * Modules must own their file_operations to keep up with
984 * reference counting.
985 */
986struct ftrace_module_file_ops {
987 struct list_head list;
988 struct module *mod;
989 struct file_operations id;
990 struct file_operations enable;
991 struct file_operations format;
992 struct file_operations filter;
993};
994
995static struct ftrace_module_file_ops *
996trace_create_file_ops(struct module *mod)
997{
998 struct ftrace_module_file_ops *file_ops;
999
1000 /*
1001 * This is a bit of a PITA. To allow for correct reference
1002 * counting, modules must "own" their file_operations.
1003 * To do this, we allocate the file operations that will be
1004 * used in the event directory.
1005 */
1006
1007 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1008 if (!file_ops)
1009 return NULL;
1010
1011 file_ops->mod = mod;
1012
1013 file_ops->id = ftrace_event_id_fops;
1014 file_ops->id.owner = mod;
1015
1016 file_ops->enable = ftrace_enable_fops;
1017 file_ops->enable.owner = mod;
1018
1019 file_ops->filter = ftrace_event_filter_fops;
1020 file_ops->filter.owner = mod;
1021
1022 file_ops->format = ftrace_event_format_fops;
1023 file_ops->format.owner = mod;
1024
1025 list_add(&file_ops->list, &ftrace_module_file_list);
1026
1027 return file_ops;
1028}
1029
Steven Rostedt6d723732009-04-10 14:53:50 -04001030static void trace_module_add_events(struct module *mod)
1031{
Steven Rostedt701970b2009-04-24 23:11:22 -04001032 struct ftrace_module_file_ops *file_ops = NULL;
Steven Rostedt6d723732009-04-10 14:53:50 -04001033 struct ftrace_event_call *call, *start, *end;
1034 struct dentry *d_events;
1035
1036 start = mod->trace_events;
1037 end = mod->trace_events + mod->num_trace_events;
1038
1039 if (start == end)
1040 return;
1041
1042 d_events = event_trace_events_dir();
1043 if (!d_events)
1044 return;
1045
1046 for_each_event(call, start, end) {
1047 /* The linker may leave blanks */
1048 if (!call->name)
1049 continue;
Steven Rostedt701970b2009-04-24 23:11:22 -04001050
1051 /*
1052 * This module has events, create file ops for this module
1053 * if not already done.
1054 */
1055 if (!file_ops) {
1056 file_ops = trace_create_file_ops(mod);
1057 if (!file_ops)
1058 return;
1059 }
Steven Rostedt6d723732009-04-10 14:53:50 -04001060 call->mod = mod;
1061 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001062 event_create_dir(call, d_events,
1063 &file_ops->id, &file_ops->enable,
1064 &file_ops->filter, &file_ops->format);
Steven Rostedt6d723732009-04-10 14:53:50 -04001065 }
1066}
1067
1068static void trace_module_remove_events(struct module *mod)
1069{
Steven Rostedt701970b2009-04-24 23:11:22 -04001070 struct ftrace_module_file_ops *file_ops;
Steven Rostedt6d723732009-04-10 14:53:50 -04001071 struct ftrace_event_call *call, *p;
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001072 bool found = false;
Steven Rostedt6d723732009-04-10 14:53:50 -04001073
1074 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1075 if (call->mod == mod) {
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001076 found = true;
Steven Rostedt6d723732009-04-10 14:53:50 -04001077 if (call->enabled) {
1078 call->enabled = 0;
1079 call->unregfunc();
1080 }
1081 if (call->event)
1082 unregister_ftrace_event(call->event);
1083 debugfs_remove_recursive(call->dir);
1084 list_del(&call->list);
Li Zefan2df75e42009-05-06 10:33:04 +08001085 trace_destroy_fields(call);
1086 destroy_preds(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04001087 }
1088 }
Steven Rostedt701970b2009-04-24 23:11:22 -04001089
1090 /* Now free the file_operations */
1091 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1092 if (file_ops->mod == mod)
1093 break;
1094 }
1095 if (&file_ops->list != &ftrace_module_file_list) {
1096 list_del(&file_ops->list);
1097 kfree(file_ops);
1098 }
Steven Rostedt9456f0f2009-05-06 21:54:09 -04001099
1100 /*
1101 * It is safest to reset the ring buffer if the module being unloaded
1102 * registered any events.
1103 */
1104 if (found)
1105 tracing_reset_current_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04001106}
1107
Steven Rostedt61f919a2009-04-14 18:22:32 -04001108static int trace_module_notify(struct notifier_block *self,
1109 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04001110{
1111 struct module *mod = data;
1112
1113 mutex_lock(&event_mutex);
1114 switch (val) {
1115 case MODULE_STATE_COMING:
1116 trace_module_add_events(mod);
1117 break;
1118 case MODULE_STATE_GOING:
1119 trace_module_remove_events(mod);
1120 break;
1121 }
1122 mutex_unlock(&event_mutex);
1123
1124 return 0;
1125}
Steven Rostedt61f919a2009-04-14 18:22:32 -04001126#else
1127static int trace_module_notify(struct notifier_block *self,
1128 unsigned long val, void *data)
1129{
1130 return 0;
1131}
1132#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04001133
1134struct notifier_block trace_module_nb = {
1135 .notifier_call = trace_module_notify,
1136 .priority = 0,
1137};
1138
Steven Rostedta59fd602009-04-10 13:52:20 -04001139extern struct ftrace_event_call __start_ftrace_events[];
1140extern struct ftrace_event_call __stop_ftrace_events[];
1141
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001142static __init int event_trace_init(void)
1143{
Steven Rostedta59fd602009-04-10 13:52:20 -04001144 struct ftrace_event_call *call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001145 struct dentry *d_tracer;
1146 struct dentry *entry;
Steven Rostedt1473e442009-02-24 14:15:08 -05001147 struct dentry *d_events;
Steven Rostedt6d723732009-04-10 14:53:50 -04001148 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001149
1150 d_tracer = tracing_init_dentry();
1151 if (!d_tracer)
1152 return 0;
1153
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001154 entry = debugfs_create_file("available_events", 0444, d_tracer,
1155 (void *)&show_event_seq_ops,
1156 &ftrace_avail_fops);
1157 if (!entry)
1158 pr_warning("Could not create debugfs "
1159 "'available_events' entry\n");
1160
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001161 entry = debugfs_create_file("set_event", 0644, d_tracer,
1162 (void *)&show_set_event_seq_ops,
1163 &ftrace_set_event_fops);
1164 if (!entry)
1165 pr_warning("Could not create debugfs "
1166 "'set_event' entry\n");
1167
Steven Rostedt1473e442009-02-24 14:15:08 -05001168 d_events = event_trace_events_dir();
1169 if (!d_events)
1170 return 0;
1171
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001172 /* ring buffer internal formats */
1173 trace_create_file("header_page", 0444, d_events,
1174 ring_buffer_print_page_header,
1175 &ftrace_show_header_fops);
1176
1177 trace_create_file("header_event", 0444, d_events,
1178 ring_buffer_print_entry_header,
1179 &ftrace_show_header_fops);
1180
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001181 trace_create_file("enable", 0644, d_events,
1182 "*:*", &ftrace_system_enable_fops);
1183
Steven Rostedt6d723732009-04-10 14:53:50 -04001184 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
Steven Rostedt1473e442009-02-24 14:15:08 -05001185 /* The linker may leave blanks */
1186 if (!call->name)
1187 continue;
Steven Rostedta59fd602009-04-10 13:52:20 -04001188 list_add(&call->list, &ftrace_events);
Steven Rostedt701970b2009-04-24 23:11:22 -04001189 event_create_dir(call, d_events, &ftrace_event_id_fops,
1190 &ftrace_enable_fops, &ftrace_event_filter_fops,
1191 &ftrace_event_format_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001192 }
1193
Steven Rostedt6d723732009-04-10 14:53:50 -04001194 ret = register_module_notifier(&trace_module_nb);
1195 if (!ret)
1196 pr_warning("Failed to register trace events module notifier\n");
1197
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001198 return 0;
1199}
1200fs_initcall(event_trace_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001201
1202#ifdef CONFIG_FTRACE_STARTUP_TEST
1203
1204static DEFINE_SPINLOCK(test_spinlock);
1205static DEFINE_SPINLOCK(test_spinlock_irq);
1206static DEFINE_MUTEX(test_mutex);
1207
1208static __init void test_work(struct work_struct *dummy)
1209{
1210 spin_lock(&test_spinlock);
1211 spin_lock_irq(&test_spinlock_irq);
1212 udelay(1);
1213 spin_unlock_irq(&test_spinlock_irq);
1214 spin_unlock(&test_spinlock);
1215
1216 mutex_lock(&test_mutex);
1217 msleep(1);
1218 mutex_unlock(&test_mutex);
1219}
1220
1221static __init int event_test_thread(void *unused)
1222{
1223 void *test_malloc;
1224
1225 test_malloc = kmalloc(1234, GFP_KERNEL);
1226 if (!test_malloc)
1227 pr_info("failed to kmalloc\n");
1228
1229 schedule_on_each_cpu(test_work);
1230
1231 kfree(test_malloc);
1232
1233 set_current_state(TASK_INTERRUPTIBLE);
1234 while (!kthread_should_stop())
1235 schedule();
1236
1237 return 0;
1238}
1239
1240/*
1241 * Do various things that may trigger events.
1242 */
1243static __init void event_test_stuff(void)
1244{
1245 struct task_struct *test_thread;
1246
1247 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1248 msleep(1);
1249 kthread_stop(test_thread);
1250}
1251
1252/*
1253 * For every trace event defined, we will test each trace point separately,
1254 * and then by groups, and finally all trace points.
1255 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001256static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04001257{
1258 struct ftrace_event_call *call;
1259 struct event_subsystem *system;
1260 char *sysname;
1261 int ret;
1262
1263 pr_info("Running tests on trace events:\n");
1264
1265 list_for_each_entry(call, &ftrace_events, list) {
1266
1267 /* Only test those that have a regfunc */
1268 if (!call->regfunc)
1269 continue;
1270
1271 pr_info("Testing event %s: ", call->name);
1272
1273 /*
1274 * If an event is already enabled, someone is using
1275 * it and the self test should not be on.
1276 */
1277 if (call->enabled) {
1278 pr_warning("Enabled event during self test!\n");
1279 WARN_ON_ONCE(1);
1280 continue;
1281 }
1282
1283 call->enabled = 1;
1284 call->regfunc();
1285
1286 event_test_stuff();
1287
1288 call->unregfunc();
1289 call->enabled = 0;
1290
1291 pr_cont("OK\n");
1292 }
1293
1294 /* Now test at the sub system level */
1295
1296 pr_info("Running tests on trace event systems:\n");
1297
1298 list_for_each_entry(system, &event_subsystems, list) {
1299
1300 /* the ftrace system is special, skip it */
1301 if (strcmp(system->name, "ftrace") == 0)
1302 continue;
1303
1304 pr_info("Testing event system %s: ", system->name);
1305
1306 /* ftrace_set_clr_event can modify the name passed in. */
1307 sysname = kstrdup(system->name, GFP_KERNEL);
1308 if (WARN_ON(!sysname)) {
1309 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001310 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001311 }
1312 ret = ftrace_set_clr_event(sysname, 1);
1313 kfree(sysname);
1314 if (WARN_ON_ONCE(ret)) {
1315 pr_warning("error enabling system %s\n",
1316 system->name);
1317 continue;
1318 }
1319
1320 event_test_stuff();
1321
1322 sysname = kstrdup(system->name, GFP_KERNEL);
1323 if (WARN_ON(!sysname)) {
1324 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001325 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001326 }
1327 ret = ftrace_set_clr_event(sysname, 0);
1328 kfree(sysname);
1329
1330 if (WARN_ON_ONCE(ret))
1331 pr_warning("error disabling system %s\n",
1332 system->name);
1333
1334 pr_cont("OK\n");
1335 }
1336
1337 /* Test with all events enabled */
1338
1339 pr_info("Running tests on all trace events:\n");
1340 pr_info("Testing all events: ");
1341
1342 sysname = kmalloc(4, GFP_KERNEL);
1343 if (WARN_ON(!sysname)) {
1344 pr_warning("Can't allocate memory, giving up!\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001345 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001346 }
1347 memcpy(sysname, "*:*", 4);
1348 ret = ftrace_set_clr_event(sysname, 1);
1349 if (WARN_ON_ONCE(ret)) {
1350 kfree(sysname);
1351 pr_warning("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001352 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001353 }
1354
1355 event_test_stuff();
1356
1357 /* reset sysname */
1358 memcpy(sysname, "*:*", 4);
1359 ret = ftrace_set_clr_event(sysname, 0);
1360 kfree(sysname);
1361
1362 if (WARN_ON_ONCE(ret)) {
1363 pr_warning("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001364 return;
Steven Rostedte6187002009-04-15 13:36:40 -04001365 }
1366
1367 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001368}
1369
1370#ifdef CONFIG_FUNCTION_TRACER
1371
1372static DEFINE_PER_CPU(atomic_t, test_event_disable);
1373
1374static void
1375function_test_events_call(unsigned long ip, unsigned long parent_ip)
1376{
1377 struct ring_buffer_event *event;
1378 struct ftrace_entry *entry;
1379 unsigned long flags;
1380 long disabled;
1381 int resched;
1382 int cpu;
1383 int pc;
1384
1385 pc = preempt_count();
1386 resched = ftrace_preempt_disable();
1387 cpu = raw_smp_processor_id();
1388 disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
1389
1390 if (disabled != 1)
1391 goto out;
1392
1393 local_save_flags(flags);
1394
1395 event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
1396 flags, pc);
1397 if (!event)
1398 goto out;
1399 entry = ring_buffer_event_data(event);
1400 entry->ip = ip;
1401 entry->parent_ip = parent_ip;
1402
Steven Rostedtcb4764a2009-04-20 18:16:44 -04001403 trace_nowake_buffer_unlock_commit(event, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04001404
1405 out:
1406 atomic_dec(&per_cpu(test_event_disable, cpu));
1407 ftrace_preempt_enable(resched);
1408}
1409
1410static struct ftrace_ops trace_ops __initdata =
1411{
1412 .func = function_test_events_call,
1413};
1414
1415static __init void event_trace_self_test_with_function(void)
1416{
1417 register_ftrace_function(&trace_ops);
1418 pr_info("Running tests again, along with the function tracer\n");
1419 event_trace_self_tests();
1420 unregister_ftrace_function(&trace_ops);
1421}
1422#else
1423static __init void event_trace_self_test_with_function(void)
1424{
1425}
1426#endif
1427
1428static __init int event_trace_self_tests_init(void)
1429{
1430
1431 event_trace_self_tests();
1432
1433 event_trace_self_test_with_function();
Steven Rostedte6187002009-04-15 13:36:40 -04001434
1435 return 0;
1436}
1437
Steven Rostedt28d20e22009-04-20 12:12:44 -04001438late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04001439
1440#endif