blob: 0567f51bbea4a4070876f3a2f59cfba11bdfb795 [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 Rostedt19384c02008-05-22 00:22:16 -0400252#define CHECK_COND(cond) \
253 if (unlikely(cond)) { \
254 tracing_disabled = 1; \
255 WARN_ON(1); \
256 return -1; \
257 }
258
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200259/**
260 * check_pages - integrity check of trace buffers
261 *
262 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400263 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200264 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400265int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200266{
267 struct page *page, *tmp;
268
Steven Rostedt19384c02008-05-22 00:22:16 -0400269 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
270 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200271
272 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400273 CHECK_COND(page->lru.next->prev != &page->lru);
274 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200275 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400276
277 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200278}
279
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200280/**
281 * head_page - page address of the first page in per_cpu buffer.
282 *
283 * head_page returns the page address of the first page in
284 * a per_cpu buffer. This also preforms various consistency
285 * checks to make sure the buffer has not been corrupted.
286 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200287void *head_page(struct trace_array_cpu *data)
288{
289 struct page *page;
290
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200291 if (list_empty(&data->trace_pages))
292 return NULL;
293
294 page = list_entry(data->trace_pages.next, struct page, lru);
295 BUG_ON(&page->lru == &data->trace_pages);
296
297 return page_address(page);
298}
299
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200300/**
301 * trace_seq_printf - sequence printing of trace information
302 * @s: trace sequence descriptor
303 * @fmt: printf format string
304 *
305 * The tracer may use either sequence operations or its own
306 * copy to user routines. To simplify formating of a trace
307 * trace_seq_printf is used to store strings into a special
308 * buffer (@s). Then the output may be either used by
309 * the sequencer or pulled into another buffer.
310 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200311int
Steven Rostedt214023c2008-05-12 21:20:46 +0200312trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
313{
314 int len = (PAGE_SIZE - 1) - s->len;
315 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200316 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200317
318 if (!len)
319 return 0;
320
321 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200322 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200323 va_end(ap);
324
Steven Rostedtb3806b42008-05-12 21:20:46 +0200325 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200326 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200327 return 0;
328
329 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200330
331 return len;
332}
333
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200334/**
335 * trace_seq_puts - trace sequence printing of simple string
336 * @s: trace sequence descriptor
337 * @str: simple string to record
338 *
339 * The tracer may use either the sequence operations or its own
340 * copy to user routines. This function records a simple string
341 * into a special buffer (@s) for later retrieval by a sequencer
342 * or other mechanism.
343 */
Ingo Molnare309b412008-05-12 21:20:51 +0200344static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200345trace_seq_puts(struct trace_seq *s, const char *str)
346{
347 int len = strlen(str);
348
349 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200350 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200351
352 memcpy(s->buffer + s->len, str, len);
353 s->len += len;
354
355 return len;
356}
357
Ingo Molnare309b412008-05-12 21:20:51 +0200358static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200359trace_seq_putc(struct trace_seq *s, unsigned char c)
360{
361 if (s->len >= (PAGE_SIZE - 1))
362 return 0;
363
364 s->buffer[s->len++] = c;
365
366 return 1;
367}
368
Ingo Molnare309b412008-05-12 21:20:51 +0200369static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200370trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
371{
372 if (len > ((PAGE_SIZE - 1) - s->len))
373 return 0;
374
375 memcpy(s->buffer + s->len, mem, len);
376 s->len += len;
377
378 return len;
379}
380
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200381#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200382static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200383
Ingo Molnare309b412008-05-12 21:20:51 +0200384static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200385trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
386{
387 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200388 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200389 unsigned char byte;
390 int i, j;
391
392 BUG_ON(len >= HEX_CHARS);
393
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200394#ifdef __BIG_ENDIAN
395 for (i = 0, j = 0; i < len; i++) {
396#else
397 for (i = len-1, j = 0; i >= 0; i--) {
398#endif
399 byte = data[i];
400
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200401 hex[j++] = hex2asc[byte & 0x0f];
402 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200403 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200404 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200405
406 return trace_seq_putmem(s, hex, j);
407}
408
Ingo Molnare309b412008-05-12 21:20:51 +0200409static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200410trace_seq_reset(struct trace_seq *s)
411{
412 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200413 s->readpos = 0;
414}
415
416ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
417{
418 int len;
419 int ret;
420
421 if (s->len <= s->readpos)
422 return -EBUSY;
423
424 len = s->len - s->readpos;
425 if (cnt > len)
426 cnt = len;
427 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
428 if (ret)
429 return -EFAULT;
430
431 s->readpos += len;
432 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200433}
434
Ingo Molnare309b412008-05-12 21:20:51 +0200435static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200436trace_print_seq(struct seq_file *m, struct trace_seq *s)
437{
438 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
439
440 s->buffer[len] = 0;
441 seq_puts(m, s->buffer);
442
443 trace_seq_reset(s);
444}
445
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200446/*
447 * flip the trace buffers between two trace descriptors.
448 * This usually is the buffers between the global_trace and
449 * the max_tr to record a snapshot of a current trace.
450 *
451 * The ftrace_max_lock must be held.
452 */
Ingo Molnare309b412008-05-12 21:20:51 +0200453static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200454flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
455{
456 struct list_head flip_pages;
457
458 INIT_LIST_HEAD(&flip_pages);
459
Steven Rostedt93a588f2008-05-12 21:20:45 +0200460 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200461 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200462 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200463
464 check_pages(tr1);
465 check_pages(tr2);
466 list_splice_init(&tr1->trace_pages, &flip_pages);
467 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
468 list_splice_init(&flip_pages, &tr2->trace_pages);
469 BUG_ON(!list_empty(&flip_pages));
470 check_pages(tr1);
471 check_pages(tr2);
472}
473
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200474/**
475 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
476 * @tr: tracer
477 * @tsk: the task with the latency
478 * @cpu: The cpu that initiated the trace.
479 *
480 * Flip the buffers between the @tr and the max_tr and record information
481 * about which task was the cause of this latency.
482 */
Ingo Molnare309b412008-05-12 21:20:51 +0200483void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200484update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
485{
486 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200487 int i;
488
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200489 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200490 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200491 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200492 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200493 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200494 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200495 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200496 }
497
498 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200499 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200500}
501
502/**
503 * update_max_tr_single - only copy one trace over, and reset the rest
504 * @tr - tracer
505 * @tsk - task with the latency
506 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200507 *
508 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200509 */
Ingo Molnare309b412008-05-12 21:20:51 +0200510void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200511update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
512{
513 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200514 int i;
515
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200516 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200517 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200518 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200519 tracing_reset(max_tr.data[i]);
520
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200521 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200522 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200523
524 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200525 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200526}
527
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200528/**
529 * register_tracer - register a tracer with the ftrace system.
530 * @type - the plugin for the tracer
531 *
532 * Register a new plugin tracer.
533 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200534int register_tracer(struct tracer *type)
535{
536 struct tracer *t;
537 int len;
538 int ret = 0;
539
540 if (!type->name) {
541 pr_info("Tracer must have a name\n");
542 return -1;
543 }
544
545 mutex_lock(&trace_types_lock);
546 for (t = trace_types; t; t = t->next) {
547 if (strcmp(type->name, t->name) == 0) {
548 /* already found */
549 pr_info("Trace %s already registered\n",
550 type->name);
551 ret = -1;
552 goto out;
553 }
554 }
555
Steven Rostedt60a11772008-05-12 21:20:44 +0200556#ifdef CONFIG_FTRACE_STARTUP_TEST
557 if (type->selftest) {
558 struct tracer *saved_tracer = current_trace;
559 struct trace_array_cpu *data;
560 struct trace_array *tr = &global_trace;
561 int saved_ctrl = tr->ctrl;
562 int i;
563 /*
564 * Run a selftest on this tracer.
565 * Here we reset the trace buffer, and set the current
566 * tracer to be this tracer. The tracer can then run some
567 * internal tracing to verify that everything is in order.
568 * If we fail, we do not register this tracer.
569 */
Steven Rostedtab464282008-05-12 21:21:00 +0200570 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200571 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200572 if (!head_page(data))
573 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200574 tracing_reset(data);
575 }
576 current_trace = type;
577 tr->ctrl = 0;
578 /* the test is responsible for initializing and enabling */
579 pr_info("Testing tracer %s: ", type->name);
580 ret = type->selftest(type, tr);
581 /* the test is responsible for resetting too */
582 current_trace = saved_tracer;
583 tr->ctrl = saved_ctrl;
584 if (ret) {
585 printk(KERN_CONT "FAILED!\n");
586 goto out;
587 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200588 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200589 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200590 data = tr->data[i];
591 if (!head_page(data))
592 continue;
593 tracing_reset(data);
594 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200595 printk(KERN_CONT "PASSED\n");
596 }
597#endif
598
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599 type->next = trace_types;
600 trace_types = type;
601 len = strlen(type->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200604
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 out:
606 mutex_unlock(&trace_types_lock);
607
608 return ret;
609}
610
611void unregister_tracer(struct tracer *type)
612{
613 struct tracer **t;
614 int len;
615
616 mutex_lock(&trace_types_lock);
617 for (t = &trace_types; *t; t = &(*t)->next) {
618 if (*t == type)
619 goto found;
620 }
621 pr_info("Trace %s not registered\n", type->name);
622 goto out;
623
624 found:
625 *t = (*t)->next;
626 if (strlen(type->name) != max_tracer_type_len)
627 goto out;
628
629 max_tracer_type_len = 0;
630 for (t = &trace_types; *t; t = &(*t)->next) {
631 len = strlen((*t)->name);
632 if (len > max_tracer_type_len)
633 max_tracer_type_len = len;
634 }
635 out:
636 mutex_unlock(&trace_types_lock);
637}
638
Ingo Molnare309b412008-05-12 21:20:51 +0200639void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640{
641 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200642 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200643 data->trace_head = data->trace_tail = head_page(data);
644 data->trace_head_idx = 0;
645 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200646}
647
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200648#define SAVED_CMDLINES 128
649static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
650static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
651static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
652static int cmdline_idx;
653static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200654
655/* trace in all context switches */
656atomic_t trace_record_cmdline_enabled __read_mostly;
657
658/* temporary disable recording */
659atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660
661static void trace_init_cmdlines(void)
662{
663 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
664 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
665 cmdline_idx = 0;
666}
667
Ingo Molnare309b412008-05-12 21:20:51 +0200668void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669
Ingo Molnare309b412008-05-12 21:20:51 +0200670static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200671{
672 unsigned map;
673 unsigned idx;
674
675 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
676 return;
677
678 /*
679 * It's not the end of the world if we don't get
680 * the lock, but we also don't want to spin
681 * nor do we want to disable interrupts,
682 * so if we miss here, then better luck next time.
683 */
684 if (!spin_trylock(&trace_cmdline_lock))
685 return;
686
687 idx = map_pid_to_cmdline[tsk->pid];
688 if (idx >= SAVED_CMDLINES) {
689 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
690
691 map = map_cmdline_to_pid[idx];
692 if (map <= PID_MAX_DEFAULT)
693 map_pid_to_cmdline[map] = (unsigned)-1;
694
695 map_pid_to_cmdline[tsk->pid] = idx;
696
697 cmdline_idx = idx;
698 }
699
700 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
701
702 spin_unlock(&trace_cmdline_lock);
703}
704
Ingo Molnare309b412008-05-12 21:20:51 +0200705static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200706{
707 char *cmdline = "<...>";
708 unsigned map;
709
710 if (!pid)
711 return "<idle>";
712
713 if (pid > PID_MAX_DEFAULT)
714 goto out;
715
716 map = map_pid_to_cmdline[pid];
717 if (map >= SAVED_CMDLINES)
718 goto out;
719
720 cmdline = saved_cmdlines[map];
721
722 out:
723 return cmdline;
724}
725
Ingo Molnare309b412008-05-12 21:20:51 +0200726void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200727{
728 if (atomic_read(&trace_record_cmdline_disabled))
729 return;
730
731 trace_save_cmdline(tsk);
732}
733
Ingo Molnare309b412008-05-12 21:20:51 +0200734static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200735trace_next_list(struct trace_array_cpu *data, struct list_head *next)
736{
737 /*
738 * Roundrobin - but skip the head (which is not a real page):
739 */
740 next = next->next;
741 if (unlikely(next == &data->trace_pages))
742 next = next->next;
743 BUG_ON(next == &data->trace_pages);
744
745 return next;
746}
747
Ingo Molnare309b412008-05-12 21:20:51 +0200748static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200749trace_next_page(struct trace_array_cpu *data, void *addr)
750{
751 struct list_head *next;
752 struct page *page;
753
754 page = virt_to_page(addr);
755
756 next = trace_next_list(data, &page->lru);
757 page = list_entry(next, struct page, lru);
758
759 return page_address(page);
760}
761
Ingo Molnare309b412008-05-12 21:20:51 +0200762static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200763tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200764{
765 unsigned long idx, idx_next;
766 struct trace_entry *entry;
767
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200768 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200769 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200770 idx_next = idx + 1;
771
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200772 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
773
Steven Rostedt93a588f2008-05-12 21:20:45 +0200774 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200775
776 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200777 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778 idx_next = 0;
779 }
780
Steven Rostedt93a588f2008-05-12 21:20:45 +0200781 if (data->trace_head == data->trace_tail &&
782 idx_next == data->trace_tail_idx) {
783 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200784 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200785 data->trace_tail_idx++;
786 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
787 data->trace_tail =
788 trace_next_page(data, data->trace_tail);
789 data->trace_tail_idx = 0;
790 }
791 }
792
793 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200794
795 return entry;
796}
797
Ingo Molnare309b412008-05-12 21:20:51 +0200798static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200799tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200800{
801 struct task_struct *tsk = current;
802 unsigned long pc;
803
804 pc = preempt_count();
805
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200806 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200807 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200808 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
810 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
811 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
812 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
813}
814
Ingo Molnare309b412008-05-12 21:20:51 +0200815void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200816trace_function(struct trace_array *tr, struct trace_array_cpu *data,
817 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818{
819 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200820 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200821
Steven Rostedt92205c22008-05-12 21:20:55 +0200822 raw_local_irq_save(irq_flags);
823 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200824 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200826 entry->type = TRACE_FN;
827 entry->fn.ip = ip;
828 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200829 __raw_spin_unlock(&data->lock);
830 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200831}
832
Ingo Molnare309b412008-05-12 21:20:51 +0200833void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200834ftrace(struct trace_array *tr, struct trace_array_cpu *data,
835 unsigned long ip, unsigned long parent_ip, unsigned long flags)
836{
837 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200838 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200839}
840
Ingo Molnar86387f72008-05-12 21:20:51 +0200841void __trace_stack(struct trace_array *tr,
842 struct trace_array_cpu *data,
843 unsigned long flags,
844 int skip)
845{
846 struct trace_entry *entry;
847 struct stack_trace trace;
848
849 if (!(trace_flags & TRACE_ITER_STACKTRACE))
850 return;
851
852 entry = tracing_get_trace_entry(tr, data);
853 tracing_generic_entry_update(entry, flags);
854 entry->type = TRACE_STACK;
855
856 memset(&entry->stack, 0, sizeof(entry->stack));
857
858 trace.nr_entries = 0;
859 trace.max_entries = FTRACE_STACK_ENTRIES;
860 trace.skip = skip;
861 trace.entries = entry->stack.caller;
862
863 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200864}
865
Ingo Molnare309b412008-05-12 21:20:51 +0200866void
Ingo Molnara4feb8342008-05-12 21:21:02 +0200867__trace_special(void *__tr, void *__data,
868 unsigned long arg1, unsigned long arg2, unsigned long arg3)
869{
870 struct trace_array_cpu *data = __data;
871 struct trace_array *tr = __tr;
872 struct trace_entry *entry;
873 unsigned long irq_flags;
874
875 raw_local_irq_save(irq_flags);
876 __raw_spin_lock(&data->lock);
877 entry = tracing_get_trace_entry(tr, data);
878 tracing_generic_entry_update(entry, 0);
879 entry->type = TRACE_SPECIAL;
880 entry->special.arg1 = arg1;
881 entry->special.arg2 = arg2;
882 entry->special.arg3 = arg3;
883 __trace_stack(tr, data, irq_flags, 4);
884 __raw_spin_unlock(&data->lock);
885 raw_local_irq_restore(irq_flags);
886
887 trace_wake_up();
888}
889
890void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200891tracing_sched_switch_trace(struct trace_array *tr,
892 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200893 struct task_struct *prev,
894 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200895 unsigned long flags)
896{
897 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200898 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200899
Steven Rostedt92205c22008-05-12 21:20:55 +0200900 raw_local_irq_save(irq_flags);
901 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200902 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200903 tracing_generic_entry_update(entry, flags);
904 entry->type = TRACE_CTX;
905 entry->ctx.prev_pid = prev->pid;
906 entry->ctx.prev_prio = prev->prio;
907 entry->ctx.prev_state = prev->state;
908 entry->ctx.next_pid = next->pid;
909 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200910 entry->ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200911 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200912 __raw_spin_unlock(&data->lock);
913 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200914}
915
Ingo Molnar57422792008-05-12 21:20:51 +0200916void
917tracing_sched_wakeup_trace(struct trace_array *tr,
918 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200919 struct task_struct *wakee,
920 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200921 unsigned long flags)
922{
923 struct trace_entry *entry;
924 unsigned long irq_flags;
925
Steven Rostedt92205c22008-05-12 21:20:55 +0200926 raw_local_irq_save(irq_flags);
927 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200928 entry = tracing_get_trace_entry(tr, data);
929 tracing_generic_entry_update(entry, flags);
930 entry->type = TRACE_WAKE;
931 entry->ctx.prev_pid = curr->pid;
932 entry->ctx.prev_prio = curr->prio;
933 entry->ctx.prev_state = curr->state;
934 entry->ctx.next_pid = wakee->pid;
935 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200936 entry->ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200937 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200938 __raw_spin_unlock(&data->lock);
939 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200940
941 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200942}
943
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200944#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200945static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200946function_trace_call(unsigned long ip, unsigned long parent_ip)
947{
948 struct trace_array *tr = &global_trace;
949 struct trace_array_cpu *data;
950 unsigned long flags;
951 long disabled;
952 int cpu;
953
954 if (unlikely(!tracer_enabled))
955 return;
956
957 local_irq_save(flags);
958 cpu = raw_smp_processor_id();
959 data = tr->data[cpu];
960 disabled = atomic_inc_return(&data->disabled);
961
962 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200963 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200964
965 atomic_dec(&data->disabled);
966 local_irq_restore(flags);
967}
968
969static struct ftrace_ops trace_ops __read_mostly =
970{
971 .func = function_trace_call,
972};
973
Ingo Molnare309b412008-05-12 21:20:51 +0200974void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200975{
976 register_ftrace_function(&trace_ops);
977}
978
Ingo Molnare309b412008-05-12 21:20:51 +0200979void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200980{
981 unregister_ftrace_function(&trace_ops);
982}
983#endif
984
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200985enum trace_file_type {
986 TRACE_FILE_LAT_FMT = 1,
987};
988
989static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200990trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
991 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200992{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200993 struct page *page;
994 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200995
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200996 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200997 iter->next_idx[cpu] >= data->trace_idx ||
998 (data->trace_head == data->trace_tail &&
999 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001000 return NULL;
1001
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001002 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001003 /* Initialize the iterator for this cpu trace buffer */
1004 WARN_ON(!data->trace_tail);
1005 page = virt_to_page(data->trace_tail);
1006 iter->next_page[cpu] = &page->lru;
1007 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001008 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001009
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001010 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001011 BUG_ON(&data->trace_pages == &page->lru);
1012
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001013 array = page_address(page);
1014
Steven Rostedt93a588f2008-05-12 21:20:45 +02001015 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001016 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001017}
1018
Ingo Molnare309b412008-05-12 21:20:51 +02001019static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1021{
1022 struct trace_array *tr = iter->tr;
1023 struct trace_entry *ent, *next = NULL;
1024 int next_cpu = -1;
1025 int cpu;
1026
Steven Rostedtab464282008-05-12 21:21:00 +02001027 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001028 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001030 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001031 /*
1032 * Pick the entry with the smallest timestamp:
1033 */
1034 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035 next = ent;
1036 next_cpu = cpu;
1037 }
1038 }
1039
1040 if (ent_cpu)
1041 *ent_cpu = next_cpu;
1042
1043 return next;
1044}
1045
Ingo Molnare309b412008-05-12 21:20:51 +02001046static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001047{
1048 iter->idx++;
1049 iter->next_idx[iter->cpu]++;
1050 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001051
Steven Rostedtb3806b42008-05-12 21:20:46 +02001052 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1053 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1054
1055 iter->next_page_idx[iter->cpu] = 0;
1056 iter->next_page[iter->cpu] =
1057 trace_next_list(data, iter->next_page[iter->cpu]);
1058 }
1059}
1060
Ingo Molnare309b412008-05-12 21:20:51 +02001061static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001062{
1063 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1064
1065 data->trace_tail_idx++;
1066 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1067 data->trace_tail = trace_next_page(data, data->trace_tail);
1068 data->trace_tail_idx = 0;
1069 }
1070
1071 /* Check if we empty it, then reset the index */
1072 if (data->trace_head == data->trace_tail &&
1073 data->trace_head_idx == data->trace_tail_idx)
1074 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001075}
1076
Ingo Molnare309b412008-05-12 21:20:51 +02001077static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001078{
1079 struct trace_entry *next;
1080 int next_cpu = -1;
1081
1082 next = find_next_entry(iter, &next_cpu);
1083
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001084 iter->prev_ent = iter->ent;
1085 iter->prev_cpu = iter->cpu;
1086
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001087 iter->ent = next;
1088 iter->cpu = next_cpu;
1089
Steven Rostedtb3806b42008-05-12 21:20:46 +02001090 if (next)
1091 trace_iterator_increment(iter);
1092
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093 return next ? iter : NULL;
1094}
1095
Ingo Molnare309b412008-05-12 21:20:51 +02001096static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001097{
1098 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099 void *last_ent = iter->ent;
1100 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001101 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102
1103 (*pos)++;
1104
1105 /* can't go backwards */
1106 if (iter->idx > i)
1107 return NULL;
1108
1109 if (iter->idx < 0)
1110 ent = find_next_entry_inc(iter);
1111 else
1112 ent = iter;
1113
1114 while (ent && iter->idx < i)
1115 ent = find_next_entry_inc(iter);
1116
1117 iter->pos = *pos;
1118
1119 if (last_ent && !ent)
1120 seq_puts(m, "\n\nvim:ft=help\n");
1121
1122 return ent;
1123}
1124
1125static void *s_start(struct seq_file *m, loff_t *pos)
1126{
1127 struct trace_iterator *iter = m->private;
1128 void *p = NULL;
1129 loff_t l = 0;
1130 int i;
1131
1132 mutex_lock(&trace_types_lock);
1133
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001134 if (!current_trace || current_trace != iter->trace) {
1135 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001136 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001137 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001138
1139 atomic_inc(&trace_record_cmdline_disabled);
1140
1141 /* let the tracer grab locks here if needed */
1142 if (current_trace->start)
1143 current_trace->start(iter);
1144
1145 if (*pos != iter->pos) {
1146 iter->ent = NULL;
1147 iter->cpu = 0;
1148 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001149 iter->prev_ent = NULL;
1150 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001151
Steven Rostedtab464282008-05-12 21:21:00 +02001152 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001153 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001154 iter->next_page[i] = NULL;
1155 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001156
1157 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1158 ;
1159
1160 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001161 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001162 p = s_next(m, p, &l);
1163 }
1164
1165 return p;
1166}
1167
1168static void s_stop(struct seq_file *m, void *p)
1169{
1170 struct trace_iterator *iter = m->private;
1171
1172 atomic_dec(&trace_record_cmdline_disabled);
1173
1174 /* let the tracer release locks here if needed */
1175 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1176 iter->trace->stop(iter);
1177
1178 mutex_unlock(&trace_types_lock);
1179}
1180
Steven Rostedtb3806b42008-05-12 21:20:46 +02001181static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001182seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001183{
1184#ifdef CONFIG_KALLSYMS
1185 char str[KSYM_SYMBOL_LEN];
1186
1187 kallsyms_lookup(address, NULL, NULL, NULL, str);
1188
Steven Rostedtb3806b42008-05-12 21:20:46 +02001189 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001191 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001192}
1193
Steven Rostedtb3806b42008-05-12 21:20:46 +02001194static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001195seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1196 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197{
1198#ifdef CONFIG_KALLSYMS
1199 char str[KSYM_SYMBOL_LEN];
1200
1201 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001202 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001203#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001204 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001205}
1206
1207#ifndef CONFIG_64BIT
1208# define IP_FMT "%08lx"
1209#else
1210# define IP_FMT "%016lx"
1211#endif
1212
Ingo Molnare309b412008-05-12 21:20:51 +02001213static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001214seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001215{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001216 int ret;
1217
1218 if (!ip)
1219 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220
1221 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001222 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001223 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001224 ret = seq_print_sym_short(s, "%s", ip);
1225
1226 if (!ret)
1227 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001228
1229 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001230 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1231 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001232}
1233
Ingo Molnare309b412008-05-12 21:20:51 +02001234static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001235{
1236 seq_puts(m, "# _------=> CPU# \n");
1237 seq_puts(m, "# / _-----=> irqs-off \n");
1238 seq_puts(m, "# | / _----=> need-resched \n");
1239 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1240 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1241 seq_puts(m, "# |||| / \n");
1242 seq_puts(m, "# ||||| delay \n");
1243 seq_puts(m, "# cmd pid ||||| time | caller \n");
1244 seq_puts(m, "# \\ / ||||| \\ | / \n");
1245}
1246
Ingo Molnare309b412008-05-12 21:20:51 +02001247static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248{
1249 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1250 seq_puts(m, "# | | | | |\n");
1251}
1252
1253
Ingo Molnare309b412008-05-12 21:20:51 +02001254static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1256{
1257 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1258 struct trace_array *tr = iter->tr;
1259 struct trace_array_cpu *data = tr->data[tr->cpu];
1260 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001261 unsigned long total = 0;
1262 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001263 int cpu;
1264 const char *name = "preemption";
1265
1266 if (type)
1267 name = type->name;
1268
Steven Rostedtab464282008-05-12 21:21:00 +02001269 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001270 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001271 total += tr->data[cpu]->trace_idx;
1272 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001274 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001275 entries += tr->data[cpu]->trace_idx;
1276 }
1277 }
1278
1279 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1280 name, UTS_RELEASE);
1281 seq_puts(m, "-----------------------------------"
1282 "---------------------------------\n");
1283 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1284 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001285 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001287 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288 tr->cpu,
1289#if defined(CONFIG_PREEMPT_NONE)
1290 "server",
1291#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1292 "desktop",
1293#elif defined(CONFIG_PREEMPT_DESKTOP)
1294 "preempt",
1295#else
1296 "unknown",
1297#endif
1298 /* These are reserved for later use */
1299 0, 0, 0, 0);
1300#ifdef CONFIG_SMP
1301 seq_printf(m, " #P:%d)\n", num_online_cpus());
1302#else
1303 seq_puts(m, ")\n");
1304#endif
1305 seq_puts(m, " -----------------\n");
1306 seq_printf(m, " | task: %.16s-%d "
1307 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1308 data->comm, data->pid, data->uid, data->nice,
1309 data->policy, data->rt_priority);
1310 seq_puts(m, " -----------------\n");
1311
1312 if (data->critical_start) {
1313 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001314 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1315 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001317 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1318 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001319 seq_puts(m, "\n");
1320 }
1321
1322 seq_puts(m, "\n");
1323}
1324
Ingo Molnare309b412008-05-12 21:20:51 +02001325static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001326lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001327{
1328 int hardirq, softirq;
1329 char *comm;
1330
1331 comm = trace_find_cmdline(entry->pid);
1332
Steven Rostedt214023c2008-05-12 21:20:46 +02001333 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1334 trace_seq_printf(s, "%d", cpu);
1335 trace_seq_printf(s, "%c%c",
1336 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1337 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338
1339 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1340 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001341 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001342 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001343 } else {
1344 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001345 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001346 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001347 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001348 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001349 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001350 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001351 }
1352 }
1353
1354 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001355 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001356 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001357 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358}
1359
1360unsigned long preempt_mark_thresh = 100;
1361
Ingo Molnare309b412008-05-12 21:20:51 +02001362static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001363lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364 unsigned long rel_usecs)
1365{
Steven Rostedt214023c2008-05-12 21:20:46 +02001366 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001367 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001368 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001369 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001370 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001371 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001372 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373}
1374
1375static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1376
Ingo Molnare309b412008-05-12 21:20:51 +02001377static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001378print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001379{
Steven Rostedt214023c2008-05-12 21:20:46 +02001380 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001381 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1382 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1383 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1384 struct trace_entry *entry = iter->ent;
1385 unsigned long abs_usecs;
1386 unsigned long rel_usecs;
1387 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001388 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001389 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001390 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001391
1392 if (!next_entry)
1393 next_entry = entry;
1394 rel_usecs = ns2usecs(next_entry->t - entry->t);
1395 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1396
1397 if (verbose) {
1398 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001399 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1400 " %ld.%03ldms (+%ld.%03ldms): ",
1401 comm,
1402 entry->pid, cpu, entry->flags,
1403 entry->preempt_count, trace_idx,
1404 ns2usecs(entry->t),
1405 abs_usecs/1000,
1406 abs_usecs % 1000, rel_usecs/1000,
1407 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001408 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001409 lat_print_generic(s, entry, cpu);
1410 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001411 }
1412 switch (entry->type) {
1413 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001414 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1415 trace_seq_puts(s, " (");
1416 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1417 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001418 break;
1419 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001420 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001421 T = entry->ctx.next_state < sizeof(state_to_char) ?
1422 state_to_char[entry->ctx.next_state] : 'X';
1423
Ankita Gargd17d9692008-05-12 21:20:58 +02001424 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1425 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001427 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001428 entry->ctx.prev_pid,
1429 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001430 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001431 entry->ctx.next_pid,
1432 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001433 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001434 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001435 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001436 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001437 entry->special.arg1,
1438 entry->special.arg2,
1439 entry->special.arg3);
1440 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001441 case TRACE_STACK:
1442 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1443 if (i)
1444 trace_seq_puts(s, " <= ");
1445 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1446 }
1447 trace_seq_puts(s, "\n");
1448 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001449 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001450 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001451 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001452 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453}
1454
Ingo Molnare309b412008-05-12 21:20:51 +02001455static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001456{
Steven Rostedt214023c2008-05-12 21:20:46 +02001457 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001459 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460 unsigned long usec_rem;
1461 unsigned long long t;
1462 unsigned long secs;
1463 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001464 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001465 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001466 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001468 entry = iter->ent;
1469
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001470 comm = trace_find_cmdline(iter->ent->pid);
1471
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001472 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473 usec_rem = do_div(t, 1000000ULL);
1474 secs = (unsigned long)t;
1475
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001476 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1477 if (!ret)
1478 return 0;
1479 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1480 if (!ret)
1481 return 0;
1482 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1483 if (!ret)
1484 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485
1486 switch (entry->type) {
1487 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001488 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1489 if (!ret)
1490 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001491 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1492 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001493 ret = trace_seq_printf(s, " <-");
1494 if (!ret)
1495 return 0;
1496 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1497 sym_flags);
1498 if (!ret)
1499 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001501 ret = trace_seq_printf(s, "\n");
1502 if (!ret)
1503 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001504 break;
1505 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001506 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001507 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1508 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001509 T = entry->ctx.next_state < sizeof(state_to_char) ?
1510 state_to_char[entry->ctx.next_state] : 'X';
1511 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001512 entry->ctx.prev_pid,
1513 entry->ctx.prev_prio,
1514 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001515 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001516 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001517 entry->ctx.next_prio,
1518 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001519 if (!ret)
1520 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001522 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001523 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001524 entry->special.arg1,
1525 entry->special.arg2,
1526 entry->special.arg3);
1527 if (!ret)
1528 return 0;
1529 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001530 case TRACE_STACK:
1531 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1532 if (i) {
1533 ret = trace_seq_puts(s, " <= ");
1534 if (!ret)
1535 return 0;
1536 }
1537 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1538 sym_flags);
1539 if (!ret)
1540 return 0;
1541 }
1542 ret = trace_seq_puts(s, "\n");
1543 if (!ret)
1544 return 0;
1545 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001547 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001548}
1549
Ingo Molnare309b412008-05-12 21:20:51 +02001550static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001551{
1552 struct trace_seq *s = &iter->seq;
1553 struct trace_entry *entry;
1554 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001555 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001556
1557 entry = iter->ent;
1558
1559 ret = trace_seq_printf(s, "%d %d %llu ",
1560 entry->pid, iter->cpu, entry->t);
1561 if (!ret)
1562 return 0;
1563
1564 switch (entry->type) {
1565 case TRACE_FN:
1566 ret = trace_seq_printf(s, "%x %x\n",
1567 entry->fn.ip, entry->fn.parent_ip);
1568 if (!ret)
1569 return 0;
1570 break;
1571 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001572 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001573 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1574 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001575 T = entry->ctx.next_state < sizeof(state_to_char) ?
1576 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001577 if (entry->type == TRACE_WAKE)
1578 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001579 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001580 entry->ctx.prev_pid,
1581 entry->ctx.prev_prio,
1582 S,
1583 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001584 entry->ctx.next_prio,
1585 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001586 if (!ret)
1587 return 0;
1588 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001589 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001590 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001591 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001592 entry->special.arg1,
1593 entry->special.arg2,
1594 entry->special.arg3);
1595 if (!ret)
1596 return 0;
1597 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001598 }
1599 return 1;
1600}
1601
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001602#define SEQ_PUT_FIELD_RET(s, x) \
1603do { \
1604 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1605 return 0; \
1606} while (0)
1607
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001608#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1609do { \
1610 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1611 return 0; \
1612} while (0)
1613
Ingo Molnare309b412008-05-12 21:20:51 +02001614static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001615{
1616 struct trace_seq *s = &iter->seq;
1617 unsigned char newline = '\n';
1618 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001619 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001620
1621 entry = iter->ent;
1622
1623 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1624 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1625 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1626
1627 switch (entry->type) {
1628 case TRACE_FN:
1629 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1630 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1631 break;
1632 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001633 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001634 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1635 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001636 T = entry->ctx.next_state < sizeof(state_to_char) ?
1637 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001638 if (entry->type == TRACE_WAKE)
1639 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001640 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1641 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1642 SEQ_PUT_HEX_FIELD_RET(s, S);
1643 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1644 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1645 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001646 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001647 break;
1648 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001649 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001650 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1651 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1652 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1653 break;
1654 }
1655 SEQ_PUT_FIELD_RET(s, newline);
1656
1657 return 1;
1658}
1659
Ingo Molnare309b412008-05-12 21:20:51 +02001660static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001661{
1662 struct trace_seq *s = &iter->seq;
1663 struct trace_entry *entry;
1664
1665 entry = iter->ent;
1666
1667 SEQ_PUT_FIELD_RET(s, entry->pid);
1668 SEQ_PUT_FIELD_RET(s, entry->cpu);
1669 SEQ_PUT_FIELD_RET(s, entry->t);
1670
1671 switch (entry->type) {
1672 case TRACE_FN:
1673 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1674 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1675 break;
1676 case TRACE_CTX:
1677 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1678 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1679 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1680 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1681 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001682 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001683 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001684 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001685 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001686 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1687 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1688 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1689 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001690 }
1691 return 1;
1692}
1693
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001694static int trace_empty(struct trace_iterator *iter)
1695{
1696 struct trace_array_cpu *data;
1697 int cpu;
1698
Steven Rostedtab464282008-05-12 21:21:00 +02001699 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001700 data = iter->tr->data[cpu];
1701
Steven Rostedtb3806b42008-05-12 21:20:46 +02001702 if (head_page(data) && data->trace_idx &&
1703 (data->trace_tail != data->trace_head ||
1704 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001705 return 0;
1706 }
1707 return 1;
1708}
1709
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001710static int print_trace_line(struct trace_iterator *iter)
1711{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001712 if (iter->trace && iter->trace->print_line)
1713 return iter->trace->print_line(iter);
1714
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001715 if (trace_flags & TRACE_ITER_BIN)
1716 return print_bin_fmt(iter);
1717
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001718 if (trace_flags & TRACE_ITER_HEX)
1719 return print_hex_fmt(iter);
1720
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001721 if (trace_flags & TRACE_ITER_RAW)
1722 return print_raw_fmt(iter);
1723
1724 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1725 return print_lat_fmt(iter, iter->idx, iter->cpu);
1726
1727 return print_trace_fmt(iter);
1728}
1729
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001730static int s_show(struct seq_file *m, void *v)
1731{
1732 struct trace_iterator *iter = v;
1733
1734 if (iter->ent == NULL) {
1735 if (iter->tr) {
1736 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1737 seq_puts(m, "#\n");
1738 }
1739 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1740 /* print nothing if the buffers are empty */
1741 if (trace_empty(iter))
1742 return 0;
1743 print_trace_header(m, iter);
1744 if (!(trace_flags & TRACE_ITER_VERBOSE))
1745 print_lat_help_header(m);
1746 } else {
1747 if (!(trace_flags & TRACE_ITER_VERBOSE))
1748 print_func_help_header(m);
1749 }
1750 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001751 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001752 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001753 }
1754
1755 return 0;
1756}
1757
1758static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001759 .start = s_start,
1760 .next = s_next,
1761 .stop = s_stop,
1762 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001763};
1764
Ingo Molnare309b412008-05-12 21:20:51 +02001765static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766__tracing_open(struct inode *inode, struct file *file, int *ret)
1767{
1768 struct trace_iterator *iter;
1769
Steven Rostedt60a11772008-05-12 21:20:44 +02001770 if (tracing_disabled) {
1771 *ret = -ENODEV;
1772 return NULL;
1773 }
1774
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1776 if (!iter) {
1777 *ret = -ENOMEM;
1778 goto out;
1779 }
1780
1781 mutex_lock(&trace_types_lock);
1782 if (current_trace && current_trace->print_max)
1783 iter->tr = &max_tr;
1784 else
1785 iter->tr = inode->i_private;
1786 iter->trace = current_trace;
1787 iter->pos = -1;
1788
1789 /* TODO stop tracer */
1790 *ret = seq_open(file, &tracer_seq_ops);
1791 if (!*ret) {
1792 struct seq_file *m = file->private_data;
1793 m->private = iter;
1794
1795 /* stop the trace while dumping */
1796 if (iter->tr->ctrl)
1797 tracer_enabled = 0;
1798
1799 if (iter->trace && iter->trace->open)
1800 iter->trace->open(iter);
1801 } else {
1802 kfree(iter);
1803 iter = NULL;
1804 }
1805 mutex_unlock(&trace_types_lock);
1806
1807 out:
1808 return iter;
1809}
1810
1811int tracing_open_generic(struct inode *inode, struct file *filp)
1812{
Steven Rostedt60a11772008-05-12 21:20:44 +02001813 if (tracing_disabled)
1814 return -ENODEV;
1815
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001816 filp->private_data = inode->i_private;
1817 return 0;
1818}
1819
1820int tracing_release(struct inode *inode, struct file *file)
1821{
1822 struct seq_file *m = (struct seq_file *)file->private_data;
1823 struct trace_iterator *iter = m->private;
1824
1825 mutex_lock(&trace_types_lock);
1826 if (iter->trace && iter->trace->close)
1827 iter->trace->close(iter);
1828
1829 /* reenable tracing if it was previously enabled */
1830 if (iter->tr->ctrl)
1831 tracer_enabled = 1;
1832 mutex_unlock(&trace_types_lock);
1833
1834 seq_release(inode, file);
1835 kfree(iter);
1836 return 0;
1837}
1838
1839static int tracing_open(struct inode *inode, struct file *file)
1840{
1841 int ret;
1842
1843 __tracing_open(inode, file, &ret);
1844
1845 return ret;
1846}
1847
1848static int tracing_lt_open(struct inode *inode, struct file *file)
1849{
1850 struct trace_iterator *iter;
1851 int ret;
1852
1853 iter = __tracing_open(inode, file, &ret);
1854
1855 if (!ret)
1856 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1857
1858 return ret;
1859}
1860
1861
Ingo Molnare309b412008-05-12 21:20:51 +02001862static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001863t_next(struct seq_file *m, void *v, loff_t *pos)
1864{
1865 struct tracer *t = m->private;
1866
1867 (*pos)++;
1868
1869 if (t)
1870 t = t->next;
1871
1872 m->private = t;
1873
1874 return t;
1875}
1876
1877static void *t_start(struct seq_file *m, loff_t *pos)
1878{
1879 struct tracer *t = m->private;
1880 loff_t l = 0;
1881
1882 mutex_lock(&trace_types_lock);
1883 for (; t && l < *pos; t = t_next(m, t, &l))
1884 ;
1885
1886 return t;
1887}
1888
1889static void t_stop(struct seq_file *m, void *p)
1890{
1891 mutex_unlock(&trace_types_lock);
1892}
1893
1894static int t_show(struct seq_file *m, void *v)
1895{
1896 struct tracer *t = v;
1897
1898 if (!t)
1899 return 0;
1900
1901 seq_printf(m, "%s", t->name);
1902 if (t->next)
1903 seq_putc(m, ' ');
1904 else
1905 seq_putc(m, '\n');
1906
1907 return 0;
1908}
1909
1910static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001911 .start = t_start,
1912 .next = t_next,
1913 .stop = t_stop,
1914 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001915};
1916
1917static int show_traces_open(struct inode *inode, struct file *file)
1918{
1919 int ret;
1920
Steven Rostedt60a11772008-05-12 21:20:44 +02001921 if (tracing_disabled)
1922 return -ENODEV;
1923
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001924 ret = seq_open(file, &show_traces_seq_ops);
1925 if (!ret) {
1926 struct seq_file *m = file->private_data;
1927 m->private = trace_types;
1928 }
1929
1930 return ret;
1931}
1932
1933static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001934 .open = tracing_open,
1935 .read = seq_read,
1936 .llseek = seq_lseek,
1937 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001938};
1939
1940static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001941 .open = tracing_lt_open,
1942 .read = seq_read,
1943 .llseek = seq_lseek,
1944 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001945};
1946
1947static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001948 .open = show_traces_open,
1949 .read = seq_read,
1950 .release = seq_release,
1951};
1952
Ingo Molnar36dfe922008-05-12 21:20:52 +02001953/*
1954 * Only trace on a CPU if the bitmask is set:
1955 */
1956static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1957
1958/*
1959 * When tracing/tracing_cpu_mask is modified then this holds
1960 * the new bitmask we are about to install:
1961 */
1962static cpumask_t tracing_cpumask_new;
1963
1964/*
1965 * The tracer itself will not take this lock, but still we want
1966 * to provide a consistent cpumask to user-space:
1967 */
1968static DEFINE_MUTEX(tracing_cpumask_update_lock);
1969
1970/*
1971 * Temporary storage for the character representation of the
1972 * CPU bitmask (and one more byte for the newline):
1973 */
1974static char mask_str[NR_CPUS + 1];
1975
Ingo Molnarc7078de2008-05-12 21:20:52 +02001976static ssize_t
1977tracing_cpumask_read(struct file *filp, char __user *ubuf,
1978 size_t count, loff_t *ppos)
1979{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001980 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001981
1982 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001983
1984 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1985 if (count - len < 2) {
1986 count = -EINVAL;
1987 goto out_err;
1988 }
1989 len += sprintf(mask_str + len, "\n");
1990 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1991
1992out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001993 mutex_unlock(&tracing_cpumask_update_lock);
1994
1995 return count;
1996}
1997
1998static ssize_t
1999tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2000 size_t count, loff_t *ppos)
2001{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002002 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002003
2004 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002005 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2006 if (err)
2007 goto err_unlock;
2008
Steven Rostedt92205c22008-05-12 21:20:55 +02002009 raw_local_irq_disable();
2010 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002011 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002012 /*
2013 * Increase/decrease the disabled counter if we are
2014 * about to flip a bit in the cpumask:
2015 */
2016 if (cpu_isset(cpu, tracing_cpumask) &&
2017 !cpu_isset(cpu, tracing_cpumask_new)) {
2018 atomic_inc(&global_trace.data[cpu]->disabled);
2019 }
2020 if (!cpu_isset(cpu, tracing_cpumask) &&
2021 cpu_isset(cpu, tracing_cpumask_new)) {
2022 atomic_dec(&global_trace.data[cpu]->disabled);
2023 }
2024 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002025 __raw_spin_unlock(&ftrace_max_lock);
2026 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002027
2028 tracing_cpumask = tracing_cpumask_new;
2029
Ingo Molnarc7078de2008-05-12 21:20:52 +02002030 mutex_unlock(&tracing_cpumask_update_lock);
2031
Ingo Molnarc7078de2008-05-12 21:20:52 +02002032 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002033
2034err_unlock:
2035 mutex_unlock(&tracing_cpumask_update_lock);
2036
2037 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002038}
2039
2040static struct file_operations tracing_cpumask_fops = {
2041 .open = tracing_open_generic,
2042 .read = tracing_cpumask_read,
2043 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002044};
2045
2046static ssize_t
2047tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2049{
2050 char *buf;
2051 int r = 0;
2052 int len = 0;
2053 int i;
2054
2055 /* calulate max size */
2056 for (i = 0; trace_options[i]; i++) {
2057 len += strlen(trace_options[i]);
2058 len += 3; /* "no" and space */
2059 }
2060
2061 /* +2 for \n and \0 */
2062 buf = kmalloc(len + 2, GFP_KERNEL);
2063 if (!buf)
2064 return -ENOMEM;
2065
2066 for (i = 0; trace_options[i]; i++) {
2067 if (trace_flags & (1 << i))
2068 r += sprintf(buf + r, "%s ", trace_options[i]);
2069 else
2070 r += sprintf(buf + r, "no%s ", trace_options[i]);
2071 }
2072
2073 r += sprintf(buf + r, "\n");
2074 WARN_ON(r >= len + 2);
2075
Ingo Molnar36dfe922008-05-12 21:20:52 +02002076 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002077
2078 kfree(buf);
2079
2080 return r;
2081}
2082
2083static ssize_t
2084tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2085 size_t cnt, loff_t *ppos)
2086{
2087 char buf[64];
2088 char *cmp = buf;
2089 int neg = 0;
2090 int i;
2091
Steven Rostedtcffae432008-05-12 21:21:00 +02002092 if (cnt >= sizeof(buf))
2093 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002094
2095 if (copy_from_user(&buf, ubuf, cnt))
2096 return -EFAULT;
2097
2098 buf[cnt] = 0;
2099
2100 if (strncmp(buf, "no", 2) == 0) {
2101 neg = 1;
2102 cmp += 2;
2103 }
2104
2105 for (i = 0; trace_options[i]; i++) {
2106 int len = strlen(trace_options[i]);
2107
2108 if (strncmp(cmp, trace_options[i], len) == 0) {
2109 if (neg)
2110 trace_flags &= ~(1 << i);
2111 else
2112 trace_flags |= (1 << i);
2113 break;
2114 }
2115 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002116 /*
2117 * If no option could be set, return an error:
2118 */
2119 if (!trace_options[i])
2120 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002121
2122 filp->f_pos += cnt;
2123
2124 return cnt;
2125}
2126
2127static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002128 .open = tracing_open_generic,
2129 .read = tracing_iter_ctrl_read,
2130 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002131};
2132
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002133static const char readme_msg[] =
2134 "tracing mini-HOWTO:\n\n"
2135 "# mkdir /debug\n"
2136 "# mount -t debugfs nodev /debug\n\n"
2137 "# cat /debug/tracing/available_tracers\n"
2138 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2139 "# cat /debug/tracing/current_tracer\n"
2140 "none\n"
2141 "# echo sched_switch > /debug/tracing/current_tracer\n"
2142 "# cat /debug/tracing/current_tracer\n"
2143 "sched_switch\n"
2144 "# cat /debug/tracing/iter_ctrl\n"
2145 "noprint-parent nosym-offset nosym-addr noverbose\n"
2146 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2147 "# echo 1 > /debug/tracing/tracing_enabled\n"
2148 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2149 "echo 0 > /debug/tracing/tracing_enabled\n"
2150;
2151
2152static ssize_t
2153tracing_readme_read(struct file *filp, char __user *ubuf,
2154 size_t cnt, loff_t *ppos)
2155{
2156 return simple_read_from_buffer(ubuf, cnt, ppos,
2157 readme_msg, strlen(readme_msg));
2158}
2159
2160static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002161 .open = tracing_open_generic,
2162 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002163};
2164
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002165static ssize_t
2166tracing_ctrl_read(struct file *filp, char __user *ubuf,
2167 size_t cnt, loff_t *ppos)
2168{
2169 struct trace_array *tr = filp->private_data;
2170 char buf[64];
2171 int r;
2172
2173 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002174 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002175}
2176
2177static ssize_t
2178tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2179 size_t cnt, loff_t *ppos)
2180{
2181 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002182 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002183 long val;
2184 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002185
Steven Rostedtcffae432008-05-12 21:21:00 +02002186 if (cnt >= sizeof(buf))
2187 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188
2189 if (copy_from_user(&buf, ubuf, cnt))
2190 return -EFAULT;
2191
2192 buf[cnt] = 0;
2193
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002194 ret = strict_strtoul(buf, 10, &val);
2195 if (ret < 0)
2196 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002197
2198 val = !!val;
2199
2200 mutex_lock(&trace_types_lock);
2201 if (tr->ctrl ^ val) {
2202 if (val)
2203 tracer_enabled = 1;
2204 else
2205 tracer_enabled = 0;
2206
2207 tr->ctrl = val;
2208
2209 if (current_trace && current_trace->ctrl_update)
2210 current_trace->ctrl_update(tr);
2211 }
2212 mutex_unlock(&trace_types_lock);
2213
2214 filp->f_pos += cnt;
2215
2216 return cnt;
2217}
2218
2219static ssize_t
2220tracing_set_trace_read(struct file *filp, char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2222{
2223 char buf[max_tracer_type_len+2];
2224 int r;
2225
2226 mutex_lock(&trace_types_lock);
2227 if (current_trace)
2228 r = sprintf(buf, "%s\n", current_trace->name);
2229 else
2230 r = sprintf(buf, "\n");
2231 mutex_unlock(&trace_types_lock);
2232
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002233 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234}
2235
2236static ssize_t
2237tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2238 size_t cnt, loff_t *ppos)
2239{
2240 struct trace_array *tr = &global_trace;
2241 struct tracer *t;
2242 char buf[max_tracer_type_len+1];
2243 int i;
2244
2245 if (cnt > max_tracer_type_len)
2246 cnt = max_tracer_type_len;
2247
2248 if (copy_from_user(&buf, ubuf, cnt))
2249 return -EFAULT;
2250
2251 buf[cnt] = 0;
2252
2253 /* strip ending whitespace. */
2254 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2255 buf[i] = 0;
2256
2257 mutex_lock(&trace_types_lock);
2258 for (t = trace_types; t; t = t->next) {
2259 if (strcmp(t->name, buf) == 0)
2260 break;
2261 }
2262 if (!t || t == current_trace)
2263 goto out;
2264
2265 if (current_trace && current_trace->reset)
2266 current_trace->reset(tr);
2267
2268 current_trace = t;
2269 if (t->init)
2270 t->init(tr);
2271
2272 out:
2273 mutex_unlock(&trace_types_lock);
2274
2275 filp->f_pos += cnt;
2276
2277 return cnt;
2278}
2279
2280static ssize_t
2281tracing_max_lat_read(struct file *filp, char __user *ubuf,
2282 size_t cnt, loff_t *ppos)
2283{
2284 unsigned long *ptr = filp->private_data;
2285 char buf[64];
2286 int r;
2287
Steven Rostedtcffae432008-05-12 21:21:00 +02002288 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002290 if (r > sizeof(buf))
2291 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002292 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002293}
2294
2295static ssize_t
2296tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2297 size_t cnt, loff_t *ppos)
2298{
2299 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002300 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002301 long val;
2302 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002303
Steven Rostedtcffae432008-05-12 21:21:00 +02002304 if (cnt >= sizeof(buf))
2305 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002306
2307 if (copy_from_user(&buf, ubuf, cnt))
2308 return -EFAULT;
2309
2310 buf[cnt] = 0;
2311
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002312 ret = strict_strtoul(buf, 10, &val);
2313 if (ret < 0)
2314 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002315
2316 *ptr = val * 1000;
2317
2318 return cnt;
2319}
2320
Steven Rostedtb3806b42008-05-12 21:20:46 +02002321static atomic_t tracing_reader;
2322
2323static int tracing_open_pipe(struct inode *inode, struct file *filp)
2324{
2325 struct trace_iterator *iter;
2326
2327 if (tracing_disabled)
2328 return -ENODEV;
2329
2330 /* We only allow for reader of the pipe */
2331 if (atomic_inc_return(&tracing_reader) != 1) {
2332 atomic_dec(&tracing_reader);
2333 return -EBUSY;
2334 }
2335
2336 /* create a buffer to store the information to pass to userspace */
2337 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2338 if (!iter)
2339 return -ENOMEM;
2340
Steven Rostedt107bad82008-05-12 21:21:01 +02002341 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002342 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002343 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002344 filp->private_data = iter;
2345
Steven Rostedt107bad82008-05-12 21:21:01 +02002346 if (iter->trace->pipe_open)
2347 iter->trace->pipe_open(iter);
2348 mutex_unlock(&trace_types_lock);
2349
Steven Rostedtb3806b42008-05-12 21:20:46 +02002350 return 0;
2351}
2352
2353static int tracing_release_pipe(struct inode *inode, struct file *file)
2354{
2355 struct trace_iterator *iter = file->private_data;
2356
2357 kfree(iter);
2358 atomic_dec(&tracing_reader);
2359
2360 return 0;
2361}
2362
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002363static unsigned int
2364tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2365{
2366 struct trace_iterator *iter = filp->private_data;
2367
2368 if (trace_flags & TRACE_ITER_BLOCK) {
2369 /*
2370 * Always select as readable when in blocking mode
2371 */
2372 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002373 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002374 if (!trace_empty(iter))
2375 return POLLIN | POLLRDNORM;
2376 poll_wait(filp, &trace_wait, poll_table);
2377 if (!trace_empty(iter))
2378 return POLLIN | POLLRDNORM;
2379
2380 return 0;
2381 }
2382}
2383
Steven Rostedtb3806b42008-05-12 21:20:46 +02002384/*
2385 * Consumer reader.
2386 */
2387static ssize_t
2388tracing_read_pipe(struct file *filp, char __user *ubuf,
2389 size_t cnt, loff_t *ppos)
2390{
2391 struct trace_iterator *iter = filp->private_data;
2392 struct trace_array_cpu *data;
2393 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002394 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002395#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002396 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002397#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002398 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002399 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002400
2401 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002402 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2403 if (sret != -EBUSY)
2404 return sret;
2405 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002406
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002407 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002408
Steven Rostedt107bad82008-05-12 21:21:01 +02002409 mutex_lock(&trace_types_lock);
2410 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002411 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2412 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002413 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002414 }
2415
Steven Rostedtb3806b42008-05-12 21:20:46 +02002416 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002417
Steven Rostedt107bad82008-05-12 21:21:01 +02002418 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002419 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002420 goto out;
2421 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002422
Steven Rostedtb3806b42008-05-12 21:20:46 +02002423 /*
2424 * This is a make-shift waitqueue. The reason we don't use
2425 * an actual wait queue is because:
2426 * 1) we only ever have one waiter
2427 * 2) the tracing, traces all functions, we don't want
2428 * the overhead of calling wake_up and friends
2429 * (and tracing them too)
2430 * Anyway, this is really very primitive wakeup.
2431 */
2432 set_current_state(TASK_INTERRUPTIBLE);
2433 iter->tr->waiter = current;
2434
Steven Rostedt107bad82008-05-12 21:21:01 +02002435 mutex_unlock(&trace_types_lock);
2436
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002437 /* sleep for 100 msecs, and try again. */
2438 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002439
Steven Rostedt107bad82008-05-12 21:21:01 +02002440 mutex_lock(&trace_types_lock);
2441
Steven Rostedtb3806b42008-05-12 21:20:46 +02002442 iter->tr->waiter = NULL;
2443
Steven Rostedt107bad82008-05-12 21:21:01 +02002444 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002445 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002446 goto out;
2447 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002448
Steven Rostedt84527992008-05-12 21:20:58 +02002449 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002450 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002451
Steven Rostedtb3806b42008-05-12 21:20:46 +02002452 /*
2453 * We block until we read something and tracing is disabled.
2454 * We still block if tracing is disabled, but we have never
2455 * read anything. This allows a user to cat this file, and
2456 * then enable tracing. But after we have read something,
2457 * we give an EOF when tracing is again disabled.
2458 *
2459 * iter->pos will be 0 if we haven't read anything.
2460 */
2461 if (!tracer_enabled && iter->pos)
2462 break;
2463
2464 continue;
2465 }
2466
2467 /* stop when tracing is finished */
2468 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002469 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002470
2471 if (cnt >= PAGE_SIZE)
2472 cnt = PAGE_SIZE - 1;
2473
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002474 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002475 memset(&iter->seq, 0,
2476 sizeof(struct trace_iterator) -
2477 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002478 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002479
2480 /*
2481 * We need to stop all tracing on all CPUS to read the
2482 * the next buffer. This is a bit expensive, but is
2483 * not done often. We fill all what we can read,
2484 * and then release the locks again.
2485 */
2486
2487 cpus_clear(mask);
2488 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002489#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002490 ftrace_save = ftrace_enabled;
2491 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002492#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002493 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002494 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002495 data = iter->tr->data[cpu];
2496
2497 if (!head_page(data) || !data->trace_idx)
2498 continue;
2499
2500 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002501 cpu_set(cpu, mask);
2502 }
2503
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002504 for_each_cpu_mask(cpu, mask) {
2505 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002506 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002507
2508 if (data->overrun > iter->last_overrun[cpu])
2509 iter->overrun[cpu] +=
2510 data->overrun - iter->last_overrun[cpu];
2511 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002512 }
2513
Steven Rostedt088b1e422008-05-12 21:20:48 +02002514 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002515 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002516 int len = iter->seq.len;
2517
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002518 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002519 if (!ret) {
2520 /* don't print partial lines */
2521 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002522 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002523 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002524
2525 trace_consume(iter);
2526
2527 if (iter->seq.len >= cnt)
2528 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002529 }
2530
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002531 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002532 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002533 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002534 }
2535
2536 for_each_cpu_mask(cpu, mask) {
2537 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002538 atomic_dec(&data->disabled);
2539 }
Ingo Molnar25770462008-05-12 21:20:49 +02002540#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002541 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002542#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002543 local_irq_restore(flags);
2544
2545 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002546 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2547 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002548 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002549 if (sret == -EBUSY)
2550 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002551
Steven Rostedt107bad82008-05-12 21:21:01 +02002552out:
2553 mutex_unlock(&trace_types_lock);
2554
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002555 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002556}
2557
Steven Rostedta98a3c32008-05-12 21:20:59 +02002558static ssize_t
2559tracing_entries_read(struct file *filp, char __user *ubuf,
2560 size_t cnt, loff_t *ppos)
2561{
2562 struct trace_array *tr = filp->private_data;
2563 char buf[64];
2564 int r;
2565
2566 r = sprintf(buf, "%lu\n", tr->entries);
2567 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2568}
2569
2570static ssize_t
2571tracing_entries_write(struct file *filp, const char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2573{
2574 unsigned long val;
2575 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002576 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002577
Steven Rostedtcffae432008-05-12 21:21:00 +02002578 if (cnt >= sizeof(buf))
2579 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002580
2581 if (copy_from_user(&buf, ubuf, cnt))
2582 return -EFAULT;
2583
2584 buf[cnt] = 0;
2585
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002586 ret = strict_strtoul(buf, 10, &val);
2587 if (ret < 0)
2588 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002589
2590 /* must have at least 1 entry */
2591 if (!val)
2592 return -EINVAL;
2593
2594 mutex_lock(&trace_types_lock);
2595
2596 if (current_trace != &no_tracer) {
2597 cnt = -EBUSY;
2598 pr_info("ftrace: set current_tracer to none"
2599 " before modifying buffer size\n");
2600 goto out;
2601 }
2602
2603 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002604 long pages_requested;
2605 unsigned long freeable_pages;
2606
2607 /* make sure we have enough memory before mapping */
2608 pages_requested =
2609 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2610
2611 /* account for each buffer (and max_tr) */
2612 pages_requested *= tracing_nr_buffers * 2;
2613
2614 /* Check for overflow */
2615 if (pages_requested < 0) {
2616 cnt = -ENOMEM;
2617 goto out;
2618 }
2619
2620 freeable_pages = determine_dirtyable_memory();
2621
2622 /* we only allow to request 1/4 of useable memory */
2623 if (pages_requested >
2624 ((freeable_pages + tracing_pages_allocated) / 4)) {
2625 cnt = -ENOMEM;
2626 goto out;
2627 }
2628
Steven Rostedta98a3c32008-05-12 21:20:59 +02002629 while (global_trace.entries < val) {
2630 if (trace_alloc_page()) {
2631 cnt = -ENOMEM;
2632 goto out;
2633 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002634 /* double check that we don't go over the known pages */
2635 if (tracing_pages_allocated > pages_requested)
2636 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002637 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002638
Steven Rostedta98a3c32008-05-12 21:20:59 +02002639 } else {
2640 /* include the number of entries in val (inc of page entries) */
2641 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2642 trace_free_page();
2643 }
2644
Steven Rostedt19384c02008-05-22 00:22:16 -04002645 /* check integrity */
2646 for_each_tracing_cpu(i)
2647 check_pages(global_trace.data[i]);
2648
Steven Rostedta98a3c32008-05-12 21:20:59 +02002649 filp->f_pos += cnt;
2650
Steven Rostedt19384c02008-05-22 00:22:16 -04002651 /* If check pages failed, return ENOMEM */
2652 if (tracing_disabled)
2653 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002654 out:
2655 max_tr.entries = global_trace.entries;
2656 mutex_unlock(&trace_types_lock);
2657
2658 return cnt;
2659}
2660
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002661static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002662 .open = tracing_open_generic,
2663 .read = tracing_max_lat_read,
2664 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002665};
2666
2667static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002668 .open = tracing_open_generic,
2669 .read = tracing_ctrl_read,
2670 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002671};
2672
2673static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002674 .open = tracing_open_generic,
2675 .read = tracing_set_trace_read,
2676 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002677};
2678
Steven Rostedtb3806b42008-05-12 21:20:46 +02002679static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002680 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002681 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002682 .read = tracing_read_pipe,
2683 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002684};
2685
Steven Rostedta98a3c32008-05-12 21:20:59 +02002686static struct file_operations tracing_entries_fops = {
2687 .open = tracing_open_generic,
2688 .read = tracing_entries_read,
2689 .write = tracing_entries_write,
2690};
2691
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002692#ifdef CONFIG_DYNAMIC_FTRACE
2693
2694static ssize_t
2695tracing_read_long(struct file *filp, char __user *ubuf,
2696 size_t cnt, loff_t *ppos)
2697{
2698 unsigned long *p = filp->private_data;
2699 char buf[64];
2700 int r;
2701
2702 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002703
2704 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002705}
2706
2707static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002708 .open = tracing_open_generic,
2709 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002710};
2711#endif
2712
2713static struct dentry *d_tracer;
2714
2715struct dentry *tracing_init_dentry(void)
2716{
2717 static int once;
2718
2719 if (d_tracer)
2720 return d_tracer;
2721
2722 d_tracer = debugfs_create_dir("tracing", NULL);
2723
2724 if (!d_tracer && !once) {
2725 once = 1;
2726 pr_warning("Could not create debugfs directory 'tracing'\n");
2727 return NULL;
2728 }
2729
2730 return d_tracer;
2731}
2732
Steven Rostedt60a11772008-05-12 21:20:44 +02002733#ifdef CONFIG_FTRACE_SELFTEST
2734/* Let selftest have access to static functions in this file */
2735#include "trace_selftest.c"
2736#endif
2737
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002738static __init void tracer_init_debugfs(void)
2739{
2740 struct dentry *d_tracer;
2741 struct dentry *entry;
2742
2743 d_tracer = tracing_init_dentry();
2744
2745 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2746 &global_trace, &tracing_ctrl_fops);
2747 if (!entry)
2748 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2749
2750 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2751 NULL, &tracing_iter_fops);
2752 if (!entry)
2753 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2754
Ingo Molnarc7078de2008-05-12 21:20:52 +02002755 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2756 NULL, &tracing_cpumask_fops);
2757 if (!entry)
2758 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2759
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002760 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2761 &global_trace, &tracing_lt_fops);
2762 if (!entry)
2763 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2764
2765 entry = debugfs_create_file("trace", 0444, d_tracer,
2766 &global_trace, &tracing_fops);
2767 if (!entry)
2768 pr_warning("Could not create debugfs 'trace' entry\n");
2769
2770 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2771 &global_trace, &show_traces_fops);
2772 if (!entry)
2773 pr_warning("Could not create debugfs 'trace' entry\n");
2774
2775 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2776 &global_trace, &set_tracer_fops);
2777 if (!entry)
2778 pr_warning("Could not create debugfs 'trace' entry\n");
2779
2780 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2781 &tracing_max_latency,
2782 &tracing_max_lat_fops);
2783 if (!entry)
2784 pr_warning("Could not create debugfs "
2785 "'tracing_max_latency' entry\n");
2786
2787 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2788 &tracing_thresh, &tracing_max_lat_fops);
2789 if (!entry)
2790 pr_warning("Could not create debugfs "
2791 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002792 entry = debugfs_create_file("README", 0644, d_tracer,
2793 NULL, &tracing_readme_fops);
2794 if (!entry)
2795 pr_warning("Could not create debugfs 'README' entry\n");
2796
Steven Rostedtb3806b42008-05-12 21:20:46 +02002797 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2798 NULL, &tracing_pipe_fops);
2799 if (!entry)
2800 pr_warning("Could not create debugfs "
2801 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002802
Steven Rostedta98a3c32008-05-12 21:20:59 +02002803 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2804 &global_trace, &tracing_entries_fops);
2805 if (!entry)
2806 pr_warning("Could not create debugfs "
2807 "'tracing_threash' entry\n");
2808
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002809#ifdef CONFIG_DYNAMIC_FTRACE
2810 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2811 &ftrace_update_tot_cnt,
2812 &tracing_read_long_fops);
2813 if (!entry)
2814 pr_warning("Could not create debugfs "
2815 "'dyn_ftrace_total_info' entry\n");
2816#endif
2817}
2818
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002819static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002820{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002821 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002822 struct page *page, *tmp;
2823 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002824 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002825 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002826 int i;
2827
2828 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002829 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002830 array = (void *)__get_free_page(GFP_KERNEL);
2831 if (array == NULL) {
2832 printk(KERN_ERR "tracer: failed to allocate page"
2833 "for trace buffer!\n");
2834 goto free_pages;
2835 }
2836
Steven Rostedt3eefae92008-05-12 21:21:04 +02002837 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002838 page = virt_to_page(array);
2839 list_add(&page->lru, &pages);
2840
2841/* Only allocate if we are actually using the max trace */
2842#ifdef CONFIG_TRACER_MAX_TRACE
2843 array = (void *)__get_free_page(GFP_KERNEL);
2844 if (array == NULL) {
2845 printk(KERN_ERR "tracer: failed to allocate page"
2846 "for trace buffer!\n");
2847 goto free_pages;
2848 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002849 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002850 page = virt_to_page(array);
2851 list_add(&page->lru, &pages);
2852#endif
2853 }
2854
2855 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002856 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002857 data = global_trace.data[i];
2858 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002859 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002860 list_add_tail(&page->lru, &data->trace_pages);
2861 ClearPageLRU(page);
2862
2863#ifdef CONFIG_TRACER_MAX_TRACE
2864 data = max_tr.data[i];
2865 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002866 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002867 list_add_tail(&page->lru, &data->trace_pages);
2868 SetPageLRU(page);
2869#endif
2870 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002871 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002872 global_trace.entries += ENTRIES_PER_PAGE;
2873
2874 return 0;
2875
2876 free_pages:
2877 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002878 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002879 __free_page(page);
2880 }
2881 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002882}
2883
Steven Rostedta98a3c32008-05-12 21:20:59 +02002884static int trace_free_page(void)
2885{
2886 struct trace_array_cpu *data;
2887 struct page *page;
2888 struct list_head *p;
2889 int i;
2890 int ret = 0;
2891
2892 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002893 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002894 data = global_trace.data[i];
2895 p = data->trace_pages.next;
2896 if (p == &data->trace_pages) {
2897 /* should never happen */
2898 WARN_ON(1);
2899 tracing_disabled = 1;
2900 ret = -1;
2901 break;
2902 }
2903 page = list_entry(p, struct page, lru);
2904 ClearPageLRU(page);
2905 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02002906 tracing_pages_allocated--;
2907 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002908 __free_page(page);
2909
2910 tracing_reset(data);
2911
2912#ifdef CONFIG_TRACER_MAX_TRACE
2913 data = max_tr.data[i];
2914 p = data->trace_pages.next;
2915 if (p == &data->trace_pages) {
2916 /* should never happen */
2917 WARN_ON(1);
2918 tracing_disabled = 1;
2919 ret = -1;
2920 break;
2921 }
2922 page = list_entry(p, struct page, lru);
2923 ClearPageLRU(page);
2924 list_del(&page->lru);
2925 __free_page(page);
2926
2927 tracing_reset(data);
2928#endif
2929 }
2930 global_trace.entries -= ENTRIES_PER_PAGE;
2931
2932 return ret;
2933}
2934
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002935__init static int tracer_alloc_buffers(void)
2936{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002937 struct trace_array_cpu *data;
2938 void *array;
2939 struct page *page;
2940 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002941 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942 int i;
2943
Steven Rostedt26994ea2008-05-12 21:20:48 +02002944 global_trace.ctrl = tracer_enabled;
2945
Steven Rostedtab464282008-05-12 21:21:00 +02002946 /* TODO: make the number of buffers hot pluggable with CPUS */
2947 tracing_nr_buffers = num_possible_cpus();
2948 tracing_buffer_mask = cpu_possible_map;
2949
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002950 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002951 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002952 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002953 max_tr.data[i] = &per_cpu(max_data, i);
2954
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002955 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002956 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002957 printk(KERN_ERR "tracer: failed to allocate page"
2958 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002959 goto free_buffers;
2960 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002961
2962 /* set the array to the list */
2963 INIT_LIST_HEAD(&data->trace_pages);
2964 page = virt_to_page(array);
2965 list_add(&page->lru, &data->trace_pages);
2966 /* use the LRU flag to differentiate the two buffers */
2967 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002968
Steven Rostedta98a3c32008-05-12 21:20:59 +02002969 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2970 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2971
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002972/* Only allocate if we are actually using the max trace */
2973#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002974 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002975 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002976 printk(KERN_ERR "tracer: failed to allocate page"
2977 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002978 goto free_buffers;
2979 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002980
2981 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2982 page = virt_to_page(array);
2983 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2984 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002985#endif
2986 }
2987
2988 /*
2989 * Since we allocate by orders of pages, we may be able to
2990 * round up a bit.
2991 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002992 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002993 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002995 while (global_trace.entries < trace_nr_entries) {
2996 if (trace_alloc_page())
2997 break;
2998 pages++;
2999 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003000 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003001
3002 pr_info("tracer: %d pages allocated for %ld",
3003 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003004 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3005 pr_info(" actual entries %ld\n", global_trace.entries);
3006
3007 tracer_init_debugfs();
3008
3009 trace_init_cmdlines();
3010
3011 register_tracer(&no_tracer);
3012 current_trace = &no_tracer;
3013
Steven Rostedt60a11772008-05-12 21:20:44 +02003014 /* All seems OK, enable tracing */
3015 tracing_disabled = 0;
3016
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003017 return 0;
3018
3019 free_buffers:
3020 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003021 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003022 struct trace_array_cpu *data = global_trace.data[i];
3023
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003024 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003025 list_for_each_entry_safe(page, tmp,
3026 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003027 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003028 __free_page(page);
3029 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003030 }
3031
3032#ifdef CONFIG_TRACER_MAX_TRACE
3033 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003034 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003035 list_for_each_entry_safe(page, tmp,
3036 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003037 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003038 __free_page(page);
3039 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003040 }
3041#endif
3042 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003043 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003044}
Steven Rostedt60a11772008-05-12 21:20:44 +02003045fs_initcall(tracer_alloc_buffers);