]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/trace/trace.c
tracing: identify which executable object the userspace address belongs to
[mirror_ubuntu-artful-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{
951 struct userstack_entry *entry;
952 struct stack_trace trace;
953 struct ring_buffer_event *event;
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)
1474 ret = trace_seq_printf(s, "[+0x%lx]",
1475 ip - vmstart);
1476 }
1477 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
1478 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1479 return ret;
1480}
1481
02b67518
TE
1482static int
1483seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
b54d3de9 1484 unsigned long sym_flags)
02b67518 1485{
b54d3de9 1486 struct mm_struct *mm = NULL;
02b67518
TE
1487 int ret = 1;
1488 unsigned i;
1489
b54d3de9
TE
1490 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
1491 struct task_struct *task;
1492 /*
1493 * we do the lookup on the thread group leader,
1494 * since individual threads might have already quit!
1495 */
1496 rcu_read_lock();
1497 task = find_task_by_vpid(entry->ent.tgid);
1498 rcu_read_unlock();
1499
1500 if (task)
1501 mm = get_task_mm(task);
1502 }
1503
02b67518
TE
1504 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1505 unsigned long ip = entry->caller[i];
1506
1507 if (ip == ULONG_MAX || !ret)
1508 break;
b54d3de9 1509 if (i && ret)
02b67518
TE
1510 ret = trace_seq_puts(s, " <- ");
1511 if (!ip) {
b54d3de9
TE
1512 if (ret)
1513 ret = trace_seq_puts(s, "??");
02b67518
TE
1514 continue;
1515 }
b54d3de9
TE
1516 if (!ret)
1517 break;
1518 if (ret)
1519 ret = seq_print_user_ip(s, mm, ip, sym_flags);
02b67518
TE
1520 }
1521
b54d3de9
TE
1522 if (mm)
1523 mmput(mm);
02b67518
TE
1524 return ret;
1525}
1526
e309b41d 1527static void print_lat_help_header(struct seq_file *m)
bc0c38d1 1528{
a6168353
ME
1529 seq_puts(m, "# _------=> CPU# \n");
1530 seq_puts(m, "# / _-----=> irqs-off \n");
1531 seq_puts(m, "# | / _----=> need-resched \n");
1532 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1533 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1534 seq_puts(m, "# |||| / \n");
1535 seq_puts(m, "# ||||| delay \n");
1536 seq_puts(m, "# cmd pid ||||| time | caller \n");
1537 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
1538}
1539
e309b41d 1540static void print_func_help_header(struct seq_file *m)
bc0c38d1 1541{
a6168353
ME
1542 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1543 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
1544}
1545
1546
e309b41d 1547static void
bc0c38d1
SR
1548print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1549{
1550 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1551 struct trace_array *tr = iter->tr;
1552 struct trace_array_cpu *data = tr->data[tr->cpu];
1553 struct tracer *type = current_trace;
3928a8a2
SR
1554 unsigned long total;
1555 unsigned long entries;
bc0c38d1
SR
1556 const char *name = "preemption";
1557
1558 if (type)
1559 name = type->name;
1560
3928a8a2
SR
1561 entries = ring_buffer_entries(iter->tr->buffer);
1562 total = entries +
1563 ring_buffer_overruns(iter->tr->buffer);
bc0c38d1
SR
1564
1565 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1566 name, UTS_RELEASE);
1567 seq_puts(m, "-----------------------------------"
1568 "---------------------------------\n");
1569 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1570 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1571 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1572 entries,
4c11d7ae 1573 total,
bc0c38d1
SR
1574 tr->cpu,
1575#if defined(CONFIG_PREEMPT_NONE)
1576 "server",
1577#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1578 "desktop",
b5c21b45 1579#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
1580 "preempt",
1581#else
1582 "unknown",
1583#endif
1584 /* These are reserved for later use */
1585 0, 0, 0, 0);
1586#ifdef CONFIG_SMP
1587 seq_printf(m, " #P:%d)\n", num_online_cpus());
1588#else
1589 seq_puts(m, ")\n");
1590#endif
1591 seq_puts(m, " -----------------\n");
1592 seq_printf(m, " | task: %.16s-%d "
1593 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1594 data->comm, data->pid, data->uid, data->nice,
1595 data->policy, data->rt_priority);
1596 seq_puts(m, " -----------------\n");
1597
1598 if (data->critical_start) {
1599 seq_puts(m, " => started at: ");
214023c3
SR
1600 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1601 trace_print_seq(m, &iter->seq);
bc0c38d1 1602 seq_puts(m, "\n => ended at: ");
214023c3
SR
1603 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1604 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1605 seq_puts(m, "\n");
1606 }
1607
1608 seq_puts(m, "\n");
1609}
1610
e309b41d 1611static void
214023c3 1612lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
bc0c38d1
SR
1613{
1614 int hardirq, softirq;
1615 char *comm;
1616
777e208d 1617 comm = trace_find_cmdline(entry->pid);
bc0c38d1 1618
777e208d 1619 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
a6168353 1620 trace_seq_printf(s, "%3d", cpu);
214023c3 1621 trace_seq_printf(s, "%c%c",
9244489a
SR
1622 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1623 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
777e208d 1624 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
bc0c38d1 1625
777e208d
SR
1626 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1627 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
afc2abc0 1628 if (hardirq && softirq) {
214023c3 1629 trace_seq_putc(s, 'H');
afc2abc0
IM
1630 } else {
1631 if (hardirq) {
214023c3 1632 trace_seq_putc(s, 'h');
afc2abc0 1633 } else {
bc0c38d1 1634 if (softirq)
214023c3 1635 trace_seq_putc(s, 's');
bc0c38d1 1636 else
214023c3 1637 trace_seq_putc(s, '.');
bc0c38d1
SR
1638 }
1639 }
1640
777e208d
SR
1641 if (entry->preempt_count)
1642 trace_seq_printf(s, "%x", entry->preempt_count);
bc0c38d1 1643 else
214023c3 1644 trace_seq_puts(s, ".");
bc0c38d1
SR
1645}
1646
1647unsigned long preempt_mark_thresh = 100;
1648
e309b41d 1649static void
3928a8a2 1650lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
bc0c38d1
SR
1651 unsigned long rel_usecs)
1652{
214023c3 1653 trace_seq_printf(s, " %4lldus", abs_usecs);
bc0c38d1 1654 if (rel_usecs > preempt_mark_thresh)
214023c3 1655 trace_seq_puts(s, "!: ");
bc0c38d1 1656 else if (rel_usecs > 1)
214023c3 1657 trace_seq_puts(s, "+: ");
bc0c38d1 1658 else
214023c3 1659 trace_seq_puts(s, " : ");
bc0c38d1
SR
1660}
1661
1662static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1663
fc5e27ae
PP
1664/*
1665 * The message is supposed to contain an ending newline.
1666 * If the printing stops prematurely, try to add a newline of our own.
1667 */
1668void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
dd0e545f 1669{
dd0e545f 1670 struct trace_entry *ent;
777e208d 1671 struct trace_field_cont *cont;
fc5e27ae 1672 bool ok = true;
dd0e545f 1673
3928a8a2 1674 ent = peek_next_entry(iter, iter->cpu, NULL);
dd0e545f
SR
1675 if (!ent || ent->type != TRACE_CONT) {
1676 trace_seq_putc(s, '\n');
1677 return;
1678 }
1679
1680 do {
777e208d 1681 cont = (struct trace_field_cont *)ent;
fc5e27ae 1682 if (ok)
777e208d 1683 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
d769041f
SR
1684
1685 ftrace_disable_cpu();
1686
1687 if (iter->buffer_iter[iter->cpu])
1688 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1689 else
1690 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1691
1692 ftrace_enable_cpu();
1693
3928a8a2 1694 ent = peek_next_entry(iter, iter->cpu, NULL);
dd0e545f 1695 } while (ent && ent->type == TRACE_CONT);
fc5e27ae
PP
1696
1697 if (!ok)
1698 trace_seq_putc(s, '\n');
dd0e545f
SR
1699}
1700
a309720c
SR
1701static void test_cpu_buff_start(struct trace_iterator *iter)
1702{
1703 struct trace_seq *s = &iter->seq;
1704
12ef7d44
SR
1705 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1706 return;
1707
1708 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1709 return;
1710
a309720c
SR
1711 if (cpu_isset(iter->cpu, iter->started))
1712 return;
1713
1714 cpu_set(iter->cpu, iter->started);
1715 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1716}
1717
2c4f035f 1718static enum print_line_t
214023c3 1719print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
bc0c38d1 1720{
214023c3 1721 struct trace_seq *s = &iter->seq;
bc0c38d1 1722 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
3928a8a2 1723 struct trace_entry *next_entry;
bc0c38d1
SR
1724 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1725 struct trace_entry *entry = iter->ent;
1726 unsigned long abs_usecs;
1727 unsigned long rel_usecs;
3928a8a2 1728 u64 next_ts;
bc0c38d1 1729 char *comm;
bac524d3 1730 int S, T;
86387f7e 1731 int i;
d17d9691 1732 unsigned state;
bc0c38d1 1733
dd0e545f 1734 if (entry->type == TRACE_CONT)
2c4f035f 1735 return TRACE_TYPE_HANDLED;
dd0e545f 1736
a309720c
SR
1737 test_cpu_buff_start(iter);
1738
3928a8a2
SR
1739 next_entry = find_next_entry(iter, NULL, &next_ts);
1740 if (!next_entry)
1741 next_ts = iter->ts;
1742 rel_usecs = ns2usecs(next_ts - iter->ts);
1743 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
bc0c38d1
SR
1744
1745 if (verbose) {
777e208d 1746 comm = trace_find_cmdline(entry->pid);
a6168353 1747 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
214023c3
SR
1748 " %ld.%03ldms (+%ld.%03ldms): ",
1749 comm,
777e208d
SR
1750 entry->pid, cpu, entry->flags,
1751 entry->preempt_count, trace_idx,
3928a8a2 1752 ns2usecs(iter->ts),
214023c3
SR
1753 abs_usecs/1000,
1754 abs_usecs % 1000, rel_usecs/1000,
1755 rel_usecs % 1000);
bc0c38d1 1756 } else {
f29c73fe
IM
1757 lat_print_generic(s, entry, cpu);
1758 lat_print_timestamp(s, abs_usecs, rel_usecs);
bc0c38d1
SR
1759 }
1760 switch (entry->type) {
777e208d 1761 case TRACE_FN: {
7104f300
SR
1762 struct ftrace_entry *field;
1763
1764 trace_assign_type(field, entry);
777e208d
SR
1765
1766 seq_print_ip_sym(s, field->ip, sym_flags);
214023c3 1767 trace_seq_puts(s, " (");
b3aa5577 1768 seq_print_ip_sym(s, field->parent_ip, sym_flags);
214023c3 1769 trace_seq_puts(s, ")\n");
bc0c38d1 1770 break;
777e208d 1771 }
bc0c38d1 1772 case TRACE_CTX:
777e208d 1773 case TRACE_WAKE: {
7104f300
SR
1774 struct ctx_switch_entry *field;
1775
1776 trace_assign_type(field, entry);
777e208d
SR
1777
1778 T = field->next_state < sizeof(state_to_char) ?
1779 state_to_char[field->next_state] : 'X';
bac524d3 1780
777e208d
SR
1781 state = field->prev_state ?
1782 __ffs(field->prev_state) + 1 : 0;
d17d9691 1783 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
777e208d 1784 comm = trace_find_cmdline(field->next_pid);
80b5e940 1785 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
777e208d
SR
1786 field->prev_pid,
1787 field->prev_prio,
57422797 1788 S, entry->type == TRACE_CTX ? "==>" : " +",
777e208d
SR
1789 field->next_cpu,
1790 field->next_pid,
1791 field->next_prio,
bac524d3 1792 T, comm);
bc0c38d1 1793 break;
777e208d
SR
1794 }
1795 case TRACE_SPECIAL: {
7104f300
SR
1796 struct special_entry *field;
1797
1798 trace_assign_type(field, entry);
777e208d 1799
88a4216c 1800 trace_seq_printf(s, "# %ld %ld %ld\n",
777e208d
SR
1801 field->arg1,
1802 field->arg2,
1803 field->arg3);
f0a920d5 1804 break;
777e208d
SR
1805 }
1806 case TRACE_STACK: {
7104f300
SR
1807 struct stack_entry *field;
1808
1809 trace_assign_type(field, entry);
777e208d 1810
86387f7e
IM
1811 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1812 if (i)
1813 trace_seq_puts(s, " <= ");
777e208d 1814 seq_print_ip_sym(s, field->caller[i], sym_flags);
86387f7e
IM
1815 }
1816 trace_seq_puts(s, "\n");
1817 break;
777e208d
SR
1818 }
1819 case TRACE_PRINT: {
7104f300
SR
1820 struct print_entry *field;
1821
1822 trace_assign_type(field, entry);
777e208d
SR
1823
1824 seq_print_ip_sym(s, field->ip, sym_flags);
1825 trace_seq_printf(s, ": %s", field->buf);
1826 if (entry->flags & TRACE_FLAG_CONT)
dd0e545f
SR
1827 trace_seq_print_cont(s, iter);
1828 break;
777e208d 1829 }
9f029e83
SR
1830 case TRACE_BRANCH: {
1831 struct trace_branch *field;
52f232cb
SR
1832
1833 trace_assign_type(field, entry);
1834
1835 trace_seq_printf(s, "[%s] %s:%s:%d\n",
68d119f0 1836 field->correct ? " ok " : " MISS ",
52f232cb
SR
1837 field->func,
1838 field->file,
1839 field->line);
1840 break;
1841 }
02b67518
TE
1842 case TRACE_USER_STACK: {
1843 struct userstack_entry *field;
1844
1845 trace_assign_type(field, entry);
1846
1847 seq_print_userip_objs(field, s, sym_flags);
b54d3de9 1848 trace_seq_putc(s, '\n');
02b67518
TE
1849 break;
1850 }
89b2f978 1851 default:
214023c3 1852 trace_seq_printf(s, "Unknown type %d\n", entry->type);
bc0c38d1 1853 }
2c4f035f 1854 return TRACE_TYPE_HANDLED;
bc0c38d1
SR
1855}
1856
2c4f035f 1857static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1858{
214023c3 1859 struct trace_seq *s = &iter->seq;
bc0c38d1 1860 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1861 struct trace_entry *entry;
bc0c38d1
SR
1862 unsigned long usec_rem;
1863 unsigned long long t;
1864 unsigned long secs;
1865 char *comm;
b3806b43 1866 int ret;
bac524d3 1867 int S, T;
86387f7e 1868 int i;
bc0c38d1 1869
4e3c3333 1870 entry = iter->ent;
dd0e545f
SR
1871
1872 if (entry->type == TRACE_CONT)
2c4f035f 1873 return TRACE_TYPE_HANDLED;
dd0e545f 1874
a309720c
SR
1875 test_cpu_buff_start(iter);
1876
777e208d 1877 comm = trace_find_cmdline(iter->ent->pid);
bc0c38d1 1878
3928a8a2 1879 t = ns2usecs(iter->ts);
bc0c38d1
SR
1880 usec_rem = do_div(t, 1000000ULL);
1881 secs = (unsigned long)t;
1882
777e208d 1883 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
f29c73fe 1884 if (!ret)
2c4f035f 1885 return TRACE_TYPE_PARTIAL_LINE;
a6168353 1886 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
f29c73fe 1887 if (!ret)
2c4f035f 1888 return TRACE_TYPE_PARTIAL_LINE;
f29c73fe
IM
1889 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1890 if (!ret)
2c4f035f 1891 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
1892
1893 switch (entry->type) {
777e208d 1894 case TRACE_FN: {
7104f300
SR
1895 struct ftrace_entry *field;
1896
1897 trace_assign_type(field, entry);
777e208d
SR
1898
1899 ret = seq_print_ip_sym(s, field->ip, sym_flags);
b3806b43 1900 if (!ret)
2c4f035f 1901 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1 1902 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
777e208d 1903 field->parent_ip) {
b3806b43
SR
1904 ret = trace_seq_printf(s, " <-");
1905 if (!ret)
2c4f035f 1906 return TRACE_TYPE_PARTIAL_LINE;
b3aa5577
SR
1907 ret = seq_print_ip_sym(s,
1908 field->parent_ip,
1909 sym_flags);
b3806b43 1910 if (!ret)
2c4f035f 1911 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1 1912 }
b3806b43
SR
1913 ret = trace_seq_printf(s, "\n");
1914 if (!ret)
2c4f035f 1915 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1 1916 break;
777e208d 1917 }
bc0c38d1 1918 case TRACE_CTX:
777e208d 1919 case TRACE_WAKE: {
7104f300
SR
1920 struct ctx_switch_entry *field;
1921
1922 trace_assign_type(field, entry);
777e208d
SR
1923
1924 S = field->prev_state < sizeof(state_to_char) ?
1925 state_to_char[field->prev_state] : 'X';
1926 T = field->next_state < sizeof(state_to_char) ?
1927 state_to_char[field->next_state] : 'X';
80b5e940 1928 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
777e208d
SR
1929 field->prev_pid,
1930 field->prev_prio,
b3806b43 1931 S,
57422797 1932 entry->type == TRACE_CTX ? "==>" : " +",
777e208d
SR
1933 field->next_cpu,
1934 field->next_pid,
1935 field->next_prio,
bac524d3 1936 T);
b3806b43 1937 if (!ret)
2c4f035f 1938 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1 1939 break;
777e208d
SR
1940 }
1941 case TRACE_SPECIAL: {
7104f300
SR
1942 struct special_entry *field;
1943
1944 trace_assign_type(field, entry);
777e208d 1945
88a4216c 1946 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
777e208d
SR
1947 field->arg1,
1948 field->arg2,
1949 field->arg3);
f0a920d5 1950 if (!ret)
2c4f035f 1951 return TRACE_TYPE_PARTIAL_LINE;
f0a920d5 1952 break;
777e208d
SR
1953 }
1954 case TRACE_STACK: {
7104f300
SR
1955 struct stack_entry *field;
1956
1957 trace_assign_type(field, entry);
777e208d 1958
86387f7e
IM
1959 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1960 if (i) {
1961 ret = trace_seq_puts(s, " <= ");
1962 if (!ret)
2c4f035f 1963 return TRACE_TYPE_PARTIAL_LINE;
86387f7e 1964 }
777e208d 1965 ret = seq_print_ip_sym(s, field->caller[i],
86387f7e
IM
1966 sym_flags);
1967 if (!ret)
2c4f035f 1968 return TRACE_TYPE_PARTIAL_LINE;
86387f7e
IM
1969 }
1970 ret = trace_seq_puts(s, "\n");
1971 if (!ret)
2c4f035f 1972 return TRACE_TYPE_PARTIAL_LINE;
86387f7e 1973 break;
777e208d
SR
1974 }
1975 case TRACE_PRINT: {
7104f300
SR
1976 struct print_entry *field;
1977
1978 trace_assign_type(field, entry);
777e208d
SR
1979
1980 seq_print_ip_sym(s, field->ip, sym_flags);
1981 trace_seq_printf(s, ": %s", field->buf);
1982 if (entry->flags & TRACE_FLAG_CONT)
dd0e545f
SR
1983 trace_seq_print_cont(s, iter);
1984 break;
bc0c38d1 1985 }
15e6cb36
FW
1986 case TRACE_FN_RET: {
1987 return print_return_function(iter);
1988 break;
1989 }
9f029e83
SR
1990 case TRACE_BRANCH: {
1991 struct trace_branch *field;
52f232cb
SR
1992
1993 trace_assign_type(field, entry);
1994
1995 trace_seq_printf(s, "[%s] %s:%s:%d\n",
68d119f0 1996 field->correct ? " ok " : " MISS ",
52f232cb
SR
1997 field->func,
1998 field->file,
1999 field->line);
2000 break;
2001 }
02b67518
TE
2002 case TRACE_USER_STACK: {
2003 struct userstack_entry *field;
2004
2005 trace_assign_type(field, entry);
2006
2007 ret = seq_print_userip_objs(field, s, sym_flags);
2008 if (!ret)
2009 return TRACE_TYPE_PARTIAL_LINE;
2010 ret = trace_seq_putc(s, '\n');
2011 if (!ret)
2012 return TRACE_TYPE_PARTIAL_LINE;
2013 break;
2014 }
777e208d 2015 }
2c4f035f 2016 return TRACE_TYPE_HANDLED;
bc0c38d1
SR
2017}
2018
2c4f035f 2019static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2020{
2021 struct trace_seq *s = &iter->seq;
2022 struct trace_entry *entry;
2023 int ret;
bac524d3 2024 int S, T;
f9896bf3
IM
2025
2026 entry = iter->ent;
dd0e545f
SR
2027
2028 if (entry->type == TRACE_CONT)
2c4f035f 2029 return TRACE_TYPE_HANDLED;
dd0e545f 2030
f9896bf3 2031 ret = trace_seq_printf(s, "%d %d %llu ",
777e208d 2032 entry->pid, iter->cpu, iter->ts);
f9896bf3 2033 if (!ret)
2c4f035f 2034 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2035
2036 switch (entry->type) {
777e208d 2037 case TRACE_FN: {
7104f300
SR
2038 struct ftrace_entry *field;
2039
2040 trace_assign_type(field, entry);
777e208d 2041
f9896bf3 2042 ret = trace_seq_printf(s, "%x %x\n",
777e208d
SR
2043 field->ip,
2044 field->parent_ip);
f9896bf3 2045 if (!ret)
2c4f035f 2046 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3 2047 break;
777e208d 2048 }
f9896bf3 2049 case TRACE_CTX:
777e208d 2050 case TRACE_WAKE: {
7104f300
SR
2051 struct ctx_switch_entry *field;
2052
2053 trace_assign_type(field, entry);
777e208d
SR
2054
2055 S = field->prev_state < sizeof(state_to_char) ?
2056 state_to_char[field->prev_state] : 'X';
2057 T = field->next_state < sizeof(state_to_char) ?
2058 state_to_char[field->next_state] : 'X';
57422797
IM
2059 if (entry->type == TRACE_WAKE)
2060 S = '+';
80b5e940 2061 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
777e208d
SR
2062 field->prev_pid,
2063 field->prev_prio,
f9896bf3 2064 S,
777e208d
SR
2065 field->next_cpu,
2066 field->next_pid,
2067 field->next_prio,
bac524d3 2068 T);
f9896bf3 2069 if (!ret)
2c4f035f 2070 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3 2071 break;
777e208d 2072 }
f0a920d5 2073 case TRACE_SPECIAL:
02b67518 2074 case TRACE_USER_STACK:
777e208d 2075 case TRACE_STACK: {
7104f300
SR
2076 struct special_entry *field;
2077
2078 trace_assign_type(field, entry);
777e208d 2079
88a4216c 2080 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
777e208d
SR
2081 field->arg1,
2082 field->arg2,
2083 field->arg3);
f0a920d5 2084 if (!ret)
2c4f035f 2085 return TRACE_TYPE_PARTIAL_LINE;
f0a920d5 2086 break;
777e208d
SR
2087 }
2088 case TRACE_PRINT: {
7104f300
SR
2089 struct print_entry *field;
2090
2091 trace_assign_type(field, entry);
777e208d
SR
2092
2093 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
2094 if (entry->flags & TRACE_FLAG_CONT)
dd0e545f
SR
2095 trace_seq_print_cont(s, iter);
2096 break;
f9896bf3 2097 }
777e208d 2098 }
2c4f035f 2099 return TRACE_TYPE_HANDLED;
f9896bf3
IM
2100}
2101
cb0f12aa
IM
2102#define SEQ_PUT_FIELD_RET(s, x) \
2103do { \
2104 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2105 return 0; \
2106} while (0)
2107
5e3ca0ec
IM
2108#define SEQ_PUT_HEX_FIELD_RET(s, x) \
2109do { \
ad0a3b68 2110 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
5e3ca0ec
IM
2111 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2112 return 0; \
2113} while (0)
2114
2c4f035f 2115static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2116{
2117 struct trace_seq *s = &iter->seq;
2118 unsigned char newline = '\n';
2119 struct trace_entry *entry;
bac524d3 2120 int S, T;
5e3ca0ec
IM
2121
2122 entry = iter->ent;
dd0e545f
SR
2123
2124 if (entry->type == TRACE_CONT)
2c4f035f 2125 return TRACE_TYPE_HANDLED;
dd0e545f 2126
777e208d 2127 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
5e3ca0ec 2128 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
3928a8a2 2129 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
5e3ca0ec
IM
2130
2131 switch (entry->type) {
777e208d 2132 case TRACE_FN: {
7104f300
SR
2133 struct ftrace_entry *field;
2134
2135 trace_assign_type(field, entry);
777e208d
SR
2136
2137 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
2138 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
5e3ca0ec 2139 break;
777e208d 2140 }
5e3ca0ec 2141 case TRACE_CTX:
777e208d 2142 case TRACE_WAKE: {
7104f300
SR
2143 struct ctx_switch_entry *field;
2144
2145 trace_assign_type(field, entry);
777e208d
SR
2146
2147 S = field->prev_state < sizeof(state_to_char) ?
2148 state_to_char[field->prev_state] : 'X';
2149 T = field->next_state < sizeof(state_to_char) ?
2150 state_to_char[field->next_state] : 'X';
57422797
IM
2151 if (entry->type == TRACE_WAKE)
2152 S = '+';
777e208d
SR
2153 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
2154 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
5e3ca0ec 2155 SEQ_PUT_HEX_FIELD_RET(s, S);
777e208d
SR
2156 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
2157 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
2158 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
bac524d3 2159 SEQ_PUT_HEX_FIELD_RET(s, T);
5e3ca0ec 2160 break;
777e208d 2161 }
5e3ca0ec 2162 case TRACE_SPECIAL:
02b67518 2163 case TRACE_USER_STACK:
777e208d 2164 case TRACE_STACK: {
7104f300
SR
2165 struct special_entry *field;
2166
2167 trace_assign_type(field, entry);
777e208d
SR
2168
2169 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
2170 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
2171 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
5e3ca0ec
IM
2172 break;
2173 }
777e208d 2174 }
5e3ca0ec
IM
2175 SEQ_PUT_FIELD_RET(s, newline);
2176
2c4f035f 2177 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2178}
2179
2c4f035f 2180static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2181{
2182 struct trace_seq *s = &iter->seq;
2183 struct trace_entry *entry;
2184
2185 entry = iter->ent;
dd0e545f
SR
2186
2187 if (entry->type == TRACE_CONT)
2c4f035f 2188 return TRACE_TYPE_HANDLED;
dd0e545f 2189
777e208d 2190 SEQ_PUT_FIELD_RET(s, entry->pid);
072ba498 2191 SEQ_PUT_FIELD_RET(s, entry->cpu);
3928a8a2 2192 SEQ_PUT_FIELD_RET(s, iter->ts);
cb0f12aa
IM
2193
2194 switch (entry->type) {
777e208d 2195 case TRACE_FN: {
7104f300
SR
2196 struct ftrace_entry *field;
2197
2198 trace_assign_type(field, entry);
777e208d
SR
2199
2200 SEQ_PUT_FIELD_RET(s, field->ip);
2201 SEQ_PUT_FIELD_RET(s, field->parent_ip);
cb0f12aa 2202 break;
777e208d
SR
2203 }
2204 case TRACE_CTX: {
7104f300
SR
2205 struct ctx_switch_entry *field;
2206
2207 trace_assign_type(field, entry);
777e208d
SR
2208
2209 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2210 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2211 SEQ_PUT_FIELD_RET(s, field->prev_state);
2212 SEQ_PUT_FIELD_RET(s, field->next_pid);
2213 SEQ_PUT_FIELD_RET(s, field->next_prio);
2214 SEQ_PUT_FIELD_RET(s, field->next_state);
cb0f12aa 2215 break;
777e208d 2216 }
f0a920d5 2217 case TRACE_SPECIAL:
02b67518 2218 case TRACE_USER_STACK:
777e208d 2219 case TRACE_STACK: {
7104f300
SR
2220 struct special_entry *field;
2221
2222 trace_assign_type(field, entry);
777e208d
SR
2223
2224 SEQ_PUT_FIELD_RET(s, field->arg1);
2225 SEQ_PUT_FIELD_RET(s, field->arg2);
2226 SEQ_PUT_FIELD_RET(s, field->arg3);
f0a920d5 2227 break;
cb0f12aa 2228 }
777e208d 2229 }
cb0f12aa
IM
2230 return 1;
2231}
2232
bc0c38d1
SR
2233static int trace_empty(struct trace_iterator *iter)
2234{
bc0c38d1
SR
2235 int cpu;
2236
ab46428c 2237 for_each_tracing_cpu(cpu) {
d769041f
SR
2238 if (iter->buffer_iter[cpu]) {
2239 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2240 return 0;
2241 } else {
2242 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2243 return 0;
2244 }
bc0c38d1 2245 }
d769041f 2246
797d3712 2247 return 1;
bc0c38d1
SR
2248}
2249
2c4f035f 2250static enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2251{
2c4f035f
FW
2252 enum print_line_t ret;
2253
2254 if (iter->trace && iter->trace->print_line) {
2255 ret = iter->trace->print_line(iter);
2256 if (ret != TRACE_TYPE_UNHANDLED)
2257 return ret;
2258 }
72829bc3 2259
cb0f12aa
IM
2260 if (trace_flags & TRACE_ITER_BIN)
2261 return print_bin_fmt(iter);
2262
5e3ca0ec
IM
2263 if (trace_flags & TRACE_ITER_HEX)
2264 return print_hex_fmt(iter);
2265
f9896bf3
IM
2266 if (trace_flags & TRACE_ITER_RAW)
2267 return print_raw_fmt(iter);
2268
2269 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2270 return print_lat_fmt(iter, iter->idx, iter->cpu);
2271
2272 return print_trace_fmt(iter);
2273}
2274
bc0c38d1
SR
2275static int s_show(struct seq_file *m, void *v)
2276{
2277 struct trace_iterator *iter = v;
2278
2279 if (iter->ent == NULL) {
2280 if (iter->tr) {
2281 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2282 seq_puts(m, "#\n");
2283 }
2284 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2285 /* print nothing if the buffers are empty */
2286 if (trace_empty(iter))
2287 return 0;
2288 print_trace_header(m, iter);
2289 if (!(trace_flags & TRACE_ITER_VERBOSE))
2290 print_lat_help_header(m);
2291 } else {
2292 if (!(trace_flags & TRACE_ITER_VERBOSE))
2293 print_func_help_header(m);
2294 }
2295 } else {
f9896bf3 2296 print_trace_line(iter);
214023c3 2297 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
2298 }
2299
2300 return 0;
2301}
2302
2303static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2304 .start = s_start,
2305 .next = s_next,
2306 .stop = s_stop,
2307 .show = s_show,
bc0c38d1
SR
2308};
2309
e309b41d 2310static struct trace_iterator *
bc0c38d1
SR
2311__tracing_open(struct inode *inode, struct file *file, int *ret)
2312{
2313 struct trace_iterator *iter;
3928a8a2
SR
2314 struct seq_file *m;
2315 int cpu;
bc0c38d1 2316
60a11774
SR
2317 if (tracing_disabled) {
2318 *ret = -ENODEV;
2319 return NULL;
2320 }
2321
bc0c38d1
SR
2322 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2323 if (!iter) {
2324 *ret = -ENOMEM;
2325 goto out;
2326 }
2327
2328 mutex_lock(&trace_types_lock);
2329 if (current_trace && current_trace->print_max)
2330 iter->tr = &max_tr;
2331 else
2332 iter->tr = inode->i_private;
2333 iter->trace = current_trace;
2334 iter->pos = -1;
2335
12ef7d44
SR
2336 /* Annotate start of buffers if we had overruns */
2337 if (ring_buffer_overruns(iter->tr->buffer))
2338 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2339
2340
3928a8a2 2341 for_each_tracing_cpu(cpu) {
d769041f 2342
3928a8a2
SR
2343 iter->buffer_iter[cpu] =
2344 ring_buffer_read_start(iter->tr->buffer, cpu);
d769041f 2345
3928a8a2
SR
2346 if (!iter->buffer_iter[cpu])
2347 goto fail_buffer;
2348 }
2349
bc0c38d1
SR
2350 /* TODO stop tracer */
2351 *ret = seq_open(file, &tracer_seq_ops);
3928a8a2
SR
2352 if (*ret)
2353 goto fail_buffer;
bc0c38d1 2354
3928a8a2
SR
2355 m = file->private_data;
2356 m->private = iter;
bc0c38d1 2357
3928a8a2 2358 /* stop the trace while dumping */
9036990d 2359 tracing_stop();
3928a8a2
SR
2360
2361 if (iter->trace && iter->trace->open)
2362 iter->trace->open(iter);
2363
bc0c38d1
SR
2364 mutex_unlock(&trace_types_lock);
2365
2366 out:
2367 return iter;
3928a8a2
SR
2368
2369 fail_buffer:
2370 for_each_tracing_cpu(cpu) {
2371 if (iter->buffer_iter[cpu])
2372 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2373 }
2374 mutex_unlock(&trace_types_lock);
0bb943c7 2375 kfree(iter);
3928a8a2
SR
2376
2377 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
2378}
2379
2380int tracing_open_generic(struct inode *inode, struct file *filp)
2381{
60a11774
SR
2382 if (tracing_disabled)
2383 return -ENODEV;
2384
bc0c38d1
SR
2385 filp->private_data = inode->i_private;
2386 return 0;
2387}
2388
2389int tracing_release(struct inode *inode, struct file *file)
2390{
2391 struct seq_file *m = (struct seq_file *)file->private_data;
2392 struct trace_iterator *iter = m->private;
3928a8a2 2393 int cpu;
bc0c38d1
SR
2394
2395 mutex_lock(&trace_types_lock);
3928a8a2
SR
2396 for_each_tracing_cpu(cpu) {
2397 if (iter->buffer_iter[cpu])
2398 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2399 }
2400
bc0c38d1
SR
2401 if (iter->trace && iter->trace->close)
2402 iter->trace->close(iter);
2403
2404 /* reenable tracing if it was previously enabled */
9036990d 2405 tracing_start();
bc0c38d1
SR
2406 mutex_unlock(&trace_types_lock);
2407
2408 seq_release(inode, file);
2409 kfree(iter);
2410 return 0;
2411}
2412
2413static int tracing_open(struct inode *inode, struct file *file)
2414{
2415 int ret;
2416
2417 __tracing_open(inode, file, &ret);
2418
2419 return ret;
2420}
2421
2422static int tracing_lt_open(struct inode *inode, struct file *file)
2423{
2424 struct trace_iterator *iter;
2425 int ret;
2426
2427 iter = __tracing_open(inode, file, &ret);
2428
2429 if (!ret)
2430 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2431
2432 return ret;
2433}
2434
2435
e309b41d 2436static void *
bc0c38d1
SR
2437t_next(struct seq_file *m, void *v, loff_t *pos)
2438{
2439 struct tracer *t = m->private;
2440
2441 (*pos)++;
2442
2443 if (t)
2444 t = t->next;
2445
2446 m->private = t;
2447
2448 return t;
2449}
2450
2451static void *t_start(struct seq_file *m, loff_t *pos)
2452{
2453 struct tracer *t = m->private;
2454 loff_t l = 0;
2455
2456 mutex_lock(&trace_types_lock);
2457 for (; t && l < *pos; t = t_next(m, t, &l))
2458 ;
2459
2460 return t;
2461}
2462
2463static void t_stop(struct seq_file *m, void *p)
2464{
2465 mutex_unlock(&trace_types_lock);
2466}
2467
2468static int t_show(struct seq_file *m, void *v)
2469{
2470 struct tracer *t = v;
2471
2472 if (!t)
2473 return 0;
2474
2475 seq_printf(m, "%s", t->name);
2476 if (t->next)
2477 seq_putc(m, ' ');
2478 else
2479 seq_putc(m, '\n');
2480
2481 return 0;
2482}
2483
2484static struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2485 .start = t_start,
2486 .next = t_next,
2487 .stop = t_stop,
2488 .show = t_show,
bc0c38d1
SR
2489};
2490
2491static int show_traces_open(struct inode *inode, struct file *file)
2492{
2493 int ret;
2494
60a11774
SR
2495 if (tracing_disabled)
2496 return -ENODEV;
2497
bc0c38d1
SR
2498 ret = seq_open(file, &show_traces_seq_ops);
2499 if (!ret) {
2500 struct seq_file *m = file->private_data;
2501 m->private = trace_types;
2502 }
2503
2504 return ret;
2505}
2506
2507static struct file_operations tracing_fops = {
4bf39a94
IM
2508 .open = tracing_open,
2509 .read = seq_read,
2510 .llseek = seq_lseek,
2511 .release = tracing_release,
bc0c38d1
SR
2512};
2513
2514static struct file_operations tracing_lt_fops = {
4bf39a94
IM
2515 .open = tracing_lt_open,
2516 .read = seq_read,
2517 .llseek = seq_lseek,
2518 .release = tracing_release,
bc0c38d1
SR
2519};
2520
2521static struct file_operations show_traces_fops = {
c7078de1
IM
2522 .open = show_traces_open,
2523 .read = seq_read,
2524 .release = seq_release,
2525};
2526
36dfe925
IM
2527/*
2528 * Only trace on a CPU if the bitmask is set:
2529 */
2530static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2531
2532/*
2533 * When tracing/tracing_cpu_mask is modified then this holds
2534 * the new bitmask we are about to install:
2535 */
2536static cpumask_t tracing_cpumask_new;
2537
2538/*
2539 * The tracer itself will not take this lock, but still we want
2540 * to provide a consistent cpumask to user-space:
2541 */
2542static DEFINE_MUTEX(tracing_cpumask_update_lock);
2543
2544/*
2545 * Temporary storage for the character representation of the
2546 * CPU bitmask (and one more byte for the newline):
2547 */
2548static char mask_str[NR_CPUS + 1];
2549
c7078de1
IM
2550static ssize_t
2551tracing_cpumask_read(struct file *filp, char __user *ubuf,
2552 size_t count, loff_t *ppos)
2553{
36dfe925 2554 int len;
c7078de1
IM
2555
2556 mutex_lock(&tracing_cpumask_update_lock);
36dfe925
IM
2557
2558 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2559 if (count - len < 2) {
2560 count = -EINVAL;
2561 goto out_err;
2562 }
2563 len += sprintf(mask_str + len, "\n");
2564 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2565
2566out_err:
c7078de1
IM
2567 mutex_unlock(&tracing_cpumask_update_lock);
2568
2569 return count;
2570}
2571
2572static ssize_t
2573tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2574 size_t count, loff_t *ppos)
2575{
36dfe925 2576 int err, cpu;
c7078de1
IM
2577
2578 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2579 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2580 if (err)
36dfe925
IM
2581 goto err_unlock;
2582
92205c23
SR
2583 raw_local_irq_disable();
2584 __raw_spin_lock(&ftrace_max_lock);
ab46428c 2585 for_each_tracing_cpu(cpu) {
36dfe925
IM
2586 /*
2587 * Increase/decrease the disabled counter if we are
2588 * about to flip a bit in the cpumask:
2589 */
2590 if (cpu_isset(cpu, tracing_cpumask) &&
2591 !cpu_isset(cpu, tracing_cpumask_new)) {
2592 atomic_inc(&global_trace.data[cpu]->disabled);
2593 }
2594 if (!cpu_isset(cpu, tracing_cpumask) &&
2595 cpu_isset(cpu, tracing_cpumask_new)) {
2596 atomic_dec(&global_trace.data[cpu]->disabled);
2597 }
2598 }
92205c23
SR
2599 __raw_spin_unlock(&ftrace_max_lock);
2600 raw_local_irq_enable();
36dfe925
IM
2601
2602 tracing_cpumask = tracing_cpumask_new;
2603
2604 mutex_unlock(&tracing_cpumask_update_lock);
c7078de1
IM
2605
2606 return count;
36dfe925
IM
2607
2608err_unlock:
2609 mutex_unlock(&tracing_cpumask_update_lock);
2610
2611 return err;
c7078de1
IM
2612}
2613
2614static struct file_operations tracing_cpumask_fops = {
2615 .open = tracing_open_generic,
2616 .read = tracing_cpumask_read,
2617 .write = tracing_cpumask_write,
bc0c38d1
SR
2618};
2619
2620static ssize_t
ee6bce52 2621tracing_trace_options_read(struct file *filp, char __user *ubuf,
bc0c38d1
SR
2622 size_t cnt, loff_t *ppos)
2623{
adf9f195 2624 int i;
bc0c38d1
SR
2625 char *buf;
2626 int r = 0;
2627 int len = 0;
adf9f195
FW
2628 u32 tracer_flags = current_trace->flags->val;
2629 struct tracer_opt *trace_opts = current_trace->flags->opts;
2630
bc0c38d1
SR
2631
2632 /* calulate max size */
2633 for (i = 0; trace_options[i]; i++) {
2634 len += strlen(trace_options[i]);
2635 len += 3; /* "no" and space */
2636 }
2637
adf9f195
FW
2638 /*
2639 * Increase the size with names of options specific
2640 * of the current tracer.
2641 */
2642 for (i = 0; trace_opts[i].name; i++) {
2643 len += strlen(trace_opts[i].name);
2644 len += 3; /* "no" and space */
2645 }
2646
bc0c38d1
SR
2647 /* +2 for \n and \0 */
2648 buf = kmalloc(len + 2, GFP_KERNEL);
2649 if (!buf)
2650 return -ENOMEM;
2651
2652 for (i = 0; trace_options[i]; i++) {
2653 if (trace_flags & (1 << i))
2654 r += sprintf(buf + r, "%s ", trace_options[i]);
2655 else
2656 r += sprintf(buf + r, "no%s ", trace_options[i]);
2657 }
2658
adf9f195
FW
2659 for (i = 0; trace_opts[i].name; i++) {
2660 if (tracer_flags & trace_opts[i].bit)
2661 r += sprintf(buf + r, "%s ",
2662 trace_opts[i].name);
2663 else
2664 r += sprintf(buf + r, "no%s ",
2665 trace_opts[i].name);
2666 }
2667
bc0c38d1
SR
2668 r += sprintf(buf + r, "\n");
2669 WARN_ON(r >= len + 2);
2670
36dfe925 2671 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2672
2673 kfree(buf);
2674
2675 return r;
2676}
2677
adf9f195
FW
2678/* Try to assign a tracer specific option */
2679static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2680{
2681 struct tracer_flags *trace_flags = trace->flags;
2682 struct tracer_opt *opts = NULL;
2683 int ret = 0, i = 0;
2684 int len;
2685
2686 for (i = 0; trace_flags->opts[i].name; i++) {
2687 opts = &trace_flags->opts[i];
2688 len = strlen(opts->name);
2689
2690 if (strncmp(cmp, opts->name, len) == 0) {
2691 ret = trace->set_flag(trace_flags->val,
2692 opts->bit, !neg);
2693 break;
2694 }
2695 }
2696 /* Not found */
2697 if (!trace_flags->opts[i].name)
2698 return -EINVAL;
2699
2700 /* Refused to handle */
2701 if (ret)
2702 return ret;
2703
2704 if (neg)
2705 trace_flags->val &= ~opts->bit;
2706 else
2707 trace_flags->val |= opts->bit;
2708
2709 return 0;
2710}
2711
bc0c38d1 2712static ssize_t
ee6bce52 2713tracing_trace_options_write(struct file *filp, const char __user *ubuf,
bc0c38d1
SR
2714 size_t cnt, loff_t *ppos)
2715{
2716 char buf[64];
2717 char *cmp = buf;
2718 int neg = 0;
adf9f195 2719 int ret;
bc0c38d1
SR
2720 int i;
2721
cffae437
SR
2722 if (cnt >= sizeof(buf))
2723 return -EINVAL;
bc0c38d1
SR
2724
2725 if (copy_from_user(&buf, ubuf, cnt))
2726 return -EFAULT;
2727
2728 buf[cnt] = 0;
2729
2730 if (strncmp(buf, "no", 2) == 0) {
2731 neg = 1;
2732 cmp += 2;
2733 }
2734
2735 for (i = 0; trace_options[i]; i++) {
2736 int len = strlen(trace_options[i]);
2737
2738 if (strncmp(cmp, trace_options[i], len) == 0) {
2739 if (neg)
2740 trace_flags &= ~(1 << i);
2741 else
2742 trace_flags |= (1 << i);
2743 break;
2744 }
2745 }
adf9f195
FW
2746
2747 /* If no option could be set, test the specific tracer options */
2748 if (!trace_options[i]) {
2749 ret = set_tracer_option(current_trace, cmp, neg);
2750 if (ret)
2751 return ret;
2752 }
bc0c38d1
SR
2753
2754 filp->f_pos += cnt;
2755
2756 return cnt;
2757}
2758
2759static struct file_operations tracing_iter_fops = {
c7078de1 2760 .open = tracing_open_generic,
ee6bce52
SR
2761 .read = tracing_trace_options_read,
2762 .write = tracing_trace_options_write,
bc0c38d1
SR
2763};
2764
7bd2f24c
IM
2765static const char readme_msg[] =
2766 "tracing mini-HOWTO:\n\n"
2767 "# mkdir /debug\n"
2768 "# mount -t debugfs nodev /debug\n\n"
2769 "# cat /debug/tracing/available_tracers\n"
2770 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2771 "# cat /debug/tracing/current_tracer\n"
2772 "none\n"
2773 "# echo sched_switch > /debug/tracing/current_tracer\n"
2774 "# cat /debug/tracing/current_tracer\n"
2775 "sched_switch\n"
ee6bce52 2776 "# cat /debug/tracing/trace_options\n"
7bd2f24c 2777 "noprint-parent nosym-offset nosym-addr noverbose\n"
ee6bce52 2778 "# echo print-parent > /debug/tracing/trace_options\n"
7bd2f24c
IM
2779 "# echo 1 > /debug/tracing/tracing_enabled\n"
2780 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2781 "echo 0 > /debug/tracing/tracing_enabled\n"
2782;
2783
2784static ssize_t
2785tracing_readme_read(struct file *filp, char __user *ubuf,
2786 size_t cnt, loff_t *ppos)
2787{
2788 return simple_read_from_buffer(ubuf, cnt, ppos,
2789 readme_msg, strlen(readme_msg));
2790}
2791
2792static struct file_operations tracing_readme_fops = {
c7078de1
IM
2793 .open = tracing_open_generic,
2794 .read = tracing_readme_read,
7bd2f24c
IM
2795};
2796
bc0c38d1
SR
2797static ssize_t
2798tracing_ctrl_read(struct file *filp, char __user *ubuf,
2799 size_t cnt, loff_t *ppos)
2800{
bc0c38d1
SR
2801 char buf[64];
2802 int r;
2803
9036990d 2804 r = sprintf(buf, "%u\n", tracer_enabled);
4e3c3333 2805 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2806}
2807
2808static ssize_t
2809tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2810 size_t cnt, loff_t *ppos)
2811{
2812 struct trace_array *tr = filp->private_data;
bc0c38d1 2813 char buf[64];
c6caeeb1
SR
2814 long val;
2815 int ret;
bc0c38d1 2816
cffae437
SR
2817 if (cnt >= sizeof(buf))
2818 return -EINVAL;
bc0c38d1
SR
2819
2820 if (copy_from_user(&buf, ubuf, cnt))
2821 return -EFAULT;
2822
2823 buf[cnt] = 0;
2824
c6caeeb1
SR
2825 ret = strict_strtoul(buf, 10, &val);
2826 if (ret < 0)
2827 return ret;
bc0c38d1
SR
2828
2829 val = !!val;
2830
2831 mutex_lock(&trace_types_lock);
9036990d
SR
2832 if (tracer_enabled ^ val) {
2833 if (val) {
bc0c38d1 2834 tracer_enabled = 1;
9036990d
SR
2835 if (current_trace->start)
2836 current_trace->start(tr);
2837 tracing_start();
2838 } else {
bc0c38d1 2839 tracer_enabled = 0;
9036990d
SR
2840 tracing_stop();
2841 if (current_trace->stop)
2842 current_trace->stop(tr);
2843 }
bc0c38d1
SR
2844 }
2845 mutex_unlock(&trace_types_lock);
2846
2847 filp->f_pos += cnt;
2848
2849 return cnt;
2850}
2851
2852static ssize_t
2853tracing_set_trace_read(struct file *filp, char __user *ubuf,
2854 size_t cnt, loff_t *ppos)
2855{
2856 char buf[max_tracer_type_len+2];
2857 int r;
2858
2859 mutex_lock(&trace_types_lock);
2860 if (current_trace)
2861 r = sprintf(buf, "%s\n", current_trace->name);
2862 else
2863 r = sprintf(buf, "\n");
2864 mutex_unlock(&trace_types_lock);
2865
4bf39a94 2866 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2867}
2868
d9e54076 2869static int tracing_set_tracer(char *buf)
bc0c38d1
SR
2870{
2871 struct trace_array *tr = &global_trace;
2872 struct tracer *t;
d9e54076 2873 int ret = 0;
bc0c38d1
SR
2874
2875 mutex_lock(&trace_types_lock);
2876 for (t = trace_types; t; t = t->next) {
2877 if (strcmp(t->name, buf) == 0)
2878 break;
2879 }
c2931e05
FW
2880 if (!t) {
2881 ret = -EINVAL;
2882 goto out;
2883 }
2884 if (t == current_trace)
bc0c38d1
SR
2885 goto out;
2886
9f029e83 2887 trace_branch_disable();
bc0c38d1
SR
2888 if (current_trace && current_trace->reset)
2889 current_trace->reset(tr);
2890
2891 current_trace = t;
1c80025a
FW
2892 if (t->init) {
2893 ret = t->init(tr);
2894 if (ret)
2895 goto out;
2896 }
bc0c38d1 2897
9f029e83 2898 trace_branch_enable(tr);
bc0c38d1
SR
2899 out:
2900 mutex_unlock(&trace_types_lock);
2901
d9e54076
PZ
2902 return ret;
2903}
2904
2905static ssize_t
2906tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2907 size_t cnt, loff_t *ppos)
2908{
2909 char buf[max_tracer_type_len+1];
2910 int i;
2911 size_t ret;
e6e7a65a
FW
2912 int err;
2913
2914 ret = cnt;
d9e54076
PZ
2915
2916 if (cnt > max_tracer_type_len)
2917 cnt = max_tracer_type_len;
2918
2919 if (copy_from_user(&buf, ubuf, cnt))
2920 return -EFAULT;
2921
2922 buf[cnt] = 0;
2923
2924 /* strip ending whitespace. */
2925 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2926 buf[i] = 0;
2927
e6e7a65a
FW
2928 err = tracing_set_tracer(buf);
2929 if (err)
2930 return err;
d9e54076 2931
e6e7a65a 2932 filp->f_pos += ret;
bc0c38d1 2933
c2931e05 2934 return ret;
bc0c38d1
SR
2935}
2936
2937static ssize_t
2938tracing_max_lat_read(struct file *filp, char __user *ubuf,
2939 size_t cnt, loff_t *ppos)
2940{
2941 unsigned long *ptr = filp->private_data;
2942 char buf[64];
2943 int r;
2944
cffae437 2945 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 2946 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
2947 if (r > sizeof(buf))
2948 r = sizeof(buf);
4bf39a94 2949 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
2950}
2951
2952static ssize_t
2953tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2954 size_t cnt, loff_t *ppos)
2955{
2956 long *ptr = filp->private_data;
bc0c38d1 2957 char buf[64];
c6caeeb1
SR
2958 long val;
2959 int ret;
bc0c38d1 2960
cffae437
SR
2961 if (cnt >= sizeof(buf))
2962 return -EINVAL;
bc0c38d1
SR
2963
2964 if (copy_from_user(&buf, ubuf, cnt))
2965 return -EFAULT;
2966
2967 buf[cnt] = 0;
2968
c6caeeb1
SR
2969 ret = strict_strtoul(buf, 10, &val);
2970 if (ret < 0)
2971 return ret;
bc0c38d1
SR
2972
2973 *ptr = val * 1000;
2974
2975 return cnt;
2976}
2977
b3806b43
SR
2978static atomic_t tracing_reader;
2979
2980static int tracing_open_pipe(struct inode *inode, struct file *filp)
2981{
2982 struct trace_iterator *iter;
2983
2984 if (tracing_disabled)
2985 return -ENODEV;
2986
2987 /* We only allow for reader of the pipe */
2988 if (atomic_inc_return(&tracing_reader) != 1) {
2989 atomic_dec(&tracing_reader);
2990 return -EBUSY;
2991 }
2992
2993 /* create a buffer to store the information to pass to userspace */
2994 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2995 if (!iter)
2996 return -ENOMEM;
2997
107bad8b 2998 mutex_lock(&trace_types_lock);
a309720c
SR
2999
3000 /* trace pipe does not show start of buffer */
3001 cpus_setall(iter->started);
3002
b3806b43 3003 iter->tr = &global_trace;
72829bc3 3004 iter->trace = current_trace;
b3806b43
SR
3005 filp->private_data = iter;
3006
107bad8b
SR
3007 if (iter->trace->pipe_open)
3008 iter->trace->pipe_open(iter);
3009 mutex_unlock(&trace_types_lock);
3010
b3806b43
SR
3011 return 0;
3012}
3013
3014static int tracing_release_pipe(struct inode *inode, struct file *file)
3015{
3016 struct trace_iterator *iter = file->private_data;
3017
3018 kfree(iter);
3019 atomic_dec(&tracing_reader);
3020
3021 return 0;
3022}
3023
2a2cc8f7
SSP
3024static unsigned int
3025tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3026{
3027 struct trace_iterator *iter = filp->private_data;
3028
3029 if (trace_flags & TRACE_ITER_BLOCK) {
3030 /*
3031 * Always select as readable when in blocking mode
3032 */
3033 return POLLIN | POLLRDNORM;
afc2abc0 3034 } else {
2a2cc8f7
SSP
3035 if (!trace_empty(iter))
3036 return POLLIN | POLLRDNORM;
3037 poll_wait(filp, &trace_wait, poll_table);
3038 if (!trace_empty(iter))
3039 return POLLIN | POLLRDNORM;
3040
3041 return 0;
3042 }
3043}
3044
b3806b43
SR
3045/*
3046 * Consumer reader.
3047 */
3048static ssize_t
3049tracing_read_pipe(struct file *filp, char __user *ubuf,
3050 size_t cnt, loff_t *ppos)
3051{
3052 struct trace_iterator *iter = filp->private_data;
6c6c2796 3053 ssize_t sret;
b3806b43
SR
3054
3055 /* return any leftover data */
6c6c2796
PP
3056 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3057 if (sret != -EBUSY)
3058 return sret;
b3806b43 3059
6c6c2796 3060 trace_seq_reset(&iter->seq);
b3806b43 3061
107bad8b
SR
3062 mutex_lock(&trace_types_lock);
3063 if (iter->trace->read) {
6c6c2796
PP
3064 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3065 if (sret)
107bad8b 3066 goto out;
107bad8b
SR
3067 }
3068
9ff4b974
PP
3069waitagain:
3070 sret = 0;
b3806b43 3071 while (trace_empty(iter)) {
2dc8f095 3072
107bad8b 3073 if ((filp->f_flags & O_NONBLOCK)) {
6c6c2796 3074 sret = -EAGAIN;
107bad8b
SR
3075 goto out;
3076 }
2dc8f095 3077
b3806b43
SR
3078 /*
3079 * This is a make-shift waitqueue. The reason we don't use
3080 * an actual wait queue is because:
3081 * 1) we only ever have one waiter
3082 * 2) the tracing, traces all functions, we don't want
3083 * the overhead of calling wake_up and friends
3084 * (and tracing them too)
3085 * Anyway, this is really very primitive wakeup.
3086 */
3087 set_current_state(TASK_INTERRUPTIBLE);
3088 iter->tr->waiter = current;
3089
107bad8b
SR
3090 mutex_unlock(&trace_types_lock);
3091
9fe068e9
IM
3092 /* sleep for 100 msecs, and try again. */
3093 schedule_timeout(HZ/10);
b3806b43 3094
107bad8b
SR
3095 mutex_lock(&trace_types_lock);
3096
b3806b43
SR
3097 iter->tr->waiter = NULL;
3098
107bad8b 3099 if (signal_pending(current)) {
6c6c2796 3100 sret = -EINTR;
107bad8b
SR
3101 goto out;
3102 }
b3806b43 3103
84527997 3104 if (iter->trace != current_trace)
107bad8b 3105 goto out;
84527997 3106
b3806b43
SR
3107 /*
3108 * We block until we read something and tracing is disabled.
3109 * We still block if tracing is disabled, but we have never
3110 * read anything. This allows a user to cat this file, and
3111 * then enable tracing. But after we have read something,
3112 * we give an EOF when tracing is again disabled.
3113 *
3114 * iter->pos will be 0 if we haven't read anything.
3115 */
3116 if (!tracer_enabled && iter->pos)
3117 break;
3118
3119 continue;
3120 }
3121
3122 /* stop when tracing is finished */
3123 if (trace_empty(iter))
107bad8b 3124 goto out;
b3806b43
SR
3125
3126 if (cnt >= PAGE_SIZE)
3127 cnt = PAGE_SIZE - 1;
3128
53d0aa77 3129 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3130 memset(&iter->seq, 0,
3131 sizeof(struct trace_iterator) -
3132 offsetof(struct trace_iterator, seq));
4823ed7e 3133 iter->pos = -1;
b3806b43 3134
088b1e42 3135 while (find_next_entry_inc(iter) != NULL) {
2c4f035f 3136 enum print_line_t ret;
088b1e42
SR
3137 int len = iter->seq.len;
3138
f9896bf3 3139 ret = print_trace_line(iter);
2c4f035f 3140 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3141 /* don't print partial lines */
3142 iter->seq.len = len;
b3806b43 3143 break;
088b1e42 3144 }
b3806b43
SR
3145
3146 trace_consume(iter);
3147
3148 if (iter->seq.len >= cnt)
3149 break;
b3806b43
SR
3150 }
3151
b3806b43 3152 /* Now copy what we have to the user */
6c6c2796
PP
3153 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3154 if (iter->seq.readpos >= iter->seq.len)
b3806b43 3155 trace_seq_reset(&iter->seq);
9ff4b974
PP
3156
3157 /*
3158 * If there was nothing to send to user, inspite of consuming trace
3159 * entries, go back to wait for more entries.
3160 */
6c6c2796 3161 if (sret == -EBUSY)
9ff4b974 3162 goto waitagain;
b3806b43 3163
107bad8b
SR
3164out:
3165 mutex_unlock(&trace_types_lock);
3166
6c6c2796 3167 return sret;
b3806b43
SR
3168}
3169
a98a3c3f
SR
3170static ssize_t
3171tracing_entries_read(struct file *filp, char __user *ubuf,
3172 size_t cnt, loff_t *ppos)
3173{
3174 struct trace_array *tr = filp->private_data;
3175 char buf[64];
3176 int r;
3177
1696b2b0 3178 r = sprintf(buf, "%lu\n", tr->entries >> 10);
a98a3c3f
SR
3179 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3180}
3181
3182static ssize_t
3183tracing_entries_write(struct file *filp, const char __user *ubuf,
3184 size_t cnt, loff_t *ppos)
3185{
3186 unsigned long val;
3187 char buf[64];
bf5e6519 3188 int ret, cpu;
a98a3c3f 3189
cffae437
SR
3190 if (cnt >= sizeof(buf))
3191 return -EINVAL;
a98a3c3f
SR
3192
3193 if (copy_from_user(&buf, ubuf, cnt))
3194 return -EFAULT;
3195
3196 buf[cnt] = 0;
3197
c6caeeb1
SR
3198 ret = strict_strtoul(buf, 10, &val);
3199 if (ret < 0)
3200 return ret;
a98a3c3f
SR
3201
3202 /* must have at least 1 entry */
3203 if (!val)
3204 return -EINVAL;
3205
3206 mutex_lock(&trace_types_lock);
3207
c76f0694 3208 tracing_stop();
a98a3c3f 3209
bf5e6519
SR
3210 /* disable all cpu buffers */
3211 for_each_tracing_cpu(cpu) {
3212 if (global_trace.data[cpu])
3213 atomic_inc(&global_trace.data[cpu]->disabled);
3214 if (max_tr.data[cpu])
3215 atomic_inc(&max_tr.data[cpu]->disabled);
3216 }
3217
1696b2b0
SR
3218 /* value is in KB */
3219 val <<= 10;
3220
3928a8a2
SR
3221 if (val != global_trace.entries) {
3222 ret = ring_buffer_resize(global_trace.buffer, val);
3223 if (ret < 0) {
3224 cnt = ret;
3eefae99
SR
3225 goto out;
3226 }
3227
3928a8a2
SR
3228 ret = ring_buffer_resize(max_tr.buffer, val);
3229 if (ret < 0) {
3230 int r;
3231 cnt = ret;
3232 r = ring_buffer_resize(global_trace.buffer,
3233 global_trace.entries);
3234 if (r < 0) {
3235 /* AARGH! We are left with different
3236 * size max buffer!!!! */
3237 WARN_ON(1);
3238 tracing_disabled = 1;
a98a3c3f 3239 }
3928a8a2 3240 goto out;
a98a3c3f 3241 }
3eefae99 3242
3928a8a2 3243 global_trace.entries = val;
a98a3c3f
SR
3244 }
3245
3246 filp->f_pos += cnt;
3247
19384c03
SR
3248 /* If check pages failed, return ENOMEM */
3249 if (tracing_disabled)
3250 cnt = -ENOMEM;
a98a3c3f 3251 out:
bf5e6519
SR
3252 for_each_tracing_cpu(cpu) {
3253 if (global_trace.data[cpu])
3254 atomic_dec(&global_trace.data[cpu]->disabled);
3255 if (max_tr.data[cpu])
3256 atomic_dec(&max_tr.data[cpu]->disabled);
3257 }
3258
c76f0694 3259 tracing_start();
a98a3c3f
SR
3260 max_tr.entries = global_trace.entries;
3261 mutex_unlock(&trace_types_lock);
3262
3263 return cnt;
3264}
3265
5bf9a1ee
PP
3266static int mark_printk(const char *fmt, ...)
3267{
3268 int ret;
3269 va_list args;
3270 va_start(args, fmt);
3271 ret = trace_vprintk(0, fmt, args);
3272 va_end(args);
3273 return ret;
3274}
3275
3276static ssize_t
3277tracing_mark_write(struct file *filp, const char __user *ubuf,
3278 size_t cnt, loff_t *fpos)
3279{
3280 char *buf;
3281 char *end;
5bf9a1ee 3282
c76f0694 3283 if (tracing_disabled)
5bf9a1ee
PP
3284 return -EINVAL;
3285
3286 if (cnt > TRACE_BUF_SIZE)
3287 cnt = TRACE_BUF_SIZE;
3288
3289 buf = kmalloc(cnt + 1, GFP_KERNEL);
3290 if (buf == NULL)
3291 return -ENOMEM;
3292
3293 if (copy_from_user(buf, ubuf, cnt)) {
3294 kfree(buf);
3295 return -EFAULT;
3296 }
3297
3298 /* Cut from the first nil or newline. */
3299 buf[cnt] = '\0';
3300 end = strchr(buf, '\n');
3301 if (end)
3302 *end = '\0';
3303
3304 cnt = mark_printk("%s\n", buf);
3305 kfree(buf);
3306 *fpos += cnt;
3307
3308 return cnt;
3309}
3310
bc0c38d1 3311static struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
3312 .open = tracing_open_generic,
3313 .read = tracing_max_lat_read,
3314 .write = tracing_max_lat_write,
bc0c38d1
SR
3315};
3316
3317static struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
3318 .open = tracing_open_generic,
3319 .read = tracing_ctrl_read,
3320 .write = tracing_ctrl_write,
bc0c38d1
SR
3321};
3322
3323static struct file_operations set_tracer_fops = {
4bf39a94
IM
3324 .open = tracing_open_generic,
3325 .read = tracing_set_trace_read,
3326 .write = tracing_set_trace_write,
bc0c38d1
SR
3327};
3328
b3806b43 3329static struct file_operations tracing_pipe_fops = {
4bf39a94 3330 .open = tracing_open_pipe,
2a2cc8f7 3331 .poll = tracing_poll_pipe,
4bf39a94
IM
3332 .read = tracing_read_pipe,
3333 .release = tracing_release_pipe,
b3806b43
SR
3334};
3335
a98a3c3f
SR
3336static struct file_operations tracing_entries_fops = {
3337 .open = tracing_open_generic,
3338 .read = tracing_entries_read,
3339 .write = tracing_entries_write,
3340};
3341
5bf9a1ee 3342static struct file_operations tracing_mark_fops = {
43a15386 3343 .open = tracing_open_generic,
5bf9a1ee
PP
3344 .write = tracing_mark_write,
3345};
3346
bc0c38d1
SR
3347#ifdef CONFIG_DYNAMIC_FTRACE
3348
b807c3d0
SR
3349int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3350{
3351 return 0;
3352}
3353
bc0c38d1 3354static ssize_t
b807c3d0 3355tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
3356 size_t cnt, loff_t *ppos)
3357{
a26a2a27
SR
3358 static char ftrace_dyn_info_buffer[1024];
3359 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 3360 unsigned long *p = filp->private_data;
b807c3d0 3361 char *buf = ftrace_dyn_info_buffer;
a26a2a27 3362 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
3363 int r;
3364
b807c3d0
SR
3365 mutex_lock(&dyn_info_mutex);
3366 r = sprintf(buf, "%ld ", *p);
4bf39a94 3367
a26a2a27 3368 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
3369 buf[r++] = '\n';
3370
3371 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3372
3373 mutex_unlock(&dyn_info_mutex);
3374
3375 return r;
bc0c38d1
SR
3376}
3377
b807c3d0 3378static struct file_operations tracing_dyn_info_fops = {
4bf39a94 3379 .open = tracing_open_generic,
b807c3d0 3380 .read = tracing_read_dyn_info,
bc0c38d1
SR
3381};
3382#endif
3383
3384static struct dentry *d_tracer;
3385
3386struct dentry *tracing_init_dentry(void)
3387{
3388 static int once;
3389
3390 if (d_tracer)
3391 return d_tracer;
3392
3393 d_tracer = debugfs_create_dir("tracing", NULL);
3394
3395 if (!d_tracer && !once) {
3396 once = 1;
3397 pr_warning("Could not create debugfs directory 'tracing'\n");
3398 return NULL;
3399 }
3400
3401 return d_tracer;
3402}
3403
60a11774
SR
3404#ifdef CONFIG_FTRACE_SELFTEST
3405/* Let selftest have access to static functions in this file */
3406#include "trace_selftest.c"
3407#endif
3408
b5ad384e 3409static __init int tracer_init_debugfs(void)
bc0c38d1
SR
3410{
3411 struct dentry *d_tracer;
3412 struct dentry *entry;
3413
3414 d_tracer = tracing_init_dentry();
3415
3416 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3417 &global_trace, &tracing_ctrl_fops);
3418 if (!entry)
3419 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3420
ee6bce52 3421 entry = debugfs_create_file("trace_options", 0644, d_tracer,
bc0c38d1
SR
3422 NULL, &tracing_iter_fops);
3423 if (!entry)
ee6bce52 3424 pr_warning("Could not create debugfs 'trace_options' entry\n");
bc0c38d1 3425
c7078de1
IM
3426 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3427 NULL, &tracing_cpumask_fops);
3428 if (!entry)
3429 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3430
bc0c38d1
SR
3431 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3432 &global_trace, &tracing_lt_fops);
3433 if (!entry)
3434 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3435
3436 entry = debugfs_create_file("trace", 0444, d_tracer,
3437 &global_trace, &tracing_fops);
3438 if (!entry)
3439 pr_warning("Could not create debugfs 'trace' entry\n");
3440
3441 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3442 &global_trace, &show_traces_fops);
3443 if (!entry)
98a983aa 3444 pr_warning("Could not create debugfs 'available_tracers' entry\n");
bc0c38d1
SR
3445
3446 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3447 &global_trace, &set_tracer_fops);
3448 if (!entry)
98a983aa 3449 pr_warning("Could not create debugfs 'current_tracer' entry\n");
bc0c38d1
SR
3450
3451 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3452 &tracing_max_latency,
3453 &tracing_max_lat_fops);
3454 if (!entry)
3455 pr_warning("Could not create debugfs "
3456 "'tracing_max_latency' entry\n");
3457
3458 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3459 &tracing_thresh, &tracing_max_lat_fops);
3460 if (!entry)
3461 pr_warning("Could not create debugfs "
98a983aa 3462 "'tracing_thresh' entry\n");
7bd2f24c
IM
3463 entry = debugfs_create_file("README", 0644, d_tracer,
3464 NULL, &tracing_readme_fops);
3465 if (!entry)
3466 pr_warning("Could not create debugfs 'README' entry\n");
3467
b3806b43
SR
3468 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3469 NULL, &tracing_pipe_fops);
3470 if (!entry)
3471 pr_warning("Could not create debugfs "
98a983aa 3472 "'trace_pipe' entry\n");
bc0c38d1 3473
a94c80e7 3474 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
a98a3c3f
SR
3475 &global_trace, &tracing_entries_fops);
3476 if (!entry)
3477 pr_warning("Could not create debugfs "
a94c80e7 3478 "'buffer_size_kb' entry\n");
a98a3c3f 3479
5bf9a1ee
PP
3480 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3481 NULL, &tracing_mark_fops);
3482 if (!entry)
3483 pr_warning("Could not create debugfs "
3484 "'trace_marker' entry\n");
3485
bc0c38d1
SR
3486#ifdef CONFIG_DYNAMIC_FTRACE
3487 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3488 &ftrace_update_tot_cnt,
b807c3d0 3489 &tracing_dyn_info_fops);
bc0c38d1
SR
3490 if (!entry)
3491 pr_warning("Could not create debugfs "
3492 "'dyn_ftrace_total_info' entry\n");
3493#endif
d618b3e6
IM
3494#ifdef CONFIG_SYSPROF_TRACER
3495 init_tracer_sysprof_debugfs(d_tracer);
3496#endif
b5ad384e 3497 return 0;
bc0c38d1
SR
3498}
3499
801fe400 3500int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
dd0e545f 3501{
dd0e545f
SR
3502 static DEFINE_SPINLOCK(trace_buf_lock);
3503 static char trace_buf[TRACE_BUF_SIZE];
f09ce573 3504
3928a8a2 3505 struct ring_buffer_event *event;
f09ce573 3506 struct trace_array *tr = &global_trace;
dd0e545f 3507 struct trace_array_cpu *data;
777e208d 3508 struct print_entry *entry;
3928a8a2 3509 unsigned long flags, irq_flags;
38697053 3510 int cpu, len = 0, size, pc;
dd0e545f 3511
c76f0694 3512 if (tracing_disabled)
dd0e545f
SR
3513 return 0;
3514
38697053
SR
3515 pc = preempt_count();
3516 preempt_disable_notrace();
dd0e545f
SR
3517 cpu = raw_smp_processor_id();
3518 data = tr->data[cpu];
dd0e545f 3519
3ea2e6d7 3520 if (unlikely(atomic_read(&data->disabled)))
dd0e545f
SR
3521 goto out;
3522
38697053 3523 spin_lock_irqsave(&trace_buf_lock, flags);
801fe400 3524 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
dd0e545f
SR
3525
3526 len = min(len, TRACE_BUF_SIZE-1);
3527 trace_buf[len] = 0;
3528
777e208d
SR
3529 size = sizeof(*entry) + len + 1;
3530 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
3928a8a2
SR
3531 if (!event)
3532 goto out_unlock;
777e208d 3533 entry = ring_buffer_event_data(event);
38697053 3534 tracing_generic_entry_update(&entry->ent, flags, pc);
777e208d
SR
3535 entry->ent.type = TRACE_PRINT;
3536 entry->ip = ip;
dd0e545f 3537
777e208d
SR
3538 memcpy(&entry->buf, trace_buf, len);
3539 entry->buf[len] = 0;
3928a8a2 3540 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
dd0e545f 3541
3928a8a2 3542 out_unlock:
38697053 3543 spin_unlock_irqrestore(&trace_buf_lock, flags);
dd0e545f
SR
3544
3545 out:
38697053 3546 preempt_enable_notrace();
dd0e545f
SR
3547
3548 return len;
3549}
801fe400
PP
3550EXPORT_SYMBOL_GPL(trace_vprintk);
3551
3552int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3553{
3554 int ret;
3555 va_list ap;
3556
3557 if (!(trace_flags & TRACE_ITER_PRINTK))
3558 return 0;
3559
3560 va_start(ap, fmt);
3561 ret = trace_vprintk(ip, fmt, ap);
3562 va_end(ap);
3563 return ret;
3564}
dd0e545f
SR
3565EXPORT_SYMBOL_GPL(__ftrace_printk);
3566
3f5a54e3
SR
3567static int trace_panic_handler(struct notifier_block *this,
3568 unsigned long event, void *unused)
3569{
944ac425
SR
3570 if (ftrace_dump_on_oops)
3571 ftrace_dump();
3f5a54e3
SR
3572 return NOTIFY_OK;
3573}
3574
3575static struct notifier_block trace_panic_notifier = {
3576 .notifier_call = trace_panic_handler,
3577 .next = NULL,
3578 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3579};
3580
3581static int trace_die_handler(struct notifier_block *self,
3582 unsigned long val,
3583 void *data)
3584{
3585 switch (val) {
3586 case DIE_OOPS:
944ac425
SR
3587 if (ftrace_dump_on_oops)
3588 ftrace_dump();
3f5a54e3
SR
3589 break;
3590 default:
3591 break;
3592 }
3593 return NOTIFY_OK;
3594}
3595
3596static struct notifier_block trace_die_notifier = {
3597 .notifier_call = trace_die_handler,
3598 .priority = 200
3599};
3600
3601/*
3602 * printk is set to max of 1024, we really don't need it that big.
3603 * Nothing should be printing 1000 characters anyway.
3604 */
3605#define TRACE_MAX_PRINT 1000
3606
3607/*
3608 * Define here KERN_TRACE so that we have one place to modify
3609 * it if we decide to change what log level the ftrace dump
3610 * should be at.
3611 */
3612#define KERN_TRACE KERN_INFO
3613
3614static void
3615trace_printk_seq(struct trace_seq *s)
3616{
3617 /* Probably should print a warning here. */
3618 if (s->len >= 1000)
3619 s->len = 1000;
3620
3621 /* should be zero ended, but we are paranoid. */
3622 s->buffer[s->len] = 0;
3623
3624 printk(KERN_TRACE "%s", s->buffer);
3625
3626 trace_seq_reset(s);
3627}
3628
3f5a54e3
SR
3629void ftrace_dump(void)
3630{
3631 static DEFINE_SPINLOCK(ftrace_dump_lock);
3632 /* use static because iter can be a bit big for the stack */
3633 static struct trace_iterator iter;
3f5a54e3
SR
3634 static cpumask_t mask;
3635 static int dump_ran;
d769041f
SR
3636 unsigned long flags;
3637 int cnt = 0, cpu;
3f5a54e3
SR
3638
3639 /* only one dump */
3640 spin_lock_irqsave(&ftrace_dump_lock, flags);
3641 if (dump_ran)
3642 goto out;
3643
3644 dump_ran = 1;
3645
3646 /* No turning back! */
81adbdc0 3647 ftrace_kill();
3f5a54e3 3648
d769041f
SR
3649 for_each_tracing_cpu(cpu) {
3650 atomic_inc(&global_trace.data[cpu]->disabled);
3651 }
3652
b54d3de9
TE
3653 /* don't look at user memory in panic mode */
3654 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3655
3f5a54e3
SR
3656 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3657
3658 iter.tr = &global_trace;
3659 iter.trace = current_trace;
3660
3661 /*
3662 * We need to stop all tracing on all CPUS to read the
3663 * the next buffer. This is a bit expensive, but is
3664 * not done often. We fill all what we can read,
3665 * and then release the locks again.
3666 */
3667
3668 cpus_clear(mask);
3669
3f5a54e3
SR
3670 while (!trace_empty(&iter)) {
3671
3672 if (!cnt)
3673 printk(KERN_TRACE "---------------------------------\n");
3674
3675 cnt++;
3676
3677 /* reset all but tr, trace, and overruns */
3678 memset(&iter.seq, 0,
3679 sizeof(struct trace_iterator) -
3680 offsetof(struct trace_iterator, seq));
3681 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3682 iter.pos = -1;
3683
3684 if (find_next_entry_inc(&iter) != NULL) {
3685 print_trace_line(&iter);
3686 trace_consume(&iter);
3687 }
3688
3689 trace_printk_seq(&iter.seq);
3690 }
3691
3692 if (!cnt)
3693 printk(KERN_TRACE " (ftrace buffer empty)\n");
3694 else
3695 printk(KERN_TRACE "---------------------------------\n");
3696
3f5a54e3
SR
3697 out:
3698 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3699}
3700
3928a8a2 3701__init static int tracer_alloc_buffers(void)
bc0c38d1 3702{
4c11d7ae 3703 struct trace_array_cpu *data;
4c11d7ae
SR
3704 int i;
3705
3928a8a2
SR
3706 /* TODO: make the number of buffers hot pluggable with CPUS */
3707 tracing_buffer_mask = cpu_possible_map;
4c11d7ae 3708
3928a8a2
SR
3709 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3710 TRACE_BUFFER_FLAGS);
3711 if (!global_trace.buffer) {
3712 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3713 WARN_ON(1);
3714 return 0;
4c11d7ae 3715 }
3928a8a2 3716 global_trace.entries = ring_buffer_size(global_trace.buffer);
4c11d7ae
SR
3717
3718#ifdef CONFIG_TRACER_MAX_TRACE
3928a8a2
SR
3719 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3720 TRACE_BUFFER_FLAGS);
3721 if (!max_tr.buffer) {
3722 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3723 WARN_ON(1);
3724 ring_buffer_free(global_trace.buffer);
3725 return 0;
4c11d7ae 3726 }
3928a8a2
SR
3727 max_tr.entries = ring_buffer_size(max_tr.buffer);
3728 WARN_ON(max_tr.entries != global_trace.entries);
a98a3c3f 3729#endif
ab46428c 3730
4c11d7ae 3731 /* Allocate the first page for all buffers */
ab46428c 3732 for_each_tracing_cpu(i) {
4c11d7ae 3733 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1 3734 max_tr.data[i] = &per_cpu(max_data, i);
4c11d7ae 3735 }
bc0c38d1 3736
bc0c38d1
SR
3737 trace_init_cmdlines();
3738
43a15386 3739 register_tracer(&nop_trace);
b5ad384e
FW
3740#ifdef CONFIG_BOOT_TRACER
3741 register_tracer(&boot_tracer);
3742 current_trace = &boot_tracer;
3743 current_trace->init(&global_trace);
3744#else
43a15386 3745 current_trace = &nop_trace;
b5ad384e 3746#endif
bc0c38d1 3747
60a11774
SR
3748 /* All seems OK, enable tracing */
3749 tracing_disabled = 0;
3928a8a2 3750
3f5a54e3
SR
3751 atomic_notifier_chain_register(&panic_notifier_list,
3752 &trace_panic_notifier);
3753
3754 register_die_notifier(&trace_die_notifier);
3755
bc0c38d1 3756 return 0;
bc0c38d1 3757}
b5ad384e
FW
3758early_initcall(tracer_alloc_buffers);
3759fs_initcall(tracer_init_debugfs);