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