trace_events.c 76.9 KB
Newer Older
1
2
3
4
5
/*
 * event tracer
 *
 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
 *
6
7
8
 *  - Added format output of fields of the trace point.
 *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
 *
9
10
 */

11
12
#define pr_fmt(fmt) fmt

13
14
15
#include <linux/workqueue.h>
#include <linux/spinlock.h>
#include <linux/kthread.h>
16
#include <linux/tracefs.h>
17
#include <linux/uaccess.h>
18
#include <linux/bsearch.h>
19
20
#include <linux/module.h>
#include <linux/ctype.h>
21
#include <linux/sort.h>
22
#include <linux/slab.h>
23
#include <linux/delay.h>
24

25
26
#include <trace/events/sched.h>

27
28
#include <asm/setup.h>

29
#include "trace_output.h"
30

31
#undef TRACE_SYSTEM
32
33
#define TRACE_SYSTEM "TRACE_SYSTEM"

34
DEFINE_MUTEX(event_mutex);
35

36
LIST_HEAD(ftrace_events);
37
static LIST_HEAD(ftrace_generic_fields);
38
static LIST_HEAD(ftrace_common_fields);
39

40
41
42
43
44
#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)

static struct kmem_cache *field_cachep;
static struct kmem_cache *file_cachep;

45
46
static inline int system_refcount(struct event_subsystem *system)
{
47
	return system->ref_count;
48
49
50
51
}

static int system_refcount_inc(struct event_subsystem *system)
{
52
	return system->ref_count++;
53
54
55
56
}

static int system_refcount_dec(struct event_subsystem *system)
{
57
	return --system->ref_count;
58
59
}

60
61
62
63
64
65
66
/* Double loops, do not use break, only goto's work */
#define do_for_each_event_file(tr, file)			\
	list_for_each_entry(tr, &ftrace_trace_arrays, list) {	\
		list_for_each_entry(file, &tr->events, list)

#define do_for_each_event_file_safe(tr, file)			\
	list_for_each_entry(tr, &ftrace_trace_arrays, list) {	\
67
		struct trace_event_file *___n;				\
68
69
70
71
72
		list_for_each_entry_safe(file, ___n, &tr->events, list)

#define while_for_each_event_file()		\
	}

73
static struct list_head *
74
trace_get_fields(struct trace_event_call *event_call)
75
76
77
78
79
80
{
	if (!event_call->class->get_fields)
		return &event_call->class->fields;
	return event_call->class->get_fields(event_call);
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
static struct ftrace_event_field *
__find_event_field(struct list_head *head, char *name)
{
	struct ftrace_event_field *field;

	list_for_each_entry(field, head, link) {
		if (!strcmp(field->name, name))
			return field;
	}

	return NULL;
}

struct ftrace_event_field *
95
trace_find_event_field(struct trace_event_call *call, char *name)
96
97
98
99
{
	struct ftrace_event_field *field;
	struct list_head *head;

100
101
102
103
	field = __find_event_field(&ftrace_generic_fields, name);
	if (field)
		return field;

104
105
106
107
108
109
110
111
	field = __find_event_field(&ftrace_common_fields, name);
	if (field)
		return field;

	head = trace_get_fields(call);
	return __find_event_field(head, name);
}

112
113
114
static int __trace_define_field(struct list_head *head, const char *type,
				const char *name, int offset, int size,
				int is_signed, int filter_type)
115
116
117
{
	struct ftrace_event_field *field;

118
	field = kmem_cache_alloc(field_cachep, GFP_TRACE);
119
	if (!field)
120
		return -ENOMEM;
121

122
123
	field->name = name;
	field->type = type;
124

125
126
127
128
129
	if (filter_type == FILTER_OTHER)
		field->filter_type = filter_assign_type(type);
	else
		field->filter_type = filter_type;

130
131
	field->offset = offset;
	field->size = size;
132
	field->is_signed = is_signed;
133

134
	list_add(&field->link, head);
135
136
137

	return 0;
}
138

139
int trace_define_field(struct trace_event_call *call, const char *type,
140
141
142
143
144
145
146
147
148
149
150
151
		       const char *name, int offset, int size, int is_signed,
		       int filter_type)
{
	struct list_head *head;

	if (WARN_ON(!call->class))
		return 0;

	head = trace_get_fields(call);
	return __trace_define_field(head, type, name, offset, size,
				    is_signed, filter_type);
}
152
EXPORT_SYMBOL_GPL(trace_define_field);
153

154
155
156
157
158
159
160
#define __generic_field(type, item, filter_type)			\
	ret = __trace_define_field(&ftrace_generic_fields, #type,	\
				   #item, 0, 0, is_signed_type(type),	\
				   filter_type);			\
	if (ret)							\
		return ret;

161
#define __common_field(type, item)					\
162
163
164
165
166
	ret = __trace_define_field(&ftrace_common_fields, #type,	\
				   "common_" #item,			\
				   offsetof(typeof(ent), item),		\
				   sizeof(ent.item),			\
				   is_signed_type(type), FILTER_OTHER);	\
167
168
169
	if (ret)							\
		return ret;

170
171
172
173
174
175
176
177
178
179
static int trace_define_generic_fields(void)
{
	int ret;

	__generic_field(int, cpu, FILTER_OTHER);
	__generic_field(char *, comm, FILTER_PTR_STRING);

	return ret;
}

180
static int trace_define_common_fields(void)
181
182
183
184
185
186
187
188
189
190
191
192
{
	int ret;
	struct trace_entry ent;

	__common_field(unsigned short, type);
	__common_field(unsigned char, flags);
	__common_field(unsigned char, preempt_count);
	__common_field(int, pid);

	return ret;
}

193
static void trace_destroy_fields(struct trace_event_call *call)
194
195
{
	struct ftrace_event_field *field, *next;
196
	struct list_head *head;
197

198
199
	head = trace_get_fields(call);
	list_for_each_entry_safe(field, next, head, link) {
200
		list_del(&field->link);
201
		kmem_cache_free(field_cachep, field);
202
203
204
	}
}

205
int trace_event_raw_init(struct trace_event_call *call)
206
207
208
{
	int id;

209
	id = register_trace_event(&call->event);
210
211
212
213
214
215
216
	if (!id)
		return -ENODEV;

	return 0;
}
EXPORT_SYMBOL_GPL(trace_event_raw_init);

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
{
	struct trace_array *tr = trace_file->tr;
	struct trace_array_cpu *data;
	struct trace_pid_list *pid_list;

	pid_list = rcu_dereference_sched(tr->filtered_pids);
	if (!pid_list)
		return false;

	data = this_cpu_ptr(tr->trace_buffer.data);

	return data->ignore_pid;
}
EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);

233
234
235
void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
				 struct trace_event_file *trace_file,
				 unsigned long len)
236
{
237
	struct trace_event_call *event_call = trace_file->event_call;
238

239
240
241
242
	if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
	    trace_event_ignore_this_pid(trace_file))
		return NULL;

243
244
	local_save_flags(fbuffer->flags);
	fbuffer->pc = preempt_count();
245
	fbuffer->trace_file = trace_file;
246
247

	fbuffer->event =
248
		trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
249
250
251
252
253
254
255
256
						event_call->event.type, len,
						fbuffer->flags, fbuffer->pc);
	if (!fbuffer->event)
		return NULL;

	fbuffer->entry = ring_buffer_event_data(fbuffer->event);
	return fbuffer->entry;
}
257
EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
258

259
260
static DEFINE_SPINLOCK(tracepoint_iter_lock);

261
static void output_printk(struct trace_event_buffer *fbuffer)
262
{
263
	struct trace_event_call *event_call;
264
265
266
267
268
269
270
	struct trace_event *event;
	unsigned long flags;
	struct trace_iterator *iter = tracepoint_print_iter;

	if (!iter)
		return;

271
	event_call = fbuffer->trace_file->event_call;
272
273
274
275
	if (!event_call || !event_call->event.funcs ||
	    !event_call->event.funcs->trace)
		return;

276
	event = &fbuffer->trace_file->event_call->event;
277
278
279
280
281
282
283
284
285
286
287

	spin_lock_irqsave(&tracepoint_iter_lock, flags);
	trace_seq_init(&iter->seq);
	iter->ent = fbuffer->entry;
	event_call->event.funcs->trace(iter, 0, event);
	trace_seq_putc(&iter->seq, 0);
	printk("%s", iter->seq.buffer);

	spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
}

288
void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
289
{
290
291
292
	if (tracepoint_printk)
		output_printk(fbuffer);

293
	event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
294
295
296
				    fbuffer->event, fbuffer->entry,
				    fbuffer->flags, fbuffer->pc);
}
297
EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
298

299
int trace_event_reg(struct trace_event_call *call,
300
		    enum trace_reg type, void *data)
301
{
302
	struct trace_event_file *file = data;
303

304
	WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
305
306
	switch (type) {
	case TRACE_REG_REGISTER:
307
		return tracepoint_probe_register(call->tp,
308
						 call->class->probe,
309
						 file);
310
	case TRACE_REG_UNREGISTER:
311
		tracepoint_probe_unregister(call->tp,
312
					    call->class->probe,
313
					    file);
314
315
316
317
		return 0;

#ifdef CONFIG_PERF_EVENTS
	case TRACE_REG_PERF_REGISTER:
318
		return tracepoint_probe_register(call->tp,
319
320
321
						 call->class->perf_probe,
						 call);
	case TRACE_REG_PERF_UNREGISTER:
322
		tracepoint_probe_unregister(call->tp,
323
324
325
					    call->class->perf_probe,
					    call);
		return 0;
326
327
	case TRACE_REG_PERF_OPEN:
	case TRACE_REG_PERF_CLOSE:
328
329
	case TRACE_REG_PERF_ADD:
	case TRACE_REG_PERF_DEL:
330
		return 0;
331
332
333
334
#endif
	}
	return 0;
}
335
EXPORT_SYMBOL_GPL(trace_event_reg);
336

337
338
void trace_event_enable_cmd_record(bool enable)
{
339
	struct trace_event_file *file;
340
	struct trace_array *tr;
341
342

	mutex_lock(&event_mutex);
343
344
	do_for_each_event_file(tr, file) {

345
		if (!(file->flags & EVENT_FILE_FL_ENABLED))
346
347
348
349
			continue;

		if (enable) {
			tracing_start_cmdline_record();
350
			set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
351
352
		} else {
			tracing_stop_cmdline_record();
353
			clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
354
		}
355
	} while_for_each_event_file();
356
357
358
	mutex_unlock(&event_mutex);
}

359
static int __ftrace_event_enable_disable(struct trace_event_file *file,
360
					 int enable, int soft_disable)
361
{
362
	struct trace_event_call *call = file->event_call;
363
	struct trace_array *tr = file->tr;
364
	int ret = 0;
365
	int disable;
366

367
368
	switch (enable) {
	case 0:
369
		/*
370
371
		 * When soft_disable is set and enable is cleared, the sm_ref
		 * reference counter is decremented. If it reaches 0, we want
372
373
374
375
376
377
378
379
380
381
382
		 * to clear the SOFT_DISABLED flag but leave the event in the
		 * state that it was. That is, if the event was enabled and
		 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
		 * is set we do not want the event to be enabled before we
		 * clear the bit.
		 *
		 * When soft_disable is not set but the SOFT_MODE flag is,
		 * we do nothing. Do not disable the tracepoint, otherwise
		 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
		 */
		if (soft_disable) {
383
384
			if (atomic_dec_return(&file->sm_ref) > 0)
				break;
385
386
			disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
			clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
387
		} else
388
			disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
389

390
391
392
		if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
			clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
			if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
393
				tracing_stop_cmdline_record();
394
				clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
395
			}
396
			call->class->reg(call, TRACE_REG_UNREGISTER, file);
397
		}
398
		/* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
399
400
		if (file->flags & EVENT_FILE_FL_SOFT_MODE)
			set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
401
		else
402
			clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
403
404
		break;
	case 1:
405
406
407
408
409
410
411
412
413
		/*
		 * When soft_disable is set and enable is set, we want to
		 * register the tracepoint for the event, but leave the event
		 * as is. That means, if the event was already enabled, we do
		 * nothing (but set SOFT_MODE). If the event is disabled, we
		 * set SOFT_DISABLED before enabling the event tracepoint, so
		 * it still seems to be disabled.
		 */
		if (!soft_disable)
414
			clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
415
416
417
		else {
			if (atomic_inc_return(&file->sm_ref) > 1)
				break;
418
			set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
419
		}
420

421
		if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
422
423
424

			/* Keep the event disabled, when going to SOFT_MODE. */
			if (soft_disable)
425
				set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
426

427
			if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
428
				tracing_start_cmdline_record();
429
				set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
430
			}
431
			ret = call->class->reg(call, TRACE_REG_REGISTER, file);
432
433
434
			if (ret) {
				tracing_stop_cmdline_record();
				pr_info("event trace: Could not enable event "
435
					"%s\n", trace_event_name(call));
436
437
				break;
			}
438
			set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
439
440
441

			/* WAS_ENABLED gets set but never cleared. */
			call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
442
443
444
		}
		break;
	}
445
446

	return ret;
447
448
}

449
int trace_event_enable_disable(struct trace_event_file *file,
450
451
452
453
454
			       int enable, int soft_disable)
{
	return __ftrace_event_enable_disable(file, enable, soft_disable);
}

455
static int ftrace_event_enable_disable(struct trace_event_file *file,
456
457
458
459
460
				       int enable)
{
	return __ftrace_event_enable_disable(file, enable, 0);
}

461
static void ftrace_clear_events(struct trace_array *tr)
462
{
463
	struct trace_event_file *file;
464
465

	mutex_lock(&event_mutex);
466
467
	list_for_each_entry(file, &tr->events, list) {
		ftrace_event_enable_disable(file, 0);
468
469
470
471
	}
	mutex_unlock(&event_mutex);
}

472
473
474
475
476
477
478
479
480
481
482
483
static int cmp_pid(const void *key, const void *elt)
{
	const pid_t *search_pid = key;
	const pid_t *pid = elt;

	if (*search_pid == *pid)
		return 0;
	if (*search_pid < *pid)
		return -1;
	return 1;
}

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
static bool
check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
{
	pid_t search_pid;
	pid_t *pid;

	/*
	 * Return false, because if filtered_pids does not exist,
	 * all pids are good to trace.
	 */
	if (!filtered_pids)
		return false;

	search_pid = task->pid;

	pid = bsearch(&search_pid, filtered_pids->pids,
		      filtered_pids->nr_pids, sizeof(pid_t),
		      cmp_pid);
	if (!pid)
		return true;

	return false;
}

static void
509
event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
510
511
512
513
514
515
516
517
518
519
520
521
522
		    struct task_struct *prev, struct task_struct *next)
{
	struct trace_array *tr = data;
	struct trace_pid_list *pid_list;

	pid_list = rcu_dereference_sched(tr->filtered_pids);

	this_cpu_write(tr->trace_buffer.data->ignore_pid,
		       check_ignore_pid(pid_list, prev) &&
		       check_ignore_pid(pid_list, next));
}

static void
523
event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
		    struct task_struct *prev, struct task_struct *next)
{
	struct trace_array *tr = data;
	struct trace_pid_list *pid_list;

	pid_list = rcu_dereference_sched(tr->filtered_pids);

	this_cpu_write(tr->trace_buffer.data->ignore_pid,
		       check_ignore_pid(pid_list, next));
}

static void
event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
{
	struct trace_array *tr = data;
	struct trace_pid_list *pid_list;

	/* Nothing to do if we are already tracing */
	if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
		return;

	pid_list = rcu_dereference_sched(tr->filtered_pids);

	this_cpu_write(tr->trace_buffer.data->ignore_pid,
		       check_ignore_pid(pid_list, task));
}

static void
event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
{
	struct trace_array *tr = data;
	struct trace_pid_list *pid_list;

	/* Nothing to do if we are not tracing */
	if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
		return;

	pid_list = rcu_dereference_sched(tr->filtered_pids);

	/* Set tracing if current is enabled */
	this_cpu_write(tr->trace_buffer.data->ignore_pid,
		       check_ignore_pid(pid_list, current));
}

568
569
570
static void __ftrace_clear_event_pids(struct trace_array *tr)
{
	struct trace_pid_list *pid_list;
571
572
	struct trace_event_file *file;
	int cpu;
573
574
575
576
577
578

	pid_list = rcu_dereference_protected(tr->filtered_pids,
					     lockdep_is_held(&event_mutex));
	if (!pid_list)
		return;

579
580
581
582
583
584
	unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
	unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);

	unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
	unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);

585
586
587
588
589
590
	unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
	unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);

	unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
	unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);

591
592
593
594
595
596
597
	list_for_each_entry(file, &tr->events, list) {
		clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
	}

	for_each_possible_cpu(cpu)
		per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
	rcu_assign_pointer(tr->filtered_pids, NULL);

	/* Wait till all users are no longer using pid filtering */
	synchronize_sched();

	free_pages((unsigned long)pid_list->pids, pid_list->order);
	kfree(pid_list);
}

static void ftrace_clear_event_pids(struct trace_array *tr)
{
	mutex_lock(&event_mutex);
	__ftrace_clear_event_pids(tr);
	mutex_unlock(&event_mutex);
}

614
615
616
617
static void __put_system(struct event_subsystem *system)
{
	struct event_filter *filter = system->filter;

618
619
	WARN_ON_ONCE(system_refcount(system) == 0);
	if (system_refcount_dec(system))
620
621
		return;

622
623
	list_del(&system->list);

624
625
626
627
	if (filter) {
		kfree(filter->filter_string);
		kfree(filter);
	}
628
	kfree_const(system->name);
629
630
631
632
633
	kfree(system);
}

static void __get_system(struct event_subsystem *system)
{
634
635
	WARN_ON_ONCE(system_refcount(system) == 0);
	system_refcount_inc(system);
636
637
}

638
static void __get_system_dir(struct trace_subsystem_dir *dir)
639
640
641
642
643
644
{
	WARN_ON_ONCE(dir->ref_count == 0);
	dir->ref_count++;
	__get_system(dir->subsystem);
}

645
static void __put_system_dir(struct trace_subsystem_dir *dir)
646
647
648
{
	WARN_ON_ONCE(dir->ref_count == 0);
	/* If the subsystem is about to be freed, the dir must be too */
649
	WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
650
651
652
653
654
655

	__put_system(dir->subsystem);
	if (!--dir->ref_count)
		kfree(dir);
}

656
static void put_system(struct trace_subsystem_dir *dir)
657
658
{
	mutex_lock(&event_mutex);
659
	__put_system_dir(dir);
660
661
662
	mutex_unlock(&event_mutex);
}

663
static void remove_subsystem(struct trace_subsystem_dir *dir)
664
665
666
667
668
{
	if (!dir)
		return;

	if (!--dir->nr_events) {
669
		tracefs_remove_recursive(dir->entry);
670
671
672
673
674
		list_del(&dir->list);
		__put_system_dir(dir);
	}
}

675
static void remove_event_file_dir(struct trace_event_file *file)
676
{
677
678
679
680
681
	struct dentry *dir = file->dir;
	struct dentry *child;

	if (dir) {
		spin_lock(&dir->d_lock);	/* probably unneeded */
682
		list_for_each_entry(child, &dir->d_subdirs, d_child) {
683
684
			if (d_really_is_positive(child))	/* probably unneeded */
				d_inode(child)->i_private = NULL;
685
686
687
		}
		spin_unlock(&dir->d_lock);

688
		tracefs_remove_recursive(dir);
689
690
	}

691
692
	list_del(&file->list);
	remove_subsystem(file->system);
693
	free_event_filter(file->filter);
694
695
696
	kmem_cache_free(file_cachep, file);
}

697
698
699
/*
 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
 */
700
701
702
static int
__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
			      const char *sub, const char *event, int set)
703
{
704
	struct trace_event_file *file;
705
	struct trace_event_call *call;
706
	const char *name;
707
	int ret = -EINVAL;
708

709
710
711
	list_for_each_entry(file, &tr->events, list) {

		call = file->event_call;
712
		name = trace_event_name(call);
713

714
		if (!name || !call->class || !call->class->reg)
715
716
			continue;

717
718
719
		if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
			continue;

720
		if (match &&
721
		    strcmp(match, name) != 0 &&
722
		    strcmp(match, call->class->system) != 0)
723
724
			continue;

725
		if (sub && strcmp(sub, call->class->system) != 0)
726
727
			continue;

728
		if (event && strcmp(event, name) != 0)
729
730
			continue;

731
		ftrace_event_enable_disable(file, set);
732
733
734

		ret = 0;
	}
735
736
737
738
739
740
741
742
743
744
745

	return ret;
}

static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
				  const char *sub, const char *event, int set)
{
	int ret;

	mutex_lock(&event_mutex);
	ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
746
747
748
749
750
	mutex_unlock(&event_mutex);

	return ret;
}

751
static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
752
{
753
	char *event = NULL, *sub = NULL, *match;
754
	int ret;
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778

	/*
	 * The buf format can be <subsystem>:<event-name>
	 *  *:<event-name> means any event by that name.
	 *  :<event-name> is the same.
	 *
	 *  <subsystem>:* means all events in that subsystem
	 *  <subsystem>: means the same.
	 *
	 *  <name> (no ':') means all events in a subsystem with
	 *  the name <name> or any event that matches <name>
	 */

	match = strsep(&buf, ":");
	if (buf) {
		sub = match;
		event = buf;
		match = NULL;

		if (!strlen(sub) || strcmp(sub, "*") == 0)
			sub = NULL;
		if (!strlen(event) || strcmp(event, "*") == 0)
			event = NULL;
	}
779

780
781
782
783
784
785
786
	ret = __ftrace_set_clr_event(tr, match, sub, event, set);

	/* Put back the colon to allow this to be called again */
	if (buf)
		*(buf - 1) = ':';

	return ret;
787
788
}

789
790
791
792
793
794
795
796
797
798
799
800
801
802
/**
 * trace_set_clr_event - enable or disable an event
 * @system: system name to match (NULL for any system)
 * @event: event name to match (NULL for all events, within system)
 * @set: 1 to enable, 0 to disable
 *
 * This is a way for other parts of the kernel to enable or disable
 * event recording.
 *
 * Returns 0 on success, -EINVAL if the parameters do not match any
 * registered events.
 */
int trace_set_clr_event(const char *system, const char *event, int set)
{
803
804
	struct trace_array *tr = top_trace_array();

805
806
807
	if (!tr)
		return -ENODEV;

808
	return __ftrace_set_clr_event(tr, NULL, system, event, set);
809
}
810
EXPORT_SYMBOL_GPL(trace_set_clr_event);
811

812
813
814
815
816
817
818
/* 128 should be much more than enough */
#define EVENT_BUF_SIZE		127

static ssize_t
ftrace_event_write(struct file *file, const char __user *ubuf,
		   size_t cnt, loff_t *ppos)
{
819
	struct trace_parser parser;
820
821
	struct seq_file *m = file->private_data;
	struct trace_array *tr = m->private;
822
	ssize_t read, ret;
823

824
	if (!cnt)
825
826
		return 0;

827
828
829
830
	ret = tracing_update_buffers();
	if (ret < 0)
		return ret;

831
	if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
832
833
		return -ENOMEM;

834
835
	read = trace_get_user(&parser, ubuf, cnt, ppos);

836
	if (read >= 0 && trace_parser_loaded((&parser))) {
837
		int set = 1;
838

839
		if (*parser.buffer == '!')
840
841
			set = 0;

842
843
		parser.buffer[parser.idx] = 0;

844
		ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
845
		if (ret)
846
			goto out_put;
847
848
849
850
	}

	ret = read;

851
852
 out_put:
	trace_parser_put(&parser);
853
854
855
856
857
858
859

	return ret;
}

static void *
t_next(struct seq_file *m, void *v, loff_t *pos)
{
860
	struct trace_event_file *file = v;
861
	struct trace_event_call *call;
862
	struct trace_array *tr = m->private;
863
864
865

	(*pos)++;

866
867
	list_for_each_entry_continue(file, &tr->events, list) {
		call = file->event_call;
868
869
870
871
		/*
		 * The ftrace subsystem is for showing formats only.
		 * They can not be enabled or disabled via the event files.
		 */
872
		if (call->class && call->class->reg)
873
			return file;
874
	}
875

876
	return NULL;
877
878
879
880
}

static void *t_start(struct seq_file *m, loff_t *pos)
{
881
	struct trace_event_file *file;
882
	struct trace_array *tr = m->private;
883
884
	loff_t l;

885
	mutex_lock(&event_mutex);
886

887
	file = list_entry(&tr->events, struct trace_event_file, list);
888
	for (l = 0; l <= *pos; ) {
889
890
		file = t_next(m, file, &l);
		if (!file)
891
892
			break;
	}
893
	return file;
894
895
896
897
898
}

static void *
s_next(struct seq_file *m, void *v, loff_t *pos)
{
899
	struct trace_event_file *file = v;
900
	struct trace_array *tr = m->private;
901
902
903

	(*pos)++;

904
	list_for_each_entry_continue(file, &tr->events, list) {
905
		if (file->flags & EVENT_FILE_FL_ENABLED)
906
			return file;
907
908
	}

909
	return NULL;
910
911
912
913
}

static void *s_start(struct seq_file *m, loff_t *pos)
{
914
	struct trace_event_file *file;
915
	struct trace_array *tr = m->private;
916
917
	loff_t l;

918
	mutex_lock(&event_mutex);
919

920
	file = list_entry(&tr->events, struct trace_event_file, list);
921
	for (l = 0; l <= *pos; ) {
922
923
		file = s_next(m, file, &l);
		if (!file)
924
925
			break;
	}
926
	return file;
927
928
929
930
}

static int t_show(struct seq_file *m, void *v)
{
931
	struct trace_event_file *file = v;
932
	struct trace_event_call *call = file->event_call;
933

934
935
	if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
		seq_printf(m, "%s:", call->class->system);
936
	seq_printf(m, "%s\n", trace_event_name(call));
937
938
939
940
941
942

	return 0;
}

static void t_stop(struct seq_file *m, void *p)
{
943
	mutex_unlock(&event_mutex);
944
945
}

946
static void *p_start(struct seq_file *m, loff_t *pos)
947
	__acquires(RCU)
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
{
	struct trace_pid_list *pid_list;
	struct trace_array *tr = m->private;

	/*
	 * Grab the mutex, to keep calls to p_next() having the same
	 * tr->filtered_pids as p_start() has.
	 * If we just passed the tr->filtered_pids around, then RCU would
	 * have been enough, but doing that makes things more complex.
	 */
	mutex_lock(&event_mutex);
	rcu_read_lock_sched();

	pid_list = rcu_dereference_sched(tr->filtered_pids);

	if (!pid_list || *pos >= pid_list->nr_pids)
		return NULL;

	return (void *)&pid_list->pids[*pos];
}

static void p_stop(struct seq_file *m, void *p)
970
	__releases(RCU)
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
{
	rcu_read_unlock_sched();
	mutex_unlock(&event_mutex);
}

static void *
p_next(struct seq_file *m, void *v, loff_t *pos)
{
	struct trace_array *tr = m->private;
	struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);

	(*pos)++;

	if (*pos >= pid_list->nr_pids)
		return NULL;

	return (void *)&pid_list->pids[*pos];
}

static int p_show(struct seq_file *m, void *v)
{
	pid_t *pid = v;

	seq_printf(m, "%d\n", *pid);
	return 0;
}

998
999
1000
1001
static ssize_t
event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
		  loff_t *ppos)
{
1002
	struct trace_event_file *file;
1003
	unsigned long flags;
1004
1005
	char buf[4] = "0";

1006
1007
1008
1009
1010
1011
1012
1013
1014
	mutex_lock(&event_mutex);
	file = event_file_data(filp);
	if (likely(file))
		flags = file->flags;
	mutex_unlock(&event_mutex);

	if (!file)
		return -ENODEV;

1015
1016
	if (flags & EVENT_FILE_FL_ENABLED &&
	    !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1017
1018
		strcpy(buf, "1");

1019
1020
	if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
	    flags & EVENT_FILE_FL_SOFT_MODE)
1021
1022
1023
		strcat(buf, "*");

	strcat(buf, "\n");
1024

1025
	return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1026
1027
1028
1029
1030
1031
}

static ssize_t
event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
		   loff_t *ppos)
{
1032
	struct trace_event_file *file;
1033
1034
1035
	unsigned long val;
	int ret;

1036
1037
	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
	if (ret)
1038
1039
		return ret;

1040
1041
1042
1043
	ret = tracing_update_buffers();
	if (ret < 0)
		return ret;

1044
1045
1046
	switch (val) {
	case 0:
	case 1:
1047
		ret = -ENODEV;
1048
		mutex_lock(&event_mutex);
1049
1050
1051
		file = event_file_data(filp);
		if (likely(file))
			ret = ftrace_event_enable_disable(file, val);
1052
		mutex_unlock(&event_mutex);
1053
1054
1055
1056
1057
1058
1059
1060
		break;

	default:
		return -EINVAL;
	}

	*ppos += cnt;

1061
	return ret ? ret : cnt;
1062
1063
}

1064
1065
1066
1067
static ssize_t
system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
		   loff_t *ppos)
{
1068
	const char set_to_char[4] = { '?', '0', '1', 'X' };
1069
	struct trace_subsystem_dir *dir = filp->private_data;
1070
	struct event_subsystem *system = dir->subsystem;
1071
	struct trace_event_call *call;
1072
	struct trace_event_file *file;
1073
	struct trace_array *tr = dir->tr;
1074
	char buf[2];
1075
	int set = 0;
1076
1077
1078
	int ret;

	mutex_lock(&event_mutex);
1079
1080
	list_for_each_entry(file, &tr->events, list) {
		call = file->event_call;
1081
		if (!trace_event_name(call) || !call->class || !call->class->reg)
1082
1083
			continue;

1084
		if (system && strcmp(call->class->system, system->name) != 0)
1085
1086
1087
1088
1089
1090
1091
			continue;

		/*
		 * We need to find out if all the events are set
		 * or if all events or cleared, or if we have
		 * a mixture.
		 */
1092
		set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1093

1094
1095
1096
		/*
		 * If we have a mixture, no need to look further.
		 */
1097
		if (set == 3)
1098
1099
1100
1101
			break;
	}
	mutex_unlock(&event_mutex);

1102
	buf[0] = set_to_char[set];
1103
1104
1105
1106
1107
1108
1109