blob: 0feae23d98931e1f1a725296fc0977411fc09eb3 [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
Steven Rostedt25b0b442008-05-12 21:21:00 +0200655/* temporary disable recording */
656atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200657
658static void trace_init_cmdlines(void)
659{
660 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
661 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
662 cmdline_idx = 0;
663}
664
Ingo Molnare309b412008-05-12 21:20:51 +0200665void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200666
Ingo Molnare309b412008-05-12 21:20:51 +0200667static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200668{
669 unsigned map;
670 unsigned idx;
671
672 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
673 return;
674
675 /*
676 * It's not the end of the world if we don't get
677 * the lock, but we also don't want to spin
678 * nor do we want to disable interrupts,
679 * so if we miss here, then better luck next time.
680 */
681 if (!spin_trylock(&trace_cmdline_lock))
682 return;
683
684 idx = map_pid_to_cmdline[tsk->pid];
685 if (idx >= SAVED_CMDLINES) {
686 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
687
688 map = map_cmdline_to_pid[idx];
689 if (map <= PID_MAX_DEFAULT)
690 map_pid_to_cmdline[map] = (unsigned)-1;
691
692 map_pid_to_cmdline[tsk->pid] = idx;
693
694 cmdline_idx = idx;
695 }
696
697 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
698
699 spin_unlock(&trace_cmdline_lock);
700}
701
Ingo Molnare309b412008-05-12 21:20:51 +0200702static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200703{
704 char *cmdline = "<...>";
705 unsigned map;
706
707 if (!pid)
708 return "<idle>";
709
710 if (pid > PID_MAX_DEFAULT)
711 goto out;
712
713 map = map_pid_to_cmdline[pid];
714 if (map >= SAVED_CMDLINES)
715 goto out;
716
717 cmdline = saved_cmdlines[map];
718
719 out:
720 return cmdline;
721}
722
Ingo Molnare309b412008-05-12 21:20:51 +0200723void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200724{
725 if (atomic_read(&trace_record_cmdline_disabled))
726 return;
727
728 trace_save_cmdline(tsk);
729}
730
Ingo Molnare309b412008-05-12 21:20:51 +0200731static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200732trace_next_list(struct trace_array_cpu *data, struct list_head *next)
733{
734 /*
735 * Roundrobin - but skip the head (which is not a real page):
736 */
737 next = next->next;
738 if (unlikely(next == &data->trace_pages))
739 next = next->next;
740 BUG_ON(next == &data->trace_pages);
741
742 return next;
743}
744
Ingo Molnare309b412008-05-12 21:20:51 +0200745static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200746trace_next_page(struct trace_array_cpu *data, void *addr)
747{
748 struct list_head *next;
749 struct page *page;
750
751 page = virt_to_page(addr);
752
753 next = trace_next_list(data, &page->lru);
754 page = list_entry(next, struct page, lru);
755
756 return page_address(page);
757}
758
Ingo Molnare309b412008-05-12 21:20:51 +0200759static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200760tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200761{
762 unsigned long idx, idx_next;
763 struct trace_entry *entry;
764
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200765 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200766 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200767 idx_next = idx + 1;
768
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200769 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
770
Steven Rostedt93a588f2008-05-12 21:20:45 +0200771 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200772
773 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200774 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200775 idx_next = 0;
776 }
777
Steven Rostedt93a588f2008-05-12 21:20:45 +0200778 if (data->trace_head == data->trace_tail &&
779 idx_next == data->trace_tail_idx) {
780 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200781 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200782 data->trace_tail_idx++;
783 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
784 data->trace_tail =
785 trace_next_page(data, data->trace_tail);
786 data->trace_tail_idx = 0;
787 }
788 }
789
790 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200791
792 return entry;
793}
794
Ingo Molnare309b412008-05-12 21:20:51 +0200795static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200796tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200797{
798 struct task_struct *tsk = current;
799 unsigned long pc;
800
801 pc = preempt_count();
802
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200803 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200804 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200805 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200806 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
807 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
808 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
809 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
810}
811
Ingo Molnare309b412008-05-12 21:20:51 +0200812void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200813trace_function(struct trace_array *tr, struct trace_array_cpu *data,
814 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200815{
816 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200817 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818
Steven Rostedt92205c22008-05-12 21:20:55 +0200819 raw_local_irq_save(irq_flags);
820 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200821 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200822 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200823 entry->type = TRACE_FN;
824 entry->fn.ip = ip;
825 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200826 __raw_spin_unlock(&data->lock);
827 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828}
829
Ingo Molnare309b412008-05-12 21:20:51 +0200830void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200831ftrace(struct trace_array *tr, struct trace_array_cpu *data,
832 unsigned long ip, unsigned long parent_ip, unsigned long flags)
833{
834 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200835 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200836}
837
Ingo Molnar86387f72008-05-12 21:20:51 +0200838void __trace_stack(struct trace_array *tr,
839 struct trace_array_cpu *data,
840 unsigned long flags,
841 int skip)
842{
843 struct trace_entry *entry;
844 struct stack_trace trace;
845
846 if (!(trace_flags & TRACE_ITER_STACKTRACE))
847 return;
848
849 entry = tracing_get_trace_entry(tr, data);
850 tracing_generic_entry_update(entry, flags);
851 entry->type = TRACE_STACK;
852
853 memset(&entry->stack, 0, sizeof(entry->stack));
854
855 trace.nr_entries = 0;
856 trace.max_entries = FTRACE_STACK_ENTRIES;
857 trace.skip = skip;
858 trace.entries = entry->stack.caller;
859
860 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200861}
862
Ingo Molnare309b412008-05-12 21:20:51 +0200863void
Ingo Molnara4feb832008-05-12 21:21:02 +0200864__trace_special(void *__tr, void *__data,
865 unsigned long arg1, unsigned long arg2, unsigned long arg3)
866{
867 struct trace_array_cpu *data = __data;
868 struct trace_array *tr = __tr;
869 struct trace_entry *entry;
870 unsigned long irq_flags;
871
872 raw_local_irq_save(irq_flags);
873 __raw_spin_lock(&data->lock);
874 entry = tracing_get_trace_entry(tr, data);
875 tracing_generic_entry_update(entry, 0);
876 entry->type = TRACE_SPECIAL;
877 entry->special.arg1 = arg1;
878 entry->special.arg2 = arg2;
879 entry->special.arg3 = arg3;
880 __trace_stack(tr, data, irq_flags, 4);
881 __raw_spin_unlock(&data->lock);
882 raw_local_irq_restore(irq_flags);
883
884 trace_wake_up();
885}
886
887void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200888tracing_sched_switch_trace(struct trace_array *tr,
889 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200890 struct task_struct *prev,
891 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200892 unsigned long flags)
893{
894 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200895 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200896
Steven Rostedt92205c22008-05-12 21:20:55 +0200897 raw_local_irq_save(irq_flags);
898 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200899 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200900 tracing_generic_entry_update(entry, flags);
901 entry->type = TRACE_CTX;
902 entry->ctx.prev_pid = prev->pid;
903 entry->ctx.prev_prio = prev->prio;
904 entry->ctx.prev_state = prev->state;
905 entry->ctx.next_pid = next->pid;
906 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200907 entry->ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200908 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200909 __raw_spin_unlock(&data->lock);
910 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200911}
912
Ingo Molnar57422792008-05-12 21:20:51 +0200913void
914tracing_sched_wakeup_trace(struct trace_array *tr,
915 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200916 struct task_struct *wakee,
917 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200918 unsigned long flags)
919{
920 struct trace_entry *entry;
921 unsigned long irq_flags;
922
Steven Rostedt92205c22008-05-12 21:20:55 +0200923 raw_local_irq_save(irq_flags);
924 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200925 entry = tracing_get_trace_entry(tr, data);
926 tracing_generic_entry_update(entry, flags);
927 entry->type = TRACE_WAKE;
928 entry->ctx.prev_pid = curr->pid;
929 entry->ctx.prev_prio = curr->prio;
930 entry->ctx.prev_state = curr->state;
931 entry->ctx.next_pid = wakee->pid;
932 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200933 entry->ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200934 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200935 __raw_spin_unlock(&data->lock);
936 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200937
938 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200939}
940
Steven Rostedt4902f882008-05-22 00:22:18 -0400941void
942ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
943{
944 struct trace_array *tr = &global_trace;
945 struct trace_array_cpu *data;
946 unsigned long flags;
947 long disabled;
948 int cpu;
949
950 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
951 return;
952
953 local_irq_save(flags);
954 cpu = raw_smp_processor_id();
955 data = tr->data[cpu];
956 disabled = atomic_inc_return(&data->disabled);
957
958 if (likely(disabled == 1))
959 __trace_special(tr, data, arg1, arg2, arg3);
960
961 atomic_dec(&data->disabled);
962 local_irq_restore(flags);
963}
964
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200965#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200966static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200967function_trace_call(unsigned long ip, unsigned long parent_ip)
968{
969 struct trace_array *tr = &global_trace;
970 struct trace_array_cpu *data;
971 unsigned long flags;
972 long disabled;
973 int cpu;
974
975 if (unlikely(!tracer_enabled))
976 return;
977
978 local_irq_save(flags);
979 cpu = raw_smp_processor_id();
980 data = tr->data[cpu];
981 disabled = atomic_inc_return(&data->disabled);
982
983 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200984 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200985
986 atomic_dec(&data->disabled);
987 local_irq_restore(flags);
988}
989
990static struct ftrace_ops trace_ops __read_mostly =
991{
992 .func = function_trace_call,
993};
994
Ingo Molnare309b412008-05-12 21:20:51 +0200995void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200996{
997 register_ftrace_function(&trace_ops);
998}
999
Ingo Molnare309b412008-05-12 21:20:51 +02001000void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001001{
1002 unregister_ftrace_function(&trace_ops);
1003}
1004#endif
1005
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001006enum trace_file_type {
1007 TRACE_FILE_LAT_FMT = 1,
1008};
1009
1010static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001011trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1012 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001013{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001014 struct page *page;
1015 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001017 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001018 iter->next_idx[cpu] >= data->trace_idx ||
1019 (data->trace_head == data->trace_tail &&
1020 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021 return NULL;
1022
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001023 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001024 /* Initialize the iterator for this cpu trace buffer */
1025 WARN_ON(!data->trace_tail);
1026 page = virt_to_page(data->trace_tail);
1027 iter->next_page[cpu] = &page->lru;
1028 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001029 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001030
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001031 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001032 BUG_ON(&data->trace_pages == &page->lru);
1033
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001034 array = page_address(page);
1035
Steven Rostedt93a588f2008-05-12 21:20:45 +02001036 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001037 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001038}
1039
Ingo Molnare309b412008-05-12 21:20:51 +02001040static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1042{
1043 struct trace_array *tr = iter->tr;
1044 struct trace_entry *ent, *next = NULL;
1045 int next_cpu = -1;
1046 int cpu;
1047
Steven Rostedtab464282008-05-12 21:21:00 +02001048 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001049 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001050 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001051 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001052 /*
1053 * Pick the entry with the smallest timestamp:
1054 */
1055 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056 next = ent;
1057 next_cpu = cpu;
1058 }
1059 }
1060
1061 if (ent_cpu)
1062 *ent_cpu = next_cpu;
1063
1064 return next;
1065}
1066
Ingo Molnare309b412008-05-12 21:20:51 +02001067static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001068{
1069 iter->idx++;
1070 iter->next_idx[iter->cpu]++;
1071 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001072
Steven Rostedtb3806b42008-05-12 21:20:46 +02001073 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1074 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1075
1076 iter->next_page_idx[iter->cpu] = 0;
1077 iter->next_page[iter->cpu] =
1078 trace_next_list(data, iter->next_page[iter->cpu]);
1079 }
1080}
1081
Ingo Molnare309b412008-05-12 21:20:51 +02001082static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001083{
1084 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1085
1086 data->trace_tail_idx++;
1087 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1088 data->trace_tail = trace_next_page(data, data->trace_tail);
1089 data->trace_tail_idx = 0;
1090 }
1091
1092 /* Check if we empty it, then reset the index */
1093 if (data->trace_head == data->trace_tail &&
1094 data->trace_head_idx == data->trace_tail_idx)
1095 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001096}
1097
Ingo Molnare309b412008-05-12 21:20:51 +02001098static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099{
1100 struct trace_entry *next;
1101 int next_cpu = -1;
1102
1103 next = find_next_entry(iter, &next_cpu);
1104
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001105 iter->prev_ent = iter->ent;
1106 iter->prev_cpu = iter->cpu;
1107
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108 iter->ent = next;
1109 iter->cpu = next_cpu;
1110
Steven Rostedtb3806b42008-05-12 21:20:46 +02001111 if (next)
1112 trace_iterator_increment(iter);
1113
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001114 return next ? iter : NULL;
1115}
1116
Ingo Molnare309b412008-05-12 21:20:51 +02001117static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118{
1119 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120 void *last_ent = iter->ent;
1121 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001122 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001123
1124 (*pos)++;
1125
1126 /* can't go backwards */
1127 if (iter->idx > i)
1128 return NULL;
1129
1130 if (iter->idx < 0)
1131 ent = find_next_entry_inc(iter);
1132 else
1133 ent = iter;
1134
1135 while (ent && iter->idx < i)
1136 ent = find_next_entry_inc(iter);
1137
1138 iter->pos = *pos;
1139
1140 if (last_ent && !ent)
1141 seq_puts(m, "\n\nvim:ft=help\n");
1142
1143 return ent;
1144}
1145
1146static void *s_start(struct seq_file *m, loff_t *pos)
1147{
1148 struct trace_iterator *iter = m->private;
1149 void *p = NULL;
1150 loff_t l = 0;
1151 int i;
1152
1153 mutex_lock(&trace_types_lock);
1154
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001155 if (!current_trace || current_trace != iter->trace) {
1156 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001158 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001159
1160 atomic_inc(&trace_record_cmdline_disabled);
1161
1162 /* let the tracer grab locks here if needed */
1163 if (current_trace->start)
1164 current_trace->start(iter);
1165
1166 if (*pos != iter->pos) {
1167 iter->ent = NULL;
1168 iter->cpu = 0;
1169 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001170 iter->prev_ent = NULL;
1171 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172
Steven Rostedtab464282008-05-12 21:21:00 +02001173 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001175 iter->next_page[i] = NULL;
1176 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177
1178 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1179 ;
1180
1181 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001182 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001183 p = s_next(m, p, &l);
1184 }
1185
1186 return p;
1187}
1188
1189static void s_stop(struct seq_file *m, void *p)
1190{
1191 struct trace_iterator *iter = m->private;
1192
1193 atomic_dec(&trace_record_cmdline_disabled);
1194
1195 /* let the tracer release locks here if needed */
1196 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1197 iter->trace->stop(iter);
1198
1199 mutex_unlock(&trace_types_lock);
1200}
1201
Steven Rostedtb3806b42008-05-12 21:20:46 +02001202static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001203seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001204{
1205#ifdef CONFIG_KALLSYMS
1206 char str[KSYM_SYMBOL_LEN];
1207
1208 kallsyms_lookup(address, NULL, NULL, NULL, str);
1209
Steven Rostedtb3806b42008-05-12 21:20:46 +02001210 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001212 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001213}
1214
Steven Rostedtb3806b42008-05-12 21:20:46 +02001215static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001216seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1217 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001218{
1219#ifdef CONFIG_KALLSYMS
1220 char str[KSYM_SYMBOL_LEN];
1221
1222 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001223 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001225 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001226}
1227
1228#ifndef CONFIG_64BIT
1229# define IP_FMT "%08lx"
1230#else
1231# define IP_FMT "%016lx"
1232#endif
1233
Ingo Molnare309b412008-05-12 21:20:51 +02001234static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001235seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001236{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001237 int ret;
1238
1239 if (!ip)
1240 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001241
1242 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001243 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001245 ret = seq_print_sym_short(s, "%s", ip);
1246
1247 if (!ret)
1248 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249
1250 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001251 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1252 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253}
1254
Ingo Molnare309b412008-05-12 21:20:51 +02001255static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001256{
1257 seq_puts(m, "# _------=> CPU# \n");
1258 seq_puts(m, "# / _-----=> irqs-off \n");
1259 seq_puts(m, "# | / _----=> need-resched \n");
1260 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1261 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1262 seq_puts(m, "# |||| / \n");
1263 seq_puts(m, "# ||||| delay \n");
1264 seq_puts(m, "# cmd pid ||||| time | caller \n");
1265 seq_puts(m, "# \\ / ||||| \\ | / \n");
1266}
1267
Ingo Molnare309b412008-05-12 21:20:51 +02001268static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269{
1270 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1271 seq_puts(m, "# | | | | |\n");
1272}
1273
1274
Ingo Molnare309b412008-05-12 21:20:51 +02001275static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1277{
1278 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1279 struct trace_array *tr = iter->tr;
1280 struct trace_array_cpu *data = tr->data[tr->cpu];
1281 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001282 unsigned long total = 0;
1283 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001284 int cpu;
1285 const char *name = "preemption";
1286
1287 if (type)
1288 name = type->name;
1289
Steven Rostedtab464282008-05-12 21:21:00 +02001290 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001291 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001292 total += tr->data[cpu]->trace_idx;
1293 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001294 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001295 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001296 entries += tr->data[cpu]->trace_idx;
1297 }
1298 }
1299
1300 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1301 name, UTS_RELEASE);
1302 seq_puts(m, "-----------------------------------"
1303 "---------------------------------\n");
1304 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1305 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001306 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001307 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001308 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309 tr->cpu,
1310#if defined(CONFIG_PREEMPT_NONE)
1311 "server",
1312#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1313 "desktop",
1314#elif defined(CONFIG_PREEMPT_DESKTOP)
1315 "preempt",
1316#else
1317 "unknown",
1318#endif
1319 /* These are reserved for later use */
1320 0, 0, 0, 0);
1321#ifdef CONFIG_SMP
1322 seq_printf(m, " #P:%d)\n", num_online_cpus());
1323#else
1324 seq_puts(m, ")\n");
1325#endif
1326 seq_puts(m, " -----------------\n");
1327 seq_printf(m, " | task: %.16s-%d "
1328 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1329 data->comm, data->pid, data->uid, data->nice,
1330 data->policy, data->rt_priority);
1331 seq_puts(m, " -----------------\n");
1332
1333 if (data->critical_start) {
1334 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001335 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1336 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001337 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001338 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1339 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340 seq_puts(m, "\n");
1341 }
1342
1343 seq_puts(m, "\n");
1344}
1345
Ingo Molnare309b412008-05-12 21:20:51 +02001346static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001347lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348{
1349 int hardirq, softirq;
1350 char *comm;
1351
1352 comm = trace_find_cmdline(entry->pid);
1353
Steven Rostedt214023c2008-05-12 21:20:46 +02001354 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1355 trace_seq_printf(s, "%d", cpu);
1356 trace_seq_printf(s, "%c%c",
1357 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1358 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001359
1360 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1361 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001362 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001363 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001364 } else {
1365 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001366 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001367 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001368 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001369 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001371 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372 }
1373 }
1374
1375 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001376 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001377 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001378 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001379}
1380
1381unsigned long preempt_mark_thresh = 100;
1382
Ingo Molnare309b412008-05-12 21:20:51 +02001383static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001384lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001385 unsigned long rel_usecs)
1386{
Steven Rostedt214023c2008-05-12 21:20:46 +02001387 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001388 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001389 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001390 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001391 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001392 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001393 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001394}
1395
1396static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1397
Ingo Molnare309b412008-05-12 21:20:51 +02001398static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001399print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001400{
Steven Rostedt214023c2008-05-12 21:20:46 +02001401 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001402 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1403 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1404 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1405 struct trace_entry *entry = iter->ent;
1406 unsigned long abs_usecs;
1407 unsigned long rel_usecs;
1408 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001409 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001410 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001411 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001412
1413 if (!next_entry)
1414 next_entry = entry;
1415 rel_usecs = ns2usecs(next_entry->t - entry->t);
1416 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1417
1418 if (verbose) {
1419 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001420 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1421 " %ld.%03ldms (+%ld.%03ldms): ",
1422 comm,
1423 entry->pid, cpu, entry->flags,
1424 entry->preempt_count, trace_idx,
1425 ns2usecs(entry->t),
1426 abs_usecs/1000,
1427 abs_usecs % 1000, rel_usecs/1000,
1428 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001430 lat_print_generic(s, entry, cpu);
1431 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001432 }
1433 switch (entry->type) {
1434 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001435 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1436 trace_seq_puts(s, " (");
1437 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1438 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001439 break;
1440 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001441 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001442 T = entry->ctx.next_state < sizeof(state_to_char) ?
1443 state_to_char[entry->ctx.next_state] : 'X';
1444
Ankita Gargd17d9692008-05-12 21:20:58 +02001445 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1446 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001448 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001449 entry->ctx.prev_pid,
1450 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001451 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001452 entry->ctx.next_pid,
1453 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001454 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001456 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001457 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001458 entry->special.arg1,
1459 entry->special.arg2,
1460 entry->special.arg3);
1461 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001462 case TRACE_STACK:
1463 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1464 if (i)
1465 trace_seq_puts(s, " <= ");
1466 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1467 }
1468 trace_seq_puts(s, "\n");
1469 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001470 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001471 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001473 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001474}
1475
Ingo Molnare309b412008-05-12 21:20:51 +02001476static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477{
Steven Rostedt214023c2008-05-12 21:20:46 +02001478 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001480 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001481 unsigned long usec_rem;
1482 unsigned long long t;
1483 unsigned long secs;
1484 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001485 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001486 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001487 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001489 entry = iter->ent;
1490
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001491 comm = trace_find_cmdline(iter->ent->pid);
1492
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001493 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001494 usec_rem = do_div(t, 1000000ULL);
1495 secs = (unsigned long)t;
1496
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001497 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1498 if (!ret)
1499 return 0;
1500 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1501 if (!ret)
1502 return 0;
1503 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1504 if (!ret)
1505 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001506
1507 switch (entry->type) {
1508 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001509 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1510 if (!ret)
1511 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001512 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1513 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001514 ret = trace_seq_printf(s, " <-");
1515 if (!ret)
1516 return 0;
1517 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1518 sym_flags);
1519 if (!ret)
1520 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001522 ret = trace_seq_printf(s, "\n");
1523 if (!ret)
1524 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525 break;
1526 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001527 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001528 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1529 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001530 T = entry->ctx.next_state < sizeof(state_to_char) ?
1531 state_to_char[entry->ctx.next_state] : 'X';
1532 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001533 entry->ctx.prev_pid,
1534 entry->ctx.prev_prio,
1535 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001536 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001537 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001538 entry->ctx.next_prio,
1539 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001540 if (!ret)
1541 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001543 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001544 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001545 entry->special.arg1,
1546 entry->special.arg2,
1547 entry->special.arg3);
1548 if (!ret)
1549 return 0;
1550 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001551 case TRACE_STACK:
1552 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1553 if (i) {
1554 ret = trace_seq_puts(s, " <= ");
1555 if (!ret)
1556 return 0;
1557 }
1558 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1559 sym_flags);
1560 if (!ret)
1561 return 0;
1562 }
1563 ret = trace_seq_puts(s, "\n");
1564 if (!ret)
1565 return 0;
1566 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001568 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569}
1570
Ingo Molnare309b412008-05-12 21:20:51 +02001571static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001572{
1573 struct trace_seq *s = &iter->seq;
1574 struct trace_entry *entry;
1575 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001576 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001577
1578 entry = iter->ent;
1579
1580 ret = trace_seq_printf(s, "%d %d %llu ",
1581 entry->pid, iter->cpu, entry->t);
1582 if (!ret)
1583 return 0;
1584
1585 switch (entry->type) {
1586 case TRACE_FN:
1587 ret = trace_seq_printf(s, "%x %x\n",
1588 entry->fn.ip, entry->fn.parent_ip);
1589 if (!ret)
1590 return 0;
1591 break;
1592 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001593 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001594 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1595 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001596 T = entry->ctx.next_state < sizeof(state_to_char) ?
1597 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001598 if (entry->type == TRACE_WAKE)
1599 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001600 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001601 entry->ctx.prev_pid,
1602 entry->ctx.prev_prio,
1603 S,
1604 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001605 entry->ctx.next_prio,
1606 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001607 if (!ret)
1608 return 0;
1609 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001610 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001611 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001612 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001613 entry->special.arg1,
1614 entry->special.arg2,
1615 entry->special.arg3);
1616 if (!ret)
1617 return 0;
1618 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001619 }
1620 return 1;
1621}
1622
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001623#define SEQ_PUT_FIELD_RET(s, x) \
1624do { \
1625 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1626 return 0; \
1627} while (0)
1628
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001629#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1630do { \
1631 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1632 return 0; \
1633} while (0)
1634
Ingo Molnare309b412008-05-12 21:20:51 +02001635static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001636{
1637 struct trace_seq *s = &iter->seq;
1638 unsigned char newline = '\n';
1639 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001640 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001641
1642 entry = iter->ent;
1643
1644 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1645 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1646 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1647
1648 switch (entry->type) {
1649 case TRACE_FN:
1650 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1651 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1652 break;
1653 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001654 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001655 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1656 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001657 T = entry->ctx.next_state < sizeof(state_to_char) ?
1658 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001659 if (entry->type == TRACE_WAKE)
1660 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001661 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1662 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1663 SEQ_PUT_HEX_FIELD_RET(s, S);
1664 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1665 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1666 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001667 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001668 break;
1669 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001670 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001671 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1672 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1673 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1674 break;
1675 }
1676 SEQ_PUT_FIELD_RET(s, newline);
1677
1678 return 1;
1679}
1680
Ingo Molnare309b412008-05-12 21:20:51 +02001681static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001682{
1683 struct trace_seq *s = &iter->seq;
1684 struct trace_entry *entry;
1685
1686 entry = iter->ent;
1687
1688 SEQ_PUT_FIELD_RET(s, entry->pid);
1689 SEQ_PUT_FIELD_RET(s, entry->cpu);
1690 SEQ_PUT_FIELD_RET(s, entry->t);
1691
1692 switch (entry->type) {
1693 case TRACE_FN:
1694 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1695 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1696 break;
1697 case TRACE_CTX:
1698 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1699 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1700 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1701 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1702 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001703 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001704 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001705 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001706 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001707 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1708 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1709 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1710 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001711 }
1712 return 1;
1713}
1714
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001715static int trace_empty(struct trace_iterator *iter)
1716{
1717 struct trace_array_cpu *data;
1718 int cpu;
1719
Steven Rostedtab464282008-05-12 21:21:00 +02001720 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001721 data = iter->tr->data[cpu];
1722
Steven Rostedtb3806b42008-05-12 21:20:46 +02001723 if (head_page(data) && data->trace_idx &&
1724 (data->trace_tail != data->trace_head ||
1725 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001726 return 0;
1727 }
1728 return 1;
1729}
1730
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001731static int print_trace_line(struct trace_iterator *iter)
1732{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001733 if (iter->trace && iter->trace->print_line)
1734 return iter->trace->print_line(iter);
1735
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001736 if (trace_flags & TRACE_ITER_BIN)
1737 return print_bin_fmt(iter);
1738
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001739 if (trace_flags & TRACE_ITER_HEX)
1740 return print_hex_fmt(iter);
1741
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001742 if (trace_flags & TRACE_ITER_RAW)
1743 return print_raw_fmt(iter);
1744
1745 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1746 return print_lat_fmt(iter, iter->idx, iter->cpu);
1747
1748 return print_trace_fmt(iter);
1749}
1750
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001751static int s_show(struct seq_file *m, void *v)
1752{
1753 struct trace_iterator *iter = v;
1754
1755 if (iter->ent == NULL) {
1756 if (iter->tr) {
1757 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1758 seq_puts(m, "#\n");
1759 }
1760 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1761 /* print nothing if the buffers are empty */
1762 if (trace_empty(iter))
1763 return 0;
1764 print_trace_header(m, iter);
1765 if (!(trace_flags & TRACE_ITER_VERBOSE))
1766 print_lat_help_header(m);
1767 } else {
1768 if (!(trace_flags & TRACE_ITER_VERBOSE))
1769 print_func_help_header(m);
1770 }
1771 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001772 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001773 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774 }
1775
1776 return 0;
1777}
1778
1779static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001780 .start = s_start,
1781 .next = s_next,
1782 .stop = s_stop,
1783 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001784};
1785
Ingo Molnare309b412008-05-12 21:20:51 +02001786static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001787__tracing_open(struct inode *inode, struct file *file, int *ret)
1788{
1789 struct trace_iterator *iter;
1790
Steven Rostedt60a11772008-05-12 21:20:44 +02001791 if (tracing_disabled) {
1792 *ret = -ENODEV;
1793 return NULL;
1794 }
1795
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001796 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1797 if (!iter) {
1798 *ret = -ENOMEM;
1799 goto out;
1800 }
1801
1802 mutex_lock(&trace_types_lock);
1803 if (current_trace && current_trace->print_max)
1804 iter->tr = &max_tr;
1805 else
1806 iter->tr = inode->i_private;
1807 iter->trace = current_trace;
1808 iter->pos = -1;
1809
1810 /* TODO stop tracer */
1811 *ret = seq_open(file, &tracer_seq_ops);
1812 if (!*ret) {
1813 struct seq_file *m = file->private_data;
1814 m->private = iter;
1815
1816 /* stop the trace while dumping */
1817 if (iter->tr->ctrl)
1818 tracer_enabled = 0;
1819
1820 if (iter->trace && iter->trace->open)
1821 iter->trace->open(iter);
1822 } else {
1823 kfree(iter);
1824 iter = NULL;
1825 }
1826 mutex_unlock(&trace_types_lock);
1827
1828 out:
1829 return iter;
1830}
1831
1832int tracing_open_generic(struct inode *inode, struct file *filp)
1833{
Steven Rostedt60a11772008-05-12 21:20:44 +02001834 if (tracing_disabled)
1835 return -ENODEV;
1836
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001837 filp->private_data = inode->i_private;
1838 return 0;
1839}
1840
1841int tracing_release(struct inode *inode, struct file *file)
1842{
1843 struct seq_file *m = (struct seq_file *)file->private_data;
1844 struct trace_iterator *iter = m->private;
1845
1846 mutex_lock(&trace_types_lock);
1847 if (iter->trace && iter->trace->close)
1848 iter->trace->close(iter);
1849
1850 /* reenable tracing if it was previously enabled */
1851 if (iter->tr->ctrl)
1852 tracer_enabled = 1;
1853 mutex_unlock(&trace_types_lock);
1854
1855 seq_release(inode, file);
1856 kfree(iter);
1857 return 0;
1858}
1859
1860static int tracing_open(struct inode *inode, struct file *file)
1861{
1862 int ret;
1863
1864 __tracing_open(inode, file, &ret);
1865
1866 return ret;
1867}
1868
1869static int tracing_lt_open(struct inode *inode, struct file *file)
1870{
1871 struct trace_iterator *iter;
1872 int ret;
1873
1874 iter = __tracing_open(inode, file, &ret);
1875
1876 if (!ret)
1877 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1878
1879 return ret;
1880}
1881
1882
Ingo Molnare309b412008-05-12 21:20:51 +02001883static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884t_next(struct seq_file *m, void *v, loff_t *pos)
1885{
1886 struct tracer *t = m->private;
1887
1888 (*pos)++;
1889
1890 if (t)
1891 t = t->next;
1892
1893 m->private = t;
1894
1895 return t;
1896}
1897
1898static void *t_start(struct seq_file *m, loff_t *pos)
1899{
1900 struct tracer *t = m->private;
1901 loff_t l = 0;
1902
1903 mutex_lock(&trace_types_lock);
1904 for (; t && l < *pos; t = t_next(m, t, &l))
1905 ;
1906
1907 return t;
1908}
1909
1910static void t_stop(struct seq_file *m, void *p)
1911{
1912 mutex_unlock(&trace_types_lock);
1913}
1914
1915static int t_show(struct seq_file *m, void *v)
1916{
1917 struct tracer *t = v;
1918
1919 if (!t)
1920 return 0;
1921
1922 seq_printf(m, "%s", t->name);
1923 if (t->next)
1924 seq_putc(m, ' ');
1925 else
1926 seq_putc(m, '\n');
1927
1928 return 0;
1929}
1930
1931static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001932 .start = t_start,
1933 .next = t_next,
1934 .stop = t_stop,
1935 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001936};
1937
1938static int show_traces_open(struct inode *inode, struct file *file)
1939{
1940 int ret;
1941
Steven Rostedt60a11772008-05-12 21:20:44 +02001942 if (tracing_disabled)
1943 return -ENODEV;
1944
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001945 ret = seq_open(file, &show_traces_seq_ops);
1946 if (!ret) {
1947 struct seq_file *m = file->private_data;
1948 m->private = trace_types;
1949 }
1950
1951 return ret;
1952}
1953
1954static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001955 .open = tracing_open,
1956 .read = seq_read,
1957 .llseek = seq_lseek,
1958 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001959};
1960
1961static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001962 .open = tracing_lt_open,
1963 .read = seq_read,
1964 .llseek = seq_lseek,
1965 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001966};
1967
1968static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001969 .open = show_traces_open,
1970 .read = seq_read,
1971 .release = seq_release,
1972};
1973
Ingo Molnar36dfe922008-05-12 21:20:52 +02001974/*
1975 * Only trace on a CPU if the bitmask is set:
1976 */
1977static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1978
1979/*
1980 * When tracing/tracing_cpu_mask is modified then this holds
1981 * the new bitmask we are about to install:
1982 */
1983static cpumask_t tracing_cpumask_new;
1984
1985/*
1986 * The tracer itself will not take this lock, but still we want
1987 * to provide a consistent cpumask to user-space:
1988 */
1989static DEFINE_MUTEX(tracing_cpumask_update_lock);
1990
1991/*
1992 * Temporary storage for the character representation of the
1993 * CPU bitmask (and one more byte for the newline):
1994 */
1995static char mask_str[NR_CPUS + 1];
1996
Ingo Molnarc7078de2008-05-12 21:20:52 +02001997static ssize_t
1998tracing_cpumask_read(struct file *filp, char __user *ubuf,
1999 size_t count, loff_t *ppos)
2000{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002001 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002002
2003 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002004
2005 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2006 if (count - len < 2) {
2007 count = -EINVAL;
2008 goto out_err;
2009 }
2010 len += sprintf(mask_str + len, "\n");
2011 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2012
2013out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002014 mutex_unlock(&tracing_cpumask_update_lock);
2015
2016 return count;
2017}
2018
2019static ssize_t
2020tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2021 size_t count, loff_t *ppos)
2022{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002023 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002024
2025 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002026 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2027 if (err)
2028 goto err_unlock;
2029
Steven Rostedt92205c22008-05-12 21:20:55 +02002030 raw_local_irq_disable();
2031 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002032 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002033 /*
2034 * Increase/decrease the disabled counter if we are
2035 * about to flip a bit in the cpumask:
2036 */
2037 if (cpu_isset(cpu, tracing_cpumask) &&
2038 !cpu_isset(cpu, tracing_cpumask_new)) {
2039 atomic_inc(&global_trace.data[cpu]->disabled);
2040 }
2041 if (!cpu_isset(cpu, tracing_cpumask) &&
2042 cpu_isset(cpu, tracing_cpumask_new)) {
2043 atomic_dec(&global_trace.data[cpu]->disabled);
2044 }
2045 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002046 __raw_spin_unlock(&ftrace_max_lock);
2047 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002048
2049 tracing_cpumask = tracing_cpumask_new;
2050
Ingo Molnarc7078de2008-05-12 21:20:52 +02002051 mutex_unlock(&tracing_cpumask_update_lock);
2052
Ingo Molnarc7078de2008-05-12 21:20:52 +02002053 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002054
2055err_unlock:
2056 mutex_unlock(&tracing_cpumask_update_lock);
2057
2058 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002059}
2060
2061static struct file_operations tracing_cpumask_fops = {
2062 .open = tracing_open_generic,
2063 .read = tracing_cpumask_read,
2064 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002065};
2066
2067static ssize_t
2068tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2069 size_t cnt, loff_t *ppos)
2070{
2071 char *buf;
2072 int r = 0;
2073 int len = 0;
2074 int i;
2075
2076 /* calulate max size */
2077 for (i = 0; trace_options[i]; i++) {
2078 len += strlen(trace_options[i]);
2079 len += 3; /* "no" and space */
2080 }
2081
2082 /* +2 for \n and \0 */
2083 buf = kmalloc(len + 2, GFP_KERNEL);
2084 if (!buf)
2085 return -ENOMEM;
2086
2087 for (i = 0; trace_options[i]; i++) {
2088 if (trace_flags & (1 << i))
2089 r += sprintf(buf + r, "%s ", trace_options[i]);
2090 else
2091 r += sprintf(buf + r, "no%s ", trace_options[i]);
2092 }
2093
2094 r += sprintf(buf + r, "\n");
2095 WARN_ON(r >= len + 2);
2096
Ingo Molnar36dfe922008-05-12 21:20:52 +02002097 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098
2099 kfree(buf);
2100
2101 return r;
2102}
2103
2104static ssize_t
2105tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2106 size_t cnt, loff_t *ppos)
2107{
2108 char buf[64];
2109 char *cmp = buf;
2110 int neg = 0;
2111 int i;
2112
Steven Rostedtcffae432008-05-12 21:21:00 +02002113 if (cnt >= sizeof(buf))
2114 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115
2116 if (copy_from_user(&buf, ubuf, cnt))
2117 return -EFAULT;
2118
2119 buf[cnt] = 0;
2120
2121 if (strncmp(buf, "no", 2) == 0) {
2122 neg = 1;
2123 cmp += 2;
2124 }
2125
2126 for (i = 0; trace_options[i]; i++) {
2127 int len = strlen(trace_options[i]);
2128
2129 if (strncmp(cmp, trace_options[i], len) == 0) {
2130 if (neg)
2131 trace_flags &= ~(1 << i);
2132 else
2133 trace_flags |= (1 << i);
2134 break;
2135 }
2136 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002137 /*
2138 * If no option could be set, return an error:
2139 */
2140 if (!trace_options[i])
2141 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002142
2143 filp->f_pos += cnt;
2144
2145 return cnt;
2146}
2147
2148static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002149 .open = tracing_open_generic,
2150 .read = tracing_iter_ctrl_read,
2151 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152};
2153
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002154static const char readme_msg[] =
2155 "tracing mini-HOWTO:\n\n"
2156 "# mkdir /debug\n"
2157 "# mount -t debugfs nodev /debug\n\n"
2158 "# cat /debug/tracing/available_tracers\n"
2159 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2160 "# cat /debug/tracing/current_tracer\n"
2161 "none\n"
2162 "# echo sched_switch > /debug/tracing/current_tracer\n"
2163 "# cat /debug/tracing/current_tracer\n"
2164 "sched_switch\n"
2165 "# cat /debug/tracing/iter_ctrl\n"
2166 "noprint-parent nosym-offset nosym-addr noverbose\n"
2167 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2168 "# echo 1 > /debug/tracing/tracing_enabled\n"
2169 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2170 "echo 0 > /debug/tracing/tracing_enabled\n"
2171;
2172
2173static ssize_t
2174tracing_readme_read(struct file *filp, char __user *ubuf,
2175 size_t cnt, loff_t *ppos)
2176{
2177 return simple_read_from_buffer(ubuf, cnt, ppos,
2178 readme_msg, strlen(readme_msg));
2179}
2180
2181static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002182 .open = tracing_open_generic,
2183 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002184};
2185
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002186static ssize_t
2187tracing_ctrl_read(struct file *filp, char __user *ubuf,
2188 size_t cnt, loff_t *ppos)
2189{
2190 struct trace_array *tr = filp->private_data;
2191 char buf[64];
2192 int r;
2193
2194 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002195 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196}
2197
2198static ssize_t
2199tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2200 size_t cnt, loff_t *ppos)
2201{
2202 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002203 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002204 long val;
2205 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002206
Steven Rostedtcffae432008-05-12 21:21:00 +02002207 if (cnt >= sizeof(buf))
2208 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002209
2210 if (copy_from_user(&buf, ubuf, cnt))
2211 return -EFAULT;
2212
2213 buf[cnt] = 0;
2214
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002215 ret = strict_strtoul(buf, 10, &val);
2216 if (ret < 0)
2217 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002218
2219 val = !!val;
2220
2221 mutex_lock(&trace_types_lock);
2222 if (tr->ctrl ^ val) {
2223 if (val)
2224 tracer_enabled = 1;
2225 else
2226 tracer_enabled = 0;
2227
2228 tr->ctrl = val;
2229
2230 if (current_trace && current_trace->ctrl_update)
2231 current_trace->ctrl_update(tr);
2232 }
2233 mutex_unlock(&trace_types_lock);
2234
2235 filp->f_pos += cnt;
2236
2237 return cnt;
2238}
2239
2240static ssize_t
2241tracing_set_trace_read(struct file *filp, char __user *ubuf,
2242 size_t cnt, loff_t *ppos)
2243{
2244 char buf[max_tracer_type_len+2];
2245 int r;
2246
2247 mutex_lock(&trace_types_lock);
2248 if (current_trace)
2249 r = sprintf(buf, "%s\n", current_trace->name);
2250 else
2251 r = sprintf(buf, "\n");
2252 mutex_unlock(&trace_types_lock);
2253
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002254 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255}
2256
2257static ssize_t
2258tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2259 size_t cnt, loff_t *ppos)
2260{
2261 struct trace_array *tr = &global_trace;
2262 struct tracer *t;
2263 char buf[max_tracer_type_len+1];
2264 int i;
2265
2266 if (cnt > max_tracer_type_len)
2267 cnt = max_tracer_type_len;
2268
2269 if (copy_from_user(&buf, ubuf, cnt))
2270 return -EFAULT;
2271
2272 buf[cnt] = 0;
2273
2274 /* strip ending whitespace. */
2275 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2276 buf[i] = 0;
2277
2278 mutex_lock(&trace_types_lock);
2279 for (t = trace_types; t; t = t->next) {
2280 if (strcmp(t->name, buf) == 0)
2281 break;
2282 }
2283 if (!t || t == current_trace)
2284 goto out;
2285
2286 if (current_trace && current_trace->reset)
2287 current_trace->reset(tr);
2288
2289 current_trace = t;
2290 if (t->init)
2291 t->init(tr);
2292
2293 out:
2294 mutex_unlock(&trace_types_lock);
2295
2296 filp->f_pos += cnt;
2297
2298 return cnt;
2299}
2300
2301static ssize_t
2302tracing_max_lat_read(struct file *filp, char __user *ubuf,
2303 size_t cnt, loff_t *ppos)
2304{
2305 unsigned long *ptr = filp->private_data;
2306 char buf[64];
2307 int r;
2308
Steven Rostedtcffae432008-05-12 21:21:00 +02002309 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002310 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002311 if (r > sizeof(buf))
2312 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002313 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002314}
2315
2316static ssize_t
2317tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2318 size_t cnt, loff_t *ppos)
2319{
2320 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002322 long val;
2323 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324
Steven Rostedtcffae432008-05-12 21:21:00 +02002325 if (cnt >= sizeof(buf))
2326 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002327
2328 if (copy_from_user(&buf, ubuf, cnt))
2329 return -EFAULT;
2330
2331 buf[cnt] = 0;
2332
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002333 ret = strict_strtoul(buf, 10, &val);
2334 if (ret < 0)
2335 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002336
2337 *ptr = val * 1000;
2338
2339 return cnt;
2340}
2341
Steven Rostedtb3806b42008-05-12 21:20:46 +02002342static atomic_t tracing_reader;
2343
2344static int tracing_open_pipe(struct inode *inode, struct file *filp)
2345{
2346 struct trace_iterator *iter;
2347
2348 if (tracing_disabled)
2349 return -ENODEV;
2350
2351 /* We only allow for reader of the pipe */
2352 if (atomic_inc_return(&tracing_reader) != 1) {
2353 atomic_dec(&tracing_reader);
2354 return -EBUSY;
2355 }
2356
2357 /* create a buffer to store the information to pass to userspace */
2358 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2359 if (!iter)
2360 return -ENOMEM;
2361
Steven Rostedt107bad82008-05-12 21:21:01 +02002362 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002363 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002364 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002365 filp->private_data = iter;
2366
Steven Rostedt107bad82008-05-12 21:21:01 +02002367 if (iter->trace->pipe_open)
2368 iter->trace->pipe_open(iter);
2369 mutex_unlock(&trace_types_lock);
2370
Steven Rostedtb3806b42008-05-12 21:20:46 +02002371 return 0;
2372}
2373
2374static int tracing_release_pipe(struct inode *inode, struct file *file)
2375{
2376 struct trace_iterator *iter = file->private_data;
2377
2378 kfree(iter);
2379 atomic_dec(&tracing_reader);
2380
2381 return 0;
2382}
2383
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002384static unsigned int
2385tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2386{
2387 struct trace_iterator *iter = filp->private_data;
2388
2389 if (trace_flags & TRACE_ITER_BLOCK) {
2390 /*
2391 * Always select as readable when in blocking mode
2392 */
2393 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002394 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002395 if (!trace_empty(iter))
2396 return POLLIN | POLLRDNORM;
2397 poll_wait(filp, &trace_wait, poll_table);
2398 if (!trace_empty(iter))
2399 return POLLIN | POLLRDNORM;
2400
2401 return 0;
2402 }
2403}
2404
Steven Rostedtb3806b42008-05-12 21:20:46 +02002405/*
2406 * Consumer reader.
2407 */
2408static ssize_t
2409tracing_read_pipe(struct file *filp, char __user *ubuf,
2410 size_t cnt, loff_t *ppos)
2411{
2412 struct trace_iterator *iter = filp->private_data;
2413 struct trace_array_cpu *data;
2414 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002415 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002416#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002417 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002418#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002419 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002420 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002421
2422 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002423 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2424 if (sret != -EBUSY)
2425 return sret;
2426 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002427
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002428 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002429
Steven Rostedt107bad82008-05-12 21:21:01 +02002430 mutex_lock(&trace_types_lock);
2431 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002432 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2433 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002434 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002435 }
2436
Steven Rostedtb3806b42008-05-12 21:20:46 +02002437 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002438
Steven Rostedt107bad82008-05-12 21:21:01 +02002439 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002440 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002441 goto out;
2442 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002443
Steven Rostedtb3806b42008-05-12 21:20:46 +02002444 /*
2445 * This is a make-shift waitqueue. The reason we don't use
2446 * an actual wait queue is because:
2447 * 1) we only ever have one waiter
2448 * 2) the tracing, traces all functions, we don't want
2449 * the overhead of calling wake_up and friends
2450 * (and tracing them too)
2451 * Anyway, this is really very primitive wakeup.
2452 */
2453 set_current_state(TASK_INTERRUPTIBLE);
2454 iter->tr->waiter = current;
2455
Steven Rostedt107bad82008-05-12 21:21:01 +02002456 mutex_unlock(&trace_types_lock);
2457
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002458 /* sleep for 100 msecs, and try again. */
2459 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002460
Steven Rostedt107bad82008-05-12 21:21:01 +02002461 mutex_lock(&trace_types_lock);
2462
Steven Rostedtb3806b42008-05-12 21:20:46 +02002463 iter->tr->waiter = NULL;
2464
Steven Rostedt107bad82008-05-12 21:21:01 +02002465 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002466 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002467 goto out;
2468 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002469
Steven Rostedt84527992008-05-12 21:20:58 +02002470 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002471 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002472
Steven Rostedtb3806b42008-05-12 21:20:46 +02002473 /*
2474 * We block until we read something and tracing is disabled.
2475 * We still block if tracing is disabled, but we have never
2476 * read anything. This allows a user to cat this file, and
2477 * then enable tracing. But after we have read something,
2478 * we give an EOF when tracing is again disabled.
2479 *
2480 * iter->pos will be 0 if we haven't read anything.
2481 */
2482 if (!tracer_enabled && iter->pos)
2483 break;
2484
2485 continue;
2486 }
2487
2488 /* stop when tracing is finished */
2489 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002490 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002491
2492 if (cnt >= PAGE_SIZE)
2493 cnt = PAGE_SIZE - 1;
2494
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002495 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002496 memset(&iter->seq, 0,
2497 sizeof(struct trace_iterator) -
2498 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002499 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002500
2501 /*
2502 * We need to stop all tracing on all CPUS to read the
2503 * the next buffer. This is a bit expensive, but is
2504 * not done often. We fill all what we can read,
2505 * and then release the locks again.
2506 */
2507
2508 cpus_clear(mask);
2509 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002510#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002511 ftrace_save = ftrace_enabled;
2512 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002513#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002514 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002515 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002516 data = iter->tr->data[cpu];
2517
2518 if (!head_page(data) || !data->trace_idx)
2519 continue;
2520
2521 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002522 cpu_set(cpu, mask);
2523 }
2524
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002525 for_each_cpu_mask(cpu, mask) {
2526 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002527 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002528
2529 if (data->overrun > iter->last_overrun[cpu])
2530 iter->overrun[cpu] +=
2531 data->overrun - iter->last_overrun[cpu];
2532 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002533 }
2534
Steven Rostedt088b1e422008-05-12 21:20:48 +02002535 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002536 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002537 int len = iter->seq.len;
2538
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002539 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002540 if (!ret) {
2541 /* don't print partial lines */
2542 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002543 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002544 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002545
2546 trace_consume(iter);
2547
2548 if (iter->seq.len >= cnt)
2549 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002550 }
2551
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002552 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002553 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002554 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002555 }
2556
2557 for_each_cpu_mask(cpu, mask) {
2558 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002559 atomic_dec(&data->disabled);
2560 }
Ingo Molnar25770462008-05-12 21:20:49 +02002561#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002562 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002563#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002564 local_irq_restore(flags);
2565
2566 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002567 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2568 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002569 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002570 if (sret == -EBUSY)
2571 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002572
Steven Rostedt107bad82008-05-12 21:21:01 +02002573out:
2574 mutex_unlock(&trace_types_lock);
2575
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002576 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002577}
2578
Steven Rostedta98a3c32008-05-12 21:20:59 +02002579static ssize_t
2580tracing_entries_read(struct file *filp, char __user *ubuf,
2581 size_t cnt, loff_t *ppos)
2582{
2583 struct trace_array *tr = filp->private_data;
2584 char buf[64];
2585 int r;
2586
2587 r = sprintf(buf, "%lu\n", tr->entries);
2588 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2589}
2590
2591static ssize_t
2592tracing_entries_write(struct file *filp, const char __user *ubuf,
2593 size_t cnt, loff_t *ppos)
2594{
2595 unsigned long val;
2596 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002597 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002598
Steven Rostedtcffae432008-05-12 21:21:00 +02002599 if (cnt >= sizeof(buf))
2600 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002601
2602 if (copy_from_user(&buf, ubuf, cnt))
2603 return -EFAULT;
2604
2605 buf[cnt] = 0;
2606
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002607 ret = strict_strtoul(buf, 10, &val);
2608 if (ret < 0)
2609 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002610
2611 /* must have at least 1 entry */
2612 if (!val)
2613 return -EINVAL;
2614
2615 mutex_lock(&trace_types_lock);
2616
2617 if (current_trace != &no_tracer) {
2618 cnt = -EBUSY;
2619 pr_info("ftrace: set current_tracer to none"
2620 " before modifying buffer size\n");
2621 goto out;
2622 }
2623
2624 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002625 long pages_requested;
2626 unsigned long freeable_pages;
2627
2628 /* make sure we have enough memory before mapping */
2629 pages_requested =
2630 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2631
2632 /* account for each buffer (and max_tr) */
2633 pages_requested *= tracing_nr_buffers * 2;
2634
2635 /* Check for overflow */
2636 if (pages_requested < 0) {
2637 cnt = -ENOMEM;
2638 goto out;
2639 }
2640
2641 freeable_pages = determine_dirtyable_memory();
2642
2643 /* we only allow to request 1/4 of useable memory */
2644 if (pages_requested >
2645 ((freeable_pages + tracing_pages_allocated) / 4)) {
2646 cnt = -ENOMEM;
2647 goto out;
2648 }
2649
Steven Rostedta98a3c32008-05-12 21:20:59 +02002650 while (global_trace.entries < val) {
2651 if (trace_alloc_page()) {
2652 cnt = -ENOMEM;
2653 goto out;
2654 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002655 /* double check that we don't go over the known pages */
2656 if (tracing_pages_allocated > pages_requested)
2657 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002658 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002659
Steven Rostedta98a3c32008-05-12 21:20:59 +02002660 } else {
2661 /* include the number of entries in val (inc of page entries) */
2662 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2663 trace_free_page();
2664 }
2665
Steven Rostedt19384c02008-05-22 00:22:16 -04002666 /* check integrity */
2667 for_each_tracing_cpu(i)
2668 check_pages(global_trace.data[i]);
2669
Steven Rostedta98a3c32008-05-12 21:20:59 +02002670 filp->f_pos += cnt;
2671
Steven Rostedt19384c02008-05-22 00:22:16 -04002672 /* If check pages failed, return ENOMEM */
2673 if (tracing_disabled)
2674 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002675 out:
2676 max_tr.entries = global_trace.entries;
2677 mutex_unlock(&trace_types_lock);
2678
2679 return cnt;
2680}
2681
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002682static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002683 .open = tracing_open_generic,
2684 .read = tracing_max_lat_read,
2685 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002686};
2687
2688static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002689 .open = tracing_open_generic,
2690 .read = tracing_ctrl_read,
2691 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002692};
2693
2694static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002695 .open = tracing_open_generic,
2696 .read = tracing_set_trace_read,
2697 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002698};
2699
Steven Rostedtb3806b42008-05-12 21:20:46 +02002700static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002701 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002702 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002703 .read = tracing_read_pipe,
2704 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002705};
2706
Steven Rostedta98a3c32008-05-12 21:20:59 +02002707static struct file_operations tracing_entries_fops = {
2708 .open = tracing_open_generic,
2709 .read = tracing_entries_read,
2710 .write = tracing_entries_write,
2711};
2712
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002713#ifdef CONFIG_DYNAMIC_FTRACE
2714
2715static ssize_t
2716tracing_read_long(struct file *filp, char __user *ubuf,
2717 size_t cnt, loff_t *ppos)
2718{
2719 unsigned long *p = filp->private_data;
2720 char buf[64];
2721 int r;
2722
2723 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002724
2725 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002726}
2727
2728static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002729 .open = tracing_open_generic,
2730 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002731};
2732#endif
2733
2734static struct dentry *d_tracer;
2735
2736struct dentry *tracing_init_dentry(void)
2737{
2738 static int once;
2739
2740 if (d_tracer)
2741 return d_tracer;
2742
2743 d_tracer = debugfs_create_dir("tracing", NULL);
2744
2745 if (!d_tracer && !once) {
2746 once = 1;
2747 pr_warning("Could not create debugfs directory 'tracing'\n");
2748 return NULL;
2749 }
2750
2751 return d_tracer;
2752}
2753
Steven Rostedt60a11772008-05-12 21:20:44 +02002754#ifdef CONFIG_FTRACE_SELFTEST
2755/* Let selftest have access to static functions in this file */
2756#include "trace_selftest.c"
2757#endif
2758
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002759static __init void tracer_init_debugfs(void)
2760{
2761 struct dentry *d_tracer;
2762 struct dentry *entry;
2763
2764 d_tracer = tracing_init_dentry();
2765
2766 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2767 &global_trace, &tracing_ctrl_fops);
2768 if (!entry)
2769 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2770
2771 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2772 NULL, &tracing_iter_fops);
2773 if (!entry)
2774 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2775
Ingo Molnarc7078de2008-05-12 21:20:52 +02002776 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2777 NULL, &tracing_cpumask_fops);
2778 if (!entry)
2779 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2780
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002781 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2782 &global_trace, &tracing_lt_fops);
2783 if (!entry)
2784 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2785
2786 entry = debugfs_create_file("trace", 0444, d_tracer,
2787 &global_trace, &tracing_fops);
2788 if (!entry)
2789 pr_warning("Could not create debugfs 'trace' entry\n");
2790
2791 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2792 &global_trace, &show_traces_fops);
2793 if (!entry)
2794 pr_warning("Could not create debugfs 'trace' entry\n");
2795
2796 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2797 &global_trace, &set_tracer_fops);
2798 if (!entry)
2799 pr_warning("Could not create debugfs 'trace' entry\n");
2800
2801 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2802 &tracing_max_latency,
2803 &tracing_max_lat_fops);
2804 if (!entry)
2805 pr_warning("Could not create debugfs "
2806 "'tracing_max_latency' entry\n");
2807
2808 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2809 &tracing_thresh, &tracing_max_lat_fops);
2810 if (!entry)
2811 pr_warning("Could not create debugfs "
2812 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002813 entry = debugfs_create_file("README", 0644, d_tracer,
2814 NULL, &tracing_readme_fops);
2815 if (!entry)
2816 pr_warning("Could not create debugfs 'README' entry\n");
2817
Steven Rostedtb3806b42008-05-12 21:20:46 +02002818 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2819 NULL, &tracing_pipe_fops);
2820 if (!entry)
2821 pr_warning("Could not create debugfs "
2822 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002823
Steven Rostedta98a3c32008-05-12 21:20:59 +02002824 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2825 &global_trace, &tracing_entries_fops);
2826 if (!entry)
2827 pr_warning("Could not create debugfs "
2828 "'tracing_threash' entry\n");
2829
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002830#ifdef CONFIG_DYNAMIC_FTRACE
2831 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2832 &ftrace_update_tot_cnt,
2833 &tracing_read_long_fops);
2834 if (!entry)
2835 pr_warning("Could not create debugfs "
2836 "'dyn_ftrace_total_info' entry\n");
2837#endif
2838}
2839
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002840static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002841{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002842 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002843 struct page *page, *tmp;
2844 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002845 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002846 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002847 int i;
2848
2849 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002850 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002851 array = (void *)__get_free_page(GFP_KERNEL);
2852 if (array == NULL) {
2853 printk(KERN_ERR "tracer: failed to allocate page"
2854 "for trace buffer!\n");
2855 goto free_pages;
2856 }
2857
Steven Rostedt3eefae92008-05-12 21:21:04 +02002858 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002859 page = virt_to_page(array);
2860 list_add(&page->lru, &pages);
2861
2862/* Only allocate if we are actually using the max trace */
2863#ifdef CONFIG_TRACER_MAX_TRACE
2864 array = (void *)__get_free_page(GFP_KERNEL);
2865 if (array == NULL) {
2866 printk(KERN_ERR "tracer: failed to allocate page"
2867 "for trace buffer!\n");
2868 goto free_pages;
2869 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002870 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002871 page = virt_to_page(array);
2872 list_add(&page->lru, &pages);
2873#endif
2874 }
2875
2876 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002877 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002878 data = global_trace.data[i];
2879 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002880 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002881 list_add_tail(&page->lru, &data->trace_pages);
2882 ClearPageLRU(page);
2883
2884#ifdef CONFIG_TRACER_MAX_TRACE
2885 data = max_tr.data[i];
2886 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002887 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002888 list_add_tail(&page->lru, &data->trace_pages);
2889 SetPageLRU(page);
2890#endif
2891 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002892 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002893 global_trace.entries += ENTRIES_PER_PAGE;
2894
2895 return 0;
2896
2897 free_pages:
2898 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002899 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002900 __free_page(page);
2901 }
2902 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002903}
2904
Steven Rostedta98a3c32008-05-12 21:20:59 +02002905static int trace_free_page(void)
2906{
2907 struct trace_array_cpu *data;
2908 struct page *page;
2909 struct list_head *p;
2910 int i;
2911 int ret = 0;
2912
2913 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002914 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002915 data = global_trace.data[i];
2916 p = data->trace_pages.next;
2917 if (p == &data->trace_pages) {
2918 /* should never happen */
2919 WARN_ON(1);
2920 tracing_disabled = 1;
2921 ret = -1;
2922 break;
2923 }
2924 page = list_entry(p, struct page, lru);
2925 ClearPageLRU(page);
2926 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02002927 tracing_pages_allocated--;
2928 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002929 __free_page(page);
2930
2931 tracing_reset(data);
2932
2933#ifdef CONFIG_TRACER_MAX_TRACE
2934 data = max_tr.data[i];
2935 p = data->trace_pages.next;
2936 if (p == &data->trace_pages) {
2937 /* should never happen */
2938 WARN_ON(1);
2939 tracing_disabled = 1;
2940 ret = -1;
2941 break;
2942 }
2943 page = list_entry(p, struct page, lru);
2944 ClearPageLRU(page);
2945 list_del(&page->lru);
2946 __free_page(page);
2947
2948 tracing_reset(data);
2949#endif
2950 }
2951 global_trace.entries -= ENTRIES_PER_PAGE;
2952
2953 return ret;
2954}
2955
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002956__init static int tracer_alloc_buffers(void)
2957{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002958 struct trace_array_cpu *data;
2959 void *array;
2960 struct page *page;
2961 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002962 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002963 int i;
2964
Steven Rostedtab464282008-05-12 21:21:00 +02002965 /* TODO: make the number of buffers hot pluggable with CPUS */
2966 tracing_nr_buffers = num_possible_cpus();
2967 tracing_buffer_mask = cpu_possible_map;
2968
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002969 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002970 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002971 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002972 max_tr.data[i] = &per_cpu(max_data, i);
2973
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 /* set the array to the list */
2982 INIT_LIST_HEAD(&data->trace_pages);
2983 page = virt_to_page(array);
2984 list_add(&page->lru, &data->trace_pages);
2985 /* use the LRU flag to differentiate the two buffers */
2986 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002987
Steven Rostedta98a3c32008-05-12 21:20:59 +02002988 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2989 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2990
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002991/* Only allocate if we are actually using the max trace */
2992#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002993 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002995 printk(KERN_ERR "tracer: failed to allocate page"
2996 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002997 goto free_buffers;
2998 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002999
3000 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3001 page = virt_to_page(array);
3002 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3003 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003004#endif
3005 }
3006
3007 /*
3008 * Since we allocate by orders of pages, we may be able to
3009 * round up a bit.
3010 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003011 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003012 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003013
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003014 while (global_trace.entries < trace_nr_entries) {
3015 if (trace_alloc_page())
3016 break;
3017 pages++;
3018 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003019 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003020
3021 pr_info("tracer: %d pages allocated for %ld",
3022 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003023 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3024 pr_info(" actual entries %ld\n", global_trace.entries);
3025
3026 tracer_init_debugfs();
3027
3028 trace_init_cmdlines();
3029
3030 register_tracer(&no_tracer);
3031 current_trace = &no_tracer;
3032
Steven Rostedt60a11772008-05-12 21:20:44 +02003033 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003034 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003035 tracing_disabled = 0;
3036
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003037 return 0;
3038
3039 free_buffers:
3040 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003041 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003042 struct trace_array_cpu *data = global_trace.data[i];
3043
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003044 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003045 list_for_each_entry_safe(page, tmp,
3046 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003047 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003048 __free_page(page);
3049 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003050 }
3051
3052#ifdef CONFIG_TRACER_MAX_TRACE
3053 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003054 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003055 list_for_each_entry_safe(page, tmp,
3056 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003057 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003058 __free_page(page);
3059 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003060 }
3061#endif
3062 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003063 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003064}
Steven Rostedt60a11772008-05-12 21:20:44 +02003065fs_initcall(tracer_alloc_buffers);