trace.c 124 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * ring buffer based function tracer
 *
 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
 *
 * Originally taken from the RT patch by:
 *    Arnaldo Carvalho de Melo <acme@redhat.com>
 *
 * Based on code from the latency_tracer, that is:
 *  Copyright (C) 2004-2006 Ingo Molnar
12
 *  Copyright (C) 2004 Nadia Yvette Chambers
13
 */
14
#include <linux/ring_buffer.h>
15
#include <generated/utsrelease.h>
16
17
#include <linux/stacktrace.h>
#include <linux/writeback.h>
18
19
#include <linux/kallsyms.h>
#include <linux/seq_file.h>
20
#include <linux/notifier.h>
21
#include <linux/irqflags.h>
22
#include <linux/irq_work.h>
23
#include <linux/debugfs.h>
24
#include <linux/pagemap.h>
25
26
27
#include <linux/hardirq.h>
#include <linux/linkage.h>
#include <linux/uaccess.h>
28
#include <linux/kprobes.h>
29
30
31
#include <linux/ftrace.h>
#include <linux/module.h>
#include <linux/percpu.h>
32
#include <linux/splice.h>
33
#include <linux/kdebug.h>
34
#include <linux/string.h>
35
#include <linux/rwsem.h>
36
#include <linux/slab.h>
37
38
#include <linux/ctype.h>
#include <linux/init.h>
39
#include <linux/poll.h>
40
#include <linux/nmi.h>
41
#include <linux/fs.h>
42
#include <linux/sched/rt.h>
Ingo Molnar's avatar
Ingo Molnar committed
43

44
#include "trace.h"
45
#include "trace_output.h"
46

47
48
49
50
/*
 * On boot up, the ring buffer is set to the minimum size, so that
 * we do not waste memory on systems that are not using tracing.
 */
51
int ring_buffer_expanded;
52

53
54
/*
 * We need to change this state when a selftest is running.
55
56
 * A selftest will lurk into the ring-buffer to count the
 * entries inserted during the selftest although some concurrent
57
 * insertions into the ring-buffer such as trace_printk could occurred
58
59
 * at the same time, giving false positive or negative results.
 */
60
static bool __read_mostly tracing_selftest_running;
61

62
63
64
/*
 * If a tracer is running, we do not want to run SELFTEST.
 */
65
bool __read_mostly tracing_selftest_disabled;
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* For tracers that don't implement custom flags */
static struct tracer_opt dummy_tracer_opt[] = {
	{ }
};

static struct tracer_flags dummy_tracer_flags = {
	.val = 0,
	.opts = dummy_tracer_opt
};

static int dummy_set_flag(u32 old_flags, u32 bit, int set)
{
	return 0;
}
81

82
83
84
85
86
87
88
/*
 * To prevent the comm cache from being overwritten when no
 * tracing is active, only save the comm when a trace event
 * occurred.
 */
static DEFINE_PER_CPU(bool, trace_cmdline_save);

89
90
91
92
93
94
95
96
/*
 * When a reader is waiting for data, then this variable is
 * set to true.
 */
static bool trace_wakeup_needed;

static struct irq_work trace_work_wakeup;

97
98
99
100
101
102
/*
 * Kill all tracing for good (never come back).
 * It is initialized to 1 but will turn to zero if the initialization
 * of the tracer is successful. But that is the only place that sets
 * this back to zero.
 */
103
static int tracing_disabled = 1;
104

105
DEFINE_PER_CPU(int, ftrace_cpu_disabled);
106

107
cpumask_var_t __read_mostly	tracing_buffer_mask;
108

109
110
111
112
113
114
115
116
117
118
119
/*
 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
 *
 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
 * is set, then ftrace_dump is called. This will output the contents
 * of the ftrace buffers to the console.  This is very useful for
 * capturing traces that lead to crashes and outputing it to a
 * serial console.
 *
 * It is default off, but you can enable it with either specifying
 * "ftrace_dump_on_oops" in the kernel command line, or setting
120
121
122
 * /proc/sys/kernel/ftrace_dump_on_oops
 * Set 1 if you want to dump buffers of all CPUs
 * Set 2 if you want to dump the buffer of the CPU that triggered oops
123
 */
124
125

enum ftrace_dump_mode ftrace_dump_on_oops;
126

127
128
static int tracing_set_tracer(const char *buf);

Li Zefan's avatar
Li Zefan committed
129
130
#define MAX_TRACER_SIZE		100
static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
131
static char *default_bootup_tracer;
132

133
static int __init set_cmdline_ftrace(char *str)
134
{
Li Zefan's avatar
Li Zefan committed
135
	strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
136
	default_bootup_tracer = bootup_tracer_buf;
137
138
	/* We are using ftrace early, expand it */
	ring_buffer_expanded = 1;
139
140
	return 1;
}
141
__setup("ftrace=", set_cmdline_ftrace);
142

143
144
static int __init set_ftrace_dump_on_oops(char *str)
{
145
146
147
148
149
150
151
152
153
154
155
	if (*str++ != '=' || !*str) {
		ftrace_dump_on_oops = DUMP_ALL;
		return 1;
	}

	if (!strcmp("orig_cpu", str)) {
		ftrace_dump_on_oops = DUMP_ORIG;
                return 1;
        }

        return 0;
156
157
}
__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt's avatar
Steven Rostedt committed
158

159
160
161
162
163
164
165
166
167
168
169
170

static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
static char *trace_boot_options __initdata;

static int __init set_trace_boot_options(char *str)
{
	strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
	trace_boot_options = trace_boot_options_buf;
	return 0;
}
__setup("trace_options=", set_trace_boot_options);

171
unsigned long long ns2usecs(cycle_t nsec)
172
173
174
175
176
177
{
	nsec += 500;
	do_div(nsec, 1000);
	return nsec;
}

Steven Rostedt's avatar
Steven Rostedt committed
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * The global_trace is the descriptor that holds the tracing
 * buffers for the live tracing. For each CPU, it contains
 * a link list of pages that will store trace entries. The
 * page descriptor of the pages in the memory is used to hold
 * the link list by linking the lru item in the page descriptor
 * to each of the pages in the buffer per CPU.
 *
 * For each active CPU there is a data field that holds the
 * pages for the buffer for that CPU. Each CPU has the same number
 * of pages allocated for its buffer.
 */
190
191
static struct trace_array	global_trace;

192
193
LIST_HEAD(ftrace_trace_arrays);

194
195
static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);

196
197
int filter_current_check_discard(struct ring_buffer *buffer,
				 struct ftrace_event_call *call, void *rec,
198
199
				 struct ring_buffer_event *event)
{
200
	return filter_check_discard(call, rec, buffer, event);
201
}
202
EXPORT_SYMBOL_GPL(filter_current_check_discard);
203

204
205
206
207
208
209
210
211
212
213
214
215
216
cycle_t ftrace_now(int cpu)
{
	u64 ts;

	/* Early boot up does not have a buffer yet */
	if (!global_trace.buffer)
		return trace_clock_local();

	ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
	ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);

	return ts;
}
217

Steven Rostedt's avatar
Steven Rostedt committed
218
219
220
221
222
223
224
225
226
227
/*
 * The max_tr is used to snapshot the global_trace when a maximum
 * latency is reached. Some tracers will use this to store a maximum
 * trace while it continues examining live traces.
 *
 * The buffers for the max_tr are set up the same as the global_trace.
 * When a snapshot is taken, the link list of the max_tr is swapped
 * with the link list of the global_trace and the buffers are reset for
 * the global_trace so the tracing can continue.
 */
228
229
static struct trace_array	max_tr;

230
static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
231

232
233
int tracing_is_enabled(void)
{
234
	return tracing_is_on();
235
236
}

Steven Rostedt's avatar
Steven Rostedt committed
237
/*
238
239
240
 * trace_buf_size is the size in bytes that is allocated
 * for a buffer. Note, the number of bytes is always rounded
 * to page size.
241
242
243
244
245
 *
 * This number is purposely set to a low number of 16384.
 * If the dump on oops happens, it will be much appreciated
 * to not have to wait for all that output. Anyway this can be
 * boot time and run time configurable.
Steven Rostedt's avatar
Steven Rostedt committed
246
 */
247
#define TRACE_BUF_SIZE_DEFAULT	1441792UL /* 16384 * 88 (sizeof(entry)) */
248

249
static unsigned long		trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
250

Steven Rostedt's avatar
Steven Rostedt committed
251
/* trace_types holds a link list of available tracers. */
252
static struct tracer		*trace_types __read_mostly;
Steven Rostedt's avatar
Steven Rostedt committed
253
254

/* current_trace points to the tracer that is currently active */
255
static struct tracer		*current_trace __read_mostly = &nop_trace;
Steven Rostedt's avatar
Steven Rostedt committed
256
257
258
259

/*
 * trace_types_lock is used to protect the trace_types list.
 */
260
static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt's avatar
Steven Rostedt committed
261

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * serialize the access of the ring buffer
 *
 * ring buffer serializes readers, but it is low level protection.
 * The validity of the events (which returns by ring_buffer_peek() ..etc)
 * are not protected by ring buffer.
 *
 * The content of events may become garbage if we allow other process consumes
 * these events concurrently:
 *   A) the page of the consumed events may become a normal page
 *      (not reader page) in ring buffer, and this page will be rewrited
 *      by events producer.
 *   B) The page of the consumed events may become a page for splice_read,
 *      and this page will be returned to system.
 *
 * These primitives allow multi process access to different cpu ring buffer
 * concurrently.
 *
 * These primitives don't distinguish read-only and read-consume access.
 * Multi read-only access are also serialized.
 */

#ifdef CONFIG_SMP
static DECLARE_RWSEM(all_cpu_access_lock);
static DEFINE_PER_CPU(struct mutex, cpu_access_lock);

static inline void trace_access_lock(int cpu)
{
290
	if (cpu == RING_BUFFER_ALL_CPUS) {
291
292
293
294
295
		/* gain it for accessing the whole ring buffer. */
		down_write(&all_cpu_access_lock);
	} else {
		/* gain it for accessing a cpu ring buffer. */

296
		/* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
297
298
299
300
301
302
303
304
305
		down_read(&all_cpu_access_lock);

		/* Secondly block other access to this @cpu ring buffer. */
		mutex_lock(&per_cpu(cpu_access_lock, cpu));
	}
}

static inline void trace_access_unlock(int cpu)
{
306
	if (cpu == RING_BUFFER_ALL_CPUS) {
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
		up_write(&all_cpu_access_lock);
	} else {
		mutex_unlock(&per_cpu(cpu_access_lock, cpu));
		up_read(&all_cpu_access_lock);
	}
}

static inline void trace_access_lock_init(void)
{
	int cpu;

	for_each_possible_cpu(cpu)
		mutex_init(&per_cpu(cpu_access_lock, cpu));
}

#else

static DEFINE_MUTEX(access_lock);

static inline void trace_access_lock(int cpu)
{
	(void)cpu;
	mutex_lock(&access_lock);
}

static inline void trace_access_unlock(int cpu)
{
	(void)cpu;
	mutex_unlock(&access_lock);
}

static inline void trace_access_lock_init(void)
{
}

#endif

Steven Rostedt's avatar
Steven Rostedt committed
344
/* trace_wait is a waitqueue for tasks blocked on trace_poll */
345
346
static DECLARE_WAIT_QUEUE_HEAD(trace_wait);

347
/* trace_flags holds trace_options default values */
348
unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
349
	TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
350
	TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
351
	TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
352

353
static int trace_stop_count;
354
static DEFINE_RAW_SPINLOCK(tracing_start_lock);
355

356
357
358
359
360
361
362
363
/**
 * trace_wake_up - wake up tasks waiting for trace input
 *
 * Schedules a delayed work to wake up any task that is blocked on the
 * trace_wait queue. These is used with trace_poll for tasks polling the
 * trace.
 */
static void trace_wake_up(struct irq_work *work)
364
{
365
	wake_up_all(&trace_wait);
366

367
}
368

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 * tracing_on - enable tracing buffers
 *
 * This function enables tracing buffers that may have been
 * disabled with tracing_off.
 */
void tracing_on(void)
{
	if (global_trace.buffer)
		ring_buffer_record_on(global_trace.buffer);
	/*
	 * This flag is only looked at when buffers haven't been
	 * allocated yet. We don't really care about the race
	 * between setting this flag and actually turning
	 * on the buffer.
	 */
	global_trace.buffer_disabled = 0;
}
EXPORT_SYMBOL_GPL(tracing_on);

/**
 * tracing_off - turn off tracing buffers
 *
 * This function stops the tracing buffers from recording data.
 * It does not disable any overhead the tracers themselves may
 * be causing. This function simply causes all recording to
 * the ring buffers to fail.
 */
void tracing_off(void)
{
	if (global_trace.buffer)
400
		ring_buffer_record_off(global_trace.buffer);
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
	/*
	 * This flag is only looked at when buffers haven't been
	 * allocated yet. We don't really care about the race
	 * between setting this flag and actually turning
	 * on the buffer.
	 */
	global_trace.buffer_disabled = 1;
}
EXPORT_SYMBOL_GPL(tracing_off);

/**
 * tracing_is_on - show state of ring buffers enabled
 */
int tracing_is_on(void)
{
	if (global_trace.buffer)
		return ring_buffer_record_is_on(global_trace.buffer);
	return !global_trace.buffer_disabled;
}
EXPORT_SYMBOL_GPL(tracing_is_on);

422
static int __init set_buf_size(char *str)
423
{
424
	unsigned long buf_size;
425

426
427
	if (!str)
		return 0;
428
	buf_size = memparse(str, &str);
429
	/* nr_entries can not be zero */
430
	if (buf_size == 0)
431
		return 0;
432
	trace_buf_size = buf_size;
433
434
	return 1;
}
435
__setup("trace_buf_size=", set_buf_size);
436

437
438
static int __init set_tracing_thresh(char *str)
{
439
	unsigned long threshold;
440
441
442
443
	int ret;

	if (!str)
		return 0;
444
	ret = kstrtoul(str, 0, &threshold);
445
446
	if (ret < 0)
		return 0;
447
	tracing_thresh = threshold * 1000;
448
449
450
451
	return 1;
}
__setup("tracing_thresh=", set_tracing_thresh);

Steven Rostedt's avatar
Steven Rostedt committed
452
453
454
455
456
unsigned long nsecs_to_usecs(unsigned long nsecs)
{
	return nsecs / 1000;
}

Steven Rostedt's avatar
Steven Rostedt committed
457
/* These must match the bit postions in trace_iterator_flags */
458
459
460
461
462
static const char *trace_options[] = {
	"print-parent",
	"sym-offset",
	"sym-addr",
	"verbose",
Ingo Molnar's avatar
Ingo Molnar committed
463
	"raw",
464
	"hex",
Ingo Molnar's avatar
Ingo Molnar committed
465
	"bin",
466
	"block",
Ingo Molnar's avatar
Ingo Molnar committed
467
	"stacktrace",
468
	"trace_printk",
469
	"ftrace_preempt",
470
	"branch",
471
	"annotate",
472
	"userstacktrace",
473
	"sym-userobj",
474
	"printk-msg-only",
475
	"context-info",
476
	"latency-format",
477
	"sleep-time",
478
	"graph-time",
479
	"record-cmd",
480
	"overwrite",
481
	"disable_on_free",
482
	"irq-info",
483
	"markers",
484
485
486
	NULL
};

487
488
489
static struct {
	u64 (*func)(void);
	const char *name;
490
	int in_ns;		/* is this clock in nanoseconds? */
491
} trace_clocks[] = {
492
493
494
	{ trace_clock_local,	"local",	1 },
	{ trace_clock_global,	"global",	1 },
	{ trace_clock_counter,	"counter",	0 },
495
	ARCH_TRACE_CLOCKS
496
497
498
499
};

int trace_clock_id;

500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
568
569
570
571
572
573
574
575
576
/*
 * trace_parser_get_init - gets the buffer for trace parser
 */
int trace_parser_get_init(struct trace_parser *parser, int size)
{
	memset(parser, 0, sizeof(*parser));

	parser->buffer = kmalloc(size, GFP_KERNEL);
	if (!parser->buffer)
		return 1;

	parser->size = size;
	return 0;
}

/*
 * trace_parser_put - frees the buffer for trace parser
 */
void trace_parser_put(struct trace_parser *parser)
{
	kfree(parser->buffer);
}

/*
 * trace_get_user - reads the user input string separated by  space
 * (matched by isspace(ch))
 *
 * For each string found the 'struct trace_parser' is updated,
 * and the function returns.
 *
 * Returns number of bytes read.
 *
 * See kernel/trace/trace.h for 'struct trace_parser' details.
 */
int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
	size_t cnt, loff_t *ppos)
{
	char ch;
	size_t read = 0;
	ssize_t ret;

	if (!*ppos)
		trace_parser_clear(parser);

	ret = get_user(ch, ubuf++);
	if (ret)
		goto out;

	read++;
	cnt--;

	/*
	 * The parser is not finished with the last write,
	 * continue reading the user input without skipping spaces.
	 */
	if (!parser->cont) {
		/* skip white space */
		while (cnt && isspace(ch)) {
			ret = get_user(ch, ubuf++);
			if (ret)
				goto out;
			read++;
			cnt--;
		}

		/* only spaces were written */
		if (isspace(ch)) {
			*ppos += read;
			ret = read;
			goto out;
		}

		parser->idx = 0;
	}

	/* read the non-space input */
	while (cnt && !isspace(ch)) {
577
		if (parser->idx < parser->size - 1)
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
			parser->buffer[parser->idx++] = ch;
		else {
			ret = -EINVAL;
			goto out;
		}
		ret = get_user(ch, ubuf++);
		if (ret)
			goto out;
		read++;
		cnt--;
	}

	/* We either got finished input or we have to wait for another call. */
	if (isspace(ch)) {
		parser->buffer[parser->idx] = 0;
		parser->cont = false;
	} else {
		parser->cont = true;
		parser->buffer[parser->idx++] = ch;
	}

	*ppos += read;
	ret = read;

out:
	return ret;
}

606
607
608
609
610
ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
{
	int len;
	int ret;

611
612
613
	if (!cnt)
		return 0;

614
615
616
617
618
619
620
	if (s->len <= s->readpos)
		return -EBUSY;

	len = s->len - s->readpos;
	if (cnt > len)
		cnt = len;
	ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
621
	if (ret == cnt)
622
623
		return -EFAULT;

624
625
	cnt -= ret;

626
	s->readpos += cnt;
627
	return cnt;
628
629
}

630
static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
631
632
633
634
635
636
637
638
639
{
	int len;

	if (s->len <= s->readpos)
		return -EBUSY;

	len = s->len - s->readpos;
	if (cnt > len)
		cnt = len;
640
	memcpy(buf, s->buffer + s->readpos, cnt);
641

642
	s->readpos += cnt;
643
644
645
	return cnt;
}

646
647
648
649
650
651
/*
 * ftrace_max_lock is used to protect the swapping of buffers
 * when taking a max snapshot. The buffers themselves are
 * protected by per_cpu spinlocks. But the action of the swap
 * needs its own lock.
 *
652
 * This is defined as a arch_spinlock_t in order to help
653
654
655
656
657
658
 * with performance when lockdep debugging is enabled.
 *
 * It is also used in other places outside the update_max_tr
 * so it needs to be defined outside of the
 * CONFIG_TRACER_MAX_TRACE.
 */
659
static arch_spinlock_t ftrace_max_lock =
660
	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
661

662
663
unsigned long __read_mostly	tracing_thresh;

664
665
666
667
668
669
670
671
672
673
674
675
#ifdef CONFIG_TRACER_MAX_TRACE
unsigned long __read_mostly	tracing_max_latency;

/*
 * Copy the new maximum trace into the separate maximum-trace
 * structure. (this way the maximum trace is permanently saved,
 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
 */
static void
__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
{
	struct trace_array_cpu *data = tr->data[cpu];
676
	struct trace_array_cpu *max_data;
677
678
679
680

	max_tr.cpu = cpu;
	max_tr.time_start = data->preempt_timestamp;

681
682
683
684
	max_data = max_tr.data[cpu];
	max_data->saved_latency = tracing_max_latency;
	max_data->critical_start = data->critical_start;
	max_data->critical_end = data->critical_end;
685

686
	memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
687
688
689
690
691
	max_data->pid = tsk->pid;
	max_data->uid = task_uid(tsk);
	max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
	max_data->policy = tsk->policy;
	max_data->rt_priority = tsk->rt_priority;
692
693
694
695
696

	/* record this tasks comm */
	tracing_record_cmdline(tsk);
}

Steven Rostedt's avatar
Steven Rostedt committed
697
698
699
700
701
702
703
704
705
/**
 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
 * @tr: tracer
 * @tsk: the task with the latency
 * @cpu: The cpu that initiated the trace.
 *
 * Flip the buffers between the @tr and the max_tr and record information
 * about which task was the cause of this latency.
 */
Ingo Molnar's avatar
Ingo Molnar committed
706
void
707
708
update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
{
709
	struct ring_buffer *buf;
710

711
712
713
	if (trace_stop_count)
		return;

714
	WARN_ON_ONCE(!irqs_disabled());
715

716
717
718
	if (!current_trace->allocated_snapshot) {
		/* Only the nop tracer should hit this when disabling */
		WARN_ON_ONCE(current_trace != &nop_trace);
719
		return;
720
	}
721

722
	arch_spin_lock(&ftrace_max_lock);
723

724
	buf = tr->buffer;
725
726
727
	tr->buffer = max_tr.buffer;
	max_tr.buffer = buf;

728
	__update_max_tr(tr, tsk, cpu);
729
	arch_spin_unlock(&ftrace_max_lock);
730
731
732
733
734
735
736
}

/**
 * update_max_tr_single - only copy one trace over, and reset the rest
 * @tr - tracer
 * @tsk - task with the latency
 * @cpu - the cpu of the buffer to copy.
Steven Rostedt's avatar
Steven Rostedt committed
737
738
 *
 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
739
 */
Ingo Molnar's avatar
Ingo Molnar committed
740
void
741
742
update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
{
743
	int ret;
744

745
746
747
	if (trace_stop_count)
		return;

748
	WARN_ON_ONCE(!irqs_disabled());
749
	if (WARN_ON_ONCE(!current_trace->allocated_snapshot))
750
751
		return;

752
	arch_spin_lock(&ftrace_max_lock);
753

754
755
	ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);

756
757
758
759
760
761
762
763
764
765
766
767
	if (ret == -EBUSY) {
		/*
		 * We failed to swap the buffer due to a commit taking
		 * place on this CPU. We fail to record, but we reset
		 * the max trace buffer (no one writes directly to it)
		 * and flag that it failed.
		 */
		trace_array_printk(&max_tr, _THIS_IP_,
			"Failed to swap buffers due to commit in progress\n");
	}

	WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
768
769

	__update_max_tr(tr, tsk, cpu);
770
	arch_spin_unlock(&ftrace_max_lock);
771
}
772
#endif /* CONFIG_TRACER_MAX_TRACE */
773

774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
static void default_wait_pipe(struct trace_iterator *iter)
{
	DEFINE_WAIT(wait);

	prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);

	/*
	 * The events can happen in critical sections where
	 * checking a work queue can cause deadlocks.
	 * After adding a task to the queue, this flag is set
	 * only to notify events to try to wake up the queue
	 * using irq_work.
	 *
	 * We don't clear it even if the buffer is no longer
	 * empty. The flag only causes the next event to run
	 * irq_work to do the work queue wake up. The worse
	 * that can happen if we race with !trace_empty() is that
	 * an event will cause an irq_work to try to wake up
	 * an empty queue.
	 *
	 * There's no reason to protect this flag either, as
	 * the work queue and irq_work logic will do the necessary
	 * synchronization for the wake ups. The only thing
	 * that is necessary is that the wake up happens after
	 * a task has been queued. It's OK for spurious wake ups.
	 */
	trace_wakeup_needed = true;

	if (trace_empty(iter))
		schedule();

	finish_wait(&trace_wait, &wait);
}

Steven Rostedt's avatar
Steven Rostedt committed
808
809
810
811
812
813
/**
 * register_tracer - register a tracer with the ftrace system.
 * @type - the plugin for the tracer
 *
 * Register a new plugin tracer.
 */
814
815
816
817
818
819
820
821
822
823
int register_tracer(struct tracer *type)
{
	struct tracer *t;
	int ret = 0;

	if (!type->name) {
		pr_info("Tracer must have a name\n");
		return -1;
	}

824
	if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefan's avatar
Li Zefan committed
825
826
827
828
		pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
		return -1;
	}

829
	mutex_lock(&trace_types_lock);
Ingo Molnar's avatar
Ingo Molnar committed
830

831
832
	tracing_selftest_running = true;

833
834
835
	for (t = trace_types; t; t = t->next) {
		if (strcmp(type->name, t->name) == 0) {
			/* already found */
Li Zefan's avatar
Li Zefan committed
836
			pr_info("Tracer %s already registered\n",
837
838
839
840
841
842
				type->name);
			ret = -1;
			goto out;
		}
	}

843
844
845
846
847
848
849
	if (!type->set_flag)
		type->set_flag = &dummy_set_flag;
	if (!type->flags)
		type->flags = &dummy_tracer_flags;
	else
		if (!type->flags->opts)
			type->flags->opts = dummy_tracer_opt;
850
851
852
	if (!type->wait_pipe)
		type->wait_pipe = default_wait_pipe;

853

Steven Rostedt's avatar
Steven Rostedt committed
854
#ifdef CONFIG_FTRACE_STARTUP_TEST
855
	if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt's avatar
Steven Rostedt committed
856
857
		struct tracer *saved_tracer = current_trace;
		struct trace_array *tr = &global_trace;
858

Steven Rostedt's avatar
Steven Rostedt committed
859
860
861
862
863
864
865
		/*
		 * Run a selftest on this tracer.
		 * Here we reset the trace buffer, and set the current
		 * tracer to be this tracer. The tracer can then run some
		 * internal tracing to verify that everything is in order.
		 * If we fail, we do not register this tracer.
		 */
866
		tracing_reset_online_cpus(tr);
Ingo Molnar's avatar
Ingo Molnar committed
867

Steven Rostedt's avatar
Steven Rostedt committed
868
		current_trace = type;
869

870
871
872
873
874
875
876
		if (type->use_max_tr) {
			/* If we expanded the buffers, make sure the max is expanded too */
			if (ring_buffer_expanded)
				ring_buffer_resize(max_tr.buffer, trace_buf_size,
						   RING_BUFFER_ALL_CPUS);
			type->allocated_snapshot = true;
		}
877

Steven Rostedt's avatar
Steven Rostedt committed
878
879
880
881
882
883
884
		/* the test is responsible for initializing and enabling */
		pr_info("Testing tracer %s: ", type->name);
		ret = type->selftest(type, tr);
		/* the test is responsible for resetting too */
		current_trace = saved_tracer;
		if (ret) {
			printk(KERN_CONT "FAILED!\n");
885
886
			/* Add the warning after printing 'FAILED' */
			WARN_ON(1);
Steven Rostedt's avatar
Steven Rostedt committed
887
888
			goto out;
		}
Steven Rostedt's avatar
Steven Rostedt committed
889
		/* Only reset on passing, to avoid touching corrupted buffers */
890
		tracing_reset_online_cpus(tr);
Ingo Molnar's avatar
Ingo Molnar committed
891

892
893
894
895
896
897
898
899
		if (type->use_max_tr) {
			type->allocated_snapshot = false;

			/* Shrink the max buffer again */
			if (ring_buffer_expanded)
				ring_buffer_resize(max_tr.buffer, 1,
						   RING_BUFFER_ALL_CPUS);
		}
900

Steven Rostedt's avatar
Steven Rostedt committed
901
902
903
904
		printk(KERN_CONT "PASSED\n");
	}
#endif

905
906
	type->next = trace_types;
	trace_types = type;
Steven Rostedt's avatar
Steven Rostedt committed
907

908
 out:
909
	tracing_selftest_running = false;
910
911
	mutex_unlock(&trace_types_lock);

Steven Rostedt's avatar
Steven Rostedt committed
912
913
914
	if (ret || !default_bootup_tracer)
		goto out_unlock;

Li Zefan's avatar
Li Zefan committed
915
	if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedt's avatar
Steven Rostedt committed
916
917
918
919
920
921
922
923
		goto out_unlock;

	printk(KERN_INFO "Starting tracer '%s'\n", type->name);
	/* Do we want this tracer to start on bootup? */
	tracing_set_tracer(type->name);
	default_bootup_tracer = NULL;
	/* disable other selftests, since this will break it. */
	tracing_selftest_disabled = 1;
924
#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedt's avatar
Steven Rostedt committed
925
926
	printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
	       type->name);
927
928
#endif

Steven Rostedt's avatar
Steven Rostedt committed
929
 out_unlock:
930
931
932
	return ret;
}

933
934
935
936
void tracing_reset(struct trace_array *tr, int cpu)
{
	struct ring_buffer *buffer = tr->buffer;

937
938
939
	if (!buffer)
		return;

940
941
942
943
	ring_buffer_record_disable(buffer);

	/* Make sure all commits have finished */
	synchronize_sched();
944
	ring_buffer_reset_cpu(buffer, cpu);
945
946
947
948

	ring_buffer_record_enable(buffer);
}

949
950
void tracing_reset_online_cpus(struct trace_array *tr)
{
951
	struct ring_buffer *buffer = tr->buffer;
952
953
	int cpu;

954
955
956
	if (!buffer)
		return;

957
958
959
960
961
	ring_buffer_record_disable(buffer);

	/* Make sure all commits have finished */
	synchronize_sched();

962
963
964
	tr->time_start = ftrace_now(tr->cpu);

	for_each_online_cpu(cpu)
965
		ring_buffer_reset_cpu(buffer, cpu);
966
967

	ring_buffer_record_enable(buffer);
968
969
}

970
971
972
973
974
975
976
977
978
979
void tracing_reset_current(int cpu)
{
	tracing_reset(&global_trace, cpu);
}

void tracing_reset_current_online_cpus(void)
{
	tracing_reset_online_cpus(&global_trace);
}

980
#define SAVED_CMDLINES 128
981
#define NO_CMDLINE_MAP UINT_MAX
982
983
984
985
static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
static int cmdline_idx;
986
static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
987
988

/* temporary disable recording */
989
static atomic_t trace_record_cmdline_disabled __read_mostly;
990
991
992

static void trace_init_cmdlines(void)
{
993
994
	memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
	memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
995
996
997
	cmdline_idx = 0;
}

998
999
1000
1001
1002
int is_tracing_stopped(void)
{
	return trace_stop_count;
}

1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/**
 * ftrace_off_permanent - disable all ftrace code permanently
 *
 * This should only be called when a serious anomally has
 * been detected.  This will turn off the function tracing,
 * ring buffers, and other tracing utilites. It takes no
 * locks and can be called from any context.
 */
void ftrace_off_permanent(void)
{
	tracing_disabled = 1;
	ftrace_stop();
	tracing_off_permanent();
}

1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
/**
 * tracing_start - quick start of the tracer
 *
 * If tracing is enabled but was stopped by tracing_stop,
 * this will start the tracer back up.
 */
void tracing_start(void)
{
	struct ring_buffer *buffer;
	unsigned long flags;

	if (tracing_disabled)
		return;

1032
	raw_spin_lock_irqsave(&tracing_start_lock, flags);
1033
1034
1035
1036
1037
1038
	if (--trace_stop_count) {
		if (trace_stop_count < 0) {
			/* Someone screwed up their debugging */
			WARN_ON_ONCE(1);
			trace_stop_count = 0;
		}
1039
1040
1041
		goto out;
	}

1042
1043
	/* Prevent the buffers from switching */
	arch_spin_lock(&ftrace_max_lock);
1044
1045
1046
1047
1048
1049
1050
1051
1052

	buffer = global_trace.buffer;
	if (buffer)
		ring_buffer_record_enable(buffer);

	buffer = max_tr.buffer;
	if (buffer)
		ring_buffer_record_enable(buffer);

1053
1054
	arch_spin_unlock(&ftrace_max_lock);

1055
1056
	ftrace_start();
 out:
1057
	raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
}

/**
 * tracing_stop - quick stop of the tracer
 *
 * Light weight way to stop tracing. Use in conjunction with
 * tracing_start.
 */
void tracing_stop(void)
{
	struct ring_buffer *buffer;
	unsigned long flags;

	ftrace_stop();
1072
	raw_spin_lock_irqsave(&tracing_start_lock, flags);
1073
1074
1075
	if (trace_stop_count++)
		goto out;

1076
1077
1078
	/* Prevent the buffers from switching */
	arch_spin_lock(&ftrace_max_lock);

1079
1080
1081
1082
1083
1084
1085
1086
	buffer = global_trace.buffer;
	if (buffer)
		ring_buffer_record_disable(buffer);

	buffer = max_tr.buffer;
	if (buffer)
		ring_buffer_record_disable(buffer);

1087
1088
	arch_spin_unlock(&ftrace_max_lock);

1089
 out:
1090
	raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
1091
1092
}

Ingo Molnar's avatar
Ingo Molnar committed
1093
void trace_stop_cmdline_recording(void);
1094

Ingo Molnar's avatar
Ingo Molnar committed
1095
static void trace_save_cmdline(struct task_struct *tsk)
1096
{
1097
	unsigned pid, idx;
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107

	if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
		return;

	/*
	 * It's not the end of the world if we don't get
	 * the lock, but we also don't want to spin
	 * nor do we want to disable interrupts,
	 * so if we miss here, then better luck next time.
	 */
1108
	if (!arch_spin_trylock(&trace_cmdline_lock))
1109
1110
1111
		return;

	idx = map_pid_to_cmdline[tsk->pid];
1112
	if (idx == NO_CMDLINE_MAP) {
1113
1114
		idx = (cmdline_idx + 1) % SAVED_CMDLINES;

1115
1116
1117
1118
1119
1120
1121
1122
1123
		/*
		 * Check whether the cmdline buffer at idx has a pid
		 * mapped. We are going to overwrite that entry so we
		 * need to clear the map_pid_to_cmdline. Otherwise we
		 * would read the new comm for the old pid.
		 */
		pid = map_cmdline_to_pid[idx];
		if (pid != NO_CMDLINE_MAP)
			map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1124

1125
		map_cmdline_to_pid[idx] = tsk->pid;
1126
1127
1128
1129
1130
1131
1132
		map_pid_to_cmdline[tsk->pid] = idx;

		cmdline_idx = idx;
	}

	memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);

1133
	arch_spin_unlock(&trace_cmdline_lock);
1134
1135
}

1136
void trace_find_cmdline(int pid, char comm[])
1137
1138
1139
{
	unsigned map;

1140
1141
1142
1143
	if (!pid) {
		strcpy(comm, "<idle>");
		return;
	}
1144

1145
1146
1147
1148
1149
	if (WARN_ON_ONCE(pid < 0)) {
		strcpy(comm, "<XXX>");
		return;
	}

1150
1151
1152
1153
	if (pid > PID_MAX_DEFAULT) {
		strcpy(comm, "<...>");
		return;
	}
1154

1155
	preempt_disable();
1156
	arch_spin_lock(&trace_cmdline_lock);
1157
	map = map_pid_to_cmdline[pid];
1158
1159
1160
1161
	if (map != NO_CMDLINE_MAP)
		strcpy(comm, saved_cmdlines[map]);
	else
		strcpy(comm, "<...>");
1162

1163
	arch_spin_unlock(&trace_cmdline_lock);
1164
	preempt_enable();
1165
1166
}

Ingo Molnar's avatar
Ingo Molnar committed
1167
void tracing_record_cmdline(struct task_struct *tsk)
1168
{
1169
	if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1170
1171
		return;

1172
1173
1174
1175
1176
	if (!__this_cpu_read(trace_cmdline_save))
		return;

	__this_cpu_write(trace_cmdline_save, false);

1177
1178
1179
	trace_save_cmdline(tsk);
}

1180
void
1181
1182
tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
			     int pc)
1183
1184
1185
{
	struct task_struct *tsk = current;

1186
1187
1188
	entry->preempt_count		= pc & 0xff;
	entry->pid			= (tsk) ? tsk->pid : 0;
	entry->flags =
1189
#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1190
		(irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1191
1192
1193
#else
		TRACE_FLAG_IRQS_NOSUPPORT |
#endif
1194
1195
1196
1197
		((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
		((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
		(need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
}
1198
EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1199

1200
1201
1202
1203
1204
struct ring_buffer_event *
trace_buffer_lock_reserve(struct ring_buffer *buffer,
			  int type,
			  unsigned long len,
			  unsigned long flags, int pc)
1205
1206
1207
{
	struct ring_buffer_event *event;

1208
	event = ring_buffer_lock_reserve(buffer, len);
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
	if (event != NULL) {
		struct trace_entry *ent = ring_buffer_event_data(event);

		tracing_generic_entry_update(ent, flags, pc);
		ent->type = type;
	}

	return event;
}

1219
1220
1221
1222
void
__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
{
	__this_cpu_write(trace_cmdline_save, true);
1223
1224
1225
1226
1227
	if (trace_wakeup_needed) {
		trace_wakeup_needed = false;
		/* irq_work_queue() supplies it's own memory barriers */
		irq_work_queue(&trace_work_wakeup);
	}
1228
1229
1230
	ring_buffer_unlock_commit(buffer, event);
}

1231
1232
1233
static inline void
__trace_buffer_unlock_commit(struct ring_buffer *buffer,
			     struct ring_buffer_event *event,
1234
			     unsigned long flags, int pc)
1235
{
1236
	__buffer_unlock_commit(buffer, event);
1237

1238
1239
	ftrace_trace_stack(buffer, flags, 6, pc);
	ftrace_trace_userstack(buffer, flags, pc);
1240
1241
}

1242
1243
1244
void trace_buffer_unlock_commit(struct ring_buffer *buffer,
				struct ring_buffer_event *event,
				unsigned long flags, int pc)
1245
{
1246
	__trace_buffer_unlock_commit(buffer, event, flags, pc);
1247
}
1248
EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1249

1250
struct ring_buffer_event *
1251
1252
trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
				  int type, unsigned long len,
1253
1254
				  unsigned long flags, int pc)
{
1255
1256
	*current_rb = global_trace.buffer;
	return trace_buffer_lock_reserve(*current_rb,
1257
1258
					 type, len, flags, pc);
}
1259
EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1260

1261