blob: 1281969103b80f1907c69733233ef5221e115425 [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>
30
Ingo Molnar86387f72008-05-12 21:20:51 +020031#include <linux/stacktrace.h>
32
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include "trace.h"
34
35unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36unsigned long __read_mostly tracing_thresh;
37
Steven Rostedtab464282008-05-12 21:21:00 +020038static unsigned long __read_mostly tracing_nr_buffers;
39static cpumask_t __read_mostly tracing_buffer_mask;
40
41#define for_each_tracing_cpu(cpu) \
42 for_each_cpu_mask(cpu, tracing_buffer_mask)
43
Steven Rostedta98a3c32008-05-12 21:20:59 +020044/* dummy trace to disable tracing */
Steven Rostedtcffae432008-05-12 21:21:00 +020045static struct tracer no_tracer __read_mostly = {
Steven Rostedta98a3c32008-05-12 21:20:59 +020046 .name = "none",
47};
48
49static int trace_alloc_page(void);
50static int trace_free_page(void);
51
Steven Rostedt60a11772008-05-12 21:20:44 +020052static int tracing_disabled = 1;
53
Thomas Gleixner72829bc2008-05-23 21:37:28 +020054long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020055ns2usecs(cycle_t nsec)
56{
57 nsec += 500;
58 do_div(nsec, 1000);
59 return nsec;
60}
61
Ingo Molnare309b412008-05-12 21:20:51 +020062cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020063{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020064 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020065}
66
Steven Rostedt4fcdae82008-05-12 21:21:00 +020067/*
68 * The global_trace is the descriptor that holds the tracing
69 * buffers for the live tracing. For each CPU, it contains
70 * a link list of pages that will store trace entries. The
71 * page descriptor of the pages in the memory is used to hold
72 * the link list by linking the lru item in the page descriptor
73 * to each of the pages in the buffer per CPU.
74 *
75 * For each active CPU there is a data field that holds the
76 * pages for the buffer for that CPU. Each CPU has the same number
77 * of pages allocated for its buffer.
78 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020079static struct trace_array global_trace;
80
81static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
82
Steven Rostedt4fcdae82008-05-12 21:21:00 +020083/*
84 * The max_tr is used to snapshot the global_trace when a maximum
85 * latency is reached. Some tracers will use this to store a maximum
86 * trace while it continues examining live traces.
87 *
88 * The buffers for the max_tr are set up the same as the global_trace.
89 * When a snapshot is taken, the link list of the max_tr is swapped
90 * with the link list of the global_trace and the buffers are reset for
91 * the global_trace so the tracing can continue.
92 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020093static struct trace_array max_tr;
94
95static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
96
Steven Rostedt4fcdae82008-05-12 21:21:00 +020097/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +020098static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +020099
100/*
101 * trace_nr_entries is the number of entries that is allocated
102 * for a buffer. Note, the number of entries is always rounded
103 * to ENTRIES_PER_PAGE.
104 */
Ingo Molnar57422792008-05-12 21:20:51 +0200105static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200106
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200107/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200109
110/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200111static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200112
113/*
114 * max_tracer_type_len is used to simplify the allocating of
115 * buffers to read userspace tracer names. We keep track of
116 * the longest tracer name registered.
117 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200118static int max_tracer_type_len;
119
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200120/*
121 * trace_types_lock is used to protect the trace_types list.
122 * This lock is also used to keep user access serialized.
123 * Accesses from userspace will grab this lock while userspace
124 * activities happen inside the kernel.
125 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200126static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200127
128/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200129static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
130
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200131/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200132unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
133
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200134/**
135 * trace_wake_up - wake up tasks waiting for trace input
136 *
137 * Simply wakes up any task that is blocked on the trace_wait
138 * queue. These is used with trace_poll for tasks polling the trace.
139 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200140void trace_wake_up(void)
141{
Ingo Molnar017730c2008-05-12 21:20:52 +0200142 /*
143 * The runqueue_is_locked() can fail, but this is the best we
144 * have for now:
145 */
146 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200147 wake_up(&trace_wait);
148}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200149
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200150#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
151
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200152static int __init set_nr_entries(char *str)
153{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200154 unsigned long nr_entries;
155 int ret;
156
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200157 if (!str)
158 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200159 ret = strict_strtoul(str, 0, &nr_entries);
160 /* nr_entries can not be zero */
161 if (ret < 0 || nr_entries == 0)
162 return 0;
163 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200164 return 1;
165}
166__setup("trace_entries=", set_nr_entries);
167
Steven Rostedt57f50be2008-05-12 21:20:44 +0200168unsigned long nsecs_to_usecs(unsigned long nsecs)
169{
170 return nsecs / 1000;
171}
172
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200173/*
174 * trace_flag_type is an enumeration that holds different
175 * states when a trace occurs. These are:
176 * IRQS_OFF - interrupts were disabled
177 * NEED_RESCED - reschedule is requested
178 * HARDIRQ - inside an interrupt handler
179 * SOFTIRQ - inside a softirq handler
180 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200181enum trace_flag_type {
182 TRACE_FLAG_IRQS_OFF = 0x01,
183 TRACE_FLAG_NEED_RESCHED = 0x02,
184 TRACE_FLAG_HARDIRQ = 0x04,
185 TRACE_FLAG_SOFTIRQ = 0x08,
186};
187
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200188/*
189 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
190 * control the output of kernel symbols.
191 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200192#define TRACE_ITER_SYM_MASK \
193 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
194
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200195/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200196static const char *trace_options[] = {
197 "print-parent",
198 "sym-offset",
199 "sym-addr",
200 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200201 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200202 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200203 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200204 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200205 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200206 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200207 NULL
208};
209
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200210/*
211 * ftrace_max_lock is used to protect the swapping of buffers
212 * when taking a max snapshot. The buffers themselves are
213 * protected by per_cpu spinlocks. But the action of the swap
214 * needs its own lock.
215 *
216 * This is defined as a raw_spinlock_t in order to help
217 * with performance when lockdep debugging is enabled.
218 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200219static raw_spinlock_t ftrace_max_lock =
220 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200221
222/*
223 * Copy the new maximum trace into the separate maximum-trace
224 * structure. (this way the maximum trace is permanently saved,
225 * for later retrieval via /debugfs/tracing/latency_trace)
226 */
Ingo Molnare309b412008-05-12 21:20:51 +0200227static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200228__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
229{
230 struct trace_array_cpu *data = tr->data[cpu];
231
232 max_tr.cpu = cpu;
233 max_tr.time_start = data->preempt_timestamp;
234
235 data = max_tr.data[cpu];
236 data->saved_latency = tracing_max_latency;
237
238 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
239 data->pid = tsk->pid;
240 data->uid = tsk->uid;
241 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
242 data->policy = tsk->policy;
243 data->rt_priority = tsk->rt_priority;
244
245 /* record this tasks comm */
246 tracing_record_cmdline(current);
247}
248
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200249/**
250 * check_pages - integrity check of trace buffers
251 *
252 * As a safty measure we check to make sure the data pages have not
253 * been corrupted. TODO: configure to disable this because it adds
254 * a bit of overhead.
255 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200256void check_pages(struct trace_array_cpu *data)
257{
258 struct page *page, *tmp;
259
260 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
261 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
262
263 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
264 BUG_ON(page->lru.next->prev != &page->lru);
265 BUG_ON(page->lru.prev->next != &page->lru);
266 }
267}
268
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200269/**
270 * head_page - page address of the first page in per_cpu buffer.
271 *
272 * head_page returns the page address of the first page in
273 * a per_cpu buffer. This also preforms various consistency
274 * checks to make sure the buffer has not been corrupted.
275 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200276void *head_page(struct trace_array_cpu *data)
277{
278 struct page *page;
279
280 check_pages(data);
281 if (list_empty(&data->trace_pages))
282 return NULL;
283
284 page = list_entry(data->trace_pages.next, struct page, lru);
285 BUG_ON(&page->lru == &data->trace_pages);
286
287 return page_address(page);
288}
289
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200290/**
291 * trace_seq_printf - sequence printing of trace information
292 * @s: trace sequence descriptor
293 * @fmt: printf format string
294 *
295 * The tracer may use either sequence operations or its own
296 * copy to user routines. To simplify formating of a trace
297 * trace_seq_printf is used to store strings into a special
298 * buffer (@s). Then the output may be either used by
299 * the sequencer or pulled into another buffer.
300 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200301int
Steven Rostedt214023c2008-05-12 21:20:46 +0200302trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
303{
304 int len = (PAGE_SIZE - 1) - s->len;
305 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200306 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200307
308 if (!len)
309 return 0;
310
311 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200312 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200313 va_end(ap);
314
Steven Rostedtb3806b42008-05-12 21:20:46 +0200315 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200316 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200317 return 0;
318
319 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200320
321 return len;
322}
323
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200324/**
325 * trace_seq_puts - trace sequence printing of simple string
326 * @s: trace sequence descriptor
327 * @str: simple string to record
328 *
329 * The tracer may use either the sequence operations or its own
330 * copy to user routines. This function records a simple string
331 * into a special buffer (@s) for later retrieval by a sequencer
332 * or other mechanism.
333 */
Ingo Molnare309b412008-05-12 21:20:51 +0200334static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200335trace_seq_puts(struct trace_seq *s, const char *str)
336{
337 int len = strlen(str);
338
339 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200340 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200341
342 memcpy(s->buffer + s->len, str, len);
343 s->len += len;
344
345 return len;
346}
347
Ingo Molnare309b412008-05-12 21:20:51 +0200348static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200349trace_seq_putc(struct trace_seq *s, unsigned char c)
350{
351 if (s->len >= (PAGE_SIZE - 1))
352 return 0;
353
354 s->buffer[s->len++] = c;
355
356 return 1;
357}
358
Ingo Molnare309b412008-05-12 21:20:51 +0200359static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200360trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
361{
362 if (len > ((PAGE_SIZE - 1) - s->len))
363 return 0;
364
365 memcpy(s->buffer + s->len, mem, len);
366 s->len += len;
367
368 return len;
369}
370
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200371#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200372static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200373
Ingo Molnare309b412008-05-12 21:20:51 +0200374static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200375trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
376{
377 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200378 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200379 unsigned char byte;
380 int i, j;
381
382 BUG_ON(len >= HEX_CHARS);
383
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200384#ifdef __BIG_ENDIAN
385 for (i = 0, j = 0; i < len; i++) {
386#else
387 for (i = len-1, j = 0; i >= 0; i--) {
388#endif
389 byte = data[i];
390
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200391 hex[j++] = hex2asc[byte & 0x0f];
392 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200393 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200394 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200395
396 return trace_seq_putmem(s, hex, j);
397}
398
Ingo Molnare309b412008-05-12 21:20:51 +0200399static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200400trace_seq_reset(struct trace_seq *s)
401{
402 s->len = 0;
403}
404
Ingo Molnare309b412008-05-12 21:20:51 +0200405static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200406trace_print_seq(struct seq_file *m, struct trace_seq *s)
407{
408 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
409
410 s->buffer[len] = 0;
411 seq_puts(m, s->buffer);
412
413 trace_seq_reset(s);
414}
415
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200416/*
417 * flip the trace buffers between two trace descriptors.
418 * This usually is the buffers between the global_trace and
419 * the max_tr to record a snapshot of a current trace.
420 *
421 * The ftrace_max_lock must be held.
422 */
Ingo Molnare309b412008-05-12 21:20:51 +0200423static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200424flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
425{
426 struct list_head flip_pages;
427
428 INIT_LIST_HEAD(&flip_pages);
429
Steven Rostedt93a588f2008-05-12 21:20:45 +0200430 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200431 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200432 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200433
434 check_pages(tr1);
435 check_pages(tr2);
436 list_splice_init(&tr1->trace_pages, &flip_pages);
437 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
438 list_splice_init(&flip_pages, &tr2->trace_pages);
439 BUG_ON(!list_empty(&flip_pages));
440 check_pages(tr1);
441 check_pages(tr2);
442}
443
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200444/**
445 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
446 * @tr: tracer
447 * @tsk: the task with the latency
448 * @cpu: The cpu that initiated the trace.
449 *
450 * Flip the buffers between the @tr and the max_tr and record information
451 * about which task was the cause of this latency.
452 */
Ingo Molnare309b412008-05-12 21:20:51 +0200453void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200454update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
455{
456 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200457 int i;
458
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200459 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200460 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200461 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200462 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200463 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200464 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200465 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200466 }
467
468 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200469 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470}
471
472/**
473 * update_max_tr_single - only copy one trace over, and reset the rest
474 * @tr - tracer
475 * @tsk - task with the latency
476 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200477 *
478 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200479 */
Ingo Molnare309b412008-05-12 21:20:51 +0200480void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
482{
483 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200484 int i;
485
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200486 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200487 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200488 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200489 tracing_reset(max_tr.data[i]);
490
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200491 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200492 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200493
494 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200495 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200496}
497
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200498/**
499 * register_tracer - register a tracer with the ftrace system.
500 * @type - the plugin for the tracer
501 *
502 * Register a new plugin tracer.
503 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504int register_tracer(struct tracer *type)
505{
506 struct tracer *t;
507 int len;
508 int ret = 0;
509
510 if (!type->name) {
511 pr_info("Tracer must have a name\n");
512 return -1;
513 }
514
515 mutex_lock(&trace_types_lock);
516 for (t = trace_types; t; t = t->next) {
517 if (strcmp(type->name, t->name) == 0) {
518 /* already found */
519 pr_info("Trace %s already registered\n",
520 type->name);
521 ret = -1;
522 goto out;
523 }
524 }
525
Steven Rostedt60a11772008-05-12 21:20:44 +0200526#ifdef CONFIG_FTRACE_STARTUP_TEST
527 if (type->selftest) {
528 struct tracer *saved_tracer = current_trace;
529 struct trace_array_cpu *data;
530 struct trace_array *tr = &global_trace;
531 int saved_ctrl = tr->ctrl;
532 int i;
533 /*
534 * Run a selftest on this tracer.
535 * Here we reset the trace buffer, and set the current
536 * tracer to be this tracer. The tracer can then run some
537 * internal tracing to verify that everything is in order.
538 * If we fail, we do not register this tracer.
539 */
Steven Rostedtab464282008-05-12 21:21:00 +0200540 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200541 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200542 if (!head_page(data))
543 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200544 tracing_reset(data);
545 }
546 current_trace = type;
547 tr->ctrl = 0;
548 /* the test is responsible for initializing and enabling */
549 pr_info("Testing tracer %s: ", type->name);
550 ret = type->selftest(type, tr);
551 /* the test is responsible for resetting too */
552 current_trace = saved_tracer;
553 tr->ctrl = saved_ctrl;
554 if (ret) {
555 printk(KERN_CONT "FAILED!\n");
556 goto out;
557 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200558 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200559 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200560 data = tr->data[i];
561 if (!head_page(data))
562 continue;
563 tracing_reset(data);
564 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200565 printk(KERN_CONT "PASSED\n");
566 }
567#endif
568
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200569 type->next = trace_types;
570 trace_types = type;
571 len = strlen(type->name);
572 if (len > max_tracer_type_len)
573 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200574
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200575 out:
576 mutex_unlock(&trace_types_lock);
577
578 return ret;
579}
580
581void unregister_tracer(struct tracer *type)
582{
583 struct tracer **t;
584 int len;
585
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
590 }
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
593
594 found:
595 *t = (*t)->next;
596 if (strlen(type->name) != max_tracer_type_len)
597 goto out;
598
599 max_tracer_type_len = 0;
600 for (t = &trace_types; *t; t = &(*t)->next) {
601 len = strlen((*t)->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
604 }
605 out:
606 mutex_unlock(&trace_types_lock);
607}
608
Ingo Molnare309b412008-05-12 21:20:51 +0200609void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610{
611 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200612 data->trace_head = data->trace_tail = head_page(data);
613 data->trace_head_idx = 0;
614 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200615}
616
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200617#define SAVED_CMDLINES 128
618static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
619static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
620static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
621static int cmdline_idx;
622static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200623
624/* trace in all context switches */
625atomic_t trace_record_cmdline_enabled __read_mostly;
626
627/* temporary disable recording */
628atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200629
630static void trace_init_cmdlines(void)
631{
632 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
633 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
634 cmdline_idx = 0;
635}
636
Ingo Molnare309b412008-05-12 21:20:51 +0200637void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200638
Ingo Molnare309b412008-05-12 21:20:51 +0200639static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640{
641 unsigned map;
642 unsigned idx;
643
644 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
645 return;
646
647 /*
648 * It's not the end of the world if we don't get
649 * the lock, but we also don't want to spin
650 * nor do we want to disable interrupts,
651 * so if we miss here, then better luck next time.
652 */
653 if (!spin_trylock(&trace_cmdline_lock))
654 return;
655
656 idx = map_pid_to_cmdline[tsk->pid];
657 if (idx >= SAVED_CMDLINES) {
658 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
659
660 map = map_cmdline_to_pid[idx];
661 if (map <= PID_MAX_DEFAULT)
662 map_pid_to_cmdline[map] = (unsigned)-1;
663
664 map_pid_to_cmdline[tsk->pid] = idx;
665
666 cmdline_idx = idx;
667 }
668
669 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
670
671 spin_unlock(&trace_cmdline_lock);
672}
673
Ingo Molnare309b412008-05-12 21:20:51 +0200674static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200675{
676 char *cmdline = "<...>";
677 unsigned map;
678
679 if (!pid)
680 return "<idle>";
681
682 if (pid > PID_MAX_DEFAULT)
683 goto out;
684
685 map = map_pid_to_cmdline[pid];
686 if (map >= SAVED_CMDLINES)
687 goto out;
688
689 cmdline = saved_cmdlines[map];
690
691 out:
692 return cmdline;
693}
694
Ingo Molnare309b412008-05-12 21:20:51 +0200695void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200696{
697 if (atomic_read(&trace_record_cmdline_disabled))
698 return;
699
700 trace_save_cmdline(tsk);
701}
702
Ingo Molnare309b412008-05-12 21:20:51 +0200703static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200704trace_next_list(struct trace_array_cpu *data, struct list_head *next)
705{
706 /*
707 * Roundrobin - but skip the head (which is not a real page):
708 */
709 next = next->next;
710 if (unlikely(next == &data->trace_pages))
711 next = next->next;
712 BUG_ON(next == &data->trace_pages);
713
714 return next;
715}
716
Ingo Molnare309b412008-05-12 21:20:51 +0200717static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200718trace_next_page(struct trace_array_cpu *data, void *addr)
719{
720 struct list_head *next;
721 struct page *page;
722
723 page = virt_to_page(addr);
724
725 next = trace_next_list(data, &page->lru);
726 page = list_entry(next, struct page, lru);
727
728 return page_address(page);
729}
730
Ingo Molnare309b412008-05-12 21:20:51 +0200731static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200732tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200733{
734 unsigned long idx, idx_next;
735 struct trace_entry *entry;
736
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200737 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200738 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739 idx_next = idx + 1;
740
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200741 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
742
Steven Rostedt93a588f2008-05-12 21:20:45 +0200743 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200744
745 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200746 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200747 idx_next = 0;
748 }
749
Steven Rostedt93a588f2008-05-12 21:20:45 +0200750 if (data->trace_head == data->trace_tail &&
751 idx_next == data->trace_tail_idx) {
752 /* overrun */
753 data->trace_tail_idx++;
754 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
755 data->trace_tail =
756 trace_next_page(data, data->trace_tail);
757 data->trace_tail_idx = 0;
758 }
759 }
760
761 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200762
763 return entry;
764}
765
Ingo Molnare309b412008-05-12 21:20:51 +0200766static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200767tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200768{
769 struct task_struct *tsk = current;
770 unsigned long pc;
771
772 pc = preempt_count();
773
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200774 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200775 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200776 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200777 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
778 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
779 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
780 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
781}
782
Ingo Molnare309b412008-05-12 21:20:51 +0200783void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200784trace_function(struct trace_array *tr, struct trace_array_cpu *data,
785 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786{
787 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200788 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200789
Steven Rostedt92205c22008-05-12 21:20:55 +0200790 raw_local_irq_save(irq_flags);
791 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200792 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200794 entry->type = TRACE_FN;
795 entry->fn.ip = ip;
796 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200797 __raw_spin_unlock(&data->lock);
798 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799}
800
Ingo Molnare309b412008-05-12 21:20:51 +0200801void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200802ftrace(struct trace_array *tr, struct trace_array_cpu *data,
803 unsigned long ip, unsigned long parent_ip, unsigned long flags)
804{
805 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200806 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200807}
808
Ingo Molnare309b412008-05-12 21:20:51 +0200809void
Ingo Molnar4e655512008-05-12 21:20:52 +0200810__trace_special(void *__tr, void *__data,
811 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200812{
Ingo Molnar4e655512008-05-12 21:20:52 +0200813 struct trace_array_cpu *data = __data;
814 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200815 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200816 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200817
Steven Rostedt92205c22008-05-12 21:20:55 +0200818 raw_local_irq_save(irq_flags);
819 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200820 entry = tracing_get_trace_entry(tr, data);
821 tracing_generic_entry_update(entry, 0);
822 entry->type = TRACE_SPECIAL;
823 entry->special.arg1 = arg1;
824 entry->special.arg2 = arg2;
825 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200826 __raw_spin_unlock(&data->lock);
827 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200828
829 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200830}
831
832void __trace_stack(struct trace_array *tr,
833 struct trace_array_cpu *data,
834 unsigned long flags,
835 int skip)
836{
837 struct trace_entry *entry;
838 struct stack_trace trace;
839
840 if (!(trace_flags & TRACE_ITER_STACKTRACE))
841 return;
842
843 entry = tracing_get_trace_entry(tr, data);
844 tracing_generic_entry_update(entry, flags);
845 entry->type = TRACE_STACK;
846
847 memset(&entry->stack, 0, sizeof(entry->stack));
848
849 trace.nr_entries = 0;
850 trace.max_entries = FTRACE_STACK_ENTRIES;
851 trace.skip = skip;
852 trace.entries = entry->stack.caller;
853
854 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200855}
856
Ingo Molnare309b412008-05-12 21:20:51 +0200857void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200858tracing_sched_switch_trace(struct trace_array *tr,
859 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200860 struct task_struct *prev,
861 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200862 unsigned long flags)
863{
864 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200865 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200866
Steven Rostedt92205c22008-05-12 21:20:55 +0200867 raw_local_irq_save(irq_flags);
868 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200869 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200870 tracing_generic_entry_update(entry, flags);
871 entry->type = TRACE_CTX;
872 entry->ctx.prev_pid = prev->pid;
873 entry->ctx.prev_prio = prev->prio;
874 entry->ctx.prev_state = prev->state;
875 entry->ctx.next_pid = next->pid;
876 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200877 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200878 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200879 __raw_spin_unlock(&data->lock);
880 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200881}
882
Ingo Molnar57422792008-05-12 21:20:51 +0200883void
884tracing_sched_wakeup_trace(struct trace_array *tr,
885 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200886 struct task_struct *wakee,
887 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200888 unsigned long flags)
889{
890 struct trace_entry *entry;
891 unsigned long irq_flags;
892
Steven Rostedt92205c22008-05-12 21:20:55 +0200893 raw_local_irq_save(irq_flags);
894 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200895 entry = tracing_get_trace_entry(tr, data);
896 tracing_generic_entry_update(entry, flags);
897 entry->type = TRACE_WAKE;
898 entry->ctx.prev_pid = curr->pid;
899 entry->ctx.prev_prio = curr->prio;
900 entry->ctx.prev_state = curr->state;
901 entry->ctx.next_pid = wakee->pid;
902 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200903 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200904 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200905 __raw_spin_unlock(&data->lock);
906 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200907
908 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200909}
910
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200911#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200912static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200913function_trace_call(unsigned long ip, unsigned long parent_ip)
914{
915 struct trace_array *tr = &global_trace;
916 struct trace_array_cpu *data;
917 unsigned long flags;
918 long disabled;
919 int cpu;
920
921 if (unlikely(!tracer_enabled))
922 return;
923
924 local_irq_save(flags);
925 cpu = raw_smp_processor_id();
926 data = tr->data[cpu];
927 disabled = atomic_inc_return(&data->disabled);
928
929 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200930 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200931
932 atomic_dec(&data->disabled);
933 local_irq_restore(flags);
934}
935
936static struct ftrace_ops trace_ops __read_mostly =
937{
938 .func = function_trace_call,
939};
940
Ingo Molnare309b412008-05-12 21:20:51 +0200941void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200942{
943 register_ftrace_function(&trace_ops);
944}
945
Ingo Molnare309b412008-05-12 21:20:51 +0200946void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200947{
948 unregister_ftrace_function(&trace_ops);
949}
950#endif
951
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200952enum trace_file_type {
953 TRACE_FILE_LAT_FMT = 1,
954};
955
956static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200957trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
958 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200959{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200960 struct page *page;
961 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200962
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200963 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200964 iter->next_idx[cpu] >= data->trace_idx ||
965 (data->trace_head == data->trace_tail &&
966 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200967 return NULL;
968
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200969 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200970 /* Initialize the iterator for this cpu trace buffer */
971 WARN_ON(!data->trace_tail);
972 page = virt_to_page(data->trace_tail);
973 iter->next_page[cpu] = &page->lru;
974 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200975 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200976
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200977 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200978 BUG_ON(&data->trace_pages == &page->lru);
979
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200980 array = page_address(page);
981
Steven Rostedt93a588f2008-05-12 21:20:45 +0200982 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200983 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200984}
985
Ingo Molnare309b412008-05-12 21:20:51 +0200986static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200987find_next_entry(struct trace_iterator *iter, int *ent_cpu)
988{
989 struct trace_array *tr = iter->tr;
990 struct trace_entry *ent, *next = NULL;
991 int next_cpu = -1;
992 int cpu;
993
Steven Rostedtab464282008-05-12 21:21:00 +0200994 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200995 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200997 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200998 /*
999 * Pick the entry with the smallest timestamp:
1000 */
1001 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001002 next = ent;
1003 next_cpu = cpu;
1004 }
1005 }
1006
1007 if (ent_cpu)
1008 *ent_cpu = next_cpu;
1009
1010 return next;
1011}
1012
Ingo Molnare309b412008-05-12 21:20:51 +02001013static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001014{
1015 iter->idx++;
1016 iter->next_idx[iter->cpu]++;
1017 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001018
Steven Rostedtb3806b42008-05-12 21:20:46 +02001019 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1020 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1021
1022 iter->next_page_idx[iter->cpu] = 0;
1023 iter->next_page[iter->cpu] =
1024 trace_next_list(data, iter->next_page[iter->cpu]);
1025 }
1026}
1027
Ingo Molnare309b412008-05-12 21:20:51 +02001028static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001029{
1030 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1031
1032 data->trace_tail_idx++;
1033 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1034 data->trace_tail = trace_next_page(data, data->trace_tail);
1035 data->trace_tail_idx = 0;
1036 }
1037
1038 /* Check if we empty it, then reset the index */
1039 if (data->trace_head == data->trace_tail &&
1040 data->trace_head_idx == data->trace_tail_idx)
1041 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001042}
1043
Ingo Molnare309b412008-05-12 21:20:51 +02001044static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045{
1046 struct trace_entry *next;
1047 int next_cpu = -1;
1048
1049 next = find_next_entry(iter, &next_cpu);
1050
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001051 iter->prev_ent = iter->ent;
1052 iter->prev_cpu = iter->cpu;
1053
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001054 iter->ent = next;
1055 iter->cpu = next_cpu;
1056
Steven Rostedtb3806b42008-05-12 21:20:46 +02001057 if (next)
1058 trace_iterator_increment(iter);
1059
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001060 return next ? iter : NULL;
1061}
1062
Ingo Molnare309b412008-05-12 21:20:51 +02001063static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064{
1065 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001066 void *last_ent = iter->ent;
1067 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001068 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001069
1070 (*pos)++;
1071
1072 /* can't go backwards */
1073 if (iter->idx > i)
1074 return NULL;
1075
1076 if (iter->idx < 0)
1077 ent = find_next_entry_inc(iter);
1078 else
1079 ent = iter;
1080
1081 while (ent && iter->idx < i)
1082 ent = find_next_entry_inc(iter);
1083
1084 iter->pos = *pos;
1085
1086 if (last_ent && !ent)
1087 seq_puts(m, "\n\nvim:ft=help\n");
1088
1089 return ent;
1090}
1091
1092static void *s_start(struct seq_file *m, loff_t *pos)
1093{
1094 struct trace_iterator *iter = m->private;
1095 void *p = NULL;
1096 loff_t l = 0;
1097 int i;
1098
1099 mutex_lock(&trace_types_lock);
1100
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001101 if (!current_trace || current_trace != iter->trace) {
1102 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001103 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001104 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105
1106 atomic_inc(&trace_record_cmdline_disabled);
1107
1108 /* let the tracer grab locks here if needed */
1109 if (current_trace->start)
1110 current_trace->start(iter);
1111
1112 if (*pos != iter->pos) {
1113 iter->ent = NULL;
1114 iter->cpu = 0;
1115 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001116 iter->prev_ent = NULL;
1117 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118
Steven Rostedtab464282008-05-12 21:21:00 +02001119 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001121 iter->next_page[i] = NULL;
1122 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001123
1124 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1125 ;
1126
1127 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001128 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001129 p = s_next(m, p, &l);
1130 }
1131
1132 return p;
1133}
1134
1135static void s_stop(struct seq_file *m, void *p)
1136{
1137 struct trace_iterator *iter = m->private;
1138
1139 atomic_dec(&trace_record_cmdline_disabled);
1140
1141 /* let the tracer release locks here if needed */
1142 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1143 iter->trace->stop(iter);
1144
1145 mutex_unlock(&trace_types_lock);
1146}
1147
Steven Rostedtb3806b42008-05-12 21:20:46 +02001148static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001149seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001150{
1151#ifdef CONFIG_KALLSYMS
1152 char str[KSYM_SYMBOL_LEN];
1153
1154 kallsyms_lookup(address, NULL, NULL, NULL, str);
1155
Steven Rostedtb3806b42008-05-12 21:20:46 +02001156 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001158 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001159}
1160
Steven Rostedtb3806b42008-05-12 21:20:46 +02001161static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001162seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1163 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001164{
1165#ifdef CONFIG_KALLSYMS
1166 char str[KSYM_SYMBOL_LEN];
1167
1168 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001169 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001171 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172}
1173
1174#ifndef CONFIG_64BIT
1175# define IP_FMT "%08lx"
1176#else
1177# define IP_FMT "%016lx"
1178#endif
1179
Ingo Molnare309b412008-05-12 21:20:51 +02001180static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001181seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001182{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001183 int ret;
1184
1185 if (!ip)
1186 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187
1188 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001189 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001191 ret = seq_print_sym_short(s, "%s", ip);
1192
1193 if (!ret)
1194 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001195
1196 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001197 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1198 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001199}
1200
Ingo Molnare309b412008-05-12 21:20:51 +02001201static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001202{
1203 seq_puts(m, "# _------=> CPU# \n");
1204 seq_puts(m, "# / _-----=> irqs-off \n");
1205 seq_puts(m, "# | / _----=> need-resched \n");
1206 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1207 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1208 seq_puts(m, "# |||| / \n");
1209 seq_puts(m, "# ||||| delay \n");
1210 seq_puts(m, "# cmd pid ||||| time | caller \n");
1211 seq_puts(m, "# \\ / ||||| \\ | / \n");
1212}
1213
Ingo Molnare309b412008-05-12 21:20:51 +02001214static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001215{
1216 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1217 seq_puts(m, "# | | | | |\n");
1218}
1219
1220
Ingo Molnare309b412008-05-12 21:20:51 +02001221static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001222print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1223{
1224 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1225 struct trace_array *tr = iter->tr;
1226 struct trace_array_cpu *data = tr->data[tr->cpu];
1227 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001228 unsigned long total = 0;
1229 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001230 int cpu;
1231 const char *name = "preemption";
1232
1233 if (type)
1234 name = type->name;
1235
Steven Rostedtab464282008-05-12 21:21:00 +02001236 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001237 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001238 total += tr->data[cpu]->trace_idx;
1239 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001241 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242 entries += tr->data[cpu]->trace_idx;
1243 }
1244 }
1245
1246 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1247 name, UTS_RELEASE);
1248 seq_puts(m, "-----------------------------------"
1249 "---------------------------------\n");
1250 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1251 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001252 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001254 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255 tr->cpu,
1256#if defined(CONFIG_PREEMPT_NONE)
1257 "server",
1258#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1259 "desktop",
1260#elif defined(CONFIG_PREEMPT_DESKTOP)
1261 "preempt",
1262#else
1263 "unknown",
1264#endif
1265 /* These are reserved for later use */
1266 0, 0, 0, 0);
1267#ifdef CONFIG_SMP
1268 seq_printf(m, " #P:%d)\n", num_online_cpus());
1269#else
1270 seq_puts(m, ")\n");
1271#endif
1272 seq_puts(m, " -----------------\n");
1273 seq_printf(m, " | task: %.16s-%d "
1274 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1275 data->comm, data->pid, data->uid, data->nice,
1276 data->policy, data->rt_priority);
1277 seq_puts(m, " -----------------\n");
1278
1279 if (data->critical_start) {
1280 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001281 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1282 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001284 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1285 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 seq_puts(m, "\n");
1287 }
1288
1289 seq_puts(m, "\n");
1290}
1291
Ingo Molnare309b412008-05-12 21:20:51 +02001292static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001293lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001294{
1295 int hardirq, softirq;
1296 char *comm;
1297
1298 comm = trace_find_cmdline(entry->pid);
1299
Steven Rostedt214023c2008-05-12 21:20:46 +02001300 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1301 trace_seq_printf(s, "%d", cpu);
1302 trace_seq_printf(s, "%c%c",
1303 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1304 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001305
1306 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1307 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001308 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001309 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001310 } else {
1311 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001312 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001313 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001314 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001315 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001317 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318 }
1319 }
1320
1321 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001322 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001324 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325}
1326
1327unsigned long preempt_mark_thresh = 100;
1328
Ingo Molnare309b412008-05-12 21:20:51 +02001329static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001330lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001331 unsigned long rel_usecs)
1332{
Steven Rostedt214023c2008-05-12 21:20:46 +02001333 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001335 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001337 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001339 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340}
1341
1342static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1343
Ingo Molnare309b412008-05-12 21:20:51 +02001344static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001345print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346{
Steven Rostedt214023c2008-05-12 21:20:46 +02001347 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1349 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1350 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1351 struct trace_entry *entry = iter->ent;
1352 unsigned long abs_usecs;
1353 unsigned long rel_usecs;
1354 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001355 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001356 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001357 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358
1359 if (!next_entry)
1360 next_entry = entry;
1361 rel_usecs = ns2usecs(next_entry->t - entry->t);
1362 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1363
1364 if (verbose) {
1365 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001366 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1367 " %ld.%03ldms (+%ld.%03ldms): ",
1368 comm,
1369 entry->pid, cpu, entry->flags,
1370 entry->preempt_count, trace_idx,
1371 ns2usecs(entry->t),
1372 abs_usecs/1000,
1373 abs_usecs % 1000, rel_usecs/1000,
1374 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001375 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001376 lat_print_generic(s, entry, cpu);
1377 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378 }
1379 switch (entry->type) {
1380 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001381 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1382 trace_seq_puts(s, " (");
1383 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1384 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001385 break;
1386 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001387 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001388 T = entry->ctx.next_state < sizeof(state_to_char) ?
1389 state_to_char[entry->ctx.next_state] : 'X';
1390
Ankita Gargd17d9692008-05-12 21:20:58 +02001391 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1392 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001394 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001395 entry->ctx.prev_pid,
1396 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001397 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001398 entry->ctx.next_pid,
1399 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001400 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001401 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001402 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001403 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001404 entry->special.arg1,
1405 entry->special.arg2,
1406 entry->special.arg3);
1407 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001408 case TRACE_STACK:
1409 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1410 if (i)
1411 trace_seq_puts(s, " <= ");
1412 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1413 }
1414 trace_seq_puts(s, "\n");
1415 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001416 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001417 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001418 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001419 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420}
1421
Ingo Molnare309b412008-05-12 21:20:51 +02001422static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001423{
Steven Rostedt214023c2008-05-12 21:20:46 +02001424 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001425 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001426 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 unsigned long usec_rem;
1428 unsigned long long t;
1429 unsigned long secs;
1430 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001431 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001432 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001433 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001434
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001435 entry = iter->ent;
1436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001437 comm = trace_find_cmdline(iter->ent->pid);
1438
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001439 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440 usec_rem = do_div(t, 1000000ULL);
1441 secs = (unsigned long)t;
1442
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001443 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1444 if (!ret)
1445 return 0;
1446 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1447 if (!ret)
1448 return 0;
1449 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1450 if (!ret)
1451 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001452
1453 switch (entry->type) {
1454 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001455 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1456 if (!ret)
1457 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1459 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001460 ret = trace_seq_printf(s, " <-");
1461 if (!ret)
1462 return 0;
1463 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1464 sym_flags);
1465 if (!ret)
1466 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001468 ret = trace_seq_printf(s, "\n");
1469 if (!ret)
1470 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001471 break;
1472 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001473 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001474 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1475 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001476 T = entry->ctx.next_state < sizeof(state_to_char) ?
1477 state_to_char[entry->ctx.next_state] : 'X';
1478 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001479 entry->ctx.prev_pid,
1480 entry->ctx.prev_prio,
1481 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001482 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001483 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001484 entry->ctx.next_prio,
1485 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001486 if (!ret)
1487 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001489 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001490 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001491 entry->special.arg1,
1492 entry->special.arg2,
1493 entry->special.arg3);
1494 if (!ret)
1495 return 0;
1496 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001497 case TRACE_STACK:
1498 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1499 if (i) {
1500 ret = trace_seq_puts(s, " <= ");
1501 if (!ret)
1502 return 0;
1503 }
1504 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1505 sym_flags);
1506 if (!ret)
1507 return 0;
1508 }
1509 ret = trace_seq_puts(s, "\n");
1510 if (!ret)
1511 return 0;
1512 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001513 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001514 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515}
1516
Ingo Molnare309b412008-05-12 21:20:51 +02001517static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001518{
1519 struct trace_seq *s = &iter->seq;
1520 struct trace_entry *entry;
1521 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001522 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001523
1524 entry = iter->ent;
1525
1526 ret = trace_seq_printf(s, "%d %d %llu ",
1527 entry->pid, iter->cpu, entry->t);
1528 if (!ret)
1529 return 0;
1530
1531 switch (entry->type) {
1532 case TRACE_FN:
1533 ret = trace_seq_printf(s, "%x %x\n",
1534 entry->fn.ip, entry->fn.parent_ip);
1535 if (!ret)
1536 return 0;
1537 break;
1538 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001539 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001540 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1541 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001542 T = entry->ctx.next_state < sizeof(state_to_char) ?
1543 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001544 if (entry->type == TRACE_WAKE)
1545 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001546 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001547 entry->ctx.prev_pid,
1548 entry->ctx.prev_prio,
1549 S,
1550 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001551 entry->ctx.next_prio,
1552 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001553 if (!ret)
1554 return 0;
1555 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001556 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001557 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001558 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001559 entry->special.arg1,
1560 entry->special.arg2,
1561 entry->special.arg3);
1562 if (!ret)
1563 return 0;
1564 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001565 }
1566 return 1;
1567}
1568
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001569#define SEQ_PUT_FIELD_RET(s, x) \
1570do { \
1571 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1572 return 0; \
1573} while (0)
1574
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001575#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1576do { \
1577 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1578 return 0; \
1579} while (0)
1580
Ingo Molnare309b412008-05-12 21:20:51 +02001581static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001582{
1583 struct trace_seq *s = &iter->seq;
1584 unsigned char newline = '\n';
1585 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001586 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001587
1588 entry = iter->ent;
1589
1590 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1591 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1592 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1593
1594 switch (entry->type) {
1595 case TRACE_FN:
1596 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1597 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1598 break;
1599 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001600 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001601 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1602 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001603 T = entry->ctx.next_state < sizeof(state_to_char) ?
1604 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001605 if (entry->type == TRACE_WAKE)
1606 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001607 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1608 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1609 SEQ_PUT_HEX_FIELD_RET(s, S);
1610 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1611 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1612 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001613 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001614 break;
1615 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001616 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001617 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1618 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1619 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1620 break;
1621 }
1622 SEQ_PUT_FIELD_RET(s, newline);
1623
1624 return 1;
1625}
1626
Ingo Molnare309b412008-05-12 21:20:51 +02001627static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001628{
1629 struct trace_seq *s = &iter->seq;
1630 struct trace_entry *entry;
1631
1632 entry = iter->ent;
1633
1634 SEQ_PUT_FIELD_RET(s, entry->pid);
1635 SEQ_PUT_FIELD_RET(s, entry->cpu);
1636 SEQ_PUT_FIELD_RET(s, entry->t);
1637
1638 switch (entry->type) {
1639 case TRACE_FN:
1640 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1641 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1642 break;
1643 case TRACE_CTX:
1644 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1645 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1646 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1647 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1648 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001649 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001650 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001651 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001652 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001653 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1654 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1655 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1656 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001657 }
1658 return 1;
1659}
1660
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001661static int trace_empty(struct trace_iterator *iter)
1662{
1663 struct trace_array_cpu *data;
1664 int cpu;
1665
Steven Rostedtab464282008-05-12 21:21:00 +02001666 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001667 data = iter->tr->data[cpu];
1668
Steven Rostedtb3806b42008-05-12 21:20:46 +02001669 if (head_page(data) && data->trace_idx &&
1670 (data->trace_tail != data->trace_head ||
1671 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001672 return 0;
1673 }
1674 return 1;
1675}
1676
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001677static int print_trace_line(struct trace_iterator *iter)
1678{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001679 if (iter->trace && iter->trace->print_line)
1680 return iter->trace->print_line(iter);
1681
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001682 if (trace_flags & TRACE_ITER_BIN)
1683 return print_bin_fmt(iter);
1684
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001685 if (trace_flags & TRACE_ITER_HEX)
1686 return print_hex_fmt(iter);
1687
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001688 if (trace_flags & TRACE_ITER_RAW)
1689 return print_raw_fmt(iter);
1690
1691 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1692 return print_lat_fmt(iter, iter->idx, iter->cpu);
1693
1694 return print_trace_fmt(iter);
1695}
1696
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001697static int s_show(struct seq_file *m, void *v)
1698{
1699 struct trace_iterator *iter = v;
1700
1701 if (iter->ent == NULL) {
1702 if (iter->tr) {
1703 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1704 seq_puts(m, "#\n");
1705 }
1706 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1707 /* print nothing if the buffers are empty */
1708 if (trace_empty(iter))
1709 return 0;
1710 print_trace_header(m, iter);
1711 if (!(trace_flags & TRACE_ITER_VERBOSE))
1712 print_lat_help_header(m);
1713 } else {
1714 if (!(trace_flags & TRACE_ITER_VERBOSE))
1715 print_func_help_header(m);
1716 }
1717 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001718 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001719 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001720 }
1721
1722 return 0;
1723}
1724
1725static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001726 .start = s_start,
1727 .next = s_next,
1728 .stop = s_stop,
1729 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001730};
1731
Ingo Molnare309b412008-05-12 21:20:51 +02001732static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001733__tracing_open(struct inode *inode, struct file *file, int *ret)
1734{
1735 struct trace_iterator *iter;
1736
Steven Rostedt60a11772008-05-12 21:20:44 +02001737 if (tracing_disabled) {
1738 *ret = -ENODEV;
1739 return NULL;
1740 }
1741
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001742 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1743 if (!iter) {
1744 *ret = -ENOMEM;
1745 goto out;
1746 }
1747
1748 mutex_lock(&trace_types_lock);
1749 if (current_trace && current_trace->print_max)
1750 iter->tr = &max_tr;
1751 else
1752 iter->tr = inode->i_private;
1753 iter->trace = current_trace;
1754 iter->pos = -1;
1755
1756 /* TODO stop tracer */
1757 *ret = seq_open(file, &tracer_seq_ops);
1758 if (!*ret) {
1759 struct seq_file *m = file->private_data;
1760 m->private = iter;
1761
1762 /* stop the trace while dumping */
1763 if (iter->tr->ctrl)
1764 tracer_enabled = 0;
1765
1766 if (iter->trace && iter->trace->open)
1767 iter->trace->open(iter);
1768 } else {
1769 kfree(iter);
1770 iter = NULL;
1771 }
1772 mutex_unlock(&trace_types_lock);
1773
1774 out:
1775 return iter;
1776}
1777
1778int tracing_open_generic(struct inode *inode, struct file *filp)
1779{
Steven Rostedt60a11772008-05-12 21:20:44 +02001780 if (tracing_disabled)
1781 return -ENODEV;
1782
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001783 filp->private_data = inode->i_private;
1784 return 0;
1785}
1786
1787int tracing_release(struct inode *inode, struct file *file)
1788{
1789 struct seq_file *m = (struct seq_file *)file->private_data;
1790 struct trace_iterator *iter = m->private;
1791
1792 mutex_lock(&trace_types_lock);
1793 if (iter->trace && iter->trace->close)
1794 iter->trace->close(iter);
1795
1796 /* reenable tracing if it was previously enabled */
1797 if (iter->tr->ctrl)
1798 tracer_enabled = 1;
1799 mutex_unlock(&trace_types_lock);
1800
1801 seq_release(inode, file);
1802 kfree(iter);
1803 return 0;
1804}
1805
1806static int tracing_open(struct inode *inode, struct file *file)
1807{
1808 int ret;
1809
1810 __tracing_open(inode, file, &ret);
1811
1812 return ret;
1813}
1814
1815static int tracing_lt_open(struct inode *inode, struct file *file)
1816{
1817 struct trace_iterator *iter;
1818 int ret;
1819
1820 iter = __tracing_open(inode, file, &ret);
1821
1822 if (!ret)
1823 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1824
1825 return ret;
1826}
1827
1828
Ingo Molnare309b412008-05-12 21:20:51 +02001829static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001830t_next(struct seq_file *m, void *v, loff_t *pos)
1831{
1832 struct tracer *t = m->private;
1833
1834 (*pos)++;
1835
1836 if (t)
1837 t = t->next;
1838
1839 m->private = t;
1840
1841 return t;
1842}
1843
1844static void *t_start(struct seq_file *m, loff_t *pos)
1845{
1846 struct tracer *t = m->private;
1847 loff_t l = 0;
1848
1849 mutex_lock(&trace_types_lock);
1850 for (; t && l < *pos; t = t_next(m, t, &l))
1851 ;
1852
1853 return t;
1854}
1855
1856static void t_stop(struct seq_file *m, void *p)
1857{
1858 mutex_unlock(&trace_types_lock);
1859}
1860
1861static int t_show(struct seq_file *m, void *v)
1862{
1863 struct tracer *t = v;
1864
1865 if (!t)
1866 return 0;
1867
1868 seq_printf(m, "%s", t->name);
1869 if (t->next)
1870 seq_putc(m, ' ');
1871 else
1872 seq_putc(m, '\n');
1873
1874 return 0;
1875}
1876
1877static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001878 .start = t_start,
1879 .next = t_next,
1880 .stop = t_stop,
1881 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882};
1883
1884static int show_traces_open(struct inode *inode, struct file *file)
1885{
1886 int ret;
1887
Steven Rostedt60a11772008-05-12 21:20:44 +02001888 if (tracing_disabled)
1889 return -ENODEV;
1890
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001891 ret = seq_open(file, &show_traces_seq_ops);
1892 if (!ret) {
1893 struct seq_file *m = file->private_data;
1894 m->private = trace_types;
1895 }
1896
1897 return ret;
1898}
1899
1900static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001901 .open = tracing_open,
1902 .read = seq_read,
1903 .llseek = seq_lseek,
1904 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001905};
1906
1907static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001908 .open = tracing_lt_open,
1909 .read = seq_read,
1910 .llseek = seq_lseek,
1911 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001912};
1913
1914static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001915 .open = show_traces_open,
1916 .read = seq_read,
1917 .release = seq_release,
1918};
1919
Ingo Molnar36dfe922008-05-12 21:20:52 +02001920/*
1921 * Only trace on a CPU if the bitmask is set:
1922 */
1923static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1924
1925/*
1926 * When tracing/tracing_cpu_mask is modified then this holds
1927 * the new bitmask we are about to install:
1928 */
1929static cpumask_t tracing_cpumask_new;
1930
1931/*
1932 * The tracer itself will not take this lock, but still we want
1933 * to provide a consistent cpumask to user-space:
1934 */
1935static DEFINE_MUTEX(tracing_cpumask_update_lock);
1936
1937/*
1938 * Temporary storage for the character representation of the
1939 * CPU bitmask (and one more byte for the newline):
1940 */
1941static char mask_str[NR_CPUS + 1];
1942
Ingo Molnarc7078de2008-05-12 21:20:52 +02001943static ssize_t
1944tracing_cpumask_read(struct file *filp, char __user *ubuf,
1945 size_t count, loff_t *ppos)
1946{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001947 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001948
1949 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001950
1951 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1952 if (count - len < 2) {
1953 count = -EINVAL;
1954 goto out_err;
1955 }
1956 len += sprintf(mask_str + len, "\n");
1957 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1958
1959out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001960 mutex_unlock(&tracing_cpumask_update_lock);
1961
1962 return count;
1963}
1964
1965static ssize_t
1966tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1967 size_t count, loff_t *ppos)
1968{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001969 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001970
1971 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001972 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1973 if (err)
1974 goto err_unlock;
1975
Steven Rostedt92205c22008-05-12 21:20:55 +02001976 raw_local_irq_disable();
1977 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001978 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001979 /*
1980 * Increase/decrease the disabled counter if we are
1981 * about to flip a bit in the cpumask:
1982 */
1983 if (cpu_isset(cpu, tracing_cpumask) &&
1984 !cpu_isset(cpu, tracing_cpumask_new)) {
1985 atomic_inc(&global_trace.data[cpu]->disabled);
1986 }
1987 if (!cpu_isset(cpu, tracing_cpumask) &&
1988 cpu_isset(cpu, tracing_cpumask_new)) {
1989 atomic_dec(&global_trace.data[cpu]->disabled);
1990 }
1991 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001992 __raw_spin_unlock(&ftrace_max_lock);
1993 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001994
1995 tracing_cpumask = tracing_cpumask_new;
1996
Ingo Molnarc7078de2008-05-12 21:20:52 +02001997 mutex_unlock(&tracing_cpumask_update_lock);
1998
Ingo Molnarc7078de2008-05-12 21:20:52 +02001999 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002000
2001err_unlock:
2002 mutex_unlock(&tracing_cpumask_update_lock);
2003
2004 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002005}
2006
2007static struct file_operations tracing_cpumask_fops = {
2008 .open = tracing_open_generic,
2009 .read = tracing_cpumask_read,
2010 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002011};
2012
2013static ssize_t
2014tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2015 size_t cnt, loff_t *ppos)
2016{
2017 char *buf;
2018 int r = 0;
2019 int len = 0;
2020 int i;
2021
2022 /* calulate max size */
2023 for (i = 0; trace_options[i]; i++) {
2024 len += strlen(trace_options[i]);
2025 len += 3; /* "no" and space */
2026 }
2027
2028 /* +2 for \n and \0 */
2029 buf = kmalloc(len + 2, GFP_KERNEL);
2030 if (!buf)
2031 return -ENOMEM;
2032
2033 for (i = 0; trace_options[i]; i++) {
2034 if (trace_flags & (1 << i))
2035 r += sprintf(buf + r, "%s ", trace_options[i]);
2036 else
2037 r += sprintf(buf + r, "no%s ", trace_options[i]);
2038 }
2039
2040 r += sprintf(buf + r, "\n");
2041 WARN_ON(r >= len + 2);
2042
Ingo Molnar36dfe922008-05-12 21:20:52 +02002043 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002044
2045 kfree(buf);
2046
2047 return r;
2048}
2049
2050static ssize_t
2051tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2052 size_t cnt, loff_t *ppos)
2053{
2054 char buf[64];
2055 char *cmp = buf;
2056 int neg = 0;
2057 int i;
2058
Steven Rostedtcffae432008-05-12 21:21:00 +02002059 if (cnt >= sizeof(buf))
2060 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002061
2062 if (copy_from_user(&buf, ubuf, cnt))
2063 return -EFAULT;
2064
2065 buf[cnt] = 0;
2066
2067 if (strncmp(buf, "no", 2) == 0) {
2068 neg = 1;
2069 cmp += 2;
2070 }
2071
2072 for (i = 0; trace_options[i]; i++) {
2073 int len = strlen(trace_options[i]);
2074
2075 if (strncmp(cmp, trace_options[i], len) == 0) {
2076 if (neg)
2077 trace_flags &= ~(1 << i);
2078 else
2079 trace_flags |= (1 << i);
2080 break;
2081 }
2082 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002083 /*
2084 * If no option could be set, return an error:
2085 */
2086 if (!trace_options[i])
2087 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002088
2089 filp->f_pos += cnt;
2090
2091 return cnt;
2092}
2093
2094static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002095 .open = tracing_open_generic,
2096 .read = tracing_iter_ctrl_read,
2097 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098};
2099
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002100static const char readme_msg[] =
2101 "tracing mini-HOWTO:\n\n"
2102 "# mkdir /debug\n"
2103 "# mount -t debugfs nodev /debug\n\n"
2104 "# cat /debug/tracing/available_tracers\n"
2105 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2106 "# cat /debug/tracing/current_tracer\n"
2107 "none\n"
2108 "# echo sched_switch > /debug/tracing/current_tracer\n"
2109 "# cat /debug/tracing/current_tracer\n"
2110 "sched_switch\n"
2111 "# cat /debug/tracing/iter_ctrl\n"
2112 "noprint-parent nosym-offset nosym-addr noverbose\n"
2113 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2114 "# echo 1 > /debug/tracing/tracing_enabled\n"
2115 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2116 "echo 0 > /debug/tracing/tracing_enabled\n"
2117;
2118
2119static ssize_t
2120tracing_readme_read(struct file *filp, char __user *ubuf,
2121 size_t cnt, loff_t *ppos)
2122{
2123 return simple_read_from_buffer(ubuf, cnt, ppos,
2124 readme_msg, strlen(readme_msg));
2125}
2126
2127static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002128 .open = tracing_open_generic,
2129 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002130};
2131
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002132static ssize_t
2133tracing_ctrl_read(struct file *filp, char __user *ubuf,
2134 size_t cnt, loff_t *ppos)
2135{
2136 struct trace_array *tr = filp->private_data;
2137 char buf[64];
2138 int r;
2139
2140 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002141 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002142}
2143
2144static ssize_t
2145tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2146 size_t cnt, loff_t *ppos)
2147{
2148 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002149 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002150 long val;
2151 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152
Steven Rostedtcffae432008-05-12 21:21:00 +02002153 if (cnt >= sizeof(buf))
2154 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002155
2156 if (copy_from_user(&buf, ubuf, cnt))
2157 return -EFAULT;
2158
2159 buf[cnt] = 0;
2160
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002161 ret = strict_strtoul(buf, 10, &val);
2162 if (ret < 0)
2163 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002164
2165 val = !!val;
2166
2167 mutex_lock(&trace_types_lock);
2168 if (tr->ctrl ^ val) {
2169 if (val)
2170 tracer_enabled = 1;
2171 else
2172 tracer_enabled = 0;
2173
2174 tr->ctrl = val;
2175
2176 if (current_trace && current_trace->ctrl_update)
2177 current_trace->ctrl_update(tr);
2178 }
2179 mutex_unlock(&trace_types_lock);
2180
2181 filp->f_pos += cnt;
2182
2183 return cnt;
2184}
2185
2186static ssize_t
2187tracing_set_trace_read(struct file *filp, char __user *ubuf,
2188 size_t cnt, loff_t *ppos)
2189{
2190 char buf[max_tracer_type_len+2];
2191 int r;
2192
2193 mutex_lock(&trace_types_lock);
2194 if (current_trace)
2195 r = sprintf(buf, "%s\n", current_trace->name);
2196 else
2197 r = sprintf(buf, "\n");
2198 mutex_unlock(&trace_types_lock);
2199
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002200 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002201}
2202
2203static ssize_t
2204tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2205 size_t cnt, loff_t *ppos)
2206{
2207 struct trace_array *tr = &global_trace;
2208 struct tracer *t;
2209 char buf[max_tracer_type_len+1];
2210 int i;
2211
2212 if (cnt > max_tracer_type_len)
2213 cnt = max_tracer_type_len;
2214
2215 if (copy_from_user(&buf, ubuf, cnt))
2216 return -EFAULT;
2217
2218 buf[cnt] = 0;
2219
2220 /* strip ending whitespace. */
2221 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2222 buf[i] = 0;
2223
2224 mutex_lock(&trace_types_lock);
2225 for (t = trace_types; t; t = t->next) {
2226 if (strcmp(t->name, buf) == 0)
2227 break;
2228 }
2229 if (!t || t == current_trace)
2230 goto out;
2231
2232 if (current_trace && current_trace->reset)
2233 current_trace->reset(tr);
2234
2235 current_trace = t;
2236 if (t->init)
2237 t->init(tr);
2238
2239 out:
2240 mutex_unlock(&trace_types_lock);
2241
2242 filp->f_pos += cnt;
2243
2244 return cnt;
2245}
2246
2247static ssize_t
2248tracing_max_lat_read(struct file *filp, char __user *ubuf,
2249 size_t cnt, loff_t *ppos)
2250{
2251 unsigned long *ptr = filp->private_data;
2252 char buf[64];
2253 int r;
2254
Steven Rostedtcffae432008-05-12 21:21:00 +02002255 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002256 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002257 if (r > sizeof(buf))
2258 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002259 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002260}
2261
2262static ssize_t
2263tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2264 size_t cnt, loff_t *ppos)
2265{
2266 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002268 long val;
2269 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270
Steven Rostedtcffae432008-05-12 21:21:00 +02002271 if (cnt >= sizeof(buf))
2272 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273
2274 if (copy_from_user(&buf, ubuf, cnt))
2275 return -EFAULT;
2276
2277 buf[cnt] = 0;
2278
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002279 ret = strict_strtoul(buf, 10, &val);
2280 if (ret < 0)
2281 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002282
2283 *ptr = val * 1000;
2284
2285 return cnt;
2286}
2287
Steven Rostedtb3806b42008-05-12 21:20:46 +02002288static atomic_t tracing_reader;
2289
2290static int tracing_open_pipe(struct inode *inode, struct file *filp)
2291{
2292 struct trace_iterator *iter;
2293
2294 if (tracing_disabled)
2295 return -ENODEV;
2296
2297 /* We only allow for reader of the pipe */
2298 if (atomic_inc_return(&tracing_reader) != 1) {
2299 atomic_dec(&tracing_reader);
2300 return -EBUSY;
2301 }
2302
2303 /* create a buffer to store the information to pass to userspace */
2304 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2305 if (!iter)
2306 return -ENOMEM;
2307
2308 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002309 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002310
2311 filp->private_data = iter;
2312
2313 return 0;
2314}
2315
2316static int tracing_release_pipe(struct inode *inode, struct file *file)
2317{
2318 struct trace_iterator *iter = file->private_data;
2319
2320 kfree(iter);
2321 atomic_dec(&tracing_reader);
2322
2323 return 0;
2324}
2325
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002326static unsigned int
2327tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2328{
2329 struct trace_iterator *iter = filp->private_data;
2330
2331 if (trace_flags & TRACE_ITER_BLOCK) {
2332 /*
2333 * Always select as readable when in blocking mode
2334 */
2335 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002336 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002337 if (!trace_empty(iter))
2338 return POLLIN | POLLRDNORM;
2339 poll_wait(filp, &trace_wait, poll_table);
2340 if (!trace_empty(iter))
2341 return POLLIN | POLLRDNORM;
2342
2343 return 0;
2344 }
2345}
2346
Steven Rostedtb3806b42008-05-12 21:20:46 +02002347/*
2348 * Consumer reader.
2349 */
2350static ssize_t
2351tracing_read_pipe(struct file *filp, char __user *ubuf,
2352 size_t cnt, loff_t *ppos)
2353{
2354 struct trace_iterator *iter = filp->private_data;
2355 struct trace_array_cpu *data;
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002356 struct trace_array *tr = iter->tr;
2357 struct tracer *tracer = iter->trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002358 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002359 static int start;
2360 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002361#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002362 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002363#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002364 int read = 0;
2365 int cpu;
2366 int len;
2367 int ret;
2368
2369 /* return any leftover data */
2370 if (iter->seq.len > start) {
2371 len = iter->seq.len - start;
2372 if (cnt > len)
2373 cnt = len;
2374 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2375 if (ret)
2376 cnt = -EFAULT;
2377
2378 start += len;
2379
2380 return cnt;
2381 }
2382
2383 trace_seq_reset(&iter->seq);
2384 start = 0;
2385
2386 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002387
2388 if ((filp->f_flags & O_NONBLOCK))
2389 return -EAGAIN;
2390
Steven Rostedtb3806b42008-05-12 21:20:46 +02002391 /*
2392 * This is a make-shift waitqueue. The reason we don't use
2393 * an actual wait queue is because:
2394 * 1) we only ever have one waiter
2395 * 2) the tracing, traces all functions, we don't want
2396 * the overhead of calling wake_up and friends
2397 * (and tracing them too)
2398 * Anyway, this is really very primitive wakeup.
2399 */
2400 set_current_state(TASK_INTERRUPTIBLE);
2401 iter->tr->waiter = current;
2402
2403 /* sleep for one second, and try again. */
2404 schedule_timeout(HZ);
2405
2406 iter->tr->waiter = NULL;
2407
2408 if (signal_pending(current))
2409 return -EINTR;
2410
Steven Rostedt84527992008-05-12 21:20:58 +02002411 if (iter->trace != current_trace)
2412 return 0;
2413
Steven Rostedtb3806b42008-05-12 21:20:46 +02002414 /*
2415 * We block until we read something and tracing is disabled.
2416 * We still block if tracing is disabled, but we have never
2417 * read anything. This allows a user to cat this file, and
2418 * then enable tracing. But after we have read something,
2419 * we give an EOF when tracing is again disabled.
2420 *
2421 * iter->pos will be 0 if we haven't read anything.
2422 */
2423 if (!tracer_enabled && iter->pos)
2424 break;
2425
2426 continue;
2427 }
2428
2429 /* stop when tracing is finished */
2430 if (trace_empty(iter))
2431 return 0;
2432
2433 if (cnt >= PAGE_SIZE)
2434 cnt = PAGE_SIZE - 1;
2435
2436 memset(iter, 0, sizeof(*iter));
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002437 iter->tr = tr;
2438 iter->trace = tracer;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002439 iter->pos = -1;
2440
2441 /*
2442 * We need to stop all tracing on all CPUS to read the
2443 * the next buffer. This is a bit expensive, but is
2444 * not done often. We fill all what we can read,
2445 * and then release the locks again.
2446 */
2447
2448 cpus_clear(mask);
2449 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002450#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002451 ftrace_save = ftrace_enabled;
2452 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002453#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002454 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002455 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002456 data = iter->tr->data[cpu];
2457
2458 if (!head_page(data) || !data->trace_idx)
2459 continue;
2460
2461 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002462 cpu_set(cpu, mask);
2463 }
2464
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002465 for_each_cpu_mask(cpu, mask) {
2466 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002467 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002468 }
2469
Steven Rostedt088b1e42008-05-12 21:20:48 +02002470 while (find_next_entry_inc(iter) != NULL) {
2471 int len = iter->seq.len;
2472
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002473 ret = print_trace_line(iter);
Steven Rostedt088b1e42008-05-12 21:20:48 +02002474 if (!ret) {
2475 /* don't print partial lines */
2476 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002477 break;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002478 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002479
2480 trace_consume(iter);
2481
2482 if (iter->seq.len >= cnt)
2483 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002484 }
2485
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002486 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002487 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002488 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002489 }
2490
2491 for_each_cpu_mask(cpu, mask) {
2492 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002493 atomic_dec(&data->disabled);
2494 }
Ingo Molnar25770462008-05-12 21:20:49 +02002495#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002496 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002497#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002498 local_irq_restore(flags);
2499
2500 /* Now copy what we have to the user */
2501 read = iter->seq.len;
2502 if (read > cnt)
2503 read = cnt;
2504
2505 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2506
2507 if (read < iter->seq.len)
2508 start = read;
2509 else
2510 trace_seq_reset(&iter->seq);
2511
2512 if (ret)
2513 read = -EFAULT;
2514
2515 return read;
2516}
2517
Steven Rostedta98a3c32008-05-12 21:20:59 +02002518static ssize_t
2519tracing_entries_read(struct file *filp, char __user *ubuf,
2520 size_t cnt, loff_t *ppos)
2521{
2522 struct trace_array *tr = filp->private_data;
2523 char buf[64];
2524 int r;
2525
2526 r = sprintf(buf, "%lu\n", tr->entries);
2527 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2528}
2529
2530static ssize_t
2531tracing_entries_write(struct file *filp, const char __user *ubuf,
2532 size_t cnt, loff_t *ppos)
2533{
2534 unsigned long val;
2535 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002536 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002537
Steven Rostedtcffae432008-05-12 21:21:00 +02002538 if (cnt >= sizeof(buf))
2539 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002540
2541 if (copy_from_user(&buf, ubuf, cnt))
2542 return -EFAULT;
2543
2544 buf[cnt] = 0;
2545
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002546 ret = strict_strtoul(buf, 10, &val);
2547 if (ret < 0)
2548 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002549
2550 /* must have at least 1 entry */
2551 if (!val)
2552 return -EINVAL;
2553
2554 mutex_lock(&trace_types_lock);
2555
2556 if (current_trace != &no_tracer) {
2557 cnt = -EBUSY;
2558 pr_info("ftrace: set current_tracer to none"
2559 " before modifying buffer size\n");
2560 goto out;
2561 }
2562
2563 if (val > global_trace.entries) {
2564 while (global_trace.entries < val) {
2565 if (trace_alloc_page()) {
2566 cnt = -ENOMEM;
2567 goto out;
2568 }
2569 }
2570 } else {
2571 /* include the number of entries in val (inc of page entries) */
2572 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2573 trace_free_page();
2574 }
2575
2576 filp->f_pos += cnt;
2577
2578 out:
2579 max_tr.entries = global_trace.entries;
2580 mutex_unlock(&trace_types_lock);
2581
2582 return cnt;
2583}
2584
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002585static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002586 .open = tracing_open_generic,
2587 .read = tracing_max_lat_read,
2588 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002589};
2590
2591static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002592 .open = tracing_open_generic,
2593 .read = tracing_ctrl_read,
2594 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002595};
2596
2597static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002598 .open = tracing_open_generic,
2599 .read = tracing_set_trace_read,
2600 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002601};
2602
Steven Rostedtb3806b42008-05-12 21:20:46 +02002603static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002604 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002605 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002606 .read = tracing_read_pipe,
2607 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002608};
2609
Steven Rostedta98a3c32008-05-12 21:20:59 +02002610static struct file_operations tracing_entries_fops = {
2611 .open = tracing_open_generic,
2612 .read = tracing_entries_read,
2613 .write = tracing_entries_write,
2614};
2615
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002616#ifdef CONFIG_DYNAMIC_FTRACE
2617
2618static ssize_t
2619tracing_read_long(struct file *filp, char __user *ubuf,
2620 size_t cnt, loff_t *ppos)
2621{
2622 unsigned long *p = filp->private_data;
2623 char buf[64];
2624 int r;
2625
2626 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002627
2628 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002629}
2630
2631static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002632 .open = tracing_open_generic,
2633 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002634};
2635#endif
2636
2637static struct dentry *d_tracer;
2638
2639struct dentry *tracing_init_dentry(void)
2640{
2641 static int once;
2642
2643 if (d_tracer)
2644 return d_tracer;
2645
2646 d_tracer = debugfs_create_dir("tracing", NULL);
2647
2648 if (!d_tracer && !once) {
2649 once = 1;
2650 pr_warning("Could not create debugfs directory 'tracing'\n");
2651 return NULL;
2652 }
2653
2654 return d_tracer;
2655}
2656
Steven Rostedt60a11772008-05-12 21:20:44 +02002657#ifdef CONFIG_FTRACE_SELFTEST
2658/* Let selftest have access to static functions in this file */
2659#include "trace_selftest.c"
2660#endif
2661
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002662static __init void tracer_init_debugfs(void)
2663{
2664 struct dentry *d_tracer;
2665 struct dentry *entry;
2666
2667 d_tracer = tracing_init_dentry();
2668
2669 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2670 &global_trace, &tracing_ctrl_fops);
2671 if (!entry)
2672 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2673
2674 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2675 NULL, &tracing_iter_fops);
2676 if (!entry)
2677 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2678
Ingo Molnarc7078de2008-05-12 21:20:52 +02002679 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2680 NULL, &tracing_cpumask_fops);
2681 if (!entry)
2682 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2683
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002684 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2685 &global_trace, &tracing_lt_fops);
2686 if (!entry)
2687 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2688
2689 entry = debugfs_create_file("trace", 0444, d_tracer,
2690 &global_trace, &tracing_fops);
2691 if (!entry)
2692 pr_warning("Could not create debugfs 'trace' entry\n");
2693
2694 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2695 &global_trace, &show_traces_fops);
2696 if (!entry)
2697 pr_warning("Could not create debugfs 'trace' entry\n");
2698
2699 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2700 &global_trace, &set_tracer_fops);
2701 if (!entry)
2702 pr_warning("Could not create debugfs 'trace' entry\n");
2703
2704 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2705 &tracing_max_latency,
2706 &tracing_max_lat_fops);
2707 if (!entry)
2708 pr_warning("Could not create debugfs "
2709 "'tracing_max_latency' entry\n");
2710
2711 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2712 &tracing_thresh, &tracing_max_lat_fops);
2713 if (!entry)
2714 pr_warning("Could not create debugfs "
2715 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002716 entry = debugfs_create_file("README", 0644, d_tracer,
2717 NULL, &tracing_readme_fops);
2718 if (!entry)
2719 pr_warning("Could not create debugfs 'README' entry\n");
2720
Steven Rostedtb3806b42008-05-12 21:20:46 +02002721 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2722 NULL, &tracing_pipe_fops);
2723 if (!entry)
2724 pr_warning("Could not create debugfs "
2725 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002726
Steven Rostedta98a3c32008-05-12 21:20:59 +02002727 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2728 &global_trace, &tracing_entries_fops);
2729 if (!entry)
2730 pr_warning("Could not create debugfs "
2731 "'tracing_threash' entry\n");
2732
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002733#ifdef CONFIG_DYNAMIC_FTRACE
2734 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2735 &ftrace_update_tot_cnt,
2736 &tracing_read_long_fops);
2737 if (!entry)
2738 pr_warning("Could not create debugfs "
2739 "'dyn_ftrace_total_info' entry\n");
2740#endif
2741}
2742
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002743static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002744{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002745 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002746 struct page *page, *tmp;
2747 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002748 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002749 int i;
2750
2751 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002752 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002753 array = (void *)__get_free_page(GFP_KERNEL);
2754 if (array == NULL) {
2755 printk(KERN_ERR "tracer: failed to allocate page"
2756 "for trace buffer!\n");
2757 goto free_pages;
2758 }
2759
2760 page = virt_to_page(array);
2761 list_add(&page->lru, &pages);
2762
2763/* Only allocate if we are actually using the max trace */
2764#ifdef CONFIG_TRACER_MAX_TRACE
2765 array = (void *)__get_free_page(GFP_KERNEL);
2766 if (array == NULL) {
2767 printk(KERN_ERR "tracer: failed to allocate page"
2768 "for trace buffer!\n");
2769 goto free_pages;
2770 }
2771 page = virt_to_page(array);
2772 list_add(&page->lru, &pages);
2773#endif
2774 }
2775
2776 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002777 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002778 data = global_trace.data[i];
2779 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002780 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002781 list_add_tail(&page->lru, &data->trace_pages);
2782 ClearPageLRU(page);
2783
2784#ifdef CONFIG_TRACER_MAX_TRACE
2785 data = max_tr.data[i];
2786 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002787 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002788 list_add_tail(&page->lru, &data->trace_pages);
2789 SetPageLRU(page);
2790#endif
2791 }
2792 global_trace.entries += ENTRIES_PER_PAGE;
2793
2794 return 0;
2795
2796 free_pages:
2797 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002798 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002799 __free_page(page);
2800 }
2801 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002802}
2803
Steven Rostedta98a3c32008-05-12 21:20:59 +02002804static int trace_free_page(void)
2805{
2806 struct trace_array_cpu *data;
2807 struct page *page;
2808 struct list_head *p;
2809 int i;
2810 int ret = 0;
2811
2812 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002813 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002814 data = global_trace.data[i];
2815 p = data->trace_pages.next;
2816 if (p == &data->trace_pages) {
2817 /* should never happen */
2818 WARN_ON(1);
2819 tracing_disabled = 1;
2820 ret = -1;
2821 break;
2822 }
2823 page = list_entry(p, struct page, lru);
2824 ClearPageLRU(page);
2825 list_del(&page->lru);
2826 __free_page(page);
2827
2828 tracing_reset(data);
2829
2830#ifdef CONFIG_TRACER_MAX_TRACE
2831 data = max_tr.data[i];
2832 p = data->trace_pages.next;
2833 if (p == &data->trace_pages) {
2834 /* should never happen */
2835 WARN_ON(1);
2836 tracing_disabled = 1;
2837 ret = -1;
2838 break;
2839 }
2840 page = list_entry(p, struct page, lru);
2841 ClearPageLRU(page);
2842 list_del(&page->lru);
2843 __free_page(page);
2844
2845 tracing_reset(data);
2846#endif
2847 }
2848 global_trace.entries -= ENTRIES_PER_PAGE;
2849
2850 return ret;
2851}
2852
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002853__init static int tracer_alloc_buffers(void)
2854{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002855 struct trace_array_cpu *data;
2856 void *array;
2857 struct page *page;
2858 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002859 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002860 int i;
2861
Steven Rostedt26994ea2008-05-12 21:20:48 +02002862 global_trace.ctrl = tracer_enabled;
2863
Steven Rostedtab464282008-05-12 21:21:00 +02002864 /* TODO: make the number of buffers hot pluggable with CPUS */
2865 tracing_nr_buffers = num_possible_cpus();
2866 tracing_buffer_mask = cpu_possible_map;
2867
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002868 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002869 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002870 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002871 max_tr.data[i] = &per_cpu(max_data, i);
2872
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002873 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002874 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002875 printk(KERN_ERR "tracer: failed to allocate page"
2876 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002877 goto free_buffers;
2878 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002879
2880 /* set the array to the list */
2881 INIT_LIST_HEAD(&data->trace_pages);
2882 page = virt_to_page(array);
2883 list_add(&page->lru, &data->trace_pages);
2884 /* use the LRU flag to differentiate the two buffers */
2885 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002886
Steven Rostedta98a3c32008-05-12 21:20:59 +02002887 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2888 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2889
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002890/* Only allocate if we are actually using the max trace */
2891#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002892 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002893 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002894 printk(KERN_ERR "tracer: failed to allocate page"
2895 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002896 goto free_buffers;
2897 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002898
2899 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2900 page = virt_to_page(array);
2901 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2902 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002903#endif
2904 }
2905
2906 /*
2907 * Since we allocate by orders of pages, we may be able to
2908 * round up a bit.
2909 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002910 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002911 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002912
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002913 while (global_trace.entries < trace_nr_entries) {
2914 if (trace_alloc_page())
2915 break;
2916 pages++;
2917 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002918 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002919
2920 pr_info("tracer: %d pages allocated for %ld",
2921 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002922 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2923 pr_info(" actual entries %ld\n", global_trace.entries);
2924
2925 tracer_init_debugfs();
2926
2927 trace_init_cmdlines();
2928
2929 register_tracer(&no_tracer);
2930 current_trace = &no_tracer;
2931
Steven Rostedt60a11772008-05-12 21:20:44 +02002932 /* All seems OK, enable tracing */
2933 tracing_disabled = 0;
2934
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002935 return 0;
2936
2937 free_buffers:
2938 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002939 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002940 struct trace_array_cpu *data = global_trace.data[i];
2941
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002942 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002943 list_for_each_entry_safe(page, tmp,
2944 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002945 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002946 __free_page(page);
2947 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002948 }
2949
2950#ifdef CONFIG_TRACER_MAX_TRACE
2951 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002952 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002953 list_for_each_entry_safe(page, tmp,
2954 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002955 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002956 __free_page(page);
2957 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002958 }
2959#endif
2960 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002961 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002962}
Steven Rostedt60a11772008-05-12 21:20:44 +02002963fs_initcall(tracer_alloc_buffers);