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