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