915c136d7bd179c853a586ca2c8b436cebe3b1f6
[profile/ivi/kernel-x86-ivi.git] / kernel / trace / trace_events.c
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
28 DEFINE_MUTEX(event_mutex);
29
30 DEFINE_MUTEX(event_storage_mutex);
31 EXPORT_SYMBOL_GPL(event_storage_mutex);
32
33 char event_storage[EVENT_STORAGE_SIZE];
34 EXPORT_SYMBOL_GPL(event_storage);
35
36 LIST_HEAD(ftrace_events);
37 static LIST_HEAD(ftrace_common_fields);
38
39 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
40
41 static struct kmem_cache *field_cachep;
42 static struct kmem_cache *file_cachep;
43
44 /* Double loops, do not use break, only goto's work */
45 #define do_for_each_event_file(tr, file)                        \
46         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
47                 list_for_each_entry(file, &tr->events, list)
48
49 #define do_for_each_event_file_safe(tr, file)                   \
50         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
51                 struct ftrace_event_file *___n;                         \
52                 list_for_each_entry_safe(file, ___n, &tr->events, list)
53
54 #define while_for_each_event_file()             \
55         }
56
57 static struct list_head *
58 trace_get_fields(struct ftrace_event_call *event_call)
59 {
60         if (!event_call->class->get_fields)
61                 return &event_call->class->fields;
62         return event_call->class->get_fields(event_call);
63 }
64
65 static struct ftrace_event_field *
66 __find_event_field(struct list_head *head, char *name)
67 {
68         struct ftrace_event_field *field;
69
70         list_for_each_entry(field, head, link) {
71                 if (!strcmp(field->name, name))
72                         return field;
73         }
74
75         return NULL;
76 }
77
78 struct ftrace_event_field *
79 trace_find_event_field(struct ftrace_event_call *call, char *name)
80 {
81         struct ftrace_event_field *field;
82         struct list_head *head;
83
84         field = __find_event_field(&ftrace_common_fields, name);
85         if (field)
86                 return field;
87
88         head = trace_get_fields(call);
89         return __find_event_field(head, name);
90 }
91
92 static int __trace_define_field(struct list_head *head, const char *type,
93                                 const char *name, int offset, int size,
94                                 int is_signed, int filter_type)
95 {
96         struct ftrace_event_field *field;
97
98         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
99         if (!field)
100                 goto err;
101
102         field->name = name;
103         field->type = type;
104
105         if (filter_type == FILTER_OTHER)
106                 field->filter_type = filter_assign_type(type);
107         else
108                 field->filter_type = filter_type;
109
110         field->offset = offset;
111         field->size = size;
112         field->is_signed = is_signed;
113
114         list_add(&field->link, head);
115
116         return 0;
117
118 err:
119         kmem_cache_free(field_cachep, field);
120
121         return -ENOMEM;
122 }
123
124 int trace_define_field(struct ftrace_event_call *call, const char *type,
125                        const char *name, int offset, int size, int is_signed,
126                        int filter_type)
127 {
128         struct list_head *head;
129
130         if (WARN_ON(!call->class))
131                 return 0;
132
133         head = trace_get_fields(call);
134         return __trace_define_field(head, type, name, offset, size,
135                                     is_signed, filter_type);
136 }
137 EXPORT_SYMBOL_GPL(trace_define_field);
138
139 #define __common_field(type, item)                                      \
140         ret = __trace_define_field(&ftrace_common_fields, #type,        \
141                                    "common_" #item,                     \
142                                    offsetof(typeof(ent), item),         \
143                                    sizeof(ent.item),                    \
144                                    is_signed_type(type), FILTER_OTHER); \
145         if (ret)                                                        \
146                 return ret;
147
148 static int trace_define_common_fields(void)
149 {
150         int ret;
151         struct trace_entry ent;
152
153         __common_field(unsigned short, type);
154         __common_field(unsigned char, flags);
155         __common_field(unsigned char, preempt_count);
156         __common_field(int, pid);
157
158         return ret;
159 }
160
161 static void trace_destroy_fields(struct ftrace_event_call *call)
162 {
163         struct ftrace_event_field *field, *next;
164         struct list_head *head;
165
166         head = trace_get_fields(call);
167         list_for_each_entry_safe(field, next, head, link) {
168                 list_del(&field->link);
169                 kmem_cache_free(field_cachep, field);
170         }
171 }
172
173 int trace_event_raw_init(struct ftrace_event_call *call)
174 {
175         int id;
176
177         id = register_ftrace_event(&call->event);
178         if (!id)
179                 return -ENODEV;
180
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(trace_event_raw_init);
184
185 int ftrace_event_reg(struct ftrace_event_call *call,
186                      enum trace_reg type, void *data)
187 {
188         struct ftrace_event_file *file = data;
189
190         switch (type) {
191         case TRACE_REG_REGISTER:
192                 return tracepoint_probe_register(call->name,
193                                                  call->class->probe,
194                                                  file);
195         case TRACE_REG_UNREGISTER:
196                 tracepoint_probe_unregister(call->name,
197                                             call->class->probe,
198                                             file);
199                 return 0;
200
201 #ifdef CONFIG_PERF_EVENTS
202         case TRACE_REG_PERF_REGISTER:
203                 return tracepoint_probe_register(call->name,
204                                                  call->class->perf_probe,
205                                                  call);
206         case TRACE_REG_PERF_UNREGISTER:
207                 tracepoint_probe_unregister(call->name,
208                                             call->class->perf_probe,
209                                             call);
210                 return 0;
211         case TRACE_REG_PERF_OPEN:
212         case TRACE_REG_PERF_CLOSE:
213         case TRACE_REG_PERF_ADD:
214         case TRACE_REG_PERF_DEL:
215                 return 0;
216 #endif
217         }
218         return 0;
219 }
220 EXPORT_SYMBOL_GPL(ftrace_event_reg);
221
222 void trace_event_enable_cmd_record(bool enable)
223 {
224         struct ftrace_event_file *file;
225         struct trace_array *tr;
226
227         mutex_lock(&event_mutex);
228         do_for_each_event_file(tr, file) {
229
230                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
231                         continue;
232
233                 if (enable) {
234                         tracing_start_cmdline_record();
235                         set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
236                 } else {
237                         tracing_stop_cmdline_record();
238                         clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
239                 }
240         } while_for_each_event_file();
241         mutex_unlock(&event_mutex);
242 }
243
244 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
245                                          int enable, int soft_disable)
246 {
247         struct ftrace_event_call *call = file->event_call;
248         int ret = 0;
249         int disable;
250
251         switch (enable) {
252         case 0:
253                 /*
254                  * When soft_disable is set and enable is cleared, we want
255                  * to clear the SOFT_DISABLED flag but leave the event in the
256                  * state that it was. That is, if the event was enabled and
257                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
258                  * is set we do not want the event to be enabled before we
259                  * clear the bit.
260                  *
261                  * When soft_disable is not set but the SOFT_MODE flag is,
262                  * we do nothing. Do not disable the tracepoint, otherwise
263                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
264                  */
265                 if (soft_disable) {
266                         disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
267                         clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
268                 } else
269                         disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
270
271                 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
272                         clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
273                         if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
274                                 tracing_stop_cmdline_record();
275                                 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
276                         }
277                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
278                 }
279                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT */
280                 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
281                         set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
282                 break;
283         case 1:
284                 /*
285                  * When soft_disable is set and enable is set, we want to
286                  * register the tracepoint for the event, but leave the event
287                  * as is. That means, if the event was already enabled, we do
288                  * nothing (but set SOFT_MODE). If the event is disabled, we
289                  * set SOFT_DISABLED before enabling the event tracepoint, so
290                  * it still seems to be disabled.
291                  */
292                 if (!soft_disable)
293                         clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
294                 else
295                         set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
296
297                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
298
299                         /* Keep the event disabled, when going to SOFT_MODE. */
300                         if (soft_disable)
301                                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
302
303                         if (trace_flags & TRACE_ITER_RECORD_CMD) {
304                                 tracing_start_cmdline_record();
305                                 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
306                         }
307                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
308                         if (ret) {
309                                 tracing_stop_cmdline_record();
310                                 pr_info("event trace: Could not enable event "
311                                         "%s\n", call->name);
312                                 break;
313                         }
314                         set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
315
316                         /* WAS_ENABLED gets set but never cleared. */
317                         call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
318                 }
319                 break;
320         }
321
322         return ret;
323 }
324
325 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
326                                        int enable)
327 {
328         return __ftrace_event_enable_disable(file, enable, 0);
329 }
330
331 static void ftrace_clear_events(struct trace_array *tr)
332 {
333         struct ftrace_event_file *file;
334
335         mutex_lock(&event_mutex);
336         list_for_each_entry(file, &tr->events, list) {
337                 ftrace_event_enable_disable(file, 0);
338         }
339         mutex_unlock(&event_mutex);
340 }
341
342 static void __put_system(struct event_subsystem *system)
343 {
344         struct event_filter *filter = system->filter;
345
346         WARN_ON_ONCE(system->ref_count == 0);
347         if (--system->ref_count)
348                 return;
349
350         list_del(&system->list);
351
352         if (filter) {
353                 kfree(filter->filter_string);
354                 kfree(filter);
355         }
356         kfree(system);
357 }
358
359 static void __get_system(struct event_subsystem *system)
360 {
361         WARN_ON_ONCE(system->ref_count == 0);
362         system->ref_count++;
363 }
364
365 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
366 {
367         WARN_ON_ONCE(dir->ref_count == 0);
368         dir->ref_count++;
369         __get_system(dir->subsystem);
370 }
371
372 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
373 {
374         WARN_ON_ONCE(dir->ref_count == 0);
375         /* If the subsystem is about to be freed, the dir must be too */
376         WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
377
378         __put_system(dir->subsystem);
379         if (!--dir->ref_count)
380                 kfree(dir);
381 }
382
383 static void put_system(struct ftrace_subsystem_dir *dir)
384 {
385         mutex_lock(&event_mutex);
386         __put_system_dir(dir);
387         mutex_unlock(&event_mutex);
388 }
389
390 /*
391  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
392  */
393 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
394                                   const char *sub, const char *event, int set)
395 {
396         struct ftrace_event_file *file;
397         struct ftrace_event_call *call;
398         int ret = -EINVAL;
399
400         mutex_lock(&event_mutex);
401         list_for_each_entry(file, &tr->events, list) {
402
403                 call = file->event_call;
404
405                 if (!call->name || !call->class || !call->class->reg)
406                         continue;
407
408                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
409                         continue;
410
411                 if (match &&
412                     strcmp(match, call->name) != 0 &&
413                     strcmp(match, call->class->system) != 0)
414                         continue;
415
416                 if (sub && strcmp(sub, call->class->system) != 0)
417                         continue;
418
419                 if (event && strcmp(event, call->name) != 0)
420                         continue;
421
422                 ftrace_event_enable_disable(file, set);
423
424                 ret = 0;
425         }
426         mutex_unlock(&event_mutex);
427
428         return ret;
429 }
430
431 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
432 {
433         char *event = NULL, *sub = NULL, *match;
434
435         /*
436          * The buf format can be <subsystem>:<event-name>
437          *  *:<event-name> means any event by that name.
438          *  :<event-name> is the same.
439          *
440          *  <subsystem>:* means all events in that subsystem
441          *  <subsystem>: means the same.
442          *
443          *  <name> (no ':') means all events in a subsystem with
444          *  the name <name> or any event that matches <name>
445          */
446
447         match = strsep(&buf, ":");
448         if (buf) {
449                 sub = match;
450                 event = buf;
451                 match = NULL;
452
453                 if (!strlen(sub) || strcmp(sub, "*") == 0)
454                         sub = NULL;
455                 if (!strlen(event) || strcmp(event, "*") == 0)
456                         event = NULL;
457         }
458
459         return __ftrace_set_clr_event(tr, match, sub, event, set);
460 }
461
462 /**
463  * trace_set_clr_event - enable or disable an event
464  * @system: system name to match (NULL for any system)
465  * @event: event name to match (NULL for all events, within system)
466  * @set: 1 to enable, 0 to disable
467  *
468  * This is a way for other parts of the kernel to enable or disable
469  * event recording.
470  *
471  * Returns 0 on success, -EINVAL if the parameters do not match any
472  * registered events.
473  */
474 int trace_set_clr_event(const char *system, const char *event, int set)
475 {
476         struct trace_array *tr = top_trace_array();
477
478         return __ftrace_set_clr_event(tr, NULL, system, event, set);
479 }
480 EXPORT_SYMBOL_GPL(trace_set_clr_event);
481
482 /* 128 should be much more than enough */
483 #define EVENT_BUF_SIZE          127
484
485 static ssize_t
486 ftrace_event_write(struct file *file, const char __user *ubuf,
487                    size_t cnt, loff_t *ppos)
488 {
489         struct trace_parser parser;
490         struct seq_file *m = file->private_data;
491         struct trace_array *tr = m->private;
492         ssize_t read, ret;
493
494         if (!cnt)
495                 return 0;
496
497         ret = tracing_update_buffers();
498         if (ret < 0)
499                 return ret;
500
501         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
502                 return -ENOMEM;
503
504         read = trace_get_user(&parser, ubuf, cnt, ppos);
505
506         if (read >= 0 && trace_parser_loaded((&parser))) {
507                 int set = 1;
508
509                 if (*parser.buffer == '!')
510                         set = 0;
511
512                 parser.buffer[parser.idx] = 0;
513
514                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
515                 if (ret)
516                         goto out_put;
517         }
518
519         ret = read;
520
521  out_put:
522         trace_parser_put(&parser);
523
524         return ret;
525 }
526
527 static void *
528 t_next(struct seq_file *m, void *v, loff_t *pos)
529 {
530         struct ftrace_event_file *file = v;
531         struct ftrace_event_call *call;
532         struct trace_array *tr = m->private;
533
534         (*pos)++;
535
536         list_for_each_entry_continue(file, &tr->events, list) {
537                 call = file->event_call;
538                 /*
539                  * The ftrace subsystem is for showing formats only.
540                  * They can not be enabled or disabled via the event files.
541                  */
542                 if (call->class && call->class->reg)
543                         return file;
544         }
545
546         return NULL;
547 }
548
549 static void *t_start(struct seq_file *m, loff_t *pos)
550 {
551         struct ftrace_event_file *file;
552         struct trace_array *tr = m->private;
553         loff_t l;
554
555         mutex_lock(&event_mutex);
556
557         file = list_entry(&tr->events, struct ftrace_event_file, list);
558         for (l = 0; l <= *pos; ) {
559                 file = t_next(m, file, &l);
560                 if (!file)
561                         break;
562         }
563         return file;
564 }
565
566 static void *
567 s_next(struct seq_file *m, void *v, loff_t *pos)
568 {
569         struct ftrace_event_file *file = v;
570         struct trace_array *tr = m->private;
571
572         (*pos)++;
573
574         list_for_each_entry_continue(file, &tr->events, list) {
575                 if (file->flags & FTRACE_EVENT_FL_ENABLED)
576                         return file;
577         }
578
579         return NULL;
580 }
581
582 static void *s_start(struct seq_file *m, loff_t *pos)
583 {
584         struct ftrace_event_file *file;
585         struct trace_array *tr = m->private;
586         loff_t l;
587
588         mutex_lock(&event_mutex);
589
590         file = list_entry(&tr->events, struct ftrace_event_file, list);
591         for (l = 0; l <= *pos; ) {
592                 file = s_next(m, file, &l);
593                 if (!file)
594                         break;
595         }
596         return file;
597 }
598
599 static int t_show(struct seq_file *m, void *v)
600 {
601         struct ftrace_event_file *file = v;
602         struct ftrace_event_call *call = file->event_call;
603
604         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
605                 seq_printf(m, "%s:", call->class->system);
606         seq_printf(m, "%s\n", call->name);
607
608         return 0;
609 }
610
611 static void t_stop(struct seq_file *m, void *p)
612 {
613         mutex_unlock(&event_mutex);
614 }
615
616 static ssize_t
617 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
618                   loff_t *ppos)
619 {
620         struct ftrace_event_file *file = filp->private_data;
621         char *buf;
622
623         if (file->flags & FTRACE_EVENT_FL_ENABLED) {
624                 if (file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)
625                         buf = "0*\n";
626                 else if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
627                         buf = "1*\n";
628                 else
629                         buf = "1\n";
630         } else
631                 buf = "0\n";
632
633         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
634 }
635
636 static ssize_t
637 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
638                    loff_t *ppos)
639 {
640         struct ftrace_event_file *file = filp->private_data;
641         unsigned long val;
642         int ret;
643
644         if (!file)
645                 return -EINVAL;
646
647         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
648         if (ret)
649                 return ret;
650
651         ret = tracing_update_buffers();
652         if (ret < 0)
653                 return ret;
654
655         switch (val) {
656         case 0:
657         case 1:
658                 mutex_lock(&event_mutex);
659                 ret = ftrace_event_enable_disable(file, val);
660                 mutex_unlock(&event_mutex);
661                 break;
662
663         default:
664                 return -EINVAL;
665         }
666
667         *ppos += cnt;
668
669         return ret ? ret : cnt;
670 }
671
672 static ssize_t
673 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
674                    loff_t *ppos)
675 {
676         const char set_to_char[4] = { '?', '0', '1', 'X' };
677         struct ftrace_subsystem_dir *dir = filp->private_data;
678         struct event_subsystem *system = dir->subsystem;
679         struct ftrace_event_call *call;
680         struct ftrace_event_file *file;
681         struct trace_array *tr = dir->tr;
682         char buf[2];
683         int set = 0;
684         int ret;
685
686         mutex_lock(&event_mutex);
687         list_for_each_entry(file, &tr->events, list) {
688                 call = file->event_call;
689                 if (!call->name || !call->class || !call->class->reg)
690                         continue;
691
692                 if (system && strcmp(call->class->system, system->name) != 0)
693                         continue;
694
695                 /*
696                  * We need to find out if all the events are set
697                  * or if all events or cleared, or if we have
698                  * a mixture.
699                  */
700                 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
701
702                 /*
703                  * If we have a mixture, no need to look further.
704                  */
705                 if (set == 3)
706                         break;
707         }
708         mutex_unlock(&event_mutex);
709
710         buf[0] = set_to_char[set];
711         buf[1] = '\n';
712
713         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
714
715         return ret;
716 }
717
718 static ssize_t
719 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
720                     loff_t *ppos)
721 {
722         struct ftrace_subsystem_dir *dir = filp->private_data;
723         struct event_subsystem *system = dir->subsystem;
724         const char *name = NULL;
725         unsigned long val;
726         ssize_t ret;
727
728         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
729         if (ret)
730                 return ret;
731
732         ret = tracing_update_buffers();
733         if (ret < 0)
734                 return ret;
735
736         if (val != 0 && val != 1)
737                 return -EINVAL;
738
739         /*
740          * Opening of "enable" adds a ref count to system,
741          * so the name is safe to use.
742          */
743         if (system)
744                 name = system->name;
745
746         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
747         if (ret)
748                 goto out;
749
750         ret = cnt;
751
752 out:
753         *ppos += cnt;
754
755         return ret;
756 }
757
758 enum {
759         FORMAT_HEADER           = 1,
760         FORMAT_FIELD_SEPERATOR  = 2,
761         FORMAT_PRINTFMT         = 3,
762 };
763
764 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
765 {
766         struct ftrace_event_call *call = m->private;
767         struct ftrace_event_field *field;
768         struct list_head *common_head = &ftrace_common_fields;
769         struct list_head *head = trace_get_fields(call);
770
771         (*pos)++;
772
773         switch ((unsigned long)v) {
774         case FORMAT_HEADER:
775                 if (unlikely(list_empty(common_head)))
776                         return NULL;
777
778                 field = list_entry(common_head->prev,
779                                    struct ftrace_event_field, link);
780                 return field;
781
782         case FORMAT_FIELD_SEPERATOR:
783                 if (unlikely(list_empty(head)))
784                         return NULL;
785
786                 field = list_entry(head->prev, struct ftrace_event_field, link);
787                 return field;
788
789         case FORMAT_PRINTFMT:
790                 /* all done */
791                 return NULL;
792         }
793
794         field = v;
795         if (field->link.prev == common_head)
796                 return (void *)FORMAT_FIELD_SEPERATOR;
797         else if (field->link.prev == head)
798                 return (void *)FORMAT_PRINTFMT;
799
800         field = list_entry(field->link.prev, struct ftrace_event_field, link);
801
802         return field;
803 }
804
805 static void *f_start(struct seq_file *m, loff_t *pos)
806 {
807         loff_t l = 0;
808         void *p;
809
810         /* Start by showing the header */
811         if (!*pos)
812                 return (void *)FORMAT_HEADER;
813
814         p = (void *)FORMAT_HEADER;
815         do {
816                 p = f_next(m, p, &l);
817         } while (p && l < *pos);
818
819         return p;
820 }
821
822 static int f_show(struct seq_file *m, void *v)
823 {
824         struct ftrace_event_call *call = m->private;
825         struct ftrace_event_field *field;
826         const char *array_descriptor;
827
828         switch ((unsigned long)v) {
829         case FORMAT_HEADER:
830                 seq_printf(m, "name: %s\n", call->name);
831                 seq_printf(m, "ID: %d\n", call->event.type);
832                 seq_printf(m, "format:\n");
833                 return 0;
834
835         case FORMAT_FIELD_SEPERATOR:
836                 seq_putc(m, '\n');
837                 return 0;
838
839         case FORMAT_PRINTFMT:
840                 seq_printf(m, "\nprint fmt: %s\n",
841                            call->print_fmt);
842                 return 0;
843         }
844
845         field = v;
846
847         /*
848          * Smartly shows the array type(except dynamic array).
849          * Normal:
850          *      field:TYPE VAR
851          * If TYPE := TYPE[LEN], it is shown:
852          *      field:TYPE VAR[LEN]
853          */
854         array_descriptor = strchr(field->type, '[');
855
856         if (!strncmp(field->type, "__data_loc", 10))
857                 array_descriptor = NULL;
858
859         if (!array_descriptor)
860                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
861                            field->type, field->name, field->offset,
862                            field->size, !!field->is_signed);
863         else
864                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
865                            (int)(array_descriptor - field->type),
866                            field->type, field->name,
867                            array_descriptor, field->offset,
868                            field->size, !!field->is_signed);
869
870         return 0;
871 }
872
873 static void f_stop(struct seq_file *m, void *p)
874 {
875 }
876
877 static const struct seq_operations trace_format_seq_ops = {
878         .start          = f_start,
879         .next           = f_next,
880         .stop           = f_stop,
881         .show           = f_show,
882 };
883
884 static int trace_format_open(struct inode *inode, struct file *file)
885 {
886         struct ftrace_event_call *call = inode->i_private;
887         struct seq_file *m;
888         int ret;
889
890         ret = seq_open(file, &trace_format_seq_ops);
891         if (ret < 0)
892                 return ret;
893
894         m = file->private_data;
895         m->private = call;
896
897         return 0;
898 }
899
900 static ssize_t
901 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
902 {
903         struct ftrace_event_call *call = filp->private_data;
904         struct trace_seq *s;
905         int r;
906
907         if (*ppos)
908                 return 0;
909
910         s = kmalloc(sizeof(*s), GFP_KERNEL);
911         if (!s)
912                 return -ENOMEM;
913
914         trace_seq_init(s);
915         trace_seq_printf(s, "%d\n", call->event.type);
916
917         r = simple_read_from_buffer(ubuf, cnt, ppos,
918                                     s->buffer, s->len);
919         kfree(s);
920         return r;
921 }
922
923 static ssize_t
924 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
925                   loff_t *ppos)
926 {
927         struct ftrace_event_call *call = filp->private_data;
928         struct trace_seq *s;
929         int r;
930
931         if (*ppos)
932                 return 0;
933
934         s = kmalloc(sizeof(*s), GFP_KERNEL);
935         if (!s)
936                 return -ENOMEM;
937
938         trace_seq_init(s);
939
940         print_event_filter(call, s);
941         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
942
943         kfree(s);
944
945         return r;
946 }
947
948 static ssize_t
949 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
950                    loff_t *ppos)
951 {
952         struct ftrace_event_call *call = filp->private_data;
953         char *buf;
954         int err;
955
956         if (cnt >= PAGE_SIZE)
957                 return -EINVAL;
958
959         buf = (char *)__get_free_page(GFP_TEMPORARY);
960         if (!buf)
961                 return -ENOMEM;
962
963         if (copy_from_user(buf, ubuf, cnt)) {
964                 free_page((unsigned long) buf);
965                 return -EFAULT;
966         }
967         buf[cnt] = '\0';
968
969         err = apply_event_filter(call, buf);
970         free_page((unsigned long) buf);
971         if (err < 0)
972                 return err;
973
974         *ppos += cnt;
975
976         return cnt;
977 }
978
979 static LIST_HEAD(event_subsystems);
980
981 static int subsystem_open(struct inode *inode, struct file *filp)
982 {
983         struct event_subsystem *system = NULL;
984         struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
985         struct trace_array *tr;
986         int ret;
987
988         /* Make sure the system still exists */
989         mutex_lock(&event_mutex);
990         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
991                 list_for_each_entry(dir, &tr->systems, list) {
992                         if (dir == inode->i_private) {
993                                 /* Don't open systems with no events */
994                                 if (dir->nr_events) {
995                                         __get_system_dir(dir);
996                                         system = dir->subsystem;
997                                 }
998                                 goto exit_loop;
999                         }
1000                 }
1001         }
1002  exit_loop:
1003         mutex_unlock(&event_mutex);
1004
1005         if (!system)
1006                 return -ENODEV;
1007
1008         /* Some versions of gcc think dir can be uninitialized here */
1009         WARN_ON(!dir);
1010
1011         ret = tracing_open_generic(inode, filp);
1012         if (ret < 0)
1013                 put_system(dir);
1014
1015         return ret;
1016 }
1017
1018 static int system_tr_open(struct inode *inode, struct file *filp)
1019 {
1020         struct ftrace_subsystem_dir *dir;
1021         struct trace_array *tr = inode->i_private;
1022         int ret;
1023
1024         /* Make a temporary dir that has no system but points to tr */
1025         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1026         if (!dir)
1027                 return -ENOMEM;
1028
1029         dir->tr = tr;
1030
1031         ret = tracing_open_generic(inode, filp);
1032         if (ret < 0)
1033                 kfree(dir);
1034
1035         filp->private_data = dir;
1036
1037         return ret;
1038 }
1039
1040 static int subsystem_release(struct inode *inode, struct file *file)
1041 {
1042         struct ftrace_subsystem_dir *dir = file->private_data;
1043
1044         /*
1045          * If dir->subsystem is NULL, then this is a temporary
1046          * descriptor that was made for a trace_array to enable
1047          * all subsystems.
1048          */
1049         if (dir->subsystem)
1050                 put_system(dir);
1051         else
1052                 kfree(dir);
1053
1054         return 0;
1055 }
1056
1057 static ssize_t
1058 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1059                       loff_t *ppos)
1060 {
1061         struct ftrace_subsystem_dir *dir = filp->private_data;
1062         struct event_subsystem *system = dir->subsystem;
1063         struct trace_seq *s;
1064         int r;
1065
1066         if (*ppos)
1067                 return 0;
1068
1069         s = kmalloc(sizeof(*s), GFP_KERNEL);
1070         if (!s)
1071                 return -ENOMEM;
1072
1073         trace_seq_init(s);
1074
1075         print_subsystem_event_filter(system, s);
1076         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1077
1078         kfree(s);
1079
1080         return r;
1081 }
1082
1083 static ssize_t
1084 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1085                        loff_t *ppos)
1086 {
1087         struct ftrace_subsystem_dir *dir = filp->private_data;
1088         char *buf;
1089         int err;
1090
1091         if (cnt >= PAGE_SIZE)
1092                 return -EINVAL;
1093
1094         buf = (char *)__get_free_page(GFP_TEMPORARY);
1095         if (!buf)
1096                 return -ENOMEM;
1097
1098         if (copy_from_user(buf, ubuf, cnt)) {
1099                 free_page((unsigned long) buf);
1100                 return -EFAULT;
1101         }
1102         buf[cnt] = '\0';
1103
1104         err = apply_subsystem_event_filter(dir, buf);
1105         free_page((unsigned long) buf);
1106         if (err < 0)
1107                 return err;
1108
1109         *ppos += cnt;
1110
1111         return cnt;
1112 }
1113
1114 static ssize_t
1115 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1116 {
1117         int (*func)(struct trace_seq *s) = filp->private_data;
1118         struct trace_seq *s;
1119         int r;
1120
1121         if (*ppos)
1122                 return 0;
1123
1124         s = kmalloc(sizeof(*s), GFP_KERNEL);
1125         if (!s)
1126                 return -ENOMEM;
1127
1128         trace_seq_init(s);
1129
1130         func(s);
1131         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1132
1133         kfree(s);
1134
1135         return r;
1136 }
1137
1138 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1139 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1140
1141 static const struct seq_operations show_event_seq_ops = {
1142         .start = t_start,
1143         .next = t_next,
1144         .show = t_show,
1145         .stop = t_stop,
1146 };
1147
1148 static const struct seq_operations show_set_event_seq_ops = {
1149         .start = s_start,
1150         .next = s_next,
1151         .show = t_show,
1152         .stop = t_stop,
1153 };
1154
1155 static const struct file_operations ftrace_avail_fops = {
1156         .open = ftrace_event_avail_open,
1157         .read = seq_read,
1158         .llseek = seq_lseek,
1159         .release = seq_release,
1160 };
1161
1162 static const struct file_operations ftrace_set_event_fops = {
1163         .open = ftrace_event_set_open,
1164         .read = seq_read,
1165         .write = ftrace_event_write,
1166         .llseek = seq_lseek,
1167         .release = seq_release,
1168 };
1169
1170 static const struct file_operations ftrace_enable_fops = {
1171         .open = tracing_open_generic,
1172         .read = event_enable_read,
1173         .write = event_enable_write,
1174         .llseek = default_llseek,
1175 };
1176
1177 static const struct file_operations ftrace_event_format_fops = {
1178         .open = trace_format_open,
1179         .read = seq_read,
1180         .llseek = seq_lseek,
1181         .release = seq_release,
1182 };
1183
1184 static const struct file_operations ftrace_event_id_fops = {
1185         .open = tracing_open_generic,
1186         .read = event_id_read,
1187         .llseek = default_llseek,
1188 };
1189
1190 static const struct file_operations ftrace_event_filter_fops = {
1191         .open = tracing_open_generic,
1192         .read = event_filter_read,
1193         .write = event_filter_write,
1194         .llseek = default_llseek,
1195 };
1196
1197 static const struct file_operations ftrace_subsystem_filter_fops = {
1198         .open = subsystem_open,
1199         .read = subsystem_filter_read,
1200         .write = subsystem_filter_write,
1201         .llseek = default_llseek,
1202         .release = subsystem_release,
1203 };
1204
1205 static const struct file_operations ftrace_system_enable_fops = {
1206         .open = subsystem_open,
1207         .read = system_enable_read,
1208         .write = system_enable_write,
1209         .llseek = default_llseek,
1210         .release = subsystem_release,
1211 };
1212
1213 static const struct file_operations ftrace_tr_enable_fops = {
1214         .open = system_tr_open,
1215         .read = system_enable_read,
1216         .write = system_enable_write,
1217         .llseek = default_llseek,
1218         .release = subsystem_release,
1219 };
1220
1221 static const struct file_operations ftrace_show_header_fops = {
1222         .open = tracing_open_generic,
1223         .read = show_header,
1224         .llseek = default_llseek,
1225 };
1226
1227 static int
1228 ftrace_event_open(struct inode *inode, struct file *file,
1229                   const struct seq_operations *seq_ops)
1230 {
1231         struct seq_file *m;
1232         int ret;
1233
1234         ret = seq_open(file, seq_ops);
1235         if (ret < 0)
1236                 return ret;
1237         m = file->private_data;
1238         /* copy tr over to seq ops */
1239         m->private = inode->i_private;
1240
1241         return ret;
1242 }
1243
1244 static int
1245 ftrace_event_avail_open(struct inode *inode, struct file *file)
1246 {
1247         const struct seq_operations *seq_ops = &show_event_seq_ops;
1248
1249         return ftrace_event_open(inode, file, seq_ops);
1250 }
1251
1252 static int
1253 ftrace_event_set_open(struct inode *inode, struct file *file)
1254 {
1255         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1256         struct trace_array *tr = inode->i_private;
1257
1258         if ((file->f_mode & FMODE_WRITE) &&
1259             (file->f_flags & O_TRUNC))
1260                 ftrace_clear_events(tr);
1261
1262         return ftrace_event_open(inode, file, seq_ops);
1263 }
1264
1265 static struct event_subsystem *
1266 create_new_subsystem(const char *name)
1267 {
1268         struct event_subsystem *system;
1269
1270         /* need to create new entry */
1271         system = kmalloc(sizeof(*system), GFP_KERNEL);
1272         if (!system)
1273                 return NULL;
1274
1275         system->ref_count = 1;
1276         system->name = name;
1277
1278         system->filter = NULL;
1279
1280         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1281         if (!system->filter)
1282                 goto out_free;
1283
1284         list_add(&system->list, &event_subsystems);
1285
1286         return system;
1287
1288  out_free:
1289         kfree(system);
1290         return NULL;
1291 }
1292
1293 static struct dentry *
1294 event_subsystem_dir(struct trace_array *tr, const char *name,
1295                     struct ftrace_event_file *file, struct dentry *parent)
1296 {
1297         struct ftrace_subsystem_dir *dir;
1298         struct event_subsystem *system;
1299         struct dentry *entry;
1300
1301         /* First see if we did not already create this dir */
1302         list_for_each_entry(dir, &tr->systems, list) {
1303                 system = dir->subsystem;
1304                 if (strcmp(system->name, name) == 0) {
1305                         dir->nr_events++;
1306                         file->system = dir;
1307                         return dir->entry;
1308                 }
1309         }
1310
1311         /* Now see if the system itself exists. */
1312         list_for_each_entry(system, &event_subsystems, list) {
1313                 if (strcmp(system->name, name) == 0)
1314                         break;
1315         }
1316         /* Reset system variable when not found */
1317         if (&system->list == &event_subsystems)
1318                 system = NULL;
1319
1320         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1321         if (!dir)
1322                 goto out_fail;
1323
1324         if (!system) {
1325                 system = create_new_subsystem(name);
1326                 if (!system)
1327                         goto out_free;
1328         } else
1329                 __get_system(system);
1330
1331         dir->entry = debugfs_create_dir(name, parent);
1332         if (!dir->entry) {
1333                 pr_warning("Failed to create system directory %s\n", name);
1334                 __put_system(system);
1335                 goto out_free;
1336         }
1337
1338         dir->tr = tr;
1339         dir->ref_count = 1;
1340         dir->nr_events = 1;
1341         dir->subsystem = system;
1342         file->system = dir;
1343
1344         entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1345                                     &ftrace_subsystem_filter_fops);
1346         if (!entry) {
1347                 kfree(system->filter);
1348                 system->filter = NULL;
1349                 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1350         }
1351
1352         trace_create_file("enable", 0644, dir->entry, dir,
1353                           &ftrace_system_enable_fops);
1354
1355         list_add(&dir->list, &tr->systems);
1356
1357         return dir->entry;
1358
1359  out_free:
1360         kfree(dir);
1361  out_fail:
1362         /* Only print this message if failed on memory allocation */
1363         if (!dir || !system)
1364                 pr_warning("No memory to create event subsystem %s\n",
1365                            name);
1366         return NULL;
1367 }
1368
1369 static int
1370 event_create_dir(struct dentry *parent,
1371                  struct ftrace_event_file *file,
1372                  const struct file_operations *id,
1373                  const struct file_operations *enable,
1374                  const struct file_operations *filter,
1375                  const struct file_operations *format)
1376 {
1377         struct ftrace_event_call *call = file->event_call;
1378         struct trace_array *tr = file->tr;
1379         struct list_head *head;
1380         struct dentry *d_events;
1381         int ret;
1382
1383         /*
1384          * If the trace point header did not define TRACE_SYSTEM
1385          * then the system would be called "TRACE_SYSTEM".
1386          */
1387         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1388                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1389                 if (!d_events)
1390                         return -ENOMEM;
1391         } else
1392                 d_events = parent;
1393
1394         file->dir = debugfs_create_dir(call->name, d_events);
1395         if (!file->dir) {
1396                 pr_warning("Could not create debugfs '%s' directory\n",
1397                            call->name);
1398                 return -1;
1399         }
1400
1401         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1402                 trace_create_file("enable", 0644, file->dir, file,
1403                                   enable);
1404
1405 #ifdef CONFIG_PERF_EVENTS
1406         if (call->event.type && call->class->reg)
1407                 trace_create_file("id", 0444, file->dir, call,
1408                                   id);
1409 #endif
1410
1411         /*
1412          * Other events may have the same class. Only update
1413          * the fields if they are not already defined.
1414          */
1415         head = trace_get_fields(call);
1416         if (list_empty(head)) {
1417                 ret = call->class->define_fields(call);
1418                 if (ret < 0) {
1419                         pr_warning("Could not initialize trace point"
1420                                    " events/%s\n", call->name);
1421                         return -1;
1422                 }
1423         }
1424         trace_create_file("filter", 0644, file->dir, call,
1425                           filter);
1426
1427         trace_create_file("format", 0444, file->dir, call,
1428                           format);
1429
1430         return 0;
1431 }
1432
1433 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
1434 {
1435         if (!dir)
1436                 return;
1437
1438         if (!--dir->nr_events) {
1439                 debugfs_remove_recursive(dir->entry);
1440                 list_del(&dir->list);
1441                 __put_system_dir(dir);
1442         }
1443 }
1444
1445 static void remove_event_from_tracers(struct ftrace_event_call *call)
1446 {
1447         struct ftrace_event_file *file;
1448         struct trace_array *tr;
1449
1450         do_for_each_event_file_safe(tr, file) {
1451
1452                 if (file->event_call != call)
1453                         continue;
1454
1455                 list_del(&file->list);
1456                 debugfs_remove_recursive(file->dir);
1457                 remove_subsystem(file->system);
1458                 kmem_cache_free(file_cachep, file);
1459
1460                 /*
1461                  * The do_for_each_event_file_safe() is
1462                  * a double loop. After finding the call for this
1463                  * trace_array, we use break to jump to the next
1464                  * trace_array.
1465                  */
1466                 break;
1467         } while_for_each_event_file();
1468 }
1469
1470 static void event_remove(struct ftrace_event_call *call)
1471 {
1472         struct trace_array *tr;
1473         struct ftrace_event_file *file;
1474
1475         do_for_each_event_file(tr, file) {
1476                 if (file->event_call != call)
1477                         continue;
1478                 ftrace_event_enable_disable(file, 0);
1479                 /*
1480                  * The do_for_each_event_file() is
1481                  * a double loop. After finding the call for this
1482                  * trace_array, we use break to jump to the next
1483                  * trace_array.
1484                  */
1485                 break;
1486         } while_for_each_event_file();
1487
1488         if (call->event.funcs)
1489                 __unregister_ftrace_event(&call->event);
1490         remove_event_from_tracers(call);
1491         list_del(&call->list);
1492 }
1493
1494 static int event_init(struct ftrace_event_call *call)
1495 {
1496         int ret = 0;
1497
1498         if (WARN_ON(!call->name))
1499                 return -EINVAL;
1500
1501         if (call->class->raw_init) {
1502                 ret = call->class->raw_init(call);
1503                 if (ret < 0 && ret != -ENOSYS)
1504                         pr_warn("Could not initialize trace events/%s\n",
1505                                 call->name);
1506         }
1507
1508         return ret;
1509 }
1510
1511 static int
1512 __register_event(struct ftrace_event_call *call, struct module *mod)
1513 {
1514         int ret;
1515
1516         ret = event_init(call);
1517         if (ret < 0)
1518                 return ret;
1519
1520         list_add(&call->list, &ftrace_events);
1521         call->mod = mod;
1522
1523         return 0;
1524 }
1525
1526 /* Add an event to a trace directory */
1527 static int
1528 __trace_add_new_event(struct ftrace_event_call *call,
1529                       struct trace_array *tr,
1530                       const struct file_operations *id,
1531                       const struct file_operations *enable,
1532                       const struct file_operations *filter,
1533                       const struct file_operations *format)
1534 {
1535         struct ftrace_event_file *file;
1536
1537         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1538         if (!file)
1539                 return -ENOMEM;
1540
1541         file->event_call = call;
1542         file->tr = tr;
1543         list_add(&file->list, &tr->events);
1544
1545         return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1546 }
1547
1548 /*
1549  * Just create a decriptor for early init. A descriptor is required
1550  * for enabling events at boot. We want to enable events before
1551  * the filesystem is initialized.
1552  */
1553 static __init int
1554 __trace_early_add_new_event(struct ftrace_event_call *call,
1555                             struct trace_array *tr)
1556 {
1557         struct ftrace_event_file *file;
1558
1559         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1560         if (!file)
1561                 return -ENOMEM;
1562
1563         file->event_call = call;
1564         file->tr = tr;
1565         list_add(&file->list, &tr->events);
1566
1567         return 0;
1568 }
1569
1570 struct ftrace_module_file_ops;
1571 static void __add_event_to_tracers(struct ftrace_event_call *call,
1572                                    struct ftrace_module_file_ops *file_ops);
1573
1574 /* Add an additional event_call dynamically */
1575 int trace_add_event_call(struct ftrace_event_call *call)
1576 {
1577         int ret;
1578         mutex_lock(&event_mutex);
1579
1580         ret = __register_event(call, NULL);
1581         if (ret >= 0)
1582                 __add_event_to_tracers(call, NULL);
1583
1584         mutex_unlock(&event_mutex);
1585         return ret;
1586 }
1587
1588 /*
1589  * Must be called under locking both of event_mutex and trace_event_sem.
1590  */
1591 static void __trace_remove_event_call(struct ftrace_event_call *call)
1592 {
1593         event_remove(call);
1594         trace_destroy_fields(call);
1595         destroy_preds(call);
1596 }
1597
1598 /* Remove an event_call */
1599 void trace_remove_event_call(struct ftrace_event_call *call)
1600 {
1601         mutex_lock(&event_mutex);
1602         down_write(&trace_event_sem);
1603         __trace_remove_event_call(call);
1604         up_write(&trace_event_sem);
1605         mutex_unlock(&event_mutex);
1606 }
1607
1608 #define for_each_event(event, start, end)                       \
1609         for (event = start;                                     \
1610              (unsigned long)event < (unsigned long)end;         \
1611              event++)
1612
1613 #ifdef CONFIG_MODULES
1614
1615 static LIST_HEAD(ftrace_module_file_list);
1616
1617 /*
1618  * Modules must own their file_operations to keep up with
1619  * reference counting.
1620  */
1621 struct ftrace_module_file_ops {
1622         struct list_head                list;
1623         struct module                   *mod;
1624         struct file_operations          id;
1625         struct file_operations          enable;
1626         struct file_operations          format;
1627         struct file_operations          filter;
1628 };
1629
1630 static struct ftrace_module_file_ops *
1631 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1632 {
1633         /*
1634          * As event_calls are added in groups by module,
1635          * when we find one file_ops, we don't need to search for
1636          * each call in that module, as the rest should be the
1637          * same. Only search for a new one if the last one did
1638          * not match.
1639          */
1640         if (file_ops && mod == file_ops->mod)
1641                 return file_ops;
1642
1643         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1644                 if (file_ops->mod == mod)
1645                         return file_ops;
1646         }
1647         return NULL;
1648 }
1649
1650 static struct ftrace_module_file_ops *
1651 trace_create_file_ops(struct module *mod)
1652 {
1653         struct ftrace_module_file_ops *file_ops;
1654
1655         /*
1656          * This is a bit of a PITA. To allow for correct reference
1657          * counting, modules must "own" their file_operations.
1658          * To do this, we allocate the file operations that will be
1659          * used in the event directory.
1660          */
1661
1662         file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1663         if (!file_ops)
1664                 return NULL;
1665
1666         file_ops->mod = mod;
1667
1668         file_ops->id = ftrace_event_id_fops;
1669         file_ops->id.owner = mod;
1670
1671         file_ops->enable = ftrace_enable_fops;
1672         file_ops->enable.owner = mod;
1673
1674         file_ops->filter = ftrace_event_filter_fops;
1675         file_ops->filter.owner = mod;
1676
1677         file_ops->format = ftrace_event_format_fops;
1678         file_ops->format.owner = mod;
1679
1680         list_add(&file_ops->list, &ftrace_module_file_list);
1681
1682         return file_ops;
1683 }
1684
1685 static void trace_module_add_events(struct module *mod)
1686 {
1687         struct ftrace_module_file_ops *file_ops = NULL;
1688         struct ftrace_event_call **call, **start, **end;
1689
1690         start = mod->trace_events;
1691         end = mod->trace_events + mod->num_trace_events;
1692
1693         if (start == end)
1694                 return;
1695
1696         file_ops = trace_create_file_ops(mod);
1697         if (!file_ops)
1698                 return;
1699
1700         for_each_event(call, start, end) {
1701                 __register_event(*call, mod);
1702                 __add_event_to_tracers(*call, file_ops);
1703         }
1704 }
1705
1706 static void trace_module_remove_events(struct module *mod)
1707 {
1708         struct ftrace_module_file_ops *file_ops;
1709         struct ftrace_event_call *call, *p;
1710         bool clear_trace = false;
1711
1712         down_write(&trace_event_sem);
1713         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1714                 if (call->mod == mod) {
1715                         if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1716                                 clear_trace = true;
1717                         __trace_remove_event_call(call);
1718                 }
1719         }
1720
1721         /* Now free the file_operations */
1722         list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1723                 if (file_ops->mod == mod)
1724                         break;
1725         }
1726         if (&file_ops->list != &ftrace_module_file_list) {
1727                 list_del(&file_ops->list);
1728                 kfree(file_ops);
1729         }
1730         up_write(&trace_event_sem);
1731
1732         /*
1733          * It is safest to reset the ring buffer if the module being unloaded
1734          * registered any events that were used. The only worry is if
1735          * a new module gets loaded, and takes on the same id as the events
1736          * of this module. When printing out the buffer, traced events left
1737          * over from this module may be passed to the new module events and
1738          * unexpected results may occur.
1739          */
1740         if (clear_trace)
1741                 tracing_reset_all_online_cpus();
1742 }
1743
1744 static int trace_module_notify(struct notifier_block *self,
1745                                unsigned long val, void *data)
1746 {
1747         struct module *mod = data;
1748
1749         mutex_lock(&event_mutex);
1750         switch (val) {
1751         case MODULE_STATE_COMING:
1752                 trace_module_add_events(mod);
1753                 break;
1754         case MODULE_STATE_GOING:
1755                 trace_module_remove_events(mod);
1756                 break;
1757         }
1758         mutex_unlock(&event_mutex);
1759
1760         return 0;
1761 }
1762
1763 static int
1764 __trace_add_new_mod_event(struct ftrace_event_call *call,
1765                           struct trace_array *tr,
1766                           struct ftrace_module_file_ops *file_ops)
1767 {
1768         return __trace_add_new_event(call, tr,
1769                                      &file_ops->id, &file_ops->enable,
1770                                      &file_ops->filter, &file_ops->format);
1771 }
1772
1773 #else
1774 static inline struct ftrace_module_file_ops *
1775 find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
1776 {
1777         return NULL;
1778 }
1779 static inline int trace_module_notify(struct notifier_block *self,
1780                                       unsigned long val, void *data)
1781 {
1782         return 0;
1783 }
1784 static inline int
1785 __trace_add_new_mod_event(struct ftrace_event_call *call,
1786                           struct trace_array *tr,
1787                           struct ftrace_module_file_ops *file_ops)
1788 {
1789         return -ENODEV;
1790 }
1791 #endif /* CONFIG_MODULES */
1792
1793 /* Create a new event directory structure for a trace directory. */
1794 static void
1795 __trace_add_event_dirs(struct trace_array *tr)
1796 {
1797         struct ftrace_module_file_ops *file_ops = NULL;
1798         struct ftrace_event_call *call;
1799         int ret;
1800
1801         list_for_each_entry(call, &ftrace_events, list) {
1802                 if (call->mod) {
1803                         /*
1804                          * Directories for events by modules need to
1805                          * keep module ref counts when opened (as we don't
1806                          * want the module to disappear when reading one
1807                          * of these files). The file_ops keep account of
1808                          * the module ref count.
1809                          */
1810                         file_ops = find_ftrace_file_ops(file_ops, call->mod);
1811                         if (!file_ops)
1812                                 continue; /* Warn? */
1813                         ret = __trace_add_new_mod_event(call, tr, file_ops);
1814                         if (ret < 0)
1815                                 pr_warning("Could not create directory for event %s\n",
1816                                            call->name);
1817                         continue;
1818                 }
1819                 ret = __trace_add_new_event(call, tr,
1820                                             &ftrace_event_id_fops,
1821                                             &ftrace_enable_fops,
1822                                             &ftrace_event_filter_fops,
1823                                             &ftrace_event_format_fops);
1824                 if (ret < 0)
1825                         pr_warning("Could not create directory for event %s\n",
1826                                    call->name);
1827         }
1828 }
1829
1830 #ifdef CONFIG_DYNAMIC_FTRACE
1831
1832 /* Avoid typos */
1833 #define ENABLE_EVENT_STR        "enable_event"
1834 #define DISABLE_EVENT_STR       "disable_event"
1835
1836 struct event_probe_data {
1837         struct ftrace_event_file        *file;
1838         unsigned long                   count;
1839         int                             ref;
1840         bool                            enable;
1841 };
1842
1843 static struct ftrace_event_file *
1844 find_event_file(struct trace_array *tr, const char *system,  const char *event)
1845 {
1846         struct ftrace_event_file *file;
1847         struct ftrace_event_call *call;
1848
1849         list_for_each_entry(file, &tr->events, list) {
1850
1851                 call = file->event_call;
1852
1853                 if (!call->name || !call->class || !call->class->reg)
1854                         continue;
1855
1856                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1857                         continue;
1858
1859                 if (strcmp(event, call->name) == 0 &&
1860                     strcmp(system, call->class->system) == 0)
1861                         return file;
1862         }
1863         return NULL;
1864 }
1865
1866 static void
1867 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1868 {
1869         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1870         struct event_probe_data *data = *pdata;
1871
1872         if (!data)
1873                 return;
1874
1875         if (data->enable)
1876                 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1877         else
1878                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1879 }
1880
1881 static void
1882 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1883 {
1884         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1885         struct event_probe_data *data = *pdata;
1886
1887         if (!data)
1888                 return;
1889
1890         if (!data->count)
1891                 return;
1892
1893         /* Skip if the event is in a state we want to switch to */
1894         if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1895                 return;
1896
1897         if (data->count != -1)
1898                 (data->count)--;
1899
1900         event_enable_probe(ip, parent_ip, _data);
1901 }
1902
1903 static int
1904 event_enable_print(struct seq_file *m, unsigned long ip,
1905                       struct ftrace_probe_ops *ops, void *_data)
1906 {
1907         struct event_probe_data *data = _data;
1908
1909         seq_printf(m, "%ps:", (void *)ip);
1910
1911         seq_printf(m, "%s:%s:%s",
1912                    data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1913                    data->file->event_call->class->system,
1914                    data->file->event_call->name);
1915
1916         if (data->count == -1)
1917                 seq_printf(m, ":unlimited\n");
1918         else
1919                 seq_printf(m, ":count=%ld\n", data->count);
1920
1921         return 0;
1922 }
1923
1924 static int
1925 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1926                   void **_data)
1927 {
1928         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1929         struct event_probe_data *data = *pdata;
1930
1931         data->ref++;
1932         return 0;
1933 }
1934
1935 static void
1936 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1937                   void **_data)
1938 {
1939         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1940         struct event_probe_data *data = *pdata;
1941
1942         if (WARN_ON_ONCE(data->ref <= 0))
1943                 return;
1944
1945         data->ref--;
1946         if (!data->ref) {
1947                 /* Remove the SOFT_MODE flag */
1948                 __ftrace_event_enable_disable(data->file, 0, 1);
1949                 module_put(data->file->event_call->mod);
1950                 kfree(data);
1951         }
1952         *pdata = NULL;
1953 }
1954
1955 static struct ftrace_probe_ops event_enable_probe_ops = {
1956         .func                   = event_enable_probe,
1957         .print                  = event_enable_print,
1958         .init                   = event_enable_init,
1959         .free                   = event_enable_free,
1960 };
1961
1962 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1963         .func                   = event_enable_count_probe,
1964         .print                  = event_enable_print,
1965         .init                   = event_enable_init,
1966         .free                   = event_enable_free,
1967 };
1968
1969 static struct ftrace_probe_ops event_disable_probe_ops = {
1970         .func                   = event_enable_probe,
1971         .print                  = event_enable_print,
1972         .init                   = event_enable_init,
1973         .free                   = event_enable_free,
1974 };
1975
1976 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1977         .func                   = event_enable_count_probe,
1978         .print                  = event_enable_print,
1979         .init                   = event_enable_init,
1980         .free                   = event_enable_free,
1981 };
1982
1983 static int
1984 event_enable_func(struct ftrace_hash *hash,
1985                   char *glob, char *cmd, char *param, int enabled)
1986 {
1987         struct trace_array *tr = top_trace_array();
1988         struct ftrace_event_file *file;
1989         struct ftrace_probe_ops *ops;
1990         struct event_probe_data *data;
1991         const char *system;
1992         const char *event;
1993         char *number;
1994         bool enable;
1995         int ret;
1996
1997         /* hash funcs only work with set_ftrace_filter */
1998         if (!enabled)
1999                 return -EINVAL;
2000
2001         if (!param)
2002                 return -EINVAL;
2003
2004         system = strsep(&param, ":");
2005         if (!param)
2006                 return -EINVAL;
2007
2008         event = strsep(&param, ":");
2009
2010         mutex_lock(&event_mutex);
2011
2012         ret = -EINVAL;
2013         file = find_event_file(tr, system, event);
2014         if (!file)
2015                 goto out;
2016
2017         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2018
2019         if (enable)
2020                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2021         else
2022                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2023
2024         if (glob[0] == '!') {
2025                 unregister_ftrace_function_probe_func(glob+1, ops);
2026                 ret = 0;
2027                 goto out;
2028         }
2029
2030         ret = -ENOMEM;
2031         data = kzalloc(sizeof(*data), GFP_KERNEL);
2032         if (!data)
2033                 goto out;
2034
2035         data->enable = enable;
2036         data->count = -1;
2037         data->file = file;
2038
2039         if (!param)
2040                 goto out_reg;
2041
2042         number = strsep(&param, ":");
2043
2044         ret = -EINVAL;
2045         if (!strlen(number))
2046                 goto out_free;
2047
2048         /*
2049          * We use the callback data field (which is a pointer)
2050          * as our counter.
2051          */
2052         ret = kstrtoul(number, 0, &data->count);
2053         if (ret)
2054                 goto out_free;
2055
2056  out_reg:
2057         /* Don't let event modules unload while probe registered */
2058         ret = try_module_get(file->event_call->mod);
2059         if (!ret)
2060                 goto out_free;
2061
2062         ret = __ftrace_event_enable_disable(file, 1, 1);
2063         if (ret < 0)
2064                 goto out_put;
2065         ret = register_ftrace_function_probe(glob, ops, data);
2066         /*
2067          * The above returns on success the # of functions enabled,
2068          * but if it didn't find any functions it returns zero.
2069          * Consider no functions a failure too.
2070          */
2071         if (!ret) {
2072                 ret = -ENOENT;
2073                 goto out_disable;
2074         } else if (ret < 0)
2075                 goto out_disable;
2076         /* Just return zero, not the number of enabled functions */
2077         ret = 0;
2078  out:
2079         mutex_unlock(&event_mutex);
2080         return ret;
2081
2082  out_disable:
2083         __ftrace_event_enable_disable(file, 0, 1);
2084  out_put:
2085         module_put(file->event_call->mod);
2086  out_free:
2087         kfree(data);
2088         goto out;
2089 }
2090
2091 static struct ftrace_func_command event_enable_cmd = {
2092         .name                   = ENABLE_EVENT_STR,
2093         .func                   = event_enable_func,
2094 };
2095
2096 static struct ftrace_func_command event_disable_cmd = {
2097         .name                   = DISABLE_EVENT_STR,
2098         .func                   = event_enable_func,
2099 };
2100
2101 static __init int register_event_cmds(void)
2102 {
2103         int ret;
2104
2105         ret = register_ftrace_command(&event_enable_cmd);
2106         if (WARN_ON(ret < 0))
2107                 return ret;
2108         ret = register_ftrace_command(&event_disable_cmd);
2109         if (WARN_ON(ret < 0))
2110                 unregister_ftrace_command(&event_enable_cmd);
2111         return ret;
2112 }
2113 #else
2114 static inline int register_event_cmds(void) { return 0; }
2115 #endif /* CONFIG_DYNAMIC_FTRACE */
2116
2117 /*
2118  * The top level array has already had its ftrace_event_file
2119  * descriptors created in order to allow for early events to
2120  * be recorded. This function is called after the debugfs has been
2121  * initialized, and we now have to create the files associated
2122  * to the events.
2123  */
2124 static __init void
2125 __trace_early_add_event_dirs(struct trace_array *tr)
2126 {
2127         struct ftrace_event_file *file;
2128         int ret;
2129
2130
2131         list_for_each_entry(file, &tr->events, list) {
2132                 ret = event_create_dir(tr->event_dir, file,
2133                                        &ftrace_event_id_fops,
2134                                        &ftrace_enable_fops,
2135                                        &ftrace_event_filter_fops,
2136                                        &ftrace_event_format_fops);
2137                 if (ret < 0)
2138                         pr_warning("Could not create directory for event %s\n",
2139                                    file->event_call->name);
2140         }
2141 }
2142
2143 /*
2144  * For early boot up, the top trace array requires to have
2145  * a list of events that can be enabled. This must be done before
2146  * the filesystem is set up in order to allow events to be traced
2147  * early.
2148  */
2149 static __init void
2150 __trace_early_add_events(struct trace_array *tr)
2151 {
2152         struct ftrace_event_call *call;
2153         int ret;
2154
2155         list_for_each_entry(call, &ftrace_events, list) {
2156                 /* Early boot up should not have any modules loaded */
2157                 if (WARN_ON_ONCE(call->mod))
2158                         continue;
2159
2160                 ret = __trace_early_add_new_event(call, tr);
2161                 if (ret < 0)
2162                         pr_warning("Could not create early event %s\n",
2163                                    call->name);
2164         }
2165 }
2166
2167 /* Remove the event directory structure for a trace directory. */
2168 static void
2169 __trace_remove_event_dirs(struct trace_array *tr)
2170 {
2171         struct ftrace_event_file *file, *next;
2172
2173         list_for_each_entry_safe(file, next, &tr->events, list) {
2174                 list_del(&file->list);
2175                 debugfs_remove_recursive(file->dir);
2176                 remove_subsystem(file->system);
2177                 kmem_cache_free(file_cachep, file);
2178         }
2179 }
2180
2181 static void
2182 __add_event_to_tracers(struct ftrace_event_call *call,
2183                        struct ftrace_module_file_ops *file_ops)
2184 {
2185         struct trace_array *tr;
2186
2187         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2188                 if (file_ops)
2189                         __trace_add_new_mod_event(call, tr, file_ops);
2190                 else
2191                         __trace_add_new_event(call, tr,
2192                                               &ftrace_event_id_fops,
2193                                               &ftrace_enable_fops,
2194                                               &ftrace_event_filter_fops,
2195                                               &ftrace_event_format_fops);
2196         }
2197 }
2198
2199 static struct notifier_block trace_module_nb = {
2200         .notifier_call = trace_module_notify,
2201         .priority = 0,
2202 };
2203
2204 extern struct ftrace_event_call *__start_ftrace_events[];
2205 extern struct ftrace_event_call *__stop_ftrace_events[];
2206
2207 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2208
2209 static __init int setup_trace_event(char *str)
2210 {
2211         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2212         ring_buffer_expanded = true;
2213         tracing_selftest_disabled = true;
2214
2215         return 1;
2216 }
2217 __setup("trace_event=", setup_trace_event);
2218
2219 /* Expects to have event_mutex held when called */
2220 static int
2221 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2222 {
2223         struct dentry *d_events;
2224         struct dentry *entry;
2225
2226         entry = debugfs_create_file("set_event", 0644, parent,
2227                                     tr, &ftrace_set_event_fops);
2228         if (!entry) {
2229                 pr_warning("Could not create debugfs 'set_event' entry\n");
2230                 return -ENOMEM;
2231         }
2232
2233         d_events = debugfs_create_dir("events", parent);
2234         if (!d_events) {
2235                 pr_warning("Could not create debugfs 'events' directory\n");
2236                 return -ENOMEM;
2237         }
2238
2239         /* ring buffer internal formats */
2240         trace_create_file("header_page", 0444, d_events,
2241                           ring_buffer_print_page_header,
2242                           &ftrace_show_header_fops);
2243
2244         trace_create_file("header_event", 0444, d_events,
2245                           ring_buffer_print_entry_header,
2246                           &ftrace_show_header_fops);
2247
2248         trace_create_file("enable", 0644, d_events,
2249                           tr, &ftrace_tr_enable_fops);
2250
2251         tr->event_dir = d_events;
2252
2253         return 0;
2254 }
2255
2256 /**
2257  * event_trace_add_tracer - add a instance of a trace_array to events
2258  * @parent: The parent dentry to place the files/directories for events in
2259  * @tr: The trace array associated with these events
2260  *
2261  * When a new instance is created, it needs to set up its events
2262  * directory, as well as other files associated with events. It also
2263  * creates the event hierachry in the @parent/events directory.
2264  *
2265  * Returns 0 on success.
2266  */
2267 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2268 {
2269         int ret;
2270
2271         mutex_lock(&event_mutex);
2272
2273         ret = create_event_toplevel_files(parent, tr);
2274         if (ret)
2275                 goto out_unlock;
2276
2277         down_write(&trace_event_sem);
2278         __trace_add_event_dirs(tr);
2279         up_write(&trace_event_sem);
2280
2281  out_unlock:
2282         mutex_unlock(&event_mutex);
2283
2284         return ret;
2285 }
2286
2287 /*
2288  * The top trace array already had its file descriptors created.
2289  * Now the files themselves need to be created.
2290  */
2291 static __init int
2292 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2293 {
2294         int ret;
2295
2296         mutex_lock(&event_mutex);
2297
2298         ret = create_event_toplevel_files(parent, tr);
2299         if (ret)
2300                 goto out_unlock;
2301
2302         down_write(&trace_event_sem);
2303         __trace_early_add_event_dirs(tr);
2304         up_write(&trace_event_sem);
2305
2306  out_unlock:
2307         mutex_unlock(&event_mutex);
2308
2309         return ret;
2310 }
2311
2312 int event_trace_del_tracer(struct trace_array *tr)
2313 {
2314         /* Disable any running events */
2315         __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2316
2317         mutex_lock(&event_mutex);
2318
2319         down_write(&trace_event_sem);
2320         __trace_remove_event_dirs(tr);
2321         debugfs_remove_recursive(tr->event_dir);
2322         up_write(&trace_event_sem);
2323
2324         tr->event_dir = NULL;
2325
2326         mutex_unlock(&event_mutex);
2327
2328         return 0;
2329 }
2330
2331 static __init int event_trace_memsetup(void)
2332 {
2333         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2334         file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2335         return 0;
2336 }
2337
2338 static __init int event_trace_enable(void)
2339 {
2340         struct trace_array *tr = top_trace_array();
2341         struct ftrace_event_call **iter, *call;
2342         char *buf = bootup_event_buf;
2343         char *token;
2344         int ret;
2345
2346         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2347
2348                 call = *iter;
2349                 ret = event_init(call);
2350                 if (!ret)
2351                         list_add(&call->list, &ftrace_events);
2352         }
2353
2354         /*
2355          * We need the top trace array to have a working set of trace
2356          * points at early init, before the debug files and directories
2357          * are created. Create the file entries now, and attach them
2358          * to the actual file dentries later.
2359          */
2360         __trace_early_add_events(tr);
2361
2362         while (true) {
2363                 token = strsep(&buf, ",");
2364
2365                 if (!token)
2366                         break;
2367                 if (!*token)
2368                         continue;
2369
2370                 ret = ftrace_set_clr_event(tr, token, 1);
2371                 if (ret)
2372                         pr_warn("Failed to enable trace event: %s\n", token);
2373         }
2374
2375         trace_printk_start_comm();
2376
2377         register_event_cmds();
2378
2379         return 0;
2380 }
2381
2382 static __init int event_trace_init(void)
2383 {
2384         struct trace_array *tr;
2385         struct dentry *d_tracer;
2386         struct dentry *entry;
2387         int ret;
2388
2389         tr = top_trace_array();
2390
2391         d_tracer = tracing_init_dentry();
2392         if (!d_tracer)
2393                 return 0;
2394
2395         entry = debugfs_create_file("available_events", 0444, d_tracer,
2396                                     tr, &ftrace_avail_fops);
2397         if (!entry)
2398                 pr_warning("Could not create debugfs "
2399                            "'available_events' entry\n");
2400
2401         if (trace_define_common_fields())
2402                 pr_warning("tracing: Failed to allocate common fields");
2403
2404         ret = early_event_add_tracer(d_tracer, tr);
2405         if (ret)
2406                 return ret;
2407
2408         ret = register_module_notifier(&trace_module_nb);
2409         if (ret)
2410                 pr_warning("Failed to register trace events module notifier\n");
2411
2412         return 0;
2413 }
2414 early_initcall(event_trace_memsetup);
2415 core_initcall(event_trace_enable);
2416 fs_initcall(event_trace_init);
2417
2418 #ifdef CONFIG_FTRACE_STARTUP_TEST
2419
2420 static DEFINE_SPINLOCK(test_spinlock);
2421 static DEFINE_SPINLOCK(test_spinlock_irq);
2422 static DEFINE_MUTEX(test_mutex);
2423
2424 static __init void test_work(struct work_struct *dummy)
2425 {
2426         spin_lock(&test_spinlock);
2427         spin_lock_irq(&test_spinlock_irq);
2428         udelay(1);
2429         spin_unlock_irq(&test_spinlock_irq);
2430         spin_unlock(&test_spinlock);
2431
2432         mutex_lock(&test_mutex);
2433         msleep(1);
2434         mutex_unlock(&test_mutex);
2435 }
2436
2437 static __init int event_test_thread(void *unused)
2438 {
2439         void *test_malloc;
2440
2441         test_malloc = kmalloc(1234, GFP_KERNEL);
2442         if (!test_malloc)
2443                 pr_info("failed to kmalloc\n");
2444
2445         schedule_on_each_cpu(test_work);
2446
2447         kfree(test_malloc);
2448
2449         set_current_state(TASK_INTERRUPTIBLE);
2450         while (!kthread_should_stop())
2451                 schedule();
2452
2453         return 0;
2454 }
2455
2456 /*
2457  * Do various things that may trigger events.
2458  */
2459 static __init void event_test_stuff(void)
2460 {
2461         struct task_struct *test_thread;
2462
2463         test_thread = kthread_run(event_test_thread, NULL, "test-events");
2464         msleep(1);
2465         kthread_stop(test_thread);
2466 }
2467
2468 /*
2469  * For every trace event defined, we will test each trace point separately,
2470  * and then by groups, and finally all trace points.
2471  */
2472 static __init void event_trace_self_tests(void)
2473 {
2474         struct ftrace_subsystem_dir *dir;
2475         struct ftrace_event_file *file;
2476         struct ftrace_event_call *call;
2477         struct event_subsystem *system;
2478         struct trace_array *tr;
2479         int ret;
2480
2481         tr = top_trace_array();
2482
2483         pr_info("Running tests on trace events:\n");
2484
2485         list_for_each_entry(file, &tr->events, list) {
2486
2487                 call = file->event_call;
2488
2489                 /* Only test those that have a probe */
2490                 if (!call->class || !call->class->probe)
2491                         continue;
2492
2493 /*
2494  * Testing syscall events here is pretty useless, but
2495  * we still do it if configured. But this is time consuming.
2496  * What we really need is a user thread to perform the
2497  * syscalls as we test.
2498  */
2499 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2500                 if (call->class->system &&
2501                     strcmp(call->class->system, "syscalls") == 0)
2502                         continue;
2503 #endif
2504
2505                 pr_info("Testing event %s: ", call->name);
2506
2507                 /*
2508                  * If an event is already enabled, someone is using
2509                  * it and the self test should not be on.
2510                  */
2511                 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2512                         pr_warning("Enabled event during self test!\n");
2513                         WARN_ON_ONCE(1);
2514                         continue;
2515                 }
2516
2517                 ftrace_event_enable_disable(file, 1);
2518                 event_test_stuff();
2519                 ftrace_event_enable_disable(file, 0);
2520
2521                 pr_cont("OK\n");
2522         }
2523
2524         /* Now test at the sub system level */
2525
2526         pr_info("Running tests on trace event systems:\n");
2527
2528         list_for_each_entry(dir, &tr->systems, list) {
2529
2530                 system = dir->subsystem;
2531
2532                 /* the ftrace system is special, skip it */
2533                 if (strcmp(system->name, "ftrace") == 0)
2534                         continue;
2535
2536                 pr_info("Testing event system %s: ", system->name);
2537
2538                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2539                 if (WARN_ON_ONCE(ret)) {
2540                         pr_warning("error enabling system %s\n",
2541                                    system->name);
2542                         continue;
2543                 }
2544
2545                 event_test_stuff();
2546
2547                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2548                 if (WARN_ON_ONCE(ret)) {
2549                         pr_warning("error disabling system %s\n",
2550                                    system->name);
2551                         continue;
2552                 }
2553
2554                 pr_cont("OK\n");
2555         }
2556
2557         /* Test with all events enabled */
2558
2559         pr_info("Running tests on all trace events:\n");
2560         pr_info("Testing all events: ");
2561
2562         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2563         if (WARN_ON_ONCE(ret)) {
2564                 pr_warning("error enabling all events\n");
2565                 return;
2566         }
2567
2568         event_test_stuff();
2569
2570         /* reset sysname */
2571         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2572         if (WARN_ON_ONCE(ret)) {
2573                 pr_warning("error disabling all events\n");
2574                 return;
2575         }
2576
2577         pr_cont("OK\n");
2578 }
2579
2580 #ifdef CONFIG_FUNCTION_TRACER
2581
2582 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2583
2584 static void
2585 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2586                           struct ftrace_ops *op, struct pt_regs *pt_regs)
2587 {
2588         struct ring_buffer_event *event;
2589         struct ring_buffer *buffer;
2590         struct ftrace_entry *entry;
2591         unsigned long flags;
2592         long disabled;
2593         int cpu;
2594         int pc;
2595
2596         pc = preempt_count();
2597         preempt_disable_notrace();
2598         cpu = raw_smp_processor_id();
2599         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2600
2601         if (disabled != 1)
2602                 goto out;
2603
2604         local_save_flags(flags);
2605
2606         event = trace_current_buffer_lock_reserve(&buffer,
2607                                                   TRACE_FN, sizeof(*entry),
2608                                                   flags, pc);
2609         if (!event)
2610                 goto out;
2611         entry   = ring_buffer_event_data(event);
2612         entry->ip                       = ip;
2613         entry->parent_ip                = parent_ip;
2614
2615         trace_buffer_unlock_commit(buffer, event, flags, pc);
2616
2617  out:
2618         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2619         preempt_enable_notrace();
2620 }
2621
2622 static struct ftrace_ops trace_ops __initdata  =
2623 {
2624         .func = function_test_events_call,
2625         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2626 };
2627
2628 static __init void event_trace_self_test_with_function(void)
2629 {
2630         int ret;
2631         ret = register_ftrace_function(&trace_ops);
2632         if (WARN_ON(ret < 0)) {
2633                 pr_info("Failed to enable function tracer for event tests\n");
2634                 return;
2635         }
2636         pr_info("Running tests again, along with the function tracer\n");
2637         event_trace_self_tests();
2638         unregister_ftrace_function(&trace_ops);
2639 }
2640 #else
2641 static __init void event_trace_self_test_with_function(void)
2642 {
2643 }
2644 #endif
2645
2646 static __init int event_trace_self_tests_init(void)
2647 {
2648         if (!tracing_selftest_disabled) {
2649                 event_trace_self_tests();
2650                 event_trace_self_test_with_function();
2651         }
2652
2653         return 0;
2654 }
2655
2656 late_initcall(event_trace_self_tests_init);
2657
2658 #endif