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