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