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