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