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