perf hists: Factor out duplicated code
[deliverable/linux.git] / kernel / trace / trace_events.c
... / ...
CommitLineData
1/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
9 */
10
11#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
14#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
18#include <linux/slab.h>
19#include <linux/delay.h>
20
21#include <asm/setup.h>
22
23#include "trace_output.h"
24
25#undef TRACE_SYSTEM
26#define TRACE_SYSTEM "TRACE_SYSTEM"
27
28DEFINE_MUTEX(event_mutex);
29
30LIST_HEAD(ftrace_events);
31LIST_HEAD(ftrace_common_fields);
32
33struct list_head *
34trace_get_fields(struct ftrace_event_call *event_call)
35{
36 if (!event_call->class->get_fields)
37 return &event_call->class->fields;
38 return event_call->class->get_fields(event_call);
39}
40
41static int __trace_define_field(struct list_head *head, const char *type,
42 const char *name, int offset, int size,
43 int is_signed, int filter_type)
44{
45 struct ftrace_event_field *field;
46
47 field = kzalloc(sizeof(*field), GFP_KERNEL);
48 if (!field)
49 goto err;
50
51 field->name = kstrdup(name, GFP_KERNEL);
52 if (!field->name)
53 goto err;
54
55 field->type = kstrdup(type, GFP_KERNEL);
56 if (!field->type)
57 goto err;
58
59 if (filter_type == FILTER_OTHER)
60 field->filter_type = filter_assign_type(type);
61 else
62 field->filter_type = filter_type;
63
64 field->offset = offset;
65 field->size = size;
66 field->is_signed = is_signed;
67
68 list_add(&field->link, head);
69
70 return 0;
71
72err:
73 if (field)
74 kfree(field->name);
75 kfree(field);
76
77 return -ENOMEM;
78}
79
80int trace_define_field(struct ftrace_event_call *call, const char *type,
81 const char *name, int offset, int size, int is_signed,
82 int filter_type)
83{
84 struct list_head *head;
85
86 if (WARN_ON(!call->class))
87 return 0;
88
89 head = trace_get_fields(call);
90 return __trace_define_field(head, type, name, offset, size,
91 is_signed, filter_type);
92}
93EXPORT_SYMBOL_GPL(trace_define_field);
94
95#define __common_field(type, item) \
96 ret = __trace_define_field(&ftrace_common_fields, #type, \
97 "common_" #item, \
98 offsetof(typeof(ent), item), \
99 sizeof(ent.item), \
100 is_signed_type(type), FILTER_OTHER); \
101 if (ret) \
102 return ret;
103
104static int trace_define_common_fields(void)
105{
106 int ret;
107 struct trace_entry ent;
108
109 __common_field(unsigned short, type);
110 __common_field(unsigned char, flags);
111 __common_field(unsigned char, preempt_count);
112 __common_field(int, pid);
113 __common_field(int, lock_depth);
114
115 return ret;
116}
117
118void trace_destroy_fields(struct ftrace_event_call *call)
119{
120 struct ftrace_event_field *field, *next;
121 struct list_head *head;
122
123 head = trace_get_fields(call);
124 list_for_each_entry_safe(field, next, head, link) {
125 list_del(&field->link);
126 kfree(field->type);
127 kfree(field->name);
128 kfree(field);
129 }
130}
131
132int trace_event_raw_init(struct ftrace_event_call *call)
133{
134 int id;
135
136 id = register_ftrace_event(&call->event);
137 if (!id)
138 return -ENODEV;
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(trace_event_raw_init);
143
144int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type)
145{
146 switch (type) {
147 case TRACE_REG_REGISTER:
148 return tracepoint_probe_register(call->name,
149 call->class->probe,
150 call);
151 case TRACE_REG_UNREGISTER:
152 tracepoint_probe_unregister(call->name,
153 call->class->probe,
154 call);
155 return 0;
156
157#ifdef CONFIG_PERF_EVENTS
158 case TRACE_REG_PERF_REGISTER:
159 return tracepoint_probe_register(call->name,
160 call->class->perf_probe,
161 call);
162 case TRACE_REG_PERF_UNREGISTER:
163 tracepoint_probe_unregister(call->name,
164 call->class->perf_probe,
165 call);
166 return 0;
167#endif
168 }
169 return 0;
170}
171EXPORT_SYMBOL_GPL(ftrace_event_reg);
172
173static int ftrace_event_enable_disable(struct ftrace_event_call *call,
174 int enable)
175{
176 int ret = 0;
177
178 switch (enable) {
179 case 0:
180 if (call->flags & TRACE_EVENT_FL_ENABLED) {
181 call->flags &= ~TRACE_EVENT_FL_ENABLED;
182 tracing_stop_cmdline_record();
183 call->class->reg(call, TRACE_REG_UNREGISTER);
184 }
185 break;
186 case 1:
187 if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
188 tracing_start_cmdline_record();
189 ret = call->class->reg(call, TRACE_REG_REGISTER);
190 if (ret) {
191 tracing_stop_cmdline_record();
192 pr_info("event trace: Could not enable event "
193 "%s\n", call->name);
194 break;
195 }
196 call->flags |= TRACE_EVENT_FL_ENABLED;
197 }
198 break;
199 }
200
201 return ret;
202}
203
204static void ftrace_clear_events(void)
205{
206 struct ftrace_event_call *call;
207
208 mutex_lock(&event_mutex);
209 list_for_each_entry(call, &ftrace_events, list) {
210 ftrace_event_enable_disable(call, 0);
211 }
212 mutex_unlock(&event_mutex);
213}
214
215/*
216 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
217 */
218static int __ftrace_set_clr_event(const char *match, const char *sub,
219 const char *event, int set)
220{
221 struct ftrace_event_call *call;
222 int ret = -EINVAL;
223
224 mutex_lock(&event_mutex);
225 list_for_each_entry(call, &ftrace_events, list) {
226
227 if (!call->name || !call->class || !call->class->reg)
228 continue;
229
230 if (match &&
231 strcmp(match, call->name) != 0 &&
232 strcmp(match, call->class->system) != 0)
233 continue;
234
235 if (sub && strcmp(sub, call->class->system) != 0)
236 continue;
237
238 if (event && strcmp(event, call->name) != 0)
239 continue;
240
241 ftrace_event_enable_disable(call, set);
242
243 ret = 0;
244 }
245 mutex_unlock(&event_mutex);
246
247 return ret;
248}
249
250static int ftrace_set_clr_event(char *buf, int set)
251{
252 char *event = NULL, *sub = NULL, *match;
253
254 /*
255 * The buf format can be <subsystem>:<event-name>
256 * *:<event-name> means any event by that name.
257 * :<event-name> is the same.
258 *
259 * <subsystem>:* means all events in that subsystem
260 * <subsystem>: means the same.
261 *
262 * <name> (no ':') means all events in a subsystem with
263 * the name <name> or any event that matches <name>
264 */
265
266 match = strsep(&buf, ":");
267 if (buf) {
268 sub = match;
269 event = buf;
270 match = NULL;
271
272 if (!strlen(sub) || strcmp(sub, "*") == 0)
273 sub = NULL;
274 if (!strlen(event) || strcmp(event, "*") == 0)
275 event = NULL;
276 }
277
278 return __ftrace_set_clr_event(match, sub, event, set);
279}
280
281/**
282 * trace_set_clr_event - enable or disable an event
283 * @system: system name to match (NULL for any system)
284 * @event: event name to match (NULL for all events, within system)
285 * @set: 1 to enable, 0 to disable
286 *
287 * This is a way for other parts of the kernel to enable or disable
288 * event recording.
289 *
290 * Returns 0 on success, -EINVAL if the parameters do not match any
291 * registered events.
292 */
293int trace_set_clr_event(const char *system, const char *event, int set)
294{
295 return __ftrace_set_clr_event(NULL, system, event, set);
296}
297
298/* 128 should be much more than enough */
299#define EVENT_BUF_SIZE 127
300
301static ssize_t
302ftrace_event_write(struct file *file, const char __user *ubuf,
303 size_t cnt, loff_t *ppos)
304{
305 struct trace_parser parser;
306 ssize_t read, ret;
307
308 if (!cnt)
309 return 0;
310
311 ret = tracing_update_buffers();
312 if (ret < 0)
313 return ret;
314
315 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
316 return -ENOMEM;
317
318 read = trace_get_user(&parser, ubuf, cnt, ppos);
319
320 if (read >= 0 && trace_parser_loaded((&parser))) {
321 int set = 1;
322
323 if (*parser.buffer == '!')
324 set = 0;
325
326 parser.buffer[parser.idx] = 0;
327
328 ret = ftrace_set_clr_event(parser.buffer + !set, set);
329 if (ret)
330 goto out_put;
331 }
332
333 ret = read;
334
335 out_put:
336 trace_parser_put(&parser);
337
338 return ret;
339}
340
341static void *
342t_next(struct seq_file *m, void *v, loff_t *pos)
343{
344 struct ftrace_event_call *call = v;
345
346 (*pos)++;
347
348 list_for_each_entry_continue(call, &ftrace_events, list) {
349 /*
350 * The ftrace subsystem is for showing formats only.
351 * They can not be enabled or disabled via the event files.
352 */
353 if (call->class && call->class->reg)
354 return call;
355 }
356
357 return NULL;
358}
359
360static void *t_start(struct seq_file *m, loff_t *pos)
361{
362 struct ftrace_event_call *call;
363 loff_t l;
364
365 mutex_lock(&event_mutex);
366
367 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
368 for (l = 0; l <= *pos; ) {
369 call = t_next(m, call, &l);
370 if (!call)
371 break;
372 }
373 return call;
374}
375
376static void *
377s_next(struct seq_file *m, void *v, loff_t *pos)
378{
379 struct ftrace_event_call *call = v;
380
381 (*pos)++;
382
383 list_for_each_entry_continue(call, &ftrace_events, list) {
384 if (call->flags & TRACE_EVENT_FL_ENABLED)
385 return call;
386 }
387
388 return NULL;
389}
390
391static void *s_start(struct seq_file *m, loff_t *pos)
392{
393 struct ftrace_event_call *call;
394 loff_t l;
395
396 mutex_lock(&event_mutex);
397
398 call = list_entry(&ftrace_events, struct ftrace_event_call, list);
399 for (l = 0; l <= *pos; ) {
400 call = s_next(m, call, &l);
401 if (!call)
402 break;
403 }
404 return call;
405}
406
407static int t_show(struct seq_file *m, void *v)
408{
409 struct ftrace_event_call *call = v;
410
411 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
412 seq_printf(m, "%s:", call->class->system);
413 seq_printf(m, "%s\n", call->name);
414
415 return 0;
416}
417
418static void t_stop(struct seq_file *m, void *p)
419{
420 mutex_unlock(&event_mutex);
421}
422
423static int
424ftrace_event_seq_open(struct inode *inode, struct file *file)
425{
426 const struct seq_operations *seq_ops;
427
428 if ((file->f_mode & FMODE_WRITE) &&
429 (file->f_flags & O_TRUNC))
430 ftrace_clear_events();
431
432 seq_ops = inode->i_private;
433 return seq_open(file, seq_ops);
434}
435
436static ssize_t
437event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
438 loff_t *ppos)
439{
440 struct ftrace_event_call *call = filp->private_data;
441 char *buf;
442
443 if (call->flags & TRACE_EVENT_FL_ENABLED)
444 buf = "1\n";
445 else
446 buf = "0\n";
447
448 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
449}
450
451static ssize_t
452event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
453 loff_t *ppos)
454{
455 struct ftrace_event_call *call = filp->private_data;
456 char buf[64];
457 unsigned long val;
458 int ret;
459
460 if (cnt >= sizeof(buf))
461 return -EINVAL;
462
463 if (copy_from_user(&buf, ubuf, cnt))
464 return -EFAULT;
465
466 buf[cnt] = 0;
467
468 ret = strict_strtoul(buf, 10, &val);
469 if (ret < 0)
470 return ret;
471
472 ret = tracing_update_buffers();
473 if (ret < 0)
474 return ret;
475
476 switch (val) {
477 case 0:
478 case 1:
479 mutex_lock(&event_mutex);
480 ret = ftrace_event_enable_disable(call, val);
481 mutex_unlock(&event_mutex);
482 break;
483
484 default:
485 return -EINVAL;
486 }
487
488 *ppos += cnt;
489
490 return ret ? ret : cnt;
491}
492
493static ssize_t
494system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
495 loff_t *ppos)
496{
497 const char set_to_char[4] = { '?', '0', '1', 'X' };
498 const char *system = filp->private_data;
499 struct ftrace_event_call *call;
500 char buf[2];
501 int set = 0;
502 int ret;
503
504 mutex_lock(&event_mutex);
505 list_for_each_entry(call, &ftrace_events, list) {
506 if (!call->name || !call->class || !call->class->reg)
507 continue;
508
509 if (system && strcmp(call->class->system, system) != 0)
510 continue;
511
512 /*
513 * We need to find out if all the events are set
514 * or if all events or cleared, or if we have
515 * a mixture.
516 */
517 set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
518
519 /*
520 * If we have a mixture, no need to look further.
521 */
522 if (set == 3)
523 break;
524 }
525 mutex_unlock(&event_mutex);
526
527 buf[0] = set_to_char[set];
528 buf[1] = '\n';
529
530 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
531
532 return ret;
533}
534
535static ssize_t
536system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
537 loff_t *ppos)
538{
539 const char *system = filp->private_data;
540 unsigned long val;
541 char buf[64];
542 ssize_t ret;
543
544 if (cnt >= sizeof(buf))
545 return -EINVAL;
546
547 if (copy_from_user(&buf, ubuf, cnt))
548 return -EFAULT;
549
550 buf[cnt] = 0;
551
552 ret = strict_strtoul(buf, 10, &val);
553 if (ret < 0)
554 return ret;
555
556 ret = tracing_update_buffers();
557 if (ret < 0)
558 return ret;
559
560 if (val != 0 && val != 1)
561 return -EINVAL;
562
563 ret = __ftrace_set_clr_event(NULL, system, NULL, val);
564 if (ret)
565 goto out;
566
567 ret = cnt;
568
569out:
570 *ppos += cnt;
571
572 return ret;
573}
574
575static void print_event_fields(struct trace_seq *s, struct list_head *head)
576{
577 struct ftrace_event_field *field;
578
579 list_for_each_entry_reverse(field, head, link) {
580 /*
581 * Smartly shows the array type(except dynamic array).
582 * Normal:
583 * field:TYPE VAR
584 * If TYPE := TYPE[LEN], it is shown:
585 * field:TYPE VAR[LEN]
586 */
587 const char *array_descriptor = strchr(field->type, '[');
588
589 if (!strncmp(field->type, "__data_loc", 10))
590 array_descriptor = NULL;
591
592 if (!array_descriptor) {
593 trace_seq_printf(s, "\tfield:%s %s;\toffset:%u;"
594 "\tsize:%u;\tsigned:%d;\n",
595 field->type, field->name, field->offset,
596 field->size, !!field->is_signed);
597 } else {
598 trace_seq_printf(s, "\tfield:%.*s %s%s;\toffset:%u;"
599 "\tsize:%u;\tsigned:%d;\n",
600 (int)(array_descriptor - field->type),
601 field->type, field->name,
602 array_descriptor, field->offset,
603 field->size, !!field->is_signed);
604 }
605 }
606}
607
608static ssize_t
609event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
610 loff_t *ppos)
611{
612 struct ftrace_event_call *call = filp->private_data;
613 struct list_head *head;
614 struct trace_seq *s;
615 char *buf;
616 int r;
617
618 if (*ppos)
619 return 0;
620
621 s = kmalloc(sizeof(*s), GFP_KERNEL);
622 if (!s)
623 return -ENOMEM;
624
625 trace_seq_init(s);
626
627 trace_seq_printf(s, "name: %s\n", call->name);
628 trace_seq_printf(s, "ID: %d\n", call->event.type);
629 trace_seq_printf(s, "format:\n");
630
631 /* print common fields */
632 print_event_fields(s, &ftrace_common_fields);
633
634 trace_seq_putc(s, '\n');
635
636 /* print event specific fields */
637 head = trace_get_fields(call);
638 print_event_fields(s, head);
639
640 r = trace_seq_printf(s, "\nprint fmt: %s\n", call->print_fmt);
641
642 if (!r) {
643 /*
644 * ug! The format output is bigger than a PAGE!!
645 */
646 buf = "FORMAT TOO BIG\n";
647 r = simple_read_from_buffer(ubuf, cnt, ppos,
648 buf, strlen(buf));
649 goto out;
650 }
651
652 r = simple_read_from_buffer(ubuf, cnt, ppos,
653 s->buffer, s->len);
654 out:
655 kfree(s);
656 return r;
657}
658
659static ssize_t
660event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
661{
662 struct ftrace_event_call *call = filp->private_data;
663 struct trace_seq *s;
664 int r;
665
666 if (*ppos)
667 return 0;
668
669 s = kmalloc(sizeof(*s), GFP_KERNEL);
670 if (!s)
671 return -ENOMEM;
672
673 trace_seq_init(s);
674 trace_seq_printf(s, "%d\n", call->event.type);
675
676 r = simple_read_from_buffer(ubuf, cnt, ppos,
677 s->buffer, s->len);
678 kfree(s);
679 return r;
680}
681
682static ssize_t
683event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
684 loff_t *ppos)
685{
686 struct ftrace_event_call *call = filp->private_data;
687 struct trace_seq *s;
688 int r;
689
690 if (*ppos)
691 return 0;
692
693 s = kmalloc(sizeof(*s), GFP_KERNEL);
694 if (!s)
695 return -ENOMEM;
696
697 trace_seq_init(s);
698
699 print_event_filter(call, s);
700 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
701
702 kfree(s);
703
704 return r;
705}
706
707static ssize_t
708event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
709 loff_t *ppos)
710{
711 struct ftrace_event_call *call = filp->private_data;
712 char *buf;
713 int err;
714
715 if (cnt >= PAGE_SIZE)
716 return -EINVAL;
717
718 buf = (char *)__get_free_page(GFP_TEMPORARY);
719 if (!buf)
720 return -ENOMEM;
721
722 if (copy_from_user(buf, ubuf, cnt)) {
723 free_page((unsigned long) buf);
724 return -EFAULT;
725 }
726 buf[cnt] = '\0';
727
728 err = apply_event_filter(call, buf);
729 free_page((unsigned long) buf);
730 if (err < 0)
731 return err;
732
733 *ppos += cnt;
734
735 return cnt;
736}
737
738static ssize_t
739subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
740 loff_t *ppos)
741{
742 struct event_subsystem *system = filp->private_data;
743 struct trace_seq *s;
744 int r;
745
746 if (*ppos)
747 return 0;
748
749 s = kmalloc(sizeof(*s), GFP_KERNEL);
750 if (!s)
751 return -ENOMEM;
752
753 trace_seq_init(s);
754
755 print_subsystem_event_filter(system, s);
756 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
757
758 kfree(s);
759
760 return r;
761}
762
763static ssize_t
764subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
765 loff_t *ppos)
766{
767 struct event_subsystem *system = filp->private_data;
768 char *buf;
769 int err;
770
771 if (cnt >= PAGE_SIZE)
772 return -EINVAL;
773
774 buf = (char *)__get_free_page(GFP_TEMPORARY);
775 if (!buf)
776 return -ENOMEM;
777
778 if (copy_from_user(buf, ubuf, cnt)) {
779 free_page((unsigned long) buf);
780 return -EFAULT;
781 }
782 buf[cnt] = '\0';
783
784 err = apply_subsystem_event_filter(system, buf);
785 free_page((unsigned long) buf);
786 if (err < 0)
787 return err;
788
789 *ppos += cnt;
790
791 return cnt;
792}
793
794static ssize_t
795show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
796{
797 int (*func)(struct trace_seq *s) = filp->private_data;
798 struct trace_seq *s;
799 int r;
800
801 if (*ppos)
802 return 0;
803
804 s = kmalloc(sizeof(*s), GFP_KERNEL);
805 if (!s)
806 return -ENOMEM;
807
808 trace_seq_init(s);
809
810 func(s);
811 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
812
813 kfree(s);
814
815 return r;
816}
817
818static const struct seq_operations show_event_seq_ops = {
819 .start = t_start,
820 .next = t_next,
821 .show = t_show,
822 .stop = t_stop,
823};
824
825static const struct seq_operations show_set_event_seq_ops = {
826 .start = s_start,
827 .next = s_next,
828 .show = t_show,
829 .stop = t_stop,
830};
831
832static const struct file_operations ftrace_avail_fops = {
833 .open = ftrace_event_seq_open,
834 .read = seq_read,
835 .llseek = seq_lseek,
836 .release = seq_release,
837};
838
839static const struct file_operations ftrace_set_event_fops = {
840 .open = ftrace_event_seq_open,
841 .read = seq_read,
842 .write = ftrace_event_write,
843 .llseek = seq_lseek,
844 .release = seq_release,
845};
846
847static const struct file_operations ftrace_enable_fops = {
848 .open = tracing_open_generic,
849 .read = event_enable_read,
850 .write = event_enable_write,
851};
852
853static const struct file_operations ftrace_event_format_fops = {
854 .open = tracing_open_generic,
855 .read = event_format_read,
856};
857
858static const struct file_operations ftrace_event_id_fops = {
859 .open = tracing_open_generic,
860 .read = event_id_read,
861};
862
863static const struct file_operations ftrace_event_filter_fops = {
864 .open = tracing_open_generic,
865 .read = event_filter_read,
866 .write = event_filter_write,
867};
868
869static const struct file_operations ftrace_subsystem_filter_fops = {
870 .open = tracing_open_generic,
871 .read = subsystem_filter_read,
872 .write = subsystem_filter_write,
873};
874
875static const struct file_operations ftrace_system_enable_fops = {
876 .open = tracing_open_generic,
877 .read = system_enable_read,
878 .write = system_enable_write,
879};
880
881static const struct file_operations ftrace_show_header_fops = {
882 .open = tracing_open_generic,
883 .read = show_header,
884};
885
886static struct dentry *event_trace_events_dir(void)
887{
888 static struct dentry *d_tracer;
889 static struct dentry *d_events;
890
891 if (d_events)
892 return d_events;
893
894 d_tracer = tracing_init_dentry();
895 if (!d_tracer)
896 return NULL;
897
898 d_events = debugfs_create_dir("events", d_tracer);
899 if (!d_events)
900 pr_warning("Could not create debugfs "
901 "'events' directory\n");
902
903 return d_events;
904}
905
906static LIST_HEAD(event_subsystems);
907
908static struct dentry *
909event_subsystem_dir(const char *name, struct dentry *d_events)
910{
911 struct event_subsystem *system;
912 struct dentry *entry;
913
914 /* First see if we did not already create this dir */
915 list_for_each_entry(system, &event_subsystems, list) {
916 if (strcmp(system->name, name) == 0) {
917 system->nr_events++;
918 return system->entry;
919 }
920 }
921
922 /* need to create new entry */
923 system = kmalloc(sizeof(*system), GFP_KERNEL);
924 if (!system) {
925 pr_warning("No memory to create event subsystem %s\n",
926 name);
927 return d_events;
928 }
929
930 system->entry = debugfs_create_dir(name, d_events);
931 if (!system->entry) {
932 pr_warning("Could not create event subsystem %s\n",
933 name);
934 kfree(system);
935 return d_events;
936 }
937
938 system->nr_events = 1;
939 system->name = kstrdup(name, GFP_KERNEL);
940 if (!system->name) {
941 debugfs_remove(system->entry);
942 kfree(system);
943 return d_events;
944 }
945
946 list_add(&system->list, &event_subsystems);
947
948 system->filter = NULL;
949
950 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
951 if (!system->filter) {
952 pr_warning("Could not allocate filter for subsystem "
953 "'%s'\n", name);
954 return system->entry;
955 }
956
957 entry = debugfs_create_file("filter", 0644, system->entry, system,
958 &ftrace_subsystem_filter_fops);
959 if (!entry) {
960 kfree(system->filter);
961 system->filter = NULL;
962 pr_warning("Could not create debugfs "
963 "'%s/filter' entry\n", name);
964 }
965
966 trace_create_file("enable", 0644, system->entry,
967 (void *)system->name,
968 &ftrace_system_enable_fops);
969
970 return system->entry;
971}
972
973static int
974event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
975 const struct file_operations *id,
976 const struct file_operations *enable,
977 const struct file_operations *filter,
978 const struct file_operations *format)
979{
980 struct list_head *head;
981 int ret;
982
983 /*
984 * If the trace point header did not define TRACE_SYSTEM
985 * then the system would be called "TRACE_SYSTEM".
986 */
987 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
988 d_events = event_subsystem_dir(call->class->system, d_events);
989
990 call->dir = debugfs_create_dir(call->name, d_events);
991 if (!call->dir) {
992 pr_warning("Could not create debugfs "
993 "'%s' directory\n", call->name);
994 return -1;
995 }
996
997 if (call->class->reg)
998 trace_create_file("enable", 0644, call->dir, call,
999 enable);
1000
1001#ifdef CONFIG_PERF_EVENTS
1002 if (call->event.type && call->class->reg)
1003 trace_create_file("id", 0444, call->dir, call,
1004 id);
1005#endif
1006
1007 /*
1008 * Other events may have the same class. Only update
1009 * the fields if they are not already defined.
1010 */
1011 head = trace_get_fields(call);
1012 if (list_empty(head)) {
1013 ret = call->class->define_fields(call);
1014 if (ret < 0) {
1015 pr_warning("Could not initialize trace point"
1016 " events/%s\n", call->name);
1017 return ret;
1018 }
1019 }
1020 trace_create_file("filter", 0644, call->dir, call,
1021 filter);
1022
1023 trace_create_file("format", 0444, call->dir, call,
1024 format);
1025
1026 return 0;
1027}
1028
1029static int
1030__trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
1031 const struct file_operations *id,
1032 const struct file_operations *enable,
1033 const struct file_operations *filter,
1034 const struct file_operations *format)
1035{
1036 struct dentry *d_events;
1037 int ret;
1038
1039 /* The linker may leave blanks */
1040 if (!call->name)
1041 return -EINVAL;
1042
1043 if (call->class->raw_init) {
1044 ret = call->class->raw_init(call);
1045 if (ret < 0) {
1046 if (ret != -ENOSYS)
1047 pr_warning("Could not initialize trace events/%s\n",
1048 call->name);
1049 return ret;
1050 }
1051 }
1052
1053 d_events = event_trace_events_dir();
1054 if (!d_events)
1055 return -ENOENT;
1056
1057 ret = event_create_dir(call, d_events, id, enable, filter, format);
1058 if (!ret)
1059 list_add(&call->list, &ftrace_events);
1060 call->mod = mod;
1061
1062 return ret;
1063}
1064
1065/* Add an additional event_call dynamically */
1066int trace_add_event_call(struct ftrace_event_call *call)
1067{
1068 int ret;
1069 mutex_lock(&event_mutex);
1070 ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1071 &ftrace_enable_fops,
1072 &ftrace_event_filter_fops,
1073 &ftrace_event_format_fops);
1074 mutex_unlock(&event_mutex);
1075 return ret;
1076}
1077
1078static void remove_subsystem_dir(const char *name)
1079{
1080 struct event_subsystem *system;
1081
1082 if (strcmp(name, TRACE_SYSTEM) == 0)
1083 return;
1084
1085 list_for_each_entry(system, &event_subsystems, list) {
1086 if (strcmp(system->name, name) == 0) {
1087 if (!--system->nr_events) {
1088 struct event_filter *filter = system->filter;
1089
1090 debugfs_remove_recursive(system->entry);
1091 list_del(&system->list);
1092 if (filter) {
1093 kfree(filter->filter_string);
1094 kfree(filter);
1095 }
1096 kfree(system->name);
1097 kfree(system);
1098 }
1099 break;
1100 }
1101 }
1102}
1103
1104/*
1105 * Must be called under locking both of event_mutex and trace_event_mutex.
1106 */
1107static void __trace_remove_event_call(struct ftrace_event_call *call)
1108{
1109 ftrace_event_enable_disable(call, 0);
1110 if (call->event.funcs)
1111 __unregister_ftrace_event(&call->event);
1112 debugfs_remove_recursive(call->dir);
1113 list_del(&call->list);
1114 trace_destroy_fields(call);
1115 destroy_preds(call);
1116 remove_subsystem_dir(call->class->system);
1117}
1118
1119/* Remove an event_call */
1120void trace_remove_event_call(struct ftrace_event_call *call)
1121{
1122 mutex_lock(&event_mutex);
1123 down_write(&trace_event_mutex);
1124 __trace_remove_event_call(call);
1125 up_write(&trace_event_mutex);
1126 mutex_unlock(&event_mutex);
1127}
1128
1129#define for_each_event(event, start, end) \
1130 for (event = start; \
1131 (unsigned long)event < (unsigned long)end; \
1132 event++)
1133
1134#ifdef CONFIG_MODULES
1135
1136static LIST_HEAD(ftrace_module_file_list);
1137
1138/*
1139 * Modules must own their file_operations to keep up with
1140 * reference counting.
1141 */
1142struct ftrace_module_file_ops {
1143 struct list_head list;
1144 struct module *mod;
1145 struct file_operations id;
1146 struct file_operations enable;
1147 struct file_operations format;
1148 struct file_operations filter;
1149};
1150
1151static struct ftrace_module_file_ops *
1152trace_create_file_ops(struct module *mod)
1153{
1154 struct ftrace_module_file_ops *file_ops;
1155
1156 /*
1157 * This is a bit of a PITA. To allow for correct reference
1158 * counting, modules must "own" their file_operations.
1159 * To do this, we allocate the file operations that will be
1160 * used in the event directory.
1161 */
1162
1163 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1164 if (!file_ops)
1165 return NULL;
1166
1167 file_ops->mod = mod;
1168
1169 file_ops->id = ftrace_event_id_fops;
1170 file_ops->id.owner = mod;
1171
1172 file_ops->enable = ftrace_enable_fops;
1173 file_ops->enable.owner = mod;
1174
1175 file_ops->filter = ftrace_event_filter_fops;
1176 file_ops->filter.owner = mod;
1177
1178 file_ops->format = ftrace_event_format_fops;
1179 file_ops->format.owner = mod;
1180
1181 list_add(&file_ops->list, &ftrace_module_file_list);
1182
1183 return file_ops;
1184}
1185
1186static void trace_module_add_events(struct module *mod)
1187{
1188 struct ftrace_module_file_ops *file_ops = NULL;
1189 struct ftrace_event_call *call, *start, *end;
1190
1191 start = mod->trace_events;
1192 end = mod->trace_events + mod->num_trace_events;
1193
1194 if (start == end)
1195 return;
1196
1197 file_ops = trace_create_file_ops(mod);
1198 if (!file_ops)
1199 return;
1200
1201 for_each_event(call, start, end) {
1202 __trace_add_event_call(call, mod,
1203 &file_ops->id, &file_ops->enable,
1204 &file_ops->filter, &file_ops->format);
1205 }
1206}
1207
1208static void trace_module_remove_events(struct module *mod)
1209{
1210 struct ftrace_module_file_ops *file_ops;
1211 struct ftrace_event_call *call, *p;
1212 bool found = false;
1213
1214 down_write(&trace_event_mutex);
1215 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1216 if (call->mod == mod) {
1217 found = true;
1218 __trace_remove_event_call(call);
1219 }
1220 }
1221
1222 /* Now free the file_operations */
1223 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1224 if (file_ops->mod == mod)
1225 break;
1226 }
1227 if (&file_ops->list != &ftrace_module_file_list) {
1228 list_del(&file_ops->list);
1229 kfree(file_ops);
1230 }
1231
1232 /*
1233 * It is safest to reset the ring buffer if the module being unloaded
1234 * registered any events.
1235 */
1236 if (found)
1237 tracing_reset_current_online_cpus();
1238 up_write(&trace_event_mutex);
1239}
1240
1241static int trace_module_notify(struct notifier_block *self,
1242 unsigned long val, void *data)
1243{
1244 struct module *mod = data;
1245
1246 mutex_lock(&event_mutex);
1247 switch (val) {
1248 case MODULE_STATE_COMING:
1249 trace_module_add_events(mod);
1250 break;
1251 case MODULE_STATE_GOING:
1252 trace_module_remove_events(mod);
1253 break;
1254 }
1255 mutex_unlock(&event_mutex);
1256
1257 return 0;
1258}
1259#else
1260static int trace_module_notify(struct notifier_block *self,
1261 unsigned long val, void *data)
1262{
1263 return 0;
1264}
1265#endif /* CONFIG_MODULES */
1266
1267static struct notifier_block trace_module_nb = {
1268 .notifier_call = trace_module_notify,
1269 .priority = 0,
1270};
1271
1272extern struct ftrace_event_call __start_ftrace_events[];
1273extern struct ftrace_event_call __stop_ftrace_events[];
1274
1275static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
1276
1277static __init int setup_trace_event(char *str)
1278{
1279 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
1280 ring_buffer_expanded = 1;
1281 tracing_selftest_disabled = 1;
1282
1283 return 1;
1284}
1285__setup("trace_event=", setup_trace_event);
1286
1287static __init int event_trace_init(void)
1288{
1289 struct ftrace_event_call *call;
1290 struct dentry *d_tracer;
1291 struct dentry *entry;
1292 struct dentry *d_events;
1293 int ret;
1294 char *buf = bootup_event_buf;
1295 char *token;
1296
1297 d_tracer = tracing_init_dentry();
1298 if (!d_tracer)
1299 return 0;
1300
1301 entry = debugfs_create_file("available_events", 0444, d_tracer,
1302 (void *)&show_event_seq_ops,
1303 &ftrace_avail_fops);
1304 if (!entry)
1305 pr_warning("Could not create debugfs "
1306 "'available_events' entry\n");
1307
1308 entry = debugfs_create_file("set_event", 0644, d_tracer,
1309 (void *)&show_set_event_seq_ops,
1310 &ftrace_set_event_fops);
1311 if (!entry)
1312 pr_warning("Could not create debugfs "
1313 "'set_event' entry\n");
1314
1315 d_events = event_trace_events_dir();
1316 if (!d_events)
1317 return 0;
1318
1319 /* ring buffer internal formats */
1320 trace_create_file("header_page", 0444, d_events,
1321 ring_buffer_print_page_header,
1322 &ftrace_show_header_fops);
1323
1324 trace_create_file("header_event", 0444, d_events,
1325 ring_buffer_print_entry_header,
1326 &ftrace_show_header_fops);
1327
1328 trace_create_file("enable", 0644, d_events,
1329 NULL, &ftrace_system_enable_fops);
1330
1331 if (trace_define_common_fields())
1332 pr_warning("tracing: Failed to allocate common fields");
1333
1334 for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1335 __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1336 &ftrace_enable_fops,
1337 &ftrace_event_filter_fops,
1338 &ftrace_event_format_fops);
1339 }
1340
1341 while (true) {
1342 token = strsep(&buf, ",");
1343
1344 if (!token)
1345 break;
1346 if (!*token)
1347 continue;
1348
1349 ret = ftrace_set_clr_event(token, 1);
1350 if (ret)
1351 pr_warning("Failed to enable trace event: %s\n", token);
1352 }
1353
1354 ret = register_module_notifier(&trace_module_nb);
1355 if (ret)
1356 pr_warning("Failed to register trace events module notifier\n");
1357
1358 return 0;
1359}
1360fs_initcall(event_trace_init);
1361
1362#ifdef CONFIG_FTRACE_STARTUP_TEST
1363
1364static DEFINE_SPINLOCK(test_spinlock);
1365static DEFINE_SPINLOCK(test_spinlock_irq);
1366static DEFINE_MUTEX(test_mutex);
1367
1368static __init void test_work(struct work_struct *dummy)
1369{
1370 spin_lock(&test_spinlock);
1371 spin_lock_irq(&test_spinlock_irq);
1372 udelay(1);
1373 spin_unlock_irq(&test_spinlock_irq);
1374 spin_unlock(&test_spinlock);
1375
1376 mutex_lock(&test_mutex);
1377 msleep(1);
1378 mutex_unlock(&test_mutex);
1379}
1380
1381static __init int event_test_thread(void *unused)
1382{
1383 void *test_malloc;
1384
1385 test_malloc = kmalloc(1234, GFP_KERNEL);
1386 if (!test_malloc)
1387 pr_info("failed to kmalloc\n");
1388
1389 schedule_on_each_cpu(test_work);
1390
1391 kfree(test_malloc);
1392
1393 set_current_state(TASK_INTERRUPTIBLE);
1394 while (!kthread_should_stop())
1395 schedule();
1396
1397 return 0;
1398}
1399
1400/*
1401 * Do various things that may trigger events.
1402 */
1403static __init void event_test_stuff(void)
1404{
1405 struct task_struct *test_thread;
1406
1407 test_thread = kthread_run(event_test_thread, NULL, "test-events");
1408 msleep(1);
1409 kthread_stop(test_thread);
1410}
1411
1412/*
1413 * For every trace event defined, we will test each trace point separately,
1414 * and then by groups, and finally all trace points.
1415 */
1416static __init void event_trace_self_tests(void)
1417{
1418 struct ftrace_event_call *call;
1419 struct event_subsystem *system;
1420 int ret;
1421
1422 pr_info("Running tests on trace events:\n");
1423
1424 list_for_each_entry(call, &ftrace_events, list) {
1425
1426 /* Only test those that have a probe */
1427 if (!call->class || !call->class->probe)
1428 continue;
1429
1430/*
1431 * Testing syscall events here is pretty useless, but
1432 * we still do it if configured. But this is time consuming.
1433 * What we really need is a user thread to perform the
1434 * syscalls as we test.
1435 */
1436#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
1437 if (call->class->system &&
1438 strcmp(call->class->system, "syscalls") == 0)
1439 continue;
1440#endif
1441
1442 pr_info("Testing event %s: ", call->name);
1443
1444 /*
1445 * If an event is already enabled, someone is using
1446 * it and the self test should not be on.
1447 */
1448 if (call->flags & TRACE_EVENT_FL_ENABLED) {
1449 pr_warning("Enabled event during self test!\n");
1450 WARN_ON_ONCE(1);
1451 continue;
1452 }
1453
1454 ftrace_event_enable_disable(call, 1);
1455 event_test_stuff();
1456 ftrace_event_enable_disable(call, 0);
1457
1458 pr_cont("OK\n");
1459 }
1460
1461 /* Now test at the sub system level */
1462
1463 pr_info("Running tests on trace event systems:\n");
1464
1465 list_for_each_entry(system, &event_subsystems, list) {
1466
1467 /* the ftrace system is special, skip it */
1468 if (strcmp(system->name, "ftrace") == 0)
1469 continue;
1470
1471 pr_info("Testing event system %s: ", system->name);
1472
1473 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
1474 if (WARN_ON_ONCE(ret)) {
1475 pr_warning("error enabling system %s\n",
1476 system->name);
1477 continue;
1478 }
1479
1480 event_test_stuff();
1481
1482 ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
1483 if (WARN_ON_ONCE(ret))
1484 pr_warning("error disabling system %s\n",
1485 system->name);
1486
1487 pr_cont("OK\n");
1488 }
1489
1490 /* Test with all events enabled */
1491
1492 pr_info("Running tests on all trace events:\n");
1493 pr_info("Testing all events: ");
1494
1495 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
1496 if (WARN_ON_ONCE(ret)) {
1497 pr_warning("error enabling all events\n");
1498 return;
1499 }
1500
1501 event_test_stuff();
1502
1503 /* reset sysname */
1504 ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
1505 if (WARN_ON_ONCE(ret)) {
1506 pr_warning("error disabling all events\n");
1507 return;
1508 }
1509
1510 pr_cont("OK\n");
1511}
1512
1513#ifdef CONFIG_FUNCTION_TRACER
1514
1515static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
1516
1517static void
1518function_test_events_call(unsigned long ip, unsigned long parent_ip)
1519{
1520 struct ring_buffer_event *event;
1521 struct ring_buffer *buffer;
1522 struct ftrace_entry *entry;
1523 unsigned long flags;
1524 long disabled;
1525 int cpu;
1526 int pc;
1527
1528 pc = preempt_count();
1529 preempt_disable_notrace();
1530 cpu = raw_smp_processor_id();
1531 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
1532
1533 if (disabled != 1)
1534 goto out;
1535
1536 local_save_flags(flags);
1537
1538 event = trace_current_buffer_lock_reserve(&buffer,
1539 TRACE_FN, sizeof(*entry),
1540 flags, pc);
1541 if (!event)
1542 goto out;
1543 entry = ring_buffer_event_data(event);
1544 entry->ip = ip;
1545 entry->parent_ip = parent_ip;
1546
1547 trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
1548
1549 out:
1550 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
1551 preempt_enable_notrace();
1552}
1553
1554static struct ftrace_ops trace_ops __initdata =
1555{
1556 .func = function_test_events_call,
1557};
1558
1559static __init void event_trace_self_test_with_function(void)
1560{
1561 register_ftrace_function(&trace_ops);
1562 pr_info("Running tests again, along with the function tracer\n");
1563 event_trace_self_tests();
1564 unregister_ftrace_function(&trace_ops);
1565}
1566#else
1567static __init void event_trace_self_test_with_function(void)
1568{
1569}
1570#endif
1571
1572static __init int event_trace_self_tests_init(void)
1573{
1574 if (!tracing_selftest_disabled) {
1575 event_trace_self_tests();
1576 event_trace_self_test_with_function();
1577 }
1578
1579 return 0;
1580}
1581
1582late_initcall(event_trace_self_tests_init);
1583
1584#endif
This page took 0.034515 seconds and 5 git commands to generate.