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