blob: 4dcc4e85c5d64d6615668fa04f99166d60d69280 [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
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>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020018#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#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>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020027#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/gfp.h>
29#include <linux/fs.h>
Steven Rostedt3eefae92008-05-12 21:21:04 +020030#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020031
Ingo Molnar86387f72008-05-12 21:20:51 +020032#include <linux/stacktrace.h>
33
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020034#include "trace.h"
35
36unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
37unsigned long __read_mostly tracing_thresh;
38
Steven Rostedtab464282008-05-12 21:21:00 +020039static unsigned long __read_mostly tracing_nr_buffers;
40static cpumask_t __read_mostly tracing_buffer_mask;
41
42#define for_each_tracing_cpu(cpu) \
43 for_each_cpu_mask(cpu, tracing_buffer_mask)
44
Steven Rostedta98a3c32008-05-12 21:20:59 +020045/* dummy trace to disable tracing */
Steven Rostedtcffae432008-05-12 21:21:00 +020046static struct tracer no_tracer __read_mostly = {
Steven Rostedta98a3c32008-05-12 21:20:59 +020047 .name = "none",
48};
49
50static int trace_alloc_page(void);
51static int trace_free_page(void);
52
Steven Rostedt60a11772008-05-12 21:20:44 +020053static int tracing_disabled = 1;
54
Steven Rostedt3eefae92008-05-12 21:21:04 +020055static unsigned long tracing_pages_allocated;
56
Thomas Gleixner72829bc2008-05-23 21:37:28 +020057long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020058ns2usecs(cycle_t nsec)
59{
60 nsec += 500;
61 do_div(nsec, 1000);
62 return nsec;
63}
64
Ingo Molnare309b412008-05-12 21:20:51 +020065cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020066{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020067 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020068}
69
Steven Rostedt4fcdae82008-05-12 21:21:00 +020070/*
71 * The global_trace is the descriptor that holds the tracing
72 * buffers for the live tracing. For each CPU, it contains
73 * a link list of pages that will store trace entries. The
74 * page descriptor of the pages in the memory is used to hold
75 * the link list by linking the lru item in the page descriptor
76 * to each of the pages in the buffer per CPU.
77 *
78 * For each active CPU there is a data field that holds the
79 * pages for the buffer for that CPU. Each CPU has the same number
80 * of pages allocated for its buffer.
81 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020082static struct trace_array global_trace;
83
84static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
85
Steven Rostedt4fcdae82008-05-12 21:21:00 +020086/*
87 * The max_tr is used to snapshot the global_trace when a maximum
88 * latency is reached. Some tracers will use this to store a maximum
89 * trace while it continues examining live traces.
90 *
91 * The buffers for the max_tr are set up the same as the global_trace.
92 * When a snapshot is taken, the link list of the max_tr is swapped
93 * with the link list of the global_trace and the buffers are reset for
94 * the global_trace so the tracing can continue.
95 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020096static struct trace_array max_tr;
97
98static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
99
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200100/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200101static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200102
103/*
104 * trace_nr_entries is the number of entries that is allocated
105 * for a buffer. Note, the number of entries is always rounded
106 * to ENTRIES_PER_PAGE.
107 */
Ingo Molnar57422792008-05-12 21:20:51 +0200108static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200109
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200110/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200111static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200112
113/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200114static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200115
116/*
117 * max_tracer_type_len is used to simplify the allocating of
118 * buffers to read userspace tracer names. We keep track of
119 * the longest tracer name registered.
120 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200121static int max_tracer_type_len;
122
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200123/*
124 * trace_types_lock is used to protect the trace_types list.
125 * This lock is also used to keep user access serialized.
126 * Accesses from userspace will grab this lock while userspace
127 * activities happen inside the kernel.
128 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200129static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200130
131/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200132static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
133
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200134/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200135unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
136
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200137/**
138 * trace_wake_up - wake up tasks waiting for trace input
139 *
140 * Simply wakes up any task that is blocked on the trace_wait
141 * queue. These is used with trace_poll for tasks polling the trace.
142 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200143void trace_wake_up(void)
144{
Ingo Molnar017730c2008-05-12 21:20:52 +0200145 /*
146 * The runqueue_is_locked() can fail, but this is the best we
147 * have for now:
148 */
149 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200150 wake_up(&trace_wait);
151}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200152
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200153#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
154
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200155static int __init set_nr_entries(char *str)
156{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200157 unsigned long nr_entries;
158 int ret;
159
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200160 if (!str)
161 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200162 ret = strict_strtoul(str, 0, &nr_entries);
163 /* nr_entries can not be zero */
164 if (ret < 0 || nr_entries == 0)
165 return 0;
166 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200167 return 1;
168}
169__setup("trace_entries=", set_nr_entries);
170
Steven Rostedt57f50be2008-05-12 21:20:44 +0200171unsigned long nsecs_to_usecs(unsigned long nsecs)
172{
173 return nsecs / 1000;
174}
175
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200176/*
177 * trace_flag_type is an enumeration that holds different
178 * states when a trace occurs. These are:
179 * IRQS_OFF - interrupts were disabled
180 * NEED_RESCED - reschedule is requested
181 * HARDIRQ - inside an interrupt handler
182 * SOFTIRQ - inside a softirq handler
183 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200184enum trace_flag_type {
185 TRACE_FLAG_IRQS_OFF = 0x01,
186 TRACE_FLAG_NEED_RESCHED = 0x02,
187 TRACE_FLAG_HARDIRQ = 0x04,
188 TRACE_FLAG_SOFTIRQ = 0x08,
189};
190
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200191/*
192 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
193 * control the output of kernel symbols.
194 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200195#define TRACE_ITER_SYM_MASK \
196 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
197
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200198/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200199static const char *trace_options[] = {
200 "print-parent",
201 "sym-offset",
202 "sym-addr",
203 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200204 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200205 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200206 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200207 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200208 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200209 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200210 NULL
211};
212
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200213/*
214 * ftrace_max_lock is used to protect the swapping of buffers
215 * when taking a max snapshot. The buffers themselves are
216 * protected by per_cpu spinlocks. But the action of the swap
217 * needs its own lock.
218 *
219 * This is defined as a raw_spinlock_t in order to help
220 * with performance when lockdep debugging is enabled.
221 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200222static raw_spinlock_t ftrace_max_lock =
223 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200224
225/*
226 * Copy the new maximum trace into the separate maximum-trace
227 * structure. (this way the maximum trace is permanently saved,
228 * for later retrieval via /debugfs/tracing/latency_trace)
229 */
Ingo Molnare309b412008-05-12 21:20:51 +0200230static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200231__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
232{
233 struct trace_array_cpu *data = tr->data[cpu];
234
235 max_tr.cpu = cpu;
236 max_tr.time_start = data->preempt_timestamp;
237
238 data = max_tr.data[cpu];
239 data->saved_latency = tracing_max_latency;
240
241 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
242 data->pid = tsk->pid;
243 data->uid = tsk->uid;
244 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
245 data->policy = tsk->policy;
246 data->rt_priority = tsk->rt_priority;
247
248 /* record this tasks comm */
249 tracing_record_cmdline(current);
250}
251
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252/**
253 * check_pages - integrity check of trace buffers
254 *
255 * As a safty measure we check to make sure the data pages have not
256 * been corrupted. TODO: configure to disable this because it adds
257 * a bit of overhead.
258 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200259void check_pages(struct trace_array_cpu *data)
260{
261 struct page *page, *tmp;
262
263 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
264 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
265
266 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
267 BUG_ON(page->lru.next->prev != &page->lru);
268 BUG_ON(page->lru.prev->next != &page->lru);
269 }
270}
271
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200272/**
273 * head_page - page address of the first page in per_cpu buffer.
274 *
275 * head_page returns the page address of the first page in
276 * a per_cpu buffer. This also preforms various consistency
277 * checks to make sure the buffer has not been corrupted.
278 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200279void *head_page(struct trace_array_cpu *data)
280{
281 struct page *page;
282
283 check_pages(data);
284 if (list_empty(&data->trace_pages))
285 return NULL;
286
287 page = list_entry(data->trace_pages.next, struct page, lru);
288 BUG_ON(&page->lru == &data->trace_pages);
289
290 return page_address(page);
291}
292
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200293/**
294 * trace_seq_printf - sequence printing of trace information
295 * @s: trace sequence descriptor
296 * @fmt: printf format string
297 *
298 * The tracer may use either sequence operations or its own
299 * copy to user routines. To simplify formating of a trace
300 * trace_seq_printf is used to store strings into a special
301 * buffer (@s). Then the output may be either used by
302 * the sequencer or pulled into another buffer.
303 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200304int
Steven Rostedt214023c2008-05-12 21:20:46 +0200305trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
306{
307 int len = (PAGE_SIZE - 1) - s->len;
308 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200309 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200310
311 if (!len)
312 return 0;
313
314 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200315 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200316 va_end(ap);
317
Steven Rostedtb3806b42008-05-12 21:20:46 +0200318 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200319 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200320 return 0;
321
322 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200323
324 return len;
325}
326
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200327/**
328 * trace_seq_puts - trace sequence printing of simple string
329 * @s: trace sequence descriptor
330 * @str: simple string to record
331 *
332 * The tracer may use either the sequence operations or its own
333 * copy to user routines. This function records a simple string
334 * into a special buffer (@s) for later retrieval by a sequencer
335 * or other mechanism.
336 */
Ingo Molnare309b412008-05-12 21:20:51 +0200337static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200338trace_seq_puts(struct trace_seq *s, const char *str)
339{
340 int len = strlen(str);
341
342 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200343 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200344
345 memcpy(s->buffer + s->len, str, len);
346 s->len += len;
347
348 return len;
349}
350
Ingo Molnare309b412008-05-12 21:20:51 +0200351static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200352trace_seq_putc(struct trace_seq *s, unsigned char c)
353{
354 if (s->len >= (PAGE_SIZE - 1))
355 return 0;
356
357 s->buffer[s->len++] = c;
358
359 return 1;
360}
361
Ingo Molnare309b412008-05-12 21:20:51 +0200362static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200363trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
364{
365 if (len > ((PAGE_SIZE - 1) - s->len))
366 return 0;
367
368 memcpy(s->buffer + s->len, mem, len);
369 s->len += len;
370
371 return len;
372}
373
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200374#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200375static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200376
Ingo Molnare309b412008-05-12 21:20:51 +0200377static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200378trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
379{
380 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200381 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200382 unsigned char byte;
383 int i, j;
384
385 BUG_ON(len >= HEX_CHARS);
386
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200387#ifdef __BIG_ENDIAN
388 for (i = 0, j = 0; i < len; i++) {
389#else
390 for (i = len-1, j = 0; i >= 0; i--) {
391#endif
392 byte = data[i];
393
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200394 hex[j++] = hex2asc[byte & 0x0f];
395 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200396 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200397 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200398
399 return trace_seq_putmem(s, hex, j);
400}
401
Ingo Molnare309b412008-05-12 21:20:51 +0200402static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200403trace_seq_reset(struct trace_seq *s)
404{
405 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200406 s->readpos = 0;
407}
408
409ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
410{
411 int len;
412 int ret;
413
414 if (s->len <= s->readpos)
415 return -EBUSY;
416
417 len = s->len - s->readpos;
418 if (cnt > len)
419 cnt = len;
420 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
421 if (ret)
422 return -EFAULT;
423
424 s->readpos += len;
425 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200426}
427
Ingo Molnare309b412008-05-12 21:20:51 +0200428static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200429trace_print_seq(struct seq_file *m, struct trace_seq *s)
430{
431 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
432
433 s->buffer[len] = 0;
434 seq_puts(m, s->buffer);
435
436 trace_seq_reset(s);
437}
438
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200439/*
440 * flip the trace buffers between two trace descriptors.
441 * This usually is the buffers between the global_trace and
442 * the max_tr to record a snapshot of a current trace.
443 *
444 * The ftrace_max_lock must be held.
445 */
Ingo Molnare309b412008-05-12 21:20:51 +0200446static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200447flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
448{
449 struct list_head flip_pages;
450
451 INIT_LIST_HEAD(&flip_pages);
452
Steven Rostedt93a588f2008-05-12 21:20:45 +0200453 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200454 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200455 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200456
457 check_pages(tr1);
458 check_pages(tr2);
459 list_splice_init(&tr1->trace_pages, &flip_pages);
460 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
461 list_splice_init(&flip_pages, &tr2->trace_pages);
462 BUG_ON(!list_empty(&flip_pages));
463 check_pages(tr1);
464 check_pages(tr2);
465}
466
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200467/**
468 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
469 * @tr: tracer
470 * @tsk: the task with the latency
471 * @cpu: The cpu that initiated the trace.
472 *
473 * Flip the buffers between the @tr and the max_tr and record information
474 * about which task was the cause of this latency.
475 */
Ingo Molnare309b412008-05-12 21:20:51 +0200476void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200477update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
478{
479 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480 int i;
481
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200482 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200483 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200484 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200485 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200486 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200487 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200488 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200489 }
490
491 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200492 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200493}
494
495/**
496 * update_max_tr_single - only copy one trace over, and reset the rest
497 * @tr - tracer
498 * @tsk - task with the latency
499 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200500 *
501 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200502 */
Ingo Molnare309b412008-05-12 21:20:51 +0200503void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
505{
506 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200507 int i;
508
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200509 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200510 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200511 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200512 tracing_reset(max_tr.data[i]);
513
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200514 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200515 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200516
517 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200518 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200519}
520
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200521/**
522 * register_tracer - register a tracer with the ftrace system.
523 * @type - the plugin for the tracer
524 *
525 * Register a new plugin tracer.
526 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200527int register_tracer(struct tracer *type)
528{
529 struct tracer *t;
530 int len;
531 int ret = 0;
532
533 if (!type->name) {
534 pr_info("Tracer must have a name\n");
535 return -1;
536 }
537
538 mutex_lock(&trace_types_lock);
539 for (t = trace_types; t; t = t->next) {
540 if (strcmp(type->name, t->name) == 0) {
541 /* already found */
542 pr_info("Trace %s already registered\n",
543 type->name);
544 ret = -1;
545 goto out;
546 }
547 }
548
Steven Rostedt60a11772008-05-12 21:20:44 +0200549#ifdef CONFIG_FTRACE_STARTUP_TEST
550 if (type->selftest) {
551 struct tracer *saved_tracer = current_trace;
552 struct trace_array_cpu *data;
553 struct trace_array *tr = &global_trace;
554 int saved_ctrl = tr->ctrl;
555 int i;
556 /*
557 * Run a selftest on this tracer.
558 * Here we reset the trace buffer, and set the current
559 * tracer to be this tracer. The tracer can then run some
560 * internal tracing to verify that everything is in order.
561 * If we fail, we do not register this tracer.
562 */
Steven Rostedtab464282008-05-12 21:21:00 +0200563 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200564 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200565 if (!head_page(data))
566 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200567 tracing_reset(data);
568 }
569 current_trace = type;
570 tr->ctrl = 0;
571 /* the test is responsible for initializing and enabling */
572 pr_info("Testing tracer %s: ", type->name);
573 ret = type->selftest(type, tr);
574 /* the test is responsible for resetting too */
575 current_trace = saved_tracer;
576 tr->ctrl = saved_ctrl;
577 if (ret) {
578 printk(KERN_CONT "FAILED!\n");
579 goto out;
580 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200581 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200582 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200583 data = tr->data[i];
584 if (!head_page(data))
585 continue;
586 tracing_reset(data);
587 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200588 printk(KERN_CONT "PASSED\n");
589 }
590#endif
591
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200592 type->next = trace_types;
593 trace_types = type;
594 len = strlen(type->name);
595 if (len > max_tracer_type_len)
596 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200597
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200598 out:
599 mutex_unlock(&trace_types_lock);
600
601 return ret;
602}
603
604void unregister_tracer(struct tracer *type)
605{
606 struct tracer **t;
607 int len;
608
609 mutex_lock(&trace_types_lock);
610 for (t = &trace_types; *t; t = &(*t)->next) {
611 if (*t == type)
612 goto found;
613 }
614 pr_info("Trace %s not registered\n", type->name);
615 goto out;
616
617 found:
618 *t = (*t)->next;
619 if (strlen(type->name) != max_tracer_type_len)
620 goto out;
621
622 max_tracer_type_len = 0;
623 for (t = &trace_types; *t; t = &(*t)->next) {
624 len = strlen((*t)->name);
625 if (len > max_tracer_type_len)
626 max_tracer_type_len = len;
627 }
628 out:
629 mutex_unlock(&trace_types_lock);
630}
631
Ingo Molnare309b412008-05-12 21:20:51 +0200632void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200633{
634 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200635 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200636 data->trace_head = data->trace_tail = head_page(data);
637 data->trace_head_idx = 0;
638 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200639}
640
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200641#define SAVED_CMDLINES 128
642static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
643static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
644static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
645static int cmdline_idx;
646static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200647
648/* trace in all context switches */
649atomic_t trace_record_cmdline_enabled __read_mostly;
650
651/* temporary disable recording */
652atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653
654static void trace_init_cmdlines(void)
655{
656 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
657 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
658 cmdline_idx = 0;
659}
660
Ingo Molnare309b412008-05-12 21:20:51 +0200661void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200662
Ingo Molnare309b412008-05-12 21:20:51 +0200663static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200664{
665 unsigned map;
666 unsigned idx;
667
668 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
669 return;
670
671 /*
672 * It's not the end of the world if we don't get
673 * the lock, but we also don't want to spin
674 * nor do we want to disable interrupts,
675 * so if we miss here, then better luck next time.
676 */
677 if (!spin_trylock(&trace_cmdline_lock))
678 return;
679
680 idx = map_pid_to_cmdline[tsk->pid];
681 if (idx >= SAVED_CMDLINES) {
682 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
683
684 map = map_cmdline_to_pid[idx];
685 if (map <= PID_MAX_DEFAULT)
686 map_pid_to_cmdline[map] = (unsigned)-1;
687
688 map_pid_to_cmdline[tsk->pid] = idx;
689
690 cmdline_idx = idx;
691 }
692
693 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
694
695 spin_unlock(&trace_cmdline_lock);
696}
697
Ingo Molnare309b412008-05-12 21:20:51 +0200698static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200699{
700 char *cmdline = "<...>";
701 unsigned map;
702
703 if (!pid)
704 return "<idle>";
705
706 if (pid > PID_MAX_DEFAULT)
707 goto out;
708
709 map = map_pid_to_cmdline[pid];
710 if (map >= SAVED_CMDLINES)
711 goto out;
712
713 cmdline = saved_cmdlines[map];
714
715 out:
716 return cmdline;
717}
718
Ingo Molnare309b412008-05-12 21:20:51 +0200719void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720{
721 if (atomic_read(&trace_record_cmdline_disabled))
722 return;
723
724 trace_save_cmdline(tsk);
725}
726
Ingo Molnare309b412008-05-12 21:20:51 +0200727static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200728trace_next_list(struct trace_array_cpu *data, struct list_head *next)
729{
730 /*
731 * Roundrobin - but skip the head (which is not a real page):
732 */
733 next = next->next;
734 if (unlikely(next == &data->trace_pages))
735 next = next->next;
736 BUG_ON(next == &data->trace_pages);
737
738 return next;
739}
740
Ingo Molnare309b412008-05-12 21:20:51 +0200741static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200742trace_next_page(struct trace_array_cpu *data, void *addr)
743{
744 struct list_head *next;
745 struct page *page;
746
747 page = virt_to_page(addr);
748
749 next = trace_next_list(data, &page->lru);
750 page = list_entry(next, struct page, lru);
751
752 return page_address(page);
753}
754
Ingo Molnare309b412008-05-12 21:20:51 +0200755static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200756tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200757{
758 unsigned long idx, idx_next;
759 struct trace_entry *entry;
760
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200761 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200762 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200763 idx_next = idx + 1;
764
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200765 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
766
Steven Rostedt93a588f2008-05-12 21:20:45 +0200767 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200768
769 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200770 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200771 idx_next = 0;
772 }
773
Steven Rostedt93a588f2008-05-12 21:20:45 +0200774 if (data->trace_head == data->trace_tail &&
775 idx_next == data->trace_tail_idx) {
776 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200777 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200778 data->trace_tail_idx++;
779 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
780 data->trace_tail =
781 trace_next_page(data, data->trace_tail);
782 data->trace_tail_idx = 0;
783 }
784 }
785
786 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787
788 return entry;
789}
790
Ingo Molnare309b412008-05-12 21:20:51 +0200791static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200792tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793{
794 struct task_struct *tsk = current;
795 unsigned long pc;
796
797 pc = preempt_count();
798
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200799 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200800 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200801 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200802 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
803 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
804 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
805 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
806}
807
Ingo Molnare309b412008-05-12 21:20:51 +0200808void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200809trace_function(struct trace_array *tr, struct trace_array_cpu *data,
810 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200811{
812 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200813 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200814
Steven Rostedt92205c22008-05-12 21:20:55 +0200815 raw_local_irq_save(irq_flags);
816 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200817 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200819 entry->type = TRACE_FN;
820 entry->fn.ip = ip;
821 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200822 __raw_spin_unlock(&data->lock);
823 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200824}
825
Ingo Molnare309b412008-05-12 21:20:51 +0200826void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200827ftrace(struct trace_array *tr, struct trace_array_cpu *data,
828 unsigned long ip, unsigned long parent_ip, unsigned long flags)
829{
830 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200831 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200832}
833
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200834#ifdef CONFIG_MMIOTRACE
835void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
836 struct mmiotrace_rw *rw)
837{
838 struct trace_entry *entry;
839 unsigned long irq_flags;
840
Ingo Molnar801a1752008-05-12 21:20:58 +0200841 raw_local_irq_save(irq_flags);
842 __raw_spin_lock(&data->lock);
843
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200844 entry = tracing_get_trace_entry(tr, data);
845 tracing_generic_entry_update(entry, 0);
846 entry->type = TRACE_MMIO_RW;
847 entry->mmiorw = *rw;
Ingo Molnar801a1752008-05-12 21:20:58 +0200848
849 __raw_spin_unlock(&data->lock);
850 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200851
852 trace_wake_up();
853}
854
855void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
856 struct mmiotrace_map *map)
857{
858 struct trace_entry *entry;
859 unsigned long irq_flags;
860
Ingo Molnar801a1752008-05-12 21:20:58 +0200861 raw_local_irq_save(irq_flags);
862 __raw_spin_lock(&data->lock);
863
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200864 entry = tracing_get_trace_entry(tr, data);
865 tracing_generic_entry_update(entry, 0);
866 entry->type = TRACE_MMIO_MAP;
867 entry->mmiomap = *map;
Ingo Molnar801a1752008-05-12 21:20:58 +0200868
869 __raw_spin_unlock(&data->lock);
870 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200871
872 trace_wake_up();
873}
874#endif
875
Ingo Molnar86387f72008-05-12 21:20:51 +0200876void __trace_stack(struct trace_array *tr,
877 struct trace_array_cpu *data,
878 unsigned long flags,
879 int skip)
880{
881 struct trace_entry *entry;
882 struct stack_trace trace;
883
884 if (!(trace_flags & TRACE_ITER_STACKTRACE))
885 return;
886
887 entry = tracing_get_trace_entry(tr, data);
888 tracing_generic_entry_update(entry, flags);
889 entry->type = TRACE_STACK;
890
891 memset(&entry->stack, 0, sizeof(entry->stack));
892
893 trace.nr_entries = 0;
894 trace.max_entries = FTRACE_STACK_ENTRIES;
895 trace.skip = skip;
896 trace.entries = entry->stack.caller;
897
898 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200899}
900
Ingo Molnare309b412008-05-12 21:20:51 +0200901void
Ingo Molnara4feb832008-05-12 21:21:02 +0200902__trace_special(void *__tr, void *__data,
903 unsigned long arg1, unsigned long arg2, unsigned long arg3)
904{
905 struct trace_array_cpu *data = __data;
906 struct trace_array *tr = __tr;
907 struct trace_entry *entry;
908 unsigned long irq_flags;
909
910 raw_local_irq_save(irq_flags);
911 __raw_spin_lock(&data->lock);
912 entry = tracing_get_trace_entry(tr, data);
913 tracing_generic_entry_update(entry, 0);
914 entry->type = TRACE_SPECIAL;
915 entry->special.arg1 = arg1;
916 entry->special.arg2 = arg2;
917 entry->special.arg3 = arg3;
918 __trace_stack(tr, data, irq_flags, 4);
919 __raw_spin_unlock(&data->lock);
920 raw_local_irq_restore(irq_flags);
921
922 trace_wake_up();
923}
924
925void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200926tracing_sched_switch_trace(struct trace_array *tr,
927 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200928 struct task_struct *prev,
929 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200930 unsigned long flags)
931{
932 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200933 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200934
Steven Rostedt92205c22008-05-12 21:20:55 +0200935 raw_local_irq_save(irq_flags);
936 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200937 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200938 tracing_generic_entry_update(entry, flags);
939 entry->type = TRACE_CTX;
940 entry->ctx.prev_pid = prev->pid;
941 entry->ctx.prev_prio = prev->prio;
942 entry->ctx.prev_state = prev->state;
943 entry->ctx.next_pid = next->pid;
944 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200945 entry->ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200946 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200947 __raw_spin_unlock(&data->lock);
948 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200949}
950
Ingo Molnar57422792008-05-12 21:20:51 +0200951void
952tracing_sched_wakeup_trace(struct trace_array *tr,
953 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200954 struct task_struct *wakee,
955 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200956 unsigned long flags)
957{
958 struct trace_entry *entry;
959 unsigned long irq_flags;
960
Steven Rostedt92205c22008-05-12 21:20:55 +0200961 raw_local_irq_save(irq_flags);
962 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200963 entry = tracing_get_trace_entry(tr, data);
964 tracing_generic_entry_update(entry, flags);
965 entry->type = TRACE_WAKE;
966 entry->ctx.prev_pid = curr->pid;
967 entry->ctx.prev_prio = curr->prio;
968 entry->ctx.prev_state = curr->state;
969 entry->ctx.next_pid = wakee->pid;
970 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200971 entry->ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200972 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200973 __raw_spin_unlock(&data->lock);
974 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200975
976 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200977}
978
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200979#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200980static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200981function_trace_call(unsigned long ip, unsigned long parent_ip)
982{
983 struct trace_array *tr = &global_trace;
984 struct trace_array_cpu *data;
985 unsigned long flags;
986 long disabled;
987 int cpu;
988
989 if (unlikely(!tracer_enabled))
990 return;
991
992 local_irq_save(flags);
993 cpu = raw_smp_processor_id();
994 data = tr->data[cpu];
995 disabled = atomic_inc_return(&data->disabled);
996
997 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200998 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200999
1000 atomic_dec(&data->disabled);
1001 local_irq_restore(flags);
1002}
1003
1004static struct ftrace_ops trace_ops __read_mostly =
1005{
1006 .func = function_trace_call,
1007};
1008
Ingo Molnare309b412008-05-12 21:20:51 +02001009void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001010{
1011 register_ftrace_function(&trace_ops);
1012}
1013
Ingo Molnare309b412008-05-12 21:20:51 +02001014void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001015{
1016 unregister_ftrace_function(&trace_ops);
1017}
1018#endif
1019
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020enum trace_file_type {
1021 TRACE_FILE_LAT_FMT = 1,
1022};
1023
1024static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001025trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1026 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001027{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001028 struct page *page;
1029 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001030
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001031 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001032 iter->next_idx[cpu] >= data->trace_idx ||
1033 (data->trace_head == data->trace_tail &&
1034 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035 return NULL;
1036
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001037 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001038 /* Initialize the iterator for this cpu trace buffer */
1039 WARN_ON(!data->trace_tail);
1040 page = virt_to_page(data->trace_tail);
1041 iter->next_page[cpu] = &page->lru;
1042 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001043 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001045 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001046 BUG_ON(&data->trace_pages == &page->lru);
1047
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001048 array = page_address(page);
1049
Steven Rostedt93a588f2008-05-12 21:20:45 +02001050 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001051 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001052}
1053
Ingo Molnare309b412008-05-12 21:20:51 +02001054static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001055find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1056{
1057 struct trace_array *tr = iter->tr;
1058 struct trace_entry *ent, *next = NULL;
1059 int next_cpu = -1;
1060 int cpu;
1061
Steven Rostedtab464282008-05-12 21:21:00 +02001062 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001063 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001065 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001066 /*
1067 * Pick the entry with the smallest timestamp:
1068 */
1069 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001070 next = ent;
1071 next_cpu = cpu;
1072 }
1073 }
1074
1075 if (ent_cpu)
1076 *ent_cpu = next_cpu;
1077
1078 return next;
1079}
1080
Ingo Molnare309b412008-05-12 21:20:51 +02001081static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001082{
1083 iter->idx++;
1084 iter->next_idx[iter->cpu]++;
1085 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001086
Steven Rostedtb3806b42008-05-12 21:20:46 +02001087 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1088 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1089
1090 iter->next_page_idx[iter->cpu] = 0;
1091 iter->next_page[iter->cpu] =
1092 trace_next_list(data, iter->next_page[iter->cpu]);
1093 }
1094}
1095
Ingo Molnare309b412008-05-12 21:20:51 +02001096static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001097{
1098 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1099
1100 data->trace_tail_idx++;
1101 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1102 data->trace_tail = trace_next_page(data, data->trace_tail);
1103 data->trace_tail_idx = 0;
1104 }
1105
1106 /* Check if we empty it, then reset the index */
1107 if (data->trace_head == data->trace_tail &&
1108 data->trace_head_idx == data->trace_tail_idx)
1109 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001110}
1111
Ingo Molnare309b412008-05-12 21:20:51 +02001112static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001113{
1114 struct trace_entry *next;
1115 int next_cpu = -1;
1116
1117 next = find_next_entry(iter, &next_cpu);
1118
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001119 iter->prev_ent = iter->ent;
1120 iter->prev_cpu = iter->cpu;
1121
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122 iter->ent = next;
1123 iter->cpu = next_cpu;
1124
Steven Rostedtb3806b42008-05-12 21:20:46 +02001125 if (next)
1126 trace_iterator_increment(iter);
1127
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001128 return next ? iter : NULL;
1129}
1130
Ingo Molnare309b412008-05-12 21:20:51 +02001131static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132{
1133 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001134 void *last_ent = iter->ent;
1135 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001136 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137
1138 (*pos)++;
1139
1140 /* can't go backwards */
1141 if (iter->idx > i)
1142 return NULL;
1143
1144 if (iter->idx < 0)
1145 ent = find_next_entry_inc(iter);
1146 else
1147 ent = iter;
1148
1149 while (ent && iter->idx < i)
1150 ent = find_next_entry_inc(iter);
1151
1152 iter->pos = *pos;
1153
1154 if (last_ent && !ent)
1155 seq_puts(m, "\n\nvim:ft=help\n");
1156
1157 return ent;
1158}
1159
1160static void *s_start(struct seq_file *m, loff_t *pos)
1161{
1162 struct trace_iterator *iter = m->private;
1163 void *p = NULL;
1164 loff_t l = 0;
1165 int i;
1166
1167 mutex_lock(&trace_types_lock);
1168
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001169 if (!current_trace || current_trace != iter->trace) {
1170 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001171 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001172 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001173
1174 atomic_inc(&trace_record_cmdline_disabled);
1175
1176 /* let the tracer grab locks here if needed */
1177 if (current_trace->start)
1178 current_trace->start(iter);
1179
1180 if (*pos != iter->pos) {
1181 iter->ent = NULL;
1182 iter->cpu = 0;
1183 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001184 iter->prev_ent = NULL;
1185 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001186
Steven Rostedtab464282008-05-12 21:21:00 +02001187 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001189 iter->next_page[i] = NULL;
1190 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001191
1192 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1193 ;
1194
1195 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001196 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 p = s_next(m, p, &l);
1198 }
1199
1200 return p;
1201}
1202
1203static void s_stop(struct seq_file *m, void *p)
1204{
1205 struct trace_iterator *iter = m->private;
1206
1207 atomic_dec(&trace_record_cmdline_disabled);
1208
1209 /* let the tracer release locks here if needed */
1210 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1211 iter->trace->stop(iter);
1212
1213 mutex_unlock(&trace_types_lock);
1214}
1215
Steven Rostedtb3806b42008-05-12 21:20:46 +02001216static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001217seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001218{
1219#ifdef CONFIG_KALLSYMS
1220 char str[KSYM_SYMBOL_LEN];
1221
1222 kallsyms_lookup(address, NULL, NULL, NULL, str);
1223
Steven Rostedtb3806b42008-05-12 21:20:46 +02001224 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001226 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001227}
1228
Steven Rostedtb3806b42008-05-12 21:20:46 +02001229static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001230seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1231 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001232{
1233#ifdef CONFIG_KALLSYMS
1234 char str[KSYM_SYMBOL_LEN];
1235
1236 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001237 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001239 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240}
1241
1242#ifndef CONFIG_64BIT
1243# define IP_FMT "%08lx"
1244#else
1245# define IP_FMT "%016lx"
1246#endif
1247
Ingo Molnare309b412008-05-12 21:20:51 +02001248static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001249seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001251 int ret;
1252
1253 if (!ip)
1254 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255
1256 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001257 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001258 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001259 ret = seq_print_sym_short(s, "%s", ip);
1260
1261 if (!ret)
1262 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001263
1264 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001265 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1266 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001267}
1268
Ingo Molnare309b412008-05-12 21:20:51 +02001269static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001270{
1271 seq_puts(m, "# _------=> CPU# \n");
1272 seq_puts(m, "# / _-----=> irqs-off \n");
1273 seq_puts(m, "# | / _----=> need-resched \n");
1274 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1275 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1276 seq_puts(m, "# |||| / \n");
1277 seq_puts(m, "# ||||| delay \n");
1278 seq_puts(m, "# cmd pid ||||| time | caller \n");
1279 seq_puts(m, "# \\ / ||||| \\ | / \n");
1280}
1281
Ingo Molnare309b412008-05-12 21:20:51 +02001282static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283{
1284 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1285 seq_puts(m, "# | | | | |\n");
1286}
1287
1288
Ingo Molnare309b412008-05-12 21:20:51 +02001289static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1291{
1292 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1293 struct trace_array *tr = iter->tr;
1294 struct trace_array_cpu *data = tr->data[tr->cpu];
1295 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001296 unsigned long total = 0;
1297 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001298 int cpu;
1299 const char *name = "preemption";
1300
1301 if (type)
1302 name = type->name;
1303
Steven Rostedtab464282008-05-12 21:21:00 +02001304 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001305 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001306 total += tr->data[cpu]->trace_idx;
1307 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001309 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001310 entries += tr->data[cpu]->trace_idx;
1311 }
1312 }
1313
1314 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1315 name, UTS_RELEASE);
1316 seq_puts(m, "-----------------------------------"
1317 "---------------------------------\n");
1318 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1319 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001320 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001322 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 tr->cpu,
1324#if defined(CONFIG_PREEMPT_NONE)
1325 "server",
1326#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1327 "desktop",
1328#elif defined(CONFIG_PREEMPT_DESKTOP)
1329 "preempt",
1330#else
1331 "unknown",
1332#endif
1333 /* These are reserved for later use */
1334 0, 0, 0, 0);
1335#ifdef CONFIG_SMP
1336 seq_printf(m, " #P:%d)\n", num_online_cpus());
1337#else
1338 seq_puts(m, ")\n");
1339#endif
1340 seq_puts(m, " -----------------\n");
1341 seq_printf(m, " | task: %.16s-%d "
1342 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1343 data->comm, data->pid, data->uid, data->nice,
1344 data->policy, data->rt_priority);
1345 seq_puts(m, " -----------------\n");
1346
1347 if (data->critical_start) {
1348 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001349 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1350 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001351 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001352 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1353 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354 seq_puts(m, "\n");
1355 }
1356
1357 seq_puts(m, "\n");
1358}
1359
Ingo Molnare309b412008-05-12 21:20:51 +02001360static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001361lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362{
1363 int hardirq, softirq;
1364 char *comm;
1365
1366 comm = trace_find_cmdline(entry->pid);
1367
Steven Rostedt214023c2008-05-12 21:20:46 +02001368 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1369 trace_seq_printf(s, "%d", cpu);
1370 trace_seq_printf(s, "%c%c",
1371 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1372 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373
1374 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1375 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001376 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001377 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001378 } else {
1379 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001380 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001381 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001382 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001383 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001385 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386 }
1387 }
1388
1389 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001390 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001391 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001392 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393}
1394
1395unsigned long preempt_mark_thresh = 100;
1396
Ingo Molnare309b412008-05-12 21:20:51 +02001397static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001398lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001399 unsigned long rel_usecs)
1400{
Steven Rostedt214023c2008-05-12 21:20:46 +02001401 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001402 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001403 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001404 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001405 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001407 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001408}
1409
1410static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1411
Ingo Molnare309b412008-05-12 21:20:51 +02001412static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001413print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414{
Steven Rostedt214023c2008-05-12 21:20:46 +02001415 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1417 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1418 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1419 struct trace_entry *entry = iter->ent;
1420 unsigned long abs_usecs;
1421 unsigned long rel_usecs;
1422 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001423 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001424 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001425 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426
1427 if (!next_entry)
1428 next_entry = entry;
1429 rel_usecs = ns2usecs(next_entry->t - entry->t);
1430 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1431
1432 if (verbose) {
1433 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001434 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1435 " %ld.%03ldms (+%ld.%03ldms): ",
1436 comm,
1437 entry->pid, cpu, entry->flags,
1438 entry->preempt_count, trace_idx,
1439 ns2usecs(entry->t),
1440 abs_usecs/1000,
1441 abs_usecs % 1000, rel_usecs/1000,
1442 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001443 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001444 lat_print_generic(s, entry, cpu);
1445 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001446 }
1447 switch (entry->type) {
1448 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001449 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1450 trace_seq_puts(s, " (");
1451 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1452 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453 break;
1454 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001455 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001456 T = entry->ctx.next_state < sizeof(state_to_char) ?
1457 state_to_char[entry->ctx.next_state] : 'X';
1458
Ankita Gargd17d9692008-05-12 21:20:58 +02001459 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1460 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001461 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001462 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001463 entry->ctx.prev_pid,
1464 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001465 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001466 entry->ctx.next_pid,
1467 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001468 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001470 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001471 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001472 entry->special.arg1,
1473 entry->special.arg2,
1474 entry->special.arg3);
1475 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001476 case TRACE_STACK:
1477 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1478 if (i)
1479 trace_seq_puts(s, " <= ");
1480 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1481 }
1482 trace_seq_puts(s, "\n");
1483 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001484 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001485 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001486 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001487 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488}
1489
Ingo Molnare309b412008-05-12 21:20:51 +02001490static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001491{
Steven Rostedt214023c2008-05-12 21:20:46 +02001492 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001493 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001494 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495 unsigned long usec_rem;
1496 unsigned long long t;
1497 unsigned long secs;
1498 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001499 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001500 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001501 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001502
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001503 entry = iter->ent;
1504
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001505 comm = trace_find_cmdline(iter->ent->pid);
1506
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001507 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001508 usec_rem = do_div(t, 1000000ULL);
1509 secs = (unsigned long)t;
1510
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001511 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1512 if (!ret)
1513 return 0;
1514 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1515 if (!ret)
1516 return 0;
1517 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1518 if (!ret)
1519 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001520
1521 switch (entry->type) {
1522 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001523 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1524 if (!ret)
1525 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1527 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001528 ret = trace_seq_printf(s, " <-");
1529 if (!ret)
1530 return 0;
1531 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1532 sym_flags);
1533 if (!ret)
1534 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001536 ret = trace_seq_printf(s, "\n");
1537 if (!ret)
1538 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539 break;
1540 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001541 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1543 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001544 T = entry->ctx.next_state < sizeof(state_to_char) ?
1545 state_to_char[entry->ctx.next_state] : 'X';
1546 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001547 entry->ctx.prev_pid,
1548 entry->ctx.prev_prio,
1549 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001550 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001551 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001552 entry->ctx.next_prio,
1553 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001554 if (!ret)
1555 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001556 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001557 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001558 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001559 entry->special.arg1,
1560 entry->special.arg2,
1561 entry->special.arg3);
1562 if (!ret)
1563 return 0;
1564 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001565 case TRACE_STACK:
1566 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1567 if (i) {
1568 ret = trace_seq_puts(s, " <= ");
1569 if (!ret)
1570 return 0;
1571 }
1572 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1573 sym_flags);
1574 if (!ret)
1575 return 0;
1576 }
1577 ret = trace_seq_puts(s, "\n");
1578 if (!ret)
1579 return 0;
1580 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001581 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001582 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583}
1584
Ingo Molnare309b412008-05-12 21:20:51 +02001585static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001586{
1587 struct trace_seq *s = &iter->seq;
1588 struct trace_entry *entry;
1589 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001590 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001591
1592 entry = iter->ent;
1593
1594 ret = trace_seq_printf(s, "%d %d %llu ",
1595 entry->pid, iter->cpu, entry->t);
1596 if (!ret)
1597 return 0;
1598
1599 switch (entry->type) {
1600 case TRACE_FN:
1601 ret = trace_seq_printf(s, "%x %x\n",
1602 entry->fn.ip, entry->fn.parent_ip);
1603 if (!ret)
1604 return 0;
1605 break;
1606 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001607 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001608 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1609 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001610 T = entry->ctx.next_state < sizeof(state_to_char) ?
1611 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001612 if (entry->type == TRACE_WAKE)
1613 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001614 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001615 entry->ctx.prev_pid,
1616 entry->ctx.prev_prio,
1617 S,
1618 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001619 entry->ctx.next_prio,
1620 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001621 if (!ret)
1622 return 0;
1623 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001624 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001625 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001626 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001627 entry->special.arg1,
1628 entry->special.arg2,
1629 entry->special.arg3);
1630 if (!ret)
1631 return 0;
1632 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001633 }
1634 return 1;
1635}
1636
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001637#define SEQ_PUT_FIELD_RET(s, x) \
1638do { \
1639 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1640 return 0; \
1641} while (0)
1642
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001643#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1644do { \
1645 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1646 return 0; \
1647} while (0)
1648
Ingo Molnare309b412008-05-12 21:20:51 +02001649static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001650{
1651 struct trace_seq *s = &iter->seq;
1652 unsigned char newline = '\n';
1653 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001654 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001655
1656 entry = iter->ent;
1657
1658 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1659 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1660 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1661
1662 switch (entry->type) {
1663 case TRACE_FN:
1664 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1665 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1666 break;
1667 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001668 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001669 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1670 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001671 T = entry->ctx.next_state < sizeof(state_to_char) ?
1672 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001673 if (entry->type == TRACE_WAKE)
1674 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001675 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1676 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1677 SEQ_PUT_HEX_FIELD_RET(s, S);
1678 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1679 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1680 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001681 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001682 break;
1683 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001684 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001685 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1686 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1687 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1688 break;
1689 }
1690 SEQ_PUT_FIELD_RET(s, newline);
1691
1692 return 1;
1693}
1694
Ingo Molnare309b412008-05-12 21:20:51 +02001695static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001696{
1697 struct trace_seq *s = &iter->seq;
1698 struct trace_entry *entry;
1699
1700 entry = iter->ent;
1701
1702 SEQ_PUT_FIELD_RET(s, entry->pid);
1703 SEQ_PUT_FIELD_RET(s, entry->cpu);
1704 SEQ_PUT_FIELD_RET(s, entry->t);
1705
1706 switch (entry->type) {
1707 case TRACE_FN:
1708 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1709 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1710 break;
1711 case TRACE_CTX:
1712 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1713 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1714 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1715 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1716 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001717 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001718 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001719 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001720 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001721 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1722 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1723 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1724 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001725 }
1726 return 1;
1727}
1728
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001729static int trace_empty(struct trace_iterator *iter)
1730{
1731 struct trace_array_cpu *data;
1732 int cpu;
1733
Steven Rostedtab464282008-05-12 21:21:00 +02001734 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001735 data = iter->tr->data[cpu];
1736
Steven Rostedtb3806b42008-05-12 21:20:46 +02001737 if (head_page(data) && data->trace_idx &&
1738 (data->trace_tail != data->trace_head ||
1739 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001740 return 0;
1741 }
1742 return 1;
1743}
1744
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001745static int print_trace_line(struct trace_iterator *iter)
1746{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001747 if (iter->trace && iter->trace->print_line)
1748 return iter->trace->print_line(iter);
1749
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001750 if (trace_flags & TRACE_ITER_BIN)
1751 return print_bin_fmt(iter);
1752
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001753 if (trace_flags & TRACE_ITER_HEX)
1754 return print_hex_fmt(iter);
1755
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001756 if (trace_flags & TRACE_ITER_RAW)
1757 return print_raw_fmt(iter);
1758
1759 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1760 return print_lat_fmt(iter, iter->idx, iter->cpu);
1761
1762 return print_trace_fmt(iter);
1763}
1764
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765static int s_show(struct seq_file *m, void *v)
1766{
1767 struct trace_iterator *iter = v;
1768
1769 if (iter->ent == NULL) {
1770 if (iter->tr) {
1771 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1772 seq_puts(m, "#\n");
1773 }
1774 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1775 /* print nothing if the buffers are empty */
1776 if (trace_empty(iter))
1777 return 0;
1778 print_trace_header(m, iter);
1779 if (!(trace_flags & TRACE_ITER_VERBOSE))
1780 print_lat_help_header(m);
1781 } else {
1782 if (!(trace_flags & TRACE_ITER_VERBOSE))
1783 print_func_help_header(m);
1784 }
1785 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001786 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001787 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001788 }
1789
1790 return 0;
1791}
1792
1793static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001794 .start = s_start,
1795 .next = s_next,
1796 .stop = s_stop,
1797 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001798};
1799
Ingo Molnare309b412008-05-12 21:20:51 +02001800static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001801__tracing_open(struct inode *inode, struct file *file, int *ret)
1802{
1803 struct trace_iterator *iter;
1804
Steven Rostedt60a11772008-05-12 21:20:44 +02001805 if (tracing_disabled) {
1806 *ret = -ENODEV;
1807 return NULL;
1808 }
1809
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001810 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1811 if (!iter) {
1812 *ret = -ENOMEM;
1813 goto out;
1814 }
1815
1816 mutex_lock(&trace_types_lock);
1817 if (current_trace && current_trace->print_max)
1818 iter->tr = &max_tr;
1819 else
1820 iter->tr = inode->i_private;
1821 iter->trace = current_trace;
1822 iter->pos = -1;
1823
1824 /* TODO stop tracer */
1825 *ret = seq_open(file, &tracer_seq_ops);
1826 if (!*ret) {
1827 struct seq_file *m = file->private_data;
1828 m->private = iter;
1829
1830 /* stop the trace while dumping */
1831 if (iter->tr->ctrl)
1832 tracer_enabled = 0;
1833
1834 if (iter->trace && iter->trace->open)
1835 iter->trace->open(iter);
1836 } else {
1837 kfree(iter);
1838 iter = NULL;
1839 }
1840 mutex_unlock(&trace_types_lock);
1841
1842 out:
1843 return iter;
1844}
1845
1846int tracing_open_generic(struct inode *inode, struct file *filp)
1847{
Steven Rostedt60a11772008-05-12 21:20:44 +02001848 if (tracing_disabled)
1849 return -ENODEV;
1850
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001851 filp->private_data = inode->i_private;
1852 return 0;
1853}
1854
1855int tracing_release(struct inode *inode, struct file *file)
1856{
1857 struct seq_file *m = (struct seq_file *)file->private_data;
1858 struct trace_iterator *iter = m->private;
1859
1860 mutex_lock(&trace_types_lock);
1861 if (iter->trace && iter->trace->close)
1862 iter->trace->close(iter);
1863
1864 /* reenable tracing if it was previously enabled */
1865 if (iter->tr->ctrl)
1866 tracer_enabled = 1;
1867 mutex_unlock(&trace_types_lock);
1868
1869 seq_release(inode, file);
1870 kfree(iter);
1871 return 0;
1872}
1873
1874static int tracing_open(struct inode *inode, struct file *file)
1875{
1876 int ret;
1877
1878 __tracing_open(inode, file, &ret);
1879
1880 return ret;
1881}
1882
1883static int tracing_lt_open(struct inode *inode, struct file *file)
1884{
1885 struct trace_iterator *iter;
1886 int ret;
1887
1888 iter = __tracing_open(inode, file, &ret);
1889
1890 if (!ret)
1891 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1892
1893 return ret;
1894}
1895
1896
Ingo Molnare309b412008-05-12 21:20:51 +02001897static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001898t_next(struct seq_file *m, void *v, loff_t *pos)
1899{
1900 struct tracer *t = m->private;
1901
1902 (*pos)++;
1903
1904 if (t)
1905 t = t->next;
1906
1907 m->private = t;
1908
1909 return t;
1910}
1911
1912static void *t_start(struct seq_file *m, loff_t *pos)
1913{
1914 struct tracer *t = m->private;
1915 loff_t l = 0;
1916
1917 mutex_lock(&trace_types_lock);
1918 for (; t && l < *pos; t = t_next(m, t, &l))
1919 ;
1920
1921 return t;
1922}
1923
1924static void t_stop(struct seq_file *m, void *p)
1925{
1926 mutex_unlock(&trace_types_lock);
1927}
1928
1929static int t_show(struct seq_file *m, void *v)
1930{
1931 struct tracer *t = v;
1932
1933 if (!t)
1934 return 0;
1935
1936 seq_printf(m, "%s", t->name);
1937 if (t->next)
1938 seq_putc(m, ' ');
1939 else
1940 seq_putc(m, '\n');
1941
1942 return 0;
1943}
1944
1945static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001946 .start = t_start,
1947 .next = t_next,
1948 .stop = t_stop,
1949 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001950};
1951
1952static int show_traces_open(struct inode *inode, struct file *file)
1953{
1954 int ret;
1955
Steven Rostedt60a11772008-05-12 21:20:44 +02001956 if (tracing_disabled)
1957 return -ENODEV;
1958
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001959 ret = seq_open(file, &show_traces_seq_ops);
1960 if (!ret) {
1961 struct seq_file *m = file->private_data;
1962 m->private = trace_types;
1963 }
1964
1965 return ret;
1966}
1967
1968static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001969 .open = tracing_open,
1970 .read = seq_read,
1971 .llseek = seq_lseek,
1972 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001973};
1974
1975static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001976 .open = tracing_lt_open,
1977 .read = seq_read,
1978 .llseek = seq_lseek,
1979 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001980};
1981
1982static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001983 .open = show_traces_open,
1984 .read = seq_read,
1985 .release = seq_release,
1986};
1987
Ingo Molnar36dfe922008-05-12 21:20:52 +02001988/*
1989 * Only trace on a CPU if the bitmask is set:
1990 */
1991static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1992
1993/*
1994 * When tracing/tracing_cpu_mask is modified then this holds
1995 * the new bitmask we are about to install:
1996 */
1997static cpumask_t tracing_cpumask_new;
1998
1999/*
2000 * The tracer itself will not take this lock, but still we want
2001 * to provide a consistent cpumask to user-space:
2002 */
2003static DEFINE_MUTEX(tracing_cpumask_update_lock);
2004
2005/*
2006 * Temporary storage for the character representation of the
2007 * CPU bitmask (and one more byte for the newline):
2008 */
2009static char mask_str[NR_CPUS + 1];
2010
Ingo Molnarc7078de2008-05-12 21:20:52 +02002011static ssize_t
2012tracing_cpumask_read(struct file *filp, char __user *ubuf,
2013 size_t count, loff_t *ppos)
2014{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002015 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002016
2017 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002018
2019 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2020 if (count - len < 2) {
2021 count = -EINVAL;
2022 goto out_err;
2023 }
2024 len += sprintf(mask_str + len, "\n");
2025 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2026
2027out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002028 mutex_unlock(&tracing_cpumask_update_lock);
2029
2030 return count;
2031}
2032
2033static ssize_t
2034tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2035 size_t count, loff_t *ppos)
2036{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002037 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002038
2039 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002040 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2041 if (err)
2042 goto err_unlock;
2043
Steven Rostedt92205c22008-05-12 21:20:55 +02002044 raw_local_irq_disable();
2045 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002046 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002047 /*
2048 * Increase/decrease the disabled counter if we are
2049 * about to flip a bit in the cpumask:
2050 */
2051 if (cpu_isset(cpu, tracing_cpumask) &&
2052 !cpu_isset(cpu, tracing_cpumask_new)) {
2053 atomic_inc(&global_trace.data[cpu]->disabled);
2054 }
2055 if (!cpu_isset(cpu, tracing_cpumask) &&
2056 cpu_isset(cpu, tracing_cpumask_new)) {
2057 atomic_dec(&global_trace.data[cpu]->disabled);
2058 }
2059 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002060 __raw_spin_unlock(&ftrace_max_lock);
2061 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002062
2063 tracing_cpumask = tracing_cpumask_new;
2064
Ingo Molnarc7078de2008-05-12 21:20:52 +02002065 mutex_unlock(&tracing_cpumask_update_lock);
2066
Ingo Molnarc7078de2008-05-12 21:20:52 +02002067 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002068
2069err_unlock:
2070 mutex_unlock(&tracing_cpumask_update_lock);
2071
2072 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002073}
2074
2075static struct file_operations tracing_cpumask_fops = {
2076 .open = tracing_open_generic,
2077 .read = tracing_cpumask_read,
2078 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002079};
2080
2081static ssize_t
2082tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2083 size_t cnt, loff_t *ppos)
2084{
2085 char *buf;
2086 int r = 0;
2087 int len = 0;
2088 int i;
2089
2090 /* calulate max size */
2091 for (i = 0; trace_options[i]; i++) {
2092 len += strlen(trace_options[i]);
2093 len += 3; /* "no" and space */
2094 }
2095
2096 /* +2 for \n and \0 */
2097 buf = kmalloc(len + 2, GFP_KERNEL);
2098 if (!buf)
2099 return -ENOMEM;
2100
2101 for (i = 0; trace_options[i]; i++) {
2102 if (trace_flags & (1 << i))
2103 r += sprintf(buf + r, "%s ", trace_options[i]);
2104 else
2105 r += sprintf(buf + r, "no%s ", trace_options[i]);
2106 }
2107
2108 r += sprintf(buf + r, "\n");
2109 WARN_ON(r >= len + 2);
2110
Ingo Molnar36dfe922008-05-12 21:20:52 +02002111 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002112
2113 kfree(buf);
2114
2115 return r;
2116}
2117
2118static ssize_t
2119tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2120 size_t cnt, loff_t *ppos)
2121{
2122 char buf[64];
2123 char *cmp = buf;
2124 int neg = 0;
2125 int i;
2126
Steven Rostedtcffae432008-05-12 21:21:00 +02002127 if (cnt >= sizeof(buf))
2128 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002129
2130 if (copy_from_user(&buf, ubuf, cnt))
2131 return -EFAULT;
2132
2133 buf[cnt] = 0;
2134
2135 if (strncmp(buf, "no", 2) == 0) {
2136 neg = 1;
2137 cmp += 2;
2138 }
2139
2140 for (i = 0; trace_options[i]; i++) {
2141 int len = strlen(trace_options[i]);
2142
2143 if (strncmp(cmp, trace_options[i], len) == 0) {
2144 if (neg)
2145 trace_flags &= ~(1 << i);
2146 else
2147 trace_flags |= (1 << i);
2148 break;
2149 }
2150 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002151 /*
2152 * If no option could be set, return an error:
2153 */
2154 if (!trace_options[i])
2155 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002156
2157 filp->f_pos += cnt;
2158
2159 return cnt;
2160}
2161
2162static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002163 .open = tracing_open_generic,
2164 .read = tracing_iter_ctrl_read,
2165 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166};
2167
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002168static const char readme_msg[] =
2169 "tracing mini-HOWTO:\n\n"
2170 "# mkdir /debug\n"
2171 "# mount -t debugfs nodev /debug\n\n"
2172 "# cat /debug/tracing/available_tracers\n"
2173 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2174 "# cat /debug/tracing/current_tracer\n"
2175 "none\n"
2176 "# echo sched_switch > /debug/tracing/current_tracer\n"
2177 "# cat /debug/tracing/current_tracer\n"
2178 "sched_switch\n"
2179 "# cat /debug/tracing/iter_ctrl\n"
2180 "noprint-parent nosym-offset nosym-addr noverbose\n"
2181 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2182 "# echo 1 > /debug/tracing/tracing_enabled\n"
2183 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2184 "echo 0 > /debug/tracing/tracing_enabled\n"
2185;
2186
2187static ssize_t
2188tracing_readme_read(struct file *filp, char __user *ubuf,
2189 size_t cnt, loff_t *ppos)
2190{
2191 return simple_read_from_buffer(ubuf, cnt, ppos,
2192 readme_msg, strlen(readme_msg));
2193}
2194
2195static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002196 .open = tracing_open_generic,
2197 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002198};
2199
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002200static ssize_t
2201tracing_ctrl_read(struct file *filp, char __user *ubuf,
2202 size_t cnt, loff_t *ppos)
2203{
2204 struct trace_array *tr = filp->private_data;
2205 char buf[64];
2206 int r;
2207
2208 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002209 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002210}
2211
2212static ssize_t
2213tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2214 size_t cnt, loff_t *ppos)
2215{
2216 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002217 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002218 long val;
2219 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002220
Steven Rostedtcffae432008-05-12 21:21:00 +02002221 if (cnt >= sizeof(buf))
2222 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002223
2224 if (copy_from_user(&buf, ubuf, cnt))
2225 return -EFAULT;
2226
2227 buf[cnt] = 0;
2228
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002229 ret = strict_strtoul(buf, 10, &val);
2230 if (ret < 0)
2231 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002232
2233 val = !!val;
2234
2235 mutex_lock(&trace_types_lock);
2236 if (tr->ctrl ^ val) {
2237 if (val)
2238 tracer_enabled = 1;
2239 else
2240 tracer_enabled = 0;
2241
2242 tr->ctrl = val;
2243
2244 if (current_trace && current_trace->ctrl_update)
2245 current_trace->ctrl_update(tr);
2246 }
2247 mutex_unlock(&trace_types_lock);
2248
2249 filp->f_pos += cnt;
2250
2251 return cnt;
2252}
2253
2254static ssize_t
2255tracing_set_trace_read(struct file *filp, char __user *ubuf,
2256 size_t cnt, loff_t *ppos)
2257{
2258 char buf[max_tracer_type_len+2];
2259 int r;
2260
2261 mutex_lock(&trace_types_lock);
2262 if (current_trace)
2263 r = sprintf(buf, "%s\n", current_trace->name);
2264 else
2265 r = sprintf(buf, "\n");
2266 mutex_unlock(&trace_types_lock);
2267
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002268 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002269}
2270
2271static ssize_t
2272tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2273 size_t cnt, loff_t *ppos)
2274{
2275 struct trace_array *tr = &global_trace;
2276 struct tracer *t;
2277 char buf[max_tracer_type_len+1];
2278 int i;
2279
2280 if (cnt > max_tracer_type_len)
2281 cnt = max_tracer_type_len;
2282
2283 if (copy_from_user(&buf, ubuf, cnt))
2284 return -EFAULT;
2285
2286 buf[cnt] = 0;
2287
2288 /* strip ending whitespace. */
2289 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2290 buf[i] = 0;
2291
2292 mutex_lock(&trace_types_lock);
2293 for (t = trace_types; t; t = t->next) {
2294 if (strcmp(t->name, buf) == 0)
2295 break;
2296 }
2297 if (!t || t == current_trace)
2298 goto out;
2299
2300 if (current_trace && current_trace->reset)
2301 current_trace->reset(tr);
2302
2303 current_trace = t;
2304 if (t->init)
2305 t->init(tr);
2306
2307 out:
2308 mutex_unlock(&trace_types_lock);
2309
2310 filp->f_pos += cnt;
2311
2312 return cnt;
2313}
2314
2315static ssize_t
2316tracing_max_lat_read(struct file *filp, char __user *ubuf,
2317 size_t cnt, loff_t *ppos)
2318{
2319 unsigned long *ptr = filp->private_data;
2320 char buf[64];
2321 int r;
2322
Steven Rostedtcffae432008-05-12 21:21:00 +02002323 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002325 if (r > sizeof(buf))
2326 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328}
2329
2330static ssize_t
2331tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2332 size_t cnt, loff_t *ppos)
2333{
2334 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002335 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002336 long val;
2337 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002338
Steven Rostedtcffae432008-05-12 21:21:00 +02002339 if (cnt >= sizeof(buf))
2340 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341
2342 if (copy_from_user(&buf, ubuf, cnt))
2343 return -EFAULT;
2344
2345 buf[cnt] = 0;
2346
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002347 ret = strict_strtoul(buf, 10, &val);
2348 if (ret < 0)
2349 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002350
2351 *ptr = val * 1000;
2352
2353 return cnt;
2354}
2355
Steven Rostedtb3806b42008-05-12 21:20:46 +02002356static atomic_t tracing_reader;
2357
2358static int tracing_open_pipe(struct inode *inode, struct file *filp)
2359{
2360 struct trace_iterator *iter;
2361
2362 if (tracing_disabled)
2363 return -ENODEV;
2364
2365 /* We only allow for reader of the pipe */
2366 if (atomic_inc_return(&tracing_reader) != 1) {
2367 atomic_dec(&tracing_reader);
2368 return -EBUSY;
2369 }
2370
2371 /* create a buffer to store the information to pass to userspace */
2372 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2373 if (!iter)
2374 return -ENOMEM;
2375
Steven Rostedt107bad82008-05-12 21:21:01 +02002376 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002377 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002378 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002379 filp->private_data = iter;
2380
Steven Rostedt107bad82008-05-12 21:21:01 +02002381 if (iter->trace->pipe_open)
2382 iter->trace->pipe_open(iter);
2383 mutex_unlock(&trace_types_lock);
2384
Steven Rostedtb3806b42008-05-12 21:20:46 +02002385 return 0;
2386}
2387
2388static int tracing_release_pipe(struct inode *inode, struct file *file)
2389{
2390 struct trace_iterator *iter = file->private_data;
2391
2392 kfree(iter);
2393 atomic_dec(&tracing_reader);
2394
2395 return 0;
2396}
2397
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002398static unsigned int
2399tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2400{
2401 struct trace_iterator *iter = filp->private_data;
2402
2403 if (trace_flags & TRACE_ITER_BLOCK) {
2404 /*
2405 * Always select as readable when in blocking mode
2406 */
2407 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002408 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002409 if (!trace_empty(iter))
2410 return POLLIN | POLLRDNORM;
2411 poll_wait(filp, &trace_wait, poll_table);
2412 if (!trace_empty(iter))
2413 return POLLIN | POLLRDNORM;
2414
2415 return 0;
2416 }
2417}
2418
Steven Rostedtb3806b42008-05-12 21:20:46 +02002419/*
2420 * Consumer reader.
2421 */
2422static ssize_t
2423tracing_read_pipe(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2425{
2426 struct trace_iterator *iter = filp->private_data;
2427 struct trace_array_cpu *data;
2428 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002429 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002430#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002431 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002432#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002433 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002434 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002435
2436 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002437 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2438 if (sret != -EBUSY)
2439 return sret;
2440 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002441
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002442 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002443
Steven Rostedt107bad82008-05-12 21:21:01 +02002444 mutex_lock(&trace_types_lock);
2445 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002446 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2447 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002448 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002449 }
2450
Steven Rostedtb3806b42008-05-12 21:20:46 +02002451 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002452
Steven Rostedt107bad82008-05-12 21:21:01 +02002453 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002454 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002455 goto out;
2456 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002457
Steven Rostedtb3806b42008-05-12 21:20:46 +02002458 /*
2459 * This is a make-shift waitqueue. The reason we don't use
2460 * an actual wait queue is because:
2461 * 1) we only ever have one waiter
2462 * 2) the tracing, traces all functions, we don't want
2463 * the overhead of calling wake_up and friends
2464 * (and tracing them too)
2465 * Anyway, this is really very primitive wakeup.
2466 */
2467 set_current_state(TASK_INTERRUPTIBLE);
2468 iter->tr->waiter = current;
2469
Steven Rostedt107bad82008-05-12 21:21:01 +02002470 mutex_unlock(&trace_types_lock);
2471
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002472 /* sleep for 100 msecs, and try again. */
2473 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002474
Steven Rostedt107bad82008-05-12 21:21:01 +02002475 mutex_lock(&trace_types_lock);
2476
Steven Rostedtb3806b42008-05-12 21:20:46 +02002477 iter->tr->waiter = NULL;
2478
Steven Rostedt107bad82008-05-12 21:21:01 +02002479 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002480 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002481 goto out;
2482 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002483
Steven Rostedt84527992008-05-12 21:20:58 +02002484 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002485 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002486
Steven Rostedtb3806b42008-05-12 21:20:46 +02002487 /*
2488 * We block until we read something and tracing is disabled.
2489 * We still block if tracing is disabled, but we have never
2490 * read anything. This allows a user to cat this file, and
2491 * then enable tracing. But after we have read something,
2492 * we give an EOF when tracing is again disabled.
2493 *
2494 * iter->pos will be 0 if we haven't read anything.
2495 */
2496 if (!tracer_enabled && iter->pos)
2497 break;
2498
2499 continue;
2500 }
2501
2502 /* stop when tracing is finished */
2503 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002504 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002505
2506 if (cnt >= PAGE_SIZE)
2507 cnt = PAGE_SIZE - 1;
2508
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002509 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002510 memset(&iter->seq, 0,
2511 sizeof(struct trace_iterator) -
2512 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002513 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002514
2515 /*
2516 * We need to stop all tracing on all CPUS to read the
2517 * the next buffer. This is a bit expensive, but is
2518 * not done often. We fill all what we can read,
2519 * and then release the locks again.
2520 */
2521
2522 cpus_clear(mask);
2523 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002524#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002525 ftrace_save = ftrace_enabled;
2526 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002527#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002528 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002529 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002530 data = iter->tr->data[cpu];
2531
2532 if (!head_page(data) || !data->trace_idx)
2533 continue;
2534
2535 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002536 cpu_set(cpu, mask);
2537 }
2538
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002539 for_each_cpu_mask(cpu, mask) {
2540 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002541 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002542
2543 if (data->overrun > iter->last_overrun[cpu])
2544 iter->overrun[cpu] +=
2545 data->overrun - iter->last_overrun[cpu];
2546 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002547 }
2548
Steven Rostedt088b1e422008-05-12 21:20:48 +02002549 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002550 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002551 int len = iter->seq.len;
2552
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002553 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002554 if (!ret) {
2555 /* don't print partial lines */
2556 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002557 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002558 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002559
2560 trace_consume(iter);
2561
2562 if (iter->seq.len >= cnt)
2563 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002564 }
2565
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002566 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002567 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002568 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002569 }
2570
2571 for_each_cpu_mask(cpu, mask) {
2572 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002573 atomic_dec(&data->disabled);
2574 }
Ingo Molnar25770462008-05-12 21:20:49 +02002575#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002576 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002577#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002578 local_irq_restore(flags);
2579
2580 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002581 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2582 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002583 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002584 if (sret == -EBUSY)
2585 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002586
Steven Rostedt107bad82008-05-12 21:21:01 +02002587out:
2588 mutex_unlock(&trace_types_lock);
2589
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002590 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002591}
2592
Steven Rostedta98a3c32008-05-12 21:20:59 +02002593static ssize_t
2594tracing_entries_read(struct file *filp, char __user *ubuf,
2595 size_t cnt, loff_t *ppos)
2596{
2597 struct trace_array *tr = filp->private_data;
2598 char buf[64];
2599 int r;
2600
2601 r = sprintf(buf, "%lu\n", tr->entries);
2602 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2603}
2604
2605static ssize_t
2606tracing_entries_write(struct file *filp, const char __user *ubuf,
2607 size_t cnt, loff_t *ppos)
2608{
2609 unsigned long val;
2610 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002611 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002612
Steven Rostedtcffae432008-05-12 21:21:00 +02002613 if (cnt >= sizeof(buf))
2614 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002615
2616 if (copy_from_user(&buf, ubuf, cnt))
2617 return -EFAULT;
2618
2619 buf[cnt] = 0;
2620
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002621 ret = strict_strtoul(buf, 10, &val);
2622 if (ret < 0)
2623 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002624
2625 /* must have at least 1 entry */
2626 if (!val)
2627 return -EINVAL;
2628
2629 mutex_lock(&trace_types_lock);
2630
2631 if (current_trace != &no_tracer) {
2632 cnt = -EBUSY;
2633 pr_info("ftrace: set current_tracer to none"
2634 " before modifying buffer size\n");
2635 goto out;
2636 }
2637
2638 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002639 long pages_requested;
2640 unsigned long freeable_pages;
2641
2642 /* make sure we have enough memory before mapping */
2643 pages_requested =
2644 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2645
2646 /* account for each buffer (and max_tr) */
2647 pages_requested *= tracing_nr_buffers * 2;
2648
2649 /* Check for overflow */
2650 if (pages_requested < 0) {
2651 cnt = -ENOMEM;
2652 goto out;
2653 }
2654
2655 freeable_pages = determine_dirtyable_memory();
2656
2657 /* we only allow to request 1/4 of useable memory */
2658 if (pages_requested >
2659 ((freeable_pages + tracing_pages_allocated) / 4)) {
2660 cnt = -ENOMEM;
2661 goto out;
2662 }
2663
Steven Rostedta98a3c32008-05-12 21:20:59 +02002664 while (global_trace.entries < val) {
2665 if (trace_alloc_page()) {
2666 cnt = -ENOMEM;
2667 goto out;
2668 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002669 /* double check that we don't go over the known pages */
2670 if (tracing_pages_allocated > pages_requested)
2671 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002672 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002673
Steven Rostedta98a3c32008-05-12 21:20:59 +02002674 } else {
2675 /* include the number of entries in val (inc of page entries) */
2676 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2677 trace_free_page();
2678 }
2679
2680 filp->f_pos += cnt;
2681
2682 out:
2683 max_tr.entries = global_trace.entries;
2684 mutex_unlock(&trace_types_lock);
2685
2686 return cnt;
2687}
2688
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002689static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002690 .open = tracing_open_generic,
2691 .read = tracing_max_lat_read,
2692 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002693};
2694
2695static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002696 .open = tracing_open_generic,
2697 .read = tracing_ctrl_read,
2698 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002699};
2700
2701static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002702 .open = tracing_open_generic,
2703 .read = tracing_set_trace_read,
2704 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002705};
2706
Steven Rostedtb3806b42008-05-12 21:20:46 +02002707static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002708 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002709 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002710 .read = tracing_read_pipe,
2711 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002712};
2713
Steven Rostedta98a3c32008-05-12 21:20:59 +02002714static struct file_operations tracing_entries_fops = {
2715 .open = tracing_open_generic,
2716 .read = tracing_entries_read,
2717 .write = tracing_entries_write,
2718};
2719
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720#ifdef CONFIG_DYNAMIC_FTRACE
2721
2722static ssize_t
2723tracing_read_long(struct file *filp, char __user *ubuf,
2724 size_t cnt, loff_t *ppos)
2725{
2726 unsigned long *p = filp->private_data;
2727 char buf[64];
2728 int r;
2729
2730 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002731
2732 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002733}
2734
2735static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002736 .open = tracing_open_generic,
2737 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002738};
2739#endif
2740
2741static struct dentry *d_tracer;
2742
2743struct dentry *tracing_init_dentry(void)
2744{
2745 static int once;
2746
2747 if (d_tracer)
2748 return d_tracer;
2749
2750 d_tracer = debugfs_create_dir("tracing", NULL);
2751
2752 if (!d_tracer && !once) {
2753 once = 1;
2754 pr_warning("Could not create debugfs directory 'tracing'\n");
2755 return NULL;
2756 }
2757
2758 return d_tracer;
2759}
2760
Steven Rostedt60a11772008-05-12 21:20:44 +02002761#ifdef CONFIG_FTRACE_SELFTEST
2762/* Let selftest have access to static functions in this file */
2763#include "trace_selftest.c"
2764#endif
2765
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002766static __init void tracer_init_debugfs(void)
2767{
2768 struct dentry *d_tracer;
2769 struct dentry *entry;
2770
2771 d_tracer = tracing_init_dentry();
2772
2773 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2774 &global_trace, &tracing_ctrl_fops);
2775 if (!entry)
2776 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2777
2778 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2779 NULL, &tracing_iter_fops);
2780 if (!entry)
2781 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2782
Ingo Molnarc7078de2008-05-12 21:20:52 +02002783 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2784 NULL, &tracing_cpumask_fops);
2785 if (!entry)
2786 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2787
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002788 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2789 &global_trace, &tracing_lt_fops);
2790 if (!entry)
2791 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2792
2793 entry = debugfs_create_file("trace", 0444, d_tracer,
2794 &global_trace, &tracing_fops);
2795 if (!entry)
2796 pr_warning("Could not create debugfs 'trace' entry\n");
2797
2798 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2799 &global_trace, &show_traces_fops);
2800 if (!entry)
2801 pr_warning("Could not create debugfs 'trace' entry\n");
2802
2803 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2804 &global_trace, &set_tracer_fops);
2805 if (!entry)
2806 pr_warning("Could not create debugfs 'trace' entry\n");
2807
2808 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2809 &tracing_max_latency,
2810 &tracing_max_lat_fops);
2811 if (!entry)
2812 pr_warning("Could not create debugfs "
2813 "'tracing_max_latency' entry\n");
2814
2815 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2816 &tracing_thresh, &tracing_max_lat_fops);
2817 if (!entry)
2818 pr_warning("Could not create debugfs "
2819 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002820 entry = debugfs_create_file("README", 0644, d_tracer,
2821 NULL, &tracing_readme_fops);
2822 if (!entry)
2823 pr_warning("Could not create debugfs 'README' entry\n");
2824
Steven Rostedtb3806b42008-05-12 21:20:46 +02002825 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2826 NULL, &tracing_pipe_fops);
2827 if (!entry)
2828 pr_warning("Could not create debugfs "
2829 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002830
Steven Rostedta98a3c32008-05-12 21:20:59 +02002831 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2832 &global_trace, &tracing_entries_fops);
2833 if (!entry)
2834 pr_warning("Could not create debugfs "
2835 "'tracing_threash' entry\n");
2836
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002837#ifdef CONFIG_DYNAMIC_FTRACE
2838 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2839 &ftrace_update_tot_cnt,
2840 &tracing_read_long_fops);
2841 if (!entry)
2842 pr_warning("Could not create debugfs "
2843 "'dyn_ftrace_total_info' entry\n");
2844#endif
2845}
2846
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002847static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002848{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002849 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002850 struct page *page, *tmp;
2851 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002852 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002853 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002854 int i;
2855
2856 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002857 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002858 array = (void *)__get_free_page(GFP_KERNEL);
2859 if (array == NULL) {
2860 printk(KERN_ERR "tracer: failed to allocate page"
2861 "for trace buffer!\n");
2862 goto free_pages;
2863 }
2864
Steven Rostedt3eefae92008-05-12 21:21:04 +02002865 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002866 page = virt_to_page(array);
2867 list_add(&page->lru, &pages);
2868
2869/* Only allocate if we are actually using the max trace */
2870#ifdef CONFIG_TRACER_MAX_TRACE
2871 array = (void *)__get_free_page(GFP_KERNEL);
2872 if (array == NULL) {
2873 printk(KERN_ERR "tracer: failed to allocate page"
2874 "for trace buffer!\n");
2875 goto free_pages;
2876 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002877 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002878 page = virt_to_page(array);
2879 list_add(&page->lru, &pages);
2880#endif
2881 }
2882
2883 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002884 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002885 data = global_trace.data[i];
2886 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002887 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002888 list_add_tail(&page->lru, &data->trace_pages);
2889 ClearPageLRU(page);
2890
2891#ifdef CONFIG_TRACER_MAX_TRACE
2892 data = max_tr.data[i];
2893 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002894 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002895 list_add_tail(&page->lru, &data->trace_pages);
2896 SetPageLRU(page);
2897#endif
2898 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002899 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002900 global_trace.entries += ENTRIES_PER_PAGE;
2901
2902 return 0;
2903
2904 free_pages:
2905 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002906 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002907 __free_page(page);
2908 }
2909 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002910}
2911
Steven Rostedta98a3c32008-05-12 21:20:59 +02002912static int trace_free_page(void)
2913{
2914 struct trace_array_cpu *data;
2915 struct page *page;
2916 struct list_head *p;
2917 int i;
2918 int ret = 0;
2919
2920 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002921 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002922 data = global_trace.data[i];
2923 p = data->trace_pages.next;
2924 if (p == &data->trace_pages) {
2925 /* should never happen */
2926 WARN_ON(1);
2927 tracing_disabled = 1;
2928 ret = -1;
2929 break;
2930 }
2931 page = list_entry(p, struct page, lru);
2932 ClearPageLRU(page);
2933 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02002934 tracing_pages_allocated--;
2935 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002936 __free_page(page);
2937
2938 tracing_reset(data);
2939
2940#ifdef CONFIG_TRACER_MAX_TRACE
2941 data = max_tr.data[i];
2942 p = data->trace_pages.next;
2943 if (p == &data->trace_pages) {
2944 /* should never happen */
2945 WARN_ON(1);
2946 tracing_disabled = 1;
2947 ret = -1;
2948 break;
2949 }
2950 page = list_entry(p, struct page, lru);
2951 ClearPageLRU(page);
2952 list_del(&page->lru);
2953 __free_page(page);
2954
2955 tracing_reset(data);
2956#endif
2957 }
2958 global_trace.entries -= ENTRIES_PER_PAGE;
2959
2960 return ret;
2961}
2962
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002963__init static int tracer_alloc_buffers(void)
2964{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002965 struct trace_array_cpu *data;
2966 void *array;
2967 struct page *page;
2968 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002969 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002970 int i;
2971
Steven Rostedt26994ea2008-05-12 21:20:48 +02002972 global_trace.ctrl = tracer_enabled;
2973
Steven Rostedtab464282008-05-12 21:21:00 +02002974 /* TODO: make the number of buffers hot pluggable with CPUS */
2975 tracing_nr_buffers = num_possible_cpus();
2976 tracing_buffer_mask = cpu_possible_map;
2977
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002978 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002979 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002980 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002981 max_tr.data[i] = &per_cpu(max_data, i);
2982
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002983 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002984 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002985 printk(KERN_ERR "tracer: failed to allocate page"
2986 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002987 goto free_buffers;
2988 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002989
2990 /* set the array to the list */
2991 INIT_LIST_HEAD(&data->trace_pages);
2992 page = virt_to_page(array);
2993 list_add(&page->lru, &data->trace_pages);
2994 /* use the LRU flag to differentiate the two buffers */
2995 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002996
Steven Rostedta98a3c32008-05-12 21:20:59 +02002997 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2998 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2999
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003000/* Only allocate if we are actually using the max trace */
3001#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003002 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003003 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003004 printk(KERN_ERR "tracer: failed to allocate page"
3005 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003006 goto free_buffers;
3007 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003008
3009 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3010 page = virt_to_page(array);
3011 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3012 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003013#endif
3014 }
3015
3016 /*
3017 * Since we allocate by orders of pages, we may be able to
3018 * round up a bit.
3019 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003020 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003021 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003022
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003023 while (global_trace.entries < trace_nr_entries) {
3024 if (trace_alloc_page())
3025 break;
3026 pages++;
3027 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003028 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003029
3030 pr_info("tracer: %d pages allocated for %ld",
3031 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003032 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3033 pr_info(" actual entries %ld\n", global_trace.entries);
3034
3035 tracer_init_debugfs();
3036
3037 trace_init_cmdlines();
3038
3039 register_tracer(&no_tracer);
3040 current_trace = &no_tracer;
3041
Steven Rostedt60a11772008-05-12 21:20:44 +02003042 /* All seems OK, enable tracing */
3043 tracing_disabled = 0;
3044
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003045 return 0;
3046
3047 free_buffers:
3048 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003049 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003050 struct trace_array_cpu *data = global_trace.data[i];
3051
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003052 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003053 list_for_each_entry_safe(page, tmp,
3054 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003055 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003056 __free_page(page);
3057 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003058 }
3059
3060#ifdef CONFIG_TRACER_MAX_TRACE
3061 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003062 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003063 list_for_each_entry_safe(page, tmp,
3064 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003065 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003066 __free_page(page);
3067 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003068 }
3069#endif
3070 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003071 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003072}
Steven Rostedt60a11772008-05-12 21:20:44 +02003073fs_initcall(tracer_alloc_buffers);