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