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