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