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