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