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