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