xref: /openbmc/linux/kernel/trace/trace.c (revision f633cef0200bbaec539e2dbb0bc4bed7f022f98b)
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
31 #include <linux/fs.h>
32 #include <linux/kprobes.h>
33 #include <linux/seq_file.h>
34 #include <linux/writeback.h>
35 
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
39 
40 #include "trace.h"
41 #include "trace_output.h"
42 
43 #define TRACE_BUFFER_FLAGS	(RB_FL_OVERWRITE)
44 
45 unsigned long __read_mostly	tracing_max_latency = (cycle_t)ULONG_MAX;
46 unsigned long __read_mostly	tracing_thresh;
47 
48 /*
49  * We need to change this state when a selftest is running.
50  * A selftest will lurk into the ring-buffer to count the
51  * entries inserted during the selftest although some concurrent
52  * insertions into the ring-buffer such as ftrace_printk could occurred
53  * at the same time, giving false positive or negative results.
54  */
55 static bool __read_mostly tracing_selftest_running;
56 
57 /* For tracers that don't implement custom flags */
58 static struct tracer_opt dummy_tracer_opt[] = {
59 	{ }
60 };
61 
62 static struct tracer_flags dummy_tracer_flags = {
63 	.val = 0,
64 	.opts = dummy_tracer_opt
65 };
66 
67 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
68 {
69 	return 0;
70 }
71 
72 /*
73  * Kill all tracing for good (never come back).
74  * It is initialized to 1 but will turn to zero if the initialization
75  * of the tracer is successful. But that is the only place that sets
76  * this back to zero.
77  */
78 int tracing_disabled = 1;
79 
80 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
81 
82 static inline void ftrace_disable_cpu(void)
83 {
84 	preempt_disable();
85 	local_inc(&__get_cpu_var(ftrace_cpu_disabled));
86 }
87 
88 static inline void ftrace_enable_cpu(void)
89 {
90 	local_dec(&__get_cpu_var(ftrace_cpu_disabled));
91 	preempt_enable();
92 }
93 
94 static cpumask_t __read_mostly		tracing_buffer_mask;
95 
96 #define for_each_tracing_cpu(cpu)	\
97 	for_each_cpu_mask(cpu, tracing_buffer_mask)
98 
99 /*
100  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
101  *
102  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
103  * is set, then ftrace_dump is called. This will output the contents
104  * of the ftrace buffers to the console.  This is very useful for
105  * capturing traces that lead to crashes and outputing it to a
106  * serial console.
107  *
108  * It is default off, but you can enable it with either specifying
109  * "ftrace_dump_on_oops" in the kernel command line, or setting
110  * /proc/sys/kernel/ftrace_dump_on_oops to true.
111  */
112 int ftrace_dump_on_oops;
113 
114 static int tracing_set_tracer(char *buf);
115 
116 static int __init set_ftrace(char *str)
117 {
118 	tracing_set_tracer(str);
119 	return 1;
120 }
121 __setup("ftrace", set_ftrace);
122 
123 static int __init set_ftrace_dump_on_oops(char *str)
124 {
125 	ftrace_dump_on_oops = 1;
126 	return 1;
127 }
128 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
129 
130 long
131 ns2usecs(cycle_t nsec)
132 {
133 	nsec += 500;
134 	do_div(nsec, 1000);
135 	return nsec;
136 }
137 
138 cycle_t ftrace_now(int cpu)
139 {
140 	u64 ts = ring_buffer_time_stamp(cpu);
141 	ring_buffer_normalize_time_stamp(cpu, &ts);
142 	return ts;
143 }
144 
145 /*
146  * The global_trace is the descriptor that holds the tracing
147  * buffers for the live tracing. For each CPU, it contains
148  * a link list of pages that will store trace entries. The
149  * page descriptor of the pages in the memory is used to hold
150  * the link list by linking the lru item in the page descriptor
151  * to each of the pages in the buffer per CPU.
152  *
153  * For each active CPU there is a data field that holds the
154  * pages for the buffer for that CPU. Each CPU has the same number
155  * of pages allocated for its buffer.
156  */
157 static struct trace_array	global_trace;
158 
159 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
160 
161 /*
162  * The max_tr is used to snapshot the global_trace when a maximum
163  * latency is reached. Some tracers will use this to store a maximum
164  * trace while it continues examining live traces.
165  *
166  * The buffers for the max_tr are set up the same as the global_trace.
167  * When a snapshot is taken, the link list of the max_tr is swapped
168  * with the link list of the global_trace and the buffers are reset for
169  * the global_trace so the tracing can continue.
170  */
171 static struct trace_array	max_tr;
172 
173 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
174 
175 /* tracer_enabled is used to toggle activation of a tracer */
176 static int			tracer_enabled = 1;
177 
178 /**
179  * tracing_is_enabled - return tracer_enabled status
180  *
181  * This function is used by other tracers to know the status
182  * of the tracer_enabled flag.  Tracers may use this function
183  * to know if it should enable their features when starting
184  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
185  */
186 int tracing_is_enabled(void)
187 {
188 	return tracer_enabled;
189 }
190 
191 /* function tracing enabled */
192 int				ftrace_function_enabled;
193 
194 /*
195  * trace_buf_size is the size in bytes that is allocated
196  * for a buffer. Note, the number of bytes is always rounded
197  * to page size.
198  *
199  * This number is purposely set to a low number of 16384.
200  * If the dump on oops happens, it will be much appreciated
201  * to not have to wait for all that output. Anyway this can be
202  * boot time and run time configurable.
203  */
204 #define TRACE_BUF_SIZE_DEFAULT	1441792UL /* 16384 * 88 (sizeof(entry)) */
205 
206 static unsigned long		trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
207 
208 /* trace_types holds a link list of available tracers. */
209 static struct tracer		*trace_types __read_mostly;
210 
211 /* current_trace points to the tracer that is currently active */
212 static struct tracer		*current_trace __read_mostly;
213 
214 /*
215  * max_tracer_type_len is used to simplify the allocating of
216  * buffers to read userspace tracer names. We keep track of
217  * the longest tracer name registered.
218  */
219 static int			max_tracer_type_len;
220 
221 /*
222  * trace_types_lock is used to protect the trace_types list.
223  * This lock is also used to keep user access serialized.
224  * Accesses from userspace will grab this lock while userspace
225  * activities happen inside the kernel.
226  */
227 static DEFINE_MUTEX(trace_types_lock);
228 
229 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
230 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
231 
232 /* trace_flags holds trace_options default values */
233 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
234 	TRACE_ITER_ANNOTATE;
235 
236 /**
237  * trace_wake_up - wake up tasks waiting for trace input
238  *
239  * Simply wakes up any task that is blocked on the trace_wait
240  * queue. These is used with trace_poll for tasks polling the trace.
241  */
242 void trace_wake_up(void)
243 {
244 	/*
245 	 * The runqueue_is_locked() can fail, but this is the best we
246 	 * have for now:
247 	 */
248 	if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
249 		wake_up(&trace_wait);
250 }
251 
252 static int __init set_buf_size(char *str)
253 {
254 	unsigned long buf_size;
255 	int ret;
256 
257 	if (!str)
258 		return 0;
259 	ret = strict_strtoul(str, 0, &buf_size);
260 	/* nr_entries can not be zero */
261 	if (ret < 0 || buf_size == 0)
262 		return 0;
263 	trace_buf_size = buf_size;
264 	return 1;
265 }
266 __setup("trace_buf_size=", set_buf_size);
267 
268 unsigned long nsecs_to_usecs(unsigned long nsecs)
269 {
270 	return nsecs / 1000;
271 }
272 
273 /* These must match the bit postions in trace_iterator_flags */
274 static const char *trace_options[] = {
275 	"print-parent",
276 	"sym-offset",
277 	"sym-addr",
278 	"verbose",
279 	"raw",
280 	"hex",
281 	"bin",
282 	"block",
283 	"stacktrace",
284 	"sched-tree",
285 	"ftrace_printk",
286 	"ftrace_preempt",
287 	"branch",
288 	"annotate",
289 	"userstacktrace",
290 	"sym-userobj",
291 	"printk-msg-only",
292 	NULL
293 };
294 
295 /*
296  * ftrace_max_lock is used to protect the swapping of buffers
297  * when taking a max snapshot. The buffers themselves are
298  * protected by per_cpu spinlocks. But the action of the swap
299  * needs its own lock.
300  *
301  * This is defined as a raw_spinlock_t in order to help
302  * with performance when lockdep debugging is enabled.
303  */
304 static raw_spinlock_t ftrace_max_lock =
305 	(raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
306 
307 /*
308  * Copy the new maximum trace into the separate maximum-trace
309  * structure. (this way the maximum trace is permanently saved,
310  * for later retrieval via /debugfs/tracing/latency_trace)
311  */
312 static void
313 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
314 {
315 	struct trace_array_cpu *data = tr->data[cpu];
316 
317 	max_tr.cpu = cpu;
318 	max_tr.time_start = data->preempt_timestamp;
319 
320 	data = max_tr.data[cpu];
321 	data->saved_latency = tracing_max_latency;
322 
323 	memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
324 	data->pid = tsk->pid;
325 	data->uid = task_uid(tsk);
326 	data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
327 	data->policy = tsk->policy;
328 	data->rt_priority = tsk->rt_priority;
329 
330 	/* record this tasks comm */
331 	tracing_record_cmdline(current);
332 }
333 
334 static void
335 trace_seq_reset(struct trace_seq *s)
336 {
337 	s->len = 0;
338 	s->readpos = 0;
339 }
340 
341 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
342 {
343 	int len;
344 	int ret;
345 
346 	if (s->len <= s->readpos)
347 		return -EBUSY;
348 
349 	len = s->len - s->readpos;
350 	if (cnt > len)
351 		cnt = len;
352 	ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
353 	if (ret)
354 		return -EFAULT;
355 
356 	s->readpos += len;
357 	return cnt;
358 }
359 
360 static void
361 trace_print_seq(struct seq_file *m, struct trace_seq *s)
362 {
363 	int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
364 
365 	s->buffer[len] = 0;
366 	seq_puts(m, s->buffer);
367 
368 	trace_seq_reset(s);
369 }
370 
371 /**
372  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
373  * @tr: tracer
374  * @tsk: the task with the latency
375  * @cpu: The cpu that initiated the trace.
376  *
377  * Flip the buffers between the @tr and the max_tr and record information
378  * about which task was the cause of this latency.
379  */
380 void
381 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
382 {
383 	struct ring_buffer *buf = tr->buffer;
384 
385 	WARN_ON_ONCE(!irqs_disabled());
386 	__raw_spin_lock(&ftrace_max_lock);
387 
388 	tr->buffer = max_tr.buffer;
389 	max_tr.buffer = buf;
390 
391 	ftrace_disable_cpu();
392 	ring_buffer_reset(tr->buffer);
393 	ftrace_enable_cpu();
394 
395 	__update_max_tr(tr, tsk, cpu);
396 	__raw_spin_unlock(&ftrace_max_lock);
397 }
398 
399 /**
400  * update_max_tr_single - only copy one trace over, and reset the rest
401  * @tr - tracer
402  * @tsk - task with the latency
403  * @cpu - the cpu of the buffer to copy.
404  *
405  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
406  */
407 void
408 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
409 {
410 	int ret;
411 
412 	WARN_ON_ONCE(!irqs_disabled());
413 	__raw_spin_lock(&ftrace_max_lock);
414 
415 	ftrace_disable_cpu();
416 
417 	ring_buffer_reset(max_tr.buffer);
418 	ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
419 
420 	ftrace_enable_cpu();
421 
422 	WARN_ON_ONCE(ret);
423 
424 	__update_max_tr(tr, tsk, cpu);
425 	__raw_spin_unlock(&ftrace_max_lock);
426 }
427 
428 /**
429  * register_tracer - register a tracer with the ftrace system.
430  * @type - the plugin for the tracer
431  *
432  * Register a new plugin tracer.
433  */
434 int register_tracer(struct tracer *type)
435 {
436 	struct tracer *t;
437 	int len;
438 	int ret = 0;
439 
440 	if (!type->name) {
441 		pr_info("Tracer must have a name\n");
442 		return -1;
443 	}
444 
445 	/*
446 	 * When this gets called we hold the BKL which means that
447 	 * preemption is disabled. Various trace selftests however
448 	 * need to disable and enable preemption for successful tests.
449 	 * So we drop the BKL here and grab it after the tests again.
450 	 */
451 	unlock_kernel();
452 	mutex_lock(&trace_types_lock);
453 
454 	tracing_selftest_running = true;
455 
456 	for (t = trace_types; t; t = t->next) {
457 		if (strcmp(type->name, t->name) == 0) {
458 			/* already found */
459 			pr_info("Trace %s already registered\n",
460 				type->name);
461 			ret = -1;
462 			goto out;
463 		}
464 	}
465 
466 	if (!type->set_flag)
467 		type->set_flag = &dummy_set_flag;
468 	if (!type->flags)
469 		type->flags = &dummy_tracer_flags;
470 	else
471 		if (!type->flags->opts)
472 			type->flags->opts = dummy_tracer_opt;
473 
474 #ifdef CONFIG_FTRACE_STARTUP_TEST
475 	if (type->selftest) {
476 		struct tracer *saved_tracer = current_trace;
477 		struct trace_array *tr = &global_trace;
478 		int i;
479 
480 		/*
481 		 * Run a selftest on this tracer.
482 		 * Here we reset the trace buffer, and set the current
483 		 * tracer to be this tracer. The tracer can then run some
484 		 * internal tracing to verify that everything is in order.
485 		 * If we fail, we do not register this tracer.
486 		 */
487 		for_each_tracing_cpu(i)
488 			tracing_reset(tr, i);
489 
490 		current_trace = type;
491 		/* the test is responsible for initializing and enabling */
492 		pr_info("Testing tracer %s: ", type->name);
493 		ret = type->selftest(type, tr);
494 		/* the test is responsible for resetting too */
495 		current_trace = saved_tracer;
496 		if (ret) {
497 			printk(KERN_CONT "FAILED!\n");
498 			goto out;
499 		}
500 		/* Only reset on passing, to avoid touching corrupted buffers */
501 		for_each_tracing_cpu(i)
502 			tracing_reset(tr, i);
503 
504 		printk(KERN_CONT "PASSED\n");
505 	}
506 #endif
507 
508 	type->next = trace_types;
509 	trace_types = type;
510 	len = strlen(type->name);
511 	if (len > max_tracer_type_len)
512 		max_tracer_type_len = len;
513 
514  out:
515 	tracing_selftest_running = false;
516 	mutex_unlock(&trace_types_lock);
517 	lock_kernel();
518 
519 	return ret;
520 }
521 
522 void unregister_tracer(struct tracer *type)
523 {
524 	struct tracer **t;
525 	int len;
526 
527 	mutex_lock(&trace_types_lock);
528 	for (t = &trace_types; *t; t = &(*t)->next) {
529 		if (*t == type)
530 			goto found;
531 	}
532 	pr_info("Trace %s not registered\n", type->name);
533 	goto out;
534 
535  found:
536 	*t = (*t)->next;
537 	if (strlen(type->name) != max_tracer_type_len)
538 		goto out;
539 
540 	max_tracer_type_len = 0;
541 	for (t = &trace_types; *t; t = &(*t)->next) {
542 		len = strlen((*t)->name);
543 		if (len > max_tracer_type_len)
544 			max_tracer_type_len = len;
545 	}
546  out:
547 	mutex_unlock(&trace_types_lock);
548 }
549 
550 void tracing_reset(struct trace_array *tr, int cpu)
551 {
552 	ftrace_disable_cpu();
553 	ring_buffer_reset_cpu(tr->buffer, cpu);
554 	ftrace_enable_cpu();
555 }
556 
557 void tracing_reset_online_cpus(struct trace_array *tr)
558 {
559 	int cpu;
560 
561 	tr->time_start = ftrace_now(tr->cpu);
562 
563 	for_each_online_cpu(cpu)
564 		tracing_reset(tr, cpu);
565 }
566 
567 #define SAVED_CMDLINES 128
568 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
569 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
570 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
571 static int cmdline_idx;
572 static DEFINE_SPINLOCK(trace_cmdline_lock);
573 
574 /* temporary disable recording */
575 atomic_t trace_record_cmdline_disabled __read_mostly;
576 
577 static void trace_init_cmdlines(void)
578 {
579 	memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
580 	memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
581 	cmdline_idx = 0;
582 }
583 
584 static int trace_stop_count;
585 static DEFINE_SPINLOCK(tracing_start_lock);
586 
587 /**
588  * ftrace_off_permanent - disable all ftrace code permanently
589  *
590  * This should only be called when a serious anomally has
591  * been detected.  This will turn off the function tracing,
592  * ring buffers, and other tracing utilites. It takes no
593  * locks and can be called from any context.
594  */
595 void ftrace_off_permanent(void)
596 {
597 	tracing_disabled = 1;
598 	ftrace_stop();
599 	tracing_off_permanent();
600 }
601 
602 /**
603  * tracing_start - quick start of the tracer
604  *
605  * If tracing is enabled but was stopped by tracing_stop,
606  * this will start the tracer back up.
607  */
608 void tracing_start(void)
609 {
610 	struct ring_buffer *buffer;
611 	unsigned long flags;
612 
613 	if (tracing_disabled)
614 		return;
615 
616 	spin_lock_irqsave(&tracing_start_lock, flags);
617 	if (--trace_stop_count)
618 		goto out;
619 
620 	if (trace_stop_count < 0) {
621 		/* Someone screwed up their debugging */
622 		WARN_ON_ONCE(1);
623 		trace_stop_count = 0;
624 		goto out;
625 	}
626 
627 
628 	buffer = global_trace.buffer;
629 	if (buffer)
630 		ring_buffer_record_enable(buffer);
631 
632 	buffer = max_tr.buffer;
633 	if (buffer)
634 		ring_buffer_record_enable(buffer);
635 
636 	ftrace_start();
637  out:
638 	spin_unlock_irqrestore(&tracing_start_lock, flags);
639 }
640 
641 /**
642  * tracing_stop - quick stop of the tracer
643  *
644  * Light weight way to stop tracing. Use in conjunction with
645  * tracing_start.
646  */
647 void tracing_stop(void)
648 {
649 	struct ring_buffer *buffer;
650 	unsigned long flags;
651 
652 	ftrace_stop();
653 	spin_lock_irqsave(&tracing_start_lock, flags);
654 	if (trace_stop_count++)
655 		goto out;
656 
657 	buffer = global_trace.buffer;
658 	if (buffer)
659 		ring_buffer_record_disable(buffer);
660 
661 	buffer = max_tr.buffer;
662 	if (buffer)
663 		ring_buffer_record_disable(buffer);
664 
665  out:
666 	spin_unlock_irqrestore(&tracing_start_lock, flags);
667 }
668 
669 void trace_stop_cmdline_recording(void);
670 
671 static void trace_save_cmdline(struct task_struct *tsk)
672 {
673 	unsigned map;
674 	unsigned idx;
675 
676 	if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
677 		return;
678 
679 	/*
680 	 * It's not the end of the world if we don't get
681 	 * the lock, but we also don't want to spin
682 	 * nor do we want to disable interrupts,
683 	 * so if we miss here, then better luck next time.
684 	 */
685 	if (!spin_trylock(&trace_cmdline_lock))
686 		return;
687 
688 	idx = map_pid_to_cmdline[tsk->pid];
689 	if (idx >= SAVED_CMDLINES) {
690 		idx = (cmdline_idx + 1) % SAVED_CMDLINES;
691 
692 		map = map_cmdline_to_pid[idx];
693 		if (map <= PID_MAX_DEFAULT)
694 			map_pid_to_cmdline[map] = (unsigned)-1;
695 
696 		map_pid_to_cmdline[tsk->pid] = idx;
697 
698 		cmdline_idx = idx;
699 	}
700 
701 	memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
702 
703 	spin_unlock(&trace_cmdline_lock);
704 }
705 
706 char *trace_find_cmdline(int pid)
707 {
708 	char *cmdline = "<...>";
709 	unsigned map;
710 
711 	if (!pid)
712 		return "<idle>";
713 
714 	if (pid > PID_MAX_DEFAULT)
715 		goto out;
716 
717 	map = map_pid_to_cmdline[pid];
718 	if (map >= SAVED_CMDLINES)
719 		goto out;
720 
721 	cmdline = saved_cmdlines[map];
722 
723  out:
724 	return cmdline;
725 }
726 
727 void tracing_record_cmdline(struct task_struct *tsk)
728 {
729 	if (atomic_read(&trace_record_cmdline_disabled))
730 		return;
731 
732 	trace_save_cmdline(tsk);
733 }
734 
735 void
736 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
737 			     int pc)
738 {
739 	struct task_struct *tsk = current;
740 
741 	entry->preempt_count		= pc & 0xff;
742 	entry->pid			= (tsk) ? tsk->pid : 0;
743 	entry->tgid               	= (tsk) ? tsk->tgid : 0;
744 	entry->flags =
745 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
746 		(irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
747 #else
748 		TRACE_FLAG_IRQS_NOSUPPORT |
749 #endif
750 		((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
751 		((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
752 		(need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
753 }
754 
755 void
756 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
757 	       unsigned long ip, unsigned long parent_ip, unsigned long flags,
758 	       int pc)
759 {
760 	struct ring_buffer_event *event;
761 	struct ftrace_entry *entry;
762 	unsigned long irq_flags;
763 
764 	/* If we are reading the ring buffer, don't trace */
765 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
766 		return;
767 
768 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
769 					 &irq_flags);
770 	if (!event)
771 		return;
772 	entry	= ring_buffer_event_data(event);
773 	tracing_generic_entry_update(&entry->ent, flags, pc);
774 	entry->ent.type			= TRACE_FN;
775 	entry->ip			= ip;
776 	entry->parent_ip		= parent_ip;
777 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
778 }
779 
780 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
781 static void __trace_graph_entry(struct trace_array *tr,
782 				struct trace_array_cpu *data,
783 				struct ftrace_graph_ent *trace,
784 				unsigned long flags,
785 				int pc)
786 {
787 	struct ring_buffer_event *event;
788 	struct ftrace_graph_ent_entry *entry;
789 	unsigned long irq_flags;
790 
791 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
792 		return;
793 
794 	event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
795 					 &irq_flags);
796 	if (!event)
797 		return;
798 	entry	= ring_buffer_event_data(event);
799 	tracing_generic_entry_update(&entry->ent, flags, pc);
800 	entry->ent.type			= TRACE_GRAPH_ENT;
801 	entry->graph_ent			= *trace;
802 	ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
803 }
804 
805 static void __trace_graph_return(struct trace_array *tr,
806 				struct trace_array_cpu *data,
807 				struct ftrace_graph_ret *trace,
808 				unsigned long flags,
809 				int pc)
810 {
811 	struct ring_buffer_event *event;
812 	struct ftrace_graph_ret_entry *entry;
813 	unsigned long irq_flags;
814 
815 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
816 		return;
817 
818 	event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
819 					 &irq_flags);
820 	if (!event)
821 		return;
822 	entry	= ring_buffer_event_data(event);
823 	tracing_generic_entry_update(&entry->ent, flags, pc);
824 	entry->ent.type			= TRACE_GRAPH_RET;
825 	entry->ret				= *trace;
826 	ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
827 }
828 #endif
829 
830 void
831 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
832        unsigned long ip, unsigned long parent_ip, unsigned long flags,
833        int pc)
834 {
835 	if (likely(!atomic_read(&data->disabled)))
836 		trace_function(tr, data, ip, parent_ip, flags, pc);
837 }
838 
839 static void ftrace_trace_stack(struct trace_array *tr,
840 			       struct trace_array_cpu *data,
841 			       unsigned long flags,
842 			       int skip, int pc)
843 {
844 #ifdef CONFIG_STACKTRACE
845 	struct ring_buffer_event *event;
846 	struct stack_entry *entry;
847 	struct stack_trace trace;
848 	unsigned long irq_flags;
849 
850 	if (!(trace_flags & TRACE_ITER_STACKTRACE))
851 		return;
852 
853 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
854 					 &irq_flags);
855 	if (!event)
856 		return;
857 	entry	= ring_buffer_event_data(event);
858 	tracing_generic_entry_update(&entry->ent, flags, pc);
859 	entry->ent.type		= TRACE_STACK;
860 
861 	memset(&entry->caller, 0, sizeof(entry->caller));
862 
863 	trace.nr_entries	= 0;
864 	trace.max_entries	= FTRACE_STACK_ENTRIES;
865 	trace.skip		= skip;
866 	trace.entries		= entry->caller;
867 
868 	save_stack_trace(&trace);
869 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
870 #endif
871 }
872 
873 void __trace_stack(struct trace_array *tr,
874 		   struct trace_array_cpu *data,
875 		   unsigned long flags,
876 		   int skip)
877 {
878 	ftrace_trace_stack(tr, data, flags, skip, preempt_count());
879 }
880 
881 static void ftrace_trace_userstack(struct trace_array *tr,
882 		   struct trace_array_cpu *data,
883 		   unsigned long flags, int pc)
884 {
885 #ifdef CONFIG_STACKTRACE
886 	struct ring_buffer_event *event;
887 	struct userstack_entry *entry;
888 	struct stack_trace trace;
889 	unsigned long irq_flags;
890 
891 	if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
892 		return;
893 
894 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
895 					 &irq_flags);
896 	if (!event)
897 		return;
898 	entry	= ring_buffer_event_data(event);
899 	tracing_generic_entry_update(&entry->ent, flags, pc);
900 	entry->ent.type		= TRACE_USER_STACK;
901 
902 	memset(&entry->caller, 0, sizeof(entry->caller));
903 
904 	trace.nr_entries	= 0;
905 	trace.max_entries	= FTRACE_STACK_ENTRIES;
906 	trace.skip		= 0;
907 	trace.entries		= entry->caller;
908 
909 	save_stack_trace_user(&trace);
910 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
911 #endif
912 }
913 
914 void __trace_userstack(struct trace_array *tr,
915 		   struct trace_array_cpu *data,
916 		   unsigned long flags)
917 {
918 	ftrace_trace_userstack(tr, data, flags, preempt_count());
919 }
920 
921 static void
922 ftrace_trace_special(void *__tr, void *__data,
923 		     unsigned long arg1, unsigned long arg2, unsigned long arg3,
924 		     int pc)
925 {
926 	struct ring_buffer_event *event;
927 	struct trace_array_cpu *data = __data;
928 	struct trace_array *tr = __tr;
929 	struct special_entry *entry;
930 	unsigned long irq_flags;
931 
932 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
933 					 &irq_flags);
934 	if (!event)
935 		return;
936 	entry	= ring_buffer_event_data(event);
937 	tracing_generic_entry_update(&entry->ent, 0, pc);
938 	entry->ent.type			= TRACE_SPECIAL;
939 	entry->arg1			= arg1;
940 	entry->arg2			= arg2;
941 	entry->arg3			= arg3;
942 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
943 	ftrace_trace_stack(tr, data, irq_flags, 4, pc);
944 	ftrace_trace_userstack(tr, data, irq_flags, pc);
945 
946 	trace_wake_up();
947 }
948 
949 void
950 __trace_special(void *__tr, void *__data,
951 		unsigned long arg1, unsigned long arg2, unsigned long arg3)
952 {
953 	ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
954 }
955 
956 void
957 tracing_sched_switch_trace(struct trace_array *tr,
958 			   struct trace_array_cpu *data,
959 			   struct task_struct *prev,
960 			   struct task_struct *next,
961 			   unsigned long flags, int pc)
962 {
963 	struct ring_buffer_event *event;
964 	struct ctx_switch_entry *entry;
965 	unsigned long irq_flags;
966 
967 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
968 					   &irq_flags);
969 	if (!event)
970 		return;
971 	entry	= ring_buffer_event_data(event);
972 	tracing_generic_entry_update(&entry->ent, flags, pc);
973 	entry->ent.type			= TRACE_CTX;
974 	entry->prev_pid			= prev->pid;
975 	entry->prev_prio		= prev->prio;
976 	entry->prev_state		= prev->state;
977 	entry->next_pid			= next->pid;
978 	entry->next_prio		= next->prio;
979 	entry->next_state		= next->state;
980 	entry->next_cpu	= task_cpu(next);
981 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
982 	ftrace_trace_stack(tr, data, flags, 5, pc);
983 	ftrace_trace_userstack(tr, data, flags, pc);
984 }
985 
986 void
987 tracing_sched_wakeup_trace(struct trace_array *tr,
988 			   struct trace_array_cpu *data,
989 			   struct task_struct *wakee,
990 			   struct task_struct *curr,
991 			   unsigned long flags, int pc)
992 {
993 	struct ring_buffer_event *event;
994 	struct ctx_switch_entry *entry;
995 	unsigned long irq_flags;
996 
997 	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
998 					   &irq_flags);
999 	if (!event)
1000 		return;
1001 	entry	= ring_buffer_event_data(event);
1002 	tracing_generic_entry_update(&entry->ent, flags, pc);
1003 	entry->ent.type			= TRACE_WAKE;
1004 	entry->prev_pid			= curr->pid;
1005 	entry->prev_prio		= curr->prio;
1006 	entry->prev_state		= curr->state;
1007 	entry->next_pid			= wakee->pid;
1008 	entry->next_prio		= wakee->prio;
1009 	entry->next_state		= wakee->state;
1010 	entry->next_cpu			= task_cpu(wakee);
1011 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
1012 	ftrace_trace_stack(tr, data, flags, 6, pc);
1013 	ftrace_trace_userstack(tr, data, flags, pc);
1014 
1015 	trace_wake_up();
1016 }
1017 
1018 void
1019 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1020 {
1021 	struct trace_array *tr = &global_trace;
1022 	struct trace_array_cpu *data;
1023 	unsigned long flags;
1024 	int cpu;
1025 	int pc;
1026 
1027 	if (tracing_disabled)
1028 		return;
1029 
1030 	pc = preempt_count();
1031 	local_irq_save(flags);
1032 	cpu = raw_smp_processor_id();
1033 	data = tr->data[cpu];
1034 
1035 	if (likely(atomic_inc_return(&data->disabled) == 1))
1036 		ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
1037 
1038 	atomic_dec(&data->disabled);
1039 	local_irq_restore(flags);
1040 }
1041 
1042 #ifdef CONFIG_FUNCTION_TRACER
1043 static void
1044 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
1045 {
1046 	struct trace_array *tr = &global_trace;
1047 	struct trace_array_cpu *data;
1048 	unsigned long flags;
1049 	long disabled;
1050 	int cpu, resched;
1051 	int pc;
1052 
1053 	if (unlikely(!ftrace_function_enabled))
1054 		return;
1055 
1056 	pc = preempt_count();
1057 	resched = ftrace_preempt_disable();
1058 	local_save_flags(flags);
1059 	cpu = raw_smp_processor_id();
1060 	data = tr->data[cpu];
1061 	disabled = atomic_inc_return(&data->disabled);
1062 
1063 	if (likely(disabled == 1))
1064 		trace_function(tr, data, ip, parent_ip, flags, pc);
1065 
1066 	atomic_dec(&data->disabled);
1067 	ftrace_preempt_enable(resched);
1068 }
1069 
1070 static void
1071 function_trace_call(unsigned long ip, unsigned long parent_ip)
1072 {
1073 	struct trace_array *tr = &global_trace;
1074 	struct trace_array_cpu *data;
1075 	unsigned long flags;
1076 	long disabled;
1077 	int cpu;
1078 	int pc;
1079 
1080 	if (unlikely(!ftrace_function_enabled))
1081 		return;
1082 
1083 	/*
1084 	 * Need to use raw, since this must be called before the
1085 	 * recursive protection is performed.
1086 	 */
1087 	local_irq_save(flags);
1088 	cpu = raw_smp_processor_id();
1089 	data = tr->data[cpu];
1090 	disabled = atomic_inc_return(&data->disabled);
1091 
1092 	if (likely(disabled == 1)) {
1093 		pc = preempt_count();
1094 		trace_function(tr, data, ip, parent_ip, flags, pc);
1095 	}
1096 
1097 	atomic_dec(&data->disabled);
1098 	local_irq_restore(flags);
1099 }
1100 
1101 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1102 int trace_graph_entry(struct ftrace_graph_ent *trace)
1103 {
1104 	struct trace_array *tr = &global_trace;
1105 	struct trace_array_cpu *data;
1106 	unsigned long flags;
1107 	long disabled;
1108 	int cpu;
1109 	int pc;
1110 
1111 	if (!ftrace_trace_task(current))
1112 		return 0;
1113 
1114 	if (!ftrace_graph_addr(trace->func))
1115 		return 0;
1116 
1117 	local_irq_save(flags);
1118 	cpu = raw_smp_processor_id();
1119 	data = tr->data[cpu];
1120 	disabled = atomic_inc_return(&data->disabled);
1121 	if (likely(disabled == 1)) {
1122 		pc = preempt_count();
1123 		__trace_graph_entry(tr, data, trace, flags, pc);
1124 	}
1125 	/* Only do the atomic if it is not already set */
1126 	if (!test_tsk_trace_graph(current))
1127 		set_tsk_trace_graph(current);
1128 	atomic_dec(&data->disabled);
1129 	local_irq_restore(flags);
1130 
1131 	return 1;
1132 }
1133 
1134 void trace_graph_return(struct ftrace_graph_ret *trace)
1135 {
1136 	struct trace_array *tr = &global_trace;
1137 	struct trace_array_cpu *data;
1138 	unsigned long flags;
1139 	long disabled;
1140 	int cpu;
1141 	int pc;
1142 
1143 	local_irq_save(flags);
1144 	cpu = raw_smp_processor_id();
1145 	data = tr->data[cpu];
1146 	disabled = atomic_inc_return(&data->disabled);
1147 	if (likely(disabled == 1)) {
1148 		pc = preempt_count();
1149 		__trace_graph_return(tr, data, trace, flags, pc);
1150 	}
1151 	if (!trace->depth)
1152 		clear_tsk_trace_graph(current);
1153 	atomic_dec(&data->disabled);
1154 	local_irq_restore(flags);
1155 }
1156 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1157 
1158 static struct ftrace_ops trace_ops __read_mostly =
1159 {
1160 	.func = function_trace_call,
1161 };
1162 
1163 void tracing_start_function_trace(void)
1164 {
1165 	ftrace_function_enabled = 0;
1166 
1167 	if (trace_flags & TRACE_ITER_PREEMPTONLY)
1168 		trace_ops.func = function_trace_call_preempt_only;
1169 	else
1170 		trace_ops.func = function_trace_call;
1171 
1172 	register_ftrace_function(&trace_ops);
1173 	ftrace_function_enabled = 1;
1174 }
1175 
1176 void tracing_stop_function_trace(void)
1177 {
1178 	ftrace_function_enabled = 0;
1179 	unregister_ftrace_function(&trace_ops);
1180 }
1181 #endif
1182 
1183 enum trace_file_type {
1184 	TRACE_FILE_LAT_FMT	= 1,
1185 	TRACE_FILE_ANNOTATE	= 2,
1186 };
1187 
1188 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1189 {
1190 	/* Don't allow ftrace to trace into the ring buffers */
1191 	ftrace_disable_cpu();
1192 
1193 	iter->idx++;
1194 	if (iter->buffer_iter[iter->cpu])
1195 		ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1196 
1197 	ftrace_enable_cpu();
1198 }
1199 
1200 static struct trace_entry *
1201 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1202 {
1203 	struct ring_buffer_event *event;
1204 	struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1205 
1206 	/* Don't allow ftrace to trace into the ring buffers */
1207 	ftrace_disable_cpu();
1208 
1209 	if (buf_iter)
1210 		event = ring_buffer_iter_peek(buf_iter, ts);
1211 	else
1212 		event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1213 
1214 	ftrace_enable_cpu();
1215 
1216 	return event ? ring_buffer_event_data(event) : NULL;
1217 }
1218 
1219 static struct trace_entry *
1220 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1221 {
1222 	struct ring_buffer *buffer = iter->tr->buffer;
1223 	struct trace_entry *ent, *next = NULL;
1224 	u64 next_ts = 0, ts;
1225 	int next_cpu = -1;
1226 	int cpu;
1227 
1228 	for_each_tracing_cpu(cpu) {
1229 
1230 		if (ring_buffer_empty_cpu(buffer, cpu))
1231 			continue;
1232 
1233 		ent = peek_next_entry(iter, cpu, &ts);
1234 
1235 		/*
1236 		 * Pick the entry with the smallest timestamp:
1237 		 */
1238 		if (ent && (!next || ts < next_ts)) {
1239 			next = ent;
1240 			next_cpu = cpu;
1241 			next_ts = ts;
1242 		}
1243 	}
1244 
1245 	if (ent_cpu)
1246 		*ent_cpu = next_cpu;
1247 
1248 	if (ent_ts)
1249 		*ent_ts = next_ts;
1250 
1251 	return next;
1252 }
1253 
1254 /* Find the next real entry, without updating the iterator itself */
1255 static struct trace_entry *
1256 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1257 {
1258 	return __find_next_entry(iter, ent_cpu, ent_ts);
1259 }
1260 
1261 /* Find the next real entry, and increment the iterator to the next entry */
1262 static void *find_next_entry_inc(struct trace_iterator *iter)
1263 {
1264 	iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1265 
1266 	if (iter->ent)
1267 		trace_iterator_increment(iter, iter->cpu);
1268 
1269 	return iter->ent ? iter : NULL;
1270 }
1271 
1272 static void trace_consume(struct trace_iterator *iter)
1273 {
1274 	/* Don't allow ftrace to trace into the ring buffers */
1275 	ftrace_disable_cpu();
1276 	ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1277 	ftrace_enable_cpu();
1278 }
1279 
1280 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1281 {
1282 	struct trace_iterator *iter = m->private;
1283 	int i = (int)*pos;
1284 	void *ent;
1285 
1286 	(*pos)++;
1287 
1288 	/* can't go backwards */
1289 	if (iter->idx > i)
1290 		return NULL;
1291 
1292 	if (iter->idx < 0)
1293 		ent = find_next_entry_inc(iter);
1294 	else
1295 		ent = iter;
1296 
1297 	while (ent && iter->idx < i)
1298 		ent = find_next_entry_inc(iter);
1299 
1300 	iter->pos = *pos;
1301 
1302 	return ent;
1303 }
1304 
1305 static void *s_start(struct seq_file *m, loff_t *pos)
1306 {
1307 	struct trace_iterator *iter = m->private;
1308 	void *p = NULL;
1309 	loff_t l = 0;
1310 	int cpu;
1311 
1312 	mutex_lock(&trace_types_lock);
1313 
1314 	if (!current_trace || current_trace != iter->trace) {
1315 		mutex_unlock(&trace_types_lock);
1316 		return NULL;
1317 	}
1318 
1319 	atomic_inc(&trace_record_cmdline_disabled);
1320 
1321 	if (*pos != iter->pos) {
1322 		iter->ent = NULL;
1323 		iter->cpu = 0;
1324 		iter->idx = -1;
1325 
1326 		ftrace_disable_cpu();
1327 
1328 		for_each_tracing_cpu(cpu) {
1329 			ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1330 		}
1331 
1332 		ftrace_enable_cpu();
1333 
1334 		for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1335 			;
1336 
1337 	} else {
1338 		l = *pos - 1;
1339 		p = s_next(m, p, &l);
1340 	}
1341 
1342 	return p;
1343 }
1344 
1345 static void s_stop(struct seq_file *m, void *p)
1346 {
1347 	atomic_dec(&trace_record_cmdline_disabled);
1348 	mutex_unlock(&trace_types_lock);
1349 }
1350 
1351 static void print_lat_help_header(struct seq_file *m)
1352 {
1353 	seq_puts(m, "#                  _------=> CPU#            \n");
1354 	seq_puts(m, "#                 / _-----=> irqs-off        \n");
1355 	seq_puts(m, "#                | / _----=> need-resched    \n");
1356 	seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1357 	seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1358 	seq_puts(m, "#                |||| /                      \n");
1359 	seq_puts(m, "#                |||||     delay             \n");
1360 	seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1361 	seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1362 }
1363 
1364 static void print_func_help_header(struct seq_file *m)
1365 {
1366 	seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1367 	seq_puts(m, "#              | |       |          |         |\n");
1368 }
1369 
1370 
1371 static void
1372 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1373 {
1374 	unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1375 	struct trace_array *tr = iter->tr;
1376 	struct trace_array_cpu *data = tr->data[tr->cpu];
1377 	struct tracer *type = current_trace;
1378 	unsigned long total;
1379 	unsigned long entries;
1380 	const char *name = "preemption";
1381 
1382 	if (type)
1383 		name = type->name;
1384 
1385 	entries = ring_buffer_entries(iter->tr->buffer);
1386 	total = entries +
1387 		ring_buffer_overruns(iter->tr->buffer);
1388 
1389 	seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1390 		   name, UTS_RELEASE);
1391 	seq_puts(m, "-----------------------------------"
1392 		 "---------------------------------\n");
1393 	seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1394 		   " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1395 		   nsecs_to_usecs(data->saved_latency),
1396 		   entries,
1397 		   total,
1398 		   tr->cpu,
1399 #if defined(CONFIG_PREEMPT_NONE)
1400 		   "server",
1401 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1402 		   "desktop",
1403 #elif defined(CONFIG_PREEMPT)
1404 		   "preempt",
1405 #else
1406 		   "unknown",
1407 #endif
1408 		   /* These are reserved for later use */
1409 		   0, 0, 0, 0);
1410 #ifdef CONFIG_SMP
1411 	seq_printf(m, " #P:%d)\n", num_online_cpus());
1412 #else
1413 	seq_puts(m, ")\n");
1414 #endif
1415 	seq_puts(m, "    -----------------\n");
1416 	seq_printf(m, "    | task: %.16s-%d "
1417 		   "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1418 		   data->comm, data->pid, data->uid, data->nice,
1419 		   data->policy, data->rt_priority);
1420 	seq_puts(m, "    -----------------\n");
1421 
1422 	if (data->critical_start) {
1423 		seq_puts(m, " => started at: ");
1424 		seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1425 		trace_print_seq(m, &iter->seq);
1426 		seq_puts(m, "\n => ended at:   ");
1427 		seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1428 		trace_print_seq(m, &iter->seq);
1429 		seq_puts(m, "\n");
1430 	}
1431 
1432 	seq_puts(m, "\n");
1433 }
1434 
1435 static void
1436 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1437 {
1438 	int hardirq, softirq;
1439 	char *comm;
1440 
1441 	comm = trace_find_cmdline(entry->pid);
1442 
1443 	trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1444 	trace_seq_printf(s, "%3d", cpu);
1445 	trace_seq_printf(s, "%c%c",
1446 			(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1447 			 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
1448 			((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1449 
1450 	hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1451 	softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1452 	if (hardirq && softirq) {
1453 		trace_seq_putc(s, 'H');
1454 	} else {
1455 		if (hardirq) {
1456 			trace_seq_putc(s, 'h');
1457 		} else {
1458 			if (softirq)
1459 				trace_seq_putc(s, 's');
1460 			else
1461 				trace_seq_putc(s, '.');
1462 		}
1463 	}
1464 
1465 	if (entry->preempt_count)
1466 		trace_seq_printf(s, "%x", entry->preempt_count);
1467 	else
1468 		trace_seq_puts(s, ".");
1469 }
1470 
1471 unsigned long preempt_mark_thresh = 100;
1472 
1473 static void
1474 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
1475 		    unsigned long rel_usecs)
1476 {
1477 	trace_seq_printf(s, " %4lldus", abs_usecs);
1478 	if (rel_usecs > preempt_mark_thresh)
1479 		trace_seq_puts(s, "!: ");
1480 	else if (rel_usecs > 1)
1481 		trace_seq_puts(s, "+: ");
1482 	else
1483 		trace_seq_puts(s, " : ");
1484 }
1485 
1486 static void test_cpu_buff_start(struct trace_iterator *iter)
1487 {
1488 	struct trace_seq *s = &iter->seq;
1489 
1490 	if (!(trace_flags & TRACE_ITER_ANNOTATE))
1491 		return;
1492 
1493 	if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1494 		return;
1495 
1496 	if (cpu_isset(iter->cpu, iter->started))
1497 		return;
1498 
1499 	cpu_set(iter->cpu, iter->started);
1500 	trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1501 }
1502 
1503 static enum print_line_t
1504 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1505 {
1506 	struct trace_seq *s = &iter->seq;
1507 	unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1508 	struct trace_entry *next_entry;
1509 	struct trace_event *event;
1510 	unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1511 	struct trace_entry *entry = iter->ent;
1512 	unsigned long abs_usecs;
1513 	unsigned long rel_usecs;
1514 	u64 next_ts;
1515 	char *comm;
1516 	int ret;
1517 
1518 	test_cpu_buff_start(iter);
1519 
1520 	next_entry = find_next_entry(iter, NULL, &next_ts);
1521 	if (!next_entry)
1522 		next_ts = iter->ts;
1523 	rel_usecs = ns2usecs(next_ts - iter->ts);
1524 	abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
1525 
1526 	if (verbose) {
1527 		comm = trace_find_cmdline(entry->pid);
1528 		trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1529 				 " %ld.%03ldms (+%ld.%03ldms): ",
1530 				 comm,
1531 				 entry->pid, cpu, entry->flags,
1532 				 entry->preempt_count, trace_idx,
1533 				 ns2usecs(iter->ts),
1534 				 abs_usecs/1000,
1535 				 abs_usecs % 1000, rel_usecs/1000,
1536 				 rel_usecs % 1000);
1537 	} else {
1538 		lat_print_generic(s, entry, cpu);
1539 		lat_print_timestamp(s, abs_usecs, rel_usecs);
1540 	}
1541 
1542 	event = ftrace_find_event(entry->type);
1543 	if (event && event->latency_trace) {
1544 		ret = event->latency_trace(s, entry, sym_flags);
1545 		if (ret)
1546 			return ret;
1547 		return TRACE_TYPE_HANDLED;
1548 	}
1549 
1550 	trace_seq_printf(s, "Unknown type %d\n", entry->type);
1551 	return TRACE_TYPE_HANDLED;
1552 }
1553 
1554 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1555 {
1556 	struct trace_seq *s = &iter->seq;
1557 	unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1558 	struct trace_entry *entry;
1559 	struct trace_event *event;
1560 	unsigned long usec_rem;
1561 	unsigned long long t;
1562 	unsigned long secs;
1563 	char *comm;
1564 	int ret;
1565 
1566 	entry = iter->ent;
1567 
1568 	test_cpu_buff_start(iter);
1569 
1570 	comm = trace_find_cmdline(iter->ent->pid);
1571 
1572 	t = ns2usecs(iter->ts);
1573 	usec_rem = do_div(t, 1000000ULL);
1574 	secs = (unsigned long)t;
1575 
1576 	ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1577 	if (!ret)
1578 		return TRACE_TYPE_PARTIAL_LINE;
1579 	ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1580 	if (!ret)
1581 		return TRACE_TYPE_PARTIAL_LINE;
1582 	ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1583 	if (!ret)
1584 		return TRACE_TYPE_PARTIAL_LINE;
1585 
1586 	event = ftrace_find_event(entry->type);
1587 	if (event && event->trace) {
1588 		ret = event->trace(s, entry, sym_flags);
1589 		if (ret)
1590 			return ret;
1591 		return TRACE_TYPE_HANDLED;
1592 	}
1593 	ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
1594 	if (!ret)
1595 		return TRACE_TYPE_PARTIAL_LINE;
1596 
1597 	return TRACE_TYPE_HANDLED;
1598 }
1599 
1600 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1601 {
1602 	struct trace_seq *s = &iter->seq;
1603 	struct trace_entry *entry;
1604 	struct trace_event *event;
1605 	int ret;
1606 
1607 	entry = iter->ent;
1608 
1609 	ret = trace_seq_printf(s, "%d %d %llu ",
1610 		entry->pid, iter->cpu, iter->ts);
1611 	if (!ret)
1612 		return TRACE_TYPE_PARTIAL_LINE;
1613 
1614 	event = ftrace_find_event(entry->type);
1615 	if (event && event->raw) {
1616 		ret = event->raw(s, entry, 0);
1617 		if (ret)
1618 			return ret;
1619 		return TRACE_TYPE_HANDLED;
1620 	}
1621 	ret = trace_seq_printf(s, "%d ?\n", entry->type);
1622 	if (!ret)
1623 		return TRACE_TYPE_PARTIAL_LINE;
1624 
1625 	return TRACE_TYPE_HANDLED;
1626 }
1627 
1628 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1629 {
1630 	struct trace_seq *s = &iter->seq;
1631 	unsigned char newline = '\n';
1632 	struct trace_entry *entry;
1633 	struct trace_event *event;
1634 
1635 	entry = iter->ent;
1636 
1637 	SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1638 	SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1639 	SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1640 
1641 	event = ftrace_find_event(entry->type);
1642 	if (event && event->hex)
1643 		event->hex(s, entry, 0);
1644 
1645 	SEQ_PUT_FIELD_RET(s, newline);
1646 
1647 	return TRACE_TYPE_HANDLED;
1648 }
1649 
1650 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1651 {
1652 	struct trace_seq *s = &iter->seq;
1653 	struct trace_entry *entry = iter->ent;
1654 	struct print_entry *field;
1655 	int ret;
1656 
1657 	trace_assign_type(field, entry);
1658 
1659 	ret = trace_seq_printf(s, field->buf);
1660 	if (!ret)
1661 		return TRACE_TYPE_PARTIAL_LINE;
1662 
1663 	return TRACE_TYPE_HANDLED;
1664 }
1665 
1666 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1667 {
1668 	struct trace_seq *s = &iter->seq;
1669 	struct trace_entry *entry;
1670 	struct trace_event *event;
1671 
1672 	entry = iter->ent;
1673 
1674 	SEQ_PUT_FIELD_RET(s, entry->pid);
1675 	SEQ_PUT_FIELD_RET(s, entry->cpu);
1676 	SEQ_PUT_FIELD_RET(s, iter->ts);
1677 
1678 	event = ftrace_find_event(entry->type);
1679 	if (event && event->binary)
1680 		event->binary(s, entry, 0);
1681 
1682 	return TRACE_TYPE_HANDLED;
1683 }
1684 
1685 static int trace_empty(struct trace_iterator *iter)
1686 {
1687 	int cpu;
1688 
1689 	for_each_tracing_cpu(cpu) {
1690 		if (iter->buffer_iter[cpu]) {
1691 			if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1692 				return 0;
1693 		} else {
1694 			if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1695 				return 0;
1696 		}
1697 	}
1698 
1699 	return 1;
1700 }
1701 
1702 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1703 {
1704 	enum print_line_t ret;
1705 
1706 	if (iter->trace && iter->trace->print_line) {
1707 		ret = iter->trace->print_line(iter);
1708 		if (ret != TRACE_TYPE_UNHANDLED)
1709 			return ret;
1710 	}
1711 
1712 	if (iter->ent->type == TRACE_PRINT &&
1713 			trace_flags & TRACE_ITER_PRINTK &&
1714 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1715 		return print_printk_msg_only(iter);
1716 
1717 	if (trace_flags & TRACE_ITER_BIN)
1718 		return print_bin_fmt(iter);
1719 
1720 	if (trace_flags & TRACE_ITER_HEX)
1721 		return print_hex_fmt(iter);
1722 
1723 	if (trace_flags & TRACE_ITER_RAW)
1724 		return print_raw_fmt(iter);
1725 
1726 	if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1727 		return print_lat_fmt(iter, iter->idx, iter->cpu);
1728 
1729 	return print_trace_fmt(iter);
1730 }
1731 
1732 static int s_show(struct seq_file *m, void *v)
1733 {
1734 	struct trace_iterator *iter = v;
1735 
1736 	if (iter->ent == NULL) {
1737 		if (iter->tr) {
1738 			seq_printf(m, "# tracer: %s\n", iter->trace->name);
1739 			seq_puts(m, "#\n");
1740 		}
1741 		if (iter->trace && iter->trace->print_header)
1742 			iter->trace->print_header(m);
1743 		else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1744 			/* print nothing if the buffers are empty */
1745 			if (trace_empty(iter))
1746 				return 0;
1747 			print_trace_header(m, iter);
1748 			if (!(trace_flags & TRACE_ITER_VERBOSE))
1749 				print_lat_help_header(m);
1750 		} else {
1751 			if (!(trace_flags & TRACE_ITER_VERBOSE))
1752 				print_func_help_header(m);
1753 		}
1754 	} else {
1755 		print_trace_line(iter);
1756 		trace_print_seq(m, &iter->seq);
1757 	}
1758 
1759 	return 0;
1760 }
1761 
1762 static struct seq_operations tracer_seq_ops = {
1763 	.start		= s_start,
1764 	.next		= s_next,
1765 	.stop		= s_stop,
1766 	.show		= s_show,
1767 };
1768 
1769 static struct trace_iterator *
1770 __tracing_open(struct inode *inode, struct file *file, int *ret)
1771 {
1772 	struct trace_iterator *iter;
1773 	struct seq_file *m;
1774 	int cpu;
1775 
1776 	if (tracing_disabled) {
1777 		*ret = -ENODEV;
1778 		return NULL;
1779 	}
1780 
1781 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1782 	if (!iter) {
1783 		*ret = -ENOMEM;
1784 		goto out;
1785 	}
1786 
1787 	mutex_lock(&trace_types_lock);
1788 	if (current_trace && current_trace->print_max)
1789 		iter->tr = &max_tr;
1790 	else
1791 		iter->tr = inode->i_private;
1792 	iter->trace = current_trace;
1793 	iter->pos = -1;
1794 
1795 	/* Notify the tracer early; before we stop tracing. */
1796 	if (iter->trace && iter->trace->open)
1797 		iter->trace->open(iter);
1798 
1799 	/* Annotate start of buffers if we had overruns */
1800 	if (ring_buffer_overruns(iter->tr->buffer))
1801 		iter->iter_flags |= TRACE_FILE_ANNOTATE;
1802 
1803 
1804 	for_each_tracing_cpu(cpu) {
1805 
1806 		iter->buffer_iter[cpu] =
1807 			ring_buffer_read_start(iter->tr->buffer, cpu);
1808 
1809 		if (!iter->buffer_iter[cpu])
1810 			goto fail_buffer;
1811 	}
1812 
1813 	/* TODO stop tracer */
1814 	*ret = seq_open(file, &tracer_seq_ops);
1815 	if (*ret)
1816 		goto fail_buffer;
1817 
1818 	m = file->private_data;
1819 	m->private = iter;
1820 
1821 	/* stop the trace while dumping */
1822 	tracing_stop();
1823 
1824 	mutex_unlock(&trace_types_lock);
1825 
1826  out:
1827 	return iter;
1828 
1829  fail_buffer:
1830 	for_each_tracing_cpu(cpu) {
1831 		if (iter->buffer_iter[cpu])
1832 			ring_buffer_read_finish(iter->buffer_iter[cpu]);
1833 	}
1834 	mutex_unlock(&trace_types_lock);
1835 	kfree(iter);
1836 
1837 	return ERR_PTR(-ENOMEM);
1838 }
1839 
1840 int tracing_open_generic(struct inode *inode, struct file *filp)
1841 {
1842 	if (tracing_disabled)
1843 		return -ENODEV;
1844 
1845 	filp->private_data = inode->i_private;
1846 	return 0;
1847 }
1848 
1849 int tracing_release(struct inode *inode, struct file *file)
1850 {
1851 	struct seq_file *m = (struct seq_file *)file->private_data;
1852 	struct trace_iterator *iter = m->private;
1853 	int cpu;
1854 
1855 	mutex_lock(&trace_types_lock);
1856 	for_each_tracing_cpu(cpu) {
1857 		if (iter->buffer_iter[cpu])
1858 			ring_buffer_read_finish(iter->buffer_iter[cpu]);
1859 	}
1860 
1861 	if (iter->trace && iter->trace->close)
1862 		iter->trace->close(iter);
1863 
1864 	/* reenable tracing if it was previously enabled */
1865 	tracing_start();
1866 	mutex_unlock(&trace_types_lock);
1867 
1868 	seq_release(inode, file);
1869 	kfree(iter);
1870 	return 0;
1871 }
1872 
1873 static int tracing_open(struct inode *inode, struct file *file)
1874 {
1875 	int ret;
1876 
1877 	__tracing_open(inode, file, &ret);
1878 
1879 	return ret;
1880 }
1881 
1882 static int tracing_lt_open(struct inode *inode, struct file *file)
1883 {
1884 	struct trace_iterator *iter;
1885 	int ret;
1886 
1887 	iter = __tracing_open(inode, file, &ret);
1888 
1889 	if (!ret)
1890 		iter->iter_flags |= TRACE_FILE_LAT_FMT;
1891 
1892 	return ret;
1893 }
1894 
1895 
1896 static void *
1897 t_next(struct seq_file *m, void *v, loff_t *pos)
1898 {
1899 	struct tracer *t = m->private;
1900 
1901 	(*pos)++;
1902 
1903 	if (t)
1904 		t = t->next;
1905 
1906 	m->private = t;
1907 
1908 	return t;
1909 }
1910 
1911 static void *t_start(struct seq_file *m, loff_t *pos)
1912 {
1913 	struct tracer *t = m->private;
1914 	loff_t l = 0;
1915 
1916 	mutex_lock(&trace_types_lock);
1917 	for (; t && l < *pos; t = t_next(m, t, &l))
1918 		;
1919 
1920 	return t;
1921 }
1922 
1923 static void t_stop(struct seq_file *m, void *p)
1924 {
1925 	mutex_unlock(&trace_types_lock);
1926 }
1927 
1928 static int t_show(struct seq_file *m, void *v)
1929 {
1930 	struct tracer *t = v;
1931 
1932 	if (!t)
1933 		return 0;
1934 
1935 	seq_printf(m, "%s", t->name);
1936 	if (t->next)
1937 		seq_putc(m, ' ');
1938 	else
1939 		seq_putc(m, '\n');
1940 
1941 	return 0;
1942 }
1943 
1944 static struct seq_operations show_traces_seq_ops = {
1945 	.start		= t_start,
1946 	.next		= t_next,
1947 	.stop		= t_stop,
1948 	.show		= t_show,
1949 };
1950 
1951 static int show_traces_open(struct inode *inode, struct file *file)
1952 {
1953 	int ret;
1954 
1955 	if (tracing_disabled)
1956 		return -ENODEV;
1957 
1958 	ret = seq_open(file, &show_traces_seq_ops);
1959 	if (!ret) {
1960 		struct seq_file *m = file->private_data;
1961 		m->private = trace_types;
1962 	}
1963 
1964 	return ret;
1965 }
1966 
1967 static struct file_operations tracing_fops = {
1968 	.open		= tracing_open,
1969 	.read		= seq_read,
1970 	.llseek		= seq_lseek,
1971 	.release	= tracing_release,
1972 };
1973 
1974 static struct file_operations tracing_lt_fops = {
1975 	.open		= tracing_lt_open,
1976 	.read		= seq_read,
1977 	.llseek		= seq_lseek,
1978 	.release	= tracing_release,
1979 };
1980 
1981 static struct file_operations show_traces_fops = {
1982 	.open		= show_traces_open,
1983 	.read		= seq_read,
1984 	.release	= seq_release,
1985 };
1986 
1987 /*
1988  * Only trace on a CPU if the bitmask is set:
1989  */
1990 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1991 
1992 /*
1993  * When tracing/tracing_cpu_mask is modified then this holds
1994  * the new bitmask we are about to install:
1995  */
1996 static cpumask_t tracing_cpumask_new;
1997 
1998 /*
1999  * The tracer itself will not take this lock, but still we want
2000  * to provide a consistent cpumask to user-space:
2001  */
2002 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2003 
2004 /*
2005  * Temporary storage for the character representation of the
2006  * CPU bitmask (and one more byte for the newline):
2007  */
2008 static char mask_str[NR_CPUS + 1];
2009 
2010 static ssize_t
2011 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2012 		     size_t count, loff_t *ppos)
2013 {
2014 	int len;
2015 
2016 	mutex_lock(&tracing_cpumask_update_lock);
2017 
2018 	len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2019 	if (count - len < 2) {
2020 		count = -EINVAL;
2021 		goto out_err;
2022 	}
2023 	len += sprintf(mask_str + len, "\n");
2024 	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2025 
2026 out_err:
2027 	mutex_unlock(&tracing_cpumask_update_lock);
2028 
2029 	return count;
2030 }
2031 
2032 static ssize_t
2033 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2034 		      size_t count, loff_t *ppos)
2035 {
2036 	int err, cpu;
2037 
2038 	mutex_lock(&tracing_cpumask_update_lock);
2039 	err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2040 	if (err)
2041 		goto err_unlock;
2042 
2043 	local_irq_disable();
2044 	__raw_spin_lock(&ftrace_max_lock);
2045 	for_each_tracing_cpu(cpu) {
2046 		/*
2047 		 * Increase/decrease the disabled counter if we are
2048 		 * about to flip a bit in the cpumask:
2049 		 */
2050 		if (cpu_isset(cpu, tracing_cpumask) &&
2051 				!cpu_isset(cpu, tracing_cpumask_new)) {
2052 			atomic_inc(&global_trace.data[cpu]->disabled);
2053 		}
2054 		if (!cpu_isset(cpu, tracing_cpumask) &&
2055 				cpu_isset(cpu, tracing_cpumask_new)) {
2056 			atomic_dec(&global_trace.data[cpu]->disabled);
2057 		}
2058 	}
2059 	__raw_spin_unlock(&ftrace_max_lock);
2060 	local_irq_enable();
2061 
2062 	tracing_cpumask = tracing_cpumask_new;
2063 
2064 	mutex_unlock(&tracing_cpumask_update_lock);
2065 
2066 	return count;
2067 
2068 err_unlock:
2069 	mutex_unlock(&tracing_cpumask_update_lock);
2070 
2071 	return err;
2072 }
2073 
2074 static struct file_operations tracing_cpumask_fops = {
2075 	.open		= tracing_open_generic,
2076 	.read		= tracing_cpumask_read,
2077 	.write		= tracing_cpumask_write,
2078 };
2079 
2080 static ssize_t
2081 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2082 		       size_t cnt, loff_t *ppos)
2083 {
2084 	int i;
2085 	char *buf;
2086 	int r = 0;
2087 	int len = 0;
2088 	u32 tracer_flags = current_trace->flags->val;
2089 	struct tracer_opt *trace_opts = current_trace->flags->opts;
2090 
2091 
2092 	/* calulate max size */
2093 	for (i = 0; trace_options[i]; i++) {
2094 		len += strlen(trace_options[i]);
2095 		len += 3; /* "no" and space */
2096 	}
2097 
2098 	/*
2099 	 * Increase the size with names of options specific
2100 	 * of the current tracer.
2101 	 */
2102 	for (i = 0; trace_opts[i].name; i++) {
2103 		len += strlen(trace_opts[i].name);
2104 		len += 3; /* "no" and space */
2105 	}
2106 
2107 	/* +2 for \n and \0 */
2108 	buf = kmalloc(len + 2, GFP_KERNEL);
2109 	if (!buf)
2110 		return -ENOMEM;
2111 
2112 	for (i = 0; trace_options[i]; i++) {
2113 		if (trace_flags & (1 << i))
2114 			r += sprintf(buf + r, "%s ", trace_options[i]);
2115 		else
2116 			r += sprintf(buf + r, "no%s ", trace_options[i]);
2117 	}
2118 
2119 	for (i = 0; trace_opts[i].name; i++) {
2120 		if (tracer_flags & trace_opts[i].bit)
2121 			r += sprintf(buf + r, "%s ",
2122 				trace_opts[i].name);
2123 		else
2124 			r += sprintf(buf + r, "no%s ",
2125 				trace_opts[i].name);
2126 	}
2127 
2128 	r += sprintf(buf + r, "\n");
2129 	WARN_ON(r >= len + 2);
2130 
2131 	r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2132 
2133 	kfree(buf);
2134 
2135 	return r;
2136 }
2137 
2138 /* Try to assign a tracer specific option */
2139 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2140 {
2141 	struct tracer_flags *trace_flags = trace->flags;
2142 	struct tracer_opt *opts = NULL;
2143 	int ret = 0, i = 0;
2144 	int len;
2145 
2146 	for (i = 0; trace_flags->opts[i].name; i++) {
2147 		opts = &trace_flags->opts[i];
2148 		len = strlen(opts->name);
2149 
2150 		if (strncmp(cmp, opts->name, len) == 0) {
2151 			ret = trace->set_flag(trace_flags->val,
2152 				opts->bit, !neg);
2153 			break;
2154 		}
2155 	}
2156 	/* Not found */
2157 	if (!trace_flags->opts[i].name)
2158 		return -EINVAL;
2159 
2160 	/* Refused to handle */
2161 	if (ret)
2162 		return ret;
2163 
2164 	if (neg)
2165 		trace_flags->val &= ~opts->bit;
2166 	else
2167 		trace_flags->val |= opts->bit;
2168 
2169 	return 0;
2170 }
2171 
2172 static ssize_t
2173 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2174 			size_t cnt, loff_t *ppos)
2175 {
2176 	char buf[64];
2177 	char *cmp = buf;
2178 	int neg = 0;
2179 	int ret;
2180 	int i;
2181 
2182 	if (cnt >= sizeof(buf))
2183 		return -EINVAL;
2184 
2185 	if (copy_from_user(&buf, ubuf, cnt))
2186 		return -EFAULT;
2187 
2188 	buf[cnt] = 0;
2189 
2190 	if (strncmp(buf, "no", 2) == 0) {
2191 		neg = 1;
2192 		cmp += 2;
2193 	}
2194 
2195 	for (i = 0; trace_options[i]; i++) {
2196 		int len = strlen(trace_options[i]);
2197 
2198 		if (strncmp(cmp, trace_options[i], len) == 0) {
2199 			if (neg)
2200 				trace_flags &= ~(1 << i);
2201 			else
2202 				trace_flags |= (1 << i);
2203 			break;
2204 		}
2205 	}
2206 
2207 	/* If no option could be set, test the specific tracer options */
2208 	if (!trace_options[i]) {
2209 		ret = set_tracer_option(current_trace, cmp, neg);
2210 		if (ret)
2211 			return ret;
2212 	}
2213 
2214 	filp->f_pos += cnt;
2215 
2216 	return cnt;
2217 }
2218 
2219 static struct file_operations tracing_iter_fops = {
2220 	.open		= tracing_open_generic,
2221 	.read		= tracing_trace_options_read,
2222 	.write		= tracing_trace_options_write,
2223 };
2224 
2225 static const char readme_msg[] =
2226 	"tracing mini-HOWTO:\n\n"
2227 	"# mkdir /debug\n"
2228 	"# mount -t debugfs nodev /debug\n\n"
2229 	"# cat /debug/tracing/available_tracers\n"
2230 	"wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2231 	"# cat /debug/tracing/current_tracer\n"
2232 	"none\n"
2233 	"# echo sched_switch > /debug/tracing/current_tracer\n"
2234 	"# cat /debug/tracing/current_tracer\n"
2235 	"sched_switch\n"
2236 	"# cat /debug/tracing/trace_options\n"
2237 	"noprint-parent nosym-offset nosym-addr noverbose\n"
2238 	"# echo print-parent > /debug/tracing/trace_options\n"
2239 	"# echo 1 > /debug/tracing/tracing_enabled\n"
2240 	"# cat /debug/tracing/trace > /tmp/trace.txt\n"
2241 	"echo 0 > /debug/tracing/tracing_enabled\n"
2242 ;
2243 
2244 static ssize_t
2245 tracing_readme_read(struct file *filp, char __user *ubuf,
2246 		       size_t cnt, loff_t *ppos)
2247 {
2248 	return simple_read_from_buffer(ubuf, cnt, ppos,
2249 					readme_msg, strlen(readme_msg));
2250 }
2251 
2252 static struct file_operations tracing_readme_fops = {
2253 	.open		= tracing_open_generic,
2254 	.read		= tracing_readme_read,
2255 };
2256 
2257 static ssize_t
2258 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2259 		  size_t cnt, loff_t *ppos)
2260 {
2261 	char buf[64];
2262 	int r;
2263 
2264 	r = sprintf(buf, "%u\n", tracer_enabled);
2265 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2266 }
2267 
2268 static ssize_t
2269 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2270 		   size_t cnt, loff_t *ppos)
2271 {
2272 	struct trace_array *tr = filp->private_data;
2273 	char buf[64];
2274 	long val;
2275 	int ret;
2276 
2277 	if (cnt >= sizeof(buf))
2278 		return -EINVAL;
2279 
2280 	if (copy_from_user(&buf, ubuf, cnt))
2281 		return -EFAULT;
2282 
2283 	buf[cnt] = 0;
2284 
2285 	ret = strict_strtoul(buf, 10, &val);
2286 	if (ret < 0)
2287 		return ret;
2288 
2289 	val = !!val;
2290 
2291 	mutex_lock(&trace_types_lock);
2292 	if (tracer_enabled ^ val) {
2293 		if (val) {
2294 			tracer_enabled = 1;
2295 			if (current_trace->start)
2296 				current_trace->start(tr);
2297 			tracing_start();
2298 		} else {
2299 			tracer_enabled = 0;
2300 			tracing_stop();
2301 			if (current_trace->stop)
2302 				current_trace->stop(tr);
2303 		}
2304 	}
2305 	mutex_unlock(&trace_types_lock);
2306 
2307 	filp->f_pos += cnt;
2308 
2309 	return cnt;
2310 }
2311 
2312 static ssize_t
2313 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2314 		       size_t cnt, loff_t *ppos)
2315 {
2316 	char buf[max_tracer_type_len+2];
2317 	int r;
2318 
2319 	mutex_lock(&trace_types_lock);
2320 	if (current_trace)
2321 		r = sprintf(buf, "%s\n", current_trace->name);
2322 	else
2323 		r = sprintf(buf, "\n");
2324 	mutex_unlock(&trace_types_lock);
2325 
2326 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2327 }
2328 
2329 static int tracing_set_tracer(char *buf)
2330 {
2331 	struct trace_array *tr = &global_trace;
2332 	struct tracer *t;
2333 	int ret = 0;
2334 
2335 	mutex_lock(&trace_types_lock);
2336 	for (t = trace_types; t; t = t->next) {
2337 		if (strcmp(t->name, buf) == 0)
2338 			break;
2339 	}
2340 	if (!t) {
2341 		ret = -EINVAL;
2342 		goto out;
2343 	}
2344 	if (t == current_trace)
2345 		goto out;
2346 
2347 	trace_branch_disable();
2348 	if (current_trace && current_trace->reset)
2349 		current_trace->reset(tr);
2350 
2351 	current_trace = t;
2352 	if (t->init) {
2353 		ret = t->init(tr);
2354 		if (ret)
2355 			goto out;
2356 	}
2357 
2358 	trace_branch_enable(tr);
2359  out:
2360 	mutex_unlock(&trace_types_lock);
2361 
2362 	return ret;
2363 }
2364 
2365 static ssize_t
2366 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2367 			size_t cnt, loff_t *ppos)
2368 {
2369 	char buf[max_tracer_type_len+1];
2370 	int i;
2371 	size_t ret;
2372 	int err;
2373 
2374 	ret = cnt;
2375 
2376 	if (cnt > max_tracer_type_len)
2377 		cnt = max_tracer_type_len;
2378 
2379 	if (copy_from_user(&buf, ubuf, cnt))
2380 		return -EFAULT;
2381 
2382 	buf[cnt] = 0;
2383 
2384 	/* strip ending whitespace. */
2385 	for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2386 		buf[i] = 0;
2387 
2388 	err = tracing_set_tracer(buf);
2389 	if (err)
2390 		return err;
2391 
2392 	filp->f_pos += ret;
2393 
2394 	return ret;
2395 }
2396 
2397 static ssize_t
2398 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2399 		     size_t cnt, loff_t *ppos)
2400 {
2401 	unsigned long *ptr = filp->private_data;
2402 	char buf[64];
2403 	int r;
2404 
2405 	r = snprintf(buf, sizeof(buf), "%ld\n",
2406 		     *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2407 	if (r > sizeof(buf))
2408 		r = sizeof(buf);
2409 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2410 }
2411 
2412 static ssize_t
2413 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2414 		      size_t cnt, loff_t *ppos)
2415 {
2416 	long *ptr = filp->private_data;
2417 	char buf[64];
2418 	long val;
2419 	int ret;
2420 
2421 	if (cnt >= sizeof(buf))
2422 		return -EINVAL;
2423 
2424 	if (copy_from_user(&buf, ubuf, cnt))
2425 		return -EFAULT;
2426 
2427 	buf[cnt] = 0;
2428 
2429 	ret = strict_strtoul(buf, 10, &val);
2430 	if (ret < 0)
2431 		return ret;
2432 
2433 	*ptr = val * 1000;
2434 
2435 	return cnt;
2436 }
2437 
2438 static atomic_t tracing_reader;
2439 
2440 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2441 {
2442 	struct trace_iterator *iter;
2443 
2444 	if (tracing_disabled)
2445 		return -ENODEV;
2446 
2447 	/* We only allow for reader of the pipe */
2448 	if (atomic_inc_return(&tracing_reader) != 1) {
2449 		atomic_dec(&tracing_reader);
2450 		return -EBUSY;
2451 	}
2452 
2453 	/* create a buffer to store the information to pass to userspace */
2454 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2455 	if (!iter)
2456 		return -ENOMEM;
2457 
2458 	mutex_lock(&trace_types_lock);
2459 
2460 	/* trace pipe does not show start of buffer */
2461 	cpus_setall(iter->started);
2462 
2463 	iter->tr = &global_trace;
2464 	iter->trace = current_trace;
2465 	filp->private_data = iter;
2466 
2467 	if (iter->trace->pipe_open)
2468 		iter->trace->pipe_open(iter);
2469 	mutex_unlock(&trace_types_lock);
2470 
2471 	return 0;
2472 }
2473 
2474 static int tracing_release_pipe(struct inode *inode, struct file *file)
2475 {
2476 	struct trace_iterator *iter = file->private_data;
2477 
2478 	kfree(iter);
2479 	atomic_dec(&tracing_reader);
2480 
2481 	return 0;
2482 }
2483 
2484 static unsigned int
2485 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2486 {
2487 	struct trace_iterator *iter = filp->private_data;
2488 
2489 	if (trace_flags & TRACE_ITER_BLOCK) {
2490 		/*
2491 		 * Always select as readable when in blocking mode
2492 		 */
2493 		return POLLIN | POLLRDNORM;
2494 	} else {
2495 		if (!trace_empty(iter))
2496 			return POLLIN | POLLRDNORM;
2497 		poll_wait(filp, &trace_wait, poll_table);
2498 		if (!trace_empty(iter))
2499 			return POLLIN | POLLRDNORM;
2500 
2501 		return 0;
2502 	}
2503 }
2504 
2505 /*
2506  * Consumer reader.
2507  */
2508 static ssize_t
2509 tracing_read_pipe(struct file *filp, char __user *ubuf,
2510 		  size_t cnt, loff_t *ppos)
2511 {
2512 	struct trace_iterator *iter = filp->private_data;
2513 	ssize_t sret;
2514 
2515 	/* return any leftover data */
2516 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2517 	if (sret != -EBUSY)
2518 		return sret;
2519 
2520 	trace_seq_reset(&iter->seq);
2521 
2522 	mutex_lock(&trace_types_lock);
2523 	if (iter->trace->read) {
2524 		sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2525 		if (sret)
2526 			goto out;
2527 	}
2528 
2529 waitagain:
2530 	sret = 0;
2531 	while (trace_empty(iter)) {
2532 
2533 		if ((filp->f_flags & O_NONBLOCK)) {
2534 			sret = -EAGAIN;
2535 			goto out;
2536 		}
2537 
2538 		/*
2539 		 * This is a make-shift waitqueue. The reason we don't use
2540 		 * an actual wait queue is because:
2541 		 *  1) we only ever have one waiter
2542 		 *  2) the tracing, traces all functions, we don't want
2543 		 *     the overhead of calling wake_up and friends
2544 		 *     (and tracing them too)
2545 		 *     Anyway, this is really very primitive wakeup.
2546 		 */
2547 		set_current_state(TASK_INTERRUPTIBLE);
2548 		iter->tr->waiter = current;
2549 
2550 		mutex_unlock(&trace_types_lock);
2551 
2552 		/* sleep for 100 msecs, and try again. */
2553 		schedule_timeout(HZ/10);
2554 
2555 		mutex_lock(&trace_types_lock);
2556 
2557 		iter->tr->waiter = NULL;
2558 
2559 		if (signal_pending(current)) {
2560 			sret = -EINTR;
2561 			goto out;
2562 		}
2563 
2564 		if (iter->trace != current_trace)
2565 			goto out;
2566 
2567 		/*
2568 		 * We block until we read something and tracing is disabled.
2569 		 * We still block if tracing is disabled, but we have never
2570 		 * read anything. This allows a user to cat this file, and
2571 		 * then enable tracing. But after we have read something,
2572 		 * we give an EOF when tracing is again disabled.
2573 		 *
2574 		 * iter->pos will be 0 if we haven't read anything.
2575 		 */
2576 		if (!tracer_enabled && iter->pos)
2577 			break;
2578 
2579 		continue;
2580 	}
2581 
2582 	/* stop when tracing is finished */
2583 	if (trace_empty(iter))
2584 		goto out;
2585 
2586 	if (cnt >= PAGE_SIZE)
2587 		cnt = PAGE_SIZE - 1;
2588 
2589 	/* reset all but tr, trace, and overruns */
2590 	memset(&iter->seq, 0,
2591 	       sizeof(struct trace_iterator) -
2592 	       offsetof(struct trace_iterator, seq));
2593 	iter->pos = -1;
2594 
2595 	while (find_next_entry_inc(iter) != NULL) {
2596 		enum print_line_t ret;
2597 		int len = iter->seq.len;
2598 
2599 		ret = print_trace_line(iter);
2600 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
2601 			/* don't print partial lines */
2602 			iter->seq.len = len;
2603 			break;
2604 		}
2605 
2606 		trace_consume(iter);
2607 
2608 		if (iter->seq.len >= cnt)
2609 			break;
2610 	}
2611 
2612 	/* Now copy what we have to the user */
2613 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2614 	if (iter->seq.readpos >= iter->seq.len)
2615 		trace_seq_reset(&iter->seq);
2616 
2617 	/*
2618 	 * If there was nothing to send to user, inspite of consuming trace
2619 	 * entries, go back to wait for more entries.
2620 	 */
2621 	if (sret == -EBUSY)
2622 		goto waitagain;
2623 
2624 out:
2625 	mutex_unlock(&trace_types_lock);
2626 
2627 	return sret;
2628 }
2629 
2630 static ssize_t
2631 tracing_entries_read(struct file *filp, char __user *ubuf,
2632 		     size_t cnt, loff_t *ppos)
2633 {
2634 	struct trace_array *tr = filp->private_data;
2635 	char buf[64];
2636 	int r;
2637 
2638 	r = sprintf(buf, "%lu\n", tr->entries >> 10);
2639 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2640 }
2641 
2642 static ssize_t
2643 tracing_entries_write(struct file *filp, const char __user *ubuf,
2644 		      size_t cnt, loff_t *ppos)
2645 {
2646 	unsigned long val;
2647 	char buf[64];
2648 	int ret, cpu;
2649 
2650 	if (cnt >= sizeof(buf))
2651 		return -EINVAL;
2652 
2653 	if (copy_from_user(&buf, ubuf, cnt))
2654 		return -EFAULT;
2655 
2656 	buf[cnt] = 0;
2657 
2658 	ret = strict_strtoul(buf, 10, &val);
2659 	if (ret < 0)
2660 		return ret;
2661 
2662 	/* must have at least 1 entry */
2663 	if (!val)
2664 		return -EINVAL;
2665 
2666 	mutex_lock(&trace_types_lock);
2667 
2668 	tracing_stop();
2669 
2670 	/* disable all cpu buffers */
2671 	for_each_tracing_cpu(cpu) {
2672 		if (global_trace.data[cpu])
2673 			atomic_inc(&global_trace.data[cpu]->disabled);
2674 		if (max_tr.data[cpu])
2675 			atomic_inc(&max_tr.data[cpu]->disabled);
2676 	}
2677 
2678 	/* value is in KB */
2679 	val <<= 10;
2680 
2681 	if (val != global_trace.entries) {
2682 		ret = ring_buffer_resize(global_trace.buffer, val);
2683 		if (ret < 0) {
2684 			cnt = ret;
2685 			goto out;
2686 		}
2687 
2688 		ret = ring_buffer_resize(max_tr.buffer, val);
2689 		if (ret < 0) {
2690 			int r;
2691 			cnt = ret;
2692 			r = ring_buffer_resize(global_trace.buffer,
2693 					       global_trace.entries);
2694 			if (r < 0) {
2695 				/* AARGH! We are left with different
2696 				 * size max buffer!!!! */
2697 				WARN_ON(1);
2698 				tracing_disabled = 1;
2699 			}
2700 			goto out;
2701 		}
2702 
2703 		global_trace.entries = val;
2704 	}
2705 
2706 	filp->f_pos += cnt;
2707 
2708 	/* If check pages failed, return ENOMEM */
2709 	if (tracing_disabled)
2710 		cnt = -ENOMEM;
2711  out:
2712 	for_each_tracing_cpu(cpu) {
2713 		if (global_trace.data[cpu])
2714 			atomic_dec(&global_trace.data[cpu]->disabled);
2715 		if (max_tr.data[cpu])
2716 			atomic_dec(&max_tr.data[cpu]->disabled);
2717 	}
2718 
2719 	tracing_start();
2720 	max_tr.entries = global_trace.entries;
2721 	mutex_unlock(&trace_types_lock);
2722 
2723 	return cnt;
2724 }
2725 
2726 static int mark_printk(const char *fmt, ...)
2727 {
2728 	int ret;
2729 	va_list args;
2730 	va_start(args, fmt);
2731 	ret = trace_vprintk(0, -1, fmt, args);
2732 	va_end(args);
2733 	return ret;
2734 }
2735 
2736 static ssize_t
2737 tracing_mark_write(struct file *filp, const char __user *ubuf,
2738 					size_t cnt, loff_t *fpos)
2739 {
2740 	char *buf;
2741 	char *end;
2742 
2743 	if (tracing_disabled)
2744 		return -EINVAL;
2745 
2746 	if (cnt > TRACE_BUF_SIZE)
2747 		cnt = TRACE_BUF_SIZE;
2748 
2749 	buf = kmalloc(cnt + 1, GFP_KERNEL);
2750 	if (buf == NULL)
2751 		return -ENOMEM;
2752 
2753 	if (copy_from_user(buf, ubuf, cnt)) {
2754 		kfree(buf);
2755 		return -EFAULT;
2756 	}
2757 
2758 	/* Cut from the first nil or newline. */
2759 	buf[cnt] = '\0';
2760 	end = strchr(buf, '\n');
2761 	if (end)
2762 		*end = '\0';
2763 
2764 	cnt = mark_printk("%s\n", buf);
2765 	kfree(buf);
2766 	*fpos += cnt;
2767 
2768 	return cnt;
2769 }
2770 
2771 static struct file_operations tracing_max_lat_fops = {
2772 	.open		= tracing_open_generic,
2773 	.read		= tracing_max_lat_read,
2774 	.write		= tracing_max_lat_write,
2775 };
2776 
2777 static struct file_operations tracing_ctrl_fops = {
2778 	.open		= tracing_open_generic,
2779 	.read		= tracing_ctrl_read,
2780 	.write		= tracing_ctrl_write,
2781 };
2782 
2783 static struct file_operations set_tracer_fops = {
2784 	.open		= tracing_open_generic,
2785 	.read		= tracing_set_trace_read,
2786 	.write		= tracing_set_trace_write,
2787 };
2788 
2789 static struct file_operations tracing_pipe_fops = {
2790 	.open		= tracing_open_pipe,
2791 	.poll		= tracing_poll_pipe,
2792 	.read		= tracing_read_pipe,
2793 	.release	= tracing_release_pipe,
2794 };
2795 
2796 static struct file_operations tracing_entries_fops = {
2797 	.open		= tracing_open_generic,
2798 	.read		= tracing_entries_read,
2799 	.write		= tracing_entries_write,
2800 };
2801 
2802 static struct file_operations tracing_mark_fops = {
2803 	.open		= tracing_open_generic,
2804 	.write		= tracing_mark_write,
2805 };
2806 
2807 #ifdef CONFIG_DYNAMIC_FTRACE
2808 
2809 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
2810 {
2811 	return 0;
2812 }
2813 
2814 static ssize_t
2815 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
2816 		  size_t cnt, loff_t *ppos)
2817 {
2818 	static char ftrace_dyn_info_buffer[1024];
2819 	static DEFINE_MUTEX(dyn_info_mutex);
2820 	unsigned long *p = filp->private_data;
2821 	char *buf = ftrace_dyn_info_buffer;
2822 	int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
2823 	int r;
2824 
2825 	mutex_lock(&dyn_info_mutex);
2826 	r = sprintf(buf, "%ld ", *p);
2827 
2828 	r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
2829 	buf[r++] = '\n';
2830 
2831 	r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2832 
2833 	mutex_unlock(&dyn_info_mutex);
2834 
2835 	return r;
2836 }
2837 
2838 static struct file_operations tracing_dyn_info_fops = {
2839 	.open		= tracing_open_generic,
2840 	.read		= tracing_read_dyn_info,
2841 };
2842 #endif
2843 
2844 static struct dentry *d_tracer;
2845 
2846 struct dentry *tracing_init_dentry(void)
2847 {
2848 	static int once;
2849 
2850 	if (d_tracer)
2851 		return d_tracer;
2852 
2853 	d_tracer = debugfs_create_dir("tracing", NULL);
2854 
2855 	if (!d_tracer && !once) {
2856 		once = 1;
2857 		pr_warning("Could not create debugfs directory 'tracing'\n");
2858 		return NULL;
2859 	}
2860 
2861 	return d_tracer;
2862 }
2863 
2864 #ifdef CONFIG_FTRACE_SELFTEST
2865 /* Let selftest have access to static functions in this file */
2866 #include "trace_selftest.c"
2867 #endif
2868 
2869 static __init int tracer_init_debugfs(void)
2870 {
2871 	struct dentry *d_tracer;
2872 	struct dentry *entry;
2873 
2874 	d_tracer = tracing_init_dentry();
2875 
2876 	entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2877 				    &global_trace, &tracing_ctrl_fops);
2878 	if (!entry)
2879 		pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2880 
2881 	entry = debugfs_create_file("trace_options", 0644, d_tracer,
2882 				    NULL, &tracing_iter_fops);
2883 	if (!entry)
2884 		pr_warning("Could not create debugfs 'trace_options' entry\n");
2885 
2886 	entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2887 				    NULL, &tracing_cpumask_fops);
2888 	if (!entry)
2889 		pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2890 
2891 	entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2892 				    &global_trace, &tracing_lt_fops);
2893 	if (!entry)
2894 		pr_warning("Could not create debugfs 'latency_trace' entry\n");
2895 
2896 	entry = debugfs_create_file("trace", 0444, d_tracer,
2897 				    &global_trace, &tracing_fops);
2898 	if (!entry)
2899 		pr_warning("Could not create debugfs 'trace' entry\n");
2900 
2901 	entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2902 				    &global_trace, &show_traces_fops);
2903 	if (!entry)
2904 		pr_warning("Could not create debugfs 'available_tracers' entry\n");
2905 
2906 	entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2907 				    &global_trace, &set_tracer_fops);
2908 	if (!entry)
2909 		pr_warning("Could not create debugfs 'current_tracer' entry\n");
2910 
2911 	entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2912 				    &tracing_max_latency,
2913 				    &tracing_max_lat_fops);
2914 	if (!entry)
2915 		pr_warning("Could not create debugfs "
2916 			   "'tracing_max_latency' entry\n");
2917 
2918 	entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2919 				    &tracing_thresh, &tracing_max_lat_fops);
2920 	if (!entry)
2921 		pr_warning("Could not create debugfs "
2922 			   "'tracing_thresh' entry\n");
2923 	entry = debugfs_create_file("README", 0644, d_tracer,
2924 				    NULL, &tracing_readme_fops);
2925 	if (!entry)
2926 		pr_warning("Could not create debugfs 'README' entry\n");
2927 
2928 	entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2929 				    NULL, &tracing_pipe_fops);
2930 	if (!entry)
2931 		pr_warning("Could not create debugfs "
2932 			   "'trace_pipe' entry\n");
2933 
2934 	entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
2935 				    &global_trace, &tracing_entries_fops);
2936 	if (!entry)
2937 		pr_warning("Could not create debugfs "
2938 			   "'buffer_size_kb' entry\n");
2939 
2940 	entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2941 				    NULL, &tracing_mark_fops);
2942 	if (!entry)
2943 		pr_warning("Could not create debugfs "
2944 			   "'trace_marker' entry\n");
2945 
2946 #ifdef CONFIG_DYNAMIC_FTRACE
2947 	entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2948 				    &ftrace_update_tot_cnt,
2949 				    &tracing_dyn_info_fops);
2950 	if (!entry)
2951 		pr_warning("Could not create debugfs "
2952 			   "'dyn_ftrace_total_info' entry\n");
2953 #endif
2954 #ifdef CONFIG_SYSPROF_TRACER
2955 	init_tracer_sysprof_debugfs(d_tracer);
2956 #endif
2957 	return 0;
2958 }
2959 
2960 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
2961 {
2962 	static DEFINE_SPINLOCK(trace_buf_lock);
2963 	static char trace_buf[TRACE_BUF_SIZE];
2964 
2965 	struct ring_buffer_event *event;
2966 	struct trace_array *tr = &global_trace;
2967 	struct trace_array_cpu *data;
2968 	int cpu, len = 0, size, pc;
2969 	struct print_entry *entry;
2970 	unsigned long irq_flags;
2971 
2972 	if (tracing_disabled || tracing_selftest_running)
2973 		return 0;
2974 
2975 	pc = preempt_count();
2976 	preempt_disable_notrace();
2977 	cpu = raw_smp_processor_id();
2978 	data = tr->data[cpu];
2979 
2980 	if (unlikely(atomic_read(&data->disabled)))
2981 		goto out;
2982 
2983 	pause_graph_tracing();
2984 	spin_lock_irqsave(&trace_buf_lock, irq_flags);
2985 	len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
2986 
2987 	len = min(len, TRACE_BUF_SIZE-1);
2988 	trace_buf[len] = 0;
2989 
2990 	size = sizeof(*entry) + len + 1;
2991 	event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
2992 	if (!event)
2993 		goto out_unlock;
2994 	entry = ring_buffer_event_data(event);
2995 	tracing_generic_entry_update(&entry->ent, irq_flags, pc);
2996 	entry->ent.type			= TRACE_PRINT;
2997 	entry->ip			= ip;
2998 	entry->depth			= depth;
2999 
3000 	memcpy(&entry->buf, trace_buf, len);
3001 	entry->buf[len] = 0;
3002 	ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
3003 
3004  out_unlock:
3005 	spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3006 	unpause_graph_tracing();
3007  out:
3008 	preempt_enable_notrace();
3009 
3010 	return len;
3011 }
3012 EXPORT_SYMBOL_GPL(trace_vprintk);
3013 
3014 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3015 {
3016 	int ret;
3017 	va_list ap;
3018 
3019 	if (!(trace_flags & TRACE_ITER_PRINTK))
3020 		return 0;
3021 
3022 	va_start(ap, fmt);
3023 	ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3024 	va_end(ap);
3025 	return ret;
3026 }
3027 EXPORT_SYMBOL_GPL(__ftrace_printk);
3028 
3029 static int trace_panic_handler(struct notifier_block *this,
3030 			       unsigned long event, void *unused)
3031 {
3032 	if (ftrace_dump_on_oops)
3033 		ftrace_dump();
3034 	return NOTIFY_OK;
3035 }
3036 
3037 static struct notifier_block trace_panic_notifier = {
3038 	.notifier_call  = trace_panic_handler,
3039 	.next           = NULL,
3040 	.priority       = 150   /* priority: INT_MAX >= x >= 0 */
3041 };
3042 
3043 static int trace_die_handler(struct notifier_block *self,
3044 			     unsigned long val,
3045 			     void *data)
3046 {
3047 	switch (val) {
3048 	case DIE_OOPS:
3049 		if (ftrace_dump_on_oops)
3050 			ftrace_dump();
3051 		break;
3052 	default:
3053 		break;
3054 	}
3055 	return NOTIFY_OK;
3056 }
3057 
3058 static struct notifier_block trace_die_notifier = {
3059 	.notifier_call = trace_die_handler,
3060 	.priority = 200
3061 };
3062 
3063 /*
3064  * printk is set to max of 1024, we really don't need it that big.
3065  * Nothing should be printing 1000 characters anyway.
3066  */
3067 #define TRACE_MAX_PRINT		1000
3068 
3069 /*
3070  * Define here KERN_TRACE so that we have one place to modify
3071  * it if we decide to change what log level the ftrace dump
3072  * should be at.
3073  */
3074 #define KERN_TRACE		KERN_INFO
3075 
3076 static void
3077 trace_printk_seq(struct trace_seq *s)
3078 {
3079 	/* Probably should print a warning here. */
3080 	if (s->len >= 1000)
3081 		s->len = 1000;
3082 
3083 	/* should be zero ended, but we are paranoid. */
3084 	s->buffer[s->len] = 0;
3085 
3086 	printk(KERN_TRACE "%s", s->buffer);
3087 
3088 	trace_seq_reset(s);
3089 }
3090 
3091 void ftrace_dump(void)
3092 {
3093 	static DEFINE_SPINLOCK(ftrace_dump_lock);
3094 	/* use static because iter can be a bit big for the stack */
3095 	static struct trace_iterator iter;
3096 	static cpumask_t mask;
3097 	static int dump_ran;
3098 	unsigned long flags;
3099 	int cnt = 0, cpu;
3100 
3101 	/* only one dump */
3102 	spin_lock_irqsave(&ftrace_dump_lock, flags);
3103 	if (dump_ran)
3104 		goto out;
3105 
3106 	dump_ran = 1;
3107 
3108 	/* No turning back! */
3109 	ftrace_kill();
3110 
3111 	for_each_tracing_cpu(cpu) {
3112 		atomic_inc(&global_trace.data[cpu]->disabled);
3113 	}
3114 
3115 	/* don't look at user memory in panic mode */
3116 	trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3117 
3118 	printk(KERN_TRACE "Dumping ftrace buffer:\n");
3119 
3120 	iter.tr = &global_trace;
3121 	iter.trace = current_trace;
3122 
3123 	/*
3124 	 * We need to stop all tracing on all CPUS to read the
3125 	 * the next buffer. This is a bit expensive, but is
3126 	 * not done often. We fill all what we can read,
3127 	 * and then release the locks again.
3128 	 */
3129 
3130 	cpus_clear(mask);
3131 
3132 	while (!trace_empty(&iter)) {
3133 
3134 		if (!cnt)
3135 			printk(KERN_TRACE "---------------------------------\n");
3136 
3137 		cnt++;
3138 
3139 		/* reset all but tr, trace, and overruns */
3140 		memset(&iter.seq, 0,
3141 		       sizeof(struct trace_iterator) -
3142 		       offsetof(struct trace_iterator, seq));
3143 		iter.iter_flags |= TRACE_FILE_LAT_FMT;
3144 		iter.pos = -1;
3145 
3146 		if (find_next_entry_inc(&iter) != NULL) {
3147 			print_trace_line(&iter);
3148 			trace_consume(&iter);
3149 		}
3150 
3151 		trace_printk_seq(&iter.seq);
3152 	}
3153 
3154 	if (!cnt)
3155 		printk(KERN_TRACE "   (ftrace buffer empty)\n");
3156 	else
3157 		printk(KERN_TRACE "---------------------------------\n");
3158 
3159  out:
3160 	spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3161 }
3162 
3163 __init static int tracer_alloc_buffers(void)
3164 {
3165 	struct trace_array_cpu *data;
3166 	int i;
3167 
3168 	/* TODO: make the number of buffers hot pluggable with CPUS */
3169 	tracing_buffer_mask = cpu_possible_map;
3170 
3171 	global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3172 						   TRACE_BUFFER_FLAGS);
3173 	if (!global_trace.buffer) {
3174 		printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3175 		WARN_ON(1);
3176 		return 0;
3177 	}
3178 	global_trace.entries = ring_buffer_size(global_trace.buffer);
3179 
3180 #ifdef CONFIG_TRACER_MAX_TRACE
3181 	max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3182 					     TRACE_BUFFER_FLAGS);
3183 	if (!max_tr.buffer) {
3184 		printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3185 		WARN_ON(1);
3186 		ring_buffer_free(global_trace.buffer);
3187 		return 0;
3188 	}
3189 	max_tr.entries = ring_buffer_size(max_tr.buffer);
3190 	WARN_ON(max_tr.entries != global_trace.entries);
3191 #endif
3192 
3193 	/* Allocate the first page for all buffers */
3194 	for_each_tracing_cpu(i) {
3195 		data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3196 		max_tr.data[i] = &per_cpu(max_data, i);
3197 	}
3198 
3199 	trace_init_cmdlines();
3200 
3201 	register_tracer(&nop_trace);
3202 #ifdef CONFIG_BOOT_TRACER
3203 	register_tracer(&boot_tracer);
3204 	current_trace = &boot_tracer;
3205 	current_trace->init(&global_trace);
3206 #else
3207 	current_trace = &nop_trace;
3208 #endif
3209 
3210 	/* All seems OK, enable tracing */
3211 	tracing_disabled = 0;
3212 
3213 	atomic_notifier_chain_register(&panic_notifier_list,
3214 				       &trace_panic_notifier);
3215 
3216 	register_die_notifier(&trace_die_notifier);
3217 
3218 	return 0;
3219 }
3220 early_initcall(tracer_alloc_buffers);
3221 fs_initcall(tracer_init_debugfs);
3222